You are here

twitter_post.module in Twitter 6.5

Hook implementations for twitter_post module.

File

twitter_post/twitter_post.module
View source
<?php

/**
 * @file
 * Hook implementations for twitter_post module.
 */

/**
 * Implements hook_menu().
 */
function twitter_post_menu() {
  $items = array();
  $items['admin/settings/twitter/post'] = array(
    'title' => 'Post',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'twitter_post_admin_settings',
    ),
    'access arguments' => array(
      'administer twitter',
    ),
    'file' => 'twitter_post.pages.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 3,
  );
  return $items;
}

/**
 * Implements hook_perm().
 */
function twitter_post_perm() {
  return array(
    // This permission is limited to only allowing the user to post using
    // authenticated accounts that they own, they may not post using a "global"
    // account.
    'post to twitter',
    // Ordinarily users are only able to post using their own authenticated
    // accounts. This option allows users to also post using someone else's
    // account that is marked as being "global". This permission must be given
    // in addition to 'Post a message to Twitter'.
    'post to twitter with global account',
  );
}

/**
 * Implements hook_form_alter().
 */
function twitter_post_form_alter(&$form, $form_state, $form_id) {

  // If we haven't enabled Twitter posting on this node type, nothing to do here.
  $allowed_types = variable_get('twitter_post_types', array());
  if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id) {
    $node = $form['#node'];
    $allowed_types = variable_get('twitter_post_types', array());
    if (empty($allowed_types[$node->type])) {
      return;
    }
    module_load_include('inc', 'twitter');
    $twitter_form = twitter_post_form();
    if (!$twitter_form) {
      return;
    }
    $form['twitter'] = array(
      '#type' => 'fieldset',
      '#group' => 'additional_settings',
      '#title' => t('Post to Twitter.com'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
    );

    // Load the default values; use the values submitted by the form if this is
    // a node preview.
    $enabled = variable_get('twitter_post_default_value', 0);
    if (!empty($node->nid) && !empty($node->status)) {
      $enabled = variable_get('twitter_post_on_update', 0);
    }
    if (isset($form_state['values']['twitter']['post'])) {
      $enabled = $form_state['values']['twitter']['post'];
    }
    $status = variable_get('twitter_post_default_format', 'New post: !title !url-alias');
    if (isset($form_state['values']['twitter']['status'])) {
      $status = $form_state['values']['twitter']['status'];
    }
    $form['twitter']['post'] = array(
      '#type' => 'checkbox',
      '#title' => t('Announce this post on Twitter'),
      '#default_value' => $enabled,
      '#id' => 'twitter-toggle',
    );
    $form['twitter'] += $twitter_form;
    $form['twitter']['status']['#default_value'] = $status;
    $form['twitter']['status']['#description'] = t('The given text will be posted to twitter.com. You can use !url, !url-alias, !tinyurl, !title and !user as replacement text. Note that if a URL is being included, the maximum length of the rest of the tweet is 117 characters.');
    $form['twitter']['status']['#maxlength'] = 150;
    twitter_include_token_fields($form);
  }
}

/**
 * Implementation of hook_content_extra_fields().
 *
 * Let's the Twitter field be positioned within the edit node form.
 */
function twitter_post_content_extra_fields($type) {
  $allowed_types = variable_get('twitter_post_types', array());
  if (empty($allowed_types[$type])) {
    return;
  }
  $extras['twitter'] = array(
    'label' => t('Twitter'),
    'description' => t('Form to send new content to twitter'),
    'weight' => 100,
  );
  return $extras;
}

/**
 * Implementation of hook_nodeapi().
 *
 * Intercepts newly published nodes and posts noticed to Twitter.
 */
function twitter_post_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']);
        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,
          )));
        }
        $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 "%node" to Twitter: <a href="@status" target="_blank">@status</a>', array(
            '@status' => _twitter_status_url($status),
            '%node' => $node->title,
          )));
        }
        else {
          drupal_set_message(t('Could not post to Twitter. Please check the site reports.'));
        }

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

/**
 * Generate a twitter posting form for the given user.
 *
 * @param $account
 *   A Drupal user object.
 */
function twitter_post_form($account = NULL) {
  if (empty($account)) {
    global $user;
    $account = user_load($user->uid);
  }
  if (!user_access('post to twitter', $account)) {
    return;
  }
  drupal_add_js(drupal_get_path('module', 'twitter_post') . '/twitter_post.js');
  $access_global = user_access('post to twitter with global account', $account);
  $twitter_accounts = twitter_load_authenticated_accounts($account->uid, $access_global);

  // Only show Twitter accounts that are either global or are tied to this user.
  $options = array();
  foreach ($twitter_accounts as $twitter_account) {
    $options[$twitter_account->id] = $twitter_account->screen_name;
  }
  if (count($options)) {
    $form = array();
    $form['status'] = array(
      '#type' => 'textfield',
      '#id' => 'twitter-textfield',
    );
    if (count($options) > 1) {
      $form['account'] = array(
        '#type' => 'select',
        '#title' => t('Account'),
        '#options' => $options,
        '#id' => 'twitter-account',
      );
    }
    else {
      $options_keys = array_keys($options);
      $form['account'] = array(
        '#type' => 'value',
        '#value' => array_pop($options_keys),
        '#id' => 'twitter-account',
      );
    }
    return $form;
  }
}

/**
 * Add token help fields to the passed form.
 *
 * @param $form
 *   A Drupal Forms API form.
 */
function twitter_include_token_fields(&$form) {
  if (module_exists('token')) {
    $form['twitter']['token_help'] = array(
      '#title' => t('Replacement patterns'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
    );
    $form['twitter']['token_help']['help'] = array(
      '#value' => theme('token_help', 'node'),
    );
  }
}

Functions

Namesort descending Description
twitter_include_token_fields Add token help fields to the passed form.
twitter_post_content_extra_fields Implementation of hook_content_extra_fields().
twitter_post_form Generate a twitter posting form for the given user.
twitter_post_form_alter Implements hook_form_alter().
twitter_post_menu Implements hook_menu().
twitter_post_nodeapi Implementation of hook_nodeapi().
twitter_post_perm Implements hook_perm().