You are here

function twitter_nodeapi in Twitter 6.3

Same name and namespace in other branches
  1. 5 twitter.module \twitter_nodeapi()
  2. 6.2 twitter.module \twitter_nodeapi()
  3. 6.4 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 164
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);
        }
        if (twitter_set_status($twitter_account, $status)) {
          drupal_set_message(t('Successfully posted to Twitter'));
        }
      }
      break;
  }
}