Completed
Branch BUG-9774-email-validation (c72797)
by
unknown
1171:55 queued 1157:09
created

EEH_MSG_Template   F

Complexity

Total Complexity 112

Size/Duplication

Total Lines 1194
Duplicated Lines 3.69 %

Coupling/Cohesion

Components 3
Dependencies 18

Importance

Changes 0
Metric Value
dl 44
loc 1194
rs 0.6314
c 0
b 0
f 0
wmc 112
lcom 3
cbo 18

33 Methods

Rating   Name   Duplication   Size   Complexity  
A _set_autoloader() 0 3 1
C generate_new_templates() 0 50 10
A already_generated() 0 17 2
A update_to_active() 0 13 3
A update_to_inactive() 0 6 1
A get_installed_message_objects() 0 8 1
D get_shortcodes() 0 106 20
A messenger_obj() 0 5 1
A message_type_obj() 0 5 1
A is_mt_active() 0 6 1
A is_messenger_active() 0 6 2
A get_active_messengers_in_db() 10 10 1
A update_active_messengers_in_db() 10 10 1
B get_url_trigger() 0 58 8
A generate_browser_trigger() 11 11 1
A generate_error_display_trigger() 13 13 1
B generate_url_trigger() 0 35 1
A get_message_action_icon() 0 4 2
B get_message_action_icons() 0 34 1
A get_message_action_url() 0 4 2
B get_message_action_urls() 0 67 4
B get_message_action_link() 0 22 5
A reg_status_to_message_type_array() 0 12 1
A convert_reg_status_to_message_type() 0 4 2
A payment_status_to_message_type_array() 0 12 1
A convert_payment_status_to_message_type() 0 4 2
B get_template_pack() 0 29 5
C get_template_pack_collection() 0 50 8
A create_new_templates() 0 11 2
B _create_new_templates() 0 31 3
C _create_custom_template_group() 0 75 10
B message_type_has_active_templates_for_messenger() 0 36 3
B get_fields() 0 26 5

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 EEH_MSG_Template 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 EEH_MSG_Template, 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
 * Event Espresso
8
 *
9
 * Event Registration and Management Plugin for WordPress
10
 *
11
 * @ package			Event Espresso
12
 * @ author			Seth Shoultes
13
 * @ copyright		(c) 2008-2011 Event Espresso  All Rights Reserved.
14
 * @ license			http://eventespresso.com/support/terms-conditions/   * see Plugin Licensing *
15
 * @ link				http://www.eventespresso.com
16
 * @ version		 	4.0
17
 *
18
 * ------------------------------------------------------------------------
19
 *
20
 * EEH_MSG_Template
21
 * Utility class containing a variety of helpers related to message templates.
22
 *
23
 * @package		Event Espresso
24
 * @subpackage	includes/core
25
 * @author		Darren Ethier
26
 *
27
 * ------------------------------------------------------------------------
28
 */
29
class EEH_MSG_Template {
30
31
32
	/**
33
	 * Holds a collection of EE_Message_Template_Pack objects.
34
	 * @type EE_Messages_Template_Pack_Collection
35
	 */
36
	protected static $_template_pack_collection;
37
38
39
40
41
	private static function _set_autoloader() {
42
		EED_Messages::set_autoloaders();
43
	}
44
45
46
	/**
47
	 * generate_new_templates
48
	 * This will handle the messenger, message_type selection when "adding a new custom template" for an event and will automatically create the defaults for the event.  The user would then be redirected to edit the default context for the event.
49
	 *
50
	 * @access protected
51
	 * @param  string $messenger     the messenger we are generating templates for
52
	 * @param array   $message_types array of message types that the templates are generated for.
53
	 * @param int     $GRP_ID        If a non global template is being generated then it is expected we'll have a GRP_ID to use as the base for the new generated template.
54
	 * @param bool    $global        true indicates generating templates on messenger activation. false requires GRP_ID for event specific template generation.
55
	 * @throws \EE_Error
56
	 * @return array  @see EEH_MSG_Template::_create_new_templates for the return value of each element in the array for templates
57
	 *                that are generated.  If this is an empty array then it means no templates were generated which usually
58
	 *                means there was an error.  Anything in the array with an empty value for `MTP_context` means that it
59
	 *                was not a new generated template but just reactivated (which only happens for global templates that
60
	 *                already exist in the database.
61
	 */
62
	public static function generate_new_templates( $messenger, $message_types, $GRP_ID = 0, $global = false ) {
63
		//make sure message_type is an array.
64
		$message_types = (array) $message_types;
65
		$templates = array();
66
67
		if ( empty( $messenger ) ) {
68
			throw new EE_Error( __('We need a messenger to generate templates!', 'event_espresso') );
69
		}
70
71
		//if we STILL have empty $message_types then we need to generate an error message b/c we NEED message types to do the template files.
72
		if ( empty( $message_types ) ) {
73
			throw new EE_Error( __('We need at least one message type to generate templates!', 'event_espresso') );
74
		}
75
76
		EEH_MSG_Template::_set_autoloader();
77
		foreach ( $message_types as $message_type ) {
78
			//if global then let's attempt to get the GRP_ID for this combo IF GRP_ID is empty.
79
			if ( $global && empty( $GRP_ID ) ) {
80
				$GRP_ID = EEM_Message_Template_Group::instance()->get_one(
81
					array(
82
						array(
83
							'MTP_messenger'    => $messenger,
84
							'MTP_message_type' => $message_type,
85
							'MTP_is_global'    => true,
86
						),
87
					)
88
				);
89
				$GRP_ID = $GRP_ID instanceof EE_Message_Template_Group ? $GRP_ID->ID() : 0;
90
			}
91
			// if this is global template generation.
92
			// First let's determine if we already HAVE global templates for this messenger and message_type combination.
93
			//  If we do then NO generation!!
94
			if ( $global && EEH_MSG_Template::already_generated( $messenger, $message_type, $GRP_ID ) ) {
95
				$templates[] = array(
96
					'GRP_ID' => $GRP_ID,
97
					'MTP_context' => '',
98
				);
99
				//we already have generated templates for this so let's go to the next message type.
100
				continue;
101
			}
102
			$new_message_template_group = EEH_MSG_Template::create_new_templates( $messenger, $message_type, $GRP_ID, $global );
103
104
			if ( ! $new_message_template_group ) {
105
				continue;
106
			}
107
			$templates[] = $new_message_template_group;
108
		}
109
110
		return $templates;
111
	}
112
113
114
	/**
115
	 * The purpose of this method is to determine if there are already generated templates in the database for the given variables.
116
	 * @param  string $messenger     messenger
117
	 * @param  string $message_type message type
118
	 * @param  int $GRP_ID        GRP ID ( if a custom template) (if not provided then we're just doing global template check)
119
	 * @return bool                true = generated, false = hasn't been generated.
120
	 */
121
	public static function already_generated( $messenger, $message_type, $GRP_ID = 0 ) {
122
		EEH_MSG_Template::_set_autoloader();
123
		//what method we use depends on whether we have an GRP_ID or not
124
		$count = empty( $GRP_ID )
125
			? EEM_Message_Template::instance()->count(
126
				array(
127
					array(
128
						'Message_Template_Group.MTP_messenger' 	  => $messenger,
129
						'Message_Template_Group.MTP_message_type' => $message_type,
130
						'Message_Template_Group.MTP_is_global'    => true
131
					)
132
				)
133
			)
134
			: EEM_Message_Template::instance()->count( array( array( 'GRP_ID' => $GRP_ID ) ) );
135
136
		return $count > 0;
137
	}
138
139
140
141
142
	/**
143
	 * Updates all message templates matching the incoming messengers and message types to active status.
144
	 *
145
	 * @static
146
	 * @param  array  $messenger_names     Messenger slug
147
	 * @param  array  $message_type_names  Message type slug
148
	 * @return  int 						count of updated records.
149
	 */
150
	public static function update_to_active( $messenger_names, $message_type_names ) {
151
		$messenger_names = is_array( $messenger_names ) ? $messenger_names : array( $messenger_names );
152
		$message_type_names = is_array( $message_type_names ) ? $message_type_names : array( $message_type_names );
153
		return EEM_Message_Template_Group::instance()->update(
154
			array( 'MTP_is_active' => 1 ),
155
			array(
156
				array(
157
					'MTP_messenger' 	=> array( 'IN', $messenger_names ),
158
					'MTP_message_type'  => array( 'IN', $message_type_names )
159
				)
160
			)
161
		);
162
	}
163
164
165
166
	/**
167
	 * Updates all message template groups matching the incoming arguments to inactive status.
168
	 *
169
	 * @static
170
	 * @param array $messenger_names 	The messenger slugs.
171
	 *                          	If empty then all templates matching the message types are marked inactive.
172
	 *                          	Otherwise only templates matching the messengers and message types.
173
	 * @param array $message_type_names 	The message type slugs.
174
	 *                              If empty then all templates matching the messengers are marked inactive.
175
	 * 								Otherwise only templates matching the messengers and message types.
176
	 *
177
	 * @return int  count of updated records.
178
	 */
179
	public static function update_to_inactive( $messenger_names = array(), $message_type_names = array() ) {
180
		return EEM_Message_Template_Group::instance()->deactivate_message_template_groups_for(
181
			$messenger_names,
182
			$message_type_names
183
		);
184
	}
185
186
187
	/**
188
	 * The purpose of this function is to return all installed message objects
189
	 * (messengers and message type regardless of whether they are ACTIVE or not)
190
	 *
191
	 * @deprecated 4.9.0
192
	 * @static
193
	 * @param string $type
194
	 * @return array array consisting of installed messenger objects and installed message type objects.
195
	 */
196
	public static function get_installed_message_objects( $type = 'all' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
197
		self::_set_autoloader();
198
		$message_resource_manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
199
		return array(
200
			'messenger' => $message_resource_manager->installed_messengers(),
201
			'message_type' => $message_resource_manager->installed_message_types()
202
		);
203
	}
204
205
206
	/**
207
	 * This will return an array of shortcodes => labels from the
208
	 * messenger and message_type objects associated with this
209
	 * template.
210
	 *
211
	 * @since 4.3.0
212
	 *
213
	 * @param string $message_type
214
	 * @param string $messenger
215
	 * @param array  $fields 	What fields we're returning valid shortcodes for.
216
	 *                          If empty then we assume all fields are to be returned. Optional.
217
	 * @param string $context 	What context we're going to return shortcodes for. Optional.
218
	 * @param bool $merged 		If TRUE then we don't return shortcodes indexed by field,
219
	 *                          but instead an array of the unique shortcodes for all the given ( or all) fields.
220
	 *                          Optional.
221
	 * @throws \EE_Error
222
	 * @return mixed (array|bool) an array of shortcodes in the format
223
	 * 												array( '[shortcode] => 'label')
224
	 *												OR
225
	 * 												FALSE if no shortcodes found.
226
	 */
227
	public static function get_shortcodes(
228
		$message_type,
229
		$messenger,
230
		$fields = array(),
231
		$context = 'admin',
232
		$merged = false
233
	) {
234
		$messenger_name = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $messenger ) ) );
235
		$mt_name = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $message_type ) ) );
236
		/** @var EE_Message_Resource_Manager $message_resource_manager */
237
		$message_resource_manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
238
		//convert slug to object
239
		$messenger = $message_resource_manager->get_messenger( $messenger );
240
241
		//if messenger isn't a EE_messenger resource then bail.
242
		if ( ! $messenger instanceof EE_messenger ) {
243
			return array();
244
		}
245
246
		//validate class for getting our list of shortcodes
247
		$classname = 'EE_Messages_' . $messenger_name . '_' . $mt_name . '_Validator';
248
		if ( ! class_exists( $classname ) ) {
249
			$msg[] = __( 'The Validator class was unable to load', 'event_espresso' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$msg was never initialized. Although not strictly required by PHP, it is generally a good practice to add $msg = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
250
			$msg[] = sprintf(
251
				__( 'The class name compiled was %s. Please check and make sure the spelling and case is correct for the class name and that there is an autoloader in place for this class', 'event_espresso' ),
252
				$classname
253
			);
254
			throw new EE_Error( implode( '||', $msg ) );
255
		}
256
257
		/** @type EE_Messages_Validator $_VLD */
258
		$_VLD = new $classname( array(), $context );
259
		$valid_shortcodes = $_VLD->get_validators();
260
261
		//let's make sure we're only getting the shortcode part of the validators
262
		$shortcodes = array();
263
		foreach ( $valid_shortcodes as $field => $validators ) {
264
			$shortcodes[ $field ] = $validators['shortcodes'];
265
		}
266
		$valid_shortcodes = $shortcodes;
267
268
		//if not all fields let's make sure we ONLY include the shortcodes for the specified fields.
269
		if ( ! empty( $fields ) ) {
270
			$specified_shortcodes = array();
271
			foreach ( $fields as $field ) {
272
				if ( isset( $valid_shortcodes[ $field ] ) ) {
273
					$specified_shortcodes[ $field ] = $valid_shortcodes[ $field ];
274
				}
275
			}
276
			$valid_shortcodes = $specified_shortcodes;
277
		}
278
279
		//if not merged then let's replace the fields with the localized fields
280
		if ( ! $merged ) {
281
			//let's get all the fields for the set messenger so that we can get the localized label and use that in the returned array.
282
			$field_settings = $messenger->get_template_fields();
283
			$localized = array();
284
			foreach ( $valid_shortcodes as $field => $shortcodes ) {
285
				//get localized field label
286
				if ( isset( $field_settings[ $field ] ) ) {
287
					//possible that this is used as a main field.
288
					if ( empty( $field_settings[ $field ] ) ) {
289
						if ( isset( $field_settings['extra'][ $field ] ) ) {
290
							$_field = $field_settings['extra'][ $field ]['main']['label'];
291
						} else {
292
							$_field = $field;
293
						}
294
					} else {
295
						$_field = $field_settings[ $field ]['label'];
296
					}
297
				} else if ( isset( $field_settings['extra'] ) ) {
298
					//loop through extra "main fields" and see if any of their children have our field
299
					foreach ( $field_settings['extra'] as $main_field => $fields ) {
300
						if ( isset( $fields[ $field ] ) ) {
301
							$_field = $fields[ $field ]['label'];
302
						} else {
303
							$_field = $field;
304
						}
305
					}
306
				} else {
307
					$_field = $field;
308
				}
309
				if ( isset( $_field ) ) {
310
					$localized[ $_field ] = $shortcodes;
311
				}
312
			}
313
			$valid_shortcodes = $localized;
314
		}
315
316
		//if $merged then let's merge all the shortcodes into one list NOT indexed by field.
317
		if ( $merged ) {
318
			$merged_codes = array();
319
			foreach ( $valid_shortcodes as $field => $shortcode ) {
320
				foreach ( $shortcode as $code => $label ) {
321
					if ( isset( $merged_codes[ $code ] ) ) {
322
						continue;
323
					} else {
324
						$merged_codes[ $code ] = $label;
325
					}
326
				}
327
			}
328
			$valid_shortcodes = $merged_codes;
329
		}
330
331
		return $valid_shortcodes;
332
	}
333
334
335
	/**
336
	 * Get Messenger object.
337
	 *
338
	 * @since 4.3.0
339
	 * @deprecated 4.9.0
340
	 * @param string $messenger messenger slug for the messenger object we want to retrieve.
341
	 * @throws \EE_Error
342
	 * @return EE_messenger
343
	 */
344
	public static function messenger_obj( $messenger ) {
345
		/** @type EE_Message_Resource_Manager $Message_Resource_Manager */
346
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
347
		return $Message_Resource_Manager->get_messenger( $messenger );
348
	}
349
350
351
	/**
352
	 * get Message type object
353
	 *
354
	 * @since 4.3.0
355
	 * @deprecated 4.9.0
356
	 * @param string $message_type the slug for the message type object to retrieve
357
	 * @throws \EE_Error
358
	 * @return EE_message_type
359
	 */
360
	public static function message_type_obj( $message_type ) {
361
		/** @type EE_Message_Resource_Manager $Message_Resource_Manager */
362
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
363
		return $Message_Resource_Manager->get_message_type( $message_type );
364
	}
365
366
367
368
369
370
	/**
371
	 * Given a message_type slug, will return whether that message type is active in the system or not.
372
	 *
373
	 * @since    4.3.0
374
	 * @param  string   $message_type message type to check for.
375
	 * @return boolean
376
	 */
377
	public static function is_mt_active( $message_type ) {
378
		/** @type EE_Message_Resource_Manager $Message_Resource_Manager */
379
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
380
		$active_mts = $Message_Resource_Manager->list_of_active_message_types();
381
		return in_array( $message_type, $active_mts );
382
	}
383
384
385
386
	/**
387
	 * Given a messenger slug, will return whether that messenger is active in the system or not.
388
	 *
389
	 * @since    4.3.0
390
	 *
391
	 * @param  string  $messenger slug for messenger to check.
392
	 * @return boolean
393
	 */
394
	public static function is_messenger_active( $messenger ) {
395
		/** @type EE_Message_Resource_Manager $Message_Resource_Manager */
396
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
397
		$active_messenger = $Message_Resource_Manager->get_active_messenger( $messenger );
398
		return $active_messenger instanceof EE_messenger ? true : false;
399
	}
400
401
402
403
	/**
404
	 * Used to return active messengers array stored in the wp options table.
405
	 * If no value is present in the option then an empty array is returned.
406
	 *
407
	 * @deprecated 4.9
408
	 * @since 4.3.1
409
	 *
410
	 * @return array
411
	 */
412 View Code Duplication
	public static function get_active_messengers_in_db() {
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...
413
		EE_Error::doing_it_wrong(
414
			__METHOD__,
415
			__( 'Please use EE_Message_Resource_Manager::get_active_messengers_option() instead.', 'event_espresso' ),
416
			'4.9.0'
417
		);
418
		/** @var EE_Message_Resource_Manager $Message_Resource_Manager */
419
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
420
		return $Message_Resource_Manager->get_active_messengers_option();
421
	}
422
423
424
425
426
	/**
427
	 * Used to update the active messengers array stored in the wp options table.
428
	 *
429
	 * @since 4.3.1
430
	 * @deprecated 4.9.0
431
	 *
432
	 * @param array $data_to_save Incoming data to save.
433
	 *
434
	 * @return bool FALSE if not updated, TRUE if updated.
435
	 */
436 View Code Duplication
	public static function update_active_messengers_in_db( $data_to_save ) {
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...
437
		EE_Error::doing_it_wrong(
438
			__METHOD__,
439
			__( 'Please use EE_Message_Resource_Manager::update_active_messengers_option() instead.', 'event_espresso' ),
440
			'4.9.0'
441
		);
442
		/** @var EE_Message_Resource_Manager $Message_Resource_Manager */
443
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
444
		return $Message_Resource_Manager->update_active_messengers_option( $data_to_save );
445
	}
446
447
448
	/**
449
	 * This does some validation of incoming params, determines what type of url is being prepped and returns the
450
	 * appropriate url trigger
451
	 *
452
	 * @param EE_message_type $message_type
453
	 * @param EE_Message $message
454
	 * @param EE_Registration | null $registration  The registration object must be included if this
455
	 *                                              is going to be a registration trigger url.
456
	 * @param string $sending_messenger             The (optional) sending messenger for the url.
457
	 *
458
	 * @return string
459
	 * @throws EE_Error
460
	 */
461
	public static function get_url_trigger(
462
		EE_message_type $message_type,
463
		EE_Message $message,
464
		$registration = null,
465
		$sending_messenger = ''
466
	) {
467
		//first determine if the url can be to the EE_Message object.
468
		if ( ! $message_type->always_generate() ) {
469
			return EEH_MSG_Template::generate_browser_trigger( $message );
470
		}
471
472
		//if $registration object is not valid then exit early because there's nothing that can be generated.
473
		if ( ! $registration instanceof EE_Registration ) {
474
			throw new EE_Error(
475
				__( 'Incoming value for registration is not a valid EE_Registration object.', 'event_espresso' )
476
			);
477
		}
478
479
		//validate given context
480
		$contexts = $message_type->get_contexts();
481
		if ( $message->context() !== '' && ! isset( $contexts[$message->context()] ) ) {
482
			throw new EE_Error(
483
				sprintf(
484
					__( 'The context %s is not a valid context for %s.', 'event_espresso' ),
485
					$message->context(),
486
					get_class( $message_type )
487
				)
488
			);
489
		}
490
491
		//valid sending messenger but only if sending messenger set.  Otherwise generating messenger is used.
492
		if ( ! empty( $sending_messenger ) ) {
493
			$with_messengers = $message_type->with_messengers();
494
			if ( ! isset( $with_messengers[$message->messenger()] )
495
			     || ! in_array( $sending_messenger, $with_messengers[$message->messenger()] ) ) {
496
				throw new EE_Error(
497
					sprintf(
498
						__(
499
							'The given sending messenger string (%1$s) does not match a valid sending messenger with the %2$s.  If this is incorrect, make sure that the message type has defined this messenger as a sending messenger in its $_with_messengers array.',
500
							'event_espresso'
501
						),
502
						$sending_messenger,
503
						get_class( $message_type )
504
					)
505
				);
506
			}
507
		} else {
508
			$sending_messenger = $message->messenger();
509
		}
510
		return EEH_MSG_Template::generate_url_trigger(
511
			$sending_messenger,
512
			$message->messenger(),
513
			$message->context(),
514
			$message->message_type(),
515
			$registration,
516
			$message->GRP_ID()
517
		);
518
	}
519
520
521
	/**
522
	 * This returns the url for triggering a in browser view of a specific EE_Message object.
523
	 * @param EE_Message $message
524
	 * @return string.
0 ignored issues
show
Documentation introduced by
The doc-type string. could not be parsed: Unknown type name "string." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
525
	 */
526 View Code Duplication
	public static function generate_browser_trigger( EE_Message $message ) {
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...
527
		$query_args = array(
528
			'ee' => 'msg_browser_trigger',
529
			'token' => $message->MSG_token()
530
		);
531
		return apply_filters(
532
			'FHEE__EEH_MSG_Template__generate_browser_trigger',
533
			add_query_arg( $query_args, site_url() ),
534
			$message
535
		);
536
	}
537
538
539
540
541
542
543
	/**
544
	 * This returns the url for triggering an in browser view of the error saved on the incoming message object.
545
	 * @param EE_Message $message
546
	 * @return string
547
	 */
548 View Code Duplication
	public static function generate_error_display_trigger( EE_Message $message ) {
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...
549
		return apply_filters(
550
			'FHEE__EEH_MSG_Template__generate_error_display_trigger',
551
			add_query_arg(
552
				array(
553
					'ee' => 'msg_browser_error_trigger',
554
					'token' => $message->MSG_token()
555
				),
556
				site_url()
557
			),
558
			$message
559
		);
560
	}
561
562
563
564
565
566
567
	/**
568
	 * This generates a url trigger for the msg_url_trigger route using the given arguments
569
	 *
570
	 * @param string          $sending_messenger    The sending messenger slug.
571
	 * @param string          $generating_messenger The generating messenger slug.
572
	 * @param string          $context              The context for the template.
573
	 * @param string          $message_type         The message type slug
574
	 * @param EE_Registration $registration
575
	 * @param integer          $message_template_group id 	The EE_Message_Template_Group ID for the template.
576
	 * @param integer          $data_id 	The id to the EE_Base_Class for getting the data used by the trigger.
577
	 * @return string          The generated url.
578
	 */
579
	public static function generate_url_trigger(
580
		$sending_messenger,
581
		$generating_messenger,
582
		$context,
583
		$message_type,
584
		EE_Registration $registration,
585
		$message_template_group,
586
		$data_id = 0
587
	) {
588
		$query_args = array(
589
			'ee' => 'msg_url_trigger',
590
			'snd_msgr' => $sending_messenger,
591
			'gen_msgr' => $generating_messenger,
592
			'message_type' => $message_type,
593
			'context' => $context,
594
			'token' => $registration->reg_url_link(),
595
			'GRP_ID' => $message_template_group,
596
			'id' => $data_id
597
			);
598
		$url = add_query_arg( $query_args, get_site_url() );
599
600
		//made it here so now we can just get the url and filter it.  Filtered globally and by message type.
601
		$url = apply_filters(
602
			'FHEE__EEH_MSG_Template__generate_url_trigger',
603
			$url,
604
			$sending_messenger,
605
			$generating_messenger,
606
			$context,
607
			$message_type,
608
			$registration,
609
			$message_template_group,
610
			$data_id
611
		);
612
		return $url;
613
	}
614
615
616
617
618
	/**
619
	 * Return the specific css for the action icon given.
620
	 *
621
	 * @since 4.9.0
622
	 *
623
	 * @param string $type  What action to return.
624
	 * @return string
625
	 */
626
	public static function get_message_action_icon( $type ) {
627
		$action_icons = self::get_message_action_icons();
628
		return isset( $action_icons[ $type ] ) ? $action_icons[ $type ] : '';
629
	}
630
631
632
	/**
633
	 * This is used for retrieving the css classes used for the icons representing message actions.
634
	 *
635
	 * @since 4.9.0
636
	 *
637
	 * @return array
638
	 */
639
	public static function get_message_action_icons() {
640
		return apply_filters( 'FHEE__EEH_MSG_Template__message_action_icons',
641
			array(
642
				'view' => array(
643
					'label' => __( 'View Message', 'event_espresso' ),
644
					'css_class' => 'dashicons dashicons-welcome-view-site',
645
				),
646
				'error' => array(
647
					'label' => __( 'View Error Message', 'event_espresso' ),
648
					'css_class' => 'dashicons dashicons-info',
649
				),
650
				'see_notifications_for' => array(
651
					'label' => __( 'View Related Messages', 'event_espresso' ),
652
					'css_class' => 'dashicons dashicons-megaphone',
653
				),
654
				'generate_now' => array(
655
					'label' => __( 'Generate the message now.', 'event_espresso' ),
656
					'css_class' => 'dashicons dashicons-admin-tools',
657
				),
658
				'send_now' => array(
659
					'label' => __( 'Send Immediately', 'event_espresso' ),
660
					'css_class' => 'dashicons dashicons-controls-forward',
661
				),
662
				'queue_for_resending' => array(
663
					'label' => __( 'Queue for Resending', 'event_espresso' ),
664
					'css_class' => 'dashicons dashicons-controls-repeat',
665
				),
666
				'view_transaction' => array(
667
					'label' => __( 'View related Transaction', 'event_espresso' ),
668
					'css_class' => 'dashicons dashicons-cart',
669
				)
670
			)
671
		);
672
	}
673
674
675
	/**
676
	 * This returns the url for a given action related to EE_Message.
677
	 *
678
	 * @since 4.9.0
679
	 *
680
	 * @param string $type  What type of action to return the url for.
681
	 * @param EE_Message $message   Required for generating the correct url for some types.
682
	 * @param array  $query_params   Any additional query params to be included with the generated url.
683
	 *
684
	 * @return string
685
	 */
686
	public static function get_message_action_url( $type, EE_Message $message = null, $query_params = array() ) {
687
		$action_urls = self::get_message_action_urls( $message, $query_params );
688
		return isset( $action_urls[ $type ] )  ? $action_urls[ $type ] : '';
689
	}
690
691
692
693
	/**
694
	 * This returns all the current urls for EE_Message actions.
695
	 *
696
	 * @since 4.9.0
697
	 *
698
	 * @param  EE_Message   $message    The EE_Message object required to generate correct urls for some types.
699
	 * @param  array    $query_params   Any additional query_params to be included with the generated url.
700
	 *
701
	 * @return array
702
	 */
703
	public static function get_message_action_urls( EE_Message $message = null, $query_params = array() ) {
704
		EE_Registry::instance()->load_helper( 'URL' );
705
		//if $message is not an instance of EE_Message then let's just do a dummy.
706
		$message = empty( $message ) ? EE_Message_Factory::create() : $message;
707
		$action_urls =  apply_filters(
708
			'FHEE__EEH_MSG_Template__get_message_action_url',
709
			array(
710
				'view' => EEH_MSG_Template::generate_browser_trigger( $message ),
711
				'error' => EEH_MSG_Template::generate_error_display_trigger( $message ),
712
				'see_notifications_for' => EEH_URL::add_query_args_and_nonce(
713
					array_merge(
714
						array(
715
							'page' => 'espresso_messages',
716
							'action' => 'default',
717
							'filterby' => 1,
718
						),
719
						$query_params
720
					),
721
					admin_url( 'admin.php' )
722
				),
723
				'generate_now' => EEH_URL::add_query_args_and_nonce(
724
					array(
725
						'page' => 'espresso_messages',
726
						'action' => 'generate_now',
727
						'MSG_ID' => $message->ID()
728
					),
729
					admin_url( 'admin.php' )
730
				),
731
				'send_now' => EEH_URL::add_query_args_and_nonce(
732
					array(
733
						'page' => 'espresso_messages',
734
						'action' => 'send_now',
735
						'MSG_ID' => $message->ID()
736
					),
737
					admin_url( 'admin.php' )
738
				),
739
				'queue_for_resending' => EEH_URL::add_query_args_and_nonce(
740
					array(
741
						'page' => 'espresso_messages',
742
						'action' => 'queue_for_resending',
743
						'MSG_ID' => $message->ID()
744
					),
745
					admin_url( 'admin.php' )
746
				),
747
			)
748
		);
749
		if (
750
			$message->TXN_ID() > 0
751
			&& EE_Registry::instance()->CAP->current_user_can(
752
				'ee_read_transaction',
753
				'espresso_transactions_default',
754
				$message->TXN_ID()
755
			)
756
		) {
757
			$action_urls['view_transaction'] = EEH_URL::add_query_args_and_nonce(
758
				array(
759
					'page' => 'espresso_transactions',
760
					'action' => 'view_transaction',
761
					'TXN_ID' => $message->TXN_ID()
762
				),
763
				admin_url( 'admin.php' )
764
			);
765
		} else {
766
			$action_urls['view_transaction'] = '';
767
		}
768
		return $action_urls;
769
	}
770
771
772
	/**
773
	 * This returns a generated link html including the icon used for the action link for EE_Message actions.
774
	 *
775
	 * @since 4.9.0
776
	 *
777
	 * @param string $type What type of action the link is for (if invalid type is passed in then an
778
	 *                     empty string is returned)
779
	 * @param EE_Message|null $message  The EE_Message object (required for some actions to generate correctly)
780
	 * @param array           $query_params Any extra query params to include in the generated link.
781
	 *
782
	 * @return string
783
	 */
784
	public static function get_message_action_link( $type, EE_Message $message = null, $query_params = array() ) {
785
		$url = EEH_MSG_Template::get_message_action_url( $type, $message, $query_params );
786
		$icon_css = EEH_MSG_Template::get_message_action_icon( $type );
787
		$title = isset( $icon_css['label'] ) ? 'title="' . $icon_css['label'] . '"' : '';
788
789
		if ( empty( $url ) || empty( $icon_css ) || ! isset( $icon_css['css_class'] ) ) {
790
			return '';
791
		}
792
793
		$icon_css['css_class'] .= esc_attr(
794
			apply_filters(
795
				'FHEE__EEH_MSG_Template__get_message_action_link__icon_css_class',
796
				' js-ee-message-action-link ee-message-action-link-' . $type,
797
				$type,
798
				$message,
799
				$query_params
800
			)
801
		);
802
803
		return '<a href="' . $url . '"' . $title . '><span class="' . esc_attr( $icon_css['css_class'] ) . '"></span></a>';
804
805
	}
806
807
808
809
810
811
	/**
812
	 * This returns an array with keys as reg statuses and values as the corresponding message type slug (filtered).
813
	 *
814
	 * @since 4.9.0
815
	 * @return array
816
	 */
817
	public static function reg_status_to_message_type_array() {
818
		return (array) apply_filters(
819
			'FHEE__EEH_MSG_Template__reg_status_to_message_type_array',
820
			array(
821
				EEM_Registration::status_id_approved => 'registration',
822
				EEM_Registration::status_id_pending_payment => 'pending_approval',
823
				EEM_Registration::status_id_not_approved => 'not_approved_registration',
824
				EEM_Registration::status_id_cancelled => 'cancelled_registration',
825
				EEM_Registration::status_id_declined => 'declined_registration'
826
			)
827
		);
828
	}
829
830
831
832
833
	/**
834
	 * This returns the corresponding registration message type slug to the given reg status. If there isn't a
835
	 * match, then returns an empty string.
836
	 *
837
	 * @since 4.9.0
838
	 * @param $reg_status
839
	 * @return string
840
	 */
841
	public static function convert_reg_status_to_message_type( $reg_status ) {
842
		$reg_status_array = self::reg_status_to_message_type_array();
843
		return isset( $reg_status_array[$reg_status] ) ? $reg_status_array[$reg_status] : '';
844
	}
845
846
847
	/**
848
	 * This returns an array with keys as payment stati and values as the corresponding message type slug (filtered).
849
	 *
850
	 * @since 4.9.0
851
	 * @return array
852
	 */
853
	public static function payment_status_to_message_type_array() {
854
		return (array) apply_filters(
855
			'FHEE__EEH_MSG_Template__payment_status_to_message_type_array',
856
			array(
857
				EEM_Payment::status_id_approved => 'payment',
858
				EEM_Payment::status_id_pending => 'payment_pending',
859
				EEM_Payment::status_id_cancelled => 'payment_cancelled',
860
				EEM_Payment::status_id_declined => 'payment_declined',
861
				EEM_Payment::status_id_failed => 'payment_failed'
862
			)
863
		);
864
	}
865
866
867
868
869
	/**
870
	 * This returns the corresponding payment message type slug to the given payment status. If there isn't a match then
871
	 * an empty string is returned
872
	 *
873
	 * @since 4.9.0
874
	 * @param $payment_status
875
	 * @return string
876
	 */
877
	public static function convert_payment_status_to_message_type( $payment_status ) {
878
		$payment_status_array = self::payment_status_to_message_type_array();
879
		return isset( $payment_status_array[$payment_status] ) ? $payment_status_array[$payment_status] : '';
880
	}
881
882
883
	/**
884
	 * This is used to retrieve the template pack for the given name.
885
	 *
886
	 * @param string $template_pack_name  should match the set `dbref` property value on the EE_Messages_Template_Pack.
887
	 *
888
	 * @return EE_Messages_Template_Pack
889
	 */
890
	public static function get_template_pack( $template_pack_name ) {
891
		if ( ! self::$_template_pack_collection instanceof EE_Object_Collection ) {
892
			self::$_template_pack_collection = new EE_Messages_Template_Pack_Collection();
893
		}
894
895
		//first see if in collection already
896
		$template_pack = self::$_template_pack_collection->get_by_name( $template_pack_name );
897
898
		if ( $template_pack instanceof EE_Messages_Template_Pack ) {
899
			return $template_pack;
900
		}
901
902
		//nope...let's get it.
903
		//not set yet so let's attempt to get it.
904
		$pack_class_name = 'EE_Messages_Template_Pack_' . str_replace(
905
				' ',
906
				'_',
907
				ucwords(
908
					str_replace( '_', ' ', $template_pack_name )
909
				)
910
			);
911
		if ( ! class_exists( $pack_class_name ) && $template_pack_name !== 'default' ) {
912
			return self::get_template_pack( 'default' );
913
		} else {
914
			$template_pack = new $pack_class_name;
915
			self::$_template_pack_collection->add( $template_pack );
916
			return $template_pack;
917
		}
918
	}
919
920
921
922
923
	/**
924
	 * Globs template packs installed in core and returns the template pack collection with all installed template packs
925
	 * in it.
926
	 *
927
	 * @since 4.9.0
928
	 *
929
	 * @return EE_Messages_Template_Pack_Collection
930
	 */
931
	public static function get_template_pack_collection() {
932
		$new_collection = false;
933
		if ( ! self::$_template_pack_collection instanceof EE_Messages_Template_Pack_Collection ) {
934
			self::$_template_pack_collection = new EE_Messages_Template_Pack_Collection();
935
			$new_collection = true;
936
		}
937
938
		//glob the defaults directory for messages
939
		$templates = glob( EE_LIBRARIES . 'messages/defaults/*', GLOB_ONLYDIR );
940
		foreach( $templates as $template_path ) {
941
			//grab folder name
942
			$template = basename( $template_path );
943
944
			if ( ! $new_collection ) {
945
				//already have it?
946
				if ( self::$_template_pack_collection->get_by_name( $template ) instanceof EE_Messages_Template_Pack ) {
947
					continue;
948
				}
949
			}
950
951
			//setup classname.
952
			$template_pack_class_name = 'EE_Messages_Template_Pack_' . str_replace(
953
					' ',
954
					'_',
955
					ucwords(
956
						str_replace(
957
							'_',
958
							' ',
959
							$template
960
						)
961
					)
962
				);
963
			if ( ! class_exists( $template_pack_class_name ) ) {
964
				continue;
965
			}
966
			self::$_template_pack_collection->add( new $template_pack_class_name );
967
		}
968
969
		/**
970
		 * Filter for plugins to add in any additional template packs
971
		 * Note the filter name here is for backward compat, this used to be found in EED_Messages.
972
		 */
973
		$additional_template_packs = apply_filters( 'FHEE__EED_Messages__get_template_packs__template_packs', array() );
974
		foreach ( (array) $additional_template_packs as $template_pack ) {
975
			if ( ! self::$_template_pack_collection->contains($template_pack ) ) {
976
				self::$_template_pack_collection->add( $template_pack );
977
			}
978
		}
979
		return self::$_template_pack_collection;
980
	}
981
982
983
984
	/**
985
	 * This is a wrapper for the protected _create_new_templates function
986
	 *
987
	 * @param string $messenger_name
988
	 * @param string $message_type_name message type that the templates are being created for
989
	 * @param int    $GRP_ID
990
	 * @param bool   $global
991
	 * @return array
992
	 * @throws \EE_Error
993
	 */
994
	public static function create_new_templates( $messenger_name, $message_type_name, $GRP_ID = 0, $global = false ) {
995
		/** @type EE_Message_Resource_Manager $Message_Resource_Manager */
996
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
997
		$messenger = $Message_Resource_Manager->valid_messenger( $messenger_name );
998
		$message_type = $Message_Resource_Manager->valid_message_type( $message_type_name );
999
		if ( ! EEH_MSG_Template::message_type_has_active_templates_for_messenger( $messenger, $message_type, $global ) ) {
1000
			return array();
1001
		}
1002
		//whew made it this far!  Okay, let's go ahead and create the templates then
1003
		return EEH_MSG_Template::_create_new_templates( $messenger, $message_type, $GRP_ID, $global );
1004
	}
1005
1006
1007
1008
	/**
1009
	 * @param \EE_messenger    $messenger
1010
	 * @param \EE_message_type $message_type
1011
	 * @param                  $GRP_ID
1012
	 * @param                  $global
1013
	 * @return array|mixed
1014
	 */
1015
	protected static function _create_new_templates( EE_messenger $messenger, EE_message_type $message_type, $GRP_ID, $global ) {
1016
		//if we're creating a custom template then we don't need to use the defaults class
1017
		if ( ! $global ) {
1018
			return EEH_MSG_Template::_create_custom_template_group( $messenger, $message_type, $GRP_ID );
1019
		}
1020
		/** @type EE_Messages_Template_Defaults $Message_Template_Defaults */
1021
		$Message_Template_Defaults = EE_Registry::factory(
1022
			'EE_Messages_Template_Defaults',
1023
			array( $messenger, $message_type, $GRP_ID )
1024
		);
1025
		//generate templates
1026
		$success = $Message_Template_Defaults->create_new_templates();
1027
1028
		//if creating the template failed.  Then we should deactivate the related message_type for the messenger because
1029
		//its not active if it doesn't have a template.  Note this is only happening for GLOBAL template creation
1030
		//attempts.
1031
		if ( ! $success ) {
1032
			/** @var EE_Message_Resource_Manager $message_resource_manager */
1033
			$message_resource_manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
1034
			$message_resource_manager->deactivate_message_type_for_messenger( $message_type->name, $messenger->name );
1035
		}
1036
1037
		/**
1038
		 * $success is in an array in the following format
1039
		 * array(
1040
		 *    'GRP_ID' => $new_grp_id,
1041
		 *    'MTP_context' => $first_context_in_new_templates,
1042
		 * )
1043
		 */
1044
		return $success;
1045
	}
1046
1047
1048
1049
	/**
1050
	 * This creates a custom template using the incoming GRP_ID
1051
	 *
1052
	 * @param \EE_messenger    $messenger
1053
	 * @param \EE_message_type $message_type
1054
	 * @param  int             $GRP_ID 		GRP_ID for the template_group being used as the base
1055
	 * @return  array $success 				This will be an array in the format:
1056
	 *                                      array(
1057
	 * 											'GRP_ID' => $new_grp_id,
1058
	 * 											'MTP_context' => $first_context_in_created_template
1059
	 * 										)
1060
	 * @access private
1061
	 */
1062
	private static function _create_custom_template_group( EE_messenger $messenger, EE_message_type $message_type, $GRP_ID ) {
1063
		//defaults
1064
		$success = array( 'GRP_ID' => null, 'MTP_context' => '' );
1065
		//get the template group to use as a template from the db.  If $GRP_ID is empty then we'll assume the base will be the global template matching the messenger and message type.
1066
		$Message_Template_Group = empty( $GRP_ID )
1067
			? EEM_Message_Template_Group::instance()->get_one(
1068
				array(
1069
					array(
1070
						'MTP_messenger'    => $messenger->name,
1071
						'MTP_message_type' => $message_type->name,
1072
						'MTP_is_global'    => true
1073
					)
1074
				)
1075
			)
1076
			: EEM_Message_Template_Group::instance()->get_one_by_ID( $GRP_ID );
1077
		//if we don't have a mtg at this point then we need to bail.
1078
		if ( ! $Message_Template_Group instanceof EE_Message_Template_Group ) {
1079
			EE_Error::add_error(
1080
				sprintf(
1081
					__(
1082
						'Something went wrong with generating the custom template from this group id: %s.  This usually happens when there is no matching message template group in the db.',
1083
						'event_espresso'
1084
					),
1085
					$GRP_ID
1086
				),
1087
				__FILE__,
1088
				__FUNCTION__,
1089
				__LINE__
1090
			);
1091
			return $success;
1092
		}
1093
		//let's get all the related message_template objects for this group.
1094
		$message_templates = $Message_Template_Group->message_templates();
1095
		//now we have what we need to setup the new template
1096
		$new_mtg = clone $Message_Template_Group;
1097
		$new_mtg->set( 'GRP_ID', 0 );
1098
		$new_mtg->set( 'MTP_is_global', false );
1099
		$template_name = defined( 'DOING_AJAX' ) && ! empty( $_POST[ 'templateName' ] )
1100
			? $_POST[ 'templateName' ]
1101
			: __(
1102
				'New Custom Template',
1103
				'event_espresso'
1104
			);
1105
		$template_description = defined( "DOING_AJAX" ) && ! empty( $_POST[ 'templateDescription' ] )
1106
			? $_POST[ 'templateDescription' ]
1107
			: sprintf(
1108
				__(
1109
					'This is a custom template that was created for the %s messenger and %s message type.',
1110
					'event_espresso'
1111
				),
1112
				$new_mtg->messenger_obj()->label[ 'singular' ],
1113
				$new_mtg->message_type_obj()->label[ 'singular' ]
1114
			);
1115
		$new_mtg->set( 'MTP_name', $template_name );
1116
		$new_mtg->set( 'MTP_description', $template_description );
1117
		//remove ALL relations on this template group so they don't get saved!
1118
		$new_mtg->_remove_relations( 'Message_Template' );
1119
		$new_mtg->save();
1120
		$success[ 'GRP_ID' ] = $new_mtg->ID();
1121
		$success[ 'template_name' ] = $template_name;
1122
		//add new message templates and add relation to.
1123
		foreach ( $message_templates as $message_template ) {
1124
			if ( ! $message_template instanceof EE_Message_Template ) {
1125
				continue;
1126
			}
1127
			$new_message_template = clone $message_template;
1128
			$new_message_template->set( 'MTP_ID', 0 );
1129
			$new_message_template->set( 'GRP_ID', $new_mtg->ID() ); //relation
1130
			$new_message_template->save();
1131
			if ( empty( $success[ 'MTP_context' ] ) ) {
1132
				$success[ 'MTP_context' ] = $new_message_template->get( 'MTP_context' );
1133
			}
1134
		}
1135
		return $success;
1136
	}
1137
1138
1139
1140
	/**
1141
	 * message_type_has_active_templates_for_messenger
1142
	 *
1143
	 * @param \EE_messenger    $messenger
1144
	 * @param \EE_message_type $message_type
1145
	 * @param bool             $global
1146
	 * @return bool
1147
	 */
1148
	public static function message_type_has_active_templates_for_messenger(
1149
		EE_messenger $messenger,
1150
		EE_message_type $message_type,
1151
		$global = false
1152
	) {
1153
		//is given message_type valid for given messenger (if this is not a global save)
1154
		if ( $global ) {
1155
			return true;
1156
		}
1157
		$active_templates = EEM_Message_Template_Group::instance()->count(
1158
			array(
1159
				array(
1160
					'MTP_is_active'    => true,
1161
					'MTP_messenger'    => $messenger->name,
1162
					'MTP_message_type' => $message_type->name
1163
				)
1164
			)
1165
		);
1166
		if ( $active_templates > 0 ) {
1167
			return true;
1168
		}
1169
		EE_Error::add_error(
1170
			sprintf(
1171
				__(
1172
					'The %1$s message type is not registered with the %2$s messenger. Please visit the Messenger activation page to assign this message type first if you want to use it.',
1173
					'event_espresso'
1174
				),
1175
				$message_type->name,
1176
				$messenger->name
1177
			),
1178
			__FILE__,
1179
			__FUNCTION__,
1180
			__LINE__
1181
		);
1182
		return false;
1183
	}
1184
1185
1186
1187
	/**
1188
	 * get_fields
1189
	 * This takes a given messenger and message type and returns all the template fields indexed by context (and with field type).
1190
	 *
1191
	 * @param  string $messenger_name    name of EE_messenger
1192
	 * @param  string $message_type_name name of EE_message_type
1193
	 * @return array
1194
	 */
1195
	public static function get_fields( $messenger_name, $message_type_name ) {
1196
		$template_fields = array();
1197
		/** @type EE_Message_Resource_Manager $Message_Resource_Manager */
1198
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
1199
		$messenger = $Message_Resource_Manager->valid_messenger( $messenger_name );
1200
		$message_type = $Message_Resource_Manager->valid_message_type( $message_type_name );
1201
		if ( ! EEH_MSG_Template::message_type_has_active_templates_for_messenger( $messenger, $message_type ) ) {
1202
			return array();
1203
		}
1204
		//okay now let's assemble an array with the messenger template fields added to the message_type contexts.
1205
		foreach ( $message_type->get_contexts() as $context => $details ) {
1206
			foreach ( $messenger->get_template_fields() as $field => $value ) {
1207
				$template_fields[ $context ][ $field ] = $value;
1208
			}
1209
		}
1210
		if ( empty( $template_fields ) ) {
1211
			EE_Error::add_error(
1212
				__( 'Something went wrong and we couldn\'t get any templates assembled', 'event_espresso' ),
1213
				__FILE__,
1214
				__FUNCTION__,
1215
				__LINE__
1216
			);
1217
			return array();
1218
		}
1219
		return $template_fields;
1220
	}
1221
1222
}
1223