PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.10
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.10
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 1.10, at inc/3rd-party/nextgen-gallery/classes/Bulk/NGG.php

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