PluginProbe
Imsanity / 2.8.4
Imsanity v2.8.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.8.4, at class-imsanity-cli.php

116 lines 3.5 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 // imsanity_init();.
38 $maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
39 $maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
40
41 if ( empty( $assoc_args['noprompt'] ) ) {
42 WP_CLI::warning(
43 __( 'Bulk Resize will alter your original images and cannot be undone!', 'imsanity' ) . "\n" .
44 __( 'It is HIGHLY recommended that you backup your wp-content/uploads folder before proceeding. You will be prompted before resizing each image.', 'imsanity' ) . "\n" .
45 __( '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' )
46 );
47 }
48
49 /* translators: 1: width in pixels, 2: height in pixels */
50 WP_CLI::line( sprintf( __( 'Resizing images to %1$d x %2$d', 'imsanity' ), $maxw, $maxh ) );
51
52 global $wpdb;
53 $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" );
54
55 $image_count = count( $attachments );
56 if ( ! $image_count ) {
57 WP_CLI::success( __( 'There are no images to resize.', 'imsanity' ) );
58 return;
59 } elseif ( empty( $assoc_args['noprompt'] ) ) {
60 WP_CLI::confirm(
61 /* translators: %d: number of images */
62 sprintf( __( 'There are %d images to check.', 'imsanity' ), $image_count ) .
63 ' ' . __( 'Continue?', 'imsanity' )
64 );
65 } else {
66 /* translators: %d: number of images */
67 WP_CLI::line( sprintf( __( 'There are %d images to check.', 'imsanity' ), $image_count ) );
68 }
69
70 $images_finished = 0;
71 foreach ( $attachments as $id ) {
72 $imagew = false;
73 $imageh = false;
74 ++$images_finished;
75
76 $path = get_attached_file( $id );
77 if ( $path ) {
78 list( $imagew, $imageh ) = getimagesize( $path );
79 }
80 if ( empty( $imagew ) || empty( $imageh ) ) {
81 continue;
82 }
83
84 if ( $imagew <= $maxw && $imageh <= $maxh ) {
85 /* translators: %s: File-name of the image */
86 WP_CLI::line( sprintf( esc_html__( 'SKIPPED: %s (Resize not required)', 'imsanity' ), $path ) . " -- $imagew x $imageh" );
87 continue;
88 }
89
90 $confirm = '';
91 if ( empty( $assoc_args['noprompt'] ) ) {
92 $confirm = \cli\prompt(
93 $path . ': ' . $imagew . 'x' . $imageh .
94 "\n" . __( 'Resize (Y/n)?', 'imsanity' )
95 );
96 }
97 if ( 'n' === $confirm ) {
98 continue;
99 }
100
101 $result = imsanity_resize_from_id( $id );
102
103 if ( $result['success'] ) {
104 WP_CLI::line( $result['message'] . " $images_finished / $image_count" );
105 } else {
106 WP_CLI::warning( $result['message'] . " $images_finished / $image_count" );
107 }
108 }
109
110 // and let the user know we are done.
111 WP_CLI::success( __( 'Finished Resizing!', 'imsanity' ) );
112 }
113 }
114
115 WP_CLI::add_command( 'imsanity', 'Imsanity_CLI' );
116