Skip to content
Browse files

Github as transport

  • Loading branch information...
1 parent 312a962 commit 3deaac29617fbbb6f028fa2558f5d85d2a85be73 @pdclark committed
Showing with 153 additions and 142 deletions.
  1. +2 −0 plugin.php
  2. +113 −0 transports/github.php
  3. +38 −142 updater.php
View
2 plugin.php
@@ -23,4 +23,6 @@
*/
+define('WP_GITHUB_FORCE_UPDATE', true);
+
include_once dirname(__FILE__).'/updater.php';
View
113 transports/github.php
@@ -0,0 +1,113 @@
+<?php
+
+class WordPress_Github_Updater {
+
+ public function __construct( $args ){
+
+ global $wp_version;
+
+ $defaults = array(
+ 'name' => $args['Name'],
+ 'slug' => $args['slug'],
+ 'folder_name' => dirname( $args['slug'] ),
+ 'key' => dirname( $args['slug'] ),
+ 'host' => $args['host'],
+ 'username' => $args['username'],
+ 'repository' => $args['repository'],
+ 'version' => $args['Version'],
+ 'author' => $args['Author'],
+ 'homepage' => $args['PluginURI'],
+ 'requires' => $wp_version,
+ 'tested' => $wp_version,
+ );
+
+ $args = wp_parse_args($args, $defaults);
+
+ $this->api_url = "https://api.github.com/repos/{$args['username']}/{$args['repository']}";
+ $this->tags_url = "https://api.github.com/repos/{$args['username']}/{$args['repository']}/tags";
+
+ foreach( $args as $key => $value ) {
+ $this->$key = $value;
+ }
+
+ $this->set_new_version_and_zip_url();
+ $this->set_last_updated();
+ $this->set_description();
+
+ var_dump($this);
+ }
+
+ /**
+ * Get New Version from github
+ *
+ * @since 1.0
+ * @return void
+ */
+ public function set_new_version_and_zip_url() {
+
+ $raw_response = wp_remote_get( $this->tags_url );
+
+ if ( is_wp_error( $raw_response ) )
+ return false;
+
+ $tags = json_decode( $raw_response['body'] );
+
+ $version = false;
+ $zip_url = false;
+ foreach ( $tags as $tag ) {
+ if ( version_compare($tag->name, $version, '>=') ) {
+ $version = $tag->name;
+ $zip_url = $tag->zipball_url;
+ }
+ }
+
+ $this->new_version = $version;
+ $this->zip_url = $zip_url;
+
+ }
+
+ /**
+ * Get GitHub Data from the specified repository
+ *
+ * @since 1.0
+ * @return array $github_data the data
+ */
+ public function get_github_data() {
+
+ if ( empty($this->github_data) ) {
+ $data = wp_remote_get( $this->api_url );
+
+ if ( is_wp_error( $data ) )
+ return false;
+
+ $this->github_data = json_decode( $data['body'] );
+ }
+
+ return $this->github_data;
+ }
+
+
+ /**
+ * Get update date
+ *
+ * @since 1.0
+ * @return string $date the date
+ */
+ public function set_last_updated() {
+ $_date = $this->get_github_data();
+ return ( !empty($_date->updated_at) ) ? date( 'Y-m-d', strtotime( $_date->updated_at ) ) : false;
+ }
+
+
+ /**
+ * Get plugin description
+ *
+ * @since 1.0
+ * @return string $description the description
+ */
+ public function set_description() {
+ $_description = $this->get_github_data();
+ return ( !empty($_description->description) ) ? $_description->description : false;
+ }
+
+}
View
180 updater.php
@@ -44,10 +44,10 @@ public function __construct( $config = array() ) {
$this->ssl_verify = apply_filters('git_sslverify', $this->ssl_verify);
if ( ( defined('WP_DEBUG') && WP_DEBUG ) || ( defined('WP_GITHUB_FORCE_UPDATE') || WP_GITHUB_FORCE_UPDATE ) )
- add_action( 'init', array( $this, 'delete_transients' ), 11 );
+ add_action( 'admin_init', array( $this, 'delete_transients' ), 11 );
// Build Git plugin list
- add_action( 'admin_init', array($this, 'load_plugins'), 0 );
+ add_action( 'admin_init', array($this, 'load_plugins'), 20 );
add_filter( 'extra_plugin_headers', array($this, 'extra_plugin_headers') );
// Check for update from Git API
@@ -82,41 +82,17 @@ public function load_plugins( $plugins ) {
if ( false !== $this->plugins ) {
return;
}
-
global $wp_version;
foreach ( get_plugins() as $slug => $meta ) {
- $repo = $this->get_repo_parts( $meta['PluginURI'] );
+ $repo = $this->get_repo_transport( array_merge( array('slug'=>$slug), $meta ) );
if (false === $repo ) {
continue;
}
- $key = dirname($slug);
-
- $settings = array(
- 'name' => $meta['Name'],
- 'slug' => $slug,
- 'folder_name' => $key,
- 'host' => $repo['host'],
- 'username' => $repo['username'],
- 'repository' => $repo['repository'],
- 'version' => $meta['Version'],
- 'author' => $meta['Author'],
- 'homepage' => $meta['PluginURI'],
- 'api_url' => "https://api.github.com/repos/{$repo['username']}/{$repo['repository']}",
- 'tags_url' => "https://api.github.com/repos/{$repo['username']}/{$repo['repository']}/tags",
- );
-
- $settings['requires'] = (empty($meta['requires'])) ? $wp_version : $meta['requires'];
- $settings['tested'] = (empty($meta['tested'])) ? $wp_version : $meta['tested'];
-
// Using folder name as key for array_key_exists() check in $this->get_plugin_info()
- $this->plugins[$key] = wp_parse_args( $settings, $this->defaults );
-
- $this->set_new_version_and_zip_url( $key );
- $this->set_last_updated( $key );
- $this->set_description( $key );
+ $this->plugins[ $repo->key ] = $repo;
}
@@ -159,10 +135,10 @@ public function disable_git_ssl_verify($args, $url) {
}
if ( empty($this->git_urls) ) {
foreach( $this->plugins as $plugin ) {
- $this->git_urls[] = $plugin['homepage'];
- $this->git_urls[] = $plugin['api_url'];
- $this->git_urls[] = $plugin['tags_url'];
- $this->git_urls[] = $plugin['zip_url'];
+ $this->git_urls[] = $plugin->homepage;
+ $this->git_urls[] = $plugin->api_url;
+ $this->git_urls[] = $plugin->tags_url;
+ $this->git_urls[] = $plugin->zip_url;
}
}
if ( in_array($url, $this->git_urls) ) {
@@ -187,104 +163,24 @@ public function delete_transients() {
/**
- * Get New Version from github
- *
- * @since 1.0
- * @return void
- */
- public function set_new_version_and_zip_url( $key ) {
- $plugin = $this->plugins[$key];
-
- $raw_response = wp_remote_get( $plugin['tags_url'] );
-
- if ( is_wp_error( $raw_response ) )
- return false;
-
- $tags = json_decode( $raw_response['body'] );
-
- $version = false;
- $zip_url = false;
- foreach ( $tags as $tag ) {
- if ( version_compare($tag->name, $version, '>=') ) {
- $version = $tag->name;
- $zip_url = $tag->zipball_url;
- }
- }
-
- $this->plugins[ $key ]['new_version'] = $version;
- $this->plugins[ $key ]['zip_url'] = $zip_url;
-
- }
-
- /**
- * Check if a URI is a github repo.
- * Return host, username, and repository name if so.
- *
- * @return array host, username, repository
- */
- public function get_repo_parts( $uri ) {
- $parsed = parse_url( $uri );
-
- if ( false !== strpos($parsed['host'], 'github.com') ) {
- list( /*nothing*/, $username, $repository ) = explode('/', $parsed['path'] );
- return array(
- 'host' => $parsed['host'],
- 'username' => $username,
- 'repository' => $repository,
- );
- }else {
- return false;
- }
- }
-
-
- /**
- * Get GitHub Data from the specified repository
+ * Return appropriate repository handler based on URI
*
- * @since 1.0
- * @return array $github_data the data
+ * @return object
*/
- public function get_github_data( $key ) {
-
- $plugin = $this->plugins[$key];
- $github_data = $plugin['github_data'];
+ public function get_repo_transport( $meta ) {
+ $parsed = parse_url( $meta['PluginURI'] );
- if ( ! isset( $github_data ) || ! $github_data || '' == $github_data ) {
- $github_data = wp_remote_get( $plugin['api_url'] );
+ switch( $parsed['host'] ) {
+ case 'github.com':
+ case 'www.github.com':
+ if ( !class_exists('WordPress_Github_Updater') ) { include 'transports/github.php'; }
+ list( /*nothing*/, $username, $repository ) = explode('/', $parsed['path'] );
+ return new WordPress_Github_Updater( array_merge($meta, array( 'username' => $username, 'repository' => $repository, )) );
+ break;
- if ( is_wp_error( $github_data ) )
- return false;
-
- $github_data = json_decode( $github_data['body'] );
-
- $this->plugins[$key]['github_data'] = $github_data;
}
- return $github_data;
- }
-
-
- /**
- * Get update date
- *
- * @since 1.0
- * @return string $date the date
- */
- public function set_last_updated( $key ) {
- $_date = $this->get_github_data( $key );
- return ( !empty($_date->updated_at) ) ? date( 'Y-m-d', strtotime( $_date->updated_at ) ) : false;
- }
-
-
- /**
- * Get plugin description
- *
- * @since 1.0
- * @return string $description the description
- */
- public function set_description( $key ) {
- $_description = $this->get_github_data( $key );
- return ( !empty($_description->description) ) ? $_description->description : false;
+ return false;
}
@@ -304,18 +200,18 @@ public function api_check( $transient ) {
foreach( $this->plugins as $plugin ) {
// check the version and decide if it's new
- $update = version_compare( $plugin['new_version'], $plugin['version'] );
+ $update = version_compare( $plugin->new_version, $plugin->version );
if ( 1 === $update ) {
$response = new stdClass;
- $response->new_version = $plugin['new_version'];
- $response->slug = $plugin['folder_name'];
- $response->url = $plugin['homepage'];
- $response->package = $plugin['zip_url'];
+ $response->new_version = $plugin->new_version;
+ $response->slug = $plugin->folder_name;
+ $response->url = $plugin->homepage;
+ $response->package = $plugin->zip_url;
// If response is false, don't alter the transient
if ( false !== $response )
- $transient->response[ $plugin['slug'] ] = $response;
+ $transient->response[ $plugin->slug ] = $response;
}
}
@@ -339,17 +235,17 @@ public function get_plugin_info( $false, $action, $response ) {
$plugin = $this->plugins[ $response->slug ];
- $response->slug = $plugin['slug'];
- $response->plugin_name = $plugin['name'];
- $response->version = $plugin['new_version'];
- $response->author = $plugin['author'];
- $response->homepage = $plugin['homepage'];
- $response->requires = $plugin['requires'];
- $response->tested = $plugin['tested'];
+ $response->slug = $plugin->slug;
+ $response->plugin_name = $plugin->name;
+ $response->version = $plugin->new_version;
+ $response->author = $plugin->author;
+ $response->homepage = $plugin->homepage;
+ $response->requires = $plugin->requires;
+ $response->tested = $plugin->tested;
$response->downloaded = 0;
- $response->last_updated = $plugin['last_updated'];
- $response->sections = array( 'description' => $plugin['description'] );
- $response->download_link = $plugin['zip_url'];
+ $response->last_updated = $plugin->last_updated;
+ $response->sections = array( 'description' => $plugin->description );
+ $response->download_link = $plugin->zip_url;
return $response;
}
@@ -372,10 +268,10 @@ public function upgrader_post_install( $true, $hook_extra, $result ) {
$plugin = $this->plugins[ $hook_extra['plugin'] ];
// Move & Activate
- $proper_destination = WP_PLUGIN_DIR.'/'.$plugin['folder_name'];
+ $proper_destination = WP_PLUGIN_DIR.'/'.$plugin->folder_name;
$wp_filesystem->move( $result['destination'], $proper_destination );
$result['destination'] = $proper_destination;
- $activate = activate_plugin( WP_PLUGIN_DIR.'/'.$plugin['slug'] );
+ $activate = activate_plugin( WP_PLUGIN_DIR.'/'.$plugin->slug );
// Output the update message
$fail = __('The plugin has been updated, but could not be reactivated. Please reactivate it manually.', 'github_plugin_updater');

0 comments on commit 3deaac2

Please sign in to comment.
Something went wrong with that request. Please try again.