Permalink
Browse files

Intial version of self hosted plugin and theme update scripts

  • Loading branch information...
0 parents commit ac1e038493c1fb1da8c05184b82fbca0bf26e57a @jeremyclark13 committed Apr 29, 2011
Showing with 286 additions and 0 deletions.
  1. +1 −0 api/.htaccess
  2. +147 −0 api/index.php
  3. +102 −0 plugin/test-plugin-update/test-plugin-update.php
  4. +27 −0 readme.txt
  5. +9 −0 theme/update.php
@@ -0,0 +1 @@
+Options +Indexes
@@ -0,0 +1,147 @@
+<?php
+/*******
+ Original Plugin & Theme API by Kaspars Dambis (kaspars@konstruktors.com)
+ Modified by Jeremy Clark http://clark-technet.com
+*******/
+
+//Kill magic quotes. Can't unserialize POST variable otherwise
+if (get_magic_quotes_gpc()) {
+ $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
+ while (list($key, $val) = each($process)) {
+ foreach ($val as $k => $v) {
+ unset($process[$key][$k]);
+ if (is_array($v)) {
+ $process[$key][stripslashes($k)] = $v;
+ $process[] = &$process[$key][stripslashes($k)];
+ } else {
+ $process[$key][stripslashes($k)] = stripslashes($v);
+ }
+ }
+ }
+ unset($process);
+}
+
+// Pull user agent set up by update.php in theme or plugin and continue if is set to Wordpress
+$user_agent = $_SERVER['HTTP_USER_AGENT'];
+if (stristr($user_agent, 'WordPress') == TRUE){
+
+ // Theme with update info
+ $packages['theme'] = array( //Replace theme with theme stylesheet slug that the update is for
+ 'versions' => array(
+ '1.0' => array( //Array name should be set to current version of update
+ 'version' => '1.0', //Current version available
+ 'date' => '2010-04-10', //Date version was released
+ 'package' => 'http://url_to_your_site/theme.zip' // The zip file of the theme update
+ )
+ ),
+ 'info' => array(
+ 'url' => 'http://url_to_your_theme_site' // Website devoted to theme if available
+ )
+ );
+
+ // Plugin with update info
+ $packages['plugin'] = array( //Replace plugin with the plugin slug that updates will be checking for
+ 'versions' => array(
+ '1.0' => array( //Array name should be set to current version of update
+ 'version' => '1.0', //Current version available
+ 'date' => '2010-04-10', //Date version was released
+ 'author' => 'Author Name', //Author name - if set to wordpress.org username will be linked
+ 'requires' => '2.8', // WP version required for plugin
+ 'tested' => '3.0.1', // WP version tested with
+ 'homepage' => 'http://your_author_website', // Your personal website
+ 'downloaded'=> '1000', // Number of times downloaded
+ 'external' => 'http://your_plugin_website', // Site devoted to your plugin if available
+ 'package' => 'http://url_to_your_site/plugin.zip', // The zip file of the plugin update
+ 'sections' => array(
+ /* Plugin Info sections tabs. Each key will be used as the title of the tab, value is the contents of tab.
+ Must be lowercase to function properly
+ */
+ 'description' => 'Description of Plugin', //Description Tab
+ 'installation' => 'Install Info', //Installaion Tab
+ 'screen shots' => 'Screen Shots', //Screen Shots
+ 'change log' => 'Change log', //Change Log Tab
+ 'faq' => 'FAQ', //FAQ Tab
+ 'other notes' => 'Other Notes' //Other Notes Tab
+ )
+ )
+ ),
+ 'info' => array(
+ 'url' => 'http://your_plugin_webiste' // Site devoted to your plugin if available
+ )
+ );
+
+
+
+ // Process API requests
+
+ $action = $_POST['action'];
+ $args = unserialize($_POST['request']);
+
+ if (is_array($args))
+ $args = array_to_object($args);
+
+ $latest_package = array_shift($packages[$args->slug]['versions']);
+
+
+
+ // basic_check
+
+ if ($action == 'basic_check') {
+ $update_info = array_to_object($latest_package);
+ $update_info->slug = $args->slug;
+
+ if (version_compare($args->version, $latest_package['version'], '<'))
+ $update_info->new_version = $update_info->version;
+
+ print serialize($update_info);
+ }
+
+
+ // plugin_information
+
+ if ($action == 'plugin_information') {
+ $data = new stdClass;
+
+ $data->slug = $args->slug;
+ $data->version = $latest_package['version'];
+ $data->last_updated = $latest_package['date'];
+ $data->download_link = $latest_package['package'];
+ $data->author = $latest_package['author'];
+ $data->external = $latest_package['external'];
+ $data->requires = $latest_package['requires'];
+ $data->tested = $latest_package['tested'];
+ $data->homepage = $latest_package['homepage'];
+ $data->downloaded = $latest_package['downloaded'];
+ $data->sections = $latest_package['sections'];
+ print serialize($data);
+ }
+
+
+ // theme_update
+
+ if ($action == 'theme_update') {
+ $update_info = array_to_object($latest_package);
+
+ //$update_data = new stdClass;
+ $update_data = array();
+ $update_data['package'] = $update_info->package;
+ $update_data['new_version'] = $update_info->version;
+ $update_data['url'] = $packages[$args->slug]['info']['url'];
+
+ if (version_compare($args->version, $latest_package['version'], '<'))
+ print serialize($update_data);
+ }
+
+
+
+ function array_to_object($array = array()) {
+ if (empty($array) || !is_array($array))
+ return false;
+
+ $data = new stdClass;
+ foreach ($array as $akey => $aval)
+ $data->{$akey} = $aval;
+ return $data;
+ }
+}
+?>
@@ -0,0 +1,102 @@
+<?php
+/*
+Plugin Name: Test Plugin Update
+Plugin URI: http://clark-technet.com
+Description: Test plugin updates
+Version: 0.9
+Author: Jeremy Clark
+Author URI: http://clark-technet.com
+*/
+
+
+/*
+// TEMP: Enable update check on every request. Normally you don't need this! This is for testing only!
+set_site_transient('update_plugins', null);
+
+// TEMP: Show which variables are being requested when query plugin API
+add_filter('plugins_api_result', 'aaa_result', 10, 3);
+function aaa_result($res, $action, $args) {
+ print_r($res);
+ return $res;
+}
+*/
+
+
+$api_url = 'http://api.damb.is/';
+$plugin_slug = basename(dirname(__FILE__));
+
+
+// Take over the update check
+add_filter('pre_set_site_transient_update_plugins', 'check_for_plugin_update');
+
+function check_for_plugin_update($checked_data) {
+ global $api_url, $plugin_slug;
+
+ if (empty($checked_data->checked))
+ return $checked_data;
+
+ $request_args = array(
+ 'slug' => $plugin_slug,
+ 'version' => $checked_data->checked[$plugin_slug .'/'. $plugin_slug .'.php'],
+ );
+
+ $request_string = prepare_request('basic_check', $request_args);
+
+ // Start checking for an update
+ $raw_response = wp_remote_post($api_url, $request_string);
+
+ if (!is_wp_error($raw_response) && ($raw_response['response']['code'] == 200))
+ $response = unserialize($raw_response['body']);
+
+ if (is_object($response) && !empty($response)) // Feed the update data into WP updater
+ $checked_data->response[$plugin_slug .'/'. $plugin_slug .'.php'] = $response;
+
+ return $checked_data;
+}
+
+
+// Take over the Plugin info screen
+add_filter('plugins_api', 'my_plugin_api_call', 10, 3);
+
+function my_plugin_api_call($def, $action, $args) {
+ global $plugin_slug, $api_url;
+
+ if ($args->slug != $plugin_slug)
+ return false;
+
+ // Get the current version
+ $plugin_info = get_site_transient('update_plugins');
+ $current_version = $plugin_info->checked[$plugin_slug .'/'. $plugin_slug .'.php'];
+ $args->version = $current_version;
+
+ $request_string = prepare_request($action, $args);
+
+ $request = wp_remote_post($api_url, $request_string);
+
+ if (is_wp_error($request)) {
+ $res = new WP_Error('plugins_api_failed', __('An Unexpected HTTP Error occurred during the API request.</p> <p><a href="?" onclick="document.location.reload(); return false;">Try again</a>'), $request->get_error_message());
+ } else {
+ $res = unserialize($request['body']);
+
+ if ($res === false)
+ $res = new WP_Error('plugins_api_failed', __('An unknown error occurred'), $request['body']);
+ }
+
+ return $res;
+}
+
+
+function prepare_request($action, $args) {
+ global $wp_version;
+
+ return array(
+ 'body' => array(
+ 'action' => $action,
+ 'request' => serialize($args),
+ 'api-key' => md5(get_bloginfo('url'))
+ ),
+ 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
+ );
+}
+
+?>
@@ -0,0 +1,27 @@
+Automatic Theme & Plugin Updater for Self-Hosted Themes/Plugins
+
+For themes and plugins that can't be submitted to official WordPress repository, ie ... commercial themes/plugins/, non-gpl licensed, written for one client.
+
+Folder structure
+- api (Folder to upload to server where updates will be housed)
+-- .htaccess (set Options+Indexes to allow checking to work properly)
+-- index.php (holds code used to check request for new versions)
+
+- plugin (folder for adding plugin update checking)
+-- test-plugin-update (simple plugin folder to show how update functions work)
+--- test-plugin-update.php (example plugin that only checks for updates to server)
+
+- theme (folder for theme update checking)
+-- update.php (file that can be included from functions.php of theme to check for updates)
+
+
+Adding new versions
+
+Edit the index.php under api folder on your server. Commented thoroughly throughout with sections that need to be changed to reflect themes/plugins that are to be updated.
+
+Important:
+
+-- Change $api_url to your api server url in:
+ /plugin/test-plugin-update/test-plugin-update.php
+ /theme/update.php
+
@@ -0,0 +1,9 @@
+<?php
+/**/// TEMP: Enable update check on every request. Normally you don't need this! This is for testing only!//set_site_transient('update_themes', null);
+add_filter('pre_set_site_transient_update_themes', 'check_for_update');
+function check_for_update($checked_data) { global $wp_version; /******************Change this*******************/ $api_url = 'http://UPDATE SERVER.com/'; /************************************************/ $theme_data = get_theme_data( TEMPLATEPATH . '/style.css'); $theme_version = $theme_data['Version']; $theme_base = get_option('stylesheet'); $request = array( 'slug' => $theme_base, 'version' => $theme_version ); // Start checking for an update $send_for_check = array( 'body' => array( 'action' => 'theme_update', 'request' => serialize($request), 'api-key' => md5(get_bloginfo('url')) ), 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url') );
+ $raw_response = wp_remote_post($api_url, $send_for_check);
+ if (!is_wp_error($raw_response) && ($raw_response['response']['code'] == 200)) $response = unserialize($raw_response['body']);
+ // Feed the update data into WP updater if (!empty($response)) $checked_data->response[$theme_base] = $response;
+ return $checked_data;}
+if (is_admin()) $current = get_transient('update_themes');?>

0 comments on commit ac1e038

Please sign in to comment.