Skip to content

Instantly share code, notes, and snippets.

@csaborio001
Created May 23, 2021 04:18
Show Gist options
  • Save csaborio001/d5a8fa84fba3ff4ff22d43f64179feba to your computer and use it in GitHub Desktop.
Save csaborio001/d5a8fa84fba3ff4ff22d43f64179feba to your computer and use it in GitHub Desktop.
<?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