Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 252aca87e1f5c205bf9a3dcd46478d55e48be451 @unisphere committed Apr 22, 2011
Showing with 204 additions and 0 deletions.
  1. +19 −0 LICENSE
  2. +32 −0 README.md
  3. +12 −0 notifier.xml
  4. +141 −0 update-notifier.php
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2011 João Araújo (http://themeforest.net/user/unisphere)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,32 @@
+# WordPress Theme Update Notifier
+
+This is a simple WordPress theme update notifier that will provide your theme Buyers with a notification every time you issue a theme update.
+
+It's a very simple script that requires and assumes you have an XML file in your own server that serves as an endpoint for the script to check what the latest version of the theme is in order to compare with the current version of the theme installed in the Client's server.
+
+## Installation
+
+Upload the **notifier.xml** file to your own server (the update notifier on the Client sites will check this file for the latest version)
+
+Copy the **update-notifier.php** file to the root of the theme.
+
+Edit your theme's **functions.php** file and include the **update-notifier.php** file with the following code:
+
+ require('update-notifier.php');
+
+Edit the **update-notifier.php** file and make the appropriate changes to the following constants:
+
+ define( 'NOTIFIER_THEME_NAME', 'JohnDoe' ); // The theme name
+ define( 'NOTIFIER_THEME_FOLDER_NAME', 'johndoe' ); // The theme folder name
+ define( 'NOTIFIER_XML_FILE', 'http://www.yourdomain.com/johndoe/notifier.xml' ); // The remote notifier XML file containing the latest version of the theme and changelog
+ define( 'NOTIFIER_CACHE_INTERVAL', 21600 ); // The time interval for the remote XML cache in the database (21600 seconds = 6 hours)
+
+Everything should be in place now. Every time you submit a theme update you should edit the XML file in your server and change the following code to the latest version you've updated:
+
+ <latest>1.0</latest>
+
+This way, your Clients will see an update notification on the WordPress admin panel informing them that there's a new version available and they should update.
+
+## Screenshot
+
+![theme update notifier](http://screencast.com/t/6613PkXAo)
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<notifier>
+ <latest>1.0</latest>
+ <changelog>
+<![CDATA[
+<h4>Version 1.0</h4>
+<ul>
+<li>Initial theme version.</li>
+</ul>
+]]>
+ </changelog>
+</notifier>
@@ -0,0 +1,141 @@
+<?php
+/**************************************************************
+ * *
+ * Provides a notification to the user everytime *
+ * your WordPress theme is updated *
+ * *
+ * Author: Jo‹o Araœjo *
+ * Profile: http://themeforest.net/user/unisphere *
+ * Follow me: http://twitter.com/unispheredesign *
+ * *
+ **************************************************************/
+
+
+
+// Constants for the theme name, folder and remote XML url
+define( 'NOTIFIER_THEME_NAME', 'JohnDoe' ); // The theme name
+define( 'NOTIFIER_THEME_FOLDER_NAME', 'johndoe' ); // The theme folder name
+define( 'NOTIFIER_XML_FILE', 'http://www.yourdomain.com/johndoe/notifier.xml' ); // The remote notifier XML file containing the latest version of the theme and changelog
+define( 'NOTIFIER_CACHE_INTERVAL', 21600 ); // The time interval for the remote XML cache in the database (21600 seconds = 6 hours)
+
+
+
+// Adds an update notification to the WordPress Dashboard menu
+function update_notifier_menu() {
+ if (function_exists('simplexml_load_string')) { // Stop if simplexml_load_string funtion isn't available
+ $xml = get_latest_theme_version(NOTIFIER_CACHE_INTERVAL); // Get the latest remote XML file on our server
+ $theme_data = get_theme_data(TEMPLATEPATH . '/style.css'); // Read theme current version from the style.css
+
+ if( (float)$xml->latest > (float)$theme_data['Version']) { // Compare current theme version with the remote XML version
+ add_dashboard_page( NOTIFIER_THEME_NAME . ' Theme Updates', NOTIFIER_THEME_NAME . ' <span class="update-plugins count-1"><span class="update-count">New Updates</span></span>', 'administrator', 'theme-update-notifier', 'update_notifier');
+ }
+ }
+}
+add_action('admin_menu', 'update_notifier_menu');
+
+
+
+// Adds an update notification to the WordPress 3.1+ Admin Bar
+function update_notifier_bar_menu() {
+ if (function_exists('simplexml_load_string')) { // Stop if simplexml_load_string funtion isn't available
+ global $wp_admin_bar, $wpdb;
+
+ if ( !is_super_admin() || !is_admin_bar_showing() ) // Don't display notification in admin bar if it's disabled or the current user isn't an administrator
+ return;
+
+ $xml = get_latest_theme_version(NOTIFIER_CACHE_INTERVAL); // Get the latest remote XML file on our server
+ $theme_data = get_theme_data(TEMPLATEPATH . '/style.css'); // Read theme current version from the style.css
+
+ if( (float)$xml->latest > (float)$theme_data['Version']) { // Compare current theme version with the remote XML version
+ $wp_admin_bar->add_menu( array( 'id' => 'update_notifier', 'title' => '<span>' . NOTIFIER_THEME_NAME . ' <span id="ab-updates">New Updates</span></span>', 'href' => get_admin_url() . 'index.php?page=theme-update-notifier' ) );
+ }
+ }
+}
+add_action( 'admin_bar_menu', 'update_notifier_bar_menu', 1000 );
+
+
+
+// The notifier page
+function update_notifier() {
+ $xml = get_latest_theme_version(NOTIFIER_CACHE_INTERVAL); // Get the latest remote XML file on our server
+ $theme_data = get_theme_data(TEMPLATEPATH . '/style.css'); // Read theme current version from the style.css ?>
+
+ <style>
+ .update-nag { display: none; }
+ #instructions {max-width: 670px;}
+ h3.title {margin: 30px 0 0 0; padding: 30px 0 0 0; border-top: 1px solid #ddd;}
+ </style>
+
+ <div class="wrap">
+
+ <div id="icon-tools" class="icon32"></div>
+ <h2><?php echo NOTIFIER_THEME_NAME ?> Theme Updates</h2>
+ <div id="message" class="updated below-h2"><p><strong>There is a new version of the <?php echo NOTIFIER_THEME_NAME; ?> theme available.</strong> You have version <?php echo $theme_data['Version']; ?> installed. Update to version <?php echo $xml->latest; ?>.</p></div>
+
+ <img style="float: left; margin: 0 20px 20px 0; border: 1px solid #ddd;" src="<?php echo get_bloginfo( 'template_url' ) . '/screenshot.png'; ?>" />
+
+ <div id="instructions">
+ <h3>Update Download and Instructions</h3>
+ <p><strong>Please note:</strong> make a <strong>backup</strong> of the Theme inside your WordPress installation folder <strong>/wp-content/themes/<?php echo NOTIFIER_THEME_FOLDER_NAME; ?>/</strong></p>
+ <p>To update the Theme, login to <a href="http://www.themeforest.net/">ThemeForest</a>, head over to your <strong>downloads</strong> section and re-download the theme like you did when you bought it.</p>
+ <p>Extract the zip's contents, look for the extracted theme folder, and after you have all the new files upload them using FTP to the <strong>/wp-content/themes/<?php echo NOTIFIER_THEME_FOLDER_NAME; ?>/</strong> folder overwriting the old ones (this is why it's important to backup any changes you've made to the theme files).</p>
+ <p>If you didn't make any changes to the theme files, you are free to overwrite them with the new ones without the risk of losing theme settings, pages, posts, etc, and backwards compatibility is guaranteed.</p>
+ </div>
+
+ <h3 class="title">Changelog</h3>
+ <?php echo $xml->changelog; ?>
+
+ </div>
+
+<?php }
+
+
+
+// Get the remote XML file contents and return its data (Version and Changelog)
+// Uses the cached version if available and inside the time interval defined
+function get_latest_theme_version($interval) {
+ $notifier_file_url = NOTIFIER_XML_FILE;
+ $db_cache_field = 'notifier-cache';
+ $db_cache_field_last_updated = 'notifier-cache-last-updated';
+ $last = get_option( $db_cache_field_last_updated );
+ $now = time();
+ // check the cache
+ if ( !$last || (( $now - $last ) > $interval) ) {
+ // cache doesn't exist, or is old, so refresh it
+ if( function_exists('curl_init') ) { // if cURL is available, use it...
+ $ch = curl_init($notifier_file_url);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_HEADER, 0);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 10);
+ $cache = curl_exec($ch);
+ curl_close($ch);
+ } else {
+ $cache = file_get_contents($notifier_file_url); // ...if not, use the common file_get_contents()
+ }
+
+ if ($cache) {
+ // we got good results
+ update_option( $db_cache_field, $cache );
+ update_option( $db_cache_field_last_updated, time() );
+ }
+ // read from the cache file
+ $notifier_data = get_option( $db_cache_field );
+ }
+ else {
+ // cache file is fresh enough, so read from it
+ $notifier_data = get_option( $db_cache_field );
+ }
+
+ // Let's see if the $xml data was returned as we expected it to.
+ // If it didn't, use the default 1.0 as the latest version so that we don't have problems when the remote server hosting the XML file is down
+ if( strpos((string)$notifier_data, '<notifier>') === false ) {
+ $notifier_data = '<?xml version="1.0" encoding="UTF-8"?><notifier><latest>1.0</latest><changelog></changelog></notifier>';
+ }
+
+ // Load the remote XML data into a variable and return it
+ $xml = simplexml_load_string($notifier_data);
+
+ return $xml;
+}
+
+?>

0 comments on commit 252aca8

Please sign in to comment.