Skip to content

Commit 7486102

Browse files
authored
Merge pull request #633 from ProgressPlanner/ari/auto-register-if-branded
auto-register branded websites
2 parents 3bbae2d + 5db9336 commit 7486102

2 files changed

Lines changed: 71 additions & 1 deletion

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/admin-page.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
$prpl_privacy_policy_accepted = \progress_planner()->is_privacy_policy_accepted();
1414
$prpl_wrapper_class = '';
1515

16+
if ( 0 !== (int) \progress_planner()->get_ui__branding()->get_branding_id() ) {
17+
$prpl_license_key = \progress_planner()->get_utils__onboard()->make_remote_onboarding_request();
18+
if ( '' !== $prpl_license_key ) {
19+
\update_option( 'progress_planner_license_key', $prpl_license_key );
20+
$prpl_privacy_policy_accepted = true;
21+
}
22+
}
23+
1624
if ( ! $prpl_privacy_policy_accepted ) {
1725
$prpl_wrapper_class = 'prpl-pp-not-accepted';
1826
}
@@ -43,4 +51,4 @@
4351
</div>
4452
<div class="prpl-overlay" style="display: none;" onclick="document.querySelector('[data-tooltip-visible=true]').removeAttribute('data-tooltip-visible')"></div>
4553

46-
<?php \progress_planner()->the_view( 'js-templates/suggested-task.html' ); ?>
54+
<?php \progress_planner()->the_view( 'js-templates/suggested-task.html' ); ?>

0 commit comments

Comments
 (0)