From da1e63570cbeb0dbb47065f09c36ff5e8bb6308d Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Wed, 28 Jun 2017 11:37:45 -0700 Subject: [PATCH 1/2] Add auto update --- auto-update.php | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ wc-api-dev.php | 10 +++++ 2 files changed, 123 insertions(+) create mode 100644 auto-update.php diff --git a/auto-update.php b/auto-update.php new file mode 100644 index 0000000..1f5e83b --- /dev/null +++ b/auto-update.php @@ -0,0 +1,113 @@ +slug, array( 'wc-api-dev' ) ) ) { + 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(); + $out_of_date = version_compare( $this->github_response['tag_name'], $checked[ $this->file ], 'gt' ); + 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; + } + + /** + * 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 ( $args->slug === current( explode( '/' , $this->file ) ) ) { + $this->maybe_fetch_github_response(); + $plugin = get_plugin_data( dirname( dirname( __FILE__ ) ) . '/' . $this->file ); + $plugin = array( + 'name' => $plugin['Name'], + 'slug' => $this->file, + 'version' => $this->github_response['tag_name'], + 'author' => $plugin['AuthorName'], + 'author_profile' => $plugin['AuthorURI'], + 'last_updated' => $this->github_response['published_at'], + 'homepage' => $plugin['PluginURI'], + 'short_description' => $plugin['Description'], + 'sections' => array( + 'Description' => $plugin['Description'], + 'Updates' => $this->github_response['body'], + ), + 'download_link' => $this->github_response['zipball_url'] + ); + return (object) $plugin; + } + } + return $result; + } + + /** + * Move the updated plugin to the correct directory, and reactivate the plugin. + */ + public function after_install( $response, $hook_extra, $result ) { + global $wp_filesystem; + $install_directory = plugin_dir_path( __FILE__ ); + $wp_filesystem->move( $result['destination'], $install_directory ); + $result['destination'] = $install_directory; + activate_plugin( $this->file ); + return $result; + } +} diff --git a/wc-api-dev.php b/wc-api-dev.php index 60c5cf6..432bd53 100644 --- a/wc-api-dev.php +++ b/wc-api-dev.php @@ -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. From 286edc3947048509c2121aedcd0490f21aae5845 Mon Sep 17 00:00:00 2001 From: Justin Shreve Date: Wed, 28 Jun 2017 12:54:27 -0700 Subject: [PATCH 2/2] Handle PR feedback --- auto-update.php | 60 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/auto-update.php b/auto-update.php index 1f5e83b..38cabab 100644 --- a/auto-update.php +++ b/auto-update.php @@ -22,7 +22,7 @@ public function __construct() { * Adds the wc-api-dev plugin to the list of allowed plugins for auto update. */ function auto_update( $update, $item ) { - if ( in_array( $item->slug, array( 'wc-api-dev' ) ) ) { + if ( 'wc-api-dev' === $item->slug ) { return true; } else { return $update; @@ -35,9 +35,9 @@ function auto_update( $update, $item ) { 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 ); + $response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri ) ), true ); if ( is_array( $response ) ) { - foreach( $response as $entry ) { + foreach ( $response as $entry ) { if ( false === ( bool ) $entry['prerelease'] ) { $this->github_response = $entry; break; @@ -51,11 +51,20 @@ private function maybe_fetch_github_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 ) { + if ( property_exists( $transient, 'checked' ) && $transient->checked ) { $checked = $transient->checked; $this->maybe_fetch_github_response(); - $out_of_date = version_compare( $this->github_response['tag_name'], $checked[ $this->file ], 'gt' ); - if( $out_of_date ) { + + 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, @@ -75,23 +84,30 @@ public function modify_transient( $transient ) { */ public function plugin_popup( $result, $action, $args ) { if ( ! empty( $args->slug ) ) { - if ( $args->slug === current( explode( '/' , $this->file ) ) ) { + if ( 'wc-api-dev' === $args->slug ) { $this->maybe_fetch_github_response(); - $plugin = get_plugin_data( dirname( dirname( __FILE__ ) ) . '/' . $this->file ); - $plugin = array( - 'name' => $plugin['Name'], + $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'] : ''; + + $plugin = array( + 'name' => $plugin_data['Name'], 'slug' => $this->file, - 'version' => $this->github_response['tag_name'], - 'author' => $plugin['AuthorName'], - 'author_profile' => $plugin['AuthorURI'], - 'last_updated' => $this->github_response['published_at'], - 'homepage' => $plugin['PluginURI'], - 'short_description' => $plugin['Description'], + '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['Description'], - 'Updates' => $this->github_response['body'], + 'Description' => $plugin_data['Description'], + 'Updates' => $body, ), - 'download_link' => $this->github_response['zipball_url'] + 'download_link' => $zipball_url, ); return (object) $plugin; } @@ -100,10 +116,14 @@ public function plugin_popup( $result, $action, $args ) { } /** - * Move the updated plugin to the correct directory, and reactivate the plugin. + * 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;