| 1 |
<?php |
| 2 |
namespace Imagify\Optimization\Process; |
| 3 |
|
| 4 |
use Imagify\Optimization\File; |
| 5 |
|
| 6 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 7 |
|
| 8 |
/** |
| 9 |
* Optimization class for the custom folders. |
| 10 |
* This class constructor accepts: |
| 11 |
* - A post ID (int). |
| 12 |
* - An array of data coming from the files DB table /!\ |
| 13 |
* - An object of data coming from the files DB table /!\ |
| 14 |
* - A \Imagify\Media\MediaInterface object. |
| 15 |
* - A \Imagify\Media\DataInterface object. |
| 16 |
* |
| 17 |
* @since 1.9 |
| 18 |
* @see Imagify\Media\CustomFolders |
| 19 |
* @author Grégory Viguier |
| 20 |
*/ |
| 21 |
class CustomFolders extends AbstractProcess { |
| 22 |
|
| 23 |
/** |
| 24 |
* Restore the thumbnails. |
| 25 |
* This context has no thumbnails. |
| 26 |
* |
| 27 |
* @since 1.9 |
| 28 |
* @access public |
| 29 |
* @author Grégory Viguier |
| 30 |
* |
| 31 |
* @return bool|WP_Error True on success. A \WP_Error instance on failure. |
| 32 |
*/ |
| 33 |
protected function restore_thumbnails() { |
| 34 |
return true; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
/** ----------------------------------------------------------------------------------------- */ |
| 39 |
/** MISSING THUMBNAILS ====================================================================== */ |
| 40 |
/** ----------------------------------------------------------------------------------------- */ |
| 41 |
|
| 42 |
/** |
| 43 |
* Get the sizes for this media that have not get through optimization. |
| 44 |
* Since this context has no thumbnails, this will always return an empty array, unless an error is triggered. |
| 45 |
* |
| 46 |
* @since 1.9 |
| 47 |
* @access public |
| 48 |
* @author Grégory Viguier |
| 49 |
* |
| 50 |
* @return array|WP_Error A WP_Error object on failure. An empty array on success: this context has no thumbnails. |
| 51 |
* The tests are kept for consistency. |
| 52 |
*/ |
| 53 |
public function get_missing_sizes() { |
| 54 |
// The media must have been optimized once and have a backup. |
| 55 |
if ( ! $this->is_valid() ) { |
| 56 |
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
$media = $this->get_media(); |
| 60 |
|
| 61 |
if ( ! $media->is_supported() ) { |
| 62 |
return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) ); |
| 63 |
} |
| 64 |
|
| 65 |
$data = $this->get_data(); |
| 66 |
|
| 67 |
if ( ! $data->is_optimized() ) { |
| 68 |
return new \WP_Error( 'media_not_optimized', __( 'This media is not optimized yet.', 'imagify' ) ); |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! $media->has_backup() ) { |
| 72 |
return new \WP_Error( 'no_backup', __( 'This file has no backup file.', 'imagify' ) ); |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! $media->is_image() ) { |
| 76 |
return new \WP_Error( 'media_not_an_image', __( 'This media is not an image.', 'imagify' ) ); |
| 77 |
} |
| 78 |
|
| 79 |
return []; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Optimize missing thumbnail sizes. |
| 84 |
* Since this context has no thumbnails, this will always return a \WP_Error object. |
| 85 |
* |
| 86 |
* @since 1.9 |
| 87 |
* @access public |
| 88 |
* @author Grégory Viguier |
| 89 |
* |
| 90 |
* @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure. |
| 91 |
*/ |
| 92 |
public function optimize_missing_thumbnails() { |
| 93 |
if ( ! $this->is_valid() ) { |
| 94 |
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! $this->get_media()->is_supported() ) { |
| 98 |
return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) ); |
| 99 |
} |
| 100 |
|
| 101 |
return new \WP_Error( 'no_sizes', __( 'No thumbnails seem to be missing.', 'imagify' ) ); |
| 102 |
} |
| 103 |
} |
| 104 |
|