C:\xampp\htdocs\landing\wp-content\plugins\wp-smushit\core\class-cli.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
<?php
/**
 * Class CLI
 *
 * @since 3.1
 * @package Smush\Core
 */

namespace Smush\Core;

use 
WP_CLI;
use 
WP_CLI_Command;
use 
WP_Smush;

if ( ! 
defined'WPINC' ) ) {
    die;
}

/**
 * Reduce image file sizes, improve performance and boost your SEO using the free WPMU DEV Smush API.
 */
class CLI extends WP_CLI_Command {

    
/**
     * Optimize image.
     *
     * ## OPTIONS
     *
     * [--type=<type>]
     * : Optimize single image, batch or all images.
     * ---
     * default: all
     * options:
     *   - all
     *   - single
     *   - batch
     * ---
     *
     * [--image=<ID>]
     * : Attachment ID to compress.
     * ---
     * default: 0
     * ---
     *
     * ## EXAMPLES
     *
     * # Smush all images.
     * $ wp smush compress
     *
     * # Smush single image with ID = 10.
     * $ wp smush compress --type=single --image=10
     *
     * # Smush first 5 images.
     * $ wp smush compress --type=batch --image=5
     */
    
public function compress$args$assoc_args ) {
        
$type  $assoc_args['type'];
        
$image $assoc_args['image'];

        switch ( 
$type ) {
            case 
'single':
                
/* translators: %d - image ID */
                
$msg sprintf__'Smushing image ID: %d''wp-smushit' ), absint$image ) );
                
$this->smush$msg, array( $image ) );
                
$this->_list( array() );
                break;
            case 
'batch':
                
/* translators: %d - number of images */
                
$msg sprintf__'Smushing first %d images''wp-smushit' ), absint$image ) );
                
$this->smush_all$msg$image );
                break;
            case 
'all':
            default:
                
$this->smush_all__'Smushing all images''wp-smushit' ) );
                break;
        }
    }

    
/**
     * List unoptimized images.
     *
     * ## OPTIONS
     *
     * [<count>]
     * : Limit number of images to get.
     *
     * ## EXAMPLES
     *
     * # Get all unoptimized images.
     * $ wp smush list
     *
     * # Get the first 100 images that are not optimized.
     * $ wp smush list 100
     *
     * @subcommand list
     * @when after_wp_load
     */
    
public function _list$args ) {
        if ( ! empty( 
$args ) ) {
            list( 
$count ) = $args;
        } else {
            
$count = -1;
        }

        
$response WP_CLI::launch_self(
            
'post list',
            array( 
'--meta_compare=NOT EXISTS' ),
            array(
                
'post_type'      => 'attachment',
                
'fields'         => 'ID, guid, post_mime_type',
                
'meta_key'       => 'wp-smpro-smush-data',
                
'format'         => 'json',
                
'posts_per_page' => (int) $count,
            ),
            
false,
            
true
        
);

        
$images json_decode$response->stdout );

        if ( empty( 
$images ) ) {
            
WP_CLI::success__'No uncompressed images found''wp-smushit' ) );
            return;
        }

        
WP_CLI::success__'Unsmushed images:''wp-smushit' ) );
        
WP_CLI\Utils\format_items'table'$images, array( 'ID''guid''post_mime_type' ) );
    }

    
/**
     * Restore image.
     *
     * ## OPTIONS
     *
     * [--id=<ID>]
     * : Attachment ID to restore.
     * ---
     * default: all
     * ---
     *
     * ## EXAMPLES
     *
     * # Restore all images that have backups.
     * $ wp smush restore
     *
     * # Restore single image with ID = 10.
     * $ wp smush restore --id=10
     */
    
public function restore$args$assoc_args ) {
        
$id $assoc_args['id'];

        if ( 
'all' === $id ) {
            
$this->restore_image();
        } else {
            
$this->restore_imageabsint$id ) );
        }
    }

    
/**
     * Smush single image.
     *
     * @since 3.1
     *
     * @param string $msg     Message for progress bar status.
     * @param array  $images  Attachment IDs.
     */
    
private function smush$msg ''$images = array() ) {
        
$success  false;
        
$errors   = array();
        
$progress WP_CLI\Utils\make_progress_bar$msgcount$images ) + );

        
$core WP_Smush::get_instance()->core();

        
// We need to initialize the database module (maybe all other modules as well?).
        
Settings::get_instance()->init();

        
$unsmushed_attachments $core->get_unsmushed_attachments();

        while ( 
$images ) {
            
$progress->tick();

            
$attachment_id array_pop$images );

            
// Skip if already Smushed.
            
if ( ! in_array( (int) $attachment_id$unsmushed_attachmentstrue ) ) {
                
/* translators: %d - attachment ID */
                
$errors[] = sprintf__'Image (ID: %d) already compressed''wp-smushit' ), $attachment_id );
                continue;
            }

            
$status $core->mod->smush->smush_single$attachment_idtrue );

            if ( 
is_array$status ) && isset( $status['error'] ) ) {
                
/* translators: %1$d - attachment ID, %2$s - error. */
                
$errors[] = sprintf__'Error compressing image (ID: %1$d). %2$s''wp-smushit' ), $attachment_id$status['error'] );
                continue;
            }

            
$success true;
        }

        
$progress->tick();
        
$progress->finish();

        if ( ! empty( 
$errors ) ) {
            foreach ( 
$errors as $error ) {
                
WP_CLI::warning$error );
            }
        }

        if ( 
$success ) {
            
WP_CLI::success__'Image compressed''wp-smushit' ) );
        }
    }

    
/**
     * Smush all uncompressed images.
     *
     * @since 3.1
     *
     * @param string $msg    Message for progress bar status.
     * @param int    $batch  Compress only this number of images.
     */
    
private function smush_all$msg$batch ) {
        
$attachments WP_Smush::get_instance()->core()->get_unsmushed_attachments();

        if ( 
$batch ) {
            
$attachments array_slice$attachments0$batch );
        }

        
$progress WP_CLI\Utils\make_progress_bar$msgcount$attachments ) );

        foreach ( 
$attachments as $attachment_id ) {
            
WP_Smush::get_instance()->core()->mod->smush->smush_single$attachment_idtrue );
            
$progress->tick();
        }

        
$progress->finish();
        
WP_CLI::success__'All images compressed''wp-smushit' ) );
    }

    
/**
     * Restore all images.
     *
     * @since 3.1
     *
     * @param int $id  Image ID to restore. Default: 0 - restores all images.
     */
    
private function restore_image$id ) {
        
$core WP_Smush::get_instance()->core();

        
$attachments = ! empty( $core->smushed_attachments ) ? $core->smushed_attachments $core->get_smushed_attachments();

        if ( empty( 
$attachments ) ) {
            
WP_CLI::success__'No images available to restore''wp-smushit' ) );
            return;
        }

        if ( 
!== $id ) {
            if ( ! 
in_array( (string) $id$attachmentstrue ) ) {
                
WP_CLI::warning__'Image with defined ID not found''wp-smushit' ) );
                return;
            }

            
$attachments = array( $id );
        }

        
$progress WP_CLI\Utils\make_progress_bar__'Restoring images''wp-smushit' ), count$attachments ) );

        
$warning false;
        foreach ( 
$attachments as $attachment_id ) {
            if ( ! 
$this->can_restore$attachment_id ) ) {
                
$warning true;
                
WP_CLI::warning__"Image {$attachment_id} cannot be restored"'wp-smushit' ) );
                
$progress->tick();
                continue;
            }

            
$core->mod->backup->restore_image$attachment_idfalse );
            
$progress->tick();
        }

        
$progress->finish();

        if ( 
$warning ) {
            
WP_CLI::error__'There were issues restoring some images''wp-smushit' ) );
        } else {
            
WP_CLI::success__'All images restored''wp-smushit' ) );
        }

    }

    
/**
     * Verify, that image can be restored.
     *
     * TODO: this copies the Smush code, so it needs to be refactored.
     *
     * @since 3.1.0
     *
     * @param int $image_id  Attachment ID.
     *
     * @return bool
     */
    
private function can_restore$image_id ) {
        
// Get the image path for all sizes.
        
$file get_attached_file$image_id );

        
// Get stored backup path, if any.
        
$backup_sizes get_post_meta$image_id'_wp_attachment_backup_sizes'true );

        
// Check if we've a backup path.
        
if ( ! empty( $backup_sizes ) && ( ! empty( $backup_sizes['smush-full'] ) || ! empty( $backup_sizes['smush_png_path'] ) ) ) {
            
// Check for PNG backup.
            
$backup = ! empty( $backup_sizes['smush_png_path'] ) ? $backup_sizes['smush_png_path'] : '';

            
// Check for original full size image backup.
            
$backup = empty( $backup ) && ! empty( $backup_sizes['smush-full'] ) ? $backup_sizes['smush-full'] : $backup;
            
$backup = ! empty( $backup['file'] ) ? $backup['file'] : '';
        }

        
// If we still don't have a backup path, use traditional method to get it.
        
if ( empty( $backup ) ) {
            
// Check backup for Full size.
            
$backup WP_Smush::get_instance()->core()->mod->backup->get_image_backup_path$file );
        } else {
            
// Get the full path for file backup.
            
$backup str_replacewp_basename$file ), wp_basename$backup ), $file );
        }

        
$file_exists apply_filters'smush_backup_exists'file_exists$backup ), $image_id$backup );

        if ( 
$file_exists ) {
            return 
true;
        }

        
// Additional Backup Check for JPEGs converted from PNG.
        
$pngjpg_savings get_post_meta$image_idWP_SMUSH_PREFIX 'pngjpg_savings'true );
        if ( ! empty( 
$pngjpg_savings ) ) {

            
// Get the original File path and check if it exists.
            
$backup get_post_meta$image_idWP_SMUSH_PREFIX 'original_file'true );
            
$backup Helper::original_file$backup );

            if ( ! empty( 
$backup ) && is_file$backup ) ) {
                return 
true;
            }
        }

        return 
false;
    }

}
x

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