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/integrat...
File: class-s3.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* S3 integration: S3 class
[2] Fix | Delete
*
[3] Fix | Delete
* Minimum supported version - Offload Media 2.4
[4] Fix | Delete
*
[5] Fix | Delete
* @package Smush\Core\Modules\Integrations
[6] Fix | Delete
* @subpackage S3
[7] Fix | Delete
* @since 2.7
[8] Fix | Delete
*
[9] Fix | Delete
* @author Umesh Kumar <umesh@incsub.com>
[10] Fix | Delete
*
[11] Fix | Delete
* @copyright (c) 2017, Incsub (http://incsub.com)
[12] Fix | Delete
*/
[13] Fix | Delete
[14] Fix | Delete
namespace Smush\Core\Integrations;
[15] Fix | Delete
[16] Fix | Delete
use Amazon_S3_And_CloudFront;
[17] Fix | Delete
use DeliciousBrains\WP_Offload_Media\Items\Media_Library_Item;
[18] Fix | Delete
use Smush\App\Admin;
[19] Fix | Delete
use Smush\Core\Helper;
[20] Fix | Delete
use Smush\Core\Settings;
[21] Fix | Delete
use WP_Smush;
[22] Fix | Delete
[23] Fix | Delete
if ( ! defined( 'WPINC' ) ) {
[24] Fix | Delete
die;
[25] Fix | Delete
}
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Class S3
[29] Fix | Delete
*/
[30] Fix | Delete
class S3 extends Abstract_Integration {
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Save list of files need to remove
[34] Fix | Delete
* when user enabling "Remove Files From Server".
[35] Fix | Delete
*
[36] Fix | Delete
* @var array
[37] Fix | Delete
*/
[38] Fix | Delete
private $files_to_remove = array();
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* Cache list of files download failed.
[42] Fix | Delete
*
[43] Fix | Delete
* @var array
[44] Fix | Delete
*/
[45] Fix | Delete
private $files_download_failed = array();
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* On Smush mode, we will disable auto download and auto-upload
[49] Fix | Delete
* while working with Smush.
[50] Fix | Delete
*
[51] Fix | Delete
* @var boolean
[52] Fix | Delete
*/
[53] Fix | Delete
private $on_smush_mode;
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Cache list of files to delete via remove_file method.
[57] Fix | Delete
*
[58] Fix | Delete
* @var array
[59] Fix | Delete
*/
[60] Fix | Delete
private $doing_files;
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Cache list of filters for get_attached_file.
[64] Fix | Delete
*
[65] Fix | Delete
* @var null|array
[66] Fix | Delete
*/
[67] Fix | Delete
private $list_file_filters;
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* S3 constructor.
[71] Fix | Delete
*/
[72] Fix | Delete
public function __construct() {
[73] Fix | Delete
$this->module = 's3';
[74] Fix | Delete
$this->class = 'pro';
[75] Fix | Delete
$this->enabled = function_exists( 'as3cf_init' ) || function_exists( 'as3cf_pro_init' );
[76] Fix | Delete
[77] Fix | Delete
parent::__construct();
[78] Fix | Delete
[79] Fix | Delete
// Hook at the end of setting row to output a error div.
[80] Fix | Delete
add_action( 'smush_setting_column_right_inside', array( $this, 's3_setup_message' ), 15 );
[81] Fix | Delete
[82] Fix | Delete
// Show S3 integration message, if user hasn't enabled it.
[83] Fix | Delete
add_action( 'wp_smush_header_notices', array( $this, 'show_s3_support_required_notice' ) );
[84] Fix | Delete
[85] Fix | Delete
// Add Pro tag.
[86] Fix | Delete
if ( ! WP_Smush::is_pro() || ! $this->enabled ) {
[87] Fix | Delete
add_action( 'smush_setting_column_tag', array( $this, 'add_pro_tag' ) );
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
// Do not continue if not enabled or S3 Offload plugin is not installed.
[91] Fix | Delete
if ( ! $this->is_active() ) {
[92] Fix | Delete
return;
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
// Load all our custom actions/filters after loading as3cf.
[96] Fix | Delete
if ( did_action( 'as3cf_init' ) ) {
[97] Fix | Delete
$this->init();
[98] Fix | Delete
} else {
[99] Fix | Delete
add_action( 'as3cf_init', array( $this, 'init' ) );
[100] Fix | Delete
}
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Register actions, filters for S3.
[105] Fix | Delete
*
[106] Fix | Delete
* @since 3.9.6
[107] Fix | Delete
*/
[108] Fix | Delete
public function init() {
[109] Fix | Delete
// TODO: (stats refactor) we still need some of the stuff from this controller e.g. notices
[110] Fix | Delete
return;
[111] Fix | Delete
[112] Fix | Delete
global $as3cf;
[113] Fix | Delete
[114] Fix | Delete
// Check file exists.
[115] Fix | Delete
add_filter( 'wp_smush_file_exists', array( $this, 'file_exists_on_s3' ), 10, 4 );
[116] Fix | Delete
[117] Fix | Delete
// Get attached file.
[118] Fix | Delete
add_filter( 'wp_smush_get_attached_file', array( $this, 'get_attached_file' ), 10, 5 );
[119] Fix | Delete
[120] Fix | Delete
// Check unique file with the backup file.
[121] Fix | Delete
add_filter( 'wp_unique_filename', array( $this, 'filter_unique_filename' ), 99, 3 );// S3 is using priority 10.
[122] Fix | Delete
[123] Fix | Delete
// Check if the file exists for the given path and download.
[124] Fix | Delete
add_action( 'wp_smush_before_restore_backup', array( $this, 'maybe_download_file' ), 10, 2 );
[125] Fix | Delete
// Update original source path of as3cf_items after converting PNG to JPG.
[126] Fix | Delete
add_action( 'wp_smush_image_url_changed', array( $this, 'update_original_source_path_after_png2jpg' ), 10, 4 );
[127] Fix | Delete
// Update original source path of as3cf_items after restore the converted PNG2JPG file.
[128] Fix | Delete
add_action( 'wp_smush_image_url_updated', array( $this, 'update_original_source_path_after_restore_png' ), 10, 3 );
[129] Fix | Delete
[130] Fix | Delete
// If user is enabling copy to S3.
[131] Fix | Delete
if ( $as3cf->get_setting( 'copy-to-s3' ) ) {
[132] Fix | Delete
/**
[133] Fix | Delete
* Activate Smush mode (Disable auto download and upload attachments).
[134] Fix | Delete
*
[135] Fix | Delete
* Note, we managed to release this mode by using Helper::wp_update_attachment_metadata() or
[136] Fix | Delete
* via action "wp_smush_after_smush_file"
[137] Fix | Delete
*/
[138] Fix | Delete
// Activate smush mode before smushing/restoring.
[139] Fix | Delete
add_action( 'wp_smush_before_smush_file', array( $this, 'activate_smush_mode' ), 1 );
[140] Fix | Delete
add_action( 'wp_smush_before_restore_backup', array( $this, 'activate_smush_mode' ), 1 );
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* When WP create a new attachment, S3 will try to upload it to the server,
[144] Fix | Delete
* and then it will upload the thumbnails later after regenerating the thumbnails.
[145] Fix | Delete
* So we use this filter to temporary disable upload if smush can do with this file.
[146] Fix | Delete
*
[147] Fix | Delete
* Note, if we don't work with the current image, e.g animated file,
[148] Fix | Delete
* we managed to release this mode by using maybe_release_smush_mode.
[149] Fix | Delete
*/
[150] Fix | Delete
add_filter( 'wp_update_attachment_metadata', array( $this, 'maybe_active_smush_mode' ), 1, 2 );
[151] Fix | Delete
[152] Fix | Delete
// Reset smush mode after smushing/restoring.
[153] Fix | Delete
add_action( 'wp_smush_after_smush_file', array( $this, 'release_smush_mode' ) );
[154] Fix | Delete
add_action( 'wp_smush_after_restore_backup', array( $this, 'release_smush_mode' ) );
[155] Fix | Delete
/**
[156] Fix | Delete
* Make sure we exit Smush mode before updating the metadata,
[157] Fix | Delete
* this will help we to upload the attachments, and remove them if it's required.
[158] Fix | Delete
*/
[159] Fix | Delete
add_action( 'wp_smush_before_update_attachment_metadata', array( $this, 'release_smush_mode' ) );
[160] Fix | Delete
// Release mode when we don't smush the image.
[161] Fix | Delete
add_action( 'wp_smush_no_smushit', array( $this, 'maybe_release_smush_mode' ) );
[162] Fix | Delete
[163] Fix | Delete
// Remove .bak file after restoring, and JPG files after restoring PNG file.
[164] Fix | Delete
add_action( 'wp_smush_after_remove_file', array( $this, 'remove_file' ), 10, 3 );
[165] Fix | Delete
// Make sure we removed all downloaded files.
[166] Fix | Delete
add_filter( 'shutdown', array( $this, 'maybe_remove_downloaded_files' ), 10, 2 );
[167] Fix | Delete
}
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
/**
[171] Fix | Delete
* Disable module functionality if not PRO.
[172] Fix | Delete
*
[173] Fix | Delete
* @return bool
[174] Fix | Delete
*/
[175] Fix | Delete
public function setting_status() {
[176] Fix | Delete
return ! WP_Smush::is_pro() ? true : ! $this->enabled;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
/**************************************
[180] Fix | Delete
*
[181] Fix | Delete
* OVERWRITE PARENT CLASS FUNCTIONALITY
[182] Fix | Delete
*/
[183] Fix | Delete
[184] Fix | Delete
/**
[185] Fix | Delete
* Filters the setting variable to add S3 setting title and description.
[186] Fix | Delete
*
[187] Fix | Delete
* @param array $settings Settings array.
[188] Fix | Delete
*
[189] Fix | Delete
* @return array
[190] Fix | Delete
*/
[191] Fix | Delete
public function register( $settings ) {
[192] Fix | Delete
$plugin_url = esc_url( 'https://wordpress.org/plugins/amazon-s3-and-cloudfront/' );
[193] Fix | Delete
$settings[ $this->module ] = array(
[194] Fix | Delete
'label' => __( 'Enable Amazon S3 support', 'wp-smushit' ),
[195] Fix | Delete
'short_label' => __( 'Amazon S3', 'wp-smushit' ),
[196] Fix | Delete
'desc' => sprintf( /* translators: %1$s - <a>, %2$s - </a> */
[197] Fix | Delete
esc_html__(
[198] Fix | Delete
"Storing your image on S3 buckets using %1\$sWP Offload Media%2\$s? Smush can detect and smush those assets for you, including when you're removing files from your host server.",
[199] Fix | Delete
'wp-smushit'
[200] Fix | Delete
),
[201] Fix | Delete
"<a href='$plugin_url' target = '_blank'>",
[202] Fix | Delete
'</a>'
[203] Fix | Delete
),
[204] Fix | Delete
);
[205] Fix | Delete
[206] Fix | Delete
return $settings;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
/**************************************
[210] Fix | Delete
*
[211] Fix | Delete
* PUBLIC CLASSES
[212] Fix | Delete
*/
[213] Fix | Delete
[214] Fix | Delete
/**
[215] Fix | Delete
* Check if the file is served by S3 and download the file for given path
[216] Fix | Delete
*
[217] Fix | Delete
* @param string $file_path Full file path.
[218] Fix | Delete
* @param string $attachment_id Attachment ID.
[219] Fix | Delete
*
[220] Fix | Delete
* @return bool|string False/ File Path
[221] Fix | Delete
*/
[222] Fix | Delete
public function maybe_download_file( $file_path, $attachment_id ) {
[223] Fix | Delete
// Download the backup file if it doesn't exist on the server.
[224] Fix | Delete
return $this->download_file( $file_path, $attachment_id );
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
/**
[228] Fix | Delete
* Checks if the given attachment is on S3 or not, Returns S3 URL or WP Error
[229] Fix | Delete
*
[230] Fix | Delete
* @param string $attachment_id Attachment ID.
[231] Fix | Delete
*
[232] Fix | Delete
* @return bool
[233] Fix | Delete
*/
[234] Fix | Delete
public function is_image_on_s3( $attachment_id = '' ) {
[235] Fix | Delete
if ( empty( $attachment_id ) ) {
[236] Fix | Delete
return false;
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
// If the file path contains S3, get the s3 URL for the file.
[240] Fix | Delete
if ( function_exists( 'as3cf_get_attachment_url' ) ) {
[241] Fix | Delete
return as3cf_get_attachment_url( $attachment_id );
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
Helper::logger()->integrations()->error( 'S3 - Function as3cf_get_attachment_url does not exists.' );
[245] Fix | Delete
return false;
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
/**
[249] Fix | Delete
* Checks if file exits on S3.
[250] Fix | Delete
*
[251] Fix | Delete
* @param bool $exists If file exists on S3.
[252] Fix | Delete
* @param mixed $file_path File path or empty value.
[253] Fix | Delete
* @param string $attachment_id Attachment ID.
[254] Fix | Delete
* @param bool $should_download Should download attachment.
[255] Fix | Delete
*
[256] Fix | Delete
* @return bool|string Returns TRUE OR File path if it exists, FALSE otherwise.
[257] Fix | Delete
*/
[258] Fix | Delete
public function file_exists_on_s3( $exists, $file_path, $attachment_id, $should_download ) {
[259] Fix | Delete
if ( ! $exists ) {
[260] Fix | Delete
if ( empty( $file_path ) ) {
[261] Fix | Delete
// Maybe file is not uploaded to provider, try to get the raw file path.
[262] Fix | Delete
$file_path = $this->get_raw_attached_file( $attachment_id );
[263] Fix | Delete
}
[264] Fix | Delete
$exists = file_exists( $file_path );
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
if ( ! $exists ) {
[268] Fix | Delete
if ( $should_download ) {
[269] Fix | Delete
$exists = $this->download_file( $file_path, $attachment_id );
[270] Fix | Delete
} else {
[271] Fix | Delete
$exists = $this->does_image_exists( $attachment_id, $file_path );
[272] Fix | Delete
}
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
return $exists;
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
/**
[279] Fix | Delete
* Error message to show when S3 support is required.
[280] Fix | Delete
*
[281] Fix | Delete
* Show a error message to admins, if they need to enable S3 support. If "remove files from
[282] Fix | Delete
* server" option is enabled in WP Offload Media plugin, we need WP Smush Pro to enable S3 support.
[283] Fix | Delete
*/
[284] Fix | Delete
public function show_s3_support_required_notice() {
[285] Fix | Delete
// Do not display it for other users. Do not display on network screens, if network-wide option is disabled.
[286] Fix | Delete
if ( ! current_user_can( 'manage_options' ) || ! Settings::can_access( 'integrations' ) ) {
[287] Fix | Delete
return;
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
// If already dismissed, do not show.
[291] Fix | Delete
if ( '1' === get_site_option( 'wp-smush-hide_s3support_alert' ) ) {
[292] Fix | Delete
return;
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
// Return early, if support is not required.
[296] Fix | Delete
if ( ! $this->s3_support_required() ) {
[297] Fix | Delete
return;
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
// Settings link.
[301] Fix | Delete
$settings_link = is_multisite() && is_network_admin()
[302] Fix | Delete
? network_admin_url( 'admin.php?page=smush-integrations' )
[303] Fix | Delete
: menu_page_url( 'smush-integrations', false );
[304] Fix | Delete
[305] Fix | Delete
if ( WP_Smush::is_pro() ) {
[306] Fix | Delete
/**
[307] Fix | Delete
* If premium user, but S3 support is not enabled.
[308] Fix | Delete
*/
[309] Fix | Delete
$message = sprintf(
[310] Fix | Delete
/* Translators: %1$s: opening strong tag, %2$s: closing strong tag, %s: settings link, %3$s: opening a and strong tags, %4$s: closing a and strong tags */
[311] Fix | Delete
__(
[312] Fix | Delete
'We can see you have WP Offload Media installed. If you want to optimize your S3 images, you’ll need to enable the %3$sAmazon S3 Support%4$s feature in Smush’s Integrations.',
[313] Fix | Delete
'wp-smushit'
[314] Fix | Delete
),
[315] Fix | Delete
'<strong>',
[316] Fix | Delete
'</strong>',
[317] Fix | Delete
"<a href='$settings_link'><strong>",
[318] Fix | Delete
'</strong></a>'
[319] Fix | Delete
);
[320] Fix | Delete
} else {
[321] Fix | Delete
/**
[322] Fix | Delete
* If not a premium user.
[323] Fix | Delete
*/
[324] Fix | Delete
$message = sprintf(
[325] Fix | Delete
/* Translators: %1$s: opening strong tag, %2$s: closing strong tag, %s: settings link, %3$s: opening a and strong tags, %4$s: closing a and strong tags */
[326] Fix | Delete
__(
[327] Fix | Delete
"We can see you have WP Offload Media installed. If you want to optimize your S3 images you'll need to %3\$supgrade to Smush Pro%4\$s",
[328] Fix | Delete
'wp-smushit'
[329] Fix | Delete
),
[330] Fix | Delete
'<strong>',
[331] Fix | Delete
'</strong>',
[332] Fix | Delete
'<a href=' . esc_url( 'https://wpmudev.com/project/wp-smush-pro' ) . '><strong>',
[333] Fix | Delete
'</strong></a>'
[334] Fix | Delete
);
[335] Fix | Delete
}
[336] Fix | Delete
$message = '<p>' . $message . '</p>';
[337] Fix | Delete
echo '<div role="alert" id="wp-smush-s3support-alert" class="sui-notice" data-message="' . esc_attr( $message ) . '" aria-live="assertive"></div>';
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
/**
[341] Fix | Delete
* Prints the message for S3 setup
[342] Fix | Delete
*
[343] Fix | Delete
* @param string $setting_key Settings key.
[344] Fix | Delete
*/
[345] Fix | Delete
public function s3_setup_message( $setting_key ) {
[346] Fix | Delete
// Return if not S3.
[347] Fix | Delete
if ( $this->module !== $setting_key ) {
[348] Fix | Delete
return;
[349] Fix | Delete
}
[350] Fix | Delete
[351] Fix | Delete
/**
[352] Fix | Delete
* Amazon_S3_And_CloudFront global.
[353] Fix | Delete
*
[354] Fix | Delete
* @var Amazon_S3_And_CloudFront $as3cf
[355] Fix | Delete
*/
[356] Fix | Delete
global $as3cf;
[357] Fix | Delete
[358] Fix | Delete
// If S3 integration is not enabled, return.
[359] Fix | Delete
$setting_val = WP_Smush::is_pro() ? $this->settings->get( $this->module ) : 0;
[360] Fix | Delete
[361] Fix | Delete
// If integration is disabled when S3 offload is active, do not continue.
[362] Fix | Delete
if ( ! $setting_val && is_object( $as3cf ) ) {
[363] Fix | Delete
return;
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
// If S3 offload global variable is not available, plugin is not active.
[367] Fix | Delete
if ( ! is_object( $as3cf ) ) {
[368] Fix | Delete
$class = '';
[369] Fix | Delete
$message = __( 'To use this feature you need to install WP Offload Media and have an Amazon S3 account setup.', 'wp-smushit' );
[370] Fix | Delete
} elseif ( ! method_exists( $as3cf, 'is_plugin_setup' ) || ! method_exists( $as3cf, 'get_plugin_page_url' ) ) {
[371] Fix | Delete
// Check if in case for some reason, we couldn't find the required function.
[372] Fix | Delete
$class = ' sui-notice-warning';
[373] Fix | Delete
$message = sprintf( /* translators: %1$s: opening a tag, %2$s: closing a tag */
[374] Fix | Delete
esc_html__(
[375] Fix | Delete
'We are having trouble interacting with WP Offload Media, make sure the plugin is activated. Or you can %1$sreport a bug%2$s.',
[376] Fix | Delete
'wp-smushit'
[377] Fix | Delete
),
[378] Fix | Delete
'<a href="' . esc_url( 'https://wpmudev.com/contact' ) . '" target="_blank">',
[379] Fix | Delete
'</a>'
[380] Fix | Delete
);
[381] Fix | Delete
} elseif ( ! $as3cf->is_plugin_setup() ) {
[382] Fix | Delete
// Plugin is not setup, or some information is missing.
[383] Fix | Delete
$class = ' sui-notice-warning';
[384] Fix | Delete
$message = sprintf( /* translators: %1$s: opening a tag, %2$s: closing a tag */
[385] Fix | Delete
esc_html__(
[386] Fix | Delete
'It seems you haven’t finished setting up WP Offload Media yet. %1$sConfigure it now%2$s to enable Amazon S3 support.',
[387] Fix | Delete
'wp-smushit'
[388] Fix | Delete
),
[389] Fix | Delete
'<a href="' . $as3cf->get_plugin_page_url() . '" target="_blank">',
[390] Fix | Delete
'</a>'
[391] Fix | Delete
);
[392] Fix | Delete
} else {
[393] Fix | Delete
// S3 support is active.
[394] Fix | Delete
$class = ' sui-notice-info';
[395] Fix | Delete
$message = __( 'Amazon S3 support is active.', 'wp-smushit' );
[396] Fix | Delete
}
[397] Fix | Delete
?>
[398] Fix | Delete
<div class="sui-toggle-content">
[399] Fix | Delete
<div class="sui-notice<?php echo esc_attr( $class ); ?>">
[400] Fix | Delete
<div class="sui-notice-content">
[401] Fix | Delete
<div class="sui-notice-message">
[402] Fix | Delete
<i class="sui-notice-icon sui-icon-info" aria-hidden="true"></i>
[403] Fix | Delete
<p><?php echo wp_kses_post( $message ); ?></p>
[404] Fix | Delete
</div>
[405] Fix | Delete
</div>
[406] Fix | Delete
</div>
[407] Fix | Delete
</div>
[408] Fix | Delete
<?php
[409] Fix | Delete
}
[410] Fix | Delete
[411] Fix | Delete
/**
[412] Fix | Delete
* Add a pro tag next to the setting title.
[413] Fix | Delete
*
[414] Fix | Delete
* @param string $setting_key Setting key name.
[415] Fix | Delete
*
[416] Fix | Delete
* @since 3.4.0
[417] Fix | Delete
*/
[418] Fix | Delete
public function add_pro_tag( $setting_key ) {
[419] Fix | Delete
// Return if not NextGen integration.
[420] Fix | Delete
if ( $this->module !== $setting_key || WP_Smush::is_pro() ) {
[421] Fix | Delete
return;
[422] Fix | Delete
}
[423] Fix | Delete
?>
[424] Fix | Delete
<span class="sui-tag sui-tag-pro">
[425] Fix | Delete
<?php esc_html_e( 'Pro', 'wp-smushit' ); ?>
[426] Fix | Delete
</span>
[427] Fix | Delete
<?php
[428] Fix | Delete
}
[429] Fix | Delete
[430] Fix | Delete
/**
[431] Fix | Delete
* Disable auto downloading the file to local while Smush doing, only allow it with our custom methods:
[432] Fix | Delete
* Helper::get_attached_file( $attachment_id, $type='smush|resize|original')
[433] Fix | Delete
* or Helper::exists_or_downloaded( $file, $attachment_id ),
[434] Fix | Delete
* and set wait_for_generate_attachment_metadata is TRUE to disable auto upload attachments while
[435] Fix | Delete
* we are working with it.
[436] Fix | Delete
*
[437] Fix | Delete
* @since 3.4.0
[438] Fix | Delete
*
[439] Fix | Delete
* @since 3.9.6
[440] Fix | Delete
* Enable copy file back to local and wait_for_generate_attachment_metadata while we working with smush or restore.
[441] Fix | Delete
*/
[442] Fix | Delete
public function activate_smush_mode() {
[443] Fix | Delete
if ( $this->is_active() && ! $this->on_smush_mode ) {
[444] Fix | Delete
global $as3cf;
[445] Fix | Delete
// Save mode.
[446] Fix | Delete
$this->on_smush_mode = true;
[447] Fix | Delete
/**
[448] Fix | Delete
* Disable auto download to local, we only enable it via self::downoad_file method to avoid automatic download link.
[449] Fix | Delete
* e.g. wp_attachment_is_image()
[450] Fix | Delete
*/
[451] Fix | Delete
add_filter( 'as3cf_get_attached_file_copy_back_to_local', '__return_false', 9998 );
[452] Fix | Delete
// Disable auto upload attachments.
[453] Fix | Delete
add_filter( 'as3cf_wait_for_generate_attachment_metadata', '__return_true', 9998 );
[454] Fix | Delete
[455] Fix | Delete
// If user enabling remove file on local.
[456] Fix | Delete
if ( $as3cf && $as3cf->get_setting( 'remove-local-file' ) ) {
[457] Fix | Delete
// We don't remove the filter because it might be called later.
[458] Fix | Delete
add_filter( 'as3cf_upload_attachment_local_files_to_remove', array( $this, 'remove_missing_files_to_avoid_error_log_from_s3' ), 99 );
[459] Fix | Delete
}
[460] Fix | Delete
}
[461] Fix | Delete
}
[462] Fix | Delete
[463] Fix | Delete
/**
[464] Fix | Delete
* Set Smush mode while creating a new image.
[465] Fix | Delete
*
[466] Fix | Delete
* @param array $image_meta Image meta.
[467] Fix | Delete
* @param int $attachment_id Attachment ID.
[468] Fix | Delete
* @return array The provided metadata.
[469] Fix | Delete
*/
[470] Fix | Delete
public function maybe_active_smush_mode( $image_meta, $attachment_id ) {
[471] Fix | Delete
if (
[472] Fix | Delete
! $this->on_smush_mode
[473] Fix | Delete
// When async uploading or the image is created from Gutenberg, activate smush mode.
[474] Fix | Delete
&& empty( $new_meta['sizes'] )
[475] Fix | Delete
&& ! empty( $image_meta['file'] )
[476] Fix | Delete
&& ( isset( $_POST['post_id'] ) || isset( $_FILES['async-upload'] ) || ! Helper::is_non_rest_media() )
[477] Fix | Delete
// If enabling auto-smush.
[478] Fix | Delete
&& $this->settings->get( 'auto' )
[479] Fix | Delete
// Managed it.
[480] Fix | Delete
&& ! did_action( 'wp_smush_before_smush_file' )
[481] Fix | Delete
// Managed it.
[482] Fix | Delete
&& ! did_action( 'wp_smush_before_restore_backup' )
[483] Fix | Delete
// Managed it.
[484] Fix | Delete
&& ! did_action( 'wp_smush_before_update_attachment_metadata' )
[485] Fix | Delete
// Don't smush it.
[486] Fix | Delete
&& ! did_action( 'wp_smush_no_smushit' )
[487] Fix | Delete
// Only support Image.
[488] Fix | Delete
&& Helper::is_smushable( $attachment_id )
[489] Fix | Delete
// Make sure we only disable while async upload new image.
[490] Fix | Delete
&& ! $this->does_image_exists( $attachment_id, $this->get_raw_attached_file( $attachment_id, 'original' ) )
[491] Fix | Delete
) {
[492] Fix | Delete
$this->activate_smush_mode();
[493] Fix | Delete
}
[494] Fix | Delete
[495] Fix | Delete
return $image_meta;
[496] Fix | Delete
}
[497] Fix | Delete
[498] Fix | Delete
/**
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function