. */ if ( amp_is_canonical() ) { add_filter( 'the_content', array( $this, 'tally_content_requiring_amp_scripts' ) ); add_action( 'wp_print_footer_scripts', array( $this, 'print_dirty_amp_scripts' ) ); } } } /** * Whitelist elements and attributes used for AMP. * * 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, $context ) { if ( 'post' !== $context ) { return $tags; } foreach ( $tags as &$tag ) { if ( ! is_array( $tag ) ) { continue; } $tag['data-amp-layout'] = true; $tag['data-amp-noloading'] = true; $tag['data-amp-lightbox'] = true; $tag['data-close-button-aria-label'] = true; } foreach ( $this->amp_blocks as $amp_block ) { if ( ! isset( $tags[ $amp_block ] ) ) { $tags[ $amp_block ] = array(); } // @todo The global attributes included here should be matched up with what is actually used by each block. $tags[ $amp_block ] = array_merge( array_fill_keys( array( 'layout', 'width', 'height', 'class', ), 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; } /** * Tally the AMP component scripts that are needed in a dirty AMP document. * * @param string $content Content. * @return string Content (unmodified). */ public function tally_content_requiring_amp_scripts( $content ) { if ( ! is_amp_endpoint() ) { $pattern = sprintf( '/<(%s)\b.*?>/s', join( '|', $this->amp_blocks ) ); if ( preg_match_all( $pattern, $content, $matches ) ) { $this->content_required_amp_scripts = array_merge( $this->content_required_amp_scripts, $matches[1] ); } } return $content; } /** * Print AMP scripts required for AMP components used in a non-AMP document (dirty AMP). */ public function print_dirty_amp_scripts() { if ( ! is_amp_endpoint() && ! empty( $this->content_required_amp_scripts ) ) { wp_scripts()->do_items( $this->content_required_amp_scripts ); } } }