Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,49 @@ public function __construct( Integration $integration ) {
* @return array|WP_Error Response data or WP_Error on failure.
*/
public function ping() {
return $this->request( 'ping', [], 2 );
$request_url = "{$this->integration->api_url}/products/{$this->integration->product_id}/ping";

$body = [
'product_version' => $this->integration->product_version,
'product_status' => $this->integration->product_status,
'wp_url' => esc_url( site_url( '', 'https' ) ),
'wp_locale' => get_locale(),
'wp_version' => get_bloginfo( 'version', 'display' ),
'admin_email' => $this->integration->admin_email,
'admin_name' => $this->integration->admin_name,
];

$request = wp_remote_post(
$request_url,
[
'timeout' => 2,
'body' => $body,
]
);

if ( is_wp_error( $request ) ) {
return $request;
}

$status_code = wp_remote_retrieve_response_code( $request );
$body = wp_remote_retrieve_body( $request );
$body = json_decode( $body, true );

if ( empty( $body ) ) {
return new WP_Error(
'wprepo_api_fail',
'No response from update server'
);
}

if ( $status_code >= 400 ) {
return new WP_Error(
! empty( $body['code'] ) ? $body['code'] : 'wprepo_api_error',
! empty( $body['message'] ) ? $body['message'] : 'API request failed'
);
}

return $body;
}

/**
Expand Down Expand Up @@ -165,18 +207,9 @@ public function details( $cache_period = 600 ) {
private function request( $method, $args = [], $timeout = 5 ) {
$request_url = "{$this->integration->api_url}/products/{$this->integration->product_id}/$method";

$args = array_merge(
[
'product_version' => $this->integration->product_version,
'product_status' => $this->integration->product_status,
'wp_url' => esc_url( site_url( '', 'https' ) ),
'wp_locale' => get_locale(),
'wp_version' => get_bloginfo( 'version', 'display' ),
],
$args
);

$request_url = add_query_arg( $args, $request_url );
if ( ! empty( $args ) ) {
$request_url = add_query_arg( $args, $request_url );
}

$request = wp_remote_request(
$request_url,
Expand Down
14 changes: 14 additions & 0 deletions src/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ class Integration {
*/
public $product_name;

/**
* Site admin email.
*
* @var string
*/
public $admin_email = '';

/**
* First admin username.
*
* @var string
*/
public $admin_name = '';

/**
* Sanitized name for the plugin license option.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function sync_license_data() {
return;
}

$this->integration->client->ping();

$response = $this->integration->client->check_license();
if ( is_wp_error( $response ) ) {
if ( 'invalid_license' === $response->get_error_code() ) {
Expand Down
10 changes: 10 additions & 0 deletions src/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public function init() {

$this->integration->product_version = $plugin['Version'];
$this->integration->product_name = $plugin['Name'];
$this->integration->admin_email = get_option( 'admin_email' );

$admins = get_users( [ 'role' => 'administrator', 'number' => 1, 'orderby' => 'ID', 'order' => 'ASC' ] );
if ( ! empty( $admins ) ) {
$this->integration->admin_name = $admins[0]->display_name;
}

// Schedule a cron job to refresh license data hourly.
$hook_name = "wprepo_sync_license_data_{$this->integration->license_name}";
Expand All @@ -80,6 +86,10 @@ public function init() {
* @return object Filtered transient.
*/
public function pre_set_transient( $transient ) {
if ( 'inactive' === $this->integration->product_status ) {
return $transient;
}

if ( property_exists( $transient, 'checked' ) && ! empty( $transient->checked ) ) {
// Use the checked version as fallback when product_version is not yet set
// (e.g. pre_set_transient fires before the init hook).
Expand Down
4 changes: 2 additions & 2 deletions tests/ClientApiRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function ping_returns_parsed_body() {

$fixture = $this->load_fixture_raw( 'ping-success.json' );

Functions\expect( 'wp_remote_request' )
Functions\expect( 'wp_remote_post' )
->once()
->andReturn( [ 'body' => $fixture ] );

Expand All @@ -66,7 +66,7 @@ public function wp_error_from_wp_remote_request_is_returned() {

$error = new WP_Error( 'http_error', 'Connection timed out' );

Functions\expect( 'wp_remote_request' )
Functions\expect( 'wp_remote_post' )
->once()
->andReturn( $error );

Expand Down
Loading