@@ -1,15 +1,15 @@ |
||
1 | 1 | <?php |
2 | 2 | //echo '<br/><h6 style="color:#2EA2CC;">'. __FILE__ . ' <span style="font-weight:normal;color:#E76700"> Line #: ' . __LINE__ . '</span></h6>'; |
3 | 3 | |
4 | -if ( is_single() || ( is_archive() && espresso_display_datetimes_in_event_list() ) ) : |
|
4 | +if (is_single() || (is_archive() && espresso_display_datetimes_in_event_list())) : |
|
5 | 5 | global $post; |
6 | -do_action( 'AHEE_event_details_before_event_date', $post ); |
|
6 | +do_action('AHEE_event_details_before_event_date', $post); |
|
7 | 7 | ?> |
8 | 8 | <div class="event-datetimes"> |
9 | - <?php espresso_list_of_event_dates( $post->ID );?> |
|
9 | + <?php espresso_list_of_event_dates($post->ID); ?> |
|
10 | 10 | </div> |
11 | 11 | <!-- .event-datetimes --> |
12 | 12 | <?php |
13 | -do_action( 'AHEE_event_details_after_event_date', $post ); |
|
13 | +do_action('AHEE_event_details_after_event_date', $post); |
|
14 | 14 | endif; |
15 | 15 | ?> |
16 | 16 | \ No newline at end of file |
@@ -697,7 +697,7 @@ |
||
697 | 697 | /** |
698 | 698 | * Gets any error message. |
699 | 699 | * |
700 | - * @return mixed|null |
|
700 | + * @return string |
|
701 | 701 | */ |
702 | 702 | public function error_message() |
703 | 703 | { |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | { |
52 | 52 | $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
53 | 53 | // if object doesn't exist, let's generate a unique token on instantiation so that its available even before saving to db. |
54 | - if (! $has_object) { |
|
54 | + if ( ! $has_object) { |
|
55 | 55 | EE_Registry::instance()->load_helper('URL'); |
56 | 56 | $props_n_values['MSG_token'] = EEH_URL::generate_unique_token(); |
57 | 57 | } |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | { |
220 | 220 | $label_type = $plural ? 'plural' : 'singular'; |
221 | 221 | $messenger = $this->messenger_object(); |
222 | - return $messenger instanceof EE_messenger ? $messenger->label[ $label_type ] : $this->messenger(); |
|
222 | + return $messenger instanceof EE_messenger ? $messenger->label[$label_type] : $this->messenger(); |
|
223 | 223 | } |
224 | 224 | |
225 | 225 | |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | $this->messenger(), |
337 | 337 | $this->message_type() |
338 | 338 | ); |
339 | - if (! $valid && $throw_exceptions) { |
|
339 | + if ( ! $valid && $throw_exceptions) { |
|
340 | 340 | throw new EE_Error( |
341 | 341 | sprintf( |
342 | 342 | __( |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | $label_type = $plural ? 'plural' : 'singular'; |
367 | 367 | $message_type = $this->message_type_object(); |
368 | 368 | return $message_type instanceof EE_message_type |
369 | - ? $message_type->label[ $label_type ] |
|
369 | + ? $message_type->label[$label_type] |
|
370 | 370 | : str_replace( |
371 | 371 | '_', |
372 | 372 | ' ', |
@@ -397,7 +397,7 @@ discard block |
||
397 | 397 | /** @type EE_Message_Resource_Manager $message_resource_manager */ |
398 | 398 | $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
399 | 399 | $contexts = $message_resource_manager->get_all_contexts(); |
400 | - return isset($contexts[ $this->context() ]) ? $contexts[ $this->context() ] : $this->context(); |
|
400 | + return isset($contexts[$this->context()]) ? $contexts[$this->context()] : $this->context(); |
|
401 | 401 | } |
402 | 402 | |
403 | 403 | |
@@ -452,7 +452,7 @@ discard block |
||
452 | 452 | */ |
453 | 453 | public function recipient_object() |
454 | 454 | { |
455 | - if (! $this->recipient_type() || ! $this->recipient_ID()) { |
|
455 | + if ( ! $this->recipient_type() || ! $this->recipient_ID()) { |
|
456 | 456 | return null; |
457 | 457 | } |
458 | 458 | |
@@ -736,13 +736,13 @@ discard block |
||
736 | 736 | /** |
737 | 737 | * This is deprecated functionality that will be removed eventually but included here now for backward compat. |
738 | 738 | */ |
739 | - if (! empty($this->template_pack)) { |
|
739 | + if ( ! empty($this->template_pack)) { |
|
740 | 740 | return $this->template_pack; |
741 | 741 | } |
742 | 742 | /** @type EE_Message_Template_Group $grp */ |
743 | 743 | $grp = $this->get_first_related('Message_Template_Group'); |
744 | 744 | // if no group then let's try to get the first related group by internal messenger and message type (will use global grp). |
745 | - if (! $grp instanceof EE_Message_Template_Group) { |
|
745 | + if ( ! $grp instanceof EE_Message_Template_Group) { |
|
746 | 746 | $grp = EEM_Message_Template_Group::instance()->get_one( |
747 | 747 | array( |
748 | 748 | array( |
@@ -768,7 +768,7 @@ discard block |
||
768 | 768 | /** |
769 | 769 | * This is deprecated functionality that will be removed eventually but included here now for backward compat. |
770 | 770 | */ |
771 | - if (! empty($this->template_variation)) { |
|
771 | + if ( ! empty($this->template_variation)) { |
|
772 | 772 | return $this->template_variation; |
773 | 773 | } |
774 | 774 | |
@@ -776,7 +776,7 @@ discard block |
||
776 | 776 | $grp = $this->get_first_related('Message_Template_Group'); |
777 | 777 | |
778 | 778 | // if no group then let's try to get the first related group by internal messenger and message type (will use global grp). |
779 | - if (! $grp instanceof EE_Message_Template_Group) { |
|
779 | + if ( ! $grp instanceof EE_Message_Template_Group) { |
|
780 | 780 | $grp = EEM_Message_Template_Group::instance()->get_one( |
781 | 781 | array( |
782 | 782 | array( |
@@ -10,875 +10,875 @@ |
||
10 | 10 | class EE_Message extends EE_Base_Class implements EEI_Admin_Links |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @deprecated 4.9.0 Added for backward compat with add-on's |
|
15 | - * @type null |
|
16 | - */ |
|
17 | - public $template_pack; |
|
18 | - |
|
19 | - /** |
|
20 | - * @deprecated 4.9.0 Added for backward compat with add-on's |
|
21 | - * @type null |
|
22 | - */ |
|
23 | - public $template_variation; |
|
24 | - |
|
25 | - /** |
|
26 | - * @deprecated 4.9.0 Added for backward compat with add-on's |
|
27 | - * @type string |
|
28 | - */ |
|
29 | - public $content = ''; |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * @type EE_messenger $_messenger |
|
34 | - */ |
|
35 | - protected $_messenger = null; |
|
36 | - |
|
37 | - /** |
|
38 | - * @type EE_message_type $_message_type |
|
39 | - */ |
|
40 | - protected $_message_type = null; |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * @param array $props_n_values |
|
45 | - * @param string $timezone |
|
46 | - * @param array $date_formats incoming date formats in an array. First value is the date_format, second is time |
|
47 | - * format. |
|
48 | - * @return EE_Message |
|
49 | - */ |
|
50 | - public static function new_instance($props_n_values = array(), $timezone = '', $date_formats = array()) |
|
51 | - { |
|
52 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
53 | - // if object doesn't exist, let's generate a unique token on instantiation so that its available even before saving to db. |
|
54 | - if (! $has_object) { |
|
55 | - EE_Registry::instance()->load_helper('URL'); |
|
56 | - $props_n_values['MSG_token'] = EEH_URL::generate_unique_token(); |
|
57 | - } |
|
58 | - return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats); |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * @param array $props_n_values |
|
64 | - * @param string $timezone |
|
65 | - * @return EE_Message |
|
66 | - */ |
|
67 | - public static function new_instance_from_db($props_n_values = array(), $timezone = '') |
|
68 | - { |
|
69 | - return new self($props_n_values, true, $timezone); |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * Gets MSG_token |
|
75 | - * |
|
76 | - * @return int |
|
77 | - */ |
|
78 | - public function MSG_token() |
|
79 | - { |
|
80 | - return $this->get('MSG_token'); |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Sets MSG_token |
|
86 | - * |
|
87 | - * @param int $MSG_token |
|
88 | - */ |
|
89 | - public function set_MSG_token($MSG_token) |
|
90 | - { |
|
91 | - $this->set('MSG_token', $MSG_token); |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * Gets GRP_ID |
|
97 | - * |
|
98 | - * @return int |
|
99 | - */ |
|
100 | - public function GRP_ID() |
|
101 | - { |
|
102 | - return $this->get('GRP_ID'); |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * Sets GRP_ID |
|
108 | - * |
|
109 | - * @param int $GRP_ID |
|
110 | - */ |
|
111 | - public function set_GRP_ID($GRP_ID) |
|
112 | - { |
|
113 | - $this->set('GRP_ID', $GRP_ID); |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * Gets TXN_ID |
|
119 | - * |
|
120 | - * @return int |
|
121 | - */ |
|
122 | - public function TXN_ID() |
|
123 | - { |
|
124 | - return $this->get('TXN_ID'); |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Sets TXN_ID |
|
130 | - * |
|
131 | - * @param int $TXN_ID |
|
132 | - */ |
|
133 | - public function set_TXN_ID($TXN_ID) |
|
134 | - { |
|
135 | - $this->set('TXN_ID', $TXN_ID); |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * Gets messenger |
|
141 | - * |
|
142 | - * @return string |
|
143 | - */ |
|
144 | - public function messenger() |
|
145 | - { |
|
146 | - return $this->get('MSG_messenger'); |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * Sets messenger |
|
152 | - * |
|
153 | - * @param string $messenger |
|
154 | - */ |
|
155 | - public function set_messenger($messenger) |
|
156 | - { |
|
157 | - $this->set('MSG_messenger', $messenger); |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - /** |
|
162 | - * Returns corresponding messenger object for the set messenger on this message |
|
163 | - * |
|
164 | - * @return EE_messenger | null |
|
165 | - */ |
|
166 | - public function messenger_object() |
|
167 | - { |
|
168 | - return $this->_messenger; |
|
169 | - } |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * Sets messenger |
|
174 | - * |
|
175 | - * @param EE_messenger $messenger |
|
176 | - */ |
|
177 | - public function set_messenger_object(EE_messenger $messenger) |
|
178 | - { |
|
179 | - $this->_messenger = $messenger; |
|
180 | - } |
|
181 | - |
|
182 | - |
|
183 | - /** |
|
184 | - * validates messenger |
|
185 | - * |
|
186 | - * @param bool $throw_exceptions |
|
187 | - * @return bool |
|
188 | - * @throws \EE_Error |
|
189 | - */ |
|
190 | - public function valid_messenger($throw_exceptions = false) |
|
191 | - { |
|
192 | - if ($this->_messenger instanceof EE_messenger) { |
|
193 | - return true; |
|
194 | - } |
|
195 | - if ($throw_exceptions) { |
|
196 | - throw new EE_Error( |
|
197 | - sprintf( |
|
198 | - __( |
|
199 | - 'The "%1$s" messenger set for this message is missing or invalid. Please double-check the spelling and verify that the correct files exist.', |
|
200 | - 'event_espresso' |
|
201 | - ), |
|
202 | - $this->messenger() |
|
203 | - ) |
|
204 | - ); |
|
205 | - } |
|
206 | - return false; |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - /** |
|
211 | - * This returns the set localized label for the messenger on this message. |
|
212 | - * Note, if unable to retrieve the EE_messenger object then will just return the messenger slug saved |
|
213 | - * with this message. |
|
214 | - * |
|
215 | - * @param bool $plural whether to return the plural label or not. |
|
216 | - * @return string |
|
217 | - */ |
|
218 | - public function messenger_label($plural = false) |
|
219 | - { |
|
220 | - $label_type = $plural ? 'plural' : 'singular'; |
|
221 | - $messenger = $this->messenger_object(); |
|
222 | - return $messenger instanceof EE_messenger ? $messenger->label[ $label_type ] : $this->messenger(); |
|
223 | - } |
|
224 | - |
|
225 | - |
|
226 | - /** |
|
227 | - * Gets message_type |
|
228 | - * |
|
229 | - * @return string |
|
230 | - */ |
|
231 | - public function message_type() |
|
232 | - { |
|
233 | - return $this->get('MSG_message_type'); |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * Sets message_type |
|
239 | - * |
|
240 | - * @param string $message_type |
|
241 | - */ |
|
242 | - public function set_message_type($message_type) |
|
243 | - { |
|
244 | - $this->set('MSG_message_type', $message_type); |
|
245 | - } |
|
246 | - |
|
247 | - |
|
248 | - /** |
|
249 | - * Returns the message type object for the set message type on this message |
|
250 | - * |
|
251 | - * @return EE_message_type | null |
|
252 | - */ |
|
253 | - public function message_type_object() |
|
254 | - { |
|
255 | - return $this->_message_type; |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * Sets message_type |
|
261 | - * |
|
262 | - * @param EE_message_type $message_type |
|
263 | - * @param bool $set_priority This indicates whether to set the priority to whatever the priority is on |
|
264 | - * the message type or not. |
|
265 | - */ |
|
266 | - public function set_message_type_object(EE_message_type $message_type, $set_priority = false) |
|
267 | - { |
|
268 | - $this->_message_type = $message_type; |
|
269 | - if ($set_priority) { |
|
270 | - $this->set_priority($this->_message_type->get_priority()); |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - |
|
275 | - /** |
|
276 | - * validates message_type |
|
277 | - * |
|
278 | - * @param bool $throw_exceptions |
|
279 | - * @return bool |
|
280 | - * @throws \EE_Error |
|
281 | - */ |
|
282 | - public function valid_message_type($throw_exceptions = false) |
|
283 | - { |
|
284 | - if ($this->_message_type instanceof EE_message_type) { |
|
285 | - return true; |
|
286 | - } |
|
287 | - if ($throw_exceptions) { |
|
288 | - throw new EE_Error( |
|
289 | - sprintf( |
|
290 | - __( |
|
291 | - 'The %1$s message type set for this message is missing or invalid. Please double-check the spelling and verify that the correct files exist.', |
|
292 | - 'event_espresso' |
|
293 | - ), |
|
294 | - $this->message_type() |
|
295 | - ) |
|
296 | - ); |
|
297 | - } |
|
298 | - return false; |
|
299 | - } |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * validates messenger and message_type (that they are valid EE_messenger and EE_message_type objects). |
|
304 | - * |
|
305 | - * @param bool $throw_exceptions |
|
306 | - * @return bool |
|
307 | - * @throws \EE_Error |
|
308 | - */ |
|
309 | - public function is_valid($throw_exceptions = false) |
|
310 | - { |
|
311 | - if ($this->valid_messenger($throw_exceptions) && $this->valid_message_type($throw_exceptions)) { |
|
312 | - return true; |
|
313 | - } |
|
314 | - return false; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /** |
|
319 | - * This validates whether the internal messenger and message type objects are valid for sending. |
|
320 | - * Three checks are done: |
|
321 | - * 1. There is a valid messenger object. |
|
322 | - * 2. There is a valid message type object. |
|
323 | - * 3. The message type object is active for the messenger. |
|
324 | - * |
|
325 | - * @throws EE_Error But only if $throw_exceptions is set to true. |
|
326 | - * @param bool $throw_exceptions |
|
327 | - * @return bool |
|
328 | - */ |
|
329 | - public function is_valid_for_sending_or_generation($throw_exceptions = false) |
|
330 | - { |
|
331 | - $valid = false; |
|
332 | - if ($this->is_valid($throw_exceptions)) { |
|
333 | - /** @var EE_Message_Resource_Manager $message_resource_manager */ |
|
334 | - $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
335 | - $valid = $message_resource_manager->is_message_type_active_for_messenger( |
|
336 | - $this->messenger(), |
|
337 | - $this->message_type() |
|
338 | - ); |
|
339 | - if (! $valid && $throw_exceptions) { |
|
340 | - throw new EE_Error( |
|
341 | - sprintf( |
|
342 | - __( |
|
343 | - 'The %1$s message type is not a valid message type for the %2$s messenger so it will not be sent.', |
|
344 | - 'event_espresso' |
|
345 | - ), |
|
346 | - $this->message_type(), |
|
347 | - $this->messenger() |
|
348 | - ) |
|
349 | - ); |
|
350 | - } |
|
351 | - } |
|
352 | - return $valid; |
|
353 | - } |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * This returns the set localized label for the message type on this message. |
|
358 | - * Note, if unable to retrieve the EE_message_type object then will just return the message type slug saved |
|
359 | - * with this message. |
|
360 | - * |
|
361 | - * @param bool $plural whether to return the plural label or not. |
|
362 | - * @return string |
|
363 | - */ |
|
364 | - public function message_type_label($plural = false) |
|
365 | - { |
|
366 | - $label_type = $plural ? 'plural' : 'singular'; |
|
367 | - $message_type = $this->message_type_object(); |
|
368 | - return $message_type instanceof EE_message_type |
|
369 | - ? $message_type->label[ $label_type ] |
|
370 | - : str_replace( |
|
371 | - '_', |
|
372 | - ' ', |
|
373 | - $this->message_type() |
|
374 | - ); |
|
375 | - } |
|
376 | - |
|
377 | - |
|
378 | - /** |
|
379 | - * Gets context |
|
380 | - * |
|
381 | - * @return string |
|
382 | - */ |
|
383 | - public function context() |
|
384 | - { |
|
385 | - return $this->get('MSG_context'); |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - /** |
|
390 | - * This returns the corresponding localized label for the given context slug, if possible from installed message |
|
391 | - * types. Otherwise, this will just return the set context slug on this object. |
|
392 | - * |
|
393 | - * @return string |
|
394 | - */ |
|
395 | - public function context_label() |
|
396 | - { |
|
397 | - /** @type EE_Message_Resource_Manager $message_resource_manager */ |
|
398 | - $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
399 | - $contexts = $message_resource_manager->get_all_contexts(); |
|
400 | - return isset($contexts[ $this->context() ]) ? $contexts[ $this->context() ] : $this->context(); |
|
401 | - } |
|
402 | - |
|
403 | - |
|
404 | - /** |
|
405 | - * Sets context |
|
406 | - * |
|
407 | - * @param string $context |
|
408 | - */ |
|
409 | - public function set_context($context) |
|
410 | - { |
|
411 | - $this->set('MSG_context', $context); |
|
412 | - } |
|
413 | - |
|
414 | - |
|
415 | - /** |
|
416 | - * Gets recipient_ID |
|
417 | - * |
|
418 | - * @return int |
|
419 | - */ |
|
420 | - public function recipient_ID() |
|
421 | - { |
|
422 | - return $this->get('MSG_recipient_ID'); |
|
423 | - } |
|
424 | - |
|
425 | - |
|
426 | - /** |
|
427 | - * Sets recipient_ID |
|
428 | - * |
|
429 | - * @param string $recipient_ID |
|
430 | - */ |
|
431 | - public function set_recipient_ID($recipient_ID) |
|
432 | - { |
|
433 | - $this->set('MSG_recipient_ID', $recipient_ID); |
|
434 | - } |
|
435 | - |
|
436 | - |
|
437 | - /** |
|
438 | - * Gets recipient_type |
|
439 | - * |
|
440 | - * @return string |
|
441 | - */ |
|
442 | - public function recipient_type() |
|
443 | - { |
|
444 | - return $this->get('MSG_recipient_type'); |
|
445 | - } |
|
446 | - |
|
447 | - |
|
448 | - /** |
|
449 | - * Return the related object matching the recipient type and ID. |
|
450 | - * |
|
451 | - * @return EE_Base_Class | null |
|
452 | - */ |
|
453 | - public function recipient_object() |
|
454 | - { |
|
455 | - if (! $this->recipient_type() || ! $this->recipient_ID()) { |
|
456 | - return null; |
|
457 | - } |
|
458 | - |
|
459 | - return $this->get_first_related($this->recipient_type()); |
|
460 | - } |
|
461 | - |
|
462 | - |
|
463 | - /** |
|
464 | - * Sets recipient_type |
|
465 | - * |
|
466 | - * @param string $recipient_type |
|
467 | - */ |
|
468 | - public function set_recipient_type($recipient_type) |
|
469 | - { |
|
470 | - $this->set('MSG_recipient_type', $recipient_type); |
|
471 | - } |
|
472 | - |
|
473 | - |
|
474 | - /** |
|
475 | - * Gets content |
|
476 | - * |
|
477 | - * @return string |
|
478 | - */ |
|
479 | - public function content() |
|
480 | - { |
|
481 | - return $this->get('MSG_content'); |
|
482 | - } |
|
483 | - |
|
484 | - |
|
485 | - /** |
|
486 | - * Sets content |
|
487 | - * |
|
488 | - * @param string $content |
|
489 | - */ |
|
490 | - public function set_content($content) |
|
491 | - { |
|
492 | - $this->set('MSG_content', $content); |
|
493 | - } |
|
494 | - |
|
495 | - |
|
496 | - /** |
|
497 | - * Gets subject |
|
498 | - * |
|
499 | - * @return string |
|
500 | - */ |
|
501 | - public function subject() |
|
502 | - { |
|
503 | - return $this->get('MSG_subject'); |
|
504 | - } |
|
505 | - |
|
506 | - |
|
507 | - /** |
|
508 | - * Sets subject |
|
509 | - * |
|
510 | - * @param string $subject |
|
511 | - */ |
|
512 | - public function set_subject($subject) |
|
513 | - { |
|
514 | - $this->set('MSG_subject', $subject); |
|
515 | - } |
|
516 | - |
|
517 | - |
|
518 | - /** |
|
519 | - * Gets to |
|
520 | - * |
|
521 | - * @return string |
|
522 | - */ |
|
523 | - public function to() |
|
524 | - { |
|
525 | - $to = $this->get('MSG_to'); |
|
526 | - return empty($to) ? __('No recipient', 'event_espresso') : $to; |
|
527 | - } |
|
528 | - |
|
529 | - |
|
530 | - /** |
|
531 | - * Sets to |
|
532 | - * |
|
533 | - * @param string $to |
|
534 | - */ |
|
535 | - public function set_to($to) |
|
536 | - { |
|
537 | - $this->set('MSG_to', $to); |
|
538 | - } |
|
539 | - |
|
540 | - |
|
541 | - /** |
|
542 | - * Gets from |
|
543 | - * |
|
544 | - * @return string |
|
545 | - */ |
|
546 | - public function from() |
|
547 | - { |
|
548 | - return $this->get('MSG_from'); |
|
549 | - } |
|
550 | - |
|
551 | - |
|
552 | - /** |
|
553 | - * Sets from |
|
554 | - * |
|
555 | - * @param string $from |
|
556 | - */ |
|
557 | - public function set_from($from) |
|
558 | - { |
|
559 | - $this->set('MSG_from', $from); |
|
560 | - } |
|
561 | - |
|
562 | - |
|
563 | - /** |
|
564 | - * Gets priority |
|
565 | - * |
|
566 | - * @return int |
|
567 | - */ |
|
568 | - public function priority() |
|
569 | - { |
|
570 | - return $this->get('MSG_priority'); |
|
571 | - } |
|
572 | - |
|
573 | - |
|
574 | - /** |
|
575 | - * Sets priority |
|
576 | - * Note. Send Now Messengers always override any priority that may be set on a Message. So |
|
577 | - * this method calls the send_now method to verify that. |
|
578 | - * |
|
579 | - * @param int $priority |
|
580 | - */ |
|
581 | - public function set_priority($priority) |
|
582 | - { |
|
583 | - $priority = $this->send_now() ? EEM_Message::priority_high : $priority; |
|
584 | - parent::set('MSG_priority', $priority); |
|
585 | - } |
|
586 | - |
|
587 | - |
|
588 | - /** |
|
589 | - * Overrides parent::set method so we can capture any sets for priority. |
|
590 | - * |
|
591 | - * @see parent::set() for phpdocs |
|
592 | - * @param string $field_name |
|
593 | - * @param mixed $field_value |
|
594 | - * @param bool $use_default |
|
595 | - * @throws EE_Error |
|
596 | - */ |
|
597 | - public function set($field_name, $field_value, $use_default = false) |
|
598 | - { |
|
599 | - if ($field_name === 'MSG_priority') { |
|
600 | - $this->set_priority($field_value); |
|
601 | - } |
|
602 | - parent::set($field_name, $field_value, $use_default); |
|
603 | - } |
|
604 | - |
|
605 | - |
|
606 | - /** |
|
607 | - * @return bool |
|
608 | - * @throws \EE_Error |
|
609 | - */ |
|
610 | - public function send_now() |
|
611 | - { |
|
612 | - $send_now = $this->valid_messenger() && $this->messenger_object()->send_now() ? EEM_Message::priority_high |
|
613 | - : $this->priority(); |
|
614 | - return $send_now === EEM_Message::priority_high ? true : false; |
|
615 | - } |
|
616 | - |
|
617 | - |
|
618 | - /** |
|
619 | - * Gets STS_ID |
|
620 | - * |
|
621 | - * @return string |
|
622 | - */ |
|
623 | - public function STS_ID() |
|
624 | - { |
|
625 | - return $this->get('STS_ID'); |
|
626 | - } |
|
627 | - |
|
628 | - |
|
629 | - /** |
|
630 | - * Sets STS_ID |
|
631 | - * |
|
632 | - * @param string $STS_ID |
|
633 | - */ |
|
634 | - public function set_STS_ID($STS_ID) |
|
635 | - { |
|
636 | - $this->set('STS_ID', $STS_ID); |
|
637 | - } |
|
638 | - |
|
639 | - |
|
640 | - /** |
|
641 | - * Gets created |
|
642 | - * |
|
643 | - * @return string |
|
644 | - */ |
|
645 | - public function created() |
|
646 | - { |
|
647 | - return $this->get('MSG_created'); |
|
648 | - } |
|
649 | - |
|
650 | - |
|
651 | - /** |
|
652 | - * Sets created |
|
653 | - * |
|
654 | - * @param string $created |
|
655 | - */ |
|
656 | - public function set_created($created) |
|
657 | - { |
|
658 | - $this->set('MSG_created', $created); |
|
659 | - } |
|
660 | - |
|
661 | - |
|
662 | - /** |
|
663 | - * Gets modified |
|
664 | - * |
|
665 | - * @return string |
|
666 | - */ |
|
667 | - public function modified() |
|
668 | - { |
|
669 | - return $this->get('MSG_modified'); |
|
670 | - } |
|
671 | - |
|
672 | - |
|
673 | - /** |
|
674 | - * Sets modified |
|
675 | - * |
|
676 | - * @param string $modified |
|
677 | - */ |
|
678 | - public function set_modified($modified) |
|
679 | - { |
|
680 | - $this->set('MSG_modified', $modified); |
|
681 | - } |
|
682 | - |
|
683 | - |
|
684 | - /** |
|
685 | - * Sets generation data for this message. |
|
686 | - * |
|
687 | - * @param mixed $data |
|
688 | - */ |
|
689 | - public function set_generation_data($data) |
|
690 | - { |
|
691 | - $this->set_field_or_extra_meta('MSG_generation_data', $data); |
|
692 | - } |
|
693 | - |
|
694 | - |
|
695 | - /** |
|
696 | - * Returns any set generation data for this message. |
|
697 | - * |
|
698 | - * @return mixed|null |
|
699 | - */ |
|
700 | - public function get_generation_data() |
|
701 | - { |
|
702 | - return $this->get_field_or_extra_meta('MSG_generation_data'); |
|
703 | - } |
|
704 | - |
|
705 | - |
|
706 | - /** |
|
707 | - * Gets any error message. |
|
708 | - * |
|
709 | - * @return mixed|null |
|
710 | - */ |
|
711 | - public function error_message() |
|
712 | - { |
|
713 | - return $this->get_field_or_extra_meta('MSG_error'); |
|
714 | - } |
|
715 | - |
|
716 | - |
|
717 | - /** |
|
718 | - * Sets an error message. |
|
719 | - * |
|
720 | - * @param $message |
|
721 | - * @return bool|int |
|
722 | - */ |
|
723 | - public function set_error_message($message) |
|
724 | - { |
|
725 | - return $this->set_field_or_extra_meta('MSG_error', $message); |
|
726 | - } |
|
727 | - |
|
728 | - |
|
729 | - /** |
|
730 | - * This retrieves the associated template pack with this message. |
|
731 | - * |
|
732 | - * @return EE_Messages_Template_Pack | null |
|
733 | - */ |
|
734 | - public function get_template_pack() |
|
735 | - { |
|
736 | - /** |
|
737 | - * This is deprecated functionality that will be removed eventually but included here now for backward compat. |
|
738 | - */ |
|
739 | - if (! empty($this->template_pack)) { |
|
740 | - return $this->template_pack; |
|
741 | - } |
|
742 | - /** @type EE_Message_Template_Group $grp */ |
|
743 | - $grp = $this->get_first_related('Message_Template_Group'); |
|
744 | - // if no group then let's try to get the first related group by internal messenger and message type (will use global grp). |
|
745 | - if (! $grp instanceof EE_Message_Template_Group) { |
|
746 | - $grp = EEM_Message_Template_Group::instance()->get_one( |
|
747 | - array( |
|
748 | - array( |
|
749 | - 'MTP_messenger' => $this->messenger(), |
|
750 | - 'MTP_message_type' => $this->message_type(), |
|
751 | - 'MTP_is_global' => true, |
|
752 | - ), |
|
753 | - ) |
|
754 | - ); |
|
755 | - } |
|
756 | - |
|
757 | - return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack() : null; |
|
758 | - } |
|
759 | - |
|
760 | - |
|
761 | - /** |
|
762 | - * Retrieves the variation used for generating this message. |
|
763 | - * |
|
764 | - * @return string |
|
765 | - */ |
|
766 | - public function get_template_pack_variation() |
|
767 | - { |
|
768 | - /** |
|
769 | - * This is deprecated functionality that will be removed eventually but included here now for backward compat. |
|
770 | - */ |
|
771 | - if (! empty($this->template_variation)) { |
|
772 | - return $this->template_variation; |
|
773 | - } |
|
774 | - |
|
775 | - /** @type EE_Message_Template_Group $grp */ |
|
776 | - $grp = $this->get_first_related('Message_Template_Group'); |
|
777 | - |
|
778 | - // if no group then let's try to get the first related group by internal messenger and message type (will use global grp). |
|
779 | - if (! $grp instanceof EE_Message_Template_Group) { |
|
780 | - $grp = EEM_Message_Template_Group::instance()->get_one( |
|
781 | - array( |
|
782 | - array( |
|
783 | - 'MTP_messenger' => $this->messenger(), |
|
784 | - 'MTP_message_type' => $this->message_type(), |
|
785 | - 'MTP_is_global' => true, |
|
786 | - ), |
|
787 | - ) |
|
788 | - ); |
|
789 | - } |
|
790 | - |
|
791 | - return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack_variation() : ''; |
|
792 | - } |
|
793 | - |
|
794 | - /** |
|
795 | - * Return the link to the admin details for the object. |
|
796 | - * |
|
797 | - * @return string |
|
798 | - */ |
|
799 | - public function get_admin_details_link() |
|
800 | - { |
|
801 | - EE_Registry::instance()->load_helper('URL'); |
|
802 | - EE_Registry::instance()->load_helper('MSG_Template'); |
|
803 | - switch ($this->STS_ID()) { |
|
804 | - case EEM_Message::status_failed: |
|
805 | - case EEM_Message::status_debug_only: |
|
806 | - return EEH_MSG_Template::generate_error_display_trigger($this); |
|
807 | - break; |
|
808 | - |
|
809 | - case EEM_Message::status_sent: |
|
810 | - return EEH_MSG_Template::generate_browser_trigger($this); |
|
811 | - break; |
|
812 | - |
|
813 | - default: |
|
814 | - return ''; |
|
815 | - } |
|
816 | - } |
|
817 | - |
|
818 | - /** |
|
819 | - * Returns the link to the editor for the object. Sometimes this is the same as the details. |
|
820 | - * |
|
821 | - * @return string |
|
822 | - */ |
|
823 | - public function get_admin_edit_link() |
|
824 | - { |
|
825 | - return $this->get_admin_details_link(); |
|
826 | - } |
|
827 | - |
|
828 | - /** |
|
829 | - * Returns the link to a settings page for the object. |
|
830 | - * |
|
831 | - * @return string |
|
832 | - */ |
|
833 | - public function get_admin_settings_link() |
|
834 | - { |
|
835 | - EE_Registry::instance()->load_helper('URL'); |
|
836 | - return EEH_URL::add_query_args_and_nonce( |
|
837 | - array( |
|
838 | - 'page' => 'espresso_messages', |
|
839 | - 'action' => 'settings', |
|
840 | - ), |
|
841 | - admin_url('admin.php') |
|
842 | - ); |
|
843 | - } |
|
844 | - |
|
845 | - /** |
|
846 | - * Returns the link to the "overview" for the object (typically the "list table" view). |
|
847 | - * |
|
848 | - * @return string |
|
849 | - */ |
|
850 | - public function get_admin_overview_link() |
|
851 | - { |
|
852 | - EE_Registry::instance()->load_helper('URL'); |
|
853 | - return EEH_URL::add_query_args_and_nonce( |
|
854 | - array( |
|
855 | - 'page' => 'espresso_messages', |
|
856 | - 'action' => 'default', |
|
857 | - ), |
|
858 | - admin_url('admin.php') |
|
859 | - ); |
|
860 | - } |
|
861 | - |
|
862 | - |
|
863 | - /** |
|
864 | - * This sets the EEM_Message::status_messenger_executing class on the message and the appropriate error message for |
|
865 | - * it. |
|
866 | - * Note this also SAVES the current message object to the db because it adds an error message to accompany the |
|
867 | - * status. |
|
868 | - * |
|
869 | - */ |
|
870 | - public function set_messenger_is_executing() |
|
871 | - { |
|
872 | - $this->set_STS_ID(EEM_Message::status_messenger_executing); |
|
873 | - $this->set_error_message( |
|
874 | - esc_html__( |
|
875 | - 'A message with this status indicates that there was a problem that occurred while the message was being |
|
13 | + /** |
|
14 | + * @deprecated 4.9.0 Added for backward compat with add-on's |
|
15 | + * @type null |
|
16 | + */ |
|
17 | + public $template_pack; |
|
18 | + |
|
19 | + /** |
|
20 | + * @deprecated 4.9.0 Added for backward compat with add-on's |
|
21 | + * @type null |
|
22 | + */ |
|
23 | + public $template_variation; |
|
24 | + |
|
25 | + /** |
|
26 | + * @deprecated 4.9.0 Added for backward compat with add-on's |
|
27 | + * @type string |
|
28 | + */ |
|
29 | + public $content = ''; |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * @type EE_messenger $_messenger |
|
34 | + */ |
|
35 | + protected $_messenger = null; |
|
36 | + |
|
37 | + /** |
|
38 | + * @type EE_message_type $_message_type |
|
39 | + */ |
|
40 | + protected $_message_type = null; |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * @param array $props_n_values |
|
45 | + * @param string $timezone |
|
46 | + * @param array $date_formats incoming date formats in an array. First value is the date_format, second is time |
|
47 | + * format. |
|
48 | + * @return EE_Message |
|
49 | + */ |
|
50 | + public static function new_instance($props_n_values = array(), $timezone = '', $date_formats = array()) |
|
51 | + { |
|
52 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
53 | + // if object doesn't exist, let's generate a unique token on instantiation so that its available even before saving to db. |
|
54 | + if (! $has_object) { |
|
55 | + EE_Registry::instance()->load_helper('URL'); |
|
56 | + $props_n_values['MSG_token'] = EEH_URL::generate_unique_token(); |
|
57 | + } |
|
58 | + return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats); |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * @param array $props_n_values |
|
64 | + * @param string $timezone |
|
65 | + * @return EE_Message |
|
66 | + */ |
|
67 | + public static function new_instance_from_db($props_n_values = array(), $timezone = '') |
|
68 | + { |
|
69 | + return new self($props_n_values, true, $timezone); |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * Gets MSG_token |
|
75 | + * |
|
76 | + * @return int |
|
77 | + */ |
|
78 | + public function MSG_token() |
|
79 | + { |
|
80 | + return $this->get('MSG_token'); |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Sets MSG_token |
|
86 | + * |
|
87 | + * @param int $MSG_token |
|
88 | + */ |
|
89 | + public function set_MSG_token($MSG_token) |
|
90 | + { |
|
91 | + $this->set('MSG_token', $MSG_token); |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * Gets GRP_ID |
|
97 | + * |
|
98 | + * @return int |
|
99 | + */ |
|
100 | + public function GRP_ID() |
|
101 | + { |
|
102 | + return $this->get('GRP_ID'); |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * Sets GRP_ID |
|
108 | + * |
|
109 | + * @param int $GRP_ID |
|
110 | + */ |
|
111 | + public function set_GRP_ID($GRP_ID) |
|
112 | + { |
|
113 | + $this->set('GRP_ID', $GRP_ID); |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * Gets TXN_ID |
|
119 | + * |
|
120 | + * @return int |
|
121 | + */ |
|
122 | + public function TXN_ID() |
|
123 | + { |
|
124 | + return $this->get('TXN_ID'); |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Sets TXN_ID |
|
130 | + * |
|
131 | + * @param int $TXN_ID |
|
132 | + */ |
|
133 | + public function set_TXN_ID($TXN_ID) |
|
134 | + { |
|
135 | + $this->set('TXN_ID', $TXN_ID); |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * Gets messenger |
|
141 | + * |
|
142 | + * @return string |
|
143 | + */ |
|
144 | + public function messenger() |
|
145 | + { |
|
146 | + return $this->get('MSG_messenger'); |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * Sets messenger |
|
152 | + * |
|
153 | + * @param string $messenger |
|
154 | + */ |
|
155 | + public function set_messenger($messenger) |
|
156 | + { |
|
157 | + $this->set('MSG_messenger', $messenger); |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + /** |
|
162 | + * Returns corresponding messenger object for the set messenger on this message |
|
163 | + * |
|
164 | + * @return EE_messenger | null |
|
165 | + */ |
|
166 | + public function messenger_object() |
|
167 | + { |
|
168 | + return $this->_messenger; |
|
169 | + } |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * Sets messenger |
|
174 | + * |
|
175 | + * @param EE_messenger $messenger |
|
176 | + */ |
|
177 | + public function set_messenger_object(EE_messenger $messenger) |
|
178 | + { |
|
179 | + $this->_messenger = $messenger; |
|
180 | + } |
|
181 | + |
|
182 | + |
|
183 | + /** |
|
184 | + * validates messenger |
|
185 | + * |
|
186 | + * @param bool $throw_exceptions |
|
187 | + * @return bool |
|
188 | + * @throws \EE_Error |
|
189 | + */ |
|
190 | + public function valid_messenger($throw_exceptions = false) |
|
191 | + { |
|
192 | + if ($this->_messenger instanceof EE_messenger) { |
|
193 | + return true; |
|
194 | + } |
|
195 | + if ($throw_exceptions) { |
|
196 | + throw new EE_Error( |
|
197 | + sprintf( |
|
198 | + __( |
|
199 | + 'The "%1$s" messenger set for this message is missing or invalid. Please double-check the spelling and verify that the correct files exist.', |
|
200 | + 'event_espresso' |
|
201 | + ), |
|
202 | + $this->messenger() |
|
203 | + ) |
|
204 | + ); |
|
205 | + } |
|
206 | + return false; |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + /** |
|
211 | + * This returns the set localized label for the messenger on this message. |
|
212 | + * Note, if unable to retrieve the EE_messenger object then will just return the messenger slug saved |
|
213 | + * with this message. |
|
214 | + * |
|
215 | + * @param bool $plural whether to return the plural label or not. |
|
216 | + * @return string |
|
217 | + */ |
|
218 | + public function messenger_label($plural = false) |
|
219 | + { |
|
220 | + $label_type = $plural ? 'plural' : 'singular'; |
|
221 | + $messenger = $this->messenger_object(); |
|
222 | + return $messenger instanceof EE_messenger ? $messenger->label[ $label_type ] : $this->messenger(); |
|
223 | + } |
|
224 | + |
|
225 | + |
|
226 | + /** |
|
227 | + * Gets message_type |
|
228 | + * |
|
229 | + * @return string |
|
230 | + */ |
|
231 | + public function message_type() |
|
232 | + { |
|
233 | + return $this->get('MSG_message_type'); |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * Sets message_type |
|
239 | + * |
|
240 | + * @param string $message_type |
|
241 | + */ |
|
242 | + public function set_message_type($message_type) |
|
243 | + { |
|
244 | + $this->set('MSG_message_type', $message_type); |
|
245 | + } |
|
246 | + |
|
247 | + |
|
248 | + /** |
|
249 | + * Returns the message type object for the set message type on this message |
|
250 | + * |
|
251 | + * @return EE_message_type | null |
|
252 | + */ |
|
253 | + public function message_type_object() |
|
254 | + { |
|
255 | + return $this->_message_type; |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * Sets message_type |
|
261 | + * |
|
262 | + * @param EE_message_type $message_type |
|
263 | + * @param bool $set_priority This indicates whether to set the priority to whatever the priority is on |
|
264 | + * the message type or not. |
|
265 | + */ |
|
266 | + public function set_message_type_object(EE_message_type $message_type, $set_priority = false) |
|
267 | + { |
|
268 | + $this->_message_type = $message_type; |
|
269 | + if ($set_priority) { |
|
270 | + $this->set_priority($this->_message_type->get_priority()); |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + |
|
275 | + /** |
|
276 | + * validates message_type |
|
277 | + * |
|
278 | + * @param bool $throw_exceptions |
|
279 | + * @return bool |
|
280 | + * @throws \EE_Error |
|
281 | + */ |
|
282 | + public function valid_message_type($throw_exceptions = false) |
|
283 | + { |
|
284 | + if ($this->_message_type instanceof EE_message_type) { |
|
285 | + return true; |
|
286 | + } |
|
287 | + if ($throw_exceptions) { |
|
288 | + throw new EE_Error( |
|
289 | + sprintf( |
|
290 | + __( |
|
291 | + 'The %1$s message type set for this message is missing or invalid. Please double-check the spelling and verify that the correct files exist.', |
|
292 | + 'event_espresso' |
|
293 | + ), |
|
294 | + $this->message_type() |
|
295 | + ) |
|
296 | + ); |
|
297 | + } |
|
298 | + return false; |
|
299 | + } |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * validates messenger and message_type (that they are valid EE_messenger and EE_message_type objects). |
|
304 | + * |
|
305 | + * @param bool $throw_exceptions |
|
306 | + * @return bool |
|
307 | + * @throws \EE_Error |
|
308 | + */ |
|
309 | + public function is_valid($throw_exceptions = false) |
|
310 | + { |
|
311 | + if ($this->valid_messenger($throw_exceptions) && $this->valid_message_type($throw_exceptions)) { |
|
312 | + return true; |
|
313 | + } |
|
314 | + return false; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /** |
|
319 | + * This validates whether the internal messenger and message type objects are valid for sending. |
|
320 | + * Three checks are done: |
|
321 | + * 1. There is a valid messenger object. |
|
322 | + * 2. There is a valid message type object. |
|
323 | + * 3. The message type object is active for the messenger. |
|
324 | + * |
|
325 | + * @throws EE_Error But only if $throw_exceptions is set to true. |
|
326 | + * @param bool $throw_exceptions |
|
327 | + * @return bool |
|
328 | + */ |
|
329 | + public function is_valid_for_sending_or_generation($throw_exceptions = false) |
|
330 | + { |
|
331 | + $valid = false; |
|
332 | + if ($this->is_valid($throw_exceptions)) { |
|
333 | + /** @var EE_Message_Resource_Manager $message_resource_manager */ |
|
334 | + $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
335 | + $valid = $message_resource_manager->is_message_type_active_for_messenger( |
|
336 | + $this->messenger(), |
|
337 | + $this->message_type() |
|
338 | + ); |
|
339 | + if (! $valid && $throw_exceptions) { |
|
340 | + throw new EE_Error( |
|
341 | + sprintf( |
|
342 | + __( |
|
343 | + 'The %1$s message type is not a valid message type for the %2$s messenger so it will not be sent.', |
|
344 | + 'event_espresso' |
|
345 | + ), |
|
346 | + $this->message_type(), |
|
347 | + $this->messenger() |
|
348 | + ) |
|
349 | + ); |
|
350 | + } |
|
351 | + } |
|
352 | + return $valid; |
|
353 | + } |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * This returns the set localized label for the message type on this message. |
|
358 | + * Note, if unable to retrieve the EE_message_type object then will just return the message type slug saved |
|
359 | + * with this message. |
|
360 | + * |
|
361 | + * @param bool $plural whether to return the plural label or not. |
|
362 | + * @return string |
|
363 | + */ |
|
364 | + public function message_type_label($plural = false) |
|
365 | + { |
|
366 | + $label_type = $plural ? 'plural' : 'singular'; |
|
367 | + $message_type = $this->message_type_object(); |
|
368 | + return $message_type instanceof EE_message_type |
|
369 | + ? $message_type->label[ $label_type ] |
|
370 | + : str_replace( |
|
371 | + '_', |
|
372 | + ' ', |
|
373 | + $this->message_type() |
|
374 | + ); |
|
375 | + } |
|
376 | + |
|
377 | + |
|
378 | + /** |
|
379 | + * Gets context |
|
380 | + * |
|
381 | + * @return string |
|
382 | + */ |
|
383 | + public function context() |
|
384 | + { |
|
385 | + return $this->get('MSG_context'); |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + /** |
|
390 | + * This returns the corresponding localized label for the given context slug, if possible from installed message |
|
391 | + * types. Otherwise, this will just return the set context slug on this object. |
|
392 | + * |
|
393 | + * @return string |
|
394 | + */ |
|
395 | + public function context_label() |
|
396 | + { |
|
397 | + /** @type EE_Message_Resource_Manager $message_resource_manager */ |
|
398 | + $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
399 | + $contexts = $message_resource_manager->get_all_contexts(); |
|
400 | + return isset($contexts[ $this->context() ]) ? $contexts[ $this->context() ] : $this->context(); |
|
401 | + } |
|
402 | + |
|
403 | + |
|
404 | + /** |
|
405 | + * Sets context |
|
406 | + * |
|
407 | + * @param string $context |
|
408 | + */ |
|
409 | + public function set_context($context) |
|
410 | + { |
|
411 | + $this->set('MSG_context', $context); |
|
412 | + } |
|
413 | + |
|
414 | + |
|
415 | + /** |
|
416 | + * Gets recipient_ID |
|
417 | + * |
|
418 | + * @return int |
|
419 | + */ |
|
420 | + public function recipient_ID() |
|
421 | + { |
|
422 | + return $this->get('MSG_recipient_ID'); |
|
423 | + } |
|
424 | + |
|
425 | + |
|
426 | + /** |
|
427 | + * Sets recipient_ID |
|
428 | + * |
|
429 | + * @param string $recipient_ID |
|
430 | + */ |
|
431 | + public function set_recipient_ID($recipient_ID) |
|
432 | + { |
|
433 | + $this->set('MSG_recipient_ID', $recipient_ID); |
|
434 | + } |
|
435 | + |
|
436 | + |
|
437 | + /** |
|
438 | + * Gets recipient_type |
|
439 | + * |
|
440 | + * @return string |
|
441 | + */ |
|
442 | + public function recipient_type() |
|
443 | + { |
|
444 | + return $this->get('MSG_recipient_type'); |
|
445 | + } |
|
446 | + |
|
447 | + |
|
448 | + /** |
|
449 | + * Return the related object matching the recipient type and ID. |
|
450 | + * |
|
451 | + * @return EE_Base_Class | null |
|
452 | + */ |
|
453 | + public function recipient_object() |
|
454 | + { |
|
455 | + if (! $this->recipient_type() || ! $this->recipient_ID()) { |
|
456 | + return null; |
|
457 | + } |
|
458 | + |
|
459 | + return $this->get_first_related($this->recipient_type()); |
|
460 | + } |
|
461 | + |
|
462 | + |
|
463 | + /** |
|
464 | + * Sets recipient_type |
|
465 | + * |
|
466 | + * @param string $recipient_type |
|
467 | + */ |
|
468 | + public function set_recipient_type($recipient_type) |
|
469 | + { |
|
470 | + $this->set('MSG_recipient_type', $recipient_type); |
|
471 | + } |
|
472 | + |
|
473 | + |
|
474 | + /** |
|
475 | + * Gets content |
|
476 | + * |
|
477 | + * @return string |
|
478 | + */ |
|
479 | + public function content() |
|
480 | + { |
|
481 | + return $this->get('MSG_content'); |
|
482 | + } |
|
483 | + |
|
484 | + |
|
485 | + /** |
|
486 | + * Sets content |
|
487 | + * |
|
488 | + * @param string $content |
|
489 | + */ |
|
490 | + public function set_content($content) |
|
491 | + { |
|
492 | + $this->set('MSG_content', $content); |
|
493 | + } |
|
494 | + |
|
495 | + |
|
496 | + /** |
|
497 | + * Gets subject |
|
498 | + * |
|
499 | + * @return string |
|
500 | + */ |
|
501 | + public function subject() |
|
502 | + { |
|
503 | + return $this->get('MSG_subject'); |
|
504 | + } |
|
505 | + |
|
506 | + |
|
507 | + /** |
|
508 | + * Sets subject |
|
509 | + * |
|
510 | + * @param string $subject |
|
511 | + */ |
|
512 | + public function set_subject($subject) |
|
513 | + { |
|
514 | + $this->set('MSG_subject', $subject); |
|
515 | + } |
|
516 | + |
|
517 | + |
|
518 | + /** |
|
519 | + * Gets to |
|
520 | + * |
|
521 | + * @return string |
|
522 | + */ |
|
523 | + public function to() |
|
524 | + { |
|
525 | + $to = $this->get('MSG_to'); |
|
526 | + return empty($to) ? __('No recipient', 'event_espresso') : $to; |
|
527 | + } |
|
528 | + |
|
529 | + |
|
530 | + /** |
|
531 | + * Sets to |
|
532 | + * |
|
533 | + * @param string $to |
|
534 | + */ |
|
535 | + public function set_to($to) |
|
536 | + { |
|
537 | + $this->set('MSG_to', $to); |
|
538 | + } |
|
539 | + |
|
540 | + |
|
541 | + /** |
|
542 | + * Gets from |
|
543 | + * |
|
544 | + * @return string |
|
545 | + */ |
|
546 | + public function from() |
|
547 | + { |
|
548 | + return $this->get('MSG_from'); |
|
549 | + } |
|
550 | + |
|
551 | + |
|
552 | + /** |
|
553 | + * Sets from |
|
554 | + * |
|
555 | + * @param string $from |
|
556 | + */ |
|
557 | + public function set_from($from) |
|
558 | + { |
|
559 | + $this->set('MSG_from', $from); |
|
560 | + } |
|
561 | + |
|
562 | + |
|
563 | + /** |
|
564 | + * Gets priority |
|
565 | + * |
|
566 | + * @return int |
|
567 | + */ |
|
568 | + public function priority() |
|
569 | + { |
|
570 | + return $this->get('MSG_priority'); |
|
571 | + } |
|
572 | + |
|
573 | + |
|
574 | + /** |
|
575 | + * Sets priority |
|
576 | + * Note. Send Now Messengers always override any priority that may be set on a Message. So |
|
577 | + * this method calls the send_now method to verify that. |
|
578 | + * |
|
579 | + * @param int $priority |
|
580 | + */ |
|
581 | + public function set_priority($priority) |
|
582 | + { |
|
583 | + $priority = $this->send_now() ? EEM_Message::priority_high : $priority; |
|
584 | + parent::set('MSG_priority', $priority); |
|
585 | + } |
|
586 | + |
|
587 | + |
|
588 | + /** |
|
589 | + * Overrides parent::set method so we can capture any sets for priority. |
|
590 | + * |
|
591 | + * @see parent::set() for phpdocs |
|
592 | + * @param string $field_name |
|
593 | + * @param mixed $field_value |
|
594 | + * @param bool $use_default |
|
595 | + * @throws EE_Error |
|
596 | + */ |
|
597 | + public function set($field_name, $field_value, $use_default = false) |
|
598 | + { |
|
599 | + if ($field_name === 'MSG_priority') { |
|
600 | + $this->set_priority($field_value); |
|
601 | + } |
|
602 | + parent::set($field_name, $field_value, $use_default); |
|
603 | + } |
|
604 | + |
|
605 | + |
|
606 | + /** |
|
607 | + * @return bool |
|
608 | + * @throws \EE_Error |
|
609 | + */ |
|
610 | + public function send_now() |
|
611 | + { |
|
612 | + $send_now = $this->valid_messenger() && $this->messenger_object()->send_now() ? EEM_Message::priority_high |
|
613 | + : $this->priority(); |
|
614 | + return $send_now === EEM_Message::priority_high ? true : false; |
|
615 | + } |
|
616 | + |
|
617 | + |
|
618 | + /** |
|
619 | + * Gets STS_ID |
|
620 | + * |
|
621 | + * @return string |
|
622 | + */ |
|
623 | + public function STS_ID() |
|
624 | + { |
|
625 | + return $this->get('STS_ID'); |
|
626 | + } |
|
627 | + |
|
628 | + |
|
629 | + /** |
|
630 | + * Sets STS_ID |
|
631 | + * |
|
632 | + * @param string $STS_ID |
|
633 | + */ |
|
634 | + public function set_STS_ID($STS_ID) |
|
635 | + { |
|
636 | + $this->set('STS_ID', $STS_ID); |
|
637 | + } |
|
638 | + |
|
639 | + |
|
640 | + /** |
|
641 | + * Gets created |
|
642 | + * |
|
643 | + * @return string |
|
644 | + */ |
|
645 | + public function created() |
|
646 | + { |
|
647 | + return $this->get('MSG_created'); |
|
648 | + } |
|
649 | + |
|
650 | + |
|
651 | + /** |
|
652 | + * Sets created |
|
653 | + * |
|
654 | + * @param string $created |
|
655 | + */ |
|
656 | + public function set_created($created) |
|
657 | + { |
|
658 | + $this->set('MSG_created', $created); |
|
659 | + } |
|
660 | + |
|
661 | + |
|
662 | + /** |
|
663 | + * Gets modified |
|
664 | + * |
|
665 | + * @return string |
|
666 | + */ |
|
667 | + public function modified() |
|
668 | + { |
|
669 | + return $this->get('MSG_modified'); |
|
670 | + } |
|
671 | + |
|
672 | + |
|
673 | + /** |
|
674 | + * Sets modified |
|
675 | + * |
|
676 | + * @param string $modified |
|
677 | + */ |
|
678 | + public function set_modified($modified) |
|
679 | + { |
|
680 | + $this->set('MSG_modified', $modified); |
|
681 | + } |
|
682 | + |
|
683 | + |
|
684 | + /** |
|
685 | + * Sets generation data for this message. |
|
686 | + * |
|
687 | + * @param mixed $data |
|
688 | + */ |
|
689 | + public function set_generation_data($data) |
|
690 | + { |
|
691 | + $this->set_field_or_extra_meta('MSG_generation_data', $data); |
|
692 | + } |
|
693 | + |
|
694 | + |
|
695 | + /** |
|
696 | + * Returns any set generation data for this message. |
|
697 | + * |
|
698 | + * @return mixed|null |
|
699 | + */ |
|
700 | + public function get_generation_data() |
|
701 | + { |
|
702 | + return $this->get_field_or_extra_meta('MSG_generation_data'); |
|
703 | + } |
|
704 | + |
|
705 | + |
|
706 | + /** |
|
707 | + * Gets any error message. |
|
708 | + * |
|
709 | + * @return mixed|null |
|
710 | + */ |
|
711 | + public function error_message() |
|
712 | + { |
|
713 | + return $this->get_field_or_extra_meta('MSG_error'); |
|
714 | + } |
|
715 | + |
|
716 | + |
|
717 | + /** |
|
718 | + * Sets an error message. |
|
719 | + * |
|
720 | + * @param $message |
|
721 | + * @return bool|int |
|
722 | + */ |
|
723 | + public function set_error_message($message) |
|
724 | + { |
|
725 | + return $this->set_field_or_extra_meta('MSG_error', $message); |
|
726 | + } |
|
727 | + |
|
728 | + |
|
729 | + /** |
|
730 | + * This retrieves the associated template pack with this message. |
|
731 | + * |
|
732 | + * @return EE_Messages_Template_Pack | null |
|
733 | + */ |
|
734 | + public function get_template_pack() |
|
735 | + { |
|
736 | + /** |
|
737 | + * This is deprecated functionality that will be removed eventually but included here now for backward compat. |
|
738 | + */ |
|
739 | + if (! empty($this->template_pack)) { |
|
740 | + return $this->template_pack; |
|
741 | + } |
|
742 | + /** @type EE_Message_Template_Group $grp */ |
|
743 | + $grp = $this->get_first_related('Message_Template_Group'); |
|
744 | + // if no group then let's try to get the first related group by internal messenger and message type (will use global grp). |
|
745 | + if (! $grp instanceof EE_Message_Template_Group) { |
|
746 | + $grp = EEM_Message_Template_Group::instance()->get_one( |
|
747 | + array( |
|
748 | + array( |
|
749 | + 'MTP_messenger' => $this->messenger(), |
|
750 | + 'MTP_message_type' => $this->message_type(), |
|
751 | + 'MTP_is_global' => true, |
|
752 | + ), |
|
753 | + ) |
|
754 | + ); |
|
755 | + } |
|
756 | + |
|
757 | + return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack() : null; |
|
758 | + } |
|
759 | + |
|
760 | + |
|
761 | + /** |
|
762 | + * Retrieves the variation used for generating this message. |
|
763 | + * |
|
764 | + * @return string |
|
765 | + */ |
|
766 | + public function get_template_pack_variation() |
|
767 | + { |
|
768 | + /** |
|
769 | + * This is deprecated functionality that will be removed eventually but included here now for backward compat. |
|
770 | + */ |
|
771 | + if (! empty($this->template_variation)) { |
|
772 | + return $this->template_variation; |
|
773 | + } |
|
774 | + |
|
775 | + /** @type EE_Message_Template_Group $grp */ |
|
776 | + $grp = $this->get_first_related('Message_Template_Group'); |
|
777 | + |
|
778 | + // if no group then let's try to get the first related group by internal messenger and message type (will use global grp). |
|
779 | + if (! $grp instanceof EE_Message_Template_Group) { |
|
780 | + $grp = EEM_Message_Template_Group::instance()->get_one( |
|
781 | + array( |
|
782 | + array( |
|
783 | + 'MTP_messenger' => $this->messenger(), |
|
784 | + 'MTP_message_type' => $this->message_type(), |
|
785 | + 'MTP_is_global' => true, |
|
786 | + ), |
|
787 | + ) |
|
788 | + ); |
|
789 | + } |
|
790 | + |
|
791 | + return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack_variation() : ''; |
|
792 | + } |
|
793 | + |
|
794 | + /** |
|
795 | + * Return the link to the admin details for the object. |
|
796 | + * |
|
797 | + * @return string |
|
798 | + */ |
|
799 | + public function get_admin_details_link() |
|
800 | + { |
|
801 | + EE_Registry::instance()->load_helper('URL'); |
|
802 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
803 | + switch ($this->STS_ID()) { |
|
804 | + case EEM_Message::status_failed: |
|
805 | + case EEM_Message::status_debug_only: |
|
806 | + return EEH_MSG_Template::generate_error_display_trigger($this); |
|
807 | + break; |
|
808 | + |
|
809 | + case EEM_Message::status_sent: |
|
810 | + return EEH_MSG_Template::generate_browser_trigger($this); |
|
811 | + break; |
|
812 | + |
|
813 | + default: |
|
814 | + return ''; |
|
815 | + } |
|
816 | + } |
|
817 | + |
|
818 | + /** |
|
819 | + * Returns the link to the editor for the object. Sometimes this is the same as the details. |
|
820 | + * |
|
821 | + * @return string |
|
822 | + */ |
|
823 | + public function get_admin_edit_link() |
|
824 | + { |
|
825 | + return $this->get_admin_details_link(); |
|
826 | + } |
|
827 | + |
|
828 | + /** |
|
829 | + * Returns the link to a settings page for the object. |
|
830 | + * |
|
831 | + * @return string |
|
832 | + */ |
|
833 | + public function get_admin_settings_link() |
|
834 | + { |
|
835 | + EE_Registry::instance()->load_helper('URL'); |
|
836 | + return EEH_URL::add_query_args_and_nonce( |
|
837 | + array( |
|
838 | + 'page' => 'espresso_messages', |
|
839 | + 'action' => 'settings', |
|
840 | + ), |
|
841 | + admin_url('admin.php') |
|
842 | + ); |
|
843 | + } |
|
844 | + |
|
845 | + /** |
|
846 | + * Returns the link to the "overview" for the object (typically the "list table" view). |
|
847 | + * |
|
848 | + * @return string |
|
849 | + */ |
|
850 | + public function get_admin_overview_link() |
|
851 | + { |
|
852 | + EE_Registry::instance()->load_helper('URL'); |
|
853 | + return EEH_URL::add_query_args_and_nonce( |
|
854 | + array( |
|
855 | + 'page' => 'espresso_messages', |
|
856 | + 'action' => 'default', |
|
857 | + ), |
|
858 | + admin_url('admin.php') |
|
859 | + ); |
|
860 | + } |
|
861 | + |
|
862 | + |
|
863 | + /** |
|
864 | + * This sets the EEM_Message::status_messenger_executing class on the message and the appropriate error message for |
|
865 | + * it. |
|
866 | + * Note this also SAVES the current message object to the db because it adds an error message to accompany the |
|
867 | + * status. |
|
868 | + * |
|
869 | + */ |
|
870 | + public function set_messenger_is_executing() |
|
871 | + { |
|
872 | + $this->set_STS_ID(EEM_Message::status_messenger_executing); |
|
873 | + $this->set_error_message( |
|
874 | + esc_html__( |
|
875 | + 'A message with this status indicates that there was a problem that occurred while the message was being |
|
876 | 876 | processed by the messenger. It is still possible that the message was sent successfully, but at some |
877 | 877 | point during the processing there was a failure. This usually is indicative of a timeout issue with PHP |
878 | 878 | or memory limits being reached. If you see this repeatedly you may want to consider upgrading the memory |
879 | 879 | available to PHP on your server.', |
880 | - 'event_espresso' |
|
881 | - ) |
|
882 | - ); |
|
883 | - } |
|
880 | + 'event_espresso' |
|
881 | + ) |
|
882 | + ); |
|
883 | + } |
|
884 | 884 | } |
@@ -603,7 +603,7 @@ |
||
603 | 603 | * @param EE_Message $message |
604 | 604 | * @param EE_messenger $messenger |
605 | 605 | * @param EE_message_type $message_type |
606 | - * @param $test_send |
|
606 | + * @param boolean $test_send |
|
607 | 607 | * @return bool true means all went well, false means, not so much. |
608 | 608 | */ |
609 | 609 | protected function _do_preview( |
@@ -171,9 +171,9 @@ discard block |
||
171 | 171 | 'order_by' => $this->_get_priority_orderby(), |
172 | 172 | 'limit' => $this->_batch_count, |
173 | 173 | ); |
174 | - $messages = EEM_Message::instance()->get_all($query_args); |
|
174 | + $messages = EEM_Message::instance()->get_all($query_args); |
|
175 | 175 | |
176 | - if (! $messages) { |
|
176 | + if ( ! $messages) { |
|
177 | 177 | return false; // nothing to generate |
178 | 178 | } |
179 | 179 | |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | |
228 | 228 | |
229 | 229 | // any to send? |
230 | - if (! $messages_to_send) { |
|
230 | + if ( ! $messages_to_send) { |
|
231 | 231 | $this->unlock_queue(EE_Messages_Queue::action_sending); |
232 | 232 | return false; |
233 | 233 | } |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | */ |
284 | 284 | protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
285 | 285 | { |
286 | - return '_ee_lock_' . $type; |
|
286 | + return '_ee_lock_'.$type; |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | */ |
356 | 356 | public function is_locked($type = EE_Messages_Queue::action_generating) |
357 | 357 | { |
358 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
358 | + if ( ! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
359 | 359 | return true; |
360 | 360 | } |
361 | 361 | $lock = (int) get_option($this->_get_lock_key($type), 0); |
@@ -505,7 +505,7 @@ discard block |
||
505 | 505 | /** @type EE_Message $message */ |
506 | 506 | $message = $this->_message_repository->current(); |
507 | 507 | // only process things that are queued for sending |
508 | - if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
508 | + if ( ! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
509 | 509 | $this->_message_repository->next(); |
510 | 510 | continue; |
511 | 511 | } |
@@ -515,13 +515,13 @@ discard block |
||
515 | 515 | continue; |
516 | 516 | } |
517 | 517 | // error checking |
518 | - if (! $message->valid_messenger()) { |
|
518 | + if ( ! $message->valid_messenger()) { |
|
519 | 519 | $error_messages[] = sprintf( |
520 | 520 | __('The %s messenger is not active at time of sending.', 'event_espresso'), |
521 | 521 | $message->messenger() |
522 | 522 | ); |
523 | 523 | } |
524 | - if (! $message->valid_message_type()) { |
|
524 | + if ( ! $message->valid_message_type()) { |
|
525 | 525 | $error_messages[] = sprintf( |
526 | 526 | __('The %s message type is not active at the time of sending.', 'event_espresso'), |
527 | 527 | $message->message_type() |
@@ -582,9 +582,9 @@ discard block |
||
582 | 582 | // send using messenger, but double check objects |
583 | 583 | if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
584 | 584 | // set hook for message type (but only if not using another messenger to send). |
585 | - if (! isset($this->_did_hook[ $message_type->name ])) { |
|
585 | + if ( ! isset($this->_did_hook[$message_type->name])) { |
|
586 | 586 | $message_type->do_messenger_hooks($messenger); |
587 | - $this->_did_hook[ $message_type->name ] = 1; |
|
587 | + $this->_did_hook[$message_type->name] = 1; |
|
588 | 588 | } |
589 | 589 | // if preview then use preview method |
590 | 590 | return $this->_message_repository->is_preview() |
@@ -635,7 +635,7 @@ discard block |
||
635 | 635 | $test_send |
636 | 636 | ) { |
637 | 637 | if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
638 | - if (! $test_send) { |
|
638 | + if ( ! $test_send) { |
|
639 | 639 | $message->set_content($preview); |
640 | 640 | } |
641 | 641 | $message->set_STS_ID(EEM_Message::status_sent); |
@@ -700,7 +700,7 @@ discard block |
||
700 | 700 | } |
701 | 701 | if (count($error_messages) > 0) { |
702 | 702 | $msg = __('Message was not executed successfully.', 'event_espresso'); |
703 | - $msg = $msg . "\n" . implode("\n", $error_messages); |
|
703 | + $msg = $msg."\n".implode("\n", $error_messages); |
|
704 | 704 | $message->set_error_message($msg); |
705 | 705 | } |
706 | 706 | } |
@@ -15,699 +15,699 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * @type string reference for sending action |
|
20 | - */ |
|
21 | - const action_sending = 'sending'; |
|
22 | - |
|
23 | - /** |
|
24 | - * @type string reference for generation action |
|
25 | - */ |
|
26 | - const action_generating = 'generation'; |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * @type EE_Message_Repository $_message_repository |
|
31 | - */ |
|
32 | - protected $_message_repository; |
|
33 | - |
|
34 | - /** |
|
35 | - * Sets the limit of how many messages are generated per process. |
|
36 | - * |
|
37 | - * @type int |
|
38 | - */ |
|
39 | - protected $_batch_count; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * This is an array of cached queue items being stored in this object. |
|
44 | - * The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
|
45 | - * is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
|
46 | - * |
|
47 | - * @type EE_Message[] |
|
48 | - */ |
|
49 | - protected $_cached_queue_items; |
|
50 | - |
|
51 | - /** |
|
52 | - * Tracks the number of unsaved queue items. |
|
53 | - * |
|
54 | - * @type int |
|
55 | - */ |
|
56 | - protected $_unsaved_count = 0; |
|
57 | - |
|
58 | - /** |
|
59 | - * used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
|
60 | - * hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
|
61 | - * |
|
62 | - * @type array |
|
63 | - */ |
|
64 | - protected $_did_hook = array(); |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Constructor. |
|
69 | - * Setup all the initial properties and load a EE_Message_Repository. |
|
70 | - * |
|
71 | - * @param \EE_Message_Repository $message_repository |
|
72 | - */ |
|
73 | - public function __construct(EE_Message_Repository $message_repository) |
|
74 | - { |
|
75 | - $this->_batch_count = apply_filters('FHEE__EE_Messages_Queue___batch_count', 50); |
|
76 | - $this->_message_repository = $message_repository; |
|
77 | - } |
|
78 | - |
|
79 | - |
|
80 | - /** |
|
81 | - * Add a EE_Message object to the queue |
|
82 | - * |
|
83 | - * @param EE_Message $message |
|
84 | - * @param array $data This will be an array of data to attach to the object in the repository. If the |
|
85 | - * object is persisted, this data will be saved on an extra_meta object related to |
|
86 | - * EE_Message. |
|
87 | - * @param bool $preview Whether this EE_Message represents a preview or not. |
|
88 | - * @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
|
89 | - * use the messenger send method but typically is based on preview data. |
|
90 | - * @return bool Whether the message was successfully added to the repository or not. |
|
91 | - */ |
|
92 | - public function add(EE_Message $message, $data = array(), $preview = false, $test_send = false) |
|
93 | - { |
|
94 | - $data['preview'] = $preview; |
|
95 | - $data['test_send'] = $test_send; |
|
96 | - return $this->_message_repository->add($message, $data); |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
|
102 | - * |
|
103 | - * @param EE_Message $message The message to detach from the queue |
|
104 | - * @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
|
105 | - * @return bool |
|
106 | - */ |
|
107 | - public function remove(EE_Message $message, $persist = false) |
|
108 | - { |
|
109 | - if ($persist && $this->_message_repository->current() !== $message) { |
|
110 | - // get pointer on right message |
|
111 | - if ($this->_message_repository->has($message)) { |
|
112 | - $this->_message_repository->rewind(); |
|
113 | - while ($this->_message_repository->valid()) { |
|
114 | - if ($this->_message_repository->current() === $message) { |
|
115 | - break; |
|
116 | - } |
|
117 | - $this->_message_repository->next(); |
|
118 | - } |
|
119 | - } else { |
|
120 | - return false; |
|
121 | - } |
|
122 | - } |
|
123 | - return $persist ? $this->_message_repository->delete() : $this->_message_repository->remove($message); |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * Persists all queued EE_Message objects to the db. |
|
129 | - * |
|
130 | - * @param bool $do_hooks_only @see EE_Message_Repository::saveAll |
|
131 | - * @return array @see EE_Messages_Repository::saveAll() for return values. |
|
132 | - */ |
|
133 | - public function save($do_hooks_only = false) |
|
134 | - { |
|
135 | - return $this->_message_repository->saveAll($do_hooks_only); |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * @return EE_Message_Repository |
|
141 | - */ |
|
142 | - public function get_message_repository() |
|
143 | - { |
|
144 | - return $this->_message_repository; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * This does the following things: |
|
150 | - * 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return |
|
151 | - * false). |
|
152 | - * 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
|
153 | - * 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
|
154 | - * Note: Callers should make sure they release the lock otherwise batch generation will be prevented from |
|
155 | - * continuing. The lock is on a transient that is set to expire after one hour as a fallback in case locks are not |
|
156 | - * removed. |
|
157 | - * |
|
158 | - * @return bool true if successfully retrieved batch, false no batch ready. |
|
159 | - */ |
|
160 | - public function get_batch_to_generate() |
|
161 | - { |
|
162 | - if ($this->is_locked(EE_Messages_Queue::action_generating)) { |
|
163 | - return false; |
|
164 | - } |
|
165 | - |
|
166 | - // lock batch generation to prevent race conditions. |
|
167 | - $this->lock_queue(EE_Messages_Queue::action_generating); |
|
168 | - |
|
169 | - $query_args = array( |
|
170 | - // key 0 = where conditions |
|
171 | - 0 => array('STS_ID' => EEM_Message::status_incomplete), |
|
172 | - 'order_by' => $this->_get_priority_orderby(), |
|
173 | - 'limit' => $this->_batch_count, |
|
174 | - ); |
|
175 | - $messages = EEM_Message::instance()->get_all($query_args); |
|
176 | - |
|
177 | - if (! $messages) { |
|
178 | - return false; // nothing to generate |
|
179 | - } |
|
180 | - |
|
181 | - foreach ($messages as $message) { |
|
182 | - if ($message instanceof EE_Message) { |
|
183 | - $data = $message->all_extra_meta_array(); |
|
184 | - $this->add($message, $data); |
|
185 | - } |
|
186 | - } |
|
187 | - return true; |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * This does the following things: |
|
193 | - * 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return |
|
194 | - * false). |
|
195 | - * 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then |
|
196 | - * return false. |
|
197 | - * 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
|
198 | - * 3. On success or unsuccessful send, sets status appropriately. |
|
199 | - * 4. Saves messages via the queue |
|
200 | - * 5. Releases lock. |
|
201 | - * |
|
202 | - * @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not |
|
203 | - * necessarily mean that all messages were successfully sent. It just means that this method |
|
204 | - * successfully completed. On true, client may want to call $this->count_STS_in_queue( |
|
205 | - * EEM_Message::status_failed ) to see if any failed EE_Message objects. Each failed message object |
|
206 | - * will also have a saved error message on it to assist with notifying user. |
|
207 | - */ |
|
208 | - public function get_to_send_batch_and_send() |
|
209 | - { |
|
210 | - $rate_limit = $this->get_rate_limit(); |
|
211 | - if ( |
|
212 | - $rate_limit < 1 |
|
213 | - || $this->is_locked(EE_Messages_Queue::action_sending) |
|
214 | - ) { |
|
215 | - return false; |
|
216 | - } |
|
217 | - |
|
218 | - $this->lock_queue(EE_Messages_Queue::action_sending); |
|
219 | - |
|
220 | - $batch = $this->_batch_count < $rate_limit ? $this->_batch_count : $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 | - $queue_count = 0; |
|
239 | - |
|
240 | - // add to queue. |
|
241 | - foreach ($messages_to_send as $message) { |
|
242 | - if ($message instanceof EE_Message) { |
|
243 | - $queue_count++; |
|
244 | - $this->add($message); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - // send messages (this also updates the rate limit) |
|
249 | - $this->execute(); |
|
250 | - |
|
251 | - // release lock |
|
252 | - $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
253 | - // update rate limit |
|
254 | - $this->set_rate_limit($queue_count); |
|
255 | - return true; |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * Locks the queue so that no other queues can call the "batch" methods. |
|
261 | - * |
|
262 | - * @param string $type The type of queue being locked. |
|
263 | - */ |
|
264 | - public function lock_queue($type = EE_Messages_Queue::action_generating) |
|
265 | - { |
|
266 | - update_option($this->_get_lock_key($type), $this->_get_lock_expiry($type)); |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * Unlocks the queue so that batch methods can be used. |
|
272 | - * |
|
273 | - * @param string $type The type of queue being unlocked. |
|
274 | - */ |
|
275 | - public function unlock_queue($type = EE_Messages_Queue::action_generating) |
|
276 | - { |
|
277 | - delete_option($this->_get_lock_key($type)); |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * Retrieve the key used for the lock transient. |
|
283 | - * |
|
284 | - * @param string $type The type of lock. |
|
285 | - * @return string |
|
286 | - */ |
|
287 | - protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
|
288 | - { |
|
289 | - return '_ee_lock_' . $type; |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - /** |
|
294 | - * Retrieve the expiry time for the lock transient. |
|
295 | - * |
|
296 | - * @param string $type The type of lock |
|
297 | - * @return int time to expiry in seconds. |
|
298 | - */ |
|
299 | - protected function _get_lock_expiry($type = EE_Messages_Queue::action_generating) |
|
300 | - { |
|
301 | - return time() + (int) apply_filters('FHEE__EE_Messages_Queue__lock_expiry', HOUR_IN_SECONDS, $type); |
|
302 | - } |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * Returns the key used for rate limit transient. |
|
307 | - * |
|
308 | - * @return string |
|
309 | - */ |
|
310 | - protected function _get_rate_limit_key() |
|
311 | - { |
|
312 | - return '_ee_rate_limit'; |
|
313 | - } |
|
314 | - |
|
315 | - |
|
316 | - /** |
|
317 | - * Returns the rate limit expiry time. |
|
318 | - * |
|
319 | - * @return int |
|
320 | - */ |
|
321 | - protected function _get_rate_limit_expiry() |
|
322 | - { |
|
323 | - return time() + (int) apply_filters('FHEE__EE_Messages_Queue__rate_limit_expiry', HOUR_IN_SECONDS); |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * Returns the default rate limit for sending messages. |
|
329 | - * |
|
330 | - * @return int |
|
331 | - */ |
|
332 | - protected function _default_rate_limit() |
|
333 | - { |
|
334 | - return (int) apply_filters('FHEE__EE_Messages_Queue___rate_limit', 200); |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - /** |
|
339 | - * Return the orderby array for priority. |
|
340 | - * |
|
341 | - * @return array |
|
342 | - */ |
|
343 | - protected function _get_priority_orderby() |
|
344 | - { |
|
345 | - return array( |
|
346 | - 'MSG_priority' => 'ASC', |
|
347 | - 'MSG_modified' => 'DESC', |
|
348 | - ); |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * Returns whether batch methods are "locked" or not, and if models an currently be used to query the database. |
|
354 | - * Return true when batch methods should not be used; returns false when they can be. |
|
355 | - * |
|
356 | - * @param string $type The type of lock being checked for. |
|
357 | - * @return bool |
|
358 | - */ |
|
359 | - public function is_locked($type = EE_Messages_Queue::action_generating) |
|
360 | - { |
|
361 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
362 | - return true; |
|
363 | - } |
|
364 | - $lock = (int) get_option($this->_get_lock_key($type), 0); |
|
365 | - /** |
|
366 | - * This filters the default is_locked behaviour. |
|
367 | - */ |
|
368 | - $is_locked = filter_var( |
|
369 | - apply_filters( |
|
370 | - 'FHEE__EE_Messages_Queue__is_locked', |
|
371 | - $lock > time(), |
|
372 | - $this |
|
373 | - ), |
|
374 | - FILTER_VALIDATE_BOOLEAN |
|
375 | - ); |
|
376 | - |
|
377 | - /** |
|
378 | - * @see usage of this filter in EE_Messages_Queue::initiate_request_by_priority() method. |
|
379 | - * Also implemented here because messages processed on the same request should not have any locks applied. |
|
380 | - */ |
|
381 | - if ( |
|
382 | - apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
383 | - || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
384 | - ) { |
|
385 | - $is_locked = false; |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - return $is_locked; |
|
390 | - } |
|
391 | - |
|
392 | - |
|
393 | - /** |
|
394 | - * Retrieves the rate limit that may be cached as a transient. |
|
395 | - * If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
|
396 | - * |
|
397 | - * @param bool $return_expiry If true then return the expiry time not the rate_limit. |
|
398 | - * @return int |
|
399 | - */ |
|
400 | - protected function get_rate_limit($return_expiry = false) |
|
401 | - { |
|
402 | - $stored_rate_info = get_option($this->_get_rate_limit_key(), array()); |
|
403 | - $rate_limit = isset($stored_rate_info[0]) |
|
404 | - ? (int) $stored_rate_info[0] |
|
405 | - : 0; |
|
406 | - $expiry = isset($stored_rate_info[1]) |
|
407 | - ? (int) $stored_rate_info[1] |
|
408 | - : 0; |
|
409 | - // set the default for tracking? |
|
410 | - if (empty($stored_rate_info) || time() > $expiry) { |
|
411 | - $expiry = $this->_get_rate_limit_expiry(); |
|
412 | - $rate_limit = $this->_default_rate_limit(); |
|
413 | - update_option($this->_get_rate_limit_key(), array($rate_limit, $expiry)); |
|
414 | - } |
|
415 | - return $return_expiry ? $expiry : $rate_limit; |
|
416 | - } |
|
417 | - |
|
418 | - |
|
419 | - /** |
|
420 | - * This updates existing rate limit with the new limit which is the old minus the batch. |
|
421 | - * |
|
422 | - * @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
|
423 | - */ |
|
424 | - protected function set_rate_limit($batch_completed) |
|
425 | - { |
|
426 | - // first get the most up to date rate limit (in case its expired and reset) |
|
427 | - $rate_limit = $this->get_rate_limit(); |
|
428 | - $expiry = $this->get_rate_limit(true); |
|
429 | - $new_limit = $rate_limit - $batch_completed; |
|
430 | - // updating the transient option directly to avoid resetting the expiry. |
|
431 | - |
|
432 | - update_option($this->_get_rate_limit_key(), array($new_limit, $expiry)); |
|
433 | - } |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
|
438 | - * If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
|
439 | - * Note: Keep in mind that there is the possibility that the request will not execute if there is already another |
|
440 | - * request running on a queue for the given task. |
|
441 | - * |
|
442 | - * @param string $task This indicates what type of request is going to be initiated. |
|
443 | - * @param int $priority This indicates the priority that triggers initiating the request. |
|
444 | - */ |
|
445 | - public function initiate_request_by_priority($task = 'generate', $priority = EEM_Message::priority_high) |
|
446 | - { |
|
447 | - // determine what status is matched with the priority as part of the trigger conditions. |
|
448 | - $status = $task == 'generate' |
|
449 | - ? EEM_Message::status_incomplete |
|
450 | - : EEM_Message::instance()->stati_indicating_to_send(); |
|
451 | - // always make sure we save because either this will get executed immediately on a separate request |
|
452 | - // or remains in the queue for the regularly scheduled queue batch. |
|
453 | - $this->save(); |
|
454 | - /** |
|
455 | - * This filter/option allows users to override processing of messages on separate requests and instead have everything |
|
456 | - * happen on the same request. If this is utilized remember: |
|
457 | - * - message priorities don't matter |
|
458 | - * - existing unprocessed messages in the queue will not get processed unless manually triggered. |
|
459 | - * - things will be perceived to take longer to happen for end users (i.e. registrations) because of the additional |
|
460 | - * processing happening on the same request. |
|
461 | - * - any race condition protection (locks) are removed because they don't apply when things are processed on |
|
462 | - * the same request. |
|
463 | - */ |
|
464 | - if ( |
|
465 | - apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
466 | - || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
467 | - ) { |
|
468 | - $messages_processor = EE_Registry::instance()->load_lib('Messages_Processor'); |
|
469 | - if ($messages_processor instanceof EE_Messages_Processor) { |
|
470 | - return $messages_processor->process_immediately_from_queue($this); |
|
471 | - } |
|
472 | - // if we get here then that means the messages processor couldn't be loaded so messages will just remain |
|
473 | - // queued for manual triggering by end user. |
|
474 | - } |
|
475 | - |
|
476 | - if ($this->_message_repository->count_by_priority_and_status($priority, $status)) { |
|
477 | - EE_Messages_Scheduler::initiate_scheduled_non_blocking_request($task); |
|
478 | - } |
|
479 | - } |
|
480 | - |
|
481 | - |
|
482 | - /** |
|
483 | - * Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
|
484 | - * |
|
485 | - * @param bool $save Used to indicate whether to save the message queue after sending |
|
486 | - * (default will save). |
|
487 | - * @param mixed $sending_messenger (optional) When the sending messenger is different than |
|
488 | - * what is on the EE_Message object in the queue. |
|
489 | - * For instance, showing the browser view of an email message, |
|
490 | - * or giving a pdf generated view of an html document. |
|
491 | - * This should be an instance of EE_messenger but if you call this |
|
492 | - * method |
|
493 | - * intending it to be a sending messenger but a valid one could not be |
|
494 | - * retrieved then send in an instance of EE_Error that contains the |
|
495 | - * related error message. |
|
496 | - * @param bool|int $by_priority When set, this indicates that only messages |
|
497 | - * matching the given priority should be executed. |
|
498 | - * @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
|
499 | - * Also, if the messenger is an request type messenger (or a preview), |
|
500 | - * its entirely possible that the messenger will exit before |
|
501 | - */ |
|
502 | - public function execute($save = true, $sending_messenger = null, $by_priority = false) |
|
503 | - { |
|
504 | - $messages_sent = 0; |
|
505 | - $this->_did_hook = array(); |
|
506 | - $this->_message_repository->rewind(); |
|
507 | - |
|
508 | - while ($this->_message_repository->valid()) { |
|
509 | - $error_messages = array(); |
|
510 | - /** @type EE_Message $message */ |
|
511 | - $message = $this->_message_repository->current(); |
|
512 | - // only process things that are queued for sending |
|
513 | - if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
514 | - $this->_message_repository->next(); |
|
515 | - continue; |
|
516 | - } |
|
517 | - // if $by_priority is set and does not match then continue; |
|
518 | - if ($by_priority && $by_priority != $message->priority()) { |
|
519 | - $this->_message_repository->next(); |
|
520 | - continue; |
|
521 | - } |
|
522 | - // error checking |
|
523 | - if (! $message->valid_messenger()) { |
|
524 | - $error_messages[] = sprintf( |
|
525 | - __('The %s messenger is not active at time of sending.', 'event_espresso'), |
|
526 | - $message->messenger() |
|
527 | - ); |
|
528 | - } |
|
529 | - if (! $message->valid_message_type()) { |
|
530 | - $error_messages[] = sprintf( |
|
531 | - __('The %s message type is not active at the time of sending.', 'event_espresso'), |
|
532 | - $message->message_type() |
|
533 | - ); |
|
534 | - } |
|
535 | - // if there was supposed to be a sending messenger for this message, but it was invalid/inactive, |
|
536 | - // then it will instead be an EE_Error object, so let's check for that |
|
537 | - if ($sending_messenger instanceof EE_Error) { |
|
538 | - $error_messages[] = $sending_messenger->getMessage(); |
|
539 | - } |
|
540 | - // if there are no errors, then let's process the message |
|
541 | - if (empty($error_messages)) { |
|
542 | - if ($save) { |
|
543 | - $message->set_messenger_is_executing(); |
|
544 | - } |
|
545 | - if ($this->_process_message($message, $sending_messenger)) { |
|
546 | - $messages_sent++; |
|
547 | - } |
|
548 | - } |
|
549 | - $this->_set_error_message($message, $error_messages); |
|
550 | - // add modified time |
|
551 | - $message->set_modified(time()); |
|
552 | - // we save each message after its processed to make sure its status persists in case PHP times-out or runs |
|
553 | - // out of memory. @see https://events.codebasehq.com/projects/event-espresso/tickets/10281 |
|
554 | - if ($save) { |
|
555 | - $message->save(); |
|
556 | - } |
|
557 | - |
|
558 | - $this->_message_repository->next(); |
|
559 | - } |
|
560 | - if ($save) { |
|
561 | - $this->save(true); |
|
562 | - } |
|
563 | - return $messages_sent; |
|
564 | - } |
|
565 | - |
|
566 | - |
|
567 | - /** |
|
568 | - * _process_message |
|
569 | - * |
|
570 | - * @param EE_Message $message |
|
571 | - * @param mixed $sending_messenger (optional) |
|
572 | - * @return bool |
|
573 | - */ |
|
574 | - protected function _process_message(EE_Message $message, $sending_messenger = null) |
|
575 | - { |
|
576 | - // these *should* have been validated in the execute() method above |
|
577 | - $messenger = $message->messenger_object(); |
|
578 | - $message_type = $message->message_type_object(); |
|
579 | - // do actions for sending messenger if it differs from generating messenger and swap values. |
|
580 | - if ( |
|
581 | - $sending_messenger instanceof EE_messenger |
|
582 | - && $messenger instanceof EE_messenger |
|
583 | - && $sending_messenger->name != $messenger->name |
|
584 | - ) { |
|
585 | - $messenger->do_secondary_messenger_hooks($sending_messenger->name); |
|
586 | - $messenger = $sending_messenger; |
|
587 | - } |
|
588 | - // send using messenger, but double check objects |
|
589 | - if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
|
590 | - // set hook for message type (but only if not using another messenger to send). |
|
591 | - if (! isset($this->_did_hook[ $message_type->name ])) { |
|
592 | - $message_type->do_messenger_hooks($messenger); |
|
593 | - $this->_did_hook[ $message_type->name ] = 1; |
|
594 | - } |
|
595 | - // if preview then use preview method |
|
596 | - return $this->_message_repository->is_preview() |
|
597 | - ? $this->_do_preview($message, $messenger, $message_type, $this->_message_repository->is_test_send()) |
|
598 | - : $this->_do_send($message, $messenger, $message_type); |
|
599 | - } |
|
600 | - return false; |
|
601 | - } |
|
602 | - |
|
603 | - |
|
604 | - /** |
|
605 | - * The intention of this method is to count how many EE_Message objects |
|
606 | - * are in the queue with a given status. |
|
607 | - * Example usage: |
|
608 | - * After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed |
|
609 | - * sends by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
|
610 | - * |
|
611 | - * @param array|string $status Stati to check for in queue |
|
612 | - * @return int Count of EE_Message's matching the given status. |
|
613 | - */ |
|
614 | - public function count_STS_in_queue($status) |
|
615 | - { |
|
616 | - $count = 0; |
|
617 | - $status = is_array($status) ? $status : array($status); |
|
618 | - $this->_message_repository->rewind(); |
|
619 | - foreach ($this->_message_repository as $message) { |
|
620 | - if (in_array($message->STS_ID(), $status)) { |
|
621 | - $count++; |
|
622 | - } |
|
623 | - } |
|
624 | - return $count; |
|
625 | - } |
|
626 | - |
|
627 | - |
|
628 | - /** |
|
629 | - * Executes the get_preview method on the provided messenger. |
|
630 | - * |
|
631 | - * @param EE_Message $message |
|
632 | - * @param EE_messenger $messenger |
|
633 | - * @param EE_message_type $message_type |
|
634 | - * @param $test_send |
|
635 | - * @return bool true means all went well, false means, not so much. |
|
636 | - */ |
|
637 | - protected function _do_preview( |
|
638 | - EE_Message $message, |
|
639 | - EE_messenger $messenger, |
|
640 | - EE_message_type $message_type, |
|
641 | - $test_send |
|
642 | - ) { |
|
643 | - if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
|
644 | - if (! $test_send) { |
|
645 | - $message->set_content($preview); |
|
646 | - } |
|
647 | - $message->set_STS_ID(EEM_Message::status_sent); |
|
648 | - return true; |
|
649 | - } else { |
|
650 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
651 | - return false; |
|
652 | - } |
|
653 | - } |
|
654 | - |
|
655 | - |
|
656 | - /** |
|
657 | - * Executes the send method on the provided messenger |
|
658 | - * EE_Messengers are expected to: |
|
659 | - * - return true if the send was successful. |
|
660 | - * - return false if the send was unsuccessful but can be tried again. |
|
661 | - * - throw an Exception if the send was unsuccessful and cannot be tried again. |
|
662 | - * |
|
663 | - * @param EE_Message $message |
|
664 | - * @param EE_messenger $messenger |
|
665 | - * @param EE_message_type $message_type |
|
666 | - * @return bool true means all went well, false means, not so much. |
|
667 | - */ |
|
668 | - protected function _do_send(EE_Message $message, EE_messenger $messenger, EE_message_type $message_type) |
|
669 | - { |
|
670 | - try { |
|
671 | - if ($messenger->send_message($message, $message_type)) { |
|
672 | - $message->set_STS_ID(EEM_Message::status_sent); |
|
673 | - return true; |
|
674 | - } else { |
|
675 | - $message->set_STS_ID(EEM_Message::status_retry); |
|
676 | - return false; |
|
677 | - } |
|
678 | - } catch (SendMessageException $e) { |
|
679 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
680 | - $message->set_error_message($e->getMessage()); |
|
681 | - return false; |
|
682 | - } |
|
683 | - } |
|
684 | - |
|
685 | - |
|
686 | - /** |
|
687 | - * This sets any necessary error messages on the message object and its status to failed. |
|
688 | - * |
|
689 | - * @param EE_Message $message |
|
690 | - * @param array $error_messages the response from the messenger. |
|
691 | - */ |
|
692 | - protected function _set_error_message(EE_Message $message, $error_messages) |
|
693 | - { |
|
694 | - $error_messages = (array) $error_messages; |
|
695 | - if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_failed_sending())) { |
|
696 | - $notices = EE_Error::has_notices(); |
|
697 | - $error_messages[] = __( |
|
698 | - 'Messenger and Message Type were valid and active, but the messenger send method failed.', |
|
699 | - 'event_espresso' |
|
700 | - ); |
|
701 | - if ($notices === 1) { |
|
702 | - $notices = EE_Error::get_vanilla_notices(); |
|
703 | - $notices['errors'] = isset($notices['errors']) ? $notices['errors'] : array(); |
|
704 | - $error_messages[] = implode("\n", $notices['errors']); |
|
705 | - } |
|
706 | - } |
|
707 | - if (count($error_messages) > 0) { |
|
708 | - $msg = __('Message was not executed successfully.', 'event_espresso'); |
|
709 | - $msg = $msg . "\n" . implode("\n", $error_messages); |
|
710 | - $message->set_error_message($msg); |
|
711 | - } |
|
712 | - } |
|
18 | + /** |
|
19 | + * @type string reference for sending action |
|
20 | + */ |
|
21 | + const action_sending = 'sending'; |
|
22 | + |
|
23 | + /** |
|
24 | + * @type string reference for generation action |
|
25 | + */ |
|
26 | + const action_generating = 'generation'; |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * @type EE_Message_Repository $_message_repository |
|
31 | + */ |
|
32 | + protected $_message_repository; |
|
33 | + |
|
34 | + /** |
|
35 | + * Sets the limit of how many messages are generated per process. |
|
36 | + * |
|
37 | + * @type int |
|
38 | + */ |
|
39 | + protected $_batch_count; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * This is an array of cached queue items being stored in this object. |
|
44 | + * The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
|
45 | + * is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
|
46 | + * |
|
47 | + * @type EE_Message[] |
|
48 | + */ |
|
49 | + protected $_cached_queue_items; |
|
50 | + |
|
51 | + /** |
|
52 | + * Tracks the number of unsaved queue items. |
|
53 | + * |
|
54 | + * @type int |
|
55 | + */ |
|
56 | + protected $_unsaved_count = 0; |
|
57 | + |
|
58 | + /** |
|
59 | + * used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
|
60 | + * hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
|
61 | + * |
|
62 | + * @type array |
|
63 | + */ |
|
64 | + protected $_did_hook = array(); |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Constructor. |
|
69 | + * Setup all the initial properties and load a EE_Message_Repository. |
|
70 | + * |
|
71 | + * @param \EE_Message_Repository $message_repository |
|
72 | + */ |
|
73 | + public function __construct(EE_Message_Repository $message_repository) |
|
74 | + { |
|
75 | + $this->_batch_count = apply_filters('FHEE__EE_Messages_Queue___batch_count', 50); |
|
76 | + $this->_message_repository = $message_repository; |
|
77 | + } |
|
78 | + |
|
79 | + |
|
80 | + /** |
|
81 | + * Add a EE_Message object to the queue |
|
82 | + * |
|
83 | + * @param EE_Message $message |
|
84 | + * @param array $data This will be an array of data to attach to the object in the repository. If the |
|
85 | + * object is persisted, this data will be saved on an extra_meta object related to |
|
86 | + * EE_Message. |
|
87 | + * @param bool $preview Whether this EE_Message represents a preview or not. |
|
88 | + * @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
|
89 | + * use the messenger send method but typically is based on preview data. |
|
90 | + * @return bool Whether the message was successfully added to the repository or not. |
|
91 | + */ |
|
92 | + public function add(EE_Message $message, $data = array(), $preview = false, $test_send = false) |
|
93 | + { |
|
94 | + $data['preview'] = $preview; |
|
95 | + $data['test_send'] = $test_send; |
|
96 | + return $this->_message_repository->add($message, $data); |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
|
102 | + * |
|
103 | + * @param EE_Message $message The message to detach from the queue |
|
104 | + * @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
|
105 | + * @return bool |
|
106 | + */ |
|
107 | + public function remove(EE_Message $message, $persist = false) |
|
108 | + { |
|
109 | + if ($persist && $this->_message_repository->current() !== $message) { |
|
110 | + // get pointer on right message |
|
111 | + if ($this->_message_repository->has($message)) { |
|
112 | + $this->_message_repository->rewind(); |
|
113 | + while ($this->_message_repository->valid()) { |
|
114 | + if ($this->_message_repository->current() === $message) { |
|
115 | + break; |
|
116 | + } |
|
117 | + $this->_message_repository->next(); |
|
118 | + } |
|
119 | + } else { |
|
120 | + return false; |
|
121 | + } |
|
122 | + } |
|
123 | + return $persist ? $this->_message_repository->delete() : $this->_message_repository->remove($message); |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * Persists all queued EE_Message objects to the db. |
|
129 | + * |
|
130 | + * @param bool $do_hooks_only @see EE_Message_Repository::saveAll |
|
131 | + * @return array @see EE_Messages_Repository::saveAll() for return values. |
|
132 | + */ |
|
133 | + public function save($do_hooks_only = false) |
|
134 | + { |
|
135 | + return $this->_message_repository->saveAll($do_hooks_only); |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * @return EE_Message_Repository |
|
141 | + */ |
|
142 | + public function get_message_repository() |
|
143 | + { |
|
144 | + return $this->_message_repository; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * This does the following things: |
|
150 | + * 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return |
|
151 | + * false). |
|
152 | + * 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
|
153 | + * 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
|
154 | + * Note: Callers should make sure they release the lock otherwise batch generation will be prevented from |
|
155 | + * continuing. The lock is on a transient that is set to expire after one hour as a fallback in case locks are not |
|
156 | + * removed. |
|
157 | + * |
|
158 | + * @return bool true if successfully retrieved batch, false no batch ready. |
|
159 | + */ |
|
160 | + public function get_batch_to_generate() |
|
161 | + { |
|
162 | + if ($this->is_locked(EE_Messages_Queue::action_generating)) { |
|
163 | + return false; |
|
164 | + } |
|
165 | + |
|
166 | + // lock batch generation to prevent race conditions. |
|
167 | + $this->lock_queue(EE_Messages_Queue::action_generating); |
|
168 | + |
|
169 | + $query_args = array( |
|
170 | + // key 0 = where conditions |
|
171 | + 0 => array('STS_ID' => EEM_Message::status_incomplete), |
|
172 | + 'order_by' => $this->_get_priority_orderby(), |
|
173 | + 'limit' => $this->_batch_count, |
|
174 | + ); |
|
175 | + $messages = EEM_Message::instance()->get_all($query_args); |
|
176 | + |
|
177 | + if (! $messages) { |
|
178 | + return false; // nothing to generate |
|
179 | + } |
|
180 | + |
|
181 | + foreach ($messages as $message) { |
|
182 | + if ($message instanceof EE_Message) { |
|
183 | + $data = $message->all_extra_meta_array(); |
|
184 | + $this->add($message, $data); |
|
185 | + } |
|
186 | + } |
|
187 | + return true; |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * This does the following things: |
|
193 | + * 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return |
|
194 | + * false). |
|
195 | + * 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then |
|
196 | + * return false. |
|
197 | + * 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
|
198 | + * 3. On success or unsuccessful send, sets status appropriately. |
|
199 | + * 4. Saves messages via the queue |
|
200 | + * 5. Releases lock. |
|
201 | + * |
|
202 | + * @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not |
|
203 | + * necessarily mean that all messages were successfully sent. It just means that this method |
|
204 | + * successfully completed. On true, client may want to call $this->count_STS_in_queue( |
|
205 | + * EEM_Message::status_failed ) to see if any failed EE_Message objects. Each failed message object |
|
206 | + * will also have a saved error message on it to assist with notifying user. |
|
207 | + */ |
|
208 | + public function get_to_send_batch_and_send() |
|
209 | + { |
|
210 | + $rate_limit = $this->get_rate_limit(); |
|
211 | + if ( |
|
212 | + $rate_limit < 1 |
|
213 | + || $this->is_locked(EE_Messages_Queue::action_sending) |
|
214 | + ) { |
|
215 | + return false; |
|
216 | + } |
|
217 | + |
|
218 | + $this->lock_queue(EE_Messages_Queue::action_sending); |
|
219 | + |
|
220 | + $batch = $this->_batch_count < $rate_limit ? $this->_batch_count : $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 | + $queue_count = 0; |
|
239 | + |
|
240 | + // add to queue. |
|
241 | + foreach ($messages_to_send as $message) { |
|
242 | + if ($message instanceof EE_Message) { |
|
243 | + $queue_count++; |
|
244 | + $this->add($message); |
|
245 | + } |
|
246 | + } |
|
247 | + |
|
248 | + // send messages (this also updates the rate limit) |
|
249 | + $this->execute(); |
|
250 | + |
|
251 | + // release lock |
|
252 | + $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
253 | + // update rate limit |
|
254 | + $this->set_rate_limit($queue_count); |
|
255 | + return true; |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * Locks the queue so that no other queues can call the "batch" methods. |
|
261 | + * |
|
262 | + * @param string $type The type of queue being locked. |
|
263 | + */ |
|
264 | + public function lock_queue($type = EE_Messages_Queue::action_generating) |
|
265 | + { |
|
266 | + update_option($this->_get_lock_key($type), $this->_get_lock_expiry($type)); |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * Unlocks the queue so that batch methods can be used. |
|
272 | + * |
|
273 | + * @param string $type The type of queue being unlocked. |
|
274 | + */ |
|
275 | + public function unlock_queue($type = EE_Messages_Queue::action_generating) |
|
276 | + { |
|
277 | + delete_option($this->_get_lock_key($type)); |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * Retrieve the key used for the lock transient. |
|
283 | + * |
|
284 | + * @param string $type The type of lock. |
|
285 | + * @return string |
|
286 | + */ |
|
287 | + protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
|
288 | + { |
|
289 | + return '_ee_lock_' . $type; |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + /** |
|
294 | + * Retrieve the expiry time for the lock transient. |
|
295 | + * |
|
296 | + * @param string $type The type of lock |
|
297 | + * @return int time to expiry in seconds. |
|
298 | + */ |
|
299 | + protected function _get_lock_expiry($type = EE_Messages_Queue::action_generating) |
|
300 | + { |
|
301 | + return time() + (int) apply_filters('FHEE__EE_Messages_Queue__lock_expiry', HOUR_IN_SECONDS, $type); |
|
302 | + } |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * Returns the key used for rate limit transient. |
|
307 | + * |
|
308 | + * @return string |
|
309 | + */ |
|
310 | + protected function _get_rate_limit_key() |
|
311 | + { |
|
312 | + return '_ee_rate_limit'; |
|
313 | + } |
|
314 | + |
|
315 | + |
|
316 | + /** |
|
317 | + * Returns the rate limit expiry time. |
|
318 | + * |
|
319 | + * @return int |
|
320 | + */ |
|
321 | + protected function _get_rate_limit_expiry() |
|
322 | + { |
|
323 | + return time() + (int) apply_filters('FHEE__EE_Messages_Queue__rate_limit_expiry', HOUR_IN_SECONDS); |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * Returns the default rate limit for sending messages. |
|
329 | + * |
|
330 | + * @return int |
|
331 | + */ |
|
332 | + protected function _default_rate_limit() |
|
333 | + { |
|
334 | + return (int) apply_filters('FHEE__EE_Messages_Queue___rate_limit', 200); |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + /** |
|
339 | + * Return the orderby array for priority. |
|
340 | + * |
|
341 | + * @return array |
|
342 | + */ |
|
343 | + protected function _get_priority_orderby() |
|
344 | + { |
|
345 | + return array( |
|
346 | + 'MSG_priority' => 'ASC', |
|
347 | + 'MSG_modified' => 'DESC', |
|
348 | + ); |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * Returns whether batch methods are "locked" or not, and if models an currently be used to query the database. |
|
354 | + * Return true when batch methods should not be used; returns false when they can be. |
|
355 | + * |
|
356 | + * @param string $type The type of lock being checked for. |
|
357 | + * @return bool |
|
358 | + */ |
|
359 | + public function is_locked($type = EE_Messages_Queue::action_generating) |
|
360 | + { |
|
361 | + if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
362 | + return true; |
|
363 | + } |
|
364 | + $lock = (int) get_option($this->_get_lock_key($type), 0); |
|
365 | + /** |
|
366 | + * This filters the default is_locked behaviour. |
|
367 | + */ |
|
368 | + $is_locked = filter_var( |
|
369 | + apply_filters( |
|
370 | + 'FHEE__EE_Messages_Queue__is_locked', |
|
371 | + $lock > time(), |
|
372 | + $this |
|
373 | + ), |
|
374 | + FILTER_VALIDATE_BOOLEAN |
|
375 | + ); |
|
376 | + |
|
377 | + /** |
|
378 | + * @see usage of this filter in EE_Messages_Queue::initiate_request_by_priority() method. |
|
379 | + * Also implemented here because messages processed on the same request should not have any locks applied. |
|
380 | + */ |
|
381 | + if ( |
|
382 | + apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
383 | + || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
384 | + ) { |
|
385 | + $is_locked = false; |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + return $is_locked; |
|
390 | + } |
|
391 | + |
|
392 | + |
|
393 | + /** |
|
394 | + * Retrieves the rate limit that may be cached as a transient. |
|
395 | + * If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
|
396 | + * |
|
397 | + * @param bool $return_expiry If true then return the expiry time not the rate_limit. |
|
398 | + * @return int |
|
399 | + */ |
|
400 | + protected function get_rate_limit($return_expiry = false) |
|
401 | + { |
|
402 | + $stored_rate_info = get_option($this->_get_rate_limit_key(), array()); |
|
403 | + $rate_limit = isset($stored_rate_info[0]) |
|
404 | + ? (int) $stored_rate_info[0] |
|
405 | + : 0; |
|
406 | + $expiry = isset($stored_rate_info[1]) |
|
407 | + ? (int) $stored_rate_info[1] |
|
408 | + : 0; |
|
409 | + // set the default for tracking? |
|
410 | + if (empty($stored_rate_info) || time() > $expiry) { |
|
411 | + $expiry = $this->_get_rate_limit_expiry(); |
|
412 | + $rate_limit = $this->_default_rate_limit(); |
|
413 | + update_option($this->_get_rate_limit_key(), array($rate_limit, $expiry)); |
|
414 | + } |
|
415 | + return $return_expiry ? $expiry : $rate_limit; |
|
416 | + } |
|
417 | + |
|
418 | + |
|
419 | + /** |
|
420 | + * This updates existing rate limit with the new limit which is the old minus the batch. |
|
421 | + * |
|
422 | + * @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
|
423 | + */ |
|
424 | + protected function set_rate_limit($batch_completed) |
|
425 | + { |
|
426 | + // first get the most up to date rate limit (in case its expired and reset) |
|
427 | + $rate_limit = $this->get_rate_limit(); |
|
428 | + $expiry = $this->get_rate_limit(true); |
|
429 | + $new_limit = $rate_limit - $batch_completed; |
|
430 | + // updating the transient option directly to avoid resetting the expiry. |
|
431 | + |
|
432 | + update_option($this->_get_rate_limit_key(), array($new_limit, $expiry)); |
|
433 | + } |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
|
438 | + * If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
|
439 | + * Note: Keep in mind that there is the possibility that the request will not execute if there is already another |
|
440 | + * request running on a queue for the given task. |
|
441 | + * |
|
442 | + * @param string $task This indicates what type of request is going to be initiated. |
|
443 | + * @param int $priority This indicates the priority that triggers initiating the request. |
|
444 | + */ |
|
445 | + public function initiate_request_by_priority($task = 'generate', $priority = EEM_Message::priority_high) |
|
446 | + { |
|
447 | + // determine what status is matched with the priority as part of the trigger conditions. |
|
448 | + $status = $task == 'generate' |
|
449 | + ? EEM_Message::status_incomplete |
|
450 | + : EEM_Message::instance()->stati_indicating_to_send(); |
|
451 | + // always make sure we save because either this will get executed immediately on a separate request |
|
452 | + // or remains in the queue for the regularly scheduled queue batch. |
|
453 | + $this->save(); |
|
454 | + /** |
|
455 | + * This filter/option allows users to override processing of messages on separate requests and instead have everything |
|
456 | + * happen on the same request. If this is utilized remember: |
|
457 | + * - message priorities don't matter |
|
458 | + * - existing unprocessed messages in the queue will not get processed unless manually triggered. |
|
459 | + * - things will be perceived to take longer to happen for end users (i.e. registrations) because of the additional |
|
460 | + * processing happening on the same request. |
|
461 | + * - any race condition protection (locks) are removed because they don't apply when things are processed on |
|
462 | + * the same request. |
|
463 | + */ |
|
464 | + if ( |
|
465 | + apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
466 | + || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
467 | + ) { |
|
468 | + $messages_processor = EE_Registry::instance()->load_lib('Messages_Processor'); |
|
469 | + if ($messages_processor instanceof EE_Messages_Processor) { |
|
470 | + return $messages_processor->process_immediately_from_queue($this); |
|
471 | + } |
|
472 | + // if we get here then that means the messages processor couldn't be loaded so messages will just remain |
|
473 | + // queued for manual triggering by end user. |
|
474 | + } |
|
475 | + |
|
476 | + if ($this->_message_repository->count_by_priority_and_status($priority, $status)) { |
|
477 | + EE_Messages_Scheduler::initiate_scheduled_non_blocking_request($task); |
|
478 | + } |
|
479 | + } |
|
480 | + |
|
481 | + |
|
482 | + /** |
|
483 | + * Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
|
484 | + * |
|
485 | + * @param bool $save Used to indicate whether to save the message queue after sending |
|
486 | + * (default will save). |
|
487 | + * @param mixed $sending_messenger (optional) When the sending messenger is different than |
|
488 | + * what is on the EE_Message object in the queue. |
|
489 | + * For instance, showing the browser view of an email message, |
|
490 | + * or giving a pdf generated view of an html document. |
|
491 | + * This should be an instance of EE_messenger but if you call this |
|
492 | + * method |
|
493 | + * intending it to be a sending messenger but a valid one could not be |
|
494 | + * retrieved then send in an instance of EE_Error that contains the |
|
495 | + * related error message. |
|
496 | + * @param bool|int $by_priority When set, this indicates that only messages |
|
497 | + * matching the given priority should be executed. |
|
498 | + * @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
|
499 | + * Also, if the messenger is an request type messenger (or a preview), |
|
500 | + * its entirely possible that the messenger will exit before |
|
501 | + */ |
|
502 | + public function execute($save = true, $sending_messenger = null, $by_priority = false) |
|
503 | + { |
|
504 | + $messages_sent = 0; |
|
505 | + $this->_did_hook = array(); |
|
506 | + $this->_message_repository->rewind(); |
|
507 | + |
|
508 | + while ($this->_message_repository->valid()) { |
|
509 | + $error_messages = array(); |
|
510 | + /** @type EE_Message $message */ |
|
511 | + $message = $this->_message_repository->current(); |
|
512 | + // only process things that are queued for sending |
|
513 | + if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
514 | + $this->_message_repository->next(); |
|
515 | + continue; |
|
516 | + } |
|
517 | + // if $by_priority is set and does not match then continue; |
|
518 | + if ($by_priority && $by_priority != $message->priority()) { |
|
519 | + $this->_message_repository->next(); |
|
520 | + continue; |
|
521 | + } |
|
522 | + // error checking |
|
523 | + if (! $message->valid_messenger()) { |
|
524 | + $error_messages[] = sprintf( |
|
525 | + __('The %s messenger is not active at time of sending.', 'event_espresso'), |
|
526 | + $message->messenger() |
|
527 | + ); |
|
528 | + } |
|
529 | + if (! $message->valid_message_type()) { |
|
530 | + $error_messages[] = sprintf( |
|
531 | + __('The %s message type is not active at the time of sending.', 'event_espresso'), |
|
532 | + $message->message_type() |
|
533 | + ); |
|
534 | + } |
|
535 | + // if there was supposed to be a sending messenger for this message, but it was invalid/inactive, |
|
536 | + // then it will instead be an EE_Error object, so let's check for that |
|
537 | + if ($sending_messenger instanceof EE_Error) { |
|
538 | + $error_messages[] = $sending_messenger->getMessage(); |
|
539 | + } |
|
540 | + // if there are no errors, then let's process the message |
|
541 | + if (empty($error_messages)) { |
|
542 | + if ($save) { |
|
543 | + $message->set_messenger_is_executing(); |
|
544 | + } |
|
545 | + if ($this->_process_message($message, $sending_messenger)) { |
|
546 | + $messages_sent++; |
|
547 | + } |
|
548 | + } |
|
549 | + $this->_set_error_message($message, $error_messages); |
|
550 | + // add modified time |
|
551 | + $message->set_modified(time()); |
|
552 | + // we save each message after its processed to make sure its status persists in case PHP times-out or runs |
|
553 | + // out of memory. @see https://events.codebasehq.com/projects/event-espresso/tickets/10281 |
|
554 | + if ($save) { |
|
555 | + $message->save(); |
|
556 | + } |
|
557 | + |
|
558 | + $this->_message_repository->next(); |
|
559 | + } |
|
560 | + if ($save) { |
|
561 | + $this->save(true); |
|
562 | + } |
|
563 | + return $messages_sent; |
|
564 | + } |
|
565 | + |
|
566 | + |
|
567 | + /** |
|
568 | + * _process_message |
|
569 | + * |
|
570 | + * @param EE_Message $message |
|
571 | + * @param mixed $sending_messenger (optional) |
|
572 | + * @return bool |
|
573 | + */ |
|
574 | + protected function _process_message(EE_Message $message, $sending_messenger = null) |
|
575 | + { |
|
576 | + // these *should* have been validated in the execute() method above |
|
577 | + $messenger = $message->messenger_object(); |
|
578 | + $message_type = $message->message_type_object(); |
|
579 | + // do actions for sending messenger if it differs from generating messenger and swap values. |
|
580 | + if ( |
|
581 | + $sending_messenger instanceof EE_messenger |
|
582 | + && $messenger instanceof EE_messenger |
|
583 | + && $sending_messenger->name != $messenger->name |
|
584 | + ) { |
|
585 | + $messenger->do_secondary_messenger_hooks($sending_messenger->name); |
|
586 | + $messenger = $sending_messenger; |
|
587 | + } |
|
588 | + // send using messenger, but double check objects |
|
589 | + if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
|
590 | + // set hook for message type (but only if not using another messenger to send). |
|
591 | + if (! isset($this->_did_hook[ $message_type->name ])) { |
|
592 | + $message_type->do_messenger_hooks($messenger); |
|
593 | + $this->_did_hook[ $message_type->name ] = 1; |
|
594 | + } |
|
595 | + // if preview then use preview method |
|
596 | + return $this->_message_repository->is_preview() |
|
597 | + ? $this->_do_preview($message, $messenger, $message_type, $this->_message_repository->is_test_send()) |
|
598 | + : $this->_do_send($message, $messenger, $message_type); |
|
599 | + } |
|
600 | + return false; |
|
601 | + } |
|
602 | + |
|
603 | + |
|
604 | + /** |
|
605 | + * The intention of this method is to count how many EE_Message objects |
|
606 | + * are in the queue with a given status. |
|
607 | + * Example usage: |
|
608 | + * After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed |
|
609 | + * sends by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
|
610 | + * |
|
611 | + * @param array|string $status Stati to check for in queue |
|
612 | + * @return int Count of EE_Message's matching the given status. |
|
613 | + */ |
|
614 | + public function count_STS_in_queue($status) |
|
615 | + { |
|
616 | + $count = 0; |
|
617 | + $status = is_array($status) ? $status : array($status); |
|
618 | + $this->_message_repository->rewind(); |
|
619 | + foreach ($this->_message_repository as $message) { |
|
620 | + if (in_array($message->STS_ID(), $status)) { |
|
621 | + $count++; |
|
622 | + } |
|
623 | + } |
|
624 | + return $count; |
|
625 | + } |
|
626 | + |
|
627 | + |
|
628 | + /** |
|
629 | + * Executes the get_preview method on the provided messenger. |
|
630 | + * |
|
631 | + * @param EE_Message $message |
|
632 | + * @param EE_messenger $messenger |
|
633 | + * @param EE_message_type $message_type |
|
634 | + * @param $test_send |
|
635 | + * @return bool true means all went well, false means, not so much. |
|
636 | + */ |
|
637 | + protected function _do_preview( |
|
638 | + EE_Message $message, |
|
639 | + EE_messenger $messenger, |
|
640 | + EE_message_type $message_type, |
|
641 | + $test_send |
|
642 | + ) { |
|
643 | + if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
|
644 | + if (! $test_send) { |
|
645 | + $message->set_content($preview); |
|
646 | + } |
|
647 | + $message->set_STS_ID(EEM_Message::status_sent); |
|
648 | + return true; |
|
649 | + } else { |
|
650 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
651 | + return false; |
|
652 | + } |
|
653 | + } |
|
654 | + |
|
655 | + |
|
656 | + /** |
|
657 | + * Executes the send method on the provided messenger |
|
658 | + * EE_Messengers are expected to: |
|
659 | + * - return true if the send was successful. |
|
660 | + * - return false if the send was unsuccessful but can be tried again. |
|
661 | + * - throw an Exception if the send was unsuccessful and cannot be tried again. |
|
662 | + * |
|
663 | + * @param EE_Message $message |
|
664 | + * @param EE_messenger $messenger |
|
665 | + * @param EE_message_type $message_type |
|
666 | + * @return bool true means all went well, false means, not so much. |
|
667 | + */ |
|
668 | + protected function _do_send(EE_Message $message, EE_messenger $messenger, EE_message_type $message_type) |
|
669 | + { |
|
670 | + try { |
|
671 | + if ($messenger->send_message($message, $message_type)) { |
|
672 | + $message->set_STS_ID(EEM_Message::status_sent); |
|
673 | + return true; |
|
674 | + } else { |
|
675 | + $message->set_STS_ID(EEM_Message::status_retry); |
|
676 | + return false; |
|
677 | + } |
|
678 | + } catch (SendMessageException $e) { |
|
679 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
680 | + $message->set_error_message($e->getMessage()); |
|
681 | + return false; |
|
682 | + } |
|
683 | + } |
|
684 | + |
|
685 | + |
|
686 | + /** |
|
687 | + * This sets any necessary error messages on the message object and its status to failed. |
|
688 | + * |
|
689 | + * @param EE_Message $message |
|
690 | + * @param array $error_messages the response from the messenger. |
|
691 | + */ |
|
692 | + protected function _set_error_message(EE_Message $message, $error_messages) |
|
693 | + { |
|
694 | + $error_messages = (array) $error_messages; |
|
695 | + if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_failed_sending())) { |
|
696 | + $notices = EE_Error::has_notices(); |
|
697 | + $error_messages[] = __( |
|
698 | + 'Messenger and Message Type were valid and active, but the messenger send method failed.', |
|
699 | + 'event_espresso' |
|
700 | + ); |
|
701 | + if ($notices === 1) { |
|
702 | + $notices = EE_Error::get_vanilla_notices(); |
|
703 | + $notices['errors'] = isset($notices['errors']) ? $notices['errors'] : array(); |
|
704 | + $error_messages[] = implode("\n", $notices['errors']); |
|
705 | + } |
|
706 | + } |
|
707 | + if (count($error_messages) > 0) { |
|
708 | + $msg = __('Message was not executed successfully.', 'event_espresso'); |
|
709 | + $msg = $msg . "\n" . implode("\n", $error_messages); |
|
710 | + $message->set_error_message($msg); |
|
711 | + } |
|
712 | + } |
|
713 | 713 | } |
@@ -70,10 +70,10 @@ |
||
70 | 70 | $model_names = array(); |
71 | 71 | if (is_array($this->_model_name_pointed_to)) { |
72 | 72 | foreach ($this->_model_name_pointed_to as $model_name) { |
73 | - $model_names[] = "EE_" . $model_name; |
|
73 | + $model_names[] = "EE_".$model_name; |
|
74 | 74 | } |
75 | 75 | } else { |
76 | - $model_names = array("EE_" . $this->_model_name_pointed_to); |
|
76 | + $model_names = array("EE_".$this->_model_name_pointed_to); |
|
77 | 77 | } |
78 | 78 | return $model_names; |
79 | 79 | } |
@@ -8,88 +8,88 @@ |
||
8 | 8 | */ |
9 | 9 | abstract class EE_Field_With_Model_Name extends EE_Model_Field_Base |
10 | 10 | { |
11 | - /** |
|
12 | - * Usually the name of a single model. However, as in the case for custom post types, |
|
13 | - * it can actually be an array of models |
|
14 | - * |
|
15 | - * @var string or array |
|
16 | - */ |
|
17 | - protected $_model_name_pointed_to; |
|
11 | + /** |
|
12 | + * Usually the name of a single model. However, as in the case for custom post types, |
|
13 | + * it can actually be an array of models |
|
14 | + * |
|
15 | + * @var string or array |
|
16 | + */ |
|
17 | + protected $_model_name_pointed_to; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param string $table_column name fo column for field |
|
21 | - * @param string $nicename should eb internationalized with __('blah','event_espresso') |
|
22 | - * @param boolean $nullable |
|
23 | - * @param mixed $default_value if this is a integer field, it shoudl be an int. if it's a string field, it shoul |
|
24 | - * dbe a string |
|
25 | - * @param string $model_name eg 'Event','Answer','Term', etc. Basically its the model class's name without the |
|
26 | - * "EEM_" |
|
27 | - */ |
|
28 | - public function __construct($table_column, $nicename, $nullable, $default_value, $model_name) |
|
29 | - { |
|
30 | - $this->_model_name_pointed_to = $model_name; |
|
31 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
32 | - } |
|
19 | + /** |
|
20 | + * @param string $table_column name fo column for field |
|
21 | + * @param string $nicename should eb internationalized with __('blah','event_espresso') |
|
22 | + * @param boolean $nullable |
|
23 | + * @param mixed $default_value if this is a integer field, it shoudl be an int. if it's a string field, it shoul |
|
24 | + * dbe a string |
|
25 | + * @param string $model_name eg 'Event','Answer','Term', etc. Basically its the model class's name without the |
|
26 | + * "EEM_" |
|
27 | + */ |
|
28 | + public function __construct($table_column, $nicename, $nullable, $default_value, $model_name) |
|
29 | + { |
|
30 | + $this->_model_name_pointed_to = $model_name; |
|
31 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Returns the name of the model(s) pointed to |
|
36 | - * |
|
37 | - * @deprecated since version 4.6.7 |
|
38 | - * @return mixed string or array of strings |
|
39 | - */ |
|
40 | - public function get_model_name_pointed_to() |
|
41 | - { |
|
42 | - EE_Error::doing_it_wrong( |
|
43 | - 'get_model_name_pointed_to', |
|
44 | - __( |
|
45 | - 'This method has been deprecated in favour of instead using get_model_names_pointed_to, which consistently returns an array', |
|
46 | - 'event_espresso' |
|
47 | - ), |
|
48 | - '4.6.7' |
|
49 | - ); |
|
50 | - return $this->_model_name_pointed_to; |
|
51 | - } |
|
34 | + /** |
|
35 | + * Returns the name of the model(s) pointed to |
|
36 | + * |
|
37 | + * @deprecated since version 4.6.7 |
|
38 | + * @return mixed string or array of strings |
|
39 | + */ |
|
40 | + public function get_model_name_pointed_to() |
|
41 | + { |
|
42 | + EE_Error::doing_it_wrong( |
|
43 | + 'get_model_name_pointed_to', |
|
44 | + __( |
|
45 | + 'This method has been deprecated in favour of instead using get_model_names_pointed_to, which consistently returns an array', |
|
46 | + 'event_espresso' |
|
47 | + ), |
|
48 | + '4.6.7' |
|
49 | + ); |
|
50 | + return $this->_model_name_pointed_to; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Gets the model names pointed to by this field, always as an array |
|
55 | - * (even if there's only one) |
|
56 | - * |
|
57 | - * @return array of model names pointed to by this field |
|
58 | - */ |
|
59 | - public function get_model_names_pointed_to() |
|
60 | - { |
|
61 | - if (is_array($this->_model_name_pointed_to)) { |
|
62 | - return $this->_model_name_pointed_to; |
|
63 | - } else { |
|
64 | - return array($this->_model_name_pointed_to); |
|
65 | - } |
|
66 | - } |
|
53 | + /** |
|
54 | + * Gets the model names pointed to by this field, always as an array |
|
55 | + * (even if there's only one) |
|
56 | + * |
|
57 | + * @return array of model names pointed to by this field |
|
58 | + */ |
|
59 | + public function get_model_names_pointed_to() |
|
60 | + { |
|
61 | + if (is_array($this->_model_name_pointed_to)) { |
|
62 | + return $this->_model_name_pointed_to; |
|
63 | + } else { |
|
64 | + return array($this->_model_name_pointed_to); |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Returns the model's classname (eg EE_Event instead of just Event) |
|
70 | - * |
|
71 | - * @return array |
|
72 | - */ |
|
73 | - public function get_model_class_names_pointed_to() |
|
74 | - { |
|
75 | - $model_names = array(); |
|
76 | - if (is_array($this->_model_name_pointed_to)) { |
|
77 | - foreach ($this->_model_name_pointed_to as $model_name) { |
|
78 | - $model_names[] = "EE_" . $model_name; |
|
79 | - } |
|
80 | - } else { |
|
81 | - $model_names = array("EE_" . $this->_model_name_pointed_to); |
|
82 | - } |
|
83 | - return $model_names; |
|
84 | - } |
|
68 | + /** |
|
69 | + * Returns the model's classname (eg EE_Event instead of just Event) |
|
70 | + * |
|
71 | + * @return array |
|
72 | + */ |
|
73 | + public function get_model_class_names_pointed_to() |
|
74 | + { |
|
75 | + $model_names = array(); |
|
76 | + if (is_array($this->_model_name_pointed_to)) { |
|
77 | + foreach ($this->_model_name_pointed_to as $model_name) { |
|
78 | + $model_names[] = "EE_" . $model_name; |
|
79 | + } |
|
80 | + } else { |
|
81 | + $model_names = array("EE_" . $this->_model_name_pointed_to); |
|
82 | + } |
|
83 | + return $model_names; |
|
84 | + } |
|
85 | 85 | |
86 | - public function is_model_obj_of_type_pointed_to($model_obj_or_ID) |
|
87 | - { |
|
88 | - foreach ($this->get_model_class_names_pointed_to() as $model_obj_classname) { |
|
89 | - if ($model_obj_or_ID instanceof $model_obj_classname) { |
|
90 | - return true; |
|
91 | - } |
|
92 | - } |
|
93 | - return false; |
|
94 | - } |
|
86 | + public function is_model_obj_of_type_pointed_to($model_obj_or_ID) |
|
87 | + { |
|
88 | + foreach ($this->get_model_class_names_pointed_to() as $model_obj_classname) { |
|
89 | + if ($model_obj_or_ID instanceof $model_obj_classname) { |
|
90 | + return true; |
|
91 | + } |
|
92 | + } |
|
93 | + return false; |
|
94 | + } |
|
95 | 95 | } |
@@ -582,7 +582,7 @@ |
||
582 | 582 | /** |
583 | 583 | * Validates that the incoming format is an allowable string to use for the _schema_format property |
584 | 584 | * @throws InvalidArgumentException |
585 | - * @param $format |
|
585 | + * @param string $format |
|
586 | 586 | */ |
587 | 587 | private function validateSchemaFormat($format) |
588 | 588 | { |
@@ -19,651 +19,650 @@ |
||
19 | 19 | */ |
20 | 20 | abstract class EE_Model_Field_Base implements HasSchemaInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * The alias for the table the column belongs to. |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $_table_alias; |
|
27 | - |
|
28 | - /** |
|
29 | - * The actual db column name for the table |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - protected $_table_column; |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * The authoritative name for the table column (used by client code to reference the field). |
|
37 | - * @var string |
|
38 | - */ |
|
39 | - protected $_name; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * A description for the field. |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $_nicename; |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * Whether the field is nullable or not |
|
51 | - * @var bool |
|
52 | - */ |
|
53 | - protected $_nullable; |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * What the default value for the field should be. |
|
58 | - * @var mixed |
|
59 | - */ |
|
60 | - protected $_default_value; |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * Other configuration for the field |
|
65 | - * @var mixed |
|
66 | - */ |
|
67 | - protected $_other_config; |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * The name of the model this field is instantiated for. |
|
72 | - * @var string |
|
73 | - */ |
|
74 | - protected $_model_name; |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * This should be a json-schema valid data type for the field. |
|
79 | - * @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
80 | - * @var string |
|
81 | - */ |
|
82 | - private $_schema_type = 'string'; |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * If the schema has a defined format then it should be defined via this property. |
|
87 | - * @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
88 | - * @var string |
|
89 | - */ |
|
90 | - private $_schema_format = ''; |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Indicates that the value of the field is managed exclusively by the server/model and not something |
|
95 | - * settable by client code. |
|
96 | - * @link http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.4 |
|
97 | - * @var bool |
|
98 | - */ |
|
99 | - private $_schema_readonly = false; |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $table_column |
|
104 | - * @param string $nicename |
|
105 | - * @param bool $nullable |
|
106 | - * @param null $default_value |
|
107 | - */ |
|
108 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
109 | - { |
|
110 | - $this->_table_column = $table_column; |
|
111 | - $this->_nicename = $nicename; |
|
112 | - $this->_nullable = $nullable; |
|
113 | - $this->_default_value = $default_value; |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * @param $table_alias |
|
119 | - * @param $name |
|
120 | - * @param $model_name |
|
121 | - */ |
|
122 | - public function _construct_finalize($table_alias, $name, $model_name) |
|
123 | - { |
|
124 | - $this->_table_alias = $table_alias; |
|
125 | - $this->_name = $name; |
|
126 | - $this->_model_name = $model_name; |
|
127 | - /** |
|
128 | - * allow for changing the defaults |
|
129 | - */ |
|
130 | - $this->_nicename = apply_filters( |
|
131 | - 'FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
|
132 | - $this->_nicename, |
|
133 | - $this |
|
134 | - ); |
|
135 | - $this->_default_value = apply_filters( |
|
136 | - 'FHEE__EE_Model_Field_Base___construct_finalize___default_value', |
|
137 | - $this->_default_value, |
|
138 | - $this |
|
139 | - ); |
|
140 | - } |
|
141 | - |
|
142 | - public function get_table_alias() |
|
143 | - { |
|
144 | - return $this->_table_alias; |
|
145 | - } |
|
146 | - |
|
147 | - public function get_table_column() |
|
148 | - { |
|
149 | - return $this->_table_column; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Returns the name of the model this field is on. Eg 'Event' or 'Ticket_Datetime' |
|
154 | - * |
|
155 | - * @return string |
|
156 | - */ |
|
157 | - public function get_model_name() |
|
158 | - { |
|
159 | - return $this->_model_name; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @throws \EE_Error |
|
164 | - * @return string |
|
165 | - */ |
|
166 | - public function get_name() |
|
167 | - { |
|
168 | - if ($this->_name) { |
|
169 | - return $this->_name; |
|
170 | - } else { |
|
171 | - throw new EE_Error(sprintf(__( |
|
172 | - "Model field '%s' has no name set. Did you make a model and forget to call the parent model constructor?", |
|
173 | - "event_espresso" |
|
174 | - ), get_class($this))); |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - public function get_nicename() |
|
179 | - { |
|
180 | - return $this->_nicename; |
|
181 | - } |
|
182 | - |
|
183 | - public function is_nullable() |
|
184 | - { |
|
185 | - return $this->_nullable; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * returns whether this field is an auto-increment field or not. If it is, then |
|
190 | - * on insertion it can be null. However, on updates it must be present. |
|
191 | - * |
|
192 | - * @return boolean |
|
193 | - */ |
|
194 | - public function is_auto_increment() |
|
195 | - { |
|
196 | - return false; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * The default value in the model object's value domain. See lengthy comment about |
|
201 | - * value domains at the top of EEM_Base |
|
202 | - * |
|
203 | - * @return mixed |
|
204 | - */ |
|
205 | - public function get_default_value() |
|
206 | - { |
|
207 | - return $this->_default_value; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Returns the table alias joined to the table column, however this isn't the right |
|
212 | - * table alias if the aliased table is being joined to. In that case, you can use |
|
213 | - * EE_Model_Parser::extract_table_alias_model_relation_chain_prefix() to find the table's current alias |
|
214 | - * in the current query |
|
215 | - * |
|
216 | - * @return string |
|
217 | - */ |
|
218 | - public function get_qualified_column() |
|
219 | - { |
|
220 | - return $this->get_table_alias() . "." . $this->get_table_column(); |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * When get() is called on a model object (eg EE_Event), before returning its value, |
|
225 | - * call this function on it, allowing us to customize the returned value based on |
|
226 | - * the field's type. Eg, we may want to unserialize it, strip tags, etc. By default, |
|
227 | - * we simply return it. |
|
228 | - * |
|
229 | - * @param mixed $value_of_field_on_model_object |
|
230 | - * @return mixed |
|
231 | - */ |
|
232 | - public function prepare_for_get($value_of_field_on_model_object) |
|
233 | - { |
|
234 | - return $value_of_field_on_model_object; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * When inserting or updating a field on a model object, run this function on each |
|
239 | - * value to prepare it for insertion into the db. Generally this converts |
|
240 | - * the validated input on the model object into the format used in the DB. |
|
241 | - * |
|
242 | - * @param mixed $value_of_field_on_model_object |
|
243 | - * @return mixed |
|
244 | - */ |
|
245 | - public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
246 | - { |
|
247 | - return $value_of_field_on_model_object; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
|
252 | - * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
|
253 | - * By default, we do nothing. |
|
254 | - * |
|
255 | - * If the model field is going to perform any validation on the input, this is where it should be done |
|
256 | - * (once the value is on the model object, it may be used in other ways besides putting it into the DB |
|
257 | - * so it's best to validate it right away). |
|
258 | - * |
|
259 | - * @param mixed $value_inputted_for_field_on_model_object |
|
260 | - * @return mixed |
|
261 | - */ |
|
262 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
263 | - { |
|
264 | - return $value_inputted_for_field_on_model_object; |
|
265 | - } |
|
266 | - |
|
267 | - |
|
268 | - /** |
|
269 | - * When instantiating a model object from DB results, this function is called before setting each field. |
|
270 | - * We may want to serialize the value, etc. By default, we return the value using prepare_for_set() method as that |
|
271 | - * is the one child classes will most often define. |
|
272 | - * |
|
273 | - * @param mixed $value_found_in_db_for_model_object |
|
274 | - * @return mixed |
|
275 | - */ |
|
276 | - public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
277 | - { |
|
278 | - return $this->prepare_for_set($value_found_in_db_for_model_object); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * When echoing a field's value on a model object, this function is run to prepare the value for presentation in a |
|
283 | - * webpage. For example, we may want to output floats with 2 decimal places by default, dates as "Monday Jan 12, |
|
284 | - * 2013, at 3:23pm" instead of |
|
285 | - * "8765678632", or any other modifications to how the value should be displayed, but not modified itself. |
|
286 | - * |
|
287 | - * @param mixed $value_on_field_to_be_outputted |
|
288 | - * @return mixed |
|
289 | - */ |
|
290 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
291 | - { |
|
292 | - return $value_on_field_to_be_outputted; |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * Returns whatever is set as the nicename for the object. |
|
298 | - * @return string |
|
299 | - */ |
|
300 | - public function getSchemaDescription() |
|
301 | - { |
|
302 | - return $this->get_nicename(); |
|
303 | - } |
|
304 | - |
|
305 | - |
|
306 | - /** |
|
307 | - * Returns whatever is set as the $_schema_type property for the object. |
|
308 | - * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
309 | - * @return string|array |
|
310 | - */ |
|
311 | - public function getSchemaType() |
|
312 | - { |
|
313 | - if ($this->is_nullable()) { |
|
314 | - $this->_schema_type = (array) $this->_schema_type; |
|
315 | - if (! in_array('null', $this->_schema_type)) { |
|
316 | - $this->_schema_type[] = 'null'; |
|
317 | - }; |
|
318 | - } |
|
319 | - return $this->_schema_type; |
|
320 | - } |
|
321 | - |
|
322 | - |
|
323 | - /** |
|
324 | - * Sets the _schema_type property. Child classes should call this in their constructors to override the default state |
|
325 | - * for this property. |
|
326 | - * @param string|array $type |
|
327 | - * @throws InvalidArgumentException |
|
328 | - */ |
|
329 | - protected function setSchemaType($type) |
|
330 | - { |
|
331 | - $this->validateSchemaType($type); |
|
332 | - $this->_schema_type = $type; |
|
333 | - } |
|
334 | - |
|
335 | - |
|
336 | - /** |
|
337 | - * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
338 | - * this method and return the properties for the schema. |
|
339 | - * |
|
340 | - * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
341 | - * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
342 | - * |
|
343 | - * @return array |
|
344 | - */ |
|
345 | - public function getSchemaProperties() |
|
346 | - { |
|
347 | - return array(); |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * By default this returns the scalar default value that was sent in on the class prepped according to the class type |
|
354 | - * as the default. However, when there are schema properties, then the default property is setup to mirror the |
|
355 | - * property keys and correctly prepare the default according to that expected property value. |
|
356 | - * The getSchema method validates whether the schema for default is setup correctly or not according to the schema type |
|
357 | - * |
|
358 | - * @return mixed |
|
359 | - */ |
|
360 | - public function getSchemaDefault() |
|
361 | - { |
|
362 | - $default_value = $this->prepare_for_use_in_db($this->prepare_for_set($this->get_default_value())); |
|
363 | - $schema_properties = $this->getSchemaProperties(); |
|
364 | - |
|
365 | - // if this schema has properties than shape the default value to match the properties shape. |
|
366 | - if ($schema_properties) { |
|
367 | - $value_to_return = array(); |
|
368 | - foreach ($schema_properties as $property_key => $property_schema) { |
|
369 | - switch ($property_key) { |
|
370 | - case 'pretty': |
|
371 | - case 'rendered': |
|
372 | - $value_to_return[ $property_key ] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
373 | - break; |
|
374 | - default: |
|
375 | - $value_to_return[ $property_key ] = $default_value; |
|
376 | - break; |
|
377 | - } |
|
378 | - } |
|
379 | - $default_value = $value_to_return; |
|
380 | - } |
|
381 | - return $default_value; |
|
382 | - } |
|
383 | - |
|
384 | - |
|
385 | - |
|
386 | - |
|
387 | - /** |
|
388 | - * If a child class has enum values, they should override this method and provide a simple array |
|
389 | - * of the enum values. |
|
390 | - |
|
391 | - * The reason this is not a property on the class is because there may be filterable enum values that |
|
392 | - * are set on the instantiated object that could be filtered after construct. |
|
393 | - * |
|
394 | - * @return array |
|
395 | - */ |
|
396 | - public function getSchemaEnum() |
|
397 | - { |
|
398 | - return array(); |
|
399 | - } |
|
400 | - |
|
401 | - |
|
402 | - /** |
|
403 | - * This returns the value of the $_schema_format property on the object. |
|
404 | - * @return string |
|
405 | - */ |
|
406 | - public function getSchemaFormat() |
|
407 | - { |
|
408 | - return $this->_schema_format; |
|
409 | - } |
|
410 | - |
|
411 | - |
|
412 | - /** |
|
413 | - * Sets the schema format property. |
|
414 | - * @throws InvalidArgumentException |
|
415 | - * @param string $format |
|
416 | - */ |
|
417 | - protected function setSchemaFormat($format) |
|
418 | - { |
|
419 | - $this->validateSchemaFormat($format); |
|
420 | - $this->_schema_format = $format; |
|
421 | - } |
|
422 | - |
|
423 | - |
|
424 | - /** |
|
425 | - * This returns the value of the $_schema_readonly property on the object. |
|
426 | - * @return bool |
|
427 | - */ |
|
428 | - public function getSchemaReadonly() |
|
429 | - { |
|
430 | - return $this->_schema_readonly; |
|
431 | - } |
|
432 | - |
|
433 | - |
|
434 | - /** |
|
435 | - * This sets the value for the $_schema_readonly property. |
|
436 | - * @param bool $readonly (only explicit boolean values are accepted) |
|
437 | - */ |
|
438 | - protected function setSchemaReadOnly($readonly) |
|
439 | - { |
|
440 | - if (! is_bool($readonly)) { |
|
441 | - throw new InvalidArgumentException( |
|
442 | - sprintf( |
|
443 | - esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
|
444 | - print_r($readonly, true) |
|
445 | - ) |
|
446 | - ); |
|
447 | - } |
|
448 | - |
|
449 | - $this->_schema_readonly = $readonly; |
|
450 | - } |
|
451 | - |
|
452 | - |
|
453 | - |
|
454 | - |
|
455 | - /** |
|
456 | - * Return `%d`, `%s` or `%f` to indicate the data type for the field. |
|
457 | - * @uses _get_wpdb_data_type() |
|
458 | - * |
|
459 | - * @return string |
|
460 | - */ |
|
461 | - public function get_wpdb_data_type() |
|
462 | - { |
|
463 | - return $this->_get_wpdb_data_type(); |
|
464 | - } |
|
465 | - |
|
466 | - |
|
467 | - /** |
|
468 | - * Return `%d`, `%s` or `%f` to indicate the data type for the field that should be indicated in wpdb queries. |
|
469 | - * @param string $type Included if a specific type is requested. |
|
470 | - * @uses get_schema_type() |
|
471 | - * @return string |
|
472 | - */ |
|
473 | - protected function _get_wpdb_data_type($type = '') |
|
474 | - { |
|
475 | - $type = empty($type) ? $this->getSchemaType() : $type; |
|
476 | - |
|
477 | - // if type is an array, then different parsing is required. |
|
478 | - if (is_array($type)) { |
|
479 | - return $this->_get_wpdb_data_type_for_type_array($type); |
|
480 | - } |
|
481 | - |
|
482 | - $wpdb_type = '%s'; |
|
483 | - switch ($type) { |
|
484 | - case 'number': |
|
485 | - $wpdb_type = '%f'; |
|
486 | - break; |
|
487 | - case 'integer': |
|
488 | - case 'boolean': |
|
489 | - $wpdb_type = '%d'; |
|
490 | - break; |
|
491 | - case 'object': |
|
492 | - $properties = $this->getSchemaProperties(); |
|
493 | - if (isset($properties['raw'], $properties['raw']['type'])) { |
|
494 | - $wpdb_type = $this->_get_wpdb_data_type($properties['raw']['type']); |
|
495 | - } |
|
496 | - break; // leave at default |
|
497 | - } |
|
498 | - return $wpdb_type; |
|
499 | - } |
|
500 | - |
|
501 | - |
|
502 | - |
|
503 | - protected function _get_wpdb_data_type_for_type_array($type) |
|
504 | - { |
|
505 | - $type = (array) $type; |
|
506 | - // first let's flip because then we can do a faster key check |
|
507 | - $type = array_flip($type); |
|
508 | - |
|
509 | - // check for things that mean '%s' |
|
510 | - if (isset($type['string'], $type['object'], $type['array'])) { |
|
511 | - return '%s'; |
|
512 | - } |
|
513 | - |
|
514 | - // if makes it past the above condition and there's float in the array |
|
515 | - // then the type is %f |
|
516 | - if (isset($type['number'])) { |
|
517 | - return '%f'; |
|
518 | - } |
|
519 | - |
|
520 | - // if it makes it above the above conditions and there is an integer in the array |
|
521 | - // then the type is %d |
|
522 | - if (isset($type['integer'])) { |
|
523 | - return '%d'; |
|
524 | - } |
|
525 | - |
|
526 | - // anything else is a string |
|
527 | - return '%s'; |
|
528 | - } |
|
529 | - |
|
530 | - |
|
531 | - /** |
|
532 | - * This returns elements used to represent this field in the json schema. |
|
533 | - * |
|
534 | - * @link http://json-schema.org/ |
|
535 | - * @return array |
|
536 | - */ |
|
537 | - public function getSchema() |
|
538 | - { |
|
539 | - $schema = array( |
|
540 | - 'description' => $this->getSchemaDescription(), |
|
541 | - 'type' => $this->getSchemaType(), |
|
542 | - 'readonly' => $this->getSchemaReadonly(), |
|
543 | - 'default' => $this->getSchemaDefault() |
|
544 | - ); |
|
545 | - |
|
546 | - // optional properties of the schema |
|
547 | - $enum = $this->getSchemaEnum(); |
|
548 | - $properties = $this->getSchemaProperties(); |
|
549 | - $format = $this->getSchemaFormat(); |
|
550 | - if ($enum) { |
|
551 | - $schema['enum'] = $enum; |
|
552 | - } |
|
553 | - |
|
554 | - if ($properties) { |
|
555 | - $schema['properties'] = $properties; |
|
556 | - } |
|
557 | - |
|
558 | - if ($format) { |
|
559 | - $schema['format'] = $format; |
|
560 | - } |
|
561 | - return $schema; |
|
562 | - } |
|
563 | - |
|
564 | - /** |
|
565 | - * Some fields are in the database-only, (ie, used in queries etc), but shouldn't necessarily be part |
|
566 | - * of the model objects (ie, client code shouldn't care to ever see their value... if client code does |
|
567 | - * want to see their value, then they shouldn't be db-only fields!) |
|
568 | - * Eg, when doing events as custom post types, querying the post_type is essential, but |
|
569 | - * post_type is irrelevant for EE_Event objects (because they will ALL be of post_type 'esp_event'). |
|
570 | - * By default, all fields aren't db-only. |
|
571 | - * |
|
572 | - * @return boolean |
|
573 | - */ |
|
574 | - public function is_db_only_field() |
|
575 | - { |
|
576 | - return false; |
|
577 | - } |
|
578 | - |
|
579 | - |
|
580 | - /** |
|
581 | - * Validates the incoming string|array to ensure its an allowable type. |
|
582 | - * @throws InvalidArgumentException |
|
583 | - * @param string|array $type |
|
584 | - */ |
|
585 | - private function validateSchemaType($type) |
|
586 | - { |
|
587 | - if (! (is_string($type) || is_array($type))) { |
|
588 | - throw new InvalidArgumentException( |
|
589 | - sprintf( |
|
590 | - esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
|
591 | - print_r($type, true) |
|
592 | - ) |
|
593 | - ); |
|
594 | - } |
|
595 | - |
|
596 | - // validate allowable types. |
|
597 | - // @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
598 | - $allowable_types = array_flip( |
|
599 | - array( |
|
600 | - 'string', |
|
601 | - 'number', |
|
602 | - 'null', |
|
603 | - 'object', |
|
604 | - 'array', |
|
605 | - 'boolean', |
|
606 | - 'integer' |
|
607 | - ) |
|
608 | - ); |
|
609 | - |
|
610 | - if (is_array($type)) { |
|
611 | - foreach ($type as $item_in_type) { |
|
612 | - $this->validateSchemaType($item_in_type); |
|
613 | - } |
|
614 | - return; |
|
615 | - } |
|
616 | - |
|
617 | - if (! isset($allowable_types[ $type ])) { |
|
618 | - throw new InvalidArgumentException( |
|
619 | - sprintf( |
|
620 | - esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
|
621 | - $type, |
|
622 | - implode(',', array_flip($allowable_types)) |
|
623 | - ) |
|
624 | - ); |
|
625 | - } |
|
626 | - } |
|
627 | - |
|
628 | - |
|
629 | - /** |
|
630 | - * Validates that the incoming format is an allowable string to use for the _schema_format property |
|
631 | - * @throws InvalidArgumentException |
|
632 | - * @param $format |
|
633 | - */ |
|
634 | - private function validateSchemaFormat($format) |
|
635 | - { |
|
636 | - if (! is_string($format)) { |
|
637 | - throw new InvalidArgumentException( |
|
638 | - sprintf( |
|
639 | - esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
|
640 | - print_r($format, true) |
|
641 | - ) |
|
642 | - ); |
|
643 | - } |
|
644 | - |
|
645 | - // validate allowable format values |
|
646 | - // @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
647 | - $allowable_formats = array_flip( |
|
648 | - array( |
|
649 | - 'date-time', |
|
650 | - 'email', |
|
651 | - 'hostname', |
|
652 | - 'ipv4', |
|
653 | - 'ipv6', |
|
654 | - 'uri', |
|
655 | - 'uriref' |
|
656 | - ) |
|
657 | - ); |
|
658 | - |
|
659 | - if (! isset($allowable_formats[ $format ])) { |
|
660 | - throw new InvalidArgumentException( |
|
661 | - sprintf( |
|
662 | - esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
|
663 | - $format, |
|
664 | - implode(',', array_flip($allowable_formats)) |
|
665 | - ) |
|
666 | - ); |
|
667 | - } |
|
668 | - } |
|
22 | + /** |
|
23 | + * The alias for the table the column belongs to. |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $_table_alias; |
|
27 | + |
|
28 | + /** |
|
29 | + * The actual db column name for the table |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + protected $_table_column; |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * The authoritative name for the table column (used by client code to reference the field). |
|
37 | + * @var string |
|
38 | + */ |
|
39 | + protected $_name; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * A description for the field. |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $_nicename; |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * Whether the field is nullable or not |
|
51 | + * @var bool |
|
52 | + */ |
|
53 | + protected $_nullable; |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * What the default value for the field should be. |
|
58 | + * @var mixed |
|
59 | + */ |
|
60 | + protected $_default_value; |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * Other configuration for the field |
|
65 | + * @var mixed |
|
66 | + */ |
|
67 | + protected $_other_config; |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * The name of the model this field is instantiated for. |
|
72 | + * @var string |
|
73 | + */ |
|
74 | + protected $_model_name; |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * This should be a json-schema valid data type for the field. |
|
79 | + * @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
80 | + * @var string |
|
81 | + */ |
|
82 | + private $_schema_type = 'string'; |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * If the schema has a defined format then it should be defined via this property. |
|
87 | + * @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
88 | + * @var string |
|
89 | + */ |
|
90 | + private $_schema_format = ''; |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Indicates that the value of the field is managed exclusively by the server/model and not something |
|
95 | + * settable by client code. |
|
96 | + * @link http://json-schema.org/latest/json-schema-hypermedia.html#rfc.section.4.4 |
|
97 | + * @var bool |
|
98 | + */ |
|
99 | + private $_schema_readonly = false; |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $table_column |
|
104 | + * @param string $nicename |
|
105 | + * @param bool $nullable |
|
106 | + * @param null $default_value |
|
107 | + */ |
|
108 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
109 | + { |
|
110 | + $this->_table_column = $table_column; |
|
111 | + $this->_nicename = $nicename; |
|
112 | + $this->_nullable = $nullable; |
|
113 | + $this->_default_value = $default_value; |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * @param $table_alias |
|
119 | + * @param $name |
|
120 | + * @param $model_name |
|
121 | + */ |
|
122 | + public function _construct_finalize($table_alias, $name, $model_name) |
|
123 | + { |
|
124 | + $this->_table_alias = $table_alias; |
|
125 | + $this->_name = $name; |
|
126 | + $this->_model_name = $model_name; |
|
127 | + /** |
|
128 | + * allow for changing the defaults |
|
129 | + */ |
|
130 | + $this->_nicename = apply_filters( |
|
131 | + 'FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
|
132 | + $this->_nicename, |
|
133 | + $this |
|
134 | + ); |
|
135 | + $this->_default_value = apply_filters( |
|
136 | + 'FHEE__EE_Model_Field_Base___construct_finalize___default_value', |
|
137 | + $this->_default_value, |
|
138 | + $this |
|
139 | + ); |
|
140 | + } |
|
141 | + |
|
142 | + public function get_table_alias() |
|
143 | + { |
|
144 | + return $this->_table_alias; |
|
145 | + } |
|
146 | + |
|
147 | + public function get_table_column() |
|
148 | + { |
|
149 | + return $this->_table_column; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Returns the name of the model this field is on. Eg 'Event' or 'Ticket_Datetime' |
|
154 | + * |
|
155 | + * @return string |
|
156 | + */ |
|
157 | + public function get_model_name() |
|
158 | + { |
|
159 | + return $this->_model_name; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @throws \EE_Error |
|
164 | + * @return string |
|
165 | + */ |
|
166 | + public function get_name() |
|
167 | + { |
|
168 | + if ($this->_name) { |
|
169 | + return $this->_name; |
|
170 | + } else { |
|
171 | + throw new EE_Error(sprintf(__( |
|
172 | + "Model field '%s' has no name set. Did you make a model and forget to call the parent model constructor?", |
|
173 | + "event_espresso" |
|
174 | + ), get_class($this))); |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + public function get_nicename() |
|
179 | + { |
|
180 | + return $this->_nicename; |
|
181 | + } |
|
182 | + |
|
183 | + public function is_nullable() |
|
184 | + { |
|
185 | + return $this->_nullable; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * returns whether this field is an auto-increment field or not. If it is, then |
|
190 | + * on insertion it can be null. However, on updates it must be present. |
|
191 | + * |
|
192 | + * @return boolean |
|
193 | + */ |
|
194 | + public function is_auto_increment() |
|
195 | + { |
|
196 | + return false; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * The default value in the model object's value domain. See lengthy comment about |
|
201 | + * value domains at the top of EEM_Base |
|
202 | + * |
|
203 | + * @return mixed |
|
204 | + */ |
|
205 | + public function get_default_value() |
|
206 | + { |
|
207 | + return $this->_default_value; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Returns the table alias joined to the table column, however this isn't the right |
|
212 | + * table alias if the aliased table is being joined to. In that case, you can use |
|
213 | + * EE_Model_Parser::extract_table_alias_model_relation_chain_prefix() to find the table's current alias |
|
214 | + * in the current query |
|
215 | + * |
|
216 | + * @return string |
|
217 | + */ |
|
218 | + public function get_qualified_column() |
|
219 | + { |
|
220 | + return $this->get_table_alias() . "." . $this->get_table_column(); |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * When get() is called on a model object (eg EE_Event), before returning its value, |
|
225 | + * call this function on it, allowing us to customize the returned value based on |
|
226 | + * the field's type. Eg, we may want to unserialize it, strip tags, etc. By default, |
|
227 | + * we simply return it. |
|
228 | + * |
|
229 | + * @param mixed $value_of_field_on_model_object |
|
230 | + * @return mixed |
|
231 | + */ |
|
232 | + public function prepare_for_get($value_of_field_on_model_object) |
|
233 | + { |
|
234 | + return $value_of_field_on_model_object; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * When inserting or updating a field on a model object, run this function on each |
|
239 | + * value to prepare it for insertion into the db. Generally this converts |
|
240 | + * the validated input on the model object into the format used in the DB. |
|
241 | + * |
|
242 | + * @param mixed $value_of_field_on_model_object |
|
243 | + * @return mixed |
|
244 | + */ |
|
245 | + public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
246 | + { |
|
247 | + return $value_of_field_on_model_object; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * When creating a brand-new model object, or setting a particular value for one of its fields, this function |
|
252 | + * is called before setting it on the model object. We may want to strip slashes, unserialize the value, etc. |
|
253 | + * By default, we do nothing. |
|
254 | + * |
|
255 | + * If the model field is going to perform any validation on the input, this is where it should be done |
|
256 | + * (once the value is on the model object, it may be used in other ways besides putting it into the DB |
|
257 | + * so it's best to validate it right away). |
|
258 | + * |
|
259 | + * @param mixed $value_inputted_for_field_on_model_object |
|
260 | + * @return mixed |
|
261 | + */ |
|
262 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
263 | + { |
|
264 | + return $value_inputted_for_field_on_model_object; |
|
265 | + } |
|
266 | + |
|
267 | + |
|
268 | + /** |
|
269 | + * When instantiating a model object from DB results, this function is called before setting each field. |
|
270 | + * We may want to serialize the value, etc. By default, we return the value using prepare_for_set() method as that |
|
271 | + * is the one child classes will most often define. |
|
272 | + * |
|
273 | + * @param mixed $value_found_in_db_for_model_object |
|
274 | + * @return mixed |
|
275 | + */ |
|
276 | + public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
277 | + { |
|
278 | + return $this->prepare_for_set($value_found_in_db_for_model_object); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * When echoing a field's value on a model object, this function is run to prepare the value for presentation in a |
|
283 | + * webpage. For example, we may want to output floats with 2 decimal places by default, dates as "Monday Jan 12, |
|
284 | + * 2013, at 3:23pm" instead of |
|
285 | + * "8765678632", or any other modifications to how the value should be displayed, but not modified itself. |
|
286 | + * |
|
287 | + * @param mixed $value_on_field_to_be_outputted |
|
288 | + * @return mixed |
|
289 | + */ |
|
290 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted) |
|
291 | + { |
|
292 | + return $value_on_field_to_be_outputted; |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * Returns whatever is set as the nicename for the object. |
|
298 | + * @return string |
|
299 | + */ |
|
300 | + public function getSchemaDescription() |
|
301 | + { |
|
302 | + return $this->get_nicename(); |
|
303 | + } |
|
304 | + |
|
305 | + |
|
306 | + /** |
|
307 | + * Returns whatever is set as the $_schema_type property for the object. |
|
308 | + * Note: this will automatically add 'null' to the schema if the object is_nullable() |
|
309 | + * @return string|array |
|
310 | + */ |
|
311 | + public function getSchemaType() |
|
312 | + { |
|
313 | + if ($this->is_nullable()) { |
|
314 | + $this->_schema_type = (array) $this->_schema_type; |
|
315 | + if (! in_array('null', $this->_schema_type)) { |
|
316 | + $this->_schema_type[] = 'null'; |
|
317 | + }; |
|
318 | + } |
|
319 | + return $this->_schema_type; |
|
320 | + } |
|
321 | + |
|
322 | + |
|
323 | + /** |
|
324 | + * Sets the _schema_type property. Child classes should call this in their constructors to override the default state |
|
325 | + * for this property. |
|
326 | + * @param string|array $type |
|
327 | + * @throws InvalidArgumentException |
|
328 | + */ |
|
329 | + protected function setSchemaType($type) |
|
330 | + { |
|
331 | + $this->validateSchemaType($type); |
|
332 | + $this->_schema_type = $type; |
|
333 | + } |
|
334 | + |
|
335 | + |
|
336 | + /** |
|
337 | + * This is usually present when the $_schema_type property is 'object'. Any child classes will need to override |
|
338 | + * this method and return the properties for the schema. |
|
339 | + * |
|
340 | + * The reason this is not a property on the class is because there may be filters set on the values for the property |
|
341 | + * that won't be exposed on construct. For example enum type schemas may have the enum values filtered. |
|
342 | + * |
|
343 | + * @return array |
|
344 | + */ |
|
345 | + public function getSchemaProperties() |
|
346 | + { |
|
347 | + return array(); |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * By default this returns the scalar default value that was sent in on the class prepped according to the class type |
|
354 | + * as the default. However, when there are schema properties, then the default property is setup to mirror the |
|
355 | + * property keys and correctly prepare the default according to that expected property value. |
|
356 | + * The getSchema method validates whether the schema for default is setup correctly or not according to the schema type |
|
357 | + * |
|
358 | + * @return mixed |
|
359 | + */ |
|
360 | + public function getSchemaDefault() |
|
361 | + { |
|
362 | + $default_value = $this->prepare_for_use_in_db($this->prepare_for_set($this->get_default_value())); |
|
363 | + $schema_properties = $this->getSchemaProperties(); |
|
364 | + |
|
365 | + // if this schema has properties than shape the default value to match the properties shape. |
|
366 | + if ($schema_properties) { |
|
367 | + $value_to_return = array(); |
|
368 | + foreach ($schema_properties as $property_key => $property_schema) { |
|
369 | + switch ($property_key) { |
|
370 | + case 'pretty': |
|
371 | + case 'rendered': |
|
372 | + $value_to_return[ $property_key ] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
373 | + break; |
|
374 | + default: |
|
375 | + $value_to_return[ $property_key ] = $default_value; |
|
376 | + break; |
|
377 | + } |
|
378 | + } |
|
379 | + $default_value = $value_to_return; |
|
380 | + } |
|
381 | + return $default_value; |
|
382 | + } |
|
383 | + |
|
384 | + |
|
385 | + |
|
386 | + |
|
387 | + /** |
|
388 | + * If a child class has enum values, they should override this method and provide a simple array |
|
389 | + * of the enum values. |
|
390 | + * The reason this is not a property on the class is because there may be filterable enum values that |
|
391 | + * are set on the instantiated object that could be filtered after construct. |
|
392 | + * |
|
393 | + * @return array |
|
394 | + */ |
|
395 | + public function getSchemaEnum() |
|
396 | + { |
|
397 | + return array(); |
|
398 | + } |
|
399 | + |
|
400 | + |
|
401 | + /** |
|
402 | + * This returns the value of the $_schema_format property on the object. |
|
403 | + * @return string |
|
404 | + */ |
|
405 | + public function getSchemaFormat() |
|
406 | + { |
|
407 | + return $this->_schema_format; |
|
408 | + } |
|
409 | + |
|
410 | + |
|
411 | + /** |
|
412 | + * Sets the schema format property. |
|
413 | + * @throws InvalidArgumentException |
|
414 | + * @param string $format |
|
415 | + */ |
|
416 | + protected function setSchemaFormat($format) |
|
417 | + { |
|
418 | + $this->validateSchemaFormat($format); |
|
419 | + $this->_schema_format = $format; |
|
420 | + } |
|
421 | + |
|
422 | + |
|
423 | + /** |
|
424 | + * This returns the value of the $_schema_readonly property on the object. |
|
425 | + * @return bool |
|
426 | + */ |
|
427 | + public function getSchemaReadonly() |
|
428 | + { |
|
429 | + return $this->_schema_readonly; |
|
430 | + } |
|
431 | + |
|
432 | + |
|
433 | + /** |
|
434 | + * This sets the value for the $_schema_readonly property. |
|
435 | + * @param bool $readonly (only explicit boolean values are accepted) |
|
436 | + */ |
|
437 | + protected function setSchemaReadOnly($readonly) |
|
438 | + { |
|
439 | + if (! is_bool($readonly)) { |
|
440 | + throw new InvalidArgumentException( |
|
441 | + sprintf( |
|
442 | + esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
|
443 | + print_r($readonly, true) |
|
444 | + ) |
|
445 | + ); |
|
446 | + } |
|
447 | + |
|
448 | + $this->_schema_readonly = $readonly; |
|
449 | + } |
|
450 | + |
|
451 | + |
|
452 | + |
|
453 | + |
|
454 | + /** |
|
455 | + * Return `%d`, `%s` or `%f` to indicate the data type for the field. |
|
456 | + * @uses _get_wpdb_data_type() |
|
457 | + * |
|
458 | + * @return string |
|
459 | + */ |
|
460 | + public function get_wpdb_data_type() |
|
461 | + { |
|
462 | + return $this->_get_wpdb_data_type(); |
|
463 | + } |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * Return `%d`, `%s` or `%f` to indicate the data type for the field that should be indicated in wpdb queries. |
|
468 | + * @param string $type Included if a specific type is requested. |
|
469 | + * @uses get_schema_type() |
|
470 | + * @return string |
|
471 | + */ |
|
472 | + protected function _get_wpdb_data_type($type = '') |
|
473 | + { |
|
474 | + $type = empty($type) ? $this->getSchemaType() : $type; |
|
475 | + |
|
476 | + // if type is an array, then different parsing is required. |
|
477 | + if (is_array($type)) { |
|
478 | + return $this->_get_wpdb_data_type_for_type_array($type); |
|
479 | + } |
|
480 | + |
|
481 | + $wpdb_type = '%s'; |
|
482 | + switch ($type) { |
|
483 | + case 'number': |
|
484 | + $wpdb_type = '%f'; |
|
485 | + break; |
|
486 | + case 'integer': |
|
487 | + case 'boolean': |
|
488 | + $wpdb_type = '%d'; |
|
489 | + break; |
|
490 | + case 'object': |
|
491 | + $properties = $this->getSchemaProperties(); |
|
492 | + if (isset($properties['raw'], $properties['raw']['type'])) { |
|
493 | + $wpdb_type = $this->_get_wpdb_data_type($properties['raw']['type']); |
|
494 | + } |
|
495 | + break; // leave at default |
|
496 | + } |
|
497 | + return $wpdb_type; |
|
498 | + } |
|
499 | + |
|
500 | + |
|
501 | + |
|
502 | + protected function _get_wpdb_data_type_for_type_array($type) |
|
503 | + { |
|
504 | + $type = (array) $type; |
|
505 | + // first let's flip because then we can do a faster key check |
|
506 | + $type = array_flip($type); |
|
507 | + |
|
508 | + // check for things that mean '%s' |
|
509 | + if (isset($type['string'], $type['object'], $type['array'])) { |
|
510 | + return '%s'; |
|
511 | + } |
|
512 | + |
|
513 | + // if makes it past the above condition and there's float in the array |
|
514 | + // then the type is %f |
|
515 | + if (isset($type['number'])) { |
|
516 | + return '%f'; |
|
517 | + } |
|
518 | + |
|
519 | + // if it makes it above the above conditions and there is an integer in the array |
|
520 | + // then the type is %d |
|
521 | + if (isset($type['integer'])) { |
|
522 | + return '%d'; |
|
523 | + } |
|
524 | + |
|
525 | + // anything else is a string |
|
526 | + return '%s'; |
|
527 | + } |
|
528 | + |
|
529 | + |
|
530 | + /** |
|
531 | + * This returns elements used to represent this field in the json schema. |
|
532 | + * |
|
533 | + * @link http://json-schema.org/ |
|
534 | + * @return array |
|
535 | + */ |
|
536 | + public function getSchema() |
|
537 | + { |
|
538 | + $schema = array( |
|
539 | + 'description' => $this->getSchemaDescription(), |
|
540 | + 'type' => $this->getSchemaType(), |
|
541 | + 'readonly' => $this->getSchemaReadonly(), |
|
542 | + 'default' => $this->getSchemaDefault() |
|
543 | + ); |
|
544 | + |
|
545 | + // optional properties of the schema |
|
546 | + $enum = $this->getSchemaEnum(); |
|
547 | + $properties = $this->getSchemaProperties(); |
|
548 | + $format = $this->getSchemaFormat(); |
|
549 | + if ($enum) { |
|
550 | + $schema['enum'] = $enum; |
|
551 | + } |
|
552 | + |
|
553 | + if ($properties) { |
|
554 | + $schema['properties'] = $properties; |
|
555 | + } |
|
556 | + |
|
557 | + if ($format) { |
|
558 | + $schema['format'] = $format; |
|
559 | + } |
|
560 | + return $schema; |
|
561 | + } |
|
562 | + |
|
563 | + /** |
|
564 | + * Some fields are in the database-only, (ie, used in queries etc), but shouldn't necessarily be part |
|
565 | + * of the model objects (ie, client code shouldn't care to ever see their value... if client code does |
|
566 | + * want to see their value, then they shouldn't be db-only fields!) |
|
567 | + * Eg, when doing events as custom post types, querying the post_type is essential, but |
|
568 | + * post_type is irrelevant for EE_Event objects (because they will ALL be of post_type 'esp_event'). |
|
569 | + * By default, all fields aren't db-only. |
|
570 | + * |
|
571 | + * @return boolean |
|
572 | + */ |
|
573 | + public function is_db_only_field() |
|
574 | + { |
|
575 | + return false; |
|
576 | + } |
|
577 | + |
|
578 | + |
|
579 | + /** |
|
580 | + * Validates the incoming string|array to ensure its an allowable type. |
|
581 | + * @throws InvalidArgumentException |
|
582 | + * @param string|array $type |
|
583 | + */ |
|
584 | + private function validateSchemaType($type) |
|
585 | + { |
|
586 | + if (! (is_string($type) || is_array($type))) { |
|
587 | + throw new InvalidArgumentException( |
|
588 | + sprintf( |
|
589 | + esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
|
590 | + print_r($type, true) |
|
591 | + ) |
|
592 | + ); |
|
593 | + } |
|
594 | + |
|
595 | + // validate allowable types. |
|
596 | + // @link http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2 |
|
597 | + $allowable_types = array_flip( |
|
598 | + array( |
|
599 | + 'string', |
|
600 | + 'number', |
|
601 | + 'null', |
|
602 | + 'object', |
|
603 | + 'array', |
|
604 | + 'boolean', |
|
605 | + 'integer' |
|
606 | + ) |
|
607 | + ); |
|
608 | + |
|
609 | + if (is_array($type)) { |
|
610 | + foreach ($type as $item_in_type) { |
|
611 | + $this->validateSchemaType($item_in_type); |
|
612 | + } |
|
613 | + return; |
|
614 | + } |
|
615 | + |
|
616 | + if (! isset($allowable_types[ $type ])) { |
|
617 | + throw new InvalidArgumentException( |
|
618 | + sprintf( |
|
619 | + esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
|
620 | + $type, |
|
621 | + implode(',', array_flip($allowable_types)) |
|
622 | + ) |
|
623 | + ); |
|
624 | + } |
|
625 | + } |
|
626 | + |
|
627 | + |
|
628 | + /** |
|
629 | + * Validates that the incoming format is an allowable string to use for the _schema_format property |
|
630 | + * @throws InvalidArgumentException |
|
631 | + * @param $format |
|
632 | + */ |
|
633 | + private function validateSchemaFormat($format) |
|
634 | + { |
|
635 | + if (! is_string($format)) { |
|
636 | + throw new InvalidArgumentException( |
|
637 | + sprintf( |
|
638 | + esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
|
639 | + print_r($format, true) |
|
640 | + ) |
|
641 | + ); |
|
642 | + } |
|
643 | + |
|
644 | + // validate allowable format values |
|
645 | + // @link http://json-schema.org/latest/json-schema-validation.html#rfc.section.7 |
|
646 | + $allowable_formats = array_flip( |
|
647 | + array( |
|
648 | + 'date-time', |
|
649 | + 'email', |
|
650 | + 'hostname', |
|
651 | + 'ipv4', |
|
652 | + 'ipv6', |
|
653 | + 'uri', |
|
654 | + 'uriref' |
|
655 | + ) |
|
656 | + ); |
|
657 | + |
|
658 | + if (! isset($allowable_formats[ $format ])) { |
|
659 | + throw new InvalidArgumentException( |
|
660 | + sprintf( |
|
661 | + esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
|
662 | + $format, |
|
663 | + implode(',', array_flip($allowable_formats)) |
|
664 | + ) |
|
665 | + ); |
|
666 | + } |
|
667 | + } |
|
669 | 668 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | /** |
128 | 128 | * allow for changing the defaults |
129 | 129 | */ |
130 | - $this->_nicename = apply_filters( |
|
130 | + $this->_nicename = apply_filters( |
|
131 | 131 | 'FHEE__EE_Model_Field_Base___construct_finalize___nicename', |
132 | 132 | $this->_nicename, |
133 | 133 | $this |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | */ |
218 | 218 | public function get_qualified_column() |
219 | 219 | { |
220 | - return $this->get_table_alias() . "." . $this->get_table_column(); |
|
220 | + return $this->get_table_alias().".".$this->get_table_column(); |
|
221 | 221 | } |
222 | 222 | |
223 | 223 | /** |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | { |
313 | 313 | if ($this->is_nullable()) { |
314 | 314 | $this->_schema_type = (array) $this->_schema_type; |
315 | - if (! in_array('null', $this->_schema_type)) { |
|
315 | + if ( ! in_array('null', $this->_schema_type)) { |
|
316 | 316 | $this->_schema_type[] = 'null'; |
317 | 317 | }; |
318 | 318 | } |
@@ -369,10 +369,10 @@ discard block |
||
369 | 369 | switch ($property_key) { |
370 | 370 | case 'pretty': |
371 | 371 | case 'rendered': |
372 | - $value_to_return[ $property_key ] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
372 | + $value_to_return[$property_key] = $this->prepare_for_pretty_echoing($this->prepare_for_set($default_value)); |
|
373 | 373 | break; |
374 | 374 | default: |
375 | - $value_to_return[ $property_key ] = $default_value; |
|
375 | + $value_to_return[$property_key] = $default_value; |
|
376 | 376 | break; |
377 | 377 | } |
378 | 378 | } |
@@ -437,7 +437,7 @@ discard block |
||
437 | 437 | */ |
438 | 438 | protected function setSchemaReadOnly($readonly) |
439 | 439 | { |
440 | - if (! is_bool($readonly)) { |
|
440 | + if ( ! is_bool($readonly)) { |
|
441 | 441 | throw new InvalidArgumentException( |
442 | 442 | sprintf( |
443 | 443 | esc_html__('The incoming argument (%s) must be a boolean.', 'event_espresso'), |
@@ -584,7 +584,7 @@ discard block |
||
584 | 584 | */ |
585 | 585 | private function validateSchemaType($type) |
586 | 586 | { |
587 | - if (! (is_string($type) || is_array($type))) { |
|
587 | + if ( ! (is_string($type) || is_array($type))) { |
|
588 | 588 | throw new InvalidArgumentException( |
589 | 589 | sprintf( |
590 | 590 | esc_html__('The incoming argument (%s) must be a string or an array.', 'event_espresso'), |
@@ -614,7 +614,7 @@ discard block |
||
614 | 614 | return; |
615 | 615 | } |
616 | 616 | |
617 | - if (! isset($allowable_types[ $type ])) { |
|
617 | + if ( ! isset($allowable_types[$type])) { |
|
618 | 618 | throw new InvalidArgumentException( |
619 | 619 | sprintf( |
620 | 620 | esc_html__('The incoming argument (%1$s) must be one of the allowable types: %2$s', 'event_espresso'), |
@@ -633,7 +633,7 @@ discard block |
||
633 | 633 | */ |
634 | 634 | private function validateSchemaFormat($format) |
635 | 635 | { |
636 | - if (! is_string($format)) { |
|
636 | + if ( ! is_string($format)) { |
|
637 | 637 | throw new InvalidArgumentException( |
638 | 638 | sprintf( |
639 | 639 | esc_html__('The incoming argument (%s) must be a string.', 'event_espresso'), |
@@ -656,7 +656,7 @@ discard block |
||
656 | 656 | ) |
657 | 657 | ); |
658 | 658 | |
659 | - if (! isset($allowable_formats[ $format ])) { |
|
659 | + if ( ! isset($allowable_formats[$format])) { |
|
660 | 660 | throw new InvalidArgumentException( |
661 | 661 | sprintf( |
662 | 662 | esc_html__('The incoming argument (%1$s) must be one of the allowable formats: %2$s', 'event_espresso'), |
@@ -12,7 +12,7 @@ |
||
12 | 12 | * @param string $table_column |
13 | 13 | * @param string $nicename |
14 | 14 | * @param bool $nullable |
15 | - * @param null $default_value |
|
15 | + * @param integer $default_value |
|
16 | 16 | */ |
17 | 17 | public function __construct($table_column, $nicename, $nullable, $default_value = null) |
18 | 18 | { |
@@ -7,92 +7,92 @@ |
||
7 | 7 | class EE_Money_Field extends EE_Float_Field |
8 | 8 | { |
9 | 9 | |
10 | - /** |
|
11 | - * @param string $table_column |
|
12 | - * @param string $nicename |
|
13 | - * @param bool $nullable |
|
14 | - * @param null $default_value |
|
15 | - */ |
|
16 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
17 | - { |
|
18 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
19 | - $this->setSchemaType('object'); |
|
20 | - } |
|
10 | + /** |
|
11 | + * @param string $table_column |
|
12 | + * @param string $nicename |
|
13 | + * @param bool $nullable |
|
14 | + * @param null $default_value |
|
15 | + */ |
|
16 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
17 | + { |
|
18 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
19 | + $this->setSchemaType('object'); |
|
20 | + } |
|
21 | 21 | |
22 | 22 | |
23 | - /** |
|
24 | - * Schemas: |
|
25 | - * 'localized_float': "3,023.00" |
|
26 | - * 'no_currency_code': "$3,023.00" |
|
27 | - * null: "$3,023.00<span>USD</span>" |
|
28 | - * |
|
29 | - * @param string $value_on_field_to_be_outputted |
|
30 | - * @param string $schema |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
34 | - { |
|
35 | - $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
23 | + /** |
|
24 | + * Schemas: |
|
25 | + * 'localized_float': "3,023.00" |
|
26 | + * 'no_currency_code': "$3,023.00" |
|
27 | + * null: "$3,023.00<span>USD</span>" |
|
28 | + * |
|
29 | + * @param string $value_on_field_to_be_outputted |
|
30 | + * @param string $schema |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
34 | + { |
|
35 | + $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
36 | 36 | |
37 | - if ($schema == 'localized_float') { |
|
38 | - return $pretty_float; |
|
39 | - } |
|
40 | - if ($schema == 'no_currency_code') { |
|
37 | + if ($schema == 'localized_float') { |
|
38 | + return $pretty_float; |
|
39 | + } |
|
40 | + if ($schema == 'no_currency_code') { |
|
41 | 41 | // echo "schema no currency!"; |
42 | - $display_code = false; |
|
43 | - } else { |
|
44 | - $display_code = true; |
|
45 | - } |
|
46 | - // we don't use the $pretty_float because format_currency will take care of it. |
|
47 | - return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
48 | - } |
|
42 | + $display_code = false; |
|
43 | + } else { |
|
44 | + $display_code = true; |
|
45 | + } |
|
46 | + // we don't use the $pretty_float because format_currency will take care of it. |
|
47 | + return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
52 | - * Rounds the float to the correct number of decimal places for this country's currency. |
|
53 | - * Also, interprets periods and commas according to the country's currency settings. |
|
54 | - * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
55 | - * |
|
56 | - * @param string $value_inputted_for_field_on_model_object |
|
57 | - * @return float |
|
58 | - */ |
|
59 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
60 | - { |
|
61 | - // remove any currencies etc. |
|
50 | + /** |
|
51 | + * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
52 | + * Rounds the float to the correct number of decimal places for this country's currency. |
|
53 | + * Also, interprets periods and commas according to the country's currency settings. |
|
54 | + * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
55 | + * |
|
56 | + * @param string $value_inputted_for_field_on_model_object |
|
57 | + * @return float |
|
58 | + */ |
|
59 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
60 | + { |
|
61 | + // remove any currencies etc. |
|
62 | 62 | // if(is_string($value_inputted_for_field_on_model_object)){ |
63 | 63 | // $value_inputted_for_field_on_model_object = preg_replace("/[^0-9,.]/", "", $value_inputted_for_field_on_model_object); |
64 | 64 | // } |
65 | - // now it's a float-style string or number |
|
66 | - $float_val = parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
67 | - // round to the correctly number of decimal places for this currency |
|
68 | - $rounded_value = round($float_val, EE_Registry::instance()->CFG->currency->dec_plc); |
|
69 | - return $rounded_value; |
|
70 | - } |
|
65 | + // now it's a float-style string or number |
|
66 | + $float_val = parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
67 | + // round to the correctly number of decimal places for this currency |
|
68 | + $rounded_value = round($float_val, EE_Registry::instance()->CFG->currency->dec_plc); |
|
69 | + return $rounded_value; |
|
70 | + } |
|
71 | 71 | |
72 | - public function prepare_for_get($value_of_field_on_model_object) |
|
73 | - { |
|
74 | - $c = EE_Registry::instance()->CFG->currency; |
|
75 | - return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc); |
|
76 | - } |
|
72 | + public function prepare_for_get($value_of_field_on_model_object) |
|
73 | + { |
|
74 | + $c = EE_Registry::instance()->CFG->currency; |
|
75 | + return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc); |
|
76 | + } |
|
77 | 77 | |
78 | - public function getSchemaProperties() |
|
79 | - { |
|
80 | - return array( |
|
81 | - 'raw' => array( |
|
82 | - 'description' => sprintf( |
|
83 | - __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
84 | - $this->get_nicename() |
|
85 | - ), |
|
86 | - 'type' => 'number', |
|
87 | - ), |
|
88 | - 'pretty' => array( |
|
89 | - 'description' => sprintf( |
|
90 | - __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
91 | - $this->get_nicename() |
|
92 | - ), |
|
93 | - 'type' => 'string', |
|
94 | - 'format' => 'money' |
|
95 | - ) |
|
96 | - ); |
|
97 | - } |
|
78 | + public function getSchemaProperties() |
|
79 | + { |
|
80 | + return array( |
|
81 | + 'raw' => array( |
|
82 | + 'description' => sprintf( |
|
83 | + __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
84 | + $this->get_nicename() |
|
85 | + ), |
|
86 | + 'type' => 'number', |
|
87 | + ), |
|
88 | + 'pretty' => array( |
|
89 | + 'description' => sprintf( |
|
90 | + __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
91 | + $this->get_nicename() |
|
92 | + ), |
|
93 | + 'type' => 'string', |
|
94 | + 'format' => 'money' |
|
95 | + ) |
|
96 | + ); |
|
97 | + } |
|
98 | 98 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | public function __construct($table_column, $nicename, $nullable, $default_value = null) |
20 | 20 | { |
21 | 21 | parent::__construct($table_column, $nicename, $nullable, $default_value); |
22 | - $this->setSchemaType(array('object','string')); |
|
22 | + $this->setSchemaType(array('object', 'string')); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 |
@@ -9,72 +9,72 @@ |
||
9 | 9 | class EE_Serialized_Text_Field extends EE_Text_Field_Base |
10 | 10 | { |
11 | 11 | |
12 | - /** |
|
13 | - * @param string $table_column |
|
14 | - * @param string $nicename |
|
15 | - * @param bool $nullable |
|
16 | - * @param null $default_value |
|
17 | - */ |
|
18 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
19 | - { |
|
20 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
21 | - $this->setSchemaType(array('object','string')); |
|
22 | - } |
|
12 | + /** |
|
13 | + * @param string $table_column |
|
14 | + * @param string $nicename |
|
15 | + * @param bool $nullable |
|
16 | + * @param null $default_value |
|
17 | + */ |
|
18 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
19 | + { |
|
20 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
21 | + $this->setSchemaType(array('object','string')); |
|
22 | + } |
|
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * Value SHOULD be an array, and we want to now convert it to a serialized string |
|
27 | - * |
|
28 | - * @param array $value_of_field_on_model_object |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
32 | - { |
|
33 | - return maybe_serialize($value_of_field_on_model_object); |
|
34 | - } |
|
25 | + /** |
|
26 | + * Value SHOULD be an array, and we want to now convert it to a serialized string |
|
27 | + * |
|
28 | + * @param array $value_of_field_on_model_object |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function prepare_for_use_in_db($value_of_field_on_model_object) |
|
32 | + { |
|
33 | + return maybe_serialize($value_of_field_on_model_object); |
|
34 | + } |
|
35 | 35 | |
36 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
37 | - { |
|
38 | - $value_inputted_for_field_on_model_object = EEH_Array::maybe_unserialize($value_inputted_for_field_on_model_object); |
|
39 | - if (is_string($value_inputted_for_field_on_model_object)) { |
|
40 | - return parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
41 | - } elseif (is_array($value_inputted_for_field_on_model_object)) { |
|
42 | - return array_map(array($this, 'prepare_for_set'), $value_inputted_for_field_on_model_object); |
|
43 | - } else {// so they passed NULL or an INT or something wack |
|
44 | - return $value_inputted_for_field_on_model_object; |
|
45 | - } |
|
46 | - } |
|
36 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
37 | + { |
|
38 | + $value_inputted_for_field_on_model_object = EEH_Array::maybe_unserialize($value_inputted_for_field_on_model_object); |
|
39 | + if (is_string($value_inputted_for_field_on_model_object)) { |
|
40 | + return parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
41 | + } elseif (is_array($value_inputted_for_field_on_model_object)) { |
|
42 | + return array_map(array($this, 'prepare_for_set'), $value_inputted_for_field_on_model_object); |
|
43 | + } else {// so they passed NULL or an INT or something wack |
|
44 | + return $value_inputted_for_field_on_model_object; |
|
45 | + } |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Value provided should definetely be a serialized string. We should unserialize into an array |
|
50 | - * |
|
51 | - * @param string $value_found_in_db_for_model_object |
|
52 | - * @return array |
|
53 | - */ |
|
54 | - public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
55 | - { |
|
56 | - return EEH_Array::maybe_unserialize($value_found_in_db_for_model_object); |
|
57 | - } |
|
48 | + /** |
|
49 | + * Value provided should definetely be a serialized string. We should unserialize into an array |
|
50 | + * |
|
51 | + * @param string $value_found_in_db_for_model_object |
|
52 | + * @return array |
|
53 | + */ |
|
54 | + public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
|
55 | + { |
|
56 | + return EEH_Array::maybe_unserialize($value_found_in_db_for_model_object); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Gets a string representation of the array |
|
61 | - * |
|
62 | - * @param type $value_on_field_to_be_outputted |
|
63 | - * @param string $schema , possible values are ',', others can be added |
|
64 | - * @return string |
|
65 | - */ |
|
66 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
67 | - { |
|
68 | - switch ($schema) { |
|
69 | - case 'print_r': |
|
70 | - $pretty_value = print_r($value_on_field_to_be_outputted, true); |
|
71 | - break; |
|
72 | - case 'as_table': |
|
73 | - $pretty_value = EEH_Template::layout_array_as_table($value_on_field_to_be_outputted); |
|
74 | - break; |
|
75 | - default: |
|
76 | - $pretty_value = implode(", ", $value_on_field_to_be_outputted); |
|
77 | - } |
|
78 | - return $pretty_value; |
|
79 | - } |
|
59 | + /** |
|
60 | + * Gets a string representation of the array |
|
61 | + * |
|
62 | + * @param type $value_on_field_to_be_outputted |
|
63 | + * @param string $schema , possible values are ',', others can be added |
|
64 | + * @return string |
|
65 | + */ |
|
66 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
67 | + { |
|
68 | + switch ($schema) { |
|
69 | + case 'print_r': |
|
70 | + $pretty_value = print_r($value_on_field_to_be_outputted, true); |
|
71 | + break; |
|
72 | + case 'as_table': |
|
73 | + $pretty_value = EEH_Template::layout_array_as_table($value_on_field_to_be_outputted); |
|
74 | + break; |
|
75 | + default: |
|
76 | + $pretty_value = implode(", ", $value_on_field_to_be_outputted); |
|
77 | + } |
|
78 | + return $pretty_value; |
|
79 | + } |
|
80 | 80 | } |
@@ -3,16 +3,16 @@ |
||
3 | 3 | |
4 | 4 | class EE_DB_Only_Float_Field extends EE_DB_Only_Field_Base |
5 | 5 | { |
6 | - /** |
|
7 | - * @param string $table_column |
|
8 | - * @param string $nicename |
|
9 | - * @param bool $nullable |
|
10 | - * @param null $default_value |
|
11 | - */ |
|
12 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | - { |
|
14 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | - $this->setSchemaType('number'); |
|
16 | - } |
|
6 | + /** |
|
7 | + * @param string $table_column |
|
8 | + * @param string $nicename |
|
9 | + * @param bool $nullable |
|
10 | + * @param null $default_value |
|
11 | + */ |
|
12 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
13 | + { |
|
14 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
15 | + $this->setSchemaType('number'); |
|
16 | + } |
|
17 | 17 | |
18 | 18 | } |
@@ -1,15 +1,15 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | //echo '<br/><h6 style="color:#2EA2CC;">'. __FILE__ . ' <span style="font-weight:normal;color:#E76700"> Line #: ' . __LINE__ . '</span></h6>'; |
3 | 3 | global $post; |
4 | -do_action( 'AHEE_event_details_before_featured_img', $post ); |
|
4 | +do_action('AHEE_event_details_before_featured_img', $post); |
|
5 | 5 | |
6 | -if ( has_post_thumbnail( $post->ID )) : |
|
7 | - if ( $img_ID = get_post_thumbnail_id( $post->ID )) : |
|
8 | - if ( $featured_img = wp_get_attachment_image_src( $img_ID, 'large' )) : |
|
9 | - $caption = esc_attr( get_post( get_post( $img_ID ))->post_excerpt ); |
|
6 | +if (has_post_thumbnail($post->ID)) : |
|
7 | + if ($img_ID = get_post_thumbnail_id($post->ID)) : |
|
8 | + if ($featured_img = wp_get_attachment_image_src($img_ID, 'large')) : |
|
9 | + $caption = esc_attr(get_post(get_post($img_ID))->post_excerpt); |
|
10 | 10 | ?> |
11 | 11 | <div id="ee-event-img-dv-<?php echo $post->ID; ?>" class="ee-event-img-dv"> |
12 | - <a class="ee-event-img-lnk" href="<?php the_permalink(); ?>"<?php echo \EED_Events_Archive::link_target();?>> |
|
12 | + <a class="ee-event-img-lnk" href="<?php the_permalink(); ?>"<?php echo \EED_Events_Archive::link_target(); ?>> |
|
13 | 13 | <img class="ee-event-img" src="<?php echo $featured_img[0]; ?>" width="<?php echo $featured_img[1]; ?>" height="<?php echo $featured_img[2]; ?>" alt="<?php echo $caption; ?>"/> |
14 | 14 | </a> |
15 | 15 | </div> |
@@ -18,4 +18,4 @@ discard block |
||
18 | 18 | endif; |
19 | 19 | endif; |
20 | 20 | ?> |
21 | -<?php do_action( 'AHEE_event_details_after_featured_img', $post );?> |
|
21 | +<?php do_action('AHEE_event_details_after_featured_img', $post); ?> |