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 / inc / 3rd-party / nextgen-gallery / classes / Bulk / NGG.php

NGG.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2.8, at inc/3rd-party/nextgen-gallery/classes/Bulk/NGG.php

248 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\ThirdParty\NGG\Bulk;
3
4 use C_Gallery_Storage;
5 use Imagify\Bulk\AbstractBulk;
6 use Imagify\ThirdParty\NGG\DB;
7
8 /**
9 * Class to use for bulk for NextGen Gallery.
10 *
11 * @since 1.9
12 */
13 class NGG extends AbstractBulk {
14 /**
15 * Context "short name".
16 *
17 * @var string
18 * @since 1.9
19 */
20 protected $context = 'ngg';
21
22 /**
23 * Get all unoptimized media ids.
24 *
25 * @since 1.9
26 *
27 * @param int $optimization_level The optimization level.
28 * @return array A list of unoptimized media IDs.
29 */
30 public function get_unoptimized_media_ids( $optimization_level ) {
31 global $wpdb;
32
33 $this->set_no_time_limit();
34
35 $storage = C_Gallery_Storage::get_instance();
36 $ngg_table = $wpdb->prefix . 'ngg_pictures';
37 $data = [];
38 $images = $wpdb->get_results(
39 $wpdb->prepare( // WPCS: unprepared SQL ok.
40 "
41 SELECT DISTINCT picture.pid as id, picture.filename, idata.optimization_level, idata.status, idata.data
42 FROM $ngg_table as picture
43 LEFT JOIN $wpdb->ngg_imagify_data as idata
44 ON picture.pid = idata.pid
45 WHERE idata.pid IS NULL
46 OR idata.optimization_level != %d
47 OR idata.status = 'error'
48 LIMIT %d",
49 $optimization_level,
50 imagify_get_unoptimized_attachment_limit()
51 ),
52 ARRAY_A
53 );
54
55 if ( ! $images ) {
56 return [];
57 }
58
59 foreach ( $images as $image ) {
60 $id = absint( $image['id'] );
61 $file_path = $storage->get_image_abspath( $id );
62
63 if ( ! $file_path || ! $this->filesystem->exists( $file_path ) ) {
64 continue;
65 }
66
67 $attachment_data = maybe_unserialize( $image['data'] );
68 $attachment_error = '';
69
70 if ( isset( $attachment_data['sizes']['full']['error'] ) ) {
71 $attachment_error = $attachment_data['sizes']['full']['error'];
72 }
73
74 $attachment_error = trim( $attachment_error );
75 $attachment_status = $image['status'];
76 $attachment_optimization_level = $image['optimization_level'];
77 $attachment_backup_path = get_imagify_ngg_attachment_backup_path( $file_path );
78
79 // Don't try to re-optimize if the optimization level is still the same.
80 if ( $optimization_level === $attachment_optimization_level && is_string( $attachment_error ) ) {
81 continue;
82 }
83
84 // Don't try to re-optimize if there is no backup file.
85 if ( 'success' === $attachment_status && $optimization_level !== $attachment_optimization_level && ! $this->filesystem->exists( $attachment_backup_path ) ) {
86 continue;
87 }
88
89 // Don't try to re-optimize images already compressed.
90 if ( 'already_optimized' === $attachment_status && $attachment_optimization_level >= $optimization_level ) {
91 continue;
92 }
93
94 // Don't try to re-optimize images with an empty error message.
95 if ( 'error' === $attachment_status && empty( $attachment_error ) ) {
96 continue;
97 }
98
99 $data[] = $id;
100 }
101
102 return $data;
103 }
104
105 /**
106 * Get ids of all optimized media without next-gen versions.
107 *
108 * @since 2.2
109 *
110 * @param string $format Format we are looking for. (webp|avif).
111 *
112 * @return array {
113 * @type array $ids A list of media IDs.
114 * @type array $errors {
115 * @type array $no_file_path A list of media IDs.
116 * @type array $no_backup A list of media IDs.
117 * }
118 * }
119 */
120 public function get_optimized_media_ids_without_format( $format ) {
121 global $wpdb;
122
123 $this->set_no_time_limit();
124
125 $storage = C_Gallery_Storage::get_instance();
126 $ngg_table = $wpdb->prefix . 'ngg_pictures';
127 $data_table = DB::get_instance()->get_table_name();
128 $suffix = constant( imagify_get_optimization_process_class_name( 'ngg' ) . '::WEBP_SUFFIX' );
129
130 if ( 'avif' === get_imagify_option( 'optimization_format' ) ) {
131 $suffix = constant( imagify_get_optimization_process_class_name( 'ngg' ) . '::AVIF_SUFFIX' );
132 }
133
134 $files = $wpdb->get_col(
135 $wpdb->prepare( // WPCS: unprepared SQL ok.
136 "
137 SELECT ngg.pid
138 FROM $ngg_table as ngg
139 INNER JOIN $data_table AS data
140 ON ( ngg.pid = data.pid )
141 WHERE
142 ( data.status = 'success' OR data.status = 'already_optimized' )
143 AND data.data NOT LIKE %s
144 ORDER BY ngg.pid DESC",
145 '%' . $wpdb->esc_like( $suffix . '";a:4:{s:7:"success";b:1;' ) . '%'
146 )
147 );
148
149 $wpdb->flush();
150 unset( $ngg_table, $data_table, $suffix );
151
152 $data = [
153 'ids' => [],
154 'errors' => [
155 'no_file_path' => [],
156 'no_backup' => [],
157 ],
158 ];
159
160 if ( ! $files ) {
161 return $data;
162 }
163
164 foreach ( $files as $file_id ) {
165 $file_id = absint( $file_id );
166 $file_path = $storage->get_image_abspath( $file_id );
167
168 if ( ! $file_path ) {
169 // Problem.
170 $data['errors']['no_file_path'][] = $file_id;
171 continue;
172 }
173
174 $backup_path = get_imagify_ngg_attachment_backup_path( $file_path );
175
176 if ( ! $this->filesystem->exists( $backup_path ) ) {
177 // No backup, no next-gen.
178 $data['errors']['no_backup'][] = $file_id;
179 continue;
180 }
181
182 $data['ids'][] = $file_id;
183 }
184
185 return $data;
186 }
187
188 /**
189 * Tell if there are optimized media without next-gen versions.
190 *
191 * @since 2.2
192 *
193 * @return int The number of media.
194 */
195 public function has_optimized_media_without_nextgen() {
196 global $wpdb;
197
198 $ngg_table = $wpdb->prefix . 'ngg_pictures';
199 $data_table = DB::get_instance()->get_table_name();
200 $suffix = constant( imagify_get_optimization_process_class_name( 'ngg' ) . '::WEBP_SUFFIX' );
201
202 if ( 'avif' === get_imagify_option( 'optimization_format' ) ) {
203 $suffix = constant( imagify_get_optimization_process_class_name( 'ngg' ) . '::AVIF_SUFFIX' );
204 }
205
206 return (int) $wpdb->get_var(
207 $wpdb->prepare( // WPCS: unprepared SQL ok.
208 "
209 SELECT COUNT(ngg.pid)
210 FROM $ngg_table as ngg
211 INNER JOIN $data_table AS data
212 ON ( ngg.pid = data.pid )
213 WHERE
214 ( data.status = 'success' OR data.status = 'already_optimized' )
215 AND data.data NOT LIKE %s",
216 '%' . $wpdb->esc_like( $suffix . '";a:4:{s:7:"success";b:1;' ) . '%'
217 )
218 );
219 }
220
221 /**
222 * Get the context data.
223 *
224 * @since 1.9
225 *
226 * @return array {
227 * The formated data.
228 *
229 * @type string $count-optimized Number of media optimized.
230 * @type string $count-errors Number of media having an optimization error, with a link to the page listing the optimization errors.
231 * @type string $optimized-size Optimized filesize.
232 * @type string $original-size Original filesize.
233 * }
234 */
235 public function get_context_data() {
236 $total_saving_data = imagify_count_saving_data();
237 $data = [
238 'count-optimized' => imagify_ngg_count_optimized_attachments(),
239 'count-errors' => imagify_ngg_count_error_attachments(),
240 'optimized-size' => $total_saving_data['optimized_size'],
241 'original-size' => $total_saving_data['original_size'],
242 'errors_url' => get_imagify_admin_url( 'folder-errors', $this->context ),
243 ];
244
245 return $this->format_context_data( $data );
246 }
247 }
248