| @@ -20,13 +20,23 @@ | ||
| 20 | 20 | /** |
| 21 | 21 | * AdminNotifier constructor. |
| 22 | 22 | */ |
| 23 | 23 | public function __construct() { |
| 24 | - $this->key .= get_current_user_id(); | |
| 25 | - $this->process(); | |
| 24 | + // Flash messages are processed on admin_init: the current user is not known yet when the plugin boots, | |
| 25 | + // and processing on every request would let a frontend visit consume a notice meant for an admin. | |
| 26 | + add_action( 'admin_init', array( $this, 'process' ) ); | |
| 26 | 27 | } |
| 27 | 28 | |
| 28 | 29 | /** |
| 30 | + * Per-user notification key. Resolved lazily so the current user is already determined. | |
| 31 | + * | |
| 32 | + * @return string | |
| 33 | + */ | |
| 34 | + private function getKey(): string { | |
| 35 | + return $this->key . get_current_user_id(); | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 29 | 39 | * Add message to show on admin_notices action |
| 30 | 40 | * |
| 31 | 41 | * @param string $message |
| 32 | 42 | * @param string $type |
| @@ -47,9 +57,9 @@ | ||
| 47 | 57 | * @param bool $isDismissible |
| 48 | 58 | */ |
| 49 | 59 | public function flash( $message, $type = self::SUCCESS, $isDismissible = false ) { |
| 50 | 60 | $message = array( 'message' => $message, 'type' => $type, 'dismissible' => $isDismissible ); |
| 51 | - $messages = get_transient( $this->key ); | |
| 61 | + $messages = get_transient( $this->getKey() ); | |
| 52 | 62 | |
| 53 | 63 | if ( ! is_array( $messages ) ) { |
| 54 | 64 | $messages = array(); |
| 55 | 65 | } |
| @@ -55,22 +65,22 @@ | ||
| 55 | 65 | } |
| 56 | 66 | |
| 57 | 67 | $messages[] = $message; |
| 58 | 68 | |
| 59 | - set_transient( $this->key, $messages, MINUTE_IN_SECONDS ); | |
| 69 | + set_transient( $this->getKey(), $messages, MINUTE_IN_SECONDS ); | |
| 60 | 70 | } |
| 61 | 71 | |
| 62 | 72 | /** |
| 63 | 73 | * Show flash messages |
| 64 | 74 | */ |
| 65 | - private function process() { | |
| 66 | - $messages = get_transient( $this->key ); | |
| 75 | + public function process() { | |
| 76 | + $messages = get_transient( $this->getKey() ); | |
| 67 | 77 | |
| 68 | 78 | //Resolve conflict with background process |
| 69 | 79 | if ( ! wp_doing_ajax() ) { |
| 70 | 80 | if ( is_array( $messages ) ) { |
| 71 | 81 | |
| 72 | - delete_transient( $this->key ); | |
| 82 | + delete_transient( $this->getKey() ); | |
| 73 | 83 | |
| 74 | 84 | foreach ( $messages as $message ) { |
| 75 | 85 | $this->push( $message['message'], $message['type'], $message['dismissible'] ); |
| 76 | 86 | } |