/** * 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 process_mentions_hashtags($tweet) { $post_id = $this->get_postId_for_tweet((string) $tweet->id_str); $has_hashtags = count($tweet->entities->hashtags) > 0; if ($has_hashtags) { $hashtags = ozh_ta_get_hashtags($tweet); $hashtags = implode(', ', $hashtags); wp_set_post_terms($post_id, $hashtags, 'hashtag'); } if (isset($tweet->entities->user_mentions) && ($mentions = $tweet->entities->user_mentions)) { $m = []; foreach ($mentions as $mention) { $m[] = $mention->screen_name; } if (0 < count($mentions)) { $mentions = implode(', ', $m); wp_set_post_terms($post_id, $mentions, 'mention'); } } }