| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The class maps settings and options from imageproxy. |
| 5 |
* |
| 6 |
* @package \Optml\Inc |
| 7 |
* @author Optimole <friends@optimole.com> |
| 8 |
*/ |
| 9 |
class Optml_Image extends Optml_Resource { |
| 10 |
use Optml_Dam_Offload_Utils; |
| 11 |
|
| 12 |
/** |
| 13 |
* Optimole Settings |
| 14 |
* |
| 15 |
* @var bool | null |
| 16 |
*/ |
| 17 |
public static $offload_enabled = null; |
| 18 |
/** |
| 19 |
* Watermark for the image. |
| 20 |
* |
| 21 |
* @var Optml_Watermark Watermark. |
| 22 |
*/ |
| 23 |
public static $watermark = null; |
| 24 |
/** |
| 25 |
* Quality of the resulting image. |
| 26 |
* |
| 27 |
* @var Optml_Quality Quality; |
| 28 |
*/ |
| 29 |
public $quality = null; |
| 30 |
/** |
| 31 |
* Width of the resulting image. |
| 32 |
* |
| 33 |
* @var Optml_Width Width. |
| 34 |
*/ |
| 35 |
private $width = null; |
| 36 |
/** |
| 37 |
* Height of the resulting image. |
| 38 |
* |
| 39 |
* @var Optml_Height Height. |
| 40 |
*/ |
| 41 |
private $height = null; |
| 42 |
/** |
| 43 |
* Resize type for the image. |
| 44 |
* |
| 45 |
* @var Optml_Resize Resize details. |
| 46 |
*/ |
| 47 |
private $resize = null; |
| 48 |
|
| 49 |
/** |
| 50 |
* The attachment id. |
| 51 |
* |
| 52 |
* @var null | int Attachment id. |
| 53 |
*/ |
| 54 |
private $attachment_id = null; |
| 55 |
|
| 56 |
/** |
| 57 |
* Optml_Image constructor. |
| 58 |
* |
| 59 |
* @param string $url Source image url. |
| 60 |
* @param array $args Transformation arguments. |
| 61 |
* |
| 62 |
* @throws \InvalidArgumentException In case that the url is not provided. |
| 63 |
*/ |
| 64 |
public function __construct( $url = '', $args = [], $cache_buster = '' ) { |
| 65 |
parent::__construct( $url, $cache_buster ); |
| 66 |
|
| 67 |
if ( self::$offload_enabled === null ) { |
| 68 |
self::$offload_enabled = ( new Optml_Settings() )->is_offload_enabled(); |
| 69 |
} |
| 70 |
|
| 71 |
$this->width->set( $args['width'] ); |
| 72 |
$this->height->set( $args['height'] ); |
| 73 |
|
| 74 |
if ( isset( $args['quality'] ) ) { |
| 75 |
$this->quality->set( $args['quality'] ); |
| 76 |
} |
| 77 |
|
| 78 |
if ( isset( $args['resize'] ) ) { |
| 79 |
$this->resize->set( $args['resize'] ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( isset( $args['attachment_id'] ) ) { |
| 83 |
$this->attachment_id = $args['attachment_id']; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Set defaults for image transformations. |
| 89 |
*/ |
| 90 |
protected function set_defaults() { |
| 91 |
$this->width = new Optml_Width( 'auto' ); |
| 92 |
$this->height = new Optml_Height( 'auto' ); |
| 93 |
$this->resize = new Optml_Resize(); |
| 94 |
$this->quality = new Optml_Quality(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Return transformed url. |
| 99 |
* |
| 100 |
* @param array $params Image transforms parameters. |
| 101 |
* |
| 102 |
* @return string Transformed image url. |
| 103 |
*/ |
| 104 |
public function get_url( $params = [] ) { |
| 105 |
$path_params = $this->get_path_params( $params ); |
| 106 |
|
| 107 |
if ( $this->is_dam_url() ) { |
| 108 |
return $this->get_dam_url( $path_params ); |
| 109 |
} |
| 110 |
|
| 111 |
if ( $this->is_offloaded_url() ) { |
| 112 |
return $this->get_offloaded_url( $path_params ); |
| 113 |
} |
| 114 |
|
| 115 |
$path = $path_params . '/' . $this->source_url; |
| 116 |
|
| 117 |
return sprintf( '%s%s', Optml_Config::$service_url, $path ); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check if this contains the DAM flag. |
| 123 |
* |
| 124 |
* @return bool |
| 125 |
*/ |
| 126 |
public function is_dam_url() { |
| 127 |
return strpos( $this->source_url, Optml_Dam::URL_DAM_FLAG ) !== false; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get the DAM image URL. |
| 132 |
* |
| 133 |
* @param string $path_params Path parameters. |
| 134 |
* |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
private function get_dam_url( $path_params ) { |
| 138 |
$url = $this->source_url; |
| 139 |
|
| 140 |
// Remove DAM flag. |
| 141 |
$url = str_replace( Optml_Dam::URL_DAM_FLAG, '', $url ); |
| 142 |
|
| 143 |
// Split the path params. |
| 144 |
$path_params_parts = explode( '/', $path_params ); |
| 145 |
|
| 146 |
foreach ( $path_params_parts as $param ) { |
| 147 |
if ( empty( $param ) ) { |
| 148 |
continue; |
| 149 |
} |
| 150 |
|
| 151 |
// Split into parts. |
| 152 |
$param_parts = explode( ':', $param ); |
| 153 |
|
| 154 |
// Check if the param is width or height and if it's auto. |
| 155 |
// Rely on the initial URL as it generates the correct image size. |
| 156 |
if ( in_array( $param_parts[0], ['w', 'h'], true ) ) { |
| 157 |
if ( $param_parts[1] === 'auto' ) { |
| 158 |
continue; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// if the param exists we do a non-greedy replace for /$param:$value/ with the updated value. |
| 163 |
if ( strpos( $url, '/' . $param_parts[0] . ':' ) !== false ) { |
| 164 |
$url = preg_replace( '/\/' . $param_parts[0] . ':.*?\//', '/' . $param . '/', $url ); |
| 165 |
|
| 166 |
continue; |
| 167 |
} |
| 168 |
|
| 169 |
// Otherwise, we add it before the /q: param. |
| 170 |
$url = str_replace( '/q:', '/' . $param . '/q:', $url ); |
| 171 |
} |
| 172 |
|
| 173 |
return $url; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Get transform params as string. |
| 178 |
* |
| 179 |
* @param array $params Image transforms parameters. |
| 180 |
* |
| 181 |
* @return string Transform parameters string to use in the URL. |
| 182 |
*/ |
| 183 |
private function get_path_params( $params = [] ) { |
| 184 |
$path_parts = []; |
| 185 |
|
| 186 |
$path_parts[] = $this->width->toString(); |
| 187 |
$path_parts[] = $this->height->toString(); |
| 188 |
$path_parts[] = $this->quality->toString(); |
| 189 |
|
| 190 |
if ( isset( $this->resize->get()['type'] ) ) { |
| 191 |
$path_parts[] = $this->resize->toString(); |
| 192 |
} |
| 193 |
if ( isset( $params['apply_watermark'] ) && $params['apply_watermark'] && is_array( self::$watermark->get() ) && isset( self::$watermark->get()['id'] ) && self::$watermark->get()['id'] > 0 ) { |
| 194 |
$path_parts[] = self::$watermark->toString(); |
| 195 |
} |
| 196 |
|
| 197 |
$path = ''; |
| 198 |
|
| 199 |
if ( isset( $params['format'] ) && ! empty( $params['format'] ) ) { |
| 200 |
$path = '/f:' . $params['format'] . $path; |
| 201 |
} |
| 202 |
|
| 203 |
if ( isset( $params['strip_metadata'] ) && '0' === $params['strip_metadata'] ) { |
| 204 |
$path = '/sm:' . $params['strip_metadata'] . $path; |
| 205 |
} |
| 206 |
|
| 207 |
if ( isset( $params['ignore_avif'] ) && $params['ignore_avif'] === true ) { |
| 208 |
$path = '/ig:avif' . $path; |
| 209 |
} |
| 210 |
|
| 211 |
if ( $this->has_attachment_id() ) { |
| 212 |
$path = $path . sprintf( '/%s%s', Optml_Media_Offload::KEYS['not_processed_flag'], $this->attachment_id ); |
| 213 |
} |
| 214 |
|
| 215 |
if ( apply_filters( 'optml_keep_copyright', false ) === true ) { |
| 216 |
$path = '/keep_copyright:true' . $path; |
| 217 |
} |
| 218 |
|
| 219 |
$path = sprintf( '/%s%s', implode( '/', $path_parts ), $path ); |
| 220 |
|
| 221 |
$cache_buster = $this->get_cache_buster(); |
| 222 |
if ( $cache_buster !== false ) { |
| 223 |
$path = sprintf( '/%s%s', 'cb:' . $cache_buster, $path ); |
| 224 |
} |
| 225 |
|
| 226 |
return $path; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Check if this URL was offloaded. |
| 231 |
* |
| 232 |
* @return bool |
| 233 |
*/ |
| 234 |
private function is_offloaded_url() { |
| 235 |
if ( ! self::$offload_enabled ) { |
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
// Catch this from URLs that explicitly have the flag when constructing the image. |
| 240 |
if ( $this->attachment_id !== null && $this->attachment_id > 0 ) { |
| 241 |
return ! empty( get_post_meta( $this->attachment_id, Optml_Media_Offload::OM_OFFLOADED_FLAG, true ) ); |
| 242 |
} |
| 243 |
|
| 244 |
$attachment_id = 0; |
| 245 |
|
| 246 |
if ( strpos( $this->source_url, Optml_Media_Offload::KEYS['not_processed_flag'] ) !== false ) { |
| 247 |
$attachment_id = (int) Optml_Media_Offload::get_attachment_id_from_url( $this->source_url ); |
| 248 |
} else { |
| 249 |
$attachment_id = $this->attachment_url_to_post_id( $this->source_url ); |
| 250 |
} |
| 251 |
|
| 252 |
if ( $attachment_id === 0 ) { |
| 253 |
return false; |
| 254 |
} |
| 255 |
|
| 256 |
if ( empty( get_post_meta( $attachment_id, Optml_Media_Offload::OM_OFFLOADED_FLAG, true ) ) ) { |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
$this->attachment_id = (int) $attachment_id; |
| 261 |
|
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Get offloaded URL. |
| 267 |
* |
| 268 |
* @param string $path_params Path parameters. |
| 269 |
* |
| 270 |
* @return string |
| 271 |
*/ |
| 272 |
private function get_offloaded_url( $path_params ) { |
| 273 |
$metadata = wp_get_attachment_metadata( $this->attachment_id ); |
| 274 |
|
| 275 |
return sprintf( '%s%s%s', Optml_Config::$service_url, $path_params, $metadata['file'] ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Check if the URL has an attachment ID. |
| 280 |
* |
| 281 |
* @return bool |
| 282 |
*/ |
| 283 |
private function has_attachment_id() { |
| 284 |
return $this->attachment_id !== null && $this->attachment_id !== 0; |
| 285 |
} |
| 286 |
} |
| 287 |
|