Hi Cristian, sounds like a good idea.
We have added a hook needed by WPML to make their plugin compatible, you can see the code here:
https://github.com/tinify/wordpress-plugin/commit/8115bba079efc81a5301bb5bd373fd9e90c07240
If you like maybe try the following code change. Notice the 2 additional hooks, one after each thumbnail size and one after completing compression of an image:
public function compress() {
if ( $this->settings->get_compressor() === null || ! $this->file_type_allowed() ) {
return;
}
$success = 0;
$failed = 0;
$compressor = $this->settings->get_compressor();
$active_tinify_sizes = $this->settings->get_active_tinify_sizes();
$uncompressed_sizes = $this->filter_image_sizes( 'uncompressed', $active_tinify_sizes );
foreach ( $uncompressed_sizes as $size_name => $size ) {
if ( ! $size->is_duplicate() ) {
$size->add_tiny_meta_start();
$this->update_tiny_post_meta();
$resize = $this->settings->get_resize_options( $size_name );
$preserve = $this->settings->get_preserve_options( $size_name );
try {
$response = $compressor->compress_file( $size->filename, $resize, $preserve );
$size->add_tiny_meta( $response );
$success++;
do_action( 'tiny_image_size_compressed', $this->id, $size->filename );
} catch ( Tiny_Exception $e ) {
$size->add_tiny_meta_error( $e );
$failed++;
}
$this->add_wp_metadata( $size_name, $size );
$this->update_tiny_post_meta();
}
}
do_action( 'tiny_image_after_compression', $this->id, $success );
return array(
'success' => $success,
'failed' => $failed,
);
}
Maybe you can try out this code or changes to the example and share the code changes?
We would be happy to include one or two hooks in the next release of the plugin scheduled for half November.
Hello,
We have implemented this in production and it seems to be working.
I am using your new function above and the ‘tiny_image_after_compression’ hook to refresh the CDN cache for that image using its ID.
I’ll let you know if I encounter any issues, otherwise please merge this new function in production as we’ll be also pushing our cache refresh code in production on our next deployment.
Best regards,
~Cristian
Hi Christian,
Thanks, we will update this next week in master so it will be released with the next version.
Just to be sure: you are only using tiny_image_after_compression
?
And another question, what CDN are you using? And are you using another plugin which we can maybe add compatibility with?
As a follow up, we have added do_action( 'tiny_image_after_compression', $this->id, $success );
and this will be included in the next release.