Created
May 23, 2021 04:18
-
-
Save csaborio001/d5a8fa84fba3ff4ff22d43f64179feba to your computer and use it in GitHub Desktop.
This file contains 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