first commit
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,103 @@
|
||||
<?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' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,413 @@
|
||||
<?php
|
||||
namespace Imagify\Optimization\Process;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Fallback class to optimize medias.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Noop implements ProcessInterface {
|
||||
|
||||
/**
|
||||
* The suffix used in the thumbnail size name.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
const WEBP_SUFFIX = '@imagify-webp';
|
||||
|
||||
/**
|
||||
* The suffix used in file name to create a temporary copy of the full size.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
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
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $id Whatever.
|
||||
* @return bool
|
||||
*/
|
||||
public static function constructor_accepts( $id ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data instance.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return DataInterface|false
|
||||
*/
|
||||
public function get_data() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the media instance.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return MediaInterface|false
|
||||
*/
|
||||
public function get_media() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the File instance.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return File|false
|
||||
*/
|
||||
public function get_file() {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the current user is allowed to operate Imagify in this context.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** OPTIMIZATION ============================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Optimize a media files by pushing tasks into the queue.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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' ) );
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** 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() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** BACKUP FILE ============================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Delete the backup file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function delete_backup() {}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** RESIZE FILE ============================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Maybe resize an image.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** WEBP ==================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Generate WebP images if they are missing.
|
||||
*
|
||||
* @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 generate_webp_versions() {
|
||||
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the WebP images.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function delete_webp_files() {}
|
||||
|
||||
/**
|
||||
* Tell if a thumbnail size is an "Imagify WebP" size.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $size_name The size name.
|
||||
* @return string|bool The unsuffixed name of the size if WebP. False if not WebP.
|
||||
*/
|
||||
public function is_size_webp( $size_name ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PROCESS STATUS ========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Tell if a process is running for this media.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_locked() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the running status to "running" for a period of time.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function lock() {}
|
||||
|
||||
/**
|
||||
* Delete the running status.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function unlock() {}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** DATA ==================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Tell if a size already has optimization data.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
<?php
|
||||
namespace Imagify\Optimization\Process;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Interface to use to optimize medias.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $id Whatever.
|
||||
* @return bool
|
||||
*/
|
||||
public static function constructor_accepts( $id );
|
||||
|
||||
/**
|
||||
* Get the data instance.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return DataInterface|false
|
||||
*/
|
||||
public function get_data();
|
||||
|
||||
/**
|
||||
* Get the media instance.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return MediaInterface|false
|
||||
*/
|
||||
public function get_media();
|
||||
|
||||
/**
|
||||
* Get the File instance of the original file.
|
||||
*
|
||||
* @since 1.9.8
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return File|false
|
||||
*/
|
||||
public function get_original_file();
|
||||
|
||||
/**
|
||||
* Get the File instance of the full size file.
|
||||
*
|
||||
* @since 1.9.8
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return File|false
|
||||
*/
|
||||
public function get_fullsize_file();
|
||||
|
||||
/**
|
||||
* Tell if the current media is valid.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_valid();
|
||||
|
||||
/**
|
||||
* Tell if the current user is allowed to operate Imagify in this context.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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 );
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** OPTIMIZATION ============================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Optimize a media files by pushing tasks into the queue.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|WP_Error True on success. A \WP_Error instance on failure.
|
||||
*/
|
||||
public function restore();
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** 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();
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** BACKUP FILE ============================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Delete the backup file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function delete_backup();
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** RESIZE FILE ============================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Maybe resize an image.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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 );
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** WEBP ==================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Generate WebP images if they are missing.
|
||||
*
|
||||
* @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 generate_webp_versions();
|
||||
|
||||
/**
|
||||
* Delete the WebP images.
|
||||
* This doesn't delete the related optimization data.
|
||||
*
|
||||
* @since 1.9
|
||||
* @since 1.9.6 Return WP_Error or true.
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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_webp_files( $keep_full = false );
|
||||
|
||||
/**
|
||||
* Tell if a thumbnail size is an "Imagify WebP" size.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $size_name The size name.
|
||||
* @return string|bool The unsuffixed name of the size if WebP. False if not WebP.
|
||||
*/
|
||||
public function is_size_webp( $size_name );
|
||||
|
||||
/**
|
||||
* Tell if the media has WebP versions.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has_webp();
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** PROCESS STATUS ========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Tell if a process is running for this media.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_locked();
|
||||
|
||||
/**
|
||||
* Set the running status to "running" for a period of time.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function lock();
|
||||
|
||||
/**
|
||||
* Delete the running status.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function unlock();
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** DATA ==================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Tell if a size already has optimization data.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @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 );
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
<?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