pages = [ 'courses' => __( 'Courses', 'learnpress' ), 'instructors' => __( 'Instructors', 'learnpress' ), 'single_instructor' => __( 'Single Instructors', 'learnpress' ), 'profile' => __( 'Profile', 'learnpress' ), 'checkout' => __( 'Checkout', 'learnpress' ), 'become_a_teacher' => __( 'Become an Instructor', 'learnpress' ), ]; $this->includes(); add_action( 'delete_user', array( $this, 'delete_user_data' ) ); //add_action( 'delete_user_form', array( $this, 'delete_user_form' ) ); add_action( 'all_admin_notices', array( $this, 'admin_notices' ), - 1 ); add_action( 'admin_enqueue_scripts', array( $this, 'load_modal' ) ); add_filter( 'admin_body_class', array( $this, 'body_class' ) ); //add_filter( 'manage_users_custom_column', array( $this, 'users_custom_column' ), 10, 3 ); //add_filter( 'manage_pages_columns', array( $this, 'page_columns_head' ) ); //add_filter( 'manage_pages_custom_column', array( $this, 'page_columns_content' ), 10, 2 ); add_filter( 'views_edit-page', array( $this, 'views_pages' ), 10 ); add_filter( 'views_users', array( $this, 'views_users' ), 10, 1 ); add_filter( 'user_row_actions', array( $this, 'user_row_actions' ), 10, 2 ); add_filter( 'get_pages', array( $this, 'add_empty_page' ), 1000, 2 ); add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ), 1 ); add_filter( 'views_plugins', array( $this, 'views_plugins' ) ); LP_Request::register( 'lp-action', array( $this, 'filter_users' ) ); add_filter( 'learn-press/modal-search-items-args', array( $this, 'filter_modal_search' ) ); /*add_filter( 'learn-press/dismissed-notice-response', array( $this, 'on_dismissed_notice_response', ), 10, 2 );*/ // get list items course of user | tungnx add_action( 'pre_get_posts', array( $this, 'get_course_items_of_user_backend' ), 10 ); add_action( 'pre_get_posts', array( $this, 'get_pages_of_lp' ), 10 ); // Set link item course when edit on Backend | tungnx add_filter( 'get_sample_permalink_html', array( $this, 'lp_course_set_link_item_backend' ), 10, 5 ); // Add "Edit with Course Builder" button below title area for courses add_action( 'edit_form_after_title', array( $this, 'add_course_builder_button' ) ); /*add_action( 'admin_init', function () { // From LP v4.2.3 temporary run create pages to add page instructors, single instructor for client upgrade LP. // After a long time, will remove this code. if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { LP_Install::create_pages(); } } );*/ add_filter( 'users_list_table_query_args', [ $this, 'exclude_temp_users' ] ); // Show label 'LearnPress Page' for page of LearnPress. //add_filter('post_states_html'); add_filter( 'display_post_states', [ $this, 'show_label_page_type' ], 10, 2 ); } /** * @since 3.2.6 */ public function load_modal() { // Now only add item(s) when create new Order is using. if ( in_array( get_post_type(), array( LP_ORDER_CPT ) ) ) { LP_Modal_Search_Items::instance(); } /*if ( in_array( get_post_type(), array( LP_ORDER_CPT ) ) ) { LP_Modal_Search_Users::instance(); }*/ } /** * @param $options * * @return array */ public function filter_modal_search( $options ) { $options = wp_parse_args( array( 'title' => __( 'Available Courses', 'learnpress' ) ), $options ); return $options; } public function add_empty_page( $pages, $args ) { if ( empty( $pages ) && ! empty( $args['class'] ) && strpos( $args['class'], 'lp-list-pages' ) !== false ) { $empty_page = get_default_post_to_edit( 'page' ); $empty_page->ID = '00000'; $pages[] = $empty_page; } return $pages; } /** * Add 'LearnPress' tab into views of plugins manage. * * @param array $views * * @return array * @since 3.0.0 */ public function views_plugins( $views ) { global $s; $search = $this->get_addons(); $count_activated = 0; $active_plugins = get_option( 'active_plugins' ); if ( $active_plugins ) { if ( $search ) { foreach ( $search as $k => $v ) { if ( in_array( $k, $active_plugins ) ) { ++ $count_activated; } } } } if ( $s && false !== stripos( $s, 'learnpress' ) ) { $views['learnpress'] = sprintf( '%s (%d/%d)', admin_url( 'plugins.php?s=learnpress' ), __( 'LearnPress', 'learnpress' ), $count_activated, sizeof( $search ) ); } else { $views['learnpress'] = sprintf( '%s (%d/%d)', admin_url( 'plugins.php?s=learnpress' ), __( 'LearnPress', 'learnpress' ), $count_activated, sizeof( $search ) ); } return $views; } public function get_addons() { $all_plugins = apply_filters( 'all_plugins', get_plugins() ); return array_filter( $all_plugins, array( $this, '_search_callback' ) ); } /** * Callback function for searching plugins have 'learnpress' inside. * * @param array $plugin * * @return bool * @since 3.0.0 */ public function _search_callback( $plugin ) { foreach ( $plugin as $value ) { if ( is_string( $value ) && false !== stripos( strip_tags( $value ), 'learnpress' ) ) { return true; } } return false; } /** * Check if a page is set for WooCommerce. * * @param int $id * * @return bool */ /*protected function _is_wc_page( $id ) { if ( class_exists( 'WooCommerce' ) ) { if ( ! class_exists( 'WC_Admin_Post_Types' ) ) { include_once dirname( WC_PLUGIN_FILE ) . '/includes/admin/class-wc-admin-post-types.php'; } $wc_admin_post_types = new WC_Admin_Post_Types(); if ( is_callable( array( $wc_admin_post_types, 'add_display_post_states' ) ) ) { $a = $wc_admin_post_types->add_display_post_states( array(), get_post( $id ) ); } else { $a = $this->wc_add_display_post_states( array(), get_post( $id ) ); } $wc_pages = array( 'wc_page_for_shop', 'wc_page_for_cart', 'wc_page_for_checkout', 'wc_page_for_myaccount', 'wc_page_for_terms', ); foreach ( $wc_pages as $for_page ) { if ( isset( $a[ $for_page ] ) ) { return $a[ $for_page ]; } } } return false; }*/ /*public function wc_add_display_post_states( $post_states, $post ) { if ( wc_get_page_id( 'shop' ) === $post->ID ) { $post_states['wc_page_for_shop'] = __( 'Shop Page', 'learnpress' ); } if ( wc_get_page_id( 'cart' ) === $post->ID ) { $post_states['wc_page_for_cart'] = __( 'Cart Page', 'learnpress' ); } if ( wc_get_page_id( 'checkout' ) === $post->ID ) { $post_states['wc_page_for_checkout'] = __( 'Checkout Page', 'learnpress' ); } if ( wc_get_page_id( 'myaccount' ) === $post->ID ) { $post_states['wc_page_for_myaccount'] = __( 'My Account Page', 'learnpress' ); } if ( wc_get_page_id( 'terms' ) === $post->ID ) { $post_states['wc_page_for_terms'] = __( 'Terms and Conditions Page', 'learnpress' ); } return $post_states; }*/ /** * Check if a page is set for Paid Membership Pro. * * @param int $id * * @return bool|mixed */ /*protected function _is_pmpro_page( $id ) { global $pmpro_pages; if ( $pmpro_pages ) { $pages = array( 'account' => __( 'Account', 'learnpress' ), 'billing' => __( 'Billing', 'learnpress' ), 'cancel' => __( 'Cancel', 'learnpress' ), 'checkout' => __( 'Checkout', 'learnpress' ), 'confirmation' => __( 'Confirmation', 'learnpress' ), 'invoice' => __( 'Invoice', 'learnpress' ), 'levels' => __( 'Levels', 'learnpress' ), ); foreach ( $pages as $name => $text ) { if ( $pmpro_pages[ $name ] == $id ) { return $text; } } } return false; }*/ /** * Check if a page is set for Paid Membership Pro. * * @param int $id * * @return bool|mixed */ /*protected function _is_bp_page( $id ) { if ( function_exists( 'buddypress' ) ) { $bp_pages = get_option( 'bp-pages' ); if ( ! $bp_pages ) { return false; } $pages = array( 'members' => __( 'Members', 'learnpress' ), 'activity' => __( 'Activity', 'learnpress' ), 'register' => __( 'Register', 'learnpress' ), 'activate' => __( 'Activate', 'learnpress' ), ); foreach ( $pages as $name => $text ) { if ( isset( $bp_pages[ $name ] ) && $bp_pages[ $name ] == $id ) { return $text; } } } return false; }*/ /** * Get pages is publish set by LearnPress. * * @return array */ protected function _get_static_pages(): array { $static_pages = []; foreach ( $this->pages as $name => $title ) { $page_id = learn_press_get_page_id( $name ); if ( $page_id && 'publish' === get_post_status( $page_id ) ) { $static_pages[ $page_id ] = $title; } } return $static_pages; } /** * Show label LearnPress Page for pages set by LearnPress. * * @param array $post_states * @param WP_Post $post * * @return array * @since 4.3.2.6 * @version 1.0.0 */ public function show_label_page_type( $post_states, $post ) { $post_type = get_post_type( $post ); if ( $post_type !== 'page' ) { return $post_states; } $page_id_compare = $post->ID; foreach ( $this->_get_static_pages() as $page_id => $title ) { if ( $page_id == $page_id_compare ) { $post_states['lp_page'] = sprintf( '%s', 'LearnPress ' . $title . ' Page' ); break; } } return $post_states; } /** * Add new column to WP Pages manage to show what page is assigned to. * * @param array $columns * * @return array */ /*public function page_columns_head( $columns ) { $_columns = $columns; $columns = array(); foreach ( $_columns as $name => $text ) { if ( $name === 'date' ) { $columns['lp-page'] = __( 'LearnPress Page', 'learnpress' ); } $columns[ $name ] = $text; } return $columns; }*/ /** * Display the page is assigned to LP Page. * * @param string $column_name * @param int $post */ /*public function page_columns_content( $column_name, $post ) { $pages = $this->_get_static_pages(); switch ( $column_name ) { case 'lp-page': if ( ! empty( $pages['learnpress'][ $post ] ) ) { echo wp_kses_post( $pages['learnpress'][ $post ] ); } foreach ( $pages as $plugin => $plugin_pages ) { if ( $plugin === 'learnpress' ) { continue; } if ( ! empty( $pages[ $plugin ][ $post ] ) ) { echo sprintf( '
(%s - %s)
', $plugin, $pages[ $plugin ][ $post ] ); } } } }*/ /** * @param $actions * * @return mixed */ public function views_pages( $actions ) { $pages = $this->_get_static_pages(); if ( $pages ) { $text = sprintf( __( 'LearnPress Pages (%d)', 'learnpress' ), sizeof( $pages ) ); if ( 'yes' !== LP_Request::get_param( 'lp-page' ) ) { $actions['lp-page'] = sprintf( '%s', admin_url( 'edit.php?post_type=page&lp-page=yes' ), $text ); } else { $actions['lp-page'] = $text; } } return $actions; } /** * Get pages set by LP with param url lp-page=yes * * @param WP_Query $q * * @return mixed */ public function get_pages_of_lp( $q ) { if ( ! is_admin() || ! $q->is_main_query() ) { return $q; } if ( 'page' == LP_Request::get_param( 'post_type' ) && 'yes' == LP_Request::get_param( 'lp-page' ) ) { $ids = $this->_get_static_pages(); if ( ! empty( $ids ) ) { $ids = array_keys( $ids ); $q->set( 'post__in', $ids ); } } return $q; } /** * Add actions to users list * * @param array $actions * @param WP_User $user * * @return mixed */ public function user_row_actions( $actions, $user ) { $pending_request = self::get_pending_requests(); if ( LP_Request::get_param( 'lp-action' ) == 'pending-request' && $pending_request ) { $actions = array(); $nonce = 'nonce=' . wp_create_nonce( 'lp-action-permit-role-teacher' ); if ( in_array( $user->ID, $pending_request ) ) { $actions['accept'] = sprintf( '%s', admin_url( "users.php?lp-action=accept-request&user_id={$user->ID}&{$nonce}" ), _x( 'Accept', 'pending-request', 'learnpress' ) ); $actions['delete deny'] = sprintf( '%s', admin_url( "users.php?lp-action=deny-request&user_id={$user->ID}&{$nonce}" ), _x( 'Deny', 'pending-request', 'learnpress' ) ); } } return $actions; } public function exclude_temp_users( $args ) { if ( LP_Request::get_param( 'lp-action' ) == 'pending-request' ) { $args['include'] = self::get_pending_requests(); } return $args; } /** * Get pending requests be come a Teacher. * * @return array */ public static function get_pending_requests() { global $wpdb; $query = $wpdb->prepare( " SELECT ID FROM {$wpdb->users} u INNER JOIN {$wpdb->usermeta} um ON um.user_id = u.ID AND um.meta_key = %s WHERE um.meta_value = %s ", '_requested_become_teacher', 'yes' ); return $wpdb->get_col( $query ); } /** * Filter user by custom param * * @param string $action */ public function filter_users( $action ) { if ( ! current_user_can( 'administrator' ) ) { return; } $user_id = LP_Request::get_param( 'user_id', 'int' ); if ( ! $user_id || ! get_user_by( 'id', $user_id ) ) { return; } $nonce = LP_Request::get_param( 'nonce' ); if ( ! wp_verify_nonce( $nonce, 'lp-action-permit-role-teacher' ) ) { return; } $user_data = get_userdata( $user_id ); if ( in_array( $action, array( 'accept-request', 'deny-request' ) ) ) { delete_user_meta( $user_id, '_requested_become_teacher' ); } switch ( $action ) { case 'accept-request': $be_teacher = new WP_User( $user_id ); $be_teacher->set_role( LP_TEACHER_ROLE ); /** * Send email to user when admin accept user become a teacher * @use SendEmailAjax::send_mail_become_a_teacher_accept */ $data_send = [ 'params' => [ $user_data->user_email ], 'lp-load-ajax' => 'send_mail_become_a_teacher_accept', ]; LPBackgroundAjax::handle( $data_send ); do_action( 'learn-press/user-become-a-teacher-accept', $user_data->user_email ); wp_redirect( admin_url( 'users.php?lp-action=accepted-request&user_id=' . $user_id ) ); exit(); case 'deny-request': /** * Send email to user when admin accept user become a teacher * @use SendEmailAjax::send_mail_become_a_teacher_deny */ $data_send = [ 'params' => [ $user_data->user_email ], 'lp-load-ajax' => 'send_mail_become_a_teacher_deny', ]; LPBackgroundAjax::handle( $data_send ); do_action( 'learn-press/user-become-a-teacher-deny', $user_data->user_email ); wp_redirect( admin_url( 'users.php?lp-action=denied-request&user_id=' . $user_id ) ); exit(); } } public function users_custom_column( $content, $column_name, $user_id ) { } /** * Add new view to users views for filtering user by "pending request" of "become a teacher". * * @param array $views * * @return mixed */ public function views_users( $views ) { $pending_request = self::get_pending_requests(); if ( $pending_request ) { if ( LP_Request::get_param( 'lp-action' ) == 'pending-request' ) { $class = ' class="current"'; foreach ( $views as $k => $view ) { $views[ $k ] = preg_replace( '!class="current"!', '', $view ); } } else { $class = ''; } $views['pending-request'] = '' . sprintf( __( 'Pending Request %s', 'learnpress' ), '(' . count( $pending_request ) . ')' ) . ''; } return $views; } /** * Custom admin body classes. * * @param array $classes * * @return array|string */ public function body_class( $classes ) { $post_type = get_post_type(); if ( preg_match( '~^lp_~', $post_type ) ) { if ( $classes ) { $classes = explode( ' ', $classes ); } else { $classes = array(); } $classes[] = 'learnpress'; $classes = array_filter( $classes ); $classes = array_unique( $classes ); $classes = join( ' ', $classes ); } return $classes; } /** * Display notices on Backend. */ public function admin_notices() { if ( function_exists( 'get_current_screen' ) ) { $wp_screen = get_current_screen(); // Not run on course edit screen, it slows, can affect performance edit course. if ( $wp_screen->id === LP_COURSE_CPT ) { return; } } // Show template file templates override. $page = LP_Request::get_param( 'page' ); $tab = LP_Request::get_param( 'tab' ); if ( $page == 'learn-press-tools' && $tab == 'templates' ) { if ( LP_Outdated_Template_Helper::detect_outdated_template() ) { learn_press_admin_view( 'html-admin-notice-templates' ); } } // Request accept/denied user can become a teacher. $action_become_teacher = LP_Request::get_param( 'lp-action' ); $user_id = LP_Request::get_param( 'user_id', 0, 'int' ); $type_action_become_teacher = array( 'accepted-request', 'denied-request' ); if ( in_array( $action_become_teacher, $type_action_become_teacher ) && $user_id && get_user_by( 'id', $user_id ) ) { ?>