From de13ce83ab3ca6ce1cfa066e8f22533171b85a03 Mon Sep 17 00:00:00 2001 From: tungnx89 Date: Sat, 12 Nov 2022 02:32:53 +0700 Subject: [PATCH] = 4.1.7.3.2 = ~ Modified: admin notices. --- assets/src/apps/js/admin/admin-notices.js | 27 ++++++++ inc/admin/class-lp-admin-ajax.php | 26 +++++++- inc/admin/class-lp-admin-assets.php | 4 ++ inc/admin/class-lp-admin-notice.php | 2 +- inc/admin/class-lp-admin.php | 5 +- inc/admin/lp-admin-functions.php | 65 ------------------- inc/admin/views/admin-notices.php | 2 + inc/admin/views/admin-notices/wp-remote.php | 19 ++++++ inc/lp-core-functions.php | 2 +- inc/lp-template-functions.php | 65 +++++++++++++++++++ .../class-lp-admin-rest-tools-controller.php | 27 +++++++- learnpress.php | 4 +- webpack.config.js | 1 + 13 files changed, 174 insertions(+), 75 deletions(-) create mode 100644 assets/src/apps/js/admin/admin-notices.js create mode 100644 inc/admin/views/admin-notices/wp-remote.php diff --git a/assets/src/apps/js/admin/admin-notices.js b/assets/src/apps/js/admin/admin-notices.js new file mode 100644 index 000000000..c7694c744 --- /dev/null +++ b/assets/src/apps/js/admin/admin-notices.js @@ -0,0 +1,27 @@ +let elLPAdminNotices; +let data_html = null; + +const urlApiAdminNotices = lpGlobalSettings.rest + 'lp/v1/admin/tools/admin-notices'; +fetch( urlApiAdminNotices, { + method: 'GET' +} ).then((res)=> + res.json() +).then((res) => { + // console.log(data); + const {status, message, data } = res; + data_html = data.content; +}).catch((err) => { + console.log(err); +}); + +document.addEventListener('DOMContentLoaded', () => { + elLPAdminNotices = document.querySelector('.lp-admin-notices'); + + const interval = setInterval(() => { + if ( data_html !== null ) { + elLPAdminNotices.innerHTML = data_html; + clearInterval(interval); + } + }, 1) + +}) diff --git a/inc/admin/class-lp-admin-ajax.php b/inc/admin/class-lp-admin-ajax.php index 79cc02f37..d61e37b39 100644 --- a/inc/admin/class-lp-admin-ajax.php +++ b/inc/admin/class-lp-admin-ajax.php @@ -19,12 +19,34 @@ * Class LP_Admin_Ajax */ class LP_Admin_Ajax { + public function __construct() { + add_action( 'wp_ajax_nopriv_check_background_available', array( $this, 'check_background_available' ) ); + } + + public function check_background_available() { + echo '[TEST_LOOPBACK]'; + exit; + } + + /** + * Tests the background handler's connection. + * + * @since 4.1.7.3.2 + * + * @return bool + */ + public static function check_wp_remote() { + $test_url = add_query_arg( 'action', 'check_background_available', admin_url( 'admin-ajax.php' ) ); + $result = wp_safe_remote_get( $test_url ); + $body = ! is_wp_error( $result ) ? wp_remote_retrieve_body( $result ) : $result; + + return $body === '[TEST_LOOPBACK]' ? true : $result; + } /** * Add action ajax */ public static function init() { - if ( ! is_user_logged_in() ) { return; } @@ -688,3 +710,5 @@ public static function update_order_exports() { add_action( 'init', array( 'LP_Admin_Ajax', 'init' ) ); } + +new LP_Admin_Ajax(); diff --git a/inc/admin/class-lp-admin-assets.php b/inc/admin/class-lp-admin-assets.php index 4f95e3bf3..c71185449 100644 --- a/inc/admin/class-lp-admin-assets.php +++ b/inc/admin/class-lp-admin-assets.php @@ -275,6 +275,10 @@ protected function _get_scripts(): array { 0, 1 ), + 'lp-admin-notices' => new LP_Asset_Key( + self::url( 'js/dist/admin/admin-notices' . self::$_min_assets . '.js' ), + ['wp-api-fetch'] + ), ) ); diff --git a/inc/admin/class-lp-admin-notice.php b/inc/admin/class-lp-admin-notice.php index 721b30b5d..7b2366d61 100644 --- a/inc/admin/class-lp-admin-notice.php +++ b/inc/admin/class-lp-admin-notice.php @@ -58,7 +58,7 @@ class LP_Admin_Notice { protected function __construct() { //add_action( 'init', array( $this, 'dismiss_notice' ) ); //add_action( 'init', array( $this, 'load' ) ); - add_action( 'admin_notices', array( $this, 'admin_notices' ) ); + //add_action( 'admin_notices', array( $this, 'admin_notices' ) ); } /** diff --git a/inc/admin/class-lp-admin.php b/inc/admin/class-lp-admin.php index 4e2660fdb..99c198480 100644 --- a/inc/admin/class-lp-admin.php +++ b/inc/admin/class-lp-admin.php @@ -618,10 +618,7 @@ public function admin_notices() { -
- plugin_path( 'inc/admin/views/' . $name ); - } - - return apply_filters( 'learn_press_admin_view', $view, $name ); -} - -function learn_press_admin_view_content( $name, $args = array() ) { - return learn_press_admin_view( $name, $args, false, true ); -} - -/** - * Find a full path of a view and display the content in admin - * - * @param $name - * @param array $args - * @param bool|false $include_once - * @param bool - * - * @return bool - */ -function learn_press_admin_view( $name, $args = array(), $include_once = false, $return = false ) { - $view = learn_press_get_admin_view( $name, ! empty( $args['plugin_file'] ) ? $args['plugin_file'] : null ); - - if ( file_exists( $view ) ) { - - ob_start(); - - is_array( $args ) && extract( $args ); - - do_action( 'learn_press_before_display_admin_view', $name, $args ); - - if ( $include_once ) { - include_once $view; - } else { - include $view; - } - - do_action( 'learn_press_after_display_admin_view', $name, $args ); - $output = ob_get_clean(); - - if ( ! $return ) { - learn_press_echo_vuejs_write_on_php( $output ); - } - - return $return ? $output : true; - } - - return false; -} - /** * List all pages as a dropdown with "Add New Page" option * diff --git a/inc/admin/views/admin-notices.php b/inc/admin/views/admin-notices.php index f7cd3db2b..835165c77 100644 --- a/inc/admin/views/admin-notices.php +++ b/inc/admin/views/admin-notices.php @@ -4,6 +4,8 @@ */ defined( 'ABSPATH' ) || exit; + +wp_enqueue_script('lp-admin-notices'); ?>
diff --git a/inc/admin/views/admin-notices/wp-remote.php b/inc/admin/views/admin-notices/wp-remote.php new file mode 100644 index 000000000..456ea52f9 --- /dev/null +++ b/inc/admin/views/admin-notices/wp-remote.php @@ -0,0 +1,19 @@ + + +
+

+ +

+
diff --git a/inc/lp-core-functions.php b/inc/lp-core-functions.php index e16bc9e2f..82f0b6e02 100644 --- a/inc/lp-core-functions.php +++ b/inc/lp-core-functions.php @@ -2780,7 +2780,7 @@ function learn_press_global_script_params() { 'button_yes' => __( 'Yes', 'learnpress' ), 'button_no' => __( 'No', 'learnpress' ), ), - 'root' => esc_url_raw( rest_url() ), + 'rest' => esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ), ); diff --git a/inc/lp-template-functions.php b/inc/lp-template-functions.php index aeae785dd..45fca06f3 100644 --- a/inc/lp-template-functions.php +++ b/inc/lp-template-functions.php @@ -924,6 +924,71 @@ function learn_press_override_templates() { return apply_filters( 'learn-press/override-templates', false ); } +/** + * Get html view path for admin to display + * + * @param $name + * @param $plugin_file + * + * @return mixed + */ +function learn_press_get_admin_view( $name, $plugin_file = null ) { + if ( ! preg_match( '/\.(html|php)$/', $name ) ) { + $name .= '.php'; + } + if ( $plugin_file ) { + $view = dirname( $plugin_file ) . '/inc/admin/views/' . $name; + } else { + $view = LearnPress::instance()->plugin_path( 'inc/admin/views/' . $name ); + } + + return apply_filters( 'learn_press_admin_view', $view, $name ); +} + +function learn_press_admin_view_content( $name, $args = array() ) { + return learn_press_admin_view( $name, $args, false, true ); +} + +/** + * Find a full path of a view and display the content in admin + * + * @param $name + * @param array $args + * @param bool|false $include_once + * @param bool + * + * @return bool + */ +function learn_press_admin_view( $name, $args = array(), $include_once = false, $return = false ) { + $view = learn_press_get_admin_view( $name, ! empty( $args['plugin_file'] ) ? $args['plugin_file'] : null ); + + if ( file_exists( $view ) ) { + + ob_start(); + + is_array( $args ) && extract( $args ); + + do_action( 'learn_press_before_display_admin_view', $name, $args ); + + if ( $include_once ) { + include_once $view; + } else { + include $view; + } + + do_action( 'learn_press_after_display_admin_view', $name, $args ); + $output = ob_get_clean(); + + if ( ! $return ) { + learn_press_echo_vuejs_write_on_php( $output ); + } + + return $return ? $output : true; + } + + return false; +} + if ( ! function_exists( 'learn_press_is_404' ) ) { /** * Set header is 404 diff --git a/inc/rest-api/v1/admin/class-lp-admin-rest-tools-controller.php b/inc/rest-api/v1/admin/class-lp-admin-rest-tools-controller.php index fd939145e..59e2b7a49 100644 --- a/inc/rest-api/v1/admin/class-lp-admin-rest-tools-controller.php +++ b/inc/rest-api/v1/admin/class-lp-admin-rest-tools-controller.php @@ -5,7 +5,7 @@ * * @since 4.0.3 * @author tungnx - * @version 1.0.0 + * @version 1.0.1 */ class LP_REST_Admin_Tools_Controller extends LP_Abstract_REST_Controller { public function __construct() { @@ -191,7 +191,32 @@ public function clean_tables( WP_REST_Request $request ) { public function admin_notices( WP_REST_Request $request ) { $response = new LP_REST_Response(); try { + $params = $request->get_params(); + + $rules = [ + 'check_wp_remote' => [ + 'class' => 'notice-error', + 'template' => 'admin-notices/wp-remote.php', + 'display' => call_user_func( [ 'LP_Admin_Ajax', 'check_wp_remote' ] ), + ], + 'check_right_plugin_base' => [ + 'class' => 'notice-error', + 'template' => 'admin-notices/wrong-name-plugin.php', + 'display' => call_user_func( [ 'LP_Admin_Notice', 'check_right_plugin_base' ] ), + ], + ]; + + foreach ( $rules as $rule => $template_data ) { + if ( $template_data[ 'display' ] ) { + if ( is_wp_error( $template_data[ 'display' ] ) ) { + $template_data[ 'error' ] = $template_data[ 'display' ]->get_error_message(); + } + + learn_press_admin_view( $template_data[ 'template' ] ?? '', [ 'data' => $template_data ], true ); + } + } + $response->data->content = ob_get_clean(); } catch ( Exception $e ) { $response->message = $e->getMessage(); } diff --git a/learnpress.php b/learnpress.php index 20843975b..3d98c432a 100644 --- a/learnpress.php +++ b/learnpress.php @@ -414,6 +414,8 @@ private function include_files_admin() { return; } + require_once 'inc/admin/class-lp-admin-ajax.php'; + require_once 'inc/admin/class-lp-admin-notice.php'; // File handle install LP @@ -424,8 +426,6 @@ private function include_files_admin() { require_once 'inc/admin/class-lp-admin.php'; // require_once 'inc/admin/settings/abstract-settings-page.php'; - - require_once 'inc/admin/class-lp-admin-ajax.php'; } /** diff --git a/webpack.config.js b/webpack.config.js index 7a3616935..558641bd4 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -11,6 +11,7 @@ const isProduction = process.env.NODE_ENV === 'production'; module.exports = { ...defaultConfig, entry: { + './assets/js/dist/admin/admin-notices': './assets/src/apps/js/admin/admin-notices.js', './assets/js/dist/admin/editor/course': './assets/src/apps/js/admin/editor/course.js', './assets/js/dist/admin/editor/quiz': './assets/src/apps/js/admin/editor/quiz.js', './assets/js/dist/admin/editor/question': './assets/src/apps/js/admin/editor/question.js',