Skip to content

Commit

Permalink
Merge pull request #4 from vendidero/release-api
Browse files Browse the repository at this point in the history
Release api
  • Loading branch information
dennisnissle authored Jul 16, 2021
2 parents 61a7b2d + d71fc19 commit e92ed35
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 20 deletions.
6 changes: 3 additions & 3 deletions includes/class-vd-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function unregister( VD_Product $product ) {
}

public function info( VD_Product $product ) {
$request = new VD_Request( "version/{$product->id}/latest/info", $product );
$request = new VD_Request( "releases/{$product->id}/latest/info", $product );

return ( ! $request->is_error() ? $request->get_response() : false );
}
Expand Down Expand Up @@ -69,8 +69,8 @@ private function _update_check( VD_Product $product, $key = '' ) {
'errors' => array(),
'notices' => array()
);

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

if ( $request->is_error() ) {
$error = $request->get_response();
Expand Down
11 changes: 8 additions & 3 deletions includes/class-vd-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public function init() {
}

private function get_endpoint() {
return VD()->get_api_url() . $this->args['request'];
$api_url = VD()->get_api_url();

if ( strpos( $this->args['request'], 'releases/' ) !== false ) {
$api_url = VD()->get_download_api_url();
}

return trailingslashit( $api_url ) . $this->args['request'];
}

public function do_request() {
Expand All @@ -52,7 +58,6 @@ public function do_request() {
'sslverify' => false
) );
} else {

$this->raw = wp_remote_post( $this->get_endpoint(), array(
'method' => 'POST',
'redirection' => 5,
Expand All @@ -72,7 +77,7 @@ public function do_request() {
}

public function is_error() {
if ( in_array( $this->code, array( 500, 404 ) ) ) {
if ( in_array( $this->code, array( 500, 404, 429 ) ) ) {
return true;
}

Expand Down
15 changes: 10 additions & 5 deletions includes/class-vd-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,28 @@ public function update_check( $transient ) {

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

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() ) {
$payload->plugin = $this->product->file;
$payload->slug = sanitize_title( $this->product->Name );
$payload->plugin = $this->product->file;
$payload->slug = sanitize_title( $this->product->Name );

// update-core.php expects icons to be formatted as array (see wp-admin/update-core.php:473
if ( isset( $payload->custom_icons ) ) {
$payload->icons = (array) $payload->custom_icons;
}
} else {
$payload = (array) $payload;
$payload['theme'] = $this->product->file;
$payload['theme'] = $this->product->file;
}

$transient->response[ ( ( $this->product->is_theme() ) ? $this->product->Name : $this->product->file ) ] = $payload;
Expand Down
90 changes: 81 additions & 9 deletions vendidero-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Vendidero Helper
* Plugin URI: http://vendidero.de
* Description: Will help vendidero users to manage their licenses and receive automatic updates
* Version: 1.3.0
* Version: 2.0.0
* Author: Vendidero
* Author URI: http://vendidero.de
* License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
Expand All @@ -22,7 +22,7 @@ final class Vendidero_Helper {
*/
protected static $_instance = null;

public $version = '1.3.0';
public $version = '2.0.0';

/**
* @var VD_API $api
Expand All @@ -31,10 +31,11 @@ final class Vendidero_Helper {
public $plugins = array();
public $themes = array();

private $debug_mode = false;
private $token = 'vendidero-api';
private $api_url = 'https://vendidero.de/wp-json/vd/v1/';
private $products = array();
private $debug_mode = false;
private $token = 'vendidero-api';
private $api_url = 'https://vendidero.de/wp-json/vd/v1/';
private $download_api_url = 'https://download.vendidero.de/api/v1/';
private $products = array();

/**
* Main Vendidero Instance
Expand Down Expand Up @@ -104,11 +105,53 @@ public function init() {
add_filter( 'http_request_host_is_external', array( $this, 'allow_local_urls' ) );
add_filter( 'http_request_args', array( $this, 'disable_ssl_verify' ), 10, 1 );
}

add_action( 'upgrader_pre_download', array( $this, 'block_expired_updates' ), 50, 2 );
}

/**
* Hooked into the upgrader_pre_download filter in order to better handle error messaging around expired
* plugin updates. Initially we were using an empty string, but the error message that no_package
* results in does not fit the cause.
*
* @since 2.0.0
* @param bool $reply Holds the current filtered response.
* @param string $package The path to the package file for the update.
* @return false|WP_Error False to proceed with the update as normal, anything else to be returned instead of updating.
*/
public function block_expired_updates( $reply, $package ) {
// Don't override a reply that was set already.
if ( false !== $reply ) {
return $reply;
}

// Only for packages with expired subscriptions.
if ( 0 !== strpos( $package, 'vendidero-expired-' ) ) {
return $reply;
}

$product_id = absint( str_replace( 'vendidero-expired-', '', $package ) );

if ( $product = $this->get_product_by_id( $product_id ) ) {
return new WP_Error(
'vendidero_expired',
sprintf(
// translators: %s: Renewal url.
__( 'Your update- and support-flat has expired. Please <a href="%s" target="_blank">renew</a> your license before updating.', 'vendidero-helper' ),
esc_url( $product->get_renewal_url() )
)
);
} else {
return new WP_Error(
'vendidero_expired',
__( 'Your update- and support-flat has expired. Please renew your license before updating.', 'vendidero-helper' )
);
}
}

public function adjust_signature_url( $signature_url, $url ) {
if ( strpos( $url, $this->api_url ) !== false ) {
$signature_url = str_replace( '/latest', '/latest.sig', $url );
if ( strpos( $url, $this->download_api_url ) !== false ) {
$signature_url = str_replace( 'latest/download', 'latest/downloadSignature', $url );
}

return $signature_url;
Expand All @@ -121,7 +164,7 @@ public function add_signature_trusted_keys( $keys ) {
}

public function add_signature_hosts( $hosts ) {
$url = @parse_url( $this->api_url );
$url = @parse_url( $this->download_api_url );
$hosts[] = $url['host'];

return $hosts;
Expand Down Expand Up @@ -443,6 +486,11 @@ public function remove_product( $file ) {
return $response;
}

/**
* @param bool $show_free
*
* @return VD_Product[]
*/
public function get_products( $show_free = true ) {
$products = $this->products;

Expand All @@ -457,14 +505,38 @@ public function get_products( $show_free = true ) {
return $products;
}

/**
* @param $key
*
* @return false|VD_Product
*/
public function get_product( $key ) {
return ( isset( $this->products[ $key ] ) ? $this->products[ $key ] : false );
}

/**
* @param $id
*
* @return false|VD_Product
*/
public function get_product_by_id( $id ) {
foreach( $this->get_products() as $key => $product ) {
if ( $product->id == $id ) {
return $product;
}
}

return false;
}

public function get_api_url() {
return $this->api_url;
}

public function get_download_api_url() {
return $this->download_api_url;
}

public function get_token() {
return $this->token;
}
Expand Down

0 comments on commit e92ed35

Please sign in to comment.