|
1
|
|
|
<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
|
2
|
|
|
/** |
|
3
|
|
|
* Message Model |
|
4
|
|
|
* |
|
5
|
|
|
* @package Event Espresso |
|
6
|
|
|
* @subpackage models |
|
7
|
|
|
* @author Darren Ethier |
|
8
|
|
|
*/ |
|
9
|
|
|
class EEM_Message extends EEM_Base implements EEI_Query_Filter { |
|
10
|
|
|
|
|
11
|
|
|
// private instance of the Message object |
|
12
|
|
|
protected static $_instance = null; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This priority indicates a message should be generated and sent ASAP |
|
17
|
|
|
* @type int |
|
18
|
|
|
*/ |
|
19
|
|
|
const priority_high = 10; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* This priority indicates a message should be generated ASAP and queued for sending. |
|
26
|
|
|
* @type |
|
27
|
|
|
*/ |
|
28
|
|
|
const priority_medium = 20; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* This priority indicates a message should be queued for generating. |
|
35
|
|
|
* @type int |
|
36
|
|
|
*/ |
|
37
|
|
|
const priority_low = 30; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* indicates this message was sent at the time modified |
|
43
|
|
|
*/ |
|
44
|
|
|
const status_sent = 'MSN'; |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* indicates this message is waiting to be sent |
|
49
|
|
|
*/ |
|
50
|
|
|
const status_idle = 'MID'; |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* indicates an attempt was a made to send this message |
|
55
|
|
|
* at the scheduled time, but it failed at the time modified. This differs from MDO status in that it will ALWAYs |
|
56
|
|
|
* appear to the end user. |
|
57
|
|
|
*/ |
|
58
|
|
|
const status_failed = 'MFL'; |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* indicates the message has been flagged for resending (at the time modified). |
|
63
|
|
|
*/ |
|
64
|
|
|
const status_resend = 'MRS'; |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* indicates the message has been flagged for generation but has not been generated yet. Messages always start as this |
|
69
|
|
|
* status when added to the queue. |
|
70
|
|
|
*/ |
|
71
|
|
|
const status_incomplete = 'MIC'; |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Indicates everything was generated fine for the message, however, the messenger was unable to send. |
|
78
|
|
|
* This status means that its possible to retry sending the message. |
|
79
|
|
|
*/ |
|
80
|
|
|
const status_retry = 'MRT'; |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* This is for failed messages that are only displayed when `WP_DEBUG` is true. Usually this is used for more informational |
|
86
|
|
|
* messages that may not indicate anything is broken. |
|
87
|
|
|
*/ |
|
88
|
|
|
const status_debug_only = 'MDO'; |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Private constructor to prevent direct creation. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and |
|
98
|
|
|
* any incoming timezone data that gets saved). Note this just sends the timezone info to the |
|
99
|
|
|
* date time model field objects. Default is null (and will be assumed using the set timezone |
|
100
|
|
|
* in the 'timezone_string' wp option) |
|
101
|
|
|
* |
|
102
|
|
|
* @return EEM_Message |
|
|
|
|
|
|
103
|
|
|
*/ |
|
104
|
|
|
protected function __construct( $timezone = null ) { |
|
105
|
|
|
$this->singular_item = __('Message','event_espresso'); |
|
106
|
|
|
$this->plural_item = __('Messages','event_espresso'); |
|
107
|
|
|
|
|
108
|
|
|
//used for token generator |
|
109
|
|
|
EE_Registry::instance()->load_helper( 'URL' ); |
|
110
|
|
|
|
|
111
|
|
|
$this->_tables = array( |
|
|
|
|
|
|
112
|
|
|
'Message'=>new EE_Primary_Table('esp_message','MSG_ID') |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
$allowed_priority = array( |
|
116
|
|
|
self::priority_high => __( 'high', 'event_espresso' ), |
|
117
|
|
|
self::priority_medium => __( 'medium', 'event_espresso' ), |
|
118
|
|
|
self::priority_low => __( 'low', 'event_espresso' ) |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
$this->_fields = array( |
|
|
|
|
|
|
122
|
|
|
'Message'=>array( |
|
123
|
|
|
'MSG_ID'=>new EE_Primary_Key_Int_Field('MSG_ID', __('Message ID','event_espresso')), |
|
124
|
|
|
'MSG_token' => new EE_Plain_Text_Field( 'MSG_token', __('Unique Token used to represent this row in publicly viewable contexts (eg. a url).', 'event_espresso' ), false, EEH_URL::generate_unique_token() ), |
|
125
|
|
|
'GRP_ID'=>new EE_Foreign_Key_Int_Field( 'GRP_ID', __('Foreign key to the EEM_Message_Template_Group table.', 'event_espresso' ), true, 0, 'Message_Template_Group' ), |
|
126
|
|
|
'TXN_ID' => new EE_Foreign_Key_Int_Field( 'TXN_ID', __( 'Foreign key to the related EE_Transaction. This is required to give context for regenerating the specific message', 'event_espresso' ), true, 0, 'Transaction' ), |
|
127
|
|
|
'MSG_messenger' => new EE_Plain_Text_Field('MSG_messenger', __( 'Corresponds to the EE_messenger::name used to send this message. This will also be used to attempt any resending of the message.', 'event_espresso' ), false, 'email' ), |
|
128
|
|
|
'MSG_message_type' => new EE_Plain_Text_Field( 'MSG_message_type', __( 'Corresponds to the EE_message_type::name used to generate this message.', 'event_espresso' ), false, 'receipt' ), |
|
129
|
|
|
'MSG_context' => new EE_Plain_Text_Field( 'MSG_context', __( 'Context', 'event_espresso' ), false ), |
|
130
|
|
|
'MSG_recipient_ID' => new EE_Foreign_Key_Int_Field( 'MSG_recipient_ID', __( 'Recipient ID', 'event_espresso' ), true, null, array( 'Registration', 'Attendee', 'WP_User' ) ), |
|
|
|
|
|
|
131
|
|
|
'MSG_recipient_type' => new EE_Any_Foreign_Model_Name_Field( 'MSG_recipient_type', __( 'Recipient Type', 'event_espresso' ), true, null, array( 'Registration', 'Attendee', 'WP_User' ) ), |
|
|
|
|
|
|
132
|
|
|
'MSG_content' => new EE_Maybe_Serialized_Text_Field( 'MSG_content', __( 'Content', 'event_espresso' ), true, '' ), |
|
133
|
|
|
'MSG_to' => new EE_Maybe_Serialized_Text_Field( 'MSG_to', __( 'Address To', 'event_espresso' ), true ), |
|
134
|
|
|
'MSG_from' => new EE_Maybe_Serialized_Text_Field( 'MSG_from', __( 'Address From', 'event_espresso' ), true ), |
|
135
|
|
|
'MSG_subject' => new EE_Maybe_Serialized_Text_Field( 'MSG_subject', __( 'Subject', 'event_espresso' ), true, '' ), |
|
136
|
|
|
'MSG_priority' => new EE_Enum_Integer_Field( 'MSG_priority', __( 'Priority', 'event_espresso' ), false, self::priority_low, $allowed_priority ), |
|
137
|
|
|
'STS_ID' => new EE_Foreign_Key_String_Field( 'STS_ID', __( 'Status', 'event_espresso' ), false, self::status_incomplete, 'Status' ), |
|
138
|
|
|
'MSG_created' => new EE_Datetime_Field( 'MSG_created', __( 'Created', 'event_espresso' ), false, time() ), |
|
139
|
|
|
'MSG_modified' => new EE_Datetime_Field( 'MSG_modified', __( 'Modified', 'event_espresso' ), true, time() ) |
|
140
|
|
|
) |
|
141
|
|
|
); |
|
142
|
|
|
$this->_model_relations = array( |
|
|
|
|
|
|
143
|
|
|
'Attendee' => new EE_Belongs_To_Any_Relation(), |
|
144
|
|
|
'Registration' => new EE_Belongs_To_Any_Relation(), |
|
145
|
|
|
'WP_User' => new EE_Belongs_To_Any_Relation(), |
|
146
|
|
|
'Message_Template_Group' => new EE_Belongs_To_Relation(), |
|
147
|
|
|
'Transaction' => new EE_Belongs_To_Relation() |
|
148
|
|
|
); |
|
149
|
|
|
parent::__construct( $timezone ); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return \EE_Message |
|
156
|
|
|
*/ |
|
157
|
|
|
public function create_default_object() { |
|
158
|
|
|
/** @type EE_Message $message */ |
|
159
|
|
|
$message = parent::create_default_object(); |
|
160
|
|
|
if ( $message instanceof EE_Message ) { |
|
161
|
|
|
return EE_Message_Factory::set_messenger_and_message_type( $message ); |
|
162
|
|
|
} |
|
163
|
|
|
return null; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param mixed $cols_n_values |
|
170
|
|
|
* @return \EE_Message |
|
171
|
|
|
*/ |
|
172
|
|
|
public function instantiate_class_from_array_or_object( $cols_n_values ) { |
|
173
|
|
|
/** @type EE_Message $message */ |
|
174
|
|
|
$message = parent::instantiate_class_from_array_or_object( $cols_n_values ); |
|
175
|
|
|
if ( $message instanceof EE_Message ) { |
|
176
|
|
|
return EE_Message_Factory::set_messenger_and_message_type( $message ); |
|
177
|
|
|
} |
|
178
|
|
|
return null; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns whether or not a message of that type was sent for a given attendee. |
|
185
|
|
|
* @param EE_Attendee|int $attendee |
|
186
|
|
|
* @param string $message_type the message type slug |
|
187
|
|
|
* @return boolean |
|
188
|
|
|
*/ |
|
189
|
|
View Code Duplication |
public function message_sent_for_attendee( $attendee, $message_type ) { |
|
|
|
|
|
|
190
|
|
|
$attendee_ID = EEM_Attendee::instance()->ensure_is_ID( $attendee ); |
|
191
|
|
|
return $this->exists( array( array( |
|
192
|
|
|
'Attendee.ATT_ID' => $attendee_ID, |
|
193
|
|
|
'MSG_message_type' => $message_type, |
|
194
|
|
|
'STS_ID' => array( 'IN', $this->stati_indicating_sent() ) |
|
195
|
|
|
) ) ); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Returns whether or not a message of that type was sent for a given registration |
|
203
|
|
|
* @param EE_Registration|int $registration |
|
204
|
|
|
* @param string $message_type the message type slug |
|
205
|
|
|
* @return boolean |
|
206
|
|
|
*/ |
|
207
|
|
View Code Duplication |
public function message_sent_for_registration( $registration, $message_type ) { |
|
|
|
|
|
|
208
|
|
|
$registrationID = EEM_Registration::instance()->ensure_is_ID( $registration ); |
|
209
|
|
|
return $this->exists( array( array( |
|
210
|
|
|
'Registration.REG_ID' => $registrationID, |
|
211
|
|
|
'MSG_message_type' => $message_type, |
|
212
|
|
|
'STS_ID' => array( 'IN', $this->stati_indicating_sent() ) |
|
213
|
|
|
) ) ); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* This retrieves an EE_Message object from the db matching the given token string. |
|
221
|
|
|
* @param string $token |
|
222
|
|
|
* @return EE_Message |
|
223
|
|
|
*/ |
|
224
|
|
|
public function get_one_by_token( $token ) { |
|
225
|
|
|
return $this->get_one( array( array( |
|
226
|
|
|
'MSG_token' => $token |
|
227
|
|
|
) ) ); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Returns stati that indicate the message HAS been sent |
|
233
|
|
|
* @return array of strings for possible stati |
|
234
|
|
|
*/ |
|
235
|
|
|
public function stati_indicating_sent(){ |
|
236
|
|
|
return apply_filters( 'FHEE__EEM_Message__stati_indicating_sent', array( self::status_sent ) ); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Returns stati that indicate the message is waiting to be sent. |
|
244
|
|
|
* @return array of strings for possible stati. |
|
245
|
|
|
*/ |
|
246
|
|
|
public function stati_indicating_to_send() { |
|
247
|
|
|
return apply_filters( 'FHEE__EEM_Message__stati_indicating_to_send', array( self::status_idle, self::status_resend ) ); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
public function stati_indicating_failed_sending() { |
|
253
|
|
|
return apply_filters( 'FHEE__EEM_Message__stati_indicating_failed_sending', array( self::status_failed, self::status_retry, self::status_debug_only ) ); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Returns filterable array of all EEM_Message statuses. |
|
261
|
|
|
* @return array |
|
262
|
|
|
*/ |
|
263
|
|
|
public function all_statuses() { |
|
264
|
|
|
return apply_filters( |
|
265
|
|
|
'FHEE__EEM_Message__all_statuses', |
|
266
|
|
|
array( |
|
267
|
|
|
EEM_Message::status_sent, |
|
268
|
|
|
EEM_Message::status_incomplete, |
|
269
|
|
|
EEM_Message::status_idle, |
|
270
|
|
|
EEM_Message::status_resend, |
|
271
|
|
|
EEM_Message::status_retry, |
|
272
|
|
|
EEM_Message::status_failed, |
|
273
|
|
|
EEM_Message::status_debug_only, |
|
274
|
|
|
) |
|
275
|
|
|
); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Detects any specific query variables in the request and uses those to setup appropriate |
|
280
|
|
|
* filter for any queries. |
|
281
|
|
|
* @return array |
|
282
|
|
|
*/ |
|
283
|
|
|
public function filter_by_query_params() { |
|
284
|
|
|
// expected possible query_vars, the key in this array matches an expected key in the request, |
|
285
|
|
|
// the value, matches the corresponding EEM_Base child reference. |
|
286
|
|
|
$expected_vars = $this->_expected_vars_for_query_inject(); |
|
287
|
|
|
$query_params[0] = array(); |
|
|
|
|
|
|
288
|
|
|
foreach ( $expected_vars as $request_key => $model_name ) { |
|
289
|
|
|
$request_value = EE_Registry::instance()->REQ->get( $request_key ); |
|
290
|
|
|
if ( $request_value ) { |
|
291
|
|
|
//special case |
|
292
|
|
|
switch ( $request_key ) { |
|
293
|
|
|
case '_REG_ID' : |
|
294
|
|
|
$query_params[0]['AND**filter_by']['OR**filter_by_REG_ID'] = array( |
|
295
|
|
|
'Transaction.Registration.REG_ID' => $request_value, |
|
296
|
|
|
); |
|
297
|
|
|
break; |
|
298
|
|
|
case 'EVT_ID' : |
|
299
|
|
|
$query_params[0]['AND**filter_by']['OR**filter_by_EVT_ID'] = array( |
|
300
|
|
|
'Transaction.Registration.EVT_ID' => $request_value, |
|
301
|
|
|
); |
|
302
|
|
|
break; |
|
303
|
|
|
default : |
|
304
|
|
|
$query_params[0]['AND**filter_by'][ 'OR**filter_by_' . $request_key ][ $model_name . '.' . $request_key ] = $request_value; |
|
305
|
|
|
break; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
return $query_params; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
|
|
314
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
public function get_pretty_label_for_results() { |
|
317
|
|
|
$expected_vars = $this->_expected_vars_for_query_inject(); |
|
318
|
|
|
$pretty_label = ''; |
|
319
|
|
|
$label_parts = array(); |
|
320
|
|
|
foreach ( $expected_vars as $request_key => $model_name ) { |
|
321
|
|
|
$model = EE_Registry::instance()->load_model( $model_name ); |
|
322
|
|
|
if ( $model_field_value = EE_Registry::instance()->REQ->get( $request_key ) ) { |
|
323
|
|
|
switch ( $request_key ) { |
|
324
|
|
|
case '_REG_ID' : |
|
325
|
|
|
$label_parts[] = sprintf( |
|
326
|
|
|
esc_html__( 'Registration with the ID: %s', 'event_espresso' ), |
|
327
|
|
|
$model_field_value |
|
328
|
|
|
); |
|
329
|
|
|
break; |
|
330
|
|
|
case 'ATT_ID' : |
|
331
|
|
|
/** @var EE_Attendee $attendee */ |
|
332
|
|
|
$attendee = $model->get_one_by_ID( $model_field_value ); |
|
333
|
|
|
$label_parts[] = $attendee instanceof EE_Attendee |
|
334
|
|
|
? sprintf( esc_html__( 'Attendee %s', 'event_espresso' ), $attendee->full_name() ) |
|
335
|
|
|
: sprintf( esc_html__( 'Attendee ID: %s', 'event_espresso' ), $model_field_value ); |
|
336
|
|
|
break; |
|
337
|
|
|
case 'ID' : |
|
338
|
|
|
/** @var EE_WP_User $wpUser */ |
|
339
|
|
|
$wpUser = $model->get_one_by_ID( $model_field_value ); |
|
340
|
|
|
$label_parts[] = $wpUser instanceof EE_WP_User |
|
341
|
|
|
? sprintf( esc_html__( 'WP User: %s', 'event_espresso' ), $wpUser->name() ) |
|
342
|
|
|
: sprintf( esc_html__( 'WP User ID: %s', 'event_espresso' ), $model_field_value ); |
|
343
|
|
|
break; |
|
344
|
|
|
case 'TXN_ID' : |
|
345
|
|
|
$label_parts[] = sprintf( |
|
346
|
|
|
esc_html__( 'Transaction with the ID: %s', 'event_espresso' ), |
|
347
|
|
|
$model_field_value |
|
348
|
|
|
); |
|
349
|
|
|
break; |
|
350
|
|
|
case 'EVT_ID' : |
|
351
|
|
|
/** @var EE_Event $Event */ |
|
352
|
|
|
$Event = $model->get_one_by_ID( $model_field_value ); |
|
353
|
|
|
$label_parts[] = $Event instanceof EE_Event |
|
354
|
|
|
? sprintf( esc_html__( 'for the Event: %s', 'event_espresso' ), $Event->name() ) |
|
355
|
|
|
: sprintf( esc_html__( 'for the Event with ID: %s', 'event_espresso' ), $model_field_value ); |
|
356
|
|
|
break; |
|
357
|
|
|
} |
|
358
|
|
|
} |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
if ( $label_parts ) { |
|
|
|
|
|
|
362
|
|
|
|
|
363
|
|
|
//prepend to the last element of $label_parts an "and". |
|
364
|
|
|
if ( count( $label_parts ) > 1 ) { |
|
365
|
|
|
$label_parts_index_to_prepend = count( $label_parts ) - 1; |
|
366
|
|
|
$label_parts[ $label_parts_index_to_prepend ] = 'and' . $label_parts[ $label_parts_index_to_prepend ]; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
$pretty_label .= sprintf( |
|
370
|
|
|
esc_html_x( |
|
371
|
|
|
'Showing messages for %s', |
|
372
|
|
|
'A label for the messages returned in a query that are filtered by items in the query. This could be Transaction, Event, Attendee, Registration, or WP_User.', |
|
373
|
|
|
'event_espresso' |
|
374
|
|
|
), |
|
375
|
|
|
implode( ', ', $label_parts ) |
|
376
|
|
|
); |
|
377
|
|
|
} |
|
378
|
|
|
return $pretty_label; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
|
|
382
|
|
|
|
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* This returns the array of expected variables for the EEI_Query_Filter methods being implemented |
|
386
|
|
|
* The array is in the format: |
|
387
|
|
|
* |
|
388
|
|
|
* array( |
|
389
|
|
|
* {$field_name} => {$model_name} |
|
390
|
|
|
* ); |
|
391
|
|
|
* |
|
392
|
|
|
* @since 4.9.0 |
|
393
|
|
|
* @return array |
|
394
|
|
|
*/ |
|
395
|
|
|
protected function _expected_vars_for_query_inject() { |
|
396
|
|
|
return array( |
|
397
|
|
|
'_REG_ID' => 'Registration', |
|
398
|
|
|
'ATT_ID' => 'Attendee', |
|
399
|
|
|
'ID' => 'WP_User', |
|
400
|
|
|
'TXN_ID' => 'Transaction', |
|
401
|
|
|
'EVT_ID' => 'Event', |
|
402
|
|
|
); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
|
|
406
|
|
|
} |
|
407
|
|
|
// End of file EEM_Message.model.php |
|
408
|
|
|
// Location: /includes/models/EEM_Message.model.php |
|
409
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.