You are here

function twitter_post_node_insert in Twitter 7.5

Same name and namespace in other branches
  1. 7.3 twitter_post/twitter_post.module \twitter_post_node_insert()
  2. 7.4 twitter_post/twitter_post.module \twitter_post_node_insert()

Implementation of hook_node_insert().

Intercepts newly published nodes and posts notices to Twitter.

1 call to twitter_post_node_insert()
twitter_post_node_update in twitter_post/twitter_post.module
Implementation of hook_node_update().

File

twitter_post/twitter_post.module, line 148
Hook implementations for twitter_post module.

Code

function twitter_post_node_insert($node) {
  if (!empty($node->status) && !empty($node->twitter) && !empty($node->twitter['post'])) {
    module_load_include('inc', 'twitter');
    $twitter_account = twitter_account_load($node->twitter['account']);
    global $user;
    $account = user_load($user->uid);
    $access_global = user_access('post to twitter with global account', $account);

    // Only allow the tweet to be posted if the Twitter account is either a
    // global account and the user has access to global accounts, or it is tied
    // to the current user.
    if (!($twitter_account->is_global && $access_global || $twitter_account->uid == $account->uid)) {
      return;
    }

    // Prepare the string replacements for the status message.
    $replacements = array(
      // 117 characters is the maximum that can be added to a tweet and still
      // have a URL, so don't allow the title to be longer than that.
      '!title' => truncate_utf8($node->title, 117, FALSE, TRUE),
      '!url' => url('node/' . $node->nid, array(
        'absolute' => TRUE,
        'alias' => TRUE,
      )),
      '!url-alias' => url('node/' . $node->nid, array(
        'absolute' => TRUE,
      )),
      '!user' => $node->name,
    );

    // Only generate the shortened URL if it's going to be used. No sense
    // burning through TinyURLs without a good reason.
    if (strstr($node->twitter['status'], '!tinyurl') !== FALSE) {
      $replacements['!tinyurl'] = twitter_shorten_url(url('node/' . $node->nid, array(
        'absolute' => TRUE,
      )));
    }

    // Modify replacement from other modules.
    drupal_alter('twitter_post_replacement', $replacements, $node);
    $status = strtr($node->twitter['status'], $replacements);
    try {
      $status = twitter_set_status($twitter_account, $status);
      $message = t('An empty status was returned. Check the system log for details.');
      $type = 'error';
      if (!empty($status)) {
        $message = t('Successfully posted "%node" to Twitter: <a href="@status" target="_blank">@status</a>', array(
          '@status' => _twitter_status_url($status),
          '%node' => $node->title,
        ));
        $type = 'status';
      }
      drupal_set_message($message, $type);
    } catch (TwitterException $e) {
      drupal_set_message(t('An error occurred when posting to Twitter. Check the system log for details.'), 'error');
    }

    // Return something useful for module_invoke().
    return $status;
  }
}