Completed
Branch FET-9046-messages-queue (fc38ba)
by
unknown
475:27 queued 459:28
created

EE_Message_Resource_Manager   C

Complexity

Total Complexity 78

Size/Duplication

Total Lines 705
Duplicated Lines 6.1 %

Coupling/Cohesion

Components 1
Dependencies 9
Metric Value
wmc 78
lcom 1
cbo 9
dl 43
loc 705
rs 5

33 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A messenger_collection() 0 3 1
A active_messengers() 0 3 1
A get_messenger() 0 3 1
A get_active_messenger() 0 3 2
A installed_messengers() 0 12 3
A valid_messenger() 12 12 2
A message_type_collection() 0 3 1
A active_message_types() 0 3 1
A get_message_type() 0 3 1
A get_active_message_type_for_messenger() 0 5 2
A get_active_message_types_for_messenger() 0 14 4
A list_of_active_message_types() 7 11 4
A get_active_message_type_objects() 7 12 4
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 4 1
A update_active_messengers_option() 0 3 1
A get_has_activated_messengers_option() 0 3 1
A update_has_activated_messengers_option() 0 3 1
B _set_active_messengers_and_message_types() 0 31 6
A ensure_messenger_is_active() 0 7 2
A ensure_message_type_is_active() 0 7 1
A activate_messenger() 0 16 3
A _activate_message_types() 0 22 3
A _add_settings_for_message_type() 0 13 3
A _set_messenger_has_activated_message_type() 0 11 3
A _add_settings_for_messenger() 5 8 3
A _deactivate_messenger() 0 6 1
A _deactivate_message_type() 0 9 2
A is_generating_messenger_and_active() 0 12 4
B get_all_contexts() 0 20 8

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 EE_Messenger_Collection $_messenger_collection_loader
24
	 */
25
	protected $_messenger_collection_loader;
26
27
	/**
28
	 * @type EE_Message_Type_Collection $_message_type_collection_loader
29
	 */
30
	protected $_message_type_collection_loader;
31
32
	/**
33
	 * @type EEM_Message_Template_Group $_message_template_group_model
34
	 */
35
	protected $_message_template_group_model;
36
37
	/**
38
	 * @type EE_Messenger[]
39
	 */
40
	protected $_installed_messengers = array();
41
42
	/**
43
	 * @type EE_message_type[]
44
	 */
45
	protected $_installed_message_types = array();
46
47
	/**
48
	 * Array of active messengers.
49
	 * Format is this:
50
	 * array(
51
	 *      'messenger_name' => EE_messenger
52
	 * )
53
	 *
54
	 * @type EE_Messenger[]
55
	 */
56
	protected $_active_messengers = array();
57
58
	/**
59
	 * Formatted array of active message types grouped per messenger.
60
	 * Format is this:
61
	 * array(
62
	 *      'messenger_name' => array(
63
	 *          'settings' => array(
64
	 *              '{messenger_name}-message_types' => array(
65
	 *                  'message_type_name' => array() //variable array of settings corresponding to message type.
66
	 *              )
67
	 *          )
68
	 *      )
69
	 * )
70
	 *
71
	 * @type array
72
	 */
73
	protected $_active_message_types = array();
74
75
	/**
76
	 * An array of unique message type contexts across all active message types.
77
	 *
78
	 * The array will be indexed by either 'slugs' or 'all'.
79
	 * The slugs index contains an array indexed by unique context slugs with the latest label representation for that slug.
80
	 * array(
81
	 *      'context_slug' => 'localized label for context obtained from latest message type in the loop'.
82
	 * );
83
	 *
84
	 * The all index returns an array in this format:
85
	 * array(
86
	 *      'message_type_name' => array(
87
	 *          'context_slug' => array(
88
	 *              'label' => 'localized label for context',
89
	 *              'description' => 'localized description for context'
90
	 *          )
91
	 *      )
92
	 * );
93
	 *
94
	 * @type array
95
	 */
96
	protected $_contexts = array();
97
98
99
100
	/**
101
	 * EE_Message_Resource_Manager constructor.
102
	 *
103
	 * @param \EE_Messenger_Collection_Loader    $Messenger_Collection_Loader
104
	 * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader
105
	 * @param \EEM_Message_Template_Group        $Message_Template_Group_Model
106
	 */
107
	function __construct(
108
		EE_Messenger_Collection_Loader $Messenger_Collection_Loader,
109
		EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader,
110
		EEM_Message_Template_Group $Message_Template_Group_Model
111
	) {
112
		$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...
113
		$this->_messenger_collection_loader->load_messengers_from_folder();
114
		$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...
115
		$this->_message_type_collection_loader->load_message_types_from_folder();
116
		$this->_message_template_group_model = $Message_Template_Group_Model;
117
		$this->_set_active_messengers_and_message_types();
118
	}
119
120
121
122
	/**
123
	 * @return EE_Messenger_Collection
124
	 */
125
	public function messenger_collection() {
126
		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...
127
	}
128
129
130
131
	/**
132
	 * @return EE_Messenger[]
133
	 */
134
	public function active_messengers() {
135
		return $this->_active_messengers;
136
	}
137
138
139
140
	/**
141
	 * @param string $messenger_name
142
	 * @return \EE_Messenger
143
	 */
144
	public function get_messenger( $messenger_name ) {
145
		return $this->messenger_collection()->get_by_info( $messenger_name );
146
	}
147
148
149
150
	/**
151
	 * This returns the corresponding EE_Messenger object for the given string if it is active.
152
	 *
153
	 * @param string $messenger
154
	 * @return EE_Messenger | null
155
	 */
156
	public function get_active_messenger( $messenger ) {
157
		return ! empty( $this->_active_messengers[ $messenger ] ) ? $this->_active_messengers[ $messenger ] : null;
158
	}
159
160
161
162
	/**
163
	 * @return \EE_Messenger[]
164
	 */
165
	public function installed_messengers() {
166
		if ( empty( $this->_installed_messengers ) ) {
167
			$this->_installed_messengers = array();
168
			$this->messenger_collection()->rewind();
169
			while ( $this->messenger_collection()->valid() ) {
170
				$this->_installed_messengers[ $this->messenger_collection()->current()->name ] = $this->messenger_collection()->current(
171
				);
172
				$this->messenger_collection()->next();
173
			}
174
		}
175
		return $this->_installed_messengers;
176
	}
177
178
179
180
	/**
181
	 * @param string $messenger_name
182
	 * @return \EE_Messenger
183
	 * @throws \EE_Error
184
	 */
185 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...
186
		$messenger = $this->get_messenger( $messenger_name );
187
		if ( $messenger instanceof EE_Messenger ) {
188
			return $messenger;
189
		}
190
		throw new EE_Error(
191
			sprintf(
192
				__( 'The "%1$s" messenger is either invalid or not installed', 'event_espresso' ),
193
				$messenger_name
194
			)
195
		);
196
	}
197
198
199
200
	/**
201
	 * @return EE_Message_Type_Collection
202
	 */
203
	public function message_type_collection() {
204
		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...
205
	}
206
207
208
209
	/**
210
	 * @return array
211
	 */
212
	public function active_message_types() {
213
		return $this->_active_message_types;
214
	}
215
216
217
218
	/**
219
	 * @param string $message_type_name
220
	 * @return \EE_Message_Type
221
	 */
222
	public function get_message_type( $message_type_name ) {
223
		return $this->message_type_collection()->get_by_info( $message_type_name );
224
	}
225
226
227
228
	/**
229
	 * This returns the EE_message_type from the active message types array ( if present );
230
	 *
231
	 * @param string $messenger_name
232
	 * @param string $message_type_name
233
	 * @return \EE_Message_Type|null
234
	 */
235
	public function get_active_message_type_for_messenger( $messenger_name, $message_type_name ) {
236
		return ! empty( $this->_active_message_types[ $messenger_name ][ $message_type_name ] )
237
			? $this->get_message_type( $message_type_name )
238
			: null;
239
	}
240
241
242
243
	/**
244
	 * This checks the _active_message_types property for any active message types
245
	 * that are present for the given messenger and returns them.
246
	 *
247
	 * @since 4.9.0
248
	 * @param string $messenger_name The messenger being checked
249
	 * @return EE_message_type[]    (or empty array if none present)
250
	 */
251
	public function get_active_message_types_for_messenger( $messenger_name ) {
252
		if ( empty( $this->_active_message_types[ $messenger_name ] ) ) {
253
			return array();
254
		}
255
		$message_types = array();
256
		$active_message_types = $this->_active_message_types[ $messenger_name ];
257
		$installed_message_types = $this->installed_message_types();
258
		foreach ( $active_message_types as $message_type_name => $settings ) {
259
			if ( ! empty( $installed_message_types[ $message_type_name ] ) ) {
260
				$message_types[] = $installed_message_types[ $message_type_name ];
261
			}
262
		}
263
		return $message_types;
264
	}
265
266
267
268
	/**
269
	 * This does NOT return the _active_message_types property but
270
	 * simply returns an array of active message type names from that property.
271
	 * (The _active_message_types property is indexed by messenger and active message_types per messenger).
272
	 *
273
	 * @return array message_type references (string)
274
	 */
275
	public function list_of_active_message_types() {
276
		$message_types = array();
277 View Code Duplication
		foreach ( $this->_active_message_types as $messenger => $message_type_data ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
278
			foreach ( $message_type_data as $message_type => $config ) {
279
				if ( ! in_array( $message_type, $message_types ) ) {
280
					$message_types[] = $message_type;
281
				}
282
			}
283
		}
284
		return $message_types;
285
	}
286
287
288
289
	/**
290
	 * Same as list_of_active_message_types() except this returns actual EE_message_type objects
291
	 *
292
	 * @since 4.9.0
293
	 * @return \EE_message_type[]
294
	 */
295
	public function get_active_message_type_objects() {
296
		$message_types = array();
297
		$installed_message_types = $this->installed_message_types();
298 View Code Duplication
		foreach ( $this->_active_message_types as $messenger => $message_type_data ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
299
			foreach ( $message_type_data as $message_type => $config ) {
300
				if ( ! isset( $message_type, $message_types ) ) {
301
					$message_types[ $message_type ] = $installed_message_types[ $message_type ];
302
				}
303
			}
304
		}
305
		return $message_types;
306
	}
307
308
309
310
	/**
311
	 * @return \EE_Message_Type[]
312
	 */
313
	public function installed_message_types() {
314
		if ( empty( $this->_installed_message_types ) ) {
315
			$this->message_type_collection()->rewind();
316
			while ( $this->message_type_collection()->valid() ) {
317
				$this->_installed_message_types[ $this->message_type_collection()->current()->name ] = $this->message_type_collection()->current();
318
				$this->message_type_collection()->next();
319
			}
320
		}
321
		return $this->_installed_message_types;
322
	}
323
324
325
	/**
326
	 * @param string $message_type_name
327
	 * @return \EE_message_type
328
	 * @throws \EE_Error
329
	 */
330 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...
331
		$message_type = $this->get_message_type( $message_type_name );
332
		if ( $message_type instanceof EE_message_type ) {
333
			return $message_type;
334
		}
335
		throw new EE_Error(
336
			sprintf(
337
				__( 'The "%1$s" message type is either invalid or not installed', 'event_espresso' ),
338
				$message_type_name
339
			)
340
		);
341
	}
342
343
344
345
	/**
346
	 * valid_message_type_for_messenger
347
	 *
348
	 * @param EE_Messenger $messenger
349
	 * @param string $message_type_name
350
	 * @return array
351
	 * @throws \EE_Error
352
	 */
353
	public function valid_message_type_for_messenger( EE_Messenger $messenger, $message_type_name ) {
354
		$valid_message_types = $messenger->get_valid_message_types();
355
		if ( ! in_array( $message_type_name, $valid_message_types ) ) {
356
			throw new EE_Error(
357
				sprintf(
358
					__(
359
						'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.',
360
						'event_espresso'
361
					),
362
					$message_type_name,
363
					__METHOD__,
364
					$messenger
365
				)
366
			);
367
		}
368
		return true;
369
	}
370
371
372
	/**
373
	 * Used to return active messengers array stored in the wp options table.
374
	 * If no value is present in the option then an empty array is returned.
375
	 *
376
	 * @return array
377
	 */
378
	public function get_active_messengers_option() {
379
		return get_option( 'ee_active_messengers', array() );
380
381
	}
382
383
384
385
	/**
386
	 * Used to update the active messengers array stored in the wp options table.
387
	 *
388
	 * @param array $active_messengers Incoming data to save.
389
	 * @return bool FALSE if not updated, TRUE if updated.
390
	 */
391
	public function update_active_messengers_option( $active_messengers ) {
392
		return update_option( 'ee_active_messengers', $active_messengers );
393
	}
394
395
396
397
	/**
398
	 * Used to return active messengers array stored in the wp options table.
399
	 * If no value is present in the option then an empty array is returned.
400
	 *
401
	 * @return array
402
	 */
403
	public function get_has_activated_messengers_option() {
404
			get_option( 'ee_has_activated_messenger', array() );
405
	}
406
407
408
409
	/**
410
	 * Used to update the active messengers array stored in the wp options table.
411
	 *
412
	 * @param array $has_activated_messengers Incoming data to save.
413
	 * @return bool FALSE if not updated, TRUE if updated.
414
	 */
415
	public function update_has_activated_messengers_option( $has_activated_messengers ) {
416
		return update_option( 'ee_has_activated_messenger', $has_activated_messengers );
417
	}
418
419
420
421
	/**
422
	 * Generate list of active messengers and message types from collection.
423
	 * This sets up the active messengers from what is present in the database.
424
	 */
425
	protected function _set_active_messengers_and_message_types() {
426
		// list of activated messengers as set via the admin
427
		$active_messengers = $this->get_active_messengers_option();
428
		$active_messengers = is_array( $active_messengers ) ? $active_messengers : array( $active_messengers );
429
		$not_installed = array();
430
		foreach ( $active_messengers as $active_messenger => $data ) {
431
			// check if supposedly active messenger is actually installed by looking in our collection
432
			if ( $this->messenger_collection()->has_by_name( $active_messenger ) ) {
433
				// grab installed messenger object
434
				$this->_active_messengers[ $active_messenger ] = $this->messenger_collection()->get_by_info(
435
					$active_messenger
436
				);
437
				$this->_active_message_types[ $active_messenger ] = ! empty( $data[ 'settings' ][ $active_messenger . '-message_types' ] )
438
					? $data[ 'settings' ][ $active_messenger . '-message_types' ]
439
					: array();
440
			} else {
441
				$not_installed[] = $active_messenger;
442
				$this->_deactivate_messenger( $active_messenger );
443
			}
444
		}
445
		if ( ! empty( $not_installed ) ) {
446
			EE_Error::add_error(
447
				sprintf(
448
					__( 'The following messengers are either not installed or are invalid:%1$s %2$s', 'event_espresso' ),
449
					'<br />',
450
					implode( ', ', $not_installed )
451
				),
452
				__FILE__, __FUNCTION__, __LINE__
453
			);
454
		}
455
	}
456
457
458
459
	/**
460
	 * Ensures that the specified messenger is currently active.
461
	 * If not, activates it and its default message types.
462
	 *
463
	 * @param string $messenger_name
464
	 * @return boolean TRUE if it was PREVIOUSLY active, and FALSE if it was previously inactive
465
	 */
466
	public function ensure_messenger_is_active( $messenger_name ) {
467
		if ( ! isset( $this->_active_messengers[ $messenger_name ] ) ) {
468
			$this->activate_messenger( $messenger_name );
469
			return false;
470
		}
471
		return true;
472
	}
473
474
475
476
	/**
477
	 * Ensures that the specified message type for the given messenger is currently active, if not activates it.
478
	 * This ALSO ensures that the given messenger is active as well!
479
	 *
480
	 * @param string $message_type message type name
481
	 * @param        $messenger_name
482
	 * @return array
483
	 * @throws \EE_Error
484
	 */
485
	public function ensure_message_type_is_active( $message_type, $messenger_name ) {
486
		// grab the messenger to work with.
487
		$messenger = $this->valid_messenger( $messenger_name );
488
		$this->valid_message_type_for_messenger( $messenger, $message_type );
489
		//all is good so let's just get it active
490
		return $this->_activate_message_types( $messenger, array( $message_type ) );
491
	}
492
493
494
	/**
495
	 * Activates the specified messenger.
496
	 *
497
	 * @param string $messenger_name
498
	 * @param array  $message_type_names An array of message type names to activate with this messenger.
499
	 *                                          If included we do NOT setup the default message types
500
	 *                                          (assuming they are already setup.)
501
	 * @return array of generated templates
502
	 */
503
	public function activate_messenger( $messenger_name, $message_type_names = array() ) {
504
		$templates = array();
505
		// grab the messenger to work with.
506
		$messenger = $this->messenger_collection()->get_by_info( $messenger_name );
507
		// it's inactive. Activate it.
508
		if ( $messenger instanceof EE_Messenger ) {
509
			$this->_active_messengers[ $messenger->name ] = $messenger;
510
			$message_type_names = $this->_activate_message_types( $messenger, $message_type_names );
511
			$this->update_active_messengers_option( $this->_active_message_types );
512
			// might need to generate new templates
513
			if ( ! empty( $message_type_names ) ) {
514
				$templates = EEH_MSG_Template::generate_new_templates( $messenger->name, $message_type_names, 0, true );
515
			}
516
		}
517
		return $templates;
518
	}
519
520
521
	/**
522
	 * Activates given message types for the given EE_Messenger object.
523
	 *
524
	 * @param \EE_Messenger $messenger
525
	 * @param  array        $message_type_names
526
	 *
527
	 * @return array
528
	 */
529
	protected function _activate_message_types( EE_Messenger $messenger, $message_type_names = array() ) {
530
		// get has_active so we can be sure its kept up to date.
531
		$has_activated = $this->get_has_activated_messengers_option();
532
		// use incoming list of message types or if that's empty, then get defaults
533
		$message_type_names = ! empty( $message_type_names )
534
			? $message_type_names
535
			: $messenger->get_default_message_types();
536
		// cycle thru message types
537
		foreach ( $message_type_names as $message_type_name ) {
538
			$this->_add_settings_for_message_type( $messenger, $message_type_name );
539
			$has_activated = $this->_set_messenger_has_activated_message_type(
540
				$messenger,
541
				$has_activated,
0 ignored issues
show
Bug introduced by
It seems like $has_activated can also be of type null; however, EE_Message_Resource_Mana...ctivated_message_type() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
542
				$message_type_name
543
			);
544
		}
545
		// setup any initial settings for the messenger
546
		$this->_add_settings_for_messenger( $messenger );
547
		$this->_active_message_types[ $messenger->name ][ 'obj' ] = $messenger;
548
		$this->update_has_activated_messengers_option( $has_activated );
0 ignored issues
show
Bug introduced by
It seems like $has_activated defined by $this->get_has_activated_messengers_option() on line 531 can also be of type null; however, EE_Message_Resource_Mana...ted_messengers_option() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
549
		return $message_type_names;
550
	}
551
552
553
554
	/**
555
	 * _get_settings_for_message_type
556
	 *
557
	 * @access protected
558
	 * @param \EE_Messenger $messenger
559
	 * @param  string       $message_type_name
560
	 */
561
	protected function _add_settings_for_message_type( EE_Messenger $messenger, $message_type_name ) {
562
		$settings = array();
563
		// get installed message type from collection
564
		$message_type = $this->message_type_collection()->get_by_info( $message_type_name );
565
		//we need to setup any initial settings for message types
566
		if ( $message_type instanceof EE_Message_Type ) {
567
			$settings_fields = $message_type->get_admin_settings_fields();
568
			foreach ( $settings_fields as $field => $values ) {
569
				$settings[ $field ] = $values[ 'default' ];
570
			}
571
		}
572
		$this->_active_message_types[ $messenger->name ][ 'settings' ][ $messenger->name . '-message_types' ][ $message_type_name ][ 'settings' ] = $settings;
573
	}
574
575
576
577
	/**
578
	 * _set_messenger_has_activated_message_type
579
	 *
580
	 * @access protected
581
	 * @param \EE_Messenger $messenger
582
	 * @param array         $has_activated
583
	 * @param string        $message_type_name
584
	 * @return array
585
	 */
586
	protected function _set_messenger_has_activated_message_type( EE_Messenger $messenger, $has_activated, $message_type_name ) {
587
		// make sure this messenger has a record in the has_activated array
588
		if ( ! isset( $has_activated[ $messenger->name ] ) ) {
589
			$has_activated[ $messenger->name ] = array();
590
		}
591
		// check if message type has already been added
592
		if ( ! in_array( $message_type_name, $has_activated[ $messenger->name ] ) ) {
593
			$has_activated[ $messenger->name ][] = $message_type_name;
594
		}
595
		return $has_activated;
596
	}
597
598
599
600
	/**
601
	 * _add_settings_for_messenger
602
	 *
603
	 * @access protected
604
	 * @param \EE_Messenger $messenger
605
	 */
606
	protected function _add_settings_for_messenger( EE_Messenger $messenger ) {
607
		$msgr_settings = $messenger->get_admin_settings_fields();
608 View Code Duplication
		if ( ! empty( $msgr_settings ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
609
			foreach ( $msgr_settings as $field => $value ) {
610
				$this->_active_message_types[ $messenger->name ][ 'settings' ][ $field ] = $value;
611
			}
612
		}
613
	}
614
615
616
617
	/**
618
	 * _deactivate_messenger
619
	 *
620
	 * @access protected
621
	 * @param  string $messenger name of messenger
622
	 * @return void
623
	 */
624
	protected function _deactivate_messenger( $messenger ) {
625
		unset( $this->_active_messengers[ $messenger ] );
626
		unset( $this->_active_message_types[ $messenger ] );
627
		$this->_message_template_group_model->deactivate_message_template_groups_for( $messenger );
628
		$this->update_active_messengers_option( $this->_active_message_types );
629
	}
630
631
632
	/**
633
	 * Deactivates a message type (note this will deactivate across all messenger's it is active on.
634
	 *
635
	 * @param  string $message_type_name name of message type being deactivated
636
	 */
637
	protected function _deactivate_message_type( $message_type_name ) {
638
		foreach ( $this->_active_message_types as $messenger => $settings ) {
639
			unset(
640
				$this->_active_message_types[ $messenger ][ 'settings' ][ $messenger . '-message_types' ][ $message_type_name ]
641
			);
642
		}
643
		$this->_message_template_group_model->deactivate_message_template_groups_for( '', $message_type_name );
644
		$this->update_active_messengers_option( $this->_active_message_types );
645
	}
646
647
648
649
	/**
650
	 * Used to verify if a message can be sent for the given messenger and message type
651
	 * and that it is a generating messenger (used for generating message templates).
652
	 *
653
	 * @param EE_Messenger    $messenger    messenger used in trigger
654
	 * @param EE_message_type $message_type message type used in trigger
655
	 *
656
	 * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send.
657
	 */
658
	public function is_generating_messenger_and_active( EE_Messenger $messenger, EE_message_type $message_type ) {
659
		//get the $messengers the message type says it can be used with.
660
		foreach ( $message_type->with_messengers() as $generating_messenger => $secondary_messengers ) {
661
			if (
662
				$messenger->name == $generating_messenger
663
				&& isset( $this->_active_message_types[ $generating_messenger ][ $message_type->name ] )
664
			) {
665
				return true;
666
			}
667
		}
668
		return false;
669
	}
670
671
672
673
	/**
674
	 * This returns all the contexts that are registered by all message types.
675
	 *
676
	 * If $slugs_only is true, then just an array indexed by unique context slugs with the latest label representation for that slug.
677
	 * array(
678
	 *      'context_slug' => 'localized label for context obtained from latest message type in the loop'.
679
	 * );
680
	 *
681
	 * If $slugs_only is false, then the format is:
682
	 * array(
683
	 *      'message_type_name' => array(
684
	 *          'context_slug' => array(
685
	 *              'label' => 'localized label for context',
686
	 *              'description' => 'localized description for context'
687
	 *          )
688
	 *      )
689
	 * );
690
	 *
691
	 * Keep in mind that although different message types may share the same context slugs, it is possible that the context
692
	 * is described differently by the message type.
693
	 *
694
	 * >>>>>>>>>>>> 1 usage in \EE_Message_List_Table::_get_table_filters()
695
	 * >>>>>>>>>>>> 1 usage in \EE_Message::context_label()
696
	 * >>>>>>>>>>>> 1 usage in \EE_messages_Test::test_get_all_contexts()
697
	 *
698
	 * @since 4.9.0
699
	 * @param   bool $slugs_only Whether to return an array of just slugs and labels (true) or all contexts indexed by message type.
700
	 * @return array
701
	 */
702
	public function get_all_contexts( $slugs_only = true ) {
703
		$key = $slugs_only ? 'slugs' : 'all';
704
		// check if contexts has been setup yet.
705
		if ( empty( $this->_contexts[ $key ] ) ) {
706
			// So let's get all active message type objects and loop through to get all unique contexts
707
			foreach ( $this->get_active_message_type_objects() as $message_type ) {
708
				if ( $message_type instanceof EE_message_type ) {
709
					$message_type_contexts = $message_type->get_contexts();
710
					if ( $slugs_only ) {
711
						foreach ( $message_type_contexts as $context => $context_details ) {
712
							$this->_contexts[ $key ][ $context ] = $context_details[ 'label' ];
713
						}
714
					} else {
715
						$this->_contexts[ $key ][ $message_type->name ] = $message_type_contexts;
716
					}
717
				}
718
			}
719
		}
720
		return ! empty( $this->_contexts[ $key ] ) ? $this->_contexts[ $key ] : array();
721
	}
722
723
724
}
725
// End of file EE_Message_Resource_Manager.lib.php
726
// Location: /EE_Message_Resource_Manager.lib.php