-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanonymous_subscriptions.module
231 lines (208 loc) · 7.93 KB
/
anonymous_subscriptions.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* @file
* The Anonymous Subscriptions module allows a non-registered user to your site
* the ability to register for email notifications to certain pre-defined
* content types.
*
* These notifications are optional on the pre-defined content types with a flag
* displayed as to whether to send e-mails or not.
*
* There is currently only one subscription list per site, future functionality
* will add the ability for users to subscribe per content type,
* and/or per node.
*/
use Drupal\Core\Url;
use Drupal\anonymous_subscriptions\Form\SettingsForm;
use Drupal\node\NodeInterface;
use Drupal\anonymous_subscriptions\Entity\Subscription;
/**
* Implements hook_help().
*/
function anonymous_subscriptions_help($route_name) {
switch ($route_name) {
case 'help.page.anonymous_subscriptions':
$output = '<p>';
$output .= '<p>' . t('Anonymous Subscriptions allows anonymous visitors to your site to subscribe to updates to nodes. The <a href="@anon">anonymous subscriptions administration page</a> allows you to configure options including whether emails need to be verified and which content types the option should appear to send notifications for.', [
'@anon' => Url::fromRoute('anonymous_subscriptions.settings_form'),
]);
$output .= '</p>';
return $output;
}
}
/**
* Implements hook_theme().
*/
function anonymous_subscriptions_theme() {
return [
'anonymous_subscriptions_message' => [
'variables' => [
'title' => FALSE,
'message' => FALSE,
'link' => FALSE,
],
'template' => 'anonymous-subscriptions-message',
],
];
}
/**
* Implements hook_mail()
*/
function anonymous_subscriptions_mail($key, &$message, $params) {
switch ($key) {
case 'anonymous_subscriptions_key':
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
break;
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for node_form().
*
* This is used to display the checkbox as to whether or not send the
* notification for this particular node.
*/
function anonymous_subscriptions_form_node_form_alter(&$form, $form_state, $form_id) {
$form_object = $form_state->getFormObject();
/** @var \Drupal\node\NodeInterface $node */
$node = $form_object->getEntity();
$config = \Drupal::config(SettingsForm::$configName);
$valid_types = $config->get('anonymous_subscriptions_node_types') ?: [];
if (!empty($valid_types[$node->getType()])) {
/** @var \Drupal\Core\Session\AccountInterface $current_user */
$current_user = \Drupal::service('current_user');
$form['options']['send_emails'] = [
'#type' => 'checkbox',
'#title' => t('Send e-mails to subscribers'),
'#default_value' => $config->get('anonymous_subscriptions_send_default'),
'#group' => 'options',
'#access' => $current_user->hasPermission('alter anonymous_subscriptions') || $current_user->hasPermission('administer anonymous_subscriptions'),
'#weight' => 10,
];
array_unshift($form['actions']['submit']['#submit'], 'anonymous_subscriptions_form_node_form_alter_submit');
}
}
function anonymous_subscriptions_form_node_form_alter_submit(&$form, &$form_state) {
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
// Saving send email flag to temporary private storage.
$tempStore = \Drupal::service('tempstore.private')->get('anonymous_subscriptions');
$id = $node->id() ?: 'new';
$tempStore->set('send_mail:' . $node->getType() . ':' . $id, $form_state->getValue('send_emails'));
}
/**
* Implements hook_node_insert().
*/
function anonymous_subscriptions_node_insert($node) {
// Resaving send_mail value for new nodes.
$tempStore = \Drupal::service('tempstore.private')->get('anonymous_subscriptions');
$tempStore->set('send_mail:' . $node->getType() . ':' . $node->id(), $tempStore->get('send_mail:' . $node->getType() . ':new'));
if (_anonymous_subscriptions_should_send($node)) {
_anonymous_subscriptions_add_pending_emails($node);
}
}
/**
* Implements hook_node_update().
*/
function anonymous_subscriptions_node_update($node) {
if (_anonymous_subscriptions_should_send($node)) {
_anonymous_subscriptions_add_pending_emails($node);
}
}
/**
* Function used to determine whether the email should be sent
* to the subscribers.
*
* This will check the status of the node currently and compare
* it to the original one.
*
* @param $node
* The node object that specifies to the email
*
* @return bool
* returns true or false depending if an email should be sent
*/
function _anonymous_subscriptions_should_send(NodeInterface $node) {
/** @var \Drupal\Core\TempStore\PrivateTempStore $tempStore */
$tempStore = \Drupal::service('tempstore.private')->get('anonymous_subscriptions');
$send_mail = $tempStore->get('send_mail:' . $node->getType() . ':' . $node->id());
$type = $node->getType();
$config = \Drupal::config(SettingsForm::$configName);
$valid_types = $config->get('anonymous_subscriptions_node_types') ?: [];
if (!empty($valid_types[$type])
&& $node->isPublished() &&
$send_mail == 1
) {
return TRUE;
}
return FALSE;
}
/**
* Function used to add pending emails to be sent to the database
* so they can be processed via cron at a later time.
*
* @param $node
* The node object that specifies to the email
*/
function _anonymous_subscriptions_add_pending_emails(NodeInterface $node) {
$query = \Drupal::entityQuery('anonymous_subscription');
$query->condition('verified', 1);
$group = $query->orConditionGroup()
->notExists('type')
->condition('type', $node->getType());
$query->condition($group);
$ids = $query->execute();
$subscriptions = Subscription::loadMultiple($ids);
/** @var \Drupal\Core\Utility\Token $token */
$token = \Drupal::service('token');
$queue_factory = \Drupal::service('queue');
$queue = $queue_factory->get('anonymous_subscriptions_queue');
/** @var \Drupal\anonymous_subscriptions\DefaultService $subscriberService */
$subscriberService = \Drupal::service('anonymous_subscriptions.default');
$config = \Drupal::config(SettingsForm::$configName);
$original_subject = $config->get('anonymous_subscriptions_subject_text');
$original_body = $config->get('anonymous_subscriptions_body_text');
$count = 0;
/** @var Subscription $subscription */
foreach ($subscriptions as $subscription) {
$email = $subscription->email->value;
$subject = $token->replace($original_subject, ['node' => $node]);
$body = $token->replace($original_body, ['node' => $node]);
$body .= t("\n\nTo unsubscribe please visit url @unsubscribe_url\nTo remove all your subscription visit url @unsubscribe_all_url", [
'@unsubscribe_url' => $subscriberService->getUnsubscribeUrl($subscription),
'@unsubscribe_all_url' => $subscriberService->getUnsubscribeUrl($subscription, TRUE),
]);
$fields = array(
'email' => $email,
'subject' => $subject,
'body' => $body,
'nid' => $node->id(),
);
$queue->createItem($fields);
$log_text = t("Adding pending email to :email with subject :subject for nid :nid", [
':email' => $fields['email'],
':subject' => $fields['subject'],
':nid' => $fields['nid']]);
\Drupal::logger('anonymous_subscriptions')->notice($log_text);
$count++;
}
if ($count > 0) {
$message = t('Queuing @count emails to be sent to your subscribers.', [
'@count' => $count,
]);
} else {
$message = t("No emails to be sent, there are no subscribers.");
}
\Drupal::messenger()->addMessage($message);
}
/**
* Implements hook_module_implements_alter().
*/
function anonymous_subscriptions_module_implements_alter(&$implementations, $hook) {
if ($hook == 'node_insert') {
// Move anonymous_subscriptions_node_insert() to the end of the list.
$group = $implementations['anonymous_subscriptions'];
unset($implementations['anonymous_subscriptions']);
$implementations['anonymous_subscriptions'] = $group;
}
}