Skip to content

Commit 5f826ba

Browse files
committed
Replace srcset image URLs
Fixes product URLs not showing for https tunnels
1 parent d0fa55f commit 5f826ba

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Download and install just like any other WordPress plugin. If you want to be rea
4747
### 1.0.0 - 2018-11-06
4848
* Refactor - Use namespaces and rename classes
4949
* Tweak - Add support for ngrok
50+
* Fix - Ensure images load for products when using an https tunnel
5051

5152
### 0.8.1 - 2017.12.13
5253
* Fix - Remove WC 3.3+ "Connect to WooCommerce" notice when official plugins are active

includes/Forwarded_URLs.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class Forwarded_URLs {
3434
/** @var string non-forwarded host as defined in the siteurl option */
3535
public $non_forwarded_host;
3636

37+
/** @var array values to find and replace in URLS */
38+
private $find_replace = [];
39+
3740

3841
/**
3942
* Setup filters
@@ -94,6 +97,7 @@ public function __construct() {
9497
'stylesheet_directory_uri',
9598
'the_content',
9699
'the_content_pre',
100+
'wp_calculate_image_srcset',
97101
);
98102

99103
foreach ( $filters as $filter ) {
@@ -156,14 +160,37 @@ public function replace_with_forwarded_url( $content ) {
156160
$non_forwarded_host = $this->non_forwarded_host;
157161
$forwarded_host = $this->get_forwarded_host();
158162

159-
// http, https and protocol-less URLs
160-
$find_replace = array(
163+
// http, https, and protocol-less URLs
164+
$this->find_replace = [
161165
"http://{$non_forwarded_host}" => "http://{$forwarded_host}",
162166
"https://{$non_forwarded_host}" => "https://{$forwarded_host}",
163167
"//{$non_forwarded_host}" => "//{$forwarded_host}",
164-
);
168+
];
169+
170+
//$new = str_replace( array_keys( $this->find_replace ), array_values( $this->find_replace ), $content );
171+
$new = is_array( $content ) ? array_walk_recursive( $content, [ $this, 'replace_url' ] ) : str_replace( array_keys( $this->find_replace ), array_values( $this->find_replace ), $content );
172+
173+
return $new;
174+
}
175+
165176

166-
return str_replace( array_keys( $find_replace ), array_values( $find_replace ), $content );
177+
/**
178+
* Replaces URL host within strings recursively.
179+
*
180+
* Required because the image srcset will use upload dir, which we don't filter
181+
* (as we filter site URL), but does so before we filter site_URL.
182+
* BUT we can't filter upload dir directly, because then WP won't auto-detect protocol
183+
* for us, as we'd be replacing the URL too soon.
184+
* So, we filter the srcset at the last minute.
185+
*
186+
* @since 1.0.0
187+
*
188+
* @param array $element the array to operate on
189+
* @param int $index the internal pointer for array_walk_recursive
190+
* @return array the updated array
191+
*/
192+
private function replace_url( &$element, $index ) {
193+
return str_replace( array_keys( $this->find_replace ), array_values( $this->find_replace ), $element );
167194
}
168195

169196

0 commit comments

Comments
 (0)