Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created September 18, 2019 18:27
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/78760a09da000bdaccc594b67732555b to your computer and use it in GitHub Desktop.
Save wpmudev-sls/78760a09da000bdaccc594b67732555b to your computer and use it in GitHub Desktop.
[Smush] - Load site icon meta tags from CDN
<?php
/**
* Plugin Name: [Smush] - Add support for site icon meta tags
* Plugin URI: https://premium.wpmudev.org/
* Description: Extend existing functionality of Smush to support for site icon meta tags (as of 3.2.4)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_Smush_Site_Icon_Meta_Tags' ) ) {
class WPMUDEV_Smush_Site_Icon_Meta_Tags {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Smush_Site_Icon_Meta_Tags();
}
return self::$_instance;
}
private function __construct() {
$this->init();
$this->settings = WP_Smush_Settings::get_instance();
}
private function init(){
if ( ! class_exists( 'WP_Smush' ) ) {
return;
}
add_filter( 'site_icon_meta_tags', array( $this, 'wpmudev_site_icon_meta_tags' ) );
}
public function wpmudev_site_icon_meta_tags( $meta_tags ){
$images = array();
if ( get_site_icon_url() ) {
if ( preg_match_all( '/(?:[href|content]=\[\'|"]?(http?s?:?\/\/[^"\']*\.(?:png|jpg|jpeg|gif))[\'|"]?)/is', implode( '', $meta_tags ), $images ) ) {
foreach ( $images as $key => $value ) {
if( ! $key ){
continue;
}
foreach( $value as $image ){
$new_image = WP_Smush::get_instance()->core()->mod->cdn->generate_cdn_url( $image );
$meta_tags = str_replace( $image, $new_image, $meta_tags );
}
}
}
}
return $meta_tags;
}
}
if ( ! function_exists( 'WPMUDEV_Smush_Site_Icon_Meta_Tags' ) ) {
function WPMUDEV_Smush_Site_Icon_Meta_Tags() {
return WPMUDEV_Smush_Site_Icon_Meta_Tags::get_instance();
};
add_action( 'plugins_loaded', 'WPMUDEV_Smush_Site_Icon_Meta_Tags', 99 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment