升級到 wp-footnotes 1.4
自從啟用 footnotes plug-in 以後,我在編輯時,就方便了許多。不過,原本的 footnotes 0.91 版,還是有一些小問題,讓我有些困擾。所以,剛剛我找了一下官方的 wordpress plug-in directory,發現 footnotes 已經更名為 wp-footnotes。測了一下,並照著原本的改法,將 <span class="footnote"> 的標示法修正進去,再加上修了一些小 bug 後,順利升級到了 wp-footnotes 1.4 版。
在原本的 footnotes 0.91 版裡,是使用 while 迴圈搭配 strpos() 尋找註腳,也許是因為這樣,有時候在後台編輯時,會發生程式陷入無窮迴圈的錯誤。新版的 wp-footnotes 改用 preg_match_all() 搜尋註腳,希望效果會好一些。
不過,也許是因為改用了 preg_match_all(),原本只要修改 $footnote_open 與 $footnote_close 兩個常數變數的技法[1],反而失效了。因此我鑽了一下,發現這兩個常數,在丟給 preg_match_all() 之前,有先用 preg_quote() 先處理過。這本來是件好事,但壞就壞在,preg_quote() 預設會替很多 delimiters 前面加反斜線,包括 < 與 > 等。然而,在 preg_match_all() 這行的 regular expression 裡,FOOTNOTE_OPEN 與 FOOTNOTE_CLOSE 所在的部份,其實式不需要替 < 與 > 加反斜線的,因而導致程式沒辦法正確地抓出註腳來。因此,我們應該告訴 preg_quote(),究竟哪些 delimiters 應該要加反斜線。改法如下:
function swas_footnote($data) {
global $post, $current_settings;
// Check for and setup the starting number
$start_number = (preg_match("|<!\-\-startnum=(\d+)\-\->|",$data,$start_number_array)==1) ? $start_number_array[1] : 1;
// Let's attempt this with Regex instead
preg_match_all("/(".preg_quote(FOOTNOTE_OPEN,'/()')."|<footnote>)(.*)(".preg_quote(FOOTNOTE_CLOSE,'/()')."|<\/footnote>)/U", $data, $fo otnotes, PREG_SET_ORDER);
...
}
將紅色的部份加上即可。這樣一來,我們就可以直接改程式一開頭定義 FOOTNOTE_OPEN 與 FOOTNOTE_CLOSE 的地方,將註腳的包裹方式,換成我們的方式。如下:
define('FOOTNOTE_OPEN', '<span class="footnote">');
define('FOOTNOTE_CLOSE', '</span>');
不過,搜尋一下,可以發現原來的 (( 與 )),在 swas_footnote() 的最尾端,被 hard code 進程式碼裡了,這當然是錯誤的。因此,將之修改成使用 FOOTNOTE_OPEN 與 FOOTNOTE_CLOSE 常數。如下:
function swas_footnote($data) {
....
$data = str_replace($note[0], $link_text, $data);
$data = str_replace(' ((ref:' . ($identifer+1) . '))', $link_text, $data);
$data = str_replace(FOOTNOTE_OPEN.'ref:' . ($identifer+1) . FOOTNOTE_CLOSE, $link_text, $data);
$data = str_replace('<footnote>' . ($identifer+1) . '</footnote>', $link_text, $data);
}
$data = $data.'</ol>'.$current_settings['post_footnotes'];
}else{
foreach($footnotes as $identifier=>$note){
$data = str_replace($note[0], '', $data);
$data = str_replace(' ((ref:' . ($identifer+1) . '))', '', $data);
$data = str_replace(FOOTNOTE_OPEN.'ref:' . ($identifer+1) . FOOTNOTE_CLOSE, '', $data);
$data = str_replace('<footnote>' . ($identifer+1) . '</footnote>', '', $data);
}
}
}
return $data;
}
另外,我在後端 note identifier options 那邊,將 before/after 設定成方括號,但在前端卻出不來。這又是一個小 bug,檢查了一下,發現原來是儲存這組設定的 pre_identifier 與 post_identifier,在真正要用時,卻被寫成了 pre_link 和 post_link。這應該是程式修改時所造成的疏漏,因為在舊版裡,這組設定確實是被叫做 pre_link 和 post_link。如下修正之即可:
function swas_footnote($data) {
....
if ($display) {
$data = $data.$current_settings['pre_footnotes'].'<ol start="'.$start_number.'" class="footnotes">';
foreach($footnotes as $identifier=>$note){
$number = $start_number+$identifier;// What number is it? Used for back link and id.
$data = $data.'<li id="footnote-'.$number.'-'.$post->ID.'" class="footnote">'.$note[2].$current_settings['pre_backlink'].'<a href="#footnote-link-'.$number.'-'.$post->ID.'" class="footnote-link footnote-back-link">'.$current_settings['backlink'].'</a>'.$current_settings['post_backlink'].'</li>';
$link_text = ($current_settings['list_style_type'] != 'decimal') ? swas_convert_num($number, $current_settings['list_style_type']) : $number;
$link_text = $current_settings['pre_link'].'<a href="#footnote-'.$number.'-'.$post->ID.'" id="footnote-link-'.$number.'-'.$post->ID.'" class="footnote-link footnote-identifier-link" title="'.strip_tags($footnotes[$number-1][2]).'">'.$link_text.'</a>'.$current_settings['post_link'];
$link_text = $current_settings['pre_identifier'].'<a href="#footnote-'.$number.'-'.$post->ID.'" id="footnote-link-'.$number.'-'.$post->ID.'" class="footnote-link footnote-identifier-link" title="'.strip_tags($footnotes[$number-1][2]).'">'.$link_text.'</a>'.$current_settings['post_identifier'];
if ($current_settings['superscript']) $link_text = '<sup class="footnote-link-sup footnote-identifier-link-sup">'.$link_text.'</sup>';
....
}
最後,再依照跑出來的 CSS classes,設定相對應的 styles 即可[2]。
- 在新版裡,已經改用
define()使之成為真正的常數。 ↩ - 我參考了一下這篇《CSS Horizontal Rules》。 ↩



One Backlink
wp-footnote 1.4 有個奇怪的 bug。它在 Wordpress 後端的設定介面里有個叫 note identifier options 的設定,我在該設定里將 Before/After 設定成方括號,但在前端網頁卻沒顯示出來。強者 jeffhung 寫的「升級到 wp-footnotes 1.4」一文中在最後兩段有提到如何修正之。 但這次搬家時,抓下來 wp-footnote 1.4 里的 footnote.php 檔案的 code 被作者改過了[1],所以現在這個 bug 沒辦法按照
Post a Comment