Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wp-smush.../core/modules
File: class-smush.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Smush core class: Smush class
[2] Fix | Delete
*
[3] Fix | Delete
* @package Smush\Core\Modules
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
namespace Smush\Core\Modules;
[7] Fix | Delete
[8] Fix | Delete
use Smush\Core\Api\Backoff;
[9] Fix | Delete
use Smush\Core\Api\Request_Multiple;
[10] Fix | Delete
use Smush\Core\Core;
[11] Fix | Delete
use Smush\Core\Helper;
[12] Fix | Delete
use Smush\Core\Error_Handler;
[13] Fix | Delete
use Smush\Core\Media\Media_Item_Cache;
[14] Fix | Delete
use Smush\Core\Media\Media_Item_Optimizer;
[15] Fix | Delete
use Smush\Core\Smush\Smusher;
[16] Fix | Delete
use Smush\Core\Smush\Smush_Optimization;
[17] Fix | Delete
use Smush\Core\Webp\Webp_Converter;
[18] Fix | Delete
use Smush\Core\Webp\Webp_Optimization;
[19] Fix | Delete
use WP_Error;
[20] Fix | Delete
use WP_Smush;
[21] Fix | Delete
[22] Fix | Delete
if ( ! defined( 'WPINC' ) ) {
[23] Fix | Delete
die;
[24] Fix | Delete
}
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Class Smush
[28] Fix | Delete
*/
[29] Fix | Delete
class Smush extends Abstract_Module {
[30] Fix | Delete
const ERROR_SSL_CERT = 'ssl_cert_error';
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Meta key to save smush result to db.
[34] Fix | Delete
*
[35] Fix | Delete
* @var string $smushed_meta_key
[36] Fix | Delete
*/
[37] Fix | Delete
public static $smushed_meta_key = 'wp-smpro-smush-data';
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Images dimensions array.
[41] Fix | Delete
*
[42] Fix | Delete
* @var array $image_sizes
[43] Fix | Delete
*/
[44] Fix | Delete
public $image_sizes = array();
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* Stores the headers returned by the latest API call.
[48] Fix | Delete
*
[49] Fix | Delete
* @var array $api_headers
[50] Fix | Delete
*/
[51] Fix | Delete
protected $api_headers = array();
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Prevent third party try to run another smush while it's running.
[55] Fix | Delete
*
[56] Fix | Delete
* @access private
[57] Fix | Delete
*
[58] Fix | Delete
* @var bool
[59] Fix | Delete
*/
[60] Fix | Delete
private $prevent_infinite_loop;
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* @var Request_Multiple
[64] Fix | Delete
*/
[65] Fix | Delete
private $request_multiple;
[66] Fix | Delete
/**
[67] Fix | Delete
* @var Backoff
[68] Fix | Delete
*/
[69] Fix | Delete
private $backoff;
[70] Fix | Delete
[71] Fix | Delete
/**
[72] Fix | Delete
* WP_Smush constructor.
[73] Fix | Delete
*/
[74] Fix | Delete
public function init() {
[75] Fix | Delete
// Update the Super Smush count, after the Smush'ing.
[76] Fix | Delete
//add_action( 'wp_smush_image_optimised', array( $this, 'update_lists' ), '', 2 );
[77] Fix | Delete
[78] Fix | Delete
// Smush image (Auto Smush) when `wp_generate_attachment_metadata` filter is fired.
[79] Fix | Delete
add_filter( 'wp_generate_attachment_metadata', array( $this, 'smush_image' ), 15, 2 );
[80] Fix | Delete
[81] Fix | Delete
// Delete backup files.
[82] Fix | Delete
//add_action( 'delete_attachment', array( $this, 'delete_images' ), 12 );
[83] Fix | Delete
[84] Fix | Delete
// Handle the Async optimisation.
[85] Fix | Delete
add_action( 'wp_async_wp_generate_attachment_metadata', array( $this, 'wp_smush_handle_async' ) );
[86] Fix | Delete
add_action( 'wp_async_wp_save_image_editor_file', array( $this, 'wp_smush_handle_editor_async' ), '', 2 );
[87] Fix | Delete
[88] Fix | Delete
// Make sure we treat scaled images as additional size.
[89] Fix | Delete
//add_filter( 'wp_smush_add_scaled_images_to_meta', array( $this, 'add_scaled_to_meta' ), 10, 2 );
[90] Fix | Delete
[91] Fix | Delete
// Fix SSL CA certificates issue.
[92] Fix | Delete
add_action( 'wp_smush_before_smush_file', array( $this, 'fix_ssl_ca_certificate_error' ) );
[93] Fix | Delete
[94] Fix | Delete
$this->request_multiple = new Request_Multiple();
[95] Fix | Delete
$this->backoff = new Backoff();
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Check whether to show warning or not for Pro users, if they don't have a valid install
[100] Fix | Delete
*
[101] Fix | Delete
* @return bool
[102] Fix | Delete
*/
[103] Fix | Delete
public function show_warning() {
[104] Fix | Delete
// If it's a free setup, Go back right away!
[105] Fix | Delete
if ( ! WP_Smush::is_pro() ) {
[106] Fix | Delete
return false;
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
// Return. If we don't have any headers.
[110] Fix | Delete
if ( ! isset( $this->api_headers ) ) {
[111] Fix | Delete
return false;
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
// Show warning, if function says it's premium and api says not premium.
[115] Fix | Delete
if ( isset( $this->api_headers['is_premium'] ) && ! (int) $this->api_headers['is_premium'] ) {
[116] Fix | Delete
return true;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
return false;
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
/**
[123] Fix | Delete
* Add/Remove image id from Super Smushed images count.
[124] Fix | Delete
*
[125] Fix | Delete
* @param int $id Image id.
[126] Fix | Delete
* @param string $op_type Add/remove, whether to add the image id or remove it from the list.
[127] Fix | Delete
* @param string $key Options key.
[128] Fix | Delete
*
[129] Fix | Delete
* @return bool Whether the Super Smushed option was update or not
[130] Fix | Delete
*/
[131] Fix | Delete
public function update_super_smush_count( $id, $op_type = 'add', $key = 'wp-smush-super_smushed' ) {
[132] Fix | Delete
// Get the existing count.
[133] Fix | Delete
$super_smushed = get_option( $key, false );
[134] Fix | Delete
[135] Fix | Delete
// Initialize if it doesn't exists.
[136] Fix | Delete
if ( ! $super_smushed || empty( $super_smushed['ids'] ) ) {
[137] Fix | Delete
$super_smushed = array(
[138] Fix | Delete
'ids' => array(),
[139] Fix | Delete
);
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
// Insert the id, if not in there already.
[143] Fix | Delete
if ( 'add' === $op_type && ! in_array( $id, $super_smushed['ids'] ) ) {
[144] Fix | Delete
$super_smushed['ids'][] = $id;
[145] Fix | Delete
} elseif ( 'remove' === $op_type && false !== ( $k = array_search( $id, $super_smushed['ids'] ) ) ) {
[146] Fix | Delete
// Else remove the id from the list.
[147] Fix | Delete
unset( $super_smushed['ids'][ $k ] );
[148] Fix | Delete
[149] Fix | Delete
// Reset all the indexes.
[150] Fix | Delete
$super_smushed['ids'] = array_values( $super_smushed['ids'] );
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
// Add the timestamp.
[154] Fix | Delete
$super_smushed['timestamp'] = time();
[155] Fix | Delete
[156] Fix | Delete
update_option( $key, $super_smushed, false );
[157] Fix | Delete
[158] Fix | Delete
// Update to database.
[159] Fix | Delete
return true;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* Checks if the image compression is lossy, stores the image id in options table
[164] Fix | Delete
*
[165] Fix | Delete
* @param int $id Image Id.
[166] Fix | Delete
* @param array $stats Compression Stats.
[167] Fix | Delete
* @param string $key Meta Key for storing the Super Smushed ids (Optional for Media Library).
[168] Fix | Delete
* Need To be specified for NextGen.
[169] Fix | Delete
*
[170] Fix | Delete
* @return bool
[171] Fix | Delete
*/
[172] Fix | Delete
public function update_lists( $id, $stats, $key = '' ) {
[173] Fix | Delete
// If Stats are empty or the image id is not provided, return.
[174] Fix | Delete
if ( empty( $stats ) || empty( $id ) || empty( $stats['stats'] ) ) {
[175] Fix | Delete
return false;
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
// Update Super Smush count.
[179] Fix | Delete
if ( isset( $stats['stats']['lossy'] ) && 1 == $stats['stats']['lossy'] ) {
[180] Fix | Delete
if ( empty( $key ) ) {
[181] Fix | Delete
update_post_meta( $id, 'wp-smush-lossy', 1 );
[182] Fix | Delete
} else {
[183] Fix | Delete
$this->update_super_smush_count( $id, 'add', $key );
[184] Fix | Delete
}
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
// Check and update re-smush list for media gallery.
[188] Fix | Delete
if ( ! empty( $this->resmush_ids ) && in_array( $id, $this->resmush_ids ) ) {
[189] Fix | Delete
$this->update_resmush_list( $id );
[190] Fix | Delete
}
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
/**
[194] Fix | Delete
* Remove the given attachment id from resmush list and updates it to db
[195] Fix | Delete
*
[196] Fix | Delete
* @param string $attachment_id Attachment ID.
[197] Fix | Delete
* @param string $mkey Option key.
[198] Fix | Delete
*/
[199] Fix | Delete
public function update_resmush_list( $attachment_id, $mkey = 'wp-smush-resmush-list' ) {
[200] Fix | Delete
$resmush_list = get_option( $mkey );
[201] Fix | Delete
[202] Fix | Delete
// If there are any items in the resmush list, Unset the Key.
[203] Fix | Delete
if ( ! empty( $resmush_list ) && count( $resmush_list ) > 0 ) {
[204] Fix | Delete
$key = array_search( $attachment_id, $resmush_list );
[205] Fix | Delete
if ( $resmush_list ) {
[206] Fix | Delete
unset( $resmush_list[ $key ] );
[207] Fix | Delete
}
[208] Fix | Delete
$resmush_list = array_values( $resmush_list );
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
// If Resmush List is empty.
[212] Fix | Delete
if ( empty( $resmush_list ) || 0 === count( $resmush_list ) ) {
[213] Fix | Delete
// Delete resmush list.
[214] Fix | Delete
delete_option( $mkey );
[215] Fix | Delete
} else {
[216] Fix | Delete
update_option( $mkey, $resmush_list, false );
[217] Fix | Delete
}
[218] Fix | Delete
}
[219] Fix | Delete
[220] Fix | Delete
/**
[221] Fix | Delete
* Remove the Update info.
[222] Fix | Delete
*
[223] Fix | Delete
* @param bool $remove_notice Remove notice.
[224] Fix | Delete
*/
[225] Fix | Delete
public function dismiss_update_info( $remove_notice = false ) {
[226] Fix | Delete
// From URL arg.
[227] Fix | Delete
if ( isset( $_GET['dismiss_smush_update_info'] ) && 1 == $_GET['dismiss_smush_update_info'] ) {
[228] Fix | Delete
$remove_notice = true;
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
// From Ajax.
[232] Fix | Delete
if ( ! empty( $_REQUEST['action'] ) && 'dismiss_update_info' === $_REQUEST['action'] ) {
[233] Fix | Delete
$remove_notice = true;
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
// Update Db.
[237] Fix | Delete
if ( $remove_notice ) {
[238] Fix | Delete
update_site_option( 'wp-smush-hide_update_info', 1 );
[239] Fix | Delete
}
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
/**
[243] Fix | Delete
* Check whether to skip a specific image size or not.
[244] Fix | Delete
*
[245] Fix | Delete
* @param string $size Registered image size.
[246] Fix | Delete
*
[247] Fix | Delete
* @return bool Skip the image size or not.
[248] Fix | Delete
*/
[249] Fix | Delete
public function skip_image_size( $size ) {
[250] Fix | Delete
// No image size specified, Don't skip.
[251] Fix | Delete
if ( empty( $size ) ) {
[252] Fix | Delete
return false;
[253] Fix | Delete
}
[254] Fix | Delete
[255] Fix | Delete
$image_sizes = $this->settings->get_setting( 'wp-smush-image_sizes' );
[256] Fix | Delete
[257] Fix | Delete
// If image sizes aren't set, don't skip any of the image size.
[258] Fix | Delete
if ( false === $image_sizes ) {
[259] Fix | Delete
return false;
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
// Check if the size is in the smush list.
[263] Fix | Delete
return is_array( $image_sizes ) && ! in_array( $size, $image_sizes, true );
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
private function validate_file( $file_path ) {
[267] Fix | Delete
$errors = new WP_Error();
[268] Fix | Delete
$dir_name = trailingslashit( dirname( $file_path ) );
[269] Fix | Delete
[270] Fix | Delete
// Check if file exists and the directory is writable.
[271] Fix | Delete
if ( empty( $file_path ) ) {
[272] Fix | Delete
$errors->add( 'empty_path', Error_Handler::get_error_message( 'empty_path' ) );
[273] Fix | Delete
} elseif ( ! file_exists( $file_path ) || ! is_file( $file_path ) ) {
[274] Fix | Delete
// Check that the file exists.
[275] Fix | Delete
/* translators: %s: file path */
[276] Fix | Delete
$errors->add( 'file_not_found', sprintf( Error_Handler::get_error_message( 'file_not_found' ), basename( $file_path ) ) );
[277] Fix | Delete
} elseif ( ! is_writable( $dir_name ) ) {
[278] Fix | Delete
// Check that the file is writable.
[279] Fix | Delete
/* translators: %s: directory name */
[280] Fix | Delete
$errors->add( 'not_writable', sprintf( Error_Handler::get_error_message( 'not_writable' ), $dir_name ) );
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
$file_size = file_exists( $file_path ) ? filesize( $file_path ) : '';
[284] Fix | Delete
[285] Fix | Delete
// Check if premium user.
[286] Fix | Delete
if ( WP_Smush::is_pro() ) {
[287] Fix | Delete
$max_size = WP_SMUSH_PREMIUM_MAX_BYTES;
[288] Fix | Delete
$size_limit_code = 'size_pro_limit';
[289] Fix | Delete
} else {
[290] Fix | Delete
$size_limit_code = 'size_limit';
[291] Fix | Delete
$max_size = WP_SMUSH_MAX_BYTES;
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
// Check if file exists.
[295] Fix | Delete
if ( 0 === (int) $file_size ) {
[296] Fix | Delete
$errors->add( 'file_not_found', sprintf( Error_Handler::get_error_message( 'file_not_found' ), basename( $file_path ) ) );
[297] Fix | Delete
} elseif ( $file_size > $max_size ) {
[298] Fix | Delete
// Check size limit.
[299] Fix | Delete
$errors->add( $size_limit_code, sprintf( Error_Handler::get_error_message( $size_limit_code ), size_format( $file_size, 1 ) ), array(
[300] Fix | Delete
'file_name' => basename( $file_path )
[301] Fix | Delete
) );
[302] Fix | Delete
}
[303] Fix | Delete
[304] Fix | Delete
return $errors;
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
private function smush_parallel( $file_paths, $convert_to_webp = false ) {
[308] Fix | Delete
$file_errors = array();
[309] Fix | Delete
$retry = array();
[310] Fix | Delete
$requests = array();
[311] Fix | Delete
foreach ( $file_paths as $file_key => $file_path ) {
[312] Fix | Delete
$error = $this->validate_file( $file_path );
[313] Fix | Delete
if ( $error->has_errors() ) {
[314] Fix | Delete
$file_errors[ $file_key ] = $error;
[315] Fix | Delete
} else {
[316] Fix | Delete
$requests[ $file_key ] = $this->get_multi_api_request_args( $convert_to_webp, $file_path );
[317] Fix | Delete
}
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
// Send off the valid paths to the API
[321] Fix | Delete
$responses = array();
[322] Fix | Delete
$this->request_multiple->do_requests( $requests, array(
[323] Fix | Delete
'timeout' => WP_SMUSH_TIMEOUT,
[324] Fix | Delete
'connect_timeout' => 5,
[325] Fix | Delete
'user-agent' => WP_SMUSH_UA,
[326] Fix | Delete
'complete' => function ( $response, $response_key ) use ( &$requests, &$responses, &$retry, $file_paths, $convert_to_webp ) {
[327] Fix | Delete
// Free up memory
[328] Fix | Delete
$requests[ $response_key ] = null;
[329] Fix | Delete
[330] Fix | Delete
$file_path = $file_paths[ $response_key ];
[331] Fix | Delete
if ( $this->should_retry_smush( $response ) ) {
[332] Fix | Delete
$retry[ $response_key ] = $file_path;
[333] Fix | Delete
} else {
[334] Fix | Delete
$responses[ $response_key ] = $this->handle_response(
[335] Fix | Delete
$response,
[336] Fix | Delete
$file_path,
[337] Fix | Delete
$convert_to_webp
[338] Fix | Delete
);
[339] Fix | Delete
}
[340] Fix | Delete
},
[341] Fix | Delete
) );
[342] Fix | Delete
[343] Fix | Delete
// Retry failures with exponential backoff
[344] Fix | Delete
foreach ( $retry as $retry_key => $retry_file_path ) {
[345] Fix | Delete
$responses[ $retry_key ] = $this->do_smushit(
[346] Fix | Delete
$retry_file_path,
[347] Fix | Delete
$convert_to_webp,
[348] Fix | Delete
WP_SMUSH_RETRY_ATTEMPTS
[349] Fix | Delete
);
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
// Merge the responses
[353] Fix | Delete
return array_merge( $responses, $file_errors );
[354] Fix | Delete
}
[355] Fix | Delete
[356] Fix | Delete
private function smush_sequential( $file_paths, $convert_to_webp = false ) {
[357] Fix | Delete
$responses = array();
[358] Fix | Delete
foreach ( $file_paths as $file_size => $file_path ) {
[359] Fix | Delete
$responses[ $file_size ] = $this->do_smushit( $file_path, $convert_to_webp, WP_SMUSH_RETRY_ATTEMPTS );
[360] Fix | Delete
}
[361] Fix | Delete
[362] Fix | Delete
return $responses;
[363] Fix | Delete
}
[364] Fix | Delete
[365] Fix | Delete
/**
[366] Fix | Delete
* @param $convert_to_webp
[367] Fix | Delete
* @param $file_path
[368] Fix | Delete
*
[369] Fix | Delete
* @return array
[370] Fix | Delete
*/
[371] Fix | Delete
private function get_multi_api_request_args( $convert_to_webp, $file_path ) {
[372] Fix | Delete
return array(
[373] Fix | Delete
'url' => $this->get_api_url(),
[374] Fix | Delete
'headers' => $this->get_api_request_headers( $convert_to_webp ),
[375] Fix | Delete
'data' => file_get_contents( $file_path ),
[376] Fix | Delete
'type' => 'POST',
[377] Fix | Delete
);
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
/**
[381] Fix | Delete
* Process an image with Smush.
[382] Fix | Delete
*
[383] Fix | Delete
* @since 3.8.0 Added new param $convert_to_webp.
[384] Fix | Delete
*
[385] Fix | Delete
* @param string $file_path Absolute path to the image.
[386] Fix | Delete
* @param bool $convert_to_webp Convert the image to webp.
[387] Fix | Delete
* @param int $retries Number of times to retry the operation
[388] Fix | Delete
*
[389] Fix | Delete
* @return array|bool|WP_Error
[390] Fix | Delete
*/
[391] Fix | Delete
public function do_smushit( $file_path = '', $convert_to_webp = false, $retries = 0 ) {
[392] Fix | Delete
// TODO: (stats refactor) handle properly
[393] Fix | Delete
return $this->do_smushit_optimization( $file_path, $convert_to_webp, $retries );
[394] Fix | Delete
[395] Fix | Delete
$errors = $this->validate_file( $file_path );
[396] Fix | Delete
if ( count( $errors->get_error_messages() ) ) {
[397] Fix | Delete
Helper::logger()->error(
[398] Fix | Delete
array(
[399] Fix | Delete
sprintf( 'Skipped file [%s] due to error:', Helper::clean_file_path( $file_path ) ),
[400] Fix | Delete
$errors->get_error_messages(),
[401] Fix | Delete
)
[402] Fix | Delete
);
[403] Fix | Delete
return $errors;
[404] Fix | Delete
}
[405] Fix | Delete
[406] Fix | Delete
// Optimize image, and fetch the response.
[407] Fix | Delete
$response = $this->backoff->set_wait( WP_SMUSH_RETRY_WAIT )
[408] Fix | Delete
->set_max_attempts( $retries )
[409] Fix | Delete
->enable_jitter()
[410] Fix | Delete
->set_decider( array( $this, 'should_retry_smush' ) )
[411] Fix | Delete
->run( function () use ( $file_path, $convert_to_webp ) {
[412] Fix | Delete
return $this->_post( $file_path, $convert_to_webp );
[413] Fix | Delete
} );
[414] Fix | Delete
[415] Fix | Delete
return $this->handle_response( $response, $file_path, $convert_to_webp );
[416] Fix | Delete
}
[417] Fix | Delete
[418] Fix | Delete
public function should_retry_smush( $response ) {
[419] Fix | Delete
return WP_SMUSH_RETRY_ATTEMPTS > 0 && (
[420] Fix | Delete
is_wp_error( $response )
[421] Fix | Delete
|| 200 !== wp_remote_retrieve_response_code( $response )
[422] Fix | Delete
);
[423] Fix | Delete
}
[424] Fix | Delete
[425] Fix | Delete
/**
[426] Fix | Delete
* Takes the raw response from the API and performs all the necessary file operations etc.
[427] Fix | Delete
*
[428] Fix | Delete
* @param $response array|WP_Error
[429] Fix | Delete
* @param $file_path string
[430] Fix | Delete
* @param $convert_to_webp boolean
[431] Fix | Delete
*
[432] Fix | Delete
* @return array|WP_Error
[433] Fix | Delete
*/
[434] Fix | Delete
private function handle_response( $response, $file_path, $convert_to_webp ) {
[435] Fix | Delete
$data = $this->parse_response( $response );
[436] Fix | Delete
[437] Fix | Delete
if ( is_wp_error( $data ) ) {
[438] Fix | Delete
if ( $data->get_error_code() === self::ERROR_SSL_CERT ) {
[439] Fix | Delete
// Switch to http protocol.
[440] Fix | Delete
$this->settings->set_setting( 'wp-smush-use_http', 1 );
[441] Fix | Delete
}
[442] Fix | Delete
[443] Fix | Delete
$error_format = $convert_to_webp
[444] Fix | Delete
? 'Cannot convert to webp for image [%s].'
[445] Fix | Delete
: 'Cannot smush image [%s].';
[446] Fix | Delete
[447] Fix | Delete
Helper::logger()->error(
[448] Fix | Delete
array(
[449] Fix | Delete
sprintf( $error_format, Helper::clean_file_path( $file_path ) ),
[450] Fix | Delete
$data->get_error_messages(),
[451] Fix | Delete
)
[452] Fix | Delete
);
[453] Fix | Delete
[454] Fix | Delete
return $data;
[455] Fix | Delete
}
[456] Fix | Delete
[457] Fix | Delete
$bytes_saved = empty( $data->bytes_saved ) ? 0 : $data->bytes_saved;
[458] Fix | Delete
if ( $bytes_saved > 0 ) {
[459] Fix | Delete
$this->save_smushed_image_file(
[460] Fix | Delete
$file_path,
[461] Fix | Delete
$convert_to_webp,
[462] Fix | Delete
$data->image
[463] Fix | Delete
);
[464] Fix | Delete
} else {
[465] Fix | Delete
// No savings, just add an entry to the log
[466] Fix | Delete
Helper::logger()->notice(
[467] Fix | Delete
sprintf(
[468] Fix | Delete
'The smushed image is larger than the original image [%s] (bytes saved %d), keep original image.',
[469] Fix | Delete
Helper::clean_file_path( $file_path ),
[470] Fix | Delete
$bytes_saved
[471] Fix | Delete
)
[472] Fix | Delete
);
[473] Fix | Delete
}
[474] Fix | Delete
[475] Fix | Delete
// No need to pass image data any further
[476] Fix | Delete
$data->image = null;
[477] Fix | Delete
$data->image_md5 = null;
[478] Fix | Delete
[479] Fix | Delete
// Check for API message and store in db.
[480] Fix | Delete
if ( ! empty( $data->api_message ) ) {
[481] Fix | Delete
$this->add_api_message( (array) $data->api_message );
[482] Fix | Delete
}
[483] Fix | Delete
[484] Fix | Delete
// If is_premium is set in response, send it over to check for member validity.
[485] Fix | Delete
if ( ! empty( $data->is_premium ) ) {
[486] Fix | Delete
$this->api_headers['is_premium'] = $data->is_premium;
[487] Fix | Delete
}
[488] Fix | Delete
[489] Fix | Delete
return array(
[490] Fix | Delete
'success' => true,
[491] Fix | Delete
'data' => $data,
[492] Fix | Delete
);
[493] Fix | Delete
}
[494] Fix | Delete
[495] Fix | Delete
/**
[496] Fix | Delete
* Posts an image to Smush.
[497] Fix | Delete
*
[498] Fix | Delete
* @since 3.8.0 Added new param $convert_to_webp.
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function