MetaBox
Source
File: classes/MetaBox.php
class MetaBox { protected $fields; public function __construct() { add_action( 'add_meta_boxes', [ $this, 'register_meta_box' ] ); /** * Frontend Course Create Support */ add_action( 'dozent/frontend/create-course', [ $this, 'frontend_course_builder' ] ); add_action( 'wp_ajax_dozent_save_course_metabox', [ $this, 'dozent_save_course_metabox' ] ); add_action( 'dozent_course/course_tab_content/before/general', [ $this, 'course_access_method' ] ); add_action( 'dozent_course/course_tab_content/after/general', [ $this, 'add_difficulty_level' ] ); add_action( 'dozent/course_metabox/saved', [ $this, 'save_difficulty_level' ] ); add_action( 'dozent/course_metabox/saved', [ $this, 'save_information' ] ); //Course Builder Frontend Support add_filter( 'dozent_course_settings_tabs', [ $this, 'course_builder_frontend_support' ] ); } public function register_meta_box() { add_meta_box( 'dozent-course', __( 'Course', 'dozent' ), [ $this, 'admin_course_builder' ], 'dozent_course', 'advanced', 'high' ); } public function get_default_fields() { $sections = []; $sections['general'] = [ 'title' => __( 'General', 'dozent' ), //'desc' => __( 'General Settings', 'dozent' ), 'icon_class' => ' dicon-cog', 'callback' => '', 'fields' => [ 'maximum_students' => [ 'type' => 'number', 'title' => __( 'Maximum Students', 'dozent' ), 'label_title' => __( 'Enable', 'dozent' ), 'default' => '0', 'desc' => __( 'Number of maximum students can enroll in this course, set zero for no limits', 'dozent' ), ], //course_levels ], ]; $sections['curriculum'] = [ 'title' => __( 'Curriculum', 'dozent' ), //'desc' => __( 'Build your course with Dozent course builder.', 'dozent' ), 'icon_class' => ' dicon-book', ]; $sections['intro_video'] = [ 'title' => __( 'Intro Video', 'dozent' ), 'desc' => __( 'A quality introduction video shows that you are prepared and provides a good first impression for your students.', 'dozent' ), 'icon_class' => ' dicon-play2', 'callback' => [ $this, 'intro_video' ], ]; $sections['information'] = [ 'title' => __( 'Information', 'dozent' ), 'desc' => __( 'Course general information like course duration, benefits of the course, requirements, target audience.', 'dozent' ), 'icon_class' => ' dicon-check', 'callback' => [ $this, 'course_information' ], ]; $sections['attach_products'] = [ 'title' => __( 'Attach Product', 'dozent' ), 'desc' => __( 'Attach WooCommerce/EDD product to sell the course.', 'dozent' ), 'icon_class' => ' dicon-product', ]; /* * TODO: need to move it pro version $sections['prerequisites'] = [ 'title' => __( 'Prerequisites', 'dozent' ), 'desc' => __( 'Student have to must complete give courses before enrol this course.', 'dozent' ), 'icon_class' => ' dicon-clipboard', ]; */ $sections['instructors'] = [ 'title' => __( 'Instructors', 'dozent' ), 'desc' => __( 'Take courses by multiple instructors.', 'dozent' ), 'icon_class' => ' dicon-instructor', 'callback' => [ $this, 'course_instructors' ], ]; return apply_filters( 'dozent_course_settings_tabs', $sections ); } public function course_builder_frontend_support( $sections ){ global $dozent_course_creation_page; if ( $dozent_course_creation_page !== 'frontend' ){ return $sections; } $course_front_field = []; $course_front_field[ 'essential' ] = [ 'title' => __( 'Essential', 'dozent' ), 'icon_class' => ' dicon-graduation-cap', 'callback' => [ $this, 'course_essential' ], ]; $sections = array_merge( $course_front_field, $sections ); return $sections; } public function admin_course_builder(){ global $dozent_course_creation_page; $dozent_course_creation_page = 'admin'; $this->course_builder(); } public function frontend_course_builder(){ global $post, $dozent_course_creation_page; $dozent_course_creation_page = 'frontend'; wp_reset_query(); $course_id = (int) dozent_input_text( 'course_id' ); if ( $course_id ){ $post_id = $course_id; }else{ $post_id = wp_insert_post( [ 'post_title' => __( 'Auto Draft', 'dozent' ), 'post_type' => 'dozent_course', 'post_status' => 'auto-draft' ] ); } $post = get_post( $post_id ); setup_postdata( $post ); dozent_load_template( 'dashboard/my-courses/course-builder-header' ); echo "<form action='' method='post' id='dozent-front-coursebuilder-form'>"; dozent_nonce_field(); do_action( 'dozent_front_coursebuilder_form_start' ); echo "<input type='hidden' name='frontend_course_id' value='{$post_id}' />"; $this->course_builder(); do_action( 'dozent_front_coursebuilder_form_end' ); echo "</form>"; wp_reset_query(); } /** * Course Builder * * This method containing main logic and view files to create course * * * @since DozentLMS 1.0.0 * * @see course_builder(); */ public function course_builder() { global $post; $this->fields = $this->get_default_fields(); $settings_meta = get_post_meta( get_the_ID(), '_dozent_course_settings', true ); $this->settings_meta = (array) maybe_unserialize( $settings_meta ); if ( dozent_count( $this->fields ) && $post->post_type === 'dozent_course' ) { include DOZENT_ABSPATH . "views/metabox/course_metabox.php"; } } public function generate_field( $fields = array() ) { if ( dozent_count( $fields ) ) { foreach ( $fields as $field_key => $field ) { $type = dozent_array_get( 'type', $field ); ?> <div class="dozent-option-field-wrap dozent-field-row-<?php echo $type; ?>"> <?php if ( isset( $field['title'] ) ) { ?> <div class="dozent-option-field-title"> <label for=""> <strong><?php echo $field['title']; ?></strong> </label> </div> <?php } ?> <div class="dozent-option-field flex-grow-1 dozent-field-<?php echo $type; ?>"> <?php $field['field_key'] = $field_key; $this->field_type( $field ); if ( isset( $field['desc'] ) ) { echo "<p class='desc dozent-text-muted'>{$field['desc']}</p>"; } ?> </div> </div> <?php } } } public function field_type( $field = array() ) { include DOZENT_ABSPATH . "views/metabox/field-types/{$field['type']}.php"; } public function get( $key = null, $default = false ) { return dozent_array_get( $key, $this->settings_meta, $default ); } /** * @param $post_ID * @param $post * * Fire when course saving... */ /** * @param $post_ID * * Save course metabox via save_course action hook */ public function save_course( $post_ID ) { do_action( 'dozent/course/save/before', $post_ID ); $this->save_course_metabox( $post_ID ); do_action( 'dozent/course/save/after', $post_ID ); } /** * Save Course metabox via Ajax */ public function dozent_save_course_metabox() { $post_ID = dozent_input_text( 'course_id' ); do_action( 'dozent/course/save/before', $post_ID ); $this->save_course_metabox( $post_ID ); do_action( 'dozent/course/save/after', $post_ID ); wp_send_json_success(); } /** * @param $post_ID * * Save Course Meta Box */ public function save_course_metabox( $post_ID ) { do_action( 'dozent/course_metabox/before_save', $post_ID ); $_dozent_course_settings = dozent_input_array_field( '_dozent_course_settings' ); if ( dozent_count( $_dozent_course_settings ) ) { update_post_meta( $post_ID, '_dozent_course_settings', $_dozent_course_settings ); } else { delete_post_meta( $post_ID, '_dozent_course_settings' ); } /** * Intro Video */ $video = dozent_input_array_field( 'video' ); update_post_meta( $post_ID, '_video', $video ); do_action( 'dozent/course_metabox/saved', $post_ID ); } public function course_access_method() { include_once DOZENT_ABSPATH . '/views/metabox/part/course_access_method.php'; } public function course_price_type_closed() { echo '<h1>Closed Price Type</h1>'; } /** * Add difficulty Level metabox at the course settings * * long description * * @since DozentLMS 1.0.0 * */ public function add_difficulty_level() { include_once DOZENT_ABSPATH . '/views/metabox/part/difficulty_level_input.php'; } public function save_difficulty_level( $post_ID ) { $difficulty_level = dozent_input_text( '_difficulty_level' ); if ( $difficulty_level ) { update_post_meta( $post_ID, '_difficulty_level', $difficulty_level ); } else { delete_post_meta( $post_ID, '_difficulty_level' ); } } /** * Course Essential Fields * * * @since DozentLMS 1.0.0 * * @see course_essential(); */ public function course_essential(){ include_once DOZENT_ABSPATH . '/views/course_builder/essential.php'; } /** * Course Intro Video Fields * * @since DozentLMS 1.0.0 * * @see intro_video(); */ public function intro_video(){ include_once DOZENT_ABSPATH . '/views/course_builder/intro_video.php'; } public function course_information() { include_once DOZENT_ABSPATH . '/views/metabox/part/information.php'; } /** * Save course information meta * * Example usage: * * save_information(); * * @since DozentLMS 1.0.0 * * @param $post_ID */ public function save_information( $post_ID ) { $_benefits = dozent_input_textarea( '_benefits' ); $_requirements = dozent_input_textarea( '_requirements' ); $_target_audience = dozent_input_textarea( '_target_audience' ); dozent_update_post_meta( $post_ID, '_benefits', $_benefits ); dozent_update_post_meta( $post_ID, '_requirements', $_requirements ); dozent_update_post_meta( $post_ID, '_target_audience', $_target_audience ); } public function course_instructors() { include_once DOZENT_ABSPATH . '/views/metabox/part/instructors.php'; } }
Methods
- __construct
- add_difficulty_level — Add difficulty Level metabox at the course settings
- admin_course_builder
- course_access_method
- course_builder — Course Builder
- course_builder_frontend_support
- course_essential — Course Essential Fields
- course_information
- course_instructors
- course_price_type_closed
- dozent_save_course_metabox — Save Course metabox via Ajax
- field_type
- frontend_course_builder
- generate_field
- get
- get_default_fields
- intro_video — Course Intro Video Fields
- register_meta_box
- save_course
- save_course_metabox
- save_difficulty_level
- save_information — Save course information meta