PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
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 4.2.1 All 138 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.7, at inc/admin/lp-admin-functions.php

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