| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace RenzoJohnson\Blocks\Transfer; |
| 6 |
|
| 7 |
\defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
final class PropertySanitizer { |
| 10 |
|
| 11 |
/** |
| 12 |
* @param array<mixed, mixed> $properties Imported properties. |
| 13 |
* @return array<string, mixed> |
| 14 |
*/ |
| 15 |
public function sanitize( array $properties ): array { |
| 16 |
$sanitized = array(); |
| 17 |
|
| 18 |
foreach ( $properties as $key => $value ) { |
| 19 |
if ( ! \is_string( $key ) ) { |
| 20 |
continue; |
| 21 |
} |
| 22 |
|
| 23 |
$key = \sanitize_key( $key ); |
| 24 |
|
| 25 |
if ( '' === $key ) { |
| 26 |
continue; |
| 27 |
} |
| 28 |
|
| 29 |
if ( 'form' === $key && \is_string( $value ) ) { |
| 30 |
$sanitized[ $key ] = \current_user_can( 'unfiltered_html' ) ? $value : \wp_kses_post( $value ); |
| 31 |
continue; |
| 32 |
} |
| 33 |
|
| 34 |
$sanitized[ $key ] = $this->sanitize_value( $value ); |
| 35 |
} |
| 36 |
|
| 37 |
return $sanitized; |
| 38 |
} |
| 39 |
|
| 40 |
private function sanitize_value( mixed $value ): mixed { |
| 41 |
if ( \is_array( $value ) ) { |
| 42 |
$sanitized = array(); |
| 43 |
|
| 44 |
foreach ( $value as $key => $nested ) { |
| 45 |
if ( \is_int( $key ) || \is_string( $key ) ) { |
| 46 |
$sanitized[ $key ] = $this->sanitize_value( $nested ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return $sanitized; |
| 51 |
} |
| 52 |
|
| 53 |
if ( null === $value ) { |
| 54 |
return ''; |
| 55 |
} |
| 56 |
|
| 57 |
if ( \is_scalar( $value ) ) { |
| 58 |
return \sanitize_text_field( (string) $value ); |
| 59 |
} |
| 60 |
|
| 61 |
return ''; |
| 62 |
} |
| 63 |
} |
| 64 |
|