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_SAVE_SETTINGS ); if ( ! AgentMySite_Options::is_connected() ) { $this->finish( null ); return; } $patterns = $this->submitted_patterns(); $error = AgentMySite_Exclude_Matcher::validate( $patterns ); if ( null !== $error ) { $this->finish( array( 'kind' => 'settings_invalid', 'error' => $error, 'patterns' => substr( $patterns, 0, self::ECHO_MAX_BYTES ), ) ); return; } AgentMySite_Options::save_preferences( AgentMySite_Options::sanitize_widget_enabled( $this->submitted_toggle() ), AgentMySite_Exclude_Matcher::normalize( $patterns ) ); $this->finish( array( 'kind' => 'settings_saved' ) ); } /** * Stop after redirecting. Overridden in tests. */ protected function terminate(): void { exit; } /** * The patterns text, sanitised as a textarea (one pattern per line), then * validated against the strict grammar in AgentMySite_Exclude_Matcher. */ private function submitted_patterns(): string { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified by check_admin_referer() in handle(). if ( ! isset( $_POST['exclude_patterns'] ) || ! is_string( $_POST['exclude_patterns'] ) ) { return ''; } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified by check_admin_referer() in handle(). return sanitize_textarea_field( wp_unslash( $_POST['exclude_patterns'] ) ); } /** * The toggle value as a key; the options class coerces it to '1' or '0'. * * @return string|null */ private function submitted_toggle(): ?string { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified by check_admin_referer() in handle(). if ( ! isset( $_POST['widget_enabled'] ) || ! is_string( $_POST['widget_enabled'] ) ) { return null; } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified by check_admin_referer() in handle(). return sanitize_key( wp_unslash( $_POST['widget_enabled'] ) ); } /** * Flash the outcome, if any, and return to the settings page. * * @param array|null $flash Notice. */ private function finish( ?array $flash ): void { if ( null !== $flash ) { AgentMySite_Flash::set( get_current_user_id(), $flash ); } wp_safe_redirect( AgentMySite_Connect::settings_url() ); $this->terminate(); } }