| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Imagify |
| 4 |
* Plugin URI: https://wordpress.org/plugins/imagify/ |
| 5 |
* |
| 6 |
* Compatibility Description: Enables support for these Imagify Image Optimizer features: |
| 7 |
* auto-optimize images on upload, bulk optimizer, resize larger images, optimization levels (normal, aggressive, ultra). |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace wpCloud\StatelessMedia { |
| 12 |
|
| 13 |
if(!class_exists('wpCloud\StatelessMedia\Imagify')) { |
| 14 |
|
| 15 |
class Imagify extends ICompatibility { |
| 16 |
protected $id = 'imagify'; |
| 17 |
protected $title = 'Imagify Image Optimizer'; |
| 18 |
protected $constant = 'WP_STATELESS_COMPATIBILITY_IMAGIFY'; |
| 19 |
protected $description = 'Enables support for these Imagify Image Optimizer features: auto-optimize images on upload, bulk optimizer, resize larger images, optimization levels (normal, aggressive, ultra).'; |
| 20 |
protected $plugin_file = 'imagify/imagify.php'; |
| 21 |
|
| 22 |
public function module_init($sm){ |
| 23 |
// We need to remove the regular handler for sync |
| 24 |
// unless in stateless mode we would remove the attachment before it's get optimized. |
| 25 |
remove_filter( 'wp_update_attachment_metadata', array( "wpCloud\StatelessMedia\Utility", 'add_media' ), 999 ); |
| 26 |
// @todo add media button returns local url. |
| 27 |
add_filter( 'wp_update_attachment_metadata', array( $this, 'add_media_wrapper' ), 999, 2 ); |
| 28 |
|
| 29 |
add_filter( 'before_imagify_optimize_attachment', array($this, 'fix_missing_file'), 10); |
| 30 |
add_action( 'after_imagify_optimize_attachment', array($this, 'after_imagify_optimize_attachment'), 10 ); |
| 31 |
|
| 32 |
add_filter( 'before_imagify_restore_attachment', array($this, 'get_image_from_gcs'), 10); |
| 33 |
add_action( 'after_imagify_restore_attachment', array($this, 'after_imagify_optimize_attachment'), 10 ); |
| 34 |
// Sync from sync tab |
| 35 |
add_action( 'sm:synced::image', array( $this, 'get_image_from_gcs') ); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @todo |
| 41 |
* Replacement for default wp_update_attachment_metadata filter of bootstrap class. |
| 42 |
* To avoid sync same image twice, once on upload and again after optimization. |
| 43 |
* We also avoid downloading image before optimization on stateless mode. |
| 44 |
*/ |
| 45 |
public function add_media_wrapper($metadata, $attachment_id){ |
| 46 |
$imagify = new \Imagify_Attachment($attachment_id); |
| 47 |
|
| 48 |
if ( is_callable( array( $imagify, 'is_extension_supported' ) ) ) { |
| 49 |
if ( ! $imagify->is_extension_supported() ) { |
| 50 |
return ud_get_stateless_media()->add_media( $metadata, $attachment_id ); |
| 51 |
} |
| 52 |
} elseif ( function_exists( 'imagify_is_attachment_mime_type_supported' ) ) { |
| 53 |
// Use `imagify_is_attachment_mime_type_supported( $attachment_id )`. |
| 54 |
if ( ! imagify_is_attachment_mime_type_supported( $attachment_id ) ) { |
| 55 |
return ud_get_stateless_media()->add_media( $metadata, $attachment_id ); |
| 56 |
} |
| 57 |
} elseif(!wp_attachment_is_image($attachment_id)){ |
| 58 |
return ud_get_stateless_media()->add_media( $metadata, $attachment_id ); |
| 59 |
} |
| 60 |
|
| 61 |
return $metadata; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Try to restore images before compression |
| 66 |
* |
| 67 |
* @param $file |
| 68 |
* @param $attachment_id |
| 69 |
* @return mixed |
| 70 |
*/ |
| 71 |
public function fix_missing_file( $attachment_id ) { |
| 72 |
/** |
| 73 |
* If hook is triggered by ShortPixel |
| 74 |
*/ |
| 75 |
if ( !$this->hook_from_imagify($attachment_id) ) return; |
| 76 |
|
| 77 |
/** |
| 78 |
* If mode is stateless then we change it to cdn in order images not being deleted before optimization |
| 79 |
* Remember that we changed mode via global var |
| 80 |
*/ |
| 81 |
if ( ud_get_stateless_media()->get( 'sm.mode' ) == 'stateless' ) { |
| 82 |
ud_get_stateless_media()->set( 'sm.mode', 'cdn' ); |
| 83 |
global $wp_stateless_imagify_mode; |
| 84 |
$wp_stateless_imagify_mode = 'stateless'; |
| 85 |
} |
| 86 |
|
| 87 |
$upload_basedir = wp_upload_dir(); |
| 88 |
$upload_basedir = trailingslashit( $upload_basedir[ 'basedir' ] ); |
| 89 |
$meta_data = wp_get_attachment_metadata( $attachment_id ); |
| 90 |
$file = $upload_basedir . $meta_data['file']; |
| 91 |
|
| 92 |
/** |
| 93 |
* Try to get all missing files from GCS |
| 94 |
*/ |
| 95 |
if ( !file_exists( $file ) ) { |
| 96 |
ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', $meta_data['file']), true, $file ); |
| 97 |
} |
| 98 |
|
| 99 |
if ( !empty( $meta_data['sizes'] ) && is_array( $meta_data['sizes'] ) ) { |
| 100 |
foreach( $meta_data['sizes'] as $image ) { |
| 101 |
if ( !empty( $image['gs_name'] ) && !file_exists( $file = $upload_basedir . $image['gs_name'] ) ) { |
| 102 |
ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', $image['gs_name']), true, $file ); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Determine where we hook from |
| 111 |
* We need to do this only for something specific in shortpixel plugin |
| 112 |
* |
| 113 |
* @return bool |
| 114 |
*/ |
| 115 |
private function hook_from_imagify($attachment_id) { |
| 116 |
$imagify = new \Imagify_Attachment($attachment_id); |
| 117 |
if(method_exists($imagify, 'is_running')){ |
| 118 |
return $imagify->is_running($attachment_id); |
| 119 |
} |
| 120 |
elseif(get_transient( 'imagify-async-in-progress-' . $attachment_id ) !== false){ |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* If image size not exist then upload it to GS. |
| 130 |
* |
| 131 |
* $args = array( |
| 132 |
* 'thumbnail' => $thumbnail, |
| 133 |
* 'p_img_large' => $p_img_large, |
| 134 |
* ) |
| 135 |
*/ |
| 136 |
public function after_imagify_optimize_attachment($id){ |
| 137 |
/** |
| 138 |
* Restore stateless mode if needed |
| 139 |
*/ |
| 140 |
global $wp_stateless_imagify_mode; |
| 141 |
if ( $wp_stateless_imagify_mode == 'stateless' ) { |
| 142 |
ud_get_stateless_media()->set( 'sm.mode', 'stateless' ); |
| 143 |
} |
| 144 |
|
| 145 |
$metadata = wp_get_attachment_metadata( $id ); |
| 146 |
ud_get_stateless_media()->add_media( $metadata, $id, true ); |
| 147 |
|
| 148 |
// Sync backup file with GCS |
| 149 |
if( current_filter() == 'after_imagify_optimize_attachment' && ud_get_stateless_media()->get( 'sm.mode' ) !== 'stateless' ) { |
| 150 |
$file_path = get_attached_file( $id ); |
| 151 |
$backup_path = get_imagify_attachment_backup_path( $file_path ); |
| 152 |
if(file_exists($backup_path)){ |
| 153 |
$upload_dir = wp_upload_dir(); |
| 154 |
$overwrite = apply_filters( 'imagify_backup_overwrite_backup', false, $file_path, $backup_path ); |
| 155 |
$name = str_replace(trailingslashit( $upload_dir[ 'basedir' ] ), '', $backup_path); |
| 156 |
$name = apply_filters( 'wp_stateless_file_name', $name); |
| 157 |
do_action( 'sm:sync::syncFile', $name, $backup_path, $overwrite); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Restore backup file from GCS if not exist. |
| 164 |
*/ |
| 165 |
public function get_image_from_gcs($id){ |
| 166 |
$file_path = get_attached_file( $id ); |
| 167 |
$backup_path = get_imagify_attachment_backup_path( $file_path ); |
| 168 |
if(!file_exists($backup_path)){ |
| 169 |
$upload_dir = wp_upload_dir(); |
| 170 |
$name = str_replace(trailingslashit( $upload_dir[ 'basedir' ] ), '', $backup_path); |
| 171 |
$name = apply_filters( 'wp_stateless_file_name', $name); |
| 172 |
do_action( 'sm:sync::syncFile', $name, $backup_path, true); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|