handle(); } /** * Handle the form post. */ public function handle(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to manage this plugin.', 'agentmysite' ), '', array( 'response' => 403 ) ); } check_admin_referer( AgentMySite_Settings_Page::ACTION_MANUAL_KEY ); $key = $this->submitted_key(); if ( ! AgentMySite_Key::is_valid( $key ) ) { $this->finish( array( 'kind' => 'manual_key_invalid' ) ); return; } AgentMySite_Options::save_connection( $key, '', null, null ); $this->finish( array( 'kind' => 'manual_key_saved' ) ); } /** * Stop after redirecting. Overridden in tests. */ protected function terminate(): void { exit; } /** * The submitted key, sanitized and trimmed. It is accepted only if it then * matches the exact key format. */ private function submitted_key(): string { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified by check_admin_referer() in handle(). if ( ! isset( $_POST['site_key'] ) || ! is_string( $_POST['site_key'] ) ) { return ''; } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified by check_admin_referer() in handle(). return trim( sanitize_text_field( wp_unslash( $_POST['site_key'] ) ) ); } /** * Flash the outcome and return to the settings page. * * @param array $flash Notice. */ private function finish( array $flash ): void { AgentMySite_Flash::set( get_current_user_id(), $flash ); wp_safe_redirect( AgentMySite_Connect::settings_url() ); $this->terminate(); } }