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/integrat.../nextgen
File: class-stats.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Handles all the stats related functions
[2] Fix | Delete
*
[3] Fix | Delete
* @package Smush\Core\Integrations\NextGen
[4] Fix | Delete
* @version 1.0
[5] Fix | Delete
*
[6] Fix | Delete
* @author Umesh Kumar <umesh@incsub.com>
[7] Fix | Delete
*
[8] Fix | Delete
* @copyright (c) 2016, Incsub (http://incsub.com)
[9] Fix | Delete
*/
[10] Fix | Delete
[11] Fix | Delete
namespace Smush\Core\Integrations\NextGen;
[12] Fix | Delete
[13] Fix | Delete
use C_Component_Registry;
[14] Fix | Delete
use C_Gallery_Storage;
[15] Fix | Delete
use C_NextGen_Serializable;
[16] Fix | Delete
use Exception;
[17] Fix | Delete
use Ngg_Serializable;
[18] Fix | Delete
use Smush\Core\Attachment_Id_List;
[19] Fix | Delete
use Smush\Core\Integrations\NextGen;
[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
[28] Fix | Delete
*
[29] Fix | Delete
* TODO refactor stats by using the new core stats to clean the code.
[30] Fix | Delete
*/
[31] Fix | Delete
class Stats extends NextGen {
[32] Fix | Delete
[33] Fix | Delete
const REOPTIMIZE_LIST_OPTION_ID = 'wp-smush-nextgen-reoptimize-list';
[34] Fix | Delete
[35] Fix | Delete
const SUPPER_SMUSHED_LIST_OPTION_ID = 'wp-smush-nextgen-super-smushed-list';
[36] Fix | Delete
[37] Fix | Delete
const SMUSH_STATS_OPTION_ID = 'wp_smush_stats_nextgen';
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Contains the total Stats, for displaying it on bulk page
[41] Fix | Delete
*
[42] Fix | Delete
* @var array
[43] Fix | Delete
*/
[44] Fix | Delete
public $stats = array();
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* PRO user status.
[48] Fix | Delete
*
[49] Fix | Delete
* @var bool
[50] Fix | Delete
*/
[51] Fix | Delete
private $is_pro_user;
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* @var Attachment_Id_List
[55] Fix | Delete
*/
[56] Fix | Delete
private $reoptimize_list;
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* @var Attachment_Id_List
[60] Fix | Delete
*/
[61] Fix | Delete
private $supper_smushed_list;
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* @var null|array
[65] Fix | Delete
*/
[66] Fix | Delete
private $global_stats;
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* @var null|array
[70] Fix | Delete
*/
[71] Fix | Delete
private $unsmushed_images;
[72] Fix | Delete
[73] Fix | Delete
/**
[74] Fix | Delete
* @var null|int.
[75] Fix | Delete
*/
[76] Fix | Delete
private $remaining_count;
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* @var int
[80] Fix | Delete
*/
[81] Fix | Delete
private $percent_optimized = 0;
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Stats constructor.
[85] Fix | Delete
*/
[86] Fix | Delete
public function __construct() {
[87] Fix | Delete
parent::__construct();
[88] Fix | Delete
$this->is_pro_user = WP_Smush::is_pro();
[89] Fix | Delete
$this->reoptimize_list = new Attachment_Id_List( self::REOPTIMIZE_LIST_OPTION_ID );
[90] Fix | Delete
$this->supper_smushed_list = new Attachment_Id_List( self::SUPPER_SMUSHED_LIST_OPTION_ID );
[91] Fix | Delete
[92] Fix | Delete
[93] Fix | Delete
// Clear stats cache when an image is restored.
[94] Fix | Delete
add_action( 'wp_smush_image_nextgen_restored', array( $this, 'clear_cache' ) );
[95] Fix | Delete
[96] Fix | Delete
// Add the resizing stats to Global stats.
[97] Fix | Delete
add_action( 'wp_smush_image_nextgen_resized', array( $this, 'update_stats' ), '', 2 );
[98] Fix | Delete
[99] Fix | Delete
// Get the stats for single image, update the global stats.
[100] Fix | Delete
add_action( 'wp_smush_nextgen_image_stats', array( $this, 'update_stats' ), '', 2 );
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Get the images id for nextgen gallery
[105] Fix | Delete
*
[106] Fix | Delete
* @param bool $force_refresh Optional. Whether to force the cache to be refreshed.
[107] Fix | Delete
* Default false.
[108] Fix | Delete
*
[109] Fix | Delete
* @param bool $return_ids Whether to return the ids array, set to false by default.
[110] Fix | Delete
*
[111] Fix | Delete
* @return int|mixed Returns the images ids or the count
[112] Fix | Delete
*/
[113] Fix | Delete
public static function total_count( $force_refresh = false, $return_ids = false ) {
[114] Fix | Delete
// Check for the wp_smush_images in the 'nextgen' group.
[115] Fix | Delete
$attachment_ids = wp_cache_get( 'wp_smush_images', 'nextgen' );
[116] Fix | Delete
[117] Fix | Delete
// If nothing is found, build the object.
[118] Fix | Delete
if ( true === $force_refresh || false === $attachment_ids ) {
[119] Fix | Delete
// Get the nextgen image IDs.
[120] Fix | Delete
$attachment_ids = self::get_nextgen_attachments();
[121] Fix | Delete
[122] Fix | Delete
if ( ! is_wp_error( $attachment_ids ) ) {
[123] Fix | Delete
// In this case we don't need a timed cache expiration.
[124] Fix | Delete
wp_cache_set( 'wp_smush_images', $attachment_ids, 'nextgen' );
[125] Fix | Delete
}
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
return $return_ids ? $attachment_ids : count( $attachment_ids );
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
/**
[132] Fix | Delete
* Returns the ngg images list(id and meta ) or count
[133] Fix | Delete
*
[134] Fix | Delete
* @param string $type Whether to return smushed images or unsmushed images.
[135] Fix | Delete
* @param bool|false $count Return count only.
[136] Fix | Delete
* @param bool|false $force_update True/false to update the cache or not.
[137] Fix | Delete
*
[138] Fix | Delete
* @return bool|mixed Returns assoc array of image ids and meta or Image count
[139] Fix | Delete
*
[140] Fix | Delete
* @throws Exception Exception.
[141] Fix | Delete
*/
[142] Fix | Delete
public function get_ngg_images( $type = 'smushed', $count = false, $force_update = false ) {
[143] Fix | Delete
global $wpdb;
[144] Fix | Delete
[145] Fix | Delete
$limit = apply_filters( 'wp_smush_nextgen_query_limit', 1000 );
[146] Fix | Delete
$offset = 0;
[147] Fix | Delete
[148] Fix | Delete
// Check type of images being queried.
[149] Fix | Delete
if ( ! in_array( $type, array( 'smushed', 'unsmushed' ), true ) ) {
[150] Fix | Delete
return false;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
// Check for the wp_smush_images_smushed in the 'nextgen' group.
[154] Fix | Delete
$images = wp_cache_get( 'wp_smush_images_' . $type, 'nextgen' );
[155] Fix | Delete
[156] Fix | Delete
// If nothing is found, build the object.
[157] Fix | Delete
if ( ! $images || $force_update ) {
[158] Fix | Delete
// Query Attachments for meta key.
[159] Fix | Delete
$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT pid, meta_data FROM {$wpdb->nggpictures} LIMIT %d, %d", $offset, $limit ) ); // Db call ok.
[160] Fix | Delete
while ( ! empty( $attachments ) ) {
[161] Fix | Delete
foreach ( $attachments as $attachment ) {
[162] Fix | Delete
// Check if it has `wp_smush` key.
[163] Fix | Delete
if ( class_exists( 'Ngg_Serializable' ) ) {
[164] Fix | Delete
$meta = ( new Ngg_Serializable() )->unserialize( $attachment->meta_data );
[165] Fix | Delete
} elseif ( class_exists( 'C_NextGen_Serializable' ) && method_exists( 'C_NextGen_Serializable', 'unserialize' ) ) {
[166] Fix | Delete
$meta = C_NextGen_Serializable::unserialize( $attachment->meta_data );
[167] Fix | Delete
} else {
[168] Fix | Delete
// If you can't parse it without NextGen - don't parse at all.
[169] Fix | Delete
continue;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
// Store pid in image meta.
[173] Fix | Delete
if ( is_array( $meta ) && empty( $meta['pid'] ) ) {
[174] Fix | Delete
$meta['pid'] = $attachment->pid;
[175] Fix | Delete
} elseif ( is_object( $meta ) && empty( $meta->pid ) ) {
[176] Fix | Delete
$meta->pid = $attachment->pid;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
// Check meta for wp_smush.
[180] Fix | Delete
if ( ! is_array( $meta ) || empty( $meta['wp_smush'] ) ) {
[181] Fix | Delete
$unsmushed_images[ $attachment->pid ] = $meta;
[182] Fix | Delete
continue;
[183] Fix | Delete
}
[184] Fix | Delete
$smushed_images[ $attachment->pid ] = $meta;
[185] Fix | Delete
}
[186] Fix | Delete
// Set the offset.
[187] Fix | Delete
$offset += $limit;
[188] Fix | Delete
[189] Fix | Delete
$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT pid, meta_data FROM {$wpdb->nggpictures} LIMIT %d, %d", $offset, $limit ) ); // Db call ok.
[190] Fix | Delete
}
[191] Fix | Delete
if ( ! empty( $smushed_images ) ) {
[192] Fix | Delete
wp_cache_set( 'wp_smush_images_smushed', $smushed_images, 'nextgen', 300 );
[193] Fix | Delete
}
[194] Fix | Delete
if ( ! empty( $unsmushed_images ) ) {
[195] Fix | Delete
wp_cache_set( 'wp_smush_images_unsmushed', $unsmushed_images, 'nextgen', 300 );
[196] Fix | Delete
}
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
if ( 'smushed' === $type ) {
[200] Fix | Delete
$smushed_images = ! empty( $smushed_images ) ? $smushed_images : $images;
[201] Fix | Delete
if ( ! $smushed_images ) {
[202] Fix | Delete
return 0;
[203] Fix | Delete
}
[204] Fix | Delete
return $count ? count( $smushed_images ) : $smushed_images;
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
$unsmushed_images = ! empty( $unsmushed_images ) ? $unsmushed_images : $images;
[208] Fix | Delete
if ( ! $unsmushed_images ) {
[209] Fix | Delete
return 0;
[210] Fix | Delete
}
[211] Fix | Delete
return $count ? count( $unsmushed_images ) : $unsmushed_images;
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
/**
[215] Fix | Delete
* Updated the global smush stats for NextGen gallery
[216] Fix | Delete
*
[217] Fix | Delete
* @param int $image_id Image ID.
[218] Fix | Delete
* @param array $stats Compression stats fo respective image.
[219] Fix | Delete
*/
[220] Fix | Delete
public function update_stats( $image_id, $stats ) {
[221] Fix | Delete
$stats = ! empty( $stats['stats'] ) ? $stats['stats'] : '';
[222] Fix | Delete
[223] Fix | Delete
$smush_stats = $this->get_cache_smush_stats();
[224] Fix | Delete
[225] Fix | Delete
if ( ! empty( $stats ) ) {
[226] Fix | Delete
// Human Readable.
[227] Fix | Delete
$smush_stats['human'] = ! empty( $smush_stats['bytes'] ) ? size_format( $smush_stats['bytes'], 1 ) : '';
[228] Fix | Delete
[229] Fix | Delete
// Size of images before the compression.
[230] Fix | Delete
$smush_stats['size_before'] = ! empty( $smush_stats['size_before'] ) ? ( $smush_stats['size_before'] + $stats['size_before'] ) : $stats['size_before'];
[231] Fix | Delete
[232] Fix | Delete
// Size of image after compression.
[233] Fix | Delete
$smush_stats['size_after'] = ! empty( $smush_stats['size_after'] ) ? ( $smush_stats['size_after'] + $stats['size_after'] ) : $stats['size_after'];
[234] Fix | Delete
[235] Fix | Delete
$smush_stats['bytes'] = ! empty( $smush_stats['size_before'] ) && ! empty( $smush_stats['size_after'] ) ? ( $smush_stats['size_before'] - $smush_stats['size_after'] ) : 0;
[236] Fix | Delete
[237] Fix | Delete
// Compression Percentage.
[238] Fix | Delete
$smush_stats['percent'] = ! empty( $smush_stats['size_before'] ) && ! empty( $smush_stats['size_after'] ) && $smush_stats['size_before'] > 0 ? ( $smush_stats['bytes'] / $smush_stats['size_before'] ) * 100 : $stats['percent'];
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
update_option( self::SMUSH_STATS_OPTION_ID, $smush_stats, false );
[242] Fix | Delete
$this->clear_cache();
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
/**
[246] Fix | Delete
* Clears the object cache for NextGen stats.
[247] Fix | Delete
*
[248] Fix | Delete
* @since 3.7.0
[249] Fix | Delete
*/
[250] Fix | Delete
public function clear_cache() {
[251] Fix | Delete
wp_cache_delete( 'wp_smush_images_smushed', 'nextgen' );
[252] Fix | Delete
wp_cache_delete( 'wp_smush_images_unsmushed', 'nextgen' );
[253] Fix | Delete
wp_cache_delete( 'wp_smush_images', 'nextgen' );
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
/**
[257] Fix | Delete
* Get the attachment stats for a image
[258] Fix | Delete
*
[259] Fix | Delete
* @param object|array|int $id Attachment ID.
[260] Fix | Delete
*
[261] Fix | Delete
* @return array
[262] Fix | Delete
*/
[263] Fix | Delete
private function get_attachment_stats( $image ) {
[264] Fix | Delete
// We'll get the image object in $image itself, else fetch it using Gallery Storage.
[265] Fix | Delete
if ( is_numeric( $image ) ) {
[266] Fix | Delete
// Registry Object for NextGen Gallery.
[267] Fix | Delete
$registry = C_Component_Registry::get_instance();
[268] Fix | Delete
[269] Fix | Delete
// Gallery Storage Object.
[270] Fix | Delete
$storage = $registry->get_utility( 'I_Gallery_Storage' );
[271] Fix | Delete
[272] Fix | Delete
// get an image object.
[273] Fix | Delete
$image = $storage->object->_image_mapper->find( $image );
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
$smush_savings = $this->get_image_smush_savings( $image );
[277] Fix | Delete
$resize_savings = $this->get_image_resize_savings( $image );
[278] Fix | Delete
[279] Fix | Delete
return $this->recalculate_stats( 'add', $smush_savings, $resize_savings );
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
/**
[283] Fix | Delete
* Get the Nextgen Smush stats
[284] Fix | Delete
*
[285] Fix | Delete
* @return bool|mixed|void
[286] Fix | Delete
*/
[287] Fix | Delete
public function get_smush_stats() {
[288] Fix | Delete
$smushed_stats = array(
[289] Fix | Delete
'bytes' => 0,
[290] Fix | Delete
'size_before' => 0,
[291] Fix | Delete
'size_after' => 0,
[292] Fix | Delete
'percent' => 0,
[293] Fix | Delete
);
[294] Fix | Delete
[295] Fix | Delete
// Clear up the stats.
[296] Fix | Delete
if ( 0 == $this->total_count() || $this->get_smushed_count() < 1 ) {
[297] Fix | Delete
delete_option( self::SMUSH_STATS_OPTION_ID );
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
// Check for the wp_smush_images in the 'nextgen' group.
[301] Fix | Delete
$stats = $this->get_cache_smush_stats();
[302] Fix | Delete
[303] Fix | Delete
$size_before = (int) $this->get_array_value( $stats, 'size_before' );
[304] Fix | Delete
if ( empty( $size_before ) ) {
[305] Fix | Delete
return $smushed_stats;
[306] Fix | Delete
}
[307] Fix | Delete
$size_after = (int) $this->get_array_value( $stats, 'size_after' );
[308] Fix | Delete
$stats['bytes'] = $size_before - $size_after;
[309] Fix | Delete
$stats['bytes'] = $stats['bytes'] > 0 ? $stats['bytes'] : 0;
[310] Fix | Delete
$stats['percent'] = ( $stats['bytes'] / $stats['size_before'] ) * 100;
[311] Fix | Delete
// Round off precentage.
[312] Fix | Delete
$stats['percent'] = ! empty( $stats['percent'] ) ? round( $stats['percent'], 1 ) : 0;
[313] Fix | Delete
$stats['human'] = size_format( $stats['bytes'], $stats['bytes'] >= 1024 ? 1 : 0 );
[314] Fix | Delete
[315] Fix | Delete
$smushed_stats = array_merge( $smushed_stats, $stats );
[316] Fix | Delete
[317] Fix | Delete
// Gotta remove the stats for re-smush ids.
[318] Fix | Delete
if ( $this->get_reoptimize_list()->get_count() ) {
[319] Fix | Delete
$resmush_stats = $this->get_stats_for_ids( $this->get_reoptimize_list()->get_ids() );
[320] Fix | Delete
// Recalculate stats, Remove stats for resmush ids.
[321] Fix | Delete
$smushed_stats = $this->recalculate_stats( 'sub', $smushed_stats, $resmush_stats );
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
return $smushed_stats;
[325] Fix | Delete
}
[326] Fix | Delete
[327] Fix | Delete
/**
[328] Fix | Delete
* Get the combined stats for given Ids
[329] Fix | Delete
*
[330] Fix | Delete
* @param array $ids Image IDs.
[331] Fix | Delete
*
[332] Fix | Delete
* @return array|bool Array of Stats for the given ids
[333] Fix | Delete
*/
[334] Fix | Delete
public function get_stats_for_ids( $ids = array() ) {
[335] Fix | Delete
// Return if we don't have an array or no ids.
[336] Fix | Delete
if ( ! is_array( $ids ) || empty( $ids ) ) {
[337] Fix | Delete
return false;
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
// Initialize the Stats array.
[341] Fix | Delete
$stats = array(
[342] Fix | Delete
'size_before' => 0,
[343] Fix | Delete
'size_after' => 0,
[344] Fix | Delete
);
[345] Fix | Delete
// Calculate the stats, Expensive Operation.
[346] Fix | Delete
foreach ( $ids as $id ) {
[347] Fix | Delete
$image_stats = $this->get_attachment_stats( $id );
[348] Fix | Delete
$stats = $this->recalculate_stats( 'add', $stats, $image_stats );
[349] Fix | Delete
}
[350] Fix | Delete
[351] Fix | Delete
return $stats;
[352] Fix | Delete
}
[353] Fix | Delete
[354] Fix | Delete
/**
[355] Fix | Delete
* Add/Subtract the values from 2nd array to First array
[356] Fix | Delete
* This function is very specific to current requirement of stats re-calculation
[357] Fix | Delete
*
[358] Fix | Delete
* @param string $op 'add', 'sub' Add or Subtract the values.
[359] Fix | Delete
* @param array $a1 First array.
[360] Fix | Delete
* @param array $a2 Second array.
[361] Fix | Delete
*
[362] Fix | Delete
* @return array Return $a1
[363] Fix | Delete
*/
[364] Fix | Delete
private function recalculate_stats( $op = 'add', $a1 = array(), $a2 = array() ) {
[365] Fix | Delete
// If the first array itself is not set, return.
[366] Fix | Delete
if ( empty( $a1 ) ) {
[367] Fix | Delete
return $a1;
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
// Iterate over keys in first array, and add/subtract the values.
[371] Fix | Delete
foreach ( $a1 as $k => $v ) {
[372] Fix | Delete
// If the key is not set in 2nd array, skip.
[373] Fix | Delete
if ( empty( $a2[ $k ] ) || ! in_array( $k, array( 'size_before', 'size_after' ) ) ) {
[374] Fix | Delete
continue;
[375] Fix | Delete
}
[376] Fix | Delete
// Else perform the operation, Considers the value to be integer, doesn't performs a check.
[377] Fix | Delete
if ( 'sub' === $op ) {
[378] Fix | Delete
// Subtract the value.
[379] Fix | Delete
$a1[ $k ] -= $a2[ $k ];
[380] Fix | Delete
} elseif ( 'add' === $op ) {
[381] Fix | Delete
// Add the value.
[382] Fix | Delete
$a1[ $k ] += $a2[ $k ];
[383] Fix | Delete
}
[384] Fix | Delete
}
[385] Fix | Delete
[386] Fix | Delete
// Recalculate percentage and human savings.
[387] Fix | Delete
$a1['bytes'] = $a1['size_before'] - $a1['size_after'];
[388] Fix | Delete
$a1['percent'] = $a1['bytes'] > 0 ? round( ( $a1['bytes'] / $a1['size_before'] ) * 100, 1 ) : 0;
[389] Fix | Delete
$a1['human'] = $a1['bytes'] > 0 ? size_format( $a1['bytes'], 1 ) : 0;
[390] Fix | Delete
[391] Fix | Delete
return $a1;
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
/**
[395] Fix | Delete
* Get Super smushed images from the given images array
[396] Fix | Delete
*
[397] Fix | Delete
* @param array $images Array of images containing metadata.
[398] Fix | Delete
*
[399] Fix | Delete
* @return array Array containing ids of supersmushed images
[400] Fix | Delete
*/
[401] Fix | Delete
private function get_super_smushed_images( $images = array() ) {
[402] Fix | Delete
if ( empty( $images ) ) {
[403] Fix | Delete
return array();
[404] Fix | Delete
}
[405] Fix | Delete
$super_smushed = array();
[406] Fix | Delete
// Iterate Over all the images.
[407] Fix | Delete
foreach ( $images as $image_k => $image ) {
[408] Fix | Delete
if ( empty( $image ) || ! is_array( $image ) || ! isset( $image['wp_smush'] ) ) {
[409] Fix | Delete
continue;
[410] Fix | Delete
}
[411] Fix | Delete
// Check for lossy compression.
[412] Fix | Delete
if ( ! empty( $image['wp_smush']['stats'] ) && ! empty( $image['wp_smush']['stats']['lossy'] ) ) {
[413] Fix | Delete
$super_smushed[] = $image_k;
[414] Fix | Delete
}
[415] Fix | Delete
}
[416] Fix | Delete
return $super_smushed;
[417] Fix | Delete
}
[418] Fix | Delete
[419] Fix | Delete
/**
[420] Fix | Delete
* Recalculate stats for the given smushed ids and update the cache
[421] Fix | Delete
* Update Super smushed image ids
[422] Fix | Delete
*
[423] Fix | Delete
* @throws Exception Exception.
[424] Fix | Delete
*/
[425] Fix | Delete
public function update_stats_cache() {
[426] Fix | Delete
// Get the Image ids.
[427] Fix | Delete
$smushed_images = $this->get_ngg_images( 'smushed' );
[428] Fix | Delete
$super_smushed = array(
[429] Fix | Delete
'ids' => array(),
[430] Fix | Delete
'timestamp' => '',
[431] Fix | Delete
);
[432] Fix | Delete
[433] Fix | Delete
$stats = $this->get_stats_for_ids( $smushed_images );
[434] Fix | Delete
$lossy = $this->get_super_smushed_images( $smushed_images );
[435] Fix | Delete
[436] Fix | Delete
if ( empty( $stats['bytes'] ) && ! empty( $stats['size_before'] ) ) {
[437] Fix | Delete
$stats['bytes'] = $stats['size_before'] - $stats['size_after'];
[438] Fix | Delete
}
[439] Fix | Delete
$stats['human'] = size_format( ! empty( $stats['bytes'] ) ? $stats['bytes'] : 0 );
[440] Fix | Delete
if ( ! empty( $stats['size_before'] ) ) {
[441] Fix | Delete
$stats['percent'] = ( $stats['bytes'] / $stats['size_before'] ) * 100;
[442] Fix | Delete
$stats['percent'] = round( $stats['percent'], 2 );
[443] Fix | Delete
}
[444] Fix | Delete
[445] Fix | Delete
// Update Re-smush list.
[446] Fix | Delete
if ( is_array( WP_Smush::get_instance()->core()->nextgen->ng_admin->resmush_ids ) && is_array( $smushed_images ) ) {
[447] Fix | Delete
$resmush_ids = array_intersect( WP_Smush::get_instance()->core()->nextgen->ng_admin->resmush_ids, array_keys( $smushed_images ) );
[448] Fix | Delete
}
[449] Fix | Delete
[450] Fix | Delete
// If we have resmush ids, add it to db.
[451] Fix | Delete
if ( ! empty( $resmush_ids ) ) {
[452] Fix | Delete
// Update re-smush images to db.
[453] Fix | Delete
$this->get_reoptimize_list()->update_ids( $resmush_ids );
[454] Fix | Delete
}
[455] Fix | Delete
[456] Fix | Delete
// Update Super smushed images in db.
[457] Fix | Delete
$this->get_supper_smushed_list()->update_ids( $lossy );
[458] Fix | Delete
[459] Fix | Delete
// Update Stats Cache.
[460] Fix | Delete
update_option( self::SMUSH_STATS_OPTION_ID, $stats, false );
[461] Fix | Delete
[462] Fix | Delete
}
[463] Fix | Delete
[464] Fix | Delete
public function get_reoptimize_list() {
[465] Fix | Delete
return $this->reoptimize_list;
[466] Fix | Delete
}
[467] Fix | Delete
[468] Fix | Delete
public function get_supper_smushed_list() {
[469] Fix | Delete
return $this->supper_smushed_list;
[470] Fix | Delete
}
[471] Fix | Delete
[472] Fix | Delete
public function get_supper_smushed_count() {
[473] Fix | Delete
return count( $this->get_supper_smushed() );
[474] Fix | Delete
}
[475] Fix | Delete
[476] Fix | Delete
private function get_supper_smushed() {
[477] Fix | Delete
$super_smushed = $this->get_supper_smushed_list()->get_ids();
[478] Fix | Delete
[479] Fix | Delete
// If we have images to be resmushed, Update supersmush list.
[480] Fix | Delete
$resmush_ids = $this->get_reoptimize_list()->get_ids();
[481] Fix | Delete
if ( ! empty( $resmush_ids ) && ! empty( $super_smushed ) ) {
[482] Fix | Delete
$super_smushed = array_diff( $super_smushed, $resmush_ids );
[483] Fix | Delete
}
[484] Fix | Delete
[485] Fix | Delete
// If supersmushed images are more than total, clean it up.
[486] Fix | Delete
if ( count( $super_smushed ) > self::total_count() ) {
[487] Fix | Delete
$super_smushed = $this->cleanup_super_smush_data();
[488] Fix | Delete
}
[489] Fix | Delete
[490] Fix | Delete
return (array) $super_smushed;
[491] Fix | Delete
}
[492] Fix | Delete
[493] Fix | Delete
/**
[494] Fix | Delete
* Cleanup Super-smush images array against the all ids in gallery
[495] Fix | Delete
*
[496] Fix | Delete
* @return array|mixed|void
[497] Fix | Delete
*/
[498] Fix | Delete
private function cleanup_super_smush_data() {
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function