HEX
Server: Apache
System: Linux uyu7574470001-7d78c9ff74-xfpwm 4.19.91-21.al7.x86_64 #1 SMP Wed Sep 2 19:47:49 CST 2020 x86_64
User: ()
PHP: 7.4.16
Disabled: chmod,exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,dl,popen,pcntl_exec,socket_accept,socket_bind,socket_clear_error,socket_close,socket_connect,socket_create_listen,socket_create_pair,socket_create,socket_get_option,socket_getpeername,socket_getsockname,socket_last_error,socket_listen,socket_read,socket_recv,socket_recvfrom,socket_select,socket_send,socket_sendto,socket_set_block,socket_set_nonblock,socket_set_option,socket_shutdown,socket_strerror,socket_write,stream_socket_client,stream_socket_server,pfsockopen,disk_total_space,disk_free_space,chown,diskfreespace,getrusage,get_current_user,getmyuid,getmypid,dl,leak,listen,chgrp,link,symlink,dlopen,proc_nice,proc_get_stats,proc_terminate,shell_exec,sh2_exec,posix_getpwuid,posix_getgrgid,posix_kill,ini_restore,mkfifo,dbmopen,dbase_open,filepro,filepro_rowcount,posix_mkfifo,putenv,sleep,fsockopen
Upload Files
File: /usr/home/uyu7574470001/htdocs/wp-content/plugins/tiny-compress-images/src/class-tiny-cli.php
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2018 Tinify B.V.
*
* 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; either version 2 of the License, or (at your option)
* any later version.
*
* 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., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

class Tiny_Cli {

	/**
	 * Tinify Settings
	 *
	 * @var Tiny_Settings
	 */
	private $tiny_settings;

	public function __construct( $settings ) {
		$this->tiny_settings = $settings;
	}

	public static function register_command( $settings ) {
		$command_instance = new Tiny_Cli( $settings );
		WP_CLI::add_command( 'tiny', $command_instance );
	}

	/**
	 * Optimize will process images
	 *
	 * [--attachments=<strings>]
	 * : A comma separated list of attachment IDs to process. If omitted
	 * will optimize all uncompressed attachments
	 *
	 *
	 * ## EXAMPLES
	 *
	 *      optimize specific attachments
	 *      wp tiny optimize --attachments=532,603,705
	 *
	 *      optimize all unprocessed images
	 *      wp tiny optimize
	 *
	 *
	 * @param array $args
	 * @param array $assoc_args
	 * @return void
	 */
	public function optimize( $args, $assoc_args ) {
		$attachments = isset( $assoc_args['attachments'] ) ?
			array_map( 'trim', explode( ',', $assoc_args['attachments'] ) ) :
			array();

		if ( empty( $attachments ) ) {
			$attachments = $this->get_unoptimized_attachments();
		}

		if ( empty( $attachments ) ) {
			WP_CLI::success( 'No images found that need optimization.' );
			return;
		}

		$total = count( $attachments );
		WP_CLI::log( 'Optimizing ' . $total . ' images.' );

		$progress  = Utils\make_progress_bar( 'Optimizing images', $total );
		$optimized = 0;
		foreach ( $attachments as $attachment_id ) {
			$attachment_id = intval( $attachment_id );

			if ( ! $this->is_valid_attachment( $attachment_id ) ) {
				WP_CLI::warning( 'skipping - invalid attachment: ' . $attachment_id );
				$progress->tick();
				continue;
			}

			try {
				$result = $this->optimize_attachment( $attachment_id );
				if ( isset( $result['success'] ) && $result['success'] > 0 ) {
					++$optimized;
				}
			} catch ( Exception $e ) {
				WP_CLI::warning(
					'skipping - error: ' .
					$e->getMessage() .
					' (ID: ' .
					$attachment_id .
					')'
				);
			}

			$progress->tick();
		}

		$progress->finish();
		WP_CLI::success( 'Done! Optimized ' . $optimized . ' of ' . $total . ' images.' );
	}

	private function get_unoptimized_attachments() {
		$stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->tiny_settings );

		if ( empty( $stats['available-for-optimization'] ) ) {
			return array();
		}

		$ids = array();
		foreach ( $stats['available-for-optimization'] as $item ) {
			if ( isset( $item['ID'] ) ) {
				$ids[] = $item['ID'];
			}
		}
		return $ids;
	}

	/**
	 * Will process an attachment for optimization
	 *
	 * @return array{ success: int, failed: int }
	 */
	private function optimize_attachment( $attachment_id ) {
		$tiny_image = new Tiny_Image( $this->tiny_settings, $attachment_id );
		return $tiny_image->compress();
	}

	private function is_valid_attachment( $attachment_id ) {
		$mime_type = get_post_mime_type( $attachment_id );
		if ( ! $mime_type || strpos( $mime_type, 'image/' ) !== 0 ) {
			return false;
		}

		$supported_types = array( 'image/jpeg', 'image/png', 'image/webp' );
		if ( ! in_array( $mime_type, $supported_types, true ) ) {
			return false;
		}

		return true;
	}
}