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

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

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