status_presenter = $status_presenter; $this->myyoast_connection_conditional = $myyoast_connection_conditional; $this->callback_handler = $callback_handler; $this->short_link_helper = $short_link_helper; $this->endpoints_repository = $endpoints_repository; $this->connection_permission = $connection_permission; } /** * Returns the MyYoast connection payload, or `null` when the feature flag * is disabled so the Integrations page can omit the key entirely. * * The `callbackOutcome` slot is populated (and consumed) when an OAuth * callback finished for this user since the last time the Integrations page * was rendered, so the React app can surface a one-shot notification. * * @return array{initialStatus: array{is_provisioned: bool, is_registered: bool, registered_at: int|null, registered_at_iso: string|null, redirect_uris: array, redirect_uris_match: bool}, callbackOutcome: array{kind: string, key: string}|null, linkParams: array, startConnection: bool, endpoints: array}|null */ public function present(): ?array { if ( ! $this->myyoast_connection_conditional->is_met() ) { return null; } return [ 'initialStatus' => $this->status_presenter->present(), 'callbackOutcome' => $this->consume_callback_outcome(), 'linkParams' => $this->short_link_helper->get_query_params(), 'startConnection' => $this->should_auto_start_connection(), 'endpoints' => $this->endpoints_repository->get_all_endpoints()->to_paths_array(), ]; } /** * Whether the page was opened by the editor's "Connect to MyYoast" link and * should auto-start the connection flow. * * Verifies the one-time nonce so the auto-start trigger can't be forged from * another site, and re-checks the connect capability so only users who may * register a client trigger it. The actual register/authorize REST calls are * independently nonce-protected; this gate is defense-in-depth on the trigger. * * @return bool Whether to auto-start the connection flow. */ private function should_auto_start_connection(): bool { if ( ! isset( $_GET['start-myyoast-connection'] ) || ! $this->connection_permission->can_manage() ) { return false; } $nonce = isset( $_GET['_wpnonce'] ) ? \sanitize_text_field( \wp_unslash( $_GET['_wpnonce'] ) ) : ''; return \wp_verify_nonce( $nonce, 'wpseo-start-myyoast-connection' ) !== false; } /** * Reads and consumes the pending OAuth callback outcome for the current user * and shapes it for the React app. * * @return array{kind: string, key: string}|null The outcome, or null when none is pending. */ private function consume_callback_outcome(): ?array { $outcome = $this->callback_handler->consume_outcome( \get_current_user_id() ); if ( $outcome === null ) { return null; } if ( $outcome->is_success() ) { return [ 'kind' => 'success', 'key' => 'verify_success', ]; } return [ 'kind' => 'error', 'key' => $this->error_message_key( $outcome ), ]; } /** * Maps a failed callback outcome to the front-end message key. * * Translates the neutral, native-OAuth outcome into the message keys the * integrations-page JS understands (see `messageFor()` in * `myyoast-integration.js`). The same missing code means different things per * OAuth phase: a provider error other than `access_denied` is unexpected, * while a token-endpoint error other than `invalid_grant` is a generic token * failure. * * @param Callback_Outcome $outcome The failed callback outcome. * * @return string The message key the front-end maps to copy. */ private function error_message_key( Callback_Outcome $outcome ): string { if ( $outcome->get_error_phase() === Callback_Outcome::PHASE_PROVIDER ) { return ( $outcome->get_error_code() === 'access_denied' ) ? 'connection_cancelled' : 'unexpected_error'; } if ( $outcome->get_error_code() === 'invalid_grant' ) { return 'token_request_failed_invalid_grant'; } if ( $outcome->get_error_code() === null ) { return 'unexpected_error'; } return 'token_request_failed'; } }