Advertisement
TwisterMc

Twitter Archiver Importer

Dec 15th, 2012
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1. // Insert tweets as posts
  2. function ozh_ta_insert_tweets( $tweets, $display = false ) {
  3.     $inserted = 0;
  4.     $user = array();
  5.    
  6.     global $ozh_ta;
  7.        
  8.     foreach ( (array)$tweets as $tweet ) {
  9.        
  10.         // Current tweet
  11.         $tid    = (string)$tweet->id;
  12.         $text   = $tweet->text;
  13.         $date   = date( 'Y-m-d H:i:s', strtotime( $tweet->created_at ) );
  14.         $source = $tweet->source;
  15.         $reply_to_name  = $tweet->in_reply_to_screen_name;
  16.         $reply_to_tweet = (string)$tweet->in_reply_to_status_id;
  17.         $tid_str = (string)$tweet->id_str;
  18.         $text_content = " [embed]https://twitter.com/TwisterMc/status/" . $tid_str . "[/embed]";
  19.        
  20.         // Info about Twitter user
  21.         if( !$user ) {
  22.             $user = array(
  23.                 'tweet_counts' => $tweet->user->statuses_count,
  24.                 'followers' => $tweet->user->followers_count,
  25.                 'following' => $tweet->user->friends_count,
  26.                 'listed_count' => $tweet->user->listed_count,
  27.                 'profile_image_url' => $tweet->user->profile_image_url,
  28.                 'tweeting_since' => date( 'Y-m-d H:i:s', strtotime( $tweet->user->created_at ) ),
  29.             );
  30.         }
  31.        
  32.         // Check for duplicate posts before inserting
  33.         global $wpdb;
  34.         $sql = "SELECT post_id
  35.                 FROM `$wpdb->postmeta`
  36.                 WHERE `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
  37.         if( !$wpdb->get_var( $sql ) ) {
  38.  
  39.             // Insert tweet as new post
  40.             $post = array(
  41.                 'post_title'   => $text,
  42.                 'post_content' => $text_content,
  43.                 'post_date'    => $date,
  44.                 'post_category'=> array( $ozh_ta['post_category'] ),
  45.                 'post_status'  => 'publish',
  46.                 'post_author'  => $ozh_ta['post_author'],
  47.             );
  48.             // Plugins: hack here
  49.             $post = apply_filters( 'ozh_ta_insert_tweets_post', $post );
  50.            
  51.             $post_id = wp_insert_post( $post );
  52.  
  53.             // Insert post meta data
  54.             add_post_meta( $post_id, 'ozh_ta_id', $tid, true );
  55.             add_post_meta( $post_id, 'ozh_ta_id_str', $tid_str, true );
  56.             if( $source )
  57.                 add_post_meta( $post_id, 'ozh_ta_source', $source, true );
  58.             if( $reply_to_name )
  59.                 add_post_meta( $post_id, 'ozh_ta_reply_to_name', $reply_to_name, true );
  60.             if( $reply_to_tweet )
  61.                 add_post_meta( $post_id, 'ozh_ta_reply_to_tweet', $reply_to_tweet, true );
  62.        
  63.             $last_tweet_id_inserted = $tid;
  64.             ozh_ta_debug( "Inserted $post_id (tweet id: $tid, tweet: ". substr($text, 0, 45) ."...)" );
  65.             $inserted++;
  66.            
  67.         } else {
  68.             // This tweet has already been imported ?!
  69.             ozh_ta_debug( "Skipping tweet $tid, already imported?!" );
  70.         }
  71.     }
  72.  
  73.     return array(
  74.         'inserted' => $inserted,
  75.         'last_tweet_id_inserted' => $tid,
  76.         'user' => $user,
  77.     );
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement