Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class Premium_Plugin {
/**
* Plugin API Url
*
* @access protected
* @var string
**/
protected $plugin_api_url = 'http://domain.tld';
/**
* Plugin file
*
* @access protected
* @var string
**/
protected $plugin_file;
/**
* Plugin Slug
*
* @access protected
* @var string
**/
protected $plugin_slug = 'premium-plugin';
/**
* Plugin version
*
* @var string
**/
protected $plugin_version = '1.0';
/**
* Constructor
*
* @return void
* @author Ralf Hortt
**/
public function __construct()
{
$this->plugin_file = plugin_basename( __FILE__ );
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'api_has_update' ) );
add_filter( 'plugins_api_result', array( $this, 'api_plugin_information' ), 10, 3 );
}
/**
* Call plugin API if an update exists
*
* @access public
* @param obj $transient Transient object
* @uses get_option
* @uses Contentboxes_PRO::api_request
* @return void
* @author Ralf Hortt
**/
public function api_has_update( $transient )
{
// Check if the transient contains the 'checked' information
// If no, just return its value without hacking it
if ( empty( $transient->checked ) )
return $transient;
$options = get_option( 'plugin-settings-api' );
if ( empty($options['license']) || '' == $options['license'] )
return $transient;
// The transient contains the 'checked' information
// Now append to it information form your own API
// POST data to send to your API
$args = array(
'action' => 'update-check',
'plugin_name' => $this->plugin_slug,
'version' => $this->plugin_version,
'license' => $options['license'],
'url' => get_bloginfo( 'wpurl' )
);
// Send request checking for an update
$response = $this->api_request( $args );
// If response is FALSE, don't alter the transient
if ( FALSE !== $response ) :
$transient->response[$this->plugin_file] = $response;
endif;
return $transient;
}
/**
* Return plugin details from alternate repository
*
* @access public
* @param $res
* @param $action
* @param $args
* @return void
* @author Ralf Hortt
**/
public function api_plugin_information( $res, $action, $args )
{
if ( $this->plugin_slug != $args->slug )
return $res;
$options = get_option( 'plugin-settings-api' );
// POST data to send to API
$args = array(
'action' => 'plugin-information',
'plugin_name' => $this->plugin_slug,
'version' => $this->plugin_version,
'license' => $options['license'],
'url' => get_bloginfo('wpurl')
);
// Send request for detailed information
$response = $this->api_request( $args );
return $response;
}
/**
* Send an update request
*
* @access public
* @param array $args Arguments
* @return void
* @author Ralf Hortt
**/
public function api_request( $args )
{
// Send request
$request = wp_remote_post( $this->plugin_api_url, array( 'body' => $args ) );
// Make sure the request was successful
if( is_wp_error( $request ) or wp_remote_retrieve_response_code( $request ) != 200 ) :
// Request failed
return FALSE;
endif;
// Read server response, which should be an object
$response = unserialize( wp_remote_retrieve_body( $request ) );
if ( is_object( $response ) ) :
return $response;
else :
// Unexpected response
return FALSE;
endif;
}
}
@studiograsshopper

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.