first commit
This commit is contained in:
@@ -0,0 +1,451 @@
|
||||
<?php
|
||||
namespace Imagify\Optimization\Data;
|
||||
|
||||
use Imagify\Media\MediaInterface;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Abstract class used to handle the optimization data of "media groups" (aka attachments).
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
abstract class AbstractData implements DataInterface {
|
||||
|
||||
/**
|
||||
* Optimization data structure.
|
||||
* This is the format returned when we "get" optimization data from the DB.
|
||||
*
|
||||
* @var array
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @see $this->get_optimization_data()
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $default_optimization_data = [
|
||||
'status' => '',
|
||||
'level' => false,
|
||||
'sizes' => [],
|
||||
'stats' => [
|
||||
'original_size' => 0,
|
||||
'optimized_size' => 0,
|
||||
'percent' => 0,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* The media object.
|
||||
*
|
||||
* @var MediaInterface
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $media;
|
||||
|
||||
/**
|
||||
* Filesystem object.
|
||||
*
|
||||
* @var object Imagify_Filesystem
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $filesystem;
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @see self::constructor_accepts()
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $id An ID, or whatever type the constructor accepts.
|
||||
*/
|
||||
public function __construct( $id ) {
|
||||
// Set the Media instance.
|
||||
if ( $id instanceof MediaInterface ) {
|
||||
$this->media = $id;
|
||||
} elseif ( static::constructor_accepts( $id ) ) {
|
||||
$media_class = str_replace( '\\Optimization\\Data\\', '\\Media\\', get_called_class() );
|
||||
$media_class = '\\' . ltrim( $media_class, '\\' );
|
||||
$this->media = new $media_class( $id );
|
||||
} else {
|
||||
$this->media = false;
|
||||
}
|
||||
|
||||
$this->filesystem = \Imagify_Filesystem::get_instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the given entry can be accepted in the constructor.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $id Whatever.
|
||||
* @return bool
|
||||
*/
|
||||
public static function constructor_accepts( $id ) {
|
||||
if ( $id instanceof MediaInterface ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$media_class = str_replace( '\\Optimization\\Data\\', '\\Media\\', get_called_class() );
|
||||
$media_class = '\\' . ltrim( $media_class, '\\' );
|
||||
|
||||
return $media_class::constructor_accepts( $id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the media instance.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return MediaInterface|false
|
||||
*/
|
||||
public function get_media() {
|
||||
return $this->media;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the current media is valid.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_valid() {
|
||||
return $this->get_media() && $this->get_media()->is_valid();
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** OPTIMIZATION DATA ======================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Check if the main file is optimized (by Imagify).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if the media is optimized.
|
||||
*/
|
||||
public function is_optimized() {
|
||||
return 'success' === $this->get_optimization_status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the main file is optimized (NOT by Imagify).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if the media is optimized.
|
||||
*/
|
||||
public function is_already_optimized() {
|
||||
return 'already_optimized' === $this->get_optimization_status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the main file is optimized (by Imagify).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if the media is optimized.
|
||||
*/
|
||||
public function is_error() {
|
||||
return 'error' === $this->get_optimization_status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the media's optimization level.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return int|false The optimization level. False if not optimized.
|
||||
*/
|
||||
public function get_optimization_level() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $this->get_optimization_data();
|
||||
return $data['level'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the media's optimization status (success or error).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string The optimization status. An empty string if there is none.
|
||||
*/
|
||||
public function get_optimization_status() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$data = $this->get_optimization_data();
|
||||
return $data['status'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Count number of optimized sizes.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return int Number of optimized sizes.
|
||||
*/
|
||||
public function get_optimized_sizes_count() {
|
||||
$data = $this->get_optimization_data();
|
||||
$count = 0;
|
||||
|
||||
if ( ! $data['sizes'] ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$context_sizes = $this->get_media()->get_media_files();
|
||||
$data['sizes'] = array_intersect_key( $data['sizes'], $context_sizes );
|
||||
|
||||
if ( ! $data['sizes'] ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
foreach ( $data['sizes'] as $size ) {
|
||||
if ( ! empty( $size['success'] ) ) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the original media's size (weight).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $human_format True to display the image human format size (1Mb).
|
||||
* @param int $decimals Precision of number of decimal places.
|
||||
* @return string|int
|
||||
*/
|
||||
public function get_original_size( $human_format = true, $decimals = 2 ) {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return $human_format ? imagify_size_format( 0, $decimals ) : 0;
|
||||
}
|
||||
|
||||
$size = $this->get_optimization_data();
|
||||
$size = ! empty( $size['sizes']['full']['original_size'] ) ? $size['sizes']['full']['original_size'] : 0;
|
||||
|
||||
// If nothing in the database, try to get the info from the file.
|
||||
if ( ! $size ) {
|
||||
// Check for the backup file first.
|
||||
$filepath = $this->get_media()->get_backup_path();
|
||||
|
||||
if ( ! $filepath ) {
|
||||
// Try the original file then.
|
||||
$filepath = $this->get_media()->get_original_path();
|
||||
}
|
||||
|
||||
$size = $filepath ? $this->filesystem->size( $filepath ) : 0;
|
||||
}
|
||||
|
||||
if ( $human_format ) {
|
||||
return imagify_size_format( (int) $size, $decimals );
|
||||
}
|
||||
|
||||
return (int) $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file size of the full size file.
|
||||
* If the WebP size is available, it is used.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $human_format True to display the image human format size (1Mb).
|
||||
* @param int $decimals Precision of number of decimal places.
|
||||
* @param bool $use_webp Use the WebP size if available.
|
||||
* @return string|int
|
||||
*/
|
||||
public function get_optimized_size( $human_format = true, $decimals = 2, $use_webp = true ) {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return $human_format ? imagify_size_format( 0, $decimals ) : 0;
|
||||
}
|
||||
|
||||
$data = $this->get_optimization_data();
|
||||
$media = $this->get_media();
|
||||
|
||||
if ( $use_webp ) {
|
||||
$process_class_name = imagify_get_optimization_process_class_name( $media->get_context() );
|
||||
$webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' );
|
||||
}
|
||||
|
||||
if ( $use_webp && ! empty( $data['sizes'][ $webp_size_name ]['optimized_size'] ) ) {
|
||||
$size = (int) $data['sizes'][ $webp_size_name ]['optimized_size'];
|
||||
} elseif ( ! empty( $data['sizes']['full']['optimized_size'] ) ) {
|
||||
$size = (int) $data['sizes']['full']['optimized_size'];
|
||||
} else {
|
||||
$size = 0;
|
||||
}
|
||||
|
||||
if ( $size ) {
|
||||
return $human_format ? imagify_size_format( $size, $decimals ) : $size;
|
||||
}
|
||||
|
||||
// If nothing in the database, try to get the info from the file.
|
||||
$filepath = false;
|
||||
|
||||
if ( $use_webp && ! empty( $data['sizes'][ $webp_size_name ]['success'] ) ) {
|
||||
// Try with the WebP file first.
|
||||
$filepath = $media->get_raw_fullsize_path();
|
||||
$filepath = $filepath ? imagify_path_to_webp( $filepath ) : false;
|
||||
|
||||
if ( ! $filepath || ! $this->filesystem->exists( $filepath ) ) {
|
||||
$filepath = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $filepath ) {
|
||||
// No WebP? The full size then.
|
||||
$filepath = $media->get_fullsize_path();
|
||||
}
|
||||
|
||||
if ( ! $filepath ) {
|
||||
return $human_format ? imagify_size_format( 0, $decimals ) : 0;
|
||||
}
|
||||
|
||||
$size = (int) $this->filesystem->size( $filepath );
|
||||
|
||||
return $human_format ? imagify_size_format( $size, $decimals ) : $size;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** OPTIMIZATION STATS ====================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get one or all statistics of a specific size.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $size The thumbnail slug.
|
||||
* @param string $key The specific data slug.
|
||||
* @return array|string
|
||||
*/
|
||||
public function get_size_data( $size = 'full', $key = '' ) {
|
||||
$data = $this->get_optimization_data();
|
||||
|
||||
if ( ! isset( $data['sizes'][ $size ] ) ) {
|
||||
return $key ? '' : [];
|
||||
}
|
||||
|
||||
if ( ! $key ) {
|
||||
return $data['sizes'][ $size ];
|
||||
}
|
||||
|
||||
if ( ! isset( $data['sizes'][ $size ][ $key ] ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $data['sizes'][ $size ][ $key ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the overall statistics data or a specific one.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $key The specific data slug.
|
||||
* @return array|string
|
||||
*/
|
||||
public function get_stats_data( $key = '' ) {
|
||||
$data = $this->get_optimization_data();
|
||||
$stats = '';
|
||||
|
||||
if ( empty( $data['stats'] ) ) {
|
||||
return $key ? '' : [];
|
||||
}
|
||||
|
||||
if ( ! isset( $data['stats'][ $key ] ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $data['stats'][ $key ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optimized/original saving of the original image in percent.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return float A 2-decimals float.
|
||||
*/
|
||||
public function get_saving_percent() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return round( (float) 0, 2 );
|
||||
}
|
||||
|
||||
$process_class_name = imagify_get_optimization_process_class_name( $this->get_media()->get_context() );
|
||||
$webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' );
|
||||
|
||||
$percent = $this->get_size_data( $webp_size_name, 'percent' );
|
||||
|
||||
if ( ! $percent ) {
|
||||
$percent = $this->get_size_data( 'full', 'percent' );
|
||||
}
|
||||
|
||||
$percent = $percent ? $percent : 0;
|
||||
|
||||
return round( (float) $percent, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the overall optimized/original saving (original image + all thumbnails) in percent.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return float A 2-decimals float.
|
||||
*/
|
||||
public function get_overall_saving_percent() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return round( (float) 0, 2 );
|
||||
}
|
||||
|
||||
$percent = $this->get_stats_data( 'percent' );
|
||||
|
||||
return round( (float) $percent, 2 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
<?php
|
||||
namespace Imagify\Optimization\Data;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Optimization data class for the custom folders.
|
||||
* This class constructor accepts:
|
||||
* - A media ID (int).
|
||||
* - An array of data coming from the files DB table /!\
|
||||
* - An object of data coming from the files DB table /!\
|
||||
* - A \Imagify\Media\MediaInterface object.
|
||||
*
|
||||
* @since 1.9
|
||||
* @see Imagify\Media\CustomFolders
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class CustomFolders extends AbstractData {
|
||||
use \Imagify\Traits\MediaRowTrait;
|
||||
|
||||
/**
|
||||
* The attachment SQL DB class.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $db_class_name = 'Imagify_Files_DB';
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $id An ID, or whatever type the "Media" class constructor accepts.
|
||||
*/
|
||||
public function __construct( $id ) {
|
||||
parent::__construct( $id );
|
||||
|
||||
if ( ! $this->is_valid() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// This is required by MediaRowTrait.
|
||||
$this->id = $this->get_media()->get_id();
|
||||
|
||||
// In this context, the media data and the optimization data are stored in the same DB table, so, no need to request twice the DB.
|
||||
$this->row = $this->get_media()->get_row();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the whole media optimization data.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array The data. See parent method for details.
|
||||
*/
|
||||
public function get_optimization_data() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return $this->default_optimization_data;
|
||||
}
|
||||
|
||||
$row = array_merge( $this->get_row_db_instance()->get_column_defaults(), $this->get_row() );
|
||||
$data = $this->default_optimization_data;
|
||||
|
||||
$data['status'] = $row['status'];
|
||||
$data['level'] = $row['optimization_level'];
|
||||
$data['level'] = is_numeric( $data['level'] ) ? (int) $data['level'] : false;
|
||||
|
||||
if ( 'success' === $row['status'] ) {
|
||||
/**
|
||||
* Success.
|
||||
*/
|
||||
$data['sizes']['full'] = [
|
||||
'success' => true,
|
||||
'original_size' => $row['original_size'],
|
||||
'optimized_size' => $row['optimized_size'],
|
||||
'percent' => $row['percent'],
|
||||
];
|
||||
} elseif ( ! empty( $row['status'] ) ) {
|
||||
/**
|
||||
* Error.
|
||||
*/
|
||||
$data['sizes']['full'] = [
|
||||
'success' => false,
|
||||
'error' => $row['error'],
|
||||
];
|
||||
}
|
||||
|
||||
if ( ! empty( $row['data']['sizes'] ) && is_array( $row['data']['sizes'] ) ) {
|
||||
unset( $row['data']['sizes']['full'] );
|
||||
$data['sizes'] = array_merge( $data['sizes'], $row['data']['sizes'] );
|
||||
$data['sizes'] = array_filter( $data['sizes'], 'is_array' );
|
||||
}
|
||||
|
||||
if ( empty( $data['sizes'] ) ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
foreach ( $data['sizes'] as $size_data ) {
|
||||
// Cast.
|
||||
if ( isset( $size_data['original_size'] ) ) {
|
||||
$size_data['original_size'] = (int) $size_data['original_size'];
|
||||
}
|
||||
if ( isset( $size_data['optimized_size'] ) ) {
|
||||
$size_data['optimized_size'] = (int) $size_data['optimized_size'];
|
||||
}
|
||||
if ( isset( $size_data['percent'] ) ) {
|
||||
$size_data['percent'] = round( $size_data['percent'], 2 );
|
||||
}
|
||||
// Stats.
|
||||
if ( ! empty( $size_data['original_size'] ) && ! empty( $size_data['optimized_size'] ) ) {
|
||||
$data['stats']['original_size'] += $size_data['original_size'];
|
||||
$data['stats']['optimized_size'] += $size_data['optimized_size'];
|
||||
}
|
||||
}
|
||||
|
||||
if ( $data['stats']['original_size'] && $data['stats']['optimized_size'] ) {
|
||||
$data['stats']['percent'] = $data['stats']['original_size'] - $data['stats']['optimized_size'];
|
||||
$data['stats']['percent'] = round( $data['stats']['percent'] / $data['stats']['original_size'] * 100, 2 );
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the optimization data, level, and status for a size.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $size The size name.
|
||||
* @param array $data The optimization data. See parent method for details.
|
||||
*/
|
||||
public function update_size_optimization_data( $size, array $data ) {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$old_data = array_merge( $this->get_reset_data(), $this->get_row() );
|
||||
|
||||
if ( 'full' === $size ) {
|
||||
/**
|
||||
* Original file.
|
||||
*/
|
||||
$old_data['optimization_level'] = $data['level'];
|
||||
$old_data['status'] = $data['status'];
|
||||
$old_data['modified'] = 0;
|
||||
|
||||
$file_path = $this->get_media()->get_fullsize_path();
|
||||
|
||||
if ( $file_path ) {
|
||||
$old_data['hash'] = md5_file( $file_path );
|
||||
}
|
||||
|
||||
if ( ! $data['success'] ) {
|
||||
/**
|
||||
* Error.
|
||||
*/
|
||||
$old_data['error'] = $data['error'];
|
||||
} else {
|
||||
/**
|
||||
* Success.
|
||||
*/
|
||||
$old_data['original_size'] = $data['original_size'];
|
||||
$old_data['optimized_size'] = $data['optimized_size'];
|
||||
$old_data['percent'] = $data['original_size'] - $data['optimized_size'];
|
||||
$old_data['percent'] = round( ( $old_data['percent'] / $data['original_size'] ) * 100, 2 );
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* WebP version or any other size.
|
||||
*/
|
||||
$old_data['data'] = ! empty( $old_data['data'] ) && is_array( $old_data['data'] ) ? $old_data['data'] : [];
|
||||
$old_data['data']['sizes'] = ! empty( $old_data['data']['sizes'] ) && is_array( $old_data['data']['sizes'] ) ? $old_data['data']['sizes'] : [];
|
||||
|
||||
if ( ! $data['success'] ) {
|
||||
/**
|
||||
* Error.
|
||||
*/
|
||||
$old_data['data']['sizes'][ $size ] = [
|
||||
'success' => false,
|
||||
'error' => $data['error'],
|
||||
];
|
||||
} else {
|
||||
/**
|
||||
* Success.
|
||||
*/
|
||||
$old_data['data']['sizes'][ $size ] = [
|
||||
'success' => true,
|
||||
'original_size' => $data['original_size'],
|
||||
'optimized_size' => $data['optimized_size'],
|
||||
'percent' => round( ( ( $data['original_size'] - $data['optimized_size'] ) / $data['original_size'] ) * 100, 2 ),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $old_data['data']['sizes'] ) && ( ! $old_data['data']['sizes'] || ! is_array( $old_data['data']['sizes'] ) ) ) {
|
||||
unset( $old_data['data']['sizes'] );
|
||||
}
|
||||
|
||||
$this->update_row( $old_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the media optimization data, level, and status.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function delete_optimization_data() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->update_row( $this->get_reset_data() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the optimization data for the given sizes.
|
||||
* If all sizes are removed, all optimization data is deleted.
|
||||
* Status and level are not modified nor removed if the "full" size is removed. This leaves the media in a Schrödinger state.
|
||||
*
|
||||
* @since 1.9.8
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $sizes A list of sizes to remove.
|
||||
*/
|
||||
public function delete_sizes_optimization_data( array $sizes ) {
|
||||
if ( ! $sizes || ! $this->is_valid() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data = array_merge( $this->get_reset_data(), $this->get_row() );
|
||||
|
||||
$data['data']['sizes'] = ! empty( $data['data']['sizes'] ) && is_array( $data['data']['sizes'] ) ? $data['data']['sizes'] : [];
|
||||
|
||||
if ( ! $data['data']['sizes'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$remaining_sizes_data = array_diff_key( $data['data']['sizes'], array_flip( $sizes ) );
|
||||
|
||||
if ( ! $remaining_sizes_data ) {
|
||||
// All sizes have been removed: delete everything.
|
||||
$this->delete_optimization_data();
|
||||
return;
|
||||
}
|
||||
|
||||
if ( count( $remaining_sizes_data ) === count( $data['data']['sizes'] ) ) {
|
||||
// Nothing has been removed.
|
||||
return;
|
||||
}
|
||||
|
||||
$data['data']['sizes'] = $remaining_sizes_data;
|
||||
|
||||
$this->update_row( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default values used to reset optimization data.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array {
|
||||
* The default values related to the optimization.
|
||||
*
|
||||
* @type string $hash The file hash.
|
||||
* @type int $modified 0 to tell that the file has not been modified
|
||||
* @type int $optimized_size File size after optimization.
|
||||
* @type int $percent Saving optimized/original in percent.
|
||||
* @type int $optimization_level The optimization level.
|
||||
* @type string $status The status: success, already_optimized, error.
|
||||
* @type string $error An error message.
|
||||
* }
|
||||
*/
|
||||
protected function get_reset_data() {
|
||||
static $column_defaults;
|
||||
|
||||
if ( ! isset( $column_defaults ) ) {
|
||||
$column_defaults = $this->get_row_db_instance()->get_column_defaults();
|
||||
|
||||
// All DB columns that have `null` as default value, are Imagify data.
|
||||
foreach ( $column_defaults as $column_name => $value ) {
|
||||
if ( 'hash' === $column_name || 'modified' === $column_name || 'data' === $column_name ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( isset( $value ) ) {
|
||||
unset( $column_defaults[ $column_name ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$imagify_columns = $column_defaults;
|
||||
|
||||
// Also set the new file hash.
|
||||
$file_path = $this->get_media()->get_fullsize_path();
|
||||
|
||||
if ( $file_path ) {
|
||||
$imagify_columns['hash'] = md5_file( $file_path );
|
||||
}
|
||||
|
||||
return $imagify_columns;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
<?php
|
||||
namespace Imagify\Optimization\Data;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Interface to use to handle the optimization data of "media groups" (aka attachments).
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
interface DataInterface {
|
||||
|
||||
/**
|
||||
* Tell if the given entry can be accepted in the constructor.
|
||||
* For example it can include `is_numeric( $id )` if the constructor accepts integers.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $id Whatever.
|
||||
* @return bool
|
||||
*/
|
||||
public static function constructor_accepts( $id );
|
||||
|
||||
/**
|
||||
* Get the media instance.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return MediaInterface|false
|
||||
*/
|
||||
public function get_media();
|
||||
|
||||
/**
|
||||
* Tell if the current media is valid.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_valid();
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** OPTIMIZATION DATA ======================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Check if the main file is optimized (by Imagify).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if the media is optimized.
|
||||
*/
|
||||
public function is_optimized();
|
||||
|
||||
/**
|
||||
* Check if the main file is optimized (NOT by Imagify).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if the media is optimized.
|
||||
*/
|
||||
public function is_already_optimized();
|
||||
|
||||
/**
|
||||
* Check if the main file is optimized (by Imagify).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if the media is optimized.
|
||||
*/
|
||||
public function is_error();
|
||||
|
||||
/**
|
||||
* Get the whole media optimization data.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array {
|
||||
* The data.
|
||||
*
|
||||
* @type string $status The optimization status of the whole media: 'success', 'already_optimized', or 'error'.
|
||||
* It is the same as the main file’s status.
|
||||
* @type int|bool $level The optimization level (0=normal, 1=aggressive, 2=ultra). False if not set.
|
||||
* @type array $sizes {
|
||||
* A list of size data, keyed by size name, and containing:
|
||||
*
|
||||
* @type bool $success Whether the optimization has been successful.
|
||||
* If a success:
|
||||
* @type int $original_size The file size before optimization.
|
||||
* @type int $optimized_size The file size after optimization.
|
||||
* @type int $percent Saving in percent.
|
||||
* If an error or 'already_optimized':
|
||||
* @type string $error An error message.
|
||||
* }
|
||||
* @type array $stats {
|
||||
* @type int $original_size Overall size before optimization.
|
||||
* @type int $optimized_size Overall size after optimization.
|
||||
* @type int $percent Overall saving in percent.
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public function get_optimization_data();
|
||||
|
||||
/**
|
||||
* Update the optimization data, level, and status for a size.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $size The size name.
|
||||
* @param array $data {
|
||||
* The optimization data.
|
||||
*
|
||||
* @type int $level The optimization level.
|
||||
* @type string $status The status: 'success', 'already_optimized', 'error'.
|
||||
* @type bool $success True if successfully optimized. False on error or if already optimized.
|
||||
* @type string $error An error message.
|
||||
* @type int $original_size The weight of the file, before optimization.
|
||||
* @type int $optimized_size The weight of the file, after optimization.
|
||||
* }
|
||||
*/
|
||||
public function update_size_optimization_data( $size, array $data );
|
||||
|
||||
/**
|
||||
* Delete the media optimization data, level, and status.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function delete_optimization_data();
|
||||
|
||||
/**
|
||||
* Delete the optimization data for the given sizes.
|
||||
* If all sizes are removed, all optimization data is deleted.
|
||||
* Status and level are not modified nor removed if the "full" size is removed. This leaves the media in a Schrödinger state.
|
||||
*
|
||||
* @since 1.9.8
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $sizes A list of sizes to remove.
|
||||
*/
|
||||
public function delete_sizes_optimization_data( array $sizes );
|
||||
|
||||
/**
|
||||
* Get the media's optimization level.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return int|bool The optimization level. False if not optimized.
|
||||
*/
|
||||
public function get_optimization_level();
|
||||
|
||||
/**
|
||||
* Get the media's optimization status (success or error).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string The optimization status. An empty string if there is none.
|
||||
*/
|
||||
public function get_optimization_status();
|
||||
|
||||
/**
|
||||
* Count number of optimized sizes.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return int Number of optimized sizes.
|
||||
*/
|
||||
public function get_optimized_sizes_count();
|
||||
|
||||
/**
|
||||
* Get the original media's size (weight).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $human_format True to display the image human format size (1Mb).
|
||||
* @param int $decimals Precision of number of decimal places.
|
||||
* @return string|int
|
||||
*/
|
||||
public function get_original_size( $human_format = true, $decimals = 2 );
|
||||
|
||||
/**
|
||||
* Get the file size of the full size file.
|
||||
* If the WebP size is available, it is used.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $human_format True to display the image human format size (1Mb).
|
||||
* @param int $decimals Precision of number of decimal places.
|
||||
* @param bool $use_webp Use the WebP size if available.
|
||||
* @return string|int
|
||||
*/
|
||||
public function get_optimized_size( $human_format = true, $decimals = 2, $use_webp = true );
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** OPTIMIZATION STATS ====================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get one or all statistics of a specific size.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $size The thumbnail slug.
|
||||
* @param string $key The specific data slug.
|
||||
* @return array|string
|
||||
*/
|
||||
public function get_size_data( $size = 'full', $key = '' );
|
||||
|
||||
/**
|
||||
* Get the overall statistics data or a specific one.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $key The specific data slug.
|
||||
* @return array|string
|
||||
*/
|
||||
public function get_stats_data( $key = '' );
|
||||
|
||||
/**
|
||||
* Get the optimized/original saving of the original image in percent.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return float A 2-decimals float.
|
||||
*/
|
||||
public function get_saving_percent();
|
||||
|
||||
/**
|
||||
* Get the overall optimized/original saving (original image + all thumbnails) in percent.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return float A 2-decimals float.
|
||||
*/
|
||||
public function get_overall_saving_percent();
|
||||
}
|
||||
285
wp/wp-content/plugins/imagify/classes/Optimization/Data/Noop.php
Normal file
285
wp/wp-content/plugins/imagify/classes/Optimization/Data/Noop.php
Normal file
@@ -0,0 +1,285 @@
|
||||
<?php
|
||||
namespace Imagify\Optimization\Data;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Fallback class optimization data of "media groups" (aka attachments).
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Noop implements DataInterface {
|
||||
|
||||
/**
|
||||
* Tell if the given entry can be accepted in the constructor.
|
||||
* For example it can include `is_numeric( $id )` if the constructor accepts integers.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $id Whatever.
|
||||
* @return bool
|
||||
*/
|
||||
public static function constructor_accepts( $id ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the media instance.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return MediaInterface|false
|
||||
*/
|
||||
public function get_media() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the current media is valid.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_valid() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** OPTIMIZATION DATA ======================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Check if the main file is optimized (by Imagify).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if the media is optimized.
|
||||
*/
|
||||
public function is_optimized() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the main file is optimized (NOT by Imagify).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if the media is optimized.
|
||||
*/
|
||||
public function is_already_optimized() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the main file is optimized (by Imagify).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if the media is optimized.
|
||||
*/
|
||||
public function is_error() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the whole media optimization data.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array The data. See parent method for details.
|
||||
*/
|
||||
public function get_optimization_data() {
|
||||
return [
|
||||
'status' => '',
|
||||
'level' => false,
|
||||
'sizes' => [],
|
||||
'stats' => [
|
||||
'original_size' => 0,
|
||||
'optimized_size' => 0,
|
||||
'percent' => 0,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the optimization data, level, and status for a size.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $size The size name.
|
||||
* @param array $data The optimization data. See parent method for details.
|
||||
*/
|
||||
public function update_size_optimization_data( $size, array $data ) {}
|
||||
|
||||
/**
|
||||
* Delete the media optimization data, level, and status.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function delete_optimization_data() {}
|
||||
|
||||
/**
|
||||
* Delete the optimization data for the given sizes.
|
||||
* If all sizes are removed, all optimization data is deleted.
|
||||
* Status and level are not modified nor removed if the "full" size is removed. This leaves the media in a Schrödinger state.
|
||||
*
|
||||
* @since 1.9.8
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $sizes A list of sizes to remove.
|
||||
*/
|
||||
public function delete_sizes_optimization_data( array $sizes ) {}
|
||||
|
||||
/**
|
||||
* Get the media's optimization level.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return int|bool The optimization level. False if not optimized.
|
||||
*/
|
||||
public function get_optimization_level() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the media's optimization status (success or error).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string The optimization status. An empty string if there is none.
|
||||
*/
|
||||
public function get_optimization_status() {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Count number of optimized sizes.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return int Number of optimized sizes.
|
||||
*/
|
||||
public function get_optimized_sizes_count() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the original media's size (weight).
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $human_format True to display the image human format size (1Mb).
|
||||
* @param int $decimals Precision of number of decimal places.
|
||||
* @return string|int
|
||||
*/
|
||||
public function get_original_size( $human_format = true, $decimals = 2 ) {
|
||||
return $human_format ? imagify_size_format( 0, $decimals ) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file size of the full size file.
|
||||
* If the WebP size is available, it is used.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $human_format True to display the image human format size (1Mb).
|
||||
* @param int $decimals Precision of number of decimal places.
|
||||
* @param bool $use_webp Use the WebP size if available.
|
||||
* @return string|int
|
||||
*/
|
||||
public function get_optimized_size( $human_format = true, $decimals = 2, $use_webp = true ) {
|
||||
return $human_format ? imagify_size_format( 0, $decimals ) : 0;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** OPTIMIZATION STATS ====================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get one or all statistics of a specific size.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $size The thumbnail slug.
|
||||
* @param string $key The specific data slug.
|
||||
* @return array|string
|
||||
*/
|
||||
public function get_size_data( $size = 'full', $key = '' ) {
|
||||
return $key ? '' : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the overall statistics data or a specific one.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $key The specific data slug.
|
||||
* @return array|string
|
||||
*/
|
||||
public function get_stats_data( $key = '' ) {
|
||||
return $key ? '' : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optimized/original saving of the original image in percent.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return float A 2-decimals float.
|
||||
*/
|
||||
public function get_saving_percent() {
|
||||
return round( (float) 0, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the overall optimized/original saving (original image + all thumbnails) in percent.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return float A 2-decimals float.
|
||||
*/
|
||||
public function get_overall_saving_percent() {
|
||||
return round( (float) 0, 2 );
|
||||
}
|
||||
}
|
||||
202
wp/wp-content/plugins/imagify/classes/Optimization/Data/WP.php
Normal file
202
wp/wp-content/plugins/imagify/classes/Optimization/Data/WP.php
Normal file
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
namespace Imagify\Optimization\Data;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Optimization data class for the medias in the WP library.
|
||||
* This class constructor accepts:
|
||||
* - A post ID (int).
|
||||
* - A \WP_Post object.
|
||||
* - A \Imagify\Media\MediaInterface object.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class WP extends AbstractData {
|
||||
|
||||
/**
|
||||
* Get the whole media optimization data.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array The data. See parent method for details.
|
||||
*/
|
||||
public function get_optimization_data() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return $this->default_optimization_data;
|
||||
}
|
||||
|
||||
$id = $this->get_media()->get_id();
|
||||
|
||||
$data = get_post_meta( $id, '_imagify_data', true );
|
||||
$data = is_array( $data ) ? $data : [];
|
||||
|
||||
if ( isset( $data['sizes'] ) && ! is_array( $data['sizes'] ) ) {
|
||||
$data['sizes'] = [];
|
||||
}
|
||||
|
||||
if ( isset( $data['stats'] ) && ! is_array( $data['stats'] ) ) {
|
||||
$data['stats'] = [];
|
||||
}
|
||||
|
||||
$data = array_merge( $this->default_optimization_data, $data );
|
||||
|
||||
$data['status'] = get_post_meta( $id, '_imagify_status', true );
|
||||
$data['status'] = is_string( $data['status'] ) ? $data['status'] : '';
|
||||
|
||||
$data['level'] = get_post_meta( $id, '_imagify_optimization_level', true );
|
||||
$data['level'] = is_numeric( $data['level'] ) ? (int) $data['level'] : false;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the optimization data, level, and status for a size.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $size The size name.
|
||||
* @param array $data The optimization data. See parent method for details.
|
||||
*/
|
||||
public function update_size_optimization_data( $size, array $data ) {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$id = $this->get_media()->get_id();
|
||||
|
||||
if ( 'full' === $size ) {
|
||||
// Optimization level.
|
||||
update_post_meta( $id, '_imagify_optimization_level', $data['level'] );
|
||||
// Optimization status.
|
||||
update_post_meta( $id, '_imagify_status', $data['status'] );
|
||||
}
|
||||
|
||||
// Size data and stats.
|
||||
$old_data = get_post_meta( $id, '_imagify_data', true );
|
||||
$old_data = is_array( $old_data ) ? $old_data : [];
|
||||
|
||||
if ( ! isset( $old_data['sizes'] ) || ! is_array( $old_data['sizes'] ) ) {
|
||||
$old_data['sizes'] = [];
|
||||
}
|
||||
|
||||
if ( ! isset( $old_data['stats'] ) || ! is_array( $old_data['stats'] ) ) {
|
||||
$old_data['stats'] = [];
|
||||
}
|
||||
|
||||
$old_data['stats'] = array_merge( [
|
||||
'original_size' => 0,
|
||||
'optimized_size' => 0,
|
||||
'percent' => 0,
|
||||
], $old_data['stats'] );
|
||||
|
||||
if ( ! $data['success'] ) {
|
||||
/**
|
||||
* Error.
|
||||
*/
|
||||
$old_data['sizes'][ $size ] = [
|
||||
'success' => false,
|
||||
'error' => $data['error'],
|
||||
];
|
||||
} else {
|
||||
/**
|
||||
* Success.
|
||||
*/
|
||||
$old_data['sizes'][ $size ] = [
|
||||
'success' => true,
|
||||
'original_size' => $data['original_size'],
|
||||
'optimized_size' => $data['optimized_size'],
|
||||
'percent' => round( ( ( $data['original_size'] - $data['optimized_size'] ) / $data['original_size'] ) * 100, 2 ),
|
||||
];
|
||||
|
||||
$old_data['stats']['original_size'] += $data['original_size'];
|
||||
$old_data['stats']['optimized_size'] += $data['optimized_size'];
|
||||
$old_data['stats']['percent'] = round( ( ( $old_data['stats']['original_size'] - $old_data['stats']['optimized_size'] ) / $old_data['stats']['original_size'] ) * 100, 2 );
|
||||
}
|
||||
|
||||
update_post_meta( $id, '_imagify_data', $old_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the media optimization data, level, and status.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function delete_optimization_data() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$id = $this->get_media()->get_id();
|
||||
|
||||
delete_post_meta( $id, '_imagify_data' );
|
||||
delete_post_meta( $id, '_imagify_status' );
|
||||
delete_post_meta( $id, '_imagify_optimization_level' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the optimization data for the given sizes.
|
||||
* If all sizes are removed, all optimization data is deleted.
|
||||
* Status and level are not modified nor removed if the "full" size is removed. This leaves the media in a Schrödinger state.
|
||||
*
|
||||
* @since 1.9.8
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $sizes A list of sizes to remove.
|
||||
*/
|
||||
public function delete_sizes_optimization_data( array $sizes ) {
|
||||
if ( ! $sizes || ! $this->is_valid() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$media_id = $this->get_media()->get_id();
|
||||
$data = get_post_meta( $media_id, '_imagify_data', true );
|
||||
|
||||
if ( empty( $data['sizes'] ) || ! is_array( $data['sizes'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$remaining_sizes_data = array_diff_key( $data['sizes'], array_flip( $sizes ) );
|
||||
|
||||
if ( ! $remaining_sizes_data ) {
|
||||
// All sizes have been removed: delete everything.
|
||||
$this->delete_optimization_data();
|
||||
return;
|
||||
}
|
||||
|
||||
if ( count( $remaining_sizes_data ) === count( $data['sizes'] ) ) {
|
||||
// Nothing has been removed.
|
||||
return;
|
||||
}
|
||||
|
||||
$data['sizes'] = $remaining_sizes_data;
|
||||
|
||||
// Update stats.
|
||||
$data['stats'] = [
|
||||
'original_size' => 0,
|
||||
'optimized_size' => 0,
|
||||
'percent' => 0,
|
||||
];
|
||||
|
||||
foreach ( $data['sizes'] as $size_data ) {
|
||||
if ( empty( $size_data['success'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data['stats']['original_size'] += $size_data['original_size'];
|
||||
$data['stats']['optimized_size'] += $size_data['optimized_size'];
|
||||
}
|
||||
|
||||
$data['stats']['percent'] = round( ( ( $data['stats']['original_size'] - $data['stats']['optimized_size'] ) / $data['stats']['original_size'] ) * 100, 2 );
|
||||
|
||||
update_post_meta( $media_id, '_imagify_data', $data );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user