dvadf
File manager - Edit - /home/centroca/public_html/bulk.tar
Back
class-smush-background-task.php 0000644 00000005570 15252526627 0012617 0 ustar 00 <?php namespace Smush\Core\Bulk; class Smush_Background_Task implements \Serializable, \JsonSerializable { private static $task_type_smush = 'SMUSH'; private static $task_type_resmush = 'RESMUSH'; private static $task_type_error = 'ERROR'; private $type; private $image_id; public function __construct( $type, $image_id ) { $this->type = $type; $this->image_id = $image_id; } public function is_valid() { return $this->is_type_valid( $this->type ) && $this->is_image_id_valid( $this->image_id ); } private function is_type_valid( $type ) { $valid_types = array( self::$task_type_smush, self::$task_type_resmush ); return in_array( $type, $valid_types ); } private function is_image_id_valid( $image_id ) { return intval( $image_id ) > 0; } /** * @return mixed */ public function get_type() { return $this->type; } /** * @param mixed $type */ public function set_type( $type ) { $this->type = $type; } /** * @return mixed */ public function get_image_id() { return $this->image_id; } /** * @param mixed $image_id */ public function set_image_id( $image_id ) { $this->image_id = $image_id; } private static function get( $array, $key ) { return empty( $array[ $key ] ) ? null : $array[ $key ]; } public function serialize() { return json_encode( $this->__serialize() ); } public function unserialize( $data ) { $this->__unserialize( json_decode( $data, true ) ); } public function __unserialize( $data ) { $type = self::get( $data, 'type' ); $type = $this->is_type_valid( $type ) ? $type : ''; $this->set_type( $type ); $image_id = self::get( $data, 'image_id' ); $image_id = $this->is_image_id_valid( $image_id ) ? $image_id : 0; $this->set_image_id( $image_id ); } public function __serialize() { return array( 'type' => $this->type, 'image_id' => $this->image_id, ); } public function __toString() { return json_encode( $this->__serialize() ); } /** * Get task_type_error. * * @return string */ public static function get_task_type_error() { return self::$task_type_error; } /** * Get task_type_resmush. * * @return string */ public static function get_task_type_resmush() { return self::$task_type_resmush; } /** * Get task_type_smush. * * @return string */ public static function get_task_type_smush() { return self::$task_type_smush; } /** * @smush-keep-signature * * @return mixed */ public function jsonSerialize(): mixed { return $this->__serialize(); } /** * Create instance from JSON string or decoded array. * * @param string|array $data JSON string or associative array. * * @return self */ public static function from_json( $data ) { if ( is_string( $data ) ) { $data = json_decode( $data, true ); } $instance = new self( '', 0 ); $instance->__unserialize( (array) $data ); return $instance; } } class-background-process-manager.php 0000644 00000006122 15252526627 0013600 0 ustar 00 <?php namespace Smush\Core\Bulk; use Smush\Core\Background\Mutex; class Background_Process_Manager { private static $active_processes_expiration = DAY_IN_SECONDS; private static $active_processes_key = 'wp_smush_bulk_smush_active_processes'; private static $max_tasks_per_request = 8; private $is_multisite; private $current_site_id; public function __construct( $is_multisite, $current_site_id ) { $this->is_multisite = $is_multisite; $this->current_site_id = $current_site_id; } public function create_process() { $identifier = $this->make_process_identifier(); $background_process = new Bulk_Smush_Background_Process( $identifier ); $tasks_per_request = $this->calculate_tasks_per_request(); if ( $tasks_per_request ) { $background_process->set_tasks_per_request( $tasks_per_request ); } $this->register( $identifier ); return $background_process; } public function register( $identifier ) { $register = function ( $identifier ) { $this->register_active_process( $identifier ); }; $unregister = function ( $identifier ) { $this->unregister_process( $identifier ); }; add_action( "{$identifier}_started", $register ); add_action( "{$identifier}_completed", $unregister ); add_action( "{$identifier}_cancelled", $unregister ); } private function make_process_identifier() { $identifier = 'wp_smush_bulk_smush_background_process'; if ( $this->is_multisite ) { $post_fix = "_" . $this->current_site_id; $identifier .= $post_fix; } return $identifier; } private function get_active_processes() { $active_processes = get_site_transient( self::$active_processes_key ); return empty( $active_processes ) || ! is_array( $active_processes ) ? array() : $active_processes; } private function mutex( $operation ) { $mutex = new Mutex( self::$active_processes_key ); $mutex->execute( $operation ); } private function register_active_process( $identifier ) { $this->mutex( function () use ( $identifier ) { $active_processes = $this->get_active_processes(); $active_processes[ $identifier ] = $identifier; $this->set_active_processes( $active_processes ); } ); } private function unregister_process( $identifier ) { $this->mutex( function () use ( $identifier ) { $active_processes = $this->get_active_processes(); unset( $active_processes[ $identifier ] ); $this->set_active_processes( $active_processes ); } ); } private function set_active_processes( $active_processes ) { set_site_transient( self::$active_processes_key, array_unique( $active_processes ), self::$active_processes_expiration ); } private function calculate_tasks_per_request() { $active_processes_count = count( $this->get_active_processes() ); $should_limit = $this->is_multisite && $active_processes_count > 1; if ( ! $should_limit ) { return false; } // Divide the available slots between the active processes $tasks_per_request = intval( floor( self::$max_tasks_per_request / $active_processes_count ) ); // At least 1 task per request return max( $tasks_per_request, 1 ); } } class-bulk-smush-background-process.php 0000644 00000006376 15252526627 0014273 0 ustar 00 <?php namespace Smush\Core\Bulk; use Smush\Core\Background\Background_Process; use Smush\Core\Helper; class Bulk_Smush_Background_Process extends Background_Process { /** * Retrival limit per 1000. * * @var int */ private static $revival_limit_unit = 5; /** * @var Bulk_Optimize */ private $bulk_optimize; public function __construct( $identifier ) { parent::__construct( $identifier ); $this->set_logger( Helper::logger() ); $this->bulk_optimize = new Bulk_Optimize(); } public function start( $tasks ) { parent::start( $tasks ); $this->bulk_optimize->start_bulk_optimization(); } /** * @param $task Smush_Background_Task * * @return boolean */ protected function task( $task ) { if ( is_array( $task ) ) { $task = Smush_Background_Task::from_json( $task ); } if ( ! is_a( $task, Smush_Background_Task::class ) || ! $task->is_valid() ) { Helper::logger()->error( 'An invalid background task was encountered.' ); return false; } $result = $this->bulk_optimize->optimize_attachment( $task->get_image_id() ); return ! is_wp_error( $result ); } /** * Email when bulk smush complete. */ protected function complete() { parent::complete(); // Send email. if ( $this->get_status()->get_total_items() ) { $mail = new Mail( 'wp_smush_background' ); if ( $mail->reporting_email_enabled() ) { if ( $mail->send_email() ) { Helper::logger()->notice( sprintf( 'Bulk Smush completed for %s, and sent a summary email to %s at %s.', get_site_url(), join( ',', $mail->get_mail_recipients() ), wp_date( 'd/m/y H:i:s' ) ) ); } else { Helper::logger()->error( sprintf( 'Bulk Smush completed for %s, but could not send a summary email to %s at %s.', get_site_url(), join( ',', $mail->get_mail_recipients() ), wp_date( 'd/m/y H:i:s' ) ) ); } } else { Helper::logger()->info( sprintf( 'Bulk Smush completed for %s, and reporting email is disabled.', get_site_url() ) ); } } $this->bulk_optimize->complete_bulk_optimization(); } protected function get_revival_limit() { $constant_value = $this->get_revival_limit_constant(); if ( $constant_value ) { return $constant_value; } $revival_limit = $this->calculate_default_revival_limit(); return apply_filters( $this->identifier . '_revival_limit', $revival_limit ); } private function get_revival_limit_constant() { if ( ! defined( 'WP_SMUSH_BULK_REVIVAL_LIMIT' ) ) { return 0; } $constant_value = (int) WP_SMUSH_BULK_REVIVAL_LIMIT; return max( $constant_value, 0 ); } private function calculate_default_revival_limit() { $total_items = $this->get_status()->get_total_items(); $default_revival_limit = (int) ceil( $total_items / 1000 ) * self::$revival_limit_unit; return max( $default_revival_limit, 5 ); } protected function mark_as_dead() { do_action( 'wp_smush_bulk_smush_dead', $this ); return parent::mark_as_dead(); } protected function get_instance_expiry_duration_seconds() { $expire_duration = 0; if ( defined( 'WP_SMUSH_BULK_SMUSH_EXPIRE_DURATION' ) ) { $expire_duration = (int) WP_SMUSH_BULK_SMUSH_EXPIRE_DURATION; } return $expire_duration > 0 ? $expire_duration : MINUTE_IN_SECONDS * 3; } } class-background-bulk-smush-controller.php 0000644 00000030540 15252526627 0014766 0 ustar 00 <?php namespace Smush\Core\Bulk; use Smush\Core\Helper; use Smush\Core\Background\Process_Status_DTO; use Smush\Core\Server_Utils; use Smush\Core\Stats\Global_Stats; use Smush\Core\Media_Library\Media_Library_Last_Process; use Smush\Core\Media_Library\Background_Media_Library_Scanner; use Smush\Core\Membership\Membership; use WP_Smush; class Background_Bulk_Smush_Controller { private static $required_mysql_version = '5.6'; /** * @var Bulk_Smush_Background_Process */ private $background_process; private $mail; private $logger; private $global_stats; private $server_utils; private $membership; /** * Static instance * * @var self */ private static $instance; public static function get_instance() { if ( empty( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } public function __construct() { $process_manager = new Background_Process_Manager( is_multisite(), get_current_blog_id() ); $this->background_process = $process_manager->create_process(); $this->mail = new Mail( 'wp_smush_background' ); $this->logger = Helper::logger(); $this->global_stats = Global_Stats::get(); $this->server_utils = new Server_Utils(); $this->membership = Membership::get_instance(); if ( ! $this->should_use_background() ) { return; } $this->register_ajax_handler( 'bulk_smush_start', array( $this, 'bulk_smush_start' ) ); $this->register_ajax_handler( 'bulk_smush_cancel', array( $this, 'bulk_smush_cancel' ) ); $this->register_ajax_handler( 'bulk_smush_pause', array( $this, 'bulk_smush_pause' ) ); $this->register_ajax_handler( 'bulk_smush_resume', array( $this, 'bulk_smush_resume' ) ); $this->register_ajax_handler( 'bulk_smush_get_status', array( $this, 'bulk_smush_get_status' ) ); $this->register_ajax_handler( 'bulk_smush_reset_status', array( $this, 'bulk_smush_reset_status' ) ); $background_scan = Background_Media_Library_Scanner::get_instance(); $scan_identifier = $background_scan->get_background_process()->get_identifier(); add_action( "{$scan_identifier}_completed", array( $this, 'on_scan_completed' ), 20 ); add_filter( 'wp_smush_frontend_poll_data', array( $this, 'add_bulk_smush_progress_to_poll' ) ); add_filter( 'wp_smush_localize_ui_script_data', array( $this, 'localize_background_stats' ), 10, 2 ); add_action( 'init', array( $this, 'cancel_programmatically' ) ); } public function __call( $method_name, $arguments ) { _deprecated_function( esc_html( $method_name ), '4.2.0' ); } public function get_background_process() { return $this->background_process; } public function cancel_programmatically() { $background_disabled = ! $this->is_background_enabled(); $constant_value = defined( 'WP_SMUSH_STOP_BACKGROUND_PROCESSING' ) && WP_SMUSH_STOP_BACKGROUND_PROCESSING; $filter_value = apply_filters( 'wp_smush_stop_background_processing', false ); $capability = is_multisite() ? 'manage_network' : 'manage_options'; $param_value = ! empty( $_GET['wp_smush_stop_background_processing'] ) && current_user_can( $capability ); $should_cancel = $background_disabled || $constant_value || $filter_value || $param_value; $status = $this->background_process->get_status(); if ( $should_cancel && $status->is_in_processing() && ! $status->is_cancelled() ) { $this->logger->notice( 'Cancelling background processing because a constant/query param/filter indicated that the process needs to be stopped.' ); $this->background_process->cancel(); } } public function bulk_smush_start() { $this->check_ajax_referrer(); if ( $this->membership->is_api_hub_access_required() ) { wp_send_json_error( array( 'error' => 'hub_access_required', 'error_message' => esc_html__( 'A WPMU DEV Hub connection is required to optimize images.', 'wp-smushit' ), ), 403 ); } if ( $this->global_stats->is_outdated() ) { wp_send_json_error( array( 'error' => 'is_outdated', 'error_message' => esc_html__( 'You need to run a scan before bulk Smush can be started.', 'wp-smushit' ), ), 409 ); } $process = $this->background_process; $in_processing = $process->get_status()->is_in_processing(); if ( $in_processing ) { // Already in progress wp_send_json_error(); } $tasks = $this->prepare_background_tasks(); if ( $tasks ) { $process->start( $tasks ); wp_send_json_success( $process->get_status()->to_array() ); } wp_send_json_error(); } public function bulk_smush_cancel() { $this->check_ajax_referrer(); if ( ! $this->background_process->get_status()->is_cancelled() ) { $this->background_process->cancel(); } wp_send_json_success(); } public function bulk_smush_pause() { $this->check_ajax_referrer(); if ( ! $this->background_process->get_status()->is_paused() ) { $this->background_process->pause(); } wp_send_json_success(); } public function bulk_smush_resume() { $this->check_ajax_referrer(); if ( $this->background_process->get_status()->is_paused() ) { $this->background_process->resume(); } wp_send_json_success(); } public function bulk_smush_get_status() { $this->check_ajax_referrer(); $is_process_stuck = Media_Library_Last_Process::get_instance()->is_process_stuck(); wp_send_json_success( array_merge( $this->background_process->get_status()->to_array(), array( 'is_process_stuck' => $is_process_stuck, 'in_process_notice' => $this->get_in_process_notice(), ) ) ); } private function check_ajax_referrer() { check_ajax_referer( 'wp-smush-ajax', '_nonce' ); // Check capability. if ( ! Helper::is_user_allowed( 'manage_options' ) ) { wp_die( esc_html__( 'Unauthorized', 'wp-smushit' ), 403 ); } } private function register_ajax_handler( $action, $handler ) { add_action( "wp_ajax_$action", $handler ); } /** * @return Smush_Background_Task[] */ private function prepare_background_tasks() { $smush_tasks = $this->prepare_smush_tasks(); $resmush_tasks = $this->prepare_resmush_tasks(); return array_merge( $smush_tasks, $resmush_tasks ); } private function prepare_smush_tasks() { $to_smush = $this->global_stats->get_optimize_list()->get_ids(); if ( empty( $to_smush ) || ! is_array( $to_smush ) ) { $to_smush = array(); } return array_map( function ( $image_id ) { return new Smush_Background_Task( Smush_Background_Task::get_task_type_smush(), $image_id ); }, $to_smush ); } private function prepare_resmush_tasks() { $to_resmush = $this->global_stats->get_redo_ids(); return array_map( function ( $image_id ) { return new Smush_Background_Task( Smush_Background_Task::get_task_type_resmush(), $image_id ); }, $to_resmush ); } private function prepare_error_tasks() { $error_items_to_retry = $this->global_stats->get_error_list()->get_ids(); return array_map( function ( $image_id ) { return new Smush_Background_Task( Smush_Background_Task::get_task_type_error(), $image_id ); }, $error_items_to_retry ); } public function localize_background_stats( $script_data, $page_slug ) { $script_data['bulkSmushStatus'] = $this->get_process_data_for_ui(); return $script_data; } /** * Whether BO is in processing or not. * * @return boolean */ public function is_in_processing() { return $this->background_process->get_status()->is_in_processing(); } /** * Whether BO is completed or not. * * @return boolean */ public function is_completed() { return $this->background_process->get_status()->is_completed(); } /** * Whether BO is dead or not. * * @return boolean */ public function is_dead() { return $this->background_process->get_status()->is_dead(); } /** * Get total items. * * @return int */ public function get_total_items() { return $this->background_process->get_status()->get_total_items(); } /** * Get processed items. * * @return int */ public function get_processed_items() { return $this->background_process->get_status()->get_processed_items(); } /** * Get failed items. * * @return int */ public function get_failed_items() { return $this->background_process->get_status()->get_failed_items(); } /** * Get revival count. * * @return int */ public function get_revival_count() { return $this->background_process->get_revival_count(); } /** * Get process id. */ public function get_process_id() { return $this->background_process->get_process_id(); } /** * Get email address of recipient. * * @return string */ public function get_mail_recipient() { $emails = $this->mail->get_mail_recipients(); return ! empty( $emails ) ? $emails[0] : get_option( 'admin_email' ); } public function get_in_process_notice() { return $this->mail->reporting_email_enabled() ? $this->get_email_enabled_notice() : $this->get_email_disabled_notice(); } private function get_email_disabled_notice() { $email_setting_link = sprintf( '<a href="#background_email-settings-row">%s</a>', esc_html__( 'Enable the email notification', 'wp-smushit' ) ); /* translators: %s: a link */ return sprintf( __( 'Feel free to close this page while Smush works its magic in the background. %s to receive an email when the process finishes.', 'wp-smushit' ), $email_setting_link ); } private function get_email_enabled_notice() { $mail_recipient = $this->get_mail_recipient(); /* translators: %s: Email address */ return sprintf( __( 'Feel free to close this page while Smush works its magic in the background. We’ll email you at <strong>%s</strong> when it’s done.', 'wp-smushit' ), $mail_recipient ); } public function is_background_enabled() { if ( ! $this->can_use_background() ) { return false; } return defined( 'WP_SMUSH_BACKGROUND' ) && WP_SMUSH_BACKGROUND; } public function should_use_background() { // TODO: [WPMUDEV SMUSH UI] Always enabled on the new UI. // Check if we should continue support ajax for background issue cases. return true; return $this->is_background_enabled() && $this->is_background_supported(); } public function is_background_supported() { return $this->is_mysql_requirement_met(); } public function can_use_background() { return true; } /** * We need the right version of MySQL for locks used by the Mutex class * * @return bool|int */ private function is_mysql_requirement_met() { return version_compare( $this->get_actual_mysql_version(), $this->get_required_mysql_version(), '>=' ); } public function get_required_mysql_version() { return self::$required_mysql_version; } public function get_actual_mysql_version() { return $this->server_utils->get_mysql_version(); } public function start_bulk_smush_direct() { if ( ! $this->should_use_background() ) { return false; } if ( $this->membership->is_api_hub_access_required() ) { return false; } $process = $this->background_process; $in_processing = $process->get_status()->is_in_processing(); if ( $in_processing ) { return $process->get_status()->to_array(); } if ( ! Helper::loopback_supported() ) { $this->logger->error( 'Loopback check failed. Not starting a new background process.' ); return false; } $tasks = $this->prepare_background_tasks(); if ( $tasks ) { $process->start( $tasks ); } return $process->get_status()->to_array(); } /** * Add bulk smush progress data to frontend poll response * * @param array $data Polling data array. * * @return array Modified polling data with bulk smush progress. */ public function add_bulk_smush_progress_to_poll( $data ) { $data['bulk-smush-progress'] = $this->get_process_data_for_ui(); if ( $this->background_process->get_status()->is_in_processing() ) { $this->background_process->maybe_do_healthcheck(); } return $data; } private function get_process_data_for_ui() { $status = $this->background_process->get_status()->to_array(); return Process_Status_DTO::to_react_props( $status ); } public function on_scan_completed() { if ( $this->should_use_background() && Background_Media_Library_Scanner::get_instance()->enabled_optimize_on_scan_completed() ) { $this->start_bulk_smush_direct(); } } public function bulk_smush_reset_status() { $this->check_ajax_referrer(); $this->background_process->get_status()->reset(); wp_send_json_success(); } } class-ajax-bulk-smush-controller.php 0000644 00000004643 15252526627 0013577 0 ustar 00 <?php namespace Smush\Core\Bulk; use Smush\Core\Controller; use Smush\Core\Error_Handler; use Smush\Core\Helper; use Smush\Core\Media\Media_Item_Cache; use Smush\Core\Membership\Membership; class Ajax_Bulk_Smush_Controller extends Controller { /** * @var Bulk_Optimize */ private $bulk_optimize; /** * @var Membership */ private $membership; private $media_item_cache; public function __construct() { $this->bulk_optimize = new Bulk_Optimize(); $this->membership = Membership::get_instance(); $this->media_item_cache = Media_Item_Cache::get_instance(); $this->register_ajax_handler( 'start_bulk_optimization', array( $this, 'start_bulk_optimization' ) ); $this->register_ajax_handler( 'bulk_optimize_attachments', array( $this, 'bulk_optimize_attachments' ) ); $this->register_ajax_handler( 'complete_bulk_optimization', array( $this, 'complete_bulk_optimization' ) ); } private function register_ajax_handler( $action, $handler ) { add_action( "wp_ajax_$action", $handler ); } public function start_bulk_optimization() { $this->bulk_optimize->start_bulk_optimization(); } public function bulk_optimize_attachments() { check_ajax_referer( 'wp-smush-ajax', '_nonce' ); if ( ! Helper::is_user_allowed( 'manage_options' ) ) { wp_send_json_error( array( 'error' => 'unauthorized', 'error_message' => esc_html__( "You don't have permission to do this.", 'wp-smushit' ), ), 403 ); } if ( empty( $_REQUEST['attachment_id'] ) ) { wp_send_json_error( array( 'error' => 'missing_id', 'continue' => false, ) ); } $attachment_id = (int) $_REQUEST['attachment_id']; $result = $this->bulk_optimize->optimize_attachment( $attachment_id ); $stats = $this->bulk_optimize->compile_stats( $attachment_id ); $show_warning = (int) $this->membership->should_show_premium_status_warning( $attachment_id ); if ( is_wp_error( $result ) && $result->has_errors() ) { $error = Error_Handler::get_error( $result, $this->media_item_cache->get( $attachment_id ) ); wp_send_json_error( array( 'stats' => $stats, 'error' => $error, 'show_warning' => $show_warning, ) ); } wp_send_json_success( array( 'stats' => $stats, 'show_warning' => $show_warning, ) ); } public function complete_bulk_optimization() { $this->bulk_optimize->complete_bulk_optimization(); } } class-mail.php 0000644 00000013536 15252526627 0007326 0 ustar 00 <?php /** * Handle mail for background process. * * @package Smush\Core\Modules\Helpers */ namespace Smush\Core\Bulk; use Smush\Core\Membership\Membership; use Smush\Core\Modules\Helpers; use Smush\Core\Settings; use WP_Smush; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Class Mail */ class Mail extends Helpers\Mail { /** * View class. * * @var Helpers\View */ private $view; private $membership; /** * Constructor. * * @param string $identifier Identifier. */ public function __construct( $identifier ) { parent::__construct( $identifier ); $this->view = new Helpers\View(); $this->view->set_template_dir( WP_SMUSH_DIR . 'app/' ); $this->membership = Membership::get_instance(); } /** * Whether to receive email or not. * * @return bool */ public function reporting_email_enabled() { return Settings::get_instance()->get( 'background_email' ); } /** * Get sender name. * * @return string */ protected function get_sender_name() { if ( $this->membership->is_pro() && $this->whitelabel->enabled() ) { $plugin_label = $this->whitelabel->get_plugin_name(); if ( empty( $plugin_label ) ) { $plugin_label = __( 'Bulk Compression', 'wp-smushit' ); } } else { $plugin_label = $this->membership->is_pro() ? __( 'Smush Pro', 'wp-smushit' ) : __( 'Smush', 'wp-smushit' ); } return $plugin_label; } /** * Get email subject. * * @return string */ protected function get_mail_subject() { $site_url = get_site_url(); $site_url = preg_replace( '#http(s)?://(www.)?#', '', $site_url ); if ( $this->whitelabel->enabled() ) { /* translators: %s: Site URL */ return sprintf( __( 'Bulk compression completed for %s', 'wp-smushit' ), esc_html( $site_url ) ); } /* translators: %s: Site URL */ return sprintf( __( 'Bulk Smush completed for %s', 'wp-smushit' ), esc_html( $site_url ) ); } /** * Get email message. * * @return string */ protected function get_mail_message() { if ( $this->whitelabel->enabled() ) { $title = __( 'Bulk Compression', 'wp-smushit' ); $temp_file_name = 'email/index-whitelabel'; } else { $title = __( 'Bulk Smush', 'wp-smushit' ); $temp_file_name = 'email/index'; } return $this->view->get_template_content( $temp_file_name, array( 'title' => $title, 'content_body' => $this->get_summary_content(), 'content_upsell' => $this->get_upsell_content(), ) ); } /** * Get the summary content of bulk smush. * * @return string */ private function get_summary_content() { $bg_optimization = WP_Smush::get_instance()->core()->mod->bg_optimization; $site_url = get_site_url(); $total_items = $bg_optimization->get_total_items(); $failed_items = $bg_optimization->get_failed_items(); if ( empty( $failed_items ) ) { $redirect_url = is_network_admin() ? network_admin_url( 'admin.php?page=smush' ) : admin_url( 'admin.php?page=smush' ); } else { $redirect_url = admin_url( 'upload.php?mode=list&attachment-filter=post_mime_type:image&m=0&smush-filter=failed_processing' ); } return $this->view->get_template_content( 'email/bulk-smush', array_merge( array( 'site_url' => $site_url, 'name' => $this->get_recipient_name(), 'total_items' => $total_items, 'failed_items' => $failed_items, 'smushed_items' => $total_items - $failed_items, 'redirect_url' => $redirect_url, ), $this->get_summary_template_args() ) ); } /** * Get extra template arguments. * * @return array */ private function get_summary_template_args() { $bg_optimization = WP_Smush::get_instance()->core()->mod->bg_optimization; $failed_items = $bg_optimization->get_failed_items(); if ( $failed_items > 0 ) { $failed_msg = __( 'The number of images unsuccessfully compressed (find out why below).', 'wp-smushit' ); } else { $failed_msg = __( 'The number of images unsuccessfully compressed.', 'wp-smushit' ); } if ( $this->whitelabel->enabled() ) { return array( /* translators: %s: Site URL */ 'mail_title' => __( 'Bulk compression completed for %s', 'wp-smushit' ), 'mail_desc' => __( 'The bulk compress you actioned has successfully completed. Here’s a quick summary of the results:', 'wp-smushit' ), 'total_title' => __( 'Total image attachments', 'wp-smushit' ), 'total_desc' => __( 'The number of images analyzed during the bulk compress.', 'wp-smushit' ), 'smushed_title' => __( 'Images compressed successfully', 'wp-smushit' ), 'smushed_desc' => __( 'The number of images successfully compressed.', 'wp-smushit' ), 'failed_title' => __( 'Images failed to compress', 'wp-smushit' ), 'failed_desc' => $failed_msg, ); } return array( /* translators: %s: Site URL */ 'mail_title' => __( 'Bulk Smush completed for %s', 'wp-smushit' ), 'mail_desc' => __( 'The bulk smush you actioned has successfully completed. Here’s a quick summary of the results:', 'wp-smushit' ), 'total_title' => __( 'Total image attachments', 'wp-smushit' ), 'total_desc' => __( 'The number of images analyzed during the bulk smush.', 'wp-smushit' ), 'smushed_title' => __( 'Images smushed successfully', 'wp-smushit' ), 'smushed_desc' => __( 'The number of images successfully compressed.', 'wp-smushit' ), 'failed_title' => __( 'Images failed to smush', 'wp-smushit' ), 'failed_desc' => $failed_msg, ); } /** * Get upsell CDN content. */ private function get_upsell_content() { if ( $this->membership->is_pro() ) { return; } $upsell_url = add_query_arg( array( 'utm_source' => 'smush', 'utm_medium' => 'plugin', 'utm_campaign' => 'smush_bulksmush_bo_email', ), 'https://wpmudev.com/project/wp-smush-pro/' ); return $this->view->get_template_content( 'email/upsell-cdn', array( 'upsell_url' => $upsell_url, ) ); } } class-bulk-optimize.php 0000644 00000004534 15252526627 0011175 0 ustar 00 <?php namespace Smush\Core\Bulk; use Smush\Core\Helper; use Smush\Core\Media\Media_Item_Cache; use Smush\Core\Media\Media_Item_Optimizer; use Smush\Core\Optimizer; use Smush\Core\Png2Jpg\Png2Jpg_Optimization; use Smush\Core\Resize\Resize_Optimization; use Smush\Core\Smush\Smush_Media_Item_Stats; use Smush\Core\Smush\Smush_Optimization; use WDEV_Logger; // TODO: [WPMUDEV SMUSH UI] does this file make sense now that we pause in background itself class Bulk_Optimize { /** * @var WDEV_Logger */ private $logger; public function __construct() { $this->logger = Helper::logger(); } public function start_bulk_optimization() { do_action( 'wp_smush_bulk_smush_start' ); } /** * @param $attachment_id * * @return true|\WP_Error */ public function optimize_attachment( $attachment_id ) { $optimizer = Optimizer::get_instance(); $optimized = $optimizer->optimize( $attachment_id ); if ( ! $optimized ) { $this->logger->error( "Error encountered while bulk Smushing attachment ID $attachment_id:" . $optimizer->get_errors()->get_error_message() ); return $optimizer->get_errors(); } do_action( 'image_smushed', $attachment_id, $this->compile_stats( $attachment_id ) ); return true; } public function complete_bulk_optimization() { do_action( 'wp_smush_bulk_smush_completed' ); } public function compile_stats( $attachment_id ) { $media_item = Media_Item_Cache::get_instance()->get( $attachment_id ); $optimizer = new Media_Item_Optimizer( $media_item ); $smush_optimization = $optimizer->get_optimization( Smush_Optimization::get_key() ); /** * @var Smush_Media_Item_Stats $smush_stats */ $smush_stats = $smush_optimization->get_stats(); $resize_optimization = $optimizer->get_optimization( Resize_Optimization::get_key() ); $png2jpg_optimization = $optimizer->get_optimization( Png2Jpg_Optimization::get_key() ); return array( 'count' => $smush_optimization->get_optimized_sizes_count(), 'size_before' => $smush_stats->get_size_before(), 'size_after' => $smush_stats->get_size_after(), 'savings_resize' => $resize_optimization ? $resize_optimization->get_stats()->get_bytes() : 0, 'savings_conversion' => $png2jpg_optimization ? $png2jpg_optimization->get_stats()->get_bytes() : 0, 'is_lossy' => $smush_stats->is_lossy(), ); } }
dvadf
dvadf
| ver. 1.4 |
Github
|
.
| PHP 7.3.33 | Generation time: 0 |
proxy
|
phpinfo
|
Settings