Permalink
Browse files

Update the update functionality yet again. You can now download and i…

…nstall updates from the dashboard!
  • Loading branch information...
jayj committed Sep 7, 2011
1 parent 1b755e8 commit 70a4b295f8248bec6b57a09f25d39dd839a1df02
View
@@ -1,12 +1,5 @@
<?php
-/* debug function, REMOVE!! */
-function convert($size)
- {
- $unit=array('b','kb','mb','gb','tb','pb');
- return @round($size/pow(1024,($i=floor(log($size,1024)))),5).' '.$unit[$i];
- }
-
/**
* The functions file is used to initialize everything in the theme. It controls how the theme is loaded and
* sets up the supported features, default actions, and default filters. If making customizations, users
@@ -94,9 +87,11 @@ function cakifo_theme_setup() {
/* Load shortcodes file. */
require_once( trailingslashit( THEME_DIR ) . 'functions/shortcodes.php' );
- /* Load Theme Settings */
- if ( is_admin() )
+ /* Load Theme Settings and upgrade functionality */
+ if ( is_admin() ) {
require_once( trailingslashit( TEMPLATEPATH ) . 'functions/admin.php' );
+ require_once( trailingslashit( TEMPLATEPATH ) . 'functions/upgrade.php' );
+ }
/* Add theme support for WordPress features */
add_theme_support( 'post-formats', array( 'aside', 'video', 'gallery', 'quote', 'link', 'image', 'status', 'chat' ) );
@@ -170,9 +165,6 @@ function cakifo_theme_setup() {
/* wp_list_comments() arguments */
add_filter( "{$prefix}_list_comments_args" , 'cakifo_change_list_comments_args' );
- /* Theme update check */
- add_action( 'admin_notices', 'cakifo_update_notice' );
-
/* Custom logo */
add_filter( 'cakifo_site_title', 'cakifo_logo' );
@@ -572,15 +564,14 @@ function cakifo_disable_sidebars( $sidebars_widgets ) {
* @since 1.3
*/
function cakifo_content_width() {
- global $content_width;
-
+
$layout = theme_layouts_get_layout();
if ( current_theme_supports( 'theme-layouts' ) ) {
if ( 'layout-3c-l' == $layout || 'layout-3c-r' == $layout )
- $content_width = 490;
+ hybrid_set_content_width( 490 );
elseif ( 'layout-3c-c' == $layout )
- $content_width = 500;
+ hybrid_set_content_width( 500 );
}
}
@@ -912,76 +903,4 @@ function cakifo_http_url_grabber() {
return esc_url_raw( $matches[0] );
}
-/**
- * Check for theme updates
- *
- * @since 1.3
- */
-function cakifo_update_check() {
-
- // Send request to see if there's an update available
- $url = 'http://wpthemes.jayj.dk/themerss/cakifo.json';
- $cakifo_update = wp_remote_retrieve_body( wp_remote_get( $url ) );
-
- if ( empty( $cakifo_update ) )
- return false;
-
- // Decode the JSON object
- $update = json_decode( $cakifo_update, true );
-
- return array(
- 'title' => $update['items'][0]['title'],
- 'version' => $update['items'][0]['version'],
- 'link' => $update['items'][0]['link'],
- 'message' => $update['items'][0]['message'],
- 'childmessage' => $update['items'][0]['childmessage'],
- 'requires' => $update['items'][0]['requires'],
- );
-}
-
-/**
- * Display an update notice if there's a new version available
- *
- * @since 1.2
- */
-function cakifo_update_notice() {
-
- if ( current_user_can( 'update_themes' ) ) :
-
- $theme_data = get_theme_data( trailingslashit( TEMPLATEPATH ) . 'style.css' );
- $update = get_transient( 'cakifo-update-check' );
- $update_available = (boolean) get_transient( 'cakifo-update-available' );
-
- // Get a fresh result from the server
- if ( false === $update ) {
- $update = cakifo_update_check();
- set_transient( 'cakifo-update-check', $update, 60*60*48 ); // 48 hours
- }
-
- // Is there a new version?
- if ( false === $update_available ) {
- if ( version_compare( $update['version'], $theme_data['Version'], '>' ) )
- $update_available = true;
- else
- $update_available = false;
-
- set_transient( 'cakifo-update-available', $update_available, 60*60*48 ); // 48 hours
- }
-
- // There's an update available
- if ( $update_available ) {
- echo '<div class="update-nag">';
- echo 'Version ' . esc_html( $update['version'] ) . ' for the Cakifo theme is available! ';
- echo '<a href="' . esc_url( $update['link'] ) . '">Click here to download the update</a> ';
- echo $update['message'];
- if ( is_child_theme() )
- echo '&nbsp;' . $update['childmessage'];
- echo '</div>';
- }
-
- endif; // current_user_can( 'update_themes' )
-
- return;
-}
-
?>
View
@@ -0,0 +1,161 @@
+<?php
+/**
+ * Handles the Cakifo upgrades
+ *
+ * The functions will check a server for information about
+ * a new version
+ *
+ * If there's a update, it will show a notice in the dashboard and
+ * allow you to upgrade directly from it
+ *
+ * @credits to the Genesis Framework for some of the code
+ */
+
+add_action( 'admin_notices', 'cakifo_update_notice' );
+add_filter( 'site_transient_update_themes', 'cakifo_update_push' );
+add_filter(' transient_update_themes', 'cakifo_update_push' );
+add_action( 'load-update.php', 'cakifo_clear_update_transient' );
+add_action( 'load-themes.php', 'cakifo_clear_update_transient' );
+
+/**
+ * Pings the server for information about the new version
+ *
+ * @since 1.3
+ * @return array List of information about the new update
+ */
+function cakifo_update_check() {
+
+ // Send request to see if there's an update available
+ $response = wp_remote_get( 'http://wpthemes.jayj.dk/themerss/cakifo.json' );
+
+ if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
+ return false;
+
+ $cakifo_update = wp_remote_retrieve_body( $response );
+
+ if ( empty( $cakifo_update ) )
+ return false;
+
+ // Decode the JSON object
+ $update = json_decode( $cakifo_update, true );
+
+ return array(
+ 'title' => $update['items'][0]['title'],
+ 'new_version' => $update['items'][0]['version'],
+ 'url' => $update['items'][0]['link'],
+ 'message' => $update['items'][0]['message'],
+ 'childmessage' => $update['items'][0]['childmessage'],
+ 'requires' => $update['items'][0]['requires'],
+ 'package' => $update['items'][0]['zip']
+ );
+}
+
+/**
+ * Checks if there's a new version available
+ *
+ * @since 1.3
+ * @return boolean true if there's an update, false if no
+ */
+function cakifo_update_available() {
+
+ $theme_data = hybrid_get_theme_data();
+ $update = get_transient( 'cakifo-update-check' );
+ $update_available = (boolean) get_transient( 'cakifo-update-available' );
+
+ // No transient, get a fresh result from the server
+ if ( false === $update ) {
+ $update = cakifo_update_check();
+ set_transient( 'cakifo-update-check', $update, 60*60*48 ); // 48 hours
+ }
+
+ // Is there a new version?
+ if ( false === $update_available ) {
+ if ( version_compare( $update['new_version'], $theme_data['Version'], '>' ) )
+ $update_available = true;
+ else
+ $update_available = false;
+
+ set_transient( 'cakifo-update-available', $update_available, 60*60*48 ); // 48 hours
+ }
+
+ return (boolean) $update_available;
+}
+
+/**
+ * Display the update notice
+ *
+ * @since 1.2
+ */
+function cakifo_update_notice() {
+
+ if ( current_user_can( 'update_themes' ) ) :
+
+ $update_available = cakifo_update_available();
+
+ // There's an update available
+ if ( $update_available ) :
+ $update = get_transient( 'cakifo-update-check' );
+ $update_url = wp_nonce_url( 'update.php?action=upgrade-theme&amp;theme=cakifo', 'upgrade-theme_cakifo' );
+ $update_onclick = __( 'Upgrading Cakifo will overwrite the current installed version of Cakifo. Are you sure you want to upgrade?. "Cancel" to stop, "OK" to upgrade.', hybrid_get_textdomain() );
+
+ // The notice
+ echo '<div class="update-nag">';
+ printf( __('Cakifo %s is available. <a href="%s">Check out what\'s new</a> or <a href="%s" onclick="return cakifo_confirm_upgrade(\'%s\');">update now</a>. ', hybrid_get_textdomain() ),
+ esc_html( $update['new_version'] ),
+ esc_url( $update['url'] ),
+ $update_url,
+ esc_js( $update_onclick )
+ );
+ echo $update['message'];
+ if ( is_child_theme() )
+ echo '&nbsp;' . $update['childmessage'];
+ echo '</div>';
+
+ // Add some inline script for upgrade alert
+ echo '<script type="text/javascript">function cakifo_confirm_upgrade( text ) {
+ var answer = confirm( text );
+ if( answer ) { return true; }
+ else { return false; }
+ }</script>';
+ endif;
+
+ endif; // current_user_can( 'update_themes' )
+
+ return;
+}
+
+/**
+ * This function clears out the Cakifo update transient data
+ * so that the server will do a fresh check, when the user
+ * loads certain admin pages.
+ *
+ * @since 1.3
+ */
+function cakifo_clear_update_transient() {
+ delete_transient( 'cakifo-update-check' );
+ delete_transient( 'cakifo-update-available' );
+ remove_action( 'admin_notices', 'cakifo_update_notice' );
+}
+
+/**
+ * This function filters the value that is returned when
+ * WordPress tries to pull theme update transient data. It uses
+ * cakifo_update_available() to check to see if we need to do an
+ * update, and if so, adds the proper array to the $value->response
+ * object. WordPress handles the rest.
+ *
+ * @since 1.3
+ */
+function cakifo_update_push( $value ) {
+
+ $update_available = cakifo_update_available();
+
+ if ( $update_available ) {
+ $update = get_transient( 'cakifo-update-check' );
+ $value->response['cakifo'] = $update;
+ }
+
+ return $value;
+}
+
+?>
@@ -17,12 +17,6 @@
*/
class Hybrid_Widget_Archives extends WP_Widget {
- /**
- * Prefix for the widget.
- * @since 0.7.0
- */
- var $prefix;
-
/**
* Textdomain for the widget.
* @since 0.7.0
@@ -31,12 +25,9 @@ class Hybrid_Widget_Archives extends WP_Widget {
/**
* Set up the widget's unique name, ID, class, description, and other options.
- * @since 0.6.0
+ * @since 1.2.0
*/
- function Hybrid_Widget_Archives() {
-
- /* Set the widget prefix. */
- $this->prefix = hybrid_get_prefix();
+ function __construct() {
/* Set the widget textdomain. */
$this->textdomain = hybrid_get_textdomain();
@@ -50,12 +41,16 @@ function Hybrid_Widget_Archives() {
/* Set up the widget control options. */
$control_options = array(
'width' => 525,
- 'height' => 350,
- 'id_base' => "{$this->prefix}-archives"
+ 'height' => 350
);
/* Create the widget. */
- $this->WP_Widget( "{$this->prefix}-archives", esc_attr__( 'Archives', $this->textdomain ), $widget_options, $control_options );
+ $this->WP_Widget(
+ 'hybrid-archives', // $this->id_base
+ __( 'Archives', $this->textdomain ), // $this->name
+ $widget_options, // $this->widget_options
+ $control_options // $this->control_options
+ );
}
/**
@@ -31,9 +31,9 @@ class Hybrid_Widget_Authors extends WP_Widget {
/**
* Set up the widget's unique name, ID, class, description, and other options.
- * @since 0.6.0
+ * @since 1.2.0
*/
- function Hybrid_Widget_Authors() {
+ function __construct() {
/* Set the widget prefix. */
$this->prefix = hybrid_get_prefix();
@@ -50,12 +50,16 @@ function Hybrid_Widget_Authors() {
/* Set up the widget control options. */
$control_options = array(
'width' => 525,
- 'height' => 350,
- 'id_base' => "{$this->prefix}-authors"
+ 'height' => 350
);
/* Create the widget. */
- $this->WP_Widget( "{$this->prefix}-authors", esc_attr__( 'Authors', $this->textdomain ), $widget_options, $control_options );
+ $this->WP_Widget(
+ 'hybrid-authors', // $this->id_base
+ __( 'Authors', $this->textdomain ), // $this->name
+ $widget_options, // $this->widget_options
+ $control_options // $this->control_options
+ );
}
/**
Oops, something went wrong.

0 comments on commit 70a4b29

Please sign in to comment.