PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
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 / deprecated / classes / class-imagify-as3cf-deprecated.php

class-imagify-as3cf-deprecated.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF trunk, at inc/deprecated/classes/class-imagify-as3cf-deprecated.php

314 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
3
4 /**
5 * Class for deprecated methods from Imagify_AS3CF.
6 *
7 * @since 1.7
8 * @author Grégory Viguier
9 * @deprecated
10 */
11 class Imagify_AS3CF_Deprecated {
12
13 /**
14 * Class version.
15 *
16 * @var string
17 * @since 1.0
18 * @since 1.9 Deprecated
19 * @author Grégory Viguier
20 * @deprecated
21 */
22 const VERSION = '1.2';
23
24 /**
25 * Context used with get_imagify_attachment().
26 * It matches the class name Imagify_AS3CF_Attachment.
27 *
28 * @var string
29 * @since 1.6.6
30 * @since 1.9 Deprecated
31 * @author Grégory Viguier
32 * @deprecated
33 */
34 const CONTEXT = 'AS3CF';
35
36
37 /** ----------------------------------------------------------------------------------------- */
38 /** VARIOUS HOOKS =========================================================================== */
39 /** ----------------------------------------------------------------------------------------- */
40
41 /**
42 * Filter the context used for the optimization (and other stuff).
43 * That way, we'll use the class Imagify_AS3CF_Attachment everywhere (instead of Imagify_Attachment), and make all the manual optimizations fine.
44 *
45 * @since 1.6.6
46 * @since 1.9 Deprecated
47 * @author Grégory Viguier
48 * @deprecated
49 *
50 * @param string $context The context to determine the class name.
51 * @param int $attachment_id The attachment ID.
52 * @return string The new context.
53 */
54 public function optimize_attachment_context( $context, $attachment_id ) {
55 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.9' );
56
57 if ( self::CONTEXT === $context || ( 'wp' === $context && imagify_is_attachment_mime_type_supported( $attachment_id ) ) ) {
58 return self::CONTEXT;
59 }
60 return $context;
61 }
62
63 /**
64 * When getting all unoptimized attachment ids before performing a bulk optimization, download the missing files from S3.
65 *
66 * @since 1.6.7
67 * @since 1.9 Deprecated
68 * @author Grégory Viguier
69 * @deprecated
70 *
71 * @param array $ids An array of attachment IDs.
72 * @param array $results An array of the data fetched from the database.
73 * @param int $optimization_level The optimization level that will be used for the optimization.
74 */
75 public function maybe_copy_files_from_s3( $ids, $results, $optimization_level ) {
76 global $wpdb, $as3cf;
77
78 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.9' );
79
80 if ( ! $as3cf || ! $as3cf->is_plugin_setup() ) {
81 return;
82 }
83
84 // Remove from the list files that exist.
85 $ids = array_flip( $ids );
86
87 foreach ( $ids as $id => $i ) {
88 if ( empty( $results['filenames'][ $id ] ) ) {
89 // Problem.
90 unset( $ids[ $id ] );
91 continue;
92 }
93
94 $file_path = get_imagify_attached_file( $results['filenames'][ $id ] );
95
96 /** This filter is documented in inc/deprecated/deprecated.php. */
97 $file_path = apply_filters( 'imagify_file_path', $file_path, $id, 'as3cf_maybe_copy_files_from_s3' );
98
99 if ( ! $file_path || $this->filesystem->exists( $file_path ) ) {
100 // The file exists, no need to retrieve it from S3.
101 unset( $ids[ $id ] );
102 } else {
103 $ids[ $id ] = $file_path;
104 }
105 }
106
107 if ( ! $ids ) {
108 // All files are already on the server.
109 return;
110 }
111
112 // Determine which files are on S3.
113 $ids = array_flip( $ids );
114 $sql_ids = implode( ',', $ids );
115
116 $s3_data = $wpdb->get_results( // WPCS: unprepared SQL ok.
117 "SELECT pm.post_id as id, pm.meta_value as value
118 FROM $wpdb->postmeta as pm
119 WHERE pm.meta_key = 'amazonS3_info'
120 AND pm.post_id IN ( $sql_ids )
121 ORDER BY pm.post_id DESC",
122 ARRAY_A
123 );
124
125 $wpdb->flush();
126
127 if ( ! $s3_data ) {
128 return;
129 }
130
131 unset( $sql_ids );
132 $s3_data = Imagify_DB::combine_query_results( $ids, $s3_data, true );
133
134 // Retrieve the missing files from S3.
135 $ids = array_flip( $ids );
136
137 foreach ( $s3_data as $id => $s3_object ) {
138 $s3_object = maybe_unserialize( $s3_object );
139 $file_path = $ids[ $id ];
140
141 $attachment_backup_path = get_imagify_attachment_backup_path( $file_path );
142 $attachment_status = isset( $results['statuses'][ $id ] ) ? $results['statuses'][ $id ] : false;
143 $attachment_optimization_level = isset( $results['optimization_levels'][ $id ] ) ? $results['optimization_levels'][ $id ] : false;
144
145 // Don't try to re-optimize if there is no backup file.
146 if ( 'success' === $attachment_status && $optimization_level !== $attachment_optimization_level && ! $this->filesystem->exists( $attachment_backup_path ) ) {
147 unset( $s3_data[ $id ], $ids[ $id ] );
148 continue;
149 }
150
151 $directory = $this->filesystem->dir_path( $s3_object['key'] );
152 $directory = $this->filesystem->is_root( $directory ) ? '' : $directory;
153 $s3_object['key'] = $directory . $this->filesystem->file_name( $file_path );
154
155 // Retrieve file from S3.
156 if ( method_exists( $as3cf->plugin_compat, 'copy_s3_file_to_server' ) ) {
157 $as3cf->plugin_compat->copy_s3_file_to_server( $s3_object, $file_path );
158 } else {
159 $as3cf->plugin_compat->copy_provider_file_to_server( $s3_object, $file_path );
160 }
161
162 unset( $s3_data[ $id ], $ids[ $id ] );
163 }
164 }
165
166 /** ----------------------------------------------------------------------------------------- */
167 /** AUTOMATIC OPTIMIZATION: OPTIMIZE AFTER S3 HAS DONE ITS WORK ============================= */
168 /** ----------------------------------------------------------------------------------------- */
169
170 /**
171 * Filter the generated attachment meta data.
172 * This is used when a new attachment has just been uploaded (or not, when wp_generate_attachment_metadata() is used).
173 * We use it to tell the difference later in wp_update_attachment_metadata().
174 *
175 * @since 1.6.6
176 * @since 1.8.4 Deprecated
177 * @author Grégory Viguier
178 * @see $this->do_async_job()
179 * @deprecated
180 *
181 * @param array $metadata An array of attachment meta data.
182 * @param int $attachment_id Current attachment ID.
183 * @return array
184 */
185 public function store_upload_ids( $metadata, $attachment_id ) {
186 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.8.4' );
187
188 if ( imagify_is_attachment_mime_type_supported( $attachment_id ) ) {
189 $this->uploads[ $attachment_id ] = 1;
190 }
191
192 return $metadata;
193 }
194
195 /**
196 * After an image (maybe) being sent to S3, launch an async optimization.
197 *
198 * @since 1.6.6
199 * @since 1.8.4 Deprecated
200 * @author Grégory Viguier
201 * @see $this->store_upload_ids()
202 * @deprecated
203 *
204 * @param array $metadata An array of attachment meta data.
205 * @param int $attachment_id Current attachment ID.
206 * @return array
207 */
208 public function do_async_job( $metadata, $attachment_id ) {
209 static $auto_optimize;
210
211 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.8.4' );
212
213 $is_new_upload = ! empty( $this->uploads[ $attachment_id ] );
214 unset( $this->uploads[ $attachment_id ] );
215
216 if ( ! $metadata || ! imagify_is_attachment_mime_type_supported( $attachment_id ) ) {
217 return $metadata;
218 }
219
220 if ( ! isset( $auto_optimize ) ) {
221 $auto_optimize = Imagify_Requirements::is_api_key_valid() && get_imagify_option( 'auto_optimize' );
222 }
223
224 if ( $is_new_upload ) {
225 // It's a new upload.
226 if ( ! $auto_optimize ) {
227 // Auto-optimization is disabled.
228 return $metadata;
229 }
230
231 /** This filter is documented in inc/common/attachments.php. */
232 $optimize = apply_filters( 'imagify_auto_optimize_attachment', true, $attachment_id, $metadata );
233
234 if ( ! $optimize ) {
235 return $metadata;
236 }
237 }
238
239 if ( ! $is_new_upload ) {
240 $attachment = get_imagify_attachment( self::CONTEXT, $attachment_id, 'as3cf_async_job' );
241
242 if ( ! $attachment->get_data() ) {
243 // It's not a new upload and the attachment is not optimized yet.
244 return $metadata;
245 }
246 }
247
248 $data = array();
249
250 // Some specifics for the image editor.
251 if ( isset( $_POST['action'], $_POST['do'], $_POST['postid'] ) && 'image-editor' === $_POST['action'] && (int) $_POST['postid'] === $attachment_id ) { // WPCS: CSRF ok.
252 check_ajax_referer( 'image_editor-' . $_POST['postid'] );
253 $data = $_POST;
254 }
255
256 imagify_do_async_job( array(
257 'action' => 'imagify_async_optimize_as3cf',
258 '_ajax_nonce' => wp_create_nonce( 'imagify_async_optimize_as3cf' ),
259 'post_id' => $attachment_id,
260 'metadata' => $metadata,
261 'data' => $data,
262 ) );
263
264 return $metadata;
265 }
266
267 /**
268 * Once an image has been sent to S3, optimize it and send it again.
269 *
270 * @since 1.6.6
271 * @since 1.8.4 Deprecated
272 * @author Grégory Viguier
273 * @deprecated
274 */
275 public function optimize() {
276 _deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.8.4' );
277
278 check_ajax_referer( 'imagify_async_optimize_as3cf' );
279
280 if ( empty( $_POST['post_id'] ) || ! imagify_current_user_can( 'auto-optimize' ) ) {
281 die();
282 }
283
284 $attachment_id = absint( $_POST['post_id'] );
285
286 if ( ! $attachment_id || empty( $_POST['metadata'] ) || ! is_array( $_POST['metadata'] ) || empty( $_POST['metadata']['sizes'] ) ) {
287 die();
288 }
289
290 if ( ! imagify_is_attachment_mime_type_supported( $attachment_id ) ) {
291 die();
292 }
293
294 $optimization_level = null;
295 $attachment = get_imagify_attachment( self::CONTEXT, $attachment_id, 'as3cf_optimize' );
296
297 // Some specifics for the image editor.
298 if ( ! empty( $_POST['data']['do'] ) ) {
299 $optimization_level = $attachment->get_optimization_level();
300
301 // Remove old optimization data.
302 $attachment->delete_imagify_data();
303
304 if ( 'restore' === $_POST['data']['do'] ) {
305 // Restore the backup file.
306 $attachment->restore();
307 }
308 }
309
310 // Optimize it.
311 $attachment->optimize( $optimization_level, $_POST['metadata'] );
312 }
313 }
314