Completed
Branch BUG-9623-config-log (c144cd)
by
unknown
18:38
created

EE_Message_Resource_Manager   D

Complexity

Total Complexity 119

Size/Duplication

Total Lines 1018
Duplicated Lines 2.36 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 24
loc 1018
rs 4.4378
c 0
b 0
f 0
wmc 119
lcom 1
cbo 7

44 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A _initialize_collections() 0 10 2
A messenger_collection() 0 4 1
A active_messengers() 0 4 1
A get_messenger() 0 3 1
A get_active_messenger() 0 4 2
A installed_messengers() 0 11 3
A valid_messenger() 12 12 2
A message_type_collection() 0 4 1
A active_message_types() 0 4 1
A get_message_type() 0 3 1
A get_active_message_type_for_messenger() 0 5 2
A is_message_type_active_for_messenger() 0 4 1
A is_messenger_active() 0 4 1
A get_message_type_settings_for_messenger() 0 9 3
A messenger_has_active_message_types() 0 6 2
A get_active_message_types_for_messenger() 0 13 4
B list_of_active_message_types() 0 15 5
A get_active_message_type_objects() 0 11 3
A installed_message_types() 0 10 3
A valid_message_type() 12 12 2
A valid_message_type_for_messenger() 0 17 2
A get_active_messengers_option() 0 6 2
A update_active_messengers_option() 0 6 2
A get_has_activated_messengers_option() 0 6 2
A update_has_activated_messengers_option() 0 6 2
A reset_active_messengers_and_message_types() 0 3 1
A _set_active_messengers_and_message_types() 0 9 1
A ensure_messenger_is_active() 0 16 3
B ensure_messengers_are_active() 0 24 6
B ensure_message_type_is_active() 0 23 4
A ensure_message_types_are_active() 0 12 3
B activate_messenger() 0 29 4
C _activate_message_types() 0 32 7
B add_settings_for_message_type() 0 19 5
A _set_messenger_has_activated_message_type() 0 10 3
B add_settings_for_messenger() 0 19 6
A deactivate_messenger() 0 9 2
A deactivate_message_type() 0 12 3
A deactivate_message_type_for_messenger() 0 7 2
A is_generating_messenger_and_active() 0 12 4
B get_all_contexts() 0 20 8
A validate_active_message_types_are_installed() 0 13 3
A has_message_type_been_activated_for_messenger() 0 5 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like EE_Message_Resource_Manager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EE_Message_Resource_Manager, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
4
	exit( 'No direct script access allowed' );
5
}
6
7
8
9
/**
10
 * Class EE_Message_Resource_Manager
11
 *
12
 * Description
13
 *
14
 * @package       Event Espresso
15
 * @subpackage    core
16
 * @author        Brent Christensen
17
 * @since         $VID:$
18
 *
19
 */
20
class EE_Message_Resource_Manager {
21
22
	/**
23
	 * @type boolean $_initialized
24
	 */
25
	protected $_initialized = false;
26
27
	/**
28
	 * @type EE_Messenger_Collection $_messenger_collection_loader
29
	 */
30
	protected $_messenger_collection_loader;
31
32
	/**
33
	 * @type EE_Message_Type_Collection $_message_type_collection_loader
34
	 */
35
	protected $_message_type_collection_loader;
36
37
	/**
38
	 * @type EEM_Message_Template_Group $_message_template_group_model
39
	 */
40
	protected $_message_template_group_model;
41
42
	/**
43
	 * @type EE_messenger[]
44
	 */
45
	protected $_installed_messengers = array();
46
47
	/**
48
	 * @type EE_message_type[]
49
	 */
50
	protected $_installed_message_types = array();
51
52
	/**
53
	 * Array of active messengers.
54
	 * Format is this:
55
	 * array(
56
	 *      'messenger_name' => EE_messenger
57
	 * )
58
	 *
59
	 * @type EE_messenger[]
60
	 */
61
	protected $_active_messengers = array();
62
63
	/**
64
	 * Formatted array of active message types grouped per messenger.
65
	 * Format is this:
66
	 * array(
67
	 *      'messenger_name' => array(
68
	 *          'settings' => array(
69
	 *              '{messenger_name}-message_types' => array(
70
	 *                  'message_type_name' => array() //variable array of settings corresponding to message type.
71
	 *              )
72
	 *          )
73
	 *      )
74
	 * )
75
	 *
76
	 * @type array
77
	 */
78
	protected $_active_message_types = array();
79
80
81
	/**
82
	 * This holds the array of messengers and their corresponding message types that have
83
	 * been activated on a site at some point.  This is an important record that helps the messages system
84
	 * not accidentally reactivate something that was intentionally deactivated by a user.
85
	 * @type array
86
	 */
87
	protected $_has_activated_messengers_and_message_types = array();
88
89
	/**
90
	 * An array of unique message type contexts across all active message types.
91
	 *
92
	 * The array will be indexed by either 'slugs' or 'all'.
93
	 * The slugs index contains an array indexed by unique context slugs with the latest label representation for that slug.
94
	 * array(
95
	 *      'context_slug' => 'localized label for context obtained from latest message type in the loop'.
96
	 * );
97
	 *
98
	 * The all index returns an array in this format:
99
	 * array(
100
	 *      'message_type_name' => array(
101
	 *          'context_slug' => array(
102
	 *              'label' => 'localized label for context',
103
	 *              'description' => 'localized description for context'
104
	 *          )
105
	 *      )
106
	 * );
107
	 *
108
	 * @type array
109
	 */
110
	protected $_contexts = array();
111
112
113
114
	/**
115
	 * EE_Message_Resource_Manager constructor.
116
	 *
117
	 * @param \EE_Messenger_Collection_Loader    $Messenger_Collection_Loader
118
	 * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader
119
	 * @param \EEM_Message_Template_Group        $Message_Template_Group_Model
120
	 */
121
	function __construct(
122
		EE_Messenger_Collection_Loader $Messenger_Collection_Loader,
123
		EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader,
124
		EEM_Message_Template_Group $Message_Template_Group_Model
125
	) {
126
		$this->_messenger_collection_loader = $Messenger_Collection_Loader;
0 ignored issues
show
Documentation Bug introduced by
It seems like $Messenger_Collection_Loader of type object<EE_Messenger_Collection_Loader> is incompatible with the declared type object<EE_Messenger_Collection> of property $_messenger_collection_loader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
127
		$this->_message_type_collection_loader = $Message_Type_Collection_Loader;
0 ignored issues
show
Documentation Bug introduced by
It seems like $Message_Type_Collection_Loader of type object<EE_Message_Type_Collection_Loader> is incompatible with the declared type object<EE_Message_Type_Collection> of property $_message_type_collection_loader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
128
		$this->_message_template_group_model = $Message_Template_Group_Model;
129
	}
130
131
132
133
	/**
134
	 * @return EE_Messenger_Collection
135
	 */
136
	protected function _initialize_collections() {
137
		if ( $this->_initialized ) {
138
			return;
139
		}
140
		$this->_initialized = true;
141
		$this->_messenger_collection_loader->load_messengers_from_folder();
0 ignored issues
show
Bug introduced by
The method load_messengers_from_folder() does not seem to exist on object<EE_Messenger_Collection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
		$this->_message_type_collection_loader->load_message_types_from_folder();
0 ignored issues
show
Bug introduced by
The method load_message_types_from_folder() does not seem to exist on object<EE_Message_Type_Collection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
		$this->get_has_activated_messengers_option( true );
144
		$this->_set_active_messengers_and_message_types();
145
	}
146
147
148
149
	/**
150
	 * @return EE_Messenger_Collection
151
	 */
152
	public function messenger_collection() {
153
		$this->_initialize_collections();
154
		return $this->_messenger_collection_loader->messenger_collection();
0 ignored issues
show
Bug introduced by
The method messenger_collection() does not seem to exist on object<EE_Messenger_Collection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
	}
156
157
158
159
	/**
160
	 * @return EE_messenger[]
161
	 */
162
	public function active_messengers() {
163
		$this->_initialize_collections();
164
		return $this->_active_messengers;
165
	}
166
167
168
169
	/**
170
	 * @param string $messenger_name
171
	 * @return \EE_messenger
172
	 */
173
	public function get_messenger( $messenger_name ) {
174
		return $this->messenger_collection()->get_by_info( $messenger_name );
175
	}
176
177
178
179
	/**
180
	 * This returns the corresponding EE_messenger object for the given string if it is active.
181
	 *
182
	 * @param string $messenger
183
	 * @return EE_messenger | null
184
	 */
185
	public function get_active_messenger( $messenger ) {
186
		$this->_initialize_collections();
187
		return ! empty( $this->_active_messengers[ $messenger ] ) ? $this->_active_messengers[ $messenger ] : null;
188
	}
189
190
191
192
	/**
193
	 * @return \EE_messenger[]
194
	 */
195
	public function installed_messengers() {
196
		if ( empty( $this->_installed_messengers ) ) {
197
			$this->_installed_messengers = array();
198
			$this->messenger_collection()->rewind();
199
			while ( $this->messenger_collection()->valid() ) {
200
				$this->_installed_messengers[ $this->messenger_collection()->current()->name ] = $this->messenger_collection()->current();
201
				$this->messenger_collection()->next();
202
			}
203
		}
204
		return $this->_installed_messengers;
205
	}
206
207
208
209
	/**
210
	 * @param string $messenger_name
211
	 * @return \EE_messenger
212
	 * @throws \EE_Error
213
	 */
214 View Code Duplication
	public function valid_messenger( $messenger_name ) {
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...
215
		$messenger = $this->get_messenger( $messenger_name );
216
		if ( $messenger instanceof EE_messenger ) {
217
			return $messenger;
218
		}
219
		throw new EE_Error(
220
			sprintf(
221
				__( 'The "%1$s" messenger is either invalid or not installed', 'event_espresso' ),
222
				$messenger_name
223
			)
224
		);
225
	}
226
227
228
229
	/**
230
	 * @return EE_Message_Type_Collection
231
	 */
232
	public function message_type_collection() {
233
		$this->_initialize_collections();
234
		return $this->_message_type_collection_loader->message_type_collection();
0 ignored issues
show
Bug introduced by
The method message_type_collection() does not seem to exist on object<EE_Message_Type_Collection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
235
	}
236
237
238
239
	/**
240
	 * @return array
241
	 */
242
	public function active_message_types() {
243
		$this->_initialize_collections();
244
		return $this->_active_message_types;
245
	}
246
247
248
249
	/**
250
	 * @param string $message_type_name
251
	 * @return \EE_message_type
252
	 */
253
	public function get_message_type( $message_type_name ) {
254
		return $this->message_type_collection()->get_by_info( $message_type_name );
255
	}
256
257
258
259
	/**
260
	 * This returns the EE_message_type from the active message types array ( if present );
261
	 *
262
	 * @param string $messenger_name
263
	 * @param string $message_type_name
264
	 * @return \EE_message_type|null
265
	 */
266
	public function get_active_message_type_for_messenger( $messenger_name, $message_type_name ) {
267
		return $this->is_message_type_active_for_messenger( $messenger_name, $message_type_name )
268
			? $this->get_message_type( $message_type_name )
269
			: null;
270
	}
271
272
273
274
	/**
275
	 * Returns whether the given message type is active for the given messenger.
276
	 *
277
	 * @param string $messenger_name
278
	 * @param string $message_type_name
279
	 *
280
	 * @return bool
281
	 */
282
	public function is_message_type_active_for_messenger( $messenger_name, $message_type_name ) {
283
		$this->_initialize_collections();
284
		return ! empty( $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ] );
285
	}
286
287
288
289
	/**
290
	 * Returns whether the given messenger is active.
291
	 *
292
	 * @param string $messenger_name  the name of the messenger to check if active.
293
	 * @return bool
294
	 */
295
	public function is_messenger_active( $messenger_name ) {
296
		$this->_initialize_collections();
297
		return ! empty( $this->_active_message_types[ $messenger_name ] );
298
	}
299
300
301
	/**
302
	 * This returns any settings that might be on a message type for a messenger
303
	 *
304
	 * @param string $messenger_name  The slug of the messenger
305
	 * @param string $message_type_name  The slug of the message type getting the settings for.
306
	 * @return array
307
	 */
308
	public function get_message_type_settings_for_messenger( $messenger_name, $message_type_name ) {
309
		$settings = array();
310
		if ( $this->is_message_type_active_for_messenger( $messenger_name, $message_type_name ) ) {
311
			$settings =  isset( $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] )
312
				? $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings']
313
				: array();
314
		}
315
		return $settings;
316
	}
317
318
319
320
	/**
321
	 * Returns whether the given messenger name has active message types on it.
322
	 * Infers whether the messenger is active or not as well.
323
	 *
324
	 * @param string $messenger_name
325
	 * @return bool
326
	 */
327
	public function messenger_has_active_message_types( $messenger_name ) {
328
		$this->_initialize_collections();
329
		return
330
			! empty( $this->_active_message_types[ $messenger_name ] )
331
			&& ! empty( $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ] );
332
	}
333
334
335
336
	/**
337
	 * This checks the _active_message_types property for any active message types
338
	 * that are present for the given messenger and returns them.
339
	 *
340
	 * @since 4.9.0
341
	 * @param string $messenger_name The messenger being checked
342
	 * @return EE_message_type[]    (or empty array if none present)
343
	 */
344
	public function get_active_message_types_for_messenger( $messenger_name ) {
345
		$message_types = array();
346
		if ( ! $this->messenger_has_active_message_types( $messenger_name ) ) {
347
			return $message_types;
348
		}
349
		$installed_message_types = $this->installed_message_types();
350
		foreach ( $installed_message_types as $message_type_name => $message_type ) {
351
			if ( $this->is_message_type_active_for_messenger( $messenger_name, $message_type_name ) ) {
352
				$message_types[ $message_type_name ] = $message_type;
353
			}
354
		}
355
		return $message_types;
356
	}
357
358
359
360
	/**
361
	 * This does NOT return the _active_message_types property but
362
	 * simply returns an array of active message type names from that property.
363
	 * (The _active_message_types property is indexed by messenger and active message_types per messenger).
364
	 *
365
	 * @return array message_type references (string)
366
	 */
367
	public function list_of_active_message_types() {
368
		$active_message_type_names = array();
369
		$this->_initialize_collections();
370
		foreach ( $this->_active_message_types as $messenger => $messenger_settings ) {
371
			if ( ! isset( $messenger_settings['settings'][ $messenger . '-message_types' ] ) ) {
372
				continue;
373
			}
374
			foreach ( $messenger_settings['settings'][ $messenger . '-message_types' ] as $message_type_name => $message_type_config ) {
375
				if ( ! in_array( $message_type_name, $active_message_type_names ) ) {
376
					$active_message_type_names[] = $message_type_name;
377
				}
378
			}
379
		}
380
		return $active_message_type_names;
381
	}
382
383
384
385
	/**
386
	 * Same as list_of_active_message_types() except this returns actual EE_message_type objects
387
	 *
388
	 * @since 4.9.0
389
	 * @return \EE_message_type[]
390
	 */
391
	public function get_active_message_type_objects() {
392
		$active_message_types = array();
393
		$installed_message_types = $this->installed_message_types();
394
		$active_message_type_names = $this->list_of_active_message_types();
395
		foreach ( $active_message_type_names as $active_message_type_name ) {
396
			if ( isset( $installed_message_types[ $active_message_type_name ] ) ) {
397
				$active_message_types[ $active_message_type_name ] = $installed_message_types[ $active_message_type_name ];
398
			}
399
		}
400
		return $active_message_types;
401
	}
402
403
404
405
	/**
406
	 * @return \EE_message_type[]
407
	 */
408
	public function installed_message_types() {
409
		if ( empty( $this->_installed_message_types ) ) {
410
			$this->message_type_collection()->rewind();
411
			while ( $this->message_type_collection()->valid() ) {
412
				$this->_installed_message_types[ $this->message_type_collection()->current()->name ] = $this->message_type_collection()->current();
413
				$this->message_type_collection()->next();
414
			}
415
		}
416
		return $this->_installed_message_types;
417
	}
418
419
420
	/**
421
	 * @param string $message_type_name
422
	 * @return \EE_message_type
423
	 * @throws \EE_Error
424
	 */
425 View Code Duplication
	public function valid_message_type( $message_type_name ) {
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...
426
		$message_type = $this->get_message_type( $message_type_name );
427
		if ( $message_type instanceof EE_message_type ) {
428
			return $message_type;
429
		}
430
		throw new EE_Error(
431
			sprintf(
432
				__( 'The "%1$s" message type is either invalid or not installed', 'event_espresso' ),
433
				$message_type_name
434
			)
435
		);
436
	}
437
438
439
440
	/**
441
	 * valid_message_type_for_messenger
442
	 *
443
	 * @param EE_messenger $messenger
444
	 * @param string $message_type_name
445
	 * @return boolean
446
	 * @throws \EE_Error
447
	 */
448
	public function valid_message_type_for_messenger( EE_messenger $messenger, $message_type_name ) {
449
		$valid_message_types = $messenger->get_valid_message_types();
450
		if ( ! in_array( $message_type_name, $valid_message_types ) ) {
451
			throw new EE_Error(
452
				sprintf(
453
					__(
454
						'The message type (%1$s) sent to "%2$s" is not valid for the "%3$s" messenger.  Double-check the spelling and verify that message type has been registered as a valid type with the messenger.',
455
						'event_espresso'
456
					),
457
					$message_type_name,
458
					__METHOD__,
459
					$messenger->name
460
				)
461
			);
462
		}
463
		return true;
464
	}
465
466
467
	/**
468
	 * Used to return active messengers array stored in the wp options table.
469
	 * If no value is present in the option then an empty array is returned.
470
	 *
471
	 * @param   bool    $reset  If true then we ignore whether the option is cached on the _active_message_types
472
	 *                          property and pull directly from the db.  Otherwise whatever is currently on the
473
	 *                          $_active_message_types property is pulled.
474
	 *
475
	 * @return array
476
	 */
477
	public function get_active_messengers_option( $reset = false) {
478
		if ( $reset ) {
479
			$this->_active_message_types = get_option( 'ee_active_messengers', array() );
480
		}
481
		return $this->_active_message_types;
482
	}
483
484
485
486
	/**
487
	 * Used to update the active messengers array stored in the wp options table.
488
	 *
489
	 * @param array $active_messenger_settings Incoming data to save.  If empty, then the internal cached property
490
	 *                                 representing this data is used.
491
	 * @return bool FALSE if not updated, TRUE if updated.
492
	 */
493
	public function update_active_messengers_option( $active_messenger_settings = array() ) {
494
		$active_messenger_settings = empty( $active_messenger_settings ) ? $this->_active_message_types : $active_messenger_settings;
495
		//make sure _active_message_types is updated (this is the internal cache for the settings).
496
		$this->_active_message_types = $active_messenger_settings;
497
		return update_option( 'ee_active_messengers', $active_messenger_settings );
498
	}
499
500
501
502
	/**
503
	 * Used to return active messengers array stored in the wp options table.
504
	 * If no value is present in the option then an empty array is returned.
505
	 *
506
	 * The value is cached on the $_has_activated_messengers_and_message_types property for future calls.
507
	 *
508
	 * @param   bool    $reset  Used to indicate that any cached value should be ignored.
509
	 *
510
	 * @return array
511
	 */
512
	public function get_has_activated_messengers_option( $reset = false ) {
513
		if ( $reset ) {
514
			$this->_has_activated_messengers_and_message_types = get_option( 'ee_has_activated_messenger', array() );
515
		}
516
		return $this->_has_activated_messengers_and_message_types;
517
	}
518
519
520
521
	/**
522
	 * Used to update the active messengers array stored in the wp options table.
523
	 *
524
	 * @param array $has_activated_messengers Incoming data to save.  If empty, then the internal cached property
525
	 *                                        representing this data is used.
526
	 *
527
	 * @return bool FALSE if not updated, TRUE if updated.
528
	 */
529
	public function update_has_activated_messengers_option( $has_activated_messengers = array() ) {
530
		$has_activated_messengers = empty( $has_activated_messengers )
531
			? $this->_has_activated_messengers_and_message_types
532
			: $has_activated_messengers;
533
		return update_option( 'ee_has_activated_messenger', $has_activated_messengers );
534
	}
535
536
537
538
	/**
539
	 * wrapper for _set_active_messengers_and_message_types()
540
	 */
541
	public function reset_active_messengers_and_message_types() {
542
		$this->_set_active_messengers_and_message_types();
543
	}
544
545
546
547
	/**
548
	 * Generate list of active messengers and message types from collection.
549
	 * This sets up the active messengers from what is present in the database.
550
	 */
551
	protected function _set_active_messengers_and_message_types() {
552
		//echo "\n\n " . __LINE__ . ") " . __METHOD__ . "() \n";
553
		// list of activated messengers as set via the admin
554
		// note calling `get_active_messengers_options` also initializes the _active_message_types property.
555
		$this->get_active_messengers_option( true );
556
		$this->ensure_messengers_are_active( array(), false, true );
557
		$this->update_active_messengers_option();
558
		$this->update_has_activated_messengers_option();
559
	}
560
561
562
563
564
565
566
	/**
567
	 * Ensures that the specified messenger is currently active.
568
	 * If not, activates it and its default message types.
569
	 *
570
	 * @param string $messenger_name
571
	 * @param bool   $update_option  Whether to update the option in the db or not.
572
	 * @return boolean true if either already active or successfully activated.
573
	 */
574
	public function ensure_messenger_is_active( $messenger_name, $update_option = true ) {
575
		if ( ! isset( $this->_active_messengers[ $messenger_name ] ) ) {
576
			try {
577
				$this->activate_messenger( $messenger_name, array(), $update_option );
578
			} catch( EE_Error $e ) {
579
				EE_Error::add_error(
580
					$e->getMessage(),
581
					__FILE__,
582
					__FUNCTION__,
583
					__LINE__
584
				);
585
				return false;
586
			}
587
		}
588
		return true;
589
	}
590
591
592
	/**
593
	 * This ensures the given array of messenger names is active in the system.
594
	 * Note, this method will not activate any NEW message types for the messenger when it is called. Instead,
595
	 * it will automatically activate the default message types for the messenger if its not active.
596
	 *
597
	 * @param array $messenger_names  Array of messenger names for messengers to be activated.  If an empty array (default)
598
	 *                                then will attempt to set the active messengers from the activated_messengers option
599
	 *                                (stored in $_active_message_types property).
600
	 * @param bool  $update_option    Whether to update the related active messengers option.
601
	 * @param bool  $verify           Whether to verify the messengers are installed before activating. Note if this is set to true
602
	 *                                and a messenger is indicated as active, but is NOT installed, then it will automatically be
603
	 *                                deactivated.
604
	 */
605
	public function ensure_messengers_are_active( $messenger_names = array(), $update_option = true, $verify = false ) {
606
		$messenger_names = empty( $messenger_names ) ? array_keys( $this->_active_message_types ) : $messenger_names;
607
608
		$not_installed = array();
609
		foreach( $messenger_names as $messenger_name ) {
610
			if ( $verify && ! $this->messenger_collection()->has_by_name( $messenger_name ) ) {
611
				$not_installed[] = $messenger_name;
612
				$this->deactivate_messenger( $messenger_name );
613
				continue;
614
			}
615
			$this->ensure_messenger_is_active( $messenger_name, $update_option );
616
		}
617
618
		if ( ! empty( $not_installed_messenger ) ) {
0 ignored issues
show
Bug introduced by
The variable $not_installed_messenger does not exist. Did you mean $not_installed?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
619
			EE_Error::add_error(
620
				sprintf(
621
					__( 'The following messengers are either not installed or are invalid:%1$s %2$s', 'event_espresso' ),
622
					'<br />',
623
					implode( ', ', $not_installed_messenger )
624
				),
625
				__FILE__, __FUNCTION__, __LINE__
626
			);
627
		}
628
	}
629
630
631
632
	/**
633
	 * Ensures that the specified message type for the given messenger is currently active, if not activates it.
634
	 * This ALSO ensures that the given messenger is active as well!
635
	 *
636
	 * @param string $message_type_name message type name.
637
	 * @param        $messenger_name
638
	 * @param bool   $update_option     Whether to update the option in the db or not.
639
	 * @return bool  Returns true if already is active or if was activated successfully.
640
	 * @throws \EE_Error
641
	 */
642
	public function ensure_message_type_is_active( $message_type_name, $messenger_name, $update_option = true ) {
643
		// grab the messenger to work with.
644
		$messenger = $this->valid_messenger( $messenger_name );
645
		if ( $this->valid_message_type_for_messenger( $messenger, $message_type_name ) ) {
646
			//ensure messenger is active (that's an inherent coupling between active message types and the
647
			//messenger they are being activated for.
648
			try {
649
				if ( ! $this->is_message_type_active_for_messenger( $messenger_name, $message_type_name ) ) {
650
					//all is good so let's just get it active
651
					$this->activate_messenger( $messenger_name, array( $message_type_name ), $update_option );
652
				}
653
			} catch( EE_Error $e ) {
654
				EE_Error::add_error(
655
					$e->getMessage(),
656
					__FILE__,
657
					__FUNCTION__,
658
					__LINE__
659
				);
660
				return false;
661
			}
662
		}
663
		return true;
664
	}
665
666
667
668
669
	/**
670
	 * This is a wrapper for `ensure_message_type_is_active` that will handle ensuring multiple message types for a
671
	 * messenger are active in one go.
672
	 *
673
	 * @param array  $message_type_names  Array of message type names to ensure are active.
674
	 * @param string $messenger_name      The name of the messenger that the message types are to be activated on.
675
	 * @param bool   $update_option       Whether to persist the activation to the database or not (default true).
676
	 */
677
	public function ensure_message_types_are_active( $message_type_names, $messenger_name, $update_option = true ) {
678
		$message_type_names = (array) $message_type_names;
679
		foreach ( $message_type_names as $message_type_name ) {
680
			// note, intentionally not updating option here because we're in a loop.
681
			// We'll follow the instructions of the incoming $update_option argument after the loop.
682
			$this->ensure_message_type_is_active( $message_type_name, $messenger_name, false );
683
		}
684
		if ( $update_option ) {
685
			$this->update_active_messengers_option();
686
			$this->update_has_activated_messengers_option();
687
		}
688
	}
689
690
691
692
	/**
693
	 * Activates the specified messenger.
694
	 *
695
	 * @param string $messenger_name
696
	 * @param array  $message_type_names        An array of message type names to activate with this messenger.
697
	 *                                          If included we do NOT setup the default message types
698
	 *                                          (assuming they are already setup.)
699
	 * @param bool   $update_active_messengers_option
700
	 *
701
	 * @return array of generated templates
702
	 * @throws \EE_Error
703
	 */
704
	public function activate_messenger(
705
		$messenger_name,
706
		$message_type_names = array(),
707
		$update_active_messengers_option = true
708
	) {
709
		$templates = array();
710
		// grab the messenger to work with.
711
		$messenger = $this->messenger_collection()->get_by_info( $messenger_name );
712
		// it's inactive. Activate it.
713
		if ( $messenger instanceof EE_messenger ) {
714
			$this->_active_messengers[ $messenger->name ] = $messenger;
715
			//activate incoming message types set to be activated with messenger.
716
			$message_type_names = $this->_activate_message_types( $messenger, $message_type_names );
717
			// setup any initial settings for the messenger if necessary.
718
			$this->add_settings_for_messenger( $messenger->name );
719
			if ( $update_active_messengers_option ) {
720
				$this->update_active_messengers_option();
721
				$this->update_has_activated_messengers_option();
722
			}
723
			//generate new templates if necessary and ensure all related templates that are already in the database are
724
			//marked active.  Note, this will also deactivate a message type for a messenger if the template
725
			//cannot be successfully created during its attempt (only happens for global template attempts).
726
			if ( ! empty( $message_type_names ) ) {
727
				$templates = EEH_MSG_Template::generate_new_templates( $messenger->name, $message_type_names, 0, true );
728
				EEH_MSG_Template::update_to_active( array( $messenger->name ), $message_type_names );
729
			}
730
		}
731
		return $templates;
732
	}
733
734
735
736
	/**
737
	 * Activates given message types for the given EE_messenger object.
738
	 *
739
	 * Note: (very important) This method does not persist the activation to the database.
740
	 * See code implementing this method in this class for examples of how to persist.
741
	 *
742
	 * @param \EE_messenger $messenger
743
	 * @param  array        $message_type_names
744
	 *
745
	 * @return array
746
	 */
747
	protected function _activate_message_types( EE_messenger $messenger, $message_type_names = array() ) {
748
		//If $message_type_names is empty, AND $this->_active_message_types is empty, then that means
749
		//things have never been initialized (which should happen on EEH_Activation::generate_message_templates).
750
		//So ONLY then do we need to actually grab defaults and cycle through them.  Otherwise we
751
		//only override _active_message_types when an explicit array of $message_type_names has been provided.
752
		$message_type_names = empty( $message_type_names ) && ! isset( $this->_active_message_types[ $messenger->name ] )
753
			? $messenger->get_default_message_types()
754
			: (array) $message_type_names;
755
756
		//now we ALWAYS need to make sure that the messenger is active for the message types we're activating!
757
		if ( ! isset( $this->_active_message_types[ $messenger->name ] ) ) {
758
			$this->_active_message_types[ $messenger->name ]['settings'] = array();
759
		}
760
761
		if ( $message_type_names ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $message_type_names of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
762
			// cycle thru message types
763
			foreach ( $message_type_names as $message_type_name ) {
764
				//only register the message type as active IF it isn't already active
765
				//and if its actually installed.
766
				if (
767
					! $this->is_message_type_active_for_messenger( $messenger->name, $message_type_name )
768
				) {
769
					$this->add_settings_for_message_type( $messenger->name, $message_type_name );
770
					$this->_set_messenger_has_activated_message_type(
771
						$messenger,
772
						$message_type_name
773
					);
774
				}
775
			}
776
		}
777
		return $message_type_names;
778
	}
779
780
781
782
	/**
783
	 * add_settings_for_message_type
784
	 *
785
	 * NOTE This does NOT automatically persist any settings to the db.  Client code should call $this->update_active_messengers_option
786
	 * to persist.
787
	 *
788
	 * @param  string       $messenger_name   The name of the messenger adding the settings for
789
	 * @param  string       $message_type_name The name of the message type adding the settings for
790
	 * @param  array        $new_settings     Any new settings being set for the message type and messenger
791
	 */
792
	public function add_settings_for_message_type( $messenger_name, $message_type_name, $new_settings = array() ) {
793
		// get installed message type from collection
794
		$message_type = $this->message_type_collection()->get_by_info( $message_type_name );
795
		$existing_settings = $this->get_message_type_settings_for_messenger( $messenger_name, $message_type_name );
796
		//we need to setup any initial settings for message types
797
		if ( $message_type instanceof EE_message_type ) {
798
			$default_settings = $message_type->get_admin_settings_fields();
799
			foreach ( $default_settings as $field => $values ) {
800
				if ( isset( $new_settings[ $field ] ) ) {
801
					$existing_settings[ $field ] = $new_settings[ $field ];
802
					continue;
803
				}
804
				if ( ! isset( $existing_settings[ $field ] ) ) {
805
					$existing_settings[ $field ] = $values['default'];
806
				}
807
			}
808
		}
809
		$this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] = $existing_settings;
810
	}
811
812
813
814
	/**
815
	 * Updates the internal cached _has_activated_messengers_and_message_types property with the given messenger
816
	 * and message type.
817
	 *
818
	 * @access protected
819
	 * @param \EE_messenger $messenger
820
	 * @param string        $message_type_name
821
	 */
822
	protected function _set_messenger_has_activated_message_type( EE_messenger $messenger, $message_type_name ) {
823
		// make sure this messenger has a record in the has_activated array
824
		if ( ! isset( $this->_has_activated_messengers_and_message_types[ $messenger->name ] ) ) {
825
			$this->_has_activated_messengers_and_message_types[ $messenger->name ] = array();
826
		}
827
		// check if message type has already been added
828
		if ( ! in_array( $message_type_name, $this->_has_activated_messengers_and_message_types[ $messenger->name ] ) ) {
829
			$this->_has_activated_messengers_and_message_types[ $messenger->name ][] = $message_type_name;
830
		}
831
	}
832
833
834
835
	/**
836
	 * add_settings_for_messenger
837
	 *
838
	 * NOTE This does NOT automatically persist any settings to the db.  Client code should call $this->update_active_messengers_option
839
	 * to persist.
840
	 *
841
	 * @param string        $messenger_name The name of the messenger the settings is being added for.
842
	 * @param array         $new_settings   An array of settings to update the existing settings.
843
	 */
844
	public function add_settings_for_messenger( $messenger_name, $new_settings = array() ) {
845
		$messenger = $this->get_messenger( $messenger_name );
846
		if ( $messenger instanceof EE_messenger ) {
847
			$msgr_settings = $messenger->get_admin_settings_fields();
848
			if ( ! empty( $msgr_settings ) ) {
849
				foreach ( $msgr_settings as $field => $value ) {
850
					//is there a new setting for this?
851
					if ( isset( $new_settings[ $field ] ) ) {
852
						$this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $new_settings[ $field ];
853
						continue;
854
					}
855
					//only set the default if it isn't already set.
856
					if ( ! isset( $this->_active_message_types[ $messenger->name ]['settings'][ $field ] ) ) {
857
						$this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $value;
858
					}
859
				}
860
			}
861
		}
862
	}
863
864
865
866
	/**
867
	 * deactivate_messenger
868
	 *
869
	 * @param  string|EE_messenger $messenger_name name of messenger
870
	 * @return void
871
	 */
872
	public function deactivate_messenger( $messenger_name ) {
873
		if ( $messenger_name instanceof EE_messenger ) {
874
			$messenger_name = $messenger_name->name;
875
		}
876
		unset( $this->_active_messengers[ $messenger_name ] );
877
		unset( $this->_active_message_types[ $messenger_name ] );
878
		$this->_message_template_group_model->deactivate_message_template_groups_for( $messenger_name );
0 ignored issues
show
Documentation introduced by
$messenger_name is of type string, but the function expects a array.

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...
879
		$this->update_active_messengers_option();
880
	}
881
882
883
	/**
884
	 * Deactivates a message type (note this will deactivate across all messenger's it is active on.
885
	 *
886
	 * @param  string $message_type_name name of message type being deactivated
887
	 */
888
	public function deactivate_message_type( $message_type_name ) {
889
		if ( $message_type_name instanceof EE_message_type ) {
890
			$message_type_name = $message_type_name->name;
891
		}
892
		foreach ( $this->_active_message_types as $messenger => $settings ) {
893
			unset(
894
				$this->_active_message_types[ $messenger ]['settings'][ $messenger . '-message_types' ][ $message_type_name ]
895
			);
896
		}
897
		$this->_message_template_group_model->deactivate_message_template_groups_for( '', $message_type_name );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

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...
898
		$this->update_active_messengers_option();
899
	}
900
901
902
903
904
905
	/**
906
	 * Deactivates a message type for a specific messenger as opposed to all messengers.
907
	 *
908
	 * @param string $message_type_name  Name of message type being deactivated.
909
	 * @param string $messenger_name     Name of messenger the message type is being deactivated for.
910
	 */
911
	public function deactivate_message_type_for_messenger( $message_type_name, $messenger_name ) {
912
		if ( $this->is_message_type_active_for_messenger( $messenger_name, $message_type_name ) ) {
913
			unset( $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ] );
914
		}
915
		$this->_message_template_group_model->deactivate_message_template_groups_for( array( $messenger_name ), array( $message_type_name ) );
916
		$this->update_active_messengers_option();
917
	}
918
919
920
921
922
923
	/**
924
	 * Used to verify if a message can be sent for the given messenger and message type
925
	 * and that it is a generating messenger (used for generating message templates).
926
	 *
927
	 * @param EE_messenger    $messenger    messenger used in trigger
928
	 * @param EE_message_type $message_type message type used in trigger
929
	 *
930
	 * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send.
931
	 */
932
	public function is_generating_messenger_and_active( EE_messenger $messenger, EE_message_type $message_type ) {
933
		//get the $messengers the message type says it can be used with.
934
		foreach ( $message_type->with_messengers() as $generating_messenger => $secondary_messengers ) {
935
			if (
936
				$messenger->name === $generating_messenger
937
				&& $this->is_message_type_active_for_messenger( $messenger->name, $message_type->name )
938
			) {
939
				return true;
940
			}
941
		}
942
		return false;
943
	}
944
945
946
947
	/**
948
	 * This returns all the contexts that are registered by all message types.
949
	 *
950
	 * If $slugs_only is true,
951
	 * then just an array indexed by unique context slugs with the latest label representation for that slug.
952
	 * array(
953
	 *      'context_slug' => 'localized label for context obtained from latest message type in the loop'.
954
	 * );
955
	 *
956
	 * If $slugs_only is false, then the format is:
957
	 * array(
958
	 *      'message_type_name' => array(
959
	 *          'context_slug' => array(
960
	 *              'label' => 'localized label for context',
961
	 *              'description' => 'localized description for context'
962
	 *          )
963
	 *      )
964
	 * );
965
	 *
966
	 * Keep in mind that although different message types may share the same context slugs,
967
	 * it is possible that the context is described differently by the message type.
968
	 *
969
	 * @since 4.9.0
970
	 * @param   bool $slugs_only Whether to return an array of just slugs and labels (true)
971
	 *                           or all contexts indexed by message type.
972
	 * @return array
973
	 */
974
	public function get_all_contexts( $slugs_only = true ) {
975
		$key = $slugs_only ? 'slugs' : 'all';
976
		// check if contexts has been setup yet.
977
		if ( empty( $this->_contexts[ $key ] ) ) {
978
			// So let's get all active message type objects and loop through to get all unique contexts
979
			foreach ( $this->get_active_message_type_objects() as $message_type ) {
980
				if ( $message_type instanceof EE_message_type ) {
981
					$message_type_contexts = $message_type->get_contexts();
982
					if ( $slugs_only ) {
983
						foreach ( $message_type_contexts as $context => $context_details ) {
984
							$this->_contexts[ $key ][ $context ] = $context_details[ 'label' ];
985
						}
986
					} else {
987
						$this->_contexts[ $key ][ $message_type->name ] = $message_type_contexts;
988
					}
989
				}
990
			}
991
		}
992
		return ! empty( $this->_contexts[ $key ] ) ? $this->_contexts[ $key ] : array();
993
	}
994
995
996
997
998
	/**
999
	 * This checks the internal record of what message types are considered "active" and verifies that
1000
	 * there is an installed class definition for that message type.  If the active message type does not have a corresponding
1001
	 * accessible message type class then it will be deactivated from all messengers it is active on and any related
1002
	 * message templates will be inactivated as well.
1003
	 *
1004
	 * @return bool   true means all active message types are valid, false means at least one message type was deactivated.
1005
	 */
1006
	public function validate_active_message_types_are_installed() {
1007
		$list_of_active_message_type_names = $this->list_of_active_message_types();
1008
		$installed_message_types = $this->installed_message_types();
1009
		$all_message_types_valid = true;
1010
		//loop through list of active message types and verify they are installed.
1011
		foreach( $list_of_active_message_type_names as $message_type_name ) {
1012
			if ( ! isset( $installed_message_types[$message_type_name] ) ) {
1013
				$this->deactivate_message_type( $message_type_name );
1014
				$all_message_types_valid = false;
1015
			}
1016
		}
1017
		return $all_message_types_valid;
1018
	}
1019
1020
1021
1022
1023
	/**
1024
	 * This method checks the `ee_has_activated_messenger` option to see if the message type has ever been
1025
	 * activated for the given messenger.  This can be called by client code on plugin updates etc to determine whether
1026
	 * to attempt automatically reactivating message types that should be activated by default or not.
1027
	 *
1028
	 * @param $message_type_name
1029
	 * @param $messenger_name
1030
	 * @return bool
1031
	 */
1032
	public function has_message_type_been_activated_for_messenger( $message_type_name, $messenger_name ) {
1033
		$has_activated = $this->get_has_activated_messengers_option();
1034
		return isset( $has_activated[ $messenger_name ] )
1035
			&& in_array( $message_type_name, $has_activated[ $messenger_name ] );
1036
	}
1037
}
1038
// End of file EE_Message_Resource_Manager.lib.php
1039
// Location: /EE_Message_Resource_Manager.lib.php