/** * Request handler * * @return void */ static function controller() { if (isset($_GET['aktt_action'])) { switch ($_GET['aktt_action']) { case 'download_account_tweets': if (empty($_GET['acct_id']) || !AKTT::social_key_auth()) { wp_die(__('Sorry, try again.', 'twitter-tools')); } $acct_id = intval($_GET['acct_id']); self::get_social_accounts(); if (isset(self::$accounts[$acct_id])) { if ($tweets = self::$accounts[$acct_id]->download_tweets()) { self::$accounts[$acct_id]->save_tweets($tweets); } } die; break; case 'import_tweet': // check for status_id && auth key if (empty($_GET['tweet_id']) || !AKTT::social_key_auth()) { wp_die(__('Sorry, try again.', 'twitter-tools')); } // check for account_name $username = !empty($_GET['username']) ? stripslashes($_GET['username']) : null; // download tweet $tweet = self::download_tweet($_GET['tweet_id'], $username); if (!is_a($tweet, 'stdClass')) { wp_die('Failed to download tweet.'); } // store tweet $t = new AKTT_Tweet($tweet); if (!$t->exists_by_guid()) { $t->add(); } die; break; case 'backfill_tweet_data': if (empty($_GET['tweet_id']) || !AKTT::social_key_auth()) { wp_die(__('Sorry, try again.', 'twitter-tools')); } $t = new AKTT_Tweet(stripslashes($_GET['tweet_id'])); if (!$t->get_post()) { die; } $usernames = wp_get_object_terms($t->post->ID, 'aktt_accounts'); $username = $usernames[0]->slug; $tweet = self::download_tweet($_GET['tweet_id'], $username); if (!is_a($tweet, 'stdClass')) { wp_die('Failed to download tweet'); } $t->update_twitter_data($tweet); die; break; } } }
/** * 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); } } }
function aktt_upgrade_30_run($count = 25) { global $wpdb; // pull next tweet(s) $count = intval($count); $tweets = $wpdb->get_results("\n\t\tSELECT *\n\t\tFROM {$wpdb->aktt}\n\t\tWHERE upgrade_30 = 0\n\t\tLIMIT {$count}\n\t"); // upgrade if (count($tweets)) { $upgraded = array(); $username = get_option('aktt_twitter_username'); // already passed sanity check to make sure this exists foreach ($tweets as $tweet) { if (trim($tweet->tw_text) == '') { continue; } $t = new AKTT_Tweet($tweet->tw_id); $t->data = new stdClass(); $t->data->id = $t->data->id_str = $tweet->tw_id; $t->data->text = $tweet->tw_text; $t->data->created_at = date('D M d H:i:s +0000 Y', strtotime($tweet->tw_created_at . ' +0000')); $t->data->in_reply_to_screen_name = $tweet->tw_reply_username; $t->data->in_reply_to_status_id = $t->data->in_reply_to_status_id_str = $tweet->tw_reply_tweet; $t->data->user = new stdClass(); $t->data->user->screen_name = $username; $t->raw_data = json_encode($t->data); // skip if duplicate if ($t->exists_by_guid()) { // already there, so mark as upgraded $upgraded[] = intval($tweet->id); continue; } if ($t->add()) { // add meta - upgraded tweet update_post_meta($t->post_id, '_aktt_upgraded_30', 1); update_post_meta($t->post_id, '_aktt_30_backfill_needed', 1); $upgraded[] = intval($tweet->id); } } if (count($upgraded)) { $wpdb->query("\n\t\t\t\tUPDATE {$wpdb->aktt}\n\t\t\t\tSET upgrade_30 = 1\n\t\t\t\tWHERE id IN (" . implode(',', $upgraded) . ")\n\t\t\t"); } } // return stats $to_upgrade = $wpdb->get_var("\n\t\tSELECT count(id)\n\t\tFROM {$wpdb->aktt}\n\t\tWHERE upgrade_30 = 0\n\t"); return $to_upgrade; }