MetaBox


Source

File: classes/MetaBox.php

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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