Skip to content

Commit 4ae6d71

Browse files
committed
Automatically register branded sites
1 parent 3d3ce98 commit 4ae6d71

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

classes/utils/class-onboard.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,66 @@ public function get_remote_nonce_url() {
9797
public function get_remote_url() {
9898
return \progress_planner()->get_remote_server_root_url() . self::REMOTE_API_URL . 'onboard';
9999
}
100+
101+
/**
102+
* Get the remote nonce.
103+
*
104+
* @return string
105+
*/
106+
public function get_remote_nonce() {
107+
// Make a POST request to the remote nonce endpoint.
108+
$response = \wp_remote_post(
109+
$this->get_remote_nonce_url(),
110+
[ 'body' => [ 'site' => \set_url_scheme( \site_url() ) ] ]
111+
);
112+
if ( \is_wp_error( $response ) ) {
113+
return '';
114+
}
115+
$body = \wp_remote_retrieve_body( $response );
116+
$body = \json_decode( $body, true );
117+
if ( ! isset( $body['nonce'] ) ) {
118+
return '';
119+
}
120+
return $body['nonce'];
121+
}
122+
123+
/**
124+
* Make a request to the remote onboarding endpoint.
125+
*
126+
* @param array $data The data to send with the request.
127+
*
128+
* @return string The license key.
129+
*/
130+
public function make_remote_onboarding_request( $data = [] ) {
131+
// Set the data.
132+
if ( ! isset( $data['nonce'] ) ) {
133+
$data['nonce'] = $this->get_remote_nonce();
134+
}
135+
$data = \wp_parse_args(
136+
$data,
137+
[
138+
'site' => \set_url_scheme( \site_url() ),
139+
'email' => \wp_get_current_user()->user_email,
140+
'name' => \get_user_meta( \wp_get_current_user()->ID, 'first_name', true ),
141+
'with-email' => 'yes',
142+
'timezone_offset' => (float) ( \wp_timezone()->getOffset( new \DateTime( 'midnight' ) ) / 3600 ),
143+
]
144+
);
145+
146+
// Make the request.
147+
$response = \wp_remote_post(
148+
$this->get_remote_url(),
149+
[ 'body' => $data ]
150+
);
151+
if ( \is_wp_error( $response ) ) {
152+
return '';
153+
}
154+
$body = \wp_remote_retrieve_body( $response );
155+
$body = \json_decode( $body, true );
156+
return ! isset( $body['status'] )
157+
|| 'ok' !== $body['status']
158+
|| ! isset( $body['license_key'] )
159+
? ''
160+
: $body['license_key'];
161+
}
100162
}

views/welcome.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
if ( false !== \get_option( 'progress_planner_license_key', false ) ) {
1616
return;
17+
} elseif ( 0 !== (int) \progress_planner()->get_ui__branding()->get_branding_id() ) {
18+
$prpl_license_key = \progress_planner()->get_utils__onboard()->make_remote_onboarding_request();
19+
if ( '' !== $prpl_license_key ) {
20+
\update_option( 'progress_planner_license_key', $prpl_license_key );
21+
}
22+
return;
1723
}
1824

1925
// Enqueue styles.

0 commit comments

Comments
 (0)