twitter_post.module 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @file
  4. * Main hooks for twitter post module
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function twitter_post_menu() {
  10. $items['admin/config/services/twitter/post'] = array(
  11. 'title' => 'Post',
  12. 'page callback' => 'drupal_get_form',
  13. 'page arguments' => array('twitter_post_admin_settings'),
  14. 'access arguments' => array('administer site configuration'),
  15. 'file' => 'twitter_post.pages.inc',
  16. 'type' => MENU_LOCAL_TASK,
  17. 'weight' => 3,
  18. );
  19. return $items;
  20. }
  21. /**
  22. * Implements hook_permission().
  23. */
  24. function twitter_post_permission() {
  25. return array(
  26. 'post to twitter' => array(
  27. 'title' => t('Post a message to Twitter'),
  28. ),
  29. );
  30. }
  31. /**
  32. * Implements hook_form_alter().
  33. */
  34. function twitter_post_form_alter(&$form, $form_state, $form_id) {
  35. // Alter any node forms.
  36. if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id) {
  37. // If we haven't enabled Twitter posting on this node type, nothing to do
  38. // here.
  39. $type = $form['#node']->type;
  40. $allowed_types = variable_get('twitter_post_types', array('story' => 'story', 'blog' => 'blog'));
  41. if (empty($allowed_types[$type])) {
  42. return;
  43. }
  44. module_load_include('inc', 'twitter');
  45. $twitter_form = twitter_post_form();
  46. if (!$twitter_form) {
  47. return;
  48. }
  49. $form['twitter'] = array(
  50. '#type' => 'fieldset',
  51. '#title' => t('Post to twitter.com'),
  52. '#collapsible' => TRUE,
  53. '#collapsed' => FALSE,
  54. '#tree' => TRUE,
  55. );
  56. $form['twitter']['post'] = array(
  57. '#type' => 'checkbox',
  58. '#title' => t('Announce this post on Twitter'),
  59. '#default_value' => (empty($form['nid']['#value'])),
  60. '#id' => 'twitter-toggle',
  61. );
  62. $form['twitter'] += $twitter_form;
  63. $form['twitter']['status']['#default_value'] = variable_get('twitter_post_default_format', 'New post: !title !tinyurl');
  64. $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.');
  65. $form['twitter']['status']['#maxlength'] = 150;
  66. }
  67. }
  68. /**
  69. * Implementation of hook_node_insert().
  70. *
  71. * Intercepts newly published nodes and posts noticed to Twitter.
  72. */
  73. function twitter_post_node_insert($node) {
  74. if (!empty($node->status) && !empty($node->twitter) && !empty($node->twitter['post'])) {
  75. module_load_include('inc', 'twitter');
  76. $twitter_account = twitter_account_load($node->twitter['account']);
  77. $replacements = array(
  78. '!title' => $node->title,
  79. '!url' => url('node/' . $node->nid, array('absolute' => TRUE, 'alias' => TRUE)),
  80. '!url-alias' => url('node/' . $node->nid, array('absolute' => TRUE)),
  81. '!user' => $node->name,
  82. );
  83. // Only generate the shortened URL if it's going to be used. No sense
  84. // burning through TinyURLs without a good reason.
  85. if (strstr($node->twitter['status'], '!tinyurl') !== FALSE) {
  86. $replacements['!tinyurl'] = twitter_shorten_url(url('node/' . $node->nid, array('absolute' => TRUE)));
  87. }
  88. $status = strtr($node->twitter['status'], $replacements);
  89. try {
  90. $result = twitter_set_status($twitter_account, $status);
  91. drupal_set_message(t('Successfully posted to Twitter'));
  92. }
  93. catch (TwitterException $e) {
  94. drupal_set_message(t('An error occurred when posting to twitter: %code %error',
  95. array('%code' => $result->code, '%error' => $result->error)), 'warning');
  96. }
  97. }
  98. }
  99. /**
  100. * Implementation of hook_node_update().
  101. */
  102. function twitter_post_node_update($node) {
  103. twitter_post_node_insert($node);
  104. }
  105. /**
  106. * Generate a twitter posting form for the given user.
  107. *
  108. * @param $account
  109. * A Drupal user object.
  110. */
  111. function twitter_post_form($account = NULL) {
  112. drupal_add_js(drupal_get_path('module', 'twitter_post') . '/twitter_post.js');
  113. if (empty($account)) {
  114. $account = user_load($GLOBALS['user']->uid);
  115. }
  116. if (!user_access('post to twitter', $account)) {
  117. return;
  118. }
  119. $options = array();
  120. foreach ($account->twitter_accounts as $twitter_account) {
  121. $options[$twitter_account->id] = $twitter_account->screen_name;
  122. }
  123. if (count($options)) {
  124. $form = array();
  125. $form['status'] = array(
  126. '#type' => 'textfield',
  127. '#id' => 'twitter-textfield',
  128. );
  129. if (count($options) > 1) {
  130. $form['account'] = array(
  131. '#type' => 'select',
  132. '#title' => t('Account'),
  133. '#options' => $options,
  134. '#id' => 'twitter-account',
  135. );
  136. }
  137. else {
  138. $form['account'] = array(
  139. '#type' => 'value',
  140. '#value' => array_pop(array_keys($options)),
  141. );
  142. }
  143. return $form;
  144. }
  145. }