Skip to content

Commit

Permalink
Merge branch 'master' into release-api
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle authored Jul 16, 2021
2 parents fc498e3 + 61a7b2d commit d71fc19
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
4 changes: 4 additions & 0 deletions includes/class-vd-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ public function process_register() {
if ( ! empty( $errors ) ) {
$this->add_notice( $errors, 'error' );
}

VD()->api->flush_update_cache();
}

public function process_unregister() {
Expand All @@ -251,6 +253,8 @@ public function process_unregister() {
if ( ! empty( $errors ) ) {
$this->add_notice( $errors, 'error' );
}

VD()->api->flush_update_cache();
}

public function add_notice( $msg = array(), $type = 'error' ) {
Expand Down
55 changes: 53 additions & 2 deletions includes/class-vd-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,61 @@ public function expiration_check( VD_Product $product ) {
return ( ! $request->is_error() ? $request->get_response( "expiration_date" ) : $request->get_response() );
}

public function update_check( VD_Product $product, $key = '' ) {
public function flush_update_cache() {
foreach( VD()->get_products() as $product ) {
delete_transient( "_vendidero_helper_updates_{$product->id}" );
}
}

private function _update_check( VD_Product $product, $key = '' ) {
$cache_key = "_vendidero_helper_updates_{$product->id}";
$data = get_transient( $cache_key );

if ( false !== $data && ! empty( $_GET['force-check'] ) ) {
// Wait at least 1 minute between multiple forced version check requests.
$timeout = MINUTE_IN_SECONDS;
$time_not_changed = ! empty( $data['updated'] ) && $timeout > ( time() - $data['updated'] );

if ( ! $time_not_changed ) {
delete_transient( $cache_key );
$data = false;
}
}

if ( false !== $data ) {
return $data;
}

$data = array(
'updated' => time(),
'payload' => array(),
'errors' => array(),
'notices' => array()
);

$request = new VD_Request( "releases/{$product->id}/latest", $product, array( 'key' => $key, 'version' => $product->Version ) );

return $request;
if ( $request->is_error() ) {
$error = $request->get_response();

foreach( $error->get_error_messages( $error->get_error_code() ) as $msg ) {
$data['errors'][] = $msg;
}
} else {
if ( $request->get_response( "notice" ) ) {
$data['notices'] = (array) $request->get_response( "notice" );
}

$data['payload'] = $request->get_response( "payload" );
}

set_transient( $cache_key, $data, 6 * HOUR_IN_SECONDS );

return $data;
}

public function update_check( VD_Product $product, $key = '' ) {
return $this->_update_check( $product, $key );
}

public function generator_version_check( VD_Product $product, $generator ) {
Expand Down
3 changes: 1 addition & 2 deletions includes/class-vd-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ private function get_endpoint() {
}

public function do_request() {

if ( 'GET' === $this->args['method'] ) {
$url = add_query_arg( $this->args, $this->get_endpoint() );

$this->raw = wp_remote_get( $url, array(
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
Expand All @@ -60,7 +60,6 @@ public function do_request() {
} else {
$this->raw = wp_remote_post( $this->get_endpoint(), array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
Expand Down
26 changes: 11 additions & 15 deletions includes/class-vd-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class VD_Updater {
public $added_upgrade_notice = false;

public function __construct( VD_Product $product ) {

$this->product = $product;

// Check For Updates
Expand All @@ -17,7 +16,6 @@ public function __construct( VD_Product $product ) {
}

public function ssl_verify( $args, $url ) {

if ( is_admin() ) {
if ( apply_filters( 'vd_helper_disable_ssl_verify', false ) && $url == VD()->get_api_url() ) {
$args['sslverify'] = false;
Expand All @@ -28,25 +26,23 @@ public function ssl_verify( $args, $url ) {
}

public function update_check( $transient ) {
$request = VD()->api->update_check( $this->product, $this->product->get_key() );

if ( $request->is_error() ) {
$error = $request->get_response();

$this->add_notice( $error->get_error_messages( $error->get_error_code() ) );
$data = VD()->api->update_check( $this->product, $this->product->get_key() );

if ( ! empty( $data['errors'] ) ) {
$this->add_notice( $data['errors'] );
} else {

if ( $request->get_response( "notice" ) ) {
$this->add_notice( (array) $request->get_response( "notice" ), 'error' );
}
if ( ! empty( $data['notices'] ) ) {
$this->add_notice( $data['notices'], 'error' );
}

if ( $request->get_response( "payload" ) ) {
$payload = $request->get_response( "payload" );

if ( ! empty( $data['payload'] ) ) {
$payload = $data['payload'];
// Do only add transient if remote version is newer than local version
if ( version_compare( $payload->new_version, $this->product->Version, "<=" ) ) {
return $transient;
}
}

// Set plugin/theme file (seems to be necessary as for 4.2)
if ( ! $this->product->is_theme() ) {
Expand Down
1 change: 0 additions & 1 deletion vendidero-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ public function get_products( $show_free = true ) {

if ( ! $show_free ) {
foreach ( $this->products as $key => $product ) {

if ( $product->free ) {
unset( $products[ $key ] );
}
Expand Down

0 comments on commit d71fc19

Please sign in to comment.