class-global-stats.php 16.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
<?php

namespace Smush\Core\Stats;

use Smush\Core\Array_Utils;
use Smush\Core\Attachment_Id_List;
use Smush\Core\Media\Media_Item;
use Smush\Core\Media\Media_Item_Cache;
use Smush\Core\Media\Media_Item_Optimization_Global_Stats;
use Smush\Core\Media\Media_Item_Optimizer;
use Smush\Core\Media\Media_Item_Query;
use Smush\Core\Media\Media_Item_Stats;
use Smush\Core\Modules\Background\Mutex;

class Global_Stats {
	const GLOBAL_STATS_OPTION_ID = 'wp_smush_global_stats';
	const OPTIMIZE_LIST_OPTION_ID = 'wp-smush-optimize-list';
	const REOPTIMIZE_LIST_OPTION_ID = 'wp-smush-reoptimize-list';
	const ERROR_LIST_OPTION_ID = 'wp-smush-error-items-list';
	const IGNORE_LIST_OPTION_ID = 'wp-smush-ignored-items-list';
	const ANIMATED_LIST_OPTION_ID = 'wp-smush-animated-items-list';
	/**
	 * @var Global_Stats
	 */
	private static $instance;

	/**
	 * @var Media_Item_Optimization_Global_Stats[]
	 */
	private $optimization_stats;

	/**
	 * @var Attachment_Id_List
	 */
	private $optimize_list;
	/**
	 * @var Attachment_Id_List
	 */
	private $reoptimize_list;
	/**
	 * @var Attachment_Id_List
	 */
	private $error_list;
	/**
	 * @var Attachment_Id_List
	 */
	private $ignore_list;
	/**
	 * @var Attachment_Id_List
	 */
	private $animated_list;
	/**
	 * @var Media_Item_Cache
	 */
	private $media_item_cache;
	/**
	 * @var Array_Utils
	 */
	private $array_utils;
	private $media_item_query;

	public function __construct() {
		$this->optimize_list   = new Attachment_Id_List( self::OPTIMIZE_LIST_OPTION_ID );
		$this->reoptimize_list = new Attachment_Id_List( self::REOPTIMIZE_LIST_OPTION_ID );
		$this->error_list      = new Attachment_Id_List( self::ERROR_LIST_OPTION_ID );
		$this->ignore_list     = new Attachment_Id_List( self::IGNORE_LIST_OPTION_ID );
		$this->animated_list   = new Attachment_Id_List( self::ANIMATED_LIST_OPTION_ID );

		$this->media_item_cache = Media_Item_Cache::get_instance();
		$this->array_utils      = new Array_Utils();
		$this->media_item_query = new Media_Item_Query();
	}

	public static function get() {
		if ( empty( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * @return Media_Item_Optimization_Global_Stats_Persistable[]
	 */
	public function get_persistable_stats_for_optimizations() {
		if ( is_null( $this->optimization_stats ) ) {
			$this->optimization_stats = $this->initialize_stats_for_optimizations();
		}

		return $this->optimization_stats;
	}

	private function initialize_stats_for_optimizations() {
		return apply_filters( 'wp_smush_global_optimization_stats', array() );
	}

	/**
	 * @param $optimization_key
	 *
	 * @return Media_Item_Optimization_Global_Stats
	 */
	public function create_global_stats_object( $optimization_key ) {
		return apply_filters(
			'wp_smush_optimization_global_stats_instance',
			new Media_Item_Optimization_Global_Stats(),
			$optimization_key
		);
	}

	/**
	 * @param $optimization_key
	 *
	 * @return Media_Item_Optimization_Global_Stats_Persistable
	 */
	public function get_persistable_stats_for_optimization( $optimization_key ) {
		return $this->get_array_value(
			$this->get_persistable_stats_for_optimizations(),
			$optimization_key
		);
	}

	private function get_array_value( $array, $key ) {
		return $array && isset( $array[ $key ] )
			? $array[ $key ]
			: null;
	}

	public function delete_global_stats_option() {
		delete_option( self::GLOBAL_STATS_OPTION_ID );
	}

	private function get_global_stats_option_value( $key ) {
		$option = $this->get_global_stats_option();

		return $this->get_array_value( $option, $key );
	}

	private function update_global_stats_option_value( $key, $value ) {
		$option = $this->get_global_stats_option();

		update_option( self::GLOBAL_STATS_OPTION_ID, array_merge( $option, array(
			$key => $value,
		) ), false );
	}

	public function is_outdated() {
		if ( $this->is_media_library_empty() ) {
			return false;
		}

		$stats_updated_timestamp = $this->get_stats_updated_timestamp();
		if ( empty( $stats_updated_timestamp ) ) {
			// The scan has never been run
			return true;
		}

		$rescan_required_timestamp = $this->get_rescan_required_timestamp();

		return $rescan_required_timestamp > $stats_updated_timestamp;
	}

	private function is_media_library_empty() {
		if ( 0 !== $this->get_image_attachment_count() ) {
			// Cached attachment count is not empty, so we definitely have some media items.
			// No need to make a DB call.
			return false;
		}

		return 0 === $this->media_item_query->get_image_attachment_count();
	}

	public function mark_as_outdated() {
		$this->update_rescan_required_timestamp( time() );
	}

	public function get_stats_update_started_timestamp() {
		return (int) $this->get_global_stats_option_value( 'stats_update_started_timestamp' );
	}

	public function update_stats_update_started_timestamp( $timestamp ) {
		$this->update_global_stats_option_value( 'stats_update_started_timestamp', $timestamp );
	}

	public function get_stats_updated_timestamp() {
		return (int) $this->get_global_stats_option_value( 'stats_updated_timestamp' );
	}

	public function update_stats_updated_timestamp( $timestamp ) {
		$this->update_global_stats_option_value( 'stats_updated_timestamp', $timestamp );
	}

	public function get_rescan_required_timestamp() {
		return (int) $this->get_global_stats_option_value( 'rescan_required_timestamp' );
	}

	public function update_rescan_required_timestamp( $timestamp ) {
		$this->update_global_stats_option_value( 'rescan_required_timestamp', $timestamp );
	}

	public function get_image_attachment_count() {
		return (int) $this->get_global_stats_option_value( 'image_attachment_count' );
	}

	public function add_image_attachment_count( $image_attachment_count ) {
		$this->mutex( function () use ( $image_attachment_count ) {
			$old_image_attachment_count = $this->get_image_attachment_count();
			$this->update_global_stats_option_value( 'image_attachment_count', $old_image_attachment_count + $image_attachment_count );
		} );
	}

	public function subtract_image_attachment_count( $image_attachment_count ) {
		$this->mutex( function () use ( $image_attachment_count ) {
			$old_image_attachment_count = $this->get_image_attachment_count();
			$this->update_global_stats_option_value( 'image_attachment_count', max( $old_image_attachment_count - $image_attachment_count, 0 ) );
		} );
	}

	public function get_optimized_images_count() {
		return (int) $this->get_global_stats_option_value( 'optimized_images_count' );
	}

	public function add_optimized_images_count( $optimized_images_count ) {
		$this->mutex( function () use ( $optimized_images_count ) {
			$old_count = $this->get_optimized_images_count();
			$this->update_global_stats_option_value( 'optimized_images_count', $old_count + $optimized_images_count );
		} );
	}

	public function subtract_optimized_images_count( $optimized_images_count ) {
		$this->mutex( function () use ( $optimized_images_count ) {
			$old_count = $this->get_optimized_images_count();
			$this->update_global_stats_option_value( 'optimized_images_count', max( $old_count - $optimized_images_count, 0 ) );
		} );
	}

	public function get_sum_of_optimization_global_stats() {
		$stats = new Media_Item_Stats();

		foreach ( $this->get_persistable_stats_for_optimizations() as $optimization ) {
			$stats->add( $optimization->get_stats() );
		}

		return $stats;
	}

	private function mutex( $operation ) {
		$option_id = self::GLOBAL_STATS_OPTION_ID;
		( new Mutex( "{$option_id}_mutex" ) )->execute( $operation );
	}

	/**
	 * @return Attachment_Id_List
	 */
	public function get_optimize_list() {
		return $this->optimize_list;
	}

	public function get_redo_ids() {
		return array_merge(
			$this->get_reoptimize_list()->get_ids(),
			$this->get_error_list()->get_ids()
		);
	}

	public function get_redo_count() {
		return $this->get_reoptimize_list()->get_count()
		       + $this->get_error_list()->get_count();
	}

	/**
	 * @return Attachment_Id_List
	 */
	public function get_reoptimize_list() {
		return $this->reoptimize_list;
	}

	/**
	 * @return Attachment_Id_List
	 */
	public function get_error_list() {
		return $this->error_list;
	}

	/**
	 * @return Attachment_Id_List
	 */
	public function get_ignore_list() {
		return $this->ignore_list;
	}

	public function get_animated_list() {
		return $this->animated_list;
	}

	public function to_array() {
		$array = array(
			'is_outdated'            => $this->is_outdated(),
			'image_attachment_count' => $this->get_image_attachment_count(),
			'optimized_images_count' => $this->get_optimized_images_count(),
		);

		foreach ( $this->get_persistable_stats_for_optimizations() as $optimization_key => $optimization_stats ) {
			$array[ $optimization_key ] = $optimization_stats->get_stats()->to_array();
		}

		$array['optimize_list']    = $this->optimize_list->get_ids();
		$array['optimize_count']   = $this->optimize_list->get_count();
		$array['reoptimize_list']  = $this->reoptimize_list->get_ids();
		$array['reoptimize_count'] = $this->reoptimize_list->get_count();
		$array['error_list']       = $this->error_list->get_ids();
		$array['error_count']      = $this->error_list->get_count();
		$array['ignore_list']      = $this->ignore_list->get_ids();
		$array['ignore_count']     = $this->ignore_list->get_count();
		$array['animated_list']    = $this->animated_list->get_ids();
		$array['animated_count']   = $this->animated_list->get_count();

		$total_stats              = $this->get_sum_of_optimization_global_stats();
		$array['size_before']     = $total_stats->get_size_before();
		$array['size_after']      = $total_stats->get_size_after();
		$array['savings_percent'] = $total_stats->get_percent();

		$array['remaining_count'] = $this->get_remaining_count();

		$array['percent_optimized'] = $this->get_percent_optimized();
		$array['percent_metric']    = $this->get_percent_metric();
		$array['grade_class']       = $this->get_grade_class();

		$array['total_optimizable_items_count'] = $this->get_total_optimizable_items_count();
		$array['skipped_ids']                   = $this->get_skipped_ids();
		$array['skipped_count']                 = $this->get_skipped_count();

		return $array;
	}

	/**
	 * @return int
	 */
	public function get_remaining_count() {
		return $this->optimize_list->get_count()
		       + $this->reoptimize_list->get_count()
		       + $this->error_list->get_count();
	}

	/**
	 * @return array
	 */
	private function get_global_stats_option() {
		// Cached values are problematic in parallel
		wp_cache_delete( self::GLOBAL_STATS_OPTION_ID, 'options' );
		$option = get_option( self::GLOBAL_STATS_OPTION_ID, array() );

		return empty( $option ) || ! is_array( $option )
			? array()
			: $option;
	}

	public function reset() {
		$this->get_reoptimize_list()->delete_ids();
		$this->get_optimize_list()->delete_ids();
		$this->get_error_list()->delete_ids();
		$this->get_ignore_list()->delete_ids();
		$this->get_animated_list()->delete_ids();

		$this->delete_global_stats_option();
		foreach ( $this->get_persistable_stats_for_optimizations() as $persistable_stats_for_optimization ) {
			$persistable_stats_for_optimization->reset();
		}
	}


	/**
	 * Total number of items that could be optimized, this includes items that have already been optimized.
	 *
	 * For a count that only contains items yet to be optimized/reoptimized {@see self::get_remaining_count()}
	 *
	 * @return int
	 */
	public function get_total_optimizable_items_count() {
		return $this->get_image_attachment_count() - $this->get_skipped_count();
	}

	public function get_skipped_count() {
		return count( $this->get_skipped_ids() );
	}

	public function get_skipped_ids() {
		$skipped_ids = array_merge(
			$this->get_ignore_list()->get_ids(),
			$this->get_animated_list()->get_ids()
		);

		return $this->array_utils->fast_array_unique( $skipped_ids );
	}

	public function get_percent_optimized() {
		$total_optimizable_count = $this->get_total_optimizable_items_count();
		$remaining_count         = $this->get_remaining_count();
		if (
			$total_optimizable_count === 0 ||
			$total_optimizable_count <= $remaining_count
		) {
			return 0;
		}
		$percent_optimized = floor( ( $total_optimizable_count - $remaining_count ) * 100 / $total_optimizable_count );
		if ( $percent_optimized > 100 ) {
			$percent_optimized = 100;
		} elseif ( $percent_optimized < 0 ) {
			$percent_optimized = 0;
		}

		return $percent_optimized;
	}

	public function get_percent_metric() {
		$percent_optimized = $this->get_percent_optimized();

		return 0.0 === (float) $percent_optimized ? 100 : $percent_optimized;
	}

	public function get_grade_class() {
		$total_optimizable_items_count = $this->get_total_optimizable_items_count();
		if ( 0 === $total_optimizable_items_count ) {
			$grade = 'sui-grade-dismissed';
		} else {
			$percent_optimized = $this->get_percent_optimized();

			$grade = 'sui-grade-f';
			if ( $percent_optimized >= 60 && $percent_optimized < 90 ) {
				$grade = 'sui-grade-c';
			} elseif ( $percent_optimized >= 90 ) {
				$grade = 'sui-grade-a';
			}
		}

		return $grade;
	}

	/**
	 * @param $media_item Media_Item
	 *
	 * @return void
	 */
	public function remove_media_item( $media_item ) {
		$attachment_id = $media_item->get_id();

		// Remove from all the lists
		$this->remove_from_all_lists( $attachment_id );

		// Remove stats
		$this->subtract_item_stats( $media_item );
	}

	public function adjust_for_attachment( $attachment_id ) {
		$media_item = $this->media_item_cache->get( $attachment_id );
		$this->adjust_for_media_item( $media_item );
	}

	/**
	 * When the status of a media item changes this method can make the necessary changes to the global stats
	 *
	 * @param $media_item Media_Item
	 *
	 * @return void
	 */
	public function adjust_for_media_item( $media_item ) {
		$this->adjust_lists_for_media_item( $media_item );

		$belongs_in_stats = ! $media_item->is_skipped() && ! $media_item->has_errors();
		if ( $belongs_in_stats ) {
			$this->add_item_stats( $media_item );
		} else {
			$this->subtract_item_stats( $media_item );
		}
	}

	/**
	 * @param $media_item Media_Item
	 *
	 * @return void
	 */
	private function add_item_stats( $media_item ) {
		$optimizer = new Media_Item_Optimizer( $media_item );
		foreach ( $this->get_persistable_stats_for_optimizations() as $optimization_key => $optimization_global_stats ) {
			$optimization = $optimizer->get_optimization( $optimization_key );
			if ( $optimization && $optimization->is_optimized() ) {
				$optimization_global_stats->add_item_stats( $media_item->get_id(), $optimization->get_stats() );
			}
		}
	}

	/**
	 * @param $media_item Media_Item
	 *
	 * @return void
	 */
	public function subtract_item_stats( $media_item ) {
		$optimizer = new Media_Item_Optimizer( $media_item );
		foreach ( $this->get_persistable_stats_for_optimizations() as $optimization_key => $optimization_global_stats ) {
			$optimization = $optimizer->get_optimization( $optimization_key );
			if ( $optimization && $optimization->is_optimized() ) {
				$optimization_global_stats->subtract_item_stats( $media_item->get_id(), $optimization->get_stats() );
			}
		}
	}

	/**
	 * @param $attachment_id
	 *
	 * @return void
	 */
	private function remove_from_all_lists( $attachment_id ) {
		$this->get_optimize_list()->remove_id( $attachment_id );
		$this->get_reoptimize_list()->remove_id( $attachment_id );
		$this->get_error_list()->remove_id( $attachment_id );
		$this->get_ignore_list()->remove_id( $attachment_id );
		$this->get_animated_list()->remove_id( $attachment_id );
	}

	/**
	 * Also:
	 * @see Global_Stats_Controller::accumulate_attachment_ids()
	 */
	public function adjust_lists_for_media_item( $media_item ) {
		$attachment_id = $media_item->get_id();
		$optimizer     = new Media_Item_Optimizer( $media_item );

		// First remove from all the lists.
		$this->remove_from_all_lists( $attachment_id );

		// Now add only to the lists where it belongs.
		if ( $media_item->is_ignored() ) {
			$this->get_ignore_list()->add_id( $attachment_id );
		} elseif ( $media_item->is_animated() ) {
			$this->get_animated_list()->add_id( $attachment_id );
		} elseif ( $media_item->has_errors() ) {
			$this->get_error_list()->add_id( $attachment_id );
		} else {
			if ( $optimizer->is_optimized() ) {
				if ( $optimizer->should_reoptimize() ) {
					$this->get_reoptimize_list()->add_id( $attachment_id );
				}
			} else {
				if ( $optimizer->should_optimize() ) {
					$this->get_optimize_list()->add_id( $attachment_id );
				}
			}
		}
	}
}