Skip to content

Commit

Permalink
= 4.1.7.3.2 =
Browse files Browse the repository at this point in the history
~ Modified: admin notices.
~ Added: session_start
  • Loading branch information
tungnxt89 committed Nov 15, 2022
1 parent f2980d4 commit f66a37f
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 62 deletions.
57 changes: 44 additions & 13 deletions assets/src/apps/js/admin/admin-notices.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
let elLPAdminNotices;
let elLPAdminNotices = null;
let elBtnDismiss;
let dataHtml = 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;
dataHtml = data.content;
} ).catch( ( err ) => {
console.log( err );
} );
const callAdminNotices = ( set ) => {
fetch( urlApiAdminNotices + `?${ set }`, {
method: 'GET',
} ).then( ( res ) =>
res.json()
).then( ( res ) => {
// console.log(data);

const { status, message, data } = res;
if ( status === 'success' ) {
dataHtml = data.content;

if ( dataHtml.length === 0 && elLPAdminNotices ) {
elLPAdminNotices.style.display = 'none';
}
} else {
dataHtml = message;
}
} ).catch( ( err ) => {
console.log( err );
} );
};
callAdminNotices();

/*** DOMContentLoaded ***/
document.addEventListener( 'DOMContentLoaded', () => {
elLPAdminNotices = document.querySelector( '.lp-admin-notices' );
elBtnDismiss = document.querySelector( '.btn-lp-notice-dismiss' );

const interval = setInterval( () => {
if ( dataHtml !== null ) {
elLPAdminNotices.innerHTML = dataHtml;
if ( dataHtml.length > 0 ) {
elLPAdminNotices.innerHTML = dataHtml;
elLPAdminNotices.style.display = 'block';
}

clearInterval( interval );
}
}, 1 );
} );

/*** Events ***/
document.addEventListener( 'click', ( e ) => {
e.preventDefault();
const el = e.target;

if ( el.classList.contains( 'btn-lp-notice-dismiss' ) ) {
const parent = el.closest( '.lp-admin-notice' );
callAdminNotices( `dismiss=${ el.getAttribute( 'data' ) }` );
parent.closest( '.lp-admin-notice' ).remove();
}
} );
8 changes: 6 additions & 2 deletions assets/src/scss/admin/_tool.scss
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@
}
}



.lp-upgrade-message {

.learn-press-message {
Expand Down Expand Up @@ -300,3 +298,9 @@
text-align: center;
display: none;
}

/*** Admin notices ***/
.lp-admin-notices {
position: relative;
display: none;
}
19 changes: 10 additions & 9 deletions inc/admin/class-lp-admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@
*/
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;
add_action(
'wp_ajax_nopriv_check_wp_remote',
function () {
echo '[TEST_REMOTE]';
exit;
}
);
}

/**
* Tests the background handler's connection.
*
* @since 4.1.7.3.2
*
* @return bool
* @return bool|WP_Error
*/
public static function check_wp_remote() {
$test_url = add_query_arg( 'action', 'check_background_available', admin_url( 'admin-ajax.php' ) );
$test_url = add_query_arg( 'action', 'check_wp_remote', 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;
return $body === '[TEST_REMOTE]' ? true : $result;
}

/**
Expand Down
15 changes: 12 additions & 3 deletions inc/admin/class-lp-admin-notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected function __construct() {
* Show notices in admin
*
* @return void
* @deprecated 4.1.7.3.2
*/
public function admin_notices() {
try {
Expand All @@ -91,11 +92,19 @@ public function admin_notices() {
}
}

public function check_right_plugin_base() {
/**
* Check is right plugin base.
*
* @return bool
*/
public static function check_right_plugin_base(): bool {
return 0 !== strcmp( LP_PLUGIN_BASENAME, 'learnpress/learnpress.php' );
}

public function load() {
/**
* @deprecated 4.1.7.3.2
*/
/*public function load() {
$notices = get_option( $this->option_id );
if ( ! $notices ) {
Expand All @@ -107,7 +116,7 @@ public function load() {
delete_option( $this->option_id );
return true;
}
}*/

/**
* Add new notice to show in admin page.
Expand Down
8 changes: 5 additions & 3 deletions inc/admin/views/admin-notices/beta-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
exit; // Exit if accessed directly
}

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

<div id="lp-mes-beta-version">
<div id="lp-mes-beta-version" class="lp-admin-notice">
<p>
The LearnPress plugin has new the beta version.
</p>
Expand All @@ -24,5 +24,7 @@
);
?>
</p>
<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
<button type="button" class="notice-dismiss btn-lp-notice-dismiss" data="lp-beta-version">
<span class="screen-reader-text">Dismiss this notice.</span>
</button>
</div>
8 changes: 6 additions & 2 deletions inc/admin/views/admin-notices/wp-remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
exit; // Exit if accessed directly
}

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

if ( ! is_wp_error( $data['check'] ) ) {
return;
}
?>

<div class="notice <?php echo esc_attr( $data['class'] ?? '' ); ?>">
<div class="lp-admin-notice">
<p>
<?php echo sprintf( '%s %s', '<strong>wp_remote_get</strong>: ', $data['error'] ?? '' ); ?>
</p>
Expand Down
4 changes: 2 additions & 2 deletions inc/admin/views/admin-notices/wrong-name-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
exit; // Exit if accessed directly
}

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

<div class="notice <?php echo esc_attr( $data['class'] ?? '' ); ?>">
<div class="lp-admin-notice">
<p>
<?php
printf(
Expand Down
6 changes: 6 additions & 0 deletions inc/class-lp-page-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,12 @@ public function learn_press_edit_admin_bar() {
* Set in param notify_url on @see LP_Gateway_Paypal::get_paypal_args()
*/
public function check_webhook_paypal_ipn() {
// Start session, code temporary, will rewrite on hook init.
if ( ! session_id() ) {
session_start();
}
// End - tungnx

//error_log( 'xxx:' . json_encode( $_POST, JSON_UNESCAPED_UNICODE ) );

// Paypal payment done
Expand Down
58 changes: 30 additions & 28 deletions inc/rest-api/v1/admin/class-lp-admin-rest-tools-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,40 +194,42 @@ public function clean_tables( WP_REST_Request $request ) {
* @param WP_REST_Request $request
*
* @return void
* @since 4.1.7.3.2
* @version 1.0.0
*/
public function admin_notices( WP_REST_Request $request ) {
$response = new LP_REST_Response();
$content = '';

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' ] ),
],
'show_beta_version' => [
'class' => 'notice-warning is-dismissible',
'template' => 'admin-notices/beta-version.php',
'display' => 1,
],
];

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();
}

$content .= learn_press_admin_view( $template_data['template'] ?? '', [ 'data' => $template_data ], true, true );
}
$params = $request->get_params();
$admin_notices = $_SESSION['lp_admin_notices_dismiss'] ?? [];

if ( isset( $params['dismiss'] ) ) {
$admin_notices[] = $params['dismiss'];
$_SESSION['lp_admin_notices_dismiss'] = $admin_notices;
}

$rules = apply_filters(
'learn-press/admin-notices',
[
'check_wp_remote' => [
'template' => 'admin-notices/wp-remote.php',
'check' => call_user_func( [ 'LP_Admin_Ajax', 'check_wp_remote' ] ),
],
'check_right_plugin_base' => [
'template' => 'admin-notices/wrong-name-plugin.php',
'check' => call_user_func( [ 'LP_Admin_Notice', 'check_right_plugin_base' ] ),
],
'lp-beta-version' => [
'template' => 'admin-notices/beta-version.php',
'display' => 1 && ! in_array( 'lp-beta-version', $admin_notices ),
],
]
);

foreach ( $rules as $template_data ) {
$content .= learn_press_admin_view( $template_data['template'] ?? '', [ 'data' => $template_data ], true, true );
}

$response->status = 'success';
Expand Down

0 comments on commit f66a37f

Please sign in to comment.