| 1 |
<?php |
| 2 |
namespace Imagify\ThirdParty\NGG; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Class that handles the optimization of thumbnails dynamically generated. |
| 8 |
* |
| 9 |
* @since 1.9 |
| 10 |
* @author Grégory Viguier |
| 11 |
*/ |
| 12 |
class DynamicThumbnails { |
| 13 |
use \Imagify\Traits\InstanceGetterTrait; |
| 14 |
|
| 15 |
/** |
| 16 |
* The queue containing the sizes, grouped by image ID. |
| 17 |
* |
| 18 |
* @var array |
| 19 |
* @since 1.9 |
| 20 |
* @access protected |
| 21 |
* @author Grégory Viguier |
| 22 |
*/ |
| 23 |
protected static $sizes = []; |
| 24 |
|
| 25 |
/** |
| 26 |
* A list of NGG image objects, grouped by image ID. |
| 27 |
* |
| 28 |
* @var array |
| 29 |
* @since 1.9 |
| 30 |
* @access protected |
| 31 |
* @author Grégory Viguier |
| 32 |
*/ |
| 33 |
protected static $images = []; |
| 34 |
|
| 35 |
/** |
| 36 |
* Add a dynamically generated thumbnail to the background process queue. |
| 37 |
* |
| 38 |
* @since 1.9 |
| 39 |
* @access public |
| 40 |
* @author Grégory Viguier |
| 41 |
* |
| 42 |
* @param object $image A NGG image object. |
| 43 |
* @param string $size The thumbnail size name. |
| 44 |
*/ |
| 45 |
public function push_to_queue( $image, $size ) { |
| 46 |
static $done = false; |
| 47 |
|
| 48 |
if ( empty( $image->pid ) ) { |
| 49 |
// WUT? |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if ( empty( static::$sizes[ $image->pid ] ) ) { |
| 54 |
static::$sizes[ $image->pid ] = []; |
| 55 |
} |
| 56 |
|
| 57 |
static::$sizes[ $image->pid ][] = $size; |
| 58 |
|
| 59 |
if ( empty( static::$images[ $image->pid ] ) ) { |
| 60 |
static::$images[ $image->pid ] = $image; |
| 61 |
} |
| 62 |
|
| 63 |
if ( $done ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$done = true; |
| 68 |
|
| 69 |
add_action( 'shutdown', [ $this, 'optimize' ], 555 ); // Must come before 666 (see Imagify_Abstract_Background_Process->init()). |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Launch the optimizations. |
| 74 |
* |
| 75 |
* @since 1.9 |
| 76 |
* @access public |
| 77 |
* @author Grégory Viguier |
| 78 |
*/ |
| 79 |
public function optimize() { |
| 80 |
if ( empty( static::$sizes ) ) { |
| 81 |
// ¯\(°_o)/¯ |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
foreach ( static::$sizes as $image_id => $sizes ) { |
| 86 |
if ( empty( static::$images[ $image_id ] ) ) { |
| 87 |
// ¯\(°_o)/¯ |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
$sizes = array_filter( $sizes ); |
| 92 |
|
| 93 |
if ( empty( $sizes ) ) { |
| 94 |
continue; |
| 95 |
} |
| 96 |
|
| 97 |
$process = imagify_get_optimization_process( static::$images[ $image_id ], 'ngg' ); |
| 98 |
|
| 99 |
if ( ! $process->is_valid() || ! $process->get_media()->is_supported() ) { |
| 100 |
continue; |
| 101 |
} |
| 102 |
|
| 103 |
$data = $process->get_data(); |
| 104 |
|
| 105 |
if ( ! $data->is_optimized() ) { |
| 106 |
// The main image is not optimized. |
| 107 |
continue; |
| 108 |
} |
| 109 |
|
| 110 |
$sizes = array_unique( $sizes ); |
| 111 |
|
| 112 |
foreach ( $sizes as $i => $size ) { |
| 113 |
$size_status = $data->get_size_data( $size, 'success' ); |
| 114 |
|
| 115 |
if ( $size_status ) { |
| 116 |
// This thumbnail has already been processed. |
| 117 |
unset( $sizes[ $i ] ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if ( empty( $sizes ) ) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
|
| 125 |
$optimization_level = $process->get_data()->get_optimization_level(); |
| 126 |
$args = [ |
| 127 |
'hook_suffix' => 'optimize_generated_image', |
| 128 |
]; |
| 129 |
|
| 130 |
$process->optimize_sizes( $sizes, $optimization_level, $args ); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|