三月 29 2007

Profile Image of evenrain
evenrain

修改 Twitter Update

Posted at 12:12:30 under blog

先前有提到 WordPress 的一個 Twitter Plugin -- Twitter Update。實際在 2.1.2 使用時,發現了幾個奇怪的地方。首先是如果你勾選了「Update Twitter when the an old post has been edited」,理論上應該在修改舊文章時才會發出 Tweets,但是實際使用時卻會發現文章尚未發佈,卻持續送出更新訊息。另外即使勾選了「Update Twitter when the new post is published」,發佈新文章時,卻反而不會送出 Tweets。

稍微研究了一下 twitter_update.php 這隻程式,發覺它判斷新文章與否,是以文章的前一個狀態是否為草稿作為依據,也就是:

PHP:
  1. if ($_POST['prev_status'] == 'draft') {
  2. // is new post
  3. } else {
  4. // is old post
  5. }

但是 WordPress 2.1 之後,多了 Autosave 的功能,在你編輯草稿時,系統會在背後幫你做自動儲存。而編輯草稿透過 Autosave 儲存時,一樣也會觸發 Twitter Update,但此時文章的 prev_status 卻是空值,因此會造成程式判斷該文章為舊文章。所以先前才會發生我文章還沒發佈,卻送出舊文章修改的 Tweets 問題。

最簡單的方式,就是修改判斷式。首先在 twitter_update.php 中找到 function vc_twit,然後將

PHP:
  1. if ($_POST['prev_status'] == 'draft') {

取代為

PHP:
  1. if ($_POST['prev_status'] != 'publish') {

即可。

另外,在判斷是否發佈的地方,原程式是寫做

PHP:
  1. if ($_POST['publish'] == 'publish') {

這個寫法在非英語系的 WordPress 都會有問題。在中文化的 WordPress 中,發佈文章後,$_POST['publish'] 傳回來的會是中文的「發表」,將會造成程式判斷為尚在修改新文章中,而送出「Update Twitter when the new post is edited (re-saved but not published)」選項中設定的訊息。

修改的方式,是將上面那段程式碼修改為:

PHP:
  1. if ( isset($_POST['publish']) ) {

修改過後 vc_twit function 的完整程式碼如下:

PHP:
  1. function vc_twit($post_ID)  {
  2.    $twitterURI = "/statuses/update.xml";
  3.    $thisposttitle = $_POST['post_title'];
  4.    $thispostlink = get_permalink($post_ID);
  5.    $sentence = ''
  6.    
  7.     //is new post
  8.     if($_POST['prev_status'] != 'publish'){
  9.  
  10.         if ( isset($_POST['publish']) ) {
  11.             // publish new post
  12.             if(get_option('newpost-published-update') == '1'){
  13.                 $sentence = get_option('newpost-published-text');
  14.                 if(get_option('newpost-published-showlink') == '1'){
  15.                     $thisposttitle = $thisposttitle . ' ( ' . $thispostlink . ' )';
  16.                 }
  17.                 $sentence = str_replace ( '#title#', $thisposttitle, $sentence);
  18.             }
  19.         }else if($_POST['action'] == 'post'){
  20.             // create new post
  21.             if(get_option('newpost-created-update') == '1'){
  22.                 $sentence = get_option('newpost-created-text');
  23.                 $sentence = str_replace ( '#title#', $thisposttitle, $sentence);
  24.             }
  25.         }else{
  26.             // edit new post
  27.             if(get_option('newpost-edited-update') == '1'){
  28.                 $sentence = get_option('newpost-edited-text');
  29.                 $sentence = str_replace ( '#title#', $thisposttitle, $sentence);
  30.             }
  31.         }
  32.     }else{
  33.         // is old post
  34.         if(get_option('oldpost-edited-update') == '1'){   
  35.             $sentence = get_option('oldpost-edited-text');
  36.             if(get_option('oldpost-edited-showlink') == '1'){
  37.                 $thisposttitle = $thisposttitle . ' ( ' . $thispostlink . ' )';
  38.             }
  39.             $sentence = str_replace ( '#title#', $thisposttitle, $sentence);
  40.         }
  41.     }
  42.    
  43.     if($sentence != ''){
  44.         $sendToTwitter = vc_doTwitterAPIPost('status='.$sentence, $twitterURI);
  45.     }
  46.  
  47.    return $post_ID;
  48. }

標籤:, ,

相關文章

2 responses so far

2 Responses to “修改 Twitter Update”

  1. 小奧on 15 六月 2007 at 21:31:44 1

    升級到2.2版本,也按你所謂而修改draft 為publish,但依然不行,是因為dreamhost的關係嗎

  2. evenrainon 15 六月 2007 at 22:31:41 2

    我也是使用 Dreamhost,
    上面有兩個修改喔,
    不只是把 draft 改成 publish 而已。

Trackback URI | Comments RSS

Leave a Reply