Skip to content
Browse files

Merge branch 'feature/facebook-comment-threading-11' into develop

  • Loading branch information...
2 parents a6dcdf5 + 4128e34 commit 8f63e35345f7ef8353ce46907b7693c2b1e2b58f @alexkingorg alexkingorg committed Apr 25, 2012
Showing with 111 additions and 58 deletions.
  1. +29 −22 assets/social.js
  2. +6 −5 lib/social/response.php
  3. +65 −19 lib/social/service/facebook.php
  4. +6 −5 lib/social/service/twitter.php
  5. +5 −7 social.php
View
51 assets/social.js
@@ -203,30 +203,41 @@
$title.data('orig-title', $title.find('span').text())
.find('span').html(Sociali18n.commentReplyTitle + ' ')
.append($cancel.show());
- if ($parent.hasClass('social-twitter')) {
- // check to see if the current user has a Twitter profile
- if ($('#post_accounts option[data-type="twitter"]').size() == 0 &&
- $form.find('.social-identity .social-twitter-icon').size() == 0) {
+ if ($parent.hasClass('social-twitter') || $parent.hasClass('social-facebook')) {
+ var commentService = ($parent.hasClass('social-twitter') ? 'twitter' : 'facebook');
+ // check to see if the current user has a profile for the service
+ if ($('#post_accounts option[data-type="' + commentService + '"]').size() == 0 &&
+ $form.find('.social-identity .social-' + commentService + '-icon').size() == 0) {
return;
}
- // look for a username at the beginning of the comment
- // if we have that account available, use it
- // if not, select the first account for that service
var username = '',
matched = false,
$option = false,
- $options = $('#post_accounts option[data-type="twitter"]');
- var content = $.trim($parent.find('.social-comment-body:first').text());
- if (content.substr(0, 1) == '@') {
- username = content.split(' ')[0].substr(1);
+ $options = $('#post_accounts option[data-type="' + commentService + '"]');
+ switch (commentService) {
+ case 'twitter':
+ // look for a username at the beginning of the comment
+ // if we have that account available, use it
+ // if not, select the first account for that service
+ var content = $.trim($parent.find('.social-comment-body:first').text());
+ if (content.substr(0, 1) == '@') {
+ username = content.split(' ')[0].substr(1);
+ }
+ $options.each(function() {
+ if ($(this).html() == username) {
+ matched = true;
+ $option = $(this);
+ return false;
+ }
+ });
+ var $author = $parent.find('.social-comment-author a'),
+ author_rel = $author.attr('rel').split(' ');
+ $('#in_reply_to_status_id').val(author_rel[0]);
+ insertTwitterUsername($author, $textarea);
+ break;
+ case 'facebook':
+ break;
}
- $options.each(function() {
- if ($(this).html() == username) {
- matched = true;
- $option = $(this);
- return false;
- }
- });
if (!matched) {
$options.each(function() {
$option = $(this);
@@ -237,10 +248,6 @@
$('#post_accounts').val($option.attr('value')).change();
}
$('#post_to_service').prop('checked', true);
- var $author = $parent.find('.social-comment-author a'),
- author_rel = $author.attr('rel').split(' ');
- $('#in_reply_to_status_id').val(author_rel[0]);
- insertTwitterUsername($author, $textarea);
}
});
View
11 lib/social/response.php
@@ -127,11 +127,12 @@ public function general_error() {
* @return string
*/
public function id() {
- $body = $this->body();
- if (isset($body->response)) {
- return $body->response->{$this->_service->response_id_key()};
+ if ($body = $this->body()) {
+ if (isset($body->response) && isset($body->response->{$this->_service->response_id_key()})) {
+ return $body->response->{$this->_service->response_id_key()};
+ }
}
- return 0;
+ return '0';
}
/**
@@ -140,7 +141,7 @@ public function id() {
* @return bool|object
*/
public function body() {
- if (isset($this->_request['body']) and isset($this->_request['body'])) {
+ if (isset($this->_request['body'])) {
return $this->_request['body'];
}
View
84 lib/social/service/facebook.php
@@ -38,35 +38,81 @@ public function request($account, $api, array $args = array(), $method = 'GET')
/**
* 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 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) {
+ public function broadcast($account, $message, array $args = array(), $post_id = null, $comment_id = null) {
global $post;
- $post = get_post($post_id);
+ // 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 (get_post_format($post->ID) !== 'status') {
- $args = $args + array(
- 'link' => get_post_permalink($post->ID),
- 'title' => $post->post_title,
- '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');
- $args = $args + array(
- 'picture' => $image[0],
- );
+ // first try to send comment to an existing Fb post
+ if (!is_null($comment_id)) {
+ $comment = get_comment($comment_id);
+ if (!empty($comment->comment_parent)) {
+ $parent_comment = get_comment($comment->comment_parent);
+ if (!is_null($parent_comment) && in_array($parent_comment->comment_type, self::comment_types())) {
+ $status_id = get_comment_meta($parent_comment->comment_ID, 'social_status_id', true);
+ if (!empty($status_id)) {
+ // we have a Facebook post to reply to
+ $parts = explode('_', $status_id);
+ if (count($parts) == 3) {
+ $status_id = $parts[0].'_'.$parts[1];
+ }
+ $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->id() !== '0') {
+ // post succeeded, return response
+ return $response;
+ }
+ // ...broadcast failed, continue and send as post to feed
+ }
+ }
}
}
+ // prep data
+ if (!is_null($comment_id)) {
+ $post = get_post($comment->comment_post_ID);
+ $type = 'comment';
+ }
+ if (!is_null($post_id)) {
+ $post = get_post($post_id);
+ $type = 'post';
+ }
+ setup_postdata($post);
+
+ $link_args = array(
+ 'link' => get_post_permalink($post->ID),
+ 'title' => $post->post_title,
+ '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();
+
+ if ($type == 'comment' || ($type == 'post' && get_post_format($post->ID) !== 'status')) {
+ $args = $args + $link_args;
+ }
+
// Set access token?
$broadcast_account = $account->broadcast_page();
if ($broadcast_account !== null) {
@@ -76,7 +122,7 @@ public function broadcast($account, $message, array $args = array(), $post_id =
);
}
- $args = apply_filters($this->key().'_broadcast_args', $args, $post_id);
+ $args = apply_filters($this->key().'_broadcast_args', $args, $post_id, $comment_id);
return $this->request($account, 'feed', $args, 'POST');
}
@@ -205,7 +251,7 @@ private function search_for_likes(&$account, $id, $parent_id, &$post, &$like_cou
$post->aggregated_ids[$this->_key][] = $result->id;
$post->results[$this->_key][$result->id] = (object) array_merge(array(
'like' => true,
- 'status_id' => $parent_id.'_'.$id,
+ 'status_id' => $id,
'raw' => $result,
), (array) $result);
++$like_count;
View
11 lib/social/service/twitter.php
@@ -28,14 +28,15 @@ public function max_broadcast_length() {
/**
* Broadcasts the message to the specified account. Returns the broadcasted ID.
*
- * @param Social_Service_Account $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 Social_Service_Account $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) {
+ public function broadcast($account, $message, array $args = array(), $post_id = null, $comment_id = null) {
$args = $args + array(
'status' => $message
);
View
12 social.php
@@ -1382,13 +1382,11 @@ public function comment_post($comment_ID) {
delete_comment_meta($comment_ID, 'social_in_reply_to_status_id');
}
Social::log(sprintf(__('Broadcasting comment #%s to %s using account #%s.', 'social'), $comment_ID, $service->title(), $account->id()));
- $response = $service->broadcast($account, $output, $args);
- if ($response === false or $response->id() === false) {
+ $response = $service->broadcast($account, $output, $args, null, $comment_ID);
+ if ($response === false or $response->id() === '0') {
wp_delete_comment($comment_ID);
- $message = sprintf(__('Error: Broadcast comment #%s to %s using account #%s, please go back and try again.', 'social'), $comment_ID, $service->title(), $account->id());
-
- Social::log($message);
- wp_die($message);
+ Social::log(sprintf(__('Error: Broadcast comment #%s to %s using account #%s, please go back and try again.', 'social'), $comment_ID, esc_html($service->title()), esc_html($account->id())));
+ wp_die(sprintf(__('Error: Your comment could not be sent to %s, please go back and try again.', 'social'), esc_html($service->title())));
}
$wpdb->query($wpdb->prepare("
@@ -1473,7 +1471,7 @@ public function wp_set_comment_status($comment_id, $comment_status) {
}
$output = $service->format_comment_content($comment, Social::option('comment_broadcast_format'));
- $response = $service->broadcast($account, $output, $args);
+ $response = $service->broadcast($account, $output, $args, null, $comment_id);
if ($response === false or $response->id() === false) {
wp_delete_comment($comment_id);
Social::log(sprintf(__('Error: Broadcast comment #%s to %s using account #%s, please go back and try again.', 'social'), $comment_id, $service->title(), $account->id()));

0 comments on commit 8f63e35

Please sign in to comment.
Something went wrong with that request. Please try again.