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-common.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Smush integration with various plugins: Common class
[2] Fix | Delete
*
[3] Fix | Delete
* @package Smush\Core\Integrations
[4] Fix | Delete
* @since 2.8.0
[5] Fix | Delete
*
[6] Fix | Delete
* @author Anton Vanyukov <anton@incsub.com>
[7] Fix | Delete
*
[8] Fix | Delete
* @copyright (c) 2018, Incsub (http://incsub.com)
[9] Fix | Delete
*/
[10] Fix | Delete
[11] Fix | Delete
namespace Smush\Core\Integrations;
[12] Fix | Delete
[13] Fix | Delete
use Smush\Core\CDN\CDN_Helper;
[14] Fix | Delete
use Smush\Core\Helper;
[15] Fix | Delete
use Smush\Core\Modules\Helpers\Parser;
[16] Fix | Delete
use Smush\Core\Modules\Smush;
[17] Fix | Delete
use WP_Smush;
[18] Fix | Delete
[19] Fix | Delete
if ( ! defined( 'WPINC' ) ) {
[20] Fix | Delete
die;
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Singleton class class Common
[25] Fix | Delete
*
[26] Fix | Delete
* @since 2.8.0
[27] Fix | Delete
*/
[28] Fix | Delete
class Common {
[29] Fix | Delete
private $cdn_helper;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Common constructor.
[33] Fix | Delete
*/
[34] Fix | Delete
public function __construct() {
[35] Fix | Delete
if ( is_admin() ) {
[36] Fix | Delete
// AJAX Thumbnail Rebuild integration.
[37] Fix | Delete
add_filter( 'wp_smush_media_image', array( $this, 'skip_images' ), 10, 2 );
[38] Fix | Delete
[39] Fix | Delete
// Optimise WP retina 2x images.
[40] Fix | Delete
add_action( 'wr2x_retina_file_added', array( $this, 'smush_retina_image' ), 20, 3 );
[41] Fix | Delete
[42] Fix | Delete
// Remove any pre_get_posts_filters added by WP Media Folder plugin.
[43] Fix | Delete
add_action( 'wp_smush_remove_filters', array( $this, 'remove_filters' ) );
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
// WPML integration.
[47] Fix | Delete
add_action( 'wpml_updated_attached_file', array( $this, 'wpml_undo_ignore_attachment' ) );
[48] Fix | Delete
add_action( 'wpml_after_duplicate_attachment', array( $this, 'wpml_ignore_duplicate_attachment' ), 10, 2 );
[49] Fix | Delete
add_action( 'wpml_after_copy_attached_file_postmeta', array( $this, 'wpml_ignore_duplicate_attachment' ), 10, 2 );
[50] Fix | Delete
[51] Fix | Delete
// ReCaptcha lazy load.
[52] Fix | Delete
add_filter( 'smush_skip_iframe_from_lazy_load', array( $this, 'exclude_recaptcha_iframe' ), 10, 2 );
[53] Fix | Delete
[54] Fix | Delete
// Soliloquy slider CDN support.
[55] Fix | Delete
add_filter( 'soliloquy_image_src', array( $this, 'soliloquy_image_src' ) );
[56] Fix | Delete
[57] Fix | Delete
// Translate Press integration.
[58] Fix | Delete
add_filter( 'smush_skip_image_from_lazy_load', array( $this, 'trp_translation_editor' ) );
[59] Fix | Delete
[60] Fix | Delete
// WP Maintenance Plugin integration.
[61] Fix | Delete
add_action( 'template_redirect', array( $this, 'wp_maintenance_mode' ) );
[62] Fix | Delete
[63] Fix | Delete
// Buddyboss theme and its platform plugin integration.
[64] Fix | Delete
add_filter( 'wp_smush_cdn_before_process_src', array( $this, 'buddyboss_platform_modify_image_src' ), 10, 2 );
[65] Fix | Delete
[66] Fix | Delete
// GiveWP donation form load lazyload images in iframe.
[67] Fix | Delete
add_action( 'give_donation_form_top', array( $this, 'givewp_skip_image_lazy_load' ), 0 );
[68] Fix | Delete
[69] Fix | Delete
// Thumbnail regeneration handler.
[70] Fix | Delete
add_filter( 'wp_generate_attachment_metadata', array( $this, 'maybe_handle_thumbnail_generation' ) );
[71] Fix | Delete
$this->cdn_helper = CDN_Helper::get_instance();
[72] Fix | Delete
[73] Fix | Delete
add_filter( 'wp_smush_should_transform_page', array( $this, 'should_transform_page' ) );
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Remove any pre_get_posts_filters added by WP Media Folder plugin.
[78] Fix | Delete
*/
[79] Fix | Delete
public function remove_filters() {
[80] Fix | Delete
// Remove any filters added b WP media Folder plugin to get the all attachments.
[81] Fix | Delete
if ( class_exists( 'Wp_Media_Folder' ) ) {
[82] Fix | Delete
global $wp_media_folder;
[83] Fix | Delete
if ( is_object( $wp_media_folder ) ) {
[84] Fix | Delete
remove_filter( 'pre_get_posts', array( $wp_media_folder, 'wpmf_pre_get_posts1' ) );
[85] Fix | Delete
remove_filter( 'pre_get_posts', array( $wp_media_folder, 'wpmf_pre_get_posts' ), 0, 1 );
[86] Fix | Delete
}
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
global $wpml_query_filter;
[90] Fix | Delete
[91] Fix | Delete
// If WPML is not installed, return.
[92] Fix | Delete
if ( ! is_object( $wpml_query_filter ) ) {
[93] Fix | Delete
return;
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
// Remove language filter and let all the images be smushed at once.
[97] Fix | Delete
if ( has_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ) ) ) {
[98] Fix | Delete
remove_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ), 10, 2 );
[99] Fix | Delete
remove_filter( 'posts_where', array( $wpml_query_filter, 'posts_where_filter' ), 10, 2 );
[100] Fix | Delete
}
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
/**************************************
[104] Fix | Delete
*
[105] Fix | Delete
* AJAX Thumbnail Rebuild
[106] Fix | Delete
*
[107] Fix | Delete
* @since 2.8
[108] Fix | Delete
*/
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* AJAX Thumbnail Rebuild integration.
[112] Fix | Delete
*
[113] Fix | Delete
* If this is a thumbnail regeneration - only continue for selected thumbs
[114] Fix | Delete
* (no need to regenerate everything else).
[115] Fix | Delete
*
[116] Fix | Delete
* @since 2.8.0
[117] Fix | Delete
*
[118] Fix | Delete
* @param string $smush_image Image size.
[119] Fix | Delete
* @param string $size_key Thumbnail size.
[120] Fix | Delete
*
[121] Fix | Delete
* @return bool
[122] Fix | Delete
*/
[123] Fix | Delete
public function skip_images( $smush_image, $size_key ) {
[124] Fix | Delete
if ( empty( $_POST['regen'] ) || ! is_array( $_POST['regen'] ) ) { // Input var ok.
[125] Fix | Delete
return $smush_image;
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
$smush_sizes = wp_unslash( $_POST['regen'] ); // Input var ok.
[129] Fix | Delete
[130] Fix | Delete
if ( in_array( $size_key, $smush_sizes, true ) ) {
[131] Fix | Delete
return $smush_image;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
// Do not regenerate other thumbnails for regenerate action.
[135] Fix | Delete
return false;
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
/**************************************
[139] Fix | Delete
*
[140] Fix | Delete
* WP Retina 2x
[141] Fix | Delete
*/
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Smush Retina images for WP Retina 2x, Update Stats.
[145] Fix | Delete
*
[146] Fix | Delete
* @param int $id Attachment ID.
[147] Fix | Delete
* @param string $retina_file Retina image.
[148] Fix | Delete
* @param string $image_size Image size.
[149] Fix | Delete
*/
[150] Fix | Delete
public function smush_retina_image( $id, $retina_file, $image_size ) {
[151] Fix | Delete
$smush = WP_Smush::get_instance()->core()->mod->smush;
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Allows to Enable/Disable WP Retina 2x Integration
[155] Fix | Delete
*/
[156] Fix | Delete
$smush_retina_images = apply_filters( 'smush_retina_images', true );
[157] Fix | Delete
[158] Fix | Delete
// Check if Smush retina images is enabled.
[159] Fix | Delete
if ( ! $smush_retina_images ) {
[160] Fix | Delete
return;
[161] Fix | Delete
}
[162] Fix | Delete
// Check for Empty fields.
[163] Fix | Delete
if ( empty( $id ) || empty( $retina_file ) || empty( $image_size ) ) {
[164] Fix | Delete
return;
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
// Do not smush if auto smush is turned off.
[168] Fix | Delete
if ( ! $smush->should_auto_smush( $id ) ) {
[169] Fix | Delete
return;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Allows to skip a image from smushing
[174] Fix | Delete
*
[175] Fix | Delete
* @param bool $smush_image Smush image or not
[176] Fix | Delete
* @param string $image_size Size of image being smushed
[177] Fix | Delete
* @param string $retina_file Retina file path.
[178] Fix | Delete
* @param int $id Attachment ID.
[179] Fix | Delete
*
[180] Fix | Delete
* @since 3.9.6 Add two parameters for the filter.
[181] Fix | Delete
*/
[182] Fix | Delete
$smush_image = apply_filters( 'wp_smush_media_image', true, $image_size, $retina_file, $id );
[183] Fix | Delete
[184] Fix | Delete
if ( ! $smush_image ) {
[185] Fix | Delete
return;
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
$stats = $smush->do_smushit( $retina_file );
[189] Fix | Delete
// If we squeezed out something, Update stats.
[190] Fix | Delete
if ( ! is_wp_error( $stats ) && ! empty( $stats['data'] ) && isset( $stats['data'] ) && $stats['data']->bytes_saved > 0 ) {
[191] Fix | Delete
$image_size = $image_size . '@2x';
[192] Fix | Delete
[193] Fix | Delete
$this->update_smush_stats_single( $id, $stats, $image_size );
[194] Fix | Delete
}
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
/**
[198] Fix | Delete
* Updates the smush stats for a single image size.
[199] Fix | Delete
*
[200] Fix | Delete
* @param int $id Attachment ID.
[201] Fix | Delete
* @param array $smush_stats Smush stats.
[202] Fix | Delete
* @param string $image_size Image size.
[203] Fix | Delete
*/
[204] Fix | Delete
private function update_smush_stats_single( $id, $smush_stats, $image_size = '' ) {
[205] Fix | Delete
// Return, if we don't have image id or stats for it.
[206] Fix | Delete
if ( empty( $id ) || empty( $smush_stats ) || empty( $image_size ) ) {
[207] Fix | Delete
return;
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
$smush = WP_Smush::get_instance()->core()->mod->smush;
[211] Fix | Delete
$data = $smush_stats['data'];
[212] Fix | Delete
// Get existing Stats.
[213] Fix | Delete
$stats = get_post_meta( $id, Smush::$smushed_meta_key, true );
[214] Fix | Delete
[215] Fix | Delete
// Update existing Stats.
[216] Fix | Delete
if ( ! empty( $stats ) ) {
[217] Fix | Delete
// Update stats for each size.
[218] Fix | Delete
if ( isset( $stats['sizes'] ) ) {
[219] Fix | Delete
// if stats for a particular size doesn't exists.
[220] Fix | Delete
if ( empty( $stats['sizes'][ $image_size ] ) ) {
[221] Fix | Delete
// Update size wise details.
[222] Fix | Delete
$stats['sizes'][ $image_size ] = (object) $smush->array_fill_placeholders( $smush->get_size_signature(), (array) $data );
[223] Fix | Delete
} else {
[224] Fix | Delete
// Update compression percent and bytes saved for each size.
[225] Fix | Delete
$stats['sizes'][ $image_size ]->bytes = $stats['sizes'][ $image_size ]->bytes + $data->bytes_saved;
[226] Fix | Delete
$stats['sizes'][ $image_size ]->percent = $stats['sizes'][ $image_size ]->percent + $data->compression;
[227] Fix | Delete
}
[228] Fix | Delete
}
[229] Fix | Delete
} else {
[230] Fix | Delete
// Create new stats.
[231] Fix | Delete
$stats = array(
[232] Fix | Delete
'stats' => array_merge(
[233] Fix | Delete
$smush->get_size_signature(),
[234] Fix | Delete
array(
[235] Fix | Delete
'api_version' => - 1,
[236] Fix | Delete
'lossy' => - 1,
[237] Fix | Delete
)
[238] Fix | Delete
),
[239] Fix | Delete
'sizes' => array(),
[240] Fix | Delete
);
[241] Fix | Delete
[242] Fix | Delete
$stats['stats']['api_version'] = $data->api_version;
[243] Fix | Delete
$stats['stats']['lossy'] = $data->lossy;
[244] Fix | Delete
$stats['stats']['keep_exif'] = ! empty( $data->keep_exif ) ? $data->keep_exif : 0;
[245] Fix | Delete
[246] Fix | Delete
// Update size wise details.
[247] Fix | Delete
$stats['sizes'][ $image_size ] = (object) $smush->array_fill_placeholders( $smush->get_size_signature(), (array) $data );
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
// Calculate the total compression.
[251] Fix | Delete
$stats = WP_Smush::get_instance()->core()->total_compression( $stats );
[252] Fix | Delete
[253] Fix | Delete
update_post_meta( $id, Smush::$smushed_meta_key, $stats );
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
/**************************************
[257] Fix | Delete
*
[258] Fix | Delete
* WPML
[259] Fix | Delete
*
[260] Fix | Delete
* @since 3.0
[261] Fix | Delete
*/
[262] Fix | Delete
[263] Fix | Delete
/**
[264] Fix | Delete
* Ignore WPML duplicated images from Smush.
[265] Fix | Delete
*
[266] Fix | Delete
* If WPML is duplicating images, we need to mark them as ignored for Smushing
[267] Fix | Delete
* because the image is same for all those duplicated attachment posts. This is
[268] Fix | Delete
* required to avoid wrong Smush stats.
[269] Fix | Delete
*
[270] Fix | Delete
* @param int $attachment_id Original attachment ID.
[271] Fix | Delete
* @param int $duplicated_attachment_id Duplicated attachment ID.
[272] Fix | Delete
*
[273] Fix | Delete
* @since 3.9.4
[274] Fix | Delete
*/
[275] Fix | Delete
public function wpml_ignore_duplicate_attachment( $attachment_id, $duplicated_attachment_id ) {
[276] Fix | Delete
// Ignore the image from Smush if duplicate.
[277] Fix | Delete
update_post_meta( $duplicated_attachment_id, 'wp-smush-ignore-bulk', true );
[278] Fix | Delete
}
[279] Fix | Delete
[280] Fix | Delete
/**
[281] Fix | Delete
* Remove an image from the ignored list.
[282] Fix | Delete
*
[283] Fix | Delete
* When a new image is added instead of duplicate, we need to remove it
[284] Fix | Delete
* from the ignored list to make it available for Smushing.
[285] Fix | Delete
*
[286] Fix | Delete
* @param int $attachment_id Attachment ID.
[287] Fix | Delete
*
[288] Fix | Delete
* @since 3.9.4
[289] Fix | Delete
*/
[290] Fix | Delete
public function wpml_undo_ignore_attachment( $attachment_id ) {
[291] Fix | Delete
// Delete ignore flag.
[292] Fix | Delete
delete_post_meta( $attachment_id, 'wp-smush-ignore-bulk' );
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
/**
[296] Fix | Delete
* Skip ReCaptcha iframes from lazy loading.
[297] Fix | Delete
*
[298] Fix | Delete
* @since 3.4.2
[299] Fix | Delete
*
[300] Fix | Delete
* @param bool $skip Should skip? Default: false.
[301] Fix | Delete
* @param string $src Iframe url.
[302] Fix | Delete
*
[303] Fix | Delete
* @return bool
[304] Fix | Delete
*/
[305] Fix | Delete
public function exclude_recaptcha_iframe( $skip, $src ) {
[306] Fix | Delete
return false !== strpos( $src, 'recaptcha/api' );
[307] Fix | Delete
}
[308] Fix | Delete
[309] Fix | Delete
/**************************************
[310] Fix | Delete
*
[311] Fix | Delete
* Soliloquy slider
[312] Fix | Delete
*
[313] Fix | Delete
* @since 3.6.2
[314] Fix | Delete
*/
[315] Fix | Delete
[316] Fix | Delete
/**
[317] Fix | Delete
* Replace slider image links with CDN links.
[318] Fix | Delete
*
[319] Fix | Delete
* @param string $src Image source.
[320] Fix | Delete
*
[321] Fix | Delete
* @return string
[322] Fix | Delete
*/
[323] Fix | Delete
public function soliloquy_image_src( $src ) {
[324] Fix | Delete
if ( ! $this->cdn_helper->is_cdn_active() || empty( $src ) ) {
[325] Fix | Delete
return $src;
[326] Fix | Delete
}
[327] Fix | Delete
[328] Fix | Delete
if ( $this->cdn_helper->is_supported_url( $src ) ) {
[329] Fix | Delete
return $this->cdn_helper->generate_cdn_url( $src );
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
return $src;
[333] Fix | Delete
}
[334] Fix | Delete
[335] Fix | Delete
/**************************************
[336] Fix | Delete
*
[337] Fix | Delete
* Translate Press
[338] Fix | Delete
*
[339] Fix | Delete
* @since 3.6.3
[340] Fix | Delete
*/
[341] Fix | Delete
[342] Fix | Delete
/**
[343] Fix | Delete
* Disables "Lazy Load" on Translate Press translate editor
[344] Fix | Delete
*
[345] Fix | Delete
* @param bool $skip Should skip? Default: false.
[346] Fix | Delete
*
[347] Fix | Delete
* @return bool
[348] Fix | Delete
*/
[349] Fix | Delete
public function trp_translation_editor( $skip ) {
[350] Fix | Delete
if ( ! class_exists( '\TRP_Translate_Press' ) || ! isset( $_GET['trp-edit-translation'] ) ) {
[351] Fix | Delete
return $skip;
[352] Fix | Delete
}
[353] Fix | Delete
[354] Fix | Delete
return true;
[355] Fix | Delete
}
[356] Fix | Delete
[357] Fix | Delete
/**************************************
[358] Fix | Delete
*
[359] Fix | Delete
* WP Maintenance Plugin
[360] Fix | Delete
*
[361] Fix | Delete
* @since 3.8.0
[362] Fix | Delete
*/
[363] Fix | Delete
[364] Fix | Delete
/**
[365] Fix | Delete
* Disable page parsing when "Maintenance" is enabled
[366] Fix | Delete
*
[367] Fix | Delete
* @since 3.8.0
[368] Fix | Delete
*/
[369] Fix | Delete
public function wp_maintenance_mode() {
[370] Fix | Delete
if ( ! class_exists( '\MTNC' ) ) {
[371] Fix | Delete
return;
[372] Fix | Delete
}
[373] Fix | Delete
[374] Fix | Delete
global $mt_options;
[375] Fix | Delete
[376] Fix | Delete
if ( ! is_user_logged_in() && ! empty( $mt_options['state'] ) ) {
[377] Fix | Delete
add_filter( 'wp_smush_should_skip_lazy_load', '__return_true' );
[378] Fix | Delete
}
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
/**************************************
[382] Fix | Delete
*
[383] Fix | Delete
* Various modules
[384] Fix | Delete
*
[385] Fix | Delete
* @since 3.5
[386] Fix | Delete
*/
[387] Fix | Delete
[388] Fix | Delete
/**
[389] Fix | Delete
* CDN compatibility with Buddyboss platform
[390] Fix | Delete
*
[391] Fix | Delete
* @param string $src Image source.
[392] Fix | Delete
* @param string $image Actual image element.
[393] Fix | Delete
*
[394] Fix | Delete
* @return string Original or modified image source.
[395] Fix | Delete
*/
[396] Fix | Delete
public function buddyboss_platform_modify_image_src( $src, $image ) {
[397] Fix | Delete
if ( ! defined( 'BP_PLATFORM_VERSION' ) ) {
[398] Fix | Delete
return $src;
[399] Fix | Delete
}
[400] Fix | Delete
[401] Fix | Delete
/**
[402] Fix | Delete
* Compatibility with buddyboss theme and it's platform plugin.
[403] Fix | Delete
*
[404] Fix | Delete
* Buddyboss platform plugin uses the placeholder image as it's main src.
[405] Fix | Delete
* And process_src() method below uses the same placeholder.png to create
[406] Fix | Delete
* the srcset when "Automatic resizing" options is enabled for CDN.
[407] Fix | Delete
* ---------
[408] Fix | Delete
* Replacing the placeholder with actual image source as early as possible.
[409] Fix | Delete
* Checks:
[410] Fix | Delete
* 1. The image source contains buddyboss-platform in its string
[411] Fix | Delete
* 2. The image source contains placeholder.png and is crucial because there are other
[412] Fix | Delete
* images as well which doesn't uses placeholder.
[413] Fix | Delete
*/
[414] Fix | Delete
if ( false !== strpos( $src, 'buddyboss-platform' ) && false !== strpos( $src, 'placeholder.png' ) ) {
[415] Fix | Delete
$new_src = Parser::get_attribute( $image, 'data-src' );
[416] Fix | Delete
[417] Fix | Delete
if ( ! empty( $new_src ) ) {
[418] Fix | Delete
$src = $new_src;
[419] Fix | Delete
}
[420] Fix | Delete
}
[421] Fix | Delete
[422] Fix | Delete
return $src;
[423] Fix | Delete
}
[424] Fix | Delete
[425] Fix | Delete
/**
[426] Fix | Delete
* Skip images from lazy loading on GiveWP forms.
[427] Fix | Delete
*
[428] Fix | Delete
* @since 3.8.8
[429] Fix | Delete
*/
[430] Fix | Delete
public function givewp_skip_image_lazy_load() {
[431] Fix | Delete
add_filter( 'wp_smush_should_skip_lazy_load', '__return_true' );
[432] Fix | Delete
}
[433] Fix | Delete
[434] Fix | Delete
/**
[435] Fix | Delete
* Add method to handle thumbnail generation.
[436] Fix | Delete
*
[437] Fix | Delete
* We use this trick to call self::thumbnail_regenerate_handler()
[438] Fix | Delete
* to avoid it called several times while calling wp_generate_attachment_metadata().
[439] Fix | Delete
* 1. wp_generate_attachment_metadata -> wp_create_image_subsizes -> wp_update_attachment_metadata().
[440] Fix | Delete
* 2. wp_generate_attachment_metadata -> _wp_make_subsizes -> wp_update_attachment_metadata().
[441] Fix | Delete
* 3. After calling wp_generate_attachment_metadata() => We should only add our filter here.
[442] Fix | Delete
*
[443] Fix | Delete
* @param array $metadata Image metadata.
[444] Fix | Delete
* @return array The provided metadata.
[445] Fix | Delete
*/
[446] Fix | Delete
public function maybe_handle_thumbnail_generation( $metadata ) {
[447] Fix | Delete
/**
[448] Fix | Delete
* Add filter to handle thumbnail generation.
[449] Fix | Delete
* We use a big priority because it seems WP has an issue for this case,
[450] Fix | Delete
* after we remove this filter, all registered filters after this priority of this hook will not call.
[451] Fix | Delete
*/
[452] Fix | Delete
add_filter( 'wp_update_attachment_metadata', array( $this, 'thumbnail_regenerate_handler' ), 99999, 2 ); // S3 is using priority 110.
[453] Fix | Delete
return $metadata;
[454] Fix | Delete
}
[455] Fix | Delete
[456] Fix | Delete
/**
[457] Fix | Delete
* Callback for 'wp_update_attachment_metadata' WordPress hook used by Smush to detect
[458] Fix | Delete
* regenerated thumbnails and mark them as pending for (re)smush.
[459] Fix | Delete
*
[460] Fix | Delete
* @since 3.9.2
[461] Fix | Delete
*
[462] Fix | Delete
* @param array $new_meta New metadata.
[463] Fix | Delete
* @param int $attachment_id The attachment ID.
[464] Fix | Delete
*
[465] Fix | Delete
* @since 3.9.6 Disable this filter while async uploading,
[466] Fix | Delete
* and update compatible with S3, and only call it after generated metadata.
[467] Fix | Delete
*
[468] Fix | Delete
* @return array
[469] Fix | Delete
*/
[470] Fix | Delete
public function thumbnail_regenerate_handler( $new_meta, $attachment_id ) {
[471] Fix | Delete
// Remove the filter as we are no longer need it.
[472] Fix | Delete
remove_filter( 'wp_update_attachment_metadata', array( $this, 'thumbnail_regenerate_handler' ), 99999 );
[473] Fix | Delete
[474] Fix | Delete
/**
[475] Fix | Delete
* Skip if there is WP uploading a new image,
[476] Fix | Delete
* or the attachment is not an image or does not have thumbnails.
[477] Fix | Delete
*/
[478] Fix | Delete
if (
[479] Fix | Delete
empty( $new_meta['sizes'] )
[480] Fix | Delete
// Async uploading.
[481] Fix | Delete
|| isset( $_POST['post_id'] ) || isset( $_FILES['async-upload'] )
[482] Fix | Delete
// Smushed it, don't need to check it again.
[483] Fix | Delete
|| did_action( 'wp_smush_before_smush_file' )
[484] Fix | Delete
// Disable when restoring.
[485] Fix | Delete
|| did_action( 'wp_smush_before_restore_backup' )
[486] Fix | Delete
// Only support Image.
[487] Fix | Delete
|| ! Helper::is_smushable( $attachment_id )
[488] Fix | Delete
) {
[489] Fix | Delete
return $new_meta;
[490] Fix | Delete
}
[491] Fix | Delete
[492] Fix | Delete
// Skip if the attachment has an active smush operation or in being restored by Smush or ignored.
[493] Fix | Delete
if ( get_transient( 'smush-in-progress-' . $attachment_id ) || get_transient( 'wp-smush-restore-' . $attachment_id ) || get_post_meta( $attachment_id, 'wp-smush-ignore-bulk', true ) ) {
[494] Fix | Delete
return $new_meta;
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
$smush_meta = get_post_meta( $attachment_id, Smush::$smushed_meta_key, true );
[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