| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: EWWW Image Optimizer |
| 4 |
* Plugin URI: https://ewww.io/ |
| 5 |
* |
| 6 |
* Compatibility Description: Enables support for these EWWW Image Optimizer Image Optimizer features |
| 7 |
* |
| 8 |
* https://github.com/wpCloud/wp-stateless/issues/371 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace wpCloud\StatelessMedia { |
| 12 |
|
| 13 |
if( !class_exists( 'wpCloud\StatelessMedia\EWWW' ) ) { |
| 14 |
|
| 15 |
class EWWW extends Compatibility { |
| 16 |
protected $id = 'ewww'; |
| 17 |
protected $title = 'EWWW Image Optimizer'; |
| 18 |
protected $constant = 'WP_STATELESS_COMPATIBILITY_EWWW'; |
| 19 |
protected $description = 'Enables limited support for EWWW Image Optimizer in CDN mode.'; |
| 20 |
protected $plugin_file = [ 'ewww-image-optimizer/ewww-image-optimizer.php' ]; |
| 21 |
protected $sm_mode_not_supported = [ 'stateless' ]; |
| 22 |
protected $enabled = false; |
| 23 |
protected $is_internal = true; |
| 24 |
|
| 25 |
public function module_init( $sm ) { |
| 26 |
// add_filter( 'ewww_image_optimizer_pre_optimization', array($this, 'pre_optimization'), 10, 3 ); |
| 27 |
add_action( 'ewww_image_optimizer_post_optimization', array( $this, 'post_optimization' ), 10, 3 ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Try to restore images before compression |
| 32 |
* |
| 33 |
* @param $file |
| 34 |
* @param $type |
| 35 |
* @param $fullsize |
| 36 |
*/ |
| 37 |
public function pre_optimization( $file, $type, $fullsize ) { |
| 38 |
// wp_stateless_file_name filter will remove the basedir from the path and prepend with root dir. |
| 39 |
$name = apply_filters( 'wp_stateless_file_name', $file ); |
| 40 |
do_action( 'sm:sync::syncFile', $name, $file, true, array( 'ephemeral' => false, 'download' => true, 'use_root' => true ) ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* If image size not exist then upload it to GS. |
| 45 |
* |
| 46 |
* @param $file |
| 47 |
* @param $type |
| 48 |
* @param $fullsize |
| 49 |
*/ |
| 50 |
public function post_optimization( $file, $type, $fullsize ) { |
| 51 |
// wp_stateless_file_name filter will remove the basedir from the path and prepend with root dir. |
| 52 |
$name = apply_filters( 'wp_stateless_file_name', $file ); |
| 53 |
do_action( 'sm:sync::syncFile', $name, $file, true, array( 'use_root' => true ) ); |
| 54 |
|
| 55 |
|
| 56 |
// if($fullsize && file_exists($file . '.bak')) |
| 57 |
// do_action( 'sm:sync::syncFile', $name . '.bak', $file . '.bak', true); |
| 58 |
|
| 59 |
if( file_exists( $file . '.webp' ) ) { |
| 60 |
add_filter( 'upload_mimes', array( $this, 'add_webp_mime' ), 10, 2 ); |
| 61 |
do_action( 'sm:sync::syncFile', $name . '.webp', $file . '.webp', true, array( 'use_root' => true ) ); |
| 62 |
remove_filter( 'upload_mimes', array( $this, 'add_webp_mime' ), 10 ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
|