| 1 |
<?php |
| 2 |
namespace Depicter\Utility; |
| 3 |
|
| 4 |
use \Averta\WordPress\Utility\Sanitize as SanitizeBase; |
| 5 |
|
| 6 |
class Sanitize extends SanitizeBase { |
| 7 |
|
| 8 |
public static function html( $input, $allowed_tags = null, $namespace = null, $auto_p = false ){ |
| 9 |
// A fix to allow empty data url for src in image tag |
| 10 |
if( $namespace === 'depicter/output' ){ |
| 11 |
add_filter( 'wp_kses_uri_attributes', [ __CLASS__, 'skipSrcEscapeTemporary' ], 25 ); |
| 12 |
} |
| 13 |
$sanitized = parent::html( $input, $allowed_tags, $namespace, $auto_p ); |
| 14 |
if( $namespace === 'depicter/output' ){ |
| 15 |
remove_filter( 'wp_kses_uri_attributes', [ __CLASS__, 'skipSrcEscapeTemporary' ], 25 ); |
| 16 |
} |
| 17 |
|
| 18 |
return $sanitized; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Retrieves default WordPress HTML tags |
| 23 |
* |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
protected static function defaultAllowedTags(){ |
| 27 |
$tags = parent::defaultAllowedTags(); |
| 28 |
|
| 29 |
$tags['style'] = [ |
| 30 |
'type' => true |
| 31 |
]; |
| 32 |
$tags['script'] = [ |
| 33 |
'id' => true, |
| 34 |
'src' => true |
| 35 |
]; |
| 36 |
$tags['link'] = [ |
| 37 |
'rel' => true, |
| 38 |
'id' => true, |
| 39 |
'href' => true, |
| 40 |
'media' => true, |
| 41 |
]; |
| 42 |
return $tags; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Ignore src escaping because `wp_kses` strips `data:` from image placeholder source in PHP 8.0+ |
| 47 |
* |
| 48 |
* @param array $uriAttributes |
| 49 |
* |
| 50 |
* @return array $uriAttributes |
| 51 |
*/ |
| 52 |
public static function skipSrcEscapeTemporary( $uriAttributes ) { |
| 53 |
if ( ( $key = array_search( 'src', $uriAttributes ) ) !== false) { |
| 54 |
unset( $uriAttributes[ $key ] ); |
| 55 |
} |
| 56 |
return $uriAttributes; |
| 57 |
} |
| 58 |
} |
| 59 |
|