| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Cli Name: Plus WebP or AVIF CLI |
| 4 | 4 | * Description: Generate WebP or AVIF by WP-CLI. |
| 5 | - * Version: 4.00 | |
| 5 | + * Version: 3.00 | |
| 6 | 6 | * Author: Katsushi Kawamori |
| 7 | 7 | * Author URI: https://riverforest-wp.info/ |
| 8 | 8 | * License: GPLv2 or later |
| 9 | 9 | * License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| @@ -26,12 +26,8 @@ | ||
| 26 | 26 | along with this program; if not, write to the Free Software |
| 27 | 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 | 28 | */ |
| 29 | 29 | |
| 30 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 31 | - exit; | |
| 32 | -} | |
| 33 | - | |
| 34 | 30 | $pluswebpavifcli = new PlusWebpAVIFCLI(); |
| 35 | 31 | |
| 36 | 32 | /** ================================================== |
| 37 | 33 | * Main |
| @@ -51,15 +47,13 @@ | ||
| 51 | 47 | /** ================================================== |
| 52 | 48 | * Settings change |
| 53 | 49 | * |
| 54 | 50 | * @param string $output output type. |
| 55 | - * @param bool $replace Replace. | |
| 56 | - * @param bool $addext Add ext. | |
| 57 | 51 | * @param array $assoc_args optional arguments. |
| 58 | 52 | * @param array $pluswebp_settings settings. |
| 59 | 53 | * @since 2.00 |
| 60 | 54 | */ |
| 61 | - private function settings_change( $output, $replace, $addext, $assoc_args, $pluswebp_settings ) { | |
| 55 | + private function settings_change( $output, $assoc_args, $pluswebp_settings ) { | |
| 62 | 56 | |
| 63 | 57 | switch ( $output ) { |
| 64 | 58 | case 'webp': |
| 65 | 59 | $pluswebp_settings['output_mime'] = 'image/webp'; |
| @@ -66,13 +60,12 @@ | ||
| 66 | 60 | break; |
| 67 | 61 | case 'avif': |
| 68 | 62 | $pluswebp_settings['output_mime'] = 'image/avif'; |
| 69 | 63 | break; |
| 64 | + default: | |
| 65 | + $pluswebp_settings['output_mime'] = 'image/webp'; | |
| 70 | 66 | } |
| 71 | 67 | |
| 72 | - $pluswebp_settings['replace'] = $replace; | |
| 73 | - $pluswebp_settings['addext'] = $addext; | |
| 74 | - | |
| 75 | 68 | if ( array_key_exists( 'quality', $assoc_args ) ) { |
| 76 | 69 | $pluswebp_settings['quality'] = intval( $assoc_args['quality'] ); |
| 77 | 70 | if ( 0 === $pluswebp_settings['quality'] || 100 < $pluswebp_settings['quality'] ) { |
| 78 | 71 | WP_CLI::error( __( 'optional argument quality(int) : Invalid value.', 'plus-webp' ) ); |
| @@ -77,8 +70,28 @@ | ||
| 77 | 70 | if ( 0 === $pluswebp_settings['quality'] || 100 < $pluswebp_settings['quality'] ) { |
| 78 | 71 | WP_CLI::error( __( 'optional argument quality(int) : Invalid value.', 'plus-webp' ) ); |
| 79 | 72 | } |
| 80 | 73 | } |
| 74 | + if ( array_key_exists( 'replace', $assoc_args ) ) { | |
| 75 | + $replace = strtolower( sanitize_text_field( $assoc_args['replace'] ) ); | |
| 76 | + if ( 'true' === $replace ) { | |
| 77 | + $pluswebp_settings['replace'] = true; | |
| 78 | + } else if ( 'false' === $replace ) { | |
| 79 | + $pluswebp_settings['replace'] = false; | |
| 80 | + } else { | |
| 81 | + WP_CLI::error( __( 'optional argument replace(bool) : Invalid value.', 'plus-webp' ) ); | |
| 82 | + } | |
| 83 | + } | |
| 84 | + if ( array_key_exists( 'addext', $assoc_args ) ) { | |
| 85 | + $addext = strtolower( sanitize_text_field( wp_unslash( $assoc_args['addext'] ) ) ); | |
| 86 | + if ( 'true' === $addext ) { | |
| 87 | + $pluswebp_settings['addext'] = true; | |
| 88 | + } else if ( 'false' === $addext ) { | |
| 89 | + $pluswebp_settings['addext'] = false; | |
| 90 | + } else { | |
| 91 | + WP_CLI::error( __( 'optional argument addext(bool) : Invalid value.', 'plus-webp' ) ); | |
| 92 | + } | |
| 93 | + } | |
| 81 | 94 | if ( array_key_exists( 'types', $assoc_args ) ) { |
| 82 | 95 | $mime_types = array( |
| 83 | 96 | 'image/jpeg', |
| 84 | 97 | 'image/png', |
| @@ -103,46 +116,11 @@ | ||
| 103 | 116 | |
| 104 | 117 | return $pluswebp_settings; |
| 105 | 118 | } |
| 106 | 119 | |
| 107 | - /** | |
| 120 | + /** ================================================== | |
| 108 | 121 | * Plus WebP or AVIF command |
| 109 | 122 | * |
| 110 | - * ## OPTIONS | |
| 111 | - * | |
| 112 | - * <string> | |
| 113 | - * : webp -> Generated WebP, avif -> Generated AVIF, help -> Specification of this command. | |
| 114 | - * | |
| 115 | - * [<string>] | |
| 116 | - * : Optional argument - mail -> Send results via email. | |
| 117 | - * | |
| 118 | - * [<string>] | |
| 119 | - * : Optional argument - replace -> WebP or AVIF replacement of images and contents. | |
| 120 | - * | |
| 121 | - * [<string>] | |
| 122 | - * : Optional argument - addext -> Append the webp or avif extension to the original filename. | |
| 123 | - * | |
| 124 | - * [--pid=<int>] | |
| 125 | - * : Optional argument - --pid=12152 -> Process only specified Media ID(Conversion source ID). | |
| 126 | - * | |
| 127 | - * [--quality=<int>] | |
| 128 | - * : Optional argument - --quality=90 -> Specifies the quality of WebP or AVIF. | |
| 129 | - * | |
| 130 | - * [--types=<string>] | |
| 131 | - * : Optional argument - --types=image/png,image/gif -> MIME type to convert. | |
| 132 | - * | |
| 133 | - * ## EXAMPLES | |
| 134 | - * | |
| 135 | - * wp pluswebpavif webp --pid=12152 | |
| 136 | - * // Create WebP. Post ID 12152 only. | |
| 137 | - * | |
| 138 | - * wp pluswebpavif avif mail --quality=90 --types=image/png,image/gif | |
| 139 | - * // Create AVIF. Result email sent. Quality 90%. Convert image/png,image/gif. | |
| 140 | - * | |
| 141 | - * wp pluswebpavif webp replace addext | |
| 142 | - * // Create WebP. WebP or AVIF replacement of images and contents. Append the webp or avif extension to the original filename. | |
| 143 | - * | |
| 144 | - * @when after_wp_load | |
| 145 | 123 | * @param array $args arguments. |
| 146 | 124 | * @param array $assoc_args optional arguments. |
| 147 | 125 | * @since 1.00 |
| 148 | 126 | */ |
| @@ -147,56 +125,40 @@ | ||
| 147 | 125 | * @since 1.00 |
| 148 | 126 | */ |
| 149 | 127 | public function pluswebpavif_cli_command( $args, $assoc_args ) { |
| 150 | 128 | |
| 151 | - $output = null; | |
| 152 | - $mail = false; | |
| 153 | - $replace = false; | |
| 154 | - $addext = false; | |
| 155 | - if ( ! empty( $args ) ) { | |
| 156 | - foreach ( $args as $key => $value ) { | |
| 157 | - switch ( $value ) { | |
| 158 | - case 'webp': | |
| 159 | - $output = 'webp'; | |
| 160 | - break; | |
| 161 | - case 'avif': | |
| 162 | - $output = 'avif'; | |
| 163 | - break; | |
| 164 | - case 'help': | |
| 165 | - $output = 'help'; | |
| 166 | - break; | |
| 167 | - case 'mail': | |
| 168 | - $mail = true; | |
| 169 | - break; | |
| 170 | - case 'replace': | |
| 171 | - $replace = true; | |
| 172 | - break; | |
| 173 | - case 'addext': | |
| 174 | - $addext = true; | |
| 175 | - break; | |
| 176 | - } | |
| 177 | - } | |
| 178 | - } | |
| 179 | - | |
| 180 | 129 | $input_error_message = __( 'Please enter the arguments.', 'plus-webp' ) . "\n"; |
| 181 | - $input_error_message .= __( '1st argument(string) : webp -> Generated WebP, avif -> Generated AVIF, help -> Specification of this command.', 'plus-webp' ) . "\n"; | |
| 182 | - $input_error_message .= __( 'optional argument(string) : mail -> Send results via email.', 'plus-webp' ) . "\n"; | |
| 183 | - $input_error_message .= __( 'optional argument(string) : replace -> WebP or AVIF replacement of images and contents.', 'plus-webp' ) . "\n"; | |
| 184 | - $input_error_message .= __( 'optional argument(string) : addext -> Append the webp or avif extension to the original filename.', 'plus-webp' ) . "\n"; | |
| 185 | - $input_error_message .= __( 'optional argument pid(int) : --pid=12152 -> Process only specified Media ID(Conversion source ID).', 'plus-webp' ) . "\n"; | |
| 186 | - $input_error_message .= __( 'optional argument quality(int) : --quality=90 -> Specifies the quality of WebP or AVIF.', 'plus-webp' ) . "\n"; | |
| 187 | - $input_error_message .= __( 'optional argument types(string) : --types=image/png,image/gif -> MIME type to convert.', 'plus-webp' ) . "\n"; | |
| 130 | + $input_error_message .= __( '1st argument(string) : webp -> Generated WebP, avif -> Generated AVIF', 'plus-webp' ) . "\n"; | |
| 131 | + $input_error_message .= __( 'optional argument mail(bool) : --mail=true -> Send results via email', 'plus-webp' ) . "\n"; | |
| 132 | + $input_error_message .= __( 'optional argument pid(int) : --pid=12152 : Media ID(Conversion source ID) -> Process only specified Media ID.', 'plus-webp' ) . "\n"; | |
| 133 | + $input_error_message .= __( 'optional argument quality(int) : --quality=90 : Specifies the quality of WebP or AVIF.', 'plus-webp' ) . "\n"; | |
| 134 | + $input_error_message .= __( 'optional argument replace(bool) : --replace=false : WebP or AVIF replacement of images and contents.', 'plus-webp' ) . "\n"; | |
| 135 | + $input_error_message .= __( 'optional argument addext(bool) : --addext=true : Append the webp or avif extension to the original filename.', 'plus-webp' ) . "\n"; | |
| 136 | + $input_error_message .= __( 'optional argument types(string) : --types=image/png,image/gif : MIME type to convert.', 'plus-webp' ) . "\n"; | |
| 137 | + if ( is_array( $args ) && ! empty( $args ) && | |
| 138 | + ( 'webp' === $args[0] || 'avif' === $args[0] ) ) { | |
| 188 | 139 | |
| 189 | - if ( 'webp' === $output || 'avif' === $output ) { | |
| 140 | + $output = $args[0]; | |
| 190 | 141 | |
| 191 | 142 | $pid = 0; |
| 192 | 143 | if ( array_key_exists( 'pid', $assoc_args ) ) { |
| 193 | 144 | $pid = intval( $assoc_args['pid'] ); |
| 194 | 145 | } |
| 146 | + $mail = false; | |
| 147 | + if ( array_key_exists( 'mail', $assoc_args ) ) { | |
| 148 | + $mail = strtolower( sanitize_text_field( wp_unslash( $assoc_args['mail'] ) ) ); | |
| 149 | + if ( 'true' === $mail ) { | |
| 150 | + $mail = true; | |
| 151 | + } else if ( 'false' === $addext ) { | |
| 152 | + $mail = false; | |
| 153 | + } else { | |
| 154 | + WP_CLI::error( __( 'optional argument mail(bool) : Invalid value.', 'plus-webp' ) ); | |
| 155 | + } | |
| 156 | + } | |
| 195 | 157 | |
| 196 | 158 | $pluswebp_settings_default = get_option( 'pluswebp' ); |
| 197 | 159 | update_option( 'pluswebp_default', $pluswebp_settings_default ); |
| 198 | - $pluswebp_settings = $this->settings_change( $output, $replace, $addext, $assoc_args, $pluswebp_settings_default ); | |
| 160 | + $pluswebp_settings = $this->settings_change( $output, $assoc_args, $pluswebp_settings_default ); | |
| 199 | 161 | update_option( 'pluswebp', $pluswebp_settings ); |
| 200 | 162 | |
| 201 | 163 | if ( 0 < $pid ) { |
| 202 | 164 | $metadata_org = wp_get_attachment_metadata( $pid ); |
| @@ -214,9 +176,9 @@ | ||
| 214 | 176 | do_action( 'pluswebp_mail_generate_message', $messages, 1 ); |
| 215 | 177 | } |
| 216 | 178 | } else { |
| 217 | 179 | $message = 'ID: ' . $pid . "\n"; |
| 218 | - $message .= __( 'This media ID has already been converted or is neither an image nor media.', 'plus-webp' ) . "\n"; | |
| 180 | + $message .= __( 'This media ID has already been converted or is neither an image nor a madia.', 'plus-webp' ) . "\n"; | |
| 219 | 181 | $message .= "\n"; |
| 220 | 182 | WP_CLI::warning( $message ); |
| 221 | 183 | } |
| 222 | 184 | } else { |