api_url = $api_url; $this->notices = $notices; } /** * Logs the current site in to the remote API * * @param License $license * @return bool */ public function create_license_activation( License $license ) { $endpoint = '/license/activations'; $data = array( 'site_url' => $license->site ); $result = $this->request( 'POST', $endpoint, $data ); if( $result ) { $this->notices->add( $result->message, 'info' ); return true; } return false; } /** * Logs the current site out of the remote API * * @param License $license * @return bool */ public function delete_license_activation( License $license ) { $endpoint = '/license/activations'; $data = array( 'site_url' => $license->site ); $result = $this->request( 'DELETE', $endpoint, $data ); if( $result ) { $this->notices->add( $result->message, 'info' ); return true; } return false; } /** * @param Plugin $plugin * @return object */ public function get_plugin( Plugin $plugin ) { $endpoint = sprintf( '/plugins/%s?format=wp', $plugin->id() ); return $this->request( 'GET', $endpoint ); } /** * @param Plugin[] $plugins * @return object */ public function get_plugins( $plugins ) { // create array of plugin ID's $plugin_slugs = $plugins->map(function( $p ) { return $p->id(); }); $endpoint = add_query_arg( array( 'sids' => implode(',', $plugin_slugs ), 'format' => 'wp' ), '/plugins' ); return $this->request( 'GET', $endpoint ); } /** * @param string $method * @param string $endpoint * @param array $data * @return object */ public function request( $method, $endpoint, $data = array() ) { $url = $this->api_url . $endpoint; $args = array( 'method' => $method ); if( in_array( $method, array( 'GET', 'DELETE' ) ) ) { $url = add_query_arg( $data, $url ); } else { $args['body'] = $data; } $request = wp_remote_request( $url, $args ); // test for wp errors if( $request instanceof WP_Error) { $this->notices->add( $request->get_error_message(), 'error' ); ; return false; } // retrieve response body $body = wp_remote_retrieve_body( $request ); $response = json_decode( $body ); if( ! is_object( $response ) ) { $this->notices->add( __( "The Boxzilla server returned an invalid response.", 'boxzilla' ), 'error' ); return false; } // store response $this->last_response = $response; // did request return an error response? if( isset( $response->error ) ) { $this->notices->add( $response->error->message, 'error' ); return null; } // return actual response data return $response->data; } /** * @return object|null */ public function get_last_response() { return $this->last_response; } }