Skip to content

Commit

Permalink
= 4.1.7.3.2 =
Browse files Browse the repository at this point in the history
~ Modified: admin notices.
  • Loading branch information
tungnxt89 committed Nov 11, 2022
1 parent ad5b93c commit de13ce8
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 75 deletions.
27 changes: 27 additions & 0 deletions assets/src/apps/js/admin/admin-notices.js
Original file line number Diff line number Diff line change
@@ -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)

})
26 changes: 25 additions & 1 deletion inc/admin/class-lp-admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -688,3 +710,5 @@ public static function update_order_exports() {

add_action( 'init', array( 'LP_Admin_Ajax', 'init' ) );
}

new LP_Admin_Ajax();
4 changes: 4 additions & 0 deletions inc/admin/class-lp-admin-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
),
)
);

Expand Down
2 changes: 1 addition & 1 deletion inc/admin/class-lp-admin-notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}

/**
Expand Down
5 changes: 1 addition & 4 deletions inc/admin/class-lp-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,7 @@ public function admin_notices() {
<?php
}

// Show notice check wp_remote_post (background) runs.
?>
<div class="notice-check-run-background-available"></div>
<?php
learn_press_admin_view( 'admin-notices.php', [], true );
}

/**
Expand Down
65 changes: 0 additions & 65 deletions inc/admin/lp-admin-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,71 +119,6 @@ function learn_press_is_hidden_post_box( $id, $user_id = 0 ) {
return false !== array_search( $id, $data );
}

/**
* 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;
}

/**
* List all pages as a dropdown with "Add New Page" option
*
Expand Down
2 changes: 2 additions & 0 deletions inc/admin/views/admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/

defined( 'ABSPATH' ) || exit;

wp_enqueue_script('lp-admin-notices');
?>

<div class="lp-admin-notices">
Expand Down
19 changes: 19 additions & 0 deletions inc/admin/views/admin-notices/wp-remote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Template for display error wrong name plugin learnpress
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

if ( ! isset( $data ) || ! isset( $data['error'] ) ) {
return;
}
?>

<div class="notice <?php echo esc_attr( $data['class'] ?? '' ); ?>">
<p>
<?php echo 'wp_remote_get: ' . $data['error'] ?? ''; ?>
</p>
</div>
2 changes: 1 addition & 1 deletion inc/lp-core-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
);

Expand Down
65 changes: 65 additions & 0 deletions inc/lp-template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion inc/rest-api/v1/admin/class-lp-admin-rest-tools-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions learnpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
}

/**
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit de13ce8

Please sign in to comment.