Skip to content

Commit

Permalink
Updating updater class
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed May 6, 2015
1 parent 3ca0635 commit e5901d0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
<?php
class EDD_SL_Theme_Updater {
/**
* Theme updater class.
*
* @package EDD Sample Theme
*/

class EDD_Theme_Updater {

private $remote_api_url;
private $request_data;
private $response_key;
private $theme_slug;
private $license_key;
private $version;
private $author;
protected $strings = null;

function __construct( $args = array(), $strings = array() ) {

function __construct( $args = array() ) {
$args = wp_parse_args( $args, array(
'remote_api_url' => 'http://easydigitaldownloads.com',
'request_data' => array(),
'theme_slug' => get_template(),
'item_name' => '',
'license' => '',
'version' => '',
'author' => ''
'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->license = $license;
$this->item_name = $item_name;
$this->version = $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';

$this->response_key = $this->theme_slug . '-update-response';
$this->strings = $strings;

add_filter( 'site_transient_update_themes', array( &$this, 'theme_update_transient' ) );
add_filter( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) );
Expand All @@ -43,30 +51,35 @@ function load_themes_screen() {
}

function update_nag() {

$strings = $this->strings;

$theme = wp_get_theme( $this->theme_slug );

$api_response = get_transient( $this->response_key );

if( false === $api_response )
if ( false === $api_response ) {
return;
}

$update_url = wp_nonce_url( 'update.php?action=upgrade-theme&amp;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;"';
$update_onclick = ' onclick="if ( confirm(\'' . esc_js( $strings['update-notice'] ) . '\') ) {return true;}return false;"';

if ( version_compare( $this->version, $api_response->new_version, '<' ) ) {

echo '<div id="update-nag">';
printf( '<strong>%1$s %2$s</strong> is available. <a href="%3$s" class="thickbox" title="%4s">Check out what\'s new</a> or <a href="%5$s"%6$s>update now</a>.',
$theme->get( 'Name' ),
$api_response->new_version,
'#TB_inline?width=640&amp;inlineId=' . $this->theme_slug . '_changelog',
$theme->get( 'Name' ),
$update_url,
$update_onclick
);
printf(
$strings['update-available'],
$theme->get( 'Name' ),
$api_response->new_version,
'#TB_inline?width=640&amp;inlineId=' . $this->theme_slug . '_changelog',
$theme->get( 'Name' ),
$update_url,
$update_onclick
);
echo '</div>';
echo '<div id="' . $this->theme_slug . '_' . 'changelog" style="display:none;">';
echo wpautop( $api_response->sections['changelog'] );
echo wpautop( $api_response->sections['changelog'] );
echo '</div>';
}
}
Expand All @@ -85,27 +98,22 @@ function delete_theme_update_transient() {

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()
'author' => $this->author
);

$response = wp_remote_post( $this->remote_api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
$response = wp_remote_post( $this->remote_api_url, array( 'timeout' => 15, 'body' => $api_params ) );

// make sure the response was successful
// Make sure the response was successful
if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
$failed = true;
}
Expand All @@ -116,15 +124,15 @@ function check_for_update() {
$failed = true;
}

// if the response failed, try again in 30 minutes
// 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 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' ) );
Expand All @@ -137,4 +145,5 @@ function check_for_update() {

return (array) $update_data;
}
}

}
4 changes: 2 additions & 2 deletions lib/updater/updater.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

if ( ! class_exists( 'EDD_SL_Theme_Updater' ) ) {
include( dirname( __FILE__ ) . '/EDD_SL_Theme_Updater.php' );
include( dirname( __FILE__ ) . '/EDD_Theme_Updater.php' );
}

function shoestrap_theme_updater() {

$edd_updater = new EDD_SL_Theme_Updater( array(
$edd_updater = new EDD_Theme_Updater( array(
'remote_api_url' => 'https://press.codes',
'version' => SHOESTRAP_VERSION,
'license' => 'c5305a091a9e61268c5be6096bfa3d38',
Expand Down

0 comments on commit e5901d0

Please sign in to comment.