PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.2
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.7.3.2, at inc/admin/lp-admin-functions.php

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