Skip to content

Instantly share code, notes, and snippets.

@westonruter
Created January 23, 2018 22:22
Show Gist options
  • Save westonruter/39d251d6f3ff8f9958f8fd1d11167da5 to your computer and use it in GitHub Desktop.
Save westonruter/39d251d6f3ff8f9958f8fd1d11167da5 to your computer and use it in GitHub Desktop.
<?php
$scripts = array();
$styles = array();
$content = 'I am <script>not</script> evil.';
$global_args = array();
if ( ! class_exists( 'AMP_DOM_Utils' ) ) {
amp_load_classes();
}
$dom = AMP_DOM_Utils::get_dom_from_content( $content );
$sanitizer_classes = apply_filters( 'amp_content_sanitizers', array(
'AMP_Style_Sanitizer' => array(),
'AMP_Img_Sanitizer' => array(),
'AMP_Video_Sanitizer' => array(),
'AMP_Audio_Sanitizer' => array(),
'AMP_Playbuzz_Sanitizer' => array(),
'AMP_Iframe_Sanitizer' => array(
'add_placeholder' => true,
),
'AMP_Tag_And_Attribute_Sanitizer' => array(),
) );
foreach ( $sanitizer_classes as $sanitizer_class => $args ) {
if ( ! class_exists( $sanitizer_class ) ) {
_doing_it_wrong( __METHOD__, sprintf( esc_html__( 'Sanitizer (%s) class does not exist', 'amp' ), esc_html( $sanitizer_class ) ), '0.4.1' );
continue;
}
$sanitizer = new $sanitizer_class( $dom, array_merge( $global_args, $args ) );
if ( ! is_subclass_of( $sanitizer, 'AMP_Base_Sanitizer' ) ) {
_doing_it_wrong( __METHOD__, sprintf( esc_html__( 'Sanitizer (%s) must extend `AMP_Base_Sanitizer`', 'amp' ), esc_html( $sanitizer_class ) ), '0.1' );
continue;
}
$sanitizer->sanitize();
$scripts = array_merge( $scripts, $sanitizer->get_scripts() );
$styles = array_merge( $styles, $sanitizer->get_styles() );
}
$sanitized_content = AMP_DOM_Utils::get_content_from_dom( $dom );
echo $sanitized_content . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment