PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.8
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.8
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 / Bulk / CustomFolders.php

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

200 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\Bulk;
3
4 use Imagify_Custom_Folders;
5 use Imagify_Files_Scan;
6 use Imagify_Files_DB;
7 use Imagify_Folders_DB;
8 use Imagify_DB;
9 use Imagify_Files_Stats;
10
11 /**
12 * Class to use for bulk for custom folders.
13 *
14 * @since 1.9
15 */
16 class CustomFolders extends AbstractBulk {
17 /**
18 * Context "short name".
19 *
20 * @var string
21 * @since 1.9
22 */
23 protected $context = 'custom-folders';
24
25 /**
26 * Get all unoptimized media ids.
27 *
28 * @since 1.9
29 *
30 * @param int $optimization_level The optimization level.
31 *
32 * @return array A list of unoptimized media IDs.
33 */
34 public function get_unoptimized_media_ids( $optimization_level ) {
35 $this->set_no_time_limit();
36
37 /**
38 * Get the folders from DB.
39 */
40 $folders = Imagify_Custom_Folders::get_folders(
41 [
42 'active' => true,
43 ]
44 );
45
46 if ( ! $folders ) {
47 return [];
48 }
49
50 /**
51 * Fires before getting file IDs.
52 *
53 * @since 1.7
54 *
55 * @param array $folders An array of folders data.
56 * @param int $optimization_level The optimization level that will be used for the optimization.
57 */
58 do_action( 'imagify_bulk_optimize_files_before_get_files', $folders, $optimization_level );
59
60 /**
61 * Get the files from DB, and from the folders.
62 */
63 $files = Imagify_Custom_Folders::get_files_from_folders(
64 $folders,
65 [
66 'optimization_level' => $optimization_level,
67 ]
68 );
69
70 if ( ! $files ) {
71 return [];
72 }
73
74 foreach ( $files as $k => $file ) {
75 $files[ $k ] = $file['file_id'];
76 }
77
78 return $files;
79 }
80
81 /**
82 * Get ids of all optimized media without Next gen versions.
83 *
84 * @since 2.2
85 *
86 * @param string $format Format we are looking for. (webp|avif).
87 *
88 * @return array {
89 * @type array $ids A list of media IDs.
90 * @type array $errors {
91 * @type array $no_file_path A list of media IDs.
92 * @type array $no_backup A list of media IDs.
93 * }
94 * }
95 */
96 public function get_optimized_media_ids_without_format( $format ) {
97 global $wpdb;
98
99 $this->set_no_time_limit();
100
101 $files_table = Imagify_Files_DB::get_instance()->get_table_name();
102 $folders_table = Imagify_Folders_DB::get_instance()->get_table_name();
103 $mime_types = Imagify_DB::get_mime_types( 'image' );
104 // Remove single quotes and explode string into array.
105 $mime_types_array = explode( ',', str_replace( "'", '', $mime_types ) );
106
107 // Iterate over array and check if string contains input.
108 foreach ( $mime_types_array as $item ) {
109 if ( strpos( $item, $format ) !== false ) {
110 $mime = $item;
111 break;
112 }
113 }
114 if ( ! isset( $mime ) && empty( $mime ) ) {
115 $mime = 'image/webp';
116 }
117 $mime_types = str_replace( ",'" . $mime . "'", '', $mime_types );
118 $nextgen_suffix = constant( imagify_get_optimization_process_class_name( 'custom-folders' ) . '::' . strtoupper( $format ) . '_SUFFIX' );
119 $files = $wpdb->get_results(
120 $wpdb->prepare( // WPCS: unprepared SQL ok.
121 "
122 SELECT fi.file_id, fi.path
123 FROM $files_table as fi
124 INNER JOIN $folders_table AS fo
125 ON ( fi.folder_id = fo.folder_id )
126 WHERE
127 fi.mime_type IN ( $mime_types )
128 AND ( fi.status = 'success' OR fi.status = 'already_optimized' )
129 AND ( fi.data NOT LIKE %s OR fi.data IS NULL )
130 ORDER BY fi.file_id DESC",
131 '%' . $wpdb->esc_like( $nextgen_suffix . '";a:4:{s:7:"success";b:1;' ) . '%'
132 )
133 );
134
135 $wpdb->flush();
136 unset( $mime_types, $files_table, $folders_table, $nextgen_suffix, $mime );
137
138 $data = [
139 'ids' => [],
140 'errors' => [
141 'no_file_path' => [],
142 'no_backup' => [],
143 ],
144 ];
145
146 if ( ! $files ) {
147 return $data;
148 }
149
150 foreach ( $files as $file ) {
151 $file_id = absint( $file->file_id );
152
153 if ( empty( $file->path ) ) {
154 // Problem.
155 $data['errors']['no_file_path'][] = $file_id;
156 continue;
157 }
158
159 $file_path = Imagify_Files_Scan::remove_placeholder( $file->path );
160 $backup_path = Imagify_Custom_Folders::get_file_backup_path( $file_path );
161
162 if ( ! $this->filesystem->exists( $backup_path ) ) {
163 // No backup, no WebP.
164 $data['errors']['no_backup'][] = $file_id;
165 continue;
166 }
167
168 $data['ids'][] = $file_id;
169 }
170
171 return $data;
172 }
173
174 /**
175 * Get the context data.
176 *
177 * @since 1.9
178 *
179 * @return array {
180 * The formated data.
181 *
182 * @type string $count-optimized Number of media optimized.
183 * @type string $count-errors Number of media having an optimization error, with a link to the page listing the optimization errors.
184 * @type string $optimized-size Optimized filesize.
185 * @type string $original-size Original filesize.
186 * }
187 */
188 public function get_context_data() {
189 $data = [
190 'count-optimized' => Imagify_Files_Stats::count_optimized_files(),
191 'count-errors' => Imagify_Files_Stats::count_error_files(),
192 'optimized-size' => Imagify_Files_Stats::get_optimized_size(),
193 'original-size' => Imagify_Files_Stats::get_original_size(),
194 'errors_url' => get_imagify_admin_url( 'folder-errors', $this->context ),
195 ];
196
197 return $this->format_context_data( $data );
198 }
199 }
200