我正在制作一个可通过github更新的主题。https://github.com/pallazzio/skeleton
我试图使用smashing杂志的插件更新程序教程,改为更新主题。https://www.smashingmagazine.com/2015/08/deploy-wordpress-plugins-with-github-using-transients/
我几乎成功了。更新将下载并安装到正确的文件夹中。然而,它并没有让同一主题保持活动状态,而是激活了一个新主题,其名称是pallazzio-skeleton-b203a7f。
奇怪的是,没有使用该名称创建文件夹。将文件安装到正确的位置。一旦我将活动主题切换回原始主题名称,更新就可以完全正常运行。
我唯一需要完成的是如何确保当前活动主题不会更改为随机名称。我卡住了。
任何帮助都将不胜感激。
下面是函数中的相关部分。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();
以下是更新程序的内容。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;
}
}
}
?>