@@ -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}
0 commit comments