Plugins
This commit is contained in:
@@ -1,468 +0,0 @@
|
||||
<?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' => '',
|
||||
'message' => '',
|
||||
'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 Nextgen 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_nextgen Use the Nextgen size if available.
|
||||
* @return string|int
|
||||
*/
|
||||
public function get_optimized_size( $human_format = true, $decimals = 2, $use_nextgen = true ) {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return $human_format ? imagify_size_format( 0, $decimals ) : 0;
|
||||
}
|
||||
|
||||
$data = $this->get_optimization_data();
|
||||
$media = $this->get_media();
|
||||
$format = 'webp';
|
||||
|
||||
$process_class_name = imagify_get_optimization_process_class_name( $media->get_context() );
|
||||
$nextgen_avif_size_name = 'full' . constant( $process_class_name . '::AVIF_SUFFIX' );
|
||||
$nextgen_webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' );
|
||||
|
||||
$size = 0;
|
||||
|
||||
if ( $use_nextgen ) {
|
||||
/**Checking for success status before size, some cases the response is false
|
||||
* because the image is already compressed, or we have a connection timed out
|
||||
* */
|
||||
$size = ! empty( $data['sizes'][ $nextgen_webp_size_name ] ) && $data['sizes'][ $nextgen_webp_size_name ]['success'] ?
|
||||
(int) $data['sizes'][ $nextgen_webp_size_name ]['optimized_size'] : 0;
|
||||
if ( ! empty( $data['sizes'][ $nextgen_avif_size_name ]['optimized_size'] ) &&
|
||||
$data['sizes'][ $nextgen_avif_size_name ] ) {
|
||||
$size = (int) $data['sizes'][ $nextgen_avif_size_name ]['optimized_size'];
|
||||
}
|
||||
} elseif ( ! empty( $data['sizes']['full']['optimized_size'] ) ) {
|
||||
$size = (int) $data['sizes']['full']['optimized_size'];
|
||||
}
|
||||
|
||||
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_nextgen ) {
|
||||
if ( ! empty( $data['sizes'][ $nextgen_avif_size_name ]['success'] ) ) {
|
||||
$format = 'avif';
|
||||
}
|
||||
// Try with the Nextgen file first.
|
||||
$filepath = $media->get_raw_fullsize_path();
|
||||
$filepath = $filepath ? imagify_path_to_nextgen( $filepath, $format ) : false;
|
||||
|
||||
if ( ! $filepath || ! $this->filesystem->exists( $filepath ) ) {
|
||||
$filepath = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $filepath ) {
|
||||
// No Nextgen? 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() );
|
||||
$nextgen_webp_size_name = 'full' . constant( $process_class_name . '::WEBP_SUFFIX' );
|
||||
$nextgen_avif_size_name = 'full' . constant( $process_class_name . '::AVIF_SUFFIX' );
|
||||
|
||||
$percent = $this->get_size_data( $nextgen_avif_size_name, 'percent' );
|
||||
|
||||
// Check for webp version if avif is not found.
|
||||
if ( ! $percent ) {
|
||||
$percent = $this->get_size_data( $nextgen_webp_size_name, 'percent' );
|
||||
}
|
||||
|
||||
if ( ! $percent ) {
|
||||
$percent = $this->get_size_data( 'full', '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 );
|
||||
}
|
||||
}
|
||||
@@ -1,320 +0,0 @@
|
||||
<?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 ( key_exists( 'message', $data ) ) {
|
||||
$old_data['message'] = $data['message'];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,275 +0,0 @@
|
||||
<?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 Nextgen 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_nextgen Use the Nextgen size if available.
|
||||
* @return string|int
|
||||
*/
|
||||
public function get_optimized_size( $human_format = true, $decimals = 2, $use_nextgen = 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();
|
||||
}
|
||||
@@ -1,285 +0,0 @@
|
||||
<?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 Nextgen 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_nextgen Use the Nextgen size if available.
|
||||
* @return string|int
|
||||
*/
|
||||
public function get_optimized_size( $human_format = true, $decimals = 2, $use_nextgen = 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 );
|
||||
}
|
||||
}
|
||||
@@ -1,207 +0,0 @@
|
||||
<?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,
|
||||
'message' => '',
|
||||
], $old_data['stats'] );
|
||||
|
||||
if ( key_exists( 'message', $data ) ) {
|
||||
$old_data['message'] = $data['message'];
|
||||
}
|
||||
|
||||
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 );
|
||||
}
|
||||
}
|
||||
@@ -1,913 +0,0 @@
|
||||
<?php
|
||||
namespace Imagify\Optimization;
|
||||
|
||||
use Imagify_Requirements;
|
||||
|
||||
/**
|
||||
* A generic optimization class focussed on the file itself.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class File {
|
||||
|
||||
/**
|
||||
* Absolute path to the file.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* Tell if the file is an image.
|
||||
*
|
||||
* @var bool
|
||||
* @since 1.9
|
||||
* @see $this->is_image()
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $is_image;
|
||||
|
||||
/**
|
||||
* Store the file mime type + file extension (if the file is supported).
|
||||
*
|
||||
* @var array
|
||||
* @since 1.9
|
||||
* @see $this->get_file_type()
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $file_type;
|
||||
|
||||
/**
|
||||
* Filesystem object.
|
||||
*
|
||||
* @var \Imagify_Filesystem
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $filesystem;
|
||||
|
||||
/**
|
||||
* The editor instance used to resize the file.
|
||||
*
|
||||
* @var \WP_Image_Editor_Imagick|\WP_Image_Editor_GD|WP_Error.
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $editor;
|
||||
|
||||
/**
|
||||
* Used to cache the plugin’s options.
|
||||
*
|
||||
* @var array
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_path Absolute path to the file.
|
||||
*/
|
||||
public function __construct( $file_path ) {
|
||||
$this->path = $file_path;
|
||||
$this->filesystem = \Imagify_Filesystem::get_instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the file is valid.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_valid() {
|
||||
return (bool) $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the file can be processed.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function can_be_processed() {
|
||||
if ( ! $this->path ) {
|
||||
return new \WP_Error( 'empty_path', __( 'File path is empty.', 'imagify' ) );
|
||||
}
|
||||
|
||||
if ( ! empty( $this->filesystem->errors->errors ) ) {
|
||||
return new \WP_Error( 'filesystem_error', __( 'Filesystem error.', 'imagify' ), $this->filesystem->errors );
|
||||
}
|
||||
|
||||
if ( ! $this->filesystem->exists( $this->path ) ) {
|
||||
return new \WP_Error(
|
||||
'not_exists',
|
||||
sprintf(
|
||||
/* translators: %s is a file path. */
|
||||
__( 'The file %s does not seem to exist.', 'imagify' ),
|
||||
'<code>' . esc_html( $this->filesystem->make_path_relative( $this->path ) ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! $this->filesystem->is_file( $this->path ) ) {
|
||||
return new \WP_Error(
|
||||
'not_a_file',
|
||||
sprintf(
|
||||
/* translators: %s is a file path. */
|
||||
__( 'This does not seem to be a file: %s.', 'imagify' ),
|
||||
'<code>' . esc_html( $this->filesystem->make_path_relative( $this->path ) ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! $this->filesystem->is_writable( $this->path ) ) {
|
||||
return new \WP_Error(
|
||||
'not_writable',
|
||||
sprintf(
|
||||
/* translators: %s is a file path. */
|
||||
__( 'The file %s does not seem to be writable.', 'imagify' ),
|
||||
'<code>' . esc_html( $this->filesystem->make_path_relative( $this->path ) ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$parent_folder = $this->filesystem->dir_path( $this->path );
|
||||
|
||||
if ( ! $this->filesystem->is_writable( $parent_folder ) ) {
|
||||
return new \WP_Error(
|
||||
'folder_not_writable',
|
||||
sprintf(
|
||||
/* translators: %s is a file path. */
|
||||
__( 'The folder %s does not seem to be writable.', 'imagify' ),
|
||||
'<code>' . esc_html( $this->filesystem->make_path_relative( $parent_folder ) ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** EDITION ================================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Resize (and rotate) an image if it is bigger than the maximum width provided.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $dimensions {
|
||||
* Array of image dimensions.
|
||||
*
|
||||
* @type int $width The image width.
|
||||
* @type int $height The image height.
|
||||
* }
|
||||
* @param int $max_width Maximum width to resize to.
|
||||
* @return string|WP_Error Path the the resized image. A WP_Error object on failure.
|
||||
*/
|
||||
public function resize( $dimensions = [], $max_width = 0 ) {
|
||||
$can_be_processed = $this->can_be_processed();
|
||||
|
||||
if ( is_wp_error( $can_be_processed ) ) {
|
||||
return $can_be_processed;
|
||||
}
|
||||
|
||||
if ( ! $max_width ) {
|
||||
return new \WP_Error(
|
||||
'no_resizing_threshold',
|
||||
__( 'No threshold provided for resizing.', 'imagify' )
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! $this->is_image() ) {
|
||||
return new \WP_Error(
|
||||
'not_an_image',
|
||||
sprintf(
|
||||
/* translators: %s is a file path. */
|
||||
__( 'The file %s does not seem to be an image, and cannot be resized.', 'imagify' ),
|
||||
'<code>' . esc_html( $this->filesystem->make_path_relative( $this->path ) ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$editor = $this->get_editor();
|
||||
|
||||
if ( is_wp_error( $editor ) ) {
|
||||
return $editor;
|
||||
}
|
||||
|
||||
// Try to correct the auto-rotation if the info is available.
|
||||
if ( $this->filesystem->can_get_exif() && 'image/jpeg' === $this->get_mime_type() ) {
|
||||
$exif = $this->filesystem->get_image_exif( $this->path );
|
||||
$orientation = isset( $exif['Orientation'] ) ? (int) $exif['Orientation'] : 1;
|
||||
|
||||
switch ( $orientation ) {
|
||||
case 2:
|
||||
// Flip horizontally.
|
||||
$editor->flip( true, false );
|
||||
break;
|
||||
case 3:
|
||||
// Rotate 180 degrees or flip horizontally and vertically.
|
||||
// Flipping seems faster/uses less resources.
|
||||
$editor->flip( true, true );
|
||||
break;
|
||||
case 4:
|
||||
// Flip vertically.
|
||||
$editor->flip( false, true );
|
||||
break;
|
||||
case 5:
|
||||
// Rotate 90 degrees counter-clockwise and flip vertically.
|
||||
$result = $editor->rotate( 90 );
|
||||
|
||||
if ( ! is_wp_error( $result ) ) {
|
||||
$editor->flip( false, true );
|
||||
}
|
||||
break;
|
||||
case 6:
|
||||
// Rotate 90 degrees clockwise (270 counter-clockwise).
|
||||
$editor->rotate( 270 );
|
||||
break;
|
||||
case 7:
|
||||
// Rotate 90 degrees counter-clockwise and flip horizontally.
|
||||
$result = $editor->rotate( 90 );
|
||||
|
||||
if ( ! is_wp_error( $result ) ) {
|
||||
$editor->flip( true, false );
|
||||
}
|
||||
break;
|
||||
case 8:
|
||||
// Rotate 90 degrees counter-clockwise.
|
||||
$editor->rotate( 90 );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $dimensions ) {
|
||||
$dimensions = $this->get_dimensions();
|
||||
}
|
||||
|
||||
// Prevent removal of the exif data when resizing (only works with Imagick).
|
||||
add_filter( 'image_strip_meta', '__return_false', 789 );
|
||||
|
||||
// Resize.
|
||||
$new_sizes = wp_constrain_dimensions( $dimensions['width'], $dimensions['height'], $max_width );
|
||||
$resized = $editor->resize( $new_sizes[0], $new_sizes[1], false );
|
||||
|
||||
// Remove the filter when we're done to prevent any conflict.
|
||||
remove_filter( 'image_strip_meta', '__return_false', 789 );
|
||||
|
||||
if ( is_wp_error( $resized ) ) {
|
||||
return $resized;
|
||||
}
|
||||
|
||||
$resized_image_path = $editor->generate_filename( 'imagifyresized' );
|
||||
$resized_image_saved = $editor->save( $resized_image_path );
|
||||
|
||||
if ( is_wp_error( $resized_image_saved ) ) {
|
||||
return $resized_image_saved;
|
||||
}
|
||||
|
||||
return $resized_image_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a thumbnail.
|
||||
* Warning: If the destination file already exists, it will be overwritten.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $destination {
|
||||
* The thumbnail data.
|
||||
*
|
||||
* @type string $path Path to the destination file.
|
||||
* @type int $width The image width.
|
||||
* @type int $height The image height.
|
||||
* @type bool $crop True to crop, false to resize.
|
||||
* @type bool $adjust_filename True to adjust the file name like what `$editor->multi_resize()` returns, like WP default behavior (default). False to prevent it, and use the file name from $path instead.
|
||||
* }
|
||||
* @return bool|array|WP_Error {
|
||||
* A WP_Error object on error. True if the file exists.
|
||||
* An array of thumbnail data if the file has just been created:
|
||||
*
|
||||
* @type string $file File name.
|
||||
* @type int $width The image width.
|
||||
* @type int $height The image height.
|
||||
* @type string $mime-type The mime type.
|
||||
* }
|
||||
*/
|
||||
public function create_thumbnail( $destination ) {
|
||||
$can_be_processed = $this->can_be_processed();
|
||||
|
||||
if ( is_wp_error( $can_be_processed ) ) {
|
||||
return $can_be_processed;
|
||||
}
|
||||
|
||||
if ( ! $this->is_image() ) {
|
||||
return new \WP_Error(
|
||||
'not_an_image',
|
||||
sprintf(
|
||||
/* translators: %s is a file path. */
|
||||
__( 'The file %s does not seem to be an image, and cannot be resized.', 'imagify' ),
|
||||
'<code>' . esc_html( $this->filesystem->make_path_relative( $this->path ) ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$editor = $this->get_editor();
|
||||
|
||||
if ( is_wp_error( $editor ) ) {
|
||||
return $editor;
|
||||
}
|
||||
|
||||
// Create the file.
|
||||
$result = $editor->multi_resize( [ $destination ] );
|
||||
|
||||
if ( ! $result ) {
|
||||
return new \WP_Error( 'image_resize_error', __( 'The thumbnail could not be created.', 'imagify' ) );
|
||||
}
|
||||
|
||||
$result = reset( $result );
|
||||
|
||||
$filename = $result['file'];
|
||||
$source_thumb_path = $this->filesystem->dir_path( $this->path ) . $filename;
|
||||
|
||||
if ( ! isset( $destination['adjust_filename'] ) || $destination['adjust_filename'] ) {
|
||||
// The file name can change from what we expected (1px wider, etc), let's use the resulting data to move the file to the right place.
|
||||
$destination_thumb_path = $this->filesystem->dir_path( $destination['path'] ) . $filename;
|
||||
} else {
|
||||
// Respect what is set in $path.
|
||||
$destination_thumb_path = $destination['path'];
|
||||
$result['file'] = $this->filesystem->file_name( $destination['path'] );
|
||||
}
|
||||
|
||||
if ( $source_thumb_path === $destination_thumb_path ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$moved = $this->filesystem->move( $source_thumb_path, $destination_thumb_path, true );
|
||||
|
||||
if ( ! $moved ) {
|
||||
return new \WP_Error( 'move_error', __( 'The file could not be moved to its final destination.', 'imagify' ) );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup a file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @since 1.9.8 Added $backup_source argument.
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $backup_path The backup path.
|
||||
* @param string $backup_source Path to the file to backup. This is useful in WP 5.3+ when we want to optimize the full size: in that case we need to backup the original file.
|
||||
* @return bool|WP_Error True on success. False if the backup option is disabled. A WP_Error object on failure.
|
||||
*/
|
||||
public function backup( $backup_path = null, $backup_source = null ) {
|
||||
$can_be_processed = $this->can_be_processed();
|
||||
|
||||
if ( is_wp_error( $can_be_processed ) ) {
|
||||
return $can_be_processed;
|
||||
}
|
||||
|
||||
// Make sure the backups directory has no errors.
|
||||
if ( ! $backup_path ) {
|
||||
return new \WP_Error( 'wp_upload_error', __( 'Error while retrieving the backups directory path.', 'imagify' ) );
|
||||
}
|
||||
|
||||
// Create sub-directories.
|
||||
$created = $this->filesystem->make_dir( $this->filesystem->dir_path( $backup_path ) );
|
||||
|
||||
if ( ! $created ) {
|
||||
return new \WP_Error( 'backup_dir_not_writable', __( 'The backup directory is not writable.', 'imagify' ) );
|
||||
}
|
||||
|
||||
$path = $backup_source && $this->filesystem->exists( $backup_source ) ? $backup_source : $this->path;
|
||||
|
||||
/**
|
||||
* Allow to overwrite the backup file if it already exists.
|
||||
*
|
||||
* @since 1.6.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $overwrite Whether to overwrite the backup file.
|
||||
* @param string $path The file path.
|
||||
* @param string $backup_path The backup path.
|
||||
*/
|
||||
$overwrite = apply_filters( 'imagify_backup_overwrite_backup', false, $path, $backup_path );
|
||||
|
||||
// Copy the file.
|
||||
$this->filesystem->copy( $path, $backup_path, $overwrite, FS_CHMOD_FILE );
|
||||
|
||||
// Make sure the backup copy exists.
|
||||
if ( ! $this->filesystem->exists( $backup_path ) ) {
|
||||
return new \WP_Error( 'backup_doesnt_exist', __( 'The file could not be saved.', 'imagify' ), array(
|
||||
'file_path' => $this->filesystem->make_path_relative( $path ),
|
||||
'backup_path' => $this->filesystem->make_path_relative( $backup_path ),
|
||||
) );
|
||||
}
|
||||
|
||||
// Check if a '-scaled' version of the image exists.
|
||||
$scaled_path = preg_replace( '/(\.)([^\.]+)$/', '-scaled.$2', $backup_source );
|
||||
if ( $this->filesystem->exists( $scaled_path ) ) {
|
||||
// Create a backup path for the scaled image.
|
||||
$scaled_backup_path = preg_replace( '/(\.)([^\.]+)$/', '-scaled.$2', $backup_path );
|
||||
// Copy the '-scaled' version to the backup.
|
||||
$this->filesystem->copy( $scaled_path, $scaled_backup_path, $overwrite, FS_CHMOD_FILE );
|
||||
|
||||
if ( ! $this->filesystem->exists( $scaled_backup_path ) ) {
|
||||
return new \WP_Error( 'backup_doesnt_exist', __( 'The file could not be saved.', 'imagify' ), array(
|
||||
'file_path' => $this->filesystem->make_path_relative( $scaled_path ),
|
||||
'backup_path' => $this->filesystem->make_path_relative( $scaled_backup_path ),
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize a file with Imagify.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $args {
|
||||
* Optional. An array of arguments.
|
||||
*
|
||||
* @type bool $backup False to prevent backup. True to follow the user's setting. A backup can't be forced.
|
||||
* @type string $backup_path If a backup must be done, this is the path to use. Default is the backup path used for the WP Media Library.
|
||||
* @type int $optimization_level The optimization level (2=ultra, 1=aggressive, 0=normal).
|
||||
* @type string $convert Set to 'webp' to convert the image to WebP, 'avif' to convert image to AVIF.
|
||||
* @type string $context The context.
|
||||
* @type int $original_size The file size, sent to the API.
|
||||
* }
|
||||
* @return \sdtClass|\WP_Error Optimized image data. A \WP_Error object on error.
|
||||
*/
|
||||
public function optimize( $args = [] ) {
|
||||
$args = array_merge( [
|
||||
'backup' => true,
|
||||
'backup_path' => null,
|
||||
'backup_source' => null,
|
||||
'optimization_level' => 0,
|
||||
'convert' => '',
|
||||
'context' => 'wp',
|
||||
'original_size' => 0,
|
||||
], $args );
|
||||
|
||||
$can_be_processed = $this->can_be_processed();
|
||||
|
||||
if ( is_wp_error( $can_be_processed ) ) {
|
||||
return $can_be_processed;
|
||||
}
|
||||
|
||||
// Check if external HTTP requests are blocked.
|
||||
if ( Imagify_Requirements::is_imagify_blocked() ) {
|
||||
return new \WP_Error( 'http_block_external', __( 'External HTTP requests are blocked.', 'imagify' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires before a media file optimization.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $path Absolute path to the media file.
|
||||
* @param array $args Arguments passed to the method.
|
||||
*/
|
||||
do_action( 'imagify_before_optimize_file', $this->path, $args );
|
||||
|
||||
/**
|
||||
* Fires before to optimize the Image with Imagify.
|
||||
*
|
||||
* @since 1.0
|
||||
* @deprecated
|
||||
*
|
||||
* @param string $path Absolute path to the image file.
|
||||
* @param bool $backup True if a backup will be make.
|
||||
*/
|
||||
do_action_deprecated( 'before_do_imagify', [ $this->path, $args['backup'] ], '1.9', 'imagify_before_optimize_file' );
|
||||
|
||||
if ( $args['backup'] ) {
|
||||
$backup_result = $this->backup( $args['backup_path'], $args['backup_source'] );
|
||||
|
||||
if ( is_wp_error( $backup_result ) ) {
|
||||
// Stop the process if we can't backup the file.
|
||||
return $backup_result;
|
||||
}
|
||||
}
|
||||
|
||||
// Send file for optimization and fetch the response.
|
||||
$data = [
|
||||
'normal' => 0 === $args['optimization_level'],
|
||||
'aggressive' => 1 === $args['optimization_level'],
|
||||
'ultra' => 2 === $args['optimization_level'],
|
||||
'keep_exif' => true,
|
||||
'original_size' => $args['original_size'],
|
||||
'context' => $args['context'],
|
||||
];
|
||||
|
||||
if ( $args['convert'] ) {
|
||||
$data['convert'] = $args['convert'];
|
||||
$format = $args['convert'];
|
||||
}
|
||||
|
||||
$response = upload_imagify_image( [
|
||||
'image' => $this->path,
|
||||
'data' => wp_json_encode( $data ),
|
||||
] );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return new \WP_Error( 'api_error', $response->get_error_message() );
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'download_url' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
}
|
||||
|
||||
$temp_file = download_url( $response->image );
|
||||
|
||||
if ( is_wp_error( $temp_file ) ) {
|
||||
return new \WP_Error( 'temp_file_not_found', $temp_file->get_error_message() );
|
||||
}
|
||||
|
||||
if ( property_exists( $response, 'message' ) ) {
|
||||
$args['convert'] = '';
|
||||
}
|
||||
|
||||
$formats = [
|
||||
'webp',
|
||||
'avif',
|
||||
];
|
||||
if ( in_array( $args['convert'], $formats, true ) ) {
|
||||
$destination_path = $this->get_path_to_nextgen( $args['convert'] );
|
||||
$this->path = $destination_path;
|
||||
$this->file_type = null;
|
||||
$this->editor = null;
|
||||
} else {
|
||||
$destination_path = $this->path;
|
||||
}
|
||||
|
||||
$moved = $this->filesystem->move( $temp_file, $destination_path, true );
|
||||
|
||||
if ( ! $moved ) {
|
||||
return new \WP_Error( 'move_error', __( 'The file could not be moved to its final destination.', 'imagify' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after to optimize the Image with Imagify.
|
||||
*
|
||||
* @since 1.0
|
||||
* @deprecated
|
||||
*
|
||||
* @param string $path Absolute path to the image file.
|
||||
* @param bool $backup True if a backup has been made.
|
||||
*/
|
||||
do_action_deprecated( 'after_do_imagify', [ $this->path, $args['backup'] ], '1.9', 'imagify_before_optimize_file' );
|
||||
|
||||
/**
|
||||
* Fires after a media file optimization.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $path Absolute path to the media file.
|
||||
* @param array $args Arguments passed to the method.
|
||||
*/
|
||||
do_action( 'imagify_after_optimize_file', $this->path, $args );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** IMAGE EDITOR (GD/IMAGEMAGICK) =========================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get an image editor instance (WP_Image_Editor_Imagick, WP_Image_Editor_GD).
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return WP_Image_Editor_Imagick|WP_Image_Editor_GD|WP_Error
|
||||
*/
|
||||
protected function get_editor() {
|
||||
if ( isset( $this->editor ) ) {
|
||||
return $this->editor;
|
||||
}
|
||||
|
||||
$this->editor = wp_get_image_editor( $this->path, [
|
||||
'methods' => $this->get_editor_methods(),
|
||||
] );
|
||||
|
||||
if ( ! is_wp_error( $this->editor ) ) {
|
||||
return $this->editor;
|
||||
}
|
||||
|
||||
$this->editor = new \WP_Error(
|
||||
'image_editor',
|
||||
sprintf(
|
||||
/* translators: %1$s is an error message, %2$s is a "More info?" link. */
|
||||
__( 'No php extensions are available to edit images on the server. ImageMagick or GD is required. The internal error is: %1$s. %2$s', 'imagify' ),
|
||||
$this->editor->get_error_message(),
|
||||
'<a href="' . esc_url( imagify_get_external_url( 'documentation-imagick-gd' ) ) . '" target="_blank">' . __( 'More info?', 'imagify' ) . '</a>'
|
||||
)
|
||||
);
|
||||
|
||||
return $this->editor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the image editor methods we will use.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_editor_methods() {
|
||||
static $methods;
|
||||
|
||||
if ( isset( $methods ) ) {
|
||||
return $methods;
|
||||
}
|
||||
|
||||
$methods = [
|
||||
'resize',
|
||||
'multi_resize',
|
||||
'generate_filename',
|
||||
'save',
|
||||
];
|
||||
|
||||
if ( $this->filesystem->can_get_exif() ) {
|
||||
$methods[] = 'rotate';
|
||||
}
|
||||
|
||||
return $methods;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** VARIOUS TOOLS =========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Check if a file exceeds the weight limit (> 5mo).
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_exceeded() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$size = $this->filesystem->size( $this->path );
|
||||
|
||||
return $size > IMAGIFY_MAX_BYTES;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the current file is supported for a given context.
|
||||
*
|
||||
* @since 1.9
|
||||
* @see imagify_get_mime_types()
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $allowed_mime_types A list of allowed mime types.
|
||||
* @return bool
|
||||
*/
|
||||
public function is_supported( $allowed_mime_types ) {
|
||||
return in_array( $this->get_mime_type(), $allowed_mime_types, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the file is an image.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_image() {
|
||||
if ( isset( $this->is_image ) ) {
|
||||
return $this->is_image;
|
||||
}
|
||||
|
||||
$this->is_image = strpos( $this->get_mime_type(), 'image/' ) === 0;
|
||||
|
||||
return $this->is_image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the file is a pdf.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_pdf() {
|
||||
return 'application/pdf' === $this->get_mime_type();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file mime type.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_mime_type() {
|
||||
return $this->get_file_type()->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file extension.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return string|false
|
||||
*/
|
||||
public function get_extension() {
|
||||
return $this->get_file_type()->ext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file path.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_path() {
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the file extension by WebP.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string|bool The file path on success. False if not an image or on failure.
|
||||
*/
|
||||
public function get_path_to_webp() {
|
||||
if ( ! $this->is_image() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $this->is_webp() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return imagify_path_to_webp( $this->path );
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the file extension by its next-gen format extension.
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param string $format the format we are targeting.
|
||||
* @return string|bool The file path on success. False if not an image or on failure.
|
||||
*/
|
||||
public function get_path_to_nextgen( string $format ) {
|
||||
if ( ! $this->is_image() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $this->is_webp() || $this->is_avif() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return imagify_path_to_nextgen( $this->path, $format );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the file is a WebP image.
|
||||
* Rejects "path/to/.webp" files.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_webp() {
|
||||
return preg_match( '@(?!^|/|\\\)\.webp$@i', $this->path );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the file is an AVIF image.
|
||||
* Rejects "path/to/.avif" files.
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_avif() {
|
||||
return preg_match( '@(?!^|/|\\\)\.avif$@i', $this->path );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file mime type + file extension.
|
||||
*
|
||||
* @since 1.9
|
||||
* @see wp_check_filetype()
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return object {
|
||||
* @type string $ext The file extension.
|
||||
* @type string $type The mime type.
|
||||
* }
|
||||
*/
|
||||
protected function get_file_type() {
|
||||
if ( isset( $this->file_type ) ) {
|
||||
return $this->file_type;
|
||||
}
|
||||
|
||||
$this->file_type = (object) [
|
||||
'ext' => '',
|
||||
'type' => '',
|
||||
];
|
||||
|
||||
if ( ! $this->is_valid() ) {
|
||||
return $this->file_type;
|
||||
}
|
||||
|
||||
$this->file_type = (object) wp_check_filetype( $this->path );
|
||||
|
||||
return $this->file_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the media is an image, get its width and height.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_dimensions() {
|
||||
if ( ! $this->is_image() ) {
|
||||
return [
|
||||
'width' => 0,
|
||||
'height' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
$values = $this->filesystem->get_image_size( $this->path );
|
||||
|
||||
if ( empty( $values ) ) {
|
||||
return [
|
||||
'width' => 0,
|
||||
'height' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'width' => $values['width'],
|
||||
'height' => $values['height'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a plugin’s option.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option_name The option nme.
|
||||
* @return mixed
|
||||
*/
|
||||
protected function get_option( $option_name ) {
|
||||
if ( isset( $this->options[ $option_name ] ) ) {
|
||||
return $this->options[ $option_name ];
|
||||
}
|
||||
|
||||
$this->options[ $option_name ] = get_imagify_option( $option_name );
|
||||
|
||||
return $this->options[ $option_name ];
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,103 +0,0 @@
|
||||
<?php
|
||||
namespace Imagify\Optimization\Process;
|
||||
|
||||
use Imagify\Optimization\File;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Optimization class for the custom folders.
|
||||
* This class constructor accepts:
|
||||
* - A post 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.
|
||||
* - A \Imagify\Media\DataInterface object.
|
||||
*
|
||||
* @since 1.9
|
||||
* @see Imagify\Media\CustomFolders
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class CustomFolders extends AbstractProcess {
|
||||
|
||||
/**
|
||||
* Restore the thumbnails.
|
||||
* This context has no thumbnails.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|WP_Error True on success. A \WP_Error instance on failure.
|
||||
*/
|
||||
protected function restore_thumbnails() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** MISSING THUMBNAILS ====================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the sizes for this media that have not get through optimization.
|
||||
* Since this context has no thumbnails, this will always return an empty array, unless an error is triggered.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array|WP_Error A WP_Error object on failure. An empty array on success: this context has no thumbnails.
|
||||
* The tests are kept for consistency.
|
||||
*/
|
||||
public function get_missing_sizes() {
|
||||
// The media must have been optimized once and have a backup.
|
||||
if ( ! $this->is_valid() ) {
|
||||
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
$media = $this->get_media();
|
||||
|
||||
if ( ! $media->is_supported() ) {
|
||||
return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) );
|
||||
}
|
||||
|
||||
$data = $this->get_data();
|
||||
|
||||
if ( ! $data->is_optimized() ) {
|
||||
return new \WP_Error( 'media_not_optimized', __( 'This media is not optimized yet.', 'imagify' ) );
|
||||
}
|
||||
|
||||
if ( ! $media->has_backup() ) {
|
||||
return new \WP_Error( 'no_backup', __( 'This file has no backup file.', 'imagify' ) );
|
||||
}
|
||||
|
||||
if ( ! $media->is_image() ) {
|
||||
return new \WP_Error( 'media_not_an_image', __( 'This media is not an image.', 'imagify' ) );
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize missing thumbnail sizes.
|
||||
* Since this context has no thumbnails, this will always return a \WP_Error object.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function optimize_missing_thumbnails() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
if ( ! $this->get_media()->is_supported() ) {
|
||||
return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) );
|
||||
}
|
||||
|
||||
return new \WP_Error( 'no_sizes', __( 'No thumbnails seem to be missing.', 'imagify' ) );
|
||||
}
|
||||
}
|
||||
@@ -1,351 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Imagify\Optimization\Process;
|
||||
|
||||
use WP_Error;
|
||||
|
||||
/**
|
||||
* Fallback class to optimize medias.
|
||||
*/
|
||||
class Noop implements ProcessInterface {
|
||||
/**
|
||||
* The suffix used in the thumbnail size name.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
*/
|
||||
const WEBP_SUFFIX = '@imagify-webp';
|
||||
|
||||
/**
|
||||
* The suffix used in the thumbnail size name.
|
||||
*
|
||||
* @var string
|
||||
* @since 2.2
|
||||
*/
|
||||
const AVIF_SUFFIX = '@imagify-avif';
|
||||
|
||||
/**
|
||||
* The suffix used in file name to create a temporary copy of the full size.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
*/
|
||||
const TMP_SUFFIX = '@imagify-tmp';
|
||||
|
||||
/**
|
||||
* Used for the name of the transient telling if a media is locked.
|
||||
* %1$s is the context, %2$s is the media ID.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
*/
|
||||
const LOCK_NAME = 'imagify_%1$s_%2$s_process_locked';
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @param mixed $id Whatever.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function constructor_accepts( $id ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data instance.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return DataInterface|false
|
||||
*/
|
||||
public function get_data() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the media instance.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return MediaInterface|false
|
||||
*/
|
||||
public function get_media() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the File instance.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return File|false
|
||||
*/
|
||||
public function get_file() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the current media is valid.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_valid() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the current user is allowed to operate Imagify in this context.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param string $describer Capacity describer. See \Imagify\Context\ContextInterface->get_capacity() for possible values. Can also be a "real" user capacity.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function current_user_can( $describer ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize a media files by pushing tasks into the queue.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra).
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A WP_Error instance on failure.
|
||||
*/
|
||||
public function optimize( $optimization_level = null ) {
|
||||
return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-optimize a media files with a different level.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra).
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A WP_Error instance on failure.
|
||||
*/
|
||||
public function reoptimize( $optimization_level = null ) {
|
||||
return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize several file sizes by pushing tasks into the queue.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param array $sizes An array of media sizes (strings). Use "full" for the size of the main file.
|
||||
* @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra).
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A WP_Error instance on failure.
|
||||
*/
|
||||
public function optimize_sizes( $sizes, $optimization_level = null ) {
|
||||
return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize one file with Imagify directly.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param string $size The media size.
|
||||
* @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra).
|
||||
*
|
||||
* @return array|WP_Error The optimization data. A WP_Error instance on failure.
|
||||
*/
|
||||
public function optimize_size( $size, $optimization_level = null ) {
|
||||
return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the media files from the backup file.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return bool|WP_Error True on success. A WP_Error instance on failure.
|
||||
*/
|
||||
public function restore() {
|
||||
return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sizes for this media that have not get through optimization.
|
||||
* No sizes are returned if the file is not optimized, has no backup, or is not an image.
|
||||
* The 'full' size os never returned.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return array|WP_Error {
|
||||
* A WP_Error object on failure.
|
||||
* An array of data for the thumbnail sizes on success.
|
||||
* Size names are used as array keys.
|
||||
*
|
||||
* @type int $width The image width.
|
||||
* @type int $height The image height.
|
||||
* @type bool $crop True to crop, false to resize.
|
||||
* @type string $name The size name.
|
||||
* @type string $file The name the thumbnail "should" have.
|
||||
* }
|
||||
*/
|
||||
public function get_missing_sizes() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize missing thumbnail sizes.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A WP_Error instance on failure.
|
||||
*/
|
||||
public function optimize_missing_thumbnails() {
|
||||
return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the backup file.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function delete_backup() {}
|
||||
|
||||
/**
|
||||
* Maybe resize an image.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param string $size The size name.
|
||||
* @param File $file A File instance.
|
||||
*
|
||||
* @return array|WP_Error A WP_Error instance on failure, an array on success as follow: {
|
||||
* @type bool $resized True when the image has been resized.
|
||||
* @type bool $backuped True when the image has been backuped.
|
||||
* @type int $file_size The file size in bytes.
|
||||
* }
|
||||
*/
|
||||
public function maybe_resize( $size, $file ) {
|
||||
return [
|
||||
'resized' => false,
|
||||
'backuped' => false,
|
||||
'file_size' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Nextgen images if they are missing.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A WP_Error instance on failure.
|
||||
*/
|
||||
public function generate_nextgen_versions() {
|
||||
return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the next gen format images.
|
||||
* This doesn't delete the related optimization data.
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param bool $keep_full Set to true to keep the full size.
|
||||
* @return bool|WP_Error True on success. A WP_Error object on failure.
|
||||
*/
|
||||
public function delete_nextgen_files( $keep_full = false ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if a thumbnail size is an "Imagify Next-Gen" size.
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param string $size_name The size name.
|
||||
*
|
||||
* @return string|bool The unsuffixed name of the size if Next-Gen. False if not a Next-Gen.
|
||||
*/
|
||||
public function is_size_next_gen( $size_name ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if a process is running for this media.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_locked() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the running status to "running" for a period of time.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function lock() {}
|
||||
|
||||
/**
|
||||
* Delete the running status.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function unlock() {}
|
||||
|
||||
/**
|
||||
* Tell if a size already has optimization data.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param string $size The size name.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function size_has_optimization_data( $size ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the optimization data for a size.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param object $response The API response.
|
||||
* @param string $size The size name.
|
||||
* @param int $level The optimization level (0=normal, 1=aggressive, 2=ultra).
|
||||
*
|
||||
* @return array {
|
||||
* The optimization data.
|
||||
*
|
||||
* @type string $size The size name.
|
||||
* @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, once optimized.
|
||||
* }
|
||||
*/
|
||||
public function update_size_optimization_data( $response, $size, $level ) {
|
||||
return [
|
||||
'size' => 'noop',
|
||||
'level' => false,
|
||||
'status' => '',
|
||||
'success' => false,
|
||||
'error' => '',
|
||||
'original_size' => 0,
|
||||
'optimized_size' => 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,293 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Imagify\Optimization\Process;
|
||||
|
||||
/**
|
||||
* Interface to use to optimize medias.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
interface ProcessInterface {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @param mixed $id Whatever.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function constructor_accepts( $id );
|
||||
|
||||
/**
|
||||
* Get the data instance.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return DataInterface|false
|
||||
*/
|
||||
public function get_data();
|
||||
|
||||
/**
|
||||
* Get the media instance.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return MediaInterface|false
|
||||
*/
|
||||
public function get_media();
|
||||
|
||||
/**
|
||||
* Get the File instance of the original file.
|
||||
*
|
||||
* @since 1.9.8
|
||||
*
|
||||
* @return File|false
|
||||
*/
|
||||
public function get_original_file();
|
||||
|
||||
/**
|
||||
* Get the File instance of the full size file.
|
||||
*
|
||||
* @since 1.9.8
|
||||
*
|
||||
* @return File|false
|
||||
*/
|
||||
public function get_fullsize_file();
|
||||
|
||||
/**
|
||||
* Tell if the current media is valid.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_valid();
|
||||
|
||||
/**
|
||||
* Tell if the current user is allowed to operate Imagify in this context.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param string $describer Capacity describer. See \Imagify\Context\ContextInterface->get_capacity() for possible values. Can also be a "real" user capacity.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function current_user_can( $describer );
|
||||
|
||||
/**
|
||||
* Optimize a media files by pushing tasks into the queue.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra).
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function optimize( $optimization_level = null );
|
||||
|
||||
/**
|
||||
* Re-optimize a media files with a different level.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra).
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function reoptimize( $optimization_level = null );
|
||||
|
||||
/**
|
||||
* Optimize several file sizes by pushing tasks into the queue.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param array $sizes An array of media sizes (strings). Use "full" for the size of the main file.
|
||||
* @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra).
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function optimize_sizes( $sizes, $optimization_level = null );
|
||||
|
||||
/**
|
||||
* Optimize one file with Imagify directly.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param string $size The media size.
|
||||
* @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra).
|
||||
*
|
||||
* @return array|WP_Error The optimization data. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function optimize_size( $size, $optimization_level = null );
|
||||
|
||||
/**
|
||||
* Restore the media files from the backup file.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return bool|WP_Error True on success. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function restore();
|
||||
|
||||
/**
|
||||
* Get the sizes for this media that have not get through optimization.
|
||||
* No sizes are returned if the file is not optimized, has no backup, or is not an image.
|
||||
* The 'full' size os never returned.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return array|WP_Error {
|
||||
* A WP_Error object on failure.
|
||||
* An array of data for the thumbnail sizes on success.
|
||||
* Size names are used as array keys.
|
||||
*
|
||||
* @type int $width The image width.
|
||||
* @type int $height The image height.
|
||||
* @type bool $crop True to crop, false to resize.
|
||||
* @type string $name The size name.
|
||||
* @type string $file The name the thumbnail "should" have.
|
||||
* }
|
||||
*/
|
||||
public function get_missing_sizes();
|
||||
|
||||
/**
|
||||
* Optimize missing thumbnail sizes.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function optimize_missing_thumbnails();
|
||||
|
||||
/**
|
||||
* Delete the backup file.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function delete_backup();
|
||||
|
||||
/**
|
||||
* Maybe resize an image.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param string $size The size name.
|
||||
* @param File $file A File instance.
|
||||
*
|
||||
* @return array|WP_Error A \WP_Error instance on failure, an array on success as follow: {
|
||||
* @type bool $resized True when the image has been resized.
|
||||
* @type bool $backuped True when the image has been backuped.
|
||||
* @type int $file_size The file size in bytes.
|
||||
* }
|
||||
*/
|
||||
public function maybe_resize( $size, $file );
|
||||
|
||||
/**
|
||||
* Generate next-gen images if they are missing.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function generate_nextgen_versions();
|
||||
|
||||
/**
|
||||
* Delete the next gen format images.
|
||||
* This doesn't delete the related optimization data.
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param bool $keep_full Set to true to keep the full size.
|
||||
*
|
||||
* @return bool|WP_Error True on success. A \WP_Error object on failure.
|
||||
*/
|
||||
public function delete_nextgen_files( $keep_full = false );
|
||||
|
||||
/**
|
||||
* Tell if a thumbnail size is an "Imagify Next-Gen" size.
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @param string $size_name The size name.
|
||||
*
|
||||
* @return string|bool The unsuffixed name of the size if next-gen. False if not next-gen.
|
||||
*/
|
||||
public function is_size_next_gen( $size_name );
|
||||
|
||||
/**
|
||||
* Tell if the media has all next-gen versions.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_full_next_gen();
|
||||
|
||||
/**
|
||||
* Tell if the media has a next-gen format.
|
||||
*
|
||||
* @since 2.2
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has_next_gen();
|
||||
|
||||
/**
|
||||
* Tell if a process is running for this media.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_locked();
|
||||
|
||||
/**
|
||||
* Set the running status to "running" for a period of time.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function lock();
|
||||
|
||||
/**
|
||||
* Delete the running status.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function unlock();
|
||||
|
||||
/**
|
||||
* Tell if a size already has optimization data.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param string $size The size name.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function size_has_optimization_data( $size );
|
||||
|
||||
/**
|
||||
* Update the optimization data for a size.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param object $response The API response.
|
||||
* @param string $size The size name.
|
||||
* @param int $level The optimization level (0=normal, 1=aggressive, 2=ultra).
|
||||
*
|
||||
* @return array {
|
||||
* The optimization data.
|
||||
*
|
||||
* @type string $size The size name.
|
||||
* @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, once optimized.
|
||||
* }
|
||||
*/
|
||||
public function update_size_optimization_data( $response, $size, $level );
|
||||
}
|
||||
@@ -1,255 +0,0 @@
|
||||
<?php
|
||||
namespace Imagify\Optimization\Process;
|
||||
|
||||
use Imagify\Optimization\File;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Optimization class for the attachments in the WP library.
|
||||
* This class constructor accepts:
|
||||
* - A post ID (int).
|
||||
* - A \WP_Post object.
|
||||
* - A \Imagify\Media\MediaInterface object.
|
||||
* - A \Imagify\Media\DataInterface object.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class WP extends AbstractProcess {
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** MISSING THUMBNAILS ====================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the sizes for this media that have not get through optimization.
|
||||
* No sizes are returned if the file is not optimized, has no backup, or is not an image.
|
||||
* The 'full' size os never returned.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array|WP_Error {
|
||||
* A WP_Error object on failure.
|
||||
* An array of data for the thumbnail sizes on success.
|
||||
* Size names are used as array keys.
|
||||
*
|
||||
* @type int $width The image width.
|
||||
* @type int $height The image height.
|
||||
* @type bool $crop True to crop, false to resize.
|
||||
* @type string $name The size name.
|
||||
* @type string $file The name the thumbnail "should" have.
|
||||
* }
|
||||
*/
|
||||
public function get_missing_sizes() {
|
||||
// The media must have been optimized once and have a backup.
|
||||
if ( ! $this->is_valid() ) {
|
||||
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
$media = $this->get_media();
|
||||
|
||||
if ( ! $media->is_supported() ) {
|
||||
return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) );
|
||||
}
|
||||
|
||||
$data = $this->get_data();
|
||||
|
||||
if ( ! $data->is_optimized() ) {
|
||||
return new \WP_Error( 'media_not_optimized', __( 'This media is not optimized yet.', 'imagify' ) );
|
||||
}
|
||||
|
||||
if ( ! $media->has_backup() ) {
|
||||
return new \WP_Error( 'no_backup', __( 'This file has no backup file.', 'imagify' ) );
|
||||
}
|
||||
|
||||
if ( ! $media->is_image() ) {
|
||||
return new \WP_Error( 'media_not_an_image', __( 'This media is not an image.', 'imagify' ) );
|
||||
}
|
||||
|
||||
// Compare registered sizes and optimized sizes.
|
||||
$context_sizes = $media->get_context_instance()->get_thumbnail_sizes();
|
||||
$optimized_sizes = $data->get_optimization_data();
|
||||
$missing_sizes = array_diff_key( $context_sizes, $optimized_sizes['sizes'] );
|
||||
|
||||
if ( ! $missing_sizes ) {
|
||||
// We have everything we need.
|
||||
return [];
|
||||
}
|
||||
|
||||
$media_sizes = $media->get_media_files();
|
||||
$full_size = $media_sizes['full'];
|
||||
|
||||
if ( ! $full_size['path'] || ! $full_size['width'] || ! $full_size['height'] ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$file_name = $this->filesystem->path_info( $full_size['path'] );
|
||||
$file_name = $file_name['file_base'] . '-{%suffix%}.' . $file_name['extension'];
|
||||
|
||||
// Test if the missing sizes are needed.
|
||||
foreach ( $missing_sizes as $size_name => $size_data ) {
|
||||
if ( $full_size['width'] === $size_data['width'] && $full_size['height'] === $size_data['height'] ) {
|
||||
// Same dimensions as the full size.
|
||||
unset( $missing_sizes[ $size_name ] );
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! empty( $media_sizes[ $size_name ]['disabled'] ) ) {
|
||||
// This size must not be optimized.
|
||||
unset( $missing_sizes[ $size_name ] );
|
||||
continue;
|
||||
}
|
||||
|
||||
$resize_result = image_resize_dimensions( $full_size['width'], $full_size['height'], $size_data['width'], $size_data['height'], $size_data['crop'] );
|
||||
|
||||
if ( ! $resize_result ) {
|
||||
// This thumbnail is not needed, it is smaller than this size.
|
||||
unset( $missing_sizes[ $size_name ] );
|
||||
continue;
|
||||
}
|
||||
|
||||
// Provide what should be the file name.
|
||||
list( , , , , $new_width, $new_height ) = $resize_result;
|
||||
$missing_sizes[ $size_name ]['file'] = str_replace( '{%suffix%}', "{$new_width}x{$new_height}", $file_name );
|
||||
}
|
||||
|
||||
return $missing_sizes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize missing thumbnail sizes.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function optimize_missing_thumbnails() {
|
||||
if ( ! $this->is_valid() ) {
|
||||
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
if ( ! $this->get_media()->is_supported() ) {
|
||||
return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) );
|
||||
}
|
||||
|
||||
$missing_sizes = $this->get_missing_sizes();
|
||||
|
||||
if ( ! $missing_sizes ) {
|
||||
return new \WP_Error( 'no_sizes', __( 'No thumbnails seem to be missing.', 'imagify' ) );
|
||||
}
|
||||
|
||||
if ( is_wp_error( $missing_sizes ) ) {
|
||||
return $missing_sizes;
|
||||
}
|
||||
|
||||
if ( $this->is_locked() ) {
|
||||
return new \WP_Error( 'media_locked', __( 'This media is already being processed.', 'imagify' ) );
|
||||
}
|
||||
|
||||
$this->lock();
|
||||
|
||||
// Create the missing thumbnails.
|
||||
$sizes = $this->create_missing_thumbnails( $missing_sizes );
|
||||
|
||||
if ( ! $sizes ) {
|
||||
$this->unlock();
|
||||
return new \WP_Error( 'thumbnail_creation_failed', __( 'The thumbnails failed to be created.', 'imagify' ) );
|
||||
}
|
||||
|
||||
$optimization_level = $this->get_data()->get_optimization_level();
|
||||
|
||||
if ( false === $optimization_level ) {
|
||||
$this->unlock();
|
||||
return new \WP_Error( 'optimization_level_not_set', __( 'The optimization level of this media seems to have disappear from the database. You should restore this media and then launch a new optimization.', 'imagify' ) );
|
||||
}
|
||||
|
||||
$args = [
|
||||
'hook_suffix' => 'optimize_missing_thumbnails',
|
||||
'locked' => true,
|
||||
];
|
||||
|
||||
// Optimize.
|
||||
return $this->optimize_sizes( array_keys( $sizes ), $optimization_level, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create all missing thumbnails if they don't exist and update the attachment metadata.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @see $this->get_missing_sizes()
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $missing_sizes array {
|
||||
* An array of data for the thumbnail sizes on success.
|
||||
* Size names are used as array keys.
|
||||
*
|
||||
* @type int $width The image width.
|
||||
* @type int $height The image height.
|
||||
* @type bool $crop True to crop, false to resize.
|
||||
* @type string $name The size name.
|
||||
* @type string $file The name the thumbnail "should" have.
|
||||
* }
|
||||
* @return array {
|
||||
* An array of thumbnail data (those without errors):
|
||||
*
|
||||
* @type string $file File name.
|
||||
* @type int $width The image width.
|
||||
* @type int $height The image height.
|
||||
* @type string $mime-type The mime type.
|
||||
* }
|
||||
*/
|
||||
protected function create_missing_thumbnails( $missing_sizes ) {
|
||||
if ( ! $missing_sizes ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$media = $this->get_media();
|
||||
$media_id = $media->get_id();
|
||||
$metadata = wp_get_attachment_metadata( $media_id );
|
||||
$metadata['sizes'] = ! empty( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ? $metadata['sizes'] : [];
|
||||
|
||||
$destination_dir = $this->filesystem->dir_path( $media->get_raw_fullsize_path() );
|
||||
$backup_file = new File( $media->get_backup_path() );
|
||||
$without_errors = [];
|
||||
$has_new_data = false;
|
||||
|
||||
// Create the missing thumbnails.
|
||||
foreach ( $missing_sizes as $size_name => $thumbnail_data ) {
|
||||
// The path to the destination file.
|
||||
$thumbnail_data['path'] = $destination_dir . $thumbnail_data['file'];
|
||||
|
||||
if ( ! $this->filesystem->exists( $thumbnail_data['path'] ) ) {
|
||||
$result = $backup_file->create_thumbnail( $thumbnail_data );
|
||||
|
||||
if ( is_array( $result ) ) {
|
||||
// New file.
|
||||
$metadata['sizes'][ $size_name ] = $result;
|
||||
$has_new_data = true;
|
||||
}
|
||||
} else {
|
||||
$result = true;
|
||||
}
|
||||
|
||||
if ( ! empty( $metadata['sizes'][ $size_name ] ) && ! is_wp_error( $result ) ) {
|
||||
// Not an error.
|
||||
$without_errors[ $size_name ] = $metadata['sizes'][ $size_name ];
|
||||
}
|
||||
}
|
||||
|
||||
// Save the new data into the attachment metadata.
|
||||
if ( $has_new_data ) {
|
||||
/**
|
||||
* Here we don't use wp_update_attachment_metadata() to prevent triggering unwanted hooks.
|
||||
*/
|
||||
update_post_meta( $media_id, '_wp_attachment_metadata', $metadata );
|
||||
}
|
||||
|
||||
return $without_errors;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user