| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cli Name: Plus WebP or AVIF CLI |
| 4 |
* Description: Generate WebP or AVIF by WP-CLI. |
| 5 |
* Version: 4.00 |
| 6 |
* Author: Katsushi Kawamori |
| 7 |
* Author URI: https://riverforest-wp.info/ |
| 8 |
* License: GPLv2 or later |
| 9 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 10 |
* |
| 11 |
* @package Plus WebP or AVIF |
| 12 |
*/ |
| 13 |
|
| 14 |
/* |
| 15 |
Copyright (c) 2024- Katsushi Kawamori (email : dodesyoswift312@gmail.com) |
| 16 |
This program is free software; you can redistribute it and/or modify |
| 17 |
it under the terms of the GNU General Public License as published by |
| 18 |
the Free Software Foundation; version 2 of the License. |
| 19 |
|
| 20 |
This program is distributed in the hope that it will be useful, |
| 21 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
GNU General Public License for more details. |
| 24 |
|
| 25 |
You should have received a copy of the GNU General Public License |
| 26 |
along with this program; if not, write to the Free Software |
| 27 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 |
*/ |
| 29 |
|
| 30 |
if ( ! defined( 'ABSPATH' ) ) { |
| 31 |
exit; |
| 32 |
} |
| 33 |
|
| 34 |
$pluswebpavifcli = new PlusWebpAVIFCLI(); |
| 35 |
|
| 36 |
/** ================================================== |
| 37 |
* Main |
| 38 |
*/ |
| 39 |
class PlusWebpAVIFCLI { |
| 40 |
|
| 41 |
/** ================================================== |
| 42 |
* Construct |
| 43 |
* |
| 44 |
* @since 1.00 |
| 45 |
*/ |
| 46 |
public function __construct() { |
| 47 |
|
| 48 |
WP_CLI::add_command( 'pluswebpavif', array( $this, 'pluswebpavif_cli_command' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
/** ================================================== |
| 52 |
* Settings change |
| 53 |
* |
| 54 |
* @param string $output output type. |
| 55 |
* @param bool $replace Replace. |
| 56 |
* @param bool $addext Add ext. |
| 57 |
* @param array $assoc_args optional arguments. |
| 58 |
* @param array $pluswebp_settings settings. |
| 59 |
* @since 2.00 |
| 60 |
*/ |
| 61 |
private function settings_change( $output, $replace, $addext, $assoc_args, $pluswebp_settings ) { |
| 62 |
|
| 63 |
switch ( $output ) { |
| 64 |
case 'webp': |
| 65 |
$pluswebp_settings['output_mime'] = 'image/webp'; |
| 66 |
break; |
| 67 |
case 'avif': |
| 68 |
$pluswebp_settings['output_mime'] = 'image/avif'; |
| 69 |
break; |
| 70 |
} |
| 71 |
|
| 72 |
$pluswebp_settings['replace'] = $replace; |
| 73 |
$pluswebp_settings['addext'] = $addext; |
| 74 |
|
| 75 |
if ( array_key_exists( 'quality', $assoc_args ) ) { |
| 76 |
$pluswebp_settings['quality'] = intval( $assoc_args['quality'] ); |
| 77 |
if ( 0 === $pluswebp_settings['quality'] || 100 < $pluswebp_settings['quality'] ) { |
| 78 |
WP_CLI::error( __( 'optional argument quality(int) : Invalid value.', 'plus-webp' ) ); |
| 79 |
} |
| 80 |
} |
| 81 |
if ( array_key_exists( 'types', $assoc_args ) ) { |
| 82 |
$mime_types = array( |
| 83 |
'image/jpeg', |
| 84 |
'image/png', |
| 85 |
'image/bmp', |
| 86 |
'image/gif', |
| 87 |
); |
| 88 |
$types_str = strtolower( sanitize_text_field( wp_unslash( $assoc_args['types'] ) ) ); |
| 89 |
$types = explode( ',', $types_str ); |
| 90 |
$find = false; |
| 91 |
foreach ( $types as $value ) { |
| 92 |
if ( in_array( $value, $mime_types ) ) { |
| 93 |
$find = true; |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
if ( $find ) { |
| 98 |
$pluswebp_settings['types'] = $types; |
| 99 |
} else { |
| 100 |
WP_CLI::error( __( 'optional argument types(string) : Invalid value.', 'plus-webp' ) ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return $pluswebp_settings; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Plus WebP or AVIF command |
| 109 |
* |
| 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 |
* @param array $args arguments. |
| 146 |
* @param array $assoc_args optional arguments. |
| 147 |
* @since 1.00 |
| 148 |
*/ |
| 149 |
public function pluswebpavif_cli_command( $args, $assoc_args ) { |
| 150 |
|
| 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 |
$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"; |
| 188 |
|
| 189 |
if ( 'webp' === $output || 'avif' === $output ) { |
| 190 |
|
| 191 |
$pid = 0; |
| 192 |
if ( array_key_exists( 'pid', $assoc_args ) ) { |
| 193 |
$pid = intval( $assoc_args['pid'] ); |
| 194 |
} |
| 195 |
|
| 196 |
$pluswebp_settings_default = get_option( 'pluswebp' ); |
| 197 |
update_option( 'pluswebp_default', $pluswebp_settings_default ); |
| 198 |
$pluswebp_settings = $this->settings_change( $output, $replace, $addext, $assoc_args, $pluswebp_settings_default ); |
| 199 |
update_option( 'pluswebp', $pluswebp_settings ); |
| 200 |
|
| 201 |
if ( 0 < $pid ) { |
| 202 |
$metadata_org = wp_get_attachment_metadata( $pid ); |
| 203 |
delete_option( 'pluswebp_generate' ); |
| 204 |
do_action( 'wp_generate_attachment_metadata', $metadata_org, $pid ); |
| 205 |
$webp_id = get_option( 'pluswebp_generate' ); |
| 206 |
delete_option( 'pluswebp_generate' ); |
| 207 |
delete_option( 'pluswebp_default' ); |
| 208 |
update_option( 'pluswebp', $pluswebp_settings_default ); |
| 209 |
if ( $webp_id ) { |
| 210 |
$messages = array(); |
| 211 |
list( $message, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id ); |
| 212 |
WP_CLI::success( $message ); |
| 213 |
if ( $mail ) { |
| 214 |
do_action( 'pluswebp_mail_generate_message', $messages, 1 ); |
| 215 |
} |
| 216 |
} else { |
| 217 |
$message = 'ID: ' . $pid . "\n"; |
| 218 |
$message .= __( 'This media ID has already been converted or is neither an image nor media.', 'plus-webp' ) . "\n"; |
| 219 |
$message .= "\n"; |
| 220 |
WP_CLI::warning( $message ); |
| 221 |
} |
| 222 |
} else { |
| 223 |
list( $post_ids, $no_file_ids ) = apply_filters( 'pluswebp_get_allimages', $pluswebp_settings['types'], $pluswebp_settings['output_mime'] ); |
| 224 |
if ( ! empty( $post_ids ) ) { |
| 225 |
$messages = array(); |
| 226 |
foreach ( $post_ids as $post_id ) { |
| 227 |
$metadata_org = wp_get_attachment_metadata( $post_id ); |
| 228 |
|
| 229 |
delete_option( 'pluswebp_generate' ); |
| 230 |
do_action( 'wp_generate_attachment_metadata', $metadata_org, $post_id ); |
| 231 |
$webp_id = get_option( 'pluswebp_generate' ); |
| 232 |
delete_option( 'pluswebp_generate' ); |
| 233 |
if ( $webp_id ) { |
| 234 |
list( $message, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id ); |
| 235 |
WP_CLI::success( $message ); |
| 236 |
} |
| 237 |
} |
| 238 |
delete_option( 'pluswebp_default' ); |
| 239 |
update_option( 'pluswebp', $pluswebp_settings_default ); |
| 240 |
if ( ! empty( $no_file_ids ) ) { |
| 241 |
foreach ( $no_file_ids as $post_id ) { |
| 242 |
$message = 'ID: ' . $post_id . "\n"; |
| 243 |
$message .= __( 'Title', 'plus-webp' ) . ': ' . get_the_title( $post_id ) . "\n"; |
| 244 |
$message .= __( 'This media exists in the database, but the file does not, so the conversion was not performed.', 'plus-webp' ) . "\n"; |
| 245 |
$message .= "\n"; |
| 246 |
WP_CLI::warning( $message ); |
| 247 |
$messages[] = $message; |
| 248 |
} |
| 249 |
} |
| 250 |
if ( $mail ) { |
| 251 |
do_action( 'pluswebp_mail_generate_message', $messages, count( $post_ids ) ); |
| 252 |
} |
| 253 |
} else { |
| 254 |
$message = __( 'No media to convert.', 'plus-webp' ) . "\n"; |
| 255 |
$message .= "\n"; |
| 256 |
WP_CLI::warning( $message ); |
| 257 |
} |
| 258 |
} |
| 259 |
} else { |
| 260 |
WP_CLI::error( $input_error_message ); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|