| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cli Name: Plus WebP CLI |
| 4 |
* Description: Generate WebP by WP-CLI. |
| 5 |
* Version: 1.03 |
| 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 pluswebp_cli |
| 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 |
/** ================================================== |
| 31 |
* Plus WebP command |
| 32 |
* |
| 33 |
* @param array $args arguments. |
| 34 |
* @param array $assoc_args optional arguments. |
| 35 |
* @since 1.00 |
| 36 |
*/ |
| 37 |
function pluswebp_cli_command( $args, $assoc_args ) { |
| 38 |
|
| 39 |
$input_error_message = __( 'Please enter the arguments.', 'plus-webp' ) . "\n"; |
| 40 |
$input_error_message .= __( '1st argument(string) : mail -> Send results via email, nomail -> Do not send results by email', 'plus-webp' ) . "\n"; |
| 41 |
$input_error_message .= __( 'optional argument(int) : --pid=12152 : Media ID(Conversion source ID) -> Process only specified Media ID.', 'plus-webp' ) . "\n"; |
| 42 |
if ( is_array( $args ) && ! empty( $args ) && |
| 43 |
( 'mail' === $args[0] || 'nomail' === $args[0] ) ) { |
| 44 |
|
| 45 |
$pid = 0; |
| 46 |
if ( array_key_exists( 'pid', $assoc_args ) ) { |
| 47 |
$pid = intval( $assoc_args['pid'] ); |
| 48 |
} |
| 49 |
if ( 0 < $pid ) { |
| 50 |
$metadata_org = wp_get_attachment_metadata( $pid ); |
| 51 |
delete_option( 'pluswebp_generate' ); |
| 52 |
do_action( 'wp_generate_attachment_metadata', $metadata_org, $pid ); |
| 53 |
$webp_id = get_option( 'pluswebp_generate' ); |
| 54 |
delete_option( 'pluswebp_generate' ); |
| 55 |
if ( $webp_id ) { |
| 56 |
$messages = array(); |
| 57 |
list( $message, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id ); |
| 58 |
WP_CLI::success( $message ); |
| 59 |
if ( 'mail' === $args[0] ) { |
| 60 |
do_action( 'pluswebp_mail_generate_message', $messages, 1 ); |
| 61 |
} |
| 62 |
} else { |
| 63 |
$message = 'ID: ' . $pid . "\n"; |
| 64 |
$message .= __( 'This media ID has already been converted or is neither an image nor a madia.', 'plus-webp' ) . "\n"; |
| 65 |
$message .= "\n"; |
| 66 |
WP_CLI::warning( $message ); |
| 67 |
} |
| 68 |
} else { |
| 69 |
$pluswebp_settings = get_option( 'pluswebp' ); |
| 70 |
list( $post_ids, $no_file_ids ) = apply_filters( 'pluswebp_get_allimages', $pluswebp_settings['types'] ); |
| 71 |
if ( ! empty( $post_ids ) ) { |
| 72 |
$messages = array(); |
| 73 |
foreach ( $post_ids as $post_id ) { |
| 74 |
$metadata_org = wp_get_attachment_metadata( $post_id ); |
| 75 |
|
| 76 |
delete_option( 'pluswebp_generate' ); |
| 77 |
do_action( 'wp_generate_attachment_metadata', $metadata_org, $post_id ); |
| 78 |
$webp_id = get_option( 'pluswebp_generate' ); |
| 79 |
delete_option( 'pluswebp_generate' ); |
| 80 |
|
| 81 |
if ( $webp_id ) { |
| 82 |
list( $message, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id ); |
| 83 |
WP_CLI::success( $message ); |
| 84 |
} |
| 85 |
} |
| 86 |
if ( ! empty( $no_file_ids ) ) { |
| 87 |
foreach ( $no_file_ids as $post_id ) { |
| 88 |
$message = 'ID: ' . $post_id . "\n"; |
| 89 |
$message .= __( 'Title' ) . ': ' . get_the_title( $post_id ) . "\n"; |
| 90 |
$message .= __( 'This media exists in the database, but the file does not, so the conversion was not performed.', 'plus-webp' ) . "\n"; |
| 91 |
$message .= "\n"; |
| 92 |
WP_CLI::warning( $message ); |
| 93 |
$messages[] = $message; |
| 94 |
} |
| 95 |
} |
| 96 |
if ( 'mail' === $args[0] ) { |
| 97 |
do_action( 'pluswebp_mail_generate_message', $messages, count( $post_ids ) ); |
| 98 |
} |
| 99 |
} else { |
| 100 |
$message = __( 'No media to convert.', 'plus-webp' ) . "\n"; |
| 101 |
$message .= "\n"; |
| 102 |
WP_CLI::warning( $message ); |
| 103 |
} |
| 104 |
} |
| 105 |
} else { |
| 106 |
WP_CLI::error( $input_error_message ); |
| 107 |
} |
| 108 |
} |
| 109 |
WP_CLI::add_command( 'pluswebp_cli', 'pluswebp_cli_command' ); |
| 110 |
|