Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 89d4b3397d48535064e759c5a23907a0261447e1 0 parents
@armanm armanm authored
228 envato-wordpress-theme-upgrader/class-envato-protected-api.php
@@ -0,0 +1,228 @@
+<?php
+/**
+ * Envato Protected API
+ *
+ * Wrapper class for the Envato marketplace protected API methods specific
+ * to the Envato WordPress Updater plugin.
+ *
+ * @package WordPress
+ * @subpackage Envato WordPress Updater
+ * @author Derek Herman <derek@envato.com>
+ * @since 1.0
+ */
+class Envato_Protected_API {
+ /**
+ * The buyer's Username
+ *
+ * @since 1.0
+ * @access public
+ *
+ * @var string
+ */
+ public $user_name;
+
+ /**
+ * The buyer's API Key
+ *
+ * @since 1.0
+ * @access public
+ *
+ * @var string
+ */
+ public $api_key;
+
+ /**
+ * The default API URL
+ *
+ * @since 1.0
+ * @access private
+ *
+ * @var string
+ */
+ protected $public_url = 'http://marketplace.envato.com/api/edge/set.json';
+
+ /**
+ * Error messages
+ *
+ * @since 1.0
+ * @access public
+ *
+ * @var array
+ */
+ public $errors = array( 'errors' => '' );
+
+ /**
+ * Class contructor method
+ *
+ * @param string The buyer's Username
+ * @param string The buyer's API Key can be accessed on the marketplaces via My Account -> My Settings -> API Key
+ * @return array|void Returns error messages if any, or void.
+ */
+ public function __construct( $user_name = '', $api_key = '' ) {
+ if ( $user_name == '' )
+ $this->set_error( 'user_name', __( 'Please enter your Envato Marketplace Username.', 'envato' ) );
+
+ if ( $api_key == '' )
+ $this->set_error( 'api_key', __( 'Please enter your Envato Marketplace API Key.', 'envato' ) );
+
+ $this->user_name = $user_name;
+ $this->api_key = $api_key;
+ }
+
+ /**
+ * Get private user data
+ *
+ * @since 1.0
+ * @access public
+ *
+ * @param string Available sets: 'vitals', 'earnings-and-sales-by-month', 'statement', 'recent-sales', 'account', 'verify-purchase', 'download-purchase', 'wp-list-themes', 'wp-download'
+ * @param string The buyer/author username to test against.
+ * @param string Additional set data such as purchase code or item id.
+ * @return array An array of values from the requested set, or an error message.
+ */
+ public function private_user_data( $set = '', $user_name = '', $set_data = '' ) {
+ if ( $set == '' )
+ $this->set_error( 'set', __( 'The API "set" is a required parameter.', 'envato' ) );
+
+ if ( $user_name == '' )
+ $user_name = $this->user_name;
+
+ if ( $set_data !== '' )
+ $set_data = ":$set_data";
+
+ $url = "http://marketplace.envato.com/api/edge/$user_name/$this->api_key/$set$set_data.json";
+
+ $result = $this->curl( $url );
+
+ if ( isset( $result->error ) )
+ $this->set_error( 'result', $result->error );
+
+ if ( $errors = $this->api_errors() )
+ return $errors;
+ else
+ return $result->$set;
+ }
+
+ /**
+ * Used to list purchased themes.
+ *
+ * @since 1.0
+ * @access public
+ *
+ * @return object If purchased themes, returns an object containing those details.
+ */
+ public function wp_list_themes() {
+ return $this->private_user_data( 'wp-list-themes', $this->user_name );
+ }
+
+ /**
+ * Used to download a purchased item.
+ *
+ * @since 1.0
+ * @access public
+ *
+ * @param string The purchased items id
+ * @return string|bool If item purchased, returns the download URL.
+ */
+ public function wp_download( $item_id ) {
+ if ( ! isset( $item_id ) )
+ $this->set_error( 'item_id', __( 'The Envato Marketplace "item ID" is a required parameter.', 'envato' ) );
+
+ $download = $this->private_user_data( 'wp-download', $this->user_name, $item_id );
+
+ if ( $errors = $this->api_errors() )
+ return $errors;
+ else
+ return isset( $download->url ) ? $download->url : false;
+ }
+
+ /**
+ * Retrieve the details for a specific marketplace item.
+ *
+ * @since 1.0
+ * @access public
+ *
+ * @param string $item_id The id of the item you need information for.
+ * @return object Details for the given item.
+ */
+ public function item_details( $item_id ) {
+ $url = preg_replace( '/set/i', 'item:' . $item_id, $this->public_url );
+ return $this->curl( $url )->item;
+ }
+
+ /**
+ * Helper function to set error messages.
+ *
+ * @since 1.0
+ * @access private
+ *
+ * @param string The error array id.
+ * @param string The error message.
+ * @return void
+ */
+ public function set_error( $id, $error ) {
+ $this->errors['errors'][$id] = $error;
+ }
+
+ /**
+ * Helper function to return errors.
+ *
+ * @since 1.0
+ * @access private
+ *
+ * @return array The errors array.
+ */
+ public function api_errors() {
+ if ( ! empty( $this->errors['errors'] ) )
+ return $this->errors['errors'];
+ }
+
+ /**
+ * Helper function to query the marketplace API via CURL.
+ *
+ * @since 1.0
+ * @access private
+ *
+ * @param string The url to access.
+ * @return object The results of the curl request.
+ */
+ protected function curl( $url ) {
+ if ( ! isset( $url ) )
+ return false;
+
+ $ch = curl_init( $url );
+ curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
+
+ $data = curl_exec( $ch );
+ $info = curl_getinfo( $ch );
+ curl_close( $ch );
+
+ $data = json_decode( $data );
+
+ if ( $info['http_code'] == 200 )
+ return $data;
+ else
+ $this->set_error( 'http_code', $info['http_code'] );
+
+ if ( isset( $data->error ) )
+ $this->set_error( 'api_error', $data->error );
+ }
+
+ /**
+ * Helper function to print arrays to the screen
+ *
+ * @since 1.0
+ * @access public
+ *
+ * @param array The array to print out
+ * @return string
+ */
+ public function pretty_print( $array ) {
+ echo '<pre>';
+ print_r( $array );
+ echo '</pre>';
+ }
+}
+
+/* End of file api.php */
+/* Location: ./includes/api.php */
380 envato-wordpress-theme-upgrader/class-envato-wordpress-theme-upgrader.php
@@ -0,0 +1,380 @@
+<?php
+
+/**
+ * Envato Theme Upgrader class to extend the WordPress Theme_Upgrader class.
+ *
+ * @package Envato WordPress Updater
+ * @author Arman Mirkazemi, Derek Herman <derek@valendesigns.com>
+ * @since 1.0
+ */
+
+include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
+include_once( 'class-envato-protected-api.php' );
+
+if ( class_exists( 'Theme_Upgrader' ) && ! class_exists( 'Envato_WordPress_Theme_Upgrader' ) ) {
+
+ /**
+ * Envato Wordpress Theme Upgrader class to extend the WordPress Theme_Upgrader class.
+ *
+ * @package Envato WordPress Theme Upgrader Library
+ * @author Derek Herman <derek@valendesigns.com>, Arman Mirkazemi
+ * @since 1.0
+ */
+ class Envato_WordPress_Theme_Upgrader extends Theme_Upgrader
+ {
+ protected $api_key;
+ protected $username;
+ protected $api;
+ protected $installation_feedback;
+
+ public function __construct( $username, $api_key )
+ {
+ parent::__construct(new Envato_Theme_Installer_Skin($this));
+
+ $this->constants();
+
+ $this->installation_feedback = array();
+ $this->username = $username;
+ $this->api_key = $api_key;
+ $this->api = &new Envato_Protected_API( $this->username, $this->api_key );
+ }
+
+ /**
+ * Checks for theme updates on ThemeForest marketplace
+ *
+ * @since 1.0
+ * @access public
+ *
+ * @param string (optional) Name of the theme. If not set checks for updates for the current theme
+ * @return array A stdClass object.
+ */
+ public function check_for_theme_update( $theme_name = '' )
+ {
+ $result = new stdClass();
+ $purchased_themes = $this->api->wp_list_themes();
+
+ if ( $errors = $this->api->api_errors() )
+ {
+ $result->errors = array();
+ foreach( $errors as $k => $v ) {
+ array_push( $result->errors , $v);
+ }
+
+ return $result;
+ }
+
+ if ( empty($theme_name) ) {
+ $theme_name = get_current_theme();
+ }
+
+ $purchased_themes = $this->filter_purchased_themes_by_name($purchased_themes, $theme_name);
+ $result->updated_themes = $this->get_updated_themes(get_themes(), $purchased_themes);
+ $result->updated_themes_count = count($result->updated_themes);
+
+ return $result;
+ }
+
+ /**
+ * Upgrades theme to its latest version
+ *
+ * @since 1.0
+ * @access public
+ *
+ * @param string (optional) Name of the theme. If not set upgrades the current theme.
+ * @return array A stdClass object.
+ */
+ public function upgrade_theme( $theme_name = '' )
+ {
+ $result = new stdClass();
+ $result->success = false;
+
+ if ( empty($theme_name) ) {
+ $theme_name = get_current_theme();
+ }
+
+ $installed_theme = $this->is_theme_installed($theme_name);
+
+ if ($installed_theme == null) {
+ $result->errors = array("'$theme_name' theme is not installed");
+ return $result;
+ }
+
+ $purchased_themes = $this->api->wp_list_themes();
+ $marketplace_theme_data = null;
+
+ if ( $errors = $this->api->api_errors() )
+ {
+ $result->errors = array();
+ foreach( $errors as $k => $v ) {
+ array_push( $result->errors , $v);
+ }
+
+ return $result;
+ }
+
+ foreach( $purchased_themes as $purchased ) {
+ if ( $this->is_matching_themes( $installed_theme, $purchased ) && $this->is_newer_version_available( $installed_theme['Version'], $purchased->version ) ) {
+ $marketplace_theme_data = $purchased;
+ break;
+ }
+ }
+
+ if ( $marketplace_theme_data == null ) {
+ $result->errors = array( "There is no update available for '$theme_name'" );
+ return $result;
+ }
+
+ $result->success = $this->do_upgrade_theme( $installed_theme['Title'], $marketplace_theme_data->item_id);
+ $result->installation_feedback = $this->installation_feedback;
+
+ return $result;
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Void.
+ */
+ public function set_installation_message($message)
+ {
+ $this->installation_feedback[] = $message;
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Array.
+ */
+ protected function filter_purchased_themes_by_name( $all_purchased_themes, $theme_name )
+ {
+ $result = $all_purchased_themes;
+
+ if ( empty($theme_name) )
+ return $result;
+
+ for ( $i = count($result) - 1; $i >= 0; $i-- ) {
+ $entry = $result[$i];
+ if ( $entry->theme_name != $theme_name )
+ unset($result[$i]);
+ }
+
+ return $result;
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Void.
+ */
+ protected function constants()
+ {
+ define( 'ETU_MAX_EXECUTION_TIME' , 60 * 5);
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Array.
+ */
+ protected function get_updated_themes($installed_themes, $purchased_themes)
+ {
+ $result = array();
+
+ if ( count( $purchased_themes ) <= 0 ) {
+ return $result;
+ }
+
+ foreach( $purchased_themes as $purchased )
+ {
+ foreach( $installed_themes as $installed => $installed_theme )
+ {
+ if ( $this->is_matching_themes( $installed_theme, $purchased ) && $this->is_newer_version_available( $installed_theme['Version'], $purchased->version ) )
+ {
+ $installed_theme['envato-theme'] = $purchased;
+ array_push($result, $installed_theme);
+ }
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Boolean.
+ */
+ protected function is_matching_themes($installed_theme, $purchased_theme)
+ {
+ return $installed_theme['Title'] == $purchased_theme->theme_name AND $installed_theme['Author Name'] == $purchased_theme->author_name;
+ }
+
+ protected function is_newer_version_available($installed_vesion, $latest_version)
+ {
+ return version_compare($installed_vesion, $latest_version, '<');
+ }
+
+ protected function is_theme_installed($theme_name)
+ {
+ $installed_themes = get_themes();
+ foreach($installed_themes as $entry)
+ {
+ if (strcmp($entry['Name'], $theme_name) == 0) {
+ return $entry;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Boolean.
+ */
+ protected function do_upgrade_theme( $installed_theme_name, $marketplace_theme_id )
+ {
+ $result = false;
+ $callback = array( &$this , '_http_request_args' );
+
+ add_filter( 'http_request_args', $callback, 10, 1 );
+ $result = $this->upgrade( $installed_theme_name, $this->api->wp_download( $marketplace_theme_id ) );
+ remove_filter( 'http_request_args', $callback );
+
+ return $result;
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Array.
+ */
+ public function _http_request_args($r)
+ {
+ if ((int)ini_get("max_execution_time") < ETU_MAX_EXECUTION_TIME)
+ {
+ ini_set("max_execution_time", ETU_MAX_EXECUTION_TIME);
+ }
+
+ $r['timeout'] = ETU_MAX_EXECUTION_TIME;
+ return $r;
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Void.
+ */
+ public function upgrade_strings() {
+ parent::upgrade_strings();
+ $this->strings['downloading_package'] = __( 'Downloading upgrade package from the Envato API&#8230;', 'envato' );
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Void.
+ */
+ public function install_strings() {
+ parent::install_strings();
+ $this->strings['downloading_package'] = __( 'Downloading install package from the Envato API&#8230;', 'envato' );
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Boolean.
+ */
+ public function upgrade( $theme, $package ) {
+
+ $this->init();
+ $this->upgrade_strings();
+
+ $options = array(
+ 'package' => $package,
+ 'destination' => WP_CONTENT_DIR . '/themes',
+ 'clear_destination' => true,
+ 'clear_working' => true,
+ 'hook_extra' => array(
+ 'theme' => $theme
+ )
+ );
+
+ $this->run( $options );
+
+ if ( ! $this->result || is_wp_error($this->result) )
+ return $this->result;
+
+ return true;
+ }
+ }
+
+ /**
+ * Envato Theme Installer Skin class to extend the WordPress Theme_Installer_Skin class.
+ *
+ * @package Envato WordPress Theme Upgrader Library
+ * @author Arman Mirkazemi
+ * @since 1.0
+ */
+ class Envato_Theme_Installer_Skin extends Theme_Installer_Skin {
+
+ protected $envato_theme_updater;
+
+ function __construct($envato_theme_updater)
+ {
+ parent::__construct();
+ $this->envato_theme_updater = $envato_theme_updater;
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Void.
+ */
+ function feedback($string)
+ {
+ if ( isset( $this->upgrader->strings[$string] ) )
+ $string = $this->upgrader->strings[$string];
+
+ if ( strpos($string, '%') !== false ) {
+ $args = func_get_args();
+ $args = array_splice($args, 1);
+ if ( !empty($args) )
+ $string = vsprintf($string, $args);
+ }
+
+ if ( empty($string) )
+ return;
+
+ $this->envato_theme_updater->set_installation_message($string);
+ }
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Void.
+ */
+ function header(){}
+
+ /**
+ * @since 1.0
+ * @access internal
+ *
+ * @return array Void.
+ */
+ function footer(){}
+ }
+
+}
+
24 functions.php
@@ -0,0 +1,24 @@
+<?php
+
+// include the library
+include_once('envato-wordpress-theme-upgrader/class-envato-wordpress-theme-upgrader.php');
+
+// register an action (can be any suitable action)
+add_action('admin_init', 'on_admin_init');
+
+function on_admin_init()
+{
+ $upgrader = new Envato_WordPress_Theme_Upgrader( 'denonstudio', 'xxxxxxxav7hny3p1ptm7xxxxxxxx' );
+
+ /*
+ * Uncomment to check if the current theme has been updated
+ */
+
+ // $upgrader->check_for_theme_update();
+
+ /*
+ * Uncomment to updated the current theme
+ */
+
+ // $upgrader->upgrade_theme();
+}
13 index.php
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>This is not a real theme</title>
+ <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
+ </head>
+ <body>
+ <div id="wrap" >
+ <h1>This is not a real theme</h1>
+ </div>
+ </body>
+</html>
+
35 readme.txt
@@ -0,0 +1,35 @@
+=== Envato WordPress Theme Upgrader ===
+Contributors: Arman Mirkazemi
+Tags: update, envato, theme, api
+Requires at least: 3.0
+Tested up to: 3.3.1
+Stable tag: 1.0
+
+Envato WordPress Theme Upgrade utility for Envato Marketplace hosted themes.
+
+== Description ==
+
+This is an example theme to demonstrate usage of the Envato WordPress Theme Upgrader Library. This library is intended for ThemeForest authors to integrate with their WordPress themes, providing automatic update functionality.
+
+The upgrader has the following methods available:
+
+`check_for_theme_update`
+* Checks for theme updates on the ThemeForest marketplace
+
+`upgrade_theme`
+* Upgrades the theme to its latest version
+
+See Installation notes for further instructions.
+
+== Installation ==
+
+1. Copy the `envato-wordpress-theme-upgrader` folder into your theme folder
+2. View the contents of the `functions.php` for usage examples
+3. Customise the code in your theme to accept the user's ThemeForest Username and Secret API Key
+4. Instantiate the `Envato_WordPress_Theme_Upgrader` class and call its `check_for_theme_update` and `upgrade_theme` methods at appropriate times
+5. Your theme should now be able to check for updates and update itself!
+
+== Changelog ==
+
+= 1.0 =
+* Initial release.
BIN  screenshot.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 style.css
@@ -0,0 +1,21 @@
+/*
+
+Theme Name: Dummy Theme
+Theme URI: http://denonstudio.net
+Description: This is a dummy theme for testing Envato Theme Upgrader
+Version: 1.0
+Author: DENONSTUDIO
+Author URI: http://codecanyon.net/user/denonstudio
+License: GNU General Public License v2.0
+License URI: http://www.gnu.org/licenses/gpl-2.0.html
+
+Tags: dummy, envato, codecanyon, wordpress
+
+*/
+
+@import url(http://fonts.googleapis.com/css?family=Alfa+Slab+One);
+
+* { padding: 0; margin: 0; }
+body { background: #FFDE00; }
+#wrap { width: 800px; margin: 350px auto 0 auto; text-align: center; }
+#wrap h1 { font-size: 30pt; font-family: 'Alfa Slab One', arial, san-serif; text-transform: uppercase; color: #FFF; text-shadow: 0px 0px 10px #000; }
Please sign in to comment.
Something went wrong with that request. Please try again.