Skip to content

Commit

Permalink
Add ajax for admin notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
seothemes committed Aug 9, 2023
1 parent 0519dd3 commit ede7f86
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 40 deletions.
123 changes: 87 additions & 36 deletions includes/admin/class-llms-admin-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,32 @@ class LLMS_Admin_Notifications {
* @return void
*/
public function __construct() {
add_action( 'current_screen', [ $this, 'show_notifications' ] );
add_action( 'current_screen', [ $this, 'dismiss_notification' ], 11 );
add_action( 'wp_ajax_llms_dismiss_notification', [ $this, 'dismiss_notification' ] );
add_action( 'wp_ajax_llms_show_notification', [ $this, 'show_notification' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
}

/**
* AJAX callback to check and display notifications.
*
* Checks permissions, retrieves the next notification, checks if notifications are paused,
* and finally, outputs the next notification if one is available.
* Enqueue admin scripts.
*
* @since [version]
*
* @param WP_Screen $current_screen WP_Screen instance.
* @return void
*/
public function show_notifications( WP_Screen $current_screen ): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}

if ( ! str_contains( $current_screen->base, 'llms' ) && ! str_contains( $current_screen->id, 'llms' ) ) {
return;
}

$notification = $this->get_next_notification();

if ( null === $notification || $this->is_paused() ) {
return;
}

$this->display_notification( $notification );
public function admin_scripts() {
$handle = 'llms-admin-notifications';

llms()->assets->enqueue_script( $handle );

wp_localize_script(
$handle,
'llmsAdminNotifications',
[
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'llms_admin_notification_nonce' ),
'paused' => $this->is_paused() ? 'true' : 'false',
]
);
}

/**
Expand All @@ -69,21 +64,71 @@ public function show_notifications( WP_Screen $current_screen ): void {
* @return void
*/
public function dismiss_notification(): void {
if ( ! wp_verify_nonce( llms_filter_input( INPUT_GET, 'llms_admin_notification_nonce' ), 'llms_admin_notification_nonce' ) ) {
return;
if ( ! wp_verify_nonce( llms_filter_input( INPUT_POST, 'nonce' ), 'llms_admin_notification_nonce' ) ) {
wp_send_json_error( __( 'Invalid nonce.', 'lifterlms' ) );
}

$id = llms_filter_input( INPUT_GET, 'llms_admin_notification_pause', FILTER_SANITIZE_NUMBER_INT );
$id = llms_filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT );

if ( ! $id ) {
return;
wp_send_json_error( __( 'Invalid notification ID.', 'lifterlms' ) );
}

$current_user = get_current_user_id();

if ( ! $current_user ) {
wp_send_json_error( __( 'Invalid user.', 'lifterlms' ) );
}

$current_user = get_current_user_id();
$archived = $this->get_archived_notifications();
$archived[ $id ] = date_i18n( 'c' );

update_user_meta( $current_user, 'llms_archived_notifications', $archived );

wp_send_json_success();
}

/**
* AJAX callback to check and display notifications.
*
* Checks permissions, retrieves the next notification, checks if notifications are paused,
* and finally, outputs the next notification if one is available.
*
* @since [version]
*
* @return void
*/
public function show_notification(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'You do not have permission to view notifications.', 'lifterlms' ) );
}

$nonce = llms_filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING );

if ( ! wp_verify_nonce( $nonce, 'llms_admin_notification_nonce' ) ) {
wp_send_json_error( __( 'Invalid nonce.', 'lifterlms' ) );
}

$notification = $this->get_next_notification();

if ( null === $notification ) {
wp_send_json_error( __( 'No notifications available.', 'lifterlms' ) );
}

$html = $this->display_notification( $notification );

if ( ! $html ) {
wp_send_json_error( __( 'An error occurred while displaying the notification.', 'lifterlms' ) );
} else {

$html = str_replace(
array( "\n", "\r", "\t" ),
array( '', '', '' ),
$html
);

wp_send_json_success( $html );
}
}

/**
Expand Down Expand Up @@ -124,13 +169,19 @@ private function get_next_notification(): ?stdClass {
private function get_notifications(): array {
$name = 'llms_notificiations_' . llms()->version;
$notifications = get_transient( $name );
$debug_id = $this->get_debug_id();

if ( ! $notifications || $this->get_debug_id() ) {
if ( ! $notifications || $debug_id ) {
$notifications = $this->fetch_notifications();

set_transient( $name, $notifications, DAY_IN_SECONDS );
}

foreach ( $notifications as $key => $notification ) {
if ( $debug_id && $notification->id === $debug_id ) {
return [ $notification ];
}

if ( ! $this->is_applicable( $notification ) ) {
unset( $notifications[ $key ] );
}
Expand Down Expand Up @@ -165,11 +216,7 @@ private function get_notifications(): array {
// Map properties.
$notification->html = wp_kses( $notification->content ?? '', $allowed_html );
$notification->icon = $notification->icon ?? $notification->dashicon ?? 'lifterlms';
$notification->dismiss_url = wp_nonce_url(
add_query_arg( 'llms_admin_notification_pause', $notification->id ),
'llms_admin_notification_nonce',
'llms_admin_notification_nonce'
);
$notification->dismiss_url = '';

if ( ( $notification->type ?? '' ) === 'general' ) {
$notification->type = 'info';
Expand Down Expand Up @@ -253,10 +300,14 @@ private function filter_and_sort_notifications( array $notifications ): array {
* @since [version]
*
* @param object $notification The notification object.
* @return void
* @return string
*/
private function display_notification( object $notification ): void {
private function display_notification( object $notification ): string {
ob_start();

llms_get_template( 'admin/notices/notice.php', (array) $notification );

return ob_get_clean();
}

/**
Expand Down
5 changes: 5 additions & 0 deletions includes/assets/llms-assets-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
'asset_file' => true,
'suffix' => '',
),
'llms-admin-notifications' => array(
'asset_file' => true,
'suffix' => '',
'dependencies' => array( 'jquery' ),
),

// Modules.
'llms-components' => array(
Expand Down
80 changes: 80 additions & 0 deletions src/js/admin-notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
( ( $ ) => {
const llmsAdminNotifications = window?.llmsAdminNotifications || {};

if ( ! llmsAdminNotifications ) {
return;
}

if ( llmsAdminNotifications?.paused === 'true' ) {
return;
}

$.post(
llmsAdminNotifications.ajaxurl,
{
'action': 'llms_show_notification',

Check failure on line 15 in src/js/admin-notifications.js

View workflow job for this annotation

GitHub Actions / lint

Unnecessarily quoted property 'action' found
'nonce': llmsAdminNotifications.nonce,

Check failure on line 16 in src/js/admin-notifications.js

View workflow job for this annotation

GitHub Actions / lint

Unnecessarily quoted property 'nonce' found
},
( response ) => {
console.log(response);

Check failure on line 19 in src/js/admin-notifications.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 19 in src/js/admin-notifications.js

View workflow job for this annotation

GitHub Actions / lint

There must be a space after this paren

Check failure on line 19 in src/js/admin-notifications.js

View workflow job for this annotation

GitHub Actions / lint

There must be a space before this paren

if ( ! response?.success ) {
return;
}

const data = response?.data ?? {};

if ( ! data ) {
return;
}

let insideWrap = document.querySelectorAll( '.lifterlms-settings form > .llms-inside-wrap' )[0];

Check failure on line 31 in src/js/admin-notifications.js

View workflow job for this annotation

GitHub Actions / lint

A space is required after '['

Check failure on line 31 in src/js/admin-notifications.js

View workflow job for this annotation

GitHub Actions / lint

A space is required before ']'

if ( ! insideWrap ) {
insideWrap = document.querySelectorAll( '.lifterlms-settings .llms-inside-wrap > *' )[0];

Check failure on line 34 in src/js/admin-notifications.js

View workflow job for this annotation

GitHub Actions / lint

A space is required after '['

Check failure on line 34 in src/js/admin-notifications.js

View workflow job for this annotation

GitHub Actions / lint

A space is required before ']'
}

if ( ! insideWrap ) {
return;
}

// Convert response to DOM element.
const parser= new DOMParser();

Check failure on line 42 in src/js/admin-notifications.js

View workflow job for this annotation

GitHub Actions / lint

Operator '=' must be spaced
const parsed = parser.parseFromString( data, 'text/html' );
const div= parsed.querySelector( 'div' );

if ( ! div ) {
return;
}

insideWrap.parentNode.insertBefore( div, insideWrap );

const id = div.getAttribute( 'id' )?.replace( 'llms-notice-', '' );

const dismissButton = div.querySelector( '.notice-dismiss' );

if ( ! dismissButton ) {
return;
}

dismissButton.addEventListener(
'click',
() => {
$.post(
llmsAdminNotifications.ajaxurl,
{
'action': 'llms_dismiss_notification',
'nonce': llmsAdminNotifications.nonce,
'id': id,
},
( response ) => {
if ( response?.success ) {
div.remove();
}
}
);
}
);
}
);
} )( jQuery );
4 changes: 2 additions & 2 deletions templates/admin/notices/notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
$remind_url = $remind_url ?? null;

?>
<div class="notice notice-<?php echo esc_attr( $type ); ?> llms-admin-notice" id="llms-notice<?php echo absint( $id ); ?>" style="position:relative;">
<div class="notice notice-<?php echo esc_attr( $type ); ?> llms-admin-notice" id="llms-notice-<?php echo absint( $id ); ?>" style="position:relative;">
<div class="llms-admin-notice-icon">
<?php if ( 'lifterlms' === $icon ) : ?>
<div class="llms-admin-notice-lifterlms-icon">
Expand All @@ -61,7 +61,7 @@

<div class="llms-admin-notice-content">
<?php if ( $dismissible ) : ?>
<a class="notice-dismiss" href="<?php echo esc_url( $dismiss_url ); ?>">
<a class="notice-dismiss" <?php echo $dismiss_url ? 'href="' . esc_url( $dismiss_url ) . '"' : ''; ?>>
<span class="screen-reader-text"><?php esc_html_e( 'Dismiss', 'lifterlms' ); ?></span>
</a>
<?php endif; ?>
Expand Down
5 changes: 3 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const { resolve } = require( 'path' ),
'admin-addons',
'admin-award-certificate',
'admin-certificate-editor',
'admin-notifications',
'quill-wordcount',

// Module packages.
Expand All @@ -24,7 +25,7 @@ const { resolve } = require( 'path' ),
'spinner',
'utils',
],
css: [
css: [
'admin-addons'
],
} );
Expand All @@ -46,7 +47,7 @@ config.plugins.push( new CleanWebpackPlugin( {

// config.entry.fontawesome = resolve( './src/scss/fontawesome.scss' );

module.exports = [
module.exports = [
blocksConfig,
config
];

0 comments on commit ede7f86

Please sign in to comment.