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

Add Auto Update #16

Merged
merged 2 commits into from
Jun 29, 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
133 changes: 133 additions & 0 deletions auto-update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Keeps wc-api-dev up to date based on GitHub releases.
*/
class WC_API_Dev_Updater {

// Plugin file path / slug
protected $file = 'wc-api-dev/wc-api-dev.php';
protected $github_response = null;

/**
* Hooks into the plugin update system.
*/
public function __construct() {
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'modify_transient' ), 10, 1 );
add_filter( 'plugins_api', array( $this, 'plugin_popup' ), 10, 3 );
add_filter( 'auto_update_plugin', array( $this, 'auto_update' ), 10, 2 );
add_filter( 'upgrader_post_install', array( $this, 'after_install' ), 10, 3 );
}

/**
* Adds the wc-api-dev plugin to the list of allowed plugins for auto update.
*/
function auto_update( $update, $item ) {
if ( 'wc-api-dev' === $item->slug ) {
return true;
} else {
return $update;
}
}

/**
* 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 ) ) {
$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;
break;
}
}
}
}
}

/**
* 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();

if (
empty( $this->github_response['tag_name'] ) ||
empty( $checked[ $this->file ] ) ||
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']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid PHP warnings, we should check github_response for these array keys before accessing them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed. For this one, I am now bailing early and not modifying the transient, if we don't have the correct GitHub response.

);
$transient->response[ $this->file ] = (object) $plugin;
}
}
return $transient;
}

/**
* Displays some basic information from our README if the user tries to
* view plugin info for the update via the manual update screen.
*/
public function plugin_popup( $result, $action, $args ) {
if ( ! empty( $args->slug ) ) {
if ( 'wc-api-dev' === $args->slug ) {
$this->maybe_fetch_github_response();
$plugin_data = get_plugin_data( dirname( dirname( __FILE__ ) ) . '/' . $this->file );

// Make sure our GitHub responses are present. If not, we can still show info from the plugin README below.
$tag_name = ! empty( $this->github_response['tag_name'] ) ? $this->github_response['tag_name'] : '';
$published_at = ! empty( $this->github_response['published_at'] ) ? $this->github_response['published_at'] : '';
$body = ! empty( $this->github_response['body'] ) ? $this->github_response['body'] : '';
$zipball_url = ! empty( $this->github_response['zipball_url'] ) ? $this->github_response['zipball_url'] : '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick - WP coding standard is to test the positive first. See: https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#ternary-operator

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeffstieler "An exception would be using ! empty(), as testing for false here is generally more intuitive.".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For shame. That'll learn me to read.


$plugin = array(
'name' => $plugin_data['Name'],
'slug' => $this->file,
'version' => $tag_name,
'author' => $plugin_data['AuthorName'],
'author_profile' => $plugin_data['AuthorURI'],
'last_updated' => $published_at,
'homepage' => $plugin_data['PluginURI'],
'short_description' => $plugin_data['Description'],
'sections' => array(
'Description' => $plugin_data['Description'],
'Updates' => $body,
),
'download_link' => $zipball_url,
);
return (object) $plugin;
}
}
return $result;
}

/**
* Move the updated plugin, which is installed in a temp directory (woocommerce-wc-api-dev-hash)
* to the correct plugin directory, and reactivate the plugin.
*/
public function after_install( $response, $hook_extra, $result ) {
global $wp_filesystem;
if ( 'wc-api-dev/wc-api-dev.php' !== $hook_extra['plugin'] ) {
return $response;
}
$install_directory = plugin_dir_path( __FILE__ );
$wp_filesystem->move( $result['destination'], $install_directory );
$result['destination'] = $install_directory;
activate_plugin( $this->file );
return $result;
}
}
10 changes: 10 additions & 0 deletions wc-api-dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
* Tested up to: 4.7
*/

// This plugin auto updates by default. To disable, set `WC_API_DEV_AUTO_UPDATE` to false.
if ( ! defined( 'WC_API_DEV_AUTO_UPDATE' ) ){
define( 'WC_API_DEV_AUTO_UPDATE', true );
}

if ( defined( 'WC_API_DEV_AUTO_UPDATE' ) && true === WC_API_DEV_AUTO_UPDATE ) {
include_once( plugin_dir_path( __FILE__ ) . 'auto-update.php' );
new WC_API_Dev_Updater;
}

/**
* WC API Dev
* Loads a development version of the WooCommerce REST API.
Expand Down