You are here

function twitter_nodeapi in Twitter 6.4

Same name and namespace in other branches
  1. 5 twitter.module \twitter_nodeapi()
  2. 6.2 twitter.module \twitter_nodeapi()
  3. 6.3 twitter_post/twitter_post.module \twitter_nodeapi()

Implementation of hook_nodeapi().

Intercepts newly published nodes and posts noticed to Twitter.

File

twitter_post/twitter_post.module, line 163
Main hooks for twitter post module

Code

function twitter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
      if (!empty($node->status) && !empty($node->twitter) && !empty($node->twitter['post'])) {
        module_load_include('inc', 'twitter');
        $twitter_account = twitter_account_load($node->twitter['account']);
        $replacements = array(
          '!title' => $node->title,
          '!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,
          )));
        }
        $status = strtr($node->twitter['status'], $replacements);

        // If token module is available, process status to do the token replacement
        if (module_exists('token')) {
          $status = token_replace($status, 'node', $node);
        }
        try {
          $result = twitter_set_status($twitter_account, $status);
          if ($result) {
            drupal_set_message(t('Successfully posted to Twitter'));
          }
        } catch (TwitterException $e) {
          drupal_set_message(t('An error occurred when posting to twitter: %code %error', array(
            '%code' => $result->code,
            '%error' => $result->error,
          )), 'warning');
        }
      }
      break;
  }
}