# plus-webp/4.11/lib/pluswebp-cli.php

Plus WebP or AVIF, version 4.11. 110 lines.

- Page: https://pluginprobe.com/plugins/plus-webp/4.11/code/lib/pluswebp-cli.php
- Raw: https://pluginprobe.com/plugins/plus-webp/4.11/raw/lib/pluswebp-cli.php
- Modified: 2024-08-17T00:41:02+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/plus-webp/4.11/code/lib/pluswebp-cli.php#L10-L20`.

```php
<?php
/**
 * Cli Name:    Plus WebP CLI
 * Description: Generate WebP by WP-CLI.
 * Version:     1.03
 * Author:      Katsushi Kawamori
 * Author URI:  https://riverforest-wp.info/
 * License:     GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 * @package pluswebp_cli
 */

/*
	Copyright (c) 2024- Katsushi Kawamori (email : dodesyoswift312@gmail.com)
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; version 2 of the License.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/** ==================================================
 * Plus WebP command
 *
 * @param array $args  arguments.
 * @param array $assoc_args  optional arguments.
 * @since 1.00
 */
function pluswebp_cli_command( $args, $assoc_args ) {

	$input_error_message = __( 'Please enter the arguments.', 'plus-webp' ) . "\n";
	$input_error_message .= __( '1st argument(string) : mail -> Send results via email, nomail -> Do not send results by email', 'plus-webp' ) . "\n";
	$input_error_message .= __( 'optional argument(int) : --pid=12152 : Media ID(Conversion source ID) -> Process only specified Media ID.', 'plus-webp' ) . "\n";
	if ( is_array( $args ) && ! empty( $args ) &&
			( 'mail' === $args[0] || 'nomail' === $args[0] ) ) {

		$pid = 0;
		if ( array_key_exists( 'pid', $assoc_args ) ) {
			$pid = intval( $assoc_args['pid'] );
		}
		if ( 0 < $pid ) {
			$metadata_org = wp_get_attachment_metadata( $pid );
			delete_option( 'pluswebp_generate' );
			do_action( 'wp_generate_attachment_metadata', $metadata_org, $pid );
			$webp_id = get_option( 'pluswebp_generate' );
			delete_option( 'pluswebp_generate' );
			if ( $webp_id ) {
				$messages = array();
				list( $message, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id );
				WP_CLI::success( $message );
				if ( 'mail' === $args[0] ) {
					do_action( 'pluswebp_mail_generate_message', $messages, 1 );
				}
			} else {
				$message = 'ID: ' . $pid . "\n";
				$message .= __( 'This media ID has already been converted or is neither an image nor a madia.', 'plus-webp' ) . "\n";
				$message .= "\n";
				WP_CLI::warning( $message );
			}
		} else {
			$pluswebp_settings = get_option( 'pluswebp' );
			list( $post_ids, $no_file_ids ) = apply_filters( 'pluswebp_get_allimages', $pluswebp_settings['types'] );
			if ( ! empty( $post_ids ) ) {
				$messages = array();
				foreach ( $post_ids as $post_id ) {
					$metadata_org = wp_get_attachment_metadata( $post_id );

					delete_option( 'pluswebp_generate' );
					do_action( 'wp_generate_attachment_metadata', $metadata_org, $post_id );
					$webp_id = get_option( 'pluswebp_generate' );
					delete_option( 'pluswebp_generate' );

					if ( $webp_id ) {
						list( $message, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id );
						WP_CLI::success( $message );
					}
				}
				if ( ! empty( $no_file_ids ) ) {
					foreach ( $no_file_ids as $post_id ) {
						$message = 'ID: ' . $post_id . "\n";
						$message .= __( 'Title' ) . ': ' . get_the_title( $post_id ) . "\n";
						$message .= __( 'This media exists in the database, but the file does not, so the conversion was not performed.', 'plus-webp' ) . "\n";
						$message .= "\n";
						WP_CLI::warning( $message );
						$messages[] = $message;
					}
				}
				if ( 'mail' === $args[0] ) {
					do_action( 'pluswebp_mail_generate_message', $messages, count( $post_ids ) );
				}
			} else {
				$message = __( 'No media to convert.', 'plus-webp' ) . "\n";
				$message .= "\n";
				WP_CLI::warning( $message );
			}
		}
	} else {
		WP_CLI::error( $input_error_message );
	}
}
WP_CLI::add_command( 'pluswebp_cli', 'pluswebp_cli_command' );

```
