前面讲了wordpress插件的自定义更新服务器来实现更新,今天讲讲 WordPress 主题的自定义主题更新类似教程。
整个教程包括两个步骤——第一步我们将配置我们的自定义更新服务,第二步——实际创建一个主题。
配置更新文件服务器
实际上,这听起来要简单得多。我们需要做的就是在您的主机上创建两个文件,这就足够了!
一个info.json文件存储更新数据,一个主题文件misha-theme.zip。
文件的内容info.json可以完全自定义。但很明显,它应该包含主题存档的 URL,以及新主题的版本。让我们添加带有主题更新信息的页面的 URL。
{
"version": "2.0",
"details_url": "https://rudrastyh.com/themes/misha-theme/changelog.html",
"download_url": "https://rudrastyh.com/themes/misha-theme/2.0.zip"
}许可证检查
我刚刚向您展示了一个静态info.json文件,该文件已经包含我们需要的所有内容,但是如果您想检查是否真的可以安装此特定更新怎么办?例如,如果主题更新许可证已过期。
在这种情况下,您的info.json文件应转换为info.phpfile.
$update = array(
'version' => '2.0',
'details_url' => 'https://rudrastyh.com/themes/misha-theme/changelog.html',
'download_url' => ''
);
if( ! empty( $_GET[ 'license_key' ] ) && license_check_logic( $_GET[ 'license_key' ] ) {
$update[ 'download_url' ] = 'https://rudrastyh.com/themes/misha-theme/2.0.zip';
}
header( 'Content-Type: application/json' );
echo json_encode( $update );license_check_logic()此处是一个自定义功能,允许检查$_GET[ 'license_key' ]提供的许可证密钥是否正确且未过期。本教程中提供了更多信息。
获取更新信息并从您的自定义服务器进行更新
这部分甚至比插件更新器中的相同部分还要简单。site_transient_update_themes在这里使用过滤器钩子就足够了。
请记住,本章不涉及性能。稍后我们将讨论性能。
add_filter( 'site_transient_update_themes', 'misha_update_themes' );
function misha_update_themes( $transient ) {
// let's get the theme directory name
// it will be "misha-theme"
$stylesheet = get_template();
// now let's get the theme version
// but maybe it is better to hardcode it in a constant
$theme = wp_get_theme();
$version = $theme->get( 'Version' );
// connect to a remote server where the update information is stored
$remote = wp_remote_get(
'https://rudrastyh.com/wp-content/uploads/theme-updater/info.json',
array(
'timeout' => 10,
'headers' => array(
'Accept' => 'application/json'
)
)
);
// do nothing if errors
if(
is_wp_error( $remote )
|| 200 !== wp_remote_retrieve_response_code( $remote )
|| empty( wp_remote_retrieve_body( $remote ) )
) {
return $transient;
}
// encode the response body
$remote = json_decode( wp_remote_retrieve_body( $remote ) );
if( ! $remote ) {
return $transient; // who knows, maybe JSON is not valid
}
$data = array(
'theme' => $stylesheet,
'url' => $remote->details_url,
'requires' => $remote->requires,
'requires_php' => $remote->requires_php,
'new_version' => $remote->version,
'package' => $remote->download_url,
);
// check all the versions now
if(
$remote
&& version_compare( $version, $remote->version, '<' )
&& version_compare( $remote->requires, get_bloginfo( 'version' ), '<' )
&& version_compare( $remote->requires_php, PHP_VERSION, '<' )
) {
$transient->response[ $stylesheet ] = $data;
} else {
$transient->no_update[ $stylesheet ] = $data;
}
return $transient;
}这已经足以让您的自定义主题从您的自定义服务器接收更新。
但是,您可能会注意到所有管理页面的加载速度开始变慢。所以你可以缓存请求数据,使其不必每次都请求更新数据。
效果
问题是过滤器挂钩site_transient_update_themes在每个管理页面上运行多次。
我们能做什么?下面是我自己的解决方案。也许它并不完美,但我愿意接受你们的建议。我决定使用临时缓存,并且我使用当前插件版本作为缓存键的一部分。
if( false == $remote = get_transient( 'misha-theme-update'.$version ) ) {
// connect to a remote server where the update information is stored
$remote = wp_remote_get(
'https://rudrastyh.com/wp-content/uploads/theme-updater/info.json',
array(
'timeout' => 10,
'headers' => array(
'Accept' => 'application/json'
)
)
);
// do nothing if errors
if(
is_wp_error( $remote )
|| 200 !== wp_remote_retrieve_response_code( $remote )
|| empty( wp_remote_retrieve_body( $remote ) )
) {
return $transient;
}
$remote = json_decode( wp_remote_retrieve_body( $remote ) );
if( ! $remote ) {
return $transient; // who knows, maybe JSON is not valid
}
set_transient( 'misha-theme-update'.$version, $remote, HOUR_IN_SECONDS );
}






评论 (0)