You are here

function fb_opengraph_publish_action in Drupal for Facebook 7.4

Helper function to publish user activity to Facebook's Open Graph.

1 call to fb_opengraph_publish_action()
fb_opengraph_fb in ./fb_opengraph.module
Implements hook_fb().
1 string reference to 'fb_opengraph_publish_action'
fb_opengraph_fb in ./fb_opengraph.module
Implements hook_fb().

File

./fb_opengraph.module, line 87
Open Graph Helpers

Code

function fb_opengraph_publish_action($action, $params, $options = array()) {
  if (empty($options['skip_access_check']) && !user_access(FB_OPENGRAPH_PERM_PUBLISH)) {
    if (fb_verbose()) {

      // Because custom modules call this function and can get tripped up by permissions, log a message.
      watchdog('fb_opengraph', 'User does not have permission to publish facebook action.', array(), WATCHDOG_WARNING);
    }
    return;
  }

  // Defaults.
  if (isset($options['fb_app'])) {
    $fb_app = $options['fb_app'];
  }
  else {
    $fb_app = fb_get_app();
  }
  if (isset($options['fbu'])) {
    $fbu = $options['fbu'];
  }
  else {
    $fbu = 'me';
  }
  if (!$action || !$fbu || !$fb_app) {

    // We don't have enough information.
    return;
  }
  if (isset($options['action_type'])) {
    $action_type = $options['action_type'];

    // Human readable verb.
  }
  else {
    $action_type = $action;
  }
  try {

    // @TODO: prepend namespace to custom actions.
    $result = fb_graph_post($fbu . '/' . $action, $params);
    if (isset($result['id'])) {
      $id = $result['id'];
      $message_args = array(
        '!delete_url' => url(FB_OPENGRAPH_PATH_DELETE . '/' . $id, array(
          'destination' => current_path(),
        )),
        '%action_type' => $action_type,
        '!profile_url' => 'https://www.facebook.com/' . $fbu,
      );
      if (variable_get(FB_OPENGRAPH_VAR_MESSAGES, TRUE)) {
        $message = t('Published %action_type activity to <a href=!profile_url>Facebook</a>.', $message_args);
        if (user_access(FB_OPENGRAPH_PERM_DELETE_OWN)) {
          $message .= '&nbsp;' . t('[<a href=!delete_url>delete from timeline</a>]');
        }
        drupal_set_message($message);
      }
      return $id;
    }
    else {
      if (fb_verbose()) {
        watchdog('fb_opengraph', 'Failed to publish %action by user %fbu to facebook.  Facebook did not return an ID!', array(
          '%action' => $action,
          '%fbu' => $fbu,
        ), WATCHDOG_WARNING);
      }
    }
  } catch (Exception $e) {
    fb_log_exception($e, t('Failed to publish %action_type action', array(
      '%action_type' => $action_type,
    )));
    if (!empty($options['throw_errors'])) {
      throw $e;
    }
  }
}