/** * Saves the tweets passed in. * * @param array $tweets - safe tweets (do error checking before passing to this function) * @return int - number of tweets saved */ function save_tweets($tweets) { global $wpdb; // strip out any tweets we already have $tweet_guids = array(); foreach ($tweets as $tweet) { $tweet_guids[] = AKTT_Tweet::guid_from_twid($tweet->id); } $existing_guids = $wpdb->get_col("\n\t\t\tSELECT guid\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE guid IN ('" . implode("','", $tweet_guids) . "')\n\t\t\tAND post_type = '" . AKTT::$post_type . "'\n\t\t"); // Set the args for any blog posts created $post_tweet_args = array('post_author' => $this->option('post_author'), 'post_category' => $this->option('post_category'), 'post_tags' => $this->option('post_tags'), 'title_prefix' => $this->option('blog_post_title_prefix')); // Save new tweets foreach ($tweets as $tweet) { if (in_array(AKTT_Tweet::guid_from_twid($tweet->id), $existing_guids)) { continue; } // Start up a tweet object $t = new AKTT_Tweet($tweet); if (!($result = $t->add())) { AKTT::log('There was an error saving a tweet. Tweet ID: ' . $t->id); continue; } // Now conditionially create the associated blog post if ($this->option('create_posts') == 1 && !($this->option('exclude_reply_tweets') && $t->is_reply()) && !($this->option('exclude_retweets') && $t->is_retweet()) && !$t->tweet_post_exists() && !$t->was_broadcast()) { AKTT::log('Creating a blog post for tweet ID: ' . $t->id); $t->create_blog_post($post_tweet_args); } } }
/** * Create blog post from tweet * * @param array $args * @return bool */ function create_blog_post($args = array()) { extract($args); // Add a space if we have a prefix $title_prefix = empty($title_prefix) ? '' : trim($title_prefix) . ' '; $post_content = $this->link_entities(false); // Append image to post if there is one, can't set it as a featured image until after save if (!empty($this->featured_image_id)) { $size = apply_filters('aktt_featured_image_size', 'medium'); $post_content .= "\n\n" . wp_get_attachment_image($this->featured_image_id, $size); } $gmt_time = self::twdate_to_time($this->date()); // Build the post data $data = array('post_title' => $title_prefix . $this->title(), 'post_content' => $post_content, 'post_author' => $post_author, 'post_status' => $post_status, 'post_type' => 'post', 'post_date' => date('Y-m-d H:i:s', AKTT::gmt_to_wp_time($gmt_time)), 'post_date_gmt' => date('Y-m-d H:i:s', $gmt_time), 'guid' => $this->guid() . '-post'); $data = apply_filters('aktt_tweet_create_blog_post_data', $data); // hook in here if you want to conditionally skip blog post creation if (!apply_filters('aktt_tweet_create_blog_post', true, $data, $this)) { return false; } $this->blog_post_id = wp_insert_post($data, true); if (is_wp_error($this->blog_post_id)) { AKTT::log('WP_Error:: ' . $this->blog_post_id->get_error_message()); return false; } // have to set up taxonomies after the insert in case we are in a context without // a 'current user' - see: http://core.trac.wordpress.org/ticket/19373 wp_set_object_terms($this->blog_post_id, intval($post_category), 'category'); wp_set_object_terms($this->blog_post_id, array_map('trim', explode(',', $post_tags)), 'post_tag'); // hook in here and return false to not set the format to "status", // or return another format to use that format instead of status if ($post_format = apply_filters('aktt_tweet_create_blog_post_format', 'status', $data, $this)) { set_post_format($this->blog_post_id, $post_format); } if (!empty($this->featured_image_id)) { update_post_meta($this->blog_post_id, '_thumbnail_id', $this->featured_image_id); } update_post_meta($this->blog_post_id, '_aktt_tweet_id', $this->id()); // twitter's tweet ID update_post_meta($this->blog_post_id, '_aktt_tweet_post_id', $this->post_id); // twitter's post ID // Add it to the tweet's post_meta as well update_post_meta($this->post_id, '_aktt_tweet_blog_post_id', $this->blog_post_id); // Let Social know to aggregate info about this post $account = false; foreach (AKTT::$accounts as $aktt_account) { if ($aktt_account->social_acct->id() == $this->data->user->id_str) { $account = $aktt_account->social_acct; break; } } if ($account) { Social::instance()->add_broadcasted_id($this->blog_post_id, 'twitter', $this->id(), $this->content(), $account, null); } // Let the account know we were successful return true; }