I am making a theme that will be updateable via github. https://github.com/pallazzio/skeleton
I am trying to use the plugin updater tutorial from smashing magazine, modified for updating a theme instead. https://www.smashingmagazine.com/2015/08/deploy-wordpress-plugins-with-github-using-transients/
I got it almost working. The update gets downloaded and installed to the correct folder. However, instead of leaving the same theme active, it is activating a new theme whose name is pallazzio-skeleton-b203a7f.
The weird part is that there is no folder created with that name. The files get installed to the correct place. The update is fully functional once I switch the active theme back to the original theme name.
The only thing I need to finish is how to make sure the current active theme doesn't change to a random name. I'm stuck.
Any help would be greatly appreciated.
Here is the relevant part in functions.php
// Init theme updater
if( ! class_exists( 'Pallazzio_Theme_Updater' ) ){
include_once( 'updater.php' );
}
$updater = new Pallazzio_Theme_Updater( 'skeleton' );
$updater->set_username( 'pallazzio' );
$updater->set_repository( 'skeleton' );
//$updater->authorize( 'abcdefghijk1234567890' ); // auth code for private repos
$updater->initialize();
Here is the contents of updater.php
<?php
class Pallazzio_Theme_Updater {
private $theme;
private $username;
private $repository;
private $authorize_token;
private $github_response;
public function __construct( $theme ) {
$this->theme = wp_get_theme( $theme );
return $this;
}
public function set_username( $username ) {
$this->username = $username;
}
public function set_repository( $repository ) {
$this->repository = $repository;
}
public function authorize( $token ) {
$this->authorize_token = $token;
}
public function initialize() {
add_filter( 'pre_set_site_transient_update_themes', array( $this, 'modify_transient' ), 10, 1 );
add_filter( 'upgrader_post_install', array( $this, 'after_install' ), 10, 3 );
}
public function modify_transient( $transient ) {
if( property_exists( $transient, 'checked') ) { // Check if transient has a checked property
if( $checked = $transient->checked ) { // Did Wordpress check for updates?
$this->get_repository_info(); // Get the repo info
$out_of_date = version_compare( $this->github_response['tag_name'], $checked[ $this->theme->template ], 'gt' ); // Check if we're out of date
if( $out_of_date ) {
$new_files = $this->github_response['zipball_url']; // Get the ZIP
$theme = array( // setup our theme info
'theme' => $this->theme->template,
'url' => $this->theme->get( 'ThemeURI' ),
'package' => $new_files,
'new_version' => $this->github_response['tag_name']
);
$transient->response[$this->theme->template] = $theme; // Return it in response
}
}
}
return $transient; // Return filtered transient
}
public function after_install( $response, $hook_extra, $result ) {
global $wp_filesystem; // Get global FS object
$install_directory = get_template_directory(); // Our theme directory
$result['destination_name'] = $this->theme->template; // Set the destination name for the rest of the stack
$result['remote_destination'] = $install_directory; // Set the remote destination for the rest of the stack
$wp_filesystem->move( $result['destination'], $install_directory ); // Move files to the theme dir
$result['destination'] = $install_directory; // Set the destination for the rest of the stack
//switch_theme( $this->theme->template );
return $result;
}
private function get_repository_info() {
if ( is_null( $this->github_response ) ) { // Do we have a response?
$request_uri = sprintf( 'https://api.github.com/repos/%s/%s/releases', $this->username, $this->repository ); // Build URI
if( $this->authorize_token ) { // Is there an access token?
$request_uri = add_query_arg( 'access_token', $this->authorize_token, $request_uri ); // Append it
}
$response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri ) ), true ); // Get JSON and parse it
if( is_array( $response ) ) { // If it is an array
$response = current( $response ); // Get the first item
}
if( $this->authorize_token ) { // Is there an access token?
$response['zipball_url'] = add_query_arg( 'access_token', $this->authorize_token, $response['zipball_url'] ); // Update our zip url with token
}
$this->github_response = $response;
}
}
}
?>