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/clone/wp-conte.../plugins/wp-smush.../core/smush
File: class-smusher.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Smush\Core\Smush;
[2] Fix | Delete
[3] Fix | Delete
use Smush\Core\Api\Backoff;
[4] Fix | Delete
use Smush\Core\Api\Request_Multiple;
[5] Fix | Delete
use Smush\Core\File_System;
[6] Fix | Delete
use Smush\Core\Helper;
[7] Fix | Delete
use Smush\Core\Server_Utils;
[8] Fix | Delete
use Smush\Core\Settings;
[9] Fix | Delete
use Smush\Core\Upload_Dir;
[10] Fix | Delete
use WP_Error;
[11] Fix | Delete
use WP_Smush;
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Takes raw image file paths and processes them through the Smush API.
[15] Fix | Delete
*/
[16] Fix | Delete
class Smusher {
[17] Fix | Delete
const ERROR_SSL_CERT = 'ssl_cert_error';
[18] Fix | Delete
/**
[19] Fix | Delete
* @var Settings
[20] Fix | Delete
*/
[21] Fix | Delete
private $settings;
[22] Fix | Delete
/**
[23] Fix | Delete
* @var Request_Multiple
[24] Fix | Delete
*/
[25] Fix | Delete
private $request_multiple;
[26] Fix | Delete
/**
[27] Fix | Delete
* @var Backoff
[28] Fix | Delete
*/
[29] Fix | Delete
private $backoff;
[30] Fix | Delete
/**
[31] Fix | Delete
* @var \WDEV_Logger|null
[32] Fix | Delete
*/
[33] Fix | Delete
private $logger;
[34] Fix | Delete
/**
[35] Fix | Delete
* @var int
[36] Fix | Delete
*/
[37] Fix | Delete
private $retry_attempts;
[38] Fix | Delete
/**
[39] Fix | Delete
* @var int
[40] Fix | Delete
*/
[41] Fix | Delete
private $retry_wait;
[42] Fix | Delete
/**
[43] Fix | Delete
* @var int
[44] Fix | Delete
*/
[45] Fix | Delete
private $timeout;
[46] Fix | Delete
/**
[47] Fix | Delete
* @var string
[48] Fix | Delete
*/
[49] Fix | Delete
private $user_agent;
[50] Fix | Delete
/**
[51] Fix | Delete
* @var int
[52] Fix | Delete
*/
[53] Fix | Delete
private $connect_timeout;
[54] Fix | Delete
/**
[55] Fix | Delete
* @var boolean
[56] Fix | Delete
*/
[57] Fix | Delete
private $smush_parallel;
[58] Fix | Delete
/**
[59] Fix | Delete
* @var WP_Error
[60] Fix | Delete
*/
[61] Fix | Delete
private $errors;
[62] Fix | Delete
/**
[63] Fix | Delete
* @var File_System
[64] Fix | Delete
*/
[65] Fix | Delete
private $fs;
[66] Fix | Delete
/**
[67] Fix | Delete
* @var Upload_Dir
[68] Fix | Delete
*/
[69] Fix | Delete
private $upload_dir;
[70] Fix | Delete
/**
[71] Fix | Delete
* @var Server_Utils
[72] Fix | Delete
*/
[73] Fix | Delete
private $server_utils;
[74] Fix | Delete
[75] Fix | Delete
public function __construct() {
[76] Fix | Delete
$this->retry_attempts = WP_SMUSH_RETRY_ATTEMPTS;
[77] Fix | Delete
$this->retry_wait = WP_SMUSH_RETRY_WAIT;
[78] Fix | Delete
$this->user_agent = WP_SMUSH_UA;
[79] Fix | Delete
$this->smush_parallel = WP_SMUSH_PARALLEL;
[80] Fix | Delete
$this->timeout = WP_SMUSH_TIMEOUT;
[81] Fix | Delete
$this->connect_timeout = 5;
[82] Fix | Delete
[83] Fix | Delete
$this->settings = Settings::get_instance();
[84] Fix | Delete
$this->logger = Helper::logger();
[85] Fix | Delete
$this->request_multiple = new Request_Multiple();
[86] Fix | Delete
$this->backoff = new Backoff();
[87] Fix | Delete
$this->errors = new WP_Error();
[88] Fix | Delete
$this->fs = new File_System();
[89] Fix | Delete
$this->upload_dir = new Upload_Dir();
[90] Fix | Delete
$this->server_utils = new Server_Utils();
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
/**
[94] Fix | Delete
* @param $file_paths string[]
[95] Fix | Delete
*
[96] Fix | Delete
* @return boolean[]|object[]
[97] Fix | Delete
*/
[98] Fix | Delete
public function smush( $file_paths, $try_parallel = true ) {
[99] Fix | Delete
$this->set_errors( new WP_Error() );
[100] Fix | Delete
[101] Fix | Delete
if (
[102] Fix | Delete
$try_parallel
[103] Fix | Delete
&& $this->smush_parallel
[104] Fix | Delete
&& $this->parallel_available_on_server()
[105] Fix | Delete
&& $this->memory_available_for_parallel()
[106] Fix | Delete
) {
[107] Fix | Delete
return $this->smush_parallel( $file_paths );
[108] Fix | Delete
} else {
[109] Fix | Delete
return $this->smush_sequential( $file_paths );
[110] Fix | Delete
}
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
private function memory_available_for_parallel() {
[114] Fix | Delete
$memory_limit = $this->server_utils->get_memory_limit() * 0.75; // 75% of max memory
[115] Fix | Delete
$memory_limit = apply_filters( 'wp_smush_parallel_memory_cutoff', $memory_limit );
[116] Fix | Delete
$current_memory = $this->server_utils->get_memory_usage();
[117] Fix | Delete
[118] Fix | Delete
return $current_memory < $memory_limit;
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
/**
[122] Fix | Delete
* @param $file_paths string[]
[123] Fix | Delete
*
[124] Fix | Delete
* @return boolean[]|object[]
[125] Fix | Delete
*/
[126] Fix | Delete
private function smush_parallel( $file_paths ) {
[127] Fix | Delete
$retry = array();
[128] Fix | Delete
$requests = array();
[129] Fix | Delete
foreach ( $file_paths as $size_key => $size_file_path ) {
[130] Fix | Delete
$requests[ $size_key ] = $this->get_parallel_request_args( $size_file_path );
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
// Send off the valid paths to the API
[134] Fix | Delete
$responses = array();
[135] Fix | Delete
$this->request_multiple->do_requests( $requests, array(
[136] Fix | Delete
'timeout' => $this->timeout,
[137] Fix | Delete
'connect_timeout' => $this->connect_timeout,
[138] Fix | Delete
'user-agent' => $this->user_agent,
[139] Fix | Delete
'complete' => function ( $response, $response_size_key ) use ( &$requests, &$responses, &$retry, $file_paths ) {
[140] Fix | Delete
// Free up memory
[141] Fix | Delete
$requests[ $response_size_key ] = null;
[142] Fix | Delete
$size_file_path = $file_paths[ $response_size_key ];
[143] Fix | Delete
[144] Fix | Delete
if ( $this->should_retry_smush( $response ) ) {
[145] Fix | Delete
$retry[ $response_size_key ] = $size_file_path;
[146] Fix | Delete
} else {
[147] Fix | Delete
$responses[ $response_size_key ] = $this->handle_response( $response, $response_size_key, $size_file_path );
[148] Fix | Delete
}
[149] Fix | Delete
},
[150] Fix | Delete
) );
[151] Fix | Delete
[152] Fix | Delete
// Retry failures with exponential backoff
[153] Fix | Delete
foreach ( $retry as $retry_size_key => $retry_size_file ) {
[154] Fix | Delete
$responses[ $retry_size_key ] = $this->smush_file( $retry_size_file, $retry_size_key );
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
return $responses;
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
/**
[161] Fix | Delete
* @param $file_paths string[]
[162] Fix | Delete
*
[163] Fix | Delete
* @return boolean[]|object[]
[164] Fix | Delete
*/
[165] Fix | Delete
private function smush_sequential( $file_paths ) {
[166] Fix | Delete
$responses = array();
[167] Fix | Delete
foreach ( $file_paths as $size_key => $size_file_path ) {
[168] Fix | Delete
$responses[ $size_key ] = $this->smush_file( $size_file_path, $size_key );
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
return $responses;
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* @param $file_path string
[176] Fix | Delete
* @param $size_key string
[177] Fix | Delete
*
[178] Fix | Delete
* @return bool|object
[179] Fix | Delete
*/
[180] Fix | Delete
public function smush_file( $file_path, $size_key = '' ) {
[181] Fix | Delete
$response = $this->backoff->set_wait( $this->retry_wait )
[182] Fix | Delete
->set_max_attempts( $this->retry_attempts )
[183] Fix | Delete
->enable_jitter()
[184] Fix | Delete
->set_decider( array( $this, 'should_retry_smush' ) )
[185] Fix | Delete
->run( function () use ( $file_path ) {
[186] Fix | Delete
return $this->make_post_request( $file_path );
[187] Fix | Delete
} );
[188] Fix | Delete
[189] Fix | Delete
return $this->handle_response( $response, $size_key, $file_path );
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
private function make_post_request( $file_path ) {
[193] Fix | Delete
// Temporary increase the limit.
[194] Fix | Delete
wp_raise_memory_limit( 'image' );
[195] Fix | Delete
[196] Fix | Delete
return wp_remote_post(
[197] Fix | Delete
$this->get_api_url(),
[198] Fix | Delete
$this->get_api_request_args( $file_path )
[199] Fix | Delete
);
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
private function get_api_request_args( $file_path ) {
[203] Fix | Delete
return array(
[204] Fix | Delete
'headers' => $this->get_api_request_headers( $file_path ),
[205] Fix | Delete
'body' => $this->fs->file_get_contents( $file_path ),
[206] Fix | Delete
'timeout' => $this->timeout,
[207] Fix | Delete
'user-agent' => $this->user_agent,
[208] Fix | Delete
);
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
/**
[212] Fix | Delete
* @param $response
[213] Fix | Delete
* @param $size_key string
[214] Fix | Delete
* @param $file_path string
[215] Fix | Delete
*
[216] Fix | Delete
* @return bool|object
[217] Fix | Delete
*/
[218] Fix | Delete
private function handle_response( $response, $size_key, $file_path ) {
[219] Fix | Delete
$data = $this->parse_response( $response, $size_key, $file_path );
[220] Fix | Delete
[221] Fix | Delete
if ( ! $data ) {
[222] Fix | Delete
if ( $this->has_error( self::ERROR_SSL_CERT ) ) {
[223] Fix | Delete
// Switch to http protocol.
[224] Fix | Delete
$this->settings->set_setting( 'wp-smush-use_http', 1 );
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
return false;
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
if ( $data->bytes_saved > 0 ) {
[231] Fix | Delete
$optimized_image_saved = $this->save_smushed_image_file( $file_path, $data->image );
[232] Fix | Delete
if ( ! $optimized_image_saved ) {
[233] Fix | Delete
$this->add_error(
[234] Fix | Delete
$size_key,
[235] Fix | Delete
'image_not_saved',
[236] Fix | Delete
/* translators: %s: File path. */
[237] Fix | Delete
sprintf( __( 'Smush was successful but we were unable to save the file due to a file system error: [%s].', 'wp-smushit' ), $this->upload_dir->get_human_readable_path( $file_path ) )
[238] Fix | Delete
);
[239] Fix | Delete
[240] Fix | Delete
return false;
[241] Fix | Delete
}
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
// No need to pass image data any further
[245] Fix | Delete
$data->image = null;
[246] Fix | Delete
$data->image_md5 = null;
[247] Fix | Delete
[248] Fix | Delete
// Check for API message and store in db.
[249] Fix | Delete
if ( ! empty( $data->api_message ) ) {
[250] Fix | Delete
$this->add_api_message( (array) $data->api_message );
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
return $data;
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
protected function save_smushed_image_file( $file_path, $image ) {
[257] Fix | Delete
$pre = apply_filters( 'wp_smush_pre_image_write', false, $file_path, $image );
[258] Fix | Delete
if ( $pre !== false ) {
[259] Fix | Delete
$this->logger->notice( 'Another plugin/theme short circuited the image write operation using the wp_smush_pre_image_write filter.' );
[260] Fix | Delete
[261] Fix | Delete
// Assume that the plugin/theme responsible took care of it
[262] Fix | Delete
return true;
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
// Backup the old permissions
[266] Fix | Delete
$permissions = $this->get_file_permissions( $file_path );
[267] Fix | Delete
[268] Fix | Delete
// Save the new file
[269] Fix | Delete
$success = $this->put_smushed_image_file( $file_path, $image );
[270] Fix | Delete
[271] Fix | Delete
// Restore the old permissions
[272] Fix | Delete
// TODO: this is the only chmod but restoring in the comment suggests that we changed the permissions before, what are we doing?
[273] Fix | Delete
chmod( $file_path, $permissions );
[274] Fix | Delete
[275] Fix | Delete
return $success;
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
private function put_smushed_image_file( $file_path, $image ) {
[279] Fix | Delete
$temp_file = $file_path . '.tmp';
[280] Fix | Delete
[281] Fix | Delete
$success = $this->put_image_using_temp_file( $file_path, $image, $temp_file );
[282] Fix | Delete
[283] Fix | Delete
// Clean up
[284] Fix | Delete
if ( $this->fs->file_exists( $temp_file ) ) {
[285] Fix | Delete
$this->fs->unlink( $temp_file );
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
return $success;
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
private function put_image_using_temp_file( $file_path, $image, $temp_file ) {
[292] Fix | Delete
$file_written = file_put_contents( $temp_file, $image );
[293] Fix | Delete
if ( ! $file_written ) {
[294] Fix | Delete
return false;
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
$renamed = rename( $temp_file, $file_path );
[298] Fix | Delete
if ( $renamed ) {
[299] Fix | Delete
return true;
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
$copied = $this->fs->copy( $temp_file, $file_path );
[303] Fix | Delete
if ( $copied ) {
[304] Fix | Delete
return true;
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
return false;
[308] Fix | Delete
}
[309] Fix | Delete
[310] Fix | Delete
private function get_file_permissions( $file_path ) {
[311] Fix | Delete
clearstatcache();
[312] Fix | Delete
$perms = fileperms( $file_path ) & 0777;
[313] Fix | Delete
// Some servers are having issue with file permission, this should fix it.
[314] Fix | Delete
if ( empty( $perms ) ) {
[315] Fix | Delete
// Source: WordPress Core.
[316] Fix | Delete
$stat = stat( dirname( $file_path ) );
[317] Fix | Delete
$perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits.
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
return $perms;
[321] Fix | Delete
}
[322] Fix | Delete
[323] Fix | Delete
private function add_api_message( $api_message = array() ) {
[324] Fix | Delete
if ( empty( $api_message ) || ! count( $api_message ) || empty( $api_message['timestamp'] ) || empty( $api_message['message'] ) ) {
[325] Fix | Delete
return;
[326] Fix | Delete
}
[327] Fix | Delete
$o_api_message = get_site_option( 'wp-smush-api_message', array() );
[328] Fix | Delete
if ( array_key_exists( $api_message['timestamp'], $o_api_message ) ) {
[329] Fix | Delete
return;
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
$message = array();
[333] Fix | Delete
$message[ $api_message['timestamp'] ] = array(
[334] Fix | Delete
'message' => sanitize_text_field( $api_message['message'] ),
[335] Fix | Delete
'type' => sanitize_text_field( $api_message['type'] ),
[336] Fix | Delete
'status' => 'show',
[337] Fix | Delete
);
[338] Fix | Delete
update_site_option( 'wp-smush-api_message', $message );
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
/**
[342] Fix | Delete
* @param $response
[343] Fix | Delete
* @param $size_key string
[344] Fix | Delete
* @param $file_path string
[345] Fix | Delete
*
[346] Fix | Delete
* @return object|false
[347] Fix | Delete
*/
[348] Fix | Delete
private function parse_response( $response, $size_key, $file_path ) {
[349] Fix | Delete
if ( is_wp_error( $response ) ) {
[350] Fix | Delete
$error = $response->get_error_message();
[351] Fix | Delete
[352] Fix | Delete
if ( strpos( $error, 'SSL CA cert' ) !== false ) {
[353] Fix | Delete
$this->add_error( $size_key, self::ERROR_SSL_CERT, $error );
[354] Fix | Delete
[355] Fix | Delete
return false;
[356] Fix | Delete
} else if ( strpos( $error, 'timed out' ) !== false ) {
[357] Fix | Delete
$this->add_error(
[358] Fix | Delete
$size_key,
[359] Fix | Delete
'time_out',
[360] Fix | Delete
esc_html__( "Skipped due to a timeout error. You can increase the request timeout to make sure Smush has enough time to process larger files. define('WP_SMUSH_TIMEOUT', 150);", 'wp-smushit' )
[361] Fix | Delete
);
[362] Fix | Delete
[363] Fix | Delete
return false;
[364] Fix | Delete
} else {
[365] Fix | Delete
$this->add_error(
[366] Fix | Delete
$size_key,
[367] Fix | Delete
'error_posting_to_api',
[368] Fix | Delete
/* translators: %s: Error message. */
[369] Fix | Delete
sprintf( __( 'Error posting to API: %s', 'wp-smushit' ), $error )
[370] Fix | Delete
);
[371] Fix | Delete
[372] Fix | Delete
return false;
[373] Fix | Delete
}
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
[377] Fix | Delete
$error = sprintf(
[378] Fix | Delete
/* translators: 1: Error code, 2: Error message. */
[379] Fix | Delete
__( 'Error posting to API: %1$s %2$s', 'wp-smushit' ),
[380] Fix | Delete
wp_remote_retrieve_response_code( $response ),
[381] Fix | Delete
wp_remote_retrieve_response_message( $response )
[382] Fix | Delete
);
[383] Fix | Delete
[384] Fix | Delete
$this->add_error( $size_key, 'non_200_response', $error );
[385] Fix | Delete
[386] Fix | Delete
return false;
[387] Fix | Delete
}
[388] Fix | Delete
[389] Fix | Delete
$json = json_decode( wp_remote_retrieve_body( $response ) );
[390] Fix | Delete
if ( empty( $json->success ) ) {
[391] Fix | Delete
$error = ! empty( $json->data )
[392] Fix | Delete
? $json->data
[393] Fix | Delete
: __( "Image couldn't be smushed", 'wp-smushit' );
[394] Fix | Delete
[395] Fix | Delete
$this->add_error( $size_key, 'unsuccessful_smush', $error );
[396] Fix | Delete
[397] Fix | Delete
return false;
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
if (
[401] Fix | Delete
empty( $json->data )
[402] Fix | Delete
|| empty( $json->data->before_size )
[403] Fix | Delete
|| empty( $json->data->after_size )
[404] Fix | Delete
) {
[405] Fix | Delete
$this->add_error( $size_key, 'no_data', __( 'Unknown API error', 'wp-smushit' ) );
[406] Fix | Delete
[407] Fix | Delete
return false;
[408] Fix | Delete
}
[409] Fix | Delete
[410] Fix | Delete
$data = $json->data;
[411] Fix | Delete
$data->bytes_saved = isset( $data->bytes_saved ) ? (int) $data->bytes_saved : 0;
[412] Fix | Delete
$optimized_image_larger = $data->after_size > $data->before_size;
[413] Fix | Delete
if ( $optimized_image_larger ) {
[414] Fix | Delete
$this->add_error(
[415] Fix | Delete
$size_key,
[416] Fix | Delete
'optimized_image_larger',
[417] Fix | Delete
/* translators: 1: File path, 2: Savings bytes. */
[418] Fix | Delete
sprintf( 'The smushed image is larger than the original image [%s] (bytes saved %d), keep original image.', $this->upload_dir->get_human_readable_path( $file_path ), $data->bytes_saved )
[419] Fix | Delete
);
[420] Fix | Delete
[421] Fix | Delete
return false;
[422] Fix | Delete
}
[423] Fix | Delete
[424] Fix | Delete
$image = empty( $data->image ) ? '' : $data->image;
[425] Fix | Delete
if ( $data->bytes_saved > 0 ) {
[426] Fix | Delete
// Because of the API response structure, the following should only be done when there are some bytes_saved.
[427] Fix | Delete
[428] Fix | Delete
if ( $data->image_md5 !== md5( $image ) ) {
[429] Fix | Delete
$error = __( 'Smush data corrupted, try again.', 'wp-smushit' );
[430] Fix | Delete
$this->add_error( $size_key, 'data_corrupted', $error );
[431] Fix | Delete
[432] Fix | Delete
return false;
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
if ( ! empty( $image ) ) {
[436] Fix | Delete
$data->image = base64_decode( $data->image );
[437] Fix | Delete
}
[438] Fix | Delete
}
[439] Fix | Delete
[440] Fix | Delete
return $data;
[441] Fix | Delete
}
[442] Fix | Delete
[443] Fix | Delete
public function should_retry_smush( $response ) {
[444] Fix | Delete
return $this->retry_attempts > 0 && (
[445] Fix | Delete
is_wp_error( $response )
[446] Fix | Delete
|| 200 !== wp_remote_retrieve_response_code( $response )
[447] Fix | Delete
);
[448] Fix | Delete
}
[449] Fix | Delete
[450] Fix | Delete
private function get_parallel_request_args( $file_path ) {
[451] Fix | Delete
return array(
[452] Fix | Delete
'url' => $this->get_api_url(),
[453] Fix | Delete
'headers' => $this->get_api_request_headers( $file_path ),
[454] Fix | Delete
'data' => $this->fs->file_get_contents( $file_path ),
[455] Fix | Delete
'type' => 'POST',
[456] Fix | Delete
);
[457] Fix | Delete
}
[458] Fix | Delete
[459] Fix | Delete
/**
[460] Fix | Delete
* @return string
[461] Fix | Delete
*/
[462] Fix | Delete
private function get_api_url() {
[463] Fix | Delete
return defined( 'WP_SMUSH_API_HTTP' ) ? WP_SMUSH_API_HTTP : WP_SMUSH_API;
[464] Fix | Delete
}
[465] Fix | Delete
[466] Fix | Delete
/**
[467] Fix | Delete
* @return string[]
[468] Fix | Delete
*/
[469] Fix | Delete
protected function get_api_request_headers( $file_path ) {
[470] Fix | Delete
$headers = array(
[471] Fix | Delete
'accept' => 'application/json', // The API returns JSON.
[472] Fix | Delete
'content-type' => 'application/binary', // Set content type to binary.
[473] Fix | Delete
'exif' => $this->settings->get( 'strip_exif' ) ? 'false' : 'true',
[474] Fix | Delete
);
[475] Fix | Delete
[476] Fix | Delete
$headers['lossy'] = $this->settings->get_lossy_level_setting();
[477] Fix | Delete
[478] Fix | Delete
// Check if premium member, add API key.
[479] Fix | Delete
$api_key = Helper::get_wpmudev_apikey();
[480] Fix | Delete
if ( ! empty( $api_key ) && WP_Smush::is_pro() ) {
[481] Fix | Delete
$headers['apikey'] = $api_key;
[482] Fix | Delete
[483] Fix | Delete
$is_large_file = $this->is_large_file( $file_path );
[484] Fix | Delete
if ( $is_large_file ) {
[485] Fix | Delete
$headers['islarge'] = 1;
[486] Fix | Delete
}
[487] Fix | Delete
}
[488] Fix | Delete
[489] Fix | Delete
return $headers;
[490] Fix | Delete
}
[491] Fix | Delete
[492] Fix | Delete
private function is_large_file( $file_path ) {
[493] Fix | Delete
$file_size = file_exists( $file_path ) ? filesize( $file_path ) : 0;
[494] Fix | Delete
$cut_off = $this->settings->get_large_file_cutoff();
[495] Fix | Delete
[496] Fix | Delete
return $file_size > $cut_off;
[497] Fix | Delete
}
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function