Completed
Branch BUG-9623-config-log (c144cd)
by
unknown
527:42 queued 509:06
created

EE_messenger   F

Complexity

Total Complexity 71

Size/Duplication

Total Lines 814
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 814
rs 2.5806
wmc 71
lcom 1
cbo 14

33 Methods

Rating   Name   Duplication   Size   Complexity  
B get_preview() 0 26 6
A __construct() 0 16 1
_set_template_fields() 0 1 ?
_set_default_message_types() 0 1 ?
_set_valid_message_types() 0 1 ?
_set_validator_config() 0 1 ?
_send_message() 0 1 ?
_preview() 0 1 ?
A enqueue_scripts_styles() 0 3 1
A send_now() 0 3 1
A allow_empty_to_field() 0 3 1
A _set_supports_labels() 0 3 1
A _set_supports_labels_defaults() 0 9 1
A get_supports_labels() 0 6 3
A get_variation() 0 7 2
A get_default_message_types() 0 10 1
A get_valid_message_types() 0 11 1
A set_validator_config() 0 3 1
A get_validator_config() 0 7 1
A get_messenger_admin_page_content() 0 3 1
F _get_admin_content_events_edit() 0 72 17
A get_template_fields() 0 5 1
A _set_template_value() 0 6 2
A send_message() 0 5 1
A add_preview_script() 0 7 1
B _validate_and_setup() 0 23 6
A _get_main_template() 0 14 3
A _set_test_settings_fields() 0 3 1
A get_test_settings_fields() 0 3 1
A get_existing_test_settings() 0 6 2
A set_existing_test_settings() 0 7 1
C get_field_label() 0 22 12
A do_secondary_messenger_hooks() 0 3 1

How to fix   Complexity   

Complex Class

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

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

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

1
<?php
2
if (!defined('EVENT_ESPRESSO_VERSION') )
3
	exit('NO direct script access allowed');
4
5
/**
6
 * Event Espresso
7
 *
8
 * Event Registration and Management Plugin for WordPress
9
 *
10
 * @ package			Event Espresso
11
 * @ author				Seth Shoultes
12
 * @ copyright		(c) 2008-2011 Event Espresso  All Rights Reserved.
13
 * @ license			http://eventespresso.com/support/terms-conditions/   * see Plugin Licensing *
14
 * @ link				http://www.eventespresso.com
15
 * @ version		 	4.0
16
 *
17
 * ------------------------------------------------------------------------
18
 *
19
 * EE_messenger class
20
 *
21
 * Abstract class for setting up messengers.
22
 * Different messengers (i.e. email, sms) can be setup by extending this class and adding them to the /includes/core/messages/messengers' directory. View examples there.
23
 *
24
 * @package			Event Espresso
25
 * @subpackage		includes/core/messages
26
 * @author			Darren Ethier, Brent Christensen
27
 *
28
 * ------------------------------------------------------------------------
29
 */
30
abstract class EE_messenger extends EE_Messages_Base {
31
32
33
34
	/**
35
	 * This property holds the default message types associated with this messenger when it is activated. The values of the array must match a valid message type.
36
	 * This property gets set by the _set_default_message_types() method.
37
	 *
38
	 * @var array
39
	 */
40
	protected $_default_message_types = array();
41
42
43
44
45
	/**
46
	 * This property holds the message types that are valid for use with this messenger.
47
	 * It gets set by the _set_valid_message_types() method.
48
	 *
49
	 * @var array
50
	 */
51
	protected $_valid_message_types = array();
52
53
54
55
	/**
56
	 * Holds the configuration for the EE_Messages_Validator class to know how to validated the different fields. Note that the Validator will match each field here with the allowed shortcodes set in the "valid_shortcodes" array for the matched message type context.  So message types don't need to set a $_validator_config property.
57
	 *
58
	 * Remember, ALL fields must be declared in this array.  However, an empty value for the field means that the field will accept all valid shortcodes set for the given context in the message type (by default).
59
	 *
60
	 * Array should be in this format:
61
	 *
62
	 * array(
63
	 * 	'field_name(i.e.to)' => array(
64
	 * 		'shortcodes' => array('email'), //an array of shortcode groups (correspond to EE_Shortcodes library class) that are allowed in the field. Typically you can just include $this->_valid_shortcodes['field_name'] as the value here (because they will match).
65
	 * 		'specific_shortcodes' => array( array('[EVENT_AUTHOR_EMAIL]' => __('Admin Email', 'event_espresso')), //if this index is present you can further restrict the field to ONLY specific shortcodes if an entire group isn't sufficient. Specific shortcodes need to be listed as an array with the index the shortcode and the value = the label.
66
	 * 		'type' => 'email' //this is the field type and should match one of the validator types (see EE_Messages_Validator::validator() for all the possible types).  If not required you can just leave empty.,
67
	 * 		'required' => array'[SHORTCODE]') //this is used to indicate the shortcodes that MUST be in the assembled array of shortcodes by the validator in order for this field to be included in validation.  Otherwise the validator will always assign shortcodes for this field (regardless of whether the field settings for the given messenger/message_type/context use the field or not.).. please note, this does NOT mean that the shortcodes listed here MUST be in the given field.
68
	 * 	)
69
	 * )
70
	 *
71
	 * @var array
72
	 */
73
	protected $_validator_config = array();
74
75
76
77
	/**
78
	 * This will hold the EEM_message_templates model for interacting with the database and retrieving active templates for the messenger
79
	 * @var object
80
	 */
81
	protected $_EEM_data;
82
83
84
85
	/**
86
	 * this property just holds an array of the various template refs.
87
	 * @var array
88
	 */
89
	protected $_template_fields = array();
90
91
92
93
94
	/**
95
	 * This holds an array of the arguments used in parsing a template for the sender.
96
	 * @var array
97
	 */
98
	protected $_template_args = array();
99
100
101
102
103
104
105
	/**
106
	 * This property will hold the configuration for any test settings fields that are required for the "test" button that is used to trigger an actual test of this messenger
107
	 *
108
	 * @protected
109
	 * @var array
110
	 */
111
	protected $_test_settings_fields = array();
112
113
114
115
116
117
118
	/**
119
	 * This will hold the EE_Messages_Template_Pack object when set on the messenger.  This is set via the validate and setup method which grabs the template pack from the incoming messages object.
120
	 *
121
	 * @since 4.5.0
122
	 *
123
	 * @var EE_Messages_Template_Pack
124
	 */
125
	protected $_tmp_pack;
126
127
128
129
130
	/**
131
	 * This will hold the variation to use when performing a send.  It is set via the validate and setup method which grabs the variation from the incoming messages object on the send method.
132
	 *
133
	 * @since 4.5.0
134
	 *
135
	 * @var string
136
	 */
137
	protected $_variation;
138
139
140
141
142
143
	/**
144
	 * This property is a stdClass that holds labels for all the various supporting properties for this messenger.  These labels are set via the _set_supports_labels() method in children classes. Initially this will include the label for:
145
	 *
146
	 * 	- template pack
147
	 * 	- template variation
148
	 *
149
	 * @since 4.5.0
150
	 *
151
	 * @var stdClass
152
	 */
153
	protected $_supports_labels;
154
155
156
157
158
159
	/**
160
	 * This property is set when the send_message() method is called and holds the Message Type used to generate templates with this messenger for the messages.
161
	 *
162
	 * @var EE_message_type
163
	 */
164
	protected $_incoming_message_type;
165
166
167
168
	/**
169
	 * This flag sets whether a messenger is activated by default  on installation (or reactivation) of EE core or not.
170
	 *
171
	 * @var bool
172
	 */
173
	public $activate_on_install = FALSE;
174
175
176
177
178
179
	public function __construct() {
180
		$this->_EEM_data = EEM_Message_Template_Group::instance();
181
		$this->_messages_item_type = 'messenger';
182
183
		parent::__construct();
184
185
		$this->_set_test_settings_fields();
186
		$this->_set_template_fields();
187
		$this->_set_default_message_types();
188
		$this->_set_valid_message_types();
189
		$this->_set_validator_config();
190
191
192
		$this->_supports_labels = new stdClass();
193
		$this->_set_supports_labels();
194
	}
195
196
197
198
199
200
	/**
201
	 * _set_template_fields
202
	 * This sets up the fields that a messenger requires for the message to go out.
203
	 *
204
	 * @abstract
205
	 * @access  protected
206
	 * @return void
207
	 */
208
	abstract protected function _set_template_fields();
209
210
211
212
213
214
215
216
217
218
	/**
219
	 * This method sets the _default_message_type property (see definition in docs attached to property)
220
	 *
221
	 * @abstract
222
	 * @access protected
223
	 * @return void
224
	 */
225
	abstract protected function _set_default_message_types();
226
227
228
229
230
231
232
233
	/**
234
	 * Sets the _valid_message_types property (see definition in cods attached to property)
235
	 *
236
	 * @since 4.5.0
237
	 *
238
	 * @abstract
239
	 * @return void
240
	 */
241
	abstract protected function _set_valid_message_types();
242
243
244
245
246
247
248
249
	/**
250
	 * Child classes must declare the $_validator_config property using this method.
251
	 * See comments for $_validator_config for details on what it is used for.
252
	 *
253
	 * NOTE:  messengers should set an array of valid shortcodes for ALL scenarios.  The corresponding validator class (validators/{messenger}) can be used to restrict only certain shortcodes per template so users cannot add certain shortcodes.
254
	 *
255
	 * @access protected
256
	 * @return void
257
	 */
258
	abstract protected function _set_validator_config();
259
260
261
262
263
264
265
	/**
266
	 * We just deliver the messages don't kill us!!  This method will need to be modified by child classes for whatever action is taken to actually send a message.
267
	 * @return bool | WP_Error
268
	 * @todo  at some point we may want to return success or fail so we know whether a message has gone off okay and we can assemble reporting.
269
	 */
270
	abstract protected function _send_message();
271
272
273
274
275
	/**
276
	 * We give you pretty previews of the messages!
277
	 * @return string html body for message content.
278
	 */
279
	abstract protected function _preview();
280
281
282
283
284
	/**
285
	 * Used by messengers (or preview) for enqueueing any scripts or styles need in message generation.
286
	 *
287
	 * @since 4.5.0
288
	 *
289
	 * @return void
290
	 */
291
	public function enqueue_scripts_styles() {
292
		do_action( 'AHEE__EE_messenger__enqueue_scripts_styles');
293
	}
294
295
296
297
298
299
	/**
300
	 * This is used to indicate whether a messenger must be sent immediately or not.
301
	 * eg. The HTML messenger will override this to return true because it should be displayed in user's browser right
302
	 * away.  The PDF messenger is similar.
303
	 *
304
	 * This flag thus overrides any priorities that may be set on the message type used to generate the message.
305
	 *
306
	 * Default for this is false.  So children classes must override this if they want a message to be executed immediately.
307
	 *
308
	 * @since  4.9.0
309
	 * @return bool
310
	 */
311
	public function send_now() {
312
		return false;
313
	}
314
315
316
317
318
319
	/**
320
	 * This is a way for a messenger to indicate whether it allows an empty to field or not.
321
	 * Note: If the generated message is a for a preview, this value is ignored.
322
	 * @since 4.9.0
323
	 * @return bool
324
	 */
325
	public function allow_empty_to_field() {
326
		return false;
327
	}
328
329
330
331
332
333
	/**
334
	 * Sets the defaults for the _supports_labels property.  Can be overridden by child classes.
335
	 * @see property definition for info on how its formatted.
336
	 *
337
	 * @since 4.5.0;
338
	 * @return void
339
	 */
340
	protected function _set_supports_labels() {
341
		$this->_set_supports_labels_defaults();
342
	}
343
344
345
346
347
348
	/**
349
	 * Sets the defaults for the _supports_labels property.
350
	 *
351
	 * @since 4.5.0
352
	 *
353
	 * @return void
354
	 */
355
	private function _set_supports_labels_defaults() {
356
		$this->_supports_labels->template_pack = __('Template Structure', 'event_espresso');
357
		$this->_supports_labels->template_variation = __('Template Style', 'event_espresso');
358
		$this->_supports_labels->template_pack_description = __('Template Structure options are bundled structural changes for templates.', 'event_espresso');
359
360
		$this->_supports_labels->template_variation_description = __('These are different styles to choose from for the selected template structure.  Usually these affect things like font style, color, borders etc.  In some cases the styles will also make minor layout changes.');
361
362
		$this->_supports_labels = apply_filters( 'FHEE__EE_messenger___set_supports_labels_defaults___supports_labels', $this->_supports_labels, $this );
363
	}
364
365
366
367
368
369
	/**
370
	 * This returns the _supports_labels property.
371
	 *
372
	 * @since 4.5.0
373
	 *
374
	 * @return stdClass
375
	 */
376
	public function get_supports_labels() {
377
		if ( empty( $this->_supports_labels->template_pack ) || empty( $this->_supports_labels->template_variation) ) {
378
			$this->_set_supports_labels_defaults();
379
		}
380
		return apply_filters( 'FHEE__EE_messenger__get_supports_labels', $this->_supports_labels, $this );
381
	}
382
383
384
385
386
	/**
387
	 * Used to retrieve a variation (typically the path/url to a css file)
388
	 *
389
	 * @since 4.5.0
390
	 *
391
	 * @param EE_Messages_Template_Pack $pack   The template pack used for retrieving the variation.
392
	 * @param string                    $message_type_name The name property of the message type that we need the variation for.
393
	 * @param bool                      $url   Whether to return url (true) or path (false). Default is false.
394
	 * @param string                    $type What variation type to return. Default is 'main'.
395
	 * @param string 	           $variation What variation for the template pack
396
	 * @param bool 	           $skip_filters This allows messengers to add a filter for another messengers get_variation but call skip filters on the callback so there is no recursion on apply_filters.
397
	 *
398
	 * @return string                    path or url for the requested variation.
399
	 */
400
	public function get_variation( EE_Messages_Template_Pack $pack, $message_type_name, $url = FALSE, $type = 'main', $variation = 'default', $skip_filters = FALSE ) {
401
		$this->_tmp_pack = $pack;
402
		$variation_path = apply_filters( 'EE_messenger__get_variation__variation', false, $pack, $this->name, $message_type_name, $url, $type, $variation, $skip_filters );
403
		$variation_path = empty( $variation_path ) ? $this->_tmp_pack->get_variation( $this->name, $message_type_name, $type, $variation, $url, '.css', $skip_filters ) : $variation_path;
404
		return $variation_path;
405
406
	}
407
408
409
410
411
412
413
414
	/**
415
	 * This just returns the default message types associated with this messenger when it is first activated.
416
	 *
417
	 * @access public
418
	 * @return array
419
	 */
420
	public function get_default_message_types() {
421
		$class = get_class( $this );
422
423
		//messenger specific filter
424
		$default_types = apply_filters( 'FHEE__' . $class . '__get_default_message_types__default_types', $this->_default_message_types, $this );
425
426
		//all messengers filter
427
		$default_types = apply_filters( 'FHEE__EE_messenger__get_default_message_types__default_types', $default_types, $this );
428
		return $default_types;
429
	}
430
431
432
433
434
	/**
435
	 * Returns the valid message types associated with this messenger.
436
	 *
437
	 * @since 4.5.0
438
	 *
439
	 * @return array
440
	 */
441
	public function get_valid_message_types() {
442
		$class = get_class( $this );
443
444
		//messenger specific filter
445
		//messenger specific filter
446
		$valid_types = apply_filters( 'FHEE__' . $class . '__get_valid_message_types__valid_types', $this->_valid_message_types, $this );
447
448
		//all messengers filter
449
		$valid_types = apply_filters( 'FHEE__EE_messenger__get_valid_message_types__valid_types', $valid_types, $this );
450
		return $valid_types;
451
	}
452
453
454
455
456
457
	/**
458
	 * this is just used by the custom validators (EE_Messages_Validator classes) to modify the _validator_config for certain message_type/messenger combos where a context may only use certain shortcodes etc.
459
	 *
460
	 * @access public
461
	 * @param array $new_config Whatever is put in here will reset the _validator_config property
462
	 */
463
	public function set_validator_config( $new_config ) {
464
		$this->_validator_config = $new_config;
465
	}
466
467
468
469
470
	/**
471
	 * This returns the _validator_config property
472
	 *
473
	 * @access public
474
	 * @return array
475
	 */
476
	public function get_validator_config() {
477
		$class = get_class($this);
478
479
		$config = apply_filters( 'FHEE__' . $class . '__get_validator_config', $this->_validator_config, $this );
480
		$config = apply_filters( 'FHEE__EE_messenger__get_validator_config', $config, $this );
481
		return $config;
482
	}
483
484
485
486
487
	/**
488
	 * this public method accepts a page slug (for an EE_admin page) and will return the response from the child class callback function if that page is registered via the `_admin_registered_page` property set by the child class.
489
	 *
490
	 * @param string $page the slug of the EE admin page
491
	 * @param array $message_types an array of active message type objects
492
	 * @param string $action the page action (to allow for more specific handling - i.e. edit vs. add pages)
493
	 * @param array $extra  This is just an extra argument that can be used to pass additional data for setting up page content.
494
	 * @access public
495
	 * @return string content for page
496
	 */
497
	public function get_messenger_admin_page_content( $page, $action = null, $extra = array(), $message_types = array() ) {
498
		return $this->_get_admin_page_content( $page, $action, $extra, $message_types );
499
	}
500
501
502
503
	/**
504
	 * @param $message_types
505
	 * @param array $extra
506
	 * @return mixed|string
507
	 */
508
	protected function _get_admin_content_events_edit( $message_types, $extra ) {
0 ignored issues
show
Unused Code introduced by
The parameter $message_types 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...
509
		//defaults
510
		$template_args = array();
511
		$selector_rows = '';
512
513
		//we don't need message types here so we're just going to ignore. we do, however, expect the event id here. The event id is needed to provide a link to setup a custom template for this event.
514
		$event_id = isset( $extra['event'] ) ? $extra['event'] : NULL;
515
516
		$template_wrapper_path = EE_LIBRARIES . 'messages/messenger/admin_templates/event_switcher_wrapper.template.php';
517
		$template_row_path = EE_LIBRARIES . 'messages/messenger/admin_templates/event_switcher_row.template.php';
518
519
		//array of template objects for global and custom (non-trashed) (but remember just for this messenger!)
520
		$global_templates = EEM_Message_Template_Group::instance()->get_all(
521
			array( array( 'MTP_messenger' => $this->name, 'MTP_is_global' => true, 'MTP_is_active' => true ) )
522
		);
523
		$templates_for_event = EEM_Message_Template_Group::instance()->get_all_custom_templates_by_event(
524
			$event_id,
525
			array(
526
				'MTP_messenger' => $this->name,
527
				'MTP_is_active' => true
528
			)
529
		);
530
		$templates_for_event = !empty( $templates_for_event ) ? $templates_for_event : array();
531
532
		//so we need to setup the rows for the selectors and we use the global mtpgs (cause those will the active message template groups)
533
		foreach ( $global_templates as $mtpgID => $mtpg ) {
534
			if ( $mtpg instanceof EE_Message_Template_Group ) {
535
				//verify this message type is supposed to show on this page
536
				$mtp_obj = $mtpg->message_type_obj();
537
				if ( ! $mtp_obj instanceof EE_message_type ) {
538
					continue;
539
				}
540
				$mtp_obj->admin_registered_pages = (array)$mtp_obj->admin_registered_pages;
541
				if ( ! in_array( 'events_edit', $mtp_obj->admin_registered_pages ) ) {
542
					continue;
543
				}
544
				$select_values = array();
545
				$select_values[ $mtpgID ] = __( 'Global', 'event_espresso' );
546
				$default_value = array_key_exists( $mtpgID, $templates_for_event ) && ! $mtpg->get( 'MTP_is_override' ) ? $mtpgID : null;
547
				//if the override has been set for the global template, then that means even if there are custom templates already created we ignore them because of the set override.
548
				if ( ! $mtpg->get( 'MTP_is_override' ) ) {
549
					//any custom templates for this message type?
550
					$custom_templates = EEM_Message_Template_Group::instance()->get_custom_message_template_by_m_and_mt( $this->name, $mtpg->message_type() );
551
					foreach ( $custom_templates as $cmtpgID => $cmtpg ) {
552
						$select_values[ $cmtpgID ] = $cmtpg->name();
553
						$default_value = array_key_exists( $cmtpgID, $templates_for_event ) ? $cmtpgID : $default_value;
554
					}
555
				}
556
				//if there is no $default_value then we set it as the global
557
				$default_value = empty( $default_value ) ? $mtpgID : $default_value;
558
				$edit_url = EEH_URL::add_query_args_and_nonce( array( 'page' => 'espresso_messages', 'action' => 'edit_message_template', 'id' => $default_value ), admin_url( 'admin.php' ) );
559
				$create_url = EEH_URL::add_query_args_and_nonce( array( 'page' => 'espresso_messages', 'action' => 'add_new_message_template', 'GRP_ID' => $default_value ), admin_url( 'admin.php' ) );
560
				$st_args[ 'mt_name' ] = ucwords( $mtp_obj->label[ 'singular' ] );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$st_args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $st_args = 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...
561
				$st_args[ 'mt_slug' ] = $mtpg->message_type();
0 ignored issues
show
Bug introduced by
The variable $st_args does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
562
				$st_args[ 'messenger_slug' ] = $this->name;
563
				$st_args[ 'selector' ] = EEH_Form_Fields::select_input( 'event_message_templates_relation[' . $mtpgID . ']', $select_values, $default_value, 'data-messenger="' . $this->name . '" data-messagetype="' . $mtpg->message_type() . '"', 'message-template-selector' );
564
				//note that  message template group that has override_all_custom set will remove the ability to set a custom message template based off of the global (and that also in turn overrides any other custom templates).
565
				$st_args[ 'create_button' ] = $mtpg->get( 'MTP_is_override' ) ? '' : '<a data-messenger="' . $this->name . '" data-messagetype="' . $mtpg->message_type() . '" data-grpid="' . $default_value . '" target="_blank" href="' . $create_url . '" class="button button-small create-mtpg-button">' . __( 'Create New Custom', 'event_espresso' ) . '</a>';
566
				$st_args[ 'create_button' ] = EE_Registry::instance()->CAP->current_user_can( 'ee_edit_messages', 'espresso_messages_add_new_message_template' ) ? $st_args[ 'create_button' ] : '';
567
				$st_args[ 'edit_button' ] = EE_Registry::instance()->CAP->current_user_can( 'ee_edit_message', 'espresso_messages_edit_message_template', $mtpgID ) ? '<a data-messagetype="' . $mtpg->message_type() . '" data-grpid="' . $default_value . '" target="_blank" href="' . $edit_url . '" class="button button-small edit-mtpg-button">' . __( 'Edit', 'event_espresso' ) . '</a>' : '';
568
				$selector_rows .= EEH_Template::display_template( $template_row_path, $st_args, true );
569
			}
570
		}
571
572
		//if no selectors present then get out.
573
		if ( empty( $selector_rows ) ) {
574
			return '';
575
		}
576
577
		$template_args['selector_rows'] = $selector_rows;
578
		return EEH_Template::display_template( $template_wrapper_path, $template_args, TRUE );
579
	}
580
581
582
583
584
585
586
	/**
587
	 * get_template_fields
588
	 *
589
	 * @access public
590
	 * @return array $this->_template_fields
591
	 */
592
	public function get_template_fields() {
593
		$template_fields = apply_filters( 'FHEE__' . get_class($this) . '__get_template_fields', $this->_template_fields, $this );
594
		$template_fields = apply_filters( 'FHEE__EE_messenger__get_template_fields', $template_fields, $this );
595
		return $template_fields;
596
	}
597
598
599
600
601
	/** SETUP METHODS **/
602
	/**
603
	 * The following method doesn't NEED to be used by child classes but might be modified by the specific messenger
604
	 * @param string $item
605
	 * @param mixed $value
606
	 */
607
	protected function _set_template_value($item, $value) {
608
		if ( array_key_exists($item, $this->_template_fields) ) {
609
			$prop = '_' . $item;
610
			$this->{$prop}= $value;
611
		}
612
	}
613
614
	/**
615
	 * Sets up the message for sending.
616
	 * @param  EE_message $message the message object that contains details about the message.
617
	 * @param EE_message_type $message_type The message type object used in combination with this messenger to generate the provided message.
618
	 * @return bool Very important that all messengers return bool for successful send or not.  Error messages can be
619
	 *              added to EE_Error.
620
	 */
621
	public function send_message( $message, EE_message_type $message_type ) {
622
		$this->_validate_and_setup( $message );
623
		$this->_incoming_message_type = $message_type;
624
		return $this->_send_message();
625
	}
626
627
628
629
	/**
630
	 * Sets up and returns message preview
631
	 * @param  EE_Message $message incoming message object
632
	 * @param EE_message_type $message_type This is whatever message type was used in combination with this messenger to generate the message.
633
	 * @param  bool   $send    true we will actually use the _send method (for test sends). FALSE we just return preview
634
	 * @return string          return the message html content
635
	 */
636
	public function get_preview( EE_Message $message, EE_message_type $message_type, $send = false ) {
637
		$this->_validate_and_setup( $message );
638
639
		$this->_incoming_message_type = $message_type;
640
641
		if ( $send ) {
642
			//are we overriding any existing template fields?
643
			$settings = $this->get_existing_test_settings();
644
			if ( ! empty( $settings ) ) {
645
				foreach ( $settings as $field => $value ) {
646
					$this->_set_template_value( $field, $value );
647
				}
648
			}
649
		}
650
651
		//enqueue preview js so that any links/buttons on the page are disabled.
652
		if ( ! $send ) {
653
			// the below may seem like duplication.  However, typically if a messenger enqueues scripts/styles,
654
			// it deregisters all existing wp scripts and styles first.  So the second hook ensures our previewer still gets setup.
655
			add_action( 'admin_enqueue_scripts', array( $this, 'add_preview_script' ), 10 );
656
			add_action( 'wp_enqueue_scripts', array( $this, 'add_preview_script' ), 10 );
657
			add_action( 'AHEE__EE_messenger__enqueue_scripts_styles', array( $this, 'add_preview_script' ), 10 );
658
		}
659
660
		return $send ? $this->_send_message() : $this->_preview();
661
	}
662
663
664
665
666
	/**
667
	 * Callback for enqueue_scripts so that we setup the preview script for all previews.
668
	 *
669
	 * @since 4.5.0
670
	 *
671
	 * @return void
672
	 */
673
	public function add_preview_script() {
674
		//error message
675
		EE_Registry::$i18n_js_strings[ 'links_disabled' ] = __( 'All the links on this page have been disabled because this is a generated preview message for the purpose of ensuring layout, style, and content setup.  To test generated links, you must trigger an actual message notification.', 'event_espresso' );
676
		wp_register_script( 'ee-messages-preview-js', EE_LIBRARIES_URL . 'messages/messenger/assets/js/ee-messages-preview.js', array( 'jquery' ), EVENT_ESPRESSO_VERSION, true );
677
		wp_localize_script( 'ee-messages-preview-js', 'eei18n', EE_Registry::$i18n_js_strings );
678
		wp_enqueue_script( 'ee-messages-preview-js' );
679
	}
680
681
682
683
684
	/**
685
	 * simply validates the incoming message object and then sets up the properties for the messenger
686
	 * @param  EE_Message $message
687
	 * @throws EE_Error
688
	 */
689
	protected function _validate_and_setup( EE_Message $message ) {
690
		$template_pack = $message->get_template_pack();
691
		$variation = $message->get_template_pack_variation();
692
693
		//verify we have the required template pack value on the $message object.
694
		if ( ! $template_pack instanceof EE_Messages_Template_Pack ) {
695
			throw new EE_Error( __('Incoming $message object must have an EE_Messages_Template_Pack object available.', 'event_espresso' ) );
696
		}
697
698
		$this->_tmp_pack = $template_pack;
699
700
		$this->_variation = $variation ? $variation : 'default';
701
702
		$template_fields = $this->get_template_fields();
703
704
		foreach ( $template_fields as $template => $value ) {
705
			if ( $template !== 'extra' ) {
706
				$column_value = $message->get_field_or_extra_meta( 'MSG_' . $template );
707
				$message_template_value = $column_value ? $column_value : null;
708
				$this->_set_template_value( $template, $message_template_value );
709
			}
710
		}
711
	}
712
713
714
715
	/**
716
	 * Utility method for child classes to get the contents of a template file and return
717
	 *
718
	 * We're assuming the child messenger class has already setup template args!
719
	 * @param  bool $preview if true we use the preview wrapper otherwise we use main wrapper.
720
	 * @return string
721
	 * @throws \EE_Error
722
	 */
723
	protected function _get_main_template( $preview = FALSE ) {
724
		$type = $preview ? 'preview' : 'main';
725
726
		$wrapper_template = $this->_tmp_pack->get_wrapper( $this->name, $type );
727
728
		//check file exists and is readable
729
		if ( !is_readable( $wrapper_template ) )
730
			throw new EE_Error( sprintf( __('Unable to access the template file for the %s messenger main content wrapper.  The location being attempted is %s.', 'event_espresso' ), ucwords($this->label['singular']) , $wrapper_template ) );
731
732
		//add message type to template args
733
		$this->_template_args['message_type'] = $this->_incoming_message_type;
734
735
		return EEH_Template::display_template( $wrapper_template, $this->_template_args, TRUE );
736
	}
737
738
739
740
	/**
741
	 * set the _test_settings_fields property
742
	 *
743
	 * @access protected
744
	 * @return void
745
	 */
746
	protected function _set_test_settings_fields() {
747
		$this->_test_settings_fields = array();
748
	}
749
750
751
752
	/**
753
	 * return the _test_settings_fields property
754
	 * @return array
755
	 */
756
	public function get_test_settings_fields() {
757
		return $this->_test_settings_fields;
758
	}
759
760
761
762
763
	/**
764
	 * This just returns any existing test settings that might be saved in the database
765
	 *
766
	 * @access public
767
	 * @return array
768
	 */
769
	public function get_existing_test_settings() {
770
		/** @var EE_Message_Resource_Manager $Message_Resource_Manager */
771
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
772
		$settings = $Message_Resource_Manager->get_active_messengers_option();
773
		return isset( $settings[ $this->name ]['test_settings'] ) ? $settings[ $this->name ]['test_settings'] : array();
774
	}
775
776
777
778
	/**
779
	 * All this does is set the existing test settings (in the db) for the messenger
780
	 *
781
	 * @access public
782
	 * @param $settings
783
	 * @return bool success/fail
784
	 */
785
	public function set_existing_test_settings( $settings ) {
786
		/** @var EE_Message_Resource_Manager $Message_Resource_Manager */
787
		$Message_Resource_Manager = EE_Registry::instance()->load_lib( 'Message_Resource_Manager' );
788
		$existing = $Message_Resource_Manager->get_active_messengers_option();
789
		$existing[ $this->name ]['test_settings'] = $settings;
790
		return $Message_Resource_Manager->update_active_messengers_option( $existing );
791
	}
792
793
794
795
	/**
796
	 * This just returns the field label for a given field setup in the _template_fields property.
797
	 *
798
	 * @since 	4.3.0
799
	 *
800
	 * @param string $field The field to retrieve the label for
801
	 * @return string        	  The label
802
	 */
803
	public function get_field_label( $field ) {
804
		//first let's see if the field requests is in the top level array.
805
		if ( isset( $this->_template_fields[$field] ) && !empty( $this->_template_fields[$field]['label'] ) )
806
			return $this->_template[$field]['label'];
0 ignored issues
show
Bug introduced by
The property _template does not seem to exist. Did you mean _template_fields?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
807
808
		//nope so let's look in the extra array to see if it's there HOWEVER if the field exists as a top level index in the extra array then we know the label is in the 'main' index.
809
		if ( isset( $this->_template_fields['extra'] ) && !empty( $this->_template_fields['extra'][$field] ) && !empty( $this->_template_fields['extra'][$field]['main']['label'] )  )
810
			return $this->_template_fields['extra'][$field]['main']['label'];
811
812
		//now it's possible this field may just be existing in any of the extra array items.
813
		if ( !empty( $this->_template_fields['extra'] ) && is_array( $this->_template_fields['extra'] ) ) {
814
			foreach ( $this->_template_fields['extra'] as $main_field => $subfields ) {
815
				if ( !is_array( $subfields ) )
816
					continue;
817
				if ( isset( $subfields[$field] ) && !empty( $subfields[$field]['label'] ) )
818
					return $subfields[$field]['label'];
819
			}
820
		}
821
822
		//if we made it here then there's no label set so let's just return the $field.
823
		return $field;
824
	}
825
826
827
828
829
	/**
830
	 * This is a method called from EE_messages when this messenger is a generating messenger and the sending messenger is a different messenger.  Child messengers can set hooks for the sending messenger to callback on if necessary (i.e. swap out css files or something else).
831
	 *
832
	 * @since 4.5.0
833
	 *
834
	 * @param string $sending_messenger_name the name of the sending messenger so we only set the hooks needed.
835
	 *
836
	 * @return void
837
	 */
838
	public function do_secondary_messenger_hooks( $sending_messenger_name ) {
0 ignored issues
show
Unused Code introduced by
The parameter $sending_messenger_name 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...
839
		return;
840
	}
841
842
843
}
844
// end EE_messenger class
845