From b08daa7cc58fa53e35573be3a2c9845450837103 Mon Sep 17 00:00:00 2001 From: David Mosterd Date: Wed, 2 Jan 2013 23:52:43 +0100 Subject: [PATCH 1/2] used a central function for building the query --- updater.php | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/updater.php b/updater.php index 9f2d5fe..ce7498f 100644 --- a/updater.php +++ b/updater.php @@ -217,10 +217,7 @@ public function get_new_version() { if ( $this->overrule_transients() || ( !isset( $version ) || !$version || '' == $version ) ) { - $query = trailingslashit( $this->config['raw_url'] ) . basename( $this->config['slug'] ); - $query = add_query_arg( array( 'access_token' => $this->config['access_token'] ), $query ); - - $raw_response = wp_remote_get( $query, array( 'sslverify' => $this->config['sslverify'] ) ); + $raw_response = $this->remote_get( trailingslashit( $this->config['raw_url'] ) . basename( $this->config['slug'] ) ); if ( is_wp_error( $raw_response ) ) $version = false; @@ -233,10 +230,7 @@ public function get_new_version() { $version = $matches[1]; // back compat for older readme version handling - $query = trailingslashit( $this->config['raw_url'] ) . $this->config['readme']; - $query = add_query_arg( array( 'access_token' => $this->config['access_token'] ), $query ); - - $raw_response = wp_remote_get( $query, array( 'sslverify' => $this->config['sslverify'] ) ); + $raw_response = $this->remote_get( trailingslashit( $this->config['raw_url'] ) . $this->config['readme'] ); if ( is_wp_error( $raw_response ) ) return $version; @@ -258,6 +252,26 @@ public function get_new_version() { } + /** + * Interact with GitHub + * + * @param string $query + * + * @since 1.6 + * @return mixed + */ + public function remote_get( $query ) { + if ( ! empty( $this->config['access_token'] ) ) + $query = add_query_arg( array( 'access_token' => $this->config['access_token'] ), $query ); + + $raw_response = wp_remote_get( $query, array( + 'sslverify' => $this->config['sslverify'] + ) ); + + return $raw_response; + } + + /** * Get GitHub Data from the specified repository * @@ -271,10 +285,7 @@ public function get_github_data() { $github_data = get_site_transient( $this->config['slug'].'_github_data' ); if ( $this->overrule_transients() || ( ! isset( $github_data ) || ! $github_data || '' == $github_data ) ) { - $query = $this->config['api_url']; - $query = add_query_arg( array( 'access_token' => $this->config['access_token'] ), $query ); - - $github_data = wp_remote_get( $query, array( 'sslverify' => $this->config['sslverify'] ) ); + $github_data = $this->remote_get( $this->config['api_url'] ); if ( is_wp_error( $github_data ) ) return false; From afe34a6e2673802768e31cb46fb9a7282f36e00b Mon Sep 17 00:00:00 2001 From: David Mosterd Date: Wed, 2 Jan 2013 23:58:45 +0100 Subject: [PATCH 2/2] enhanced the get_new_version a bit --- updater.php | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/updater.php b/updater.php index ce7498f..df5c9c5 100644 --- a/updater.php +++ b/updater.php @@ -215,36 +215,30 @@ public function http_request_sslverify( $args, $url ) { public function get_new_version() { $version = get_site_transient( $this->config['slug'].'_new_version' ); - if ( $this->overrule_transients() || ( !isset( $version ) || !$version || '' == $version ) ) { + if ( $this->overrule_transients() || empty( $version ) ) { + $version = null; $raw_response = $this->remote_get( trailingslashit( $this->config['raw_url'] ) . basename( $this->config['slug'] ) ); - if ( is_wp_error( $raw_response ) ) - $version = false; + if ( ! is_wp_error( $raw_response ) ) { + preg_match( '#^\s*Version\:\s*(.*)$#im', $raw_response['body'], $matches ); - preg_match( '#^\s*Version\:\s*(.*)$#im', $raw_response['body'], $matches ); - - if ( empty( $matches[1] ) ) - $version = false; - else - $version = $matches[1]; + if ( ! empty( $matches[1] ) ) + $version = $matches[1]; + } // back compat for older readme version handling $raw_response = $this->remote_get( trailingslashit( $this->config['raw_url'] ) . $this->config['readme'] ); - if ( is_wp_error( $raw_response ) ) - return $version; - - preg_match( '#^\s*`*~Current Version\:\s*([^~]*)~#im', $raw_response['body'], $__version ); + if ( ! is_wp_error( $raw_response ) ) { + preg_match( '#^\s*`*~Current Version\:\s*([^~]*)~#im', $raw_response['body'], $__version ); - if ( isset( $__version[1] ) ) { - $version_readme = $__version[1]; - if ( -1 == version_compare( $version, $version_readme ) ) - $version = $version_readme; + if ( isset( $__version[1] ) && -1 == version_compare( $version, $__version[1] ) ) + $version = $__version[1]; } // refresh every 6 hours - if ( false !== $version ) + if ( ! empty( $version ) ) set_site_transient( $this->config['slug'].'_new_version', $version, 60*60*6 ); }