Skip to content
This repository was archived by the owner on Oct 4, 2019. It is now read-only.

Fix plugin auto update logic #37

Merged
merged 3 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
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
49 changes: 26 additions & 23 deletions auto-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,48 +33,47 @@ function auto_update( $update, $item ) {
* Fetch the first non pre-release zip from GitHub releases and store the response for later use.
*/
private function maybe_fetch_github_response() {
if ( is_null( $this->github_response ) ) {
if ( false === ( $gh_response = get_transient( 'wc_api_dev_gh_response' ) ) ) {
$request_uri = 'https://api.github.com/repos/woocommerce/wc-api-dev/releases';
$response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri ) ), true );
if ( is_array( $response ) ) {
foreach ( $response as $entry ) {
if ( false === ( bool ) $entry['prerelease'] ) {
$this->github_response = $entry;
$gh_response = $entry;
set_transient( 'wc_api_dev_gh_response', $entry, 2 * HOUR_IN_SECONDS );
break;
}
}
}
}
$this->github_response = $gh_response;
}

/**
* Add our plugin to the list of plugins to update, if we find the version is out of date.
*/
public function modify_transient( $transient ) {
if ( property_exists( $transient, 'checked' ) && $transient->checked ) {
$checked = $transient->checked;
$this->maybe_fetch_github_response();
$this->maybe_fetch_github_response();

if (
empty( $this->github_response['tag_name'] ) ||
empty( $checked[ $this->file ] ) ||
empty( $this->github_response['zipball_url'] )
) {
return $transient;
}
if (
empty( $this->github_response['tag_name'] ) ||
empty( $this->github_response['zipball_url'] )
) {
return $transient;
}

$out_of_date = version_compare( $this->github_response['tag_name'], $checked[ $this->file ], '>' );
if ( $out_of_date ) {
$plugin = array(
'url' => 'https://github.com/woocommerce/wc-api-dev',
'plugin' => $this->file,
'slug' => 'wc-api-dev',
'package' => $this->github_response['zipball_url'],
'new_version' => $this->github_response['tag_name']
);
$transient->response[ $this->file ] = (object) $plugin;
}
$out_of_date = version_compare( $this->github_response['tag_name'], WC_API_Dev::CURRENT_VERSION, '>' );
if ( $out_of_date ) {
$plugin = array(
'url' => 'https://github.com/woocommerce/wc-api-dev',
'plugin' => $this->file,
'slug' => 'wc-api-dev',
'package' => $this->github_response['zipball_url'],
'new_version' => $this->github_response['tag_name']
);
$transient->response[ $this->file ] = (object) $plugin;
}

return $transient;
}

Expand Down Expand Up @@ -128,6 +127,10 @@ public function after_install( $response, $hook_extra, $result ) {
$wp_filesystem->move( $result['destination'], $install_directory );
$result['destination'] = $install_directory;
activate_plugin( $this->file );

// Prevent double notice being displayed. At this point we don't need the plugin injected.
remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'modify_transient' ), 10 );

return $result;
}
}
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: automattic, woothemes
Tags: woocommerce, rest-api, api
Requires at least: 4.6
Tested up to: 4.8
Stable tag: 0.8.0
Stable tag: 0.8.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -26,6 +26,7 @@ This section describes how to install the plugin and get it working.
== Changelog ==

= 0.8.1 =
* Fix - Fix auto plugin update logic
* Fix - Don't auto enable the cheque payment method.
* Adds a new config constant (WC_API_DEV_ENABLE_HOTFIXES) to make it easy to disable hotfixes.

Expand Down
5 changes: 5 additions & 0 deletions wc-api-dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
*/
class WC_API_Dev {

/**
* Current version of the API plugin.
*/
const CURRENT_VERSION = '0.8.1';

/**
* Minimum version needed to run this version of the API.
*/
Expand Down