PluginProbe
Imsanity / 2.9.4
Imsanity v2.9.4
2.9.4 2.9.3 2.9.2 trunk 2.4.3 2.5.0 2.6.0 2.7.0 2.7.2 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.9.0 2.9.1
imsanity / class-imsanity-cli.php

class-imsanity-cli.php in Imsanity 2.9.4, at class-imsanity-cli.php

122 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class file for Imsanity_CLI
4 *
5 * Imsanity_CLI contains an extension for WP-CLI to enable bulk resizing of images via command line.
6 *
7 * @package Imsanity
8 */
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13 /**
14 * Implements wp-cli extension for bulk resizing.
15 */
16 class Imsanity_CLI extends WP_CLI_Command {
17 /**
18 * Bulk Resize Images
19 *
20 * ## OPTIONS
21 *
22 * <noprompt>
23 * : do not prompt, just resize everything
24 *
25 * ## EXAMPLES
26 *
27 * wp-cli imsanity resize --noprompt
28 *
29 * @synopsis [--noprompt]
30 *
31 * @param array $args A numbered array of arguments provided via WP-CLI without option names.
32 * @param array $assoc_args An array of named arguments provided via WP-CLI.
33 */
34 public function resize( $args, $assoc_args ) {
35
36 // let's get started, shall we?
37 if ( ! defined( 'IMSANITY_DEFAULT_MAX_WIDTH' ) ) {
38 imsanity_init();
39 }
40 $maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
41 $maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
42
43 if ( empty( $assoc_args['noprompt'] ) ) {
44 WP_CLI::warning(
45 __( 'Bulk Resize will alter your original images and cannot be undone!', 'imsanity' ) . "\n" .
46 __( 'It is HIGHLY recommended that you backup your wp-content/uploads folder before proceeding. You will be prompted before resizing each image.', 'imsanity' ) . "\n" .
47 __( 'It is also recommended that you initially resize only 1 or 2 images and verify that everything is working properly before processing your entire library.', 'imsanity' )
48 );
49 }
50
51 /* translators: 1: width in pixels, 2: height in pixels */
52 WP_CLI::line( sprintf( __( 'Resizing images to %1$d x %2$d', 'imsanity' ), $maxw, $maxh ) );
53
54 global $wpdb;
55 $attachments = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE (post_type = 'attachment' OR post_type = 'ims_image') AND post_mime_type LIKE '%%image%%' ORDER BY ID DESC" );
56
57 $image_count = count( $attachments );
58 if ( ! $image_count ) {
59 WP_CLI::success( __( 'There are no images to resize.', 'imsanity' ) );
60 return;
61 } elseif ( empty( $assoc_args['noprompt'] ) ) {
62 WP_CLI::confirm(
63 /* translators: %d: number of images */
64 sprintf( __( 'There are %d images to check.', 'imsanity' ), $image_count ) .
65 ' ' . __( 'Continue?', 'imsanity' )
66 );
67 } else {
68 /* translators: %d: number of images */
69 WP_CLI::line( sprintf( __( 'There are %d images to check.', 'imsanity' ), $image_count ) );
70 }
71
72 $images_finished = 0;
73 foreach ( $attachments as $id ) {
74 $imagew = false;
75 $imageh = false;
76 ++$images_finished;
77
78 $path = get_attached_file( $id );
79 if ( $path ) {
80 $dimensions = getimagesize( $path );
81 if ( is_array( $dimensions ) && count( $dimensions ) >= 2 ) {
82 $imagew = $dimensions[0];
83 $imageh = $dimensions[1];
84 }
85 }
86 if ( empty( $imagew ) || empty( $imageh ) ) {
87 continue;
88 }
89
90 if ( $imagew <= $maxw && $imageh <= $maxh ) {
91 /* translators: %s: File-name of the image */
92 WP_CLI::line( sprintf( esc_html__( 'SKIPPED: %s (Resize not required)', 'imsanity' ), $path ) . " -- $imagew x $imageh" );
93 continue;
94 }
95
96 $confirm = '';
97 if ( empty( $assoc_args['noprompt'] ) ) {
98 $confirm = \cli\prompt(
99 $path . ': ' . $imagew . 'x' . $imageh .
100 "\n" . __( 'Resize (Y/n)?', 'imsanity' )
101 );
102 }
103 if ( 'n' === $confirm ) {
104 continue;
105 }
106
107 $result = imsanity_resize_from_id( $id );
108
109 if ( $result['success'] ) {
110 WP_CLI::line( $result['message'] . " $images_finished / $image_count" );
111 } else {
112 WP_CLI::warning( $result['message'] . " $images_finished / $image_count" );
113 }
114 }
115
116 // and let the user know we are done.
117 WP_CLI::success( __( 'Finished Resizing!', 'imsanity' ) );
118 }
119 }
120
121 WP_CLI::add_command( 'imsanity', 'Imsanity_CLI' );
122