| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn; |
| 4 |
|
| 5 |
use Forge12\Shared\Logger; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class SanitizeHelper { |
| 12 |
/** |
| 13 |
* Sanitizes an array by sanitizing the keys and values recursively. |
| 14 |
* |
| 15 |
* @param array $data The array to be sanitized. |
| 16 |
* |
| 17 |
* @return array The sanitized array. |
| 18 |
*/ |
| 19 |
public static function sanitize_array( $data ): array { |
| 20 |
$logger = Logger::getInstance(); |
| 21 |
$logger->info( 'Starting sanitization of an array.', [ |
| 22 |
'plugin' => 'double-opt-in', |
| 23 |
'data_type' => gettype($data), |
| 24 |
] ); |
| 25 |
|
| 26 |
if ( ! is_array( $data ) ) { |
| 27 |
$logger->warning( 'Provided data is not an array. Returning an empty array.', [ |
| 28 |
'plugin' => 'double-opt-in', |
| 29 |
'data_type' => gettype($data), |
| 30 |
] ); |
| 31 |
return []; |
| 32 |
} |
| 33 |
|
| 34 |
$sanitized_data = []; |
| 35 |
foreach ( $data as $key => $value ) { |
| 36 |
$sanitized_key = sanitize_text_field( $key ); |
| 37 |
|
| 38 |
$do_sanitize_key_filter = apply_filters('f12_cf7_doubleoptin_do_sanitize_key_'.$key, true); |
| 39 |
$logger->debug( 'Applying sanitization filter for key: ' . $key, [ |
| 40 |
'plugin' => 'double-opt-in', |
| 41 |
'filter_result' => $do_sanitize_key_filter, |
| 42 |
] ); |
| 43 |
|
| 44 |
// Use a more explicit check for 'body' to avoid issues if the filter returns false for a different reason |
| 45 |
if ( ! $do_sanitize_key_filter || $key === 'body' ) { |
| 46 |
$sanitized_data[$key] = $value; |
| 47 |
$logger->info( 'Skipping sanitization for key "' . $key . '" based on filter or key name.', [ |
| 48 |
'plugin' => 'double-opt-in', |
| 49 |
] ); |
| 50 |
continue; |
| 51 |
} |
| 52 |
|
| 53 |
if ( is_array( $value ) ) { |
| 54 |
$sanitized_data[ $sanitized_key ] = self::sanitize_array( $value ); |
| 55 |
$logger->debug( 'Recursively sanitizing array value for key: ' . $sanitized_key, [ |
| 56 |
'plugin' => 'double-opt-in', |
| 57 |
] ); |
| 58 |
} else { |
| 59 |
$sanitized_data[ $sanitized_key ] = wp_kses_post( $value ); |
| 60 |
$logger->debug( 'Sanitizing string value for key "' . $sanitized_key . '" using wp_kses_post.', [ |
| 61 |
'plugin' => 'double-opt-in', |
| 62 |
] ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$logger->notice( 'Array sanitization completed successfully.', [ |
| 67 |
'plugin' => 'double-opt-in', |
| 68 |
] ); |
| 69 |
|
| 70 |
return $sanitized_data; |
| 71 |
} |
| 72 |
|
| 73 |
} |