From 54a9350500fe8bf146c10219a565aaa23e2b05b3 Mon Sep 17 00:00:00 2001 From: Aristeides Stathopoulos Date: Mon, 31 Mar 2014 19:37:37 +0300 Subject: [PATCH] Updater + Version 3.2.2 --- functions.php | 26 +---- lib/updater/EDD_SL_Theme_Updater.php | 140 +++++++++++++++++++++++++++ lib/updater/updater.php | 51 ++++++++++ style.css | 2 +- 4 files changed, 196 insertions(+), 23 deletions(-) create mode 100755 lib/updater/EDD_SL_Theme_Updater.php create mode 100644 lib/updater/updater.php diff --git a/functions.php b/functions.php index 999744d287..f73239d123 100644 --- a/functions.php +++ b/functions.php @@ -53,6 +53,10 @@ require_once locate_template( '/lib/class-TGM_Plugin_Activation.php' ); // TGM_Plugin_Activation require_once locate_template( '/lib/dependencies.php' ); // load our dependencies +// Setup our custom updater +if ( file_exists( locate_template( '/lib/updater/updater.php' ) ) ) { + require_once locate_template( '/lib/updater/updater.php' ); +} if ( class_exists( 'bbPress' ) ) { require_once locate_template( '/lib/bbpress.php' ); // Scripts and stylesheets @@ -60,28 +64,6 @@ do_action( 'shoestrap_include_files' ); -function shoestrap_core_updater() { - - $args = array( - 'remote_api_url' => 'http://shoestrap.org', - 'item_name' => 'Shoestrap 3', - 'license' => 'c5305a091a9e61268c5be6096bfa3d38', - 'version' => '3.2.1', - 'author' => 'aristath, fovoc, dovy', - 'mode' => 'theme', - 'title' => 'Shoestrap Theme License', - 'field_name' => 'shoestrap_theme_license', - 'description' => 'The Shoestrap theme already contains a pre-defined license number to allow auto-updating itself.', - 'single_license' => true - ); - - if ( class_exists( 'SS_EDD_SL_Updater' ) ) { - $updater = new SS_EDD_SL_Updater( $args ); - } - -} -add_action( 'admin_init', 'shoestrap_core_updater' ); - /* * Notice for Shoestrap Updater */ diff --git a/lib/updater/EDD_SL_Theme_Updater.php b/lib/updater/EDD_SL_Theme_Updater.php new file mode 100755 index 0000000000..a8d205425d --- /dev/null +++ b/lib/updater/EDD_SL_Theme_Updater.php @@ -0,0 +1,140 @@ + 'http://easydigitaldownloads.com', + 'request_data' => array(), + 'theme_slug' => get_template(), + 'item_name' => '', + 'license' => '', + 'version' => '', + 'author' => '' + ) ); + extract( $args ); + + $theme = wp_get_theme( sanitize_key( $theme_slug ) ); + $this->license = $license; + $this->item_name = $item_name; + $this->version = ! empty( $version ) ? $version : $theme->get( 'Version' ); + $this->theme_slug = sanitize_key( $theme_slug ); + $this->author = $author; + $this->remote_api_url = $remote_api_url; + $this->response_key = $this->theme_slug . '-update-response'; + + + add_filter( 'site_transient_update_themes', array( &$this, 'theme_update_transient' ) ); + add_filter( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) ); + add_action( 'load-update-core.php', array( &$this, 'delete_theme_update_transient' ) ); + add_action( 'load-themes.php', array( &$this, 'delete_theme_update_transient' ) ); + add_action( 'load-themes.php', array( &$this, 'load_themes_screen' ) ); + } + + function load_themes_screen() { + add_thickbox(); + add_action( 'admin_notices', array( &$this, 'update_nag' ) ); + } + + function update_nag() { + $theme = wp_get_theme( $this->theme_slug ); + + $api_response = get_transient( $this->response_key ); + + if( false === $api_response ) + return; + + $update_url = wp_nonce_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $this->theme_slug ), 'upgrade-theme_' . $this->theme_slug ); + $update_onclick = ' onclick="if ( confirm(\'' . esc_js( __( "Updating this theme will lose any customizations you have made. 'Cancel' to stop, 'OK' to update." ) ) . '\') ) {return true;}return false;"'; + + if ( version_compare( $this->version, $api_response->new_version, '<' ) ) { + + echo '
'; + printf( '%1$s %2$s is available. Check out what\'s new or update now.', + $theme->get( 'Name' ), + $api_response->new_version, + '#TB_inline?width=640&inlineId=' . $this->theme_slug . '_changelog', + $theme->get( 'Name' ), + $update_url, + $update_onclick + ); + echo '
'; + echo ''; + } + } + + function theme_update_transient( $value ) { + $update_data = $this->check_for_update(); + if ( $update_data ) { + $value->response[ $this->theme_slug ] = $update_data; + } + return $value; + } + + function delete_theme_update_transient() { + delete_transient( $this->response_key ); + } + + function check_for_update() { + + $theme = wp_get_theme( $this->theme_slug ); + + $update_data = get_transient( $this->response_key ); + if ( false === $update_data ) { + $failed = false; + + if( empty( $this->license ) ) + return false; + + $api_params = array( + 'edd_action' => 'get_version', + 'license' => $this->license, + 'name' => $this->item_name, + 'slug' => $this->theme_slug, + 'author' => $this->author, + 'url' => home_url() + ); + + $response = wp_remote_post( $this->remote_api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) ); + + // make sure the response was successful + if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) { + $failed = true; + } + + $update_data = json_decode( wp_remote_retrieve_body( $response ) ); + + if ( ! is_object( $update_data ) ) { + $failed = true; + } + + // if the response failed, try again in 30 minutes + if ( $failed ) { + $data = new stdClass; + $data->new_version = $this->version; + set_transient( $this->response_key, $data, strtotime( '+30 minutes' ) ); + return false; + } + + // if the status is 'ok', return the update arguments + if ( ! $failed ) { + $update_data->sections = maybe_unserialize( $update_data->sections ); + set_transient( $this->response_key, $update_data, strtotime( '+12 hours' ) ); + } + } + + if ( version_compare( $this->version, $update_data->new_version, '>=' ) ) { + return false; + } + + return (array) $update_data; + } +} \ No newline at end of file diff --git a/lib/updater/updater.php b/lib/updater/updater.php new file mode 100644 index 0000000000..a6cdd4d9c9 --- /dev/null +++ b/lib/updater/updater.php @@ -0,0 +1,51 @@ + 'http://shoestrap.org', + 'version' => '3.2.2', + 'license' => 'c5305a091a9e61268c5be6096bfa3d38', + 'item_name' => 'Shoestrap 3', + 'author' => 'aristath, fovoc, dovyp' + ) + ); +} +add_action( 'admin_init', 'shoestrap_theme_updater' ); + + +function shoestrap_theme_activate_license() { + global $wp_version; + + // If the license is valid there's no need to process this further. + if ( get_transient( 'shoestrap_theme_license_status' ) == 'valid' ) { + return; + } + + $api_params = array( + 'edd_action' => 'activate_license', + 'license' => 'c5305a091a9e61268c5be6096bfa3d38', + 'item_name' => urlencode( 'Shoestrap 3' ) + ); + + // Get the server response + $response = wp_remote_get( add_query_arg( $api_params, 'http://shoestrap.org' ), array( 'timeout' => 15, 'sslverify' => false ) ); + + // Make sure no error has occured + if ( is_wp_error( $response ) ) { + return false; + } + + // Get the license data + $license_data = json_decode( wp_remote_retrieve_body( $response ) ); + + if ( 'valid' == $license_data->license ) { + // Set a 24-hour transient. + set_transient( 'shoestrap_theme_license_status', $license_data->license, 24 * 60 * 60 ); + } +} +add_action('admin_init', 'shoestrap_theme_activate_license'); diff --git a/style.css b/style.css index 4465f9dab5..a10edf5b73 100644 --- a/style.css +++ b/style.css @@ -2,7 +2,7 @@ Theme Name: Shoestrap 3 Theme URI: http://shoestrap.org Description: Advanced WordPress theme based on Bootstrap. -Version: 3.2.1 +Version: 3.2.2 Author: aristath, fovoc, dovyp Author URI: http://wpmu.io License: GPL v3 License