You are here

function _tweet_to_twitter in Tweet 7.4

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

Creates a link to post a URL and optionally title to twitter. Uses the current page by default.

Parameters

$site: The site to which the generated Tweet link leads. Twitter by default.

$type: Specifies what will show up in the link: the twitter icon, the twitter icon and text, or just text. Pass 'icon' to show just the icon, 'icon_text' to show the icon and text, and 'text' to show just the text. Required if display options for nodes are set to 'none' on the settings page.

$format: A string representing the tweet text, optionally with the case-insensitive tokens [url], [title], and [node-tags]. If not passed, the format from the settings page will be used.

$nid: The NID of the node for which the twitter link should be constructed, or the absolute URL of the page for which the twitter link should be constructed. If the URL given is not the current URL, and if $nid is not a NID, the title must be set manually (instead of using the [title] token) or it will be incorrect.

Return value

A themed link to post the specified or current page to twitter.

2 calls to _tweet_to_twitter()
tweet_node_view in ./tweet.module
Implements hook_node_view().
tweet_to_twitter in ./tweet.module
Returns a link from _tweet_to_twitter().

File

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

Code

function _tweet_to_twitter($site = 'Twitter', $type = '', $format = '', $nid = '') {
  $q = '';
  $node_tags = '';
  $teaser = '';
  if (!$format) {
    $format = variable_get('tweet_format', '[url] [title] [node-tags]');
  }
  $sites = tweet_sites();
  $site_info = $sites[$site];
  if (empty($nid) && arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
  }
  if (is_numeric($nid)) {
    $q = url('node/' . $nid, array(
      'absolute' => TRUE,
    ));
    if (module_exists('taxonomy') && strpos($format, '[node-tags]') !== FALSE) {
      $node_tag_vocabs = variable_get('tweet_node_vocabs', array());
      $tags = array();
      foreach ($node_tag_vocabs as $vid => $enabled) {
        if ($enabled) {

          // In Drupal 6, we had taxonomy_node_get_terms_by_vocabulary().
          // That doesn't exist in Drupal 7, so it is ported below.
          $query = db_select('taxonomy_term_data', 't')
            ->addTag('node_access')
            ->fields('t', array(
            'tid',
            'vid',
            'name',
            'description',
            'format',
            'weight',
          ));
          $query
            ->innerJoin('taxonomy_index', 'r', 'r.tid = t.tid');
          $terms = $query
            ->condition('t.vid', $vid)
            ->condition('r.nid', $nid)
            ->orderBy('weight')
            ->execute()
            ->fetchAll();
          foreach ($terms as $term) {
            $tags[] = '#' . check_plain($term->name);
          }
        }
      }
      $node_tags = implode(' ', $tags);
    }
    if (strpos($format, '[node-teaser]') !== FALSE) {
      if (!isset($node)) {
        $node = node_load($nid);
      }

      // Don't execute PHP -- doing so can cause WSODs.
      $filters = filter_list_format($node->format);
      if (isset($filters['php/0']) && strpos($node->teaser, '<?') !== FALSE) {
        $teaser = '';
      }
      else {
        $teaser = html_entity_decode(filter_xss(check_markup($node->teaser, $node->format, FALSE), array()), ENT_QUOTES);
      }
    }
    $url = $q;
  }
  elseif (is_string($nid)) {
    $url = $nid;
    $q = $nid;
  }
  else {
    $url = url($_GET['q'], array(
      'absolute' => TRUE,
    ));
  }
  if (module_exists('shorten')) {
    $url = shorten_url($q);
  }
  $title = _tweet_get_title($nid);
  $tweet = _tweet_process($format, array(
    '[url]' => $url,
    '[title]' => $title,
    '[node-teaser]' => $teaser,
    '[node-tags]' => check_plain($node_tags),
  ));
  $path = $site_info['path'];
  $text = _tweet_text($site);
  $image_location = $site_info['image'];
  $image = theme('image', array(
    'path' => $image_location,
    'alt' => $text,
    'title' => $text,
  ));
  if (!$type) {

    // Note that $type can be 'none', in which case nothing shows up.
    $type = variable_get('tweet_node', 'icon');
  }
  if ($type == 'icon') {
    $show = $image;
  }
  elseif ($type == 'icon_text') {
    $show = t('!tweeticon !tweettext', array(
      '!tweeticon' => $image,
      '!tweettext' => $text,
    ));
  }
  elseif ($type == 'text') {
    $show = $text;
  }
  $attributes = array(
    'class' => 'tweet',
    'rel' => 'nofollow',
  );
  if (variable_get('tweet_new_window', 'target') == 'target') {
    $attributes['target'] = '_blank';
  }
  elseif (variable_get('tweet_new_window', 'target') == 'js') {
    $attributes['onclick'] = "window.open(this.href); return false;";
  }
  return array(
    'title' => $show,
    'href' => $path,
    'attributes' => $attributes,
    'query' => array(
      $site_info['query_key'] => $tweet,
    ),
    'html' => TRUE,
  );
}