Skip to content

Instantly share code, notes, and snippets.

@pento
Created July 2, 2013 12:29
Show Gist options
  • Save pento/5908857 to your computer and use it in GitHub Desktop.
Save pento/5908857 to your computer and use it in GitHub Desktop.
Sample Commercial Plugin update server and client
<?php
/*
* Plugin Name: Commercial Client
* Plugin URI: http://pento.net/
* Description: A sample client plugin for showing updates for non-WordPress.org plugins
* Author: pento
* Version: 0.1
* Author URI: http://pento.net/
* License: GPL2+
*/
// Don't allow the plugin to be loaded directly
if ( ! function_exists( 'add_action' ) ) {
echo "Please enable this plugin from your wp-admin.";
exit;
}
class CommercialClient {
// This is the URL of the page we defined in CommercialServer.
private $APIurl = 'http://localhost/api/';
// This is the URL for your own changelog. You should make it look pretty.
private $changelogURL = 'http://localhost/changelog/';
// This should match the slug that CommercialServer::createAPI() sends
private $slug = 'commercial-client';
function init() {
static $instance;
if ( empty( $instance ) )
$instance = new CommercialClient();
return $instance;
}
function CommercialClient() {
// WordPress doesn't let us hook directly into the update process.
// The update info is storted in the 'update_plugins' transient, however,
// so we can hook into that, instead.
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'checkForUpdates' ), 10, 1 );
// The changelog isn't available on WordPress.org, so we need to override the update information
add_action( 'install_plugins_pre_plugin-information', array( $this, 'overrideUpdateInformation' ), 1 );
}
function checkForUpdates( $value ) {
include ABSPATH . WPINC . '/version.php';
$plugin_data = get_plugin_data( __FILE__ );
// If your plugin has a key that you want to check on the server side,
// you should include it in the 'body' element of this array.
$options = array(
'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ),
'body' => array(
'version' => $plugin_data['Version'],
)
);
$raw_response = wp_remote_post( $this->APIurl, $options );
if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
return $value;
$response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) );
if ( empty( $response ) )
return $value;
// If you decide to have the server return different data (for example, for an invalid key),
// you should process it here, then return $value, unmodified.
$value->response[ plugin_basename( __FILE__ ) ] = $response;
return $value;
}
function overrideUpdateInformation() {
if ( wp_unslash( $_REQUEST['plugin'] ) !== $this->slug )
return;
wp_redirect( $this->changelogURL );
exit;
}
}
add_action( 'plugins_loaded', array( 'CommercialClient', 'init' ) );
<?php
/*
* Plugin Name: Commercial Server
* Plugin URI: http://pento.net/
* Description: A sample server for showing updates for non-WordPress.org plugins
* Author: pento
* Version: 0.1
* Author URI: http://pento.net/
* License: GPL2+
*/
// Don't allow the plugin to be loaded directly
if ( ! function_exists( 'add_action' ) ) {
echo "Please enable this plugin from your wp-admin.";
exit;
}
class CommercialServer {
// When the client site checks for an update, it will request this page.
// We override it, for great justice.
private $APIpageID = 26;
private $currentVersion = '0.2';
function init() {
static $instance;
if ( empty( $instance ) )
$instance = new CommercialServer();
return $instance;
}
function CommercialServer() {
add_action( 'parse_query', array( $this, 'createAPI' ) );
}
function createAPI() {
global $wp_query;
if ( empty( $wp_query->queried_object ) )
return;
if ( ! is_a( $wp_query->queried_object, 'WP_Post' ) )
return;
if ( $this->APIpageID !== $wp_query->queried_object->ID )
return;
if ( empty( $_REQUEST['version'] ) )
exit;
// If there's no update, don't bother returning any info
if ( version_compare( $this->currentVersion, $_REQUEST['version'], '<=' ) )
exit;
// If you want to have the client send a licence key, here would be a good place to check it.
// So, here's a sample update. You probably want to fill this info from your version DB, or something.
$response = new stdClass();
$response->id = 0;
$response->slug = 'commercial-client';
$response->new_version = $this->currentVersion;
$response->url = 'http://localhost/';
$response->package = 'http://localhost/downloads/commercial-client-0.2.zip';
echo serialize( $response );
exit;
}
}
add_action( 'plugins_loaded', array( 'CommercialServer', 'init' ) );
@azharisubroto
Copy link

Hi pento, great work!...
could you please tell us how to use this plugins?
is it just need to plug and play?

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment