PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / admin / lp-admin-functions.php

lp-admin-functions.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.6.9, at inc/admin/lp-admin-functions.php

2,597 lines 65.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Common functions used for admin
4 *
5 * @package LearnPress
6 * @author ThimPress
7 * @version 1.0
8 */
9
10 /**
11 * Prevent loading this file directly
12 */
13 defined( 'ABSPATH' ) || exit();
14
15 if ( ! function_exists( 'learn_press_add_row_action_link' ) ) {
16 /**
17 * Setup action links to the admin course, lesson, quiz, question. e.g: Add Duplicate link, hide View link for lesson, quiz so on.
18 *
19 * @param $actions
20 *
21 * @return mixed
22 */
23 function learn_press_add_row_action_link( $actions ) {
24
25 global $post;
26
27 if ( LP_COURSE_CPT == $post->post_type ) {
28 $duplicate_link = '#';
29 $duplicate_link = array(
30 array(
31 'link' => $duplicate_link,
32 'title' => _x( 'Duplicate', 'copy course', 'learnpress' ),
33 'class' => 'lp-duplicate-post lp-duplicate-course',
34 'data' => $post->ID,
35 ),
36 );
37
38 $links = apply_filters( 'learn_press_row_action_links', $duplicate_link );
39
40 if ( count( $links ) > 1 ) {
41 $drop_down = array( '<ul class="lpr-row-action-dropdown">' );
42
43 foreach ( $links as $link ) {
44 $drop_down[] = '<li>' . sprintf(
45 '<a href="%s" class="%s" data-post-id="%s">%s</a>',
46 $link['link'],
47 $link['class'],
48 $link['data'],
49 $link['title']
50 ) . '</li>';
51 };
52
53 $drop_down[] = '</ul>';
54 $link = sprintf(
55 '<div class="lpr-row-actions"><a href="%s">%s</a>%s</div>',
56 'javascript: void(0);',
57 __( 'Course', 'learnpress' ),
58 join( "\n", $drop_down )
59 );
60
61 } else {
62 $link = array_shift( $links );
63 $link = sprintf(
64 '<a href="%s" class="%s" data-post-id="%s">%s</a>',
65 $link['link'],
66 $link['class'],
67 $link['data'],
68 $link['title']
69 );
70 }
71
72 $actions['lp-duplicate-row-action'] = $link;
73
74 } elseif ( LP_QUIZ_CPT === $post->post_type ) {
75 unset( $actions['view'] );
76 $link = sprintf(
77 '<a href="#" class="lp-duplicate-post lp-duplicate-quiz" data-post-id="%s">%s</a>',
78 $post->ID,
79 _x( 'Duplicate', 'copy quiz', 'learnpress' )
80 );
81 $actions['lp-duplicate-row-action'] = $link;
82
83 } elseif ( LP_QUESTION_CPT === $post->post_type ) {
84 unset( $actions['view'] );
85 $link = sprintf(
86 '<a href="#" class="lp-duplicate-post lp-duplicate-question" data-post-id="%s">%s</a>',
87 $post->ID,
88 _x( 'Duplicate', 'copy question', 'learnpress' )
89 );
90 $actions['lp-duplicate-row-action'] = $link;
91
92 } elseif ( LP_LESSON_CPT === $post->post_type ) {
93 unset( $actions['view'] );
94 $link = sprintf(
95 '<a href="#" class="lp-duplicate-post lp-duplicate-lesson" data-post-id="%s">%s</a>',
96 $post->ID,
97 _x( 'Duplicate', 'copy lesson', 'learnpress' )
98 );
99 $actions['lp-duplicate-row-action'] = $link;
100 }
101
102 return apply_filters( 'learn-press/row-action-links', $actions );
103 }
104
105 add_filter( 'post_row_actions', 'learn_press_add_row_action_link' );
106 add_filter( 'page_row_actions', 'learn_press_add_row_action_link' );
107 }
108
109 if ( ! function_exists( 'learn_press_settings_tabs_array' ) ) {
110 /**
111 * Default admin settings pages
112 *
113 * @return mixed
114 * @depecated 4.1.6.4
115 */
116 /*function learn_press_settings_tabs_array() {
117 $tabs = array(
118 'general' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-general.php',
119 'courses' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-courses.php',
120 'profile' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-profile.php',
121 'payments' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-payments.php',
122 'emails' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-emails.php',
123 'advanced' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-advanced.php',
124 );
125
126 return apply_filters( 'learn-press/admin/settings-tabs-array', $tabs );
127 }*/
128 }
129
130 function learn_press_is_hidden_post_box( $id, $user_id = 0 ) {
131 if ( ! $user_id ) {
132 $user_id = get_current_user_id();
133 }
134
135 $data = learn_press_get_user_option( 'post-closed-box' );
136 if ( ! $data ) {
137 $data = array();
138 }
139
140 return false !== array_search( $id, $data );
141 }
142
143 /**
144 * Get html view path for admin to display
145 *
146 * @param $name
147 * @param $plugin_file
148 *
149 * @return mixed
150 */
151 function learn_press_get_admin_view( $name, $plugin_file = null ) {
152 if ( ! preg_match( '/\.(html|php)$/', $name ) ) {
153 $name .= '.php';
154 }
155 if ( $plugin_file ) {
156 $view = dirname( $plugin_file ) . '/inc/admin/views/' . $name;
157 } else {
158 $view = LP()->plugin_path( 'inc/admin/views/' . $name );
159 }
160
161 return apply_filters( 'learn_press_admin_view', $view, $name );
162 }
163
164 function learn_press_admin_view_content( $name, $args = array() ) {
165 return learn_press_admin_view( $name, $args, false, true );
166 }
167
168 /**
169 * Find a full path of a view and display the content in admin
170 *
171 * @param $name
172 * @param array $args
173 * @param bool|false $include_once
174 * @param bool
175 *
176 * @return bool
177 */
178 function learn_press_admin_view( $name, $args = array(), $include_once = false, $return = false ) {
179 $view = learn_press_get_admin_view( $name, ! empty( $args['plugin_file'] ) ? $args['plugin_file'] : null );
180
181 if ( file_exists( $view ) ) {
182
183 ob_start();
184
185 is_array( $args ) && extract( $args );
186
187 do_action( 'learn_press_before_display_admin_view', $name, $args );
188
189 if ( $include_once ) {
190 include_once $view;
191 } else {
192 include $view;
193 }
194
195 do_action( 'learn_press_after_display_admin_view', $name, $args );
196 $output = ob_get_clean();
197
198 if ( ! $return ) {
199 learn_press_echo_vuejs_write_on_php( $output );
200 }
201
202 return $return ? $output : true;
203 }
204
205 return false;
206 }
207
208 /**
209 * Vue write php has script, so when sanitize will error, so use code is mask pass check sanitize, esc
210 *
211 * @param $content
212 *
213 * @return void
214 * @since 4.1.6.9
215 */
216 function learn_press_echo_vuejs_write_on_php( $content ) {
217 echo ( $content );
218 }
219
220 /**
221 * List all pages as a dropdown with "Add New Page" option
222 *
223 * @param $name
224 * @param bool|false $selected
225 * @param array $args
226 *
227 * @return mixed|string
228 */
229 function learn_press_pages_dropdown( $name, $selected = false, $args = array() ) {
230 $id = null;
231 $class = null;
232 $css = null;
233 $before = array(
234 'add_new_page' => __( '[ Add a new page ]', 'learnpress' ),
235 );
236 $after = null;
237 $echo = true;
238 $allow_create = true;
239
240 if ( func_num_args() == 1 && is_array( $name ) ) {
241 $args = $name;
242 }
243
244 is_array( $args ) && extract( $args );
245
246 if ( empty( $id ) ) {
247 $id = $name;
248 }
249
250 $class .= 'list-pages lp-list-pages learn-press-select2';
251
252 $args = array(
253 'name' => $name,
254 'id' => $id,
255 'sort_column' => 'menu_order',
256 'sort_order' => 'ASC',
257 'show_option_none' => __( 'Select Page', 'learnpress' ),
258 'class' => $class,
259 'echo' => false,
260 'selected' => $selected,
261 'allow_create' => true,
262 );
263 $output = wp_dropdown_pages( $args );
264 $replace = '';
265
266 if ( $class ) {
267 $replace .= ' class="' . $class . '"';
268 }
269
270 if ( $css ) {
271 $replace .= ' style="' . $css . '"';
272 }
273
274 $replace .= ' data-selected="' . $selected . '"';
275 $replace .= " data-placeholder='" . __( 'Select a page&hellip;', 'learnpress' ) . "' id=";
276 $output = '<div class="list-pages-wrapper">' . str_replace( ' id=', $replace, $output );
277
278 if ( $before ) {
279 $before_output = array();
280
281 foreach ( $before as $v => $l ) {
282 $before_output[] = sprintf( '<option value="%s">%s</option>', $v, $l );
283 }
284
285 $before_output = join( "\n", $before_output );
286 $output = preg_replace(
287 '!(<option class=".*" value="[0-9]+".*>.*</option>)!',
288 $before_output . "\n$1",
289 $output,
290 1
291 );
292 }
293
294 $output = str_replace( '<option class="level-0" value="00000">#0 (no title)</option>', '', $output );
295
296 if ( $selected && get_post_status( $selected ) !== 'publish' ) {
297 $selected = 0;
298 }
299
300 if ( $allow_create ) {
301 ob_start(); ?>
302
303 <?php echo esc_html( _x( 'or', 'drop down pages', 'learnpress' ) ); ?>
304
305 <button class="button button-quick-add-page" data-id="<?php echo esc_attr( $id ); ?>" type="button">
306 <?php esc_html_e( 'Create new', 'learnpress' ); ?>
307 </button>
308 <?php echo '</div>'; ?>
309
310 <p class="quick-add-page-inline <?php echo esc_attr( $id ); ?> hide-if-js">
311 <input type="text" placeholder="<?php esc_attr_e( 'New page title', 'learnpress' ); ?>"/>
312 <button class="button" type="button">
313 <?php esc_html_e( 'Ok [Enter]', 'learnpress' ); ?>
314 </button>
315 <a href=""><?php esc_html_e( 'Cancel [ESC]', 'learnpress' ); ?></a>
316 </p>
317
318 <p class="quick-add-page-actions <?php echo esc_attr( $id ); ?><?php echo esc_attr( $selected ? '' : ' hide-if-js' ); ?>">
319 <a class="edit-page" href="<?php echo get_edit_post_link( $selected ); ?>"
320 target="_blank"><?php esc_html_e( 'Edit page', 'learnpress' ); ?></a>
321 &#124;
322 <a class="view-page" href="<?php echo get_permalink( $selected ); ?>"
323 target="_blank"><?php esc_html_e( 'View page', 'learnpress' ); ?></a>
324 </p>
325
326 <?php
327 $output .= ob_get_clean();
328 } else {
329 $output .= '</div>';
330 }
331
332 $output = sprintf( '<div class="learn-press-dropdown-pages">%s</div>', $output );
333
334 if ( $echo ) {
335 $allowed_html = wp_kses_allowed_html( 'post' );
336 $allowed_html['select'] = [
337 'name' => [],
338 ];
339 $allowed_html['option'] = [
340 'value' => [],
341 'selected' => [],
342 'class' => [],
343 ];
344 $allowed_html['input'] = [
345 'type' => [],
346 'placeholder' => [],
347 ];
348
349 echo wp_kses( $output, $allowed_html );
350 }
351
352 return $output;
353 }
354
355 /**
356 * List all registered question types into dropdown
357 *
358 * @param array
359 *
360 * @return string
361 */
362 function learn_press_dropdown_question_types( $args = array() ) {
363 $args = wp_parse_args(
364 $args,
365 array(
366 'name' => 'learn-press-dropdown-question-types',
367 'id' => '',
368 'class' => '',
369 'selected' => '',
370 'echo' => true,
371 )
372 );
373
374 if ( ! $args['id'] ) {
375 $args['id'] = $args['name'];
376 }
377
378 $args['class'] = 'lp-dropdown-question-types' . ( $args['class'] ? ' ' . $args['class'] : '' );
379 $types = learn_press_question_types();
380 $output = sprintf(
381 '<select name="%s" id="%s" class="%s"%s>',
382 $args['name'],
383 $args['id'],
384 $args['class'],
385 $args['selected'] ? 'data-selected="' . $args['selected'] . '"' : ''
386 );
387
388 foreach ( $types as $slug => $name ) {
389 $output .= sprintf(
390 '<option value="%s"%s>%s</option>',
391 $slug,
392 selected( $slug == $args['selected'], true, false ),
393 $name
394 );
395 }
396
397 $output .= '</select>';
398
399 if ( $args['echo'] ) {
400 echo wp_kses_post( $output );
401 }
402
403 return $output;
404 }
405
406 /**
407 * List all registered question types into dropdown
408 *
409 * @param array $args
410 * @param LP_Question $question
411 *
412 * @return string
413 */
414 function learn_press_field_question_duration( $args = array(), $question = null ) {
415 global $post;
416
417 $duration_type = get_post_meta( $post->ID, '_lp_duration_type', true );
418 $value = get_post_meta( $question->id, '_question_duration', true );
419
420 $wrap_class = 'learn-press-question-duration';
421
422 if ( 'questions_duration' !== $duration_type ) {
423 $wrap_class .= ' hide';
424 }
425
426 $args = wp_parse_args(
427 $args,
428 array(
429 'name' => 'learn_press_question[' . $question->id . '][duration]',
430 'id' => 'learn-press-question-duration-' . $question->id,
431 'class' => 'learn-press-question-duration',
432 'selected' => '',
433 'echo' => true,
434 'value' => 0,
435 'step' => 1,
436 'min' => 0,
437 'placeholder' => __( 'Minutes', 'learnpress' ),
438 )
439 );
440
441 $args['value'] = $value;
442
443 if ( ! $args['id'] ) {
444 $args['id'] = $args['name'];
445 }
446
447 return '<span class="' . esc_attr( $wrap_class ) . '">' . sprintf(
448 '<input type="number" class="%s" name="%s" id="%s" value="%s" step="%s" min="%s" max="%s" placeholder="%s"/>',
449 $args['class'],
450 $args['name'],
451 empty( $args['clone'] ) ? $args['id'] : '',
452 $args['value'],
453 $args['step'],
454 $args['min'],
455 ! empty( $args['max'] ) ? $args['max'] : '',
456 $args['placeholder']
457 ) . $args['placeholder'] . '</span>';
458 }
459
460 /**
461 * Displays email formats support into a dropdown
462 *
463 * @param array $args
464 *
465 * @return string
466 */
467 function learn_press_email_formats_dropdown( $args = array() ) {
468 $args = wp_parse_args(
469 $args,
470 array(
471 'name' => 'learn-press-dropdown-email-formats',
472 'id' => '',
473 'class' => '',
474 'selected' => '',
475 'option_none' => '',
476 'echo' => true,
477 )
478 );
479
480 $formats = array(
481 'plain_text' => __( 'Plain Text', 'learnpress' ),
482 'html' => __( 'HTML', 'learnpress' ),
483 );
484
485 if ( empty( $args['id'] ) ) {
486 $args['id'] = sanitize_file_name( $args['name'] );
487 }
488
489 $output = sprintf( '<select name="%s" id="%s" class="%s" %s>', $args['name'], $args['id'], $args['class'], '' );
490
491 if ( $args['option_none'] ) {
492 if ( is_array( $args['option_none'] ) ) {
493 $text = reset( $args['option_none'] );
494 $value = key( $args['option_none'] );
495 } else {
496 $text = $args['option_none'];
497 $value = '';
498 }
499
500 $output .= sprintf( '<option value="%s">%s</option>', $value, $text );
501 }
502
503 foreach ( $formats as $name => $text ) {
504 $output .= sprintf(
505 '<option value="%s" %s>%s</option>',
506 $name,
507 selected( $args['selected'] == $name, true, false ),
508 $text
509 ) . "\n";
510 }
511 $output .= '</select>';
512
513 if ( $args['echo'] ) {
514 echo wp_kses_post( $output );
515 }
516
517 return $output;
518 }
519
520 /**
521 * Return array of email formats.
522 *
523 * @return mixed
524 */
525 function learn_press_email_formats() {
526 $formats = array(
527 'plain' => esc_html__( 'Plain Text', 'learnpress' ),
528 'html' => esc_html__( 'HTML', 'learnpress' ),
529 );
530
531 return apply_filters( 'learn-press/email-formats', $formats );
532 }
533
534 function learn_press_trim_content( $content, $count = 0 ) {
535 $content = preg_replace( '/(?<=\S,)(?=\S)/', ' ', $content );
536 $content = str_replace( "\n", ' ', $content );
537 $content = explode( ' ', $content );
538
539 $count = $count > 0 ? $count : sizeof( $content ) - 1;
540 $full = $count >= sizeof( $content ) - 1;
541
542 $content = array_slice( $content, 0, $count );
543 $content = implode( ' ', $content );
544
545 if ( ! $full ) {
546 $content .= '...';
547 }
548
549 return $content;
550 }
551
552 /**
553 * Get list of themes that support LearnPress.
554 *
555 * @return mixed
556 */
557 function learn_press_get_education_themes() {
558
559 // New theme can be added here
560 return apply_filters(
561 'learn-press/education-themes',
562 array(
563 '23451388' => 'kindergarten',
564 '22773871' => 'ivy-school',
565 '20370918' => 'wordpress-lms',
566 '14058034' => 'eduma',
567 '17097658' => 'coach',
568 '11797847' => 'lms',
569 )
570 );
571 }
572
573 if ( ! function_exists( 'learn_press_get_item_referral' ) ) {
574 /**
575 * Set item link referral.
576 *
577 * @param int|string $item_id
578 *
579 * @return string
580 */
581 function learn_press_get_item_referral( $item_id ) {
582 $affiliate_links = array(
583 14058034 => 'https://1.envato.market/G5Ook', // Eduma
584 22773871 => 'https://1.envato.market/akrzZ', // Ivy-school
585 20370918 => 'https://1.envato.market/13Zkd', // Course Builder LMS
586 17097658 => 'https://1.envato.market/Xq2Ra', // Coach
587 23451388 => 'https://1.envato.market/oWov9', // StarKid
588 11797847 => 'https://1.envato.market/zknvM', // Epsilon
589 13321455 => 'https://1.envato.market/G5Rkk', // Sailing
590 19029758 => 'https://1.envato.market/mAYdZ', // Travel Tour Booking
591 12124219 => 'https://1.envato.market/qJYdO', // Resca
592 18828322 => 'https://1.envato.market/VW2K3', // LuxStay
593 8254575 => 'https://1.envato.market/xWYdO', // Squareroot
594 13782850 => 'https://1.envato.market/Qo71z',
595 14025178 => 'https://1.envato.market/9R0oQ',
596 17739078 => 'https://1.envato.market/jkYda',
597 16210005 => 'https://1.envato.market/56oJD',
598 11733602 => 'https://1.envato.market/keYdv',
599 13513609 => 'https://1.envato.market/aq7dQ',
600 19693761 => 'https://1.envato.market/A976D',
601 12532973 => 'https://1.envato.market/Ge79B',
602 19305239 => 'https://1.envato.market/10JAB',
603 23716060 => 'https://1.envato.market/ZO7WW',
604 20466233 => 'https://1.envato.market/gjYd0',
605 21070438 => 'https://1.envato.market/xPz65',
606 20794183 => 'https://1.envato.market/o1YdY',
607 20979215 => 'https://1.envato.market/03R5V',
608 11151269 => 'https://1.envato.market/Br7L4',
609 8905392 => 'https://1.envato.market/zEYd7',
610 23168294 => 'https://1.envato.market/Wn7on',
611 17719422 => 'https://1.envato.market/0WVaL',
612 21680592 => 'https://1.envato.market/qqO6y',
613 );
614
615 return isset( $affiliate_links[ $item_id ] ) ? $affiliate_links[ $item_id ] : 'https://themeforest.net/user/thimpress/portfolio/';
616 }
617 }
618
619 /**
620 * Display advertisement about related themes at the bottom of admin pages.
621 *
622 * @updated 12 Nov 2018 - Enable/Disable shuffle the list of themes
623 *
624 * @return bool|void
625 */
626 function learn_press_footer_advertisement() {
627 $admin_post_type = array(
628 'lp_course',
629 'lp_lesson',
630 'lp_quiz',
631 'lp_question',
632 'lp_order',
633 );
634
635 // And our admin pages
636 $pages = array(
637 'learnpress_page_learn-press-statistics',
638 'learnpress_page_learn-press-settings',
639 'learnpress_page_learn-press-tools',
640
641 );
642
643 $screen = get_current_screen();
644
645 if ( ! $screen ) {
646 return;
647 }
648
649 if ( ! ( ( in_array(
650 $screen->post_type,
651 $admin_post_type
652 ) && $screen->base === 'edit' ) || ( in_array( $screen->id, $pages ) ) ) ) {
653 return;
654 }
655
656 $theme_ids = learn_press_get_education_themes();
657 $current_theme = wp_get_theme();
658
659 $include = array_keys( $theme_ids );
660
661 $key = array_search( $current_theme->name, $theme_ids, true );
662
663 if ( false !== $key ) {
664 unset( $theme_ids[ $key ] );
665 }
666
667 $list_themes = (array) LP_Plugins_Helper::get_related_themes(
668 'education',
669 array(
670 'include' => $include,
671 )
672 );
673
674 if ( empty( $list_themes ) ) {
675 return;
676 }
677
678 // Disable shuffle themes for 3 days
679 $shuffle = LP_Settings::instance()->get( 'ad_shuffle_themes' );
680
681 if ( ! $shuffle ) {
682 if ( wp_next_scheduled( 'learn-press/schedule-enable-shuffle-themes' ) === false ) {
683 wp_schedule_single_event( time() + 3 * DAY_IN_SECONDS, 'learn-press/schedule-enable-shuffle-themes' );
684 }
685
686 // Keep the first theme always in #1 and shuffle other themes
687 $first_theme = array_shift( $list_themes );
688 shuffle( $list_themes );
689 array_unshift( $list_themes, $first_theme );
690 } else {
691 shuffle( $list_themes );
692 }
693 ?>
694
695 <div id="learn-press-advertisement" class="learn-press-advertisement-slider">
696 <?php
697 foreach ( $list_themes as $theme ) {
698 if ( empty( $theme['url'] ) ) {
699 continue;
700 }
701
702 $url = learn_press_get_item_referral( $theme['id'] );
703 $full_description = learn_press_trim_content( $theme['description'] );
704 $short_description = learn_press_trim_content( $theme['description'], 75 );
705 $url_demo = $theme['attributes'][4]['value'];
706 ?>
707
708 <div id="thimpress-<?php echo esc_attr( $theme['id'] ); ?>" class="slide-item">
709 <div class="slide-thumbnail">
710 <a href="<?php echo esc_url_raw( $url ); ?>">
711 <img src="<?php echo esc_url_raw( $theme['previews']['landscape_preview']['landscape_url'] ); ?>"/>
712 </a>
713 </div>
714
715 <div class="slide-detail">
716 <h2><a href="<?php echo esc_url_raw( $url ); ?>"><?php echo esc_html( $theme['name'] ); ?></a></h2>
717 <p class="slide-description description-full">
718 <?php echo wp_kses_post( $full_description ); ?>
719 </p>
720
721 <p class="slide-description description-short">
722 <?php echo wp_kses_post( $short_description ); ?>
723 </p>
724
725 <p class="slide-controls">
726 <a href="<?php echo esc_url_raw( $url ); ?>" class="button button-primary"
727 target="_blank"><?php esc_html_e( 'Get it now', 'learnpress' ); ?></a>
728 <a href="<?php echo esc_url_raw( $url_demo ); ?>" class="button"
729 target="_blank"><?php esc_html_e( 'View Demo', 'learnpress' ); ?></a>
730 </p>
731 </div>
732 </div>
733
734 <?php } ?>
735 </div>
736 <?php
737 }
738
739 /**
740 * Count number of orders between to dates
741 *
742 * @param string
743 * @param int
744 *
745 * @return int
746 */
747 function learn_press_get_order_by_time( $by, $time ) {
748 global $wpdb;
749
750 $user_id = get_current_user_id();
751
752 $y = gmdate( 'Y', $time );
753 $m = gmdate( 'm', $time );
754 $d = gmdate( 'd', $time );
755
756 switch ( $by ) {
757 case 'days':
758 $orders = $wpdb->get_var(
759 $wpdb->prepare(
760 "SELECT COUNT(*)
761 FROM $wpdb->posts AS p
762 INNER JOIN $wpdb->postmeta AS m ON p.ID = m.post_id
763 WHERE p.post_author = %d
764 AND p.post_type = %s
765 AND p.post_status = %s
766 AND m.meta_key = %s
767 AND m.meta_value = %s
768 AND YEAR(p.post_date) = %s AND MONTH(p.post_date) = %s AND DAY(p.post_date) = %s",
769 $user_id,
770 LP_ORDER_CPT,
771 'publish',
772 '_learn_press_transaction_status',
773 'completed',
774 $y,
775 $m,
776 $d
777 )
778 );
779 break;
780 case 'months':
781 $orders = $wpdb->get_var(
782 $wpdb->prepare(
783 "SELECT COUNT(*)
784 FROM $wpdb->posts AS p
785 INNER JOIN $wpdb->postmeta AS m ON p.ID = m.post_id
786 WHERE p.post_author = %d
787 AND p.post_type = %s
788 AND p.post_status = %s
789 AND m.meta_key = %s
790 AND m.meta_value = %s
791 AND YEAR(p.post_date) = %s AND MONTH(p.post_date) = %s",
792 $user_id,
793 LP_ORDER_CPT,
794 'publish',
795 '_learn_press_transaction_status',
796 'completed',
797 $y,
798 $m
799 )
800 );
801 break;
802 }
803
804 return $orders;
805 }
806
807 /**
808 * Count number of orders by status
809 *
810 * @param string Status of the orders
811 *
812 * @return int
813 */
814 function learn_press_get_courses_by_status( $status ) {
815 global $wpdb;
816
817 $user_id = get_current_user_id();
818
819 $courses = $wpdb->get_var(
820 $wpdb->prepare(
821 "SELECT COUNT(*)
822 FROM $wpdb->posts
823 WHERE post_author = %d
824 AND post_type = %s
825 AND post_status = %s",
826 $user_id,
827 LP_COURSE_CPT,
828 $status
829 )
830 );
831
832 return $courses;
833 }
834
835 /**
836 * Count number of orders by price
837 *
838 * @param string
839 *
840 * @return int
841 */
842 function learn_press_get_courses_by_price( $fee ) {
843 global $wpdb;
844
845 $user_id = get_current_user_id();
846
847 $courses = $wpdb->get_var(
848 $wpdb->prepare(
849 "SELECT COUNT(*)
850 FROM $wpdb->posts AS p
851 INNER JOIN $wpdb->postmeta AS m ON p.ID = m.post_id
852 WHERE p.post_author = %d
853 AND p.post_type = %s
854 AND p.post_status IN (%s, %s)
855 AND m.meta_key = %s
856 AND m.meta_value = %s",
857 $user_id,
858 LP_COURSE_CPT,
859 'publish',
860 'pending',
861 '_lpr_course_payment',
862 $fee
863 )
864 );
865
866 return $courses;
867 }
868
869 /**
870 * Get data about students to render in chart
871 *
872 * @param null $from
873 * @param null $by
874 * @param float $time_ago
875 *
876 * @return array
877 */
878 function learn_press_get_chart_students( $from = null, $by = null, $time_ago = 0 ) {
879 $labels = array();
880 $datasets = array();
881
882 if ( is_null( $from ) ) {
883 $from = current_time( 'mysql' );
884 }
885
886 if ( is_null( $by ) ) {
887 $by = 'days';
888 }
889
890 switch ( $by ) {
891 case 'days':
892 $date_format = 'M d';
893 break;
894 case 'months':
895 $date_format = 'M Y';
896 break;
897 case 'years':
898 $date_format = 'Y';
899 break;
900 }
901
902 for ( $i = - $time_ago + 1; $i <= 0; $i ++ ) {
903 $labels[] = gmdate( $date_format, strtotime( "$i $by", strtotime( $from ) ) );
904 $datasets[0]['data'][] = learn_press_get_order_by_time( $by, strtotime( "$i $by", strtotime( $from ) ) );
905 }
906
907 $colors = learn_press_get_admin_colors();
908 $datasets[0]['fillColor'] = 'rgba(255,255,255,0.1)';
909 $datasets[0]['strokeColor'] = $colors[0];
910 $datasets[0]['pointColor'] = $colors[0];
911 $datasets[0]['pointStrokeColor'] = $colors[2];
912 $datasets[0]['pointHighlightFill'] = $colors[2];
913 $datasets[0]['pointHighlightStroke'] = $colors[0];
914
915 return array(
916 'labels' => $labels,
917 'datasets' => $datasets,
918 );
919 }
920
921 /**
922 * Get data about students to render in chart
923 *
924 * @param null $from
925 * @param null $by
926 * @param float $time_ago
927 *
928 * @return array
929 */
930 function learn_press_get_chart_users( $from = null, $by = null, $time_ago = 0 ) {
931 global $wpdb;
932
933 $labels = array();
934 $datasets = array();
935
936 if ( is_null( $from ) ) {
937 $from = current_time( 'mysql' );
938 }
939
940 if ( is_null( $by ) ) {
941 $by = 'days';
942 }
943
944 $results = array(
945 'all' => array(),
946 'instructors' => array(),
947 );
948
949 $from_time = is_numeric( $from ) ? $from : strtotime( $from );
950
951 switch ( $by ) {
952 case 'days':
953 $date_format = 'M d Y';
954 $_from = - $time_ago + 1;
955 $_from = gmdate( 'Y-m-d', strtotime( "{$_from} {$by}", $from_time ) );
956 $_to = date( 'Y-m-d', $from_time );
957 $_sql_format = '%Y-%m-%d';
958 $_key_format = 'Y-m-d';
959 break;
960 case 'months':
961 $date_format = 'M Y';
962 $_from = - $time_ago + 1;
963 $_from = date( 'Y-m-01', strtotime( "{$_from} {$by}", $from_time ) );
964 $days = date( 't', mktime( 0, 0, 0, date( 'm', $from_time ), 1, date( 'Y', $from_time ) ) );
965 $_to = date( 'Y-m-' . $days, $from_time );
966 $_sql_format = '%Y-%m';
967 $_key_format = 'Y-m';
968 break;
969 case 'years':
970 $date_format = 'Y';
971 $_from = - $time_ago + 1;
972 $_from = date( 'Y-01-01', strtotime( "{$_from} {$by}", $from_time ) );
973 $days = date( 't', mktime( 0, 0, 0, date( 'm', $from_time ), 1, date( 'Y', $from_time ) ) );
974 $_to = date( 'Y-12-' . $days, $from_time );
975 $_sql_format = '%Y';
976 $_key_format = 'Y';
977
978 break;
979 }
980 $query = $wpdb->prepare(
981 "
982 SELECT count(u.ID) as c, DATE_FORMAT( u.user_registered, %s) as d
983 FROM {$wpdb->users} u
984 WHERE 1
985 GROUP BY d
986 HAVING d BETWEEN %s AND %s
987 ORDER BY d ASC
988 ",
989 $_sql_format,
990 $_from,
991 $_to
992 );
993
994 if ( $_results = $wpdb->get_results( $query ) ) {
995 foreach ( $_results as $k => $v ) {
996 $results['all'][ $v->d ] = $v;
997 }
998 }
999
1000 $query = $wpdb->prepare(
1001 "
1002 SELECT count(u.ID) as c, DATE_FORMAT( u.user_registered, %s) as d
1003 FROM {$wpdb->users} u
1004 INNER JOIN {$wpdb->usermeta} um ON um.user_id = u.ID AND um.meta_key = %s AND ( um.meta_value LIKE %s OR um.meta_value LIKE %s )
1005 WHERE 1
1006 GROUP BY d
1007 HAVING d BETWEEN %s AND %s
1008 ORDER BY d ASC
1009 ",
1010 $_sql_format,
1011 'wp_capabilities',
1012 '%' . $wpdb->esc_like( 's:13:"administrator"' ) . '%',
1013 '%' . $wpdb->esc_like( 's:10:"lp_teacher"' ) . '%',
1014 $_from,
1015 $_to
1016 );
1017
1018 if ( $_results = $wpdb->get_results( $query ) ) {
1019 foreach ( $_results as $k => $v ) {
1020 $results['instructors'][ $v->d ] = $v;
1021 }
1022 }
1023
1024 for ( $i = - $time_ago + 1; $i <= 0; $i ++ ) {
1025 $date = strtotime( "$i $by", $from_time );
1026 $labels[] = date( $date_format, $date );
1027 $key = date( $_key_format, $date );
1028
1029 $all = ! empty( $results['all'][ $key ] ) ? $results['all'][ $key ]->c : 0;
1030 $instructors = ! empty( $results['instructors'][ $key ] ) ? $results['instructors'][ $key ]->c : 0;
1031
1032 $datasets[0]['data'][] = $all;
1033 $datasets[1]['data'][] = $instructors;
1034 $datasets[2]['data'][] = $all - $instructors;
1035 }
1036
1037 $dataset_params = array(
1038 array(
1039 'color1' => 'rgba(47, 167, 255, %s)',
1040 'color2' => '#FFF',
1041 'label' => __( 'All', 'learnpress' ),
1042 ),
1043 array(
1044 'color1' => 'rgba(212, 208, 203, %s)',
1045 'color2' => '#FFF',
1046 'label' => __( 'Instructors', 'learnpress' ),
1047 ),
1048 array(
1049 'color1' => 'rgba(234, 199, 155, %s)',
1050 'color2' => '#FFF',
1051 'label' => __( 'Students', 'learnpress' ),
1052 ),
1053 );
1054
1055 foreach ( $dataset_params as $k => $v ) {
1056 $datasets[ $k ]['fillColor'] = sprintf( $v['color1'], '0.2' );
1057 $datasets[ $k ]['strokeColor'] = sprintf( $v['color1'], '1' );
1058 $datasets[ $k ]['pointColor'] = sprintf( $v['color1'], '1' );
1059 $datasets[ $k ]['pointStrokeColor'] = $v['color2'];
1060 $datasets[ $k ]['pointHighlightFill'] = $v['color2'];
1061 $datasets[ $k ]['pointHighlightStroke'] = sprintf( $v['color1'], '1' );
1062 $datasets[ $k ]['label'] = $v['label'];
1063 }
1064
1065 return array(
1066 'labels' => $labels,
1067 'datasets' => $datasets,
1068 'sql' => $query,
1069 );
1070 }
1071
1072
1073 /**
1074 * Get data about students to render in chart
1075 *
1076 * @param null $from
1077 * @param null $by
1078 * @param float $time_ago
1079 *
1080 * @return array
1081 */
1082 function learn_press_get_chart_courses( $from = null, $by = null, $time_ago = 0 ) {
1083 global $wpdb;
1084
1085 $labels = array();
1086 $datasets = array();
1087
1088 if ( is_null( $from ) ) {
1089 $from = current_time( 'mysql' );
1090 }
1091
1092 if ( is_null( $by ) ) {
1093 $by = 'days';
1094 }
1095
1096 $results = array(
1097 'all' => array(),
1098 'public' => array(),
1099 'pending' => array(),
1100 'free' => array(),
1101 'paid' => array(),
1102 );
1103
1104 $from_time = is_numeric( $from ) ? $from : strtotime( $from );
1105
1106 switch ( $by ) {
1107 case 'days':
1108 $date_format = 'M d Y';
1109 $_from = - $time_ago + 1;
1110 $_from = date( 'Y-m-d', strtotime( "{$_from} {$by}", $from_time ) );
1111 $_to = date( 'Y-m-d', $from_time );
1112 $_sql_format = '%Y-%m-%d';
1113 $_key_format = 'Y-m-d';
1114 break;
1115 case 'months':
1116 $date_format = 'M Y';
1117 $_from = - $time_ago + 1;
1118 $_from = date( 'Y-m-01', strtotime( "{$_from} {$by}", $from_time ) );
1119 $days = date( 't', mktime( 0, 0, 0, date( 'm', $from_time ), 1, date( 'Y', $from_time ) ) );
1120 $_to = date( 'Y-m-' . $days, $from_time );
1121 $_sql_format = '%Y-%m';
1122 $_key_format = 'Y-m';
1123 break;
1124 case 'years':
1125 $date_format = 'Y';
1126 $_from = - $time_ago + 1;
1127 $_from = date( 'Y-01-01', strtotime( "{$_from} {$by}", $from_time ) );
1128 $days = date( 't', mktime( 0, 0, 0, date( 'm', $from_time ), 1, date( 'Y', $from_time ) ) );
1129 $_to = date( 'Y-12-' . $days, $from_time );
1130 $_sql_format = '%Y';
1131 $_key_format = 'Y';
1132
1133 break;
1134 }
1135
1136 $query_where = '';
1137
1138 if ( current_user_can( LP_TEACHER_ROLE ) ) {
1139 $user_id = learn_press_get_current_user_id();
1140 $query_where .= $wpdb->prepare( ' AND c.post_author=%d ', $user_id );
1141 }
1142
1143 $query = $wpdb->prepare(
1144 "
1145 SELECT count(c.ID) as c, DATE_FORMAT( c.post_date, %s) as d
1146 FROM {$wpdb->posts} c
1147 WHERE 1
1148 {$query_where}
1149 AND c.post_status IN('publish', 'pending') AND c.post_type = %s
1150 GROUP BY d
1151 HAVING d BETWEEN %s AND %s
1152 ORDER BY d ASC
1153 ",
1154 $_sql_format,
1155 'lp_course',
1156 $_from,
1157 $_to
1158 );
1159 if ( $_results = $wpdb->get_results( $query ) ) {
1160 foreach ( $_results as $k => $v ) {
1161 $results['all'][ $v->d ] = $v;
1162 }
1163 }
1164 $query = $wpdb->prepare(
1165 "
1166 SELECT count(c.ID) as c, DATE_FORMAT( c.post_date, %s) as d
1167 FROM {$wpdb->posts} c
1168 WHERE 1
1169 {$query_where}
1170 AND c.post_status = %s AND c.post_type = %s
1171 GROUP BY d
1172 HAVING d BETWEEN %s AND %s
1173 ORDER BY d ASC
1174 ",
1175 $_sql_format,
1176 'publish',
1177 'lp_course',
1178 $_from,
1179 $_to
1180 );
1181
1182 $_results = $wpdb->get_results( $query );
1183 if ( $_results ) {
1184 foreach ( $_results as $k => $v ) {
1185 $results['publish'][ $v->d ] = $v;
1186 }
1187 }
1188
1189 $query = $wpdb->prepare(
1190 "
1191 SELECT count(c.ID) as c, DATE_FORMAT( c.post_date, %s) as d
1192 FROM {$wpdb->posts} c
1193 INNER JOIN {$wpdb->postmeta} cm ON cm.post_id = c.ID
1194 WHERE 1
1195 {$query_where}
1196 AND c.post_status = %s AND c.post_type = %s
1197 GROUP BY d
1198 HAVING d BETWEEN %s AND %s
1199 ORDER BY d ASC
1200 ",
1201 $_sql_format,
1202 'publish',
1203 'lp_course',
1204 $_from,
1205 $_to
1206 );
1207
1208 $_results = $wpdb->get_results( $query );
1209 if ( $_results ) {
1210 foreach ( $_results as $k => $v ) {
1211 $results['paid'][ $v->d ] = $v;
1212 }
1213 }
1214
1215 for ( $i = - $time_ago + 1; $i <= 0; $i ++ ) {
1216 $date = strtotime( "$i $by", $from_time );
1217 $labels[] = date( $date_format, $date );
1218 $key = date( $_key_format, $date );
1219
1220 $all = ! empty( $results['all'][ $key ] ) ? $results['all'][ $key ]->c : 0;
1221 $publish = ! empty( $results['publish'][ $key ] ) ? $results['publish'][ $key ]->c : 0;
1222 $paid = ! empty( $results['paid'][ $key ] ) ? $results['paid'][ $key ]->c : 0;
1223
1224 $datasets[0]['data'][] = $all;
1225 $datasets[1]['data'][] = $publish;
1226 $datasets[2]['data'][] = $all - $publish;
1227 $datasets[3]['data'][] = $paid;
1228 $datasets[4]['data'][] = $all - $paid;
1229 }
1230
1231 $dataset_params = array(
1232 array(
1233 'color1' => 'rgba(47, 167, 255, %s)',
1234 'color2' => '#FFF',
1235 'label' => __( 'All', 'learnpress' ),
1236 ),
1237 array(
1238 'color1' => 'rgba(212, 208, 203, %s)',
1239 'color2' => '#FFF',
1240 'label' => __( 'Publish', 'learnpress' ),
1241 ),
1242 array(
1243 'color1' => 'rgba(234, 199, 155, %s)',
1244 'color2' => '#FFF',
1245 'label' => __( 'Pending', 'learnpress' ),
1246 ),
1247 array(
1248 'color1' => 'rgba(234, 199, 155, %s)',
1249 'color2' => '#FFF',
1250 'label' => __( 'Paid', 'learnpress' ),
1251 ),
1252 array(
1253 'color1' => 'rgba(234, 199, 155, %s)',
1254 'color2' => '#FFF',
1255 'label' => __( 'Free', 'learnpress' ),
1256 ),
1257 );
1258
1259 foreach ( $dataset_params as $k => $v ) {
1260 $datasets[ $k ]['fillColor'] = sprintf( $v['color1'], '0.2' );
1261 $datasets[ $k ]['strokeColor'] = sprintf( $v['color1'], '1' );
1262 $datasets[ $k ]['pointColor'] = sprintf( $v['color1'], '1' );
1263 $datasets[ $k ]['pointStrokeColor'] = $v['color2'];
1264 $datasets[ $k ]['pointHighlightFill'] = $v['color2'];
1265 $datasets[ $k ]['pointHighlightStroke'] = sprintf( $v['color1'], '1' );
1266 $datasets[ $k ]['label'] = $v['label'];
1267 }
1268
1269 return array(
1270 'labels' => $labels,
1271 'datasets' => $datasets,
1272 'sql' => $query,
1273 );
1274 }
1275
1276
1277 /**
1278 * Get data about students to render in chart
1279 *
1280 * @param null $from
1281 * @param null $by
1282 * @param float $time_ago
1283 *
1284 * @return array
1285 */
1286 function learn_press_get_chart_orders( $from = null, $by = null, $time_ago = 0 ) {
1287 global $wpdb;
1288 // var_dump( current_user_can(LP_TEACHER_ROLE) );
1289 // exit();
1290 $report_sales_by = learn_press_get_request( 'report_sales_by' );
1291 $course_id = learn_press_get_request( 'course_id' );
1292 $cat_id = learn_press_get_request( 'cat_id' );
1293
1294 $labels = array();
1295 $datasets = array();
1296 if ( is_null( $from ) ) {
1297 $from = current_time( 'mysql' );
1298 }
1299 if ( is_null( $by ) ) {
1300 $by = 'days';
1301 }
1302 $results = array(
1303 'all' => array(),
1304 'completed' => array(),
1305 'pending' => array(),
1306 );
1307 $from_time = is_numeric( $from ) ? $from : strtotime( $from );
1308
1309 switch ( $by ) {
1310 case 'days':
1311 $date_format = 'M d Y';
1312 $_from = - $time_ago + 1;
1313 $_from = date( 'Y-m-d', strtotime( "{$_from} {$by}", $from_time ) );
1314 $_to = date( 'Y-m-d', $from_time );
1315 $_sql_format = '%Y-%m-%d';
1316 $_key_format = 'Y-m-d';
1317 break;
1318 case 'months':
1319 $date_format = 'M Y';
1320 $_from = - $time_ago + 1;
1321 $_from = date( 'Y-m-01', strtotime( "{$_from} {$by}", $from_time ) );
1322 $days = date( 't', mktime( 0, 0, 0, date( 'm', $from_time ), 1, date( 'Y', $from_time ) ) );
1323 $_to = date( 'Y-m-' . $days, $from_time );
1324 $_sql_format = '%Y-%m';
1325 $_key_format = 'Y-m';
1326 break;
1327 case 'years':
1328 $date_format = 'Y';
1329 $_from = - $time_ago + 1;
1330 $_from = date( 'Y-01-01', strtotime( "{$_from} {$by}", $from_time ) );
1331 $days = date( 't', mktime( 0, 0, 0, date( 'm', $from_time ), 1, date( 'Y', $from_time ) ) );
1332 $_to = date( 'Y-12-' . $days, $from_time );
1333 $_sql_format = '%Y';
1334 $_key_format = 'Y';
1335
1336 break;
1337 }
1338
1339 $query_join = '';
1340 $query_where = '';
1341 if ( 'course' === $report_sales_by ) {
1342 $sql_join .= " INNER JOIN `{$wpdb->prefix}learnpress_order_items` `lpoi` "
1343 . ' ON o.ID=lpoi.order_id '
1344 . " INNER JOIN {$wpdb->prefix}learnpress_order_itemmeta loim "
1345 . ' ON lpoi.order_item_id=loim.learnpress_order_item_id '
1346 . " AND loim.meta_key='_course_id' "
1347 . ' AND CAST(loim.meta_value AS SIGNED)=%d ';
1348 if ( current_user_can( LP_TEACHER_ROLE ) ) {
1349 $user_id = learn_press_get_current_user_id();
1350 $sql_join .= $wpdb->prepare(
1351 ' AND CAST(loim.meta_value AS SIGNED) IN '
1352 . ' ( '
1353 . " SELECT ID FROM {$wpdb->posts} p WHERE p.ID = CAST(loim.meta_value AS SIGNED) AND `post_author`=" . intval( $user_id )
1354 . ' ) '
1355 );
1356 }
1357 $query_join .= $wpdb->prepare( $sql_join, $course_id );
1358
1359 } elseif ( 'category' === $report_sales_by ) {
1360 $sql_join .= " INNER JOIN `{$wpdb->prefix}learnpress_order_items` `lpoi` "
1361 . ' ON o.ID=lpoi.order_id '
1362 . " INNER JOIN {$wpdb->prefix}learnpress_order_itemmeta loim "
1363 . ' ON lpoi.order_item_id=loim.learnpress_order_item_id '
1364 . " AND loim.meta_key='_course_id' "
1365 . ' AND CAST(loim.meta_value AS SIGNED) IN('
1366 // sub query
1367 . ' SELECT tr.object_id '
1368 . " FROM {$wpdb->prefix}term_taxonomy tt INNER JOIN {$wpdb->prefix}term_relationships tr "
1369 . " ON tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy='course_category' "
1370 . ' WHERE tt.term_id=%d)';
1371 $query_join .= $wpdb->prepare( $sql_join, $cat_id );
1372 }
1373 if ( current_user_can( LP_TEACHER_ROLE ) ) {
1374 $user_id = learn_press_get_current_user_id();
1375 $query_where .= $wpdb->prepare(
1376 " AND o.ID IN( SELECT oi.order_id
1377 FROM lptest.{$wpdb->prefix}learnpress_order_items oi
1378 inner join {$wpdb->prefix}learnpress_order_itemmeta oim
1379 on oi.order_item_id = oim.learnpress_order_item_id
1380 and oim.meta_key='_course_id'
1381 and cast(oim.meta_value as SIGNED) IN (
1382 SELECT sp.ID FROM {$wpdb->prefix}posts sp WHERE sp.post_author=%d and sp.ID=cast(oim.meta_value as SIGNED)
1383 )
1384 ) ",
1385 $user_id
1386 );
1387 }
1388
1389 $query = $wpdb->prepare(
1390 "
1391 SELECT count(o.ID) as c, DATE_FORMAT( o.post_date, %s) as d
1392 FROM {$wpdb->posts} o {$query_join}
1393 WHERE 1 {$query_where}
1394 AND o.post_status IN('lp-completed') AND o.post_type = %s
1395 GROUP BY d
1396 HAVING d BETWEEN %s AND %s
1397 ORDER BY d ASC
1398 ",
1399 $_sql_format,
1400 'lp_order',
1401 $_from,
1402 $_to
1403 );
1404
1405 if ( $_results = $wpdb->get_results( $query ) ) {
1406 foreach ( $_results as $k => $v ) {
1407 $results['completed'][ $v->d ] = $v;
1408 }
1409 }
1410
1411 $query = $wpdb->prepare(
1412 "
1413 SELECT count(o.ID) as c, DATE_FORMAT( o.post_date, %s) as d
1414 FROM {$wpdb->posts} o {$query_join}
1415 WHERE 1 {$query_where}
1416 AND o.post_status IN('lp-pending', 'lp-processing') AND o.post_type = %s
1417 GROUP BY d
1418 HAVING d BETWEEN %s AND %s
1419 ORDER BY d ASC
1420 ",
1421 $_sql_format,
1422 'lp_order',
1423 $_from,
1424 $_to
1425 );
1426
1427 if ( $_results = $wpdb->get_results( $query ) ) {
1428 foreach ( $_results as $k => $v ) {
1429 $results['pending'][ $v->d ] = $v;
1430 }
1431 }
1432
1433 for ( $i = - $time_ago + 1; $i <= 0; $i ++ ) {
1434 $date = strtotime( "$i $by", $from_time );
1435 $labels[] = date( $date_format, $date );
1436 $key = date( $_key_format, $date );
1437
1438 $completed = ! empty( $results['completed'][ $key ] ) ? $results['completed'][ $key ]->c : 0;
1439 $pending = ! empty( $results['pending'][ $key ] ) ? $results['pending'][ $key ]->c : 0;
1440 $all = $completed + $pending;
1441
1442 $datasets[0]['data'][] = $all;
1443 $datasets[1]['data'][] = $completed;
1444 $datasets[2]['data'][] = $pending;
1445 }
1446
1447 $dataset_params = array(
1448 array(
1449 'color1' => 'rgba(47, 167, 255, %s)',
1450 'color2' => '#FFF',
1451 'label' => __( 'All', 'learnpress' ),
1452 ),
1453 array(
1454 'color1' => 'rgba(212, 208, 203, %s)',
1455 'color2' => '#FFF',
1456 'label' => __( 'Completed', 'learnpress' ),
1457 ),
1458 array(
1459 'color1' => 'rgba(234, 199, 155, %s)',
1460 'color2' => '#FFF',
1461 'label' => __( 'Pending', 'learnpress' ),
1462 ),
1463 );
1464
1465 foreach ( $dataset_params as $k => $v ) {
1466 $datasets[ $k ]['fillColor'] = sprintf( $v['color1'], '0.2' );
1467 $datasets[ $k ]['strokeColor'] = sprintf( $v['color1'], '1' );
1468 $datasets[ $k ]['pointColor'] = sprintf( $v['color1'], '1' );
1469 $datasets[ $k ]['pointStrokeColor'] = $v['color2'];
1470 $datasets[ $k ]['pointHighlightFill'] = $v['color2'];
1471 $datasets[ $k ]['pointHighlightStroke'] = sprintf( $v['color1'], '1' );
1472 $datasets[ $k ]['label'] = $v['label'];
1473 }
1474
1475 return array(
1476 'labels' => $labels,
1477 'datasets' => $datasets,
1478 'sql' => $query,
1479 );
1480 }
1481
1482 /**
1483 * Get data about courses to render in the chart
1484 *
1485 * @return array
1486 */
1487 function learn_press_get_chart_courses2() {
1488 $labels = array(
1489 __( 'Pending Courses / Publish Courses', 'learnpress' ),
1490 __( 'Free Courses / Priced Courses', 'learnpress' ),
1491 );
1492
1493 $datasets = array();
1494 $datasets[0]['data'] = array(
1495 learn_press_get_courses_by_status( 'pending' ),
1496 learn_press_get_courses_by_price( 'free' ),
1497 );
1498
1499 $datasets[1]['data'] = array(
1500 learn_press_get_courses_by_status( 'publish' ),
1501 learn_press_get_courses_by_price( 'not_free' ),
1502 );
1503
1504 $colors = learn_press_get_admin_colors();
1505 $datasets[0]['fillColor'] = $colors[1];
1506 $datasets[0]['strokeColor'] = $colors[1];
1507 $datasets[1]['fillColor'] = $colors[3];
1508 $datasets[1]['strokeColor'] = $colors[3];
1509
1510 return array(
1511 'labels' => $labels,
1512 'datasets' => $datasets,
1513 );
1514 }
1515
1516 /**
1517 * Get colors setting up by admin user
1518 *
1519 * @return array
1520 */
1521 function learn_press_get_admin_colors() {
1522 $admin_color = get_user_meta( get_current_user_id(), 'admin_color', true );
1523 global $_wp_admin_css_colors;
1524 $colors = array();
1525
1526 if ( ! empty( $_wp_admin_css_colors[ $admin_color ]->colors ) ) {
1527 $colors = $_wp_admin_css_colors[ $admin_color ]->colors;
1528 }
1529
1530 if ( empty( $colors[0] ) ) {
1531 $colors[0] = '#000000';
1532 }
1533
1534 if ( empty( $colors[2] ) ) {
1535 $colors[2] = '#00FF00';
1536 }
1537
1538 return $colors;
1539 }
1540
1541 /**
1542 * Convert an array to json format and print out to browser
1543 *
1544 * @param array $chart
1545 */
1546 function learn_press_process_chart( $chart = array() ) {
1547 $data = json_encode(
1548 array(
1549 'labels' => $chart['labels'],
1550 'datasets' => $chart['datasets'],
1551 )
1552 );
1553 echo wp_kses_post( $data );
1554 }
1555
1556 /**
1557 * Print out the configuration for admin chart
1558 */
1559 function learn_press_config_chart() {
1560 $colors = learn_press_get_admin_colors();
1561
1562 $config = array(
1563 'scaleShowGridLines' => true,
1564 'scaleGridLineColor' => '#777',
1565 'scaleGridLineWidth' => 0.3,
1566 'scaleFontColor' => '#444',
1567 'scaleLineColor' => $colors[0],
1568 'bezierCurve' => true,
1569 'bezierCurveTension' => 0.2,
1570 'pointDotRadius' => 5,
1571 'pointDotStrokeWidth' => 5,
1572 'pointHitDetectionRadius' => 20,
1573 'datasetStroke' => true,
1574 'responsive' => true,
1575 'tooltipFillColor' => $colors[2],
1576 'tooltipFontColor' => '#eee',
1577 'tooltipCornerRadius' => 0,
1578 'tooltipYPadding' => 10,
1579 'tooltipXPadding' => 10,
1580 'barDatasetSpacing' => 10,
1581 'barValueSpacing' => 200,
1582
1583 );
1584
1585 echo json_encode( $config );
1586 }
1587
1588 function set_post_order_in_admin( $wp_query ) {
1589 global $pagenow;
1590
1591 if ( isset( $_GET['post_type'] ) ) {
1592 $post_type = LP_Helper::sanitize_params_submitted( $_GET['post_type'] );
1593 } else {
1594 $post_type = '';
1595 }
1596
1597 if ( is_admin() && 'edit.php' == $pagenow && $post_type == LP_COURSE_CPT && ! isset( $_GET['orderby'] ) ) {
1598 $wp_query->set( 'orderby', 'date' );
1599 $wp_query->set( 'order', 'DSC' );
1600 }
1601 }
1602
1603 // add_filter( 'pre_get_posts', 'set_post_order_in_admin' );
1604
1605 function learn_press_copy_post_meta( $from_id, $to_id ) {
1606 global $wpdb;
1607
1608 $course_meta = $wpdb->get_results(
1609 $wpdb->prepare( "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = %d", $from_id )
1610 );
1611
1612 if ( count( $course_meta ) != 0 ) {
1613 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
1614 $sql_query_sel = array();
1615
1616 foreach ( $course_meta as $meta ) {
1617 $meta_key = $meta->meta_key;
1618 $meta_value = addslashes( $meta->meta_value );
1619
1620 $sql_query_sel[] = "SELECT {$to_id}, '$meta_key', '$meta_value'";
1621 }
1622
1623 $sql_query .= implode( ' UNION ALL ', $sql_query_sel );
1624 $wpdb->query( $sql_query );
1625 }
1626 }
1627
1628 /**
1629 * Check to see if a plugin is already installed or not
1630 *
1631 * @param $plugin
1632 *
1633 * @return bool
1634 */
1635 function learn_press_is_plugin_install( $plugin ) {
1636 $installed_plugins = get_plugins();
1637
1638 return isset( $installed_plugins[ $plugin ] );
1639 }
1640
1641 /**
1642 * Get plugin file that contains the information from slug
1643 *
1644 * @param $slug
1645 *
1646 * @return mixed
1647 */
1648 function learn_press_plugin_basename_from_slug( $slug ) {
1649 $keys = array_keys( get_plugins() );
1650
1651 foreach ( $keys as $key ) {
1652 if ( preg_match( '|^' . $slug . '/|', $key ) ) {
1653 return $key;
1654 }
1655 }
1656
1657 return $slug;
1658 }
1659
1660 function learn_press_request_query( $vars = array() ) {
1661 global $typenow, $wp_query, $wp_post_statuses;
1662
1663 if ( LP_ORDER_CPT === $typenow ) {
1664 if ( ! isset( $vars['post_status'] ) ) {
1665 $post_statuses = learn_press_get_order_statuses();
1666
1667 foreach ( $post_statuses as $status => $value ) {
1668 if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) {
1669 unset( $post_statuses[ $status ] );
1670 }
1671 }
1672
1673 $vars['post_status'] = array_keys( $post_statuses );
1674
1675 }
1676 }
1677
1678 return $vars;
1679 }
1680
1681 add_filter( 'request', 'learn_press_request_query', 0 );
1682
1683 function _learn_press_reset_course_data() {
1684 if ( empty( $_REQUEST['reset-course-data'] ) ) {
1685 return false;
1686 }
1687
1688 learn_press_reset_course_data( intval( $_REQUEST['reset-course-data'] ) );
1689
1690 wp_redirect( esc_url_raw( remove_query_arg( 'reset-course-data' ) ) );
1691 }
1692
1693 add_action( 'init', '_learn_press_reset_course_data' );
1694
1695 function learn_press_admin_section_loop_item_class( $item, $section ) {
1696 $classes = array(
1697 'lp-section-item',
1698 );
1699
1700 $classes[] = 'lp-item-' . $item->post_type;
1701
1702 if ( ! absint( $item->ID ) ) {
1703 $classes[] = 'lp-item-empty lp-item-new focus';
1704 }
1705
1706 $classes = apply_filters( 'learn_press_section_loop_item_class', $classes, $item, $section );
1707
1708 if ( $classes ) {
1709 echo 'class="' . join( ' ', $classes ) . '"';
1710 }
1711
1712 return $classes;
1713 }
1714
1715 function learn_press_disable_checked_ontop( $args ) {
1716
1717 if ( 'course_category' == $args['taxonomy'] ) {
1718 $args['checked_ontop'] = false;
1719 }
1720
1721 return $args;
1722 }
1723
1724 add_filter( 'wp_terms_checklist_args', 'learn_press_disable_checked_ontop' );
1725
1726 function learn_press_get_screens() {
1727 $screen_id = sanitize_title( __( 'LearnPress', 'learnpress' ) );
1728
1729 $screens = array(
1730 'toplevel_page_' . $screen_id,
1731 $screen_id . '_page_learn-press-statistics',
1732 $screen_id . '_page_learn-press-addons',
1733 $screen_id . '_page_learn-press-settings',
1734 $screen_id . '_page_learn-press-tools',
1735 );
1736
1737 foreach ( array( LP_COURSE_CPT, LP_LESSON_CPT, LP_QUIZ_CPT, LP_QUESTION_CPT, LP_ORDER_CPT ) as $post_type ) {
1738 $screens[] = 'edit-' . $post_type;
1739 $screens[] = $post_type;
1740 }
1741
1742 return apply_filters( 'learn_press_screen_ids', $screens );
1743 }
1744
1745 function learn_press_is_post_type_screen( $post_type, $union = 'OR' ) {
1746 if ( is_array( $post_type ) ) {
1747 $return = null;
1748
1749 foreach ( $post_type as $_post_type ) {
1750 $check = learn_press_is_post_type_screen( $_post_type );
1751
1752 if ( $union == 'OR' && $check ) {
1753 return true;
1754 }
1755
1756 if ( $return == null ) {
1757 $return = $check;
1758 } else {
1759 $return = $return && $check;
1760 }
1761
1762 if ( $union !== 'OR' ) {
1763 return $return;
1764 }
1765 }
1766
1767 return $return;
1768 }
1769
1770 $screen = get_current_screen();
1771
1772 if ( ! $screen ) {
1773 return;
1774 }
1775
1776 $screen_id = $screen->id;
1777
1778 return in_array( $screen_id, array( $post_type, "edit-{$post_type}" ) );
1779 }
1780
1781 function learn_press_get_notice_dismiss( $context, $type = 'transient' ) {
1782 if ( $type == 'transient' ) {
1783 return get_transient( 'learn_press_dismiss_notice_' . $context );
1784 }
1785
1786 return get_option( 'learn_press_dismiss_notice_' . $context );
1787 }
1788
1789 if ( ! function_exists( 'learn_press_course_insert_section' ) ) {
1790 function learn_press_course_insert_section( $section = array() ) {
1791 global $wpdb;
1792
1793 $section = wp_parse_args(
1794 $section,
1795 array(
1796 'section_name' => '',
1797 'section_course_id' => 0,
1798 'section_order' => 0,
1799 'section_description' => '',
1800 )
1801 );
1802
1803 $section = stripslashes_deep( $section );
1804
1805 extract( $section );
1806
1807 $insert_data = compact( 'section_name', 'section_course_id', 'section_order', 'section_description' );
1808
1809 $wpdb->insert(
1810 $wpdb->learnpress_sections,
1811 $insert_data,
1812 array( '%s', '%d', '%d' )
1813 );
1814
1815 return $wpdb->insert_id;
1816 }
1817 }
1818
1819 if ( ! function_exists( 'learn_press_course_insert_section_item' ) ) {
1820 function learn_press_course_insert_section_item( $item_data = array() ) {
1821 global $wpdb;
1822
1823 $wpdb->insert(
1824 $wpdb->learnpress_section_items,
1825 array(
1826 'section_id' => $item_data['section_id'],
1827 'item_id' => $item_data['item_id'],
1828 'item_order' => $item_data['item_order'],
1829 'item_type' => $item_data['item_type'],
1830 ),
1831 array(
1832 '%d',
1833 '%d',
1834 '%d',
1835 '%s',
1836 )
1837 );
1838
1839 return $wpdb->insert_id;
1840 }
1841 }
1842
1843 if ( ! function_exists( 'learn_press_duplicate_post' ) ) {
1844 /**
1845 * Duplicate post.
1846 *
1847 * @param null $post_id
1848 * @param array $args
1849 * @param bool $meta
1850 *
1851 * @return bool|mixed
1852 * @since 3.0.0
1853 */
1854 function learn_press_duplicate_post( $post_id = null, $args = array(), $meta = true ) {
1855 $post = get_post( $post_id );
1856
1857 if ( ! $post ) {
1858 return false;
1859 }
1860
1861 $default = array(
1862 'comment_status' => $post->comment_status,
1863 'ping_status' => $post->ping_status,
1864 'post_author' => get_current_user_id(),
1865 'post_content' => $post->post_content,
1866 'post_excerpt' => $post->post_excerpt,
1867 'post_parent' => $post->post_parent,
1868 'post_password' => $post->post_password,
1869 'post_status' => 'draft',
1870 'post_title' => $post->post_title . __( ' Copy', 'learnpress' ),
1871 'post_type' => $post->post_type,
1872 'to_ping' => $post->to_ping,
1873 'menu_order' => $post->menu_order,
1874 'exclude_meta' => array(),
1875 );
1876
1877 $args = wp_parse_args( $args, $default );
1878 $exclude_meta = array();
1879
1880 if ( ! empty( $args['exclude_meta'] ) ) {
1881 $exclude_meta = $args['exclude_meta'];
1882 unset( $args['exclude_meta'] );
1883 }
1884
1885 $new_post_id = wp_insert_post( $args );
1886
1887 if ( ! is_wp_error( $new_post_id ) && $meta ) {
1888 learn_press_duplicate_post_meta( $post_id, $new_post_id, $exclude_meta );
1889
1890 $taxonomies = get_object_taxonomies( $post->post_type );
1891
1892 foreach ( $taxonomies as $taxonomy ) {
1893 $post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
1894 wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
1895 }
1896 }
1897
1898 return apply_filters( 'learn_press_duplicate_post', $new_post_id, $post_id );
1899 }
1900 }
1901
1902 /**
1903 * @editor tungnx
1904 * @modify 4.1.4 - fix sanitize query
1905 */
1906 if ( ! function_exists( 'learn_press_duplicate_post_meta' ) ) {
1907 function learn_press_duplicate_post_meta( $old_post_id, $new_post_id, $excerpt = array() ) {
1908 global $wpdb;
1909
1910 $post_meta_infos = $wpdb->get_results(
1911 $wpdb->prepare(
1912 "SELECT meta_key, meta_value FROM $wpdb->postmeta
1913 WHERE post_id = %d",
1914 $old_post_id
1915 )
1916 );
1917
1918 if ( count( $post_meta_infos ) != 0 ) {
1919 $excerpt = array_merge( array( '_edit_lock', '_edit_last' ), $excerpt );
1920 $excerpt = apply_filters(
1921 'learn_press_excerpt_duplicate_post_meta',
1922 $excerpt,
1923 $old_post_id,
1924 $new_post_id
1925 );
1926 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
1927 $sql_query_sel = array();
1928
1929 foreach ( $post_meta_infos as $meta ) {
1930 if ( in_array( $meta->meta_key, $excerpt ) ) {
1931 continue;
1932 }
1933
1934 if ( $meta->meta_key === '_lp_course_author' ) {
1935 $meta->meta_value = get_current_user_id();
1936 }
1937
1938 $meta_key = $meta->meta_key;
1939 $meta_value = addslashes( $meta->meta_value );
1940 $sql_query_sel[] = $wpdb->prepare(
1941 'SELECT %d, %s, %s',
1942 $new_post_id,
1943 $meta_key,
1944 $meta_value
1945 );
1946 }
1947
1948 $sql_query .= implode( ' UNION ALL ', $sql_query_sel );
1949 $wpdb->query( $sql_query );
1950 }
1951 }
1952 }
1953
1954 if ( ! function_exists( 'learn_press_sort_questions' ) ) {
1955 function learn_press_sort_questions( $types ) {
1956 $user_id = get_current_user_id();
1957 $question_types = get_user_meta( $user_id, '_learn_press_memorize_question_types', true );
1958
1959 if ( ! empty( $question_types ) ) {
1960 $sort = array();
1961 arsort( $types );
1962 $new_types = array();
1963 $ktypes = array_keys( $types );
1964
1965 for ( $i = 0; $i < count( $ktypes ) - 1; $i ++ ) {
1966 $max = $i;
1967
1968 if ( ! isset( $question_types[ $ktypes[ $i ] ] ) ) {
1969 $question_types[ $ktypes[ $i ] ] = 0;
1970 }
1971
1972 for ( $j = $i + 1; $j < count( $ktypes ); $j ++ ) {
1973 if ( isset( $question_types[ $ktypes[ $j ] ], $question_types[ $ktypes[ $max ] ] )
1974 && $question_types[ $ktypes[ $j ] ] > $question_types[ $ktypes[ $max ] ]
1975 ) {
1976 $max = $j;
1977 }
1978 }
1979
1980 $tmp = $ktypes[ $i ];
1981 $ktypes[ $i ] = $ktypes[ $max ];
1982 $ktypes[ $max ] = $tmp;
1983 }
1984
1985 $ktypes = array_flip( $ktypes );
1986 $types = array_merge( $ktypes, $types );
1987 }
1988
1989 return $types;
1990 }
1991 }
1992
1993 if ( ! function_exists( 'learn_press_duplicate_question' ) ) {
1994 function learn_press_duplicate_question( $question_id = null, $quiz_id = null, $args = array() ) {
1995 if ( ! $question_id || ! get_post( $question_id ) ) {
1996 return new WP_Error( sprintf( __( 'Question id %s does not exist.', 'learnpress' ), $question_id ) );
1997 }
1998
1999 if ( $quiz_id && ! get_post( $quiz_id ) ) {
2000 return new WP_Error( sprintf( __( 'Quiz id %s does not exist.', 'learnpress' ), $quiz_id ) );
2001 }
2002
2003 global $wpdb;
2004 $new_question_id = learn_press_duplicate_post( $question_id, $args );
2005
2006 if ( $quiz_id ) {
2007 $sql = $wpdb->prepare(
2008 "
2009 SELECT * FROM $wpdb->learnpress_quiz_questions WHERE quiz_id = %d AND question_id = %d
2010 ",
2011 $quiz_id,
2012 $question_id
2013 );
2014
2015 $quiz_question_data = $wpdb->get_row( $sql );
2016 $max_order = $wpdb->get_var(
2017 $wpdb->prepare(
2018 "SELECT max(question_order) FROM {$wpdb->prefix}learnpress_quiz_questions WHERE quiz_id = %d",
2019 $quiz_id
2020 )
2021 );
2022
2023 if ( $quiz_question_data ) {
2024 $wpdb->insert(
2025 $wpdb->learnpress_quiz_questions,
2026 array(
2027 'quiz_id' => $quiz_id,
2028 'question_id' => $new_question_id,
2029 'question_order' => $max_order + 1,
2030 'params' => $quiz_question_data->params,
2031 ),
2032 array(
2033 '%d',
2034 '%d',
2035 '%d',
2036 '%s',
2037 )
2038 );
2039 }
2040 }
2041
2042 $sql = $wpdb->prepare(
2043 "
2044 SELECT * FROM $wpdb->learnpress_question_answers WHERE question_id = %d
2045 ",
2046 $question_id
2047 );
2048
2049 $question_answers = $wpdb->get_results( $sql );
2050
2051 if ( $question_answers ) {
2052 foreach ( $question_answers as $q_a ) {
2053 $wpdb->insert(
2054 $wpdb->learnpress_question_answers,
2055 array(
2056 'question_id' => $new_question_id,
2057 'title' => $q_a->title,
2058 'value' => $q_a->value,
2059 'is_true' => $q_a->is_true,
2060 'order' => $q_a->order,
2061 ),
2062 array(
2063 '%d',
2064 '%s',
2065 '%s',
2066 )
2067 );
2068 }
2069 }
2070
2071 return $new_question_id;
2072 }
2073 }
2074
2075 if ( ! function_exists( 'learn_press_duplicate_quiz' ) ) {
2076
2077 function learn_press_duplicate_quiz( $quiz_id = null, $args = array() ) {
2078 global $wpdb;
2079
2080 $new_quiz_id = learn_press_duplicate_post( $quiz_id, $args, true );
2081 $quiz = LP_Quiz::get_quiz( $quiz_id );
2082 $questions = $quiz->get_questions();
2083
2084 if ( $questions ) {
2085 $questions = array_keys( $questions );
2086 foreach ( $questions as $question_id ) {
2087 $new_question_id = learn_press_duplicate_post( $question_id );
2088
2089 $sql = $wpdb->prepare(
2090 "
2091 SELECT * FROM $wpdb->learnpress_quiz_questions WHERE quiz_id = %d AND question_id = %d
2092 ",
2093 $quiz_id,
2094 $question_id
2095 );
2096
2097 $quiz_question_data = $wpdb->get_row( $sql );
2098
2099 if ( $quiz_question_data ) {
2100 $wpdb->insert(
2101 $wpdb->learnpress_quiz_questions,
2102 array(
2103 'quiz_id' => $new_quiz_id,
2104 'question_id' => $new_question_id,
2105 'question_order' => $quiz_question_data->question_order,
2106 'params' => $quiz_question_data->params,
2107 ),
2108 array(
2109 '%d',
2110 '%d',
2111 '%d',
2112 '%s',
2113 )
2114 );
2115 }
2116
2117 $sql = $wpdb->prepare(
2118 "
2119 SELECT * FROM $wpdb->learnpress_question_answers WHERE question_id = %d
2120 ",
2121 $question_id
2122 );
2123
2124 $question_answers = $wpdb->get_results( $sql );
2125 if ( $question_answers ) {
2126 foreach ( $question_answers as $q_a ) {
2127 $wpdb->insert(
2128 $wpdb->learnpress_question_answers,
2129 array(
2130 'question_id' => $new_question_id,
2131 'title' => $q_a->title,
2132 'value' => $q_a->value,
2133 'is_true' => $q_a->is_true,
2134 'order' => $q_a->order,
2135 ),
2136 array(
2137 '%d',
2138 '%s',
2139 '%s',
2140 )
2141 );
2142 }
2143 }
2144 }
2145 }
2146
2147 return $new_quiz_id;
2148 }
2149 }
2150
2151 /**
2152 * Get general data to render in chart
2153 *
2154 * @param null $from
2155 * @param null $by
2156 * @param float $time_ago
2157 *
2158 * @return array
2159 */
2160 function learn_press_get_chart_general( $from = null, $by = null, $time_ago = 0 ) {
2161 global $wpdb;
2162
2163 $labels = array();
2164 $datasets = array();
2165
2166 if ( is_null( $from ) ) {
2167 $from = current_time( 'mysql' );
2168 }
2169
2170 if ( is_null( $by ) ) {
2171 $by = 'days';
2172 }
2173
2174 $results = array(
2175 'all' => array(),
2176 'public' => array(),
2177 'pending' => array(),
2178 'free' => array(),
2179 'paid' => array(),
2180 );
2181
2182 $results = array(
2183 'course' => array(),
2184 'lesson' => array(),
2185 'quiz' => array(),
2186 'student' => array(),
2187 'teacher' => array(),
2188 'revenue' => array(),
2189 );
2190
2191 $from_time = is_numeric( $from ) ? $from : strtotime( $from );
2192 $_from = '';
2193 $_to = '';
2194 $_sql_format = '';
2195 $date_format = '';
2196
2197 switch ( $by ) {
2198 case 'days':
2199 $date_format = 'M d Y';
2200 $_from = - $time_ago + 1;
2201 $_from = date( 'Y-m-d', strtotime( "{$_from} {$by}", $from_time ) );
2202 $_to = date( 'Y-m-d', $from_time );
2203 $_sql_format = '%Y-%m-%d';
2204 $_key_format = 'Y-m-d';
2205 break;
2206
2207 case 'months':
2208 $date_format = 'M Y';
2209 $_from = - $time_ago + 1;
2210 $_from = date( 'Y-m-01', strtotime( "{$_from} {$by}", $from_time ) );
2211 $days = date( 't', mktime( 0, 0, 0, date( 'm', $from_time ), 1, date( 'Y', $from_time ) ) );
2212 $_to = date( 'Y-m-' . $days, $from_time );
2213 $_sql_format = '%Y-%m';
2214 $_key_format = 'Y-m';
2215 break;
2216
2217 case 'years':
2218 $date_format = 'Y';
2219 $_from = - $time_ago + 1;
2220 $_from = date( 'Y-01-01', strtotime( "{$_from} {$by}", $from_time ) );
2221 $days = date( 't', mktime( 0, 0, 0, date( 'm', $from_time ), 1, date( 'Y', $from_time ) ) );
2222 $_to = date( 'Y-12-' . $days, $from_time );
2223 $_sql_format = '%Y';
2224 $_key_format = 'Y';
2225 break;
2226
2227 }
2228
2229 $query_where = '';
2230 if ( current_user_can( LP_TEACHER_ROLE ) ) {
2231 $user_id = learn_press_get_current_user_id();
2232 $query_where .= $wpdb->prepare( ' AND c.post_author=%d ', $user_id );
2233 }
2234
2235 $query = $wpdb->prepare(
2236 "
2237 SELECT count(c.ID) as c, DATE_FORMAT( c.post_date, %s) as d
2238 FROM {$wpdb->posts} c
2239 WHERE 1
2240 {$query_where}
2241 AND c.post_status IN('publish', 'pending') AND c.post_type = %s
2242 GROUP BY d
2243 HAVING d BETWEEN %s AND %s
2244 ORDER BY d ASC
2245 ",
2246 $_sql_format,
2247 'lp_course',
2248 $_from,
2249 $_to
2250 );
2251
2252 if ( $_results = $wpdb->get_results( $query ) ) {
2253 foreach ( $_results as $k => $v ) {
2254 $results['all'][ $v->d ] = $v;
2255 }
2256 }
2257
2258 $query = $wpdb->prepare(
2259 "
2260 SELECT count(c.ID) as c, DATE_FORMAT( c.post_date, %s) as d
2261 FROM {$wpdb->posts} c
2262 WHERE 1
2263 {$query_where}
2264 AND c.post_status = %s AND c.post_type = %s
2265 GROUP BY d
2266 HAVING d BETWEEN %s AND %s
2267 ORDER BY d ASC
2268 ",
2269 $_sql_format,
2270 'publish',
2271 'lp_course',
2272 $_from,
2273 $_to
2274 );
2275
2276 if ( $_results = $wpdb->get_results( $query ) ) {
2277 foreach ( $_results as $k => $v ) {
2278 $results['publish'][ $v->d ] = $v;
2279 }
2280 }
2281
2282 $query = $wpdb->prepare(
2283 "
2284 SELECT count(c.ID) as c, DATE_FORMAT( c.post_date, %s) as d
2285 FROM {$wpdb->posts} c
2286 INNER JOIN {$wpdb->postmeta} cm ON cm.post_id = c.ID
2287 WHERE 1
2288 {$query_where}
2289 AND c.post_status = %s AND c.post_type = %s
2290 GROUP BY d
2291 HAVING d BETWEEN %s AND %s
2292 ORDER BY d ASC
2293 ",
2294 $_sql_format,
2295 'publish',
2296 'lp_course',
2297 $_from,
2298 $_to
2299 );
2300
2301 if ( $_results = $wpdb->get_results( $query ) ) {
2302 foreach ( $_results as $k => $v ) {
2303 $results['paid'][ $v->d ] = $v;
2304 }
2305 }
2306
2307 for ( $i = - $time_ago + 1; $i <= 0; $i ++ ) {
2308 $date = strtotime( "$i $by", $from_time );
2309 $labels[] = date( $date_format, $date );
2310 $key = date( $_key_format, $date );
2311
2312 $all = ! empty( $results['all'][ $key ] ) ? $results['all'][ $key ]->c : 0;
2313 $publish = ! empty( $results['publish'][ $key ] ) ? $results['publish'][ $key ]->c : 0;
2314 $paid = ! empty( $results['paid'][ $key ] ) ? $results['paid'][ $key ]->c : 0;
2315
2316 $datasets[0]['data'][] = $all;
2317 $datasets[1]['data'][] = $publish;
2318 $datasets[2]['data'][] = $all - $publish;
2319 $datasets[3]['data'][] = $paid;
2320 $datasets[4]['data'][] = $all - $paid;
2321 }
2322
2323 $dataset_params = array(
2324 array(
2325 'color1' => 'rgba(47, 167, 255, %s)',
2326 'color2' => '#FFF',
2327 'label' => __( 'All', 'learnpress' ),
2328 ),
2329 array(
2330 'color1' => 'rgba(212, 208, 203, %s)',
2331 'color2' => '#FFF',
2332 'label' => __( 'Publish', 'learnpress' ),
2333 ),
2334 array(
2335 'color1' => 'rgba(234, 199, 155, %s)',
2336 'color2' => '#FFF',
2337 'label' => __( 'Pending', 'learnpress' ),
2338 ),
2339 array(
2340 'color1' => 'rgba(234, 199, 155, %s)',
2341 'color2' => '#FFF',
2342 'label' => __( 'Paid', 'learnpress' ),
2343 ),
2344 array(
2345 'color1' => 'rgba(234, 199, 155, %s)',
2346 'color2' => '#FFF',
2347 'label' => __( 'Free', 'learnpress' ),
2348 ),
2349 );
2350
2351 foreach ( $dataset_params as $k => $v ) {
2352 $datasets[ $k ]['fillColor'] = sprintf( $v['color1'], '0.2' );
2353 $datasets[ $k ]['strokeColor'] = sprintf( $v['color1'], '1' );
2354 $datasets[ $k ]['pointColor'] = sprintf( $v['color1'], '1' );
2355 $datasets[ $k ]['pointStrokeColor'] = $v['color2'];
2356 $datasets[ $k ]['pointHighlightFill'] = $v['color2'];
2357 $datasets[ $k ]['pointHighlightStroke'] = sprintf( $v['color1'], '1' );
2358 $datasets[ $k ]['label'] = $v['label'];
2359 }
2360
2361 return array(
2362 'labels' => $labels,
2363 'datasets' => $datasets,
2364 'sql' => $query,
2365 );
2366 }
2367
2368 function learn_press_get_default_section( $section = null ) {
2369 if ( ! $section ) {
2370 $section = new stdClass();
2371 }
2372 foreach (
2373 array(
2374 'section_id' => null,
2375 'section_name' => '',
2376 'section_course_id' => null,
2377 'section_order' => null,
2378 'section_description' => '',
2379 ) as $k => $v
2380 ) {
2381 if ( ! property_exists( $section, $k ) ) {
2382 $section->{$k} = $v;
2383 }
2384 }
2385
2386 return $section;
2387 }
2388
2389 /**
2390 * Display time fields for a post in editing mode.
2391 *
2392 * @param int $edit
2393 * @param int $for_post
2394 * @param int $tab_index
2395 * @param int $multi
2396 */
2397 function learn_press_touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) {
2398 global $wp_locale;
2399 $post = get_post();
2400
2401 if ( $for_post ) {
2402 $edit = ! ( in_array(
2403 $post->post_status,
2404 array(
2405 'draft',
2406 'pending',
2407 )
2408 ) && ( ! $post->post_date_gmt || '0000-00-00 00:00:00' == $post->post_date_gmt ) );
2409 }
2410
2411 $tab_index_attribute = '';
2412 if ( (int) $tab_index > 0 ) {
2413 $tab_index_attribute = " tabindex=\"$tab_index\"";
2414 }
2415
2416 $time_adj = current_time( 'timestamp' );
2417 $post_date = ( $for_post ) ? $post->post_date : get_comment()->comment_date;
2418 $jj = ( $edit ) ? mysql2date( 'd', $post_date, false ) : gmdate( 'd', $time_adj );
2419 $mm = ( $edit ) ? mysql2date( 'm', $post_date, false ) : gmdate( 'm', $time_adj );
2420 $aa = ( $edit ) ? mysql2date( 'Y', $post_date, false ) : gmdate( 'Y', $time_adj );
2421 $hh = ( $edit ) ? mysql2date( 'H', $post_date, false ) : gmdate( 'H', $time_adj );
2422 $mn = ( $edit ) ? mysql2date( 'i', $post_date, false ) : gmdate( 'i', $time_adj );
2423 $ss = ( $edit ) ? mysql2date( 's', $post_date, false ) : gmdate( 's', $time_adj );
2424
2425 $cur_jj = gmdate( 'd', $time_adj );
2426 $cur_mm = gmdate( 'm', $time_adj );
2427 $cur_aa = gmdate( 'Y', $time_adj );
2428 $cur_hh = gmdate( 'H', $time_adj );
2429 $cur_mn = gmdate( 'i', $time_adj );
2430
2431 $map = array(
2432 'mm' => array( $mm, $cur_mm ),
2433 'jj' => array( $jj, $cur_jj ),
2434 'aa' => array( $aa, $cur_aa ),
2435 'hh' => array( $hh, $cur_hh ),
2436 'mn' => array( $mn, $cur_mn ),
2437 );
2438
2439 foreach ( $map as $timeunit => $value ) {
2440 list( $unit, $curr ) = $value;
2441
2442 echo '<input type="hidden" id="hidden_' . $timeunit . '" name="hidden_' . $timeunit . '" value="' . $unit . '" />' . "\n";
2443 $cur_timeunit = 'cur_' . $timeunit;
2444 echo '<input type="hidden" id="' . $cur_timeunit . '" name="' . $cur_timeunit . '" value="' . $curr . '" />' . "\n";
2445 }
2446 }
2447
2448 /**
2449 * Filter to modal search items to void filter the posts by author.
2450 *
2451 * @param int|string $context_id
2452 * @param string $context
2453 *
2454 * @return bool|int|string
2455 * @since 3.0.4
2456 */
2457 function learn_press_modal_search_items_context( $context_id, $context ) {
2458 if ( 'order-items' === $context ) {
2459 $context_id = false;
2460 }
2461
2462 return $context_id;
2463 }
2464
2465 add_filter( 'learn-press/modal-search-items/context-id', 'learn_press_modal_search_items_context', 10, 2 );
2466
2467 /**
2468 * Sync post meta when saving post type.
2469 *
2470 * @param int $post_id
2471 *
2472 * @since 3.2.0
2473 * @editor tungnx
2474 * @modify 1.4.1 - comment - not use
2475 */
2476 /*function learn_press_maybe_sync_data( $post_id ) {
2477 $post_type = get_post_type( $post_id );
2478
2479 switch ( $post_type ) {
2480 case LP_COURSE_CPT:
2481 LP_Repair_Database::instance()->sync_user_courses();
2482 break;
2483 case LP_LESSON_CPT:
2484 break;
2485 case LP_QUIZ_CPT:
2486 break;
2487 default:
2488 }
2489 }*/
2490
2491 //add_action( 'save_post', 'learn_press_maybe_sync_data' );
2492
2493 /**
2494 * Return id of current screen.
2495 *
2496 * @return bool|string
2497 * @since 3.2.6
2498 */
2499 function learn_press_get_screen_id() {
2500 $screen = get_current_screen();
2501 $screen_id = $screen ? $screen->id : false;
2502
2503 return $screen_id;
2504 }
2505
2506 /**
2507 * Check if current screen is a page of LP or
2508 * editing post type of LP such as course, lesson, etc...
2509 *
2510 * @return bool
2511 * @since 3.2.6
2512 */
2513 /*
2514 function learn_press_is_admin_page() {
2515 $screen_id = learn_press_get_screen_id();
2516 $is_learnpress = false;
2517
2518 $post_types = apply_filters(
2519 'learn-press/admin-post-type-pages',
2520 array( LP_COURSE_CPT, LP_QUIZ_CPT, LP_LESSON_CPT, LP_QUESTION_CPT, LP_ORDER_CPT, 'lp_cert', 'lp_assignment' )
2521 );
2522 foreach ( $post_types as $post_type ) {
2523 if ( in_array( $screen_id, array( "edit-{$post_type}", $post_type ) ) ) {
2524 $is_learnpress = true;
2525 break;
2526 }
2527 }
2528
2529 if ( strpos( $screen_id, 'learnpress_page_' ) === 0 ) {
2530 $is_learnpress = true;
2531 }
2532
2533 return apply_filters( 'learn-press/is-admin-page', $is_learnpress, $screen_id );
2534 }*/
2535
2536 function learn_press_get_orders_status_chart_data() {
2537 $data = array(
2538 'type' => 'pie',
2539 'data' => array(
2540 'labels' => array(),
2541 'datasets' => array(
2542 array(
2543 'label' => '# of Votes',
2544 'data' => array(),
2545 'backgroundColor' => array(
2546 'rgba(54, 162, 235, 0.2)',
2547 'rgba(255, 206, 86, 0.2)',
2548 'rgba(75, 192, 192, 0.2)',
2549 'rgba(153, 102, 255, 0.2)',
2550 'rgba(255, 159, 64, 0.2)',
2551 ),
2552 'borderColor' => array(
2553 'rgba(54, 162, 235, 1)',
2554 'rgba(255, 206, 86, 1)',
2555 'rgba(75, 192, 192, 1)',
2556 'rgba(153, 102, 255, 1)',
2557 'rgba(255, 159, 64, 1)',
2558 ),
2559 'borderWidth' => 1,
2560 ),
2561 ),
2562 ),
2563 'options' => array(
2564 'scales' => array(
2565 'yAxes' => array(
2566 array(
2567 'ticks' => array(
2568 'beginAtZero' => true,
2569 ),
2570 ),
2571 ),
2572 ),
2573 ),
2574 );
2575
2576 $order_statuses = learn_press_get_order_statuses( true, true );
2577 $specific_statuses = array( 'lp-completed', 'lp-failed' );
2578
2579 foreach ( $order_statuses as $status ) {
2580 if ( ! in_array( $status, $specific_statuses ) ) {
2581 $specific_statuses[] = $status;
2582 }
2583 }
2584
2585 $labels = learn_press_get_order_statuses();
2586 $counts = learn_press_count_orders( array( 'status' => $specific_statuses ) );
2587
2588 foreach ( $counts as $k => $v ) {
2589 $data['data']['labels'][] = isset( $labels[ $k ] ) ? $labels[ $k ] : 'Untitled';
2590 $data['data']['datasets'][0]['data'][] = $v;
2591 }
2592
2593 return $data;
2594 }
2595
2596 require_once 'class-lp-post-type-actions.php';
2597