From: Steve Sutton Date: Tue, 19 Apr 2016 18:29:54 +0000 (-0400) Subject: For updatable non wordpress updates X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/index.cgi?a=commitdiff_plain;h=refs%2Fheads%2FpluginUpdater;p=WP-Plugins%2Fglm-member-db-events.git For updatable non wordpress updates --- diff --git a/glm-plugin-updater.php b/glm-plugin-updater.php new file mode 100644 index 0000000..91e656e --- /dev/null +++ b/glm-plugin-updater.php @@ -0,0 +1,133 @@ +current_version = $current_version; + $this->update_path = $update_path; + $this->plugin_slug = $plugin_slug; + list ($t1, $t2) = explode('/', $plugin_slug); + $this->slug = str_replace('.php', '', $t2); + + // define the alternative API for updating checking + add_filter('pre_set_site_transient_update_plugins', array(&$this, 'check_update')); + + // Define the alternative response for information checking + add_filter('plugins_api', array(&$this, 'check_info'), 10, 3); + } + + /** + * Add our self-hosted autoupdate plugin to the filter transient + * + * @param $transient + * @return object $ transient + */ + public function check_update($transient) + { + if (empty($transient->checked)) { + return $transient; + } + + // Get the remote version + $remote_version = $this->getRemote_version(); + + // If a newer version is available, add the update + if (version_compare($this->current_version, $remote_version, '<')) { + $obj = new stdClass(); + $obj->slug = $this->slug; + $obj->new_version = $remote_version; + $obj->url = $this->update_path; + $obj->package = $this->update_path; + $transient->response[$this->plugin_slug] = $obj; + } + //var_dump($transient); + return $transient; + } + + /** + * Add our self-hosted description to the filter + * + * @param boolean $false + * @param array $action + * @param object $arg + * @return bool|object + */ + public function check_info($false, $action, $arg) + { + if ($arg->slug === $this->slug) { + $information = $this->getRemote_information(); + return $information; + } + return false; + } + + /** + * Return the remote version + * @return string $remote_version + */ + public function getRemote_version() + { + $request = wp_remote_post($this->update_path, array('body' => array('action' => 'version'))); + if (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) { + return $request['body']; + } + return false; + } + + /** + * Get information about the remote version + * @return bool|object + */ + public function getRemote_information() + { + $request = wp_remote_post($this->update_path, array('body' => array('action' => 'info'))); + if (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) { + return unserialize($request['body']); + } + return false; + } + + /** + * Return the status of the plugin licensing + * @return boolean $remote_license + */ + public function getRemote_license() + { + $request = wp_remote_post($this->update_path, array('body' => array('action' => 'license'))); + if (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) { + return $request['body']; + } + return false; + } +} diff --git a/index.php b/index.php index b474015..da3eb3b 100644 --- a/index.php +++ b/index.php @@ -212,3 +212,12 @@ add_filter('glm-member-db-register-addon','glmMembersRegisterEvents', 10, 1); */ require_once(GLM_MEMBERS_EVENTS_PLUGIN_SETUP_PATH.'/permissions.php'); +add_action('init', 'glm_event_update_plugins'); +function glm_event_update_plugins() +{ + require_once 'glm-plugin-updater.php'; + $current_version = '0.0.1'; + $remote_path = 'http://localhost/update.php'; + $plugin_slug = plugin_basename(__FILE__); + new glm_plugin_updater($current_version, $remote_path, $plugin_slug); +}