Skip to content

Commit 7635d91

Browse files
authored
Merge pull request #11 from skyverge/refactor/use-namespaces
Refactor: use namespaces + add ngrok support
2 parents 72d92a1 + 826a4e9 commit 7635d91

8 files changed

Lines changed: 121 additions & 57 deletions

File tree

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ This is a simple and opinionated plugin for helping develop/debug WooCommerce &
55

66
### Use Forwarded URLs
77

8-
Once activated, this plugin will make WordPress use `HTTP_X_FORWARDED_HOST` for all your links instead of your site URL. No configuration needed.
8+
Once activated, this plugin will make WordPress use `HTTP_X_FORWARDED_HOST` or `HTTP_X_ORIGINAL_HOST` for all your links instead of your site URL. No configuration needed.
99

10-
We like [ForwardHQ](https://fwd.wf) for this, along with a `ForwardFile` in your WordPress root.
10+
We like to use [NGrok](http://ngrok.com/) for this.
1111

1212
### Subscriptions (v1.5.x and v2.x.x)
1313

@@ -44,6 +44,12 @@ Download and install just like any other WordPress plugin. If you want to be rea
4444

4545
## Changelog
4646

47+
### 1.0.0 - 2018-11-06
48+
* Refactor - Use namespaces and rename classes
49+
* Tweak - Add support for ngrok
50+
* Fix - Ensure images load for products when using an https tunnel
51+
* Misc - Require PHP 5.3+
52+
4753
### 0.8.1 - 2017.12.13
4854
* Fix - Remove WC 3.3+ "Connect to WooCommerce" notice when official plugins are active
4955

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
*
1313
* @package WC-Dev-Helper/Classes
1414
* @author SkyVerge
15-
* @copyright Copyright (c) 2015-2017, SkyVerge, Inc.
15+
* @copyright Copyright (c) 2015-2018, SkyVerge, Inc.
1616
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
1717
*/
1818

19+
namespace SkyVerge\WooCommerce\DevHelper;
20+
1921
defined( 'ABSPATH' ) or exit;
2022

21-
class WC_Dev_Helper_Ajax {
23+
class Ajax {
2224

2325

2426
/**

includes/class-wc-dev-helper-bogus-gateway.php renamed to includes/Bogus_Gateway.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
*
1313
* @package WC-Dev-Helper/Classes
1414
* @author SkyVerge
15-
* @copyright Copyright (c) 2015-2017, SkyVerge, Inc.
15+
* @copyright Copyright (c) 2015-2018, SkyVerge, Inc.
1616
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
1717
*/
1818

19+
namespace SkyVerge\WooCommerce\DevHelper;
20+
1921
defined( 'ABSPATH' ) or exit;
2022

21-
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
23+
if ( ! class_exists( '\\WC_Payment_Gateway' ) ) {
2224
return;
2325
}
2426

@@ -27,7 +29,12 @@
2729
*
2830
* @since 0.6.0
2931
*/
30-
class WC_Bogus_Gateway extends WC_Payment_Gateway {
32+
class Bogus_Gateway extends \WC_Payment_Gateway {
33+
34+
35+
/** @var string subscriptions setting */
36+
private $subscriptions;
37+
3138

3239
/**
3340
* Constructor for the gateway.
@@ -37,7 +44,7 @@ class WC_Bogus_Gateway extends WC_Payment_Gateway {
3744
public function __construct() {
3845

3946
$this->id = 'bogus_gateway';
40-
$this->icon = apply_filters('woocommerce_bogus_icon', '');
47+
$this->icon = apply_filters( 'woocommerce_bogus_icon', '' );
4148
$this->has_fields = false;
4249
$this->method_title = __( 'Bogus', 'woocommerce-dev-helper' );
4350
$this->method_description = __( 'A testing gateway that calls "payment complete" to simulate credit card transactions.', 'woocommerce-dev-helper' );
@@ -154,8 +161,8 @@ public function process_payment( $order_id ) {
154161

155162
// Return thank you redirect
156163
return array(
157-
'result' => 'success',
158-
'redirect' => $this->get_return_url( $order )
164+
'result' => 'success',
165+
'redirect' => $this->get_return_url( $order ),
159166
);
160167
}
161168

@@ -167,7 +174,7 @@ public function process_payment( $order_id ) {
167174
* @param float $amount_to_charge subscription amount to charge, could include
168175
* multiple renewals if they've previously failed and the admin
169176
* has enabled it
170-
* @param WC_Order $order original order containing the subscription
177+
* @param \WC_Order $order original order containing the subscription
171178
*/
172179
public function process_renewal_payment( $amount_to_charge, $order ) {
173180

includes/class-wc-dev-helper-use-forwarded-urls.php renamed to includes/Forwarded_URLs.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
*
1313
* @package WC-Dev-Helper/Classes
1414
* @author SkyVerge
15-
* @copyright Copyright (c) 2015-2017, SkyVerge, Inc.
15+
* @copyright Copyright (c) 2015-2018, SkyVerge, Inc.
1616
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
1717
*/
1818

19+
namespace SkyVerge\WooCommerce\DevHelper;
20+
1921
defined( 'ABSPATH' ) or exit;
2022

2123
/**
@@ -26,12 +28,15 @@
2628
*
2729
* @since 0.1.0
2830
*/
29-
class WC_Dev_Helper_Use_Forwarded_URLs {
31+
class Forwarded_URLs {
3032

3133

3234
/** @var string non-forwarded host as defined in the siteurl option */
3335
public $non_forwarded_host;
3436

37+
/** @var array values to find and replace in URLS */
38+
private $find_replace = array();
39+
3540

3641
/**
3742
* Setup filters
@@ -92,6 +97,7 @@ public function __construct() {
9297
'stylesheet_directory_uri',
9398
'the_content',
9499
'the_content_pre',
100+
'wp_calculate_image_srcset',
95101
);
96102

97103
foreach ( $filters as $filter ) {
@@ -116,7 +122,7 @@ public function __construct() {
116122
*/
117123
private function has_forwarded_host() {
118124

119-
return array_key_exists( 'HTTP_X_FORWARDED_HOST', $_SERVER );
125+
return array_key_exists( 'HTTP_X_FORWARDED_HOST', $_SERVER ) || array_key_exists( 'HTTP_X_ORIGINAL_HOST', $_SERVER );
120126
}
121127

122128

@@ -128,7 +134,13 @@ private function has_forwarded_host() {
128134
*/
129135
public function get_forwarded_host() {
130136

131-
return $_SERVER['HTTP_X_FORWARDED_HOST'];
137+
// are we using Forward HQ?
138+
$host = isset( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : false;
139+
140+
// if not, check if we're using ngrok
141+
$host = ! $host && isset( $_SERVER['HTTP_X_ORIGINAL_HOST'] ) ? $_SERVER['HTTP_X_ORIGINAL_HOST'] : $_SERVER['HTTP_HOST'];
142+
143+
return $host;
132144
}
133145

134146

@@ -148,14 +160,36 @@ public function replace_with_forwarded_url( $content ) {
148160
$non_forwarded_host = $this->non_forwarded_host;
149161
$forwarded_host = $this->get_forwarded_host();
150162

151-
// http, https and protocol-less URLs
152-
$find_replace = array(
163+
// http, https, and protocol-less URLs
164+
$this->find_replace = array(
153165
"http://{$non_forwarded_host}" => "http://{$forwarded_host}",
154166
"https://{$non_forwarded_host}" => "https://{$forwarded_host}",
155167
"//{$non_forwarded_host}" => "//{$forwarded_host}",
156168
);
157169

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

161195

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @package WC-Dev-Helper/Classes
1414
* @author SkyVerge
15-
* @copyright Copyright (c) 2015-2017, SkyVerge, Inc.
15+
* @copyright Copyright (c) 2015-2018, SkyVerge, Inc.
1616
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
1717
*/
1818

includes/class-wc-dev-helper-memberships.php renamed to includes/Integrations/Memberships.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
*
1313
* @package WC-Dev-Helper/Classes
1414
* @author SkyVerge
15-
* @copyright Copyright (c) 2015-2017, SkyVerge, Inc.
15+
* @copyright Copyright (c) 2015-2018, SkyVerge, Inc.
1616
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
1717
*/
1818

19+
namespace SkyVerge\WooCommerce\DevHelper\Integrations;
20+
1921
defined( 'ABSPATH' ) or exit;
2022

2123
/**
@@ -25,7 +27,7 @@
2527
*
2628
* @since 0.4.0
2729
*/
28-
class WC_Dev_Helper_Memberships {
30+
class Memberships {
2931

3032

3133
/**
@@ -37,6 +39,7 @@ public function __construct() {
3739

3840
// add support for minutes and hours-long membership plans
3941
add_filter( 'wc_memberships_plan_access_period_options', array( $this, 'add_membership_plan_access_period_options' ) );
42+
4043
// filter the human access length information so it can work with minutes and hours
4144
add_filter( 'wc_memberships_membership_plan_human_access_length', array( $this, 'filter_membership_human_access_length' ), 10, 2 );
4245
}

includes/class-wc-dev-helper-subscriptions.php renamed to includes/Integrations/Subscriptions.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
*
1313
* @package WC-Dev-Helper/Classes
1414
* @author SkyVerge
15-
* @copyright Copyright (c) 2015-2017, SkyVerge, Inc.
15+
* @copyright Copyright (c) 2015-2018, SkyVerge, Inc.
1616
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
1717
*/
1818

19+
namespace SkyVerge\WooCommerce\DevHelper\Integrations;
20+
1921
defined( 'ABSPATH' ) or exit;
2022

2123
/**
@@ -26,7 +28,7 @@
2628
*
2729
* @since 0.1.0
2830
*/
29-
class WC_Dev_Helper_Subscriptions {
31+
class Subscriptions {
3032

3133

3234
/**
@@ -147,7 +149,7 @@ public function maybe_render_renewal_success_message() {
147149

148150
$subscription = wcs_get_subscription( absint( $_REQUEST['id'] ) );
149151

150-
if ( $subscription instanceof WC_Subscription ) {
152+
if ( $subscription instanceof \WC_Subscription ) {
151153
echo '<div class="updated"><p>' . sprintf( esc_html__( 'Subscription renewal processed. %sView Renewal Order%s', 'woocommerce-dev-helper' ), '<a href="' . wcs_get_edit_post_link( $subscription->get_last_order() ) . '">', ' &#8594;</a>' ) . '</p></div>';
152154
}
153155
}
@@ -178,7 +180,7 @@ public function process_pre_subs_2_0_renew_action() {
178180
WC()->payment_gateways();
179181

180182
// trigger the renewal payment
181-
WC_Subscriptions_Payment_Gateways::gateway_scheduled_subscription_payment( absint( $_GET['user'] ), $_GET['subscription'] );
183+
\WC_Subscriptions_Payment_Gateways::gateway_scheduled_subscription_payment( absint( $_GET['user'] ), $_GET['subscription'] );
182184

183185
// success message
184186
add_filter( 'woocommerce_subscriptions_list_table_pre_process_actions', array( $this, 'maybe_render_pre_subs_2_0_renewal_success_message' ) );
@@ -214,7 +216,7 @@ public function maybe_render_pre_subs_2_0_renewal_success_message( $args ) {
214216
*/
215217
protected function is_subs_gte_2_0() {
216218

217-
return version_compare( WC_Subscriptions::$version, '1.6.0', '>' );
219+
return version_compare( \WC_Subscriptions::$version, '2.0.0', '>=' );
218220
}
219221

220222

@@ -229,7 +231,7 @@ public function new_subscription_periods( $subscription_periods ) {
229231

230232
$new_periods = array(
231233
'minute' => 'minute',
232-
'hour' => 'hour',
234+
'hour' => 'hour',
233235
);
234236

235237
return array_merge( $new_periods, $subscription_periods);

0 commit comments

Comments
 (0)