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

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

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