Skip to content

Commit

Permalink
Merge pull request #4 from hivepress/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
hivepress authored Jul 9, 2020
2 parents 108e44c + b22003f commit 1fa0b5d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
2 changes: 1 addition & 1 deletion hivepress-updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: HivePress Updates
* Description: Get updates for purchased themes and extensions.
* Version: 1.0.1
* Version: 1.0.2
* Author: HivePress
* Author URI: https://hivepress.io/
* Text Domain: hivepress-updates
Expand Down
100 changes: 98 additions & 2 deletions includes/components/class-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function __construct( $args = [] ) {
return;
}

// Check theme updates.
add_filter( 'pre_set_site_transient_update_themes', [ $this, 'check_theme_updates' ] );

// Check plugin updates.
add_filter( 'pre_set_site_transient_update_plugins', [ $this, 'check_plugin_updates' ] );

Expand All @@ -40,6 +43,71 @@ public function __construct( $args = [] ) {
parent::__construct( $args );
}

/**
* Gets license key.
*
* @return string
*/
protected function get_license_key() {
return implode( ',', explode( "\n", get_option( 'hp_hivepress_license_key' ) ) );
}

/**
* Gets themes.
*
* @return array
*/
protected function get_themes() {

// Get license key.
$license_key = $this->get_license_key();

// Get cache key.
$cache_key = 'themes_' . md5( $license_key );

// Get cached themes.
$themes = hivepress()->cache->get_cache( $cache_key );

if ( is_null( $themes ) ) {
$themes = [];

// Get API response.
$response = json_decode(
wp_remote_retrieve_body(
wp_remote_get(
'https://store.hivepress.io/api/v1/products?' . http_build_query(
[
'type' => 'theme',
'license_key' => $license_key,
]
)
)
),
true
);

if ( is_array( $response ) && isset( $response['data'] ) ) {
foreach ( $response['data'] as $theme ) {

// Add theme.
$themes[ $theme['slug'] ] = [
'theme' => $theme['slug'],
'new_version' => $theme['version'],
'requires' => $theme['wp_min_version'],
'requires_php' => $theme['php_version'],
'url' => $theme['buy_url'],
'package' => $theme['download_url'],
];
}

// Cache themes.
hivepress()->cache->set_cache( $cache_key, null, $themes, HOUR_IN_SECONDS );
}
}

return $themes;
}

/**
* Gets plugins.
*
Expand All @@ -48,7 +116,7 @@ public function __construct( $args = [] ) {
protected function get_plugins() {

// Get license key.
$license_key = implode( ',', explode( "\n", get_option( 'hp_hivepress_license_key' ) ) );
$license_key = $this->get_license_key();

// Get cache key.
$cache_key = 'plugins_' . md5( $license_key );
Expand Down Expand Up @@ -108,13 +176,41 @@ protected function get_plugins() {
}

// Cache plugins.
hivepress()->cache->set_cache( $cache_key, null, $plugins, DAY_IN_SECONDS );
hivepress()->cache->set_cache( $cache_key, null, $plugins, HOUR_IN_SECONDS );
}
}

return $plugins;
}

/**
* Checks theme updates.
*
* @param object $transient Transient object.
* @return object
*/
public function check_theme_updates( $transient ) {
if ( ! empty( $transient->checked ) ) {

// Get themes.
$themes = $this->get_themes();

foreach ( $themes as $theme ) {

// Get version.
$version = hp\get_array_value( $transient->checked, $theme['theme'] );

if ( $version && version_compare( $version, $theme['new_version'], '<' ) ) {

// Add update.
$transient->response[ $theme['theme'] ] = $theme;
}
}
}

return $transient;
}

/**
* Checks plugin updates.
*
Expand Down

0 comments on commit 1fa0b5d

Please sign in to comment.