| @@ -21,8 +21,25 @@ | ||
| 21 | 21 | */ |
| 22 | 22 | protected static $instance = null; |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | + * The image size WooCommerce is currently regenerating, captured via | |
| 26 | + * `intermediate_image_sizes` so the reupload can be scoped to just that size. | |
| 27 | + * | |
| 28 | + * @since 1.4.0 | |
| 29 | + * @var string|null | |
| 30 | + */ | |
| 31 | + protected $regenerating_size = null; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Whether the `intermediate_image_sizes` capture hook has been armed. | |
| 35 | + * | |
| 36 | + * @since 1.4.0 | |
| 37 | + * @var bool | |
| 38 | + */ | |
| 39 | + protected $size_capture_hooked = false; | |
| 40 | + | |
| 41 | + /** | |
| 25 | 42 | * The class constructor. |
| 26 | 43 | * |
| 27 | 44 | * @since 1.3.7 |
| 28 | 45 | */ |
| @@ -39,45 +56,56 @@ | ||
| 39 | 56 | /******** FIX Size updation in WooCommerce Customizer *****/ |
| 40 | 57 | // Return File path for WooCommerce image resizing to prevent url being used and causing issues with image regeneration. |
| 41 | 58 | add_filter( 'woocommerce_resize_images', [ $this, 'resize_images' ] ); |
| 42 | 59 | |
| 43 | - // resize_and_return_image is a WooCommerce function that resizes an image and returns its URL. | |
| 44 | - // it's used in the `wpmcs_get_attached_file` filter. | |
| 45 | - // If it is called from WooCommerce's `resize_and_return_image` function, it will return the original file path without modification. | |
| 46 | - // Also restore file from cloud if the file is missing locally, to prevent woocommerce skipping regeneration of missing images. | |
| 60 | + // Restore the file from cloud (if missing) and return the local path during | |
| 61 | + // WooCommerce's on-the-fly image regeneration. | |
| 47 | 62 | add_filter( 'wpmcs_get_attached_file', array( $this, 'get_attached_file' ), 10, 4 ); |
| 48 | - // Note: There has still been an issue removing the images from local need a look in | |
| 49 | - // future to see if we can prevent that or handle it better, but this should at least prevent the issue of images | |
| 50 | - // not being regenerated due to missing local files when using cloud storage. | |
| 63 | + | |
| 64 | + // Force a reupload of just the size WooCommerce is regenerating — same name/ | |
| 65 | + // dimensions as an existing size, but different crop, so the normal | |
| 66 | + // "unchanged, skip reupload" check can't tell it apart from a real no-op. | |
| 67 | + add_filter( 'wpmcs_do_reupload_media', array( $this, 'force_reupload_during_regeneration' ), 10, 3 ); | |
| 51 | 68 | /******* End Fix Size updation in WooCommerce Customizer */ |
| 52 | - | |
| 53 | - | |
| 54 | 69 | } |
| 55 | 70 | |
| 56 | 71 | |
| 57 | 72 | /** |
| 58 | 73 | * Resize images for WooCommerce. |
| 59 | - * Used to add a filter to return the file path instead of the URL for WooCommerce image resizing, | |
| 74 | + * Used to add a filter to return the file path instead of the URL for WooCommerce image resizing, | |
| 60 | 75 | * to prevent issues with image regeneration when using cloud storage. |
| 61 | - * | |
| 76 | + * | |
| 77 | + * WooCommerce fires 'woocommerce_resize_images' unconditionally from | |
| 78 | + * maybe_resize_image(), which itself runs on every wp_get_attachment_image_src() | |
| 79 | + * call for any size — not just when an actual WooCommerce resize is about to | |
| 80 | + * happen. resize_and_return_image() (the method that genuinely needs the local | |
| 81 | + * path) hasn't been called yet at this point, so is_called_from() would always | |
| 82 | + * be false if checked here. The added filter must instead check at the time it's | |
| 83 | + * actually invoked — otherwise it permanently returns the local path for every | |
| 84 | + * wpmcs_get_attached_file call for the rest of the request, once any image has | |
| 85 | + * rendered anywhere (confirmed live: this broke the unrelated BuddyBoss | |
| 86 | + * regenerate-thumbnails restore window entirely). | |
| 87 | + * | |
| 62 | 88 | * @param bool $resize Whether to resize images or not. |
| 63 | 89 | * @return bool The modified value of $resize. |
| 64 | 90 | * @since 1.3.7 |
| 65 | - * | |
| 66 | - * @return bool The modified value of $resize. | |
| 67 | 91 | */ |
| 68 | 92 | public function resize_images( $resize ) { |
| 69 | 93 | add_filter( 'wpmcs_get_attached_file', function ($url, $file, $attachment_id, $item) { |
| 94 | + if ( ! Utils::is_called_from( 'WC_Regenerate_Images', 'resize_and_return_image' ) ) { | |
| 95 | + return $url; | |
| 96 | + } | |
| 70 | 97 | return $file; |
| 71 | 98 | }, 10, 4 ); |
| 72 | - | |
| 99 | + | |
| 73 | 100 | return $resize; |
| 74 | 101 | } |
| 75 | 102 | |
| 76 | 103 | /** |
| 77 | - * Get attached file URL. | |
| 78 | - * This method is hooked to the 'wpmcs_get_attached_file' filter and is used to modify the URL of an attached file. | |
| 79 | - * | |
| 104 | + * Restore the file from cloud (all registered sizes, not just this one — the | |
| 105 | + * source may be needed at other sizes too) and return the local path during | |
| 106 | + * WooCommerce's on-the-fly image regeneration. | |
| 107 | + * | |
| 80 | 108 | * @param string $url The original file URL. |
| 81 | 109 | * @param string $file The file path. |
| 82 | 110 | * @param int $attachment_id The attachment ID. |
| 83 | 111 | * @param array $item The item data associated with the attachment. |
| @@ -82,23 +110,73 @@ | ||
| 82 | 110 | * @param int $attachment_id The attachment ID. |
| 83 | 111 | * @param array $item The item data associated with the attachment. |
| 84 | 112 | * @return string The modified file URL. |
| 85 | 113 | * @since 1.3.7 |
| 114 | + */ | |
| 115 | + public function get_attached_file( $url, $file, $attachment_id, $item ) { | |
| 116 | + if ( ! Utils::is_called_from( 'WC_Regenerate_Images', 'resize_and_return_image' ) ) { | |
| 117 | + return $url; | |
| 118 | + } | |
| 119 | + | |
| 120 | + // Always attempted, not gated on $file specifically being missing — | |
| 121 | + // move_to_server_by_key_and_path() already no-ops per-file on file_exists(), | |
| 122 | + // and gating here would leave OTHER missing sizes unrestored, which then get | |
| 123 | + // silently dropped (not just skipped) if a reupload is later forced without | |
| 124 | + // a local file present. | |
| 125 | + $results = Item::instance()->moveToServerByItem( $item, 'full', true ); | |
| 126 | + | |
| 127 | + if ( is_array( $results ) && ! empty( $results ) ) { | |
| 128 | + Item::instance()->track_restored_for_cleanup( array_values( $results ) ); | |
| 129 | + } | |
| 130 | + | |
| 131 | + $this->capture_regenerating_size(); | |
| 132 | + | |
| 133 | + return $file; | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * Arms a one-time `intermediate_image_sizes` capture so the exact size | |
| 138 | + * WooCommerce is regenerating is known when `wpmcs_do_reupload_media` fires. | |
| 139 | + * Priority 20 — after WooCommerce's own `adjust_intermediate_image_sizes` | |
| 140 | + * (default priority 10) has already narrowed the array to that one size. | |
| 86 | 141 | * |
| 87 | - * @return string The modified file URL. | |
| 142 | + * @return void | |
| 143 | + * @since 1.4.0 | |
| 88 | 144 | */ |
| 89 | - public function get_attached_file( $url, $file, $attachment_id, $item ) { | |
| 90 | - // Check if the function 'resize_and_return_image' exists and if the current call is from that function. | |
| 91 | - if ( Utils::is_called_from( 'WC_Regenerate_Images', 'resize_and_return_image' ) ) { | |
| 92 | - // If the call is from 'resize_and_return_image', return the original file path without modification. | |
| 93 | - if ( ! file_exists( $file ) ) { | |
| 94 | - // If the file does not exist locally, attempt to restore it from the cloud. | |
| 95 | - Item::instance()->moveToServerByItem( $item, 'full', true ); | |
| 145 | + protected function capture_regenerating_size() { | |
| 146 | + if ( $this->size_capture_hooked ) { | |
| 147 | + return; | |
| 148 | + } | |
| 149 | + $this->size_capture_hooked = true; | |
| 150 | + | |
| 151 | + add_filter( 'intermediate_image_sizes', function ( $sizes ) { | |
| 152 | + if ( is_array( $sizes ) && 1 === count( $sizes ) ) { | |
| 153 | + $this->regenerating_size = reset( $sizes ); | |
| 96 | 154 | } |
| 155 | + return $sizes; | |
| 156 | + }, 20 ); | |
| 157 | + } | |
| 97 | 158 | |
| 98 | - return $file; | |
| 159 | + /** | |
| 160 | + * Force a reupload of just the size WooCommerce is regenerating — an existing | |
| 161 | + * registered size with a different crop, which the normal same-name/dimensions | |
| 162 | + * check can't otherwise tell apart from an unchanged file. | |
| 163 | + * | |
| 164 | + * @param bool|array $do_reupload | |
| 165 | + * @param int $attachment_id | |
| 166 | + * @param string $source_type | |
| 167 | + * @return bool|array | |
| 168 | + * @since 1.4.0 | |
| 169 | + */ | |
| 170 | + public function force_reupload_during_regeneration( $do_reupload, $attachment_id, $source_type ) { | |
| 171 | + if ( true === $do_reupload || empty( $this->regenerating_size ) ) { | |
| 172 | + return $do_reupload; | |
| 99 | 173 | } |
| 100 | - return $url; | |
| 174 | + | |
| 175 | + $size = $this->regenerating_size; | |
| 176 | + $this->regenerating_size = null; | |
| 177 | + | |
| 178 | + return array( $size ); | |
| 101 | 179 | } |
| 102 | 180 | |
| 103 | 181 | /** |
| 104 | 182 | * Is installed? |