1
|
|
|
<?php |
2
|
|
|
use \EventEspresso\core\exceptions\SendMessageException; |
3
|
|
|
|
4
|
|
|
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
5
|
|
|
exit( 'No direct script access allowed' ); |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This class is used for managing and interacting with the EE_messages Queue. An instance |
10
|
|
|
* of this object is used for interacting with a specific batch of EE_Message objects. |
11
|
|
|
* |
12
|
|
|
* @package Event Espresso |
13
|
|
|
* @subpackage messages |
14
|
|
|
* @author Darren Ethier |
15
|
|
|
* @since 4.9.0 |
16
|
|
|
*/ |
17
|
|
|
class EE_Messages_Queue { |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @type string reference for sending action |
22
|
|
|
*/ |
23
|
|
|
const action_sending = 'sending'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @type string reference for generation action |
27
|
|
|
*/ |
28
|
|
|
const action_generating = 'generation'; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @type EE_Message_Repository $_message_repository |
34
|
|
|
*/ |
35
|
|
|
protected $_message_repository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sets the limit of how many messages are generated per process. |
39
|
|
|
* @type int |
40
|
|
|
*/ |
41
|
|
|
protected $_batch_count; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Sets the limit of how many messages can be sent per hour. |
45
|
|
|
* @type int |
46
|
|
|
*/ |
47
|
|
|
protected $_rate_limit; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* This is an array of cached queue items being stored in this object. |
51
|
|
|
* The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
52
|
|
|
* is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
53
|
|
|
* @type EE_Message[] |
54
|
|
|
*/ |
55
|
|
|
protected $_cached_queue_items; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Tracks the number of unsaved queue items. |
59
|
|
|
* @type int |
60
|
|
|
*/ |
61
|
|
|
protected $_unsaved_count = 0; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
65
|
|
|
* hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
66
|
|
|
* |
67
|
|
|
* @type array |
68
|
|
|
*/ |
69
|
|
|
protected $_did_hook = array(); |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constructor. |
75
|
|
|
* Setup all the initial properties and load a EE_Message_Repository. |
76
|
|
|
* |
77
|
|
|
* @param \EE_Message_Repository $message_repository |
78
|
|
|
*/ |
79
|
|
|
public function __construct( EE_Message_Repository $message_repository ) { |
80
|
|
|
$this->_batch_count = apply_filters( 'FHEE__EE_Messages_Queue___batch_count', 50 ); |
81
|
|
|
$this->_rate_limit = $this->get_rate_limit(); |
82
|
|
|
$this->_message_repository = $message_repository; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Add a EE_Message object to the queue |
89
|
|
|
* |
90
|
|
|
* @param EE_Message $message |
91
|
|
|
* @param array $data This will be an array of data to attach to the object in the repository. If the |
92
|
|
|
* object is persisted, this data will be saved on an extra_meta object related to |
93
|
|
|
* EE_Message. |
94
|
|
|
* @param bool $preview Whether this EE_Message represents a preview or not. |
95
|
|
|
* @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
96
|
|
|
* use the messenger send method but typically is based on preview data. |
97
|
|
|
* @return bool Whether the message was successfully added to the repository or not. |
98
|
|
|
*/ |
99
|
|
|
public function add( EE_Message $message, $data = array(), $preview = false, $test_send = false ) { |
100
|
|
|
$data['preview'] = $preview; |
101
|
|
|
$data['test_send'] = $test_send; |
102
|
|
|
return $this->_message_repository->add( $message, $data ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
110
|
|
|
* @param EE_Message $message The message to detach from the queue |
111
|
|
|
* @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function remove( EE_Message $message, $persist = false ) { |
115
|
|
|
if ( $persist && $this->_message_repository->current() !== $message ) { |
116
|
|
|
//get pointer on right message |
117
|
|
|
if ( $this->_message_repository->has( $message ) ) { |
118
|
|
|
$this->_message_repository->rewind(); |
119
|
|
|
while( $this->_message_repository->valid() ) { |
120
|
|
|
if ( $this->_message_repository->current() === $message ) { |
121
|
|
|
break; |
122
|
|
|
} |
123
|
|
|
$this->_message_repository->next(); |
124
|
|
|
} |
125
|
|
|
} else { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
return $persist ? $this->_message_repository->delete() : $this->_message_repository->remove( $message ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Persists all queued EE_Message objects to the db. |
137
|
|
|
* @return array() @see EE_Messages_Repository::saveAll() for return values. |
|
|
|
|
138
|
|
|
*/ |
139
|
|
|
public function save() { |
140
|
|
|
return $this->_message_repository->saveAll(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
|
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return EE_Message_Repository |
149
|
|
|
*/ |
150
|
|
|
public function get_message_repository() { |
151
|
|
|
return $this->_message_repository; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* This does the following things: |
159
|
|
|
* 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return false). |
160
|
|
|
* 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
161
|
|
|
* 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
162
|
|
|
* |
163
|
|
|
* Note: Callers should make sure they release the lock otherwise batch generation will be prevented from continuing. |
164
|
|
|
* The lock is on a transient that is set to expire after one hour as a fallback in case locks are not removed. |
165
|
|
|
* |
166
|
|
|
* @return bool true if successfully retrieved batch, false no batch ready. |
167
|
|
|
*/ |
168
|
|
|
public function get_batch_to_generate() { |
169
|
|
|
if ( $this->is_locked( EE_Messages_Queue::action_generating ) ) { |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
//lock batch generation to prevent race conditions. |
174
|
|
|
$this->lock_queue( EE_Messages_Queue::action_generating ); |
175
|
|
|
|
176
|
|
|
$query_args = array( |
177
|
|
|
// key 0 = where conditions |
178
|
|
|
0 => array( 'STS_ID' => EEM_Message::status_incomplete ), |
179
|
|
|
'order_by' => $this->_get_priority_orderby(), |
180
|
|
|
'limit' => $this->_batch_count |
181
|
|
|
); |
182
|
|
|
$messages = EEM_Message::instance()->get_all( $query_args ); |
183
|
|
|
|
184
|
|
|
if ( ! $messages ) { |
|
|
|
|
185
|
|
|
return false; //nothing to generate |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
foreach ( $messages as $message ) { |
189
|
|
|
if ( $message instanceof EE_Message ) { |
190
|
|
|
$data = $message->all_extra_meta_array(); |
191
|
|
|
$this->add( $message, $data ); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* This does the following things: |
200
|
|
|
* 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return false). |
201
|
|
|
* 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then return false. |
202
|
|
|
* 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
203
|
|
|
* 3. On success or unsuccessful send, sets status appropriately. |
204
|
|
|
* 4. Saves messages via the queue |
205
|
|
|
* 5. Releases lock. |
206
|
|
|
* |
207
|
|
|
* @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not necessarily |
208
|
|
|
* mean that all messages were successfully sent. It just means that this method successfully completed. |
209
|
|
|
* On true, client may want to call $this->count_STS_in_queue( EEM_Message::status_failed ) to see if |
210
|
|
|
* any failed EE_Message objects. Each failed message object will also have a saved error message on it |
211
|
|
|
* to assist with notifying user. |
212
|
|
|
*/ |
213
|
|
|
public function get_to_send_batch_and_send() { |
214
|
|
|
if ( $this->is_locked( EE_Messages_Queue::action_sending ) || $this->_rate_limit < 1 ) { |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$this->lock_queue( EE_Messages_Queue::action_sending ); |
219
|
|
|
|
220
|
|
|
$batch = $this->_batch_count < $this->_rate_limit ? $this->_batch_count : $this->_rate_limit; |
221
|
|
|
|
222
|
|
|
$query_args = array( |
223
|
|
|
// key 0 = where conditions |
224
|
|
|
0 => array( 'STS_ID' => array( 'IN', EEM_Message::instance()->stati_indicating_to_send() ) ), |
225
|
|
|
'order_by' => $this->_get_priority_orderby(), |
226
|
|
|
'limit' => $batch |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
$messages_to_send = EEM_Message::instance()->get_all( $query_args ); |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
//any to send? |
233
|
|
|
if ( ! $messages_to_send ) { |
|
|
|
|
234
|
|
|
$this->unlock_queue( EE_Messages_Queue::action_sending ); |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
//add to queue. |
239
|
|
|
foreach ( $messages_to_send as $message ) { |
240
|
|
|
if ( $message instanceof EE_Message ) { |
241
|
|
|
$this->add( $message ); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
//send messages (this also updates the rate limit) |
246
|
|
|
$this->execute(); |
247
|
|
|
|
248
|
|
|
//release lock |
249
|
|
|
$this->unlock_queue( EE_Messages_Queue::action_sending ); |
250
|
|
|
return true; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
|
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Locks the queue so that no other queues can call the "batch" methods. |
258
|
|
|
* |
259
|
|
|
* @param string $type The type of queue being locked. |
260
|
|
|
*/ |
261
|
|
|
public function lock_queue( $type = EE_Messages_Queue::action_generating ) { |
262
|
|
|
set_transient( $this->_get_lock_key( $type ), 1, $this->_get_lock_expiry( $type ) ); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Unlocks the queue so that batch methods can be used. |
270
|
|
|
* |
271
|
|
|
* @param string $type The type of queue being unlocked. |
272
|
|
|
*/ |
273
|
|
|
public function unlock_queue( $type = EE_Messages_Queue::action_generating ) { |
274
|
|
|
delete_transient( $this->_get_lock_key( $type ) ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
|
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Retrieve the key used for the lock transient. |
282
|
|
|
* @param string $type The type of lock. |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
protected function _get_lock_key( $type = EE_Messages_Queue::action_generating ) { |
286
|
|
|
return '_ee_lock_' . $type; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
|
290
|
|
|
|
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Retrieve the expiry time for the lock transient. |
294
|
|
|
* @param string $type The type of lock |
295
|
|
|
* @return int time to expiry in seconds. |
296
|
|
|
*/ |
297
|
|
|
protected function _get_lock_expiry( $type = EE_Messages_Queue::action_generating ) { |
298
|
|
|
return (int) apply_filters( 'FHEE__EE_Messages_Queue__lock_expiry', HOUR_IN_SECONDS, $type ); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns the key used for rate limit transient. |
304
|
|
|
* @return string |
305
|
|
|
*/ |
306
|
|
|
protected function _get_rate_limit_key() { |
307
|
|
|
return '_ee_rate_limit'; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Returns the rate limit expiry time. |
313
|
|
|
* @return int |
314
|
|
|
*/ |
315
|
|
|
protected function _get_rate_limit_expiry() { |
316
|
|
|
return (int) apply_filters( 'FHEE__EE_Messages_Queue__rate_limit_expiry', HOUR_IN_SECONDS ); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
|
320
|
|
|
|
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Returns the default rate limit for sending messages. |
324
|
|
|
* @return int |
325
|
|
|
*/ |
326
|
|
|
protected function _default_rate_limit() { |
327
|
|
|
return (int) apply_filters( 'FHEE__EE_Messages_Queue___rate_limit', 200 ); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
|
331
|
|
|
|
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Return the orderby array for priority. |
335
|
|
|
* @return array |
336
|
|
|
*/ |
337
|
|
|
protected function _get_priority_orderby() { |
338
|
|
|
return array( |
339
|
|
|
'MSG_priority' => 'ASC', |
340
|
|
|
'MSG_modified' => 'DESC' |
341
|
|
|
); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
|
345
|
|
|
|
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Returns whether batch methods are "locked" or not. |
349
|
|
|
* |
350
|
|
|
* @param string $type The type of lock being checked for. |
351
|
|
|
* @return bool |
352
|
|
|
*/ |
353
|
|
|
public function is_locked( $type = EE_Messages_Queue::action_generating ) { |
354
|
|
|
/** |
355
|
|
|
* This filters the default is_locked behaviour. |
356
|
|
|
*/ |
357
|
|
|
$is_locked = filter_var( |
358
|
|
|
apply_filters( |
359
|
|
|
'FHEE__EE_Messages_Queue__is_locked', |
360
|
|
|
get_transient( $this->_get_lock_key( $type ) ), |
361
|
|
|
$this |
362
|
|
|
), |
363
|
|
|
FILTER_VALIDATE_BOOLEAN |
364
|
|
|
); |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @see usage of this filter in EE_Messages_Queue::initiate_request_by_priority() method. |
368
|
|
|
* Also implemented here because messages processed on the same request should not have any locks applied. |
369
|
|
|
*/ |
370
|
|
|
if ( |
371
|
|
|
apply_filters( 'FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false ) |
372
|
|
|
|| EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
373
|
|
|
) { |
374
|
|
|
$is_locked = false; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
|
378
|
|
|
return $is_locked; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
|
382
|
|
|
|
383
|
|
|
|
384
|
|
|
|
385
|
|
|
|
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Retrieves the rate limit that may be cached as a transient. |
389
|
|
|
* If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
390
|
|
|
* @return int |
391
|
|
|
*/ |
392
|
|
|
public function get_rate_limit() { |
393
|
|
|
if ( ! $rate_limit = get_transient( $this->_get_rate_limit_key() ) ) { |
394
|
|
|
$rate_limit = $this->_default_rate_limit(); |
395
|
|
|
set_transient( $this->_get_rate_limit_key(), $rate_limit, $this->_get_rate_limit_key() ); |
396
|
|
|
} |
397
|
|
|
return $rate_limit; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
|
401
|
|
|
|
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* This updates existing rate limit with the new limit which is the old minus the batch. |
405
|
|
|
* @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
406
|
|
|
*/ |
407
|
|
|
public function set_rate_limit( $batch_completed ) { |
408
|
|
|
//first get the most up to date rate limit (in case its expired and reset) |
409
|
|
|
$rate_limit = $this->get_rate_limit(); |
410
|
|
|
$new_limit = $rate_limit - $batch_completed; |
411
|
|
|
//updating the transient option directly to avoid resetting the expiry. |
412
|
|
|
update_option( '_transient_' . $this->_get_rate_limit_key(), $new_limit ); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
418
|
|
|
* If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
419
|
|
|
* |
420
|
|
|
* Note: Keep in mind that there is the possibility that the request will not execute if there is already another request |
421
|
|
|
* running on a queue for the given task. |
422
|
|
|
* @param string $task This indicates what type of request is going to be initiated. |
423
|
|
|
* @param int $priority This indicates the priority that triggers initiating the request. |
424
|
|
|
*/ |
425
|
|
|
public function initiate_request_by_priority( $task = 'generate', $priority = EEM_Message::priority_high ) { |
426
|
|
|
//determine what status is matched with the priority as part of the trigger conditions. |
427
|
|
|
$status = $task == 'generate' |
428
|
|
|
? EEM_Message::status_incomplete |
429
|
|
|
: EEM_Message::instance()->stati_indicating_to_send(); |
430
|
|
|
// always make sure we save because either this will get executed immediately on a separate request |
431
|
|
|
// or remains in the queue for the regularly scheduled queue batch. |
432
|
|
|
$this->save(); |
433
|
|
|
/** |
434
|
|
|
* This filter/option allows users to override processing of messages on separate requests and instead have everything |
435
|
|
|
* happen on the same request. If this is utilized remember: |
436
|
|
|
* |
437
|
|
|
* - message priorities don't matter |
438
|
|
|
* - existing unprocessed messages in the queue will not get processed unless manually triggered. |
439
|
|
|
* - things will be perceived to take longer to happen for end users (i.e. registrations) because of the additional |
440
|
|
|
* processing happening on the same request. |
441
|
|
|
* - any race condition protection (locks) are removed because they don't apply when things are processed on |
442
|
|
|
* the same request. |
443
|
|
|
*/ |
444
|
|
|
if ( |
445
|
|
|
apply_filters( 'FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false ) |
446
|
|
|
|| EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
447
|
|
|
) { |
448
|
|
|
$messages_processor = EE_Registry::instance()->load_lib( 'Messages_Processor' ); |
449
|
|
|
if ( $messages_processor instanceof EE_Messages_Processor ) { |
450
|
|
|
return $messages_processor->process_immediately_from_queue( $this ); |
451
|
|
|
} |
452
|
|
|
//if we get here then that means the messages processor couldn't be loaded so messages will just remain |
453
|
|
|
//queued for manual triggering by end user. |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
if ( $this->_message_repository->count_by_priority_and_status( $priority, $status ) ) { |
457
|
|
|
EE_Messages_Scheduler::initiate_scheduled_non_blocking_request( $task ); |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
|
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
465
|
|
|
* |
466
|
|
|
* @param bool $save Used to indicate whether to save the message queue after sending |
467
|
|
|
* (default will save). |
468
|
|
|
* @param mixed $sending_messenger (optional) When the sending messenger is different than |
469
|
|
|
* what is on the EE_Message object in the queue. |
470
|
|
|
* For instance, showing the browser view of an email message, |
471
|
|
|
* or giving a pdf generated view of an html document. |
472
|
|
|
* This should be an instance of EE_messenger but if you call this method |
473
|
|
|
* intending it to be a sending messenger but a valid one could not be retrieved |
474
|
|
|
* then send in an instance of EE_Error that contains the related error message. |
475
|
|
|
* @param bool|int $by_priority When set, this indicates that only messages |
476
|
|
|
* matching the given priority should be executed. |
477
|
|
|
* |
478
|
|
|
* @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
479
|
|
|
* Also, if the messenger is an request type messenger (or a preview), |
480
|
|
|
* its entirely possible that the messenger will exit before |
481
|
|
|
*/ |
482
|
|
|
public function execute( $save = true, $sending_messenger = null, $by_priority = false ) { |
483
|
|
|
$messages_sent = 0; |
484
|
|
|
$this->_did_hook = array(); |
485
|
|
|
$this->_message_repository->rewind(); |
486
|
|
|
|
487
|
|
|
while ( $this->_message_repository->valid() ) { |
488
|
|
|
$error_messages = array(); |
489
|
|
|
/** @type EE_Message $message */ |
490
|
|
|
$message = $this->_message_repository->current(); |
491
|
|
|
//only process things that are queued for sending |
492
|
|
|
if ( ! in_array( $message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send() ) ) { |
493
|
|
|
$this->_message_repository->next(); |
494
|
|
|
continue; |
495
|
|
|
} |
496
|
|
|
//if $by_priority is set and does not match then continue; |
497
|
|
|
if ( $by_priority && $by_priority != $message->priority() ) { |
498
|
|
|
$this->_message_repository->next(); |
499
|
|
|
continue; |
500
|
|
|
} |
501
|
|
|
//error checking |
502
|
|
|
if ( ! $message->valid_messenger() ) { |
503
|
|
|
$error_messages[] = sprintf( |
504
|
|
|
__( 'The %s messenger is not active at time of sending.', 'event_espresso' ), |
505
|
|
|
$message->messenger() |
506
|
|
|
); |
507
|
|
|
} |
508
|
|
|
if ( ! $message->valid_message_type() ) { |
509
|
|
|
$error_messages[] = sprintf( |
510
|
|
|
__( 'The %s message type is not active at the time of sending.', 'event_espresso' ), |
511
|
|
|
$message->message_type() |
512
|
|
|
); |
513
|
|
|
} |
514
|
|
|
// if there was supposed to be a sending messenger for this message, but it was invalid/inactive, |
515
|
|
|
// then it will instead be an EE_Error object, so let's check for that |
516
|
|
|
if ( $sending_messenger instanceof EE_Error ) { |
517
|
|
|
$error_messages[] = $sending_messenger->getMessage(); |
518
|
|
|
} |
519
|
|
|
// if there are no errors, then let's process the message |
520
|
|
|
if ( empty( $error_messages ) && $this->_process_message( $message, $sending_messenger ) ) { |
521
|
|
|
$messages_sent++; |
522
|
|
|
} |
523
|
|
|
$this->_set_error_message( $message, $error_messages ); |
524
|
|
|
//add modified time |
525
|
|
|
$message->set_modified( time() ); |
526
|
|
|
$this->_message_repository->next(); |
527
|
|
|
} |
528
|
|
|
if ( $save ) { |
529
|
|
|
$this->save(); |
530
|
|
|
} |
531
|
|
|
return $messages_sent; |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
|
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* _process_message |
538
|
|
|
* |
539
|
|
|
* @param EE_Message $message |
540
|
|
|
* @param mixed $sending_messenger (optional) |
541
|
|
|
* @return bool |
542
|
|
|
*/ |
543
|
|
|
protected function _process_message( EE_Message $message, $sending_messenger = null ) { |
544
|
|
|
// these *should* have been validated in the execute() method above |
545
|
|
|
$messenger = $message->messenger_object(); |
546
|
|
|
$message_type = $message->message_type_object(); |
547
|
|
|
//do actions for sending messenger if it differs from generating messenger and swap values. |
548
|
|
|
if ( |
549
|
|
|
$sending_messenger instanceof EE_messenger |
550
|
|
|
&& $messenger instanceof EE_messenger |
551
|
|
|
&& $sending_messenger->name != $messenger->name |
552
|
|
|
) { |
553
|
|
|
$messenger->do_secondary_messenger_hooks( $sending_messenger->name ); |
554
|
|
|
$messenger = $sending_messenger; |
555
|
|
|
} |
556
|
|
|
// send using messenger, but double check objects |
557
|
|
|
if ( $messenger instanceof EE_messenger && $message_type instanceof EE_message_type ) { |
558
|
|
|
//set hook for message type (but only if not using another messenger to send). |
559
|
|
|
if ( ! isset( $this->_did_hook[ $message_type->name ] ) ) { |
560
|
|
|
$message_type->do_messenger_hooks( $messenger ); |
561
|
|
|
$this->_did_hook[ $message_type->name ] = 1; |
562
|
|
|
} |
563
|
|
|
//if preview then use preview method |
564
|
|
|
return $this->_message_repository->is_preview() |
565
|
|
|
? $this->_do_preview( $message, $messenger, $message_type, $this->_message_repository->is_test_send() ) |
566
|
|
|
: $this->_do_send( $message, $messenger, $message_type ); |
567
|
|
|
} |
568
|
|
|
return false; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
|
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* The intention of this method is to count how many EE_Message objects |
575
|
|
|
* are in the queue with a given status. |
576
|
|
|
* |
577
|
|
|
* Example usage: |
578
|
|
|
* After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed sends |
579
|
|
|
* by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
580
|
|
|
* |
581
|
|
|
* @param array $status Stati to check for in queue |
582
|
|
|
* @return int Count of EE_Message's matching the given status. |
583
|
|
|
*/ |
584
|
|
|
public function count_STS_in_queue( $status ) { |
585
|
|
|
$count = 0; |
586
|
|
|
$status = is_array( $status ) ? $status : array( $status ); |
587
|
|
|
$this->_message_repository->rewind(); |
588
|
|
|
foreach( $this->_message_repository as $message ) { |
589
|
|
|
if ( in_array( $message->STS_ID(), $status ) ) { |
590
|
|
|
$count++; |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
return $count; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Executes the get_preview method on the provided messenger. |
599
|
|
|
* |
600
|
|
|
*@param EE_Message $message |
601
|
|
|
* @param EE_messenger $messenger |
602
|
|
|
* @param EE_message_type $message_type |
603
|
|
|
* @param $test_send |
604
|
|
|
* @return bool true means all went well, false means, not so much. |
605
|
|
|
*/ |
606
|
|
|
protected function _do_preview( EE_Message $message, EE_messenger $messenger, EE_message_type $message_type, $test_send ) { |
607
|
|
|
if ( $preview = $messenger->get_preview( $message, $message_type, $test_send ) ) { |
608
|
|
|
if ( ! $test_send ) { |
609
|
|
|
$message->set_content( $preview ); |
610
|
|
|
} |
611
|
|
|
$message->set_STS_ID( EEM_Message::status_sent ); |
612
|
|
|
return true; |
613
|
|
|
} else { |
614
|
|
|
$message->set_STS_ID( EEM_Message::status_failed ); |
615
|
|
|
return false; |
616
|
|
|
} |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
|
620
|
|
|
|
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Executes the send method on the provided messenger |
624
|
|
|
* |
625
|
|
|
* EE_Messengers are expected to: |
626
|
|
|
* - return true if the send was successful. |
627
|
|
|
* - return false if the send was unsuccessful but can be tried again. |
628
|
|
|
* - throw an Exception if the send was unsuccessful and cannot be tried again. |
629
|
|
|
* |
630
|
|
|
* @param EE_Message $message |
631
|
|
|
* @param EE_messenger $messenger |
632
|
|
|
* @param EE_message_type $message_type |
633
|
|
|
* |
634
|
|
|
* @return bool true means all went well, false means, not so much. |
635
|
|
|
*/ |
636
|
|
|
protected function _do_send( EE_Message $message, EE_messenger $messenger, EE_message_type $message_type ) { |
637
|
|
|
try { |
638
|
|
|
if ( $messenger->send_message( $message, $message_type ) ) { |
639
|
|
|
$message->set_STS_ID( EEM_Message::status_sent ); |
640
|
|
|
return true; |
641
|
|
|
} else { |
642
|
|
|
$message->set_STS_ID( EEM_Message::status_retry ); |
643
|
|
|
return false; |
644
|
|
|
} |
645
|
|
|
} catch( SendMessageException $e ) { |
646
|
|
|
$message->set_STS_ID( EEM_Message::status_failed ); |
647
|
|
|
$message->set_error_message( $e->getMessage() ); |
648
|
|
|
return false; |
649
|
|
|
} |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
|
653
|
|
|
|
654
|
|
|
|
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* This sets any necessary error messages on the message object and its status to failed. |
658
|
|
|
* @param EE_Message $message |
659
|
|
|
* @param array $error_messages the response from the messenger. |
660
|
|
|
*/ |
661
|
|
|
protected function _set_error_message( EE_Message $message, $error_messages ) { |
662
|
|
|
$error_messages = (array) $error_messages; |
663
|
|
|
if ( in_array( $message->STS_ID(), EEM_Message::instance()->stati_indicating_failed_sending() ) ) { |
664
|
|
|
$notices = EE_Error::has_notices(); |
665
|
|
|
$error_messages[] = __( 'Messenger and Message Type were valid and active, but the messenger send method failed.', 'event_espresso' ); |
666
|
|
|
if ( $notices === 1 ) { |
667
|
|
|
$notices = EE_Error::get_vanilla_notices(); |
668
|
|
|
$notices['errors'] = isset( $notices['errors'] ) ? $notices['errors'] : array(); |
669
|
|
|
$error_messages[] = implode( "\n", $notices['errors'] ); |
670
|
|
|
} |
671
|
|
|
} |
672
|
|
|
if ( count( $error_messages ) > 0 ) { |
673
|
|
|
$msg = __( 'Message was not executed successfully.', 'event_espresso' ); |
674
|
|
|
$msg = $msg . "\n" . implode( "\n", $error_messages ); |
675
|
|
|
$message->set_error_message( $msg ); |
676
|
|
|
} |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
} //end EE_Messages_Queue class |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.