You are here

function FBAutopost::publish in Facebook Autopost 7

Publishes content in the selected pages

Parameters

$publication: Keyed array containing the info for the publication. Must contain the keys:

  • 'type': The publication type as defined in $publication_types
  • 'params': Associative array with the necessary params for a successful FB publication

$page_id: Page id (among those already selected via UI). If this is present it will override the parameter destination. Use 'me' to publish to the user's timeline

Return value

string Facebook id string for the publication. Needed to edit, or delete the publication.

Throws

FBAutopostException

2 calls to FBAutopost::publish()
FBAutopost::remoteEdit in class/FBAutopost.php
Edits a publication from Facebook
FBAutopostEntity::publishEntity in fb_autopost_entity/class/FBAutopostEntity.php
Publishes content stored in a Facebook publication entity

File

class/FBAutopost.php, line 157

Class

FBAutopost
API class to handle common actions when autoposting This class uses FBAutopostException for error handling. Severity is passed resusing watchdog severity (See: http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/watch...)

Code

function publish($publication, $page_id = NULL) {

  // $page_id parameter for backwards compatibility
  if (!is_null($page_id)) {
    $this
      ->setDestination($page_id);
  }
  $page_id = $this
    ->getDestination();

  // Override publication type if it's present in the publication array.
  if (!empty($publication['type'])) {
    try {
      $this
        ->setType($publication['type']);
    } catch (FBAutopostException $e) {
    }
  }
  if (empty($this->type)) {
    throw new FBAutopostException(t('The publication array must contain publication type.'), FBAutopost::missing_param, WATCHDOG_ERROR);
  }

  // Error generation
  if (!isset($publication['params'])) {
    throw new FBAutopostException(t('The publication array must contain publication parameters.'), FBAutopost::missing_param, WATCHDOG_ERROR);
  }
  $this
    ->checkPagesAvailability();

  // Now we can start the publication.
  try {
    return $this
      ->publishOn($publication);
  } catch (FacebookApiException $e) {
    if ($this
      ->getRetry()) {
      $session = $this
        ->getSession();

      // If trying to publish on timeline for the first time
      if ($page_id == 'me' && !$session
        ->isStored()) {

        // Here we can handle the unauthorized user error
        // To do so we save the relevant data in $_SESSION, then redirect to
        // Facebook athorization URL. This URL redirects back to a fixed
        // location that handles the publishing and redirects back to the
        // original location
        $publication['type'] = $this
          ->getType();
        $session
          ->storePublication($publication);
        $login_url = $this
          ->getLoginUrl(array(
          'scope' => fb_permissions_get_facebook_permissions(),
          'redirect_uri' => url('fbautopost/authorization/retry', array(
            'absolute' => TRUE,
          )),
        ));

        // Redirect the user token the login URL that will redirect back to
        // the retry URI.
        // Remove the destination parameter, this is bad for redirections
        // outside Drupal.
        if (isset($_GET['destination'])) {
          unset($_GET['destination']);
        }
        drupal_goto($login_url);

        // Since there is a redirection the return statement is never
        // executed. Its presence is only for code clarity.
        return NULL;
      }
    }

    // Throw an exception
    throw new FBAutopostException(t('Facebook SDK threw an error: %error', array(
      '%error' => $e,
    )), FBAutopost::sdk_error, WATCHDOG_ERROR);
  }
}