'ampify_categories_block', 'core/archives' => 'ampify_archives_block', 'core/video' => 'ampify_video_block', 'core/file' => 'ampify_file_block', ]; /** * Register embed. */ public function register_embed() { add_filter( 'render_block', [ $this, 'filter_rendered_block' ], 0, 2 ); add_filter( 'widget_text_content', [ $this, 'preserve_widget_text_element_dimensions' ], PHP_INT_MAX ); } /** * Unregister embed. */ public function unregister_embed() { remove_filter( 'render_block', [ $this, 'filter_rendered_block' ], 0 ); remove_filter( 'widget_text_content', [ $this, 'preserve_widget_text_element_dimensions' ], PHP_INT_MAX ); } /** * Filters the content of a single block to make it AMP valid. * * @param string $block_content The block content about to be appended. * @param array $block The full block, including name and attributes. * @return string Filtered block content. */ public function filter_rendered_block( $block_content, $block ) { if ( ! isset( $block['blockName'] ) ) { return $block_content; // @codeCoverageIgnore } if ( isset( $block['attrs'] ) && 'core/shortcode' !== $block['blockName'] ) { $injected_attributes = ''; $prop_attribute_mapping = [ 'ampCarousel' => 'data-amp-carousel', 'ampLayout' => 'data-amp-layout', 'ampLightbox' => 'data-amp-lightbox', 'ampNoLoading' => 'data-amp-noloading', ]; foreach ( $prop_attribute_mapping as $prop => $attr ) { if ( isset( $block['attrs'][ $prop ] ) ) { $property_value = $block['attrs'][ $prop ]; if ( is_bool( $property_value ) ) { $property_value = $property_value ? 'true' : 'false'; } $injected_attributes .= sprintf( ' %s="%s"', $attr, esc_attr( $property_value ) ); } } if ( $injected_attributes ) { $block_content = preg_replace( '/(<\w+)/', '$1' . $injected_attributes, $block_content, 1 ); } } if ( isset( $this->block_ampify_methods[ $block['blockName'] ] ) ) { $method_name = $this->block_ampify_methods[ $block['blockName'] ]; $block_content = $this->{$method_name}( $block_content, $block ); } elseif ( 'core/image' === $block['blockName'] || 'core/audio' === $block['blockName'] ) { /* * While the video block placeholder just outputs an empty video element, the placeholders for image and * audio blocks output empty and