Completed
Branch ENH/stop-messages-extra-meta (9692b0)
by
unknown
12:25 queued 10:11
created
core/db_classes/EE_Message.class.php 1 patch
Indentation   +869 added lines, -869 removed lines patch added patch discarded remove patch
@@ -9,877 +9,877 @@
 block discarded – undo
9 9
  */
10 10
 class EE_Message extends EE_Base_Class implements EEI_Admin_Links
11 11
 {
12
-    /**
13
-     * @deprecated 4.9.0  Added for backward compat with add-on's
14
-     * @type null
15
-     */
16
-    public $template_pack;
17
-
18
-    /**
19
-     * @deprecated 4.9.0 Added for backward compat with add-on's
20
-     * @type null
21
-     */
22
-    public $template_variation;
23
-
24
-    /**
25
-     * @deprecated 4.9.0 Added for backward compat with add-on's
26
-     * @type string
27
-     */
28
-    public $content = '';
29
-
30
-
31
-    /**
32
-     * @type EE_messenger $_messenger
33
-     */
34
-    protected $_messenger = null;
35
-
36
-    /**
37
-     * @type EE_message_type $_message_type
38
-     */
39
-    protected $_message_type = null;
40
-
41
-
42
-    /**
43
-     * @param array  $props_n_values
44
-     * @param string $timezone
45
-     * @param array  $date_formats incoming date formats in an array.  First value is the date_format, second is time
46
-     *                             format.
47
-     * @return EE_Message
48
-     */
49
-    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
50
-    {
51
-        $has_object = parent::_check_for_object($props_n_values, __CLASS__);
52
-        // if object doesn't exist, let's generate a unique token on instantiation so that its available even before saving to db.
53
-        if (! $has_object) {
54
-            EE_Registry::instance()->load_helper('URL');
55
-            $props_n_values['MSG_token'] = EEH_URL::generate_unique_token();
56
-        }
57
-        return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
58
-    }
59
-
60
-
61
-    /**
62
-     * @param array  $props_n_values
63
-     * @param string $timezone
64
-     * @return EE_Message
65
-     */
66
-    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
67
-    {
68
-        return new self($props_n_values, true, $timezone);
69
-    }
70
-
71
-
72
-    /**
73
-     * Gets MSG_token
74
-     *
75
-     * @return int
76
-     */
77
-    public function MSG_token()
78
-    {
79
-        return $this->get('MSG_token');
80
-    }
81
-
82
-
83
-    /**
84
-     * Sets MSG_token
85
-     *
86
-     * @param int $MSG_token
87
-     */
88
-    public function set_MSG_token($MSG_token)
89
-    {
90
-        $this->set('MSG_token', $MSG_token);
91
-    }
92
-
93
-
94
-    /**
95
-     * Gets GRP_ID
96
-     *
97
-     * @return int
98
-     */
99
-    public function GRP_ID()
100
-    {
101
-        return $this->get('GRP_ID');
102
-    }
103
-
104
-
105
-    /**
106
-     * Sets GRP_ID
107
-     *
108
-     * @param int $GRP_ID
109
-     */
110
-    public function set_GRP_ID($GRP_ID)
111
-    {
112
-        $this->set('GRP_ID', $GRP_ID);
113
-    }
114
-
115
-
116
-    /**
117
-     * Gets TXN_ID
118
-     *
119
-     * @return int
120
-     */
121
-    public function TXN_ID()
122
-    {
123
-        return $this->get('TXN_ID');
124
-    }
125
-
126
-
127
-    /**
128
-     * Sets TXN_ID
129
-     *
130
-     * @param int $TXN_ID
131
-     */
132
-    public function set_TXN_ID($TXN_ID)
133
-    {
134
-        $this->set('TXN_ID', $TXN_ID);
135
-    }
136
-
137
-
138
-    /**
139
-     * Gets messenger
140
-     *
141
-     * @return string
142
-     */
143
-    public function messenger()
144
-    {
145
-        return $this->get('MSG_messenger');
146
-    }
147
-
148
-
149
-    /**
150
-     * Sets messenger
151
-     *
152
-     * @param string $messenger
153
-     */
154
-    public function set_messenger($messenger)
155
-    {
156
-        $this->set('MSG_messenger', $messenger);
157
-    }
158
-
159
-
160
-    /**
161
-     * Returns corresponding messenger object for the set messenger on this message
162
-     *
163
-     * @return EE_messenger | null
164
-     */
165
-    public function messenger_object()
166
-    {
167
-        return $this->_messenger;
168
-    }
169
-
170
-
171
-    /**
172
-     * Sets messenger
173
-     *
174
-     * @param EE_messenger $messenger
175
-     */
176
-    public function set_messenger_object(EE_messenger $messenger)
177
-    {
178
-        $this->_messenger = $messenger;
179
-    }
180
-
181
-
182
-    /**
183
-     * validates messenger
184
-     *
185
-     * @param bool $throw_exceptions
186
-     * @return bool
187
-     * @throws \EE_Error
188
-     */
189
-    public function valid_messenger($throw_exceptions = false)
190
-    {
191
-        if ($this->_messenger instanceof EE_messenger) {
192
-            return true;
193
-        }
194
-        if ($throw_exceptions) {
195
-            throw new EE_Error(
196
-                sprintf(
197
-                    esc_html__(
198
-                        'The "%1$s" messenger set for this message is missing or invalid. Please double-check the spelling and verify that the correct files exist.',
199
-                        'event_espresso'
200
-                    ),
201
-                    $this->messenger()
202
-                )
203
-            );
204
-        }
205
-        return false;
206
-    }
207
-
208
-
209
-    /**
210
-     * This returns the set localized label for the messenger on this message.
211
-     * Note, if unable to retrieve the EE_messenger object then will just return the messenger slug saved
212
-     * with this message.
213
-     *
214
-     * @param   bool $plural whether to return the plural label or not.
215
-     * @return string
216
-     */
217
-    public function messenger_label($plural = false)
218
-    {
219
-        $label_type = $plural ? 'plural' : 'singular';
220
-        $messenger = $this->messenger_object();
221
-        return $messenger instanceof EE_messenger ? $messenger->label[ $label_type ] : $this->messenger();
222
-    }
223
-
224
-
225
-    /**
226
-     * Gets message_type
227
-     *
228
-     * @return string
229
-     */
230
-    public function message_type()
231
-    {
232
-        return $this->get('MSG_message_type');
233
-    }
234
-
235
-
236
-    /**
237
-     * Sets message_type
238
-     *
239
-     * @param string $message_type
240
-     */
241
-    public function set_message_type($message_type)
242
-    {
243
-        $this->set('MSG_message_type', $message_type);
244
-    }
245
-
246
-
247
-    /**
248
-     * Returns the message type object for the set message type on this message
249
-     *
250
-     * @return EE_message_type | null
251
-     */
252
-    public function message_type_object()
253
-    {
254
-        return $this->_message_type;
255
-    }
256
-
257
-
258
-    /**
259
-     * Sets message_type
260
-     *
261
-     * @param EE_message_type $message_type
262
-     * @param bool            $set_priority   This indicates whether to set the priority to whatever the priority is on
263
-     *                                        the message type or not.
264
-     */
265
-    public function set_message_type_object(EE_message_type $message_type, $set_priority = false)
266
-    {
267
-        $this->_message_type = $message_type;
268
-        if ($set_priority) {
269
-            $this->set_priority($this->_message_type->get_priority());
270
-        }
271
-    }
272
-
273
-
274
-    /**
275
-     * validates message_type
276
-     *
277
-     * @param bool $throw_exceptions
278
-     * @return bool
279
-     * @throws \EE_Error
280
-     */
281
-    public function valid_message_type($throw_exceptions = false)
282
-    {
283
-        if ($this->_message_type instanceof EE_message_type) {
284
-            return true;
285
-        }
286
-        if ($throw_exceptions) {
287
-            throw new EE_Error(
288
-                sprintf(
289
-                    esc_html__(
290
-                        '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.',
291
-                        'event_espresso'
292
-                    ),
293
-                    $this->message_type()
294
-                )
295
-            );
296
-        }
297
-        return false;
298
-    }
299
-
300
-
301
-    /**
302
-     * validates messenger and message_type (that they are valid EE_messenger and EE_message_type objects).
303
-     *
304
-     * @param bool $throw_exceptions
305
-     * @return bool
306
-     * @throws \EE_Error
307
-     */
308
-    public function is_valid($throw_exceptions = false)
309
-    {
310
-        if ($this->valid_messenger($throw_exceptions) && $this->valid_message_type($throw_exceptions)) {
311
-            return true;
312
-        }
313
-        return false;
314
-    }
315
-
316
-
317
-    /**
318
-     * This validates whether the internal messenger and message type objects are valid for sending.
319
-     * Three checks are done:
320
-     * 1. There is a valid messenger object.
321
-     * 2. There is a valid message type object.
322
-     * 3. The message type object is active for the messenger.
323
-     *
324
-     * @throws EE_Error  But only if $throw_exceptions is set to true.
325
-     * @param bool $throw_exceptions
326
-     * @return bool
327
-     */
328
-    public function is_valid_for_sending_or_generation($throw_exceptions = false)
329
-    {
330
-        $valid = false;
331
-        if ($this->is_valid($throw_exceptions)) {
332
-            /** @var EE_Message_Resource_Manager $message_resource_manager */
333
-            $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
334
-            $valid = $message_resource_manager->is_message_type_active_for_messenger(
335
-                $this->messenger(),
336
-                $this->message_type()
337
-            );
338
-            if (! $valid && $throw_exceptions) {
339
-                throw new EE_Error(
340
-                    sprintf(
341
-                        esc_html__(
342
-                            'The %1$s message type is not a valid message type for the %2$s messenger so it will not be sent.',
343
-                            'event_espresso'
344
-                        ),
345
-                        $this->message_type(),
346
-                        $this->messenger()
347
-                    )
348
-                );
349
-            }
350
-        }
351
-        return $valid;
352
-    }
353
-
354
-
355
-    /**
356
-     * This returns the set localized label for the message type on this message.
357
-     * Note, if unable to retrieve the EE_message_type object then will just return the message type slug saved
358
-     * with this message.
359
-     *
360
-     * @param   bool $plural whether to return the plural label or not.
361
-     * @return string
362
-     */
363
-    public function message_type_label($plural = false)
364
-    {
365
-        $label_type = $plural ? 'plural' : 'singular';
366
-        $message_type = $this->message_type_object();
367
-        return $message_type instanceof EE_message_type
368
-            ? $message_type->label[ $label_type ]
369
-            : str_replace(
370
-                '_',
371
-                ' ',
372
-                $this->message_type()
373
-            );
374
-    }
375
-
376
-
377
-    /**
378
-     * Gets context
379
-     *
380
-     * @return string
381
-     */
382
-    public function context()
383
-    {
384
-        return $this->get('MSG_context');
385
-    }
386
-
387
-
388
-    /**
389
-     * This returns the corresponding localized label for the given context slug, if possible from installed message
390
-     * types. Otherwise, this will just return the set context slug on this object.
391
-     *
392
-     * @return string
393
-     */
394
-    public function context_label()
395
-    {
396
-        /** @type EE_Message_Resource_Manager $message_resource_manager */
397
-        $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
398
-        $contexts = $message_resource_manager->get_all_contexts();
399
-        return isset($contexts[ $this->context() ]) ? $contexts[ $this->context() ] : $this->context();
400
-    }
401
-
402
-
403
-    /**
404
-     * Sets context
405
-     *
406
-     * @param string $context
407
-     */
408
-    public function set_context($context)
409
-    {
410
-        $this->set('MSG_context', $context);
411
-    }
412
-
413
-
414
-    /**
415
-     * Gets recipient_ID
416
-     *
417
-     * @return int
418
-     */
419
-    public function recipient_ID()
420
-    {
421
-        return $this->get('MSG_recipient_ID');
422
-    }
423
-
424
-
425
-    /**
426
-     * Sets recipient_ID
427
-     *
428
-     * @param string $recipient_ID
429
-     */
430
-    public function set_recipient_ID($recipient_ID)
431
-    {
432
-        $this->set('MSG_recipient_ID', $recipient_ID);
433
-    }
434
-
435
-
436
-    /**
437
-     * Gets recipient_type
438
-     *
439
-     * @return string
440
-     */
441
-    public function recipient_type()
442
-    {
443
-        return $this->get('MSG_recipient_type');
444
-    }
445
-
446
-
447
-    /**
448
-     * Return the related object matching the recipient type and ID.
449
-     *
450
-     * @return EE_Base_Class | null
451
-     */
452
-    public function recipient_object()
453
-    {
454
-        if (! $this->recipient_type() || ! $this->recipient_ID()) {
455
-            return null;
456
-        }
457
-
458
-        return $this->get_first_related($this->recipient_type());
459
-    }
460
-
461
-
462
-    /**
463
-     * Sets recipient_type
464
-     *
465
-     * @param string $recipient_type
466
-     */
467
-    public function set_recipient_type($recipient_type)
468
-    {
469
-        $this->set('MSG_recipient_type', $recipient_type);
470
-    }
471
-
472
-
473
-    /**
474
-     * Gets content
475
-     *
476
-     * @return string
477
-     */
478
-    public function content()
479
-    {
480
-        return $this->get('MSG_content');
481
-    }
482
-
483
-
484
-    /**
485
-     * Sets content
486
-     *
487
-     * @param string $content
488
-     */
489
-    public function set_content($content)
490
-    {
491
-        $this->set('MSG_content', $content);
492
-    }
493
-
494
-
495
-    /**
496
-     * Gets subject
497
-     *
498
-     * @return string
499
-     */
500
-    public function subject()
501
-    {
502
-        return $this->get('MSG_subject');
503
-    }
504
-
505
-
506
-    /**
507
-     * Sets subject
508
-     *
509
-     * @param string $subject
510
-     */
511
-    public function set_subject($subject)
512
-    {
513
-        $this->set('MSG_subject', $subject);
514
-    }
515
-
516
-
517
-    /**
518
-     * Gets to
519
-     *
520
-     * @return string
521
-     */
522
-    public function to()
523
-    {
524
-        $to = $this->get('MSG_to');
525
-        return empty($to) ? esc_html__('No recipient', 'event_espresso') : $to;
526
-    }
527
-
528
-
529
-    /**
530
-     * Sets to
531
-     *
532
-     * @param string $to
533
-     */
534
-    public function set_to($to)
535
-    {
536
-        $this->set('MSG_to', $to);
537
-    }
538
-
539
-
540
-    /**
541
-     * Gets from
542
-     *
543
-     * @return string
544
-     */
545
-    public function from()
546
-    {
547
-        return $this->get('MSG_from');
548
-    }
549
-
550
-
551
-    /**
552
-     * Sets from
553
-     *
554
-     * @param string $from
555
-     */
556
-    public function set_from($from)
557
-    {
558
-        $this->set('MSG_from', $from);
559
-    }
560
-
561
-
562
-    /**
563
-     * Gets priority
564
-     *
565
-     * @return int
566
-     */
567
-    public function priority()
568
-    {
569
-        return $this->get('MSG_priority');
570
-    }
571
-
572
-
573
-    /**
574
-     * Sets priority
575
-     * Note.  Send Now Messengers always override any priority that may be set on a Message.  So
576
-     * this method calls the send_now method to verify that.
577
-     *
578
-     * @param int $priority
579
-     */
580
-    public function set_priority($priority)
581
-    {
582
-        $priority = $this->send_now() ? EEM_Message::priority_high : $priority;
583
-        parent::set('MSG_priority', $priority);
584
-    }
585
-
586
-
587
-    /**
588
-     * Overrides parent::set method so we can capture any sets for priority.
589
-     *
590
-     * @see parent::set() for phpdocs
591
-     * @param string $field_name
592
-     * @param mixed  $field_value
593
-     * @param bool   $use_default
594
-     * @throws EE_Error
595
-     */
596
-    public function set($field_name, $field_value, $use_default = false)
597
-    {
598
-        if ($field_name === 'MSG_priority') {
599
-            $this->set_priority($field_value);
600
-        }
601
-        parent::set($field_name, $field_value, $use_default);
602
-    }
603
-
604
-
605
-    /**
606
-     * @return bool
607
-     * @throws \EE_Error
608
-     */
609
-    public function send_now()
610
-    {
611
-        $send_now = $this->valid_messenger() && $this->messenger_object()->send_now() ? EEM_Message::priority_high
612
-            : $this->priority();
613
-        return $send_now === EEM_Message::priority_high ? true : false;
614
-    }
615
-
616
-
617
-    /**
618
-     * Gets STS_ID
619
-     *
620
-     * @return string
621
-     */
622
-    public function STS_ID()
623
-    {
624
-        return $this->get('STS_ID');
625
-    }
626
-
627
-
628
-    /**
629
-     * Sets STS_ID
630
-     *
631
-     * @param string $STS_ID
632
-     */
633
-    public function set_STS_ID($STS_ID)
634
-    {
635
-        $this->set('STS_ID', $STS_ID);
636
-    }
637
-
638
-
639
-    /**
640
-     * Gets created
641
-     *
642
-     * @return string
643
-     */
644
-    public function created()
645
-    {
646
-        return $this->get('MSG_created');
647
-    }
648
-
649
-
650
-    /**
651
-     * Sets created
652
-     *
653
-     * @param string $created
654
-     */
655
-    public function set_created($created)
656
-    {
657
-        $this->set('MSG_created', $created);
658
-    }
659
-
660
-
661
-    /**
662
-     * Gets modified
663
-     *
664
-     * @return string
665
-     */
666
-    public function modified()
667
-    {
668
-        return $this->get('MSG_modified');
669
-    }
670
-
671
-
672
-    /**
673
-     * Sets modified
674
-     *
675
-     * @param string $modified
676
-     */
677
-    public function set_modified($modified)
678
-    {
679
-        $this->set('MSG_modified', $modified);
680
-    }
681
-
682
-
683
-    /**
684
-     * Sets generation data for this message.
685
-     *
686
-     * @param mixed $data
687
-     */
688
-    public function set_generation_data($data)
689
-    {
690
-        $this->set_field_or_extra_meta('MSG_generation_data', $data);
691
-    }
692
-
693
-
694
-    /**
695
-     * Returns any set generation data for this message.
696
-     *
697
-     * @return mixed|null
698
-     */
699
-    public function get_generation_data()
700
-    {
701
-        return $this->get_field_or_extra_meta('MSG_generation_data');
702
-    }
703
-
704
-
705
-    /**
706
-     * Gets any error message.
707
-     *
708
-     * @return mixed|null
709
-     */
710
-    public function error_message()
711
-    {
712
-        return $this->get_field_or_extra_meta('MSG_error');
713
-    }
714
-
715
-
716
-    /**
717
-     * Sets an error message.
718
-     *
719
-     * @param $message
720
-     * @return bool|int
721
-     */
722
-    public function set_error_message($message)
723
-    {
724
-        return $this->set_field_or_extra_meta('MSG_error', $message);
725
-    }
726
-
727
-
728
-    /**
729
-     * This retrieves the associated template pack with this message.
730
-     *
731
-     * @return EE_Messages_Template_Pack | null
732
-     */
733
-    public function get_template_pack()
734
-    {
735
-        /**
736
-         * This is deprecated functionality that will be removed eventually but included here now for backward compat.
737
-         */
738
-        if (! empty($this->template_pack)) {
739
-            return $this->template_pack;
740
-        }
741
-        /** @type EE_Message_Template_Group $grp */
742
-        $grp = $this->get_first_related('Message_Template_Group');
743
-        // if no group then let's try to get the first related group by internal messenger and message type (will use global grp).
744
-        if (! $grp instanceof EE_Message_Template_Group) {
745
-            $grp = EEM_Message_Template_Group::instance()->get_one(
746
-                array(
747
-                    array(
748
-                        'MTP_messenger'    => $this->messenger(),
749
-                        'MTP_message_type' => $this->message_type(),
750
-                        'MTP_is_global'    => true,
751
-                    ),
752
-                )
753
-            );
754
-        }
755
-
756
-        return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack() : null;
757
-    }
758
-
759
-
760
-    /**
761
-     * Retrieves the variation used for generating this message.
762
-     *
763
-     * @return string
764
-     */
765
-    public function get_template_pack_variation()
766
-    {
767
-        /**
768
-         * This is deprecated functionality that will be removed eventually but included here now for backward compat.
769
-         */
770
-        if (! empty($this->template_variation)) {
771
-            return $this->template_variation;
772
-        }
773
-
774
-        /** @type EE_Message_Template_Group $grp */
775
-        $grp = $this->get_first_related('Message_Template_Group');
776
-
777
-        // if no group then let's try to get the first related group by internal messenger and message type (will use global grp).
778
-        if (! $grp instanceof EE_Message_Template_Group) {
779
-            $grp = EEM_Message_Template_Group::instance()->get_one(
780
-                array(
781
-                    array(
782
-                        'MTP_messenger'    => $this->messenger(),
783
-                        'MTP_message_type' => $this->message_type(),
784
-                        'MTP_is_global'    => true,
785
-                    ),
786
-                )
787
-            );
788
-        }
789
-
790
-        return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack_variation() : '';
791
-    }
792
-
793
-    /**
794
-     * Return the link to the admin details for the object.
795
-     *
796
-     * @return string
797
-     */
798
-    public function get_admin_details_link()
799
-    {
800
-        EE_Registry::instance()->load_helper('URL');
801
-        EE_Registry::instance()->load_helper('MSG_Template');
802
-        switch ($this->STS_ID()) {
803
-            case EEM_Message::status_failed:
804
-            case EEM_Message::status_debug_only:
805
-                return EEH_MSG_Template::generate_error_display_trigger($this);
806
-                break;
807
-
808
-            case EEM_Message::status_sent:
809
-                return EEH_MSG_Template::generate_browser_trigger($this);
810
-                break;
811
-
812
-            default:
813
-                return '';
814
-        }
815
-    }
816
-
817
-    /**
818
-     * Returns the link to the editor for the object.  Sometimes this is the same as the details.
819
-     *
820
-     * @return string
821
-     */
822
-    public function get_admin_edit_link()
823
-    {
824
-        return $this->get_admin_details_link();
825
-    }
826
-
827
-    /**
828
-     * Returns the link to a settings page for the object.
829
-     *
830
-     * @return string
831
-     */
832
-    public function get_admin_settings_link()
833
-    {
834
-        EE_Registry::instance()->load_helper('URL');
835
-        return EEH_URL::add_query_args_and_nonce(
836
-            array(
837
-                'page'   => 'espresso_messages',
838
-                'action' => 'settings',
839
-            ),
840
-            admin_url('admin.php')
841
-        );
842
-    }
843
-
844
-    /**
845
-     * Returns the link to the "overview" for the object (typically the "list table" view).
846
-     *
847
-     * @return string
848
-     */
849
-    public function get_admin_overview_link()
850
-    {
851
-        EE_Registry::instance()->load_helper('URL');
852
-        return EEH_URL::add_query_args_and_nonce(
853
-            array(
854
-                'page'   => 'espresso_messages',
855
-                'action' => 'default',
856
-            ),
857
-            admin_url('admin.php')
858
-        );
859
-    }
860
-
861
-
862
-    /**
863
-     * This sets the EEM_Message::status_messenger_executing class on the message and the appropriate error message for
864
-     * it.
865
-     * Note this also SAVES the current message object to the db because it adds an error message to accompany the
866
-     * status.
867
-     *
868
-     */
869
-    public function set_messenger_is_executing()
870
-    {
871
-        $this->set_STS_ID(EEM_Message::status_messenger_executing);
872
-        if (EEM_Message::debug()) {
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
12
+	/**
13
+	 * @deprecated 4.9.0  Added for backward compat with add-on's
14
+	 * @type null
15
+	 */
16
+	public $template_pack;
17
+
18
+	/**
19
+	 * @deprecated 4.9.0 Added for backward compat with add-on's
20
+	 * @type null
21
+	 */
22
+	public $template_variation;
23
+
24
+	/**
25
+	 * @deprecated 4.9.0 Added for backward compat with add-on's
26
+	 * @type string
27
+	 */
28
+	public $content = '';
29
+
30
+
31
+	/**
32
+	 * @type EE_messenger $_messenger
33
+	 */
34
+	protected $_messenger = null;
35
+
36
+	/**
37
+	 * @type EE_message_type $_message_type
38
+	 */
39
+	protected $_message_type = null;
40
+
41
+
42
+	/**
43
+	 * @param array  $props_n_values
44
+	 * @param string $timezone
45
+	 * @param array  $date_formats incoming date formats in an array.  First value is the date_format, second is time
46
+	 *                             format.
47
+	 * @return EE_Message
48
+	 */
49
+	public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
50
+	{
51
+		$has_object = parent::_check_for_object($props_n_values, __CLASS__);
52
+		// if object doesn't exist, let's generate a unique token on instantiation so that its available even before saving to db.
53
+		if (! $has_object) {
54
+			EE_Registry::instance()->load_helper('URL');
55
+			$props_n_values['MSG_token'] = EEH_URL::generate_unique_token();
56
+		}
57
+		return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
58
+	}
59
+
60
+
61
+	/**
62
+	 * @param array  $props_n_values
63
+	 * @param string $timezone
64
+	 * @return EE_Message
65
+	 */
66
+	public static function new_instance_from_db($props_n_values = array(), $timezone = null)
67
+	{
68
+		return new self($props_n_values, true, $timezone);
69
+	}
70
+
71
+
72
+	/**
73
+	 * Gets MSG_token
74
+	 *
75
+	 * @return int
76
+	 */
77
+	public function MSG_token()
78
+	{
79
+		return $this->get('MSG_token');
80
+	}
81
+
82
+
83
+	/**
84
+	 * Sets MSG_token
85
+	 *
86
+	 * @param int $MSG_token
87
+	 */
88
+	public function set_MSG_token($MSG_token)
89
+	{
90
+		$this->set('MSG_token', $MSG_token);
91
+	}
92
+
93
+
94
+	/**
95
+	 * Gets GRP_ID
96
+	 *
97
+	 * @return int
98
+	 */
99
+	public function GRP_ID()
100
+	{
101
+		return $this->get('GRP_ID');
102
+	}
103
+
104
+
105
+	/**
106
+	 * Sets GRP_ID
107
+	 *
108
+	 * @param int $GRP_ID
109
+	 */
110
+	public function set_GRP_ID($GRP_ID)
111
+	{
112
+		$this->set('GRP_ID', $GRP_ID);
113
+	}
114
+
115
+
116
+	/**
117
+	 * Gets TXN_ID
118
+	 *
119
+	 * @return int
120
+	 */
121
+	public function TXN_ID()
122
+	{
123
+		return $this->get('TXN_ID');
124
+	}
125
+
126
+
127
+	/**
128
+	 * Sets TXN_ID
129
+	 *
130
+	 * @param int $TXN_ID
131
+	 */
132
+	public function set_TXN_ID($TXN_ID)
133
+	{
134
+		$this->set('TXN_ID', $TXN_ID);
135
+	}
136
+
137
+
138
+	/**
139
+	 * Gets messenger
140
+	 *
141
+	 * @return string
142
+	 */
143
+	public function messenger()
144
+	{
145
+		return $this->get('MSG_messenger');
146
+	}
147
+
148
+
149
+	/**
150
+	 * Sets messenger
151
+	 *
152
+	 * @param string $messenger
153
+	 */
154
+	public function set_messenger($messenger)
155
+	{
156
+		$this->set('MSG_messenger', $messenger);
157
+	}
158
+
159
+
160
+	/**
161
+	 * Returns corresponding messenger object for the set messenger on this message
162
+	 *
163
+	 * @return EE_messenger | null
164
+	 */
165
+	public function messenger_object()
166
+	{
167
+		return $this->_messenger;
168
+	}
169
+
170
+
171
+	/**
172
+	 * Sets messenger
173
+	 *
174
+	 * @param EE_messenger $messenger
175
+	 */
176
+	public function set_messenger_object(EE_messenger $messenger)
177
+	{
178
+		$this->_messenger = $messenger;
179
+	}
180
+
181
+
182
+	/**
183
+	 * validates messenger
184
+	 *
185
+	 * @param bool $throw_exceptions
186
+	 * @return bool
187
+	 * @throws \EE_Error
188
+	 */
189
+	public function valid_messenger($throw_exceptions = false)
190
+	{
191
+		if ($this->_messenger instanceof EE_messenger) {
192
+			return true;
193
+		}
194
+		if ($throw_exceptions) {
195
+			throw new EE_Error(
196
+				sprintf(
197
+					esc_html__(
198
+						'The "%1$s" messenger set for this message is missing or invalid. Please double-check the spelling and verify that the correct files exist.',
199
+						'event_espresso'
200
+					),
201
+					$this->messenger()
202
+				)
203
+			);
204
+		}
205
+		return false;
206
+	}
207
+
208
+
209
+	/**
210
+	 * This returns the set localized label for the messenger on this message.
211
+	 * Note, if unable to retrieve the EE_messenger object then will just return the messenger slug saved
212
+	 * with this message.
213
+	 *
214
+	 * @param   bool $plural whether to return the plural label or not.
215
+	 * @return string
216
+	 */
217
+	public function messenger_label($plural = false)
218
+	{
219
+		$label_type = $plural ? 'plural' : 'singular';
220
+		$messenger = $this->messenger_object();
221
+		return $messenger instanceof EE_messenger ? $messenger->label[ $label_type ] : $this->messenger();
222
+	}
223
+
224
+
225
+	/**
226
+	 * Gets message_type
227
+	 *
228
+	 * @return string
229
+	 */
230
+	public function message_type()
231
+	{
232
+		return $this->get('MSG_message_type');
233
+	}
234
+
235
+
236
+	/**
237
+	 * Sets message_type
238
+	 *
239
+	 * @param string $message_type
240
+	 */
241
+	public function set_message_type($message_type)
242
+	{
243
+		$this->set('MSG_message_type', $message_type);
244
+	}
245
+
246
+
247
+	/**
248
+	 * Returns the message type object for the set message type on this message
249
+	 *
250
+	 * @return EE_message_type | null
251
+	 */
252
+	public function message_type_object()
253
+	{
254
+		return $this->_message_type;
255
+	}
256
+
257
+
258
+	/**
259
+	 * Sets message_type
260
+	 *
261
+	 * @param EE_message_type $message_type
262
+	 * @param bool            $set_priority   This indicates whether to set the priority to whatever the priority is on
263
+	 *                                        the message type or not.
264
+	 */
265
+	public function set_message_type_object(EE_message_type $message_type, $set_priority = false)
266
+	{
267
+		$this->_message_type = $message_type;
268
+		if ($set_priority) {
269
+			$this->set_priority($this->_message_type->get_priority());
270
+		}
271
+	}
272
+
273
+
274
+	/**
275
+	 * validates message_type
276
+	 *
277
+	 * @param bool $throw_exceptions
278
+	 * @return bool
279
+	 * @throws \EE_Error
280
+	 */
281
+	public function valid_message_type($throw_exceptions = false)
282
+	{
283
+		if ($this->_message_type instanceof EE_message_type) {
284
+			return true;
285
+		}
286
+		if ($throw_exceptions) {
287
+			throw new EE_Error(
288
+				sprintf(
289
+					esc_html__(
290
+						'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.',
291
+						'event_espresso'
292
+					),
293
+					$this->message_type()
294
+				)
295
+			);
296
+		}
297
+		return false;
298
+	}
299
+
300
+
301
+	/**
302
+	 * validates messenger and message_type (that they are valid EE_messenger and EE_message_type objects).
303
+	 *
304
+	 * @param bool $throw_exceptions
305
+	 * @return bool
306
+	 * @throws \EE_Error
307
+	 */
308
+	public function is_valid($throw_exceptions = false)
309
+	{
310
+		if ($this->valid_messenger($throw_exceptions) && $this->valid_message_type($throw_exceptions)) {
311
+			return true;
312
+		}
313
+		return false;
314
+	}
315
+
316
+
317
+	/**
318
+	 * This validates whether the internal messenger and message type objects are valid for sending.
319
+	 * Three checks are done:
320
+	 * 1. There is a valid messenger object.
321
+	 * 2. There is a valid message type object.
322
+	 * 3. The message type object is active for the messenger.
323
+	 *
324
+	 * @throws EE_Error  But only if $throw_exceptions is set to true.
325
+	 * @param bool $throw_exceptions
326
+	 * @return bool
327
+	 */
328
+	public function is_valid_for_sending_or_generation($throw_exceptions = false)
329
+	{
330
+		$valid = false;
331
+		if ($this->is_valid($throw_exceptions)) {
332
+			/** @var EE_Message_Resource_Manager $message_resource_manager */
333
+			$message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
334
+			$valid = $message_resource_manager->is_message_type_active_for_messenger(
335
+				$this->messenger(),
336
+				$this->message_type()
337
+			);
338
+			if (! $valid && $throw_exceptions) {
339
+				throw new EE_Error(
340
+					sprintf(
341
+						esc_html__(
342
+							'The %1$s message type is not a valid message type for the %2$s messenger so it will not be sent.',
343
+							'event_espresso'
344
+						),
345
+						$this->message_type(),
346
+						$this->messenger()
347
+					)
348
+				);
349
+			}
350
+		}
351
+		return $valid;
352
+	}
353
+
354
+
355
+	/**
356
+	 * This returns the set localized label for the message type on this message.
357
+	 * Note, if unable to retrieve the EE_message_type object then will just return the message type slug saved
358
+	 * with this message.
359
+	 *
360
+	 * @param   bool $plural whether to return the plural label or not.
361
+	 * @return string
362
+	 */
363
+	public function message_type_label($plural = false)
364
+	{
365
+		$label_type = $plural ? 'plural' : 'singular';
366
+		$message_type = $this->message_type_object();
367
+		return $message_type instanceof EE_message_type
368
+			? $message_type->label[ $label_type ]
369
+			: str_replace(
370
+				'_',
371
+				' ',
372
+				$this->message_type()
373
+			);
374
+	}
375
+
376
+
377
+	/**
378
+	 * Gets context
379
+	 *
380
+	 * @return string
381
+	 */
382
+	public function context()
383
+	{
384
+		return $this->get('MSG_context');
385
+	}
386
+
387
+
388
+	/**
389
+	 * This returns the corresponding localized label for the given context slug, if possible from installed message
390
+	 * types. Otherwise, this will just return the set context slug on this object.
391
+	 *
392
+	 * @return string
393
+	 */
394
+	public function context_label()
395
+	{
396
+		/** @type EE_Message_Resource_Manager $message_resource_manager */
397
+		$message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
398
+		$contexts = $message_resource_manager->get_all_contexts();
399
+		return isset($contexts[ $this->context() ]) ? $contexts[ $this->context() ] : $this->context();
400
+	}
401
+
402
+
403
+	/**
404
+	 * Sets context
405
+	 *
406
+	 * @param string $context
407
+	 */
408
+	public function set_context($context)
409
+	{
410
+		$this->set('MSG_context', $context);
411
+	}
412
+
413
+
414
+	/**
415
+	 * Gets recipient_ID
416
+	 *
417
+	 * @return int
418
+	 */
419
+	public function recipient_ID()
420
+	{
421
+		return $this->get('MSG_recipient_ID');
422
+	}
423
+
424
+
425
+	/**
426
+	 * Sets recipient_ID
427
+	 *
428
+	 * @param string $recipient_ID
429
+	 */
430
+	public function set_recipient_ID($recipient_ID)
431
+	{
432
+		$this->set('MSG_recipient_ID', $recipient_ID);
433
+	}
434
+
435
+
436
+	/**
437
+	 * Gets recipient_type
438
+	 *
439
+	 * @return string
440
+	 */
441
+	public function recipient_type()
442
+	{
443
+		return $this->get('MSG_recipient_type');
444
+	}
445
+
446
+
447
+	/**
448
+	 * Return the related object matching the recipient type and ID.
449
+	 *
450
+	 * @return EE_Base_Class | null
451
+	 */
452
+	public function recipient_object()
453
+	{
454
+		if (! $this->recipient_type() || ! $this->recipient_ID()) {
455
+			return null;
456
+		}
457
+
458
+		return $this->get_first_related($this->recipient_type());
459
+	}
460
+
461
+
462
+	/**
463
+	 * Sets recipient_type
464
+	 *
465
+	 * @param string $recipient_type
466
+	 */
467
+	public function set_recipient_type($recipient_type)
468
+	{
469
+		$this->set('MSG_recipient_type', $recipient_type);
470
+	}
471
+
472
+
473
+	/**
474
+	 * Gets content
475
+	 *
476
+	 * @return string
477
+	 */
478
+	public function content()
479
+	{
480
+		return $this->get('MSG_content');
481
+	}
482
+
483
+
484
+	/**
485
+	 * Sets content
486
+	 *
487
+	 * @param string $content
488
+	 */
489
+	public function set_content($content)
490
+	{
491
+		$this->set('MSG_content', $content);
492
+	}
493
+
494
+
495
+	/**
496
+	 * Gets subject
497
+	 *
498
+	 * @return string
499
+	 */
500
+	public function subject()
501
+	{
502
+		return $this->get('MSG_subject');
503
+	}
504
+
505
+
506
+	/**
507
+	 * Sets subject
508
+	 *
509
+	 * @param string $subject
510
+	 */
511
+	public function set_subject($subject)
512
+	{
513
+		$this->set('MSG_subject', $subject);
514
+	}
515
+
516
+
517
+	/**
518
+	 * Gets to
519
+	 *
520
+	 * @return string
521
+	 */
522
+	public function to()
523
+	{
524
+		$to = $this->get('MSG_to');
525
+		return empty($to) ? esc_html__('No recipient', 'event_espresso') : $to;
526
+	}
527
+
528
+
529
+	/**
530
+	 * Sets to
531
+	 *
532
+	 * @param string $to
533
+	 */
534
+	public function set_to($to)
535
+	{
536
+		$this->set('MSG_to', $to);
537
+	}
538
+
539
+
540
+	/**
541
+	 * Gets from
542
+	 *
543
+	 * @return string
544
+	 */
545
+	public function from()
546
+	{
547
+		return $this->get('MSG_from');
548
+	}
549
+
550
+
551
+	/**
552
+	 * Sets from
553
+	 *
554
+	 * @param string $from
555
+	 */
556
+	public function set_from($from)
557
+	{
558
+		$this->set('MSG_from', $from);
559
+	}
560
+
561
+
562
+	/**
563
+	 * Gets priority
564
+	 *
565
+	 * @return int
566
+	 */
567
+	public function priority()
568
+	{
569
+		return $this->get('MSG_priority');
570
+	}
571
+
572
+
573
+	/**
574
+	 * Sets priority
575
+	 * Note.  Send Now Messengers always override any priority that may be set on a Message.  So
576
+	 * this method calls the send_now method to verify that.
577
+	 *
578
+	 * @param int $priority
579
+	 */
580
+	public function set_priority($priority)
581
+	{
582
+		$priority = $this->send_now() ? EEM_Message::priority_high : $priority;
583
+		parent::set('MSG_priority', $priority);
584
+	}
585
+
586
+
587
+	/**
588
+	 * Overrides parent::set method so we can capture any sets for priority.
589
+	 *
590
+	 * @see parent::set() for phpdocs
591
+	 * @param string $field_name
592
+	 * @param mixed  $field_value
593
+	 * @param bool   $use_default
594
+	 * @throws EE_Error
595
+	 */
596
+	public function set($field_name, $field_value, $use_default = false)
597
+	{
598
+		if ($field_name === 'MSG_priority') {
599
+			$this->set_priority($field_value);
600
+		}
601
+		parent::set($field_name, $field_value, $use_default);
602
+	}
603
+
604
+
605
+	/**
606
+	 * @return bool
607
+	 * @throws \EE_Error
608
+	 */
609
+	public function send_now()
610
+	{
611
+		$send_now = $this->valid_messenger() && $this->messenger_object()->send_now() ? EEM_Message::priority_high
612
+			: $this->priority();
613
+		return $send_now === EEM_Message::priority_high ? true : false;
614
+	}
615
+
616
+
617
+	/**
618
+	 * Gets STS_ID
619
+	 *
620
+	 * @return string
621
+	 */
622
+	public function STS_ID()
623
+	{
624
+		return $this->get('STS_ID');
625
+	}
626
+
627
+
628
+	/**
629
+	 * Sets STS_ID
630
+	 *
631
+	 * @param string $STS_ID
632
+	 */
633
+	public function set_STS_ID($STS_ID)
634
+	{
635
+		$this->set('STS_ID', $STS_ID);
636
+	}
637
+
638
+
639
+	/**
640
+	 * Gets created
641
+	 *
642
+	 * @return string
643
+	 */
644
+	public function created()
645
+	{
646
+		return $this->get('MSG_created');
647
+	}
648
+
649
+
650
+	/**
651
+	 * Sets created
652
+	 *
653
+	 * @param string $created
654
+	 */
655
+	public function set_created($created)
656
+	{
657
+		$this->set('MSG_created', $created);
658
+	}
659
+
660
+
661
+	/**
662
+	 * Gets modified
663
+	 *
664
+	 * @return string
665
+	 */
666
+	public function modified()
667
+	{
668
+		return $this->get('MSG_modified');
669
+	}
670
+
671
+
672
+	/**
673
+	 * Sets modified
674
+	 *
675
+	 * @param string $modified
676
+	 */
677
+	public function set_modified($modified)
678
+	{
679
+		$this->set('MSG_modified', $modified);
680
+	}
681
+
682
+
683
+	/**
684
+	 * Sets generation data for this message.
685
+	 *
686
+	 * @param mixed $data
687
+	 */
688
+	public function set_generation_data($data)
689
+	{
690
+		$this->set_field_or_extra_meta('MSG_generation_data', $data);
691
+	}
692
+
693
+
694
+	/**
695
+	 * Returns any set generation data for this message.
696
+	 *
697
+	 * @return mixed|null
698
+	 */
699
+	public function get_generation_data()
700
+	{
701
+		return $this->get_field_or_extra_meta('MSG_generation_data');
702
+	}
703
+
704
+
705
+	/**
706
+	 * Gets any error message.
707
+	 *
708
+	 * @return mixed|null
709
+	 */
710
+	public function error_message()
711
+	{
712
+		return $this->get_field_or_extra_meta('MSG_error');
713
+	}
714
+
715
+
716
+	/**
717
+	 * Sets an error message.
718
+	 *
719
+	 * @param $message
720
+	 * @return bool|int
721
+	 */
722
+	public function set_error_message($message)
723
+	{
724
+		return $this->set_field_or_extra_meta('MSG_error', $message);
725
+	}
726
+
727
+
728
+	/**
729
+	 * This retrieves the associated template pack with this message.
730
+	 *
731
+	 * @return EE_Messages_Template_Pack | null
732
+	 */
733
+	public function get_template_pack()
734
+	{
735
+		/**
736
+		 * This is deprecated functionality that will be removed eventually but included here now for backward compat.
737
+		 */
738
+		if (! empty($this->template_pack)) {
739
+			return $this->template_pack;
740
+		}
741
+		/** @type EE_Message_Template_Group $grp */
742
+		$grp = $this->get_first_related('Message_Template_Group');
743
+		// if no group then let's try to get the first related group by internal messenger and message type (will use global grp).
744
+		if (! $grp instanceof EE_Message_Template_Group) {
745
+			$grp = EEM_Message_Template_Group::instance()->get_one(
746
+				array(
747
+					array(
748
+						'MTP_messenger'    => $this->messenger(),
749
+						'MTP_message_type' => $this->message_type(),
750
+						'MTP_is_global'    => true,
751
+					),
752
+				)
753
+			);
754
+		}
755
+
756
+		return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack() : null;
757
+	}
758
+
759
+
760
+	/**
761
+	 * Retrieves the variation used for generating this message.
762
+	 *
763
+	 * @return string
764
+	 */
765
+	public function get_template_pack_variation()
766
+	{
767
+		/**
768
+		 * This is deprecated functionality that will be removed eventually but included here now for backward compat.
769
+		 */
770
+		if (! empty($this->template_variation)) {
771
+			return $this->template_variation;
772
+		}
773
+
774
+		/** @type EE_Message_Template_Group $grp */
775
+		$grp = $this->get_first_related('Message_Template_Group');
776
+
777
+		// if no group then let's try to get the first related group by internal messenger and message type (will use global grp).
778
+		if (! $grp instanceof EE_Message_Template_Group) {
779
+			$grp = EEM_Message_Template_Group::instance()->get_one(
780
+				array(
781
+					array(
782
+						'MTP_messenger'    => $this->messenger(),
783
+						'MTP_message_type' => $this->message_type(),
784
+						'MTP_is_global'    => true,
785
+					),
786
+				)
787
+			);
788
+		}
789
+
790
+		return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack_variation() : '';
791
+	}
792
+
793
+	/**
794
+	 * Return the link to the admin details for the object.
795
+	 *
796
+	 * @return string
797
+	 */
798
+	public function get_admin_details_link()
799
+	{
800
+		EE_Registry::instance()->load_helper('URL');
801
+		EE_Registry::instance()->load_helper('MSG_Template');
802
+		switch ($this->STS_ID()) {
803
+			case EEM_Message::status_failed:
804
+			case EEM_Message::status_debug_only:
805
+				return EEH_MSG_Template::generate_error_display_trigger($this);
806
+				break;
807
+
808
+			case EEM_Message::status_sent:
809
+				return EEH_MSG_Template::generate_browser_trigger($this);
810
+				break;
811
+
812
+			default:
813
+				return '';
814
+		}
815
+	}
816
+
817
+	/**
818
+	 * Returns the link to the editor for the object.  Sometimes this is the same as the details.
819
+	 *
820
+	 * @return string
821
+	 */
822
+	public function get_admin_edit_link()
823
+	{
824
+		return $this->get_admin_details_link();
825
+	}
826
+
827
+	/**
828
+	 * Returns the link to a settings page for the object.
829
+	 *
830
+	 * @return string
831
+	 */
832
+	public function get_admin_settings_link()
833
+	{
834
+		EE_Registry::instance()->load_helper('URL');
835
+		return EEH_URL::add_query_args_and_nonce(
836
+			array(
837
+				'page'   => 'espresso_messages',
838
+				'action' => 'settings',
839
+			),
840
+			admin_url('admin.php')
841
+		);
842
+	}
843
+
844
+	/**
845
+	 * Returns the link to the "overview" for the object (typically the "list table" view).
846
+	 *
847
+	 * @return string
848
+	 */
849
+	public function get_admin_overview_link()
850
+	{
851
+		EE_Registry::instance()->load_helper('URL');
852
+		return EEH_URL::add_query_args_and_nonce(
853
+			array(
854
+				'page'   => 'espresso_messages',
855
+				'action' => 'default',
856
+			),
857
+			admin_url('admin.php')
858
+		);
859
+	}
860
+
861
+
862
+	/**
863
+	 * This sets the EEM_Message::status_messenger_executing class on the message and the appropriate error message for
864
+	 * it.
865
+	 * Note this also SAVES the current message object to the db because it adds an error message to accompany the
866
+	 * status.
867
+	 *
868
+	 */
869
+	public function set_messenger_is_executing()
870
+	{
871
+		$this->set_STS_ID(EEM_Message::status_messenger_executing);
872
+		if (EEM_Message::debug()) {
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
-        }
884
-    }
880
+					'event_espresso'
881
+				)
882
+			);
883
+		}
884
+	}
885 885
 }
Please login to merge, or discard this patch.