Typecho——主题配置数据备份和恢复

前言

~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;
}

效果

posted @ 2023-02-24 21:44:00 王洋 阅读(3274) 评论(7)

  1. #1楼 2023-03-08 01:08 思索。 Windows 10.0 Edge 110.0.1587.63 中国 上海 上海市 移动


    你好啊,我想看看QQ头像

  2. #2楼 [楼主] 2023-03-06 12:28 王洋 Windows 10.0 Edge 110.0.1587.63 中国 上海 上海市 移动


    hello world

  3. #3楼 [楼主] 2023-03-06 12:27 王洋 Windows 10.0 Edge 110.0.1587.63 中国 上海 上海市 移动


  4. #4楼 [楼主] 2023-03-06 12:23 王洋 Windows 10.0 Edge 110.0.1587.63 中国 上海 上海市 移动


  5. #5楼 [楼主] 2023-03-06 12:23 王洋 Windows 10.0 Edge 110.0.1587.63 中国 上海 上海市 移动


  6. #6楼 [楼主] 2023-03-06 06:26 王洋 Windows 10.0 Edge 110.0.1587.63 中国 上海 上海市 移动


    浏览器显示信息测试

  7. #7楼 [楼主] 2023-03-05 20:42 王洋 Windows 10.0 Edge 110.0.1587.63 中国 上海 上海市 移动


发表评论
昵称
邮箱
网址