client = null === $client ? new AgentMySite_Exchange_Client() : $client; } /** * `admin_init` callback. */ public static function on_admin_init(): void { ( new self() )->handle( self::request_query() ); } /** * The return parameters, sanitised and limited to the keys handle() reads. * Anything that is not a string is dropped; handle() verifies the nonce. * * @return array */ public static function request_query(): array { $query = array(); // phpcs:disable WordPress.Security.NonceVerification.Recommended -- handle() verifies the return nonce itself. if ( isset( $_GET['page'] ) && is_string( $_GET['page'] ) ) { $query['page'] = sanitize_key( wp_unslash( $_GET['page'] ) ); } if ( isset( $_GET['ams_code'] ) && is_string( $_GET['ams_code'] ) ) { $query['ams_code'] = sanitize_text_field( wp_unslash( $_GET['ams_code'] ) ); } if ( isset( $_GET['ams_nonce'] ) && is_string( $_GET['ams_nonce'] ) ) { $query['ams_nonce'] = sanitize_text_field( wp_unslash( $_GET['ams_nonce'] ) ); } // phpcs:enable WordPress.Security.NonceVerification.Recommended return $query; } /** * Process a return. * * @param array $query Unslashed query parameters. */ public function handle( array $query ): void { if ( ! isset( $query['page'], $query['ams_code'] ) || AgentMySite_Connect::PAGE_SLUG !== $query['page'] ) { return; } if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to manage this plugin.', 'agentmysite' ), '', array( 'response' => 403 ) ); } $user_id = get_current_user_id(); $nonce = isset( $query['ams_nonce'] ) ? $query['ams_nonce'] : null; if ( ! is_string( $nonce ) || ! wp_verify_nonce( $nonce, AgentMySite_Connect::RETURN_NONCE_ACTION ) ) { $this->finish( $user_id, self::error( 'expired' ) ); return; } $code = $query['ams_code']; if ( ! AgentMySite_Pkce::is_token( $code ) ) { $this->finish( $user_id, self::error( AgentMySite_Error_Copy::UNKNOWN ) ); return; } $verifier = AgentMySite_Verifier_Store::take( $user_id ); if ( null === $verifier ) { $this->finish( $user_id, self::error( 'expired' ) ); return; } $result = $this->client->exchange( $code, $verifier, AgentMySite_Connect::site_url() ); if ( ! $result['ok'] ) { $this->finish( $user_id, self::error( $result['code'] ) ); return; } $catalog_status = isset( $result['catalog_status'] ) && is_string( $result['catalog_status'] ) ? $result['catalog_status'] : null; AgentMySite_Options::save_connection( $result['site_key'], $result['site_name'], null, null, $catalog_status ); $this->finish( $user_id, array( 'kind' => 'connected', 'sitemap_status' => $result['sitemap_status'], 'pages_queued' => $result['pages_queued'], 'catalog_status' => $catalog_status, ) ); } /** * Stop after redirecting. Overridden in tests. */ protected function terminate(): void { exit; } /** * Flash the outcome and return to the clean settings page. * * @param int $user_id User. * @param array $flash Notice. */ private function finish( int $user_id, array $flash ): void { AgentMySite_Flash::set( $user_id, $flash ); wp_safe_redirect( AgentMySite_Connect::settings_url() ); $this->terminate(); } /** * Error notice. * * @param string $code Failure code. */ private static function error( string $code ): array { return array( 'kind' => 'error', 'code' => $code, ); } }