/** * Saves the aggregated comments. * * @param object $post * @param bool $skip_approval * @return void */ public function save_aggregated_comments(&$post, $skip_approval = false) { if (isset($post->results[$this->_key])) { global $wpdb; foreach ($post->results[$this->_key] as $result) { if (!isset($result->user->protected) or $result->user->protected == false) { $account = (object) array('user' => (object) array('id' => $result->from_user_id, 'screen_name' => $result->from_user)); $class = 'Social_Service_' . $this->_key . '_Account'; $account = new $class($account); $commentdata = array('comment_post_ID' => $post->ID, 'comment_type' => $result->comment_type, 'comment_author' => $wpdb->escape($account->username()), 'comment_author_email' => $wpdb->escape($this->_key . '.' . $account->id() . '@example.com'), 'comment_author_url' => $account->url(), 'comment_content' => $wpdb->escape($result->text), 'comment_date' => date('Y-m-d H:i:s', strtotime($result->created_at) + get_option('gmt_offset') * 3600), 'comment_date_gmt' => gmdate('Y-m-d H:i:s', strtotime($result->created_at)), 'comment_author_IP' => $_SERVER['SERVER_ADDR'], 'comment_agent' => 'Social Aggregator'); if ($skip_approval || apply_filters('social_approve_likes_and_retweets', false) && Social_Twitter::is_retweet(null, $result)) { $commentdata['comment_approved'] = 1; } else { if (($commentdata = $this->allow_comment($commentdata, $result->id, $post)) === false) { continue; } } // sanity check to make sure this comment is not a duplicate if ($this->is_duplicate_comment($post, $result->id)) { Social::log('Result #:result_id already exists, skipping.', array('result_id' => $result->id), 'duplicate-comment'); continue; } Social::log('Saving #:result_id for account :account_id.', array('result_id' => $result->id, 'account_id' => $account->id())); $comment_id = 0; try { Social::Log('Attempting to save commentdata: :commentdata', array('commentdata' => print_r($commentdata, true))); $comment_id = wp_insert_comment($commentdata); update_comment_meta($comment_id, 'social_account_id', addslashes_deep($result->from_user_id)); update_comment_meta($comment_id, 'social_profile_image_url', addslashes_deep($result->profile_image_url_https)); update_comment_meta($comment_id, 'social_status_id', addslashes_deep($result->id)); // Attempt to see if the comment is in response to an existing Tweet. if (!isset($result->in_reply_to_status_id)) { // This "should" only happen on tweets found on the URL search foreach ($this->accounts() as $account) { $response = $this->request($account, '1.1/statuses/show/' . $result->id)->body(); if (isset($response->in_reply_to_status_id)) { if (!empty($response->in_reply_to_status_id)) { $result->in_reply_to_status_id = $response->in_reply_to_status_id; } break; } } } if (isset($result->in_reply_to_status_id)) { update_comment_meta($comment_id, 'social_in_reply_to_status_id', addslashes_deep($result->in_reply_to_status_id)); } if (!isset($result->raw)) { $result = (object) array_merge((array) $result, array('raw' => $result)); } update_comment_meta($comment_id, 'social_raw_data', addslashes_deep(base64_encode(json_encode($result->raw)))); if ($commentdata['comment_approved'] !== 'spam') { if ($commentdata['comment_approved'] == '0') { wp_notify_moderator($comment_id); } if (get_option('comments_notify') and $commentdata['comment_approved'] and (!isset($commentdata['user_id']) or $post->post_author != $commentdata['user_id'])) { wp_notify_postauthor($comment_id, 'comment'); } } } catch (Exception $e) { // Something went wrong, remove the aggregated ID. if (($key = array_search($result->id, $post->aggregated_ids['twitter'])) !== false) { unset($post->aggregated_ids['twitter'][$key]); } if ((int) $comment_id) { // Delete the comment in case it wasn't the insert that failed. wp_delete_comment($comment_id); } } } } } }
/** * Aggregates comments by the service's API. * * @param object $post * @return array */ public function aggregate_by_api(&$post) { $accounts = $this->get_aggregation_accounts($post); if (isset($accounts[$this->_key]) and count($accounts[$this->_key])) { foreach ($accounts[$this->_key] as $account) { if (isset($post->broadcasted_ids[$this->_key][$account->id()])) { $broadcasted_ids = $post->broadcasted_ids[$this->_key][$account->id()]; // Retweets foreach ($broadcasted_ids as $broadcasted_id => $data) { Social::log('Aggregating Twitter via statuses/retweets'); $response = $this->request($account, 'statuses/retweets/' . $broadcasted_id, array('count' => 200)); if ($response !== false and is_array($response->body()->response) and count($response->body()->response)) { foreach ($response->body()->response as $result) { $data = array('username' => $result->user->screen_name); if (in_array($result->id, $post->aggregated_ids[$this->_key])) { Social_Aggregation_Log::instance($post->ID)->add($this->_key, $result->id, 'retweet', true, $data); continue; } // sanity check if ($this->is_original_broadcast($post, $result->id)) { continue; } Social_Aggregation_Log::instance($post->ID)->add($this->_key, $result->id, 'retweet', false, $data); $post->aggregated_ids[$this->_key][] = $result->id; $post->results[$this->_key][$result->id] = (object) array('id' => $result->id, 'from_user_id' => $result->user->id, 'from_user' => $result->user->screen_name, 'text' => $result->text, 'created_at' => $result->created_at, 'profile_image_url' => $result->user->profile_image_url, 'in_reply_to_status_id' => $result->in_reply_to_status_id, 'raw' => $result, 'comment_type' => 'social-twitter-rt'); } } } // Mentions Social::log('Aggregating Twitter via statuses/mentions'); $response = $this->request($account, 'statuses/mentions', array('count' => 200)); if ($response !== false and is_array($response->body()->response) and count($response->body()->response)) { foreach ($response->body()->response as $result) { if ($this->is_original_broadcast($post, $result->id) || isset($post->results[$this->_key][$result->id])) { continue; } $data = array('username' => $result->user->screen_name); // existing comment if (in_array($result->id, $post->aggregated_ids[$this->_key])) { Social_Aggregation_Log::instance($post->ID)->add($this->_key, $result->id, 'reply', true, $data); continue; } // not a reply to a broadcast, or a reply to an aggregated (or broadcast) comment if (!isset($broadcasted_ids[$result->in_reply_to_status_id]) && (!isset($post->aggregated_ids[$this->_key]) || !in_array($result->in_reply_to_status_id, $post->aggregated_ids[$this->_key]))) { continue; } Social_Aggregation_Log::instance($post->ID)->add($this->_key, $result->id, 'reply', false, $data); $post->aggregated_ids[$this->_key][] = $result->id; $post->results[$this->_key][$result->id] = (object) array('id' => $result->id, 'from_user_id' => $result->user->id, 'from_user' => $result->user->screen_name, 'text' => $result->text, 'created_at' => $result->created_at, 'profile_image_url' => $result->user->profile_image_url, 'in_reply_to_status_id' => $result->in_reply_to_status_id, 'raw' => $result, 'comment_type' => Social_Twitter::is_retweet(null, $result) ? 'social-twitter-rt' : 'social-twitter'); } } } } } }