記事内に広告が含まれています

Twitter Digest & brBrbrの変更

WordPress
この記事は約15分で読めます。
 WordPressでプラグインTwitter Digestの表示が気に入らないので、変更。

 まず、何が気に入らないのかというと…
  1. 表示順が、「つぶやき」「投稿時間」「投稿日」となっている。
  2. 改行がやたらと入る。
 以上の2点。

 まず、1.の表示順が、「つぶやき」「投稿時間」「投稿日」となっている。から。

 まぁ、気にしなければ何の問題も無いし、”Twitter Digest”の設定で”1week”にしないで”1day”にして、”Time”を非表示にしちゃえば問題ないんだろうけど、1日でそんなにつぶやかないし、日付が頭に来るべきだろうと思ったので、変更してみる。

 ちなみにオラはPHPに関しての知識はほぼ無いに等しい。
 一応、解説本は持っているが、全ての内容を把握してるかというとほぼ全く頭の中に入っていない(笑)。
 つまり、ほぼ素人です。

プラグインTwitter Digest編

 まずは「つぶやき」の部分を探す。
 twitter-digest.phpの293-338行が、どうも実際の表示にかかわる部分らしい。

// Returns an html formatted $tweet.  This is almost directly borrowed from Twitter Tools
function ws_format_tweet($tweet) {
  
  $output = '<li class="ws_tweet">';
  $output .= ws_make_clickable(wp_specialchars($tweet->text));
  if (!empty($tweet->in_reply_to_screen_name)
      && (!empty($tweet->in_reply_to_status_id))) {
    $output .= 	' <a href="'.ws_status_url($tweet->in_reply_to_screen_name, $tweet->in_reply_to_status_id).'">'.sprintf(__('in reply to %s', 'twitter-digest'), $tweet->in_reply_to_screen_name).'</a>';
  }

  // Show the date/time if the options are selected
  $showTime = get_option('ws_td_showtime');
  if (!$showTime) { $showTime = 0; }
  // Show the date if the option is selectd
  $showDate = get_option('ws_td_showdate');
  if (!$showDate) { $showDate = 0; }
  
  $time_display = '';
  if ($showTime == 1) {
    $time_display = gmdate('H:i:s', $tweet->created_at);

    // Add the comma if we are going to show the date as well
    if ($showDate == 1) {
      $time_display .= ", ";
    }
  }

  // Add the date
  if ($showDate == 1) {
    $time_display .= gmdate('Y-m-d', $tweet->created_at);
  }

  // Use a small arrow for the status links if time and date are off
  if ($showDate != 1 && $showTime != 1) {
    $time_display = "->";
  }

  // Add the status link 
  $username = get_option('ws_td_username'); 
  $output .= ' <a class="ws_tweet_time" href="'.ws_status_url($username, $tweet->id_str).'">'.$time_display.'</a>';

      
  $output .= '</li>';
  return $output;
  
}


 このなかで「つぶやき」の部分はどこかというと…

  $output .= ws_make_clickable(wp_specialchars($tweet->text));
  if (!empty($tweet->in_reply_to_screen_name)
      && (!empty($tweet->in_reply_to_status_id))) {
    $output .= 	' <a href="'.ws_status_url($tweet->in_reply_to_screen_name, $tweet->in_reply_to_status_id).'">'.sprintf(__('in reply to %s', 'twitter-digest'), $tweet->in_reply_to_screen_name).'</a>';
  }


…どうも297-301行らしい。

 さて今度は、「投稿時間」の部分…と思ったら、310-332行の間で「投稿日」と一緒に配列$time_displayとして処理されているみたい。

 配列$time_displayに入れる順番を変えたら良いじゃん!と思ったが、どう変えたら良いんかわからんし、そのうち飽きて表示順をまた変えるかもしれないから、変数にしとけば順序を変えやすいかな?と思い以下を追加。

 313行に、

$tt = gmdate('H:i:s', $tweet->created_at);


…を追加し、323行目に、

$td = gmdate('Y-m-d', $tweet->created_at);


…を追加。

 そして、332行目を、

//  $output .= ' <a class="ws_tweet_time" href="'.ws_status_url($username, $tweet->id_str).'">'.$time_display.'</a>';


…コメントアウトし、333行に、

$output .= ' <a class="ws_tweet_time" href="'.ws_status_url($username, $tweet->id_str).'">'.$td.' '.$tt.'</a>';


…を追加。

 これで、デフォルトの「つぶやき」「投稿時間」「投稿日」から「つぶやき」「投稿日」「投稿時間」になった。

 でも、望む順序は「投稿日」「投稿時間」「つぶやき」であり、ついでに「投稿日」「投稿時間」「つぶやき」の間に改行を入れたいんだよね〜。

 ということで、どうしたかというと「つぶやき」部分の297-301行を333行の後に持って行き、無駄な改行を削除後の324行の</a>直後に<br>を追加するとこんな感じ。

// Returns an html formatted $tweet.  This is almost directly borrowed from Twitter Tools
function ws_format_tweet($tweet) {
$output = &#039;&lt;li class=&quot;ws_tweet&quot;&gt;&#039;;
// Show the date/time if the options are selected
$showTime = get_option(&#039;ws_td_showtime&#039;);
if (!$showTime) { $showTime = 0; }
// Show the date if the option is selectd
$showDate = get_option(&#039;ws_td_showdate&#039;);
if (!$showDate) { $showDate = 0; }
$time_display = &#039;&#039;;
if ($showTime == 1) {
$time_display = gmdate(&#039;H:i:s&#039;, $tweet-&gt;created_at);
$tt = gmdate(&#039;H:i:s&#039;, $tweet-&gt;created_at);
// Add the comma if we are going to show the date as well
if ($showDate == 1) {
$time_display .= &quot;, &quot;;
}
}
// Add the date
if ($showDate == 1) {
$time_display .= gmdate(&#039;Y-m-d&#039;, $tweet-&gt;created_at);
$td = gmdate(&#039;Y-m-d&#039;, $tweet-&gt;created_at);
}
// Use a small arrow for the status links if time and date are off
if ($showDate != 1 &amp;&amp; $showTime != 1) {
$time_display = &quot;-&gt;&quot;;
}
// Add the status link 
$username = get_option(&#039;ws_td_username&#039;); 
// ▼▼順序変更▼▼
//  $output .= &#039; &lt;a class=&quot;ws_tweet_time&quot; href=&quot;&#039;.ws_status_url($username, $tweet-&gt;id_str).&#039;&quot;&gt;&#039;.$time_display.&#039;&lt;/a&gt;&#039;;
$output .= ' <a class="ws_tweet_time" href="'.ws_status_url($username, $tweet->id_str).'">'.$td.' '.$tt.'</a><br>';
// ▼▼位置変更ーここから▼▼
$output .= ws_make_clickable(wp_specialchars($tweet-&gt;text));
if (!empty($tweet-&gt;in_reply_to_screen_name)
&amp;&amp; (!empty($tweet-&gt;in_reply_to_status_id))) {
$output .= 	&#039; &lt;a href=&quot;&#039;.ws_status_url($tweet-&gt;in_reply_to_screen_name, $tweet-&gt;in_reply_to_status_id).&#039;&quot;&gt;&#039;.sprintf(__(&#039;in reply to %s&#039;, &#039;twitter-digest&#039;), $tweet-&gt;in_reply_to_screen_name).&#039;&lt;/a&gt;&#039;;}
// ▲▲ここまで▲▲
$output .= &#039;&lt;/li&gt;&#039;;
return $output;
}


 オラがわかりやすいようにコメントを追加&字下げを削除しちゃってますが…(笑)。
 ついでに改行がやたらと入る対策もしちゃってます。
 それは、プラグインbrBrbrをインストールして有効化しているため、phpコード内の改行にたいしHTMLの改行をやたらと入れているのでは?という推測からです。

 これで改行が、3つから1つに減りました。…が、まだ不完全…。

プラグインbrBrbr編

 残る1つの改行タグの撲滅を目指し、Googleで検索をしていると以下のサイトがヒット♪

 上記サイトを参考に、brBrbr.phpを以下の様に変更。

function brBrbr($brbr) {
	$brbr = str_replace(array("\r\n", "\r"), "\n", $brbr); // cross-platform newlines 
	$brbr = str_replace("\n", "<br />\n", $brbr); // cross-platform newlines 
	$brbr = preg_replace('!(</?(?:table|img|thead|tfoot|caption|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|textarea|input|blockquote|address|p|math|script|h[1-6])[^>]*>)\s*<br />!', "$1", $brbr);
	$brbr = preg_replace('|<blockquote([^>]*)>|i', "</p>\n<blockquote$1><p>", $brbr);
	$brbr = str_replace('</blockquote>', "</p></blockquote>\n<p>", $brbr);
	$brbr = preg_replace('/(<pre.*?>)(.*?)<\/pre>/ise', "clr_br2('$0')", $brbr);
	$brbr = preg_replace('/(<script.*?>)(.*?)<\/script>/ise', "clr_br('$0')", $brbr);
	$brbr = preg_replace('/(<form.*?>)(.*?)<\/form>/ise', "clr_br('$0')", $brbr);
	$brbr = preg_replace('/(<li.*?>)(.*?)<\/li>/ise', "clr_br2('$0')", $brbr);
	$brbr = preg_replace('/(<\/li>)(.*?)<li.*?>/ise', "clr_br('$0')", $brbr);
//	$brbr="<p>\n".$brbr."</p>\n";
	return $brbr; 
}

function clr_br($str){
    $str  = str_replace("<br />","",$str);
    $str  = str_replace('"','"',$str);
    return $str;
}

function clr_br2($str){
    $str  = str_replace("<br />n","",$str);
    $str  = str_replace('"','"',$str);
    return $str;
}


 これで、望み通りの表示が得られました♪あとは、CSSをいじって見た目を変更したいですね(笑)。

【追記】
 プラグインWP SyntaxHighlighterをインストールしたところ、不具合が発生しました。
WP SyntaxHighlighter導入
WP SyntaxHighlighter表示不具合の原因
 というわけで、プラグインbrBRbrは(6/12)現在、停止中です。

コメント

  1. […] Twitter Digest & brBrbrの変更 […]
タイトルとURLをコピーしました