Skip to content

Commit

Permalink
Override default updates with GitHub release updater
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed May 21, 2017
1 parent 5f3b56d commit 076c7b9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require( dirname( __FILE__ ) . '/inc/tags.php' );
require( dirname( __FILE__ ) . '/inc/updater.php' );

/**
* Returns the current version of the theme.
Expand Down
76 changes: 76 additions & 0 deletions inc/updater.php
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' );

0 comments on commit 076c7b9

Please sign in to comment.