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

Commit 1b5d6f3

Browse files
author
Justin Shreve
authored
Fix plugin auto update logic (#37)
* Fixes auto-update logic * Unhooked our tranisent injection after update, so that we don't see an update still pending. * Bump version number in readme.txt
1 parent 90807fd commit 1b5d6f3

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

auto-update.php

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,48 +33,47 @@ function auto_update( $update, $item ) {
3333
* Fetch the first non pre-release zip from GitHub releases and store the response for later use.
3434
*/
3535
private function maybe_fetch_github_response() {
36-
if ( is_null( $this->github_response ) ) {
36+
if ( false === ( $gh_response = get_transient( 'wc_api_dev_gh_response' ) ) ) {
3737
$request_uri = 'https://api.github.com/repos/woocommerce/wc-api-dev/releases';
3838
$response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri ) ), true );
3939
if ( is_array( $response ) ) {
4040
foreach ( $response as $entry ) {
4141
if ( false === ( bool ) $entry['prerelease'] ) {
42-
$this->github_response = $entry;
42+
$gh_response = $entry;
43+
set_transient( 'wc_api_dev_gh_response', $entry, 2 * HOUR_IN_SECONDS );
4344
break;
4445
}
4546
}
4647
}
4748
}
49+
$this->github_response = $gh_response;
4850
}
4951

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

58-
if (
59-
empty( $this->github_response['tag_name'] ) ||
60-
empty( $checked[ $this->file ] ) ||
61-
empty( $this->github_response['zipball_url'] )
62-
) {
63-
return $transient;
64-
}
58+
if (
59+
empty( $this->github_response['tag_name'] ) ||
60+
empty( $this->github_response['zipball_url'] )
61+
) {
62+
return $transient;
63+
}
6564

66-
$out_of_date = version_compare( $this->github_response['tag_name'], $checked[ $this->file ], '>' );
67-
if ( $out_of_date ) {
68-
$plugin = array(
69-
'url' => 'https://github.com/woocommerce/wc-api-dev',
70-
'plugin' => $this->file,
71-
'slug' => 'wc-api-dev',
72-
'package' => $this->github_response['zipball_url'],
73-
'new_version' => $this->github_response['tag_name']
74-
);
75-
$transient->response[ $this->file ] = (object) $plugin;
76-
}
65+
$out_of_date = version_compare( $this->github_response['tag_name'], WC_API_Dev::CURRENT_VERSION, '>' );
66+
if ( $out_of_date ) {
67+
$plugin = array(
68+
'url' => 'https://github.com/woocommerce/wc-api-dev',
69+
'plugin' => $this->file,
70+
'slug' => 'wc-api-dev',
71+
'package' => $this->github_response['zipball_url'],
72+
'new_version' => $this->github_response['tag_name']
73+
);
74+
$transient->response[ $this->file ] = (object) $plugin;
7775
}
76+
7877
return $transient;
7978
}
8079

@@ -128,6 +127,10 @@ public function after_install( $response, $hook_extra, $result ) {
128127
$wp_filesystem->move( $result['destination'], $install_directory );
129128
$result['destination'] = $install_directory;
130129
activate_plugin( $this->file );
130+
131+
// Prevent double notice being displayed. At this point we don't need the plugin injected.
132+
remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'modify_transient' ), 10 );
133+
131134
return $result;
132135
}
133136
}

readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: automattic, woothemes
33
Tags: woocommerce, rest-api, api
44
Requires at least: 4.6
55
Tested up to: 4.8
6-
Stable tag: 0.8.0
6+
Stable tag: 0.8.1
77
License: GPLv2 or later
88
License URI: http://www.gnu.org/licenses/gpl-2.0.html
99

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

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

wc-api-dev.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
*/
3131
class WC_API_Dev {
3232

33+
/**
34+
* Current version of the API plugin.
35+
*/
36+
const CURRENT_VERSION = '0.8.1';
37+
3338
/**
3439
* Minimum version needed to run this version of the API.
3540
*/

0 commit comments

Comments
 (0)