Twitter Digestを改造した

WordPressがTwitterのStatusURLを入れると,見やすい展開をはかってくれるようになったのに,
Twitter Digestは相変わらずリストでしか表示してくれないし,画像は展開してくれないので,以下のような魔改造を施しました.akiで検索してみてください.
あんまり重たいようだったらやめよう.

それにしてもどうして,定期まとめツイートが走らなかったんだろう…?しばらく様子見.

Twitter account.

Version: 2.9
Author: Tim Beck
Author URI: http://whalespine.org
*/

// Copyright (c) 2009 - 2010 Tim Beck and Paul Wlodarczyk. All rights reserved.
//
// Released under the GPL license
// http://www.opensource.org/licenses/gpl-license.php
//
// This is an add-on for WordPress
// http://wordpress.org/
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************
//
//=======================
// Defines

define('WS_FETCH_SPACE', 30); //X seconds between fetches
define('WS_TWITTER_LIMIT', 200); //the most tweets you can pull from the Twitter REST API
define('WS_API_USER_TIMELINE', 'https://api.twitter.com/1.1/statuses/user_timeline.json');
define('WS_STATUS_URL', 'https://twitter.com/###USERNAME###/statuses/###STATUS###');
define('WS_PROFILE_URL', 'https://twitter.com/###USERNAME###');
define('WS_HASHTAG_URL', 'https://twitter.com/search?q=###HASHTAG###&src=hash');
define('WS_VERSION', '2.9');

//========================
// Digest cron activation

register_activation_hook(__FILE__, 'ws_activate_digest');
register_deactivation_hook(__FILE__, 'ws_deactivate_digest');
add_action('ws_td_digest_event', 'ws_ping_twitter');

function ws_activate_digest() {
wp_schedule_event(time(), 'hourly', 'ws_td_digest_event');
}

function ws_deactivate_digest() {
wp_clear_scheduled_hook('ws_td_digest_event');
}

// Check Twitter to see if there are any new tweets that need to be published.
// Return values:
// null -> all good
// 1 -> Error w/ twitter
// 2 -> Too soon
// 3 -> Missing username/password
// 4 -> Post created and published now or put in draft (depending on status option)
// 5 -> No tweets to post
// 6 -> Post created but not published until later
// 7 -> Wrong day of week

function ws_ping_twitter() {

// Do we have a username?
if (!get_option("ws_td_username")) {
return 3;
}

// We don't need the password anymore, so delete it.
if (get_option("ws_td_password")) {
delete_option("ws_td_password");
}

// Has there been enough time since the last check? This avoids a race
// condition which would produce a duplicate post.

$last = get_option('ws_td_last_check');
if (time() - $last < WS_FETCH_SPACE) { return 2; } update_option("ws_td_last_check", time()); // Is this a daily or weekly post? // Need to know today's local midnight to work out start/end times $midnight = strtotime(date('Y-m-d, 00:00:00', time() + ws_gmtOffset())); $pub_day = get_option('ws_td_pub_day'); $pub_freq = get_option('ws_td_freq'); $current_day_of_week = date("w", $midnight); // Test if it's weekly, then if so is today the right day of the week. If // not, return; pub_freq=0 is daily if ($pub_freq == 1) { if ($current_day_of_week != $pub_day) { return 7; } else { // 7 days ago at midnight $startDate = strtotime('-7 days', $midnight); } } else { // yesterday at midnight $startDate = strtotime('-1 days', $midnight); // $startDate = strtotime('-15 days', $midnight); } // yesterday at 11:59:59 $endDate = strtotime('-1 second', $midnight); $startDateStr = date(ws_getDateFormat(), $startDate); $endDateStr = date(ws_getDateFormat(), $endDate); // Get the last tweet id from the last post $lastTweet = get_option('ws_td_last_tweet'); if (!$lastTweet) { $lastTweet = 0; update_option('ws_td_last_tweet', $lastTweet); } // Get the tweets $numtweets = get_option('ws_td_num_tweets'); // range check. Twitter limit is to 200 if ($numtweets > WS_TWITTER_LIMIT) {
$numtweets = WS_TWITTER_LIMIT;
}

if ($numtweets == 0) {
$numtweets = 20; //default is 20 with no count argument, so fetch 20 if NULL
}

// get the last N tweets, since the last tweet
$url = WS_API_USER_TIMELINE . "?screen_name=".get_option('ws_td_username') ."&count=". $numtweets ;

if (get_option('ws_td_includeRTs')) {
$url .= '&include_rts=1';
}

if ($lastTweet) {
$url .= "&since_id=" . $lastTweet;
}

// Fetch the tweets
$tweets = ws_fetch_tweets($url);

// Go through the array and process any tweets from the desired time period
$tweet_content = array();
$tweet_ids = array(); //aki
$newLastTweet = false;
if ($tweets && count($tweets) >= 0) {
// process the tweets

foreach ($tweets as $tw_data) {
// Convert the time to local
$tw_data->created_at = strtotime($tw_data->created_at) + ws_gmtOffset();

// Was this tweet added in the time period of interest?
if ($tw_data->created_at < $startDate) { continue; } if ($tw_data->created_at > $endDate ) {
continue;
}

// Are we dropping replies?
if (get_option('ws_td_drop_replies') && preg_match('/^@.*/', $tw_data->text)) {
continue;
}

// All good, so format and add to the content
array_push($tweet_content, ws_format_tweet($tw_data)."\n\n");
array_push($tweet_ids, ws_format_tweet_id($tw_data)); //aki

// Remember the first tweet in the list, which is actually the most
// recently tweeted.
if (!$newLastTweet) {
$newLastTweet = $tw_data->id_str;
}
}

// To avoid race conditions, check to make sure lastTweet hasn't changed
if ($lastTweet == get_option('ws_td_last_tweet')) {

$count = count($tweet_content);

if ($count != 0 && $count >= get_option('ws_td_min_tweets')) {

// Before we push the content, we need to put it in the right order
if (get_option('ws_td_chrono') == 1) {
$tweet_content = array_reverse($tweet_content);
$tweet_ids = array_reverse($tweet_ids); //aki
}

$title = get_option('ws_td_title');
if (!$title) {
$title = "Tweets for %startdate";
}

// We look for %date to be backwards compatible
$title = preg_replace("/%date/", $startDateStr, $title);
$title = preg_replace("/%startdate/", $startDateStr, $title);
$title = preg_replace("/%enddate/", $endDateStr, $title);

$excerpt = get_option('ws_td_excerpt');
$excerpt = preg_replace("/%date/", $startDateStr, $excerpt);
$excerpt = preg_replace("/%startdate/", $startDateStr, $excerpt);
$excerpt = preg_replace("/%enddate/", $endDateStr, $excerpt);

//aki start
$postcontent = join("\n", $tweet_ids);
$postcontent .= "\n\n

    \n\n";
    //aki end
    //aki $postcontent .= "

      \n\n";
      $postcontent .= join("\n\n", $tweet_content);
      $postcontent .= "\n

    ";

    // This is messy, but good enough for now.
    if (ws_create_post($postcontent, $title, get_option('ws_td_pub_time'), $excerpt)
    == 1 /* Published in future */) {
    $retval = 6;
    } else {
    // Published now or drafted.
    $retval = 4;
    }

    // Update the last tweet id
    update_option('ws_td_last_tweet', $newLastTweet);

    return $retval;

    }
    else {
    return 5;
    }
    }

    }
    else {
    return 5;
    }

    }

    // This function creates the actual post and schedules it for publishing time
    // at $pubtime, if the status option is set to 'publish'. Otherwise the post
    // is put in 'draft' status.
    // Return values:
    // 0: published now
    // 1: published in future
    // 2: drafted
    function ws_create_post($content, $title, $pubtime, $excerpt) {

    global $wpdb;
    $result = 0;

    // Are we putting this in draft or publishing (now or later)?
    if (get_option('ws_td_status') == 'draft') {
    $status = 'draft';
    $result = 2;
    } else {

    $status='publish';

    // Are we doing this now or later?
    if ($pubtime) {
    $time = date('Y-m-d ').$pubtime.":00";
    $timestamp = strtotime($time);
    if ($timestamp > current_time('timestamp')) {
    $result = 1;
    }
    } else {
    $time = current_time('mysql');
    }
    }

    // Create the post
    $post_data = array(
    'post_content' => $wpdb->escape($content),
    'post_title' => $wpdb->escape($title),
    'post_date' => $time,
    'post_category' => array(get_option('ws_td_category')),
    'post_status' => $status,
    'post_author' => $wpdb->escape(get_option('ws_td_author')),
    'post_excerpt' => $wpdb->escape($excerpt)
    );

    // Insert post
    $post_id = wp_insert_post($post_data);
    add_post_meta($post_id, 'ws_tweeted', '1', true);

    // Tag it
    wp_set_post_tags($post_id, get_option('ws_td_post_tags'));

    return $result;
    }
    // customized by aki 2014/3/26
    function ws_format_tweet_id($tweet) {
    $output = ws_status_url(get_option('ws_td_username'), $tweet->id_str);
    return $output;
    }

    // Returns an html formatted $tweet. This is almost directly borrowed from Twitter Tools
    function ws_format_tweet($tweet) {
    $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 .= ' '.sprintf(__('in reply to %s', 'twitter-digest'), $tweet->in_reply_to_screen_name).'';
    }

    // 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 .= ' '.$time_display.'';

    $output .= '

  • ';
    return $output;

    }

    // Most of the following formatting functions are borrowed from Twitter Tool
    function ws_make_clickable($tweet) {

    // $tweet = preg_replace('/\@([a-zA-Z0-9_]{1,15}) /','@\\1 ', $tweet);
    $tweet = preg_replace_callback(
    '/((?:^|\s+|[\"\'\[\(]))\@([\w]{1,30})/',
    create_function(
    '$matches',
    'return $matches[1].ws_profile_link($matches[2]);'
    ),
    $tweet
    );
    $tweet = preg_replace_callback(
    '/((?:^|\s+|[\"\'\[\(]))\#([\w]{1,30})/',
    create_function(
    '$matches',
    'return $matches[1].ws_hashtag_link($matches[2]);'
    ),
    $tweet
    );

    // make_clickable doesn't handle (url) very well, so we'll chuck some spaces in
    $tweet = preg_replace("/\((http.*)\)/", "( $1 )", $tweet);

    return make_clickable($tweet);
    }

    function ws_status_url($username, $status) {
    return str_replace(
    array('###USERNAME###', '###STATUS###'),
    array($username, $status),
    WS_STATUS_URL);
    }

    function ws_profile_link($username, $prefix = '@') {
    return ''.$prefix.$username.'';
    }

    function ws_hashtag_link($hashtag, $prefix = '#') {
    return ''.$prefix.htmlspecialchars($hashtag).'';
    }

    function ws_profile_url($username) {
    return str_replace('###USERNAME###', $username, WS_PROFILE_URL);
    }

    function ws_hashtag_url($hashtag) {
    $hashtag = urlencode('#'.$hashtag);
    return str_replace('###HASHTAG###', $hashtag, WS_HASHTAG_URL);
    }

    // Based on Alex King's implementation for the Twitter Tools plugin
    function ws_fetch_tweets($url) {

    // First get our oauth connection
    require_once("twitteroauth.php");
    $consumerkey = get_option('ws_td_conskey'); // "3JucGrvAxOuaAYbLml5nkQ";
    $consumersecret = get_option('ws_td_conssecret'); //"iIRXb7c0VFwTYmI4TCNNIrUWB2YDxmL6c11YogcqQw0";
    $accesstoken = get_option('ws_td_acctoken'); // "14198382-SnzuP0AMz3qVWTolneB8QG7D1kHt6WNMosZzb48LE";
    $accesstokensecret = get_option('ws_td_accsecret'); // "9O7sc2tbea8PEx3Py9S4VWsHkbZtDBcP0n4HvnzokqM";

    if (!$consumerkey || !$consumersecret || !$accesstoken || !$accesstokensecret) {
    update_option('ws_td_error', 'Check your account information and make sure all token fields have a valid value.');
    return false;
    }

    $connection = new TwitterOAuth($consumerkey, $consumersecret,
    $accesstoken, $accesstokensecret);

    // Now make the request
    $tweets = $connection->get($url);

    // Check the error code
    if(empty($connection->http_code) || $connection->http_code != 200) {
    update_option('ws_td_error', 'Error retrieving tweets.
    Status: '.$connection->http_code.' Check your authentication tokens.
    '.print_r($connection->http_info, true));
    return false;
    }

    // Everything is ok
    update_option('ws_td_error','');

    return $tweets;
    }

    // Simply resets the 'ws_td_last_tweet' option
    function ws_td_resetDatabase() {
    update_option('ws_td_last_tweet',0);
    }

    // The menu hook
    function ws_menu_item() {
    if (current_user_can('manage_options')) {
    add_options_page(
    __('Twitter Digest Options', 'twitter-digest')
    , __('Twitter Digest', 'twitter-digest')
    , 'manage_options'
    , basename(__FILE__)
    , 'ws_options_form'
    );
    }
    }
    add_action('admin_menu', 'ws_menu_item');

    function ws_options_form() {

    // Check here if we are going to do the check now
    global $wpdb;

    // Reset the database if necessary
    if (isset($_POST["action"]) && $_POST["action"] == 'resetdb') {
    ws_td_resetDatabase();
    }

    // Get some variables together
    $ping_message = "(This may take a while if there are tweets to process.)";
    $ping_style="color: black";

    // Range check the publish day
    $pub_day_value = get_option('ws_td_pub_day');
    if ($pub_day_value < 0 || $pub_day_value > 6) { $pub_day_value = date("w"); }

    // yesterday or last week?
    if (get_option('ws_td_freq') == 0) {
    $periodStr = 'yesterday';
    } else {
    $periodStr = 'last week';
    }

    // Ping if necessary, and show the correct message.
    if (isset($_POST["action"]) && $_POST["action"] == "ping") {
    switch(ws_ping_twitter(true)) {
    case 1:
    $pwuser = get_option('ws_td_username');

    $ping_message = "Something went wrong with Twitter. Check for an error message above. Current Twitter username is " . $pwuser. "." ;
    $ping_style = "color: red";
    break;
    case 2:
    $ping_message = "To keep things sane, we're going to wait ".WS_FETCH_SPACE." seconds between pings.";
    $ping_style = "color: red";
    break;
    case 3:
    $ping_message = "Missing Twitter username and/or password";
    $ping_style = "color: red";
    break;
    case 4:
    $ping_message = "Post containing ".$periodStr."'s tweets has been ".(get_option('ws_td_status'). 'ed.');
    $ping_style ="color: green";
    break;
    case 6: $ping_message = "Post containing ".$periodStr ."'s tweets scheduled.";
    $ping_style = "color: green";
    break;
    case 5: $ping_message = "No new tweets found from " . $periodStr;
    break;
    case 7:
    $daylabel = date("l");
    $pub_day_str = ws_getDoW($pub_day_value);
    $now = date("H:i:s");
    $pub_time_str = get_option('ws_td_pub_time');
    if (!$pub_time_str) { $pub_time_str = '00:00:00'; }

    $ping_message = "Today is " . $daylabel . " and the local time is " . $now . " and you are configured to publish on " . $pub_day_str . " at " . $pub_time_str . ".";
    $ping_style = "color: red";
    }
    }

    // Get all the options together
    $categories = get_categories('hide_empty=0');
    $cat_options = '';
    foreach ($categories as $category) {
    if ($category->term_id == get_option('ws_td_category')) {
    $selected = 'selected="selected"';
    }
    else {
    $selected = '';
    }
    $cat_options .= "\n\t";
    }

    // get_users_of_blog is deprecated.
    $authors = get_users_of_blog();
    $author_options = '';
    foreach ($authors as $user) {
    $usero = new WP_User($user->user_id);
    $author = $usero->data;
    // Only list users who are allowed to publish
    if (! $usero->has_cap('publish_posts')) {
    continue;
    }
    if ($author->ID == get_option('ws_td_author')) {
    $selected = 'selected="selected"';
    }
    else {
    $selected = '';
    }
    $author_options .= "\n\t";
    }

    // Set up the options for the status. Just draft or publish for now.
    $status_options = '';
    if (get_option('ws_td_status') == 'draft') {
    $status_options ='';
    } else {
    $status_options ='';
    }

    // Drop replies options
    $drop_replies_check = get_option('ws_td_drop_replies') == 1 ? 'checked="true"' : '';
    // Chrono options
    $chrono_check = get_option('ws_td_chrono') == 1 ? 'checked="true"' : '';
    // Show date options
    $showDate_check = get_option('ws_td_showdate') == 1 ? 'checked="true"' : '';
    $showTime_check = get_option('ws_td_showtime') == 1 ? 'checked="true"' : '';
    $includeRTs_check = get_option('ws_td_includeRTs') == 1 ? 'checked="true"' : '';

    // Publishing frequency options
    if (get_option('ws_td_freq') == 1) {
    $freq_options = '';

    $pub_day_display = '';
    } else {
    $freq_options = '';
    $pub_day_display = 'display: none';
    }

    // DoW option
    $pub_day_options = '';
    for ($i = 0; $i < 7; $i++) { $pub_day_options .= '

    // Default the min tweet value to 1
    $min_tweets_value = get_option('ws_td_min_tweets');
    if ($min_tweets_value < 1) { $min_tweets_value = 1; } // Default the number of tweets to 20 $num_tweets_value = get_option('ws_td_num_tweets'); if (!$num_tweets_value) { $num_tweets_value = 20; } print('

    '.__('Twitter Digest Options', 'twitter-tools').'

    '.ws_getErrorMessage().'


    '.wp_nonce_field('update-options').'


    Twitter Account Info






    Publish Options




    '._('Specify the time at which the tweet post should be published in 24 hour format e.g. 2:22pm = 14:22 or leave empty for ASAP').'

    '._('Specify the day of the week the post should be published in number format (0-6). e.g. Sunday = 0').'

    Post Options



    '._('Use %startdate and %enddate to insert the date').'


    '._('Use %startdate and %enddate to insert the dates').'


    '._('Defaults to Y-m-d').'



    '._('Separate multiple tags with commas. Example: tweets, twitter').'











    '.$ping_message.'



    Clicking this button resets the Twitter Digest database and may result in duplicate posts.

    ');
    }

    // Get the date option or fall back to the default
    function ws_getDateFormat() {
    $format = get_option('ws_td_dateFormat');
    if ($format == '') {
    $format = 'Y-m-d';
    }
    return $format;
    }

    // Get the string rep of a day given the index. Sunday = 0;
    function ws_getDoW($num) {
    switch($num) {
    case 0:
    return "Sunday";
    break;
    case 1:
    return "Monday";
    break;
    case 2:
    return "Tuesday";
    break;
    case 3:
    return "Wednesday";
    break;
    case 4:
    return "Thursday";
    break;
    case 5:
    return "Friday";
    break;
    case 6:
    return "Saturday";
    }

    }

    // Returns the gmt offset for WordPress in seconds
    function ws_gmtOffset() {
    return get_option('gmt_offset') * 3600;
    }

    function ws_gmtTime() {
    return time() - date("Z");
    }

    function ws_getErrorMessage() {
    $error = get_option('ws_td_error');
    if ($error) {
    return '

    '.$error.'

    ';
    }
    }

    define('MYPLUGIN_FOLDER', dirname(__FILE__));

Twitter Digestを改造した」への6件のフィードバック

  1. 上記のソースで “aki” というキーワードで検索していただければ修正箇所は見えてくると思います.
    それでもわからないのであれば,何か試してみたブログでも書いていただければ・・・ただ私自身も1年以上前の作業なのであまり覚えていません.

    また元のソースはTim Beckのもので,改変権を持っているわけではないので,いきなり公開することはできないと思います.

  2. akiさん アドバイス通りいろいろいじってみて、tweetを埋め込みのかたちで投稿することができました。
    ご助言ありがとうございました。

  3. 無事に動いたようでよかったです!
    せっかくなのでURLご紹介いただけますか?

コメントは受け付けていません。