/** * Imports a tweet by URL. * * @return void */ public function action_from_url() { $this->verify_nonce(); Social::log('Import tweet by URL started.'); $service = $this->social->service('twitter'); if ($service !== false) { $response = $service->import_tweet_by_url($this->request->query('post_id'), $this->request->query('url')); if ($response !== true) { echo $response; exit; } Social::log('Import tweet by URL finished.'); } else { Social::log('Import tweet by URL failed, Twitter class not found.'); } echo Social_Aggregation_Log::instance($this->request->query('post_id')); exit; }
/** * Aggregates comments by the service's API. * * @param object $post * * @return array */ public function aggregate_by_api(&$post) { // find broadcasts for service $accounts = $this->get_aggregation_accounts($post); if (isset($accounts[$this->_key]) and count($accounts[$this->_key])) { $like_count = 0; foreach ($accounts[$this->_key] as $account) { if (isset($post->broadcasted_ids[$this->_key][$account->id()])) { foreach ($post->broadcasted_ids[$this->_key][$account->id()] as $broadcasted_id => $data) { $id = explode('_', $broadcasted_id); $request = $this->request($account, $broadcasted_id . '/comments', array('filter' => 'stream', 'fields' => 'parent,message,from,created_time,can_comment', 'limit' => '500')); if ($request !== false && isset($request->body()->response)) { $response = $request->body()->response; if (isset($response->data) and is_array($response->data) and count($response->data)) { foreach ($response->data as $result) { $data = array('parent_id' => $broadcasted_id); 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; } else { if ($this->is_original_broadcast($post, $result->id)) { continue; } else { if ($this->is_duplicate_comment($post, $result->id)) { $post->aggregated_ids[$this->_key][] = $result->id; continue; } } } Social_Aggregation_Log::instance($post->ID)->add($this->_key, $result->id, 'reply', false, $data); if ($result->can_comment) { $result->reply_to_id = $result->id; } else { $result->reply_to_id = $broadcasted_id; } $result->status_id = $broadcasted_id; $post->aggregated_ids[$this->_key][] = $result->id; $post->results[$this->_key][$result->id] = $result; } } } $this->search_for_likes($account, $broadcasted_id, $id[0], $post, $like_count); } } } if (count($like_count)) { Social_Aggregation_Log::instance($post->ID)->add($this->_key, $post->ID . time(), 'like', !$like_count, array('total' => $like_count)); } } }
/** * Runs the aggregation for the requested post ID. * * @return void */ public function action_run() { $this->verify_nonce(); $fetch = Social::option('fetch_comments'); if (empty($fetch)) { Social::log('Aggregation has been disabled, exiting.'); return; } $post = get_post($this->request->query('post_id')); if ($post === null) { return; } Social::log('Begin aggregation for post #:post_id.', array('post_id' => $post->ID)); // Get URLs to query $default_urls = array(home_url('?p=' . $post->ID)); $url = wp_get_shortlink($post->ID); if (strpos($url, '?p=') === false) { $default_urls[] = $url; } // Add the permalink? $permalink = get_permalink($post->ID); if ($default_urls[0] != $permalink) { $default_urls[] = $permalink; } $broadcasted_ids = get_post_meta($post->ID, '_social_broadcasted_ids', true); if (empty($broadcasted_ids)) { $broadcasted_ids = array(); } $aggregated_ids = get_post_meta($post->ID, '_social_aggregated_ids', true); if (empty($aggregated_ids)) { $aggregated_ids = array(); } $post->broadcasted_ids = $broadcasted_ids; $post->aggregated_ids = $aggregated_ids; $post->results = array(); foreach ($this->social->services() as $key => $service) { $urls = $default_urls; $post->results[$key] = array(); if (!isset($post->aggregated_ids[$key])) { $post->aggregated_ids[$key] = array(); } if (isset($broadcasted_ids[$key]) and count($broadcasted_ids[$key])) { $service->aggregate_by_api($post); foreach ($broadcasted_ids[$key] as $broadcasted) { foreach ($broadcasted as $data) { if (isset($data['urls']) and is_array($data['urls'])) { foreach ($data['urls'] as $url) { $urls[] = $url; } } } } } // URL Search $urls = apply_filters('social_search_urls', $urls, $key); $urls = array_unique($urls); if (count($urls)) { foreach ($urls as $key => $url) { $urls[$key] = urlencode($url); } $service->aggregate_by_url($post, $urls); } } if (count($post->results)) { foreach ($post->results as $key => $results) { if (count($results)) { $this->social->service($key)->save_aggregated_comments($post); } } update_post_meta($post->ID, '_social_aggregated_ids', $post->aggregated_ids); } Social::log('Aggregation for post #:post_id complete.', array('post_id' => $post->ID)); // Some cleanup... unset($post->broadcasted_ids); unset($post->aggregated_ids); unset($post->results); if ($this->request->is_ajax()) { // Re-add to the queue? $queue = Social_Aggregation_Queue::factory(); if (!$queue->find($post->ID)) { $queue->add($post->ID, '24hr')->save(); } $log = Social_Aggregation_Log::instance($post->ID); $log->save(true); if (isset($_GET['render']) and $_GET['render'] == 'false') { $total = 0; $log = $log->current(); if (isset($log->items)) { foreach ($log->items as $service => $items) { foreach ($items as $item) { if (!$item->ignored) { ++$total; } } } } $awaiting_mod = wp_count_comments(); $awaiting_mod = $awaiting_mod->moderated; $link = esc_url(admin_url('edit-comments.php?p=' . $post->ID)); $html = ''; if (!isset($_GET['hide_li']) or $_GET['hide_li'] == 'false') { $html = '<li id="wp-adminbar-comments-social">'; } $html .= '<a href="' . $link . '"><span class="social-aggregation-results">' . sprintf(__('(%s New)', 'social'), $total) . '</span></a>'; if (!isset($_GET['hide_li']) or $_GET['hide_li'] == 'false') { $html .= '</li>'; } $response = array('total' => number_format_i18n($awaiting_mod), 'link' => $link, 'html' => $html); echo json_encode($response); } else { $queue = $queue->find($post->ID); $next_run = 0; if ($queue !== false) { $next_run = Social_Aggregation_Queue::next_run($queue->next_run); } echo json_encode(array('html' => $log->render(), 'next_run' => $next_run)); } exit; } else { Social_Aggregation_Log::instance($post->ID)->save(); } // Decrement the semaphore Social_Semaphore::factory()->decrement(); }
/** * Checks to see if the comment is allowed. * * [!!] Handles the exception for duplicate comments. * * @param array $commentdata * @param int $result_id * @param object $post * @return array|bool */ public function allow_comment(array $commentdata, $result_id, &$post) { try { add_filter('wp_die_handler', array('Social', 'wp_die_handler')); $commentdata['comment_approved'] = wp_allow_comment($commentdata); remove_filter('wp_die_handler', array('Social', 'wp_die_handler')); return $commentdata; } catch (Exception $e) { remove_filter('wp_die_handler', array('Social', 'wp_die_handler')); if ($e->getMessage() == Social::$duplicate_comment_message) { // Remove the aggregation ID from the stack unset($post->results[$this->_key][$result_id]); $aggregated_ids = array(); foreach ($post->aggregated_ids[$this->_key] as $id) { if ($id != $result_id) { $aggregated_ids[] = $id; } } $post->aggregated_ids[$this->_key] = $aggregated_ids; // Mark the result as ignored Social_Aggregation_Log::instance($post->ID)->ignore($result_id); } } return false; }
<p class="submit" style="clear:both;float:none;padding:0;"> <a href="<?php echo esc_url(wp_nonce_url(admin_url('index.php?social_controller=aggregation&social_action=run&post_id=' . $post->ID), 'run')); ?> " id="run_aggregation" class="button" style="float:left;margin-bottom:10px;"><?php _e('Find Social Comments', 'social'); ?> </a> <img src="<?php echo esc_url(admin_url('images/wpspin_light.gif')); ?> " style="float:left;position:relative;top:4px;left:5px;display:none;" id="run_aggregation_loader" /> </p> </div><!-- .social-meta-box-block --> <?php } ?> <div class="social-meta-box-block"> <h4><?php _e('Log', 'social'); ?> </h4> <div id="aggregation_log"> <?php echo Social_Aggregation_Log::instance($post->ID); ?> </div> </div><!-- .social-meta-box-block -->
/** * Imports a Tweet by URL. * * @param int $post_id * @param string $url * @return bool|string */ public function import_tweet_by_url($post_id, $url) { if (!($account = $this->api_account())) { return; } $post = get_post($post_id); $post->broadcasted_ids = get_post_meta($post->ID, '_social_broadcasted_ids', true); if (empty($post->broadcasted_ids)) { $post->broadcasted_ids = array(); } $invalid = false; $id = $this->tweet_url_to_id($url); if (!empty($id) and !$this->is_original_broadcast($post, $id)) { Social::log('Importing tweet. -- ID: :id -- URL: :url', array("id" => $id, "url" => $url)); $social_response = $this->request($account, '1.1/statuses/show/' . $id, array('include_entities' => 'true')); error_log(print_r($social_response, true)); if ($social_response !== false and is_object($social_response->body()->response)) { $response = $social_response->body()->response; if ($response !== null and !isset($response->error)) { $logger = Social_Aggregation_Log::instance($post->ID); $post->aggregated_ids = get_post_meta($post->ID, '_social_aggregated_ids', true); if (empty($post->aggregated_ids)) { $post->aggregated_ids = array(); } if (!isset($post->aggregated_ids[$this->_key])) { $post->aggregated_ids[$this->_key] = array(); } if (in_array($id, $post->aggregated_ids[$this->_key])) { $logger->add($this->_key, $response->id, 'Imported', true, array('username' => $response->user->screen_name)); } else { $logger->add($this->_key, $response->id, 'Imported', false, array('username' => $response->user->screen_name)); $post->aggregated_ids[$this->_key][] = $response->id; $post->results = array($this->_key => array($response->id => (object) array('id' => $response->id, 'from_user_id' => $response->user->id, 'from_user' => $response->user->screen_name, 'text' => $response->text, 'created_at' => $response->created_at, 'profile_image_url' => $response->user->profile_image_url, 'profile_image_url_https' => $response->user->profile_image_url_https, 'in_reply_to_status_id' => $response->in_reply_to_status_id, 'raw' => $response, 'comment_type' => 'social-twitter'))); $this->save_aggregated_comments($post, true); // Some cleanup... unset($post->aggregated_ids); unset($post->results); } $logger->save(true); } else { Social::log('Something went wrong... -- :response', array('response' => print_r($response, true))); if (isset($response->error)) { if ($response->error == 'Sorry, you are not authorized to see this status.') { return 'protected'; } else { $invalid = true; } } } } else { $invalid = true; } } else { Social::log('Something went wrong... -- ID: :id -- URL: :url', array('id' => $id, 'url' => implode('/', $url))); $invalid = true; } unset($post->broadcasted_ids); if ($invalid) { return 'invalid'; } return true; }
/** * Imports a Tweet by URL. * * @param int $post_id * @param string $url * @return bool|string */ public function import_tweet_by_url($post_id, $url) { $post = get_post($post_id); $post->broadcasted_ids = get_post_meta($post->ID, '_social_broadcasted_ids', true); if (empty($post->broadcasted_ids)) { $post->broadcasted_ids = array(); } $invalid = false; $id = $this->tweet_url_to_id($url); if (!empty($id) and !$this->is_original_broadcast($post, $id)) { Social::log('Importing tweet. -- ID: :id -- URL: :url'); $url = 'http://api.twitter.com/1/statuses/show.json?id=' . $id; $request = wp_remote_get($url); if (!is_wp_error($request)) { $response = apply_filters('social_response_body', $request['body'], $this->_key); if ($response !== null and !isset($response->error)) { $logger = Social_Aggregation_Log::instance($post->ID); $post->aggregated_ids = get_post_meta($post->ID, '_social_aggregated_ids', true); if (empty($post->aggregated_ids)) { $post->aggregated_ids = array(); } if (!isset($post->aggregated_ids[$this->_key])) { $post->aggregated_ids[$this->_key] = array(); } if (in_array($id, $post->aggregated_ids[$this->_key])) { $logger->add($this->_key, $response->id, 'Imported', true, array('username' => $response->user->screen_name)); } else { $logger->add($this->_key, $response->id, 'Imported', false, array('username' => $response->user->screen_name)); $post->aggregated_ids[$this->_key][] = $response->id; $post->results[$this->_key][$response->id] = (object) array('id' => $response->id, 'from_user_id' => $response->user->id, 'from_user' => $response->user->screen_name, 'text' => $response->text, 'created_at' => $response->created_at, 'profile_image_url' => $response->user->profile_image_url, 'in_reply_to_status_id' => $response->in_reply_to_status_id, 'raw' => $response, 'comment_type' => 'social-twitter'); $this->save_aggregated_comments($post, true); // Some cleanup... unset($post->aggregated_ids); unset($post->results); } $logger->save(true); } else { Social::log('Something went wrong... -- :response', array('response' => print_r($response, true))); if (isset($response->error)) { if ($response->error == 'Sorry, you are not authorized to see this status.') { return 'protected'; } else { $invalid = true; } } } } else { Social::log('Something went wrong... -- :response', array('response' => print_r($request, true))); $invalid = true; } } else { Social::log('Something went wrong... -- ID: :id -- URL: :url', array('id' => $id, 'url' => implode('/', $url))); $invalid = true; } unset($post->broadcasted_ids); if ($invalid) { return 'invalid'; } return true; }