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