Last active
April 9, 2021 09:10
-
-
Save wpmudev-sls/eb9de49fbdc284c51028714b3ba0b9ec to your computer and use it in GitHub Desktop.
[Smush] - Integration for AMP for WP plugin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Smush] - Integration for AMP for WP plugin | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: This is an integration specifically for https://wordpress.org/plugins/accelerated-mobile-pages/ plugin. This is not required for the official one https://wordpress.org/plugins/amp/. | |
* Task: SLS-1919 | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_filter( | |
'ampforwp_modify_the_content', | |
function( $content ) { | |
if ( ! class_exists( 'Smush\Core\Modules\CDN' ) ) { | |
return $content; | |
} | |
$smush_parser = new Smush\Core\Modules\Helpers\Parser(); | |
$smush_cdn = new Smush\Core\Modules\CDN( $smush_parser ); | |
if ( ! $smush_cdn->get_status() ) { | |
return $content; | |
} | |
$images = $smush_parser->get_images_from_content( str_replace( 'amp-img' , 'img', $content ) ); | |
if ( ! empty( $images ) && ! empty( $images[ 'src' ] ) ) { | |
foreach( $images['src'] as $img_src ) { | |
if ( ! $img_src || ! $smush_cdn->is_supported_path( $img_src ) ) { | |
continue; | |
} | |
$smush_cdn->set_cdn_url(); | |
$cdn_src = $smush_cdn->generate_cdn_url( $img_src ); | |
$content = str_replace( $img_src, $cdn_src, $content ); | |
} | |
} | |
return $content; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment