Skip to content

Commit

Permalink
Ensure that AMP HTML is not removed via Kses when unfiltered_html absent
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed May 23, 2018
1 parent 5752aff commit f9bbe30
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions includes/admin/class-amp-editor-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit f9bbe30

Please sign in to comment.