| 1 |
<?php |
| 2 |
namespace Imagify\ThirdParty\NGG\Optimization\Data; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Optimization data class for the custom folders. |
| 8 |
* This class constructor accepts: |
| 9 |
* - A NGG image ID (int). |
| 10 |
* - A \nggImage object. |
| 11 |
* - A \nggdb object. |
| 12 |
* - An anonym object containing a pid property (and everything else). |
| 13 |
* - A \Imagify\Media\MediaInterface object. |
| 14 |
* |
| 15 |
* @since 1.9 |
| 16 |
* @see Imagify\ThirdParty\NGG\Media\NGG |
| 17 |
* @author Grégory Viguier |
| 18 |
*/ |
| 19 |
class NGG extends \Imagify\Optimization\Data\AbstractData { |
| 20 |
use \Imagify\Traits\MediaRowTrait; |
| 21 |
|
| 22 |
/** |
| 23 |
* The attachment SQL DB class. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
* @since 1.9 |
| 27 |
* @access protected |
| 28 |
* @author Grégory Viguier |
| 29 |
*/ |
| 30 |
protected $db_class_name = '\\Imagify\\ThirdParty\\NGG\\DB'; |
| 31 |
|
| 32 |
/** |
| 33 |
* The constructor. |
| 34 |
* |
| 35 |
* @since 1.9 |
| 36 |
* @access public |
| 37 |
* @author Grégory Viguier |
| 38 |
* |
| 39 |
* @param mixed $id An ID, or whatever type the "Media" class constructor accepts. |
| 40 |
*/ |
| 41 |
public function __construct( $id ) { |
| 42 |
parent::__construct( $id ); |
| 43 |
|
| 44 |
if ( ! $this->is_valid() ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
// This is required by MediaRowTrait. |
| 49 |
$this->id = $this->get_media()->get_id(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get the whole media optimization data. |
| 54 |
* |
| 55 |
* @since 1.9 |
| 56 |
* @access public |
| 57 |
* @author Grégory Viguier |
| 58 |
* |
| 59 |
* @return array The data. See parent method for details. |
| 60 |
*/ |
| 61 |
public function get_optimization_data() { |
| 62 |
if ( ! $this->is_valid() ) { |
| 63 |
return $this->default_optimization_data; |
| 64 |
} |
| 65 |
|
| 66 |
$row = array_merge( $this->get_row_db_instance()->get_column_defaults(), $this->get_row() ); |
| 67 |
$data = $this->default_optimization_data; |
| 68 |
|
| 69 |
$data['status'] = $row['status']; |
| 70 |
$data['level'] = $row['optimization_level']; |
| 71 |
$data['level'] = is_numeric( $data['level'] ) ? (int) $data['level'] : false; |
| 72 |
$data['stats'] = [ |
| 73 |
'original_size' => 0, |
| 74 |
'optimized_size' => 0, |
| 75 |
'percent' => 0, |
| 76 |
]; |
| 77 |
|
| 78 |
if ( ! empty( $row['data']['sizes'] ) && is_array( $row['data']['sizes'] ) ) { |
| 79 |
$data['sizes'] = $row['data']['sizes']; |
| 80 |
$data['sizes'] = array_filter( $data['sizes'], 'is_array' ); |
| 81 |
} |
| 82 |
|
| 83 |
if ( empty( $data['sizes']['full'] ) ) { |
| 84 |
if ( 'success' === $row['status'] ) { |
| 85 |
$data['sizes']['full'] = [ |
| 86 |
'success' => true, |
| 87 |
'original_size' => 0, |
| 88 |
'optimized_size' => 0, |
| 89 |
'percent' => 0, |
| 90 |
]; |
| 91 |
} elseif ( ! empty( $row['status'] ) ) { |
| 92 |
$data['sizes']['full'] = [ |
| 93 |
'success' => false, |
| 94 |
'error' => '', |
| 95 |
]; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if ( empty( $data['sizes'] ) ) { |
| 100 |
return $data; |
| 101 |
} |
| 102 |
|
| 103 |
foreach ( $data['sizes'] as $size_data ) { |
| 104 |
// Cast. |
| 105 |
if ( isset( $size_data['original_size'] ) ) { |
| 106 |
$size_data['original_size'] = (int) $size_data['original_size']; |
| 107 |
} |
| 108 |
if ( isset( $size_data['optimized_size'] ) ) { |
| 109 |
$size_data['optimized_size'] = (int) $size_data['optimized_size']; |
| 110 |
} |
| 111 |
if ( isset( $size_data['percent'] ) ) { |
| 112 |
$size_data['percent'] = round( $size_data['percent'], 2 ); |
| 113 |
} |
| 114 |
// Stats. |
| 115 |
if ( ! empty( $size_data['original_size'] ) && ! empty( $size_data['optimized_size'] ) ) { |
| 116 |
$data['stats']['original_size'] += $size_data['original_size']; |
| 117 |
$data['stats']['optimized_size'] += $size_data['optimized_size']; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if ( $data['stats']['original_size'] && $data['stats']['optimized_size'] ) { |
| 122 |
$data['stats']['percent'] = $data['stats']['original_size'] - $data['stats']['optimized_size']; |
| 123 |
$data['stats']['percent'] = round( $data['stats']['percent'] / $data['stats']['original_size'] * 100, 2 ); |
| 124 |
} |
| 125 |
|
| 126 |
return $data; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Update the optimization data, level, and status for a size. |
| 131 |
* |
| 132 |
* @since 1.9 |
| 133 |
* @access public |
| 134 |
* @author Grégory Viguier |
| 135 |
* |
| 136 |
* @param string $size The size name. |
| 137 |
* @param array $data The optimization data. See parent method for details. |
| 138 |
*/ |
| 139 |
public function update_size_optimization_data( $size, array $data ) { |
| 140 |
if ( ! $this->is_valid() ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
$old_data = array_merge( $this->get_reset_data(), $this->get_row() ); |
| 145 |
|
| 146 |
$old_data['data']['sizes'] = ! empty( $old_data['data']['sizes'] ) && is_array( $old_data['data']['sizes'] ) ? $old_data['data']['sizes'] : []; |
| 147 |
|
| 148 |
if ( 'full' === $size ) { |
| 149 |
/** |
| 150 |
* Original file. |
| 151 |
*/ |
| 152 |
$old_data['optimization_level'] = $data['level']; |
| 153 |
$old_data['status'] = $data['status']; |
| 154 |
} |
| 155 |
|
| 156 |
if ( ! $data['success'] ) { |
| 157 |
/** |
| 158 |
* Error. |
| 159 |
*/ |
| 160 |
$old_data['data']['sizes'][ $size ] = [ |
| 161 |
'success' => false, |
| 162 |
'error' => $data['error'], |
| 163 |
]; |
| 164 |
} else { |
| 165 |
/** |
| 166 |
* Success. |
| 167 |
*/ |
| 168 |
$old_data['data']['sizes'][ $size ] = [ |
| 169 |
'success' => true, |
| 170 |
'original_size' => $data['original_size'], |
| 171 |
'optimized_size' => $data['optimized_size'], |
| 172 |
'percent' => round( ( $data['original_size'] - $data['optimized_size'] ) / $data['original_size'] * 100, 2 ), |
| 173 |
]; |
| 174 |
} |
| 175 |
|
| 176 |
$this->update_row( $old_data ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Delete the media optimization data, level, and status. |
| 181 |
* |
| 182 |
* @since 1.9 |
| 183 |
* @access public |
| 184 |
* @author Grégory Viguier |
| 185 |
*/ |
| 186 |
public function delete_optimization_data() { |
| 187 |
if ( ! $this->is_valid() ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
$this->delete_row(); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Delete the optimization data for the given sizes. |
| 196 |
* If all sizes are removed, all optimization data is deleted. |
| 197 |
* Status and level are not modified nor removed if the "full" size is removed. This leaves the media in a Schrödinger state. |
| 198 |
* |
| 199 |
* @since 1.9.8 |
| 200 |
* @access public |
| 201 |
* @author Grégory Viguier |
| 202 |
* |
| 203 |
* @param array $sizes A list of sizes to remove. |
| 204 |
*/ |
| 205 |
public function delete_sizes_optimization_data( array $sizes ) { |
| 206 |
if ( ! $sizes || ! $this->is_valid() ) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
$data = array_merge( $this->get_reset_data(), $this->get_row() ); |
| 211 |
|
| 212 |
$data['data']['sizes'] = ! empty( $data['data']['sizes'] ) && is_array( $data['data']['sizes'] ) ? $data['data']['sizes'] : []; |
| 213 |
|
| 214 |
if ( ! $data['data']['sizes'] ) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
$remaining_sizes_data = array_diff_key( $data['data']['sizes'], array_flip( $sizes ) ); |
| 219 |
|
| 220 |
if ( ! $remaining_sizes_data ) { |
| 221 |
// All sizes have been removed: delete everything. |
| 222 |
$this->delete_optimization_data(); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
if ( count( $remaining_sizes_data ) === count( $data['data']['sizes'] ) ) { |
| 227 |
// Nothing has been removed. |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
$data['data']['sizes'] = $remaining_sizes_data; |
| 232 |
|
| 233 |
$this->update_row( $data ); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Get default values used to reset optimization data. |
| 238 |
* |
| 239 |
* @since 1.9 |
| 240 |
* @access protected |
| 241 |
* @author Grégory Viguier |
| 242 |
* |
| 243 |
* @return array { |
| 244 |
* The default values related to the optimization. |
| 245 |
* |
| 246 |
* @type string $optimization_level The optimization level. |
| 247 |
* @type string $status The status: success, already_optimized, error. |
| 248 |
* @type array $data Data related to the thumbnails. |
| 249 |
* } |
| 250 |
*/ |
| 251 |
protected function get_reset_data() { |
| 252 |
$db_instance = $this->get_row_db_instance(); |
| 253 |
$primary_key = $db_instance->get_primary_key(); |
| 254 |
$column_defaults = $db_instance->get_column_defaults(); |
| 255 |
|
| 256 |
return array_diff_key( $column_defaults, [ |
| 257 |
'data_id' => 0, |
| 258 |
$primary_key => 0, |
| 259 |
] ); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Update the row. |
| 264 |
* |
| 265 |
* @since 1.9 |
| 266 |
* @access public |
| 267 |
* @author Grégory Viguier |
| 268 |
* |
| 269 |
* @param array $data The data to update. |
| 270 |
*/ |
| 271 |
public function update_row( $data ) { |
| 272 |
if ( ! $this->db_class_name || $this->id <= 0 ) { |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
$primary_key = $this->get_row_db_instance()->get_primary_key(); |
| 277 |
// This is needed in case the row doesn't exist yet. |
| 278 |
$data[ $primary_key ] = $this->id; |
| 279 |
|
| 280 |
$this->get_row_db_instance()->update( $this->id, $data ); |
| 281 |
|
| 282 |
$this->reset_row_cache(); |
| 283 |
} |
| 284 |
} |
| 285 |
|