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

Commit f734013

Browse files
author
Justin Shreve
authored
Add Auto Update (#16)
* Add auto update * Handle PR feedback
1 parent 5ed0c56 commit f734013

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

auto-update.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Keeps wc-api-dev up to date based on GitHub releases.
4+
*/
5+
class WC_API_Dev_Updater {
6+
7+
// Plugin file path / slug
8+
protected $file = 'wc-api-dev/wc-api-dev.php';
9+
protected $github_response = null;
10+
11+
/**
12+
* Hooks into the plugin update system.
13+
*/
14+
public function __construct() {
15+
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'modify_transient' ), 10, 1 );
16+
add_filter( 'plugins_api', array( $this, 'plugin_popup' ), 10, 3 );
17+
add_filter( 'auto_update_plugin', array( $this, 'auto_update' ), 10, 2 );
18+
add_filter( 'upgrader_post_install', array( $this, 'after_install' ), 10, 3 );
19+
}
20+
21+
/**
22+
* Adds the wc-api-dev plugin to the list of allowed plugins for auto update.
23+
*/
24+
function auto_update( $update, $item ) {
25+
if ( 'wc-api-dev' === $item->slug ) {
26+
return true;
27+
} else {
28+
return $update;
29+
}
30+
}
31+
32+
/**
33+
* Fetch the first non pre-release zip from GitHub releases and store the response for later use.
34+
*/
35+
private function maybe_fetch_github_response() {
36+
if ( is_null( $this->github_response ) ) {
37+
$request_uri = 'https://api.github.com/repos/woocommerce/wc-api-dev/releases';
38+
$response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri ) ), true );
39+
if ( is_array( $response ) ) {
40+
foreach ( $response as $entry ) {
41+
if ( false === ( bool ) $entry['prerelease'] ) {
42+
$this->github_response = $entry;
43+
break;
44+
}
45+
}
46+
}
47+
}
48+
}
49+
50+
/**
51+
* Add our plugin to the list of plugins to update, if we find the version is out of date.
52+
*/
53+
public function modify_transient( $transient ) {
54+
if ( property_exists( $transient, 'checked' ) && $transient->checked ) {
55+
$checked = $transient->checked;
56+
$this->maybe_fetch_github_response();
57+
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+
}
65+
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+
}
77+
}
78+
return $transient;
79+
}
80+
81+
/**
82+
* Displays some basic information from our README if the user tries to
83+
* view plugin info for the update via the manual update screen.
84+
*/
85+
public function plugin_popup( $result, $action, $args ) {
86+
if ( ! empty( $args->slug ) ) {
87+
if ( 'wc-api-dev' === $args->slug ) {
88+
$this->maybe_fetch_github_response();
89+
$plugin_data = get_plugin_data( dirname( dirname( __FILE__ ) ) . '/' . $this->file );
90+
91+
// Make sure our GitHub responses are present. If not, we can still show info from the plugin README below.
92+
$tag_name = ! empty( $this->github_response['tag_name'] ) ? $this->github_response['tag_name'] : '';
93+
$published_at = ! empty( $this->github_response['published_at'] ) ? $this->github_response['published_at'] : '';
94+
$body = ! empty( $this->github_response['body'] ) ? $this->github_response['body'] : '';
95+
$zipball_url = ! empty( $this->github_response['zipball_url'] ) ? $this->github_response['zipball_url'] : '';
96+
97+
$plugin = array(
98+
'name' => $plugin_data['Name'],
99+
'slug' => $this->file,
100+
'version' => $tag_name,
101+
'author' => $plugin_data['AuthorName'],
102+
'author_profile' => $plugin_data['AuthorURI'],
103+
'last_updated' => $published_at,
104+
'homepage' => $plugin_data['PluginURI'],
105+
'short_description' => $plugin_data['Description'],
106+
'sections' => array(
107+
'Description' => $plugin_data['Description'],
108+
'Updates' => $body,
109+
),
110+
'download_link' => $zipball_url,
111+
);
112+
return (object) $plugin;
113+
}
114+
}
115+
return $result;
116+
}
117+
118+
/**
119+
* Move the updated plugin, which is installed in a temp directory (woocommerce-wc-api-dev-hash)
120+
* to the correct plugin directory, and reactivate the plugin.
121+
*/
122+
public function after_install( $response, $hook_extra, $result ) {
123+
global $wp_filesystem;
124+
if ( 'wc-api-dev/wc-api-dev.php' !== $hook_extra['plugin'] ) {
125+
return $response;
126+
}
127+
$install_directory = plugin_dir_path( __FILE__ );
128+
$wp_filesystem->move( $result['destination'], $install_directory );
129+
$result['destination'] = $install_directory;
130+
activate_plugin( $this->file );
131+
return $result;
132+
}
133+
}

wc-api-dev.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
* Tested up to: 4.7
1111
*/
1212

13+
// This plugin auto updates by default. To disable, set `WC_API_DEV_AUTO_UPDATE` to false.
14+
if ( ! defined( 'WC_API_DEV_AUTO_UPDATE' ) ){
15+
define( 'WC_API_DEV_AUTO_UPDATE', true );
16+
}
17+
18+
if ( defined( 'WC_API_DEV_AUTO_UPDATE' ) && true === WC_API_DEV_AUTO_UPDATE ) {
19+
include_once( plugin_dir_path( __FILE__ ) . 'auto-update.php' );
20+
new WC_API_Dev_Updater;
21+
}
22+
1323
/**
1424
* WC API Dev
1525
* Loads a development version of the WooCommerce REST API.

0 commit comments

Comments
 (0)