function do_digest_post($start, $end, $title) { if (!$start || !$end) { return false; } // flag us as busy update_option('aktt_doing_digest_post', '1'); remove_action('publish_post', 'aktt_notify_twitter', 99); remove_action('publish_post', 'aktt_store_post_options', 1, 2); remove_action('save_post', 'aktt_store_post_options', 1, 2); // see if there's any tweets in the time range global $wpdb; $startGMT = gmdate("Y-m-d H:i:s", $start); $endGMT = gmdate("Y-m-d H:i:s", $end); // build sql $conditions = array(); $conditions[] = "tw_created_at >= '{$startGMT}'"; $conditions[] = "tw_created_at <= '{$endGMT}'"; $conditions[] = "tw_text NOT LIKE '" . $wpdb->escape($this->tweet_prefix) . "%'"; if ($this->exclude_reply_tweets) { $conditions[] = "tw_text NOT LIKE '@%'"; } $where = implode(' AND ', $conditions); $sql = "\n\t\t\tSELECT * FROM {$wpdb->aktt}\n\t\t\tWHERE {$where}\n\t\t\tORDER BY tw_created_at {$this->digest_tweet_order}\n\t\t"; $tweets = $wpdb->get_results($sql); if (count($tweets) > 0) { $tweets_to_post = array(); foreach ($tweets as $data) { $tweet = new aktt_tweet(); $tweet->tw_text = $data->tw_text; $tweet->tw_reply_tweet = $data->tw_reply_tweet; if (!$tweet->tweet_is_post_notification() || $tweet->tweet_is_reply() && $this->exclude_reply_tweets) { $tweets_to_post[] = $data; } } $tweets_to_post = apply_filters('aktt_tweets_to_digest_post', $tweets_to_post); // here's your chance to alter the tweet list that will be posted as the digest if (count($tweets_to_post) > 0) { $content = '<ul class="aktt_tweet_digest">' . "\n"; foreach ($tweets_to_post as $tweet) { $content .= ' <li>' . aktt_tweet_display($tweet, 'absolute') . '</li>' . "\n"; } $content .= '</ul>' . "\n"; if ($this->give_tt_credit == '1') { $content .= '<p class="aktt_credit">' . __('Powered by <a href="http://alexking.org/projects/wordpress">Twitter Tools</a>', 'twitter-tools') . '</p>'; } $post_data = array('post_content' => $wpdb->escape($content), 'post_title' => $wpdb->escape(sprintf($title, date('Y-m-d'))), 'post_date' => date('Y-m-d H:i:s', $end), 'post_category' => array($this->blog_post_category), 'post_status' => 'publish', 'post_author' => $wpdb->escape($this->blog_post_author)); $post_data = apply_filters('aktt_digest_post_data', $post_data); // last chance to alter the digest content $post_id = wp_insert_post($post_data); add_post_meta($post_id, 'aktt_tweeted', '1', true); wp_set_post_tags($post_id, $this->blog_post_tags); } } add_action('publish_post', 'aktt_notify_twitter', 99); add_action('publish_post', 'aktt_store_post_options', 1, 2); add_action('save_post', 'aktt_store_post_options', 1, 2); update_option('aktt_doing_digest_post', '0'); return true; }
function update_my_tweets() { $profile_id = get_profile_id(); if (!$profile_id) { return; } // activate Twitter Tools $_GET['activate'] = true; // trip the init() function aktt_init(); // get the Twitter Tools object global $wpdb, $aktt, $db; if (empty($aktt->twitter_username) || empty($aktt->twitter_password)) { return; } // make a new tweet object $tweet = new aktt_tweet(); // let the last update run for 5 minutes if (time() - intval(get_option('aktt_doing_tweet_download')) < 300) { return; } update_option('aktt_doing_tweet_download', time()); if (empty($aktt->twitter_username) || empty($aktt->twitter_password)) { update_option('aktt_doing_tweet_download', '0'); die; } require_once ABSPATH . WPINC . '/class-snoopy.php'; $snoop = new Snoopy(); $snoop->agent = 'Twitter Tools http://alexking.org/projects/wordpress'; $snoop->user = $aktt->twitter_username; $snoop->pass = $aktt->twitter_password; $snoop->fetch('http://tweetpass.com/statuses/friends_timeline.json'); if (!strpos($snoop->response_code, '200')) { update_option('aktt_doing_tweet_download', '0'); return; } $data = $snoop->results; $hash = md5($data); if ($hash == get_option('aktt_update_hash')) { update_option('aktt_doing_tweet_download', '0'); return; } $json = new Services_JSON(); $tweets = $json->decode($data); if (is_array($tweets) && count($tweets) > 0) { $tweet_ids = array(); foreach ($tweets as $tweet) { $tweet_ids[] = $wpdb->escape($tweet->id); } $existing_ids = $wpdb->get_col("\n SELECT tw_id\n FROM {$wpdb->aktt}\n WHERE tw_id\n IN ('" . implode("', '", $tweet_ids) . "')\n "); $new_tweets = array(); foreach ($tweets as $tw_data) { if (!$existing_ids || !in_array($tw_data->id, $existing_ids)) { $tweet = new aktt_tweet($tw_data->id, $tw_data->text); $tweet->tw_created_at = $tweet->twdate_to_time($tw_data->created_at); $new_tweets[] = $tweet; } } foreach ($new_tweets as $tweet) { $AkTwitter =& $db->get_table('ak_twitter'); $Entry =& $db->get_table('entries'); $t = $AkTwitter->find_by('tw_id', $tweet->tw_id); if (!$t) { $tweet->add(); $created = date("Y-m-d H:i:s", $tweet->tw_created_at - 8 * 3600); $t = $AkTwitter->find($db->last_insert_id($AkTwitter)); if ($t) { $t->set_etag(); } $atomentry = $Entry->find_by(array('resource' => 'ak_twitter', 'record_id' => $t->id), $t->id); if ($atomentry) { $result = $db->get_result("UPDATE entries SET last_modified = '{$created}' WHERE id = " . $atomentry->id); } $user = new Snoopy(); $user->agent = 'Twitter Tools http://alexking.org/projects/wordpress'; $user->user = $aktt->twitter_username; $user->pass = $aktt->twitter_password; $user->fetch('http://tweetpass.com/statuses/show/' . $tweet->tw_id . '.json'); $data = $user->results; $json = new Services_JSON(); $notice = $json->decode($data); $uarr = $notice->user; $TwitterUser =& $db->model('TwitterUser'); $twuser = $TwitterUser->find_by('twitter_id', $uarr->id); if (!$twuser) { $twuser = $TwitterUser->base(); $twuser->set_value('description', $uarr->description); $twuser->set_value('screen_name', $uarr->screen_name); $twuser->set_value('url', $uarr->url); $twuser->set_value('name', $uarr->name); $twuser->set_value('protected', $uarr->protected); $twuser->set_value('followers_count', $uarr->followers_count); $twuser->set_value('profile_image_url', $uarr->profile_image_url); $twuser->set_value('location', $uarr->location); $twuser->set_value('twitter_id', $uarr->id); $twuser->save_changes(); } $t->set_value('profile_id', $twuser->twitter_id); $t->save_changes(); } $AkTwitter->has_and_belongs_to_many('identities'); $join =& $db->get_table($Entry->join_table_for('ak_twitter', 'identities')); $j = $join->base(); $j->set_value('aktwitter_id', $t->id); $j->set_value('identity_id', get_profile_id()); $j->save_changes(); } } update_option('aktt_update_hash', $hash); update_option('aktt_last_tweet_download', time()); update_option('aktt_doing_tweet_download', '0'); }