Overview
  • Package
  • Class
  • Tree

Packages

  • EDD
    • License
    • Reviews
      • Licensing
      • Shortcodes
      • Widgets

Classes

  • EDD_SL_Plugin_Updater
  1 <?php
  2 /**
  3  * Allows plugins to use their own update API.
  4  *
  5  * @package EDD_Reviews
  6  * @subpackage Licensing
  7  * @author Pippin Williamson
  8  * @version 1.0
  9  */
 10 class EDD_SL_Plugin_Updater {
 11     /**
 12      * @var string
 13      */
 14     private $api_url = '';
 15 
 16     /**
 17      * @var string
 18      */
 19     private $api_data = array();
 20 
 21     /**
 22      * @var string
 23      */
 24     private $name = '';
 25 
 26     /**
 27      * @var string
 28      */
 29     private $slug = '';
 30 
 31     /**
 32      * Class constructor.
 33      *
 34      * @since 1.0
 35      *
 36      * @uses plugin_basename()
 37      * @uses hook()
 38      *
 39      * @param string $_api_url The URL pointing to the custom API endpoint.
 40      * @param string $_plugin_file Path to the plugin file.
 41      * @param array $_api_data Optional data to send with API calls.
 42      * @return void
 43      */
 44     function __construct( $_api_url, $_plugin_file, $_api_data = null ) {
 45         $this->api_url  = trailingslashit( $_api_url );
 46         $this->api_data = urlencode_deep( $_api_data );
 47         $this->name     = plugin_basename( $_plugin_file );
 48         $this->slug     = basename( $_plugin_file, '.php');
 49         $this->version  = $_api_data['version'];
 50 
 51         // Set up hooks.
 52         $this->hook();
 53     }
 54 
 55     /**
 56      * Set up Wordpress filters to hook into WP's update process.
 57      *
 58      * @since 1.0
 59      * @access private
 60      *
 61      * @uses add_filter()
 62      *
 63      * @return void
 64      */
 65     private function hook() {
 66         add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'pre_set_site_transient_update_plugins_filter' ) );
 67         add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
 68     }
 69 
 70     /**
 71      * Check for Updates at the defined API endpoint and modify the update array.
 72      *
 73      * This function dives into the update api just when Wordpress creates its update array,
 74      * then adds a custom API call and injects the custom plugin data retrieved from the API.
 75      * It is reassembled from parts of the native Wordpress plugin update code.
 76      * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
 77      *
 78      * @since 1.0
 79      * @access public
 80      *
 81      * @uses EDD_SL_Plugin_Updater::api_request()
 82      *
 83      * @param array $_transient_data Update array build by Wordpress.
 84      * @return array Modified update array with custom plugin data.
 85      */
 86     function pre_set_site_transient_update_plugins_filter( $_transient_data ) {
 87         if ( empty( $_transient_data ) ) return $_transient_data;
 88 
 89         $to_send = array( 'slug' => $this->slug );
 90 
 91         $api_response = $this->api_request( 'plugin_latest_version', $to_send );
 92 
 93         if ( false !== $api_response && is_object( $api_response ) ) {
 94             if( version_compare( $this->version, $api_response->new_version, '<' ) )
 95                 $_transient_data->response[$this->name] = $api_response;
 96     }
 97         return $_transient_data;
 98     }
 99 
100 
101     /**
102      * Updates information on the "View version x.x details" page with custom data.
103      *
104      * @since 1.0
105      * @access public
106      *
107      * @uses EDD_SL_Plugin_Updater::api_request()
108      *
109      * @param mixed $_data
110      * @param string $_action
111      * @param object $_args
112      * @return object $_data
113      */
114     function plugins_api_filter( $_data, $_action = '', $_args = null ) {
115         if ( ( $_action != 'plugin_information' ) || ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) return $_data;
116 
117         $to_send = array( 'slug' => $this->slug );
118 
119         $api_response = $this->api_request( 'plugin_information', $to_send );
120         if ( false !== $api_response ) $_data = $api_response;
121 
122         return $_data;
123     }
124 
125     /**
126      * Calls the API and, if successfull, returns the object delivered by the API.
127      *
128      * @since 1.0
129      * @access private
130      *
131      * @uses get_bloginfo()
132      * @uses wp_remote_post()
133      * @uses is_wp_error()
134      *
135      * @param string $_action The requested action.
136      * @param array $_data Parameters for the API action.
137      * @return false||object
138      */
139     private function api_request( $_action, $_data ) {
140         global $wp_version;
141 
142         $data = array_merge( $this->api_data, $_data );
143         if ( $data['slug'] != $this->slug )
144             return;
145 
146         $api_params = array(
147             'edd_action'    => 'get_version',
148             'license'       => $data['license'],
149             'name'          => $data['item_name'],
150             'slug'          => $this->slug,
151             'author'        => $data['author']
152         );
153         $request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
154 
155         if ( ! is_wp_error( $request ) ):
156             $request = json_decode( wp_remote_retrieve_body( $request ) );
157             if ( $request )
158                 $request->sections = maybe_unserialize( $request->sections );
159             return $request;
160         else:
161             return false;
162         endif;
163     }
164 }
API documentation generated by ApiGen 2.8.0