simplenews.tokens.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @file
  4. * Token related hook implementations.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function simplenews_token_info() {
  10. $types['simplenews-subscriber'] = array(
  11. 'name' => t('Simplenews subscriber'),
  12. 'description' => t('Tokens related to the newsletter recipient'),
  13. 'needs-data' => 'simplenews_subscriber',
  14. );
  15. $types['simplenews-category'] = array(
  16. 'name' => t('Simplenews newsletter category'),
  17. 'description' => t('Tokens related to the newsletter category'),
  18. 'needs-data' => 'category',
  19. );
  20. // Tokens for simplenews subscriber.
  21. $subscriber['subscribe-url'] = array(
  22. 'name' => t('Subscription URL'),
  23. 'description' => t('The URL of the page where the subscription is confirmed.'),
  24. );
  25. $subscriber['unsubscribe-url'] = array(
  26. 'name' => t('Unsubscribe URL'),
  27. 'description' => t('The URL of the page where the cancellation of the subscription is confirmed.'),
  28. );
  29. $subscriber['manage-url'] = array(
  30. 'name' => t('Manage URL'),
  31. 'description' => t('The URL of the page where the subscribers can manage their newsletter subscriptions.'),
  32. );
  33. $subscriber['combined-url'] = array(
  34. 'name' => t('Combined confirmation URL'),
  35. 'description' => t('The URL of the page where subscribers can confirm their subscription changes.'),
  36. );
  37. $subscriber['mail'] = array(
  38. 'name' => t('Subscriber email'),
  39. 'description' => t('The email address of the newsletter receiver.'),
  40. );
  41. $subscriber['user'] = array(
  42. 'name' => t('Corresponding user'),
  43. 'description' => t('The user object that corresponds to this subscriber. This is not set for anonymous subscribers.'),
  44. 'type' => 'user',
  45. );
  46. // Tokens for simplenews newsletter category.
  47. $category['name'] = array(
  48. 'name' => t('Newsletter category'),
  49. 'description' => t('The name of the newsletter category.'),
  50. );
  51. $category['url'] = array(
  52. 'name' => t('Newsletter category URL'),
  53. 'description' => t('The URL of the page listing the issues of this newsletter category.'),
  54. );
  55. $category['term'] = array(
  56. 'name' => t('Corresponding term'),
  57. 'description' => t('The taxonomy term of this newsletter category'),
  58. 'type' => 'term',
  59. );
  60. return array(
  61. 'types' => $types,
  62. 'tokens' => array(
  63. 'simplenews-subscriber' => $subscriber,
  64. 'simplenews-category' => $category,
  65. ),
  66. );
  67. }
  68. /**
  69. * Implements hook_tokens().
  70. *
  71. */
  72. function simplenews_tokens($type, $tokens, $data = array(), $options = array()) {
  73. $replacements = array();
  74. $sanitize = !empty($options['sanitize']);
  75. if (isset($options['language'])) {
  76. $url_options['language'] = $options['language'];
  77. $language_code = $options['language']->language;
  78. }
  79. else {
  80. $language_code = NULL;
  81. }
  82. switch ($type) {
  83. case 'simplenews-subscriber':
  84. if (!isset($data['simplenews_subscriber'])) {
  85. return;
  86. }
  87. $subscriber = $data['simplenews_subscriber'];
  88. $category = isset($data['category']) ? $data['category'] : NULL;
  89. $language = isset($subscriber->language->language) ? $subscriber->language : user_preferred_language($subscriber);
  90. // Build hash for the URL of the (un)subscribe confirmation page.
  91. $hash = '';
  92. if (isset($subscriber->snid) && isset($category->tid)) {
  93. $hash = simplenews_generate_hash($subscriber->mail, $subscriber->snid, $category->tid);
  94. }
  95. elseif (isset($subscriber->snid)) {
  96. $combined_hash = simplenews_generate_hash($subscriber->mail, $subscriber->snid, 0);
  97. }
  98. foreach ($tokens as $name => $original) {
  99. switch ($name) {
  100. // Simple key values on the node.
  101. case 'subscribe-url':
  102. $replacements[$original] = url('newsletter/confirm/add/' . $hash, array('absolute' => TRUE, 'language' => $language));
  103. break;
  104. case 'unsubscribe-url':
  105. $replacements[$original] = url('newsletter/confirm/remove/' . $hash, array('absolute' => TRUE, 'language' => $language));
  106. break;
  107. case 'combined-url':
  108. $replacements[$original] = url('newsletter/confirm/combined/' . $combined_hash, array('absolute' => TRUE, 'language' => $language));
  109. break;
  110. case 'manage-url':
  111. $replacements[$original] = url('newsletter/subscriptions/' . $hash, array('absolute' => TRUE, 'language' => $language));
  112. break;
  113. case 'mail':
  114. $replacements[$original] = $sanitize ? check_plain($subscriber->mail) : $subscriber->mail;
  115. break;
  116. }
  117. }
  118. if (($user_tokens = token_find_with_prefix($tokens, 'user')) && !empty($subscriber->uid)) {
  119. $replacements += token_generate('user', $user_tokens, array('user' => user_load($subscriber->uid)), $options);
  120. }
  121. break;
  122. case 'simplenews-category':
  123. if (!isset($data['category'])) {
  124. return;
  125. }
  126. $category = $data['category'];
  127. foreach ($tokens as $name => $original) {
  128. switch ($name) {
  129. case 'name':
  130. if (isset($category->name)) {
  131. $newsletter_name = _simplenews_newsletter_name($category, $language_code);
  132. $replacements[$original] = $sanitize ? check_plain($newsletter_name) : $newsletter_name;
  133. }
  134. else {
  135. $replacements[$original] = t('Unassigned newsletter');
  136. }
  137. break;
  138. case 'url':
  139. $uri = entity_uri('taxonomy_term', $category);
  140. $replacements[$original] = url($uri['path'], $uri['options']);
  141. break;
  142. }
  143. }
  144. if ($term_tokens = token_find_with_prefix($tokens, 'term')) {
  145. $replacements += token_generate('taxonomy_term', $term_tokens, array('taxonomy_term' => $category), $options);
  146. }
  147. break;
  148. }
  149. return $replacements;
  150. }