social_share.module 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @file
  4. * Implements configurable social network share links to nodes
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function social_share_menu() {
  10. $items = array();
  11. $items['admin/config/content/social-share'] = array(
  12. 'title' => 'Social Share',
  13. 'description' => 'Configure share link styling.',
  14. 'page callback' => 'drupal_get_form',
  15. 'page arguments' => array('social_share_admin_settings'),
  16. 'access arguments' => array('administer site configuration'),
  17. 'type' => MENU_NORMAL_ITEM,
  18. 'file' => 'social_share.admin.inc',
  19. );
  20. return $items;
  21. }
  22. /**
  23. * Implements hook_node_view().
  24. */
  25. function social_share_node_view($node, $view_mode, $language) {
  26. // these three lines check to see if we can display the share box
  27. if (($view_mode == 'teaser' && variable_get('social_share_teaser', 1)) || ($view_mode == 'full')) {
  28. $enabledTypes = variable_get('social_share_node_types', array());
  29. if (isset($enabledTypes[$node->type]) && ($enabledTypes[$node->type])) {
  30. // get a list of enabled share links and generate them individually
  31. $sites = variable_get('social_share_sites', array());
  32. foreach ($sites as $site => $enabled) {
  33. if ($enabled) {
  34. $node->content['social_share'][] = _social_share_link($site, $node);
  35. }
  36. }
  37. // put the shareBox into the node before it's rendered
  38. $suffix = (variable_get('social_share_label') == '') ? '' : ': ';
  39. $node->content['social_share']['#weight'] = variable_get('social_share_weight', 0);
  40. $node->content['social_share']['#prefix'] = '<div class="social-share"><span>'. variable_get('social_share_label', 'Share to') . $suffix .'</span>';
  41. $node->content['social_share']['#suffix'] = '</div>';
  42. }
  43. }
  44. }
  45. function _social_share_link($site, $node) {
  46. // define the share links
  47. $sites['facebook'] = array('name' => 'Facebook', 'url' => 'http://facebook.com/sharer.php?u=%URL%&t=%TITLE%');
  48. $sites['twitter'] = array('name' => 'Twitter', 'url' => 'http://twitter.com/intent/tweet?url=%URL%&text=%TITLE%');
  49. $sites['googleplus'] = array('name' => 'Google Plus', 'url' => 'https://plus.google.com/share?url=%URL%');
  50. $sites['myspace'] = array('name' => 'Myspace', 'url' => 'http://www.myspace.com/Modules/PostTo/Pages/default.aspx?u=%URL%&c=%TITLE%');
  51. $sites['msnlive'] = array('name' => 'MSN Live', 'url' => 'http://profile.live.com/badge/?url=%URL%&title=%TITLE%&description=%DESC%');
  52. $sites['yahoo'] = array('name' => 'Yahoo', 'url' => 'http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=%URL%&t=%TITLE%&d=%DESC%');
  53. $sites['linkedin'] = array('name' => 'LinkedIn', 'url' => 'http://www.linkedin.com/shareArticle?url=%URL%&mini=true&title=%TITLE%&ro=false&summary=%DESC%&source=');
  54. $sites['orkut'] = array('name' => 'Orkut', 'url' => 'http://promote.orkut.com/preview?nt=orkut.com&tt=%TITLE%&du=%URL%&cn=%DESC%');
  55. $sites['digg'] = array('name' => 'Digg', 'url' => 'http://digg.com/share?url=%URL%&title=%TITLE%');
  56. $sites['delicious'] = array('name' => 'Delicious', 'url' => 'http://www.delicious.com/save?v=5&noui&jump=close&url=%URL%&title=%TITLE%');
  57. // get the URL for the node, using path aliases if available
  58. if (isset($node->nid)) {
  59. $url = url('node/' . $node->nid, array('absolute' => true));
  60. }
  61. elseif (isset($node->link)) {
  62. $url = $node->link;
  63. }
  64. else {
  65. $url = url($_GET['q'], array('absolute' => TRUE));
  66. }
  67. // if the shorten_urls module is installed & enabled, shorten the url being shared.
  68. if (module_exists('shorten')) {
  69. $url = shorten_url($url);
  70. }
  71. // switch out placeholders with node information
  72. $maxDescLength = variable_get('social_share_max_desc_length', 50);
  73. $target = variable_get('social_share_target', 0);
  74. $placeholders = array(
  75. '%TITLE%',
  76. '%URL%',
  77. '%DESC%'
  78. );
  79. if (isset($node->nid)) {
  80. $body = field_get_items('node', $node, 'body');
  81. if (!empty($body) && $body[0]['format'] == 'php_code') {
  82. $body = $body[0]['value'];
  83. }
  84. else {
  85. $body = (!empty($body) && isset($body[0]['safe_value'])) ? $body[0]['safe_value'] : '';
  86. }
  87. }
  88. else {
  89. $body = '';
  90. }
  91. // Trim title so it will fit in a tweet.
  92. if ($site == 'twitter' && variable_get('social_share_twitter_truncate', 0)) {
  93. if ((strlen($url) + strlen($node->title)) > 140) {
  94. $length = 136 - strlen($url);
  95. $title = substr($node->title, 0, $length);
  96. $title .= '...';
  97. }
  98. }
  99. if (!isset($title) || $title == '') {
  100. $title = $node->title;
  101. }
  102. $replacements = array(
  103. urlencode($title),
  104. $url,
  105. urlencode(strip_tags(strlen($body) > $maxDescLength ? substr($body, 0, $maxDescLength) .'...' : $body))
  106. );
  107. $options = array('attributes' => array('class' => 'social-share-'. $site));
  108. if (variable_get('social_share_new_window', 0)) {
  109. $options['attributes']['target'] = '_blank';
  110. }
  111. $link = array(
  112. '#type' => 'link',
  113. '#title' => $sites[$site]['name'],
  114. '#href' => str_replace($placeholders, $replacements, $sites[$site]['url']),
  115. '#options' => $options,
  116. '#suffix' => '&nbsp;',
  117. );
  118. // Return the link
  119. return $link;
  120. }
  121. /**
  122. * Implements hook_block_info().
  123. */
  124. function social_share_block_info() {
  125. $blocks = array();
  126. if (variable_get('social_share_block', 0)) {
  127. $blocks['social_share'] = array(
  128. 'info' => t('Social Share'),
  129. 'visibility' => 1,
  130. 'status' =>TRUE,
  131. 'region' => 'header',
  132. 'weight' => 0,
  133. 'cache' => DRUPAL_NO_CACHE,
  134. );
  135. }
  136. return $blocks;
  137. }
  138. /**
  139. * Implements hook_block_view().
  140. */
  141. function social_share_block_view($delta = '') {
  142. // This example comes from node.module. Note that you can also return a
  143. // renderable array rather than rendered HTML for 'content'.
  144. $block = array();
  145. if (user_access('access content') && variable_get('social_share_block', 0)) {
  146. $block['subject'] = t('Social Share');
  147. $sites = variable_get('social_share_sites', array());
  148. $node = (object)node_load(arg(1));
  149. if (!isset($node->nid)) {
  150. $path = isset($_GET['q']) ? $_GET['q'] : '<front>';
  151. $node->link = url($path, array('absolute' => TRUE));
  152. }
  153. if (!isset($node->title)) {
  154. $node->title = variable_get('site_name', "");
  155. }
  156. foreach ($sites as $site => $enabled) {
  157. if ($enabled) {
  158. $block['content']['social_share'][] = _social_share_link($site, $node);
  159. }
  160. }
  161. }
  162. return $block;
  163. }