/** * Broadcasts the message to the specified account. Returns the broadcasted ID. * * @param Social_Service_Facebook_Account|object $account account to broadcast to * @param string $message message to broadcast * @param array $args extra arguments to pass to the request * @param int $post_id post ID being broadcasted * @param int $comment_id comment ID being broadcasted * * @return Social_Response */ public function broadcast($account, $message, array $args = array(), $post_id = null, $comment_id = null) { global $post; // if post ID is set, this is a broadcast of a post, // if the comment ID is set it is a broadcast of a comment // TODO - add wrapper functions that abstract these actions out to separate methods // check comment being replied to, if it is a facebook comment on a post then // send the comment as a reply on the same post. // If that fails, then send as posting a link with a comment. $args = $args + array('message' => $message); if ($comment_id && ($comment = get_comment($comment_id))) { // Check for facebook comment reply if ($comment->comment_parent && ($parent_comment = get_comment($comment->comment_parent)) && in_array($parent_comment->comment_type, self::comment_types())) { if ($status_id = get_comment_meta($parent_comment->comment_ID, 'social_reply_to_id', true)) { $parent_status_id = get_comment_meta($comment->comment_parent, 'social_status_id', true); $args = apply_filters($this->key() . '_broadcast_args', $args, $post_id, $comment_id); $response = $this->request($account, $status_id . '/comments', $args, 'POST'); if ($response !== false && $response->body()->result == 'success') { // post succeeded, return response update_comment_meta($comment->comment_ID, 'social_reply_to_id', addslashes_deep($status_id)); update_comment_meta($comment->comment_ID, 'social_status_id', addslashes_deep($parent_status_id)); update_comment_meta($comment->comment_ID, 'social_broadcast_id', addslashes_deep($response->body()->response->id)); return $response; } } } $broadcasted_ids = get_post_meta($comment->comment_post_ID, '_social_broadcasted_ids', true); // If only 1 account has been posted to if (count($broadcasted_ids[$this->key()]) === 1) { $broadcast_account = array_shift($broadcasted_ids[$this->key()]); // And only one broadcast has been made from that account if (count($broadcast_account) === 1) { reset($broadcast_account); $status_id = key($broadcast_account); $args = apply_filters($this->key() . '_broadcast_args', $args, $post_id, $comment_id); $response = $this->request($account, $status_id . '/comments', $args, 'POST'); if ($response !== false && $response->body()->result == 'success') { // post succeeded, return response $broadcasted_id = $response->body()->response->id; $response = $this->request($account, $broadcasted_id, array('fields' => 'can_comment')); if ($response !== false && $response->body()->result == 'success' && $response->body()->response->can_comment) { update_comment_meta($comment->comment_ID, 'social_reply_to_id', addslashes_deep($response->body()->response->id)); } else { update_comment_meta($comment->comment_ID, 'social_reply_to_id', addslashes_deep($status_id)); } update_comment_meta($comment->comment_ID, 'social_broadcast_id', addslashes_deep($broadcasted_id)); update_comment_meta($comment->comment_ID, 'social_status_id', addslashes_deep($status_id)); return $response; } } } // Continuing on to simply post on user's wall // posting with a link, do not include URL in comment. $format = trim(str_replace('{url}', '', Social::option('comment_broadcast_format'))); $message = $this->format_comment_content($comment, $format); $args['message'] = $message; // prep data $post = get_post($comment->comment_post_ID); setup_postdata($post); $link_args = array('link' => social_get_shortlink($post->ID), 'title' => get_the_title($post->ID), 'description' => get_the_excerpt()); if (function_exists('has_post_thumbnail') and has_post_thumbnail($post->ID)) { $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); $link_args = $link_args + array('picture' => $image[0]); } wp_reset_postdata(); $args = $args + $link_args; } // Set access token? $broadcast_account = $account->broadcast_page(); if ($broadcast_account !== null) { $args = $args + array('access_token' => $broadcast_account->access_token, 'page_id' => $broadcast_account->id); } $args = apply_filters($this->key() . '_broadcast_args', $args, $post_id, $comment_id); $request = apply_filters($this->key() . '_broadcast_request', array('url' => 'me/feed', 'args' => $args, 'post_id' => $post_id, 'comment_id' => $comment_id)); $response = $this->request($account, $request['url'], $request['args'], 'POST'); if ($response !== false && $response->body()->result == 'success') { // post succeeded, return response update_comment_meta($comment->comment_ID, 'social_reply_to_id', addslashes_deep($response->body()->response->id)); update_comment_meta($comment->comment_ID, 'social_broadcast_id', addslashes_deep($response->body()->response->id)); } return $response; }
/** * Runs the aggregation for the requested post ID. * * @return void */ public function action_run() { $this->verify_nonce(); $fetch = Social::option('aggregate_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 = social_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] = $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(); }
/** * Sets the broadcasted IDs for the post. * * @param int $post_id post id * @param string $service service key * @param string $broadcasted_id broadcasted id * @param string $message broadcasted message * @param Social_Service_Account $account account * @param Social_Response $response response object * @return void */ public function add_broadcasted_id($post_id, $service, $broadcasted_id, $message, $account, Social_Response $response = null) { $broadcasted_ids = get_post_meta($post_id, '_social_broadcasted_ids', true); if (empty($broadcasted_ids)) { $broadcasted_ids = array(); } if (!isset($broadcasted_ids[$service])) { $broadcasted_ids[$service] = array(); } if (!isset($broadcasted_ids[$service][$account->id()])) { $broadcasted_ids[$service][$account->id()] = array(); } if (!isset($broadcasted_ids[$service][$account->id()][$broadcasted_id])) { $urls = array(get_permalink($post_id)); $shortlink = social_get_shortlink($post_id); if (!in_array($shortlink, $urls)) { $urls[] = $shortlink; } $home_url = home_url('?p=' . $post_id); if (!in_array($home_url, $urls)) { $urls[] = $home_url; } $data = array('message' => $message, 'urls' => $urls); $data = apply_filters('social_save_broadcasted_ids_data', $data, $account, $service, $post_id, $response); $broadcasted_ids[$service][$account->id()][$broadcasted_id] = $data; update_post_meta($post_id, '_social_broadcasted_ids', $broadcasted_ids); } }
/** * Formats a comment before it's broadcasted. * * @param WP_Comment $comment * @param array $format * @return string */ public function format_comment_content($comment, $format) { // Filter the format $format = apply_filters('social_comment_broadcast_format', $format, $comment, $this); $_format = $format; $available = $this->max_broadcast_length(); $used_tokens = array(); // Gather used tokens and subtract remaining characters from available length foreach (Social::comment_broadcast_tokens() as $token => $description) { $replaced = 0; $_format = str_replace($token, '', $_format, $replaced); if ($replaced) { $used_tokens[$token] = ''; } } $available = $available - social_strlen($_format); // Prep token replacement content foreach ($used_tokens as $token => $content) { switch ($token) { case '{url}': $url = social_get_shortlink($comment->comment_post_ID); if (empty($url)) { $url = home_url('?p=' . $comment->comment_post_ID); } $url .= '#comment-' . $comment->comment_ID; $url = apply_filters('social_comment_broadcast_permalink', $url, $comment, $this); $used_tokens[$token] = esc_url($url); break; case '{content}': $used_tokens[$token] = strip_tags($comment->comment_content); $used_tokens[$token] = str_replace(' ', '', $used_tokens[$token]); break; } } // if {url} is used, pre-allocate its length if (isset($used_tokens['{url}'])) { $available = $available - social_strlen($used_tokens['{url}']); } $used_tokens['{content}'] = apply_filters('social_format_comment_content', $used_tokens['{content}'], $comment, $format, $this); // Truncate content to size limit if (social_strlen($used_tokens['{content}']) > $available) { $used_tokens['{content}'] = social_substr($used_tokens['{content}'], 0, $available - 3) . '...'; } foreach ($used_tokens as $token => $replacement) { if (strpos($format, $token) !== false) { $format = str_replace($token, $replacement, $format); } } $format = apply_filters('social_comment_broadcast_content_formatted', $format, $comment, $this); return $format; }