You are here

twitter_post.module in Twitter 6.4

Main hooks for twitter post module

File

twitter_post/twitter_post.module
View source
<?php

/**
 * @file
 * Main hooks for twitter post module
 */

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

/**
 * Implementation of hook_perm()
 */
function twitter_post_perm() {
  return array(
    'post to twitter',
  );
}

/**
 * Implementation of hook_form_alter().
 */
function twitter_post_form_alter(&$form, $form_state, $form_id) {
  $allowed_types = variable_get('twitter_post_types', array(
    'story' => 'story',
    'blog' => 'blog',
  ));

  // Add per-content type settings to node type forms
  if ($form_id == 'node_type_form') {
    $type = $form['#node_type']->type;
    if (empty($allowed_types[$type])) {
      return;
    }
    $form['twitter'] = array(
      '#type' => 'fieldset',
      '#description' => t('Users with proper permissions will be given the option to post announcements to their Twitter accounts when they create new content. Editing the following fields will override the !twitter_admin.', array(
        '!twitter_admin' => l('site defaults', 'admin/settings/twitter'),
      )),
      '#title' => 'Twitter settings',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['twitter']['twitter_post_default_state'] = array(
      '#type' => 'radios',
      '#title' => t('Default post announcements'),
      '#maxlength' => 140,
      '#options' => array(
        'default' => t('Use site default'),
        FALSE => t('Disabled by default'),
        'all' => t('Enabled by default'),
        'create' => t('Enabled by default on create only'),
        'update' => t('Enabled by default on update only'),
      ),
      '#default_value' => variable_get('twitter_post_default_state_' . $type, 'default'),
    );
    $form['twitter']['twitter_post_default_format'] = array(
      '#type' => 'textfield',
      '#title' => t('Default format string'),
      '#maxlength' => 140,
      '#description' => t('The given text will be posted to twitter.com. You can use !url, !url-alias, !shorturl, !title and !author-name as placeholders. Leave blank to use the site default.'),
      '#default_value' => variable_get('twitter_post_default_format_' . $type, ''),
    );
    twitter_include_token_fields($form);
  }

  // Alter any node forms.
  if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id) {

    // If we haven't enabled Twitter posting on this node type, nothing to do
    // here.
    $type = $form['#node']->type;
    if (empty($allowed_types[$type])) {
      return;
    }
    module_load_include('inc', 'twitter');
    $twitter_form = twitter_post_form();
    if (!$twitter_form) {
      return;
    }
    $form['twitter'] = array(
      '#type' => 'fieldset',
      '#title' => t('Post to twitter.com'),
      '#group' => 'additional_settings',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
    );
    $type_default = variable_get('twitter_post_default_state_' . $type, 'default');
    $type_default = $type_default != 'default' ? $type_default : variable_get('twitter_post_default_state', FALSE);
    switch ($type_default) {
      case 'all':
        $post_default = TRUE;
        break;
      case 'create':
        $post_default = empty($form['nid']['#value']);
        break;
      case 'update':
        $post_default = !empty($form['nid']['#value']);
        break;
      case FALSE:
      default:
        $post_default = FALSE;
        break;
    }
    $form['twitter']['post'] = array(
      '#type' => 'checkbox',
      '#title' => t('Announce this post on Twitter'),
      '#default_value' => $post_default,
      '#id' => 'twitter-toggle',
    );
    $form['twitter'] += $twitter_form;
    $default_format = variable_get('twitter_post_default_format_' . $type, FALSE);
    $default_format = !empty($default_format) ? $default_format : variable_get('twitter_post_default_format', 'New post: !title !tinyurl');
    $form['twitter']['status']['#default_value'] = $default_format;
    $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.  If the token module is enabled, you may also choose from the tokens listed in the replacement patterns section.');
    $form['twitter']['status']['#maxlength'] = 150;
    twitter_include_token_fields($form);
  }
}

/**
 * Implementation of hook_content_extra_fields().
 */
function twitter_post_content_extra_fields($type) {
  $allowed_types = variable_get('twitter_types', array(
    'story' => 'story',
    'blog' => 'blog',
  ));
  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_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;
  }
}

/**
 * Generate a twitter posting form for the given user.
 *
 * @param $account
 *   A Drupal user object.
 */
function twitter_post_form($account = NULL) {
  drupal_add_js(drupal_get_path('module', 'twitter_post') . '/twitter_post.js', 'module');
  if (empty($account)) {
    global $user;
    $account = $user;
  }
  if (!user_access('post to twitter', $account)) {
    return;
  }
  $twitter_accounts = twitter_get_user_accounts($account->uid);
  $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),
      );
    }
    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_nodeapi Implementation of hook_nodeapi().
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 Implementation of hook_form_alter().
twitter_post_menu Implementation of hook_menu().
twitter_post_perm Implementation of hook_perm()