Skip to content

Commit 1466073

Browse files
authored
Merge pull request #634 from ProgressPlanner/ari/detect-domain-changes
Detect site-url changes
2 parents 2e0dd23 + a6710c2 commit 1466073

2 files changed

Lines changed: 88 additions & 25 deletions

File tree

classes/admin/class-page.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ public function enqueue_scripts() {
180180
$default_localization_data = [
181181
'name' => 'progressPlanner',
182182
'data' => [
183-
'onboardNonceURL' => \progress_planner()->get_utils__onboard()->get_remote_nonce_url(),
184-
'onboardAPIUrl' => \progress_planner()->get_utils__onboard()->get_remote_url(),
183+
'onboardNonceURL' => \progress_planner()->get_utils__onboard()->get_remote_url( 'get-nonce' ),
184+
'onboardAPIUrl' => \progress_planner()->get_utils__onboard()->get_remote_url( 'onboard' ),
185185
'ajaxUrl' => \admin_url( 'admin-ajax.php' ),
186186
'nonce' => \wp_create_nonce( 'progress_planner' ),
187187
],

classes/utils/class-onboard.php

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public function __construct() {
2626
// Handle saving data from the onboarding form response.
2727
\add_action( 'wp_ajax_progress_planner_save_onboard_data', [ $this, 'save_onboard_response' ] );
2828

29+
// Detect domain changes.
30+
\add_action( 'shutdown', [ $this, 'detect_site_url_changes' ] );
31+
2932
if ( \get_option( 'progress_planner_license_key' ) ) {
3033
return;
3134
}
@@ -85,22 +88,19 @@ public function save_onboard_response() {
8588
\wp_send_json_error( [ 'message' => \esc_html__( 'Unable to save data.', 'progress-planner' ) ] );
8689
}
8790

88-
/**
89-
* Get the remote nonce URL.
90-
*
91-
* @return string
92-
*/
93-
public function get_remote_nonce_url() {
94-
return \progress_planner()->get_remote_server_root_url() . self::REMOTE_API_URL . 'get-nonce';
95-
}
96-
9791
/**
9892
* Get the onboarding remote URL.
9993
*
94+
* @param string $endpoint The endpoint to append to the remote URL.
95+
*
10096
* @return string
10197
*/
102-
public function get_remote_url() {
103-
return \progress_planner()->get_remote_server_root_url() . self::REMOTE_API_URL . 'onboard';
98+
public function get_remote_url( $endpoint = '' ) {
99+
// Sanitize and validate the endpoint parameter.
100+
$endpoint = \sanitize_key( $endpoint );
101+
$endpoint = \ltrim( $endpoint, '/' );
102+
103+
return \progress_planner()->get_remote_server_root_url() . self::REMOTE_API_URL . $endpoint;
104104
}
105105

106106
/**
@@ -111,18 +111,20 @@ public function get_remote_url() {
111111
public function get_remote_nonce() {
112112
// Make a POST request to the remote nonce endpoint.
113113
$response = \wp_remote_post(
114-
$this->get_remote_nonce_url(),
115-
[ 'body' => [ 'site' => \set_url_scheme( \site_url() ) ] ]
114+
$this->get_remote_url( 'get-nonce' ),
115+
[
116+
'timeout' => 10,
117+
'body' => [ 'site' => \set_url_scheme( \site_url() ) ],
118+
]
116119
);
120+
117121
if ( \is_wp_error( $response ) ) {
118122
return '';
119123
}
120-
$body = \wp_remote_retrieve_body( $response );
121-
$body = \json_decode( $body, true );
122-
if ( ! isset( $body['nonce'] ) ) {
123-
return '';
124-
}
125-
return $body['nonce'];
124+
125+
$body = \json_decode( \wp_remote_retrieve_body( $response ), true );
126+
127+
return isset( $body['nonce'] ) ? $body['nonce'] : '';
126128
}
127129

128130
/**
@@ -150,18 +152,79 @@ public function make_remote_onboarding_request( $data = [] ) {
150152

151153
// Make the request.
152154
$response = \wp_remote_post(
153-
$this->get_remote_url(),
154-
[ 'body' => $data ]
155+
$this->get_remote_url( 'onboard' ),
156+
[
157+
'timeout' => 10,
158+
'body' => $data,
159+
]
155160
);
161+
162+
// Bail early if there is an error.
156163
if ( \is_wp_error( $response ) ) {
157164
return '';
158165
}
159-
$body = \wp_remote_retrieve_body( $response );
160-
$body = \json_decode( $body, true );
166+
167+
$body = \json_decode( \wp_remote_retrieve_body( $response ), true );
168+
161169
return ! isset( $body['status'] )
162170
|| 'ok' !== $body['status']
163171
|| ! isset( $body['license_key'] )
164172
? ''
165173
: $body['license_key'];
166174
}
175+
176+
/**
177+
* Detect domain changes.
178+
*
179+
* @return void
180+
*/
181+
public function detect_site_url_changes() {
182+
// Only check once per day to avoid performance issues.
183+
if ( \get_transient( 'progress_planner_site_url_check_done' ) ) {
184+
return;
185+
}
186+
\set_transient( 'progress_planner_site_url_check_done', 1, DAY_IN_SECONDS );
187+
188+
$saved_site_url = \get_option( 'progress_planner_saved_site_url', false );
189+
$current_site_url = \set_url_scheme( \site_url() );
190+
191+
// Update the saved site URL if it's not set, then bail early.
192+
if ( ! $saved_site_url ) {
193+
\update_option( 'progress_planner_saved_site_url', $current_site_url, false );
194+
return;
195+
}
196+
197+
$saved_license_key = \get_option( 'progress_planner_license_key', false );
198+
199+
// Bail early if the license key is not set, or if the site URL has not changed.
200+
if ( ! $saved_license_key || $saved_site_url === $current_site_url ) {
201+
return;
202+
}
203+
204+
// Make a request to the remote endpoint to update the license key.
205+
$response = \wp_remote_post(
206+
$this->get_remote_url( 'change-site-url' ),
207+
[
208+
'timeout' => 10,
209+
'body' => [
210+
'license_key' => $saved_license_key,
211+
'old_url' => $saved_site_url,
212+
'new_url' => $current_site_url,
213+
'nonce' => $this->get_remote_nonce(),
214+
],
215+
]
216+
);
217+
218+
// Bail early if there is an error.
219+
if ( \is_wp_error( $response ) ) {
220+
return;
221+
}
222+
223+
$body = \json_decode( \wp_remote_retrieve_body( $response ), true );
224+
225+
// Update the saved site URL if the request was successful.
226+
if ( isset( $body['status'] ) && 'ok' === $body['status'] ) {
227+
\update_option( 'progress_planner_saved_site_url', $current_site_url, false );
228+
}
229+
}
167230
}

0 commit comments

Comments
 (0)