Completed
Branch FET-9046-messages-queue (bdca24)
by
unknown
547:25 queued 529:16
created

EE_Message::is_valid()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
1
<?php if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
2
	exit( 'No direct script access allowed' );
3
}
4
/**
5
 * EE_Message class
6
 *
7
 * @package 	Event Espresso
8
 * @subpackage 	db classes
9
 * @author 		Darren Ethier
10
 */
11
class EE_Message extends EE_Base_Class implements EEI_Admin_Links {
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
	/**
45
	 *
46
	 * @param array  $props_n_values
47
	 * @param string $timezone
48
	 * @param array $date_formats incoming date formats in an array.  First value is the date_format, second is time format.
49
	 * @return EE_Message
50
	 */
51
	public static function new_instance( $props_n_values = array(), $timezone = null, $date_formats = array() ) {
52
		$has_object = parent::_check_for_object( $props_n_values, __CLASS__ );
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (_check_for_object() instead of new_instance()). Are you sure this is correct? If so, you might want to change this to $this->_check_for_object().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
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
	/**
64
	 *
65
	 * @param array  $props_n_values
66
	 * @param string $timezone
67
	 * @return EE_Message
68
	 */
69
	public static function new_instance_from_db( $props_n_values = array(), $timezone = null ) {
70
		return new self( $props_n_values, true, $timezone );
71
	}
72
73
74
75
	/**
76
	 * Gets MSG_token
77
	 *
78
	 * @return int
79
	 */
80
	public function MSG_token() {
81
		return $this->get( 'MSG_token' );
82
	}
83
84
85
86
	/**
87
	 * Sets MSG_token
88
	 *
89
	 * @param int $MSG_token
90
	 */
91
	public function set_MSG_token( $MSG_token) {
92
		$this->set( 'MSG_token', $MSG_token );
93
	}
94
95
96
97
98
	/**
99
	 * Gets GRP_ID
100
	 *
101
	 * @return int
102
	 */
103
	public function GRP_ID() {
104
		return $this->get( 'GRP_ID' );
105
	}
106
107
108
109
	/**
110
	 * Sets GRP_ID
111
	 *
112
	 * @param int $GRP_ID
113
	 */
114
	public function set_GRP_ID( $GRP_ID ) {
115
		$this->set( 'GRP_ID', $GRP_ID );
116
	}
117
118
119
120
121
	/**
122
	 * Gets TXN_ID
123
	 *
124
	 * @return int
125
	 */
126
	public function TXN_ID() {
127
		return $this->get( 'TXN_ID' );
128
	}
129
130
131
132
	/**
133
	 * Sets TXN_ID
134
	 *
135
	 * @param int $TXN_ID
136
	 */
137
	public function set_TXN_ID( $TXN_ID) {
138
		$this->set( 'TXN_ID', $TXN_ID );
139
	}
140
141
142
143
144
	/**
145
	 * Gets messenger
146
	 *
147
	 * @return string
148
	 */
149
	public function messenger() {
150
		return $this->get( 'MSG_messenger' );
151
	}
152
153
154
155
	/**
156
	 * Sets messenger
157
	 *
158
	 * @param string $messenger
159
	 */
160
	public function set_messenger( $messenger ) {
161
		$this->set( 'MSG_messenger', $messenger );
162
	}
163
164
165
166
	/**
167
	 * Returns corresponding messenger object for the set messenger on this message
168
	 *
169
	 * @return EE_Messenger | null
170
	 */
171
	public function messenger_object() {
172
		return $this->_messenger;
173
	}
174
175
176
177
	/**
178
	 * Sets messenger
179
	 *
180
	 * @param EE_Messenger $messenger
181
	 */
182
	public function set_messenger_object( EE_Messenger $messenger ) {
183
		$this->_messenger = $messenger;
184
	}
185
186
187
188
	/**
189
	 * validates messenger
190
	 *
191
	 * @param bool $throw_exceptions
192
	 * @return bool
193
	 * @throws \EE_Error
194
	 */
195
	public function valid_messenger( $throw_exceptions = false ) {
196
		if ( $this->_messenger instanceof EE_Messenger ) {
197
			return true;
198
		}
199
		if ( $throw_exceptions ) {
200
			throw new EE_Error(
201
				sprintf(
202
					__(
203
						'The "%1$s" messenger set for this message is missing or invalid. Please double-check the spelling and verify that the correct files exist.',
204
						'event_espresso'
205
					),
206
					$this->messenger()
207
				)
208
			);
209
		}
210
		return false;
211
	}
212
213
214
215
	/**
216
	 * This returns the set localized label for the messenger on this message.
217
	 * Note, if unable to retrieve the EE_Messenger object then will just return the messenger slug saved
218
	 * with this message.
219
	 *
220
	 * @param   bool    $plural whether to return the plural label or not.
221
	 * @return string
222
	 */
223
	public function messenger_label( $plural = false ) {
224
		$label_type = $plural ? 'plural' : 'singular';
225
		$messenger = $this->messenger_object();
226
		return $messenger instanceof EE_Messenger ? $messenger->label[ $label_type ] : $this->messenger();
227
	}
228
229
230
231
	/**
232
	 * Gets message_type
233
	 *
234
	 * @return string
235
	 */
236
	public function message_type() {
237
		return $this->get( 'MSG_message_type' );
238
	}
239
240
241
242
	/**
243
	 * Sets message_type
244
	 *
245
	 * @param string $message_type
246
	 */
247
	public function set_message_type( $message_type ) {
248
		$this->set( 'MSG_message_type', $message_type );
249
	}
250
251
252
253
	/**
254
	 * Returns the message type object for the set message type on this message
255
	 *
256
	 * @return EE_message_type | null
257
	 */
258
	public function message_type_object() {
259
		return $this->_message_type;
260
	}
261
262
263
264
	/**
265
	 * Sets message_type
266
	 *
267
	 * @param EE_Message_Type $message_type
268
	 */
269
	public function set_message_type_object( EE_Message_Type $message_type ) {
270
		$this->_message_type = $message_type;
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
		if ( $this->_message_type instanceof EE_Message_Type ) {
284
			return true;
285
		}
286
		if ( $throw_exceptions ) {
287
			throw new EE_Error(
288
				sprintf(
289
					__(
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
	/**
303
	 * validates messenger and message_type
304
	 *
305
	 * @param bool $throw_exceptions
306
	 * @return bool
307
	 * @throws \EE_Error
308
	 */
309
	public function is_valid( $throw_exceptions = false ) {
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
	/**
319
	 * This returns the set localized label for the message type on this message.
320
	 * Note, if unable to retrieve the EE_message_type object then will just return the message type slug saved
321
	 * with this message.
322
	 *
323
	 * @param   bool    $plural whether to return the plural label or not.
324
	 * @return string
325
	 */
326
	public function message_type_label( $plural = false ) {
327
		$label_type = $plural ? 'plural' : 'singular';
328
		$message_type = $this->message_type_object();
329
		return $message_type instanceof EE_message_type ? $message_type->label[ $label_type ] : $this->message_type();
330
	}
331
332
333
334
	/**
335
	 * Gets context
336
	 *
337
	 * @return string
338
	 */
339
	public function context() {
340
		return $this->get( 'MSG_context' );
341
	}
342
343
344
345
346
	/**
347
	 * This returns the corresponding localized label for the given context slug, if possible from installed message types.
348
	 * Otherwise, this will just return the set context slug on this object.
349
	 *
350
	 * @return string
351
	 */
352
	public function context_label() {
353
		/** @type EE_Messages $messages_controller */
354
		$messages_controller = EE_Registry::instance()->load_lib( 'messages' );
355
		$contexts = $messages_controller->get_all_contexts();
356
		return isset( $contexts[ $this->context() ] ) ? $contexts[ $this->context() ] : $this->context();
357
	}
358
359
360
361
	/**
362
	 * Sets context
363
	 *
364
	 * @param string $context
365
	 */
366
	public function set_context( $context ) {
367
		$this->set( 'MSG_context', $context );
368
	}
369
370
371
372
	/**
373
	 * Gets recipient_ID
374
	 *
375
	 * @return int
376
	 */
377
	public function recipient_ID() {
378
		return $this->get( 'MSG_recipient_ID' );
379
	}
380
381
382
383
	/**
384
	 * Sets recipient_ID
385
	 *
386
	 * @param string $recipient_ID
387
	 */
388
	public function set_recipient_ID( $recipient_ID ) {
389
		$this->set( 'MSG_recipient_ID', $recipient_ID );
390
	}
391
392
393
394
	/**
395
	 * Gets recipient_type
396
	 *
397
	 * @return string
398
	 */
399
	public function recipient_type() {
400
		return $this->get( 'MSG_recipient_type' );
401
	}
402
403
404
405
406
	/**
407
	 * Return the related object matching the recipient type and ID.
408
	 *
409
	 * @return EE_Base_Class | null
410
	 */
411
	public function recipient_object() {
412
		if ( ! $this->recipient_type() || ! $this->recipient_ID() ) {
413
			return null;
414
		}
415
416
		return $this->get_first_related( $this->recipient_type() );
0 ignored issues
show
Documentation introduced by
$this->recipient_type() is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
417
	}
418
419
420
421
	/**
422
	 * Sets recipient_type
423
	 *
424
	 * @param string $recipient_type
425
	 */
426
	public function set_recipient_type( $recipient_type ) {
427
		$this->set( 'MSG_recipient_type', $recipient_type );
428
	}
429
430
431
432
	/**
433
	 * Gets content
434
	 *
435
	 * @return string
436
	 */
437
	public function content() {
438
		return $this->get( 'MSG_content' );
439
	}
440
441
442
443
	/**
444
	 * Sets content
445
	 *
446
	 * @param string $content
447
	 */
448
	public function set_content( $content ) {
449
		$this->set( 'MSG_content', $content );
450
	}
451
452
453
454
	/**
455
	 * Gets subject
456
	 *
457
	 * @return string
458
	 */
459
	public function subject() {
460
		return $this->get( 'MSG_subject' );
461
	}
462
463
464
465
	/**
466
	 * Sets subject
467
	 *
468
	 * @param string $subject
469
	 */
470
	public function set_subject( $subject ) {
471
		$this->set( 'MSG_subject', $subject );
472
	}
473
474
475
476
	/**
477
	 * Gets to
478
	 *
479
	 * @return string
480
	 */
481
	public function to() {
482
		$to = $this->get( 'MSG_to' );
483
		return empty( $to ) ? __( 'No recipient', 'event_espresso' ) : $to;
484
	}
485
486
487
488
	/**
489
	 * Sets to
490
	 *
491
	 * @param string $to
492
	 */
493
	public function set_to( $to ) {
494
		$this->set( 'MSG_to', $to );
495
	}
496
497
498
499
	/**
500
	 * Gets from
501
	 *
502
	 * @return string
503
	 */
504
	public function from() {
505
		return $this->get( 'MSG_from' );
506
	}
507
508
509
510
	/**
511
	 * Sets from
512
	 *
513
	 * @param string $from
514
	 */
515
	public function set_from( $from ) {
516
		$this->set( 'MSG_from', $from );
517
	}
518
519
520
521
522
523
	/**
524
	 * Gets priority
525
	 *
526
	 * @return int
527
	 */
528
	public function priority() {
529
		return $this->get( 'MSG_priority' );
530
	}
531
532
533
534
	/**
535
	 * Sets priority
536
	 *
537
	 * @param int $priority
538
	 */
539
	public function set_priority( $priority ) {
540
		$this->set( 'MSG_priority', $priority );
541
	}
542
543
544
545
	/**
546
	 * Gets STS_ID
547
	 *
548
	 * @return string
549
	 */
550
	public function STS_ID() {
551
		return $this->get( 'STS_ID' );
552
	}
553
554
555
556
	/**
557
	 * Sets STS_ID
558
	 *
559
	 * @param string $STS_ID
560
	 */
561
	public function set_STS_ID( $STS_ID ) {
562
		$this->set( 'STS_ID', $STS_ID );
563
	}
564
565
566
567
	/**
568
	 * Gets created
569
	 *
570
	 * @return string
571
	 */
572
	public function created() {
573
		return $this->get( 'MSG_created' );
574
	}
575
576
577
578
	/**
579
	 * Sets created
580
	 *
581
	 * @param string $created
582
	 */
583
	public function set_created( $created ) {
584
		$this->set( 'MSG_created', $created );
585
	}
586
587
588
589
	/**
590
	 * Gets modified
591
	 *
592
	 * @return string
593
	 */
594
	public function modified() {
595
		return $this->get( 'MSG_modified' );
596
	}
597
598
599
600
	/**
601
	 * Sets modified
602
	 *
603
	 * @param string $modified
604
	 */
605
	public function set_modified( $modified ) {
606
		$this->set( 'MSG_modified', $modified );
607
	}
608
609
610
611
612
	/**
613
	 * Sets generation data for this message.
614
	 * @param mixed $data
615
	 */
616
	public function set_generation_data( $data ) {
617
		$this->set_field_or_extra_meta( 'MSG_generation_data', $data );
618
	}
619
620
621
622
623
624
	/**
625
	 * Returns any set generation data for this message.
626
	 * @return mixed|null
627
	 */
628
	public function get_generation_data() {
629
		return $this->get_field_or_extra_meta( 'MSG_generation_data' );
630
	}
631
632
633
634
635
	/**
636
	 * Gets any error message.
637
	 * @return mixed|null
638
	 */
639
	public function error_message() {
640
		return $this->get_field_or_extra_meta( 'MSG_error' );
641
	}
642
643
644
	/**
645
	 * Sets an error message.
646
	 * @param $message
647
	 * @return bool|int
648
	 */
649
	public function set_error_message( $message ) {
650
		return $this->set_field_or_extra_meta( 'MSG_error', $message );
651
	}
652
653
654
655
656
	/**
657
	 * This retrieves the associated template pack with this message.
658
	 * @return EE_Messages_Template_Pack | null
659
	 */
660 View Code Duplication
	public function get_template_pack() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
661
		/**
662
		 * This is deprecated functionality that will be removed eventually but included here now for backward compat.
663
		 */
664
		if ( ! empty( $this->template_pack ) ) {
0 ignored issues
show
Deprecated Code introduced by
The property EE_Message::$template_pack has been deprecated with message: 4.9.0 Added for backward compat with add-on's

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
665
			return $this->template_pack;
0 ignored issues
show
Deprecated Code introduced by
The property EE_Message::$template_pack has been deprecated with message: 4.9.0 Added for backward compat with add-on's

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
666
		}
667
		/** @type EE_Message_Template_Group $grp */
668
		$grp = $this->get_first_related( 'Message_Template_Group' );
669
		//if no group then let's try to get the first related group by internal messenger and message type (will use global grp).
670
		if ( ! $grp instanceof EE_Message_Template_Group ) {
671
			$grp = EEM_Message_Template_Group::instance()->get_one( array( array(
672
				'MTP_messenger' => $this->messenger(),
673
				'MTP_message_type' => $this->message_type(),
674
				'MTP_is_global' => true
675
			) ) );
676
		}
677
678
		return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack() : null;
679
	}
680
681
682
683
684
	/**
685
	 * Retrieves the variation used for generating this message.
686
	 * @return string
687
	 */
688 View Code Duplication
	public function get_template_pack_variation() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
689
		/**
690
		 * This is deprecated functionality that will be removed eventually but included here now for backward compat.
691
		 */
692
		if ( ! empty( $this->template_variation ) ) {
0 ignored issues
show
Deprecated Code introduced by
The property EE_Message::$template_variation has been deprecated with message: 4.9.0 Added for backward compat with add-on's

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
693
			return $this->template_variation;
0 ignored issues
show
Deprecated Code introduced by
The property EE_Message::$template_variation has been deprecated with message: 4.9.0 Added for backward compat with add-on's

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
694
		}
695
696
		/** @type EE_Message_Template_Group $grp */
697
		$grp = $this->get_first_related( 'Message_Template_Group' );
698
699
		//if no group then let's try to get the first related group by internal messenger and message type (will use global grp).
700
		if ( ! $grp instanceof EE_Message_Template_Group ) {
701
			$grp = EEM_Message_Template_Group::instance()->get_one( array( array(
702
				'MTP_messenger' => $this->messenger(),
703
				'MTP_message_type' => $this->message_type(),
704
				'MTP_is_global' => true
705
			) ) );
706
		}
707
708
		return $grp instanceof EE_Message_Template_Group ? $grp->get_template_pack_variation() : '';
709
	}
710
711
	/**
712
	 * Return the link to the admin details for the object.
713
	 * @return string
714
	 */
715
	public function get_admin_details_link() {
716
		EE_Registry::instance()->load_helper( 'URL' );
717
		EE_Registry::instance()->load_helper( 'MSG_Template' );
718
		switch ( $this->STS_ID() ) {
719
			case EEM_Message::status_failed :
720
				return EEH_MSG_Template::generate_error_display_trigger( $this );
721
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
722
723
			case EEM_Message::status_sent :
724
				return EEH_MSG_Template::generate_browser_trigger( $this );
725
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
726
727
			default :
728
				return '';
729
		}
730
	}
731
732
	/**
733
	 * Returns the link to the editor for the object.  Sometimes this is the same as the details.
734
	 * @return string
735
	 */
736
	public function get_admin_edit_link() {
737
		return $this->get_admin_details_link();
738
	}
739
740
	/**
741
	 * Returns the link to a settings page for the object.
742
	 * @return string
743
	 */
744 View Code Duplication
	public function get_admin_settings_link() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
745
		EE_Registry::instance()->load_helper( 'URL' );
746
		return EEH_URL::add_query_args_and_nonce(
747
			array(
748
				'page' => 'espresso_messages',
749
				'action' => 'settings',
750
			),
751
			admin_url( 'admin.php' )
752
		);
753
	}
754
755
	/**
756
	 * Returns the link to the "overview" for the object (typically the "list table" view).
757
	 * @return string
758
	 */
759 View Code Duplication
	public function get_admin_overview_link() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
760
		EE_Registry::instance()->load_helper( 'URL' );
761
		return EEH_URL::add_query_args_and_nonce(
762
			array(
763
				'page' => 'espresso_messages',
764
				'action' => 'default',
765
			),
766
			admin_url( 'admin.php' )
767
		);
768
	}
769
770
771
}
772
/* End of file EE_Message.class.php */
773
/* Location: /core/db_classes/EE_Message.class.php */