Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Insert tweets as posts
- function ozh_ta_insert_tweets( $tweets, $display = false ) {
- $inserted = 0;
- $user = array();
- global $ozh_ta;
- foreach ( (array)$tweets as $tweet ) {
- // Current tweet
- $tid = (string)$tweet->id;
- $text = $tweet->text;
- $date = date( 'Y-m-d H:i:s', strtotime( $tweet->created_at ) );
- $source = $tweet->source;
- $reply_to_name = $tweet->in_reply_to_screen_name;
- $reply_to_tweet = (string)$tweet->in_reply_to_status_id;
- $tid_str = (string)$tweet->id_str;
- $text_content = " [embed]https://twitter.com/TwisterMc/status/" . $tid_str . "[/embed]";
- // 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
- FROM `$wpdb->postmeta`
- 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
- if( !$wpdb->get_var( $sql ) ) {
- // Insert tweet as new post
- $post = array(
- 'post_title' => $text,
- 'post_content' => $text_content,
- 'post_date' => $date,
- 'post_category'=> array( $ozh_ta['post_category'] ),
- 'post_status' => 'publish',
- 'post_author' => $ozh_ta['post_author'],
- );
- // Plugins: hack here
- $post = apply_filters( 'ozh_ta_insert_tweets_post', $post );
- $post_id = wp_insert_post( $post );
- // Insert post meta data
- add_post_meta( $post_id, 'ozh_ta_id', $tid, true );
- add_post_meta( $post_id, 'ozh_ta_id_str', $tid_str, true );
- if( $source )
- add_post_meta( $post_id, 'ozh_ta_source', $source, true );
- if( $reply_to_name )
- add_post_meta( $post_id, 'ozh_ta_reply_to_name', $reply_to_name, true );
- if( $reply_to_tweet )
- add_post_meta( $post_id, 'ozh_ta_reply_to_tweet', $reply_to_tweet, true );
- $last_tweet_id_inserted = $tid;
- ozh_ta_debug( "Inserted $post_id (tweet id: $tid, tweet: ". substr($text, 0, 45) ."...)" );
- $inserted++;
- } else {
- // This tweet has already been imported ?!
- ozh_ta_debug( "Skipping tweet $tid, already imported?!" );
- }
- }
- return array(
- 'inserted' => $inserted,
- 'last_tweet_id_inserted' => $tid,
- 'user' => $user,
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement