id; } /** * Get the action name for the notice * * @return string */ public function get_action_name() : string { return self::ACTION_PREFIX . '_' . $this->get_id(); } public function show(): bool { if ( ! $this->conditions ) { return false; } if ( $this->is_dismissible && $this->is_dismissed() ) { return false; } if ( $this->capability && ! current_user_can( $this->capability ) ) { return false; } return true; } public function is_dismissed() : bool { if ( $this->per_user ) { $dismissed = get_user_meta( get_current_user_id(), self::DISMISSED_NOTICES, true ); } else { $dismissed = get_option( self::DISMISSED_NOTICES, [] ); } return in_array( $this->get_id(), (array) $dismissed ); } public function dismiss(): void { $dismissed = get_option( self::DISMISSED_NOTICES, [] ); $dismissed[] = $this->get_id(); update_option( self::DISMISSED_NOTICES, $dismissed, false ); } public function undismiss(): void { $dismissed = get_option( self::DISMISSED_NOTICES, [] ); if ( ! in_array( $this->get_id(), $dismissed ) ) { return; } $dismissed = array_diff( $dismissed, [ $this->get_id() ] ); update_option( self::DISMISSED_NOTICES, $dismissed, false ); } public function dismiss_per_user(): void { $user_id = get_current_user_id(); if ( ! $user_id ) { wp_send_json_error( [ 'message' => 'Invalid user' ] ); } $dismissed = get_user_meta( $user_id, self::DISMISSED_NOTICES, true ); if ( ! $dismissed ) { $dismissed = []; } $dismissed[] = $this->get_id(); update_user_meta( $user_id, self::DISMISSED_NOTICES, $dismissed ); } public function handle_dismiss() { if ( ! $this->is_dismissible ) { return; } if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ), $this->get_action_name() ) ) { wp_send_json_error( [ 'message' => 'Invalid nonce' ] ); } if ( $this->per_user ) { $this->dismiss_per_user(); } else { $this->dismiss(); } wp_send_json_success( [] ); } public function render() { echo sprintf( '