api = $api; } /** * Hooks the notice and its dismiss handler. */ public function register(): void { add_action( 'admin_notices', array( $this, 'render' ) ); add_action( 'admin_post_' . self::DISMISS_ACTION, array( $this, 'handle_dismiss' ) ); } /** * Renders the notice when every condition holds. */ public function render(): void { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( null === $screen || ! in_array( $screen->id, array( 'dashboard', 'plugins' ), true ) ) { return; } if ( ! current_user_can( Capabilities::MANAGE ) || $this->api->has_credentials() || '' !== (string) get_user_meta( get_current_user_id(), self::DISMISS_META, true ) ) { return; } $dismiss_url = wp_nonce_url( admin_url( 'admin-post.php?action=' . self::DISMISS_ACTION ), self::DISMISS_ACTION ); printf( '
', esc_html__( 'Mapsemble is almost ready — enter your API credentials under', 'mapsemble' ), esc_url( SettingsPage::url() ), esc_html__( 'Mapsemble → Settings', 'mapsemble' ), esc_url( $dismiss_url ), esc_html__( 'Dismiss', 'mapsemble' ) ); } /** * Persists the per-user dismissal and returns to where the user was. */ public function handle_dismiss(): void { check_admin_referer( self::DISMISS_ACTION ); if ( ! current_user_can( Capabilities::MANAGE ) ) { wp_die( esc_html__( 'You are not allowed to dismiss this notice.', 'mapsemble' ), '', array( 'response' => 403 ) ); } update_user_meta( get_current_user_id(), self::DISMISS_META, '1' ); wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url() ); exit; } }