| 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 |
// Show template file templates override. |
| 569 |
$page = LP_Request::get_param( 'page' ); |
| 570 |
$tab = LP_Request::get_param( 'tab' ); |
| 571 |
if ( $page == 'learn-press-tools' && $tab == 'templates' ) { |
| 572 |
if ( LP_Outdated_Template_Helper::detect_outdated_template() ) { |
| 573 |
learn_press_admin_view( 'html-admin-notice-templates' ); |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
// Request accept/denied user can become a teacher. |
| 578 |
$action_become_teacher = LP_Request::get_param( 'lp-action' ); |
| 579 |
$user_id = LP_Request::get_param( 'user_id', 0, 'int' ); |
| 580 |
$type_action_become_teacher = array( 'accepted-request', 'denied-request' ); |
| 581 |
if ( in_array( $action_become_teacher, $type_action_become_teacher ) && $user_id && get_user_by( 'id', $user_id ) ) { |
| 582 |
?> |
| 583 |
<div class="updated notice"> |
| 584 |
<p> |
| 585 |
<?php |
| 586 |
echo sprintf( |
| 587 |
__( 'A user has %s to become a teacher.', 'learnpress' ), |
| 588 |
$action_become_teacher == 'accepted-request' ? 'accepted' : 'denied' |
| 589 |
); |
| 590 |
?> |
| 591 |
</p> |
| 592 |
</div> |
| 593 |
<?php |
| 594 |
} |
| 595 |
|
| 596 |
learn_press_admin_view( 'admin-notices.php', [], true ); |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* Update option data user rated. |
| 601 |
*/ |
| 602 |
public function rated() { |
| 603 |
update_option( 'learn_press_message_user_rated', 'yes' ); |
| 604 |
die(); |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* Admin footer add review. |
| 609 |
* |
| 610 |
* @param $footer_text |
| 611 |
* |
| 612 |
* @return string |
| 613 |
*/ |
| 614 |
public function admin_footer_text( $footer_text ) { |
| 615 |
$current_screen = get_current_screen(); |
| 616 |
$pages = learn_press_get_screens(); |
| 617 |
|
| 618 |
if ( isset( $current_screen->id ) && apply_filters( |
| 619 |
'learn_press_display_admin_footer_text', |
| 620 |
in_array( $current_screen->id, $pages ) |
| 621 |
) ) { |
| 622 |
if ( ! get_option( 'learn_press_message_user_rated' ) ) { |
| 623 |
$footer_text = sprintf( |
| 624 |
__( |
| 625 |
'If you like <strong>LearnPress</strong> please leave us a %1$s★★★★★%2$s rating. A huge thanks from the LearnPress team for your generosity.', |
| 626 |
'learnpress' |
| 627 |
), |
| 628 |
'<a href="https://wordpress.org/support/plugin/learnpress/reviews/?filter=5#postform" target="_blank" class="lp-rating-link" data-rated="' . esc_attr__( |
| 629 |
'Thanks :)', |
| 630 |
'learnpress' |
| 631 |
) . '">', |
| 632 |
'</a>' |
| 633 |
); |
| 634 |
|
| 635 |
ob_start(); |
| 636 |
?> |
| 637 |
|
| 638 |
<script> |
| 639 |
jQuery(function ($) { |
| 640 |
var $ratingLink = $('a.lp-rating-link').click(function (e) { |
| 641 |
$.ajax({ |
| 642 |
url: '<?php echo admin_url( 'admin-ajax.php' ); ?>', |
| 643 |
data: { |
| 644 |
action: 'learn_press_rated' |
| 645 |
}, |
| 646 |
success: function () { |
| 647 |
$ratingLink.parent().html($ratingLink.data('rated')) |
| 648 |
} |
| 649 |
}) |
| 650 |
}) |
| 651 |
}) |
| 652 |
</script> |
| 653 |
|
| 654 |
<?php |
| 655 |
echo ob_get_clean(); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
return $footer_text; |
| 660 |
} |
| 661 |
|
| 662 |
function delete_user_form() { |
| 663 |
// What should be displayed here? |
| 664 |
} |
| 665 |
|
| 666 |
/** |
| 667 |
* Delete records related user being deleted in other tables |
| 668 |
* |
| 669 |
* @param int $user_id |
| 670 |
*/ |
| 671 |
function delete_user_data( $user_id ) { |
| 672 |
learn_press_delete_user_data( $user_id ); |
| 673 |
} |
| 674 |
|
| 675 |
/** |
| 676 |
* Send data to join newsletter or dismiss. |
| 677 |
* |
| 678 |
* @param array $data |
| 679 |
* @param string $notice |
| 680 |
* |
| 681 |
* @return array |
| 682 |
* @since 3.0.10 |
| 683 |
*/ |
| 684 |
public function on_dismissed_notice_response( $data, $notice ) { |
| 685 |
switch ( $notice ) { |
| 686 |
case 'skip-setup-wizard': |
| 687 |
delete_option( 'learn_press_install' ); |
| 688 |
break; |
| 689 |
case 'newsletter-button': |
| 690 |
$context = LP_Request::get_string( 'context' ); |
| 691 |
if ( ! $context || $context != 'newsletter' ) { |
| 692 |
break; |
| 693 |
} |
| 694 |
|
| 695 |
$user = learn_press_get_current_user(); |
| 696 |
if ( ! $user || $user->get_email() == '' ) { |
| 697 |
$data['error'] = __( 'Failed while joining the newsletter! Please try again!', 'learnpress' ); |
| 698 |
} |
| 699 |
|
| 700 |
$url = 'https://thimpress.com/mailster/subscribe'; |
| 701 |
$response = wp_remote_post( |
| 702 |
$url, |
| 703 |
array( |
| 704 |
'method' => 'POST', |
| 705 |
'timeout' => 45, |
| 706 |
'redirection' => 5, |
| 707 |
'httpversion' => '1.0', |
| 708 |
'blocking' => true, |
| 709 |
'headers' => array(), |
| 710 |
'body' => array( |
| 711 |
'_referer' => 'extern', |
| 712 |
'_nonce' => '4b266caf7b', |
| 713 |
'formid' => '19', |
| 714 |
'email' => $user->get_email(), |
| 715 |
'website' => site_url(), |
| 716 |
), |
| 717 |
'cookies' => array(), |
| 718 |
) |
| 719 |
); |
| 720 |
|
| 721 |
if ( is_wp_error( $response ) ) { |
| 722 |
$error_message = $response->get_error_message(); |
| 723 |
$data['message'] = __( 'Something went wrong: ', 'learnpress' ) . $error_message; |
| 724 |
} else { |
| 725 |
$data['message'] = __( |
| 726 |
'Thank you for subscribing! Please check and click the confirmation link from the email that we\'ve just sent to your inbox.', |
| 727 |
'learnpress' |
| 728 |
); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
return $data; |
| 733 |
} |
| 734 |
|
| 735 |
/** |
| 736 |
* Include all classes and functions used for admin |
| 737 |
*/ |
| 738 |
public function includes() { |
| 739 |
// Common function used in admin |
| 740 |
include_once 'lp-admin-functions.php'; |
| 741 |
include_once 'lp-admin-actions.php'; |
| 742 |
require_once LP_PLUGIN_PATH . 'inc/background-process/class-lp-background-query-items.php'; |
| 743 |
include_once 'class-lp-admin-assets.php'; |
| 744 |
include_once 'class-lp-admin-dashboard.php'; |
| 745 |
// include_once 'class-lp-admin-tools.php'; |
| 746 |
include_once 'class-lp-admin-ajax.php'; |
| 747 |
include_once 'editor/class-lp-admin-editor.php'; |
| 748 |
include_once 'class-lp-admin-menu.php'; |
| 749 |
include_once 'helpers/class-lp-outdated-template-helper.php'; |
| 750 |
include_once 'helpers/class-lp-plugins-helper.php'; |
| 751 |
include_once 'class-lp-modal-search-items.php'; |
| 752 |
include_once 'class-lp-modal-search-users.php'; |
| 753 |
include_once 'class-lp-setup-wizard.php'; |
| 754 |
// include_once 'class-lp-updater.php'; |
| 755 |
include_once 'class-lp-install-sample-data.php'; |
| 756 |
include_once 'class-lp-reset-data.php'; |
| 757 |
include_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/course/settings.php'; |
| 758 |
include_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/quiz/settings.php'; |
| 759 |
include_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/lesson/settings.php'; |
| 760 |
include_once LP_PLUGIN_PATH . 'inc/admin/views/meta-boxes/question/settings.php'; |
| 761 |
} |
| 762 |
|
| 763 |
/** |
| 764 |
* Get courses, item's courses on Backend page post_type |
| 765 |
* |
| 766 |
* @param WP_Query $query |
| 767 |
*/ |
| 768 |
public function get_course_items_of_user_backend( WP_Query $query ) { |
| 769 |
if ( ! $query->is_main_query() ) { |
| 770 |
return; |
| 771 |
} |
| 772 |
|
| 773 |
global $post_type, $pagenow; |
| 774 |
|
| 775 |
if ( current_user_can( ADMIN_ROLE ) ) { |
| 776 |
return; |
| 777 |
} |
| 778 |
|
| 779 |
if ( ! current_user_can( LP_TEACHER_ROLE ) || ( $pagenow != 'edit.php' ) ) { |
| 780 |
return; |
| 781 |
} |
| 782 |
|
| 783 |
$post_type_valid = apply_filters( |
| 784 |
'learn-press/filter-user-access-types', |
| 785 |
array( LP_COURSE_CPT, LP_LESSON_CPT, LP_QUIZ_CPT, LP_QUESTION_CPT ) |
| 786 |
); |
| 787 |
|
| 788 |
if ( ! in_array( $post_type, $post_type_valid ) ) { |
| 789 |
return; |
| 790 |
} |
| 791 |
|
| 792 |
// $query->set( 'author', get_current_user_id() ); |
| 793 |
|
| 794 |
$query = apply_filters( 'learnpress/get-post-type-lp-on-backend', $query ); |
| 795 |
|
| 796 |
//add_filter( 'views_edit-' . $post_type . '', '_learn_press_restrict_view_items', 10 ); |
| 797 |
remove_filter( 'pre_get_posts', array( $this, 'get_course_items_of_user_backend' ), 10 ); |
| 798 |
} |
| 799 |
|
| 800 |
/** |
| 801 |
* Set link item of course when edit item on Backend |
| 802 |
* |
| 803 |
* @param string $post_link |
| 804 |
* @param int $post_id |
| 805 |
* @param string $new_title |
| 806 |
* @param string $new_slug |
| 807 |
* @param WP_Post|null $post |
| 808 |
* |
| 809 |
* @return string |
| 810 |
* @author tungnx |
| 811 |
* @since 3.2.7.5 |
| 812 |
* @version 4.0.1 |
| 813 |
*/ |
| 814 |
public function lp_course_set_link_item_backend( $post_link = '', $post_id = 0, $new_title = '', $new_slug = '', $post = null ) { |
| 815 |
if ( ! in_array( $post->post_type, learn_press_get_course_item_types() ) ) { |
| 816 |
return $post_link; |
| 817 |
} |
| 818 |
|
| 819 |
try { |
| 820 |
$course_id_of_item = LP_Course_DB::getInstance()->get_course_by_item_id( $post->ID ); |
| 821 |
|
| 822 |
if ( $course_id_of_item ) { |
| 823 |
$course = learn_press_get_course( $course_id_of_item ); |
| 824 |
|
| 825 |
if ( $course ) { |
| 826 |
$link_item = $course->get_item_link( $post->ID ); |
| 827 |
|
| 828 |
$post_slug = $post->post_name; |
| 829 |
$link_item_edit_slug = preg_replace( '/' . $post_slug . '$/', '', $link_item ); |
| 830 |
|
| 831 |
// For update new slug |
| 832 |
if ( $new_slug ) { |
| 833 |
$post_slug = $new_slug; |
| 834 |
} |
| 835 |
|
| 836 |
$post_link = '<strong>Permalink: </strong>'; |
| 837 |
$post_link .= '<span id="sample-permalink">'; |
| 838 |
$post_link .= '<a href="' . $link_item . '">' . $link_item_edit_slug . '<span id="editable-post-name">' . $post_slug . '</span>/</a>'; |
| 839 |
$post_link .= '</span>'; |
| 840 |
$post_link .= '‎<span id="edit-slug-buttons">'; |
| 841 |
$post_link .= '<button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="Edit permalink">Edit</button>'; |
| 842 |
$post_link .= '</span>'; |
| 843 |
$post_link .= '<span id="editable-post-name-full">' . $post_slug . '</span>'; |
| 844 |
} |
| 845 |
} else { |
| 846 |
// $post_link_preview = sprintf( '<a class="button" href="%s" target="_blank">%s</a>', learn_press_get_preview_url( $post_id ), __( 'Preview', 'learnpress' ) ); |
| 847 |
$post_link_message = '<span>' . __( |
| 848 |
'Permalink is only available if the item is already assigned to a course.', |
| 849 |
'learnpress' |
| 850 |
) . '</span>'; |
| 851 |
$post_link = sprintf( '<div id="learn-press-box-edit-slug">%s</div>', $post_link_message ); |
| 852 |
} |
| 853 |
} catch ( Throwable $e ) { |
| 854 |
error_log( $e->getMessage() ); |
| 855 |
} |
| 856 |
|
| 857 |
return $post_link; |
| 858 |
} |
| 859 |
|
| 860 |
/** |
| 861 |
* @return false|string |
| 862 |
* @since 3.2.8 |
| 863 |
* @editor tungnx |
| 864 |
*/ |
| 865 |
public function get_screen_id() { |
| 866 |
global $current_screen; |
| 867 |
|
| 868 |
return $current_screen ? $current_screen->id : false; |
| 869 |
} |
| 870 |
|
| 871 |
/** |
| 872 |
* Get single instance of self |
| 873 |
* |
| 874 |
* @return bool|LP_Admin |
| 875 |
* @return bool|LP_Admin |
| 876 |
* @since 3.0.0 |
| 877 |
* |
| 878 |
*/ |
| 879 |
public static function instance() { |
| 880 |
if ( ! self::$instance ) { |
| 881 |
self::$instance = new self(); |
| 882 |
} |
| 883 |
|
| 884 |
return self::$instance; |
| 885 |
} |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
return LP_Admin::instance(); |
| 890 |
|