( $stats['sizes'][ $size_k ] ) && ! empty( $savings ) ) {
$stats['sizes'][ $size_k ]->bytes = $stats['sizes'][ $size_k ]->bytes + $savings['bytes'];
$stats['sizes'][ $size_k ]->size_before = $stats['sizes'][ $size_k ]->size_before > $savings['size_before'] ? $stats['sizes'][ $size_k ]->size_before : $savings['size_before'];
$stats['sizes'][ $size_k ]->percent = ! empty( $stats['sizes'][ $size_k ]->bytes ) && $stats['sizes'][ $size_k ]->size_before > 0 ? ( $stats['sizes'][ $size_k ]->bytes / $stats['sizes'][ $size_k ]->size_before ) * 100 : $stats['sizes'][ $size_k ]->percent;
$stats['sizes'][ $size_k ]->percent = round( $stats['sizes'][ $size_k ]->percent, 1 );
}
}
return $this->total_compression( $stats );
}
/**
* Iterate over all the size stats and calculate the total stats
*
* @param array $stats Stats array.
*
* @return mixed
*/
public function total_compression( $stats ) {
$stats['stats']['size_before'] = 0;
$stats['stats']['size_after'] = 0;
$stats['stats']['time'] = 0;
foreach ( $stats['sizes'] as $size_stats ) {
$stats['stats']['size_before'] += ! empty( $size_stats->size_before ) ? $size_stats->size_before : 0;
$stats['stats']['size_after'] += ! empty( $size_stats->size_after ) ? $size_stats->size_after : 0;
$stats['stats']['time'] += ! empty( $size_stats->time ) ? $size_stats->time : 0;
}
$stats['stats']['bytes'] = ! empty( $stats['stats']['size_before'] ) && $stats['stats']['size_before'] > $stats['stats']['size_after'] ? $stats['stats']['size_before'] - $stats['stats']['size_after'] : 0;
if ( ! empty( $stats['stats']['bytes'] ) && ! empty( $stats['stats']['size_before'] ) ) {
$stats['stats']['percent'] = ( $stats['stats']['bytes'] / $stats['stats']['size_before'] ) * 100;
}
return $stats;
}
/**
* Get all the attachment meta, sum up the stats and return
*
* @param bool $force_update Whether to forcefully update the cache.
*
* @return array|bool|mixed
*/
private function global_stats( $force_update = false ) {
$stats = get_option( 'smush_global_stats' );
// Remove id from global stats stored in db.
if ( ! $force_update && ! empty( $stats ) && isset( $stats['size_before'] ) ) {
if ( isset( $stats['id'] ) ) {
unset( $stats['id'] );
}
return $stats;
}
global $wpdb;
$smush_data = array(
'size_before' => 0,
'size_after' => 0,
'percent' => 0,
'human' => 0,
'bytes' => 0,
'total_images' => 0,
);
$offset = 0;
$supersmushed = 0;
$query_next = true;
while ( $query_next ) {
$global_data = $wpdb->get_results(
$wpdb->prepare(
"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key=%s GROUP BY post_id LIMIT %d, %d",
Modules\Smush::$smushed_meta_key,
$offset,
$this->query_limit
)
); // Db call ok; no-cache ok.
// If we didn't got any results.
if ( ! $global_data ) {
break;
}
foreach ( $global_data as $data ) {
// Skip attachment, if not in attachment list.
if ( ! in_array( $data->post_id, $this->attachments, true ) ) {
continue;
}
$smush_data['id'][] = $data->post_id;
if ( ! empty( $data->meta_value ) ) {
$meta = maybe_unserialize( $data->meta_value );
if ( ! empty( $meta['stats'] ) ) {
// Check for lossy compression.
if ( true === $meta['stats']['lossy'] ) {
$supersmushed++;
}
// If the image was optimised.
if ( ! empty( $meta['stats'] ) && $meta['stats']['size_before'] >= $meta['stats']['size_after'] ) {
// Total Image Smushed.
$smush_data['total_images'] += ! empty( $meta['sizes'] ) ? count( $meta['sizes'] ) : 0;
$smush_data['size_before'] += ! empty( $meta['stats']['size_before'] ) ? (int) $meta['stats']['size_before'] : 0;
$smush_data['size_after'] += ! empty( $meta['stats']['size_after'] ) ? (int) $meta['stats']['size_after'] : 0;
}
}
}
}
$smush_data['bytes'] = $smush_data['size_before'] - $smush_data['size_after'];
// Update the offset.
$offset += $this->query_limit;
// Compare the Offset value to total images.
if ( ! empty( $this->total_count ) && $this->total_count <= $offset ) {
$query_next = false;
}
}
// Add directory smush image bytes.
if ( ! empty( $this->dir_stats['bytes'] ) && $this->dir_stats['bytes'] > 0 ) {
$smush_data['bytes'] += $this->dir_stats['bytes'];
}
// Add directory smush image total size.
if ( ! empty( $this->dir_stats['orig_size'] ) && $this->dir_stats['orig_size'] > 0 ) {
$smush_data['size_before'] += $this->dir_stats['orig_size'];
}
// Add directory smush saved size.
if ( ! empty( $this->dir_stats['image_size'] ) && $this->dir_stats['image_size'] > 0 ) {
$smush_data['size_after'] += $this->dir_stats['image_size'];
}
// Add directory smushed images.
if ( ! empty( $this->dir_stats['optimised'] ) && $this->dir_stats['optimised'] > 0 ) {
$smush_data['total_images'] += $this->dir_stats['optimised'];
}
// Resize Savings.
$smush_data['resize_count'] = $this->get_savings( 'resize', false, false, true );
$resize_savings = $this->get_savings( 'resize', false );
$smush_data['resize_savings'] = ! empty( $resize_savings['bytes'] ) ? $resize_savings['bytes'] : 0;
// Conversion Savings.
$conversion_savings = $this->get_savings( 'pngjpg', false );
$smush_data['conversion_savings'] = ! empty( $conversion_savings['bytes'] ) ? $conversion_savings['bytes'] : 0;
if ( ! isset( $smush_data['bytes'] ) || $smush_data['bytes'] < 0 ) {
$smush_data['bytes'] = 0;
}
// Add the resize savings to bytes.
$smush_data['bytes'] += $smush_data['resize_savings'];
$smush_data['size_before'] += $resize_savings['size_before'];
$smush_data['size_after'] += $resize_savings['size_after'];
// Add Conversion Savings.
$smush_data['bytes'] += $smush_data['conversion_savings'];
$smush_data['size_before'] += $conversion_savings['size_before'];
$smush_data['size_after'] += $conversion_savings['size_after'];
if ( $smush_data['size_before'] > 0 ) {
$smush_data['percent'] = ( $smush_data['bytes'] / $smush_data['size_before'] ) * 100;
}
// Round off percentage.
$smush_data['percent'] = round( $smush_data['percent'], 1 );
// Human-readable format.
$smush_data['human'] = size_format(
$smush_data['bytes'],
( $smush_data['bytes'] >= 1024 ) ? 1 : 0
);
// Setup Smushed attachment IDs.
$this->smushed_attachments = ! empty( $smush_data['id'] ) ? $smush_data['id'] : '';
// Super Smushed attachment count.
$this->super_smushed = $supersmushed;
// Remove ids from stats.
unset( $smush_data['id'] );
// Update cache.
update_option( 'smush_global_stats', $smush_data, false );
return $smush_data;
}
/**
* Returns remaining count
*
* @return int
*/
public function remaining_count() {
$resmush_count = count( $this->resmush_ids );
$unsmushed_count = $this->total_count - $this->smushed_count - $this->skipped_count;
// Just a failsafe - can't have remaining value be a negative value.
$unsmushed_count = $unsmushed_count > 0 ? $unsmushed_count : 0;
return $resmush_count + $unsmushed_count;
}
/**
* Return the number of skipped attachments.
*
* @since 3.0
*
* @param bool $force Force data refresh.
*
* @return array
*/
private function skipped_count( $force ) {
$images = wp_cache_get( 'skipped_images', 'wp-smush' );
if ( ! $force && $images ) {
return $images;
}
global $wpdb;
$ignored_query = "SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = %s";
$args[] = Error_Handler::IGNORE_KEY;
// Animated files are considered ignored
$ignored_query .= ' OR meta_key = %s AND meta_value = %s';
$args[] = Error_Handler::ERROR_KEY;
$args[] = Error_Handler::ANIMATED_ERROR_CODE;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared
$images = $wpdb->get_col( $wpdb->prepare( $ignored_query, $args ) );
wp_cache_set( 'skipped_images', $images, 'wp-smush' );
return $images;
}
/**
* Checks every place where ignored or animated flag has been stored in the past to get a count.
*
* TODO: this is meant to be a temporary method to be used until the new stats are adopted. Remove in a few versions.
*
* @return int
*/
private function get_skipped_count() {
global $wpdb;
$animated_key = Media_Item::ANIMATED_META_KEY;
$ignored_key = Media_Item::IGNORED_META_KEY;
$error_meta_key = Error_Handler::ERROR_KEY;
$animated_error_value = 'animated';
$mime_types = ( new Smush_File() )->get_supported_mime_types();
$mime_types = implode( "','", $mime_types );
$query = $wpdb->prepare(
"SELECT COUNT(DISTINCT postmeta.post_id) FROM $wpdb->postmeta as postmeta
INNER JOIN $wpdb->posts as posts
ON postmeta.post_id = posts.ID AND posts.post_type = 'attachment'
AND posts.post_mime_type IN ('". $mime_types ."')
WHERE meta_key = %s
OR meta_key = %s
OR (meta_key = %s AND meta_value = %s)",
$ignored_key,
$animated_key,
$error_meta_key,
$animated_error_value
);
return (int) $wpdb->get_var( $query );
}
/**
* On sites where the new scan has never been run, this method is meant act as a fallback.
*
* TODO: this is meant to be a temporary method to be used until the new stats are adopted. Remove in a few versions.
*
* @return array
*/
public function get_backup_global_stats() {
$backup_stats = wp_cache_get( 'backup_global_stats', 'wp-smush' );
if ( empty( $backup_stats ) ) {
$backup_stats = $this->fetch_backup_global_stats();
wp_cache_set( 'backup_global_stats', $backup_stats, 'wp-smush' );
}
return $backup_stats;
}
public function fetch_backup_global_stats() {
$stats = get_option( 'smush_global_stats' );
$array_utils = new Array_Utils();
$savings_percent = $array_utils->get_array_value( $stats, 'percent' );
$query = new Media_Item_Query();
$image_attachment_count = $query->get_image_attachment_count();
$smushed_count = $query->get_smushed_count();
$skipped_count = $this->get_skipped_count();
$total_optimizable_items_count = $image_attachment_count - $skipped_count;
$total_optimizable_items_count = $total_optimizable_items_count > 0 ? $total_optimizable_items_count : 0;
$unsmushed_count = $image_attachment_count - $smushed_count - $skipped_count;
$unsmushed_count = $unsmushed_count > 0 ? $unsmushed_count : 0;
$resmush_count = count( (array) get_option( 'wp-smush-resmush-list', array() ) );
$remaining_count = $unsmushed_count + $resmush_count;
$bytes = (int) $array_utils->get_array_value( $stats, 'bytes' );
$human_bytes = size_format(
$bytes,
$bytes >= 1024 ? 1 : 0
);
$resize_savings = (int) $array_utils->get_array_value( $stats, 'resize_savings' );
$resize_savings_human = size_format(
$resize_savings,
$resize_savings >= 1024 ? 1 : 0
);
$conversion_savings = (int) $array_utils->get_array_value( $stats, 'conversion_savings' );
$conversion_savings_human = size_format(
$conversion_savings,
$conversion_savings >= 1024 ? 1 : 0
);
list( $percent_optimized, $percent_metric, $grade ) = $this->get_grade_data( $remaining_count, $image_attachment_count, $skipped_count );
return array(
'stats_updated_timestamp' => false,
'is_outdated' => true,
'count_supersmushed' => $query->get_lossy_count(),
'count_smushed' => $smushed_count,
'count_total' => $total_optimizable_items_count,
'count_images' => (int) $array_utils->get_array_value( $stats, 'total_images' ),
'count_resize' => (int) $array_utils->get_array_value( $stats, 'resize_count' ),
'count_skipped' => $skipped_count,
'unsmushed' => array(),
'count_unsmushed' => $unsmushed_count,
'resmush' => array(),
'count_resmush' => $resmush_count,
'size_before' => (int) $array_utils->get_array_value( $stats, 'size_before' ),
'size_after' => (int) $array_utils->get_array_value( $stats, 'size_after' ),
'savings_bytes' => $bytes,
'human_bytes' => $human_bytes,
'savings_resize' => $resize_savings,
'savings_resize_human' => $resize_savings_human,
'savings_conversion' => $conversion_savings,
'savings_conversion_human'=> $conversion_savings_human,
'savings_dir_smush' => $this->dir_stats,
'savings_percent' => $savings_percent > 0 ? number_format_i18n( $savings_percent, 1 ) : 0,
'percent_grade' => $grade,
'percent_metric' => $percent_metric,
'percent_optimized' => $percent_optimized,
'remaining_count' => $remaining_count,
);
}
/**
* Returns an array that can be consumed by the JS
*
* TODO: When we have rewritten the frontend of the plugin we can directly use {@see Global_Stats::to_array()} instead
*
* @return array
*/
public function get_global_stats() {
$global_stats = Global_Stats::get();
if ( empty( $global_stats->get_stats_update_started_timestamp() ) ) {
// A scan was never started, use the old stats
return $this->get_backup_global_stats();
}
$total_stats = $global_stats->get_sum_of_optimization_global_stats();
/**
* @var $smush_stats Smush_Optimization_Global_Stats
*/
$smush_stats = $global_stats->get_persistable_stats_for_optimization( Smush_Optimization::KEY )
->get_stats();
$resize_stats = $global_stats->get_persistable_stats_for_optimization( Resize_Optimization::KEY )
->get_stats();
$png2jpg_stats = $global_stats->get_persistable_stats_for_optimization( Png2Jpg_Optimization::KEY )
->get_stats();
return array(
'stats_updated_timestamp' => $global_stats->get_stats_updated_timestamp(),
'is_outdated' => $global_stats->is_outdated(),
'count_supersmushed' => $smush_stats->get_lossy_count(),
'count_smushed' => $smush_stats->get_count(),
'count_total' => $global_stats->get_total_optimizable_items_count(),
'count_images' => $global_stats->get_optimized_images_count(),
'count_resize' => $resize_stats->get_count(),
'count_skipped' => $global_stats->get_skipped_count(),
'unsmushed' => $global_stats->get_optimize_list()->get_ids(),
'count_unsmushed' => $global_stats->get_optimize_list()->get_count(),
'resmush' => $global_stats->get_redo_ids(),
'count_resmush' => $global_stats->get_redo_count(),
'size_before' => $total_stats->get_size_before(),
'size_after' => $total_stats->get_size_after(),
'savings_bytes' => $total_stats->get_bytes(),
'human_bytes' => $total_stats->get_human_bytes(),
'savings_resize' => $resize_stats->get_bytes(),
'savings_resize_human' => $resize_stats->get_human_bytes(),
'savings_conversion' => $png2jpg_stats->get_bytes(),
'savings_conversion_human' => $png2jpg_stats->get_human_bytes(),
'savings_dir_smush' => $this->dir_stats,
'savings_percent' => $total_stats->get_percent() > 0 ? number_format_i18n( $total_stats->get_percent(), 1 ) : 0,
'percent_grade' => $global_stats->get_grade_class(),
'percent_metric' => $global_stats->get_percent_metric(),
'percent_optimized' => $global_stats->get_percent_optimized(),
'remaining_count' => $global_stats->get_remaining_count(),
);
}
/**
* @return int
*/
public function get_query_limit() {
return $this->query_limit;
}
/**
* @param int $query_limit
*/
public function set_query_limit( $query_limit ) {
$this->query_limit = $query_limit;
return $this;
}
/**
* @return int
*/
public function get_max_rows() {
return $this->max_rows;
}
/**
* @param int $max_rows
*/
public function set_max_rows( $max_rows ) {
$this->max_rows = $max_rows;
return $this;
}
/**
* Get grade data (percent optimized and class name) for the score widget in summary meta box.
*
* @return array
* @since 3.12.0 Moved it from Abstract_Summary_Page for reuse.
*
* @since 3.10.0
*
*/
public function get_grade_data( $total_images_to_smush, $total_count, $skipped_count ) {
$total_images = $total_count - $skipped_count;
$percent_optimized = 0;
if ( 0 === $total_images ) {
$grade = 'sui-grade-dismissed';
} elseif ( $total_images === $total_images_to_smush ) {
$grade = 'sui-grade-f';
} else {
$percent_optimized = floor( ( $total_images - $total_images_to_smush ) * 100 / $total_images );
$grade = 'sui-grade-f';
if ( $percent_optimized >= 60 && $percent_optimized < 90 ) {
$grade = 'sui-grade-c';
} elseif ( $percent_optimized >= 90 ) {
$grade = 'sui-grade-a';
}
}
// Don't let percentage go beyond 100 or less than 0
if ( $percent_optimized > 100 ) {
$percent_optimized = 100;
} elseif ( $percent_optimized < 0 ) {
$percent_optimized = 0;
}
return array(
$percent_optimized,
0.0 === (float) $percent_optimized ? 100 : $percent_optimized,
$grade,
);
}
/**
* Get resmush ids.
*
* @return array
*/
public function get_resmush_ids() {
if ( $this->resmush_ids ) {
return $this->resmush_ids;
}
return (array) get_option( 'wp-smush-resmush-list', array() );
}
}
Fatal error: Uncaught Error: Class 'Smush\Core\Stats' not found in /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/core/class-core.php:21
Stack trace:
#0 /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/wp-smush.php(338): require()
#1 [internal function]: WP_Smush->autoload('Smush\\Core\\Core')
#2 /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/wp-smush.php(359): spl_autoload_call('Smush\\Core\\Core')
#3 /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/wp-smush.php(279): WP_Smush->init()
#4 /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/wp-smush.php(257): WP_Smush->__construct()
#5 /home/deltakeprico/public_html/wp-includes/class-wp-hook.php(324): WP_Smush::get_instance('')
#6 /home/deltakeprico/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#7 /home/deltakeprico/public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#8 /home/deltakeprico/public_html/wp-settings.php(559): do_action('plugins_loaded')
#9 /home/de in /home/deltakeprico/public_html/wp-content/plugins/wp-smushit/core/class-core.php on line 21