Changeset 3775310
- Timestamp:
- 10/30/2026 01:09:40 PM (4 weeks ago)
- Location:
- empirical-responsive-images
- Files:
-
- 2 added
- 16 edited
- 1 copied
-
tags/0.3.12 (copied) (copied from empirical-responsive-images/trunk )
-
tags/0.3.12/LICENSE (added)
-
tags/0.3.12/changelog.txt (modified) (1 diff)
-
tags/0.3.12/empirical-responsive-images.php (modified) (2 diffs)
-
tags/0.3.12/includes/class-background-optimizer.php (modified) (4 diffs)
-
tags/0.3.12/includes/class-observation-store.php (modified) (1 diff)
-
tags/0.3.12/includes/class-plugin.php (modified) (1 diff)
-
tags/0.3.12/includes/class-renderer.php (modified) (2 diffs)
-
tags/0.3.12/includes/class-visitor-gap-store.php (modified) (14 diffs)
-
tags/0.3.12/readme.txt (modified) (2 diffs)
-
trunk/LICENSE (added)
-
trunk/changelog.txt (modified) (1 diff)
-
trunk/empirical-responsive-images.php (modified) (2 diffs)
-
trunk/includes/class-background-optimizer.php (modified) (4 diffs)
-
trunk/includes/class-observation-store.php (modified) (1 diff)
-
trunk/includes/class-plugin.php (modified) (1 diff)
-
trunk/includes/class-renderer.php (modified) (2 diffs)
-
trunk/includes/class-visitor-gap-store.php (modified) (14 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
empirical-responsive-images/tags/0.3.12/changelog.txt
r3735260 r3775310 1 = 0.3.12 - 2026-10-30 = 2 * Fixed - WebP and AVIF picture sources no longer use http on HTTPS pages when the uploads base URL is stored with http. Browsers blocked those sources as mixed content, which left images missing in places such as WooCommerce product galleries. Sources are upgraded to https when the page or the original image is secure, and are never downgraded. 3 * Fixed - Visitor reports no longer queue jobs that change nothing. A report is dropped when the image files exist and its layout is already stored, or could not be stored, so it no longer spends a daily work unit or purges pages. 4 5 = 0.3.11 - 2026-10-19 = 6 * Fixed - Background image work no longer clears every cached page. Once sizes are generated, only the published parent post and featured-image posts of each image are purged (up to 100 pages per run, filterable with empirical_responsive_images_background_purge_urls); other pages pick up the new sizes when their cache expires. Long queues previously cleared the whole site, CDN included, every night as the daily limit reset at midnight UTC. Visitor reports still carry no page URLs. 7 1 8 = 0.3.10 - 2026-10-08 = 2 9 * Fixed - Galleries, sliders and variation scripts that change the src or srcset of a wrapped image no longer keep showing the original photo. A small inline footer script parks the picture's WebP/AVIF source srcsets while another image is shown and restores them when the original returns. Lazy-loading placeholders, foreign pictures and unmarked images are left untouched. -
empirical-responsive-images/tags/0.3.12/empirical-responsive-images.php
r3735260 r3775310 4 4 * Plugin URI: https://github.com/gsusI/empirical-responsive-images 5 5 * Description: Helps WordPress serve better-sized images with responsive thumbnails and WebP/AVIF versions. 6 * Version: 0.3.1 06 * Version: 0.3.12 7 7 * Requires at least: 6.9 8 8 * Requires PHP: 7.4 … … 21 21 } 22 22 23 define( 'EMPIRICAL_RESPONSIVE_IMAGES_VERSION', '0.3.1 0' );23 define( 'EMPIRICAL_RESPONSIVE_IMAGES_VERSION', '0.3.12' ); 24 24 define( 'EMPIRICAL_RESPONSIVE_IMAGES_FILE', __FILE__ ); 25 25 define( 'EMPIRICAL_RESPONSIVE_IMAGES_DIR', plugin_dir_path( __FILE__ ) ); -
empirical-responsive-images/tags/0.3.12/includes/class-background-optimizer.php
r3635536 r3775310 419 419 $lease, 420 420 ! empty( $result['success'] ), 421 sanitize_key( (string) ( $result['error'] ?? '' ) ) 422 ); 421 sanitize_key( (string) ( $result['error'] ?? '' ) ), 422 ! empty( $result['success'] ) ? $this->target_page_urls( $target ) : array() 423 ); 424 } 425 426 /** 427 * Find the published pages known to show an attachment target. 428 * 429 * Covers the post the image was uploaded to and posts using it as their 430 * featured image. Archives and other listings pick up new widths when their 431 * cache expires. Visitor reports never carry page URLs. 432 * 433 * @param array $target Resolved target. 434 * @return string[] 435 */ 436 private function target_page_urls( array $target ): array { 437 $attachment_id = 'attachment' === (string) ( $target['type'] ?? '' ) ? absint( $target['attachment_id'] ?? 0 ) : 0; 438 $urls = array(); 439 440 try { 441 $urls = $attachment_id > 0 ? $this->attachment_page_urls( $attachment_id ) : array(); 442 } catch ( \Throwable $error ) { 443 $urls = array(); 444 } 445 446 /** 447 * Filter the pages purged after background work generates sizes for a target. 448 * 449 * @param string[] $urls Page URLs. 450 * @param array $target Resolved target. 451 */ 452 $urls = apply_filters( 'empirical_responsive_images_background_purge_urls', $urls, $target ); 453 454 return is_array( $urls ) ? array_values( array_filter( $urls, 'is_string' ) ) : array(); 455 } 456 457 /** 458 * Published permalinks of an attachment's parent post and featured-image users. 459 * 460 * @param int $attachment_id Attachment ID. 461 * @return string[] 462 */ 463 private function attachment_page_urls( int $attachment_id ): array { 464 $urls = array(); 465 $post_ids = get_posts( 466 array( 467 'post_type' => 'any', 468 'post_status' => 'publish', 469 'meta_key' => '_thumbnail_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Bounded, indexed by meta_key. 470 'meta_value' => (string) $attachment_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Bounded, indexed by meta_key. 471 'fields' => 'ids', 472 'posts_per_page' => 10, 473 'no_found_rows' => true, 474 ) 475 ); 476 477 array_unshift( $post_ids, wp_get_post_parent_id( $attachment_id ) ); 478 479 foreach ( array_unique( array_map( 'absint', $post_ids ) ) as $post_id ) { 480 $post_type = $post_id > 0 ? get_post_type( $post_id ) : false; 481 482 if ( false === $post_type || 'publish' !== get_post_status( $post_id ) || ! is_post_type_viewable( $post_type ) ) { 483 continue; 484 } 485 486 $url = get_permalink( $post_id ); 487 488 if ( is_string( $url ) && '' !== $url ) { 489 $urls[] = $url; 490 } 491 } 492 493 return $urls; 423 494 } 424 495 … … 720 791 * Purge stale page markup only after the job store declares a completed run. 721 792 * 793 * Only the pages known to show the finished targets are purged. A full purge 794 * here cleared whole sites, CDN included, each night the daily-capped queue 795 * resumed. Other pages pick up the new widths when their cache expires. 796 * 722 797 * @return void 723 798 */ … … 730 805 731 806 try { 732 Cache_Compat::purge_all(); 807 foreach ( $this->store->get_cache_purge_urls() as $url ) { 808 Cache_Compat::purge_url( $url ); 809 } 810 733 811 $this->store->mark_cache_purged( $generation ); 734 812 } catch ( \Throwable $error ) { … … 744 822 * @param bool $success Whether processing succeeded. 745 823 * @param string $error Stable error key. 824 * @param array $purge_urls Pages to purge once the run's work is done. 746 825 * @return void 747 826 */ 748 private function complete_safely( string $job_id, string $lease_token, bool $success, string $error ): void {827 private function complete_safely( string $job_id, string $lease_token, bool $success, string $error, array $purge_urls = array() ): void { 749 828 for ( $attempt = 0; $attempt < 120; ++$attempt ) { 750 829 try { 751 $result = $this->store->complete_job( $job_id, $lease_token, $success, $error );830 $result = $this->store->complete_job( $job_id, $lease_token, $success, $error, $purge_urls ); 752 831 753 832 if ( ! empty( $result['updated'] ) || ! empty( $result['stale'] ) ) { -
empirical-responsive-images/tags/0.3.12/includes/class-observation-store.php
r3629091 r3775310 338 338 ? $this->sanitize_slot_width_map( $entry['slot_widths'] ) 339 339 : array(); 340 } 341 342 /** 343 * Check whether an attachment layout report would leave stored slots unchanged. 344 * 345 * @param int $attachment_id Attachment ID. 346 * @param int $viewport_width Coarse viewport bucket. 347 * @param int $slot_width Coarse rendered slot bucket. 348 * @return bool 349 */ 350 public function attachment_layout_is_recorded( int $attachment_id, int $viewport_width, int $slot_width ): bool { 351 return $this->slot_map_covers( $this->get_attachment_slot_widths( $attachment_id ), $viewport_width, $slot_width ); 352 } 353 354 /** 355 * Check whether an asset layout report would leave stored data unchanged. 356 * 357 * @param string $url Manageable local asset URL. 358 * @param int $width Target width. 359 * @param int $viewport_width Coarse viewport bucket. 360 * @param int $slot_width Coarse rendered slot bucket. 361 * @return bool 362 */ 363 public function asset_layout_is_recorded( string $url, int $width, int $viewport_width, int $slot_width ): bool { 364 $key = null === $this->asset_manager ? '' : $this->asset_manager->key_for_url( $url ); 365 366 if ( '' === $key ) { 367 return false; 368 } 369 370 $data = $this->get_data(); 371 $entry = isset( $data['assets'][ $key ] ) && is_array( $data['assets'][ $key ] ) ? $data['assets'][ $key ] : array(); 372 373 if ( empty( $entry ) ) { 374 return false; 375 } 376 377 $slot_widths = is_array( $entry['slot_widths'] ?? null ) ? $this->sanitize_slot_width_map( $entry['slot_widths'] ) : array(); 378 379 if ( ! $this->slot_map_covers( $slot_widths, $viewport_width, $slot_width ) ) { 380 return false; 381 } 382 383 $source_width = absint( $entry['natural_width'] ?? 0 ); 384 $automatic_widths = is_array( $entry['automatic_target_widths'] ?? null ) ? $entry['automatic_target_widths'] : array(); 385 386 return isset( $entry['target_widths'][ 'w' . $width ] ) 387 || ( $source_width > 0 && $width >= $source_width ) 388 || count( $automatic_widths ) >= self::MAX_AUTOMATIC_ASSET_WIDTHS; 389 } 390 391 /** 392 * Check whether storing one layout would leave a slot map unchanged. 393 * 394 * Full maps keep their smallest viewports, so a larger viewport is never stored. 395 * 396 * @param array $slot_widths Sanitized slot widths keyed by viewport. 397 * @param int $viewport_width Coarse viewport bucket. 398 * @param int $slot_width Coarse rendered slot bucket. 399 * @return bool 400 */ 401 private function slot_map_covers( array $slot_widths, int $viewport_width, int $slot_width ): bool { 402 if ( absint( $slot_widths[ (string) $viewport_width ] ?? 0 ) >= max( 1, $slot_width ) ) { 403 return true; 404 } 405 406 return count( $slot_widths ) >= self::MAX_SLOT_WIDTHS_PER_TARGET && $viewport_width > max( array_keys( $slot_widths ) ); 340 407 } 341 408 -
empirical-responsive-images/tags/0.3.12/includes/class-plugin.php
r3735260 r3775310 132 132 $this->observation_store = new Observation_Store( $this->asset_manager ); 133 133 $this->format_generator = new Format_Generator( $this->settings ); 134 $this->visitor_gap_store = new Visitor_Gap_Store( $this->asset_manager, $this->format_generator );134 $this->visitor_gap_store = new Visitor_Gap_Store( $this->asset_manager, $this->format_generator, $this->observation_store ); 135 135 $this->image_sizes = new Image_Sizes( $this->settings, $this->observation_store, $this->format_generator ); 136 136 $this->regenerator = new Regenerator( $this->format_generator, $this->observation_store, $this->asset_manager, $this->settings ); -
empirical-responsive-images/tags/0.3.12/includes/class-renderer.php
r3735260 r3775310 972 972 973 973 if ( Format_Generator::is_plugin_sidecar_path( $mapped_relative ) && Visitor_Gap_Target::is_current_file( $source_path, $mapped_path ) ) { 974 return $this->upload_url_for_relative_path( $mapped_relative );974 return $this->upload_url_for_relative_path( $mapped_relative, $url ); 975 975 } 976 976 } … … 1066 1066 * 1067 1067 * @param string $relative_path Upload-relative path. 1068 * @return string 1069 */ 1070 private function upload_url_for_relative_path( string $relative_path ): string { 1068 * @param string $source_url URL of the image this file stands in for. 1069 * @return string 1070 */ 1071 private function upload_url_for_relative_path( string $relative_path, string $source_url ): string { 1071 1072 $uploads = wp_get_upload_dir(); 1072 1073 return esc_url_raw( trailingslashit( $uploads['baseurl'] ) . ltrim( $relative_path, '/' ) ); 1073 $url = trailingslashit( $uploads['baseurl'] ) . ltrim( $relative_path, '/' ); 1074 1075 // WordPress upgrades attachment URLs on secure pages but not the uploads base URL. 1076 if ( is_ssl() || 'https' === wp_parse_url( $source_url, PHP_URL_SCHEME ) ) { 1077 $url = set_url_scheme( $url, 'https' ); 1078 } 1079 1080 return esc_url_raw( $url ); 1074 1081 } 1075 1082 } -
empirical-responsive-images/tags/0.3.12/includes/class-visitor-gap-store.php
r3706969 r3775310 42 42 private const MAX_ATTEMPTS = 3; 43 43 private const CACHE_PURGE_INTERVAL = 900; 44 private const MAX_PENDING_PURGE_URLS = 100; 44 45 private const TRUSTED_CONFIRMATIONS_REQUIRED = 1; 45 46 … … 59 60 60 61 /** 62 * Observation store holding learned layouts. 63 * 64 * @var Observation_Store|null 65 */ 66 private $observation_store; 67 68 /** 61 69 * Token owned by current write lock. 62 70 * … … 68 76 * Constructor. 69 77 * 70 * @param Asset_Manager|null $asset_manager Local asset manager. 71 * @param Format_Generator|null $format_generator Modern format generator. 72 */ 73 public function __construct( ?Asset_Manager $asset_manager = null, ?Format_Generator $format_generator = null ) { 74 $this->asset_manager = $asset_manager; 75 $this->format_generator = $format_generator; 78 * @param Asset_Manager|null $asset_manager Local asset manager. 79 * @param Format_Generator|null $format_generator Modern format generator. 80 * @param Observation_Store|null $observation_store Observation store. 81 */ 82 public function __construct( ?Asset_Manager $asset_manager = null, ?Format_Generator $format_generator = null, ?Observation_Store $observation_store = null ) { 83 $this->asset_manager = $asset_manager; 84 $this->format_generator = $format_generator; 85 $this->observation_store = $observation_store; 76 86 } 77 87 … … 240 250 'generation' => 0, 241 251 'last_purge_at' => 0, 252 'urls' => array(), 242 253 ), 243 254 ); … … 271 282 $data['daily'] = wp_parse_args( $data['daily'], $this->empty_data()['daily'] ); 272 283 $data['cache'] = wp_parse_args( $data['cache'], $this->empty_data()['cache'] ); 284 $data['cache']['urls'] = self::merge_page_urls( $data['cache']['urls'], array(), self::MAX_PENDING_PURGE_URLS ); 273 285 $data['targets'] = $this->trim_completed_targets( $data['targets'] ); 274 286 $data['queue_sequence'] = absint( $data['queue_sequence'] ?? 0 ); … … 645 657 * @param bool $success Whether target generation succeeded. 646 658 * @param string $error Bounded error description. 659 * @param array $purge_urls Pages showing the target, purged with the next debounced cache purge. 647 660 * @return array 648 661 */ 649 public function complete_job( string $job_id, string $lease_token, bool $success, string $error = '' ): array {662 public function complete_job( string $job_id, string $lease_token, bool $success, string $error = '', array $purge_urls = array() ): array { 650 663 if ( ! $this->acquire_write_lock() ) { 651 664 return array( … … 694 707 $data['cache']['dirty'] = true; 695 708 $data['cache']['generation'] = min( PHP_INT_MAX, absint( $data['cache']['generation'] ?? 0 ) + 1 ); 709 $data['cache']['urls'] = self::merge_page_urls( $data['cache']['urls'], $purge_urls, self::MAX_PENDING_PURGE_URLS ); 696 710 $data['targets'] = $this->trim_completed_targets( $data['targets'] ); 697 711 unset( $data['jobs'][ $job_id ] ); … … 890 904 891 905 /** 906 * Get pages whose cached markup predates completed work. 907 * 908 * @return string[] 909 */ 910 public function get_cache_purge_urls(): array { 911 return $this->get_data()['cache']['urls']; 912 } 913 914 /** 892 915 * Mark one completed, debounced cache purge if its generation is current. 893 916 * … … 909 932 $data['cache']['dirty'] = false; 910 933 $data['cache']['last_purge_at'] = time(); 934 $data['cache']['urls'] = array(); 911 935 $data['updated_at'] = time(); 912 936 … … 976 1000 : ( $is_critical ? self::critical_width( $reported_width, $maximum ) : self::canonical_width( $reported_width, $maximum ) ); 977 1001 1002 if ( $requested_width < $minimum ) { 1003 return array(); 1004 } 1005 978 1006 $work_width = $native_width > 0 ? $native_width : $requested_width; 979 1007 $job_id = $this->job_id( $target_token, $work_width ); … … 1023 1051 ) 1024 1052 : Visitor_Gap_Target::is_optimization_covered( $target, $reported_width, $this->get_asset_manager(), $required_formats ); 1025 1026 if ( $requested_width < $minimum || ( $already_optimized && ! $has_layout_hint ) ) { 1053 $files_ready = $already_optimized 1054 || ( 1055 0 === $native_width 1056 && Visitor_Gap_Target::has_exact_width( $target, $work_width, $this->get_asset_manager() ) 1057 && Visitor_Gap_Target::is_optimization_covered( $target, $work_width, $this->get_asset_manager(), $required_formats ) 1058 ); 1059 1060 // A job that would change nothing still spends daily work and purges pages. 1061 if ( $files_ready && ( ! $has_layout_hint || $this->layout_is_recorded( $target, $work_width, $viewport_width, $slot_width ) ) ) { 1027 1062 return array(); 1028 1063 } … … 1045 1080 'trusted_confirmation' => $trusted_confirmation, 1046 1081 ); 1082 } 1083 1084 /** 1085 * Check whether a layout report would leave learned data unchanged. 1086 * 1087 * @param array $target Resolved target. 1088 * @param int $width Work width. 1089 * @param int $viewport_width Coarse viewport bucket. 1090 * @param int $slot_width Coarse rendered slot bucket. 1091 * @return bool 1092 */ 1093 private function layout_is_recorded( array $target, int $width, int $viewport_width, int $slot_width ): bool { 1094 if ( 'attachment' === (string) ( $target['type'] ?? '' ) ) { 1095 return $this->get_observation_store()->attachment_layout_is_recorded( absint( $target['attachment_id'] ?? 0 ), $viewport_width, $slot_width ); 1096 } 1097 1098 return $this->get_observation_store()->asset_layout_is_recorded( (string) ( $target['url'] ?? '' ), $width, $viewport_width, $slot_width ); 1047 1099 } 1048 1100 … … 1503 1555 1504 1556 /** 1557 * Merge page URLs into a bounded, de-duplicated list. 1558 * 1559 * @param mixed $urls Stored URLs. 1560 * @param mixed $added URLs to append. 1561 * @param int $limit Maximum kept URLs; earlier entries win. 1562 * @return string[] 1563 */ 1564 private static function merge_page_urls( $urls, $added, int $limit ): array { 1565 $merged = array(); 1566 1567 foreach ( array_merge( is_array( $urls ) ? $urls : array(), is_array( $added ) ? $added : array() ) as $url ) { 1568 $url = is_string( $url ) && strlen( $url ) <= 2048 ? esc_url_raw( $url, array( 'http', 'https' ) ) : ''; 1569 1570 if ( '' !== $url ) { 1571 $merged[ $url ] = true; 1572 } 1573 } 1574 1575 return array_slice( array_keys( $merged ), 0, $limit ); 1576 } 1577 1578 /** 1505 1579 * Keep a small recent failure history without consuming active queue space. 1506 1580 * … … 1635 1709 1636 1710 /** 1711 * Get request-local observation store. 1712 * 1713 * @return Observation_Store 1714 */ 1715 private function get_observation_store(): Observation_Store { 1716 if ( null === $this->observation_store ) { 1717 $this->observation_store = new Observation_Store( $this->get_asset_manager() ); 1718 } 1719 1720 return $this->observation_store; 1721 } 1722 1723 /** 1637 1724 * Acquire short-lived fixed-key write lock. 1638 1725 * -
empirical-responsive-images/tags/0.3.12/readme.txt
r3735260 r3775310 5 5 Tested up to: 7.1 6 6 Requires PHP: 7.4 7 Stable tag: 0.3.1 07 Stable tag: 0.3.12 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 151 151 == Changelog == 152 152 153 = 0.3.12 = 154 * Fix WebP and AVIF images not showing on HTTPS sites whose uploads URL is stored with http. Picture sources now load over HTTPS whenever the page or the original image does, which stops browsers blocking them as mixed content. WooCommerce product galleries were one place this showed up. 155 * Stop repeat visitor reports from using up daily image work and refreshing pages when the image files and layout are already in place. 156 157 = 0.3.11 = 158 * Fix a full-site cache clear after background image work. Once new image sizes are ready, only the published post each image belongs to and posts using it as their featured image are refreshed; other pages pick up the new sizes when their cache expires. Sites with a long queue were cleared every night as the daily limit reset at midnight UTC. 159 153 160 = 0.3.10 = 154 161 * Fix galleries, sliders and product variation swaps that kept showing the original photo when the plugin had wrapped the image in a picture element. The WebP/AVIF sources now step aside while a script shows another image and return when the original comes back. -
empirical-responsive-images/trunk/changelog.txt
r3735260 r3775310 1 = 0.3.12 - 2026-10-30 = 2 * Fixed - WebP and AVIF picture sources no longer use http on HTTPS pages when the uploads base URL is stored with http. Browsers blocked those sources as mixed content, which left images missing in places such as WooCommerce product galleries. Sources are upgraded to https when the page or the original image is secure, and are never downgraded. 3 * Fixed - Visitor reports no longer queue jobs that change nothing. A report is dropped when the image files exist and its layout is already stored, or could not be stored, so it no longer spends a daily work unit or purges pages. 4 5 = 0.3.11 - 2026-10-19 = 6 * Fixed - Background image work no longer clears every cached page. Once sizes are generated, only the published parent post and featured-image posts of each image are purged (up to 100 pages per run, filterable with empirical_responsive_images_background_purge_urls); other pages pick up the new sizes when their cache expires. Long queues previously cleared the whole site, CDN included, every night as the daily limit reset at midnight UTC. Visitor reports still carry no page URLs. 7 1 8 = 0.3.10 - 2026-10-08 = 2 9 * Fixed - Galleries, sliders and variation scripts that change the src or srcset of a wrapped image no longer keep showing the original photo. A small inline footer script parks the picture's WebP/AVIF source srcsets while another image is shown and restores them when the original returns. Lazy-loading placeholders, foreign pictures and unmarked images are left untouched. -
empirical-responsive-images/trunk/empirical-responsive-images.php
r3735260 r3775310 4 4 * Plugin URI: https://github.com/gsusI/empirical-responsive-images 5 5 * Description: Helps WordPress serve better-sized images with responsive thumbnails and WebP/AVIF versions. 6 * Version: 0.3.1 06 * Version: 0.3.12 7 7 * Requires at least: 6.9 8 8 * Requires PHP: 7.4 … … 21 21 } 22 22 23 define( 'EMPIRICAL_RESPONSIVE_IMAGES_VERSION', '0.3.1 0' );23 define( 'EMPIRICAL_RESPONSIVE_IMAGES_VERSION', '0.3.12' ); 24 24 define( 'EMPIRICAL_RESPONSIVE_IMAGES_FILE', __FILE__ ); 25 25 define( 'EMPIRICAL_RESPONSIVE_IMAGES_DIR', plugin_dir_path( __FILE__ ) ); -
empirical-responsive-images/trunk/includes/class-background-optimizer.php
r3635536 r3775310 419 419 $lease, 420 420 ! empty( $result['success'] ), 421 sanitize_key( (string) ( $result['error'] ?? '' ) ) 422 ); 421 sanitize_key( (string) ( $result['error'] ?? '' ) ), 422 ! empty( $result['success'] ) ? $this->target_page_urls( $target ) : array() 423 ); 424 } 425 426 /** 427 * Find the published pages known to show an attachment target. 428 * 429 * Covers the post the image was uploaded to and posts using it as their 430 * featured image. Archives and other listings pick up new widths when their 431 * cache expires. Visitor reports never carry page URLs. 432 * 433 * @param array $target Resolved target. 434 * @return string[] 435 */ 436 private function target_page_urls( array $target ): array { 437 $attachment_id = 'attachment' === (string) ( $target['type'] ?? '' ) ? absint( $target['attachment_id'] ?? 0 ) : 0; 438 $urls = array(); 439 440 try { 441 $urls = $attachment_id > 0 ? $this->attachment_page_urls( $attachment_id ) : array(); 442 } catch ( \Throwable $error ) { 443 $urls = array(); 444 } 445 446 /** 447 * Filter the pages purged after background work generates sizes for a target. 448 * 449 * @param string[] $urls Page URLs. 450 * @param array $target Resolved target. 451 */ 452 $urls = apply_filters( 'empirical_responsive_images_background_purge_urls', $urls, $target ); 453 454 return is_array( $urls ) ? array_values( array_filter( $urls, 'is_string' ) ) : array(); 455 } 456 457 /** 458 * Published permalinks of an attachment's parent post and featured-image users. 459 * 460 * @param int $attachment_id Attachment ID. 461 * @return string[] 462 */ 463 private function attachment_page_urls( int $attachment_id ): array { 464 $urls = array(); 465 $post_ids = get_posts( 466 array( 467 'post_type' => 'any', 468 'post_status' => 'publish', 469 'meta_key' => '_thumbnail_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Bounded, indexed by meta_key. 470 'meta_value' => (string) $attachment_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Bounded, indexed by meta_key. 471 'fields' => 'ids', 472 'posts_per_page' => 10, 473 'no_found_rows' => true, 474 ) 475 ); 476 477 array_unshift( $post_ids, wp_get_post_parent_id( $attachment_id ) ); 478 479 foreach ( array_unique( array_map( 'absint', $post_ids ) ) as $post_id ) { 480 $post_type = $post_id > 0 ? get_post_type( $post_id ) : false; 481 482 if ( false === $post_type || 'publish' !== get_post_status( $post_id ) || ! is_post_type_viewable( $post_type ) ) { 483 continue; 484 } 485 486 $url = get_permalink( $post_id ); 487 488 if ( is_string( $url ) && '' !== $url ) { 489 $urls[] = $url; 490 } 491 } 492 493 return $urls; 423 494 } 424 495 … … 720 791 * Purge stale page markup only after the job store declares a completed run. 721 792 * 793 * Only the pages known to show the finished targets are purged. A full purge 794 * here cleared whole sites, CDN included, each night the daily-capped queue 795 * resumed. Other pages pick up the new widths when their cache expires. 796 * 722 797 * @return void 723 798 */ … … 730 805 731 806 try { 732 Cache_Compat::purge_all(); 807 foreach ( $this->store->get_cache_purge_urls() as $url ) { 808 Cache_Compat::purge_url( $url ); 809 } 810 733 811 $this->store->mark_cache_purged( $generation ); 734 812 } catch ( \Throwable $error ) { … … 744 822 * @param bool $success Whether processing succeeded. 745 823 * @param string $error Stable error key. 824 * @param array $purge_urls Pages to purge once the run's work is done. 746 825 * @return void 747 826 */ 748 private function complete_safely( string $job_id, string $lease_token, bool $success, string $error ): void {827 private function complete_safely( string $job_id, string $lease_token, bool $success, string $error, array $purge_urls = array() ): void { 749 828 for ( $attempt = 0; $attempt < 120; ++$attempt ) { 750 829 try { 751 $result = $this->store->complete_job( $job_id, $lease_token, $success, $error );830 $result = $this->store->complete_job( $job_id, $lease_token, $success, $error, $purge_urls ); 752 831 753 832 if ( ! empty( $result['updated'] ) || ! empty( $result['stale'] ) ) { -
empirical-responsive-images/trunk/includes/class-observation-store.php
r3629091 r3775310 338 338 ? $this->sanitize_slot_width_map( $entry['slot_widths'] ) 339 339 : array(); 340 } 341 342 /** 343 * Check whether an attachment layout report would leave stored slots unchanged. 344 * 345 * @param int $attachment_id Attachment ID. 346 * @param int $viewport_width Coarse viewport bucket. 347 * @param int $slot_width Coarse rendered slot bucket. 348 * @return bool 349 */ 350 public function attachment_layout_is_recorded( int $attachment_id, int $viewport_width, int $slot_width ): bool { 351 return $this->slot_map_covers( $this->get_attachment_slot_widths( $attachment_id ), $viewport_width, $slot_width ); 352 } 353 354 /** 355 * Check whether an asset layout report would leave stored data unchanged. 356 * 357 * @param string $url Manageable local asset URL. 358 * @param int $width Target width. 359 * @param int $viewport_width Coarse viewport bucket. 360 * @param int $slot_width Coarse rendered slot bucket. 361 * @return bool 362 */ 363 public function asset_layout_is_recorded( string $url, int $width, int $viewport_width, int $slot_width ): bool { 364 $key = null === $this->asset_manager ? '' : $this->asset_manager->key_for_url( $url ); 365 366 if ( '' === $key ) { 367 return false; 368 } 369 370 $data = $this->get_data(); 371 $entry = isset( $data['assets'][ $key ] ) && is_array( $data['assets'][ $key ] ) ? $data['assets'][ $key ] : array(); 372 373 if ( empty( $entry ) ) { 374 return false; 375 } 376 377 $slot_widths = is_array( $entry['slot_widths'] ?? null ) ? $this->sanitize_slot_width_map( $entry['slot_widths'] ) : array(); 378 379 if ( ! $this->slot_map_covers( $slot_widths, $viewport_width, $slot_width ) ) { 380 return false; 381 } 382 383 $source_width = absint( $entry['natural_width'] ?? 0 ); 384 $automatic_widths = is_array( $entry['automatic_target_widths'] ?? null ) ? $entry['automatic_target_widths'] : array(); 385 386 return isset( $entry['target_widths'][ 'w' . $width ] ) 387 || ( $source_width > 0 && $width >= $source_width ) 388 || count( $automatic_widths ) >= self::MAX_AUTOMATIC_ASSET_WIDTHS; 389 } 390 391 /** 392 * Check whether storing one layout would leave a slot map unchanged. 393 * 394 * Full maps keep their smallest viewports, so a larger viewport is never stored. 395 * 396 * @param array $slot_widths Sanitized slot widths keyed by viewport. 397 * @param int $viewport_width Coarse viewport bucket. 398 * @param int $slot_width Coarse rendered slot bucket. 399 * @return bool 400 */ 401 private function slot_map_covers( array $slot_widths, int $viewport_width, int $slot_width ): bool { 402 if ( absint( $slot_widths[ (string) $viewport_width ] ?? 0 ) >= max( 1, $slot_width ) ) { 403 return true; 404 } 405 406 return count( $slot_widths ) >= self::MAX_SLOT_WIDTHS_PER_TARGET && $viewport_width > max( array_keys( $slot_widths ) ); 340 407 } 341 408 -
empirical-responsive-images/trunk/includes/class-plugin.php
r3735260 r3775310 132 132 $this->observation_store = new Observation_Store( $this->asset_manager ); 133 133 $this->format_generator = new Format_Generator( $this->settings ); 134 $this->visitor_gap_store = new Visitor_Gap_Store( $this->asset_manager, $this->format_generator );134 $this->visitor_gap_store = new Visitor_Gap_Store( $this->asset_manager, $this->format_generator, $this->observation_store ); 135 135 $this->image_sizes = new Image_Sizes( $this->settings, $this->observation_store, $this->format_generator ); 136 136 $this->regenerator = new Regenerator( $this->format_generator, $this->observation_store, $this->asset_manager, $this->settings ); -
empirical-responsive-images/trunk/includes/class-renderer.php
r3735260 r3775310 972 972 973 973 if ( Format_Generator::is_plugin_sidecar_path( $mapped_relative ) && Visitor_Gap_Target::is_current_file( $source_path, $mapped_path ) ) { 974 return $this->upload_url_for_relative_path( $mapped_relative );974 return $this->upload_url_for_relative_path( $mapped_relative, $url ); 975 975 } 976 976 } … … 1066 1066 * 1067 1067 * @param string $relative_path Upload-relative path. 1068 * @return string 1069 */ 1070 private function upload_url_for_relative_path( string $relative_path ): string { 1068 * @param string $source_url URL of the image this file stands in for. 1069 * @return string 1070 */ 1071 private function upload_url_for_relative_path( string $relative_path, string $source_url ): string { 1071 1072 $uploads = wp_get_upload_dir(); 1072 1073 return esc_url_raw( trailingslashit( $uploads['baseurl'] ) . ltrim( $relative_path, '/' ) ); 1073 $url = trailingslashit( $uploads['baseurl'] ) . ltrim( $relative_path, '/' ); 1074 1075 // WordPress upgrades attachment URLs on secure pages but not the uploads base URL. 1076 if ( is_ssl() || 'https' === wp_parse_url( $source_url, PHP_URL_SCHEME ) ) { 1077 $url = set_url_scheme( $url, 'https' ); 1078 } 1079 1080 return esc_url_raw( $url ); 1074 1081 } 1075 1082 } -
empirical-responsive-images/trunk/includes/class-visitor-gap-store.php
r3706969 r3775310 42 42 private const MAX_ATTEMPTS = 3; 43 43 private const CACHE_PURGE_INTERVAL = 900; 44 private const MAX_PENDING_PURGE_URLS = 100; 44 45 private const TRUSTED_CONFIRMATIONS_REQUIRED = 1; 45 46 … … 59 60 60 61 /** 62 * Observation store holding learned layouts. 63 * 64 * @var Observation_Store|null 65 */ 66 private $observation_store; 67 68 /** 61 69 * Token owned by current write lock. 62 70 * … … 68 76 * Constructor. 69 77 * 70 * @param Asset_Manager|null $asset_manager Local asset manager. 71 * @param Format_Generator|null $format_generator Modern format generator. 72 */ 73 public function __construct( ?Asset_Manager $asset_manager = null, ?Format_Generator $format_generator = null ) { 74 $this->asset_manager = $asset_manager; 75 $this->format_generator = $format_generator; 78 * @param Asset_Manager|null $asset_manager Local asset manager. 79 * @param Format_Generator|null $format_generator Modern format generator. 80 * @param Observation_Store|null $observation_store Observation store. 81 */ 82 public function __construct( ?Asset_Manager $asset_manager = null, ?Format_Generator $format_generator = null, ?Observation_Store $observation_store = null ) { 83 $this->asset_manager = $asset_manager; 84 $this->format_generator = $format_generator; 85 $this->observation_store = $observation_store; 76 86 } 77 87 … … 240 250 'generation' => 0, 241 251 'last_purge_at' => 0, 252 'urls' => array(), 242 253 ), 243 254 ); … … 271 282 $data['daily'] = wp_parse_args( $data['daily'], $this->empty_data()['daily'] ); 272 283 $data['cache'] = wp_parse_args( $data['cache'], $this->empty_data()['cache'] ); 284 $data['cache']['urls'] = self::merge_page_urls( $data['cache']['urls'], array(), self::MAX_PENDING_PURGE_URLS ); 273 285 $data['targets'] = $this->trim_completed_targets( $data['targets'] ); 274 286 $data['queue_sequence'] = absint( $data['queue_sequence'] ?? 0 ); … … 645 657 * @param bool $success Whether target generation succeeded. 646 658 * @param string $error Bounded error description. 659 * @param array $purge_urls Pages showing the target, purged with the next debounced cache purge. 647 660 * @return array 648 661 */ 649 public function complete_job( string $job_id, string $lease_token, bool $success, string $error = '' ): array {662 public function complete_job( string $job_id, string $lease_token, bool $success, string $error = '', array $purge_urls = array() ): array { 650 663 if ( ! $this->acquire_write_lock() ) { 651 664 return array( … … 694 707 $data['cache']['dirty'] = true; 695 708 $data['cache']['generation'] = min( PHP_INT_MAX, absint( $data['cache']['generation'] ?? 0 ) + 1 ); 709 $data['cache']['urls'] = self::merge_page_urls( $data['cache']['urls'], $purge_urls, self::MAX_PENDING_PURGE_URLS ); 696 710 $data['targets'] = $this->trim_completed_targets( $data['targets'] ); 697 711 unset( $data['jobs'][ $job_id ] ); … … 890 904 891 905 /** 906 * Get pages whose cached markup predates completed work. 907 * 908 * @return string[] 909 */ 910 public function get_cache_purge_urls(): array { 911 return $this->get_data()['cache']['urls']; 912 } 913 914 /** 892 915 * Mark one completed, debounced cache purge if its generation is current. 893 916 * … … 909 932 $data['cache']['dirty'] = false; 910 933 $data['cache']['last_purge_at'] = time(); 934 $data['cache']['urls'] = array(); 911 935 $data['updated_at'] = time(); 912 936 … … 976 1000 : ( $is_critical ? self::critical_width( $reported_width, $maximum ) : self::canonical_width( $reported_width, $maximum ) ); 977 1001 1002 if ( $requested_width < $minimum ) { 1003 return array(); 1004 } 1005 978 1006 $work_width = $native_width > 0 ? $native_width : $requested_width; 979 1007 $job_id = $this->job_id( $target_token, $work_width ); … … 1023 1051 ) 1024 1052 : Visitor_Gap_Target::is_optimization_covered( $target, $reported_width, $this->get_asset_manager(), $required_formats ); 1025 1026 if ( $requested_width < $minimum || ( $already_optimized && ! $has_layout_hint ) ) { 1053 $files_ready = $already_optimized 1054 || ( 1055 0 === $native_width 1056 && Visitor_Gap_Target::has_exact_width( $target, $work_width, $this->get_asset_manager() ) 1057 && Visitor_Gap_Target::is_optimization_covered( $target, $work_width, $this->get_asset_manager(), $required_formats ) 1058 ); 1059 1060 // A job that would change nothing still spends daily work and purges pages. 1061 if ( $files_ready && ( ! $has_layout_hint || $this->layout_is_recorded( $target, $work_width, $viewport_width, $slot_width ) ) ) { 1027 1062 return array(); 1028 1063 } … … 1045 1080 'trusted_confirmation' => $trusted_confirmation, 1046 1081 ); 1082 } 1083 1084 /** 1085 * Check whether a layout report would leave learned data unchanged. 1086 * 1087 * @param array $target Resolved target. 1088 * @param int $width Work width. 1089 * @param int $viewport_width Coarse viewport bucket. 1090 * @param int $slot_width Coarse rendered slot bucket. 1091 * @return bool 1092 */ 1093 private function layout_is_recorded( array $target, int $width, int $viewport_width, int $slot_width ): bool { 1094 if ( 'attachment' === (string) ( $target['type'] ?? '' ) ) { 1095 return $this->get_observation_store()->attachment_layout_is_recorded( absint( $target['attachment_id'] ?? 0 ), $viewport_width, $slot_width ); 1096 } 1097 1098 return $this->get_observation_store()->asset_layout_is_recorded( (string) ( $target['url'] ?? '' ), $width, $viewport_width, $slot_width ); 1047 1099 } 1048 1100 … … 1503 1555 1504 1556 /** 1557 * Merge page URLs into a bounded, de-duplicated list. 1558 * 1559 * @param mixed $urls Stored URLs. 1560 * @param mixed $added URLs to append. 1561 * @param int $limit Maximum kept URLs; earlier entries win. 1562 * @return string[] 1563 */ 1564 private static function merge_page_urls( $urls, $added, int $limit ): array { 1565 $merged = array(); 1566 1567 foreach ( array_merge( is_array( $urls ) ? $urls : array(), is_array( $added ) ? $added : array() ) as $url ) { 1568 $url = is_string( $url ) && strlen( $url ) <= 2048 ? esc_url_raw( $url, array( 'http', 'https' ) ) : ''; 1569 1570 if ( '' !== $url ) { 1571 $merged[ $url ] = true; 1572 } 1573 } 1574 1575 return array_slice( array_keys( $merged ), 0, $limit ); 1576 } 1577 1578 /** 1505 1579 * Keep a small recent failure history without consuming active queue space. 1506 1580 * … … 1635 1709 1636 1710 /** 1711 * Get request-local observation store. 1712 * 1713 * @return Observation_Store 1714 */ 1715 private function get_observation_store(): Observation_Store { 1716 if ( null === $this->observation_store ) { 1717 $this->observation_store = new Observation_Store( $this->get_asset_manager() ); 1718 } 1719 1720 return $this->observation_store; 1721 } 1722 1723 /** 1637 1724 * Acquire short-lived fixed-key write lock. 1638 1725 * -
empirical-responsive-images/trunk/readme.txt
r3735260 r3775310 5 5 Tested up to: 7.1 6 6 Requires PHP: 7.4 7 Stable tag: 0.3.1 07 Stable tag: 0.3.12 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 151 151 == Changelog == 152 152 153 = 0.3.12 = 154 * Fix WebP and AVIF images not showing on HTTPS sites whose uploads URL is stored with http. Picture sources now load over HTTPS whenever the page or the original image does, which stops browsers blocking them as mixed content. WooCommerce product galleries were one place this showed up. 155 * Stop repeat visitor reports from using up daily image work and refreshing pages when the image files and layout are already in place. 156 157 = 0.3.11 = 158 * Fix a full-site cache clear after background image work. Once new image sizes are ready, only the published post each image belongs to and posts using it as their featured image are refreshed; other pages pick up the new sizes when their cache expires. Sites with a long queue were cleared every night as the daily limit reset at midnight UTC. 159 153 160 = 0.3.10 = 154 161 * Fix galleries, sliders and product variation swaps that kept showing the original photo when the plugin had wrapped the image in a picture element. The WebP/AVIF sources now step aside while a script shows another image and return when the original comes back.
Note:
See TracChangeset
for help on using the changeset viewer.