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