* The class to optimize image.
use WpOrg\Requests\Autoload;
use WpOrg\Requests\Requests;
defined( 'WPINC' ) || exit();
* Handles image optimization operations with QUIC.cloud service.
class Img_Optm extends Base {
const CLOUD_ACTION_NEW_REQ = 'new_req';
const CLOUD_ACTION_TAKEN = 'taken';
const CLOUD_ACTION_REQUEST_DESTROY = 'imgoptm_destroy';
const CLOUD_ACTION_CLEAN = 'clean';
const TYPE_NEW_REQ = 'new_req';
const TYPE_RESCAN = 'rescan';
const TYPE_DESTROY = 'destroy';
const TYPE_RESET_COUNTER = 'reset_counter';
const TYPE_CLEAN = 'clean';
const TYPE_PULL = 'pull';
const TYPE_BATCH_SWITCH_ORI = 'batch_switch_ori';
const TYPE_BATCH_SWITCH_OPTM = 'batch_switch_optm';
const TYPE_CALC_BKUP = 'calc_bkup';
const TYPE_RESET_ROW = 'reset_row';
const TYPE_RM_BKUP = 'rm_bkup';
const STATUS_NEW = 0; // 'new';
const STATUS_RAW = 1; // 'raw';
const STATUS_REQUESTED = 3; // 'requested';
const STATUS_NOTIFIED = 6; // 'notified';
const STATUS_DUPLICATED = 8; // 'duplicated';
const STATUS_PULLED = 9; // 'pulled';
const STATUS_FAILED = -1; // 'failed';
const STATUS_MISS = -3; // 'miss';
const STATUS_ERR_FETCH = -5; // 'err_fetch';
const STATUS_ERR_404 = -6; // 'err_404';
const STATUS_ERR_OPTM = -7; // 'err_optm';
const STATUS_XMETA = -8; // 'xmeta';
const STATUS_ERR = -9; // 'err';
const DB_SIZE = 'litespeed-optimize-size';
const DB_SET = 'litespeed-optimize-set';
const DB_NEED_PULL = 'need_pull';
* WordPress upload directory info.
* Temporary post ID for current operation.
* Temporary type for current operation.
* Temporary path for current operation.
* Images queued for optimization.
private $_img_in_queue = [];
* List of existing source files.
private $_existed_src_list = [];
private $_thumbnail_set = '';
* Image optimization table name.
private $_table_img_optm;
* Image optimization working table name.
private $_table_img_optming;
* Flag indicating if cron has run.
private $_cron_ran = false;
* Sizes to skip during optimization.
private $_sizes_skipped = [];
* Output format (webp/avif).
public function __construct() {
Debug2::debug2( '[ImgOptm] init' );
$this->wp_upload_dir = wp_upload_dir();
$this->__media = $this->cls( 'Media' );
$this->__data = $this->cls( 'Data' );
$this->_table_img_optm = $this->__data->tb( 'img_optm' );
$this->_table_img_optming = $this->__data->tb( 'img_optming' );
$this->_summary = self::get_summary();
if ( empty( $this->_summary['next_post_id'] ) ) {
$this->_summary['next_post_id'] = 0;
if ( $this->conf( Base::O_IMG_OPTM_WEBP ) ) {
if ( $this->conf( Base::O_IMG_OPTM_WEBP ) === 2 ) {
// Allow users to ignore custom sizes.
$this->_sizes_skipped = apply_filters( 'litespeed_imgoptm_sizes_skipped', $this->conf( Base::O_IMG_OPTM_SIZES_SKIPPED ) );
* Handle all request actions from main cls
public function handler() {
$type = Router::verify_type();
case self::TYPE_RESET_ROW:
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$id = ! empty( $_GET['id'] ) ? absint( wp_unslash( $_GET['id'] ) ) : false;
case self::TYPE_CALC_BKUP:
case self::TYPE_RESET_COUNTER:
case self::TYPE_BATCH_SWITCH_ORI:
case self::TYPE_BATCH_SWITCH_OPTM:
$this->batch_switch( $type );
case substr( $type, 0, 4 ) === 'avif':
case substr( $type, 0, 4 ) === 'webp':
case substr( $type, 0, 4 ) === 'orig':
$this->_switch_optm_file( $type );