PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.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.1, at classes/Bulk/CustomFolders.php

178 lines 4.4 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 WebP versions.
78 *
79 * @since 1.9
80 * @since 1.9.5 The method doesn't return the IDs directly anymore.
81 *
82 * @return array {
83 * @type array $ids A list of media IDs.
84 * @type array $errors {
85 * @type array $no_file_path A list of media IDs.
86 * @type array $no_backup A list of media IDs.
87 * }
88 * }
89 */
90 public function get_optimized_media_ids_without_webp() {
91 global $wpdb;
92
93 $this->set_no_time_limit();
94
95 $files_table = Imagify_Files_DB::get_instance()->get_table_name();
96 $folders_table = Imagify_Folders_DB::get_instance()->get_table_name();
97 $mime_types = Imagify_DB::get_mime_types( 'image' );
98 $webp_suffix = constant( imagify_get_optimization_process_class_name( 'custom-folders' ) . '::WEBP_SUFFIX' );
99 $files = $wpdb->get_results( $wpdb->prepare( // WPCS: unprepared SQL ok.
100 "
101 SELECT fi.file_id, fi.path
102 FROM $files_table as fi
103 INNER JOIN $folders_table AS fo
104 ON ( fi.folder_id = fo.folder_id )
105 WHERE
106 fi.mime_type IN ( $mime_types )
107 AND ( fi.status = 'success' OR fi.status = 'already_optimized' )
108 AND ( fi.data NOT LIKE %s OR fi.data IS NULL )
109 ORDER BY fi.file_id DESC",
110 '%' . $wpdb->esc_like( $webp_suffix . '";a:4:{s:7:"success";b:1;' ) . '%'
111 ) );
112
113 $wpdb->flush();
114 unset( $mime_types, $files_table, $folders_table, $webp_suffix );
115
116 $data = [
117 'ids' => [],
118 'errors' => [
119 'no_file_path' => [],
120 'no_backup' => [],
121 ],
122 ];
123
124 if ( ! $files ) {
125 return $data;
126 }
127
128 foreach ( $files as $file ) {
129 $file_id = absint( $file->file_id );
130
131 if ( empty( $file->path ) ) {
132 // Problem.
133 $data['errors']['no_file_path'][] = $file_id;
134 continue;
135 }
136
137 $file_path = Imagify_Files_Scan::remove_placeholder( $file->path );
138 $backup_path = Imagify_Custom_Folders::get_file_backup_path( $file_path );
139
140 if ( ! $this->filesystem->exists( $backup_path ) ) {
141 // No backup, no WebP.
142 $data['errors']['no_backup'][] = $file_id;
143 continue;
144 }
145
146 $data['ids'][] = $file_id;
147 } // End foreach().
148
149 return $data;
150 }
151
152 /**
153 * Get the context data.
154 *
155 * @since 1.9
156 *
157 * @return array {
158 * The formated data.
159 *
160 * @type string $count-optimized Number of media optimized.
161 * @type string $count-errors Number of media having an optimization error, with a link to the page listing the optimization errors.
162 * @type string $optimized-size Optimized filesize.
163 * @type string $original-size Original filesize.
164 * }
165 */
166 public function get_context_data() {
167 $data = [
168 'count-optimized' => Imagify_Files_Stats::count_optimized_files(),
169 'count-errors' => Imagify_Files_Stats::count_error_files(),
170 'optimized-size' => Imagify_Files_Stats::get_optimized_size(),
171 'original-size' => Imagify_Files_Stats::get_original_size(),
172 'errors_url' => get_imagify_admin_url( 'folder-errors', $this->context ),
173 ];
174
175 return $this->format_context_data( $data );
176 }
177 }
178