: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Handles all the stats related functions
* @package Smush\Core\Integrations\NextGen
* @author Umesh Kumar <umesh@incsub.com>
* @copyright (c) 2016, Incsub (http://incsub.com)
namespace Smush\Core\Integrations\NextGen;
use C_Component_Registry;
use C_NextGen_Serializable;
use Smush\Core\Attachment_Id_List;
use Smush\Core\Integrations\NextGen;
if ( ! defined( 'WPINC' ) ) {
* TODO refactor stats by using the new core stats to clean the code.
class Stats extends NextGen {
const REOPTIMIZE_LIST_OPTION_ID = 'wp-smush-nextgen-reoptimize-list';
const SUPPER_SMUSHED_LIST_OPTION_ID = 'wp-smush-nextgen-super-smushed-list';
const SMUSH_STATS_OPTION_ID = 'wp_smush_stats_nextgen';
* Contains the total Stats, for displaying it on bulk page
* @var Attachment_Id_List
private $reoptimize_list;
* @var Attachment_Id_List
private $supper_smushed_list;
private $unsmushed_images;
private $remaining_count;
private $percent_optimized = 0;
public function __construct() {
$this->is_pro_user = WP_Smush::is_pro();
$this->reoptimize_list = new Attachment_Id_List( self::REOPTIMIZE_LIST_OPTION_ID );
$this->supper_smushed_list = new Attachment_Id_List( self::SUPPER_SMUSHED_LIST_OPTION_ID );
// Clear stats cache when an image is restored.
add_action( 'wp_smush_image_nextgen_restored', array( $this, 'clear_cache' ) );
// Add the resizing stats to Global stats.
add_action( 'wp_smush_image_nextgen_resized', array( $this, 'update_stats' ), '', 2 );
// Get the stats for single image, update the global stats.
add_action( 'wp_smush_nextgen_image_stats', array( $this, 'update_stats' ), '', 2 );
* Get the images id for nextgen gallery
* @param bool $force_refresh Optional. Whether to force the cache to be refreshed.
* @param bool $return_ids Whether to return the ids array, set to false by default.
* @return int|mixed Returns the images ids or the count
public static function total_count( $force_refresh = false, $return_ids = false ) {
// Check for the wp_smush_images in the 'nextgen' group.
$attachment_ids = wp_cache_get( 'wp_smush_images', 'nextgen' );
// If nothing is found, build the object.
if ( true === $force_refresh || false === $attachment_ids ) {
// Get the nextgen image IDs.
$attachment_ids = self::get_nextgen_attachments();
if ( ! is_wp_error( $attachment_ids ) ) {
// In this case we don't need a timed cache expiration.
wp_cache_set( 'wp_smush_images', $attachment_ids, 'nextgen' );
return $return_ids ? $attachment_ids : count( $attachment_ids );
* Returns the ngg images list(id and meta ) or count
* @param string $type Whether to return smushed images or unsmushed images.
* @param bool|false $count Return count only.
* @param bool|false $force_update True/false to update the cache or not.
* @return bool|mixed Returns assoc array of image ids and meta or Image count
* @throws Exception Exception.
public function get_ngg_images( $type = 'smushed', $count = false, $force_update = false ) {
$limit = apply_filters( 'wp_smush_nextgen_query_limit', 1000 );
// Check type of images being queried.
if ( ! in_array( $type, array( 'smushed', 'unsmushed' ), true ) ) {
// Check for the wp_smush_images_smushed in the 'nextgen' group.
$images = wp_cache_get( 'wp_smush_images_' . $type, 'nextgen' );
// If nothing is found, build the object.
if ( ! $images || $force_update ) {
// Query Attachments for meta key.
$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT pid, meta_data FROM {$wpdb->nggpictures} LIMIT %d, %d", $offset, $limit ) ); // Db call ok.
while ( ! empty( $attachments ) ) {
foreach ( $attachments as $attachment ) {
// Check if it has `wp_smush` key.
if ( class_exists( 'Ngg_Serializable' ) ) {
$meta = ( new Ngg_Serializable() )->unserialize( $attachment->meta_data );
} elseif ( class_exists( 'C_NextGen_Serializable' ) && method_exists( 'C_NextGen_Serializable', 'unserialize' ) ) {
$meta = C_NextGen_Serializable::unserialize( $attachment->meta_data );
// If you can't parse it without NextGen - don't parse at all.
// Store pid in image meta.
if ( is_array( $meta ) && empty( $meta['pid'] ) ) {
$meta['pid'] = $attachment->pid;
} elseif ( is_object( $meta ) && empty( $meta->pid ) ) {
$meta->pid = $attachment->pid;
// Check meta for wp_smush.
if ( ! is_array( $meta ) || empty( $meta['wp_smush'] ) ) {
$unsmushed_images[ $attachment->pid ] = $meta;
$smushed_images[ $attachment->pid ] = $meta;
$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT pid, meta_data FROM {$wpdb->nggpictures} LIMIT %d, %d", $offset, $limit ) ); // Db call ok.
if ( ! empty( $smushed_images ) ) {
wp_cache_set( 'wp_smush_images_smushed', $smushed_images, 'nextgen', 300 );
if ( ! empty( $unsmushed_images ) ) {
wp_cache_set( 'wp_smush_images_unsmushed', $unsmushed_images, 'nextgen', 300 );
if ( 'smushed' === $type ) {
$smushed_images = ! empty( $smushed_images ) ? $smushed_images : $images;
if ( ! $smushed_images ) {
return $count ? count( $smushed_images ) : $smushed_images;
$unsmushed_images = ! empty( $unsmushed_images ) ? $unsmushed_images : $images;
if ( ! $unsmushed_images ) {
return $count ? count( $unsmushed_images ) : $unsmushed_images;
* Updated the global smush stats for NextGen gallery
* @param int $image_id Image ID.
* @param array $stats Compression stats fo respective image.
public function update_stats( $image_id, $stats ) {
$stats = ! empty( $stats['stats'] ) ? $stats['stats'] : '';
$smush_stats = $this->get_cache_smush_stats();
if ( ! empty( $stats ) ) {
$smush_stats['human'] = ! empty( $smush_stats['bytes'] ) ? size_format( $smush_stats['bytes'], 1 ) : '';
// Size of images before the compression.
$smush_stats['size_before'] = ! empty( $smush_stats['size_before'] ) ? ( $smush_stats['size_before'] + $stats['size_before'] ) : $stats['size_before'];
// Size of image after compression.
$smush_stats['size_after'] = ! empty( $smush_stats['size_after'] ) ? ( $smush_stats['size_after'] + $stats['size_after'] ) : $stats['size_after'];
$smush_stats['bytes'] = ! empty( $smush_stats['size_before'] ) && ! empty( $smush_stats['size_after'] ) ? ( $smush_stats['size_before'] - $smush_stats['size_after'] ) : 0;
// Compression Percentage.
$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'];
update_option( self::SMUSH_STATS_OPTION_ID, $smush_stats, false );
* Clears the object cache for NextGen stats.
public function clear_cache() {
wp_cache_delete( 'wp_smush_images_smushed', 'nextgen' );
wp_cache_delete( 'wp_smush_images_unsmushed', 'nextgen' );
wp_cache_delete( 'wp_smush_images', 'nextgen' );
* Get the attachment stats for a image
* @param object|array|int $id Attachment ID.
private function get_attachment_stats( $image ) {
// We'll get the image object in $image itself, else fetch it using Gallery Storage.
if ( is_numeric( $image ) ) {
// Registry Object for NextGen Gallery.
$registry = C_Component_Registry::get_instance();
// Gallery Storage Object.
$storage = $registry->get_utility( 'I_Gallery_Storage' );
$image = $storage->object->_image_mapper->find( $image );
$smush_savings = $this->get_image_smush_savings( $image );
$resize_savings = $this->get_image_resize_savings( $image );
return $this->recalculate_stats( 'add', $smush_savings, $resize_savings );
* Get the Nextgen Smush stats
* @return bool|mixed|void
public function get_smush_stats() {
if ( 0 == $this->total_count() || $this->get_smushed_count() < 1 ) {
delete_option( self::SMUSH_STATS_OPTION_ID );
// Check for the wp_smush_images in the 'nextgen' group.
$stats = $this->get_cache_smush_stats();
$size_before = (int) $this->get_array_value( $stats, 'size_before' );
if ( empty( $size_before ) ) {
$size_after = (int) $this->get_array_value( $stats, 'size_after' );
$stats['bytes'] = $size_before - $size_after;
$stats['bytes'] = $stats['bytes'] > 0 ? $stats['bytes'] : 0;
$stats['percent'] = ( $stats['bytes'] / $stats['size_before'] ) * 100;
$stats['percent'] = ! empty( $stats['percent'] ) ? round( $stats['percent'], 1 ) : 0;
$stats['human'] = size_format( $stats['bytes'], $stats['bytes'] >= 1024 ? 1 : 0 );
$smushed_stats = array_merge( $smushed_stats, $stats );
// Gotta remove the stats for re-smush ids.
if ( $this->get_reoptimize_list()->get_count() ) {
$resmush_stats = $this->get_stats_for_ids( $this->get_reoptimize_list()->get_ids() );
// Recalculate stats, Remove stats for resmush ids.
$smushed_stats = $this->recalculate_stats( 'sub', $smushed_stats, $resmush_stats );
* Get the combined stats for given Ids
* @param array $ids Image IDs.
* @return array|bool Array of Stats for the given ids
public function get_stats_for_ids( $ids = array() ) {
// Return if we don't have an array or no ids.
if ( ! is_array( $ids ) || empty( $ids ) ) {
// Initialize the Stats array.
// Calculate the stats, Expensive Operation.
foreach ( $ids as $id ) {
$image_stats = $this->get_attachment_stats( $id );
$stats = $this->recalculate_stats( 'add', $stats, $image_stats );
* Add/Subtract the values from 2nd array to First array
* This function is very specific to current requirement of stats re-calculation
* @param string $op 'add', 'sub' Add or Subtract the values.
* @param array $a1 First array.
* @param array $a2 Second array.
* @return array Return $a1
private function recalculate_stats( $op = 'add', $a1 = array(), $a2 = array() ) {
// If the first array itself is not set, return.
// Iterate over keys in first array, and add/subtract the values.
foreach ( $a1 as $k => $v ) {
// If the key is not set in 2nd array, skip.
if ( empty( $a2[ $k ] ) || ! in_array( $k, array( 'size_before', 'size_after' ) ) ) {
// Else perform the operation, Considers the value to be integer, doesn't performs a check.
} elseif ( 'add' === $op ) {
// Recalculate percentage and human savings.
$a1['bytes'] = $a1['size_before'] - $a1['size_after'];
$a1['percent'] = $a1['bytes'] > 0 ? round( ( $a1['bytes'] / $a1['size_before'] ) * 100, 1 ) : 0;
$a1['human'] = $a1['bytes'] > 0 ? size_format( $a1['bytes'], 1 ) : 0;
* Get Super smushed images from the given images array
* @param array $images Array of images containing metadata.
* @return array Array containing ids of supersmushed images
private function get_super_smushed_images( $images = array() ) {
if ( empty( $images ) ) {
$super_smushed = array();
// Iterate Over all the images.
foreach ( $images as $image_k => $image ) {
if ( empty( $image ) || ! is_array( $image ) || ! isset( $image['wp_smush'] ) ) {
// Check for lossy compression.
if ( ! empty( $image['wp_smush']['stats'] ) && ! empty( $image['wp_smush']['stats']['lossy'] ) ) {
$super_smushed[] = $image_k;
* Recalculate stats for the given smushed ids and update the cache
* Update Super smushed image ids
* @throws Exception Exception.
public function update_stats_cache() {
$smushed_images = $this->get_ngg_images( 'smushed' );
$stats = $this->get_stats_for_ids( $smushed_images );
$lossy = $this->get_super_smushed_images( $smushed_images );
if ( empty( $stats['bytes'] ) && ! empty( $stats['size_before'] ) ) {
$stats['bytes'] = $stats['size_before'] - $stats['size_after'];
$stats['human'] = size_format( ! empty( $stats['bytes'] ) ? $stats['bytes'] : 0 );
if ( ! empty( $stats['size_before'] ) ) {
$stats['percent'] = ( $stats['bytes'] / $stats['size_before'] ) * 100;
$stats['percent'] = round( $stats['percent'], 2 );
if ( is_array( WP_Smush::get_instance()->core()->nextgen->ng_admin->resmush_ids ) && is_array( $smushed_images ) ) {
$resmush_ids = array_intersect( WP_Smush::get_instance()->core()->nextgen->ng_admin->resmush_ids, array_keys( $smushed_images ) );
// If we have resmush ids, add it to db.
if ( ! empty( $resmush_ids ) ) {
// Update re-smush images to db.
$this->get_reoptimize_list()->update_ids( $resmush_ids );
// Update Super smushed images in db.
$this->get_supper_smushed_list()->update_ids( $lossy );
update_option( self::SMUSH_STATS_OPTION_ID, $stats, false );
public function get_reoptimize_list() {
return $this->reoptimize_list;
public function get_supper_smushed_list() {
return $this->supper_smushed_list;
public function get_supper_smushed_count() {
return count( $this->get_supper_smushed() );
private function get_supper_smushed() {
$super_smushed = $this->get_supper_smushed_list()->get_ids();
// If we have images to be resmushed, Update supersmush list.
$resmush_ids = $this->get_reoptimize_list()->get_ids();
if ( ! empty( $resmush_ids ) && ! empty( $super_smushed ) ) {
$super_smushed = array_diff( $super_smushed, $resmush_ids );
// If supersmushed images are more than total, clean it up.
if ( count( $super_smushed ) > self::total_count() ) {
$super_smushed = $this->cleanup_super_smush_data();
return (array) $super_smushed;
* Cleanup Super-smush images array against the all ids in gallery
* @return array|mixed|void
private function cleanup_super_smush_data() {