來(lái)源于:
https://qqdie.com/archives/typecho-templates-backup-and-restore.html
typecho模板設(shè)置數(shù)據(jù)會(huì)在你換模板的時(shí)候被清空,這樣的設(shè)置其實(shí)我還是蠻喜歡的,不會(huì)有數(shù)據(jù)殘留。
然而WP用戶轉(zhuǎn)到typecho后跟我抱怨,說(shuō)模板換成別的然后再切換回來(lái),之前設(shè)置好的數(shù)據(jù)都沒(méi)了,希望我在yodu模板上動(dòng)動(dòng)手腳解決這個(gè)問(wèn)題。
然而我是有些遲疑的,東西不是你說(shuō)做我就要做的,我必須試一下…否則答應(yīng)太快又搞不出來(lái),豈不是很難受!
不過(guò)既然發(fā)了這篇文章,就說(shuō)明已經(jīng)搞定了

代碼
在themeConfig($form)
函數(shù)里添加
$db = Typecho_Db::get();
$sjdq=$db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodu'));
$ysj = $sjdq['value'];
if(isset($_POST['type']))
{
if($_POST["type"]=="備份模板數(shù)據(jù)"){
if($db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'))){
$update = $db->update('table.options')->rows(array('value'=>$ysj))->where('name = ?', 'theme:Yodubf');
$updateRows= $db->query($update);
echo '<div class="tongzhi">備份已更新,請(qǐng)等待自動(dòng)刷新!如果等不到請(qǐng)點(diǎn)擊';
?>
<a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">這里</a></div>
<script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2500);</script>
<?php
}else{
if($ysj){
$insert = $db->insert('table.options')->rows(array('name' => 'theme:Yodubf','user' => '0','value' => $ysj));
$insertId = $db->query($insert);
echo '<div class="tongzhi">備份完成,請(qǐng)等待自動(dòng)刷新!如果等不到請(qǐng)點(diǎn)擊';
?>
<a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">這里</a></div>
<script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2500);</script>
<?php
}
}
}
if($_POST["type"]=="還原模板數(shù)據(jù)"){
if($db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'))){
$sjdub=$db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'));
$bsj = $sjdub['value'];
$update = $db->update('table.options')->rows(array('value'=>$bsj))->where('name = ?', 'theme:Yodu');
$updateRows= $db->query($update);
echo '<div class="tongzhi">檢測(cè)到模板備份數(shù)據(jù),恢復(fù)完成,請(qǐng)等待自動(dòng)刷新!如果等不到請(qǐng)點(diǎn)擊';
?>
<a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">這里</a></div>
<script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2000);</script>
<?php
}else{
echo '<div class="tongzhi">沒(méi)有模板備份數(shù)據(jù),恢復(fù)不了哦!</div>';
}
}
if($_POST["type"]=="刪除備份數(shù)據(jù)"){
if($db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Yodubf'))){
$delete = $db->delete('table.options')->where ('name = ?', 'theme:Yodubf');
$deletedRows = $db->query($delete);
echo '<div class="tongzhi">刪除成功,請(qǐng)等待自動(dòng)刷新,如果等不到請(qǐng)點(diǎn)擊';
?>
<a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">這里</a></div>
<script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2500);</script>
<?php
}else{
echo '<div class="tongzhi">不用刪了!備份不存在!!!</div>';
}
}
}
echo '<form class="protected" action="?yodubf" method="post">
<input type="submit" name="type" class="btn btn-s" value="備份模板數(shù)據(jù)" /> <input type="submit" name="type" class="btn btn-s" value="還原模板數(shù)據(jù)" /> <input type="submit" name="type" class="btn btn-s" value="刪除備份數(shù)據(jù)" /></form>';
SQL
然后將里面出現(xiàn)的所有“yodu”改成你的模板目錄的名字,如果拿不準(zhǔn)就去數(shù)據(jù)庫(kù)里看看模板的值名字。
備份
當(dāng)用戶點(diǎn)擊備份時(shí),先判斷是否已經(jīng)存在備份,如果不存在就插入一條新的數(shù)據(jù),數(shù)據(jù)name為yodubf,value為模板原本的數(shù)據(jù)。此時(shí)就存在了一條備份數(shù)據(jù)。
如果再次點(diǎn)擊備份按鈕會(huì)發(fā)生什么呢?會(huì)觸發(fā)更新數(shù)據(jù)的語(yǔ)句,就是讀取模板的設(shè)置數(shù)據(jù),然后將備份的模板數(shù)據(jù)更新。
還原
當(dāng)用戶點(diǎn)擊還原按鈕時(shí),會(huì)判斷是否存在備份,如果不存在就發(fā)出提示說(shuō)不存在數(shù)據(jù)無(wú)法恢復(fù);如果存在,就會(huì)進(jìn)行一個(gè)反向的更新操作,將備份的數(shù)據(jù)更新到模板默認(rèn)設(shè)置數(shù)據(jù)。
這個(gè)操作完成后會(huì)觸發(fā)個(gè)小問(wèn)題,比較影響體驗(yàn)的。就是在點(diǎn)擊還原按鈕時(shí)網(wǎng)頁(yè)是先刷新后執(zhí)行php還原語(yǔ)句的,也就是說(shuō)還原完成后,你看到的模板設(shè)置頁(yè)面數(shù)據(jù)并沒(méi)有還原,但是實(shí)際數(shù)據(jù)庫(kù)里面已經(jīng)還原好了的,這一點(diǎn)很影響體驗(yàn)。
于是乎,我雞賊的弄了個(gè)js自動(dòng)刷新語(yǔ)句,并發(fā)出提示文字,這樣一下子就友好多了,注意文章中代碼方面我并未給出css樣式,所以美觀度上需要自行優(yōu)化。
刪除
刪除就簡(jiǎn)單了,判斷是否存在備份,不存在就告訴用戶不用刪了,你壓根就沒(méi)有備份數(shù)據(jù),如果有備份就執(zhí)行刪除語(yǔ)句,發(fā)出提示。
一些沒(méi)用的說(shuō)明
1,其實(shí)這東西應(yīng)該可以寫(xiě)成懶人版的,模板名字什么的用php獲取下,就不用我這樣寫(xiě)死了,但是當(dāng)時(shí)我處于試一試的心態(tài)寫(xiě)的,所以就能簡(jiǎn)單就簡(jiǎn)單了,現(xiàn)在又懶得弄了,要不是為了水文,這個(gè)我都懶得貼出來(lái)。
2,別看文章中代碼這么亂,條例就不清晰,其實(shí)我當(dāng)時(shí)找了張紙寫(xiě)的邏輯然后才按照順序一步一步的寫(xiě)的,也測(cè)試了很多回。
3,最開(kāi)始想寫(xiě)自動(dòng)還原模板數(shù)據(jù)來(lái)著,就是檢測(cè)到模板啟用就自動(dòng)還原曾經(jīng)的備份數(shù)據(jù),然而當(dāng)時(shí)想不通如果去判斷模板啟用。
4,當(dāng)你想將本文章代碼投入使用時(shí),最好再測(cè)試博客進(jìn)行測(cè)試,以免傷害你的數(shù)據(jù)庫(kù),同時(shí)建議測(cè)試時(shí)打開(kāi)數(shù)據(jù)庫(kù)管理頁(yè)面,觀看數(shù)據(jù)庫(kù)對(duì)應(yīng)表的變化
網(wǎng)頁(yè)下載(防代碼丟失):
備用網(wǎng)頁(yè)
這個(gè)主題就使用了這個(gè)功能