CourseBuilder
Source
File: classes/CourseBuilder.php
class CourseBuilder { public function __construct() { //Frontend Course Save add_action( 'wp_ajax_front_save_course', [ $this, 'front_save_course' ] ); add_action( 'save_post_dozent_course', [ $this, '_save_course' ], 10, 2 ); add_action( 'dozent/save_course', [ $this, 'attach_current_instructor' ] ); add_action( 'dozent/course_metabox/saved', [ $this, 'save_course_access_method' ] ); add_action( 'dozent_course/course_tab_content/before/curriculum', [ $this, 'course_builder' ] ); add_action( 'wp_ajax_dozent_save_new_section', [ $this, 'save_new_section' ] ); add_action( 'wp_ajax_dozent_update_section', [ $this, 'update_section' ] ); add_action( 'wp_ajax_dozent_save_lecture', [ $this, 'dozent_save_lecture' ] ); add_action( 'wp_ajax_load_lecture_editor', [ $this, 'load_lecture_editor' ] ); add_action( 'wp_ajax_lecture_load_edit', [ $this, 'lecture_load_edit' ] ); add_action( 'wp_ajax_delete_course_content', [ $this, 'delete_course_content' ] ); add_action( 'wp_ajax_sorting_curriculum_items', [ $this, 'sorting_curriculum_items' ] ); add_action( 'wp_ajax_delete_section', [ $this, 'delete_section' ] ); //Sync Content with Course add_action( 'dozent/lecture/saved', [ $this, 'sync_content_with_course' ], 10, 3 ); add_action( 'dozent/quiz/saved', [ $this, 'sync_content_with_course' ], 10, 3 ); } public function front_save_course(){ $course_id = dozent_input_text( 'frontend_course_id' ); $course_title = dozent_input_text( 'course_title' ); $short_description = dozent_input_textarea( 'short_description' ); $course_description = ! empty( $_POST[ 'course_description' ] ) ? wp_kses_post( $_POST[ 'course_description' ] ) : ''; $course_thumbnail = (int) dozent_input_textarea( 'course_thumbnail' ); $save_type = dozent_input_text( 'save_type' ); $front_course = get_post( $course_id ); $tax_input = dozent_input_array_field( 'tax_input' ); $course_data = [ 'ID' => $course_id, 'post_title' => $course_title, 'post_name' => sanitize_title( $course_title ), 'post_content' => $course_description, 'post_excerpt' => $short_description, ]; /** * Course Publish, save as draft or submit for review. */ if ( $save_type === 'save' ){ $course_data['post_status'] = 'draft'; }else{ if ( current_user_can( 'instructor_can_publish_course' ) ){ $course_data['post_status'] = 'publish'; }else{ $course_data['post_status'] = 'pending'; } } wp_update_post( $course_data ); /** * Update or remove thumbnail */ if ( $course_thumbnail ){ update_post_meta( $course_id , '_thumbnail_id', $course_thumbnail); } else { delete_post_meta( $course_id , '_thumbnail_id' ); } /** * Adding course categories */ if ( dozent_count( $tax_input ) ) { foreach ( $tax_input as $taxonomy => $tags ) { $taxonomy_obj = get_taxonomy( $taxonomy ); if ( ! $taxonomy_obj ) { /* translators: %s: taxonomy name */ _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid taxonomy: %s.', 'dozent' ), $taxonomy ), '4.4.0' ); continue; } // array = hierarchical, string = non-hierarchical. if ( is_array( $tags ) ) { $tags = array_filter( $tags ); } wp_set_post_terms( $course_id, $tags, $taxonomy ); } } /** * Supports for others meta save */ $redirect_url = null; if ( $front_course->post_status === 'auto-draft' ){ $redirect_url = dozent_get_dashboard_permalink( 'my-courses/create-course' ); $redirect_url = add_query_arg( [ 'course_id' => $course_id ], $redirect_url ); } if ( $save_type === 'publish' ){ $redirect_url = dozent_get_dashboard_permalink( 'my-courses' ); } $response = [ 'redirect_url' => $redirect_url, ]; wp_send_json_success( $response ); } /** * Fire action hook when save the course * * * @since DozentLMS 1.0.0 * * * @param int $post_ID Course ID * @param object $post Course Object as post class instance */ public function _save_course( $post_ID, $post ) { do_action( 'dozent/save_course', $post_ID, $post ); do_action( 'dozent/course_metabox/saved', $post_ID ); } /** * Attach current instructor with course * * * @since DozentLMS 1.0.0 * * @see attach_current_instructor(); */ public function attach_current_instructor( $post_ID ){ dozent_instructor_attach_course( $post_ID, get_current_user_id() ); } /** * Save course acces method * * * @since DozentLMS 1.0.0 * * * @param $post_ID */ function save_course_access_method( $post_ID ) { $course_price_type = dozent_input_text( 'dozent_course_price_type' ); update_post_meta( $post_ID, '_course_price_type', $course_price_type ); } public function course_builder( $tab ) { include_once DOZENT_ABSPATH . 'views/course_builder/course_builder.php'; } public function frontend_course_builder(){ } public function save_new_section() { $course_id = (int) dozent_input_text( 'course_id' ); $section_name = dozent_input_text( 'section_name' ); $section_summery = dozent_input_textarea( 'section_summery' ); $section_order_id = dozent_get_next_curriculum_item_order_id( $course_id ); if ( ! $section_name ) { wp_send_json_error(); } $post_arr = apply_filters( 'dozent_new_section_attr', [ 'post_type' => 'dozent_section', 'post_title' => $section_name, 'post_content' => $section_summery, 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'post_parent' => $course_id, 'menu_order' => $section_order_id, ] ); $section_id = wp_insert_post( $post_arr ); ob_start(); include DOZENT_ABSPATH . 'views/course_builder/curriculum.php'; $course_contents = ob_get_clean(); wp_send_json_success( [ 'curriculum' => $course_contents ] ); } /** * Update Course Section * * @since DozentLMS 1.0.0 */ public function update_section() { $course_id = (int) dozent_input_text( 'course_id' ); $section_id = (int) dozent_input_text( 'section_id' ); $section_name = dozent_input_text( 'section_name' ); $section_summery = dozent_input_textarea( 'section_summery' ); if ( ! $section_name ) { wp_send_json_error(); } $post_arr = apply_filters( 'dozent_update_section_attr', [ 'ID' => $section_id, 'post_title' => $section_name, 'post_content' => $section_summery, ] ); do_action( 'dozent/section/update/before', $course_id, $post_arr ); wp_update_post( $post_arr ); do_action( 'dozent/section/update/after', $course_id, $post_arr ); ob_start(); include DOZENT_ABSPATH . 'views/course_builder/curriculum.php'; $course_contents = ob_get_clean(); wp_send_json_success( [ 'curriculum' => $course_contents ] ); } /** * Add Lecture Action * * @since DozentLMS 1.0.0 */ public function dozent_save_lecture() { $course_id = (int) dozent_input_text( 'course_id' ); $item_id = (int) dozent_input_text( 'item_id' ); $title = dozent_input_text( 'title' ); $lecture_content = dozent_input_array_field( 'lecture_content' ); $section_id = dozent_input_array_field( 'section_id' ); $preview = dozent_input_text( 'lecture_preview' ); $post_arr = [ 'post_type' => 'dozent_lecture', 'post_title' => $title, 'post_content' => $lecture_content, 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'post_parent' => $section_id, ]; if ( $item_id ) { $item = get_post( $item_id ); $post_arr['ID'] = $item_id; $post_arr['menu_order'] = $item->menu_order; } else { $menu_order_id = dozent_get_next_curriculum_item_order_id( $course_id ); $post_arr['menu_order'] = $menu_order_id; } $lecture_id = wp_insert_post( $post_arr ); if ( $lecture_id ) { update_post_meta( $lecture_id, '_dozent_course_id', $course_id ); //Save Video Info $video = dozent_input_array_field( 'video' ); update_post_meta( $lecture_id, '_video', $video ); /** * Adding Attachments if users select anyone. */ $attachments = dozent_input_array_field( 'attachments' ); if ( dozent_count( $attachments ) ) { update_post_meta( $lecture_id, '_attachments', $attachments ); } else { delete_post_meta( $lecture_id, '_attachments' ); } if ( $preview ) { update_post_meta( $lecture_id, '_lecture_preview', $preview ); } else { delete_post_meta( $lecture_id, '_lecture_preview' ); } //Register Action Hooks do_action( 'dozent/lecture/saved', $lecture_id, $course_id, $post_arr ); if ( $lecture_id ) { do_action( 'dozent/lecture/updated', $lecture_id, $course_id, $post_arr ); } else { do_action( 'dozent/lecture/created', $lecture_id, $course_id, $post_arr ); } } ob_start(); include DOZENT_ABSPATH . 'views/course_builder/curriculum.php'; $course_contents = ob_get_clean(); wp_send_json_success( [ 'lecture_id' => $lecture_id, 'curriculum' => $course_contents ] ); } /** * Append lecture editor via wp_editor * * @since DozentLMS 1.0.0 */ public function load_lecture_editor() { ob_start(); wp_editor( '', 'lecture_content', [ 'textarea_rows' => 4 ] ); $editor_html = ob_get_clean(); wp_send_json_success( [ 'editor_html' => $editor_html ] ); } public function lecture_load_edit() { $item_id = (int) dozent_input_text( 'item_id' ); $item = get_post( $item_id ); ob_start(); include DOZENT_ABSPATH . 'views/course_builder/edit_lecture.php'; $html = ob_get_clean(); wp_send_json_success( [ 'html' => $html ] ); } /** * Delete course content * * @since DozentLMS 1.0.0 */ public function delete_course_content() { global $wpdb; $post_id = (int) dozent_input_text( 'item_id' ); do_action( 'dozent/delete_course_content/before', $post_id ); wp_delete_post( $post_id, true ); $wpdb->delete( $wpdb->postmeta, [ 'post_id' => $post_id ] ); do_action( 'dozent/delete_course_content/after', $post_id ); } /** * Sorting curriculum Items * * @since DozentLMS 1.0.0 */ public function sorting_curriculum_items() { global $wpdb; $sections = dozent_input_array_field( 'sections' ); if ( dozent_count( $sections ) ) { $item_i = 1; foreach ( $sections as $skey => $section ) { $section_id = dozent_array_get( 'section_id', $section ); //Sorting Section $wpdb->update( $wpdb->posts, [ 'menu_order' => $skey ], [ 'ID' => $section_id ] ); $item_ids = dozent_array_get( 'item_ids', $section ); if ( dozent_count( $item_ids ) ) { foreach ( $item_ids as $ikey => $item_id ) { $wpdb->update( $wpdb->posts, [ 'post_parent' => $section_id, 'menu_order' => $item_i ], [ 'ID' => $item_id ] ); $item_i ++; } } } } } /** * Delete Section from the course curriculum * * @since DozentLMS 1.0.0 */ public function delete_section() { $post_id = (int) dozent_input_text( 'section_id' ); $this->_delete_section( $post_id ); } /** * @param $section_id * * Delete Section and all it's related, lecture, quiz, assignments, etc. */ public function _delete_section( $section_id ) { global $wpdb; do_action( 'dozent/delete_section/before', $section_id ); $lecture_ids = (array) $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = {$section_id} " ); wp_delete_post( $section_id, true ); /** * Delete all data related this section. */ $wpdb->delete( $wpdb->posts, [ 'post_parent' => $section_id ] ); if ( dozent_count( $lecture_ids ) ) { $lecture_ids = implode( ',', array_map( 'absint', $lecture_ids ) ); $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN({$lecture_ids})" ); } do_action( 'dozent/delete_section/after', $section_id ); } public function sync_content_with_course( $lecture_id, $course_id, $post_arr ) { sync_course_with_contents( $course_id ); } }
Methods
- __construct
- _delete_section
- _save_course — Fire action hook when save the course
- attach_current_instructor — Attach current instructor with course
- course_builder
- delete_course_content — Delete course content
- delete_section — Delete Section from the course curriculum
- dozent_save_lecture — Add Lecture Action
- front_save_course
- frontend_course_builder
- lecture_load_edit
- load_lecture_editor — Append lecture editor via wp_editor
- save_course_access_method — Save course acces method
- save_new_section
- sorting_curriculum_items — Sorting curriculum Items
- sync_content_with_course
- update_section — Update Course Section