前言
~c Typecho c~ 的配置项有个很神奇的特效,就是切换主题后,配置文件会丢失,但是EasyBe
主题有很多配置,一旦丢失再重新配置会比较麻烦,所以特地给主题增加下保存配置文件的操作;
内容
代码
?> 将下面的代码复制到functions.php
中,如果你的搏皮中有自己的一些配置,注意整合下;
!> 我这里是在点击保存设置
按钮的时候,根据$_POST["jqueryConfig"]
来进行备份配置的,这里的判断需要你根据自己的主题换成对应的变量;
// 主题配置
function themeConfig($form) {
// 备份主题配置信息
$name = 'easybe';
$db = Typecho_Db::get();
$themeData = $db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:'.$name))['value'];
$themeBackup = $db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'themeBackup:'.$name))['value'];
if(isset($_POST) && $themeData) {
if($_POST["jqueryConfig"]){
if($themeBackup){
$db->query($db->update('table.options')->rows(array('value'=>$themeData))->where('name = ?', 'themeBackup:'.$name));
}else{
$db->query($db->insert('table.options')->rows(array('name' => 'themeBackup:'.$name,'user' => '0','value' => $themeData)));
}
}
if($_POST["restore"]){
if($themeBackup){
$updateRows= $db->query($db->update('table.options')->rows(array('value'=>$themeBackup))->where('name = ?', 'theme:'.$name));
if ($updateRows) {
echo '<div class="message popup success" style="position: absolute; top: 36px; display: block;"><ul><li>主题备份数据已恢复</li></ul></div>';
echo "<meta http-equiv='refresh' content ='1';url=".getUrl().">";
}
}else{
echo '<div class="message popup error" style="position: absolute; top: 36px; display: block;"><ul><li>不存在主题备份数据,请先进行主题数据备份</li></ul></div>';
echo "<meta http-equiv='refresh' content ='1';url=".getUrl().">";
}
}
}
echo '<form class="protected home col-mb-12" action="" method="post"><ul class="typecho-option" style="position: relative;left: -9px;"><li><label class="typecho-label">主题配置恢复</label><input type="submit" name="restore" class="btn primary" value="恢复主题配置"></li></ul></form>';
}
// 获取当前页面地址
function getUrl() {
$protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
$php_self = $_SERVER['PHP_SELF'] ?? $_SERVER['SCRIPT_NAME'];
$path_info = $_SERVER['PATH_INFO'] ?? '';
$relate_url = $_SERVER['REQUEST_URI'] ?? $php_self.$path_info;
return $protocal.($_SERVER['HTTP_HOST'] ?? '').$relate_url;
}