Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active April 9, 2021 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/eb9de49fbdc284c51028714b3ba0b9ec to your computer and use it in GitHub Desktop.
Save wpmudev-sls/eb9de49fbdc284c51028714b3ba0b9ec to your computer and use it in GitHub Desktop.
[Smush] - Integration for AMP for WP plugin.
<?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