Forked from csaborio001/wordpress-notices-with-nonce.php
Last active
August 23, 2022 21:53
-
-
Save cdsaenz/92fa2c9e6c33fc9c195126828e62de2c to your computer and use it in GitHub Desktop.
WP notices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Proptotype | |
*/ | |
namespace DoggieRescue\Notices; | |
class AdminNotice { | |
public static function process_messages() { | |
$notice = self::get_admin_message(); | |
if ( ! empty( $notice ) && is_array( $notice ) ) { | |
$status = \array_key_exists( 'status' , $notice) ? $notice['status'] : 'success'; | |
$message = \array_key_exists('message', $notice) ? $notice['message'] : ''; | |
$nonce = \array_key_exists('nonce', $notice) ? $notice['nonce'] : ''; | |
if ( false === \wp_verify_nonce( $nonce, 'message_for__' . \get_current_user_id() ) ) { | |
return; | |
} | |
?> | |
<div class="notice notice-<?php echo $status ?> is-dismissible"> | |
<h3><?php echo \ucfirst( $status); ?></h3> | |
<p><?php echo $message ?></p> | |
</div> | |
<?php | |
} | |
} | |
public static function display_admin_message( $message, $status = 'success' ) { | |
$user_id = get_current_user_id(); | |
set_transient( | |
"{$user_id}_message", | |
array( | |
'message' => $message, | |
'status' => $status, | |
'nonce' => \wp_create_nonce( "message_for_{$user_id}"), | |
) | |
); | |
} | |
private static function get_admin_message() { | |
$transient = \get_transient( \get_current_user_id() . '_message' ); | |
if ( $transient ) { | |
\delete_transient( \get_current_user_id() . '_message' ); | |
} | |
return $transient; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment