C:\xampp\htdocs\landing\wp-content\updraft\plugins-old\imagify\classes\Imagifybeat\Actions.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
namespace Imagify\Imagifybeat;

use 
Imagify_Requirements;

defined'ABSPATH' ) || die( 'Cheatin’ uh?' );

/**
 * Imagifybeat actions.
 *
 * @since  1.9.3
 * @author Grégory Viguier
 */
class Actions {
    use \
Imagify\Traits\InstanceGetterTrait;

    
/**
     * The list of action IDs.
     * Keys are related to method names, values are Imagifybeat IDs.
     *
     * @var    array
     * @since  1.9.3
     * @access private
     * @author Grégory Viguier
     */
    
private $imagifybeat_ids = [
        
'requirements'                       => 'imagify_requirements',
        
'bulk_optimization_stats'            => 'imagify_bulk_optimization_stats',
        
'bulk_optimization_status'           => 'imagify_bulk_optimization_status',
        
'options_optimization_status'        => 'imagify_options_optimization_status',
        
'library_optimization_status'        => 'imagify_library_optimization_status',
        
'custom_folders_optimization_status' => 'imagify_custom_folders_optimization_status',
    ];

    
/**
     * Class init: launch hooks.
     *
     * @since  1.9.3
     * @access public
     * @author Grégory Viguier
     */
    
public function init() {
        foreach ( 
$this->imagifybeat_ids as $action => $imagifybeat_id ) {
            
add_filter'imagifybeat_received', [ $this'add_' $action '_to_response' ], 10);
        }
    }


    
/** ----------------------------------------------------------------------------------------- */
    /** IMAGIFYBEAT CALLBACKS =================================================================== */
    /** ----------------------------------------------------------------------------------------- */

    /**
     * Add requirements to Imagifybeat data.
     *
     * @since  1.9.3
     * @access public
     * @author Grégory Viguier
     *
     * @param  array $response The Imagifybeat response.
     * @param  array $data     The $_POST data sent.
     * @return array
     */
    
public function add_requirements_to_response$response$data ) {
        
$imagifybeat_id $this->get_imagifybeat_id_for_callback__FUNCTION__ );

        if ( ! 
$imagifybeat_id || empty( $data$imagifybeat_id ] ) ) {
            return 
$response;
        }

        
$response$imagifybeat_id ] = [
            
'curl_missing'          => ! Imagify_Requirements::supports_curl(),
            
'editor_missing'        => ! Imagify_Requirements::supports_image_editor(),
            
'external_http_blocked' => Imagify_Requirements::is_imagify_blocked(),
            
'api_down'              => ! Imagify_Requirements::is_api_up(),
            
'key_is_valid'          => Imagify_Requirements::is_api_key_valid(),
            
'is_over_quota'         => Imagify_Requirements::is_over_quota(),
        ];

        return 
$response;
    }

    
/**
     * Add bulk stats to Imagifybeat data.
     *
     * @since  1.9.3
     * @access public
     * @author Grégory Viguier
     *
     * @param  array $response The Imagifybeat response.
     * @param  array $data     The $_POST data sent.
     * @return array
     */
    
public function add_bulk_optimization_stats_to_response$response$data ) {
        
$imagifybeat_id $this->get_imagifybeat_id_for_callback__FUNCTION__ );

        if ( ! 
$imagifybeat_id || empty( $data$imagifybeat_id ] ) ) {
            return 
$response;
        }

        
$folder_types array_fliparray_filter$data$imagifybeat_id ] ) );

        
$response$imagifybeat_id ] = imagify_get_bulk_stats(
            
$folder_types,
            [
                
'fullset' => true,
            ]
        );

        return 
$response;
    }

    
/**
     * Look for media where status has changed, compared to what Imagifybeat sends.
     * This is used in the bulk optimization page.
     *
     * @since  1.9.3
     * @access public
     * @author Grégory Viguier
     *
     * @param  array $response The Imagifybeat response.
     * @param  array $data     The $_POST data sent.
     * @return array
     */
    
public function add_bulk_optimization_status_to_response$response$data ) {
        
$imagifybeat_id $this->get_imagifybeat_id_for_callback__FUNCTION__ );

        if ( ! 
$imagifybeat_id || empty( $data$imagifybeat_id ] ) || ! is_array$data$imagifybeat_id ] ) ) {
            return 
$response;
        }

        
$statuses = [];

        foreach ( 
$data$imagifybeat_id ] as $item ) {
            if ( empty( 
$statuses$item['context'] ] ) ) {
                
$statuses$item['context'] ] = [];
            }

            
$statuses$item['context'] ][ '_' $item['mediaID'] ] = 1;
        }

        
$results $this->get_modified_optimization_statuses$statuses );

        if ( ! 
$results ) {
            return 
$response;
        }

        
$response$imagifybeat_id ] = [];

        
// Sanitize received data and grab some other info.
        
foreach ( $results as $context_id => $media_atts ) {
            
$process    imagify_get_optimization_process$media_atts['media_id'], $media_atts['context'] );
            
$optim_data $process->get_data();

            if ( 
$optim_data->is_optimized() ) {
                
// Successfully optimized.
                
$full_size_data                $optim_data->get_size_data();
                
$full_size_data                array_merge(
                    [
                        
'original_size'  => 0,
                        
'optimized_size' => 0,
                        
'percent'        => 0,
                    ],
                    
$full_size_data
                
);
                
$response$imagifybeat_id ][] = [
                    
'mediaID'                  => $media_atts['media_id'],
                    
'context'                  => $media_atts['context'],
                    
'success'                  => true,
                    
'status'                   => 'optimized',
                    
// Raw data.
                    
'originalOverallSize'      => $full_size_data['original_size'],
                    
'newOverallSize'           => $full_size_data['optimized_size'],
                    
'overallSaving'            => $full_size_data['original_size'] - $full_size_data['optimized_size'],
                    
'thumbnailsCount'          => $optim_data->get_optimized_sizes_count(),
                    
// Human readable data.
                    
'originalSizeHuman'        => imagify_size_format$full_size_data['original_size'], ),
                    
'newSizeHuman'             => imagify_size_format$full_size_data['optimized_size'], ),
                    
'overallSavingHuman'       => imagify_size_format$full_size_data['original_size'] - $full_size_data['optimized_size'], ),
                    
'originalOverallSizeHuman' => imagify_size_format$full_size_data['original_size'], ),
                    
'percentHuman'             => $full_size_data['percent'] . '%',
                ];
            } elseif ( 
$optim_data->is_already_optimized() ) {
                
// Already optimized.
                
$response$imagifybeat_id ][] = [
                    
'mediaID' => $media_atts['media_id'],
                    
'context' => $media_atts['context'],
                    
'success' => true,
                    
'status'  => 'already-optimized',
                ];
            } else {
                
// Error.
                
$full_size_data $optim_data->get_size_data();
                
$message        = ! empty( $full_size_data['error'] ) ? $full_size_data['error'] : '';
                
$status         'error';

                if ( 
'You\'ve consumed all your data. You have to upgrade your account to continue' === $message ) {
                    
$status 'over-quota';
                }

                
$response$imagifybeat_id ][] = [
                    
'mediaID' => $media_atts['media_id'],
                    
'context' => $media_atts['context'],
                    
'success' => false,
                    
'status'  => $status,
                    
'error'   => imagify_translate_api_message$message ),
                ];
            }
        }

        return 
$response;
    }

    
/**
     * Look for media where status has changed, compared to what Imagifybeat sends.
     * This is used in the settings page.
     *
     * @since  1.9
     * @author Grégory Viguier
     *
     * @param  array $response The Imagifybeat response.
     * @param  array $data     The $_POST data sent.
     * @return array
     */
    
public function add_options_optimization_status_to_response$response$data ) {
        
$imagifybeat_id $this->get_imagifybeat_id_for_callback__FUNCTION__ );

        if ( ! 
$imagifybeat_id || empty( $data$imagifybeat_id ] ) || ! is_array$data$imagifybeat_id ] ) ) {
            return 
$response;
        }

        
$statuses = [];

        foreach ( 
$data$imagifybeat_id ] as $item ) {
            if ( empty( 
$statuses$item['context'] ] ) ) {
                
$statuses$item['context'] ] = [];
            }

            
$statuses$item['context'] ][ '_' $item['mediaID'] ] = 1;
        }

        
$results $this->get_modified_optimization_statuses$statuses );

        if ( ! 
$results ) {
            return 
$response;
        }

        
$response$imagifybeat_id ] = [];

        foreach ( 
$results as $result ) {
            
$response$imagifybeat_id ][] = [
                
'mediaID' => $result['media_id'],
                
'context' => $result['context'],
            ];
        }

        return 
$response;
    }

    
/**
     * Look for media where status has changed, compared to what Imagifybeat sends.
     * This is used in the WP Media Library.
     *
     * @since  1.9.3
     * @access public
     * @author Grégory Viguier
     *
     * @param  array $response The Imagifybeat response.
     * @param  array $data     The $_POST data sent.
     * @return array
     */
    
public function add_library_optimization_status_to_response$response$data ) {
        
$imagifybeat_id $this->get_imagifybeat_id_for_callback__FUNCTION__ );

        if ( ! 
$imagifybeat_id || empty( $data$imagifybeat_id ] ) || ! is_array$data$imagifybeat_id ] ) ) {
            return 
$response;
        }

        
$response$imagifybeat_id ] = $this->get_modified_optimization_statuses$data$imagifybeat_id ] );

        if ( ! 
$response$imagifybeat_id ] ) {
            return 
$response;
        }

        
// Sanitize received data and grab some other info.
        
foreach ( $response$imagifybeat_id ] as $context_id => $media_atts ) {
            
$process imagify_get_optimization_process$media_atts['media_id'], $media_atts['context'] );

            
$response$imagifybeat_id ][ $context_id ] = get_imagify_media_column_content$processfalse );
        }

        return 
$response;
    }

    
/**
     * Look for media where status has changed, compared to what Imagifybeat sends.
     * This is used in the custom folders list (the "Other Media" page).
     *
     * @since  1.9.3
     * @access public
     * @author Grégory Viguier
     *
     * @param  array $response The Imagifybeat response.
     * @param  array $data     The $_POST data sent.
     * @return array
     */
    
public function add_custom_folders_optimization_status_to_response$response$data ) {
        
$imagifybeat_id $this->get_imagifybeat_id_for_callback__FUNCTION__ );

        if ( ! 
$imagifybeat_id || empty( $data$imagifybeat_id ] ) || ! is_array$data$imagifybeat_id ] ) ) {
            return 
$response;
        }

        
$response$imagifybeat_id ] = $this->get_modified_optimization_statuses$data$imagifybeat_id ] );

        if ( ! 
$response$imagifybeat_id ] ) {
            return 
$response;
        }

        
$admin_ajax_post = \Imagify_Admin_Ajax_Post::get_instance();
        
$list_table      = new \Imagify_Files_List_Table( [
            
'screen' => 'imagify-files',
        ] );

        
// Sanitize received data and grab some other info.
        
foreach ( $response$imagifybeat_id ] as $context_id => $media_atts ) {
            
$process imagify_get_optimization_process$media_atts['media_id'], $media_atts['context'] );

            
$response$imagifybeat_id ][ $context_id ] = $admin_ajax_post->get_media_columns$process$list_table );
        }

        return 
$response;
    }


    
/** ----------------------------------------------------------------------------------------- */
    /** TOOLS =================================================================================== */
    /** ----------------------------------------------------------------------------------------- */

    /**
     * Look for media where status has changed, compared to what Imagifybeat sends.
     *
     * @since  1.9.3
     * @access public
     * @author Grégory Viguier
     *
     * @param  array $data The data received.
     * @return array
     */
    
public function get_modified_optimization_statuses$data ) {
        if ( ! 
$data ) {
            return [];
        }

        
$output = [];

        
// Sanitize received data and grab some other info.
        
foreach ( $data as $context => $media_statuses ) {
            if ( ! 
$context || ! $media_statuses || ! is_array$media_statuses ) ) {
                continue;
            }

            
// Sanitize the IDs: IDs come as strings, prefixed with an undescore character (to prevent JavaScript from screwing everything).
            
$media_ids array_keys$media_statuses );
            
$media_ids array_map( function( $media_id ) {
                return (int) 
substr$media_id);
            }, 
$media_ids );
            
$media_ids array_filter$media_ids );

            if ( ! 
$media_ids ) {
                continue;
            }

            
// Sanitize the context.
            
$context_instance   imagify_get_context$context );
            
$context            $context_instance->get_name();
            
$process_class_name imagify_get_optimization_process_class_name$context );
            
$transient_name     sprintf$process_class_name::LOCK_NAME$context'%' );
            
$is_network_wide    $context_instance->is_network_wide();

            \
Imagify_DB::cache_process_locks$context$media_ids );

            
// Now that everything is cached for this context, we can get the transients without hitting the DB.
            
foreach ( $media_ids as $id ) {
                
$is_locked   = (bool) $media_statuses'_' $id ];
                
$option_name str_replace'%'$id$transient_name );

                if ( 
$is_network_wide ) {
                    
$in_db = (bool) get_site_transient$option_name );
                } else {
                    
$in_db = (bool) get_transient$option_name );
                }

                if ( 
$is_locked === $in_db ) {
                    continue;
                }

                
$output$context '_' $id ] = [
                    
'media_id' => $id,
                    
'context'  => $context,
                ];
            }
        }

        return 
$output;
    }

    
/**
     * Get an Imagifybeat ID, given an action.
     *
     * @since  1.9.3
     * @access public
     * @author Grégory Viguier
     *
     * @param  string $action An action corresponding to the ID we want.
     * @return string|bool    The ID. False on failure.
     */
    
public function get_imagifybeat_id$action ) {
        if ( ! empty( 
$this->imagifybeat_ids$action ] ) ) {
            return 
$this->imagifybeat_ids$action ];
        }

        return 
false;
    }

    
/**
     * Get an Imagifybeat ID, given a callback name.
     *
     * @since  1.9.3
     * @access private
     * @author Grégory Viguier
     *
     * @param  string $callback A method’s name.
     * @return string|bool      The ID. False on failure.
     */
    
private function get_imagifybeat_id_for_callback$callback ) {
        if ( 
preg_match'@^add_(?<id>.+)_to_response$@'$callback$matches ) ) {
            return 
$this->get_imagifybeat_id$matches['id'] );
        }

        return 
false;
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586