∵ Takayoshi Saito ∴ 2013-06-19 ∞ 3'
WordPressのFeedBack ChampuruプラグインがTwitter API Ver1.1に対応していなくて、6月14日ごろからTwitterのコメントが取得できなくなっていたので、ちょっと改造してOAuthでつぶやきを取ってくるようにしました。
臨時対処でちょっとハードコーディングになってしまうのですが、Feddback Champuruプラグインの_get_twitter関数を以下のように書き換えました。
private function _get_twitter($type, $post_id, $permalink, $get_new = false){ $cache = $this->_get_cache($type, $post_id); if ( !$get_new ) return $cache["comments"];
$comments = array(); foreach ( $cache["comments"] as $cache_comment ) { $id = preg_replace('#https?://twitter.com/[^/]+/status/#i', '', $cache_comment->comment_author_url); $cache_comment->comment_ID = $type . '-' . $id; $comments[$id] = $cache_comment; }
require_once("twitter/oauth/twitteroauth/twitteroauth.php");
$consumer_key = ""; $consumer_secret = ""; $access_token = ""; $access_token_secret = "****"; $url = urlencode("$permalink"); $url="https://api.twitter.com/1.1/search/tweets.json?q=$url&result_type=mixed&count=4";
$to = new TwitterOAuth($consumer_key,$consumer_secret,$access_token,$access_token_secret); $req = $to->OAuthRequest($url,'GET',array('count'=>'100'));
if ($req !== false){ $json = json_decode($req); $json = (array)$json; for ($i = 1; $iuser->name; $author_url = "https://twitter.com/".$json['statuses'][$i]->user->screen_name; $datetime = (int) strtotime($json[$i]->created_at); $content = $json['statuses'][$i]->text; $photo_url = $json['statuses'][$i]->user->profile_image_url; $id_str = $json['statuses'][$i]->user->id_str;
$content = apply_filters($this->plugin_name.'/content', $content, $type, $author, $author_url, $datetime, $photo_url, $json[$i]['statuses']->user->id_str);
if ( $content ) $comments[$i] = $this->comment_build($type, $post_id, $author, $author_url, $datetime, $content, $photo_url, $id_str); } $comments = (count($comments) time() + apply_filters($this->plugin_name.'/cache_expired', $this->cache_expired * 60, $post_id) , "comments" => $comments , ); $this->_update_post_meta($post_id, self::META_KEY_PRE . $type, $cache); } $results = array(); foreach ($comments as $comment) { $results[] = $comment; } return $comments; }
これにより、コメント欄に正常にTweetが表示されるようになりました。まだまだTwitter Ver1.1に対応していないプラグインは沢山あるようで、自分もできるだけコードを公開したり、場合によってはフォークして新しいプラグインを作ったりして、皆さんのお役に立とうと思います。
一部のTwitterのコメントだけ重複表示されることがある(リツィート?)。
原因を調べているところです。