Skip to content

Commit

Permalink
refactor rollback/branch switch transient set
Browse files Browse the repository at this point in the history
using the 'wp_get_update_data' hook should put an end to the 'plugin or theme is up to date message during a rollback or branch switch
  • Loading branch information
afragen committed Nov 8, 2016
1 parent 6290502 commit 4dd351f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* added ability to update from GitHub release asset
* added our own PHP version check
* oops, added back a reset of _'update\_plugins'_ and _'update\_themes'_ transients with our `delete_all_transients()` which helps to show new updates quicker
* refactored setting of update transient during rollback, should eliminate the _up to date_ message and rollback failures

#### 6.0.0 / 2016-10-26
* added `class Language_Pack` and new repo, [Language Pack Maker](https://github.com/afragen/github-updater-language-pack-maker), to create and update from a separate Language Pack repository.
Expand Down
2 changes: 1 addition & 1 deletion github-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Plugin Name: GitHub Updater
* Plugin URI: https://github.com/afragen/github-updater
* Description: A plugin to automatically update GitHub, Bitbucket, or GitLab hosted plugins, themes, and language packs. It also allows for remote installation of plugins or themes into WordPress.
* Version: 6.0.0.6
* Version: 6.0.0.7
* Author: Andy Fragen
* License: GNU General Public License v2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down
77 changes: 77 additions & 0 deletions src/GitHub_Updater/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,83 @@ protected function get_update_url( $type, $action, $repo_name ) {
return $update_url;
}

/**
* Test if rollback and then run `set_rollback_transient`.
*
* @uses filter hook 'wp_get_update_data'
*
* @param mixed $update_data
*
* @return mixed $update_data
*/
public function set_rollback( $update_data ) {
if ( empty( $_GET['rollback'] ) && ! isset( $_GET['action'] ) ) {
return $update_data;
}

if ( isset( $_GET['plugin'] ) && 'upgrade-plugin' === $_GET['action'] ) {
$slug = dirname( $_GET['plugin'] );
$type = 'plugin';
}

if ( isset( $_GET['theme'] ) && 'upgrade-theme' === $_GET['action'] ) {
$slug = $_GET['theme'];
$type = 'theme';
}

if ( array_key_exists( $slug, $this->config ) ) {
$repo = $this->config[ $slug ];
$this->set_rollback_transient( $type, $repo );
}

return $update_data;
}

/**
* Update transient for rollback or branch switch.
*
* @param string $type plugin|theme
* @param object $repo
*/
private function set_rollback_transient( $type, $repo ) {
switch ( $repo->type ) {
case 'github_plugin':
case 'github_theme':
$this->repo_api = new GitHub_API( $repo );
break;
case 'bitbucket_plugin':
case 'bitbucket_theme':
$this->repo_api = new Bitbucket_API( $repo );
break;
case 'gitlab_plugin':
case 'gitlab_theme':
$this->repo_api = new GitLab_API( $repo );
break;
}

$transient = 'update_' . $type . 's';
$this->tag = $_GET['rollback'];
$slug = 'plugin' === $type ? $repo->slug : $repo->repo;
$updates_transient = get_site_transient( $transient );
$rollback = array(
$type => $slug,
'new_version' => $this->tag,
'url' => $repo->uri,
'package' => $this->repo_api->construct_download_link( false, $this->tag ),
'branch' => $repo->branch,
'branches' => $repo->branches,
);

if ( 'plugin' === $type ) {
$rollback['slug'] = $repo->repo;
$updates_transient->response[ $slug ] = (object) $rollback;
}
if ( 'theme' === $type ) {
$updates_transient->response[ $slug ] = (array) $rollback;
}
set_site_transient( $transient, $updates_transient );
}

/**
* Checks to see if a heartbeat is resulting in activity.
*
Expand Down
23 changes: 2 additions & 21 deletions src/GitHub_Updater/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,27 +229,8 @@ public function get_remote_plugin_meta() {
continue;
}

/*
* Update plugin transient with rollback (branch switching) data.
*/
if ( ! empty( $_GET['rollback'] ) &&
( isset( $_GET['plugin'] ) && $_GET['plugin'] === $plugin->slug )
) {
$this->tag = $_GET['rollback'];
$updates_transient = get_site_transient( 'update_plugins' );
$rollback = array(
'slug' => $plugin->repo,
'plugin' => $plugin->slug,
'new_version' => $this->tag,
'url' => $plugin->uri,
'package' => $this->repo_api->construct_download_link( false, $this->tag ),
);
if ( array_key_exists( $this->tag, $plugin->branches ) ) {
$rollback['new_version'] = '0.0.0';
}
$updates_transient->response[ $plugin->slug ] = (object) $rollback;
set_site_transient( 'update_plugins', $updates_transient );
}
// Update plugin transient with rollback (branch switching) data.
add_filter( 'wp_get_update_data', array( &$this, 'set_rollback' ) );

if ( ( ! is_multisite() || is_network_admin() ) && ! $plugin->release_asset &&
'init' === current_filter() //added due to calling hook for shiny updates, don't show row twice
Expand Down
22 changes: 2 additions & 20 deletions src/GitHub_Updater/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,26 +226,8 @@ public function get_remote_theme_meta() {
continue;
}

/*
* Update theme transient with rollback data.
*/
if ( ! empty( $_GET['rollback'] ) &&
( isset( $_GET['theme'] ) && $theme->repo === $_GET['theme'] )
) {
$this->tag = $_GET['rollback'];
$updates_transient = get_site_transient( 'update_themes' );
$rollback = array(
'theme' => $theme->repo,
'new_version' => $this->tag,
'url' => $theme->uri,
'package' => $this->repo_api->construct_download_link( $this->tag, false ),
);
if ( array_key_exists( $this->tag, $theme->branches ) ) {
$rollback['new_version'] = '0.0.0';
}
$updates_transient->response[ $theme->repo ] = $rollback;
set_site_transient( 'update_themes', $updates_transient );
}
// Update theme transient with rollback data.
add_filter( 'wp_get_update_data', array( &$this, 'set_rollback' ) );

/*
* Add update row to theme row, only in multisite.
Expand Down

0 comments on commit 4dd351f

Please sign in to comment.