| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
class Imagify_Abstract_Attachment { |
| 5 |
/** |
| 6 |
* The attachment ID |
| 7 |
* |
| 8 |
* @since 1.0 |
| 9 |
* |
| 10 |
* @var int |
| 11 |
* @access public |
| 12 |
*/ |
| 13 |
public $id; |
| 14 |
|
| 15 |
/** |
| 16 |
* The constructor |
| 17 |
* |
| 18 |
* @since 1.0 |
| 19 |
* |
| 20 |
* @return void |
| 21 |
**/ |
| 22 |
function __construct( $id = 0 ) {} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get the attachment backup filepath. |
| 26 |
* |
| 27 |
* @since 1.0 |
| 28 |
* |
| 29 |
* @access public |
| 30 |
* @return string|false |
| 31 |
*/ |
| 32 |
public function get_backup_path() {} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get the attachment backup URL. |
| 36 |
* |
| 37 |
* @since 1.4 |
| 38 |
* |
| 39 |
* @access public |
| 40 |
* @return string|false |
| 41 |
*/ |
| 42 |
public function get_backup_url() { |
| 43 |
$backup_path = $this->get_backup_path(); |
| 44 |
$backup_url = str_replace( ABSPATH, site_url( '/' ), $backup_path ); |
| 45 |
|
| 46 |
return $backup_url; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get the attachment optimization data. |
| 51 |
* |
| 52 |
* @since 1.0 |
| 53 |
* |
| 54 |
* @access public |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
public function get_data() {} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get the attachment extension. |
| 61 |
* |
| 62 |
* @since 1.0 |
| 63 |
* |
| 64 |
* @access public |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function get_extension() { |
| 68 |
$fullsize_path = $this->get_original_path(); |
| 69 |
return pathinfo( $fullsize_path, PATHINFO_EXTENSION ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get the attachment error if there is one. |
| 74 |
* |
| 75 |
* @since 1.1.5 |
| 76 |
* |
| 77 |
* @access public |
| 78 |
* @return string The message error |
| 79 |
*/ |
| 80 |
public function get_optimized_error() { |
| 81 |
$error = $this->get_size_data( 'full', 'error' ); |
| 82 |
|
| 83 |
if ( is_string( $error ) ) { |
| 84 |
return $error; |
| 85 |
} |
| 86 |
|
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get the attachment optimization level. |
| 92 |
* |
| 93 |
* @since 1.0 |
| 94 |
* |
| 95 |
* @access public |
| 96 |
* @return int |
| 97 |
*/ |
| 98 |
public function get_optimization_level() {} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get the attachment optimization level label. |
| 102 |
* |
| 103 |
* @since 1.2 |
| 104 |
* |
| 105 |
* @access public |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
public function get_optimization_level_label() { |
| 109 |
$label = ''; |
| 110 |
$level = $this->get_optimization_level(); |
| 111 |
|
| 112 |
switch( $level ) { |
| 113 |
case 2: |
| 114 |
$label = __( 'Ultra', 'imagify' ); |
| 115 |
break; |
| 116 |
case 1: |
| 117 |
$label = __( 'Aggressive', 'imagify' ); |
| 118 |
break; |
| 119 |
case 0: |
| 120 |
$label = __( 'Normal', 'imagify' ); |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
return $label; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Count number of optimized sizes. |
| 129 |
* |
| 130 |
* @since 1.0 |
| 131 |
* |
| 132 |
* @access public |
| 133 |
* @return int |
| 134 |
*/ |
| 135 |
public function get_optimized_sizes_count() { |
| 136 |
$data = $this->get_data(); |
| 137 |
$sizes = (array) $data['sizes']; |
| 138 |
$count = 0; |
| 139 |
|
| 140 |
if ( isset( $sizes['full'] ) ) { |
| 141 |
unset( $sizes['full'] ); |
| 142 |
} |
| 143 |
|
| 144 |
foreach ( $sizes as $size ) { |
| 145 |
if ( $size['success'] ) { |
| 146 |
$count++; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return (int) $count; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Get the attachment optimization status (success or error). |
| 155 |
* |
| 156 |
* @since 1.0 |
| 157 |
* |
| 158 |
* @access public |
| 159 |
* @return string |
| 160 |
*/ |
| 161 |
public function get_status() {} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get the original attachment path. |
| 165 |
* |
| 166 |
* @since 1.0 |
| 167 |
* |
| 168 |
* @access public |
| 169 |
* @return string |
| 170 |
*/ |
| 171 |
public function get_original_path() {} |
| 172 |
|
| 173 |
/** |
| 174 |
* Get the original attachment size. |
| 175 |
* |
| 176 |
* @since 1.0 |
| 177 |
* |
| 178 |
* @access public |
| 179 |
* @param bool $human_format True to display the image human format size (1Mb). |
| 180 |
* @return string |
| 181 |
*/ |
| 182 |
public function get_original_size( $human_format = true ) { |
| 183 |
$original_size = $this->get_size_data( 'full', 'original_size' ); |
| 184 |
$original_size = ( empty( $original_size ) ) ? @filesize( $this->get_original_path() ) : 0; |
| 185 |
|
| 186 |
if ( true === $human_format ) { |
| 187 |
$original_size = @size_format( $original_size, 2 ); |
| 188 |
} |
| 189 |
|
| 190 |
return $original_size; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get the original attachment URL. |
| 195 |
* |
| 196 |
* @since 1.0 |
| 197 |
* |
| 198 |
* @access public |
| 199 |
* @return string |
| 200 |
*/ |
| 201 |
public function get_original_url() {} |
| 202 |
|
| 203 |
/* |
| 204 |
* Get the statistics of a specific size. |
| 205 |
* |
| 206 |
* @since 1.0 |
| 207 |
* |
| 208 |
* @access public |
| 209 |
* @param string $size The thumbnail slug. |
| 210 |
* @param string $key The specific data slug. |
| 211 |
* @return array|string |
| 212 |
*/ |
| 213 |
public function get_size_data( $size = 'full', $key = '' ) { |
| 214 |
$data = $this->get_data(); |
| 215 |
$stats = array(); |
| 216 |
|
| 217 |
if ( isset( $data['sizes'][ $size ] ) ) { |
| 218 |
$stats = $data['sizes'][ $size ]; |
| 219 |
} |
| 220 |
|
| 221 |
if ( isset( $stats[ $key ] ) ) { |
| 222 |
$stats = $stats[ $key ]; |
| 223 |
} |
| 224 |
|
| 225 |
return $stats; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get the global statistics data or a specific one. |
| 230 |
* |
| 231 |
* @since 1.0 |
| 232 |
* |
| 233 |
* @access public |
| 234 |
* @param string $key The specific data slug. |
| 235 |
* @return array|string |
| 236 |
*/ |
| 237 |
public function get_stats_data( $key = '' ) { |
| 238 |
$data = $this->get_data(); |
| 239 |
$stats = ''; |
| 240 |
|
| 241 |
if ( isset( $data['stats'] ) ) { |
| 242 |
$stats = $data['stats']; |
| 243 |
} |
| 244 |
|
| 245 |
if ( isset( $stats[ $key ] ) ) { |
| 246 |
$stats = $stats[ $key ]; |
| 247 |
} |
| 248 |
|
| 249 |
return $stats; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Check if the attachment is already optimized (before Imagify). |
| 254 |
* |
| 255 |
* @since 1.1.6 |
| 256 |
* |
| 257 |
* @access public |
| 258 |
* @return bool True if the attachment is optimized. |
| 259 |
*/ |
| 260 |
public function is_already_optimized() { |
| 261 |
return ( 'already_optimized' === $this->get_status() ) > 0; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Check if the attachment is optimized. |
| 266 |
* |
| 267 |
* @since 1.0 |
| 268 |
* |
| 269 |
* @access public |
| 270 |
* @return bool True if the attachment is optimized. |
| 271 |
*/ |
| 272 |
public function is_optimized() { |
| 273 |
return ( 'success' === $this->get_status() ) > 0; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Check if the attachment exceeding the limit size (> 5mo). |
| 278 |
* |
| 279 |
* @since 1.0 |
| 280 |
* |
| 281 |
* @access public |
| 282 |
* @return bool True if the attachment is skipped. |
| 283 |
*/ |
| 284 |
public function is_exceeded() { |
| 285 |
$filepath = $this->get_original_path(); |
| 286 |
$size = 0; |
| 287 |
|
| 288 |
if ( file_exists( $filepath ) ) { |
| 289 |
$size = filesize( $filepath ); |
| 290 |
} |
| 291 |
|
| 292 |
return ( $size > IMAGIFY_MAX_BYTES ) > 0; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Check if the attachment has a backup of the original size. |
| 297 |
* |
| 298 |
* @since 1.0 |
| 299 |
* |
| 300 |
* @access public |
| 301 |
* @return bool True if the attachment has a backup. |
| 302 |
*/ |
| 303 |
public function has_backup() { |
| 304 |
return (bool) $this->get_backup_path(); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Check if the attachment has an error. |
| 309 |
* |
| 310 |
* @since 1.0 |
| 311 |
* |
| 312 |
* @access public |
| 313 |
* @return bool True if the attachment has an error. |
| 314 |
*/ |
| 315 |
public function has_error() { |
| 316 |
$has_error = $this->get_size_data( 'full', 'error' ); |
| 317 |
return ( is_string( $has_error ) ) > 0; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Update the metadata size of the attachment |
| 322 |
* |
| 323 |
* @since 1.2 |
| 324 |
* |
| 325 |
* @access public |
| 326 |
* @return void |
| 327 |
*/ |
| 328 |
public function update_metadata_size() {} |
| 329 |
|
| 330 |
/** |
| 331 |
* Delete the backup file. |
| 332 |
* |
| 333 |
* @since 1.0 |
| 334 |
* |
| 335 |
* @access public |
| 336 |
* @return void |
| 337 |
*/ |
| 338 |
public function delete_backup() { |
| 339 |
$backup_path = $this->get_backup_path(); |
| 340 |
|
| 341 |
if ( ! empty( $backup_path ) ) { |
| 342 |
@unlink( $backup_path ); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Fills statistics data with values from $data array |
| 348 |
* |
| 349 |
* @since 1.0 |
| 350 |
* |
| 351 |
* @access public |
| 352 |
* @static |
| 353 |
* @param array $data The statistics data |
| 354 |
* @param object $response The API response |
| 355 |
* @param int $id The attachment ID |
| 356 |
* @param int $url The attachment URL |
| 357 |
* @param string $size The attachment size key |
| 358 |
* @return bool|array False if the original size has an error or an array contains the data for other result |
| 359 |
*/ |
| 360 |
static public function fill_data( $data, $response, $id, $url, $size = 'full' ) {} |
| 361 |
|
| 362 |
/** |
| 363 |
* Optimize all sizes with Imagify. |
| 364 |
* |
| 365 |
* @since 1.0 |
| 366 |
* |
| 367 |
* @access public |
| 368 |
* @param int $optimization_level The optimization level (2=ultra, 1=aggressive, 0=normal) |
| 369 |
* @param array $metadata The attachment meta data |
| 370 |
* @return array $optimized_data The optimization data |
| 371 |
*/ |
| 372 |
public function optimize( $optimization_level = null, $metadata = array() ) {} |
| 373 |
|
| 374 |
/** |
| 375 |
* Process an attachment restoration from the backup file. |
| 376 |
* |
| 377 |
* @since 1.0 |
| 378 |
* |
| 379 |
* @access public |
| 380 |
* @return void |
| 381 |
*/ |
| 382 |
public function restore() {} |
| 383 |
} |