| @@ -1,28 +1,26 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Imagify\Imagifybeat; |
| 3 | 3 | |
| 4 | 4 | use Imagify_Requirements; |
| 5 | +use Imagify\Traits\InstanceGetterTrait; | |
| 6 | +use Imagify\Bulk\Bulk; | |
| 5 | 7 | |
| 6 | -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 7 | - | |
| 8 | 8 | /** |
| 9 | 9 | * Imagifybeat actions. |
| 10 | 10 | * |
| 11 | - * @since 1.9.3 | |
| 12 | - * @author Grégory Viguier | |
| 11 | + * @since 1.9.3 | |
| 13 | 12 | */ |
| 14 | -class Actions { | |
| 15 | - use \Imagify\Traits\InstanceGetterTrait; | |
| 13 | +final class Actions { | |
| 14 | + use InstanceGetterTrait; | |
| 16 | 15 | |
| 17 | 16 | /** |
| 18 | 17 | * The list of action IDs. |
| 19 | 18 | * Keys are related to method names, values are Imagifybeat IDs. |
| 20 | 19 | * |
| 21 | - * @var array | |
| 22 | - * @since 1.9.3 | |
| 23 | - * @access private | |
| 24 | - * @author Grégory Viguier | |
| 20 | + * @var array | |
| 21 | + * | |
| 22 | + * @since 1.9.3 | |
| 25 | 23 | */ |
| 26 | 24 | private $imagifybeat_ids = [ |
| 27 | 25 | 'requirements' => 'imagify_requirements', |
| 28 | 26 | 'bulk_optimization_stats' => 'imagify_bulk_optimization_stats', |
| @@ -34,11 +32,9 @@ | ||
| 34 | 32 | |
| 35 | 33 | /** |
| 36 | 34 | * Class init: launch hooks. |
| 37 | 35 | * |
| 38 | - * @since 1.9.3 | |
| 39 | - * @access public | |
| 40 | - * @author Grégory Viguier | |
| 36 | + * @since 1.9.3 | |
| 41 | 37 | */ |
| 42 | 38 | public function init() { |
| 43 | 39 | foreach ( $this->imagifybeat_ids as $action => $imagifybeat_id ) { |
| 44 | 40 | add_filter( 'imagifybeat_received', [ $this, 'add_' . $action . '_to_response' ], 10, 2 ); |
| @@ -44,9 +40,8 @@ | ||
| 44 | 40 | add_filter( 'imagifybeat_received', [ $this, 'add_' . $action . '_to_response' ], 10, 2 ); |
| 45 | 41 | } |
| 46 | 42 | } |
| 47 | 43 | |
| 48 | - | |
| 49 | 44 | /** ----------------------------------------------------------------------------------------- */ |
| 50 | 45 | /** IMAGIFYBEAT CALLBACKS =================================================================== */ |
| 51 | 46 | /** ----------------------------------------------------------------------------------------- */ |
| 52 | 47 | |
| @@ -52,11 +47,9 @@ | ||
| 52 | 47 | |
| 53 | 48 | /** |
| 54 | 49 | * Add requirements to Imagifybeat data. |
| 55 | 50 | * |
| 56 | - * @since 1.9.3 | |
| 57 | - * @access public | |
| 58 | - * @author Grégory Viguier | |
| 51 | + * @since 1.9.3 | |
| 59 | 52 | * |
| 60 | 53 | * @param array $response The Imagifybeat response. |
| 61 | 54 | * @param array $data The $_POST data sent. |
| 62 | 55 | * @return array |
| @@ -82,11 +75,9 @@ | ||
| 82 | 75 | |
| 83 | 76 | /** |
| 84 | 77 | * Add bulk stats to Imagifybeat data. |
| 85 | 78 | * |
| 86 | - * @since 1.9.3 | |
| 87 | - * @access public | |
| 88 | - * @author Grégory Viguier | |
| 79 | + * @since 1.9.3 | |
| 89 | 80 | * |
| 90 | 81 | * @param array $response The Imagifybeat response. |
| 91 | 82 | * @param array $data The $_POST data sent. |
| 92 | 83 | * @return array |
| @@ -113,11 +104,9 @@ | ||
| 113 | 104 | /** |
| 114 | 105 | * Look for media where status has changed, compared to what Imagifybeat sends. |
| 115 | 106 | * This is used in the bulk optimization page. |
| 116 | 107 | * |
| 117 | - * @since 1.9.3 | |
| 118 | - * @access public | |
| 119 | - * @author Grégory Viguier | |
| 108 | + * @since 1.9.3 | |
| 120 | 109 | * |
| 121 | 110 | * @param array $response The Imagifybeat response. |
| 122 | 111 | * @param array $data The $_POST data sent. |
| 123 | 112 | * @return array |
| @@ -128,86 +117,42 @@ | ||
| 128 | 117 | if ( ! $imagifybeat_id || empty( $data[ $imagifybeat_id ] ) || ! is_array( $data[ $imagifybeat_id ] ) ) { |
| 129 | 118 | return $response; |
| 130 | 119 | } |
| 131 | 120 | |
| 132 | - $statuses = []; | |
| 133 | - | |
| 134 | - foreach ( $data[ $imagifybeat_id ] as $item ) { | |
| 135 | - if ( empty( $statuses[ $item['context'] ] ) ) { | |
| 136 | - $statuses[ $item['context'] ] = []; | |
| 137 | - } | |
| 138 | - | |
| 139 | - $statuses[ $item['context'] ][ '_' . $item['mediaID'] ] = 1; | |
| 121 | + if ( ! isset( $data[ $imagifybeat_id ] ) ) { | |
| 122 | + return $response; | |
| 140 | 123 | } |
| 141 | 124 | |
| 142 | - $results = $this->get_modified_optimization_statuses( $statuses ); | |
| 125 | + $bulk = Bulk::get_instance(); | |
| 126 | + $groups_data = []; | |
| 127 | + $types = []; | |
| 128 | + $total = 0; | |
| 129 | + $remaining = 0; | |
| 130 | + $percentage = 0; | |
| 143 | 131 | |
| 144 | - if ( ! $results ) { | |
| 145 | - return $response; | |
| 146 | - } | |
| 132 | + foreach ( $data[ $imagifybeat_id ] as $group ) { | |
| 133 | + $types[ $group['groupID'] . '|' . $group['context'] ] = true; | |
| 147 | 134 | |
| 148 | - $response[ $imagifybeat_id ] = []; | |
| 135 | + $transient = get_transient( "imagify_{$group['context']}_optimize_running" ); | |
| 149 | 136 | |
| 150 | - // Sanitize received data and grab some other info. | |
| 151 | - foreach ( $results as $context_id => $media_atts ) { | |
| 152 | - $process = imagify_get_optimization_process( $media_atts['media_id'], $media_atts['context'] ); | |
| 153 | - $optim_data = $process->get_data(); | |
| 137 | + if ( false !== $transient ) { | |
| 138 | + $total += $transient['total']; | |
| 139 | + $remaining += $transient['remaining']; | |
| 140 | + } | |
| 154 | 141 | |
| 155 | - if ( $optim_data->is_optimized() ) { | |
| 156 | - // Successfully optimized. | |
| 157 | - $full_size_data = $optim_data->get_size_data(); | |
| 158 | - $full_size_data = array_merge( | |
| 159 | - [ | |
| 160 | - 'original_size' => 0, | |
| 161 | - 'optimized_size' => 0, | |
| 162 | - 'percent' => 0, | |
| 163 | - ], | |
| 164 | - $full_size_data | |
| 165 | - ); | |
| 166 | - $response[ $imagifybeat_id ][] = [ | |
| 167 | - 'mediaID' => $media_atts['media_id'], | |
| 168 | - 'context' => $media_atts['context'], | |
| 169 | - 'success' => true, | |
| 170 | - 'status' => 'optimized', | |
| 171 | - // Raw data. | |
| 172 | - 'originalOverallSize' => $full_size_data['original_size'], | |
| 173 | - 'newOverallSize' => $full_size_data['optimized_size'], | |
| 174 | - 'overallSaving' => $full_size_data['original_size'] - $full_size_data['optimized_size'], | |
| 175 | - 'thumbnailsCount' => $optim_data->get_optimized_sizes_count(), | |
| 176 | - // Human readable data. | |
| 177 | - 'originalSizeHuman' => imagify_size_format( $full_size_data['original_size'], 2 ), | |
| 178 | - 'newSizeHuman' => imagify_size_format( $full_size_data['optimized_size'], 2 ), | |
| 179 | - 'overallSavingHuman' => imagify_size_format( $full_size_data['original_size'] - $full_size_data['optimized_size'], 2 ), | |
| 180 | - 'originalOverallSizeHuman' => imagify_size_format( $full_size_data['original_size'], 2 ), | |
| 181 | - 'percentHuman' => $full_size_data['percent'] . '%', | |
| 182 | - ]; | |
| 183 | - } elseif ( $optim_data->is_already_optimized() ) { | |
| 184 | - // Already optimized. | |
| 185 | - $response[ $imagifybeat_id ][] = [ | |
| 186 | - 'mediaID' => $media_atts['media_id'], | |
| 187 | - 'context' => $media_atts['context'], | |
| 188 | - 'success' => true, | |
| 189 | - 'status' => 'already-optimized', | |
| 190 | - ]; | |
| 191 | - } else { | |
| 192 | - // Error. | |
| 193 | - $full_size_data = $optim_data->get_size_data(); | |
| 194 | - $message = ! empty( $full_size_data['error'] ) ? $full_size_data['error'] : ''; | |
| 195 | - $status = 'error'; | |
| 142 | + $groups_data[ $group['context'] ] = $bulk->get_bulk_instance( $group['context'] )->get_context_data(); | |
| 143 | + } | |
| 196 | 144 | |
| 197 | - if ( 'You\'ve consumed all your data. You have to upgrade your account to continue' === $message ) { | |
| 198 | - $status = 'over-quota'; | |
| 199 | - } | |
| 145 | + if ( 0 !== $total ) { | |
| 146 | + $percentage = ( $total - $remaining ) / $total * 100; | |
| 147 | + } | |
| 200 | 148 | |
| 201 | - $response[ $imagifybeat_id ][] = [ | |
| 202 | - 'mediaID' => $media_atts['media_id'], | |
| 203 | - 'context' => $media_atts['context'], | |
| 204 | - 'success' => false, | |
| 205 | - 'status' => $status, | |
| 206 | - 'error' => imagify_translate_api_message( $message ), | |
| 207 | - ]; | |
| 208 | - } | |
| 209 | - } | |
| 149 | + $response[ $imagifybeat_id ] = [ | |
| 150 | + 'groups_data' => $groups_data, | |
| 151 | + 'remaining' => $remaining, | |
| 152 | + 'percentage' => round( $percentage ), | |
| 153 | + 'result' => get_transient( 'imagify_bulk_optimization_result' ), | |
| 154 | + ]; | |
| 210 | 155 | |
| 211 | 156 | return $response; |
| 212 | 157 | } |
| 213 | 158 | |
| @@ -214,10 +159,9 @@ | ||
| 214 | 159 | /** |
| 215 | 160 | * Look for media where status has changed, compared to what Imagifybeat sends. |
| 216 | 161 | * This is used in the settings page. |
| 217 | 162 | * |
| 218 | - * @since 1.9 | |
| 219 | - * @author Grégory Viguier | |
| 163 | + * @since 1.9 | |
| 220 | 164 | * |
| 221 | 165 | * @param array $response The Imagifybeat response. |
| 222 | 166 | * @param array $data The $_POST data sent. |
| 223 | 167 | * @return array |
| @@ -228,32 +172,32 @@ | ||
| 228 | 172 | if ( ! $imagifybeat_id || empty( $data[ $imagifybeat_id ] ) || ! is_array( $data[ $imagifybeat_id ] ) ) { |
| 229 | 173 | return $response; |
| 230 | 174 | } |
| 231 | 175 | |
| 232 | - $statuses = []; | |
| 176 | + $remaining = 0; | |
| 177 | + $total = get_transient( 'imagify_missing_next_gen_total' ); | |
| 233 | 178 | |
| 234 | - foreach ( $data[ $imagifybeat_id ] as $item ) { | |
| 235 | - if ( empty( $statuses[ $item['context'] ] ) ) { | |
| 236 | - $statuses[ $item['context'] ] = []; | |
| 237 | - } | |
| 179 | + if ( false === $total ) { | |
| 180 | + return $response; | |
| 181 | + } | |
| 238 | 182 | |
| 239 | - $statuses[ $item['context'] ][ '_' . $item['mediaID'] ] = 1; | |
| 240 | - } | |
| 183 | + $bulk = Bulk::get_instance(); | |
| 241 | 184 | |
| 242 | - $results = $this->get_modified_optimization_statuses( $statuses ); | |
| 185 | + $format = get_imagify_option( 'optimization_format' ); | |
| 243 | 186 | |
| 244 | - if ( ! $results ) { | |
| 187 | + if ( 'off' === $format ) { | |
| 245 | 188 | return $response; |
| 246 | 189 | } |
| 247 | 190 | |
| 248 | - $response[ $imagifybeat_id ] = []; | |
| 191 | + foreach ( $data[ $imagifybeat_id ] as $context ) { | |
| 192 | + $media = $bulk->get_bulk_instance( $context )->get_optimized_media_ids_without_format( $format ); | |
| 193 | + $remaining += count( $media['ids'] ); | |
| 194 | + } | |
| 249 | 195 | |
| 250 | - foreach ( $results as $result ) { | |
| 251 | - $response[ $imagifybeat_id ][] = [ | |
| 252 | - 'mediaID' => $result['media_id'], | |
| 253 | - 'context' => $result['context'], | |
| 254 | - ]; | |
| 255 | - } | |
| 196 | + $response[ $imagifybeat_id ] = [ | |
| 197 | + 'remaining' => $remaining, | |
| 198 | + 'total' => (int) $total, | |
| 199 | + ]; | |
| 256 | 200 | |
| 257 | 201 | return $response; |
| 258 | 202 | } |
| 259 | 203 | |
| @@ -260,11 +204,9 @@ | ||
| 260 | 204 | /** |
| 261 | 205 | * Look for media where status has changed, compared to what Imagifybeat sends. |
| 262 | 206 | * This is used in the WP Media Library. |
| 263 | 207 | * |
| 264 | - * @since 1.9.3 | |
| 265 | - * @access public | |
| 266 | - * @author Grégory Viguier | |
| 208 | + * @since 1.9.3 | |
| 267 | 209 | * |
| 268 | 210 | * @param array $response The Imagifybeat response. |
| 269 | 211 | * @param array $data The $_POST data sent. |
| 270 | 212 | * @return array |
| @@ -295,11 +237,9 @@ | ||
| 295 | 237 | /** |
| 296 | 238 | * Look for media where status has changed, compared to what Imagifybeat sends. |
| 297 | 239 | * This is used in the custom folders list (the "Other Media" page). |
| 298 | 240 | * |
| 299 | - * @since 1.9.3 | |
| 300 | - * @access public | |
| 301 | - * @author Grégory Viguier | |
| 241 | + * @since 1.9.3 | |
| 302 | 242 | * |
| 303 | 243 | * @param array $response The Imagifybeat response. |
| 304 | 244 | * @param array $data The $_POST data sent. |
| 305 | 245 | * @return array |
| @@ -317,11 +257,13 @@ | ||
| 317 | 257 | return $response; |
| 318 | 258 | } |
| 319 | 259 | |
| 320 | 260 | $admin_ajax_post = \Imagify_Admin_Ajax_Post::get_instance(); |
| 321 | - $list_table = new \Imagify_Files_List_Table( [ | |
| 322 | - 'screen' => 'imagify-files', | |
| 323 | - ] ); | |
| 261 | + $list_table = new \Imagify_Files_List_Table( | |
| 262 | + [ | |
| 263 | + 'screen' => 'imagify-files', | |
| 264 | + ] | |
| 265 | + ); | |
| 324 | 266 | |
| 325 | 267 | // Sanitize received data and grab some other info. |
| 326 | 268 | foreach ( $response[ $imagifybeat_id ] as $context_id => $media_atts ) { |
| 327 | 269 | $process = imagify_get_optimization_process( $media_atts['media_id'], $media_atts['context'] ); |
| @@ -339,16 +281,14 @@ | ||
| 339 | 281 | |
| 340 | 282 | /** |
| 341 | 283 | * Look for media where status has changed, compared to what Imagifybeat sends. |
| 342 | 284 | * |
| 343 | - * @since 1.9.3 | |
| 344 | - * @access public | |
| 345 | - * @author Grégory Viguier | |
| 285 | + * @since 1.9.3 | |
| 346 | 286 | * |
| 347 | 287 | * @param array $data The data received. |
| 348 | 288 | * @return array |
| 349 | 289 | */ |
| 350 | - public function get_modified_optimization_statuses( $data ) { | |
| 290 | + private function get_modified_optimization_statuses( $data ) { | |
| 351 | 291 | if ( ! $data ) { |
| 352 | 292 | return []; |
| 353 | 293 | } |
| 354 | 294 | |
| @@ -361,11 +301,14 @@ | ||
| 361 | 301 | } |
| 362 | 302 | |
| 363 | 303 | // Sanitize the IDs: IDs come as strings, prefixed with an undescore character (to prevent JavaScript from screwing everything). |
| 364 | 304 | $media_ids = array_keys( $media_statuses ); |
| 365 | - $media_ids = array_map( function( $media_id ) { | |
| 366 | - return (int) substr( $media_id, 1 ); | |
| 367 | - }, $media_ids ); | |
| 305 | + $media_ids = array_map( | |
| 306 | + function ( $media_id ) { | |
| 307 | + return substr( $media_id, 1 ); | |
| 308 | + }, | |
| 309 | + $media_ids | |
| 310 | + ); | |
| 368 | 311 | $media_ids = array_filter( $media_ids ); |
| 369 | 312 | |
| 370 | 313 | if ( ! $media_ids ) { |
| 371 | 314 | continue; |
| @@ -407,11 +350,9 @@ | ||
| 407 | 350 | |
| 408 | 351 | /** |
| 409 | 352 | * Get an Imagifybeat ID, given an action. |
| 410 | 353 | * |
| 411 | - * @since 1.9.3 | |
| 412 | - * @access public | |
| 413 | - * @author Grégory Viguier | |
| 354 | + * @since 1.9.3 | |
| 414 | 355 | * |
| 415 | 356 | * @param string $action An action corresponding to the ID we want. |
| 416 | 357 | * @return string|bool The ID. False on failure. |
| 417 | 358 | */ |
| @@ -425,11 +366,9 @@ | ||
| 425 | 366 | |
| 426 | 367 | /** |
| 427 | 368 | * Get an Imagifybeat ID, given a callback name. |
| 428 | 369 | * |
| 429 | - * @since 1.9.3 | |
| 430 | - * @access private | |
| 431 | - * @author Grégory Viguier | |
| 370 | + * @since 1.9.3 | |
| 432 | 371 | * |
| 433 | 372 | * @param string $callback A method’s name. |
| 434 | 373 | * @return string|bool The ID. False on failure. |
| 435 | 374 | */ |