This repository was archived by the owner on Oct 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Add Auto Update #16
Merged
Merged
Add Auto Update #16
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
); | ||
$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'] : ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.". There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.