0.0 ? (string) $f : ''; } /** * Sanitize a JSON array string (e.g., for legacy aliases field or _pr_aliases). * * Accepts a JSON string or PHP array. Sanitizes each element individually. * Input is UNTRUSTED. * * @param mixed $value Raw input (JSON string or array). * @return string JSON array of sanitized strings, or '[]'. */ public static function sanitize_json_array( $value ): string { if ( is_array( $value ) ) { $value = wp_json_encode( array_map( 'sanitize_text_field', $value ) ); } $decoded = json_decode( (string) $value, true ); if ( ! is_array( $decoded ) ) { return '[]'; } return wp_json_encode( array_values( array_map( 'sanitize_text_field', $decoded ) ) ); } /** * Sanitize FAQ items JSON (for _pr_faq_items). * * Expects a JSON array of {question: string, answer: string} objects. * Sanitizes each question and answer individually. * Input is UNTRUSTED — malformed entries are silently dropped. * * @param mixed $value Raw input (JSON string or PHP array). * @return string JSON array of sanitized FAQ item objects, or '[]'. */ public static function sanitize_faq_items( $value ): string { if ( is_string( $value ) ) { $value = json_decode( $value, true ); } if ( ! is_array( $value ) ) { return '[]'; } $clean = []; foreach ( $value as $item ) { if ( ! is_array( $item ) ) { continue; } $question = sanitize_text_field( (string) ( $item['question'] ?? '' ) ); $answer = sanitize_textarea_field( (string) ( $item['answer'] ?? '' ) ); if ( '' !== $question && '' !== $answer ) { $clean[] = [ 'question' => $question, 'answer' => $answer, ]; } } return wp_json_encode( $clean, JSON_UNESCAPED_UNICODE ) ?: '[]'; } }