From f9bbe30683824b1dcaa3543e34e29f5a31aaaf7b Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 23 May 2018 14:39:06 -0700 Subject: [PATCH] Ensure that AMP HTML is not removed via Kses when unfiltered_html absent --- includes/admin/class-amp-editor-blocks.php | 56 ++++++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/includes/admin/class-amp-editor-blocks.php b/includes/admin/class-amp-editor-blocks.php index 857de04fe66..b847e2362fd 100644 --- a/includes/admin/class-amp-editor-blocks.php +++ b/includes/admin/class-amp-editor-blocks.php @@ -17,21 +17,69 @@ class AMP_Editor_Blocks { public function init() { if ( function_exists( 'gutenberg_init' ) ) { add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); - add_filter( 'wp_kses_allowed_html', array( $this, 'whitelist_block_atts_in_wp_kses_allowed_html' ), 10 ); + add_filter( 'wp_kses_allowed_html', array( $this, 'whitelist_block_atts_in_wp_kses_allowed_html' ), 10, 2 ); } } /** - * Whitelist used data-amp-* attributes. + * Whitelist elements and attributes used for AMP. * - * @param array $tags Array of allowed post tags. + * This prevents AMP markup from being deleted in + * + * @param array $tags Array of allowed post tags. + * @param string $context Context. * @return mixed Modified array. */ - public function whitelist_block_atts_in_wp_kses_allowed_html( $tags ) { + public function whitelist_block_atts_in_wp_kses_allowed_html( $tags, $context ) { + if ( 'post' !== $context ) { + return $tags; + } + foreach ( $tags as &$tag ) { $tag['data-amp-layout'] = true; $tag['data-amp-noloading'] = true; } + + $amp_blocks = array( + 'amp-mathml', + 'amp-o2-player', + 'amp-ooyala-player', + 'amp-reach-player', + 'amp-springboard-player', + 'amp-jwplayer', + 'amp-brid-player', + 'amp-ima-video', + ); + + foreach ( $amp_blocks as $amp_block ) { + if ( ! isset( $tags[ $amp_block ] ) ) { + $tags[ $amp_block ] = array(); + } + + $tags[ $amp_block ] = array_merge( + array_fill_keys( + array( + 'layout', + 'width', + 'height', + ), + true + ), + $tags[ $amp_block ] + ); + + $amp_tag_specs = AMP_Allowed_Tags_Generated::get_allowed_tag( $amp_block ); + foreach ( $amp_tag_specs as $amp_tag_spec ) { + if ( ! isset( $amp_tag_spec[ AMP_Rule_Spec::ATTR_SPEC_LIST ] ) ) { + continue; + } + $tags[ $amp_block ] = array_merge( + $tags[ $amp_block ], + array_fill_keys( array_keys( $amp_tag_spec[ AMP_Rule_Spec::ATTR_SPEC_LIST ] ), true ) + ); + } + } + return $tags; }