Created
September 4, 2012 14:12
-
-
Save gilbitron/3621514 to your computer and use it in GitHub Desktop.
WPUpdates Theme Updater Class (with Envato API verification)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
WPUpdates Theme Updater Class (with Envato API verification) | |
http://wp-updates.com | |
v1.1 | |
Example Usage: | |
require_once('wp-updates-theme.php'); | |
new WPUpdatesThemeUpdater( 'http://wp-updates.com/api/1/theme', 1, basename(get_template_directory()) ); | |
*/ | |
if( !class_exists('WPUpdatesThemeUpdater') ) { | |
class WPUpdatesThemeUpdater { | |
var $api_url; | |
var $theme_id; | |
var $theme_slug; | |
function __construct( $api_url, $theme_id, $theme_slug ) { | |
$this->api_url = $api_url; | |
$this->theme_id = $theme_id; | |
$this->theme_slug = $theme_slug; | |
add_filter( 'pre_set_site_transient_update_themes', array(&$this, 'check_for_update') ); | |
// This is for testing only! | |
set_site_transient('update_themes', null); | |
// Add notice that theme update is available | |
if( get_option( 'wpu_'. $this->theme_slug .'_update_available' ) ){ | |
add_action( 'admin_notices', array(&$this, 'update_notice') ); | |
} | |
} | |
function check_for_update( $transient ) { | |
if (empty($transient->checked)) return $transient; | |
/* Envato Verify Purchase */ | |
$envato_username = ''; // your envato username | |
$envato_api_key = ''; // your envato API key | |
$envato_purchase_code = get_option('envato_purchase_code'); // the purchase code setting in your theme options | |
$response = wp_remote_get( 'http://marketplace.envato.com/api/v3/'. $envato_username .'/'. $envato_api_key .'/verify-purchase:'. $envato_purchase_code .'.json' ); | |
if( is_wp_error( $response ) ) { | |
return $transient; | |
} else { | |
$json_data = json_decode($response['body'], true); | |
if(!isset($json_data['verify-purchase']['item_name'])){ | |
update_option( 'wpu_'. $this->theme_slug .'_update_available', true ); | |
return $transient; | |
} | |
} | |
/* End Envato Verify Purchase */ | |
$request_args = array( | |
'id' => $this->theme_id, | |
'slug' => $this->theme_slug, | |
'version' => $transient->checked[$this->theme_slug] | |
); | |
$request_string = $this->prepare_request( 'theme_update', $request_args ); | |
$raw_response = wp_remote_post( $this->api_url, $request_string ); | |
$response = null; | |
if( !is_wp_error($raw_response) && ($raw_response['response']['code'] == 200) ) | |
$response = unserialize($raw_response['body']); | |
if( !empty($response) ) // Feed the update data into WP updater | |
$transient->response[$this->theme_slug] = $response; | |
return $transient; | |
} | |
function prepare_request( $action, $args ) { | |
global $wp_version; | |
return array( | |
'body' => array( | |
'action' => $action, | |
'request' => serialize($args), | |
'api-key' => md5(home_url()) | |
), | |
'user-agent' => 'WordPress/'. $wp_version .'; '. home_url() | |
); | |
} | |
function update_notice(){ | |
global $pagenow; | |
// Change this to a theme setting page URL where users | |
// can enter their ThemeForest purchase code | |
$theme_settings_link = admin_url( 'themes.php' ); | |
if ( $pagenow == 'themes.php' || $pagenow == 'update-core.php' ) { | |
$theme_name = 'current'; | |
if( function_exists('wp_get_theme') ) $theme_name = '<strong>'. wp_get_theme() .'</strong>'; | |
echo '<div class="updated"> | |
<p>'. __( 'There is an update availble for the '. $theme_name .' theme, but your ThemeForest purchase code is invalid.' ) .' <a href="'. $theme_settings_link .'">'. __( 'Update settings' ) .'</a></p> | |
</div>'; | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Do we have to use your service to see what's inside : http://wp-updates.com/api/1/theme ?
Because it pretty hard to figure out how it's working without :)
Regards,
2F