| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cli Name: Plus WebP or AVIF CLI |
| 4 |
* Description: Generate WebP or AVIF by WP-CLI. |
| 5 |
* Version: 3.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 |
$pluswebpavifcli = new PlusWebpAVIFCLI(); |
| 31 |
|
| 32 |
/** ================================================== |
| 33 |
* Main |
| 34 |
*/ |
| 35 |
class PlusWebpAVIFCLI { |
| 36 |
|
| 37 |
/** ================================================== |
| 38 |
* Construct |
| 39 |
* |
| 40 |
* @since 1.00 |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
|
| 44 |
WP_CLI::add_command( 'pluswebpavif', array( $this, 'pluswebpavif_cli_command' ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** ================================================== |
| 48 |
* Settings change |
| 49 |
* |
| 50 |
* @param string $output output type. |
| 51 |
* @param array $assoc_args optional arguments. |
| 52 |
* @param array $pluswebp_settings settings. |
| 53 |
* @since 2.00 |
| 54 |
*/ |
| 55 |
private function settings_change( $output, $assoc_args, $pluswebp_settings ) { |
| 56 |
|
| 57 |
switch ( $output ) { |
| 58 |
case 'webp': |
| 59 |
$pluswebp_settings['output_mime'] = 'image/webp'; |
| 60 |
break; |
| 61 |
case 'avif': |
| 62 |
$pluswebp_settings['output_mime'] = 'image/avif'; |
| 63 |
break; |
| 64 |
default: |
| 65 |
$pluswebp_settings['output_mime'] = 'image/webp'; |
| 66 |
} |
| 67 |
|
| 68 |
if ( array_key_exists( 'quality', $assoc_args ) ) { |
| 69 |
$pluswebp_settings['quality'] = intval( $assoc_args['quality'] ); |
| 70 |
if ( 0 === $pluswebp_settings['quality'] || 100 < $pluswebp_settings['quality'] ) { |
| 71 |
WP_CLI::error( __( 'optional argument quality(int) : Invalid value.', 'plus-webp' ) ); |
| 72 |
} |
| 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 |
} |
| 94 |
if ( array_key_exists( 'types', $assoc_args ) ) { |
| 95 |
$mime_types = array( |
| 96 |
'image/jpeg', |
| 97 |
'image/png', |
| 98 |
'image/bmp', |
| 99 |
'image/gif', |
| 100 |
); |
| 101 |
$types_str = strtolower( sanitize_text_field( wp_unslash( $assoc_args['types'] ) ) ); |
| 102 |
$types = explode( ',', $types_str ); |
| 103 |
$find = false; |
| 104 |
foreach ( $types as $value ) { |
| 105 |
if ( in_array( $value, $mime_types ) ) { |
| 106 |
$find = true; |
| 107 |
break; |
| 108 |
} |
| 109 |
} |
| 110 |
if ( $find ) { |
| 111 |
$pluswebp_settings['types'] = $types; |
| 112 |
} else { |
| 113 |
WP_CLI::error( __( 'optional argument types(string) : Invalid value.', 'plus-webp' ) ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return $pluswebp_settings; |
| 118 |
} |
| 119 |
|
| 120 |
/** ================================================== |
| 121 |
* Plus WebP or AVIF command |
| 122 |
* |
| 123 |
* @param array $args arguments. |
| 124 |
* @param array $assoc_args optional arguments. |
| 125 |
* @since 1.00 |
| 126 |
*/ |
| 127 |
public function pluswebpavif_cli_command( $args, $assoc_args ) { |
| 128 |
|
| 129 |
$input_error_message = __( 'Please enter the arguments.', '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] ) ) { |
| 139 |
|
| 140 |
$output = $args[0]; |
| 141 |
|
| 142 |
$pid = 0; |
| 143 |
if ( array_key_exists( 'pid', $assoc_args ) ) { |
| 144 |
$pid = intval( $assoc_args['pid'] ); |
| 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 |
} |
| 157 |
|
| 158 |
$pluswebp_settings_default = get_option( 'pluswebp' ); |
| 159 |
update_option( 'pluswebp_default', $pluswebp_settings_default ); |
| 160 |
$pluswebp_settings = $this->settings_change( $output, $assoc_args, $pluswebp_settings_default ); |
| 161 |
update_option( 'pluswebp', $pluswebp_settings ); |
| 162 |
|
| 163 |
if ( 0 < $pid ) { |
| 164 |
$metadata_org = wp_get_attachment_metadata( $pid ); |
| 165 |
delete_option( 'pluswebp_generate' ); |
| 166 |
do_action( 'wp_generate_attachment_metadata', $metadata_org, $pid ); |
| 167 |
$webp_id = get_option( 'pluswebp_generate' ); |
| 168 |
delete_option( 'pluswebp_generate' ); |
| 169 |
delete_option( 'pluswebp_default' ); |
| 170 |
update_option( 'pluswebp', $pluswebp_settings_default ); |
| 171 |
if ( $webp_id ) { |
| 172 |
$messages = array(); |
| 173 |
list( $message, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id ); |
| 174 |
WP_CLI::success( $message ); |
| 175 |
if ( $mail ) { |
| 176 |
do_action( 'pluswebp_mail_generate_message', $messages, 1 ); |
| 177 |
} |
| 178 |
} else { |
| 179 |
$message = 'ID: ' . $pid . "\n"; |
| 180 |
$message .= __( 'This media ID has already been converted or is neither an image nor a madia.', 'plus-webp' ) . "\n"; |
| 181 |
$message .= "\n"; |
| 182 |
WP_CLI::warning( $message ); |
| 183 |
} |
| 184 |
} else { |
| 185 |
list( $post_ids, $no_file_ids ) = apply_filters( 'pluswebp_get_allimages', $pluswebp_settings['types'], $pluswebp_settings['output_mime'] ); |
| 186 |
if ( ! empty( $post_ids ) ) { |
| 187 |
$messages = array(); |
| 188 |
foreach ( $post_ids as $post_id ) { |
| 189 |
$metadata_org = wp_get_attachment_metadata( $post_id ); |
| 190 |
|
| 191 |
delete_option( 'pluswebp_generate' ); |
| 192 |
do_action( 'wp_generate_attachment_metadata', $metadata_org, $post_id ); |
| 193 |
$webp_id = get_option( 'pluswebp_generate' ); |
| 194 |
delete_option( 'pluswebp_generate' ); |
| 195 |
if ( $webp_id ) { |
| 196 |
list( $message, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id ); |
| 197 |
WP_CLI::success( $message ); |
| 198 |
} |
| 199 |
} |
| 200 |
delete_option( 'pluswebp_default' ); |
| 201 |
update_option( 'pluswebp', $pluswebp_settings_default ); |
| 202 |
if ( ! empty( $no_file_ids ) ) { |
| 203 |
foreach ( $no_file_ids as $post_id ) { |
| 204 |
$message = 'ID: ' . $post_id . "\n"; |
| 205 |
$message .= __( 'Title', 'plus-webp' ) . ': ' . get_the_title( $post_id ) . "\n"; |
| 206 |
$message .= __( 'This media exists in the database, but the file does not, so the conversion was not performed.', 'plus-webp' ) . "\n"; |
| 207 |
$message .= "\n"; |
| 208 |
WP_CLI::warning( $message ); |
| 209 |
$messages[] = $message; |
| 210 |
} |
| 211 |
} |
| 212 |
if ( $mail ) { |
| 213 |
do_action( 'pluswebp_mail_generate_message', $messages, count( $post_ids ) ); |
| 214 |
} |
| 215 |
} else { |
| 216 |
$message = __( 'No media to convert.', 'plus-webp' ) . "\n"; |
| 217 |
$message .= "\n"; |
| 218 |
WP_CLI::warning( $message ); |
| 219 |
} |
| 220 |
} |
| 221 |
} else { |
| 222 |
WP_CLI::error( $input_error_message ); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|