← All changes
|
inc/3rd-party/nextgen-gallery/classes/Media/NGG.php
+63
-38
1.10
→
trunk
View file →
| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Imagify\ThirdParty\NGG\Media; |
| 3 | 3 | |
| 4 | -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 4 | +use Imagify\Deprecated\Traits\Media\NGGDeprecatedTrait; | |
| 5 | 5 | |
| 6 | 6 | /** |
| 7 | 7 | * Media class for the medias from NextGen Gallery. |
| 8 | 8 | * |
| @@ -9,9 +9,9 @@ | ||
| 9 | 9 | * @since 1.9 |
| 10 | 10 | * @author Grégory Viguier |
| 11 | 11 | */ |
| 12 | 12 | class NGG extends \Imagify\Media\AbstractMedia { |
| 13 | - use \Imagify\Deprecated\Traits\Media\NGGDeprecatedTrait; | |
| 13 | + use NGGDeprecatedTrait; | |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | 16 | * Context (where the media "comes from"). |
| 17 | 17 | * |
| @@ -80,18 +80,24 @@ | ||
| 80 | 80 | |
| 81 | 81 | // NGG storage. |
| 82 | 82 | if ( ! empty( $this->image->_ngiw ) ) { |
| 83 | 83 | $this->storage = $this->image->_ngiw->get_storage()->object; |
| 84 | - } else { | |
| 84 | + // @codeCoverageIgnoreStart — requires live NGG v3 POPE classes or NGGFOLDER constant; not available in unit tests. | |
| 85 | + } elseif ( imagify_ngg_has_pope_storage() ) { | |
| 86 | + // C_Gallery_Storage is part of the NGG v3 POPE framework; absent on v4. | |
| 85 | 87 | $this->storage = \C_Gallery_Storage::get_instance()->object; |
| 86 | 88 | } |
| 89 | + // else: $this->storage remains null on NGG v4 (no _ngiw and no POPE framework). | |
| 87 | 90 | |
| 88 | - // Load nggAdmin class. | |
| 89 | - $ngg_admin_functions_path = WP_PLUGIN_DIR . '/' . NGGFOLDER . '/products/photocrati_nextgen/modules/ngglegacy/admin/functions.php'; | |
| 91 | + // Load nggAdmin class (NGGFOLDER may be undefined on NGG v4). | |
| 92 | + if ( defined( 'NGGFOLDER' ) && ! class_exists( 'nggAdmin' ) ) { | |
| 93 | + $ngg_admin_functions_path = WP_PLUGIN_DIR . '/' . NGGFOLDER . '/products/photocrati_nextgen/modules/ngglegacy/admin/functions.php'; | |
| 90 | 94 | |
| 91 | - if ( ! class_exists( 'nggAdmin' ) && $this->filesystem->exists( $ngg_admin_functions_path ) ) { | |
| 92 | - require_once $ngg_admin_functions_path; | |
| 95 | + if ( $this->filesystem->exists( $ngg_admin_functions_path ) ) { | |
| 96 | + require_once $ngg_admin_functions_path; | |
| 97 | + } | |
| 93 | 98 | } |
| 99 | + // @codeCoverageIgnoreEnd | |
| 94 | 100 | } |
| 95 | 101 | |
| 96 | 102 | /** |
| 97 | 103 | * Tell if the given entry can be accepted in the constructor. |
| @@ -177,9 +183,9 @@ | ||
| 177 | 183 | /** FULL SIZE FILE ========================================================================== */ |
| 178 | 184 | /** ----------------------------------------------------------------------------------------- */ |
| 179 | 185 | |
| 180 | 186 | /** |
| 181 | - * Get the URL of the media’s full size file. | |
| 187 | + * Get the URL of the media's full size file. | |
| 182 | 188 | * |
| 183 | 189 | * @since 1.9.8 |
| 184 | 190 | * @access public |
| 185 | 191 | * @author Grégory Viguier |
| @@ -198,9 +204,9 @@ | ||
| 198 | 204 | return ! empty( $this->image->imageURL ) ? $this->image->imageURL : false; |
| 199 | 205 | } |
| 200 | 206 | |
| 201 | 207 | /** |
| 202 | - * Get the path to the media’s full size file, even if the file doesn't exist. | |
| 208 | + * Get the path to the media's full size file, even if the file doesn't exist. | |
| 203 | 209 | * |
| 204 | 210 | * @since 1.9.8 |
| 205 | 211 | * @access public |
| 206 | 212 | * @author Grégory Viguier |
| @@ -276,12 +282,17 @@ | ||
| 276 | 282 | if ( ! $this->is_valid() ) { |
| 277 | 283 | return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); |
| 278 | 284 | } |
| 279 | 285 | |
| 280 | - $image_data = $this->storage->_image_mapper->find( $this->get_id() ); // stdClass Object. | |
| 286 | + // @codeCoverageIgnoreStart — requires live NGG storage; not available in unit tests. | |
| 287 | + if ( null === $this->storage ) { | |
| 288 | + return new \WP_Error( 'no_ngg_storage', __( 'NextGen Gallery storage is not available.', 'imagify' ) ); | |
| 289 | + } | |
| 290 | + // @codeCoverageIgnoreEnd | |
| 281 | 291 | |
| 292 | + $image_data = $this->storage->_image_mapper->find( $this->get_id() ); | |
| 293 | + | |
| 282 | 294 | if ( ! $image_data ) { |
| 283 | - // ¯\(°_o)/¯ | |
| 284 | 295 | return new \WP_Error( 'no_ngg_image', __( 'Image not found in NextGen Gallery data.', 'imagify' ) ); |
| 285 | 296 | } |
| 286 | 297 | |
| 287 | 298 | if ( empty( $image_data->meta_data['backup'] ) || ! is_array( $image_data->meta_data['backup'] ) ) { |
| @@ -303,9 +314,9 @@ | ||
| 303 | 314 | continue; |
| 304 | 315 | } |
| 305 | 316 | |
| 306 | 317 | $params = $this->storage->get_image_size_params( $image_data, $size_name ); |
| 307 | - $thumbnail = @$this->storage->generate_image_clone( // Don't remove this @ or the sky will fall. | |
| 318 | + $thumbnail = @$this->storage->generate_image_clone( // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged | |
| 308 | 319 | $backup_path, |
| 309 | 320 | $this->storage->get_image_abspath( $image_data, $size_name ), |
| 310 | 321 | $params |
| 311 | 322 | ); |
| @@ -335,13 +346,17 @@ | ||
| 335 | 346 | $size_meta['crop_frame'] = $params['crop_frame']; |
| 336 | 347 | } |
| 337 | 348 | |
| 338 | 349 | $image_data->meta_data[ $size_name ] = $size_meta; |
| 339 | - } // End foreach(). | |
| 350 | + } | |
| 340 | 351 | |
| 341 | - // Keep our property up to date. | |
| 342 | - $this->image->_ngiw->_cache['meta_data'] = $image_data->meta_data; | |
| 343 | - $this->image->_ngiw->_orig_image = $image_data; | |
| 352 | + // Keep our property up to date (defensively guard _ngiw in case it is absent). | |
| 353 | + // @codeCoverageIgnoreStart — requires live NGG _ngiw storage object; not available in unit tests. | |
| 354 | + if ( isset( $this->image->_ngiw ) ) { | |
| 355 | + $this->image->_ngiw->_cache['meta_data'] = $image_data->meta_data; | |
| 356 | + $this->image->_ngiw->_orig_image = $image_data; | |
| 357 | + } | |
| 358 | + // @codeCoverageIgnoreEnd | |
| 344 | 359 | |
| 345 | 360 | $post_id = $this->storage->_image_mapper->save( $image_data ); |
| 346 | 361 | |
| 347 | 362 | if ( ! $post_id ) { |
| @@ -401,9 +416,9 @@ | ||
| 401 | 416 | * @type string $path Absolute path to the file. |
| 402 | 417 | * @type int $width The file width. |
| 403 | 418 | * @type int $height The file height. |
| 404 | 419 | * @type string $mime-type The file mime type. |
| 405 | - * @type bool $disabled True if the size is disabled in the plugin’s settings. | |
| 420 | + * @type bool $disabled True if the size is disabled in the plugin's settings. | |
| 406 | 421 | * } |
| 407 | 422 | */ |
| 408 | 423 | public function get_media_files() { |
| 409 | 424 | if ( ! $this->is_valid() ) { |
| @@ -432,33 +447,42 @@ | ||
| 432 | 447 | return $this->filter_media_files( $all_sizes ); |
| 433 | 448 | } |
| 434 | 449 | |
| 435 | 450 | // Remove common values (that have no value for us here, lol). Also remove 'full' and 'backup'. |
| 436 | - $image_data = array_diff_key( $this->image->meta_data, [ | |
| 437 | - 'full' => 1, | |
| 438 | - 'backup' => 1, | |
| 439 | - 'width' => 1, | |
| 440 | - 'height' => 1, | |
| 441 | - 'md5' => 1, | |
| 442 | - 'aperture' => 1, | |
| 443 | - 'credit' => 1, | |
| 444 | - 'camera' => 1, | |
| 445 | - 'caption' => 1, | |
| 446 | - 'created_timestamp' => 1, | |
| 447 | - 'copyright' => 1, | |
| 448 | - 'focal_length' => 1, | |
| 449 | - 'iso' => 1, | |
| 450 | - 'shutter_speed' => 1, | |
| 451 | - 'flash' => 1, | |
| 452 | - 'title' => 1, | |
| 453 | - 'keywords' => 1, | |
| 454 | - 'saved' => 1, | |
| 455 | - ] ); | |
| 451 | + $image_data = array_diff_key( | |
| 452 | + is_array( $this->image->meta_data ) ? $this->image->meta_data : [], | |
| 453 | + [ | |
| 454 | + 'full' => 1, | |
| 455 | + 'backup' => 1, | |
| 456 | + 'width' => 1, | |
| 457 | + 'height' => 1, | |
| 458 | + 'md5' => 1, | |
| 459 | + 'aperture' => 1, | |
| 460 | + 'credit' => 1, | |
| 461 | + 'camera' => 1, | |
| 462 | + 'caption' => 1, | |
| 463 | + 'created_timestamp' => 1, | |
| 464 | + 'copyright' => 1, | |
| 465 | + 'focal_length' => 1, | |
| 466 | + 'iso' => 1, | |
| 467 | + 'shutter_speed' => 1, | |
| 468 | + 'flash' => 1, | |
| 469 | + 'title' => 1, | |
| 470 | + 'keywords' => 1, | |
| 471 | + 'saved' => 1, | |
| 472 | + ] | |
| 473 | + ); | |
| 456 | 474 | |
| 457 | 475 | if ( ! $image_data ) { |
| 458 | 476 | return $this->filter_media_files( $all_sizes ); |
| 459 | 477 | } |
| 460 | 478 | |
| 479 | + // @codeCoverageIgnoreStart — requires live NGG storage; not available in unit tests. | |
| 480 | + if ( null === $this->storage ) { | |
| 481 | + return $this->filter_media_files( $all_sizes ); | |
| 482 | + } | |
| 483 | + // @codeCoverageIgnoreEnd | |
| 484 | + | |
| 461 | 485 | $ngg_data = $this->storage->_image_mapper->find( $this->get_id() ); |
| 462 | 486 | |
| 463 | 487 | foreach ( $image_data as $size => $size_data ) { |
| 464 | 488 | if ( ! isset( $size_data['width'], $size_data['height'], $size_data['filename'], $size_data['generated'] ) ) { |
| @@ -501,9 +525,9 @@ | ||
| 501 | 525 | ]; |
| 502 | 526 | } |
| 503 | 527 | |
| 504 | 528 | return [ |
| 505 | - 'width' => ! empty( $this->image->meta_data['width'] ) ? (int) $this->image->meta_data['width'] : 0, | |
| 529 | + 'width' => ! empty( $this->image->meta_data['width'] ) ? (int) $this->image->meta_data['width'] : 0, | |
| 506 | 530 | 'height' => ! empty( $this->image->meta_data['height'] ) ? (int) $this->image->meta_data['height'] : 0, |
| 507 | 531 | ]; |
| 508 | 532 | } |
| 509 | 533 | |
| @@ -531,8 +555,9 @@ | ||
| 531 | 555 | |
| 532 | 556 | foreach ( $data as $k => $v ) { |
| 533 | 557 | if ( ! isset( $this->image->meta_data[ $k ] ) || $this->image->meta_data[ $k ] !== $v ) { |
| 534 | 558 | $this->image->meta_data[ $k ] = $v; |
| 559 | + | |
| 535 | 560 | $changed = true; |
| 536 | 561 | } |
| 537 | 562 | } |
| 538 | 563 | |