Edit File by line
/home/zeestwma/ceyloniy.../wp-conte.../plugins/litespee.../src
File: img-optm.cls.php
<?php
[0] Fix | Delete
// phpcs:ignoreFile
[1] Fix | Delete
[2] Fix | Delete
/**
[3] Fix | Delete
* The class to optimize image.
[4] Fix | Delete
*
[5] Fix | Delete
* @since 2.0
[6] Fix | Delete
* @package LiteSpeed
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
namespace LiteSpeed;
[10] Fix | Delete
[11] Fix | Delete
use WpOrg\Requests\Autoload;
[12] Fix | Delete
use WpOrg\Requests\Requests;
[13] Fix | Delete
[14] Fix | Delete
defined('WPINC') || exit();
[15] Fix | Delete
[16] Fix | Delete
class Img_Optm extends Base {
[17] Fix | Delete
[18] Fix | Delete
const LOG_TAG = '🗜️';
[19] Fix | Delete
[20] Fix | Delete
const CLOUD_ACTION_NEW_REQ = 'new_req';
[21] Fix | Delete
const CLOUD_ACTION_TAKEN = 'taken';
[22] Fix | Delete
const CLOUD_ACTION_REQUEST_DESTROY = 'imgoptm_destroy';
[23] Fix | Delete
const CLOUD_ACTION_CLEAN = 'clean';
[24] Fix | Delete
[25] Fix | Delete
const TYPE_NEW_REQ = 'new_req';
[26] Fix | Delete
const TYPE_RESCAN = 'rescan';
[27] Fix | Delete
const TYPE_DESTROY = 'destroy';
[28] Fix | Delete
const TYPE_RESET_COUNTER = 'reset_counter';
[29] Fix | Delete
const TYPE_CLEAN = 'clean';
[30] Fix | Delete
const TYPE_PULL = 'pull';
[31] Fix | Delete
const TYPE_BATCH_SWITCH_ORI = 'batch_switch_ori';
[32] Fix | Delete
const TYPE_BATCH_SWITCH_OPTM = 'batch_switch_optm';
[33] Fix | Delete
const TYPE_CALC_BKUP = 'calc_bkup';
[34] Fix | Delete
const TYPE_RESET_ROW = 'reset_row';
[35] Fix | Delete
const TYPE_RM_BKUP = 'rm_bkup';
[36] Fix | Delete
[37] Fix | Delete
const STATUS_NEW = 0; // 'new';
[38] Fix | Delete
const STATUS_RAW = 1; // 'raw';
[39] Fix | Delete
const STATUS_REQUESTED = 3; // 'requested';
[40] Fix | Delete
const STATUS_NOTIFIED = 6; // 'notified';
[41] Fix | Delete
const STATUS_DUPLICATED = 8; // 'duplicated';
[42] Fix | Delete
const STATUS_PULLED = 9; // 'pulled';
[43] Fix | Delete
const STATUS_FAILED = -1; // 'failed';
[44] Fix | Delete
const STATUS_MISS = -3; // 'miss';
[45] Fix | Delete
const STATUS_ERR_FETCH = -5; // 'err_fetch';
[46] Fix | Delete
const STATUS_ERR_404 = -6; // 'err_404';
[47] Fix | Delete
const STATUS_ERR_OPTM = -7; // 'err_optm';
[48] Fix | Delete
const STATUS_XMETA = -8; // 'xmeta';
[49] Fix | Delete
const STATUS_ERR = -9; // 'err';
[50] Fix | Delete
const DB_SIZE = 'litespeed-optimize-size';
[51] Fix | Delete
const DB_SET = 'litespeed-optimize-set';
[52] Fix | Delete
[53] Fix | Delete
const DB_NEED_PULL = 'need_pull';
[54] Fix | Delete
[55] Fix | Delete
private $wp_upload_dir;
[56] Fix | Delete
private $tmp_pid;
[57] Fix | Delete
private $tmp_type;
[58] Fix | Delete
private $tmp_path;
[59] Fix | Delete
private $_img_in_queue = [];
[60] Fix | Delete
private $_existed_src_list = [];
[61] Fix | Delete
private $_pids_set = [];
[62] Fix | Delete
private $_thumbnail_set = '';
[63] Fix | Delete
private $_table_img_optm;
[64] Fix | Delete
private $_table_img_optming;
[65] Fix | Delete
private $_cron_ran = false;
[66] Fix | Delete
private $_sizes_skipped = [];
[67] Fix | Delete
[68] Fix | Delete
private $__media;
[69] Fix | Delete
private $__data;
[70] Fix | Delete
protected $_summary;
[71] Fix | Delete
private $_format = '';
[72] Fix | Delete
[73] Fix | Delete
/**
[74] Fix | Delete
* Init
[75] Fix | Delete
*
[76] Fix | Delete
* @since 2.0
[77] Fix | Delete
*/
[78] Fix | Delete
public function __construct() {
[79] Fix | Delete
Debug2::debug2('[ImgOptm] init');
[80] Fix | Delete
[81] Fix | Delete
$this->wp_upload_dir = wp_upload_dir();
[82] Fix | Delete
$this->__media = $this->cls('Media');
[83] Fix | Delete
$this->__data = $this->cls('Data');
[84] Fix | Delete
$this->_table_img_optm = $this->__data->tb('img_optm');
[85] Fix | Delete
$this->_table_img_optming = $this->__data->tb('img_optming');
[86] Fix | Delete
[87] Fix | Delete
$this->_summary = self::get_summary();
[88] Fix | Delete
if (empty($this->_summary['next_post_id'])) {
[89] Fix | Delete
$this->_summary['next_post_id'] = 0;
[90] Fix | Delete
}
[91] Fix | Delete
if ($this->conf(Base::O_IMG_OPTM_WEBP)) {
[92] Fix | Delete
$this->_format = 'webp';
[93] Fix | Delete
if ($this->conf(Base::O_IMG_OPTM_WEBP) == 2) {
[94] Fix | Delete
$this->_format = 'avif';
[95] Fix | Delete
}
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
// Allow users to ignore custom sizes.
[99] Fix | Delete
$this->_sizes_skipped = apply_filters( 'litespeed_imgoptm_sizes_skipped', $this->conf( Base::O_IMG_OPTM_SIZES_SKIPPED ) );
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
/**
[103] Fix | Delete
* Gather images auto when update attachment meta
[104] Fix | Delete
* This is to optimize new uploaded images first. Stored in img_optm table.
[105] Fix | Delete
* Later normal process will auto remove these records when trying to optimize these images again
[106] Fix | Delete
*
[107] Fix | Delete
* @since 4.0
[108] Fix | Delete
*/
[109] Fix | Delete
public function wp_update_attachment_metadata( $meta_value, $post_id ) {
[110] Fix | Delete
global $wpdb;
[111] Fix | Delete
[112] Fix | Delete
self::debug2('🖌️ Auto update attachment meta [id] ' . $post_id);
[113] Fix | Delete
if (empty($meta_value['file'])) {
[114] Fix | Delete
return;
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
// Load gathered images
[118] Fix | Delete
if (!$this->_existed_src_list) {
[119] Fix | Delete
// To aavoid extra query when recalling this function
[120] Fix | Delete
self::debug('SELECT src from img_optm table');
[121] Fix | Delete
if ($this->__data->tb_exist('img_optm')) {
[122] Fix | Delete
$q = "SELECT src FROM `$this->_table_img_optm` WHERE post_id = %d";
[123] Fix | Delete
$list = $wpdb->get_results($wpdb->prepare($q, $post_id));
[124] Fix | Delete
foreach ($list as $v) {
[125] Fix | Delete
$this->_existed_src_list[] = $post_id . '.' . $v->src;
[126] Fix | Delete
}
[127] Fix | Delete
}
[128] Fix | Delete
if ($this->__data->tb_exist('img_optming')) {
[129] Fix | Delete
$q = "SELECT src FROM `$this->_table_img_optming` WHERE post_id = %d";
[130] Fix | Delete
$list = $wpdb->get_results($wpdb->prepare($q, $post_id));
[131] Fix | Delete
foreach ($list as $v) {
[132] Fix | Delete
$this->_existed_src_list[] = $post_id . '.' . $v->src;
[133] Fix | Delete
}
[134] Fix | Delete
} else {
[135] Fix | Delete
$this->__data->tb_create('img_optming');
[136] Fix | Delete
}
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
// Prepare images
[140] Fix | Delete
$this->tmp_pid = $post_id;
[141] Fix | Delete
$this->tmp_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/';
[142] Fix | Delete
$this->_append_img_queue($meta_value, true);
[143] Fix | Delete
if (!empty($meta_value['sizes'])) {
[144] Fix | Delete
foreach( $meta_value['sizes'] as $img_size_name => $img_size ){
[145] Fix | Delete
$this->_append_img_queue($img_size, false, $img_size_name );
[146] Fix | Delete
}
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
if (!$this->_img_in_queue) {
[150] Fix | Delete
self::debug('auto update attachment meta 2 bypass: empty _img_in_queue');
[151] Fix | Delete
return;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
// Save to DB
[155] Fix | Delete
$this->_save_raw();
[156] Fix | Delete
[157] Fix | Delete
// $this->_send_request();
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
/**
[161] Fix | Delete
* Auto send optm request
[162] Fix | Delete
*
[163] Fix | Delete
* @since 2.4.1
[164] Fix | Delete
* @access public
[165] Fix | Delete
*/
[166] Fix | Delete
public static function cron_auto_request() {
[167] Fix | Delete
if (!wp_doing_cron()) {
[168] Fix | Delete
return false;
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
$instance = self::cls();
[172] Fix | Delete
$instance->new_req();
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* Calculate wet run allowance
[177] Fix | Delete
*
[178] Fix | Delete
* @since 3.0
[179] Fix | Delete
*/
[180] Fix | Delete
public function wet_limit() {
[181] Fix | Delete
$wet_limit = 1;
[182] Fix | Delete
if (!empty($this->_summary['img_taken'])) {
[183] Fix | Delete
$wet_limit = pow($this->_summary['img_taken'], 2);
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
if ($wet_limit == 1 && !empty($this->_summary['img_status.' . self::STATUS_ERR_OPTM])) {
[187] Fix | Delete
$wet_limit = pow($this->_summary['img_status.' . self::STATUS_ERR_OPTM], 2);
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
if ($wet_limit < Cloud::IMG_OPTM_DEFAULT_GROUP) {
[191] Fix | Delete
return $wet_limit;
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
// No limit
[195] Fix | Delete
return false;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
/**
[199] Fix | Delete
* Push raw img to image optm server
[200] Fix | Delete
*
[201] Fix | Delete
* @since 1.6
[202] Fix | Delete
* @access public
[203] Fix | Delete
*/
[204] Fix | Delete
public function new_req() {
[205] Fix | Delete
global $wpdb;
[206] Fix | Delete
[207] Fix | Delete
// check if is running
[208] Fix | Delete
if (!empty($this->_summary['is_running']) && time() - $this->_summary['is_running'] < apply_filters('litespeed_imgoptm_new_req_interval', 3600)) {
[209] Fix | Delete
self::debug('The previous req was in 3600s.');
[210] Fix | Delete
return;
[211] Fix | Delete
}
[212] Fix | Delete
$this->_summary['is_running'] = time();
[213] Fix | Delete
self::save_summary();
[214] Fix | Delete
[215] Fix | Delete
// Check if has credit to push
[216] Fix | Delete
$err = false;
[217] Fix | Delete
$allowance = Cloud::cls()->allowance(Cloud::SVC_IMG_OPTM, $err);
[218] Fix | Delete
[219] Fix | Delete
$wet_limit = $this->wet_limit();
[220] Fix | Delete
[221] Fix | Delete
self::debug("allowance_max $allowance wet_limit $wet_limit");
[222] Fix | Delete
if ($wet_limit && $wet_limit < $allowance) {
[223] Fix | Delete
$allowance = $wet_limit;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
if (!$allowance) {
[227] Fix | Delete
self::debug('❌ No credit');
[228] Fix | Delete
Admin_Display::error(Error::msg($err));
[229] Fix | Delete
$this->_finished_running();
[230] Fix | Delete
return;
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
self::debug('preparing images to push');
[234] Fix | Delete
[235] Fix | Delete
$this->__data->tb_create('img_optming');
[236] Fix | Delete
[237] Fix | Delete
$q = "SELECT COUNT(1) FROM `$this->_table_img_optming` WHERE optm_status = %d";
[238] Fix | Delete
$q = $wpdb->prepare($q, array( self::STATUS_REQUESTED ));
[239] Fix | Delete
$total_requested = $wpdb->get_var($q);
[240] Fix | Delete
$max_requested = $allowance * 1;
[241] Fix | Delete
[242] Fix | Delete
if ($total_requested > $max_requested) {
[243] Fix | Delete
self::debug('❌ Too many queued images (' . $total_requested . ' > ' . $max_requested . ')');
[244] Fix | Delete
Admin_Display::error(Error::msg('too_many_requested'));
[245] Fix | Delete
$this->_finished_running();
[246] Fix | Delete
return;
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
$allowance -= $total_requested;
[250] Fix | Delete
[251] Fix | Delete
if ($allowance < 1) {
[252] Fix | Delete
self::debug('❌ Too many requested images ' . $total_requested);
[253] Fix | Delete
Admin_Display::error(Error::msg('too_many_requested'));
[254] Fix | Delete
$this->_finished_running();
[255] Fix | Delete
return;
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
// Limit maximum number of items waiting to be pulled
[259] Fix | Delete
$q = "SELECT COUNT(1) FROM `$this->_table_img_optming` WHERE optm_status = %d";
[260] Fix | Delete
$q = $wpdb->prepare($q, array( self::STATUS_NOTIFIED ));
[261] Fix | Delete
$total_notified = $wpdb->get_var($q);
[262] Fix | Delete
if ($total_notified > 0) {
[263] Fix | Delete
self::debug('❌ Too many notified images (' . $total_notified . ')');
[264] Fix | Delete
Admin_Display::error(Error::msg('too_many_notified'));
[265] Fix | Delete
$this->_finished_running();
[266] Fix | Delete
return;
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
$q = "SELECT COUNT(1) FROM `$this->_table_img_optming` WHERE optm_status IN (%d, %d)";
[270] Fix | Delete
$q = $wpdb->prepare($q, array( self::STATUS_NEW, self::STATUS_RAW ));
[271] Fix | Delete
$total_new = $wpdb->get_var($q);
[272] Fix | Delete
// $allowance -= $total_new;
[273] Fix | Delete
[274] Fix | Delete
// May need to get more images
[275] Fix | Delete
$list = [];
[276] Fix | Delete
$more = $allowance - $total_new;
[277] Fix | Delete
if ($more > 0) {
[278] Fix | Delete
$q = "SELECT b.post_id, b.meta_value
[279] Fix | Delete
FROM `$wpdb->posts` a
[280] Fix | Delete
LEFT JOIN `$wpdb->postmeta` b ON b.post_id = a.ID
[281] Fix | Delete
WHERE b.meta_key = '_wp_attachment_metadata'
[282] Fix | Delete
AND a.post_type = 'attachment'
[283] Fix | Delete
AND a.post_status = 'inherit'
[284] Fix | Delete
AND a.ID>%d
[285] Fix | Delete
AND a.post_mime_type IN ('image/jpeg', 'image/png', 'image/gif')
[286] Fix | Delete
ORDER BY a.ID
[287] Fix | Delete
LIMIT %d
[288] Fix | Delete
";
[289] Fix | Delete
$q = $wpdb->prepare($q, array( $this->_summary['next_post_id'], $more ));
[290] Fix | Delete
$list = $wpdb->get_results($q);
[291] Fix | Delete
foreach ($list as $v) {
[292] Fix | Delete
if (!$v->post_id) {
[293] Fix | Delete
continue;
[294] Fix | Delete
}
[295] Fix | Delete
[296] Fix | Delete
$this->_summary['next_post_id'] = $v->post_id;
[297] Fix | Delete
[298] Fix | Delete
$meta_value = $this->_parse_wp_meta_value($v);
[299] Fix | Delete
if (!$meta_value) {
[300] Fix | Delete
continue;
[301] Fix | Delete
}
[302] Fix | Delete
$meta_value['file'] = wp_normalize_path($meta_value['file']);
[303] Fix | Delete
$basedir = $this->wp_upload_dir['basedir'] . '/';
[304] Fix | Delete
if (strpos($meta_value['file'], $basedir) === 0) {
[305] Fix | Delete
$meta_value['file'] = substr($meta_value['file'], strlen($basedir));
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
$this->tmp_pid = $v->post_id;
[309] Fix | Delete
$this->tmp_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/';
[310] Fix | Delete
$this->_append_img_queue($meta_value, true);
[311] Fix | Delete
if (!empty($meta_value['sizes'])) {
[312] Fix | Delete
foreach( $meta_value['sizes'] as $img_size_name => $img_size ){
[313] Fix | Delete
$this->_append_img_queue($img_size, false, $img_size_name );
[314] Fix | Delete
}
[315] Fix | Delete
}
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
self::save_summary();
[319] Fix | Delete
[320] Fix | Delete
$num_a = count($this->_img_in_queue);
[321] Fix | Delete
self::debug('Images found: ' . $num_a);
[322] Fix | Delete
$this->_filter_duplicated_src();
[323] Fix | Delete
self::debug('Images after duplicated: ' . count($this->_img_in_queue));
[324] Fix | Delete
$this->_filter_invalid_src();
[325] Fix | Delete
self::debug('Images after invalid: ' . count($this->_img_in_queue));
[326] Fix | Delete
// Check w/ legacy imgoptm table, bypass finished images
[327] Fix | Delete
$this->_filter_legacy_src();
[328] Fix | Delete
[329] Fix | Delete
$num_b = count($this->_img_in_queue);
[330] Fix | Delete
if ($num_b != $num_a) {
[331] Fix | Delete
self::debug('Images after filtered duplicated/invalid/legacy src: ' . $num_b);
[332] Fix | Delete
}
[333] Fix | Delete
[334] Fix | Delete
// Save to DB
[335] Fix | Delete
$this->_save_raw();
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
// Push to Cloud server
[339] Fix | Delete
$accepted_imgs = $this->_send_request($allowance);
[340] Fix | Delete
[341] Fix | Delete
$this->_finished_running();
[342] Fix | Delete
if (!$accepted_imgs) {
[343] Fix | Delete
return;
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
$placeholder1 = Admin_Display::print_plural($accepted_imgs[0], 'image');
[347] Fix | Delete
$placeholder2 = Admin_Display::print_plural($accepted_imgs[1], 'image');
[348] Fix | Delete
$msg = sprintf(__('Pushed %1$s to Cloud server, accepted %2$s.', 'litespeed-cache'), $placeholder1, $placeholder2);
[349] Fix | Delete
Admin_Display::success($msg);
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
/**
[353] Fix | Delete
* Set running to done
[354] Fix | Delete
*/
[355] Fix | Delete
private function _finished_running() {
[356] Fix | Delete
$this->_summary['is_running'] = 0;
[357] Fix | Delete
self::save_summary();
[358] Fix | Delete
}
[359] Fix | Delete
[360] Fix | Delete
/**
[361] Fix | Delete
* Add a new img to queue which will be pushed to request
[362] Fix | Delete
*
[363] Fix | Delete
* @since 1.6
[364] Fix | Delete
* @since 7.5 Allow to choose which image sizes should be optimized + added parameter $img_size_name.
[365] Fix | Delete
* @access private
[366] Fix | Delete
*/
[367] Fix | Delete
private function _append_img_queue( $meta_value, $is_ori_file = false, $img_size_name = false ) {
[368] Fix | Delete
if (empty($meta_value['file']) || empty($meta_value['width']) || empty($meta_value['height'])) {
[369] Fix | Delete
self::debug2('bypass image due to lack of file/w/h: pid ' . $this->tmp_pid, $meta_value);
[370] Fix | Delete
return;
[371] Fix | Delete
}
[372] Fix | Delete
[373] Fix | Delete
$short_file_path = $meta_value['file'];
[374] Fix | Delete
[375] Fix | Delete
// Test if need to skip image size.
[376] Fix | Delete
if (!$is_ori_file) {
[377] Fix | Delete
$short_file_path = $this->tmp_path . $short_file_path;
[378] Fix | Delete
$skip = false !== array_search( $img_size_name, $this->_sizes_skipped, true );
[379] Fix | Delete
if($skip){
[380] Fix | Delete
self::debug2( 'bypass image ' . $short_file_path . ' due to skipped size: ' . $img_size_name );
[381] Fix | Delete
return;
[382] Fix | Delete
}
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
// Check if src is gathered already or not
[386] Fix | Delete
if (in_array($this->tmp_pid . '.' . $short_file_path, $this->_existed_src_list)) {
[387] Fix | Delete
// Debug2::debug2( '[Img_Optm] bypass image due to gathered: pid ' . $this->tmp_pid . ' ' . $short_file_path );
[388] Fix | Delete
return;
[389] Fix | Delete
} else {
[390] Fix | Delete
// Append handled images
[391] Fix | Delete
$this->_existed_src_list[] = $this->tmp_pid . '.' . $short_file_path;
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
// check file exists or not
[395] Fix | Delete
$_img_info = $this->__media->info($short_file_path, $this->tmp_pid);
[396] Fix | Delete
[397] Fix | Delete
$extension = pathinfo($short_file_path, PATHINFO_EXTENSION);
[398] Fix | Delete
if (!$_img_info || !in_array($extension, array( 'jpg', 'jpeg', 'png', 'gif' ))) {
[399] Fix | Delete
self::debug2('bypass image due to file not exist: pid ' . $this->tmp_pid . ' ' . $short_file_path);
[400] Fix | Delete
return;
[401] Fix | Delete
}
[402] Fix | Delete
[403] Fix | Delete
// Check if optimized file exists or not
[404] Fix | Delete
$target_needed = false;
[405] Fix | Delete
if ($this->_format) {
[406] Fix | Delete
$target_file_path = $short_file_path . '.' . $this->_format;
[407] Fix | Delete
if (!$this->__media->info($target_file_path, $this->tmp_pid)) {
[408] Fix | Delete
$target_needed = true;
[409] Fix | Delete
}
[410] Fix | Delete
}
[411] Fix | Delete
if ($this->conf(self::O_IMG_OPTM_ORI)) {
[412] Fix | Delete
$target_file_path = substr($short_file_path, 0, -strlen($extension)) . 'bk.' . $extension;
[413] Fix | Delete
if (!$this->__media->info($target_file_path, $this->tmp_pid)) {
[414] Fix | Delete
$target_needed = true;
[415] Fix | Delete
}
[416] Fix | Delete
}
[417] Fix | Delete
if (!$target_needed) {
[418] Fix | Delete
self::debug2('bypass image due to optimized file exists: pid ' . $this->tmp_pid . ' ' . $short_file_path);
[419] Fix | Delete
return;
[420] Fix | Delete
}
[421] Fix | Delete
[422] Fix | Delete
// Debug2::debug2( '[Img_Optm] adding image: pid ' . $this->tmp_pid );
[423] Fix | Delete
[424] Fix | Delete
$this->_img_in_queue[] = array(
[425] Fix | Delete
'pid' => $this->tmp_pid,
[426] Fix | Delete
'md5' => $_img_info['md5'],
[427] Fix | Delete
'url' => $_img_info['url'],
[428] Fix | Delete
'src' => $short_file_path, // not needed in LiteSpeed IAPI, just leave for local storage after post
[429] Fix | Delete
'mime_type' => !empty($meta_value['mime-type']) ? $meta_value['mime-type'] : '',
[430] Fix | Delete
);
[431] Fix | Delete
}
[432] Fix | Delete
[433] Fix | Delete
/**
[434] Fix | Delete
* Save gathered image raw data
[435] Fix | Delete
*
[436] Fix | Delete
* @since 3.0
[437] Fix | Delete
*/
[438] Fix | Delete
private function _save_raw() {
[439] Fix | Delete
if (empty($this->_img_in_queue)) {
[440] Fix | Delete
return;
[441] Fix | Delete
}
[442] Fix | Delete
$data = [];
[443] Fix | Delete
$pid_list = [];
[444] Fix | Delete
foreach ($this->_img_in_queue as $k => $v) {
[445] Fix | Delete
$_img_info = $this->__media->info($v['src'], $v['pid']);
[446] Fix | Delete
[447] Fix | Delete
// attachment doesn't exist, delete the record
[448] Fix | Delete
if (empty($_img_info['url']) || empty($_img_info['md5'])) {
[449] Fix | Delete
unset($this->_img_in_queue[$k]);
[450] Fix | Delete
continue;
[451] Fix | Delete
}
[452] Fix | Delete
$pid_list[] = (int) $v['pid'];
[453] Fix | Delete
[454] Fix | Delete
$data[] = $v['pid'];
[455] Fix | Delete
$data[] = self::STATUS_RAW;
[456] Fix | Delete
$data[] = $v['src'];
[457] Fix | Delete
}
[458] Fix | Delete
[459] Fix | Delete
global $wpdb;
[460] Fix | Delete
$fields = 'post_id, optm_status, src';
[461] Fix | Delete
$q = "INSERT INTO `$this->_table_img_optming` ( $fields ) VALUES ";
[462] Fix | Delete
[463] Fix | Delete
// Add placeholder
[464] Fix | Delete
$q .= Utility::chunk_placeholder($data, $fields);
[465] Fix | Delete
[466] Fix | Delete
// Store data
[467] Fix | Delete
$wpdb->query($wpdb->prepare($q, $data));
[468] Fix | Delete
[469] Fix | Delete
$count = count($this->_img_in_queue);
[470] Fix | Delete
self::debug('Added raw images [total] ' . $count);
[471] Fix | Delete
[472] Fix | Delete
$this->_img_in_queue = [];
[473] Fix | Delete
[474] Fix | Delete
// Save thumbnail groups for future rescan index
[475] Fix | Delete
$this->_gen_thumbnail_set();
[476] Fix | Delete
[477] Fix | Delete
$pid_list = array_unique($pid_list);
[478] Fix | Delete
self::debug('pid list to append to postmeta', $pid_list);
[479] Fix | Delete
$pid_list = array_diff($pid_list, $this->_pids_set);
[480] Fix | Delete
$this->_pids_set = array_merge($this->_pids_set, $pid_list);
[481] Fix | Delete
[482] Fix | Delete
$existed_meta = $wpdb->get_results("SELECT * FROM `$wpdb->postmeta` WHERE post_id IN ('" . implode("','", $pid_list) . "') AND meta_key='" . self::DB_SET . "'");
[483] Fix | Delete
$existed_pid = [];
[484] Fix | Delete
if ($existed_meta) {
[485] Fix | Delete
foreach ($existed_meta as $v) {
[486] Fix | Delete
$existed_pid[] = $v->post_id;
[487] Fix | Delete
}
[488] Fix | Delete
self::debug('pid list to update postmeta', $existed_pid);
[489] Fix | Delete
$wpdb->query(
[490] Fix | Delete
$wpdb->prepare("UPDATE `$wpdb->postmeta` SET meta_value=%s WHERE post_id IN ('" . implode("','", $existed_pid) . "') AND meta_key=%s", array(
[491] Fix | Delete
$this->_thumbnail_set,
[492] Fix | Delete
self::DB_SET,
[493] Fix | Delete
))
[494] Fix | Delete
);
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
// Add new meta
[498] Fix | Delete
$new_pids = $existed_pid ? array_diff($pid_list, $existed_pid) : $pid_list;
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function