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

EEH_MSG_Template::get_fields()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.439
cc 5
eloc 18
nc 7
nop 2
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|bool array of data required for the redirect to the correct edit page or FALSE if encountering problems.
57
	 */
58
	public static function generate_new_templates($messenger, $message_types, $GRP_ID = 0,  $global = FALSE) {
59
		//make sure message_type is an array.
60
		$message_types = (array) $message_types;
61
		$templates = array();
62
		$success = TRUE;
63
64
		if ( empty($messenger) ) {
65
			throw new EE_Error( __('We need a messenger to generate templates!', 'event_espresso') );
66
		}
67
68
		//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.
69
		if ( empty($message_types) ) {
70
			throw new EE_Error( __('We need at least one message type to generate templates!', 'event_espresso') );
71
		}
72
73
		self::_set_autoloader();
74
		/** @type EE_Messages $messages_controller */
75
		$messages_controller = EE_Registry::instance()->load_lib( 'messages' );
76
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( array( array( 'MTP_messenger' => $messenger, 'MTP_message_type' => $message_type, 'MTP_is_global' => TRUE ) ) );
81
				$GRP_ID = $GRP_ID instanceof EE_Message_Template_Group ? $GRP_ID->ID() : 0;
82
			}
83
			//if this is global template generation. First let's determine if we already HAVE global templates for this messenger and message_type combination.  If we do then NO generation!!
84
			if ( $global && self::already_generated($messenger, $message_type, $GRP_ID  ) ) {
85
				$templates = TRUE;
86
				continue; //get out we've already got generated templates for this.
87
			}
88
89
			$new_message_template_group = $messages_controller->create_new_templates($messenger, $message_type, $GRP_ID, $global);
0 ignored issues
show
Deprecated Code introduced by
The method EE_Messages::create_new_templates() has been deprecated with message: 4.9.0

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

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

Loading history...
90
91
			if ( !$new_message_template_group ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $new_message_template_group 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...
92
				$success = FALSE;
93
				continue;
94
			}
95
			if ( $templates === TRUE ) $templates = array();
96
			$templates[] = $new_message_template_group;
97
		}
98
99
		return ($success) ? $templates : $success;
100
	}
101
102
103
	/**
104
	 * The purpose of this method is to determine if there are already generated templates in the database for the given variables.
105
	 * @param  string $messenger     messenger
106
	 * @param  string $message_type message type
107
	 * @param  int $GRP_ID        GRP ID ( if a custom template) (if not provided then we're just doing global template check)
108
	 * @param  bool  $update_to_active if true then we also toggle the template to active.
109
	 * @return bool                true = generated, false = hasn't been generated.
110
	 */
111
	public static function already_generated( $messenger, $message_type, $GRP_ID = 0, $update_to_active = TRUE ) {
112
		self::_set_autoloader();
113
		$MTP = EEM_Message_Template::instance();
114
115
		//what method we use depends on whether we have an GRP_ID or not
116
		$count = empty( $GRP_ID ) ? EEM_Message_Template::instance()->count( array( array( 'Message_Template_Group.MTP_messenger' => $messenger, 'Message_Template_Group.MTP_message_type' => $message_type, 'Message_Template_Group.MTP_is_global' => TRUE ) ) ) :  $MTP->count( array( array( 'GRP_ID' => $GRP_ID ) ) );
117
118
		if ( $update_to_active ) {
119
			self::update_to_active( $messenger, $message_type );
120
		}
121
122
		return ( $count > 0 ) ? TRUE : FALSE;
123
	}
124
125
126
127
128
	/**
129
	 * Updates all message templates matching the incoming messenger and message type to active status.
130
	 *
131
	 * @param  string $messenger    	Messenger slug
132
	 * @param  string $message_type  Message type slug
133
	 * @static
134
	 * @return  int 						count of updated records.
135
	 */
136
	public static function update_to_active( $messenger, $message_type ) {
137
		return EEM_Message_Template_Group::instance()->update(
138
			array( 'MTP_is_active' => 1 ),
139
			array(
140
				array(
141
					'MTP_messenger' => $messenger, 'MTP_message_type' => $message_type
142
				)
143
			)
144
		);
145
	}
146
147
148
149
	/**
150
	 * Updates all message template groups matching the incoming arguments to inactive status.
151
	 *
152
	 * @param string $messenger 	The messenger slug.
153
	 *                          	If empty then all templates matching the message type are marked inactive.
154
	 *                          	Otherwise only templates matching the messenger and message type.
155
	 * @param string $message_type 	The message type slug.
156
	 *                              If empty then all templates matching the messenger are marked inactive.
157
	 * 								Otherwise only templates matching the messenger and message type.
158
	 *
159
	 * @return int  count of updated records.
160
	 */
161
	public static function update_to_inactive( $messenger = '', $message_type = '' ) {
162
		return EEM_Message_Template_Group::instance()->deactivate_message_template_groups_for(
163
			$messenger,
164
			$message_type
165
		);
166
	}
167
168
169
	/**
170
	 * The purpose of this function is to return all installed message objects
171
	 * (messengers and message type regardless of whether they are ACTIVE or not)
172
	 *
173
	 * @param string $type
174
	 * @return array array consisting of installed messenger objects and installed message type objects.
175
	 */
176
	public static function get_installed_message_objects( $type = 'all' ) {
177
		self::_set_autoloader();
178
		//get all installed messengers and message_types
179
		/** @type EE_Messages $messages_controller */
180
		$messages_controller = EE_Registry::instance()->load_lib( 'messages' );
181
		return $messages_controller->get_installed($type);
0 ignored issues
show
Deprecated Code introduced by
The method EE_Messages::get_installed() has been deprecated with message: 4.9.0

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

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

Loading history...
182
	}
183
184
185
	/**
186
	 * This will return an array of shortcodes => labels from the
187
	 * messenger and message_type objects associated with this
188
	 * template.
189
	 *
190
	 * @since 4.3.0
191
	 *
192
	 * @param string $message_type
193
	 * @param string $messenger
194
	 * @param array  $fields 	What fields we're returning valid shortcodes for.
195
	 *                          If empty then we assume all fields are to be returned. Optional.
196
	 * @param string $context 	What context we're going to return shortcodes for. Optional.
197
	 * @param bool $merged 		If TRUE then we don't return shortcodes indexed by field,
198
	 *                          but instead an array of the unique shortcodes for all the given ( or all) fields.
199
	 *                          Optional.
200
	 * @throws \EE_Error
201
	 * @return mixed (array|bool) an array of shortcodes in the format
202
	 * 												array( '[shortcode] => 'label')
203
	 *												OR
204
	 * 												FALSE if no shortcodes found.
205
	 */
206
	public static function get_shortcodes(
207
		$message_type,
208
		$messenger,
209
		$fields = array(),
210
		$context = 'admin',
211
		$merged = false
212
	) {
213
		$messenger_name = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $messenger ) ) );
214
		$mt_name = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $message_type ) ) );
215
216
		//convert slug to object
217
		$messenger = self::messenger_obj( $messenger );
0 ignored issues
show
Deprecated Code introduced by
The method EEH_MSG_Template::messenger_obj() has been deprecated with message: 4.9.0

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

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

Loading history...
218
219
		//validate class for getting our list of shortcodes
220
		$classname = 'EE_Messages_' . $messenger_name . '_' . $mt_name . '_Validator';
221
		if ( !class_exists( $classname ) ) {
222
			$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...
223
			$msg[] = sprintf(
224
				__('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'),
225
				$classname
226
			);
227
			throw new EE_Error( implode( '||', $msg ) );
228
		}
229
		/** @type EE_Messages_Validator $_VLD */
230
		$_VLD = new $classname( array(), $context );
231
		$valid_shortcodes = $_VLD->get_validators();
232
233
		//let's make sure we're only getting the shortcode part of the validators
234
		$shortcodes = array();
235
		foreach( $valid_shortcodes as $field => $validators ) {
236
			$shortcodes[$field] = $validators['shortcodes'];
237
		}
238
		$valid_shortcodes = $shortcodes;
239
240
		//if not all fields let's make sure we ONLY include the shortcodes for the specified fields.
241
		if ( ! empty( $fields ) ) {
242
			$specified_shortcodes = array();
243
			foreach ( $fields as $field ) {
244
				if ( isset( $valid_shortcodes[$field] ) )
245
					$specified_shortcodes[$field] = $valid_shortcodes[$field];
246
			}
247
			$valid_shortcodes = $specified_shortcodes;
248
		}
249
250
251
		//if not merged then let's replace the fields with the localized fields
252
		if ( ! $merged ) {
253
			//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.
254
			$field_settings = $messenger->get_template_fields();
255
			$localized = array();
256
			foreach ( $valid_shortcodes as $field => $shortcodes ) {
257
				//get localized field label
258
				if ( isset( $field_settings[$field] ) ) {
259
					//possible that this is used as a main field.
260
					if ( empty( $field_settings[$field] ) ) {
261
						if ( isset( $field_settings['extra'][$field] ) ) {
262
							$_field = $field_settings['extra'][$field]['main']['label'];
263
						} else {
264
							$_field = $field;
265
						}
266
					} else {
267
						$_field = $field_settings[$field]['label'];
268
					}
269
				} else if ( isset( $field_settings['extra'] ) ) {
270
					//loop through extra "main fields" and see if any of their children have our field
271
					foreach ( $field_settings['extra'] as $main_field => $fields ) {
272
						if ( isset( $fields[$field] ) )
273
							$_field = $fields[$field]['label'];
274
						else
275
							$_field = $field;
276
					}
277
				} else {
278
					$_field = $field;
279
				}
280
				if ( isset( $_field )) {
281
					$localized[ $_field ] = $shortcodes;
282
				}
283
			}
284
			$valid_shortcodes = $localized;
285
		}
286
287
288
		//if $merged then let's merge all the shortcodes into one list NOT indexed by field.
289
		if ( $merged ) {
290
			$merged_codes = array();
291
			foreach ( $valid_shortcodes as $field => $shortcode ) {
292
				foreach ( $shortcode as $code => $label ) {
293
					if ( isset( $merged_codes[$code] ) )
294
						continue;
295
					else
296
						$merged_codes[$code] = $label;
297
				}
298
			}
299
			$valid_shortcodes = $merged_codes;
300
		}
301
302
		return $valid_shortcodes;
303
	}
304
305
306
	/**
307
	 * Get Messenger object.
308
	 *
309
	 * @since 4.3.0
310
	 * @deprecated 4.9.0
311
	 * @param string $messenger messenger slug for the messenger object we want to retrieve.
312
	 * @throws \EE_Error
313
	 * @return EE_Messenger
314
	 */
315
	public static function messenger_obj( $messenger ) {
316
		/** @type EE_Messages $messages_controller */
317
		$messages_controller = EE_Registry::instance()->load_lib( 'messages' );
318
		$installed_messengers = $messages_controller->get_installed_messengers();
0 ignored issues
show
Deprecated Code introduced by
The method EE_Messages::get_installed_messengers() has been deprecated with message: 4.9.0

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

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

Loading history...
319
		return isset( $installed_messengers[ $messenger ] ) ? $installed_messengers[ $messenger ] : null;
320
	}
321
322
323
	/**
324
	 * get Message type object
325
	 *
326
	 * @since 4.3.0
327
	 * @deprecated 4.9.0
328
	 * @param string $message_type the slug for the message type object to retrieve
329
	 * @throws \EE_Error
330
	 * @return EE_message_type
331
	 */
332
	public static function message_type_obj( $message_type ) {
333
		/** @type EE_Messages $messages_controller */
334
		$messages_controller = EE_Registry::instance()->load_lib( 'messages' );
335
		$installed_message_types = $messages_controller->get_installed_message_types();
0 ignored issues
show
Deprecated Code introduced by
The method EE_Messages::get_installed_message_types() has been deprecated with message: 4.9.0

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

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

Loading history...
336
		return isset( $installed_message_types[ $message_type ] ) ? $installed_message_types[ $message_type ] : null;
337
	}
338
339
340
341
342
343
	/**
344
	 * Given a message_type slug, will return whether that message type is active in the system or not.
345
	 *
346
	 * @since    4.3.0
347
	 * @param  string   $message_type message type to check for.
348
	 * @return boolean
349
	 */
350
	public static function is_mt_active( $message_type ) {
351
		self::_set_autoloader();
352
		/** @type EE_Messages $messages_controller */
353
		$messages_controller = EE_Registry::instance()->load_lib( 'messages' );
354
		$active_mts = $messages_controller->get_active_message_types();
0 ignored issues
show
Deprecated Code introduced by
The method EE_Messages::get_active_message_types() has been deprecated with message: 4.9.0

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

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

Loading history...
355
		return in_array( $message_type, $active_mts );
356
	}
357
358
359
360
	/**
361
	 * Given a messenger slug, will return whether that messenger is active in the system or not.
362
	 *
363
	 * @since    4.3.0
364
	 *
365
	 * @param  string  $messenger slug for messenger to check.
366
	 * @return boolean
367
	 */
368
	public static function is_messenger_active( $messenger ) {
369
		self::_set_autoloader();
370
		/** @type EE_Messages $messages_controller */
371
		$messages_controller = EE_Registry::instance()->load_lib( 'messages' );
372
		$active_messengers = $messages_controller->get_active_messengers();
0 ignored issues
show
Deprecated Code introduced by
The method EE_Messages::get_active_messengers() has been deprecated with message: 4.9.0

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

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

Loading history...
373
		return isset( $active_messengers[ $messenger ] );
374
	}
375
376
377
378
	/**
379
	 * Used to return active messengers array stored in the wp options table.
380
	 * If no value is present in the option then an empty array is returned.
381
	 *
382
	 * @since 4.3.1
383
	 *
384
	 * @return array
385
	 */
386
	public static function get_active_messengers_in_db() {
387
		return apply_filters(
388
			'FHEE__EEH_MSG_Template__get_active_messengers_in_db',
389
			get_option( 'ee_active_messengers', array() )
390
		);
391
	}
392
393
394
395
396
	/**
397
	 * Used to update the active messengers array stored in the wp options table.
398
	 *
399
	 * @since 4.3.1
400
	 *
401
	 * @param array $data_to_save Incoming data to save.
402
	 *
403
	 * @return bool FALSE if not updated, TRUE if updated.
404
	 */
405
	public static function update_active_messengers_in_db( $data_to_save ) {
406
		return update_option( 'ee_active_messengers', $data_to_save );
407
	}
408
409
410
	/**
411
	 * This does some validation of incoming params, determines what type of url is being prepped and returns the
412
	 * appropriate url trigger
413
	 *
414
	 * @param EE_message_type $message_type
415
	 * @param EE_Message $message
416
	 * @param EE_Registration | null $registration  The registration object must be included if this
417
	 *                                              is going to be a registration trigger url.
418
	 * @param string $sending_messenger             The (optional) sending messenger for the url.
419
	 *
420
	 * @return string
421
	 * @throws EE_Error
422
	 */
423
	public static function get_url_trigger(
424
		EE_message_type $message_type,
425
		EE_Message $message,
426
		$registration = null,
427
		$sending_messenger = ''
428
	) {
429
		//first determine if the url can be to the EE_Message object.
430
		if ( ! $message_type->always_generate() ) {
431
			return EEH_MSG_Template::generate_browser_trigger( $message );
432
		}
433
434
		//if $registration object is not valid then exit early because there's nothing that can be generated.
435
		if ( ! $registration instanceof EE_Registration ) {
436
			throw new EE_Error(
437
				__( 'Incoming value for registration is not a valid EE_Registration object.', 'event_espresso' )
438
			);
439
		}
440
441
		//validate given context
442
		$contexts = $message_type->get_contexts();
443
		if ( $message->context() !== '' && ! isset( $contexts[$message->context()] ) ) {
444
			throw new EE_Error(
445
				sprintf(
446
					__( 'The context %s is not a valid context for %s.', 'event_espresso' ),
447
					$message->context(),
448
					get_class( $message_type )
449
				)
450
			);
451
		}
452
453
		//valid sending messenger but only if sending messenger set.  Otherwise generating messenger is used.
454
		if ( ! empty( $sending_messenger ) ) {
455
			$with_messengers = $message_type->with_messengers();
456
			if ( ! isset( $with_messengers[$message->messenger()] )
457
			     || ! in_array( $sending_messenger, $with_messengers[$message->messenger()] ) ) {
458
				throw new EE_Error(
459
					sprintf(
460
						__(
461
							'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.',
462
							'event_espresso'
463
						),
464
						$sending_messenger,
465
						get_class( $message_type )
466
					)
467
				);
468
			}
469
		} else {
470
			$sending_messenger = $message->messenger();
471
		}
472
		return EEH_MSG_Template::generate_url_trigger(
473
			$sending_messenger,
0 ignored issues
show
Bug introduced by
It seems like $sending_messenger defined by $message->messenger() on line 470 can also be of type boolean; however, EEH_MSG_Template::generate_url_trigger() does only seem to accept string, 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...
474
			$message->messenger(),
0 ignored issues
show
Documentation introduced by
$message->messenger() is of type boolean, but the function expects a string.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
475
			$message->context(),
0 ignored issues
show
Documentation introduced by
$message->context() is of type boolean, but the function expects a string.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
476
			$message->message_type(),
0 ignored issues
show
Documentation introduced by
$message->message_type() is of type boolean, but the function expects a string.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
477
			$registration,
478
			$message->GRP_ID()
0 ignored issues
show
Documentation introduced by
$message->GRP_ID() is of type boolean, but the function expects a integer.

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...
479
		);
480
	}
481
482
483
	/**
484
	 * This returns the url for triggering a in browser view of a specific EE_Message object.
485
	 * @param EE_Message $message
486
	 * @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...
487
	 */
488 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...
489
		$query_args = array(
490
			'ee' => 'msg_browser_trigger',
491
			'token' => $message->MSG_token()
492
		);
493
		return apply_filters(
494
			'FHEE__EEH_MSG_Template__generate_browser_trigger',
495
			add_query_arg( $query_args, site_url() ),
496
			$message
497
		);
498
	}
499
500
501
502
503
504
505
	/**
506
	 * This returns the url for triggering an in browser view of the error saved on the incoming message object.
507
	 * @param EE_Message $message
508
	 * @return string
509
	 */
510 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...
511
		return apply_filters(
512
			'FHEE__EEH_MSG_Template__generate_error_display_trigger',
513
			add_query_arg(
514
				array(
515
					'ee' => 'msg_browser_error_trigger',
516
					'token' => $message->MSG_token()
517
				),
518
				site_url()
519
			),
520
			$message
521
		);
522
	}
523
524
525
526
527
528
529
	/**
530
	 * This generates a url trigger for the msg_url_trigger route using the given arguments
531
	 *
532
	 * @param string          $sending_messenger    The sending messenger slug.
533
	 * @param string          $generating_messenger The generating messenger slug.
534
	 * @param string          $context              The context for the template.
535
	 * @param string          $message_type         The message type slug
536
	 * @param EE_Registration $registration
537
	 * @param integer          $message_template_group id 	The EE_Message_Template_Group ID for the template.
538
	 * @param integer          $data_id 	The id to the EE_Base_Class for getting the data used by the trigger.
539
	 * @return string          The generated url.
540
	 */
541
	public static function generate_url_trigger(
542
		$sending_messenger,
543
		$generating_messenger,
544
		$context,
545
		$message_type,
546
		EE_Registration $registration,
547
		$message_template_group,
548
		$data_id = 0
549
	) {
550
		$query_args = array(
551
			'ee' => 'msg_url_trigger',
552
			'snd_msgr' => $sending_messenger,
553
			'gen_msgr' => $generating_messenger,
554
			'message_type' => $message_type,
555
			'context' => $context,
556
			'token' => $registration->reg_url_link(),
557
			'GRP_ID' => $message_template_group,
558
			'id' => $data_id
559
			);
560
		$url = add_query_arg( $query_args, get_site_url() );
561
562
		//made it here so now we can just get the url and filter it.  Filtered globally and by message type.
563
		$url = apply_filters(
564
			'FHEE__EEH_MSG_Template__generate_url_trigger',
565
			$url,
566
			$sending_messenger,
567
			$generating_messenger,
568
			$context,
569
			$message_type,
570
			$registration,
571
			$message_template_group,
572
			$data_id
573
		);
574
		return $url;
575
	}
576
577
578
579
580
	/**
581
	 * Return the specific css for the action icon given.
582
	 *
583
	 * @since 4.9.0
584
	 *
585
	 * @param string $type  What action to return.
586
	 * @return string
587
	 */
588
	public static function get_message_action_icon( $type ) {
589
		$action_icons = self::get_message_action_icons();
590
		return isset( $action_icons[ $type ] ) ? $action_icons[ $type ] : '';
591
	}
592
593
594
	/**
595
	 * This is used for retrieving the css classes used for the icons representing message actions.
596
	 *
597
	 * @since 4.9.0
598
	 *
599
	 * @return array
600
	 */
601
	public static function get_message_action_icons() {
602
		return apply_filters( 'FHEE__EEH_MSG_Template__message_action_icons',
603
			array(
604
				'view' => array(
605
					'label' => __( 'View Message', 'event_espresso' ),
606
					'css_class' => 'dashicons dashicons-welcome-view-site',
607
				),
608
				'error' => array(
609
					'label' => __( 'View Error Message', 'event_espresso' ),
610
					'css_class' => 'dashicons dashicons-info',
611
				),
612
				'see_notifications_for' => array(
613
					'label' => __( 'View Related Messages', 'event_espresso' ),
614
					'css_class' => 'dashicons dashicons-images-alt',
615
				),
616
				'generate_now' => array(
617
					'label' => __( 'Generate the message now.', 'event_espresso' ),
618
					'css_class' => 'dashicons dashicons-admin-tools',
619
				),
620
				'send_now' => array(
621
					'label' => __( 'Send Immediately', 'event_espresso' ),
622
					'css_class' => 'dashicons dashicons-controls-forward',
623
				),
624
				'queue_for_resending' => array(
625
					'label' => __( 'Queue for Resending', 'event_espresso' ),
626
					'css_class' => 'dashicons dashicons-controls-repeat',
627
				),
628
				'view_transaction' => array(
629
					'label' => __( 'View related Transaction', 'event_espresso' ),
630
					'css_class' => 'dashicons dashicons-cart',
631
				)
632
			)
633
		);
634
	}
635
636
637
	/**
638
	 * This returns the url for a given action related to EE_Message.
639
	 *
640
	 * @since 4.9.0
641
	 *
642
	 * @param string $type  What type of action to return the url for.
643
	 * @param EE_Message $message   Required for generating the correct url for some types.
644
	 * @param array  $query_params   Any additional query params to be included with the generated url.
645
	 *
646
	 * @return string
647
	 */
648
	public static function get_message_action_url( $type, EE_Message $message = null, $query_params = array() ) {
649
		$action_urls = self::get_message_action_urls( $message, $query_params );
650
		return isset( $action_urls[ $type ] )  ? $action_urls[ $type ] : '';
651
	}
652
653
654
	/**
655
	 * This returns all the current urls for EE_Message actions.
656
	 *
657
	 * @since 4.9.0
658
	 *
659
	 * @param  EE_Message   $message    The EE_Message object required to generate correct urls for some types.
660
	 * @param  array    $query_params   Any additional query_params to be included with the generated url.
661
	 *
662
	 * @return array
663
	 */
664
	public static function get_message_action_urls( EE_Message $message = null, $query_params = array() ) {
665
		EE_Registry::instance()->load_helper( 'URL' );
666
		//if $message is not an instance of EE_Message then let's just do a dummy.
667
		$message = empty( $message ) ? EE_Message_Factory::create() : $message;
668
		$action_urls =  apply_filters(
669
			'FHEE__EEH_MSG_Template__get_message_action_url',
670
			array(
671
				'view' => EEH_MSG_Template::generate_browser_trigger( $message ),
672
				'error' => EEH_MSG_Template::generate_error_display_trigger( $message ),
673
				'see_notifications_for' => EEH_URL::add_query_args_and_nonce(
674
					array_merge(
675
						array(
676
							'page' => 'espresso_messages',
677
							'action' => 'default',
678
							'filterby' => 1,
679
						),
680
						$query_params
681
					),
682
					admin_url( 'admin.php' )
683
				),
684
				'generate_now' => EEH_URL::add_query_args_and_nonce(
685
					array(
686
						'page' => 'espresso_messages',
687
						'action' => 'generate_now',
688
						'MSG_ID' => $message->ID()
689
					),
690
					admin_url( 'admin.php' )
691
				),
692
				'send_now' => EEH_URL::add_query_args_and_nonce(
693
					array(
694
						'page' => 'espresso_messages',
695
						'action' => 'send_now',
696
						'MSG_ID' => $message->ID()
697
					),
698
					admin_url( 'admin.php' )
699
				),
700
				'queue_for_resending' => EEH_URL::add_query_args_and_nonce(
701
					array(
702
						'page' => 'espresso_messages',
703
						'action' => 'queue_for_resending',
704
						'MSG_ID' => $message->ID()
705
					),
706
					admin_url( 'admin.php' )
707
				),
708
			)
709
		);
710
		if (
711
			$message->TXN_ID() > 0
712
			&& EE_Registry::instance()->CAP->current_user_can(
713
				'ee_read_transaction',
714
				'espresso_transactions_default',
715
				$message->TXN_ID()
716
			)
717
		) {
718
			$action_urls['view_transaction'] = EEH_URL::add_query_args_and_nonce(
719
				array(
720
					'page' => 'espresso_transactions',
721
					'action' => 'view_transaction',
722
					'TXN_ID' => $message->TXN_ID()
723
				),
724
				admin_url( 'admin.php' )
725
			);
726
		} else {
727
			$action_urls['view_transaction'] = '';
728
		}
729
		return $action_urls;
730
	}
731
732
733
	/**
734
	 * This returns a generated link html including the icon used for the action link for EE_Message actions.
735
	 *
736
	 * @since 4.9.0
737
	 *
738
	 * @param string $type What type of action the link is for (if invalid type is passed in then an
739
	 *                     empty string is returned)
740
	 * @param EE_Message|null $message  The EE_Message object (required for some actions to generate correctly)
741
	 * @param array           $query_params Any extra query params to include in the generated link.
742
	 *
743
	 * @return string
744
	 */
745
	public static function get_message_action_link( $type, EE_Message $message = null, $query_params = array() ) {
746
		$url = EEH_MSG_Template::get_message_action_url( $type, $message, $query_params );
747
		$icon_css = EEH_MSG_Template::get_message_action_icon( $type );
748
		if ( empty( $url ) || empty( $icon_css ) || ! isset( $icon_css['css_class'] ) ) {
749
			return '';
750
		}
751
752
		$icon_css['css_class'] .= esc_attr(
753
			apply_filters(
754
				'FHEE__EEH_MSG_Template__get_message_action_link__icon_css_class',
755
				' js-ee-message-action-link ee-message-action-link-' . $type,
756
				$type,
757
				$message,
758
				$query_params
759
			)
760
		);
761
762
		return '<a href="' . $url . '"><span class="' . esc_attr( $icon_css['css_class'] ) . '"></span></a>';
763
764
	}
765
766
767
768
769
770
	/**
771
	 * This returns an array with keys as reg statuses and values as the corresponding message type slug (filtered).
772
	 *
773
	 * @since 4.9.0
774
	 * @return array
775
	 */
776
	public static function reg_status_to_message_type_array() {
777
		return (array) apply_filters(
778
			'FHEE__EEH_MSG_Template__reg_status_to_message_type_array',
779
			array(
780
				EEM_Registration::status_id_approved => 'registration',
781
				EEM_Registration::status_id_pending_payment => 'pending_approval',
782
				EEM_Registration::status_id_not_approved => 'not_approved_registration',
783
				EEM_Registration::status_id_cancelled => 'cancelled_registration',
784
				EEM_Registration::status_id_declined => 'declined_registration'
785
			)
786
		);
787
	}
788
789
790
791
792
	/**
793
	 * This returns the corresponding registration message type slug to the given reg status. If there isn't a
794
	 * match, then returns an empty string.
795
	 *
796
	 * @since 4.9.0
797
	 * @param $reg_status
798
	 * @return string
799
	 */
800
	public static function convert_reg_status_to_message_type( $reg_status ) {
801
		$reg_status_array = self::reg_status_to_message_type_array();
802
		return isset( $reg_status_array[$reg_status] ) ? $reg_status_array[$reg_status] : '';
803
	}
804
805
806
	/**
807
	 * This returns an array with keys as payment stati and values as the corresponding message type slug (filtered).
808
	 *
809
	 * @since 4.9.0
810
	 * @return array
811
	 */
812
	public static function payment_status_to_message_type_array() {
813
		return (array) apply_filters(
814
			'FHEE__EEH_MSG_Template__payment_status_to_message_type_array',
815
			array(
816
				EEM_Payment::status_id_approved => 'payment',
817
				EEM_Payment::status_id_pending => 'payment_pending',
818
				EEM_Payment::status_id_cancelled => 'payment_cancelled',
819
				EEM_Payment::status_id_declined => 'payment_declined',
820
				EEM_Payment::status_id_failed => 'payment_failed'
821
			)
822
		);
823
	}
824
825
826
827
828
	/**
829
	 * This returns the corresponding payment message type slug to the given payment status. If there isn't a match then
830
	 * an empty string is returned
831
	 *
832
	 * @since 4.9.0
833
	 * @param $payment_status
834
	 * @return string
835
	 */
836
	public static function convert_payment_status_to_message_type( $payment_status ) {
837
		$payment_status_array = self::payment_status_to_message_type_array();
838
		return isset( $payment_status_array[$payment_status] ) ? $payment_status_array[$payment_status] : '';
839
	}
840
841
842
	/**
843
	 * This is used to retrieve the template pack for the given name.
844
	 *
845
	 * @param string $template_pack_name  should match the set `dbref` property value on the EE_Messages_Template_Pack.
846
	 *
847
	 * @return EE_Messages_Template_Pack
848
	 */
849
	public static function get_template_pack( $template_pack_name ) {
850
		if ( ! self::$_template_pack_collection instanceof EE_Object_Collection ) {
851
			self::$_template_pack_collection = new EE_Messages_Template_Pack_Collection();
852
		}
853
854
		//first see if in collection already
855
		$template_pack = self::$_template_pack_collection->get_by_name( $template_pack_name );
856
857
		if ( $template_pack instanceof EE_Messages_Template_Pack ) {
858
			return $template_pack;
859
		}
860
861
		//nope...let's get it.
862
		//not set yet so let's attempt to get it.
863
		$pack_class_name = 'EE_Messages_Template_Pack_' . str_replace(
864
				' ',
865
				'_',
866
				ucwords(
867
					str_replace( '_', ' ', $template_pack_name )
868
				)
869
			);
870
		if ( ! class_exists( $pack_class_name ) && $template_pack_name !== 'default' ) {
871
			return self::get_template_pack( 'default' );
872
		} else {
873
			$template_pack = new $pack_class_name;
874
			self::$_template_pack_collection->add( $template_pack );
875
			return $template_pack;
876
		}
877
	}
878
879
880
881
882
	/**
883
	 * Globs template packs installed in core and returns the template pack collection with all installed template packs
884
	 * in it.
885
	 *
886
	 * @since 4.9.0
887
	 *
888
	 * @return EE_Messages_Template_Pack_Collection
889
	 */
890
	public static function get_template_pack_collection() {
891
		$new_collection = false;
892
		if ( ! self::$_template_pack_collection instanceof EE_Messages_Template_Pack_Collection ) {
893
			self::$_template_pack_collection = new EE_Messages_Template_Pack_Collection();
894
			$new_collection = true;
895
		}
896
897
		//glob the defaults directory for messages
898
		$templates = glob( EE_LIBRARIES . 'messages/defaults/*', GLOB_ONLYDIR );
899
		foreach( $templates as $template_path ) {
900
			//grab folder name
901
			$template = basename( $template_path );
902
903
			if ( ! $new_collection ) {
904
				//already have it?
905
				if ( self::$_template_pack_collection->get_by_name( $template ) instanceof EE_Messages_Template_Pack ) {
906
					continue;
907
				}
908
			}
909
910
			//setup classname.
911
			$template_pack_class_name = 'EE_Messages_Template_Pack_' . str_replace(
912
					' ',
913
					'_',
914
					ucwords(
915
						str_replace(
916
							'_',
917
							' ',
918
							$template
919
						)
920
					)
921
				);
922
			if ( ! class_exists( $template_pack_class_name ) ) {
923
				continue;
924
			}
925
			self::$_template_pack_collection->add( new $template_pack_class_name );
926
		}
927
928
		/**
929
		 * Filter for plugins to add in any additional template packs
930
		 * Note the filter name here is for backward compat, this used to be found in EED_Messages.
931
		 */
932
		$additional_template_packs = apply_filters( 'FHEE__EED_Messages__get_template_packs__template_packs', array() );
933
		foreach ( (array) $additional_template_packs as $template_pack ) {
934
			if ( ! self::$_template_pack_collection->contains($template_pack ) ) {
935
				self::$_template_pack_collection->add( $template_pack );
936
			}
937
		}
938
		return self::$_template_pack_collection;
939
	}
940
941
942
943
	/**
944
	 * This is a wrapper for the protected _create_new_templates function
945
	 *
946
	 * @param string $messenger_name
947
	 * @param string $message_type_name message type that the templates are being created for
948
	 * @param int    $GRP_ID
949
	 * @param bool   $global
950
	 * @return array
951
	 * @throws \EE_Error
952
	 */
953
	public function create_new_templates( $messenger_name, $message_type_name, $GRP_ID = 0, $global = false ) {
954
		/** @type EE_Message_Resource_Manager $Message_Resource_Manager */
955
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
956
		$messenger = $Message_Resource_Manager->valid_messenger( $messenger_name );
957
		$message_type = $Message_Resource_Manager->valid_message_type( $message_type_name );
958
		if ( ! $this->message_type_has_active_templates_for_messenger( $messenger, $message_type, $global ) ) {
959
			return array();
960
		}
961
		//whew made it this far!  Okay, let's go ahead and create the templates then
962
		return $this->_create_new_templates( $messenger, $message_type, $GRP_ID, $global );
963
	}
964
965
966
967
	/**
968
	 * @param \EE_Messenger    $messenger
969
	 * @param \EE_message_type $message_type
970
	 * @param                  $GRP_ID
971
	 * @param                  $global
972
	 * @return array|mixed
973
	 */
974
	protected function _create_new_templates( EE_Messenger $messenger, EE_message_type $message_type, $GRP_ID, $global ) {
975
		//if we're creating a custom template then we don't need to use the defaults class
976
		if ( ! $global ) {
977
			return $this->_create_custom_template_group( $messenger, $message_type, $GRP_ID );
978
		}
979
		$Message_Template_Defaults = new EE_Message_Template_Defaults(
980
			EE_Registry::instance()->load_lib( 'messages' ),
0 ignored issues
show
Documentation introduced by
\EE_Registry::instance()->load_lib('messages') is of type null|object, but the function expects a object<EE_Messages>.

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...
981
			$messenger->name,
982
			$message_type->name,
983
			$GRP_ID
984
		);
985
		//generate templates
986
		$success = $Message_Template_Defaults->create_new_templates();
987
		/**
988
		 * $success is in an array in the following format
989
		 * array(
990
		 *    'GRP_ID' => $new_grp_id,
991
		 *    'MTP_context' => $first_context_in_new_templates,
992
		 * )
993
		 */
994
		return $success;
995
	}
996
997
998
999
	/**
1000
	 * This creates a custom template using the incoming GRP_ID
1001
	 *
1002
	 * @param \EE_Messenger    $messenger
1003
	 * @param \EE_message_type $message_type
1004
	 * @param  int             $GRP_ID 		GRP_ID for the template_group being used as the base
1005
	 * @return  array $success 				This will be an array in the format:
1006
	 *                                      array(
1007
	 * 											'GRP_ID' => $new_grp_id,
1008
	 * 											'MTP_context' => $first_context_in_created_template
1009
	 * 										)
1010
	 * @access private
1011
	 */
1012
	private function _create_custom_template_group( EE_Messenger $messenger, EE_message_type $message_type, $GRP_ID ) {
1013
		//defaults
1014
		$success = array( 'GRP_ID' => null, 'MTP_context' => '' );
1015
		//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.
1016
		$Message_Template_Group = empty( $GRP_ID )
1017
			? EEM_Message_Template_Group::instance()->get_one(
1018
				array(
1019
					array(
1020
						'MTP_messenger'    => $messenger->name,
1021
						'MTP_message_type' => $message_type->name,
1022
						'MTP_is_global'    => true
1023
					)
1024
				)
1025
			)
1026
			: EEM_Message_Template_Group::instance()->get_one_by_ID( $GRP_ID );
1027
		//if we don't have a mtg at this point then we need to bail.
1028
		if ( ! $Message_Template_Group instanceof EE_Message_Template_Group ) {
1029
			EE_Error::add_error(
1030
				sprintf(
1031
					__(
1032
						'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.',
1033
						'event_espresso'
1034
					),
1035
					$GRP_ID
1036
				),
1037
				__FILE__,
1038
				__FUNCTION__,
1039
				__LINE__
1040
			);
1041
			return $success;
1042
		}
1043
		//let's get all the related message_template objects for this group.
1044
		$mtts = $Message_Template_Group->message_templates();
1045
		//now we have what we need to setup the new template
1046
		$new_mtg = clone $Message_Template_Group;
1047
		$new_mtg->set( 'GRP_ID', 0 );
1048
		$new_mtg->set( 'MTP_is_global', false );
1049
		$template_name = defined( 'DOING_AJAX' ) && ! empty( $_POST[ 'templateName' ] )
1050
			? $_POST[ 'templateName' ]
1051
			: __(
1052
				'New Custom Template',
1053
				'event_espresso'
1054
			);
1055
		$template_description = defined( "DOING_AJAX" ) && ! empty( $_POST[ 'templateDescription' ] )
1056
			? $_POST[ 'templateDescription' ]
1057
			: sprintf(
1058
				__(
1059
					'This is a custom template that was created for the %s messenger and %s message type.',
1060
					'event_espresso'
1061
				),
1062
				$new_mtg->messenger_obj()->label[ 'singular' ],
1063
				$new_mtg->message_type_obj()->label[ 'singular' ]
1064
			);
1065
		$new_mtg->set( 'MTP_name', $template_name );
1066
		$new_mtg->set( 'MTP_description', $template_description );
1067
		//remove ALL relations on this template group so they don't get saved!
1068
		$new_mtg->_remove_relations( 'Message_Template' );
1069
		$new_mtg->save();
1070
		$success[ 'GRP_ID' ] = $new_mtg->ID();
1071
		$success[ 'template_name' ] = $template_name;
1072
		//add new message templates and add relation to.
1073
		foreach ( $mtts as $mtt ) {
1074
			if ( ! $mtt instanceof EE_Message_Template ) {
1075
				continue;
1076
			}
1077
			$nmtt = clone $mtt;
1078
			$nmtt->set( 'MTP_ID', 0 );
1079
			$nmtt->set( 'GRP_ID', $new_mtg->ID() ); //relation
1080
			$nmtt->save();
1081
			if ( empty( $success[ 'MTP_context' ] ) ) {
1082
				$success[ 'MTP_context' ] = $nmtt->get( 'MTP_context' );
1083
			}
1084
		}
1085
		return $success;
1086
	}
1087
1088
1089
1090
	/**
1091
	 * message_type_has_active_templates_for_messenger
1092
	 *
1093
	 * @param \EE_Messenger    $messenger
1094
	 * @param \EE_message_type $message_type
1095
	 * @param bool             $global
1096
	 * @return bool
1097
	 */
1098
	public function message_type_has_active_templates_for_messenger(
1099
		EE_Messenger $messenger,
1100
		EE_message_type $message_type,
1101
		$global = false
1102
	) {
1103
		//is given message_type valid for given messenger (if this is not a global save)
1104
		if ( $global ) {
1105
			return true;
1106
		}
1107
		$active_templates = EEM_Message_Template_Group::instance()->count(
1108
			array(
1109
				array(
1110
					'MTP_is_active'    => true,
1111
					'MTP_messenger'    => $messenger->name,
1112
					'MTP_message_type' => $message_type->name
1113
				)
1114
			)
1115
		);
1116
		if ( $active_templates > 0 ) {
1117
			return true;
1118
		}
1119
		EE_Error::add_error(
1120
			sprintf(
1121
				__(
1122
					'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.',
1123
					'event_espresso'
1124
				),
1125
				$message_type->name,
1126
				$messenger->name
1127
			),
1128
			__FILE__,
1129
			__FUNCTION__,
1130
			__LINE__
1131
		);
1132
		return false;
1133
	}
1134
1135
1136
1137
	/**
1138
	 * get_fields
1139
	 * This takes a given messenger and message type and returns all the template fields indexed by context (and with field type).
1140
	 *
1141
	 * @param  string $messenger_name    name of EE_Messenger
1142
	 * @param  string $message_type_name name of EE_message_type
1143
	 * @return array
1144
	 */
1145
	public function get_fields( $messenger_name, $message_type_name ) {
1146
		$template_fields = array();
1147
		/** @type EE_Message_Resource_Manager $Message_Resource_Manager */
1148
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
1149
		$messenger = $Message_Resource_Manager->valid_messenger( $messenger_name );
1150
		$message_type = $Message_Resource_Manager->valid_message_type( $message_type_name );
1151
		if ( ! $this->message_type_has_active_templates_for_messenger( $messenger, $message_type ) ) {
1152
			return array();
1153
		}
1154
		//okay now let's assemble an array with the messenger template fields added to the message_type contexts.
1155
		foreach ( $message_type->get_contexts() as $context => $details ) {
1156
			foreach ( $messenger->get_template_fields() as $field => $value ) {
1157
				$template_fields[ $context ][ $field ] = $value;
1158
			}
1159
		}
1160
		if ( empty( $template_fields ) ) {
1161
			EE_Error::add_error(
1162
				__( 'Something went wrong and we couldn\'t get any templates assembled', 'event_espresso' ),
1163
				__FILE__,
1164
				__FUNCTION__,
1165
				__LINE__
1166
			);
1167
			return array();
1168
		}
1169
		return $template_fields;
1170
	}
1171
1172
}
1173