|null */ private ?array $last = null; /** * @param PostTypes $post_types Available post types. * @param Options $options Saved settings. */ public function __construct( private PostTypes $post_types, private Options $options ) {} /** * Cleans submitted settings. * * @param mixed $input Raw submitted value. * @return array Settings by name. */ public function sanitize( mixed $input ): array { // WordPress calls the sanitizer a second time with its own output on the first save. if ( is_array( $input ) && null !== $this->last && $input === $this->last ) { return $input; } $input = is_array( $input ) ? $input : array(); $selectors = new Selector(); $content = $selectors->parse_list( isset( $input['content_selector'] ) ? sanitize_text_field( $input['content_selector'] ) : '' ); $strip = $selectors->parse_list( isset( $input['strip_selectors'] ) ? sanitize_textarea_field( $input['strip_selectors'] ) : '' ); $rules = new HeadingRules( $selectors ); $headings = $rules->parse( isset( $input['heading_selectors'] ) ? sanitize_textarea_field( $input['heading_selectors'] ) : '' ); $this->report_invalid( array_merge( $content['invalid'], $strip['invalid'], $headings['invalid'] ) ); $post_types = isset( $input['excluded_post_types'] ) && is_array( $input['excluded_post_types'] ) ? array_values( array_intersect( $this->post_types->available(), array_map( 'sanitize_key', $input['excluded_post_types'] ) ) ) : array(); $terms = isset( $input['excluded_terms'] ) && is_array( $input['excluded_terms'] ) ? array_values( array_filter( array_map( 'absint', $input['excluded_terms'] ) ) ) : array(); $this->last = array_merge( array( 'excluded_post_types' => $post_types, 'excluded_posts' => isset( $input['excluded_posts'] ) ? sanitize_textarea_field( $input['excluded_posts'] ) : '', 'excluded_terms' => $terms, 'exclude_noindex' => ! empty( $input['exclude_noindex'] ), 'exclude_restricted' => ! empty( $input['exclude_restricted'] ), 'content_selector' => implode( ', ', $content['valid'] ), 'strip_selectors' => implode( "\n", $strip['valid'] ), 'heading_selectors' => $rules->to_text( $headings['valid'] ), 'title_source' => isset( $input['title_source'] ) ? ( 'post' === $input['title_source'] ? 'post' : 'page' ) : $this->options->get( 'title_source', 'page' ), 'title_strip_site' => isset( $input['title_strip_site'] ) ? ! empty( $input['title_strip_site'] ) : (bool) $this->options->get( 'title_strip_site', true ), 'show_meta_box' => ! empty( $input['show_meta_box'] ), 'http_auth_enabled' => ! empty( $input['http_auth_enabled'] ), 'allowed_environments' => isset( $input['allowed_environments'] ) && is_array( $input['allowed_environments'] ) ? array_values( array_intersect( Environment::OPTIONAL, array_map( 'sanitize_key', $input['allowed_environments'] ) ) ) : array(), 'home_markdown_url' => isset( $input['home_markdown_url'] ) && Urls::HOME_LONG === $input['home_markdown_url'] ? Urls::HOME_LONG : Urls::HOME_SHORT, 'markdown_by_accept' => ! empty( $input['markdown_by_accept'] ), 'allowed_urls' => $this->allowed_urls( $input ), 'delete_data' => ! empty( $input['delete_data'] ), ), $this->http_auth( $input ) ); return $this->last; } /** * Cleans the list of allowed addresses. When the list is set in wp-config.php the saved value is kept. * * @param array $input Raw submitted values. * @return string One address per line. */ private function allowed_urls( array $input ): string { if ( defined( 'AGENTWELCOME_ALLOWED_URLS' ) ) { return (string) $this->options->get( 'allowed_urls', '' ); } return implode( "\n", Environment::parse_urls( isset( $input['allowed_urls'] ) ? sanitize_textarea_field( $input['allowed_urls'] ) : '' ) ); } /** * Shows a warning that lists selectors that cannot be used. * * @param string[] $invalid Selectors that were ignored. * @return void */ private function report_invalid( array $invalid ): void { if ( empty( $invalid ) ) { return; } add_settings_error( Options::KEY, 'agentwelcome_invalid_selector', sprintf( /* translators: %s: list of CSS selectors. */ __( 'These selectors are not supported, or have no heading level such as => h3, and were ignored: %s', 'agentwelcome' ), esc_html( implode( ', ', $invalid ) ) ), 'warning' ); } /** * Cleans the HTTP Basic credentials. The password is encrypted, and the saved one is kept when none is submitted. * * @param array $input Raw submitted values. * @return array The user and the encrypted password. */ private function http_auth( array $input ): array { $current = $this->options->all(); if ( $this->options->http_auth_from_constants() ) { return array( 'http_auth_user' => (string) $current['http_auth_user'], 'http_auth_password' => (string) $current['http_auth_password'], ); } $user = isset( $input['http_auth_user'] ) ? sanitize_text_field( $input['http_auth_user'] ) : ''; if ( '' === $user ) { return array( 'http_auth_user' => '', 'http_auth_password' => '', ); } $password = isset( $input['http_auth_password'] ) ? (string) preg_replace( '/[\x00-\x1F\x7F]/', '', (string) $input['http_auth_password'] ) : ''; return array( 'http_auth_user' => $user, 'http_auth_password' => '' === $password ? (string) $current['http_auth_password'] : Secret::encrypt( $password ), ); } }