/** * Insert tweets as posts * * @param array $tweets Array of tweet objects * @return array Array of stats about the insertion */ function ozh_ta_insert_tweets($tweets) { // Flag as importing : this will cut some queries in the process, regarding (ping|track)backs if (!defined('WP_IMPORTING')) { define('WP_IMPORTING', true); } global $ozh_ta; $inserted = $skipped = $tagged = $num_tags = 0; $user = array(); if (ozh_ta_is_debug()) { $num_sql_batch = new ozh_ta_query_count(); } foreach ((array) $tweets as $tweet) { if (ozh_ta_is_debug()) { $num_sql_post = new ozh_ta_query_count(); } // Current tweet $tid = (string) $tweet->id_str; $text = ozh_ta_linkify_tweet($tweet); $date = date('Y-m-d H:i:s', strtotime($tweet->created_at) + 3600 * get_option('gmt_offset')); $source = $tweet->source; $has_hashtags = count($tweet->entities->hashtags) > 0; $reply_to_name = $tweet->in_reply_to_screen_name; $reply_to_tweet = (string) $tweet->in_reply_to_status_id_str; // Info about Twitter user if (!$user) { $user = array('tweet_counts' => $tweet->user->statuses_count, 'followers' => $tweet->user->followers_count, 'following' => $tweet->user->friends_count, 'listed_count' => $tweet->user->listed_count, 'profile_image_url' => $tweet->user->profile_image_url, 'tweeting_since' => date('Y-m-d H:i:s', strtotime($tweet->user->created_at))); } // Check for duplicate posts before inserting global $wpdb; $sql = "SELECT post_id\n\t\t FROM `{$wpdb->postmeta}`\n\t\t\t\tWHERE `meta_key` = 'ozh_ta_id' AND `meta_value` = '{$tid} ' LIMIT 0,1"; // Yeah, trusting api.twitter.com so we don't sanitize the SQL query, yeeeha if (!$wpdb->get_var($sql)) { // Insert tweet as new post $post = array('post_title' => strip_tags($text), 'post_content' => $text, 'post_date' => $date, 'post_category' => array($ozh_ta['post_category']), 'post_status' => 'publish', 'post_author' => $ozh_ta['post_author'], 'guid' => home_url() . '/?tid=' . $tid); // Post format if ('standard' != $ozh_ta['post_format']) { $post['tax_input'] = array('post_format' => array('post-format-' . $ozh_ta['post_format'])); } // Plugins: hack here $post = apply_filters('ozh_ta_insert_tweets_post', $post); $post_id = wp_insert_post($post); // Apply post format when line 294-296 failed process the request if ('standard' != $ozh_ta['post_format']) { set_post_format($post_id, $ozh_ta['post_format']); } // Insert post meta data add_post_meta($post_id, 'ozh_ta_id', $tid); if ($source) { add_post_meta($post_id, 'ozh_ta_source', $source); } if ($reply_to_name) { add_post_meta($post_id, 'ozh_ta_reply_to_name', $reply_to_name); } if ($reply_to_tweet) { add_post_meta($post_id, 'ozh_ta_reply_to_tweet', $reply_to_tweet); } ozh_ta_debug(" Inserted #{$post_id} (tweet id: {$tid}, tweet: " . ozh_ta_trim_long_string($text, 100) . ')'); if (ozh_ta_is_debug()) { ozh_ta_debug(' Import query cost: ' . $num_sql_post->stop()); } // Tag post if applicable if ($has_hashtags && $ozh_ta['add_hash_as_tags'] == 'yes') { $hashtags = ozh_ta_get_hashtags($tweet); $num_tags += count($hashtags); $hashtags = implode(', ', $hashtags); ozh_ta_debug(" Tagging post {$post_id} with " . $hashtags); $tagged++; if (ozh_ta_is_debug()) { $num_sql_tag = new ozh_ta_query_count(); } wp_set_post_tags($post_id, $hashtags); if (ozh_ta_is_debug()) { ozh_ta_debug(' Tagging query cost: ' . $num_sql_tag->stop()); } } $inserted++; } else { // This tweet has already been imported ?! ozh_ta_debug(" Skipping tweet {$tid}, already imported?!"); $skipped++; } } if (ozh_ta_is_debug()) { ozh_ta_debug('Batch import query cost: ' . $num_sql_batch->stop()); } return array('inserted' => $inserted, 'skipped' => $skipped, 'tagged' => $tagged, 'num_tags' => $num_tags, 'user' => $user); }
function ozh_ta_convert_old_posts($text) { // Has this post already been converted? Assuming a span means already formatted if (strpos($text, '<span class="') !== false) { return $text; } global $ozh_ta; // Get unformatted title: this will be the unmodified original tweet -- pure text, no HTML global $post; $title = $post->post_title; $ID = $post->ID; // Keep track of whether the post has been formatted $updated = false; // Tweet has links that have not been converted if ((strpos($title, 'http://') !== false or strpos($title, 'https://') !== false) && strpos($text, 'class="link') === false) { preg_match_all('!https?://\\S*!', $title, $matches); foreach ($matches[0] as $url) { // t.co URL ? if ($ozh_ta['un_tco'] == 'yes' && strpos($url, 'http://t.co/') === 0) { $expanded_url = ozh_ta_expand_tco_url($url); $tco_url = $url; } else { $expanded_url = $tco_url = $url; } $display_url = ozh_ta_trim_long_string(preg_replace('/https?:\\/\\//', '', $expanded_url)); $text = ozh_ta_convert_links($text, $expanded_url, $display_url, $tco_url); } $updated = true; } // Tweet has @mentions that have not been converted if (strpos($title, '@') !== false && strpos($text, 'class="username') === false) { preg_match_all('/\\B@(\\w+)/', $title, $matches); // good news, this won't match joe@site.com if (isset($matches[1])) { foreach ($matches[1] as $mention) { $text = ozh_ta_convert_mentions($text, $mention, $mention); } } $updated = true; } // Tweet has #hashtags that have not been converted if (strpos($title, '#') !== false && strpos($text, 'class="hashtag') === false) { preg_match_all('/\\B#(\\w*[a-zA-Z-]+\\w*)/', $text, $matches); if (isset($matches[1])) { foreach ($matches[1] as $tag) { $text = ozh_ta_convert_hashtags($text, $tag); } if ($ozh_ta['add_hash_as_tags'] == 'yes') { wp_set_post_tags($ID, implode(', ', $matches[1])); } } $updated = true; } // Did we alter the post? Update it, then if ($updated) { $post->post_content = $text; wp_update_post($post); } return $text; }