*/ public static function fieldKeysOfType( $fields, string $type ): array { $out = array(); self::collectFieldKeysOfType( $fields, $type, $out ); return $out; } /** * @param mixed $fields Schema fields. * @param string $type Field type. * @param list $out Accumulator. */ private static function collectFieldKeysOfType( $fields, string $type, array &$out ): void { if ( ! is_array( $fields ) ) { return; } foreach ( $fields as $field ) { if ( ! is_array( $field ) ) { continue; } if ( ( $field['type'] ?? '' ) === $type && ! empty( $field['key'] ) ) { $out[] = (string) $field['key']; } if ( ! empty( $field['fields'] ) ) { self::collectFieldKeysOfType( $field['fields'], $type, $out ); } if ( ! empty( $field['children'] ) ) { self::collectFieldKeysOfType( $field['children'], $type, $out ); } if ( ! empty( $field['columns'] ) && is_array( $field['columns'] ) ) { foreach ( $field['columns'] as $column ) { self::collectFieldKeysOfType( $column, $type, $out ); } } } } /** * Sanitize classic-form redisplay values (transient). * * Plain-text leaves use sanitizeSubmissionValue() so characters like "&" * are not entity-encoded by wp_kses. Only keys listed in $richTextKeys * (schema rich_text fields) keep allowlisted markup via sanitizeRichText(). * * @param mixed $arr Input array. * @param list $richTextKeys Field keys allowed to keep markup. * @param int $depth Current depth. * @param int $maxDepth Max nesting. * @return array */ public static function sanitizeClassicRedisplay( $arr, array $richTextKeys = array(), int $depth = 0, int $maxDepth = self::MAX_DEPTH ): array { if ( ! is_array( $arr ) ) { return array(); } if ( $depth >= $maxDepth ) { return array(); } $richLookup = array_fill_keys( $richTextKeys, true ); $sanitized = array(); foreach ( $arr as $key => $value ) { $sanitizedKey = self::sanitizeText( $key ); if ( is_array( $value ) ) { $sanitized[ $sanitizedKey ] = self::sanitizeClassicRedisplay( $value, $richTextKeys, $depth + 1, $maxDepth ); } elseif ( is_string( $value ) ) { $sanitized[ $sanitizedKey ] = isset( $richLookup[ $sanitizedKey ] ) ? self::sanitizeRichText( $value ) : self::sanitizeSubmissionValue( $value ); } elseif ( is_int( $value ) || is_float( $value ) || is_bool( $value ) || is_null( $value ) ) { $sanitized[ $sanitizedKey ] = $value; } else { $sanitized[ $sanitizedKey ] = self::sanitizeText( (string) $value ); } } return $sanitized; } public static function sanitizeArrayDeep( $arr, int $depth = 0, int $maxDepth = self::MAX_DEPTH ): array { if ( ! is_array( $arr ) ) { return array(); } // Prevent infinite recursion if ( $depth >= $maxDepth ) { return array(); } $sanitized = array(); foreach ( $arr as $key => $value ) { // Sanitize key $sanitizedKey = self::sanitizeText( $key ); // Sanitize value if ( is_array( $value ) ) { $sanitized[ $sanitizedKey ] = self::sanitizeArrayDeep( $value, $depth + 1, $maxDepth ); } elseif ( is_string( $value ) ) { $sanitized[ $sanitizedKey ] = self::sanitizeSubmissionValue( $value ); } elseif ( is_int( $value ) || is_float( $value ) || is_bool( $value ) || is_null( $value ) ) { $sanitized[ $sanitizedKey ] = $value; } else { // Convert objects to string $sanitized[ $sanitizedKey ] = self::sanitizeText( (string) $value ); } } return $sanitized; } /** * Sanitize JSON string * * Decodes JSON, sanitizes deeply, and returns sanitized array. * Throws ValidationException if JSON is invalid or exceeds size limit. * * @param mixed $json JSON string to sanitize * @param int $maxBytes Maximum allowed bytes * @return array Sanitized array * @throws ValidationException If JSON is invalid or too large */ public static function sanitizeJsonString( $json, int $maxBytes = self::MAX_JSON_BYTES ): array { if ( ! is_string( $json ) ) { throw new ValidationException( 'Invalid JSON: expected string', array( '__root' => 'JSON must be a string' ), 'invalid_json' ); } // Check size limit if ( strlen( $json ) > $maxBytes ) { // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped throw new ValidationException( 'JSON payload too large', array( '__root' => sprintf( 'Maximum size is %d bytes', $maxBytes ) ), 'payload_too_large' ); // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped } // Decode JSON $decoded = json_decode( $json, true ); if ( json_last_error() !== JSON_ERROR_NONE ) { // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped throw new ValidationException( 'Invalid JSON: ' . json_last_error_msg(), array( '__root' => 'JSON is malformed' ), 'invalid_json' ); // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped } // Sanitize decoded array if ( ! is_array( $decoded ) ) { return array(); } return self::sanitizeArrayDeep( $decoded ); } /** * Sanitize submission value (form payload field) * * Strips HTML tags and trims whitespace. * * @param mixed $value Value to sanitize * @return string */ /** * Allowed markup for rich_text submission values. * * Deliberately narrow. This is the one place a public, unauthenticated * submission may carry HTML, so the set is limited to paragraphs, line * breaks, bold and italic, lists and links, with no attributes beyond a * scheme-checked href. It mirrors the * frontend editor's allowlist so what the visitor sees is what is stored. * * @return array> wp_kses allowlist. */ public static function richTextAllowedHtml(): array { return array( 'b' => array(), 'strong' => array(), 'i' => array(), 'em' => array(), 'p' => array(), 'br' => array(), 'ul' => array(), 'ol' => array(), 'li' => array(), 'a' => array( 'href' => true, ), ); } /** * Sanitize a rich_text value, preserving the allowlisted markup. * * @param mixed $value Raw submitted value. * @return string Sanitized HTML. */ public static function sanitizeRichText( $value ): string { if ( is_array( $value ) || is_object( $value ) ) { return ''; } // wp_kses resolves protocols itself, so javascript: and data: hrefs are // dropped rather than merely escaped. return trim( wp_kses( (string) $value, self::richTextAllowedHtml(), array( 'http', 'https', 'mailto' ) ) ); } public static function sanitizeSubmissionValue( $value ): string { if ( is_array( $value ) || is_object( $value ) ) { return ''; } // Strip all HTML tags using WordPress function, then trim whitespace. return self::keepLessThan( (string) $value, static function ( string $text ): string { return trim( wp_strip_all_tags( $text ) ); } ); } /** * Run a sanitizer over an answer without losing a "<" that is plain text. * * A "<" that does not open a tag ("budget <5000", "I <3 it", "x>"). So the * pass is repeated until the answer no longer changes: what is returned is * what the sanitizer would leave alone, and it holds no tag. * * An unclosed tag start ("hi " ("{value}"). So a "<" followed by a * letter, "/", "!" or "?" is only kept when nothing after it reads as an * attribute ("name=", with a space or "/" before it): "x]*?((?=<)|>|$)%', static function ( $match ) { if ( '>' === $match[1] ) { return $match[0]; } // A run from "<" up to the next "<" or the end: no tag closes here, // so it is plain text, unless it looks like a tag start with an // attribute, which a later ">" could turn into a working tag. $run = $match[0]; if ( preg_match( '%^<[A-Za-z/!?]%', $run ) && preg_match( '%[\s/][A-Za-z_:][\w:.-]*\s*=%', $run ) ) { return substr( $run, 1 ); } return str_replace( '<', self::LESS_THAN_MARK, $run ); }, $value ) ?? $value; return str_replace( self::LESS_THAN_MARK, '<', (string) $sanitizer( $value ) ); } }