| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Smush Image Compression and Optimization |
| 4 |
* Plugin URI: https://wordpress.org/plugins/wp-smushit/ |
| 5 |
* |
| 6 |
* Compatibility Description: Ensures compatibility with WPSmush. |
| 7 |
* |
| 8 |
* @todo Compatibility for backup feature |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace wpCloud\StatelessMedia { |
| 12 |
|
| 13 |
if( !class_exists( 'wpCloud\StatelessMedia\WPSmush' ) ) { |
| 14 |
|
| 15 |
class WPSmush extends ICompatibility { |
| 16 |
protected $id = 'wp-smush'; |
| 17 |
protected $title = 'WP Smush'; |
| 18 |
protected $constant = 'WP_STATELESS_COMPATIBILITY_WPSMUSH'; |
| 19 |
protected $description = 'Ensures compatibility with WP Smush.'; |
| 20 |
protected $plugin_file = [ 'wp-smushit/wp-smush.php', 'wp-smush-pro/wp-smush.php', 'wp-smushit-pro/wp-smush-pro.php' ]; |
| 21 |
|
| 22 |
/** |
| 23 |
* @param $sm |
| 24 |
*/ |
| 25 |
public function module_init( $sm ) { |
| 26 |
add_action( 'wp_smush_image_optimised', array( $this, 'image_optimized' ), 10, 2 ); |
| 27 |
// Check if the file not exists for the given path then download |
| 28 |
// Useful in Ephemeral mode |
| 29 |
add_action( 'smush_file_exists', array( $this, 'maybe_download_file' ), 10, 3 ); |
| 30 |
|
| 31 |
// Skip sync when attachment is image, sync will be handled after image is optimized. |
| 32 |
// add_filter( 'wp_stateless_skip_add_media', array( $this, 'skip_add_media' ), 10, 5 ); |
| 33 |
|
| 34 |
add_filter( 'delete_attachment', array( $this, 'remove_backup' ) ); |
| 35 |
add_filter( 'smush_backup_exists', array( $this, 'backup_exists_on_gcs' ), 10, 3 ); |
| 36 |
add_action( 'sm:synced::image', array( $this, 'sync_backup' ), 10, 2 ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Whether to skip the sync on image upload before the image is optimized. |
| 41 |
* The sync is skipped if the image is compatible with Smush. |
| 42 |
* |
| 43 |
* The image will be synced after it's get optimized using the 'wp_smush_image_optimised' action. |
| 44 |
* |
| 45 |
* |
| 46 |
* @param bool $return This should return true if want to skip the sync. |
| 47 |
* @param int $metadata Metadata for the attachment. |
| 48 |
* @param string $attachment_id Attachment ID. |
| 49 |
* @param bool $force Whether to force the sync even the file already exist in GCS. |
| 50 |
* @param bool $args Whether to only sync the full size image. |
| 51 |
* |
| 52 |
* @return bool $return True to skip the sync and false to do the sync. |
| 53 |
* |
| 54 |
*/ |
| 55 |
public function skip_add_media( $return, $metadata, $attachment_id, $force = false, $args = array() ) { |
| 56 |
global $doing_manual_sync; |
| 57 |
|
| 58 |
if( $force || $doing_manual_sync ) return false; |
| 59 |
|
| 60 |
if( class_exists( 'WP_Smush_Modules' ) ) { |
| 61 |
$auto_smush = \WP_Smush::get_instance()->core()->mod->settings->get( 'auto' ); |
| 62 |
} else { |
| 63 |
global $wpsmush_settings; |
| 64 |
$auto_smush = $wpsmush_settings->settings[ 'auto' ]; |
| 65 |
} |
| 66 |
|
| 67 |
if (!$auto_smush || !wp_attachment_is_image($attachment_id) || |
| 68 |
!apply_filters('wp_smush_image', true, $attachment_id) || |
| 69 |
!( |
| 70 |
((!empty($_POST['action']) && 'upload-attachment' == $_POST['action']) || isset($_POST['post_id'])) && |
| 71 |
// And, check if Async is enabled. |
| 72 |
defined('WP_SMUSH_ASYNC') && WP_SMUSH_ASYNC |
| 73 |
) |
| 74 |
) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Sync image after it's been optimized. |
| 82 |
* |
| 83 |
* @param int $attachment_id attachment id |
| 84 |
* @param array $stats compression stats |
| 85 |
* |
| 86 |
* @return null |
| 87 |
*/ |
| 88 |
public function image_optimized( $attachment_id, $stats ) { |
| 89 |
// Sync the attachment to GCS |
| 90 |
ud_get_stateless_media()->add_media( array(), $attachment_id, true ); |
| 91 |
|
| 92 |
// also sync the backup images |
| 93 |
$this->sync_backup( $attachment_id ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* If local file don't exists then download it from GCS |
| 98 |
* |
| 99 |
* @param string $file_path Full file path |
| 100 |
* @param string $attachment_id |
| 101 |
* @param array $size_details Array of width and height for the image |
| 102 |
* |
| 103 |
* @return null |
| 104 |
*/ |
| 105 |
function maybe_download_file( $file_path = '', $attachment_id = '', $size_details = array() ) { |
| 106 |
if( empty( $file_path ) || empty( $attachment_id ) ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
//Download if file not exists |
| 111 |
if( !file_exists( $file_path ) ) { |
| 112 |
$client = ud_get_stateless_media()->get_client(); |
| 113 |
$metadata = wp_get_attachment_metadata( $attachment_id ); |
| 114 |
if( !empty( $metadata[ 'gs_name' ] ) ) { |
| 115 |
$image_sizes = Utility::get_path_and_url( $metadata, $attachment_id ); |
| 116 |
foreach( $image_sizes as $size => $img ) { |
| 117 |
$client->get_media( apply_filters( 'wp_stateless_file_name', $img[ 'gs_name' ] ), true, $img[ 'path' ] ); |
| 118 |
} |
| 119 |
|
| 120 |
$gs_name = dirname( $metadata[ 'gs_name' ] ) . '/' . basename( $file_path ); |
| 121 |
// We need to remove backup from GCS if it's a restore action |
| 122 |
// @todo revise this code |
| 123 |
if( $this->hook_from_restore_image() ) { |
| 124 |
$client->remove_media( apply_filters( 'wp_stateless_file_name', $gs_name ) ); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Remove backup when attachment is removed |
| 132 |
* |
| 133 |
* @param $attachment_id |
| 134 |
*/ |
| 135 |
function remove_backup( $attachment_id ) { |
| 136 |
$upload_dir = wp_get_upload_dir(); |
| 137 |
$metadata = wp_get_attachment_metadata( $attachment_id ); |
| 138 |
$backup_paths = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); |
| 139 |
|
| 140 |
if( !empty( $metadata[ 'gs_name' ] ) && !empty( $backup_paths ) && is_array( $backup_paths ) ) { |
| 141 |
// Getting local dir path for backup image |
| 142 |
$base_dir = $upload_dir[ 'basedir' ] . '/' . dirname( $metadata[ 'file' ] ); |
| 143 |
// Getting GCS dir name from meta data. In case Bucket Folder used. |
| 144 |
$gs_dir = dirname( $metadata[ 'gs_name' ] ); |
| 145 |
foreach( $backup_paths as $key => $data ) { |
| 146 |
$gs_name = $gs_dir . '/' . basename( $data[ 'file' ] ); |
| 147 |
// Path of backup image |
| 148 |
$backup_path = $base_dir . '/' . basename( $data[ 'file' ] ); |
| 149 |
do_action( 'sm:sync::deleteFile', apply_filters( 'wp_stateless_file_name', $gs_name ), $backup_path ); |
| 150 |
delete_transient( 'sm-wp-smush-backup-exists-' . $attachment_id ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Checks if we've backup on gcs for the given attachment id and backup path |
| 158 |
* |
| 159 |
* @param string $attachment_id |
| 160 |
* @param string $backup_path |
| 161 |
* |
| 162 |
* @return bool |
| 163 |
*/ |
| 164 |
function backup_exists_on_gcs( $exists, $attachment_id = '', $backup_path = '' ) { |
| 165 |
if( !$exists && $attachment_id ) { |
| 166 |
if( get_transient( 'sm-wp-smush-backup-exists-' . $attachment_id ) ) { |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
$metadata = wp_get_attachment_metadata( $attachment_id ); |
| 171 |
if( !empty( $metadata[ 'gs_name' ] ) ) { |
| 172 |
$gs_name = dirname( $metadata[ 'gs_name' ] ) . '/' . basename( $backup_path ); |
| 173 |
if( ud_get_stateless_media()->get_client()->media_exists( apply_filters( 'wp_stateless_file_name', $gs_name ) ) ) { |
| 174 |
set_transient( 'sm-wp-smush-backup-exists-' . $attachment_id, true, HOUR_IN_SECONDS ); |
| 175 |
return true; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return $exists; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Sync backup image to GCS |
| 185 |
* |
| 186 |
* @param $attachment_id |
| 187 |
* @param array $metadata |
| 188 |
*/ |
| 189 |
public function sync_backup( $attachment_id, $metadata = array() ) { |
| 190 |
$upload_dir = wp_get_upload_dir(); |
| 191 |
if( empty( $metadata ) || empty( $metadata[ 'gs_name' ] ) ) { |
| 192 |
$metadata = wp_get_attachment_metadata( $attachment_id ); |
| 193 |
} |
| 194 |
|
| 195 |
// Getting backup path from smush settings in db |
| 196 |
$backup_paths = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); |
| 197 |
|
| 198 |
if( !empty( $metadata[ 'gs_name' ] ) && !empty( $backup_paths ) && is_array( $backup_paths ) ) { |
| 199 |
// Getting local dir for backup image |
| 200 |
$base_dir = $upload_dir[ 'basedir' ] . '/' . dirname( $metadata[ 'file' ] ); |
| 201 |
// Getting GCS dir name from meta data. In case Bucket Folder used. |
| 202 |
$gs_dir = dirname( $metadata[ 'gs_name' ] ); |
| 203 |
|
| 204 |
foreach( $backup_paths as $key => $data ) { |
| 205 |
$gs_name = $gs_dir . '/' . basename( $data[ 'file' ] ); |
| 206 |
// Path of backup image |
| 207 |
$backup_path = $base_dir . '/' . basename( $data[ 'file' ] ); |
| 208 |
// Sync backup image with GCS |
| 209 |
do_action( 'sm:sync::syncFile', apply_filters( 'wp_stateless_file_name', $gs_name ), $backup_path ); |
| 210 |
delete_transient( 'sm-wp-smush-backup-exists-' . $attachment_id ); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Determine where we hook from |
| 217 |
* Is this a hook from wp smush restore image or not. |
| 218 |
* |
| 219 |
* @return bool |
| 220 |
*/ |
| 221 |
private function hook_from_restore_image() { |
| 222 |
$call_stack = debug_backtrace(); |
| 223 |
$class_name = class_exists( 'WpSmushBackup' ) ? 'WpSmushBackup' : 'WP_Smush_Backup'; |
| 224 |
|
| 225 |
if( !empty( $call_stack ) && is_array( $call_stack ) ) { |
| 226 |
foreach( $call_stack as $step ) { |
| 227 |
|
| 228 |
if( $step[ 'function' ] == 'restore_image' && $step[ 'class' ] == $class_name ) { |
| 229 |
return true; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
} |
| 242 |
|