public
Created

Sample Commercial Plugin update server and client

  • Download Gist
commercial-client.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
<?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' ) );
commercial-server.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
<?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' ) );

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

thanks

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.