PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.11.2
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.11.2
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / inc / traits / dam_offload_utils.php

dam_offload_utils.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 3.11.2, at inc/traits/dam_offload_utils.php

267 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 trait Optml_Dam_Offload_Utils {
4 /**
5 * Checks that the attachment is a DAM image.
6 *
7 * @param int $post_id The attachment ID.
8 *
9 * @return bool
10 */
11 private function is_dam_imported_image( $post_id ) {
12 $meta = get_post_meta( $post_id, Optml_Dam::OM_DAM_IMPORTED_FLAG, true );
13
14 if ( empty( $meta ) ) {
15 return false;
16 }
17
18 return true;
19 }
20
21 /**
22 * Checks if the attachment is offloaded using the old method.
23 *
24 * @param int $id The attachment ID.
25 *
26 * @return bool
27 */
28 private function is_legacy_offloaded_attachment( $id ) {
29 return ! $this->is_new_offloaded_attachment( $id ) && ! empty( get_post_meta( $id, Optml_Media_Offload::META_KEYS['offloaded'] ) );
30 }
31
32 /**
33 * Check if it's a newly offloaded attachment
34 *
35 * @param int $id The attachment ID.
36 *
37 * @return bool
38 */
39 private function is_new_offloaded_attachment( $id ) {
40 return ! empty( get_post_meta( $id, Optml_Media_Offload::OM_OFFLOADED_FLAG, true ) );
41 }
42
43 /**
44 * Get all registered image sizes.
45 *
46 * @return array
47 */
48 private function get_all_image_sizes() {
49 $additional_sizes = wp_get_additional_image_sizes();
50 $intermediate = get_intermediate_image_sizes();
51 $all = [];
52
53 foreach ( $intermediate as $size ) {
54 if ( isset( $additional_sizes[ $size ] ) ) {
55 $all[ $size ] = [
56 'width' => $additional_sizes[ $size ]['width'],
57 'height' => $additional_sizes[ $size ]['height'],
58 'crop' => isset( $additional_sizes[ $size ]['crop'] ) ? $additional_sizes[ $size ]['crop'] : false,
59 ];
60 } else {
61 $all[ $size ] = [
62 'width' => (int) get_option( $size . '_size_w' ),
63 'height' => (int) get_option( $size . '_size_h' ),
64 'crop' => get_option( $size . '_crop' ),
65 ];
66 }
67
68 if ( ! empty( $additional_sizes[ $size ]['crop'] ) ) {
69 $all[ $size ]['crop'] = is_array( $additional_sizes[ $size ]['crop'] ) ? $additional_sizes[ $size ]['crop'] : (bool) $additional_sizes[ $size ]['crop'];
70
71 } else {
72 $all[ $size ]['crop'] = (bool) get_option( $size . '_crop' );
73 }
74 }
75
76 return $all;
77 }
78
79 /**
80 * Get the dimension from optimized url.
81 *
82 * @param string $url The image url.
83 *
84 * @return array Contains the width and height values in this order.
85 */
86 private function parse_dimension_from_optimized_url( $url ) {
87 $catch = [];
88 $height = 'auto';
89 $width = 'auto';
90 preg_match( '/\/w:(.*)\/h:(.*)\/q:/', $url, $catch );
91 if ( isset( $catch[1] ) && isset( $catch[2] ) ) {
92 $width = $catch[1];
93 $height = $catch[2];
94 }
95 return [ $width, $height ];
96 }
97
98 /**
99 * Check if we're in the attachment edit page.
100 *
101 * /wp-admin/post.php?post=<id>&action=edit
102 *
103 * Send whatever comes from the DAM.
104 *
105 * @param int $attachment_id attachment id.
106 *
107 * @return bool
108 */
109 private function is_attachment_edit_page( $attachment_id ) {
110 if ( ! is_admin() ) {
111 return false;
112 }
113
114 if ( ! function_exists( 'get_current_screen' ) ) {
115 return false;
116 }
117
118 $screen = get_current_screen();
119
120 if ( ! isset( $screen->base ) ) {
121 return false;
122 }
123
124 if ( $screen->base !== 'post' ) {
125 return false;
126 }
127
128 if ( $screen->post_type !== 'attachment' ) {
129 return false;
130 }
131
132 if ( $screen->id !== 'attachment' ) {
133 return false;
134 }
135
136 if ( ! isset( $_GET['post'] ) ) {
137 return false;
138 }
139
140 if ( (int) sanitize_text_field( $_GET['post'] ) !== $attachment_id ) {
141 return false;
142 }
143
144 return true;
145 }
146
147 /**
148 * Used to filter the image metadata. Adds optimized image url for all image sizes.
149 *
150 * @param array $metadata The attachment metadata.
151 * @param int $id The attachment id.
152 *
153 * @return mixed
154 */
155 private function get_altered_metadata_for_remote_images( $metadata, $id ) {
156 $sizes = $this->get_all_image_sizes();
157
158 $post = get_post( $id );
159
160 $sizes_meta = [];
161
162 // SVG files don't have a width/height so we add a dummy one. These are vector images so it doesn't matter.
163 $is_svg = ( $post->post_mime_type === Optml_Config::$image_extensions['svg'] );
164
165 if ( $is_svg ) {
166 $metadata['width'] = 150;
167 $metadata['height'] = 150;
168 }
169
170 if ( ! isset( $metadata['height'] ) || ! isset( $metadata['width'] ) ) {
171 return $metadata;
172 }
173
174 foreach ( $sizes as $size => $args ) {
175 // check if the image is portrait or landscape using attachment metadata.
176 $is_portrait = $metadata['height'] > $metadata['width'];
177
178 // proportionally set the width/height based on this if image is uncropped.
179 if ( ! (bool) $args['crop'] ) {
180 if ( $is_portrait ) {
181 $args['width'] = (int) ( $args['height'] * round( $metadata['width'] / $metadata['height'] ) );
182 } else {
183 $args['height'] = (int) ( $args['width'] * round( $metadata['height'] / $metadata['width'] ) );
184 }
185 }
186
187 $sizes_meta[ $size ] = [
188 'file' => $metadata['file'],
189 'width' => $args['width'],
190 'height' => $args['height'],
191 'mime-type' => $post->post_mime_type,
192 ];
193 }
194
195 $metadata['sizes'] = $sizes_meta;
196
197 return $metadata;
198 }
199
200 /**
201 * Get the scaled URL.
202 *
203 * @param string $url Original URL.
204 *
205 * @return string
206 */
207 private function get_scaled_url( $url ) {
208 $extension = pathinfo( $url, PATHINFO_EXTENSION );
209
210 return str_replace( '.' . $extension, '-scaled.' . $extension, $url );
211 }
212 /**
213 * Check if the URL is a scaled image.
214 *
215 * @param string $url The URL to check.
216 *
217 * @return bool
218 */
219 private function is_scaled_url( $url ) {
220 return strpos( $url, '-scaled.' ) !== false;
221 }
222 /**
223 * Get the attachment ID from URL.
224 *
225 * @param string $url The attachment URL.
226 *
227 * @return int
228 */
229 private function attachment_url_to_post_id( $url ) {
230 $cached = Optml_Attachment_Cache::get_cached_attachment_id( $url );
231
232 if ( $cached !== false ) {
233 return $cached;
234 }
235
236 $url = $this->strip_image_size( $url );
237
238 $attachment_id = attachment_url_to_postid( $url );
239
240 if ( $attachment_id === 0 && ! $this->is_scaled_url( $url ) ) {
241 $scaled_url = $this->get_scaled_url( $url );
242
243 $attachment_id = attachment_url_to_postid( $scaled_url );
244 }
245
246 Optml_Attachment_Cache::set_cached_attachment_id( $url, $attachment_id );
247
248 return $attachment_id;
249 }
250
251 /**
252 * Strips the image size from the URL.
253 *
254 * @param string $url URL to strip.
255 *
256 * @return string
257 */
258 private function strip_image_size( $url ) {
259 if ( preg_match( '#(-\d+x\d+(?:_c)?|(@2x))\.(' . implode( '|', array_keys( Optml_Config::$image_extensions ) ) . '){1}$#i', $url, $src_parts ) ) {
260 $url = str_replace( $src_parts[1], '', $url );
261 }
262
263 return $url;
264
265 }
266 }
267