Support » Plugin: TinyPNG - JPEG, PNG & WebP image compression » Actions for successful compression

  • Resolved cristianuibar

    (@cristianuibar)


    Hello,

    We are looking for a way to hook to use your plugin so we can flush the CDN cache for a specific image after it has been optimized.

    I was looking in your code for any do_action hooks I could use but found none. Would it be possible to add some for cases like tiny_optimization_started, tiny_optimization_completed, even for restore functions (if any) something like tiny_restore_complete and pass the ID as param for all of these so we can use wp_get_attachment_url with it, would be very useful.

    Looking forward to this feature and your reply.

    Best regards,
    ~Cristian

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author TinyPNG

    (@tinypng)

    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.

    Thread Starter cristianuibar

    (@cristianuibar)

    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

    Plugin Author TinyPNG

    (@tinypng)

    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?

    Plugin Author TinyPNG

    (@tinypng)

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Actions for successful compression’ is closed to new replies.