You are here

function janrain_capture_comment_view_alter in Janrain Registration 7.4

Same name and namespace in other branches
  1. 7.2 janrain_capture.module \janrain_capture_comment_view_alter()
  2. 7.3 janrain_capture.module \janrain_capture_comment_view_alter()

Implements hook_comment_view_alter().

Add Engage social sharing to comment links and, if a comment's just been added, pop-up the social widget.

See also

janrain_capture_comment_insert()

File

./janrain_capture.module, line 920
This module implements authentication endpoints for Janrain Capture.

Code

function janrain_capture_comment_view_alter(&$build) {
  $janrain_share_settings = variable_get('janrain_capture_share', array());

  // Should we bother?
  if ($build['#view_mode'] != 'full' || !isset($janrain_share_settings['enabled']) || !$janrain_share_settings['enabled'] || isset($build['#comment']->in_preview)) {
    return;
  }
  global $user;
  $comment = $build['#comment'];
  $node = node_load($comment->nid);
  $attach_share = variable_get('janrain_capture_share_attach_' . $node->type, FALSE);

  // We should automatically pop up the Social Sharing widget if this is the
  // comment that has just been added.
  $popup_social = variable_get('janrain_capture_share_popup_' . $node->type, FALSE) && isset($_SESSION['janrain_capture_comment_social_cid']) && $comment->cid == $_SESSION['janrain_capture_comment_social_cid'];
  if (!$attach_share && !$popup_social) {
    return;
  }
  $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . request_uri() . '#comment-' . $comment->cid;

  // Prepare arguments for Social Sharing
  $args = array();
  foreach (array(
    'message',
    'title',
    'summary',
  ) as $arg) {
    $args[$arg] = $janrain_share_settings['comments']['default_' . $arg];

    // We disable sanitize for token_replace() to prevent it from encoding
    // apostrophes and such.
    $args[$arg] = filter_xss(token_replace($args[$arg], array(
      'node' => $node,
      'user' => $user,
      'comment' => $comment,
    ), array(
      'clear' => TRUE,
      'sanitize' => FALSE,
    )));
  }
  $summary = addslashes(text_summary(strip_tags($args['summary']), NULL, 150) . '...');
  $args['title'] = addslashes($args['title']);
  $args['message'] = addslashes($args['message']);

  // Attach a "share" link to this comment.
  $attributes = array(
    'class' => array(
      'janrain-capture-link-social',
    ),
    'id' => 'janrain-capture-link-social-comment-' . $comment->cid,
    'onclick' => "janrainCaptureSetShare('{$url}','{$args['title']}','{$summary}','{$args['message']}','facebook'); return false;",
  );
  $build['links']['comment']['#links']['comment-janrain-capture-share'] = array(
    'title' => t('share'),
    'href' => $url,
    'attributes' => $attributes,
  );

  // output the js
  janrain_capture_share_js();

  // Pass arguments for the social widget that will be invoked for this
  // comment immediately upon page reload.
  if ($popup_social) {

    // FIXME: this is prone to race conditions; should fire when the widget
    // finishes loading instead.
    $output = "setTimeout(function(){janrainCaptureSetShare('{$url}','{$args['title']}','{$summary}','{$args['message']}','facebook')},600);";
    drupal_add_js($output, array(
      'type' => 'inline',
      'scope' => 'footer',
      'preprocess' => FALSE,
    ));
    unset($_SESSION['janrain_capture_comment_social_cid']);
  }
}