PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.1
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Optimization / Process / CustomFolders.php

CustomFolders.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.1, at classes/Optimization/Process/CustomFolders.php

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