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

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

918 lines 25.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @author ThimPress
4 * @package LearnPress/Admin/Classes
5 * @version 1.0.1
6 */
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly
9 }
10
11 if ( ! class_exists( 'LP_Admin' ) ) {
12 /**
13 * Class LP_Admin
14 */
15 class LP_Admin {
16 protected static $instance;
17
18 /**
19 * @var array
20 */
21 protected $_static_pages = false;
22
23 /**
24 * Constructor
25 */
26 protected function __construct() {
27 $this->includes();
28 add_action( 'delete_user', array( $this, 'delete_user_data' ) );
29 add_action( 'delete_user_form', array( $this, 'delete_user_form' ) );
30 add_action( 'wp_ajax_learn_press_rated', array( $this, 'rated' ) );
31 add_action( 'admin_notices', array( $this, 'admin_notices' ) );
32 add_action( 'admin_enqueue_scripts', array( $this, 'load_modal' ) );
33 add_filter( 'admin_body_class', array( $this, 'body_class' ) );
34 add_filter( 'manage_users_custom_column', array( $this, 'users_custom_column' ), 10, 3 );
35 add_filter( 'manage_pages_columns', array( $this, 'page_columns_head' ) );
36 add_filter( 'manage_pages_custom_column', array( $this, 'page_columns_content' ), 10, 2 );
37 add_filter( 'views_edit-page', array( $this, 'views_pages' ), 10 );
38 add_filter( 'views_users', array( $this, 'views_users' ), 10, 1 );
39 add_filter( 'user_row_actions', array( $this, 'user_row_actions' ), 10, 2 );
40 add_filter( 'get_pages', array( $this, 'add_empty_page' ), 1000, 2 );
41 add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ), 1 );
42 add_filter( 'views_plugins', array( $this, 'views_plugins' ) );
43
44 LP_Request::register( 'lp-action', array( $this, 'filter_users' ) );
45
46 add_filter( 'learn-press/modal-search-items-args', array( $this, 'filter_modal_search' ) );
47
48 add_filter(
49 'learn-press/dismissed-notice-response',
50 array(
51 $this,
52 'on_dismissed_notice_response',
53 ),
54 10,
55 2
56 );
57
58 // get list items course of user | tungnx
59 add_action( 'pre_get_posts', array( $this, 'get_course_items_of_user_backend' ), 10 );
60 add_action( 'pre_get_posts', array( $this, 'get_pages_of_lp' ), 10 );
61
62 // Set link item course when edit on Backend | tungnx
63 add_filter( 'get_sample_permalink_html', array( $this, 'lp_course_set_link_item_backend' ), 10, 5 );
64 }
65
66 /**
67 * @since 3.2.6
68 */
69 public function load_modal() {
70 if ( in_array( get_post_type(), array( LP_COURSE_CPT, LP_QUIZ_CPT, LP_QUESTION_CPT, LP_ORDER_CPT ) ) ) {
71 LP_Modal_Search_Items::instance();
72 }
73
74 if ( in_array( get_post_type(), array( LP_ORDER_CPT ) ) ) {
75 LP_Modal_Search_Users::instance();
76 }
77 }
78
79 /**
80 * @param $options
81 *
82 * @return array
83 */
84 public function filter_modal_search( $options ) {
85 $options = wp_parse_args( array( 'title' => __( 'Available Courses', 'learnpress' ) ), $options );
86
87 return $options;
88 }
89
90 public function add_empty_page( $pages, $args ) {
91 if ( empty( $pages ) && ! empty( $args['class'] ) && strpos( $args['class'], 'lp-list-pages' ) !== false ) {
92 $empty_page = get_default_post_to_edit( 'page' );
93 $empty_page->ID = '00000';
94 $pages[] = $empty_page;
95 }
96
97 return $pages;
98 }
99
100 /**
101 * Add 'LearnPress' tab into views of plugins manage.
102 *
103 * @param array $views
104 *
105 * @return array
106 * @since 3.0.0
107 */
108 public function views_plugins( $views ) {
109 global $s;
110
111 $search = $this->get_addons();
112 $count_activated = 0;
113
114 $active_plugins = get_option( 'active_plugins' );
115
116 if ( $active_plugins ) {
117 if ( $search ) {
118 foreach ( $search as $k => $v ) {
119 if ( in_array( $k, $active_plugins ) ) {
120 $count_activated ++;
121 }
122 }
123 }
124 }
125
126 if ( $s && false !== stripos( $s, 'learnpress' ) ) {
127 $views['learnpress'] = sprintf(
128 '<a href="%s" class="current">%s <span class="count">(%d/%d)</span></a>',
129 admin_url( 'plugins.php?s=learnpress' ),
130 __( 'LearnPress', 'learnpress' ),
131 $count_activated,
132 sizeof( $search )
133 );
134 } else {
135 $views['learnpress'] = sprintf(
136 '<a href="%s">%s <span class="count">(%d/%d)</span></a>',
137 admin_url( 'plugins.php?s=learnpress' ),
138 __( 'LearnPress', 'learnpress' ),
139 $count_activated,
140 sizeof( $search )
141 );
142 }
143
144 return $views;
145 }
146
147 public function get_addons() {
148 $all_plugins = apply_filters( 'all_plugins', get_plugins() );
149
150 return array_filter( $all_plugins, array( $this, '_search_callback' ) );
151 }
152
153 /**
154 * Callback function for searching plugins have 'learnpress' inside.
155 *
156 * @param array $plugin
157 *
158 * @return bool
159 * @since 3.0.0
160 */
161 public function _search_callback( $plugin ) {
162 foreach ( $plugin as $value ) {
163 if ( is_string( $value ) && false !== stripos( strip_tags( $value ), 'learnpress' ) ) {
164 return true;
165 }
166 }
167
168 return false;
169 }
170
171 /**
172 * Check if a page is set for WooCommerce.
173 *
174 * @param int $id
175 *
176 * @return bool
177 */
178 protected function _is_wc_page( $id ) {
179 if ( class_exists( 'WooCommerce' ) ) {
180 if ( ! class_exists( 'WC_Admin_Post_Types' ) ) {
181 include_once dirname( WC_PLUGIN_FILE ) . '/includes/admin/class-wc-admin-post-types.php';
182 }
183
184 $wc_admin_post_types = new WC_Admin_Post_Types();
185 if ( is_callable( array( $wc_admin_post_types, 'add_display_post_states' ) ) ) {
186 $a = $wc_admin_post_types->add_display_post_states( array(), get_post( $id ) );
187 } else {
188 $a = $this->wc_add_display_post_states( array(), get_post( $id ) );
189 }
190 $wc_pages = array(
191 'wc_page_for_shop',
192 'wc_page_for_cart',
193 'wc_page_for_checkout',
194 'wc_page_for_myaccount',
195 'wc_page_for_terms',
196 );
197 foreach ( $wc_pages as $for_page ) {
198 if ( isset( $a[ $for_page ] ) ) {
199 return $a[ $for_page ];
200 }
201 }
202 }
203
204 return false;
205 }
206
207 public function wc_add_display_post_states( $post_states, $post ) {
208 if ( wc_get_page_id( 'shop' ) === $post->ID ) {
209 $post_states['wc_page_for_shop'] = __( 'Shop Page', 'learnpress' );
210 }
211
212 if ( wc_get_page_id( 'cart' ) === $post->ID ) {
213 $post_states['wc_page_for_cart'] = __( 'Cart Page', 'learnpress' );
214 }
215
216 if ( wc_get_page_id( 'checkout' ) === $post->ID ) {
217 $post_states['wc_page_for_checkout'] = __( 'Checkout Page', 'learnpress' );
218 }
219
220 if ( wc_get_page_id( 'myaccount' ) === $post->ID ) {
221 $post_states['wc_page_for_myaccount'] = __( 'My Account Page', 'learnpress' );
222 }
223
224 if ( wc_get_page_id( 'terms' ) === $post->ID ) {
225 $post_states['wc_page_for_terms'] = __( 'Terms and Conditions Page', 'learnpress' );
226 }
227
228 return $post_states;
229 }
230
231 /**
232 * Check if a page is set for Paid Membership Pro.
233 *
234 * @param int $id
235 *
236 * @return bool|mixed
237 */
238 protected function _is_pmpro_page( $id ) {
239 global $pmpro_pages;
240 if ( $pmpro_pages ) {
241 $pages = array(
242 'account' => __( 'Account', 'learnpress' ),
243 'billing' => __( 'Billing', 'learnpress' ),
244 'cancel' => __( 'Cancel', 'learnpress' ),
245 'checkout' => __( 'Checkout', 'learnpress' ),
246 'confirmation' => __( 'Confirmation', 'learnpress' ),
247 'invoice' => __( 'Invoice', 'learnpress' ),
248 'levels' => __( 'Levels', 'learnpress' ),
249 );
250
251 foreach ( $pages as $name => $text ) {
252 if ( $pmpro_pages[ $name ] == $id ) {
253 return $text;
254 }
255 }
256 }
257
258 return false;
259 }
260
261 /**
262 * Check if a page is set for Paid Membership Pro.
263 *
264 * @param int $id
265 *
266 * @return bool|mixed
267 */
268 protected function _is_bp_page( $id ) {
269 if ( function_exists( 'buddypress' ) ) {
270 $bp_pages = get_option( 'bp-pages' );
271 if ( ! $bp_pages ) {
272 return false;
273 }
274
275 $pages = array(
276 'members' => __( 'Members', 'learnpress' ),
277 'activity' => __( 'Activity', 'learnpress' ),
278 'register' => __( 'Register', 'learnpress' ),
279 'activate' => __( 'Activate', 'learnpress' ),
280 );
281
282 foreach ( $pages as $name => $text ) {
283 if ( isset( $bp_pages[ $name ] ) && $bp_pages[ $name ] == $id ) {
284 return $text;
285 }
286 }
287 }
288
289 return false;
290 }
291
292 /**
293 * @param string $plugin
294 *
295 * @return array|bool
296 */
297 protected function _get_static_pages( $plugin = '' ) {
298 if ( false === $this->_static_pages ) {
299 $this->_static_pages = array(
300 'learnpress' => array(),
301 'WooCommerce' => array(),
302 'Paid Membership Pro' => array(),
303 'BuddyPress' => array(),
304 );
305
306 $all_pages = array(
307 'courses' => __( 'Courses', 'learnpress' ),
308 'profile' => __( 'Profile', 'learnpress' ),
309 'checkout' => __( 'Checkout', 'learnpress' ),
310 'become_a_teacher' => __( 'Become a Teacher', 'learnpress' ),
311 );
312
313 foreach ( $all_pages as $name => $title ) {
314 $page_id = learn_press_get_page_id( $name );
315
316 if ( $page_id && 'publish' === get_post_status( $page_id ) ) {
317 $this->_static_pages['learnpress'][ $page_id ] = $title;
318
319 $for_page = $this->_is_wc_page( $page_id );
320 if ( $for_page ) {
321 $this->_static_pages['WooCommerce'][ $page_id ] = $for_page;
322 }
323
324 $for_page = $this->_is_pmpro_page( $page_id );
325 if ( $for_page ) {
326 $this->_static_pages['Paid Membership Pro'][ $page_id ] = $for_page;
327 }
328
329 $for_page = $this->_is_bp_page( $page_id );
330 if ( $for_page ) {
331 $this->_static_pages['BuddyPress'][ $page_id ] = $for_page;
332 }
333 }
334 }
335 }
336
337 return $plugin ? ( ! empty( $this->_static_pages[ $plugin ] ) ? $this->_static_pages[ $plugin ] : false ) : $this->_static_pages;
338 }
339
340 /**
341 * Add new column to WP Pages manage to show what page is assigned to.
342 *
343 * @param array $columns
344 *
345 * @return array
346 */
347 public function page_columns_head( $columns ) {
348
349 $_columns = $columns;
350 $columns = array();
351
352 foreach ( $_columns as $name => $text ) {
353 if ( $name === 'date' ) {
354 $columns['lp-page'] = __( 'LearnPress Page', 'learnpress' );
355 }
356 $columns[ $name ] = $text;
357 }
358
359 return $columns;
360 }
361
362 /**
363 * Display the page is assigned to LP Page.
364 *
365 * @param string $column_name
366 * @param int $post
367 */
368 public function page_columns_content( $column_name, $post ) {
369 $pages = $this->_get_static_pages();
370 switch ( $column_name ) {
371 case 'lp-page':
372 if ( ! empty( $pages['learnpress'][ $post ] ) ) {
373 echo wp_kses_post( $pages['learnpress'][ $post ] );
374 }
375
376 foreach ( $pages as $plugin => $plugin_pages ) {
377 if ( $plugin === 'learnpress' ) {
378 continue;
379 }
380
381 if ( ! empty( $pages[ $plugin ][ $post ] ) ) {
382 echo sprintf(
383 '<p class="for-plugin-page">(%s - %s)</p>',
384 $plugin,
385 $pages[ $plugin ][ $post ]
386 );
387 }
388 }
389 }
390 }
391
392 /**
393 * @param $actions
394 *
395 * @return mixed
396 */
397 public function views_pages( $actions ) {
398 $this->_get_static_pages();
399
400 $pages = $this->_get_static_pages( 'learnpress' );
401
402 if ( $pages ) {
403 $text = sprintf( __( 'LearnPress Pages (%d)', 'learnpress' ), sizeof( $pages ) );
404 if ( 'yes' !== LP_Request::get( 'lp-page' ) ) {
405 $actions['lp-page'] = sprintf(
406 '<a href="%s">%s</a>',
407 admin_url( 'edit.php?post_type=page&lp-page=yes' ),
408 $text
409 );
410 } else {
411 $actions['lp-page'] = $text;
412 }
413 }
414
415 return $actions;
416 }
417
418 /**
419 * Get pages set by LP with param url lp-page=yes
420 *
421 * @param WP_Query $q
422 *
423 * @return mixed
424 */
425 public function get_pages_of_lp( $q ) {
426 if ( 'page' == LP_Request::get( 'post_type' ) && 'yes' == LP_Request::get( 'lp-page' ) ) {
427 $ids = $this->_get_static_pages( 'learnpress' );
428
429 if ( ! empty( $ids ) ) {
430 $ids = array_keys( $ids );
431 $q->set( 'post__in', $ids );
432 }
433 }
434
435 return $q;
436 }
437
438 /**
439 * Add actions to users list
440 *
441 * @param array $actions
442 * @param WP_User $user
443 *
444 * @return mixed
445 */
446 public function user_row_actions( $actions, $user ) {
447 $pending_request = LP_User_Factory::get_pending_requests();
448 if ( LP_Request::get_string( 'lp-action' ) == 'pending-request' && $pending_request ) {
449 $actions = array();
450 if ( in_array( $user->ID, $pending_request ) ) {
451 $actions['accept'] = sprintf(
452 '<a href="' . admin_url( 'users.php?lp-action=accept-request&user_id=' . $user->ID ) . '">%s</a>',
453 _x( 'Accept', 'pending-request', 'learnpress' )
454 );
455 $actions['delete deny'] = sprintf(
456 '<a class="submitdelete" href="' . admin_url( 'users.php?lp-action=deny-request&user_id=' . $user->ID ) . '">%s</a>',
457 _x( 'Deny', 'pending-request', 'learnpress' )
458 );
459 }
460 }
461
462 return $actions;
463 }
464
465 /**
466 * Filter user by custom param
467 *
468 * @param string $action
469 */
470 public function filter_users( $action ) {
471 if ( ! current_user_can( 'administrator' ) ) {
472 return;
473 }
474
475 $user_id = LP_Request::get_int( 'user_id' );
476
477 if ( ! $user_id || ! get_user_by( 'id', $user_id ) ) {
478 return;
479 }
480
481 $user_data = get_userdata( $user_id );
482
483 if ( in_array( $action, array( 'accept-request', 'deny-request' ) ) ) {
484
485 delete_user_meta( $user_id, '_requested_become_teacher' );
486
487 switch ( $action ) {
488 case 'accept-request':
489 $be_teacher = new WP_User( $user_id );
490 $be_teacher->set_role( LP_TEACHER_ROLE );
491
492 do_action( 'learn-press/user-become-a-teacher-accept', $user_data->user_email );
493 wp_redirect( admin_url( 'users.php?lp-action=accepted-request&user_id=' . $user_id ) );
494 exit();
495 case 'deny-request':
496 do_action( 'learn-press/user-become-a-teacher-deny', $user_data->user_email );
497 wp_redirect( admin_url( 'users.php?lp-action=denied-request&user_id=' . $user_id ) );
498 exit();
499 }
500 }
501 }
502
503 public function users_custom_column( $content, $column_name, $user_id ) {
504
505 }
506
507 /**
508 * Add new view to users views for filtering user by "pending request" of "become a teacher".
509 *
510 * @param array $views
511 *
512 * @return mixed
513 */
514 public function views_users( $views ) {
515 $pending_request = LP_User_Factory::get_pending_requests();
516
517 if ( $pending_request ) {
518 if ( LP_Request::get_string( 'lp-action' ) == 'pending-request' ) {
519 $class = ' class="current"';
520 foreach ( $views as $k => $view ) {
521 $views[ $k ] = preg_replace( '!class="current"!', '', $view );
522 }
523 } else {
524 $class = '';
525 }
526 $views['pending-request'] = '<a href="' . admin_url( 'users.php?lp-action=pending-request' ) . '"' . $class . '>' . sprintf(
527 __(
528 'Pending Request %s',
529 'learnpress'
530 ),
531 '<span class="count">(' . count( $pending_request ) . ')</span>'
532 ) . '</a>';
533 }
534
535 return $views;
536 }
537
538 /**
539 * Custom admin body classes.
540 *
541 * @param array $classes
542 *
543 * @return array|string
544 */
545 public function body_class( $classes ) {
546 $post_type = get_post_type();
547
548 if ( preg_match( '~^lp_~', $post_type ) ) {
549 if ( $classes ) {
550 $classes = explode( ' ', $classes );
551 } else {
552 $classes = array();
553 }
554
555 $classes[] = 'learnpress';
556 $classes = array_filter( $classes );
557 $classes = array_unique( $classes );
558 $classes = join( ' ', $classes );
559 }
560
561 return $classes;
562 }
563
564 /**
565 * Display notices on Backend.
566 */
567 public function admin_notices() {
568 if ( ! current_user_can( ADMIN_ROLE ) ) {
569 return;
570 }
571
572 // Show template file templates override.
573 $page = LP_Request::get_param( 'page' );
574 $tab = LP_Request::get_param( 'tab' );
575 if ( $page == 'learn-press-tools' && $tab == 'templates' ) {
576 if ( LP_Outdated_Template_Helper::detect_outdated_template() ) {
577 learn_press_admin_view( 'html-admin-notice-templates' );
578 }
579 }
580
581 // Show notice permalink structure wrong.
582 if ( ! get_option( 'permalink_structure' ) ) {
583 ?>
584 <div class="notice notice-error">
585 <p>
586 <?php
587 echo sprintf(
588 'LearnPress requires permalink option <strong>Post name</strong> is enabled. Please enable it <a href="%s">here</a> to ensure that all functions work properly.',
589 admin_url( 'options-permalink.php' )
590 )
591 ?>
592 </p>
593 </div>
594 <?php
595 }
596
597 // Show notice setup wizard.
598 if ( ! get_option( 'learn_press_setup_wizard_completed', false ) ) {
599 learn_press_admin_view( 'setup/notice-setup' );
600 }
601
602 // Request accept/denied user can become a teacher.
603 $action_become_teacher = LP_Request::get_param( 'lp-action' );
604 $user_id = LP_Request::get_param( 'user_id', 0, 'int' );
605 $type_action_become_teacher = array( 'accepted-request', 'denied-request' );
606 if ( in_array( $action_become_teacher, $type_action_become_teacher ) && $user_id && get_user_by( 'id', $user_id ) ) {
607 ?>
608 <div class="updated notice">
609 <p>
610 <?php
611 echo sprintf(
612 __( 'A user has %s to become a teacher.', 'learnpress' ),
613 $action_become_teacher == 'accepted-request' ? 'accepted' : 'denied'
614 );
615 ?>
616 </p>
617 </div>
618 <?php
619 }
620
621 // Show notice check wp_remote_post (background) runs.
622 ?>
623 <div class="notice-check-run-background-available"></div>
624 <?php
625 }
626
627 /**
628 * Update option data user rated.
629 */
630 public function rated() {
631 update_option( 'learn_press_message_user_rated', 'yes' );
632 die();
633 }
634
635 /**
636 * Admin footer add review.
637 *
638 * @param $footer_text
639 *
640 * @return string
641 */
642 public function admin_footer_text( $footer_text ) {
643 $current_screen = get_current_screen();
644 $pages = learn_press_get_screens();
645
646 if ( isset( $current_screen->id ) && apply_filters(
647 'learn_press_display_admin_footer_text',
648 in_array( $current_screen->id, $pages )
649 ) ) {
650 if ( ! get_option( 'learn_press_message_user_rated' ) ) {
651 $footer_text = sprintf(
652 __(
653 'If you like <strong>LearnPress</strong> please leave us a %1$s&#9733;&#9733;&#9733;&#9733;&#9733;%2$s rating. A huge thanks from the LearnPress team for your generosity.',
654 'learnpress'
655 ),
656 '<a href="https://wordpress.org/support/plugin/learnpress/reviews/?filter=5#postform" target="_blank" class="lp-rating-link" data-rated="' . esc_attr__(
657 'Thanks :)',
658 'learnpress'
659 ) . '">',
660 '</a>'
661 );
662
663 ob_start();
664 ?>
665
666 <script>
667 jQuery(function ($) {
668 var $ratingLink = $('a.lp-rating-link').click(function (e) {
669 $.ajax({
670 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
671 data: {
672 action: 'learn_press_rated'
673 },
674 success: function () {
675 $ratingLink.parent().html($ratingLink.data('rated'))
676 }
677 })
678 })
679 })
680 </script>
681
682 <?php
683 echo ob_get_clean();
684 }
685 }
686
687 return $footer_text;
688 }
689
690 function delete_user_form() {
691 // What should be displayed here?
692 }
693
694 /**
695 * Delete records related user being deleted in other tables
696 *
697 * @param int $user_id
698 */
699 function delete_user_data( $user_id ) {
700 learn_press_delete_user_data( $user_id );
701 }
702
703 /**
704 * Send data to join newsletter or dismiss.
705 *
706 * @param array $data
707 * @param string $notice
708 *
709 * @return array
710 * @since 3.0.10
711 */
712 public function on_dismissed_notice_response( $data, $notice ) {
713 switch ( $notice ) {
714 case 'skip-setup-wizard':
715 delete_option( 'learn_press_install' );
716 break;
717 case 'newsletter-button':
718 $context = LP_Request::get_string( 'context' );
719 if ( ! $context || $context != 'newsletter' ) {
720 break;
721 }
722
723 $user = learn_press_get_current_user();
724 if ( ! $user || $user->get_email() == '' ) {
725 $data['error'] = __( 'Failed while joining the newsletter! Please try again!', 'learnpress' );
726 }
727
728 $url = 'https://thimpress.com/mailster/subscribe';
729 $response = wp_remote_post(
730 $url,
731 array(
732 'method' => 'POST',
733 'timeout' => 45,
734 'redirection' => 5,
735 'httpversion' => '1.0',
736 'blocking' => true,
737 'headers' => array(),
738 'body' => array(
739 '_referer' => 'extern',
740 '_nonce' => '4b266caf7b',
741 'formid' => '19',
742 'email' => $user->get_email(),
743 'website' => site_url(),
744 ),
745 'cookies' => array(),
746 )
747 );
748
749 if ( is_wp_error( $response ) ) {
750 $error_message = $response->get_error_message();
751 $data['message'] = __( 'Something went wrong: ', 'learnpress' ) . $error_message;
752 } else {
753 $data['message'] = __(
754 'Thank you for subscribing! Please check and click the confirmation link from the email that we\'ve just sent to your inbox.',
755 'learnpress'
756 );
757 }
758 }
759
760 return $data;
761 }
762
763 /**
764 * Include all classes and functions used for admin
765 */
766 public function includes() {
767 // Common function used in admin
768 include_once 'lp-admin-functions.php';
769 include_once 'lp-admin-actions.php';
770 require_once LP_PLUGIN_PATH . 'inc/background-process/class-lp-background-query-items.php';
771 include_once 'class-lp-admin-assets.php';
772 include_once 'class-lp-admin-dashboard.php';
773 // include_once 'class-lp-admin-tools.php';
774 include_once 'class-lp-admin-ajax.php';
775 include_once 'editor/class-lp-admin-editor.php';
776 include_once 'class-lp-admin-menu.php';
777 include_once 'helpers/class-lp-outdated-template-helper.php';
778 include_once 'helpers/class-lp-plugins-helper.php';
779 include_once 'class-lp-modal-search-items.php';
780 include_once 'class-lp-modal-search-users.php';
781 include_once 'class-lp-setup-wizard.php';
782 // include_once 'class-lp-updater.php';
783 include_once 'class-lp-install-sample-data.php';
784 include_once 'class-lp-reset-data.php';
785 include_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/course/settings.php';
786 include_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/quiz/settings.php';
787 include_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/lesson/settings.php';
788 include_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/question/settings.php';
789 }
790
791 /**
792 * Get courses, item's courses on Backend page post_type
793 *
794 * @param WP_Query $query
795 */
796 public function get_course_items_of_user_backend( WP_Query $query ) {
797 if ( ! $query->is_main_query() ) {
798 return;
799 }
800
801 global $post_type, $pagenow;
802
803 if ( current_user_can( ADMIN_ROLE ) ) {
804 return;
805 }
806
807 if ( ! current_user_can( LP_TEACHER_ROLE ) || ( $pagenow != 'edit.php' ) ) {
808 return;
809 }
810
811 $post_type_valid = apply_filters(
812 'learn-press/filter-user-access-types',
813 array( LP_COURSE_CPT, LP_LESSON_CPT, LP_QUIZ_CPT, LP_QUESTION_CPT )
814 );
815
816 if ( ! in_array( $post_type, $post_type_valid ) ) {
817 return;
818 }
819
820 // $query->set( 'author', get_current_user_id() );
821
822 $query = apply_filters( 'learnpress/get-post-type-lp-on-backend', $query );
823
824 //add_filter( 'views_edit-' . $post_type . '', '_learn_press_restrict_view_items', 10 );
825 remove_filter( 'pre_get_posts', array( $this, 'get_course_items_of_user_backend' ), 10 );
826 }
827
828 /**
829 * Set link item of course when edit item on Backend
830 *
831 * @param string $post_link
832 * @param int $post_id
833 * @param string $new_title
834 * @param string $new_slug
835 * @param WP_Post|null $post
836 *
837 * @return string
838 * @author tungnx
839 * @since 3.2.7.5
840 * @version 4.0.1
841 */
842 public function lp_course_set_link_item_backend( $post_link = '', $post_id = 0, $new_title = '', $new_slug = '', $post = null ) {
843 if ( ! in_array( $post->post_type, learn_press_get_course_item_types() ) ) {
844 return $post_link;
845 }
846
847 try {
848 $course_id_of_item = LP_Course_DB::getInstance()->get_course_by_item_id( $post->ID );
849
850 if ( $course_id_of_item ) {
851 $course = learn_press_get_course( $course_id_of_item );
852
853 if ( $course ) {
854 $link_item = $course->get_item_link( $post->ID );
855
856 $post_slug = $post->post_name;
857 $link_item_edit_slug = preg_replace( '/' . $post_slug . '$/', '', $link_item );
858
859 // For update new slug
860 if ( $new_slug ) {
861 $post_slug = $new_slug;
862 }
863
864 $post_link = '<strong>Permalink: </strong>';
865 $post_link .= '<span id="sample-permalink">';
866 $post_link .= '<a href="' . $link_item . '">' . $link_item_edit_slug . '<span id="editable-post-name">' . $post_slug . '</span>/</a>';
867 $post_link .= '</span>';
868 $post_link .= '&lrm;<span id="edit-slug-buttons">';
869 $post_link .= '<button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="Edit permalink">Edit</button>';
870 $post_link .= '</span>';
871 $post_link .= '<span id="editable-post-name-full">' . $post_slug . '</span>';
872 }
873 } else {
874 // $post_link_preview = sprintf( '<a class="button" href="%s" target="_blank">%s</a>', learn_press_get_preview_url( $post_id ), __( 'Preview', 'learnpress' ) );
875 $post_link_message = '<span>' . __(
876 'Permalink is only available if the item is already assigned to a course.',
877 'learnpress'
878 ) . '</span>';
879 $post_link = sprintf( '<div id="learn-press-box-edit-slug">%s</div>', $post_link_message );
880 }
881 } catch ( Throwable $e ) {
882 error_log( $e->getMessage() );
883 }
884
885 return $post_link;
886 }
887
888 /**
889 * @return false|string
890 * @since 3.2.8
891 * @editor tungnx
892 */
893 public function get_screen_id() {
894 global $current_screen;
895
896 return $current_screen ? $current_screen->id : false;
897 }
898
899 /**
900 * Get single instance of self
901 *
902 * @return bool|LP_Admin
903 * @return bool|LP_Admin
904 * @since 3.0.0
905 *
906 */
907 public static function instance() {
908 if ( ! self::$instance ) {
909 self::$instance = new self();
910 }
911
912 return self::$instance;
913 }
914 }
915 }
916
917 return LP_Admin::instance();
918