Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced get_new_version (depends on #35) #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 35 additions & 30 deletions updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,49 +215,57 @@ 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;

$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'] ) );

if ( is_wp_error( $raw_response ) )
$version = false;
$raw_response = $this->remote_get( trailingslashit( $this->config['raw_url'] ) . basename( $this->config['slug'] ) );

preg_match( '#^\s*Version\:\s*(.*)$#im', $raw_response['body'], $matches );
if ( ! is_wp_error( $raw_response ) ) {
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
$query = trailingslashit( $this->config['raw_url'] ) . $this->config['readme'];
$query = add_query_arg( array( 'access_token' => $this->config['access_token'] ), $query );
$raw_response = $this->remote_get( trailingslashit( $this->config['raw_url'] ) . $this->config['readme'] );

$raw_response = wp_remote_get( $query, array( 'sslverify' => $this->config['sslverify'] ) );
if ( ! is_wp_error( $raw_response ) ) {
preg_match( '#^\s*`*~Current Version\:\s*([^~]*)~#im', $raw_response['body'], $__version );

if ( is_wp_error( $raw_response ) )
return $version;

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 );
}

return $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
*
Expand All @@ -271,10 +279,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;
Expand Down