Skip to content

Instantly share code, notes, and snippets.

@IAmAdamTaylor
Last active November 28, 2022 03:41
Show Gist options
  • Save IAmAdamTaylor/a65084467872708f1c3114691231f7d6 to your computer and use it in GitHub Desktop.
Save IAmAdamTaylor/a65084467872708f1c3114691231f7d6 to your computer and use it in GitHub Desktop.
WordPress & WPMU Dev: Regenerate Smush Pro Local WebP images programatically

Enable Media Replace & WPMU Dev Connector (WordPress plugin)

Connects the Enable Media Replace and WPMU Dev (Smush Pro, Hummingbird) plugins together. When an image is replaced using Enable Media Replace, local WebP files created by Smush Pro will be regenerated, and Hummingbird cache will be cleared.

Regnerate Smush Pro Local WebP images programatically when the original image changes.

Tested and working with these versions:

  • WordPress v6.0.1
  • Enable Media Replace v3.6.3
  • Smush Pro v3.10.3
  • Hummingbird Pro v3.3.4
  • WPMU Dev Dashboard v4.11.14

How to Use

  1. Install and activate Enable Media Replace, Smush Pro and Hummingbird plugins
  2. Download the plugin code and place it in a separate file within the /wp-content/mu-plugins/ directory
  3. The plugin will be loaded automatically, no need to activate it
  4. When you use Enable Media Replace to update an image, the WebP images will be updated as well

Possible Issues

The code has not been tested on every version of the software and may not work as intended with future releases. Please see the version information above for which plugin versions this has been tested up to.

At the moment, there are no checks that the plugins are installed and active. The code may produce an error if used without them. I will try to add this soon and then remove this notice.

The functions this plugin calls, within Smush Pro, may not have been intended to be called publicly in this way. This may cause unintended behaviour, although none have been observed while testing.

Contact

If you have any questions or changes, please send them to me on Twitter @IAmAdamTaylor. DMs are open.

<?php
/**
* Main plugin file.
*
* @package Enable Media Replace & WPMU Dev Connector
* @license GPL2
*/
/**
* Plugin Name: Enable Media Replace & WPMU Dev Connector
* Description: Connects the Enable Media Replace and WPMU Dev (Smush Pro, Hummingbird) plugins together. When an image is replaced using Enable Media Replace, local WebP files created by Smush Pro will be regenerated, and Hummingbird cache will be cleared.
* Version: 1.0.0
* License: GPL2
*/
/* Copyright 2022 (email : me@adamtaylor.dev)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class EMR_2_WPMU {
/**
* Link to the WebP class defined by Smush.
* @var Smush\Core\Modules\WebP
*/
private $webp_instance;
function __construct() {
// store WebP instance
$this->webp_instance = WP_Smush::get_instance()->core()->mod->webp;
// listen for Enable Media Replace cache flush and 'flush' webp files
add_action( 'emr/cache/flush', array($this, 'regen_smush_webp') );
add_action( 'emr/cache/flush', array($this, 'flush_hummingbird_cache') );
}
/**
* From Smush\Core\Modules\WebP class
* Check whether the given attachment id or mime type can be converted to WebP.
*
* @param string $id Atachment ID.
* @param string $mime Mime type.
*
* @return bool
*/
private function can_be_converted( $id = '', $mime = '' ) {
if ( empty( $id ) && empty( $mime ) ) {
return false;
}
$mime = empty( $mime ) ? get_post_mime_type( $id ) : $mime;
// This image can not be converted to webp.
if ( ! ( 'image/jpeg' === $mime || 'image/png' === $mime ) ) {
return false;
}
return true;
}
/**
* Regenerate Smush WebP files for a specific attachment ID.
*
* @param int $attachment_id Attachment ID.
*/
public function regen_smush_webp( $attachment_id ) {
// check that attachment can be converted before starting
if ( !$this->can_be_converted( $attachment_id ) ) {
return;
}
$ref_meta = wp_get_attachment_metadata( $attachment_id, true );
// delete existing WebP files
$this->webp_instance->delete_images( $attachment_id, $ref_meta );
// create new ones
$webp_files = $this->webp_instance->convert_to_webp( $attachment_id, $ref_meta );
}
/**
* Flush entire Hummingbird page cache.
*/
public function flush_hummingbird_cache() {
do_action( 'wphb_clear_page_cache' );
}
}
function emr_2_wpmu_run() {
$plugin = new EMR_2_WPMU();
}
add_action( 'plugins_loaded', 'emr_2_wpmu_run' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment