diff --git a/php/media/class-filter.php b/php/media/class-filter.php index 4c37611a4..eb9a13430 100644 --- a/php/media/class-filter.php +++ b/php/media/class-filter.php @@ -236,6 +236,23 @@ public function filter_video_shortcodes( $content ) { return $content; } + /** + * If the post is a web story or AMP-powered, take care of the special AMP tags. + * + * @param array $data The post data array to save. + * + * @return array + */ + public function prepare_amp_posts( $data ) { + if ( ! Utils::is_webstory_post_type( $data['post_type'] ) && ! Utils::is_amp( $data['post_content'] ) ) { + return $data; + } + + $data['post_content'] = $this->filter_out_local( wp_unslash( $data['post_content'] ), 'amp-img|source' ); + + return $data; + } + /** * Filter out Cloudinary URL when saving to the DB. * @@ -284,12 +301,13 @@ public function filter_out_cloudinary( $data ) { * Filter content to replace local src urls with Cloudinary urls. * * @param string $content The content to filter urls. + * @param string $tags The tags to look out for in the post content. * * @return string The filtered content. */ - public function filter_out_local( $content ) { + public function filter_out_local( $content, $tags = 'img' ) { - $assets = $this->get_media_tags( $content, 'img' ); + $assets = $this->get_media_tags( $content, $tags ); foreach ( $assets as $asset ) { $url = $this->get_url_from_tag( $asset ); @@ -718,6 +736,7 @@ public function init_rest_filters() { public function setup_hooks() { // Filter URLS within content. add_action( 'wp_insert_post_data', array( $this, 'filter_out_cloudinary' ) ); + add_action( 'wp_insert_post_data', array( $this, 'prepare_amp_posts' ), 11 ); add_filter( 'wp_prepare_attachment_for_js', array( $this, 'filter_attachment_for_js' ), 11 ); // Add support for custom header.