Commit d5f71967 authored by John James Jacoby's avatar John James Jacoby

Simplify updater code.

* Move into Flox updaters
* Move methods into base item class
* Plugin still more to do
parent 45c53436
This diff is collapsed.
This diff is collapsed.
<?php
/**
* The Theme Updater Class
* Flox Theme Updater Class
*
* This class talks back to store.flox.io to check for and serve theme updates
* to licensed customers who purchased Swifter.
* to licensed customers who purchased a Flox theme.
*
* As you can imagine, muting this is pretty easy. Hopefully people find enough
* value in Swifter to pay for updates to it.
* value to pay for updates to it.
*
* @package Swifter
* @since Swifter (1.0.0)
* @package Flox
*/
// Exit if accessed directly
......@@ -19,45 +18,45 @@ defined( 'ABSPATH' ) || exit;
/**
* Theme updater class
*/
class Swifter_Theme_Updater {
class Flox_Theme_Updater extends Flox_Licensed_Product {
private $license = '';
private $item_name = '';
private $version = '';
private $theme_slug = '';
private $author = '';
private $remote_api_url = '';
private $response_key = '';
/**
* @var string The license as entered by the user
*/
protected $license = null;
/**
* Construct & initialize the object
*
* @param array $args
*/
public function __construct( $args = array() ) {
// Parse arguments
$r = wp_parse_args( $args, array(
'remote_api_url' => 'https://store.flox.io',
'theme_slug' => '',
'item_name' => '',
'license' => '',
'version' => '',
'author' => ''
) );
parent::__construct( $args );
// Populate variables
$this->remote_api_url = $r['remote_api_url'];
$this->theme_slug = sanitize_key( $r['theme_slug'] );
$this->item_name = $r['item_name'];
$this->license = $r['license'];
$this->version = $r['version'];
$this->author = $r['author'];
$this->response_key = $this->theme_slug . '_update_response';
$this->license = $this->get_license();
// Filters
add_filter( 'site_transient_update_themes', array( $this, 'theme_update_transient' ) );
add_filter( 'delete_site_transient_update_themes', array( $this, 'delete_theme_update_transient' ) );
// Hooks
$this->init();
}
/**
* Hook into theme updater actions & filters
*/
private function init() {
// Actions
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' ) );
// Filters
add_filter( 'site_transient_update_themes', array( $this, 'theme_update_transient' ) );
add_filter( 'delete_site_transient_update_themes', array( $this, 'delete_theme_update_transient' ) );
// Disable wporg requests
add_filter( 'http_request_args', array( $this, 'disable_wporg_request' ), 5, 2 );
}
/** Public ****************************************************************/
......@@ -71,16 +70,16 @@ class Swifter_Theme_Updater {
add_thickbox();
// Prepare for theme update nag
add_action( 'admin_notices', array( &$this, 'update_nag' ) );
add_action( 'admin_notices', array( $this, 'update_nag' ) );
}
/**
* Maybe nag the user to update their Swifter theme
* Maybe nag the user to update their theme
*/
public function update_nag() {
// Check for response
$api_response = get_transient( $this->response_key );
$api_response = $this->get_update_status();
// Bail if no response
if ( false === $api_response ) {
......@@ -88,22 +87,22 @@ class Swifter_Theme_Updater {
}
// Bail if no new version
if ( ! version_compare( $this->version, $api_response->new_version, '<' ) ) {
if ( ! $this->update_available( $api_response ) ) {
return;
}
// Add theme upgrade arguments to update URL
$url = add_query_arg( array(
'action' => 'upgrade-theme',
'theme' => urlencode( $this->theme_slug )
'theme' => urlencode( $this->slug )
), 'update.php' );
// Setup variables for updating
$options = __( '<strong>%1$s %2$s</strong> is available. <a href="%5$s"%6$s>Update now</a> or <a href="%3$s" class="thickbox" title="%4s">check out what is new</a>.', 'swifter' );
$name = wp_get_theme( $this->theme_slug )->get( 'Name' );
$thickbox = '#TB_inline?width=640&amp;inlineId=' . $this->theme_slug . '_changelog';
$update_url = wp_nonce_url( $url, 'upgrade-theme_' . $this->theme_slug );
$reminder = __( 'Reminder: If you directly modified the code in this theme, your changes will be lost.', 'swifter' );
$options = __( '<strong>%1$s %2$s</strong> is available. <a href="%5$s"%6$s>Update now</a> or <a href="%3$s" class="thickbox" title="%4s">check out what is new</a>.', 'flox' );
$name = wp_get_theme( $this->slug )->get( 'Name' );
$thickbox = '#TB_inline?width=640&amp;inlineId=' . $this->slug . '_changelog';
$update_url = wp_nonce_url( $url, 'upgrade-theme_' . $this->slug );
$reminder = __( 'Reminder: If you directly modified the code in this theme, your changes will be lost.', 'flox' );
$update_onclick = ' onclick="if ( confirm(\'' . esc_js( $reminder ) . '\') ) {return true;}return false;"';
// Start the buffer
......@@ -123,7 +122,7 @@ class Swifter_Theme_Updater {
</div>
<div id="<?php echo esc_attr( $this->theme_slug ); ?>_changelog" style="display:none;">
<div id="<?php echo esc_attr( $this->slug ); ?>_changelog" style="display:none;">
<?php
......@@ -131,7 +130,7 @@ class Swifter_Theme_Updater {
if ( ! empty( $api_response->sections['changelog'] ) ) :
echo wpautop( $api_response->sections['changelog'] );
else :
esc_html_e( 'The changlog for this update is missing.', 'swifter' );
esc_html_e( 'The changlog for this update is missing.', 'flox' );
endif;
?>
......@@ -150,10 +149,10 @@ class Swifter_Theme_Updater {
* @param array $value
* @return array
*/
public function theme_update_transient( $value ) {
public function theme_update_transient( $value = false ) {
$update_data = $this->check_for_update();
if ( ! empty( $update_data ) ) {
$value->response[ $this->theme_slug ] = $update_data;
if ( false !== $update_data ) {
$value->response[ $this->slug ] = $update_data;
}
return $value;
}
......@@ -162,78 +161,32 @@ class Swifter_Theme_Updater {
* Delete the Swifter theme update transient
*/
public function delete_theme_update_transient() {
delete_transient( $this->response_key );
$this->set_update_transient( false );
}
/** Private ***************************************************************/
/**
* Check store.flox.io for available update
*
* @return boolean
* Disable requests to wp.org repository for this theme.
*/
private function check_for_update() {
// Look for API response
$update_data = false; //get_transient( $this->response_key );
// No response, so ping the API
if ( false === $update_data ) {
$failed = false;
$api_params = array(
'edd_action' => 'get_version',
'license' => $this->license,
'name' => $this->item_name,
'slug' => $this->theme_slug,
'author' => $this->author
);
// Ping the API
$response = wp_remote_post( $this->remote_api_url, array(
'timeout' => 5,
'body' => $api_params
) );
// Make sure the response was successful
if ( is_wp_error( $response ) || ( 200 !== wp_remote_retrieve_response_code( $response ) ) ) {
$failed = true;
}
// Decode the response body
$update_data = json_decode( wp_remote_retrieve_body( $response ) );
// Failure if not an object
if ( ! is_object( $update_data ) ) {
$failed = true;
}
// If the response failed, try again in 30 minutes
if ( true === $failed ) {
// Setup basic version data
$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 ( false === $failed ) {
$update_data->sections = maybe_unserialize( $update_data->sections );
// Check again in 12 hours
set_transient( $this->response_key, $update_data, strtotime( '+12 hours' ) );
}
}
public function disable_wporg_request( $args = array(), $url = '' ) {
// Bail if no new version
if ( version_compare( $this->version, $update_data->new_version, '>=' ) ) {
return false;
}
// Bail if not WordPress.org theme update request
if ( 0 !== strpos( $url, 'https://api.wordpress.org/themes/update-check/1.1/' ) ) {
return $args;
}
// Decode the JSON response
$themes = json_decode( $args['body']['themes'] );
// Remove the active parent and child themes from the check
$parent = get_option( 'template' );
$child = get_option( 'stylesheet' );
unset( $themes->themes->$parent );
unset( $themes->themes->$child );
// Encode the updated JSON response
$args['body']['themes'] = json_encode( $themes );
// Return update data array
return (array) $update_data;
return $args;
}
}
<?php
/**
* Theme Updater
*
* This file ensures that theme update checks only occur when visiting
* WordPress's administration area.
*
* As you can imagine, muting this is pretty easy. Hopefully people find enough
* value in Swifter to pay for updates to it.
*
* @package Swifter
* @since Swifter (1.0.0)
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
// Bail if not in admin
if ( ! is_admin() ) {
return;
}
// Instantiate the theme updater
new Swifter_Theme_Updater_Admin(
array(
'remote_api_url' => 'https://store.flox.io/',
'item_name' => 'Swifter',
'theme_slug' => 'swifter',
'version' => '1.0.0',
'author' => 'The Flox Team',
'download_id' => '', // Optional, used for generating a license renewal link
'renew_url' => '' // Optional, allows for a custom license renewal link
)
);
......@@ -81,11 +81,13 @@ add_action( 'after_setup_theme', 'swifter_setup' );
// Current directory
$_inc_dir = dirname( __FILE__ ) . '/_inc/';
// Updater
require $_inc_dir . 'updater/admin.php';
// Flox Updaters
require $_inc_dir . 'updater/item.php';
require $_inc_dir . 'updater/theme.php';
require $_inc_dir . 'updater/plugins.php';
require $_inc_dir . 'updater/updater.php';
require $_inc_dir . 'updater/plugin.php';
// Theme Updater
require $_inc_dir . 'updater/admin.php';
// Require WordPress functions.
require $_inc_dir . 'wordpress/actions.php';
......@@ -131,3 +133,12 @@ if ( function_exists( 'bbpress' ) ) {
// Cleanup
unset( $_inc_dir );
// Setup the updater
new Swifter_Theme_Updater_Admin( array(
'remote_api_url' => 'https://store.flox.io/',
'name' => 'Swifter',
'slug' => 'swifter',
'version' => '1.0.0',
'author' => 'The Flox Team',
) );
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment