-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Override default updates with GitHub release updater
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,76 @@ | ||
<?php | ||
|
||
/** | ||
* Prevent Dones from being considered in theme update checks. | ||
* | ||
* @param array $r An array of HTTP request arguments. | ||
* @param string $url The request URL. | ||
* @return array $r An array of HTTP request arguments. | ||
*/ | ||
function dones_exclude_from_update( $r, $url ) { | ||
$normalized_url = set_url_scheme( $url, 'http' ); | ||
if ( 0 === strpos( $normalized_url, 'http://api.wordpress.org/themes/update-check' ) ) { | ||
$themes = json_decode( $r['body']['themes'], true ); | ||
unset( $themes['themes']['dones'] ); | ||
$r['body']['themes'] = json_encode( $themes ); | ||
} | ||
|
||
return $r; | ||
} | ||
add_filter( 'http_request_args', 'dones_exclude_from_update', 10, 2 ); | ||
|
||
/** | ||
* Performs an update check for the latest version of the theme, returning a | ||
* modified theme updates transient with result. | ||
* | ||
* @param object $value Value of theme updates transient | ||
* @return object $value Modified value of theme updates transient | ||
*/ | ||
function dones_check_for_update( $value ) { | ||
// Bypass update check while installing | ||
if ( defined( 'WP_INSTALLING' ) ) { | ||
return $value; | ||
} | ||
|
||
// Fetch latest version | ||
$response = wp_remote_get( 'https://api.github.com/repos/aduth/dones/releases/latest' ); | ||
if ( is_wp_error( $response ) || ! is_array( $response ) ) { | ||
return $value; | ||
} | ||
|
||
// Skip if same version | ||
$release = json_decode( $response['body'], true ); | ||
$version = $release['tag_name']; | ||
$theme = wp_get_theme(); | ||
if ( $theme->get( 'Version' ) === $version ) { | ||
return $value; | ||
} | ||
|
||
// Find pre-built release zip URL, or bail | ||
foreach ( $release['assets'] as $asset ) { | ||
if ( 'dones.zip' === $asset['name'] ) { | ||
$url = $asset['browser_download_url']; | ||
break; | ||
} | ||
} | ||
|
||
if ( ! isset( $url ) ) { | ||
return $value; | ||
} | ||
|
||
// Initialize pending updates | ||
if ( ! isset( $value->response ) ) { | ||
$value->response = array(); | ||
} | ||
|
||
// Assign update | ||
$value->response['dones'] = array( | ||
'theme' => 'dones', | ||
'new_version' => $version, | ||
'url' => $theme->get( 'ThemeURI' ), | ||
'package' => $url | ||
); | ||
|
||
return $value; | ||
} | ||
add_action( 'pre_set_site_transient_update_themes', 'dones_check_for_update' ); |