You are here

function fb_stream_post_on_shutdown in Drupal for Facebook 7.3

Logic to post to a Facebook stream. This callback is invoked as the page request shuts down, as late as possible in the order. This runs after all node insert/update hooks have completed.

2 string references to 'fb_stream_post_on_shutdown'
fb_stream_node_insert in ./fb_stream.module
Implements hook_node_insert().
fb_stream_node_update in ./fb_stream.module
Implements hook_node_update().

File

./fb_stream.module, line 79
Support for Facebook's Stream API.

Code

function fb_stream_post_on_shutdown($node) {
  if (TRUE) {

    // Maintain same level of indentation as D6 branch, so that patches easily apply.
    if (!empty($node->fb_stream_do_post)) {
      $node_url = url("node/{$node->nid}", array(
        'absolute' => TRUE,
      ));
      $body = field_get_items('node', $node, 'body');
      $params = array(
        'access_token' => $node->fb_stream_from_token,
        'message' => $node->fb_stream_message,
        'link' => $node_url,
        'name' => $node->title,
        'description' => text_summary($body[0]['value']),
        'caption' => variable_get('site_name', ''),
        'actions' => json_encode(array(
          'name' => t('View More'),
          'link' => $node_url,
        )),
        'method' => 'POST',
      );

      // Let third parties alter params.
      $params = fb_invoke(FB_STREAM_OP_PRE_POST, array(
        'node' => $node,
      ), $params, 'fb_stream');
      if ($params['name']) {

        // http://stackoverflow.com/questions/4652628/what-markup-is-allowed-in-the-description-of-a-facebook-feed-post
        $params['description'] = strip_tags($params['description'], '<b><i><small><center>');
        try {
          $result = fb_graph($node->fb_stream_to . '/feed', $params, 'POST');

          // ID returned to us is not a normal graph ID.  We can't query it, but can build a permalink.
          if ($result['id']) {
            $exploded_result = explode('_', $result['id']);
            $story_fbid = 'story_fbid=' . $exploded_result[1];
            $id = 'id=' . $exploded_result[0];
            $link = FB_FACEBOOK_BASE_URL . '/' . 'permalink.php?' . $story_fbid . '&' . $id;
            drupal_set_message(t('Posted %node to <a href="!url" target="_blank">Facebook</a>.', array(
              '!url' => $link,
              '%node' => $node->title,
            )));
          }
        } catch (Exception $e) {
          $msg = t('Failed to post content to Facebook.');
          drupal_set_message($msg, 'warning');
          fb_log_exception($e, $msg);
        }
      }
      else {

        // Cannot post without link name.  If here, implement hook_fb_stream() to ensure name is set.
        drupal_set_message(t('Cannot post <a href=!link>%title (node/!nid)</a> to facebook.  Post has no name.', array(
          '!nid' => $node->nid,
          '!link' => $node_url,
          '%title' => $node->title,
        )), 'warning');
      }
    }
  }
}