You are here

function _tweet_process in Tweet 7.4

Same name and namespace in other branches
  1. 5.2 tweet.module \_tweet_process()
  2. 6.4 tweet.module \_tweet_process()
  3. 6.2 tweet.module \_tweet_process()
  4. 6.3 tweet.module \_tweet_process()

Determines what will be in the tweet itself.

Parameters

$format: A string containing the text of the tweet before it gets processed.

$tokens: An associative array where keys represent text that will be replaced by their value in $format. Because only 140 characters are allowed, it is possible that values will be truncated or not included. Tokens are assumed to be ordered by importance (most important first).

Return value

The URL-ready tweet text.

1 call to _tweet_process()
_tweet_to_twitter in ./tweet.module
Creates a link to post a URL and optionally title to twitter. Uses the current page by default.

File

./tweet.module, line 280
Builds links to post pages to Twitter API sites.

Code

function _tweet_process($format = '', $tokens = array()) {
  if (!$format) {
    $format = variable_get('tweet_format', '[url] [title] [node-tags]');
  }

  // The major downside of the way we're counting characters here is that we
  // can't guarantee a minimum number of characters for a given token. However
  // in order to do that, the process would need to be much more complex. It
  // just seems like overkill.
  $len = 140 - drupal_strlen(str_replace(array_keys($tokens), array(), $format));
  foreach ($tokens as $search => $replace) {

    // In order to include text for a token, we want at least 3 letters and an ellipsis.
    if ($len < 5 || strpos($format, $search) === FALSE) {
      $replace = '';
    }
    elseif (drupal_strlen($replace) > $len) {

      // "\xE2\x80\xA6" is the UTF8 character sequence for the ellipsis, which must be enclosed in double quotes.
      
      $replace = drupal_substr($replace, 0, $len - 1) . "…";
    }
    $len -= drupal_strlen($replace);
    $format = str_replace($search, $replace, $format);
  }

  // Collapse whitespace (multiple spaces can occur if any token is empty).
  if (strpos($format, '  ') !== FALSE) {
    $format = preg_replace('/\\s\\s+/', ' ', $format);
  }

  // Drupal does not automatically urlencode links.

  //$format = drupal_encode_path($format);

  // The #, &, and / characters get double-encoded by drupal_urlencode,
  // but they must appear single-encoded for Twitter to recognize them.
  // We replace urlencoded spaces with plus signs here for clarity.

  //$format = str_replace(array('%2523', '%2526', '%252F', '%20'), array('%23', '%26', '%2F', '+'), $format);
  return $format;
}