Completed
Pull Request — master (#1424)
by Darren
08:35
created
admin_pages/messages/Messages_Admin_Page.core.php 2 patches
Indentation   +4525 added lines, -4525 removed lines patch added patch discarded remove patch
@@ -19,2625 +19,2625 @@  discard block
 block discarded – undo
19 19
 class Messages_Admin_Page extends EE_Admin_Page
20 20
 {
21 21
 
22
-    /**
23
-     * @type EE_Message_Resource_Manager $_message_resource_manager
24
-     */
25
-    protected $_message_resource_manager;
26
-
27
-    /**
28
-     * @type string $_active_message_type_name
29
-     */
30
-    protected $_active_message_type_name = '';
31
-
32
-    /**
33
-     * @type EE_messenger $_active_messenger
34
-     */
35
-    protected $_active_messenger;
36
-    protected $_activate_state;
37
-    protected $_activate_meta_box_type;
38
-    protected $_current_message_meta_box;
39
-    protected $_current_message_meta_box_object;
40
-    protected $_context_switcher;
41
-    protected $_shortcodes = array();
42
-    protected $_active_messengers = array();
43
-    protected $_active_message_types = array();
44
-
45
-    /**
46
-     * @var EE_Message_Template_Group $_message_template_group
47
-     */
48
-    protected $_message_template_group;
49
-    protected $_m_mt_settings = array();
50
-
51
-
52
-    /**
53
-     * This is set via the _set_message_template_group method and holds whatever the template pack for the group is.
54
-     * IF there is no group then it gets automatically set to the Default template pack.
55
-     *
56
-     * @since 4.5.0
57
-     *
58
-     * @var EE_Messages_Template_Pack
59
-     */
60
-    protected $_template_pack;
61
-
62
-
63
-    /**
64
-     * This is set via the _set_message_template_group method and holds whatever the template pack variation for the
65
-     * group is.  If there is no group then it automatically gets set to default.
66
-     *
67
-     * @since 4.5.0
68
-     *
69
-     * @var string
70
-     */
71
-    protected $_variation;
72
-
73
-
74
-    /**
75
-     * @param bool $routing
76
-     * @throws EE_Error
77
-     */
78
-    public function __construct($routing = true)
79
-    {
80
-        // make sure messages autoloader is running
81
-        EED_Messages::set_autoloaders();
82
-        parent::__construct($routing);
83
-    }
84
-
85
-
86
-    protected function _init_page_props()
87
-    {
88
-        $this->page_slug = EE_MSG_PG_SLUG;
89
-        $this->page_label = esc_html__('Messages Settings', 'event_espresso');
90
-        $this->_admin_base_url = EE_MSG_ADMIN_URL;
91
-        $this->_admin_base_path = EE_MSG_ADMIN;
92
-
93
-        $this->_activate_state = isset($this->_req_data['activate_state']) ? (array) $this->_req_data['activate_state']
94
-            : array();
95
-
96
-        $this->_active_messenger = isset($this->_req_data['messenger']) ? $this->_req_data['messenger'] : null;
97
-        $this->_load_message_resource_manager();
98
-    }
99
-
100
-
101
-    /**
102
-     * loads messenger objects into the $_active_messengers property (so we can access the needed methods)
103
-     *
104
-     * @throws EE_Error
105
-     * @throws InvalidDataTypeException
106
-     * @throws InvalidInterfaceException
107
-     * @throws InvalidArgumentException
108
-     * @throws ReflectionException
109
-     */
110
-    protected function _load_message_resource_manager()
111
-    {
112
-        $this->_message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
113
-    }
114
-
115
-
116
-    /**
117
-     * @deprecated 4.9.9.rc.014
118
-     * @return array
119
-     * @throws EE_Error
120
-     * @throws InvalidArgumentException
121
-     * @throws InvalidDataTypeException
122
-     * @throws InvalidInterfaceException
123
-     */
124
-    public function get_messengers_for_list_table()
125
-    {
126
-        EE_Error::doing_it_wrong(
127
-            __METHOD__,
128
-            sprintf(
129
-                esc_html__(
130
-                    'This method is no longer in use.  There is no replacement for it. The method was used to generate a set of values for use in creating a messenger filter dropdown which is now generated differently via %s',
131
-                    'event_espresso'
132
-                ),
133
-                'Messages_Admin_Page::get_messengers_select_input()'
134
-            ),
135
-            '4.9.9.rc.014'
136
-        );
137
-
138
-        $m_values = array();
139
-        $active_messengers = EEM_Message::instance()->get_all(array('group_by' => 'MSG_messenger'));
140
-        // setup messengers for selects
141
-        $i = 1;
142
-        foreach ($active_messengers as $active_messenger) {
143
-            if ($active_messenger instanceof EE_Message) {
144
-                $m_values[ $i ]['id'] = $active_messenger->messenger();
145
-                $m_values[ $i ]['text'] = ucwords($active_messenger->messenger_label());
146
-                $i++;
147
-            }
148
-        }
149
-
150
-        return $m_values;
151
-    }
152
-
153
-
154
-    /**
155
-     * @deprecated 4.9.9.rc.014
156
-     * @return array
157
-     * @throws EE_Error
158
-     * @throws InvalidArgumentException
159
-     * @throws InvalidDataTypeException
160
-     * @throws InvalidInterfaceException
161
-     */
162
-    public function get_message_types_for_list_table()
163
-    {
164
-        EE_Error::doing_it_wrong(
165
-            __METHOD__,
166
-            sprintf(
167
-                esc_html__(
168
-                    'This method is no longer in use.  There is no replacement for it. The method was used to generate a set of values for use in creating a message type filter dropdown which is now generated differently via %s',
169
-                    'event_espresso'
170
-                ),
171
-                'Messages_Admin_Page::get_message_types_select_input()'
172
-            ),
173
-            '4.9.9.rc.014'
174
-        );
175
-
176
-        $mt_values = array();
177
-        $active_messages = EEM_Message::instance()->get_all(array('group_by' => 'MSG_message_type'));
178
-        $i = 1;
179
-        foreach ($active_messages as $active_message) {
180
-            if ($active_message instanceof EE_Message) {
181
-                $mt_values[ $i ]['id'] = $active_message->message_type();
182
-                $mt_values[ $i ]['text'] = ucwords($active_message->message_type_label());
183
-                $i++;
184
-            }
185
-        }
186
-
187
-        return $mt_values;
188
-    }
189
-
190
-
191
-    /**
192
-     * @deprecated 4.9.9.rc.014
193
-     * @return array
194
-     * @throws EE_Error
195
-     * @throws InvalidArgumentException
196
-     * @throws InvalidDataTypeException
197
-     * @throws InvalidInterfaceException
198
-     */
199
-    public function get_contexts_for_message_types_for_list_table()
200
-    {
201
-        EE_Error::doing_it_wrong(
202
-            __METHOD__,
203
-            sprintf(
204
-                esc_html__(
205
-                    'This method is no longer in use.  There is no replacement for it. The method was used to generate a set of values for use in creating a message type context filter dropdown which is now generated differently via %s',
206
-                    'event_espresso'
207
-                ),
208
-                'Messages_Admin_Page::get_contexts_for_message_types_select_input()'
209
-            ),
210
-            '4.9.9.rc.014'
211
-        );
212
-
213
-        $contexts = array();
214
-        $active_message_contexts = EEM_Message::instance()->get_all(array('group_by' => 'MSG_context'));
215
-        foreach ($active_message_contexts as $active_message) {
216
-            if ($active_message instanceof EE_Message) {
217
-                $message_type = $active_message->message_type_object();
218
-                if ($message_type instanceof EE_message_type) {
219
-                    $message_type_contexts = $message_type->get_contexts();
220
-                    foreach ($message_type_contexts as $context => $context_details) {
221
-                        $contexts[ $context ] = $context_details['label'];
222
-                    }
223
-                }
224
-            }
225
-        }
226
-
227
-        return $contexts;
228
-    }
229
-
230
-
231
-    /**
232
-     * Generate select input with provided messenger options array.
233
-     *
234
-     * @param array $messenger_options Array of messengers indexed by messenger slug and values are the messenger
235
-     *                                 labels.
236
-     * @return string
237
-     * @throws EE_Error
238
-     */
239
-    public function get_messengers_select_input($messenger_options)
240
-    {
241
-        // if empty or just one value then just return an empty string
242
-        if (empty($messenger_options)
243
-            || ! is_array($messenger_options)
244
-            || count($messenger_options) === 1
245
-        ) {
246
-            return '';
247
-        }
248
-        // merge in default
249
-        $messenger_options = array_merge(
250
-            array('none_selected' => esc_html__('Show All Messengers', 'event_espresso')),
251
-            $messenger_options
252
-        );
253
-        $input = new EE_Select_Input(
254
-            $messenger_options,
255
-            array(
256
-                'html_name'  => 'ee_messenger_filter_by',
257
-                'html_id'    => 'ee_messenger_filter_by',
258
-                'html_class' => 'wide',
259
-                'default'    => isset($this->_req_data['ee_messenger_filter_by'])
260
-                    ? sanitize_title($this->_req_data['ee_messenger_filter_by'])
261
-                    : 'none_selected',
262
-            )
263
-        );
264
-
265
-        return $input->get_html_for_input();
266
-    }
267
-
268
-
269
-    /**
270
-     * Generate select input with provided message type options array.
271
-     *
272
-     * @param array $message_type_options Array of message types indexed by message type slug, and values are the
273
-     *                                    message type labels
274
-     * @return string
275
-     * @throws EE_Error
276
-     */
277
-    public function get_message_types_select_input($message_type_options)
278
-    {
279
-        // if empty or count of options is 1 then just return an empty string
280
-        if (empty($message_type_options)
281
-            || ! is_array($message_type_options)
282
-            || count($message_type_options) === 1
283
-        ) {
284
-            return '';
285
-        }
286
-        // merge in default
287
-        $message_type_options = array_merge(
288
-            array('none_selected' => esc_html__('Show All Message Types', 'event_espresso')),
289
-            $message_type_options
290
-        );
291
-        $input = new EE_Select_Input(
292
-            $message_type_options,
293
-            array(
294
-                'html_name'  => 'ee_message_type_filter_by',
295
-                'html_id'    => 'ee_message_type_filter_by',
296
-                'html_class' => 'wide',
297
-                'default'    => isset($this->_req_data['ee_message_type_filter_by'])
298
-                    ? sanitize_title($this->_req_data['ee_message_type_filter_by'])
299
-                    : 'none_selected',
300
-            )
301
-        );
302
-
303
-        return $input->get_html_for_input();
304
-    }
305
-
306
-
307
-    /**
308
-     * Generate select input with provide message type contexts array.
309
-     *
310
-     * @param array $context_options Array of message type contexts indexed by context slug, and values are the
311
-     *                               context label.
312
-     * @return string
313
-     * @throws EE_Error
314
-     */
315
-    public function get_contexts_for_message_types_select_input($context_options)
316
-    {
317
-        // if empty or count of options is one then just return empty string
318
-        if (empty($context_options)
319
-            || ! is_array($context_options)
320
-            || count($context_options) === 1
321
-        ) {
322
-            return '';
323
-        }
324
-        // merge in default
325
-        $context_options = array_merge(
326
-            array('none_selected' => esc_html__('Show all Contexts', 'event_espresso')),
327
-            $context_options
328
-        );
329
-        $input = new EE_Select_Input(
330
-            $context_options,
331
-            array(
332
-                'html_name'  => 'ee_context_filter_by',
333
-                'html_id'    => 'ee_context_filter_by',
334
-                'html_class' => 'wide',
335
-                'default'    => isset($this->_req_data['ee_context_filter_by'])
336
-                    ? sanitize_title($this->_req_data['ee_context_filter_by'])
337
-                    : 'none_selected',
338
-            )
339
-        );
340
-
341
-        return $input->get_html_for_input();
342
-    }
343
-
344
-
345
-    protected function _ajax_hooks()
346
-    {
347
-        add_action('wp_ajax_activate_messenger', array($this, 'activate_messenger_toggle'));
348
-        add_action('wp_ajax_activate_mt', array($this, 'activate_mt_toggle'));
349
-        add_action('wp_ajax_ee_msgs_save_settings', array($this, 'save_settings'));
350
-        add_action('wp_ajax_ee_msgs_update_mt_form', array($this, 'update_mt_form'));
351
-        add_action('wp_ajax_switch_template_pack', array($this, 'switch_template_pack'));
352
-        add_action('wp_ajax_toggle_context_template', array($this, 'toggle_context_template'));
353
-    }
354
-
355
-
356
-    protected function _define_page_props()
357
-    {
358
-        $this->_admin_page_title = $this->page_label;
359
-        $this->_labels = array(
360
-            'buttons'    => array(
361
-                'add'    => esc_html__('Add New Message Template', 'event_espresso'),
362
-                'edit'   => esc_html__('Edit Message Template', 'event_espresso'),
363
-                'delete' => esc_html__('Delete Message Template', 'event_espresso'),
364
-            ),
365
-            'publishbox' => esc_html__('Update Actions', 'event_espresso'),
366
-        );
367
-    }
368
-
369
-
370
-    /**
371
-     *        an array for storing key => value pairs of request actions and their corresponding methods
372
-     *
373
-     * @access protected
374
-     * @return void
375
-     */
376
-    protected function _set_page_routes()
377
-    {
378
-        $grp_id = ! empty($this->_req_data['GRP_ID']) && ! is_array($this->_req_data['GRP_ID'])
379
-            ? $this->_req_data['GRP_ID']
380
-            : 0;
381
-        $grp_id = empty($grp_id) && ! empty($this->_req_data['id'])
382
-            ? $this->_req_data['id']
383
-            : $grp_id;
384
-        $msg_id = ! empty($this->_req_data['MSG_ID']) && ! is_array($this->_req_data['MSG_ID'])
385
-            ? $this->_req_data['MSG_ID']
386
-            : 0;
387
-
388
-        $this->_page_routes = array(
389
-            'default'                          => array(
390
-                'func'       => '_message_queue_list_table',
391
-                'capability' => 'ee_read_global_messages',
392
-            ),
393
-            'global_mtps'                      => array(
394
-                'func'       => '_ee_default_messages_overview_list_table',
395
-                'capability' => 'ee_read_global_messages',
396
-            ),
397
-            'custom_mtps'                      => array(
398
-                'func'       => '_custom_mtps_preview',
399
-                'capability' => 'ee_read_messages',
400
-            ),
401
-            'add_new_message_template'         => array(
402
-                'func'       => '_add_message_template',
403
-                'capability' => 'ee_edit_messages',
404
-                'noheader'   => true,
405
-            ),
406
-            'edit_message_template'            => array(
407
-                'func'       => '_edit_message_template',
408
-                'capability' => 'ee_edit_message',
409
-                'obj_id'     => $grp_id,
410
-            ),
411
-            'preview_message'                  => array(
412
-                'func'               => '_preview_message',
413
-                'capability'         => 'ee_read_message',
414
-                'obj_id'             => $grp_id,
415
-                'noheader'           => true,
416
-                'headers_sent_route' => 'display_preview_message',
417
-            ),
418
-            'display_preview_message'          => array(
419
-                'func'       => '_display_preview_message',
420
-                'capability' => 'ee_read_message',
421
-                'obj_id'     => $grp_id,
422
-            ),
423
-            'insert_message_template'          => array(
424
-                'func'       => '_insert_or_update_message_template',
425
-                'capability' => 'ee_edit_messages',
426
-                'args'       => array('new_template' => true),
427
-                'noheader'   => true,
428
-            ),
429
-            'update_message_template'          => array(
430
-                'func'       => '_insert_or_update_message_template',
431
-                'capability' => 'ee_edit_message',
432
-                'obj_id'     => $grp_id,
433
-                'args'       => array('new_template' => false),
434
-                'noheader'   => true,
435
-            ),
436
-            'trash_message_template'           => array(
437
-                'func'       => '_trash_or_restore_message_template',
438
-                'capability' => 'ee_delete_message',
439
-                'obj_id'     => $grp_id,
440
-                'args'       => array('trash' => true, 'all' => true),
441
-                'noheader'   => true,
442
-            ),
443
-            'trash_message_template_context'   => array(
444
-                'func'       => '_trash_or_restore_message_template',
445
-                'capability' => 'ee_delete_message',
446
-                'obj_id'     => $grp_id,
447
-                'args'       => array('trash' => true),
448
-                'noheader'   => true,
449
-            ),
450
-            'restore_message_template'         => array(
451
-                'func'       => '_trash_or_restore_message_template',
452
-                'capability' => 'ee_delete_message',
453
-                'obj_id'     => $grp_id,
454
-                'args'       => array('trash' => false, 'all' => true),
455
-                'noheader'   => true,
456
-            ),
457
-            'restore_message_template_context' => array(
458
-                'func'       => '_trash_or_restore_message_template',
459
-                'capability' => 'ee_delete_message',
460
-                'obj_id'     => $grp_id,
461
-                'args'       => array('trash' => false),
462
-                'noheader'   => true,
463
-            ),
464
-            'delete_message_template'          => array(
465
-                'func'       => '_delete_message_template',
466
-                'capability' => 'ee_delete_message',
467
-                'obj_id'     => $grp_id,
468
-                'noheader'   => true,
469
-            ),
470
-            'reset_to_default'                 => array(
471
-                'func'       => '_reset_to_default_template',
472
-                'capability' => 'ee_edit_message',
473
-                'obj_id'     => $grp_id,
474
-                'noheader'   => true,
475
-            ),
476
-            'settings'                         => array(
477
-                'func'       => '_settings',
478
-                'capability' => 'manage_options',
479
-            ),
480
-            'update_global_settings'           => array(
481
-                'func'       => '_update_global_settings',
482
-                'capability' => 'manage_options',
483
-                'noheader'   => true,
484
-            ),
485
-            'generate_now'                     => array(
486
-                'func'       => '_generate_now',
487
-                'capability' => 'ee_send_message',
488
-                'noheader'   => true,
489
-            ),
490
-            'generate_and_send_now'            => array(
491
-                'func'       => '_generate_and_send_now',
492
-                'capability' => 'ee_send_message',
493
-                'noheader'   => true,
494
-            ),
495
-            'queue_for_resending'              => array(
496
-                'func'       => '_queue_for_resending',
497
-                'capability' => 'ee_send_message',
498
-                'noheader'   => true,
499
-            ),
500
-            'send_now'                         => array(
501
-                'func'       => '_send_now',
502
-                'capability' => 'ee_send_message',
503
-                'noheader'   => true,
504
-            ),
505
-            'delete_ee_message'                => array(
506
-                'func'       => '_delete_ee_messages',
507
-                'capability' => 'ee_delete_messages',
508
-                'noheader'   => true,
509
-            ),
510
-            'delete_ee_messages'               => array(
511
-                'func'       => '_delete_ee_messages',
512
-                'capability' => 'ee_delete_messages',
513
-                'noheader'   => true,
514
-                'obj_id'     => $msg_id,
515
-            ),
516
-        );
517
-    }
518
-
519
-
520
-    protected function _set_page_config()
521
-    {
522
-        $this->_page_config = array(
523
-            'default'                  => array(
524
-                'nav'           => array(
525
-                    'label' => esc_html__('Message Activity', 'event_espresso'),
526
-                    'order' => 10,
527
-                ),
528
-                'list_table'    => 'EE_Message_List_Table',
529
-                // 'qtips' => array( 'EE_Message_List_Table_Tips' ),
530
-                'require_nonce' => false,
531
-            ),
532
-            'global_mtps'              => array(
533
-                'nav'           => array(
534
-                    'label' => esc_html__('Default Message Templates', 'event_espresso'),
535
-                    'order' => 20,
536
-                ),
537
-                'list_table'    => 'Messages_Template_List_Table',
538
-                'help_tabs'     => array(
539
-                    'messages_overview_help_tab'                                => array(
540
-                        'title'    => esc_html__('Messages Overview', 'event_espresso'),
541
-                        'filename' => 'messages_overview',
542
-                    ),
543
-                    'messages_overview_messages_table_column_headings_help_tab' => array(
544
-                        'title'    => esc_html__('Messages Table Column Headings', 'event_espresso'),
545
-                        'filename' => 'messages_overview_table_column_headings',
546
-                    ),
547
-                    'messages_overview_messages_filters_help_tab'               => array(
548
-                        'title'    => esc_html__('Message Filters', 'event_espresso'),
549
-                        'filename' => 'messages_overview_filters',
550
-                    ),
551
-                    'messages_overview_messages_views_help_tab'                 => array(
552
-                        'title'    => esc_html__('Message Views', 'event_espresso'),
553
-                        'filename' => 'messages_overview_views',
554
-                    ),
555
-                    'message_overview_message_types_help_tab'                   => array(
556
-                        'title'    => esc_html__('Message Types', 'event_espresso'),
557
-                        'filename' => 'messages_overview_types',
558
-                    ),
559
-                    'messages_overview_messengers_help_tab'                     => array(
560
-                        'title'    => esc_html__('Messengers', 'event_espresso'),
561
-                        'filename' => 'messages_overview_messengers',
562
-                    ),
563
-                ),
564
-                'help_tour'     => array('Messages_Overview_Help_Tour'),
565
-                'require_nonce' => false,
566
-            ),
567
-            'custom_mtps'              => array(
568
-                'nav'           => array(
569
-                    'label' => esc_html__('Custom Message Templates', 'event_espresso'),
570
-                    'order' => 30,
571
-                ),
572
-                'help_tabs'     => array(),
573
-                'help_tour'     => array(),
574
-                'require_nonce' => false,
575
-            ),
576
-            'add_new_message_template' => array(
577
-                'nav'           => array(
578
-                    'label'      => esc_html__('Add New Message Templates', 'event_espresso'),
579
-                    'order'      => 5,
580
-                    'persistent' => false,
581
-                ),
582
-                'require_nonce' => false,
583
-            ),
584
-            'edit_message_template'    => array(
585
-                'labels'        => array(
586
-                    'buttons'    => array(
587
-                        'reset' => esc_html__('Reset Templates', 'event_espresso'),
588
-                    ),
589
-                    'publishbox' => esc_html__('Update Actions', 'event_espresso'),
590
-                ),
591
-                'nav'           => array(
592
-                    'label'      => esc_html__('Edit Message Templates', 'event_espresso'),
593
-                    'order'      => 5,
594
-                    'persistent' => false,
595
-                    'url'        => '',
596
-                ),
597
-                'metaboxes'     => array('_publish_post_box', '_register_edit_meta_boxes'),
598
-                'has_metaboxes' => true,
599
-                'help_tour'     => array('Message_Templates_Edit_Help_Tour'),
600
-                'help_tabs'     => array(
601
-                    'edit_message_template'            => array(
602
-                        'title'    => esc_html__('Message Template Editor', 'event_espresso'),
603
-                        'callback' => 'edit_message_template_help_tab',
604
-                    ),
605
-                    'message_templates_help_tab'       => array(
606
-                        'title'    => esc_html__('Message Templates', 'event_espresso'),
607
-                        'filename' => 'messages_templates',
608
-                    ),
609
-                    'message_template_shortcodes'      => array(
610
-                        'title'    => esc_html__('Message Shortcodes', 'event_espresso'),
611
-                        'callback' => 'message_template_shortcodes_help_tab',
612
-                    ),
613
-                    'message_preview_help_tab'         => array(
614
-                        'title'    => esc_html__('Message Preview', 'event_espresso'),
615
-                        'filename' => 'messages_preview',
616
-                    ),
617
-                    'messages_overview_other_help_tab' => array(
618
-                        'title'    => esc_html__('Messages Other', 'event_espresso'),
619
-                        'filename' => 'messages_overview_other',
620
-                    ),
621
-                ),
622
-                'require_nonce' => false,
623
-            ),
624
-            'display_preview_message'  => array(
625
-                'nav'           => array(
626
-                    'label'      => esc_html__('Message Preview', 'event_espresso'),
627
-                    'order'      => 5,
628
-                    'url'        => '',
629
-                    'persistent' => false,
630
-                ),
631
-                'help_tabs'     => array(
632
-                    'preview_message' => array(
633
-                        'title'    => esc_html__('About Previews', 'event_espresso'),
634
-                        'callback' => 'preview_message_help_tab',
635
-                    ),
636
-                ),
637
-                'require_nonce' => false,
638
-            ),
639
-            'settings'                 => array(
640
-                'nav'           => array(
641
-                    'label' => esc_html__('Settings', 'event_espresso'),
642
-                    'order' => 40,
643
-                ),
644
-                'metaboxes'     => array('_messages_settings_metaboxes'),
645
-                'help_tabs'     => array(
646
-                    'messages_settings_help_tab'               => array(
647
-                        'title'    => esc_html__('Messages Settings', 'event_espresso'),
648
-                        'filename' => 'messages_settings',
649
-                    ),
650
-                    'messages_settings_message_types_help_tab' => array(
651
-                        'title'    => esc_html__('Activating / Deactivating Message Types', 'event_espresso'),
652
-                        'filename' => 'messages_settings_message_types',
653
-                    ),
654
-                    'messages_settings_messengers_help_tab'    => array(
655
-                        'title'    => esc_html__('Activating / Deactivating Messengers', 'event_espresso'),
656
-                        'filename' => 'messages_settings_messengers',
657
-                    ),
658
-                ),
659
-                'help_tour'     => array('Messages_Settings_Help_Tour'),
660
-                'require_nonce' => false,
661
-            ),
662
-        );
663
-    }
664
-
665
-
666
-    protected function _add_screen_options()
667
-    {
668
-        // todo
669
-    }
670
-
671
-
672
-    protected function _add_screen_options_global_mtps()
673
-    {
674
-        /**
675
-         * Note: the reason for the value swap here on $this->_admin_page_title is because $this->_per_page_screen_options
676
-         * uses the $_admin_page_title property and we want different outputs in the different spots.
677
-         */
678
-        $page_title = $this->_admin_page_title;
679
-        $this->_admin_page_title = esc_html__('Global Message Templates', 'event_espresso');
680
-        $this->_per_page_screen_option();
681
-        $this->_admin_page_title = $page_title;
682
-    }
683
-
684
-
685
-    protected function _add_screen_options_default()
686
-    {
687
-        $this->_admin_page_title = esc_html__('Message Activity', 'event_espresso');
688
-        $this->_per_page_screen_option();
689
-    }
690
-
691
-
692
-    // none of the below group are currently used for Messages
693
-    protected function _add_feature_pointers()
694
-    {
695
-    }
696
-
697
-    public function admin_init()
698
-    {
699
-    }
700
-
701
-    public function admin_notices()
702
-    {
703
-    }
704
-
705
-    public function admin_footer_scripts()
706
-    {
707
-    }
708
-
709
-
710
-    public function messages_help_tab()
711
-    {
712
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_help_tab.template.php');
713
-    }
714
-
715
-
716
-    public function messengers_help_tab()
717
-    {
718
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messenger_help_tab.template.php');
719
-    }
720
-
721
-
722
-    public function message_types_help_tab()
723
-    {
724
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_type_help_tab.template.php');
725
-    }
726
-
727
-
728
-    public function messages_overview_help_tab()
729
-    {
730
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_overview_help_tab.template.php');
731
-    }
732
-
733
-
734
-    public function message_templates_help_tab()
735
-    {
736
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_templates_help_tab.template.php');
737
-    }
738
-
739
-
740
-    public function edit_message_template_help_tab()
741
-    {
742
-        $args['img1'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/editor.png' . '" alt="'
743
-                        . esc_attr__('Editor Title', 'event_espresso')
744
-                        . '" />';
745
-        $args['img2'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/switch-context.png' . '" alt="'
746
-                        . esc_attr__('Context Switcher and Preview', 'event_espresso')
747
-                        . '" />';
748
-        $args['img3'] = '<img class="left" src="' . EE_MSG_ASSETS_URL . 'images/form-fields.png' . '" alt="'
749
-                        . esc_attr__('Message Template Form Fields', 'event_espresso')
750
-                        . '" />';
751
-        $args['img4'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/shortcodes-metabox.png' . '" alt="'
752
-                        . esc_attr__('Shortcodes Metabox', 'event_espresso')
753
-                        . '" />';
754
-        $args['img5'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/publish-meta-box.png' . '" alt="'
755
-                        . esc_attr__('Publish Metabox', 'event_espresso')
756
-                        . '" />';
757
-        EEH_Template::display_template(
758
-            EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_templates_editor_help_tab.template.php',
759
-            $args
760
-        );
761
-    }
762
-
763
-
764
-    public function message_template_shortcodes_help_tab()
765
-    {
766
-        $this->_set_shortcodes();
767
-        $args['shortcodes'] = $this->_shortcodes;
768
-        EEH_Template::display_template(
769
-            EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_shortcodes_help_tab.template.php',
770
-            $args
771
-        );
772
-    }
773
-
774
-
775
-    public function preview_message_help_tab()
776
-    {
777
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_preview_help_tab.template.php');
778
-    }
779
-
780
-
781
-    public function settings_help_tab()
782
-    {
783
-        $args['img1'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-active.png'
784
-                        . '" alt="' . esc_attr__('Active Email Tab', 'event_espresso') . '" />';
785
-        $args['img2'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-inactive.png'
786
-                        . '" alt="' . esc_attr__('Inactive Email Tab', 'event_espresso') . '" />';
787
-        $args['img3'] = '<div class="switch">'
788
-                        . '<input class="ee-on-off-toggle ee-toggle-round-flat"'
789
-                        . ' type="checkbox" checked="checked">'
790
-                        . '<label for="ee-on-off-toggle-on"></label>'
791
-                        . '</div>';
792
-        $args['img4'] = '<div class="switch">'
793
-                        . '<input class="ee-on-off-toggle ee-toggle-round-flat"'
794
-                        . ' type="checkbox">'
795
-                        . '<label for="ee-on-off-toggle-on"></label>'
796
-                        . '</div>';
797
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_settings_help_tab.template.php', $args);
798
-    }
799
-
800
-
801
-    public function load_scripts_styles()
802
-    {
803
-        wp_register_style('espresso_ee_msg', EE_MSG_ASSETS_URL . 'ee_message_admin.css', EVENT_ESPRESSO_VERSION);
804
-        wp_enqueue_style('espresso_ee_msg');
805
-
806
-        wp_register_script(
807
-            'ee-messages-settings',
808
-            EE_MSG_ASSETS_URL . 'ee-messages-settings.js',
809
-            array('jquery-ui-droppable', 'ee-serialize-full-array'),
810
-            EVENT_ESPRESSO_VERSION,
811
-            true
812
-        );
813
-        wp_register_script(
814
-            'ee-msg-list-table-js',
815
-            EE_MSG_ASSETS_URL . 'ee_message_admin_list_table.js',
816
-            array('ee-dialog'),
817
-            EVENT_ESPRESSO_VERSION
818
-        );
819
-    }
820
-
821
-
822
-    public function load_scripts_styles_default()
823
-    {
824
-        wp_enqueue_script('ee-msg-list-table-js');
825
-    }
826
-
827
-
828
-    public function wp_editor_css($mce_css)
829
-    {
830
-        // if we're on the edit_message_template route
831
-        if ($this->_req_action === 'edit_message_template' && $this->_active_messenger instanceof EE_messenger) {
832
-            $message_type_name = $this->_active_message_type_name;
833
-
834
-            // we're going to REPLACE the existing mce css
835
-            // we need to get the css file location from the active messenger
836
-            $mce_css = $this->_active_messenger->get_variation(
837
-                $this->_template_pack,
838
-                $message_type_name,
839
-                true,
840
-                'wpeditor',
841
-                $this->_variation
842
-            );
843
-        }
844
-
845
-        return $mce_css;
846
-    }
847
-
848
-
849
-    public function load_scripts_styles_edit_message_template()
850
-    {
851
-
852
-        $this->_set_shortcodes();
853
-
854
-        EE_Registry::$i18n_js_strings['confirm_default_reset'] = sprintf(
855
-            esc_html__(
856
-                'Are you sure you want to reset the %s %s message templates?  Remember continuing will reset the templates for all contexts in this messenger and message type group.',
857
-                'event_espresso'
858
-            ),
859
-            $this->_message_template_group->messenger_obj()->label['singular'],
860
-            $this->_message_template_group->message_type_obj()->label['singular']
861
-        );
862
-        EE_Registry::$i18n_js_strings['confirm_switch_template_pack'] = esc_html__(
863
-            'Switching the template pack for a messages template will reset the content for the template so the new layout is loaded.  Any custom content in the existing template will be lost. Are you sure you wish to do this?',
864
-            'event_espresso'
865
-        );
866
-        EE_Registry::$i18n_js_strings['server_error'] = esc_html__(
867
-            'An unknown error occurred on the server while attempting to process your request. Please refresh the page and try again or contact support.',
868
-            'event_espresso'
869
-        );
870
-
871
-        wp_register_script(
872
-            'ee_msgs_edit_js',
873
-            EE_MSG_ASSETS_URL . 'ee_message_editor.js',
874
-            array('jquery'),
875
-            EVENT_ESPRESSO_VERSION
876
-        );
877
-
878
-        wp_enqueue_script('ee_admin_js');
879
-        wp_enqueue_script('ee_msgs_edit_js');
880
-
881
-        // add in special css for tiny_mce
882
-        add_filter('mce_css', array($this, 'wp_editor_css'));
883
-    }
884
-
885
-
886
-    public function load_scripts_styles_display_preview_message()
887
-    {
888
-
889
-        $this->_set_message_template_group();
890
-
891
-        if (isset($this->_req_data['messenger'])) {
892
-            $this->_active_messenger = $this->_message_resource_manager->get_active_messenger(
893
-                $this->_req_data['messenger']
894
-            );
895
-        }
896
-
897
-        $message_type_name = isset($this->_req_data['message_type']) ? $this->_req_data['message_type'] : '';
898
-
899
-
900
-        wp_enqueue_style(
901
-            'espresso_preview_css',
902
-            $this->_active_messenger->get_variation(
903
-                $this->_template_pack,
904
-                $message_type_name,
905
-                true,
906
-                'preview',
907
-                $this->_variation
908
-            )
909
-        );
910
-    }
911
-
912
-
913
-    public function load_scripts_styles_settings()
914
-    {
915
-        wp_register_style(
916
-            'ee-message-settings',
917
-            EE_MSG_ASSETS_URL . 'ee_message_settings.css',
918
-            array(),
919
-            EVENT_ESPRESSO_VERSION
920
-        );
921
-        wp_enqueue_style('ee-text-links');
922
-        wp_enqueue_style('ee-message-settings');
923
-        wp_enqueue_script('ee-messages-settings');
924
-    }
925
-
926
-
927
-    /**
928
-     * set views array for List Table
929
-     */
930
-    public function _set_list_table_views_global_mtps()
931
-    {
932
-        $this->_views = array(
933
-            'in_use' => array(
934
-                'slug'  => 'in_use',
935
-                'label' => esc_html__('In Use', 'event_espresso'),
936
-                'count' => 0,
937
-            ),
938
-        );
939
-    }
940
-
941
-
942
-    /**
943
-     * Set views array for the Custom Template List Table
944
-     */
945
-    public function _set_list_table_views_custom_mtps()
946
-    {
947
-        $this->_set_list_table_views_global_mtps();
948
-        $this->_views['in_use']['bulk_action'] = array(
949
-            'trash_message_template' => esc_html__('Move to Trash', 'event_espresso'),
950
-        );
951
-    }
952
-
953
-
954
-    /**
955
-     * set views array for message queue list table
956
-     *
957
-     * @throws InvalidDataTypeException
958
-     * @throws InvalidInterfaceException
959
-     * @throws InvalidArgumentException
960
-     * @throws EE_Error
961
-     * @throws ReflectionException
962
-     */
963
-    public function _set_list_table_views_default()
964
-    {
965
-        EE_Registry::instance()->load_helper('Template');
966
-
967
-        $common_bulk_actions = EE_Registry::instance()->CAP->current_user_can(
968
-            'ee_send_message',
969
-            'message_list_table_bulk_actions'
970
-        )
971
-            ? array(
972
-                'generate_now'          => esc_html__('Generate Now', 'event_espresso'),
973
-                'generate_and_send_now' => esc_html__('Generate and Send Now', 'event_espresso'),
974
-                'queue_for_resending'   => esc_html__('Queue for Resending', 'event_espresso'),
975
-                'send_now'              => esc_html__('Send Now', 'event_espresso'),
976
-            )
977
-            : array();
978
-
979
-        $delete_bulk_action = EE_Registry::instance()->CAP->current_user_can(
980
-            'ee_delete_messages',
981
-            'message_list_table_bulk_actions'
982
-        )
983
-            ? array('delete_ee_messages' => esc_html__('Delete Messages', 'event_espresso'))
984
-            : array();
985
-
986
-
987
-        $this->_views = array(
988
-            'all' => array(
989
-                'slug'        => 'all',
990
-                'label'       => esc_html__('All', 'event_espresso'),
991
-                'count'       => 0,
992
-                'bulk_action' => array_merge($common_bulk_actions, $delete_bulk_action),
993
-            ),
994
-        );
995
-
996
-
997
-        foreach (EEM_Message::instance()->all_statuses() as $status) {
998
-            if ($status === EEM_Message::status_debug_only && ! EEM_Message::debug()) {
999
-                continue;
1000
-            }
1001
-            $status_bulk_actions = $common_bulk_actions;
1002
-            // unset bulk actions not applying to status
1003
-            if (! empty($status_bulk_actions)) {
1004
-                switch ($status) {
1005
-                    case EEM_Message::status_idle:
1006
-                    case EEM_Message::status_resend:
1007
-                        $status_bulk_actions['send_now'] = $common_bulk_actions['send_now'];
1008
-                        break;
1009
-
1010
-                    case EEM_Message::status_failed:
1011
-                    case EEM_Message::status_debug_only:
1012
-                    case EEM_Message::status_messenger_executing:
1013
-                        $status_bulk_actions = array();
1014
-                        break;
1015
-
1016
-                    case EEM_Message::status_incomplete:
1017
-                        unset($status_bulk_actions['queue_for_resending'], $status_bulk_actions['send_now']);
1018
-                        break;
1019
-
1020
-                    case EEM_Message::status_retry:
1021
-                    case EEM_Message::status_sent:
1022
-                        unset($status_bulk_actions['generate_now'], $status_bulk_actions['generate_and_send_now']);
1023
-                        break;
1024
-                }
1025
-            }
1026
-
1027
-            // skip adding messenger executing status to views because it will be included with the Failed view.
1028
-            if ($status === EEM_Message::status_messenger_executing) {
1029
-                continue;
1030
-            }
1031
-
1032
-            $this->_views[ strtolower($status) ] = array(
1033
-                'slug'        => strtolower($status),
1034
-                'label'       => EEH_Template::pretty_status($status, false, 'sentence'),
1035
-                'count'       => 0,
1036
-                'bulk_action' => array_merge($status_bulk_actions, $delete_bulk_action),
1037
-            );
1038
-        }
1039
-    }
1040
-
1041
-
1042
-    protected function _ee_default_messages_overview_list_table()
1043
-    {
1044
-        $this->_admin_page_title = esc_html__('Default Message Templates', 'event_espresso');
1045
-        $this->display_admin_list_table_page_with_no_sidebar();
1046
-    }
1047
-
1048
-
1049
-    protected function _message_queue_list_table()
1050
-    {
1051
-        $this->_search_btn_label = esc_html__('Message Activity', 'event_espresso');
1052
-        $this->_template_args['per_column'] = 6;
1053
-        $this->_template_args['after_list_table'] = $this->_display_legend($this->_message_legend_items());
1054
-        $this->_template_args['before_list_table'] = '<h3>'
1055
-                                                     . EEM_Message::instance()->get_pretty_label_for_results()
1056
-                                                     . '</h3>';
1057
-        $this->display_admin_list_table_page_with_no_sidebar();
1058
-    }
1059
-
1060
-
1061
-    protected function _message_legend_items()
1062
-    {
1063
-
1064
-        $action_css_classes = EEH_MSG_Template::get_message_action_icons();
1065
-        $action_items = array();
1066
-
1067
-        foreach ($action_css_classes as $action_item => $action_details) {
1068
-            if ($action_item === 'see_notifications_for') {
1069
-                continue;
1070
-            }
1071
-            $action_items[ $action_item ] = array(
1072
-                'class' => $action_details['css_class'],
1073
-                'desc'  => $action_details['label'],
1074
-            );
1075
-        }
1076
-
1077
-        /** @type array $status_items status legend setup */
1078
-        $status_items = array(
1079
-            'sent_status'                => array(
1080
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_sent,
1081
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_sent, false, 'sentence'),
1082
-            ),
1083
-            'idle_status'                => array(
1084
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_idle,
1085
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_idle, false, 'sentence'),
1086
-            ),
1087
-            'failed_status'              => array(
1088
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_failed,
1089
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_failed, false, 'sentence'),
1090
-            ),
1091
-            'messenger_executing_status' => array(
1092
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_messenger_executing,
1093
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_messenger_executing, false, 'sentence'),
1094
-            ),
1095
-            'resend_status'              => array(
1096
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_resend,
1097
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_resend, false, 'sentence'),
1098
-            ),
1099
-            'incomplete_status'          => array(
1100
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_incomplete,
1101
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_incomplete, false, 'sentence'),
1102
-            ),
1103
-            'retry_status'               => array(
1104
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_retry,
1105
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_retry, false, 'sentence'),
1106
-            ),
1107
-        );
1108
-        if (EEM_Message::debug()) {
1109
-            $status_items['debug_only_status'] = array(
1110
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_debug_only,
1111
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_debug_only, false, 'sentence'),
1112
-            );
1113
-        }
1114
-
1115
-        return array_merge($action_items, $status_items);
1116
-    }
1117
-
1118
-
1119
-    protected function _custom_mtps_preview()
1120
-    {
1121
-        $this->_admin_page_title = esc_html__('Custom Message Templates (Preview)', 'event_espresso');
1122
-        $this->_template_args['preview_img'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/custom_mtps_preview.png"'
1123
-                                               . ' alt="' . esc_attr__(
1124
-                                                   'Preview Custom Message Templates screenshot',
1125
-                                                   'event_espresso'
1126
-                                               ) . '" />';
1127
-        $this->_template_args['preview_text'] = '<strong>'
1128
-                                                . esc_html__(
1129
-                                                    'Custom Message Templates is a feature that is only available in the premium version of Event Espresso 4 which is available with a support license purchase on EventEspresso.com. With the Custom Message Templates feature, you are able to create custom message templates and assign them on a per-event basis.',
1130
-                                                    'event_espresso'
1131
-                                                )
1132
-                                                . '</strong>';
1133
-
1134
-        $this->display_admin_caf_preview_page('custom_message_types', false);
1135
-    }
1136
-
1137
-
1138
-    /**
1139
-     * get_message_templates
1140
-     * This gets all the message templates for listing on the overview list.
1141
-     *
1142
-     * @access public
1143
-     * @param int    $perpage the amount of templates groups to show per page
1144
-     * @param string $type    the current _view we're getting templates for
1145
-     * @param bool   $count   return count?
1146
-     * @param bool   $all     disregard any paging info (get all data);
1147
-     * @param bool   $global  whether to return just global (true) or custom templates (false)
1148
-     * @return array
1149
-     * @throws EE_Error
1150
-     * @throws InvalidArgumentException
1151
-     * @throws InvalidDataTypeException
1152
-     * @throws InvalidInterfaceException
1153
-     */
1154
-    public function get_message_templates(
1155
-        $perpage = 10,
1156
-        $type = 'in_use',
1157
-        $count = false,
1158
-        $all = false,
1159
-        $global = true
1160
-    ) {
1161
-
1162
-        $MTP = EEM_Message_Template_Group::instance();
1163
-
1164
-        $this->_req_data['orderby'] = empty($this->_req_data['orderby']) ? 'GRP_ID' : $this->_req_data['orderby'];
1165
-        $orderby = $this->_req_data['orderby'];
1166
-
1167
-        $order = (isset($this->_req_data['order']) && ! empty($this->_req_data['order']))
1168
-            ? $this->_req_data['order']
1169
-            : 'ASC';
1170
-
1171
-        $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged'])
1172
-            ? $this->_req_data['paged']
1173
-            : 1;
1174
-        $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage'])
1175
-            ? $this->_req_data['perpage']
1176
-            : $perpage;
1177
-
1178
-        $offset = ($current_page - 1) * $per_page;
1179
-        $limit = $all ? null : array($offset, $per_page);
1180
-
1181
-
1182
-        // options will match what is in the _views array property
1183
-        switch ($type) {
1184
-            case 'in_use':
1185
-                $templates = $MTP->get_all_active_message_templates($orderby, $order, $limit, $count, $global, true);
1186
-                break;
1187
-            default:
1188
-                $templates = $MTP->get_all_trashed_grouped_message_templates($orderby, $order, $limit, $count, $global);
1189
-        }
1190
-
1191
-        return $templates;
1192
-    }
1193
-
1194
-
1195
-    /**
1196
-     * filters etc might need a list of installed message_types
1197
-     *
1198
-     * @return array an array of message type objects
1199
-     */
1200
-    public function get_installed_message_types()
1201
-    {
1202
-        $installed_message_types = $this->_message_resource_manager->installed_message_types();
1203
-        $installed = array();
1204
-
1205
-        foreach ($installed_message_types as $message_type) {
1206
-            $installed[ $message_type->name ] = $message_type;
1207
-        }
1208
-
1209
-        return $installed;
1210
-    }
1211
-
1212
-
1213
-    /**
1214
-     * _add_message_template
1215
-     *
1216
-     * This is used when creating a custom template. All Custom Templates start based off another template.
1217
-     *
1218
-     * @param string $message_type
1219
-     * @param string $messenger
1220
-     * @param string $GRP_ID
1221
-     *
1222
-     * @throws EE_error
1223
-     */
1224
-    protected function _add_message_template($message_type = '', $messenger = '', $GRP_ID = '')
1225
-    {
1226
-        // set values override any request data
1227
-        $message_type = ! empty($message_type) ? $message_type : '';
1228
-        $message_type = empty($message_type) && ! empty($this->_req_data['message_type'])
1229
-            ? $this->_req_data['message_type']
1230
-            : $message_type;
1231
-
1232
-        $messenger = ! empty($messenger) ? $messenger : '';
1233
-        $messenger = empty($messenger) && ! empty($this->_req_data['messenger'])
1234
-            ? $this->_req_data['messenger']
1235
-            : $messenger;
1236
-
1237
-        $GRP_ID = ! empty($GRP_ID) ? $GRP_ID : '';
1238
-        $GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : $GRP_ID;
1239
-
1240
-        // we need messenger and message type.  They should be coming from the event editor. If not here then return error
1241
-        if (empty($message_type) || empty($messenger)) {
1242
-            throw new EE_Error(
1243
-                esc_html__(
1244
-                    'Sorry, but we can\'t create new templates because we\'re missing the messenger or message type',
1245
-                    'event_espresso'
1246
-                )
1247
-            );
1248
-        }
1249
-
1250
-        // we need the GRP_ID for the template being used as the base for the new template
1251
-        if (empty($GRP_ID)) {
1252
-            throw new EE_Error(
1253
-                esc_html__(
1254
-                    'In order to create a custom message template the GRP_ID of the template being used as a base is needed',
1255
-                    'event_espresso'
1256
-                )
1257
-            );
1258
-        }
1259
-
1260
-        // let's just make sure the template gets generated!
1261
-
1262
-        // we need to reassign some variables for what the insert is expecting
1263
-        $this->_req_data['MTP_messenger'] = $messenger;
1264
-        $this->_req_data['MTP_message_type'] = $message_type;
1265
-        $this->_req_data['GRP_ID'] = $GRP_ID;
1266
-        $this->_insert_or_update_message_template(true);
1267
-    }
1268
-
1269
-
1270
-    /**
1271
-     * public wrapper for the _add_message_template method
1272
-     *
1273
-     * @param string $message_type     message type slug
1274
-     * @param string $messenger        messenger slug
1275
-     * @param int    $GRP_ID           GRP_ID for the related message template group this new template will be based
1276
-     *                                 off of.
1277
-     * @throws EE_error
1278
-     */
1279
-    public function add_message_template($message_type, $messenger, $GRP_ID)
1280
-    {
1281
-        $this->_add_message_template($message_type, $messenger, $GRP_ID);
1282
-    }
1283
-
1284
-
1285
-    /**
1286
-     * _edit_message_template
1287
-     *
1288
-     * @access protected
1289
-     * @return void
1290
-     * @throws InvalidIdentifierException
1291
-     * @throws DomainException
1292
-     * @throws EE_Error
1293
-     * @throws InvalidArgumentException
1294
-     * @throws ReflectionException
1295
-     * @throws InvalidDataTypeException
1296
-     * @throws InvalidInterfaceException
1297
-     */
1298
-    protected function _edit_message_template()
1299
-    {
1300
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1301
-        $template_fields = '';
1302
-        $sidebar_fields = '';
1303
-        // we filter the tinyMCE settings to remove the validation since message templates by their nature will not have
1304
-        // valid html in the templates.
1305
-        add_filter('tiny_mce_before_init', array($this, 'filter_tinymce_init'), 10, 2);
1306
-
1307
-        $GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id'])
1308
-            ? absint($this->_req_data['id'])
1309
-            : false;
1310
-
1311
-        $this->_set_shortcodes(); // this also sets the _message_template property.
1312
-        $message_template_group = $this->_message_template_group;
1313
-        $c_label = $message_template_group->context_label();
1314
-        $c_config = $message_template_group->contexts_config();
1315
-
1316
-        reset($c_config);
1317
-        $context = isset($this->_req_data['context']) && ! empty($this->_req_data['context'])
1318
-            ? strtolower($this->_req_data['context'])
1319
-            : key($c_config);
1320
-
1321
-
1322
-        if (empty($GRP_ID)) {
1323
-            $action = 'insert_message_template';
1324
-            $edit_message_template_form_url = add_query_arg(
1325
-                array('action' => $action, 'noheader' => true),
1326
-                EE_MSG_ADMIN_URL
1327
-            );
1328
-        } else {
1329
-            $action = 'update_message_template';
1330
-            $edit_message_template_form_url = add_query_arg(
1331
-                array('action' => $action, 'noheader' => true),
1332
-                EE_MSG_ADMIN_URL
1333
-            );
1334
-        }
1335
-
1336
-        // set active messenger for this view
1337
-        $this->_active_messenger = $this->_message_resource_manager->get_active_messenger(
1338
-            $message_template_group->messenger()
1339
-        );
1340
-        $this->_active_message_type_name = $message_template_group->message_type();
1341
-
1342
-
1343
-        // Do we have any validation errors?
1344
-        $validators = $this->_get_transient();
1345
-        $v_fields = ! empty($validators) ? array_keys($validators) : array();
1346
-
1347
-
1348
-        // we need to assemble the title from Various details
1349
-        $context_label = sprintf(
1350
-            esc_html__('(%s %s)', 'event_espresso'),
1351
-            $c_config[ $context ]['label'],
1352
-            ucwords($c_label['label'])
1353
-        );
1354
-
1355
-        $title = sprintf(
1356
-            esc_html__(' %s %s Template %s', 'event_espresso'),
1357
-            ucwords($message_template_group->messenger_obj()->label['singular']),
1358
-            ucwords($message_template_group->message_type_obj()->label['singular']),
1359
-            $context_label
1360
-        );
1361
-
1362
-        $this->_template_args['GRP_ID'] = $GRP_ID;
1363
-        $this->_template_args['message_template'] = $message_template_group;
1364
-        $this->_template_args['is_extra_fields'] = false;
1365
-
1366
-
1367
-        // let's get EEH_MSG_Template so we can get template form fields
1368
-        $template_field_structure = EEH_MSG_Template::get_fields(
1369
-            $message_template_group->messenger(),
1370
-            $message_template_group->message_type()
1371
-        );
1372
-
1373
-        if (! $template_field_structure) {
1374
-            $template_field_structure = false;
1375
-            $template_fields = esc_html__(
1376
-                'There was an error in assembling the fields for this display (you should see an error message)',
1377
-                'event_espresso'
1378
-            );
1379
-        }
1380
-
1381
-
1382
-        $message_templates = $message_template_group->context_templates();
1383
-
1384
-
1385
-        // if we have the extra key.. then we need to remove the content index from the template_field_structure as it
1386
-        // will get handled in the "extra" array.
1387
-        if (is_array($template_field_structure[ $context ]) && isset($template_field_structure[ $context ]['extra'])) {
1388
-            foreach ($template_field_structure[ $context ]['extra'] as $reference_field => $new_fields) {
1389
-                unset($template_field_structure[ $context ][ $reference_field ]);
1390
-            }
1391
-        }
1392
-
1393
-        // let's loop through the template_field_structure and actually assemble the input fields!
1394
-        if (! empty($template_field_structure)) {
1395
-            foreach ($template_field_structure[ $context ] as $template_field => $field_setup_array) {
1396
-                // if this is an 'extra' template field then we need to remove any existing fields that are keyed up in
1397
-                // the extra array and reset them.
1398
-                if ($template_field === 'extra') {
1399
-                    $this->_template_args['is_extra_fields'] = true;
1400
-                    foreach ($field_setup_array as $reference_field => $new_fields_array) {
1401
-                        $message_template = $message_templates[ $context ][ $reference_field ];
1402
-                        $content = $message_template instanceof EE_Message_Template
1403
-                            ? $message_template->get('MTP_content')
1404
-                            : '';
1405
-                        foreach ($new_fields_array as $extra_field => $extra_array) {
1406
-                            // let's verify if we need this extra field via the shortcodes parameter.
1407
-                            $continue = false;
1408
-                            if (isset($extra_array['shortcodes_required'])) {
1409
-                                foreach ((array) $extra_array['shortcodes_required'] as $shortcode) {
1410
-                                    if (! array_key_exists($shortcode, $this->_shortcodes)) {
1411
-                                        $continue = true;
1412
-                                    }
1413
-                                }
1414
-                                if ($continue) {
1415
-                                    continue;
1416
-                                }
1417
-                            }
1418
-
1419
-                            $field_id = $reference_field
1420
-                                        . '-'
1421
-                                        . $extra_field
1422
-                                        . '-content';
1423
-                            $template_form_fields[ $field_id ] = $extra_array;
1424
-                            $template_form_fields[ $field_id ]['name'] = 'MTP_template_fields['
1425
-                                                                         . $reference_field
1426
-                                                                         . '][content]['
1427
-                                                                         . $extra_field . ']';
1428
-                            $css_class = isset($extra_array['css_class'])
1429
-                                ? $extra_array['css_class']
1430
-                                : '';
1431
-
1432
-                            $template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
1433
-                                                                              && in_array($extra_field, $v_fields, true)
1434
-                                                                              &&
1435
-                                                                              (
1436
-                                                                                  is_array($validators[ $extra_field ])
1437
-                                                                                  && isset($validators[ $extra_field ]['msg'])
1438
-                                                                              )
1439
-                                ? 'validate-error ' . $css_class
1440
-                                : $css_class;
1441
-
1442
-                            $template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
1443
-                                                                          && isset($content[ $extra_field ])
1444
-                                ? $content[ $extra_field ]
1445
-                                : '';
1446
-
1447
-                            // do we have a validation error?  if we do then let's use that value instead
1448
-                            $template_form_fields[ $field_id ]['value'] = isset($validators[ $extra_field ])
1449
-                                ? $validators[ $extra_field ]['value']
1450
-                                : $template_form_fields[ $field_id ]['value'];
1451
-
1452
-
1453
-                            $template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
1454
-
1455
-                            // shortcode selector
1456
-                            $field_name_to_use = $extra_field === 'main'
1457
-                                ? 'content'
1458
-                                : $extra_field;
1459
-                            $template_form_fields[ $field_id ]['append_content'] = $this->_get_shortcode_selector(
1460
-                                $field_name_to_use,
1461
-                                $field_id
1462
-                            );
1463
-
1464
-                            if (isset($extra_array['input']) && $extra_array['input'] === 'wp_editor') {
1465
-                                // we want to decode the entities
1466
-                                $template_form_fields[ $field_id ]['value'] = $template_form_fields[ $field_id ]['value'];
1467
-                            }/**/
1468
-                        }
1469
-                        $templatefield_MTP_id = $reference_field . '-MTP_ID';
1470
-                        $templatefield_templatename_id = $reference_field . '-name';
1471
-
1472
-                        $template_form_fields[ $templatefield_MTP_id ] = array(
1473
-                            'name'       => 'MTP_template_fields[' . $reference_field . '][MTP_ID]',
1474
-                            'label'      => null,
1475
-                            'input'      => 'hidden',
1476
-                            'type'       => 'int',
1477
-                            'required'   => false,
1478
-                            'validation' => false,
1479
-                            'value'      => ! empty($message_templates) ? $message_template->ID() : '',
1480
-                            'css_class'  => '',
1481
-                            'format'     => '%d',
1482
-                            'db-col'     => 'MTP_ID',
1483
-                        );
1484
-
1485
-                        $template_form_fields[ $templatefield_templatename_id ] = array(
1486
-                            'name'       => 'MTP_template_fields[' . $reference_field . '][name]',
1487
-                            'label'      => null,
1488
-                            'input'      => 'hidden',
1489
-                            'type'       => 'string',
1490
-                            'required'   => false,
1491
-                            'validation' => true,
1492
-                            'value'      => $reference_field,
1493
-                            'css_class'  => '',
1494
-                            'format'     => '%s',
1495
-                            'db-col'     => 'MTP_template_field',
1496
-                        );
1497
-                    }
1498
-                    continue; // skip the next stuff, we got the necessary fields here for this dataset.
1499
-                } else {
1500
-                    $field_id = $template_field . '-content';
1501
-                    $template_form_fields[ $field_id ] = $field_setup_array;
1502
-                    $template_form_fields[ $field_id ]['name'] = 'MTP_template_fields[' . $template_field . '][content]';
1503
-                    $message_template = isset($message_templates[ $context ][ $template_field ])
1504
-                        ? $message_templates[ $context ][ $template_field ]
1505
-                        : null;
1506
-                    $template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
1507
-                                                                  && is_array($message_templates[ $context ])
1508
-                                                                  && $message_template instanceof EE_Message_Template
1509
-                        ? $message_template->get('MTP_content')
1510
-                        : '';
1511
-
1512
-                    // do we have a validator error for this field?  if we do then we'll use that value instead
1513
-                    $template_form_fields[ $field_id ]['value'] = isset($validators[ $template_field ])
1514
-                        ? $validators[ $template_field ]['value']
1515
-                        : $template_form_fields[ $field_id ]['value'];
1516
-
1517
-
1518
-                    $template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
1519
-                    $css_class = isset($field_setup_array['css_class'])
1520
-                        ? $field_setup_array['css_class']
1521
-                        : '';
1522
-                    $template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
1523
-                                                                      && in_array($template_field, $v_fields, true)
1524
-                                                                      && isset($validators[ $template_field ]['msg'])
1525
-                        ? 'validate-error ' . $css_class
1526
-                        : $css_class;
1527
-
1528
-                    // shortcode selector
1529
-                    $template_form_fields[ $field_id ]['append_content'] = $this->_get_shortcode_selector(
1530
-                        $template_field,
1531
-                        $field_id
1532
-                    );
1533
-                }
1534
-
1535
-                // k took care of content field(s) now let's take care of others.
1536
-
1537
-                $templatefield_MTP_id = $template_field . '-MTP_ID';
1538
-                $templatefield_field_templatename_id = $template_field . '-name';
1539
-
1540
-                // foreach template field there are actually two form fields created
1541
-                $template_form_fields[ $templatefield_MTP_id ] = array(
1542
-                    'name'       => 'MTP_template_fields[' . $template_field . '][MTP_ID]',
1543
-                    'label'      => null,
1544
-                    'input'      => 'hidden',
1545
-                    'type'       => 'int',
1546
-                    'required'   => false,
1547
-                    'validation' => true,
1548
-                    'value'      => $message_template instanceof EE_Message_Template ? $message_template->ID() : '',
1549
-                    'css_class'  => '',
1550
-                    'format'     => '%d',
1551
-                    'db-col'     => 'MTP_ID',
1552
-                );
1553
-
1554
-                $template_form_fields[ $templatefield_field_templatename_id ] = array(
1555
-                    'name'       => 'MTP_template_fields[' . $template_field . '][name]',
1556
-                    'label'      => null,
1557
-                    'input'      => 'hidden',
1558
-                    'type'       => 'string',
1559
-                    'required'   => false,
1560
-                    'validation' => true,
1561
-                    'value'      => $template_field,
1562
-                    'css_class'  => '',
1563
-                    'format'     => '%s',
1564
-                    'db-col'     => 'MTP_template_field',
1565
-                );
1566
-            }
1567
-
1568
-            // add other fields
1569
-            $template_form_fields['ee-msg-current-context'] = array(
1570
-                'name'       => 'MTP_context',
1571
-                'label'      => null,
1572
-                'input'      => 'hidden',
1573
-                'type'       => 'string',
1574
-                'required'   => false,
1575
-                'validation' => true,
1576
-                'value'      => $context,
1577
-                'css_class'  => '',
1578
-                'format'     => '%s',
1579
-                'db-col'     => 'MTP_context',
1580
-            );
1581
-
1582
-            $template_form_fields['ee-msg-grp-id'] = array(
1583
-                'name'       => 'GRP_ID',
1584
-                'label'      => null,
1585
-                'input'      => 'hidden',
1586
-                'type'       => 'int',
1587
-                'required'   => false,
1588
-                'validation' => true,
1589
-                'value'      => $GRP_ID,
1590
-                'css_class'  => '',
1591
-                'format'     => '%d',
1592
-                'db-col'     => 'GRP_ID',
1593
-            );
1594
-
1595
-            $template_form_fields['ee-msg-messenger'] = array(
1596
-                'name'       => 'MTP_messenger',
1597
-                'label'      => null,
1598
-                'input'      => 'hidden',
1599
-                'type'       => 'string',
1600
-                'required'   => false,
1601
-                'validation' => true,
1602
-                'value'      => $message_template_group->messenger(),
1603
-                'css_class'  => '',
1604
-                'format'     => '%s',
1605
-                'db-col'     => 'MTP_messenger',
1606
-            );
1607
-
1608
-            $template_form_fields['ee-msg-message-type'] = array(
1609
-                'name'       => 'MTP_message_type',
1610
-                'label'      => null,
1611
-                'input'      => 'hidden',
1612
-                'type'       => 'string',
1613
-                'required'   => false,
1614
-                'validation' => true,
1615
-                'value'      => $message_template_group->message_type(),
1616
-                'css_class'  => '',
1617
-                'format'     => '%s',
1618
-                'db-col'     => 'MTP_message_type',
1619
-            );
1620
-
1621
-            $sidebar_form_fields['ee-msg-is-global'] = array(
1622
-                'name'       => 'MTP_is_global',
1623
-                'label'      => esc_html__('Global Template', 'event_espresso'),
1624
-                'input'      => 'hidden',
1625
-                'type'       => 'int',
1626
-                'required'   => false,
1627
-                'validation' => true,
1628
-                'value'      => $message_template_group->get('MTP_is_global'),
1629
-                'css_class'  => '',
1630
-                'format'     => '%d',
1631
-                'db-col'     => 'MTP_is_global',
1632
-            );
1633
-
1634
-            $sidebar_form_fields['ee-msg-is-override'] = array(
1635
-                'name'       => 'MTP_is_override',
1636
-                'label'      => esc_html__('Override all custom', 'event_espresso'),
1637
-                'input'      => $message_template_group->is_global() ? 'checkbox' : 'hidden',
1638
-                'type'       => 'int',
1639
-                'required'   => false,
1640
-                'validation' => true,
1641
-                'value'      => $message_template_group->get('MTP_is_override'),
1642
-                'css_class'  => '',
1643
-                'format'     => '%d',
1644
-                'db-col'     => 'MTP_is_override',
1645
-            );
1646
-
1647
-            $sidebar_form_fields['ee-msg-is-active'] = array(
1648
-                'name'       => 'MTP_is_active',
1649
-                'label'      => esc_html__('Active Template', 'event_espresso'),
1650
-                'input'      => 'hidden',
1651
-                'type'       => 'int',
1652
-                'required'   => false,
1653
-                'validation' => true,
1654
-                'value'      => $message_template_group->is_active(),
1655
-                'css_class'  => '',
1656
-                'format'     => '%d',
1657
-                'db-col'     => 'MTP_is_active',
1658
-            );
1659
-
1660
-            $sidebar_form_fields['ee-msg-deleted'] = array(
1661
-                'name'       => 'MTP_deleted',
1662
-                'label'      => null,
1663
-                'input'      => 'hidden',
1664
-                'type'       => 'int',
1665
-                'required'   => false,
1666
-                'validation' => true,
1667
-                'value'      => $message_template_group->get('MTP_deleted'),
1668
-                'css_class'  => '',
1669
-                'format'     => '%d',
1670
-                'db-col'     => 'MTP_deleted',
1671
-            );
1672
-            $sidebar_form_fields['ee-msg-author'] = array(
1673
-                'name'       => 'MTP_user_id',
1674
-                'label'      => esc_html__('Author', 'event_espresso'),
1675
-                'input'      => 'hidden',
1676
-                'type'       => 'int',
1677
-                'required'   => false,
1678
-                'validation' => false,
1679
-                'value'      => $message_template_group->user(),
1680
-                'format'     => '%d',
1681
-                'db-col'     => 'MTP_user_id',
1682
-            );
1683
-
1684
-            $sidebar_form_fields['ee-msg-route'] = array(
1685
-                'name'  => 'action',
1686
-                'input' => 'hidden',
1687
-                'type'  => 'string',
1688
-                'value' => $action,
1689
-            );
1690
-
1691
-            $sidebar_form_fields['ee-msg-id'] = array(
1692
-                'name'  => 'id',
1693
-                'input' => 'hidden',
1694
-                'type'  => 'int',
1695
-                'value' => $GRP_ID,
1696
-            );
1697
-            $sidebar_form_fields['ee-msg-evt-nonce'] = array(
1698
-                'name'  => $action . '_nonce',
1699
-                'input' => 'hidden',
1700
-                'type'  => 'string',
1701
-                'value' => wp_create_nonce($action . '_nonce'),
1702
-            );
1703
-
1704
-            if (isset($this->_req_data['template_switch']) && $this->_req_data['template_switch']) {
1705
-                $sidebar_form_fields['ee-msg-template-switch'] = array(
1706
-                    'name'  => 'template_switch',
1707
-                    'input' => 'hidden',
1708
-                    'type'  => 'int',
1709
-                    'value' => 1,
1710
-                );
1711
-            }
1712
-
1713
-
1714
-            $template_fields = $this->_generate_admin_form_fields($template_form_fields);
1715
-            $sidebar_fields = $this->_generate_admin_form_fields($sidebar_form_fields);
1716
-        } //end if ( !empty($template_field_structure) )
1717
-
1718
-        // set extra content for publish box
1719
-        $this->_template_args['publish_box_extra_content'] = $sidebar_fields;
1720
-        $this->_set_publish_post_box_vars(
1721
-            'id',
1722
-            $GRP_ID,
1723
-            false,
1724
-            add_query_arg(
1725
-                array('action' => 'global_mtps'),
1726
-                $this->_admin_base_url
1727
-            )
1728
-        );
1729
-
1730
-        // add preview button
1731
-        $preview_url = parent::add_query_args_and_nonce(
1732
-            array(
1733
-                'message_type' => $message_template_group->message_type(),
1734
-                'messenger'    => $message_template_group->messenger(),
1735
-                'context'      => $context,
1736
-                'GRP_ID'       => $GRP_ID,
1737
-                'action'       => 'preview_message',
1738
-            ),
1739
-            $this->_admin_base_url
1740
-        );
1741
-        $preview_button = '<a href="' . $preview_url . '" class="button-secondary messages-preview-button">'
1742
-                          . esc_html__('Preview', 'event_espresso')
1743
-                          . '</a>';
1744
-
1745
-
1746
-        // setup context switcher
1747
-        $context_switcher_args = array(
1748
-            'page'    => 'espresso_messages',
1749
-            'action'  => 'edit_message_template',
1750
-            'id'      => $GRP_ID,
1751
-            'context' => $context,
1752
-            'extra'   => $preview_button,
1753
-        );
1754
-        $this->_set_context_switcher($message_template_group, $context_switcher_args);
1755
-
1756
-
1757
-        // main box
1758
-        $this->_template_args['template_fields'] = $template_fields;
1759
-        $this->_template_args['sidebar_box_id'] = 'details';
1760
-        $this->_template_args['action'] = $action;
1761
-        $this->_template_args['context'] = $context;
1762
-        $this->_template_args['edit_message_template_form_url'] = $edit_message_template_form_url;
1763
-        $this->_template_args['learn_more_about_message_templates_link'] =
1764
-            $this->_learn_more_about_message_templates_link();
1765
-
1766
-
1767
-        $this->_template_args['before_admin_page_content'] = $this->add_context_switcher();
1768
-        $this->_template_args['before_admin_page_content'] .= $this->add_active_context_element(
1769
-            $message_template_group,
1770
-            $context,
1771
-            $context_label
1772
-        );
1773
-        $this->_template_args['before_admin_page_content'] .= $this->_add_form_element_before();
1774
-        $this->_template_args['after_admin_page_content'] = $this->_add_form_element_after();
1775
-
1776
-        $this->_template_path = $this->_template_args['GRP_ID']
1777
-            ? EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_edit_meta_box.template.php'
1778
-            : EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_add_meta_box.template.php';
1779
-
1780
-        // send along EE_Message_Template_Group object for further template use.
1781
-        $this->_template_args['MTP'] = $message_template_group;
1782
-
1783
-        $this->_template_args['admin_page_content'] = EEH_Template::display_template(
1784
-            $this->_template_path,
1785
-            $this->_template_args,
1786
-            true
1787
-        );
1788
-
1789
-
1790
-        // finally, let's set the admin_page title
1791
-        $this->_admin_page_title = sprintf(__('Editing %s', 'event_espresso'), $title);
1792
-
1793
-
1794
-        // we need to take care of setting the shortcodes property for use elsewhere.
1795
-        $this->_set_shortcodes();
1796
-
1797
-
1798
-        // final template wrapper
1799
-        $this->display_admin_page_with_sidebar();
1800
-    }
1801
-
1802
-
1803
-    public function filter_tinymce_init($mceInit, $editor_id)
1804
-    {
1805
-        return $mceInit;
1806
-    }
1807
-
1808
-
1809
-    public function add_context_switcher()
1810
-    {
1811
-        return $this->_context_switcher;
1812
-    }
1813
-
1814
-
1815
-    /**
1816
-     * Adds the activation/deactivation toggle for the message template context.
1817
-     *
1818
-     * @param EE_Message_Template_Group $message_template_group
1819
-     * @param string                    $context
1820
-     * @param string                    $context_label
1821
-     * @return string
1822
-     * @throws DomainException
1823
-     * @throws EE_Error
1824
-     * @throws InvalidIdentifierException
1825
-     */
1826
-    protected function add_active_context_element(
1827
-        EE_Message_Template_Group $message_template_group,
1828
-        $context,
1829
-        $context_label
1830
-    ) {
1831
-        $template_args = array(
1832
-            'context'                   => $context,
1833
-            'nonce'                     => wp_create_nonce('activate_' . $context . '_toggle_nonce'),
1834
-            'is_active'                 => $message_template_group->is_context_active($context),
1835
-            'on_off_action'             => $message_template_group->is_context_active($context)
1836
-                ? 'context-off'
1837
-                : 'context-on',
1838
-            'context_label'             => str_replace(array('(', ')'), '', $context_label),
1839
-            'message_template_group_id' => $message_template_group->ID(),
1840
-        );
1841
-        return EEH_Template::display_template(
1842
-            EE_MSG_TEMPLATE_PATH . 'ee_msg_editor_active_context_element.template.php',
1843
-            $template_args,
1844
-            true
1845
-        );
1846
-    }
1847
-
1848
-
1849
-    /**
1850
-     * Ajax callback for `toggle_context_template` ajax action.
1851
-     * Handles toggling the message context on or off.
1852
-     *
1853
-     * @throws EE_Error
1854
-     * @throws InvalidArgumentException
1855
-     * @throws InvalidDataTypeException
1856
-     * @throws InvalidIdentifierException
1857
-     * @throws InvalidInterfaceException
1858
-     */
1859
-    public function toggle_context_template()
1860
-    {
1861
-        $success = true;
1862
-        // check for required data
1863
-        if (! isset(
1864
-            $this->_req_data['message_template_group_id'],
1865
-            $this->_req_data['context'],
1866
-            $this->_req_data['status']
1867
-        )) {
1868
-            EE_Error::add_error(
1869
-                esc_html__('Required data for doing this action is not available.', 'event_espresso'),
1870
-                __FILE__,
1871
-                __FUNCTION__,
1872
-                __LINE__
1873
-            );
1874
-            $success = false;
1875
-        }
1876
-
1877
-        $nonce = isset($this->_req_data['toggle_context_nonce'])
1878
-            ? sanitize_text_field($this->_req_data['toggle_context_nonce'])
1879
-            : '';
1880
-        $nonce_ref = 'activate_' . $this->_req_data['context'] . '_toggle_nonce';
1881
-        $this->_verify_nonce($nonce, $nonce_ref);
1882
-        $status = $this->_req_data['status'];
1883
-        if ($status !== 'off' && $status !== 'on') {
1884
-            EE_Error::add_error(
1885
-                sprintf(
1886
-                    esc_html__('The given status (%s) is not valid. Must be "off" or "on"', 'event_espresso'),
1887
-                    $this->_req_data['status']
1888
-                ),
1889
-                __FILE__,
1890
-                __FUNCTION__,
1891
-                __LINE__
1892
-            );
1893
-            $success = false;
1894
-        }
1895
-        $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID(
1896
-            $this->_req_data['message_template_group_id']
1897
-        );
1898
-        if (! $message_template_group instanceof EE_Message_Template_Group) {
1899
-            EE_Error::add_error(
1900
-                sprintf(
1901
-                    esc_html__(
1902
-                        'Unable to change the active state because the given id "%1$d" does not match a valid "%2$s"',
1903
-                        'event_espresso'
1904
-                    ),
1905
-                    $this->_req_data['message_template_group_id'],
1906
-                    'EE_Message_Template_Group'
1907
-                ),
1908
-                __FILE__,
1909
-                __FUNCTION__,
1910
-                __LINE__
1911
-            );
1912
-            $success = false;
1913
-        }
1914
-        if ($success) {
1915
-            $success = $status === 'off'
1916
-                ? $message_template_group->deactivate_context($this->_req_data['context'])
1917
-                : $message_template_group->activate_context($this->_req_data['context']);
1918
-        }
1919
-        $this->_template_args['success'] = $success;
1920
-        $this->_return_json();
1921
-    }
1922
-
1923
-
1924
-    public function _add_form_element_before()
1925
-    {
1926
-        return '<form method="post" action="'
1927
-               . $this->_template_args["edit_message_template_form_url"]
1928
-               . '" id="ee-msg-edit-frm">';
1929
-    }
1930
-
1931
-    public function _add_form_element_after()
1932
-    {
1933
-        return '</form>';
1934
-    }
1935
-
1936
-
1937
-    /**
1938
-     * This executes switching the template pack for a message template.
1939
-     *
1940
-     * @since 4.5.0
1941
-     * @throws EE_Error
1942
-     * @throws InvalidDataTypeException
1943
-     * @throws InvalidInterfaceException
1944
-     * @throws InvalidArgumentException
1945
-     * @throws ReflectionException
1946
-     */
1947
-    public function switch_template_pack()
1948
-    {
1949
-        $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
1950
-        $template_pack = ! empty($this->_req_data['template_pack']) ? $this->_req_data['template_pack'] : '';
1951
-
1952
-        // verify we have needed values.
1953
-        if (empty($GRP_ID) || empty($template_pack)) {
1954
-            $this->_template_args['error'] = true;
1955
-            EE_Error::add_error(
1956
-                esc_html__('The required date for switching templates is not available.', 'event_espresso'),
1957
-                __FILE__,
1958
-                __FUNCTION__,
1959
-                __LINE__
1960
-            );
1961
-        } else {
1962
-            // get template, set the new template_pack and then reset to default
1963
-            /** @type EE_Message_Template_Group $message_template_group */
1964
-            $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID);
1965
-
1966
-            $message_template_group->set_template_pack_name($template_pack);
1967
-            $this->_req_data['msgr'] = $message_template_group->messenger();
1968
-            $this->_req_data['mt'] = $message_template_group->message_type();
1969
-
1970
-            $query_args = $this->_reset_to_default_template();
1971
-
1972
-            if (empty($query_args['id'])) {
1973
-                EE_Error::add_error(
1974
-                    esc_html__(
1975
-                        'Something went wrong with switching the template pack. Please try again or contact EE support',
1976
-                        'event_espresso'
1977
-                    ),
1978
-                    __FILE__,
1979
-                    __FUNCTION__,
1980
-                    __LINE__
1981
-                );
1982
-                $this->_template_args['error'] = true;
1983
-            } else {
1984
-                $template_label = $message_template_group->get_template_pack()->label;
1985
-                $template_pack_labels = $message_template_group->messenger_obj()->get_supports_labels();
1986
-                EE_Error::add_success(
1987
-                    sprintf(
1988
-                        esc_html__(
1989
-                            'This message template has been successfully switched to use the %1$s %2$s.  Please wait while the page reloads with your new template.',
1990
-                            'event_espresso'
1991
-                        ),
1992
-                        $template_label,
1993
-                        $template_pack_labels->template_pack
1994
-                    )
1995
-                );
1996
-                // generate the redirect url for js.
1997
-                $url = self::add_query_args_and_nonce(
1998
-                    $query_args,
1999
-                    $this->_admin_base_url
2000
-                );
2001
-                $this->_template_args['data']['redirect_url'] = $url;
2002
-                $this->_template_args['success'] = true;
2003
-            }
2004
-
2005
-            $this->_return_json();
2006
-        }
2007
-    }
2008
-
2009
-
2010
-    /**
2011
-     * This handles resetting the template for the given messenger/message_type so that users can start from scratch if
2012
-     * they want.
2013
-     *
2014
-     * @access protected
2015
-     * @return array|null
2016
-     * @throws EE_Error
2017
-     * @throws InvalidArgumentException
2018
-     * @throws InvalidDataTypeException
2019
-     * @throws InvalidInterfaceException
2020
-     */
2021
-    protected function _reset_to_default_template()
2022
-    {
2023
-
2024
-        $templates = array();
2025
-        $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
2026
-        // we need to make sure we've got the info we need.
2027
-        if (! isset($this->_req_data['msgr'], $this->_req_data['mt'], $this->_req_data['GRP_ID'])) {
2028
-            EE_Error::add_error(
2029
-                esc_html__(
2030
-                    'In order to reset the template to its default we require the messenger, message type, and message template GRP_ID to know what is being reset.  At least one of these is missing.',
2031
-                    'event_espresso'
2032
-                ),
2033
-                __FILE__,
2034
-                __FUNCTION__,
2035
-                __LINE__
2036
-            );
2037
-        }
2038
-
2039
-        // all templates will be reset to whatever the defaults are
2040
-        // for the global template matching the messenger and message type.
2041
-        $success = ! empty($GRP_ID) ? true : false;
2042
-
2043
-        if ($success) {
2044
-            // let's first determine if the incoming template is a global template,
2045
-            // if it isn't then we need to get the global template matching messenger and message type.
2046
-            // $MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID( $GRP_ID );
2047
-
2048
-
2049
-            // note this is ONLY deleting the template fields (Message Template rows) NOT the message template group.
2050
-            $success = $this->_delete_mtp_permanently($GRP_ID, false);
2051
-
2052
-            if ($success) {
2053
-                // if successfully deleted, lets generate the new ones.
2054
-                // Note. We set GLOBAL to true, because resets on ANY template
2055
-                // will use the related global template defaults for regeneration.
2056
-                // This means that if a custom template is reset it resets to whatever the related global template is.
2057
-                // HOWEVER, we DO keep the template pack and template variation set
2058
-                // for the current custom template when resetting.
2059
-                $templates = $this->_generate_new_templates(
2060
-                    $this->_req_data['msgr'],
2061
-                    $this->_req_data['mt'],
2062
-                    $GRP_ID,
2063
-                    true
2064
-                );
2065
-            }
2066
-        }
2067
-
2068
-        // any error messages?
2069
-        if (! $success) {
2070
-            EE_Error::add_error(
2071
-                esc_html__(
2072
-                    'Something went wrong with deleting existing templates. Unable to reset to default',
2073
-                    'event_espresso'
2074
-                ),
2075
-                __FILE__,
2076
-                __FUNCTION__,
2077
-                __LINE__
2078
-            );
2079
-        }
2080
-
2081
-        // all good, let's add a success message!
2082
-        if ($success && ! empty($templates)) {
2083
-            // the info for the template we generated is the first element in the returned array
2084
-            // $templates = $templates[0];
2085
-            EE_Error::overwrite_success();
2086
-            EE_Error::add_success(__('Templates have been reset to defaults.', 'event_espresso'));
2087
-        }
2088
-
2089
-
2090
-        $query_args = array(
2091
-            'id'      => isset($templates[0]['GRP_ID']) ? $templates[0]['GRP_ID'] : null,
2092
-            'context' => isset($templates[0]['MTP_context']) ? $templates[0]['MTP_context'] : null,
2093
-            'action'  => isset($templates[0]['GRP_ID']) ? 'edit_message_template' : 'global_mtps',
2094
-        );
2095
-
2096
-        // if called via ajax then we return query args otherwise redirect
2097
-        if (defined('DOING_AJAX') && DOING_AJAX) {
2098
-            return $query_args;
2099
-        } else {
2100
-            $this->_redirect_after_action(false, '', '', $query_args, true);
2101
-
2102
-            return null;
2103
-        }
2104
-    }
2105
-
2106
-
2107
-    /**
2108
-     * Retrieve and set the message preview for display.
2109
-     *
2110
-     * @param bool $send if TRUE then we are doing an actual TEST send with the results of the preview.
2111
-     * @return string
2112
-     * @throws ReflectionException
2113
-     * @throws EE_Error
2114
-     * @throws InvalidArgumentException
2115
-     * @throws InvalidDataTypeException
2116
-     * @throws InvalidInterfaceException
2117
-     */
2118
-    public function _preview_message($send = false)
2119
-    {
2120
-        // first make sure we've got the necessary parameters
2121
-        if (! isset(
2122
-            $this->_req_data['message_type'],
2123
-            $this->_req_data['messenger'],
2124
-            $this->_req_data['messenger'],
2125
-            $this->_req_data['GRP_ID']
2126
-        )) {
2127
-            EE_Error::add_error(
2128
-                esc_html__('Missing necessary parameters for displaying preview', 'event_espresso'),
2129
-                __FILE__,
2130
-                __FUNCTION__,
2131
-                __LINE__
2132
-            );
2133
-        }
2134
-
2135
-        EE_Registry::instance()->REQ->set('GRP_ID', $this->_req_data['GRP_ID']);
2136
-
2137
-
2138
-        // get the preview!
2139
-        $preview = EED_Messages::preview_message(
2140
-            $this->_req_data['message_type'],
2141
-            $this->_req_data['context'],
2142
-            $this->_req_data['messenger'],
2143
-            $send
2144
-        );
2145
-
2146
-        if ($send) {
2147
-            return $preview;
2148
-        }
2149
-
2150
-        // let's add a button to go back to the edit view
2151
-        $query_args = array(
2152
-            'id'      => $this->_req_data['GRP_ID'],
2153
-            'context' => $this->_req_data['context'],
2154
-            'action'  => 'edit_message_template',
2155
-        );
2156
-        $go_back_url = parent::add_query_args_and_nonce($query_args, $this->_admin_base_url);
2157
-        $preview_button = '<a href="'
2158
-                          . $go_back_url
2159
-                          . '" class="button-secondary messages-preview-go-back-button">'
2160
-                          . esc_html__('Go Back to Edit', 'event_espresso')
2161
-                          . '</a>';
2162
-        $message_types = $this->get_installed_message_types();
2163
-        $active_messenger = $this->_message_resource_manager->get_active_messenger(
2164
-            $this->_req_data['messenger']
2165
-        );
2166
-        $active_messenger_label = $active_messenger instanceof EE_messenger
2167
-            ? ucwords($active_messenger->label['singular'])
2168
-            : esc_html__('Unknown Messenger', 'event_espresso');
2169
-        // let's provide a helpful title for context
2170
-        $preview_title = sprintf(
2171
-            esc_html__('Viewing Preview for %s %s Message Template', 'event_espresso'),
2172
-            $active_messenger_label,
2173
-            ucwords($message_types[ $this->_req_data['message_type'] ]->label['singular'])
2174
-        );
2175
-        if ( empty( $preview ) ) {
2176
-            $this->noEventsErrorMessage();
2177
-        }
2178
-        // setup display of preview.
2179
-        $this->_admin_page_title = $preview_title;
2180
-        $this->_template_args['admin_page_title'] = $preview_title;
2181
-        $this->_template_args['admin_page_content'] = $preview_button . '<br />' . $preview;
2182
-        $this->_template_args['data']['force_json'] = true;
2183
-
2184
-        return '';
2185
-    }
2186
-
2187
-
2188
-    /**
2189
-     * Used to set an error if there are no events available for generating a preview/test send.
2190
-     *
2191
-     * @param bool $test_send  Whether the error should be generated for the context of a test send.
2192
-     */
2193
-    protected function noEventsErrorMessage($test_send = false) {
2194
-        $events_url = parent::add_query_args_and_nonce(
2195
-            array(
2196
-                'action' => 'default',
2197
-                'page' => 'espresso_events',
2198
-            ),
2199
-            admin_url( 'admin.php' )
2200
-        );
2201
-        $message = $test_send
2202
-            ? __(
2203
-                'A test message could not be sent for this message template because there are no events created yet. The preview system uses actual events for generating the test message. %1$sGo see your events%2$s!',
2204
-                'event_espresso'
2205
-            )
2206
-            : __(
2207
-                'There is no preview for this message template available because there are no events created yet. The preview system uses actual events for generating the preview. %1$sGo see your events%2$s!',
2208
-                'event_espresso'
2209
-            );
2210
-
2211
-        EE_Error::add_attention(
2212
-            sprintf(
2213
-                $message,
2214
-                "<a href='{$events_url}'>",
2215
-                '</a>'
2216
-            )
2217
-        );
2218
-    }
2219
-
2220
-
2221
-    /**
2222
-     * The initial _preview_message is on a no headers route.  It will optionally call this if necessary otherwise it
2223
-     * gets called automatically.
2224
-     *
2225
-     * @since 4.5.0
2226
-     *
2227
-     * @return string
2228
-     */
2229
-    protected function _display_preview_message()
2230
-    {
2231
-        $this->display_admin_page_with_no_sidebar();
2232
-    }
2233
-
2234
-
2235
-    /**
2236
-     * registers metaboxes that should show up on the "edit_message_template" page
2237
-     *
2238
-     * @access protected
2239
-     * @return void
2240
-     */
2241
-    protected function _register_edit_meta_boxes()
2242
-    {
2243
-        add_meta_box(
2244
-            'mtp_valid_shortcodes',
2245
-            esc_html__('Valid Shortcodes', 'event_espresso'),
2246
-            array($this, 'shortcode_meta_box'),
2247
-            $this->_current_screen->id,
2248
-            'side',
2249
-            'default'
2250
-        );
2251
-        add_meta_box(
2252
-            'mtp_extra_actions',
2253
-            esc_html__('Extra Actions', 'event_espresso'),
2254
-            array($this, 'extra_actions_meta_box'),
2255
-            $this->_current_screen->id,
2256
-            'side',
2257
-            'high'
2258
-        );
2259
-        add_meta_box(
2260
-            'mtp_templates',
2261
-            esc_html__('Template Styles', 'event_espresso'),
2262
-            array($this, 'template_pack_meta_box'),
2263
-            $this->_current_screen->id,
2264
-            'side',
2265
-            'high'
2266
-        );
2267
-    }
2268
-
2269
-
2270
-    /**
2271
-     * metabox content for all template pack and variation selection.
2272
-     *
2273
-     * @since 4.5.0
2274
-     * @return string
2275
-     * @throws DomainException
2276
-     * @throws EE_Error
2277
-     * @throws InvalidArgumentException
2278
-     * @throws ReflectionException
2279
-     * @throws InvalidDataTypeException
2280
-     * @throws InvalidInterfaceException
2281
-     */
2282
-    public function template_pack_meta_box()
2283
-    {
2284
-        $this->_set_message_template_group();
2285
-
2286
-        $tp_collection = EEH_MSG_Template::get_template_pack_collection();
2287
-
2288
-        $tp_select_values = array();
2289
-
2290
-        foreach ($tp_collection as $tp) {
2291
-            // only include template packs that support this messenger and message type!
2292
-            $supports = $tp->get_supports();
2293
-            if (! isset($supports[ $this->_message_template_group->messenger() ])
2294
-                || ! in_array(
2295
-                    $this->_message_template_group->message_type(),
2296
-                    $supports[ $this->_message_template_group->messenger() ],
2297
-                    true
2298
-                )
2299
-            ) {
2300
-                // not supported
2301
-                continue;
2302
-            }
2303
-
2304
-            $tp_select_values[] = array(
2305
-                'text' => $tp->label,
2306
-                'id'   => $tp->dbref,
2307
-            );
2308
-        }
2309
-
2310
-        // if empty $tp_select_values then we make sure default is set because EVERY message type should be supported by
2311
-        // the default template pack.  This still allows for the odd template pack to override.
2312
-        if (empty($tp_select_values)) {
2313
-            $tp_select_values[] = array(
2314
-                'text' => esc_html__('Default', 'event_espresso'),
2315
-                'id'   => 'default',
2316
-            );
2317
-        }
2318
-
2319
-        // setup variation select values for the currently selected template.
2320
-        $variations = $this->_message_template_group->get_template_pack()->get_variations(
2321
-            $this->_message_template_group->messenger(),
2322
-            $this->_message_template_group->message_type()
2323
-        );
2324
-        $variations_select_values = array();
2325
-        foreach ($variations as $variation => $label) {
2326
-            $variations_select_values[] = array(
2327
-                'text' => $label,
2328
-                'id'   => $variation,
2329
-            );
2330
-        }
2331
-
2332
-        $template_pack_labels = $this->_message_template_group->messenger_obj()->get_supports_labels();
2333
-
2334
-        $template_args['template_packs_selector'] = EEH_Form_Fields::select_input(
2335
-            'MTP_template_pack',
2336
-            $tp_select_values,
2337
-            $this->_message_template_group->get_template_pack_name()
2338
-        );
2339
-        $template_args['variations_selector'] = EEH_Form_Fields::select_input(
2340
-            'MTP_template_variation',
2341
-            $variations_select_values,
2342
-            $this->_message_template_group->get_template_pack_variation()
2343
-        );
2344
-        $template_args['template_pack_label'] = $template_pack_labels->template_pack;
2345
-        $template_args['template_variation_label'] = $template_pack_labels->template_variation;
2346
-        $template_args['template_pack_description'] = $template_pack_labels->template_pack_description;
2347
-        $template_args['template_variation_description'] = $template_pack_labels->template_variation_description;
2348
-
2349
-        $template = EE_MSG_TEMPLATE_PATH . 'template_pack_and_variations_metabox.template.php';
2350
-
2351
-        EEH_Template::display_template($template, $template_args);
2352
-    }
2353
-
2354
-
2355
-    /**
2356
-     * This meta box holds any extra actions related to Message Templates
2357
-     * For now, this includes Resetting templates to defaults and sending a test email.
2358
-     *
2359
-     * @access  public
2360
-     * @return void
2361
-     * @throws EE_Error
2362
-     */
2363
-    public function extra_actions_meta_box()
2364
-    {
2365
-        $template_form_fields = array();
2366
-
2367
-        $extra_args = array(
2368
-            'msgr'   => $this->_message_template_group->messenger(),
2369
-            'mt'     => $this->_message_template_group->message_type(),
2370
-            'GRP_ID' => $this->_message_template_group->GRP_ID(),
2371
-        );
2372
-        // first we need to see if there are any fields
2373
-        $fields = $this->_message_template_group->messenger_obj()->get_test_settings_fields();
2374
-
2375
-        if (! empty($fields)) {
2376
-            // yup there be fields
2377
-            foreach ($fields as $field => $config) {
2378
-                $field_id = $this->_message_template_group->messenger() . '_' . $field;
2379
-                $existing = $this->_message_template_group->messenger_obj()->get_existing_test_settings();
2380
-                $default = isset($config['default']) ? $config['default'] : '';
2381
-                $default = isset($config['value']) ? $config['value'] : $default;
2382
-
2383
-                // if type is hidden and the value is empty
2384
-                // something may have gone wrong so let's correct with the defaults
2385
-                $fix = $config['input'] === 'hidden'
2386
-                       && isset($existing[ $field ])
2387
-                       && empty($existing[ $field ])
2388
-                    ? $default
2389
-                    : '';
2390
-                $existing[ $field ] = isset($existing[ $field ]) && empty($fix)
2391
-                    ? $existing[ $field ]
2392
-                    : $fix;
2393
-
2394
-                $template_form_fields[ $field_id ] = array(
2395
-                    'name'       => 'test_settings_fld[' . $field . ']',
2396
-                    'label'      => $config['label'],
2397
-                    'input'      => $config['input'],
2398
-                    'type'       => $config['type'],
2399
-                    'required'   => $config['required'],
2400
-                    'validation' => $config['validation'],
2401
-                    'value'      => isset($existing[ $field ]) ? $existing[ $field ] : $default,
2402
-                    'css_class'  => $config['css_class'],
2403
-                    'options'    => isset($config['options']) ? $config['options'] : array(),
2404
-                    'default'    => $default,
2405
-                    'format'     => $config['format'],
2406
-                );
2407
-            }
2408
-        }
2409
-
2410
-        $test_settings_fields = ! empty($template_form_fields)
2411
-            ? $this->_generate_admin_form_fields($template_form_fields, 'string', 'ee_tst_settings_flds')
2412
-            : '';
2413
-
2414
-        $test_settings_html = '';
2415
-        // print out $test_settings_fields
2416
-        if (! empty($test_settings_fields)) {
2417
-            echo $test_settings_fields;
2418
-            $test_settings_html = '<input type="submit" class="button-primary mtp-test-button alignright" ';
2419
-            $test_settings_html .= 'name="test_button" value="';
2420
-            $test_settings_html .= esc_html__('Test Send', 'event_espresso');
2421
-            $test_settings_html .= '" /><div style="clear:both"></div>';
2422
-        }
2423
-
2424
-        // and button
2425
-        $test_settings_html .= '<p>'
2426
-                               . esc_html__('Need to reset this message type and start over?', 'event_espresso')
2427
-                               . '</p>';
2428
-        $test_settings_html .= '<div class="publishing-action alignright resetbutton">';
2429
-        $test_settings_html .= $this->get_action_link_or_button(
2430
-            'reset_to_default',
2431
-            'reset',
2432
-            $extra_args,
2433
-            'button-primary reset-default-button'
2434
-        );
2435
-        $test_settings_html .= '</div><div style="clear:both"></div>';
2436
-        echo $test_settings_html;
2437
-    }
2438
-
2439
-
2440
-    /**
2441
-     * This returns the shortcode selector skeleton for a given context and field.
2442
-     *
2443
-     * @since 4.9.rc.000
2444
-     * @param string $field           The name of the field retrieving shortcodes for.
2445
-     * @param string $linked_input_id The css id of the input that the shortcodes get added to.
2446
-     * @return string
2447
-     * @throws DomainException
2448
-     * @throws EE_Error
2449
-     * @throws InvalidArgumentException
2450
-     * @throws ReflectionException
2451
-     * @throws InvalidDataTypeException
2452
-     * @throws InvalidInterfaceException
2453
-     */
2454
-    protected function _get_shortcode_selector($field, $linked_input_id)
2455
-    {
2456
-        $template_args = array(
2457
-            'shortcodes'      => $this->_get_shortcodes(array($field), true),
2458
-            'fieldname'       => $field,
2459
-            'linked_input_id' => $linked_input_id,
2460
-        );
2461
-
2462
-        return EEH_Template::display_template(
2463
-            EE_MSG_TEMPLATE_PATH . 'shortcode_selector_skeleton.template.php',
2464
-            $template_args,
2465
-            true
2466
-        );
2467
-    }
2468
-
2469
-
2470
-    /**
2471
-     * This just takes care of returning the meta box content for shortcodes (only used on the edit message template
2472
-     * page)
2473
-     *
2474
-     * @access public
2475
-     * @return void
2476
-     * @throws EE_Error
2477
-     * @throws InvalidArgumentException
2478
-     * @throws ReflectionException
2479
-     * @throws InvalidDataTypeException
2480
-     * @throws InvalidInterfaceException
2481
-     */
2482
-    public function shortcode_meta_box()
2483
-    {
2484
-        $shortcodes = $this->_get_shortcodes(array(), false); // just make sure shortcodes property is set
2485
-        // $messenger = $this->_message_template_group->messenger_obj();
2486
-        // now let's set the content depending on the status of the shortcodes array
2487
-        if (empty($shortcodes)) {
2488
-            $content = '<p>' . esc_html__('There are no valid shortcodes available', 'event_espresso') . '</p>';
2489
-            echo $content;
2490
-        } else {
2491
-            // $alt = 0;
2492
-            ?>
22
+	/**
23
+	 * @type EE_Message_Resource_Manager $_message_resource_manager
24
+	 */
25
+	protected $_message_resource_manager;
26
+
27
+	/**
28
+	 * @type string $_active_message_type_name
29
+	 */
30
+	protected $_active_message_type_name = '';
31
+
32
+	/**
33
+	 * @type EE_messenger $_active_messenger
34
+	 */
35
+	protected $_active_messenger;
36
+	protected $_activate_state;
37
+	protected $_activate_meta_box_type;
38
+	protected $_current_message_meta_box;
39
+	protected $_current_message_meta_box_object;
40
+	protected $_context_switcher;
41
+	protected $_shortcodes = array();
42
+	protected $_active_messengers = array();
43
+	protected $_active_message_types = array();
44
+
45
+	/**
46
+	 * @var EE_Message_Template_Group $_message_template_group
47
+	 */
48
+	protected $_message_template_group;
49
+	protected $_m_mt_settings = array();
50
+
51
+
52
+	/**
53
+	 * This is set via the _set_message_template_group method and holds whatever the template pack for the group is.
54
+	 * IF there is no group then it gets automatically set to the Default template pack.
55
+	 *
56
+	 * @since 4.5.0
57
+	 *
58
+	 * @var EE_Messages_Template_Pack
59
+	 */
60
+	protected $_template_pack;
61
+
62
+
63
+	/**
64
+	 * This is set via the _set_message_template_group method and holds whatever the template pack variation for the
65
+	 * group is.  If there is no group then it automatically gets set to default.
66
+	 *
67
+	 * @since 4.5.0
68
+	 *
69
+	 * @var string
70
+	 */
71
+	protected $_variation;
72
+
73
+
74
+	/**
75
+	 * @param bool $routing
76
+	 * @throws EE_Error
77
+	 */
78
+	public function __construct($routing = true)
79
+	{
80
+		// make sure messages autoloader is running
81
+		EED_Messages::set_autoloaders();
82
+		parent::__construct($routing);
83
+	}
84
+
85
+
86
+	protected function _init_page_props()
87
+	{
88
+		$this->page_slug = EE_MSG_PG_SLUG;
89
+		$this->page_label = esc_html__('Messages Settings', 'event_espresso');
90
+		$this->_admin_base_url = EE_MSG_ADMIN_URL;
91
+		$this->_admin_base_path = EE_MSG_ADMIN;
92
+
93
+		$this->_activate_state = isset($this->_req_data['activate_state']) ? (array) $this->_req_data['activate_state']
94
+			: array();
95
+
96
+		$this->_active_messenger = isset($this->_req_data['messenger']) ? $this->_req_data['messenger'] : null;
97
+		$this->_load_message_resource_manager();
98
+	}
99
+
100
+
101
+	/**
102
+	 * loads messenger objects into the $_active_messengers property (so we can access the needed methods)
103
+	 *
104
+	 * @throws EE_Error
105
+	 * @throws InvalidDataTypeException
106
+	 * @throws InvalidInterfaceException
107
+	 * @throws InvalidArgumentException
108
+	 * @throws ReflectionException
109
+	 */
110
+	protected function _load_message_resource_manager()
111
+	{
112
+		$this->_message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
113
+	}
114
+
115
+
116
+	/**
117
+	 * @deprecated 4.9.9.rc.014
118
+	 * @return array
119
+	 * @throws EE_Error
120
+	 * @throws InvalidArgumentException
121
+	 * @throws InvalidDataTypeException
122
+	 * @throws InvalidInterfaceException
123
+	 */
124
+	public function get_messengers_for_list_table()
125
+	{
126
+		EE_Error::doing_it_wrong(
127
+			__METHOD__,
128
+			sprintf(
129
+				esc_html__(
130
+					'This method is no longer in use.  There is no replacement for it. The method was used to generate a set of values for use in creating a messenger filter dropdown which is now generated differently via %s',
131
+					'event_espresso'
132
+				),
133
+				'Messages_Admin_Page::get_messengers_select_input()'
134
+			),
135
+			'4.9.9.rc.014'
136
+		);
137
+
138
+		$m_values = array();
139
+		$active_messengers = EEM_Message::instance()->get_all(array('group_by' => 'MSG_messenger'));
140
+		// setup messengers for selects
141
+		$i = 1;
142
+		foreach ($active_messengers as $active_messenger) {
143
+			if ($active_messenger instanceof EE_Message) {
144
+				$m_values[ $i ]['id'] = $active_messenger->messenger();
145
+				$m_values[ $i ]['text'] = ucwords($active_messenger->messenger_label());
146
+				$i++;
147
+			}
148
+		}
149
+
150
+		return $m_values;
151
+	}
152
+
153
+
154
+	/**
155
+	 * @deprecated 4.9.9.rc.014
156
+	 * @return array
157
+	 * @throws EE_Error
158
+	 * @throws InvalidArgumentException
159
+	 * @throws InvalidDataTypeException
160
+	 * @throws InvalidInterfaceException
161
+	 */
162
+	public function get_message_types_for_list_table()
163
+	{
164
+		EE_Error::doing_it_wrong(
165
+			__METHOD__,
166
+			sprintf(
167
+				esc_html__(
168
+					'This method is no longer in use.  There is no replacement for it. The method was used to generate a set of values for use in creating a message type filter dropdown which is now generated differently via %s',
169
+					'event_espresso'
170
+				),
171
+				'Messages_Admin_Page::get_message_types_select_input()'
172
+			),
173
+			'4.9.9.rc.014'
174
+		);
175
+
176
+		$mt_values = array();
177
+		$active_messages = EEM_Message::instance()->get_all(array('group_by' => 'MSG_message_type'));
178
+		$i = 1;
179
+		foreach ($active_messages as $active_message) {
180
+			if ($active_message instanceof EE_Message) {
181
+				$mt_values[ $i ]['id'] = $active_message->message_type();
182
+				$mt_values[ $i ]['text'] = ucwords($active_message->message_type_label());
183
+				$i++;
184
+			}
185
+		}
186
+
187
+		return $mt_values;
188
+	}
189
+
190
+
191
+	/**
192
+	 * @deprecated 4.9.9.rc.014
193
+	 * @return array
194
+	 * @throws EE_Error
195
+	 * @throws InvalidArgumentException
196
+	 * @throws InvalidDataTypeException
197
+	 * @throws InvalidInterfaceException
198
+	 */
199
+	public function get_contexts_for_message_types_for_list_table()
200
+	{
201
+		EE_Error::doing_it_wrong(
202
+			__METHOD__,
203
+			sprintf(
204
+				esc_html__(
205
+					'This method is no longer in use.  There is no replacement for it. The method was used to generate a set of values for use in creating a message type context filter dropdown which is now generated differently via %s',
206
+					'event_espresso'
207
+				),
208
+				'Messages_Admin_Page::get_contexts_for_message_types_select_input()'
209
+			),
210
+			'4.9.9.rc.014'
211
+		);
212
+
213
+		$contexts = array();
214
+		$active_message_contexts = EEM_Message::instance()->get_all(array('group_by' => 'MSG_context'));
215
+		foreach ($active_message_contexts as $active_message) {
216
+			if ($active_message instanceof EE_Message) {
217
+				$message_type = $active_message->message_type_object();
218
+				if ($message_type instanceof EE_message_type) {
219
+					$message_type_contexts = $message_type->get_contexts();
220
+					foreach ($message_type_contexts as $context => $context_details) {
221
+						$contexts[ $context ] = $context_details['label'];
222
+					}
223
+				}
224
+			}
225
+		}
226
+
227
+		return $contexts;
228
+	}
229
+
230
+
231
+	/**
232
+	 * Generate select input with provided messenger options array.
233
+	 *
234
+	 * @param array $messenger_options Array of messengers indexed by messenger slug and values are the messenger
235
+	 *                                 labels.
236
+	 * @return string
237
+	 * @throws EE_Error
238
+	 */
239
+	public function get_messengers_select_input($messenger_options)
240
+	{
241
+		// if empty or just one value then just return an empty string
242
+		if (empty($messenger_options)
243
+			|| ! is_array($messenger_options)
244
+			|| count($messenger_options) === 1
245
+		) {
246
+			return '';
247
+		}
248
+		// merge in default
249
+		$messenger_options = array_merge(
250
+			array('none_selected' => esc_html__('Show All Messengers', 'event_espresso')),
251
+			$messenger_options
252
+		);
253
+		$input = new EE_Select_Input(
254
+			$messenger_options,
255
+			array(
256
+				'html_name'  => 'ee_messenger_filter_by',
257
+				'html_id'    => 'ee_messenger_filter_by',
258
+				'html_class' => 'wide',
259
+				'default'    => isset($this->_req_data['ee_messenger_filter_by'])
260
+					? sanitize_title($this->_req_data['ee_messenger_filter_by'])
261
+					: 'none_selected',
262
+			)
263
+		);
264
+
265
+		return $input->get_html_for_input();
266
+	}
267
+
268
+
269
+	/**
270
+	 * Generate select input with provided message type options array.
271
+	 *
272
+	 * @param array $message_type_options Array of message types indexed by message type slug, and values are the
273
+	 *                                    message type labels
274
+	 * @return string
275
+	 * @throws EE_Error
276
+	 */
277
+	public function get_message_types_select_input($message_type_options)
278
+	{
279
+		// if empty or count of options is 1 then just return an empty string
280
+		if (empty($message_type_options)
281
+			|| ! is_array($message_type_options)
282
+			|| count($message_type_options) === 1
283
+		) {
284
+			return '';
285
+		}
286
+		// merge in default
287
+		$message_type_options = array_merge(
288
+			array('none_selected' => esc_html__('Show All Message Types', 'event_espresso')),
289
+			$message_type_options
290
+		);
291
+		$input = new EE_Select_Input(
292
+			$message_type_options,
293
+			array(
294
+				'html_name'  => 'ee_message_type_filter_by',
295
+				'html_id'    => 'ee_message_type_filter_by',
296
+				'html_class' => 'wide',
297
+				'default'    => isset($this->_req_data['ee_message_type_filter_by'])
298
+					? sanitize_title($this->_req_data['ee_message_type_filter_by'])
299
+					: 'none_selected',
300
+			)
301
+		);
302
+
303
+		return $input->get_html_for_input();
304
+	}
305
+
306
+
307
+	/**
308
+	 * Generate select input with provide message type contexts array.
309
+	 *
310
+	 * @param array $context_options Array of message type contexts indexed by context slug, and values are the
311
+	 *                               context label.
312
+	 * @return string
313
+	 * @throws EE_Error
314
+	 */
315
+	public function get_contexts_for_message_types_select_input($context_options)
316
+	{
317
+		// if empty or count of options is one then just return empty string
318
+		if (empty($context_options)
319
+			|| ! is_array($context_options)
320
+			|| count($context_options) === 1
321
+		) {
322
+			return '';
323
+		}
324
+		// merge in default
325
+		$context_options = array_merge(
326
+			array('none_selected' => esc_html__('Show all Contexts', 'event_espresso')),
327
+			$context_options
328
+		);
329
+		$input = new EE_Select_Input(
330
+			$context_options,
331
+			array(
332
+				'html_name'  => 'ee_context_filter_by',
333
+				'html_id'    => 'ee_context_filter_by',
334
+				'html_class' => 'wide',
335
+				'default'    => isset($this->_req_data['ee_context_filter_by'])
336
+					? sanitize_title($this->_req_data['ee_context_filter_by'])
337
+					: 'none_selected',
338
+			)
339
+		);
340
+
341
+		return $input->get_html_for_input();
342
+	}
343
+
344
+
345
+	protected function _ajax_hooks()
346
+	{
347
+		add_action('wp_ajax_activate_messenger', array($this, 'activate_messenger_toggle'));
348
+		add_action('wp_ajax_activate_mt', array($this, 'activate_mt_toggle'));
349
+		add_action('wp_ajax_ee_msgs_save_settings', array($this, 'save_settings'));
350
+		add_action('wp_ajax_ee_msgs_update_mt_form', array($this, 'update_mt_form'));
351
+		add_action('wp_ajax_switch_template_pack', array($this, 'switch_template_pack'));
352
+		add_action('wp_ajax_toggle_context_template', array($this, 'toggle_context_template'));
353
+	}
354
+
355
+
356
+	protected function _define_page_props()
357
+	{
358
+		$this->_admin_page_title = $this->page_label;
359
+		$this->_labels = array(
360
+			'buttons'    => array(
361
+				'add'    => esc_html__('Add New Message Template', 'event_espresso'),
362
+				'edit'   => esc_html__('Edit Message Template', 'event_espresso'),
363
+				'delete' => esc_html__('Delete Message Template', 'event_espresso'),
364
+			),
365
+			'publishbox' => esc_html__('Update Actions', 'event_espresso'),
366
+		);
367
+	}
368
+
369
+
370
+	/**
371
+	 *        an array for storing key => value pairs of request actions and their corresponding methods
372
+	 *
373
+	 * @access protected
374
+	 * @return void
375
+	 */
376
+	protected function _set_page_routes()
377
+	{
378
+		$grp_id = ! empty($this->_req_data['GRP_ID']) && ! is_array($this->_req_data['GRP_ID'])
379
+			? $this->_req_data['GRP_ID']
380
+			: 0;
381
+		$grp_id = empty($grp_id) && ! empty($this->_req_data['id'])
382
+			? $this->_req_data['id']
383
+			: $grp_id;
384
+		$msg_id = ! empty($this->_req_data['MSG_ID']) && ! is_array($this->_req_data['MSG_ID'])
385
+			? $this->_req_data['MSG_ID']
386
+			: 0;
387
+
388
+		$this->_page_routes = array(
389
+			'default'                          => array(
390
+				'func'       => '_message_queue_list_table',
391
+				'capability' => 'ee_read_global_messages',
392
+			),
393
+			'global_mtps'                      => array(
394
+				'func'       => '_ee_default_messages_overview_list_table',
395
+				'capability' => 'ee_read_global_messages',
396
+			),
397
+			'custom_mtps'                      => array(
398
+				'func'       => '_custom_mtps_preview',
399
+				'capability' => 'ee_read_messages',
400
+			),
401
+			'add_new_message_template'         => array(
402
+				'func'       => '_add_message_template',
403
+				'capability' => 'ee_edit_messages',
404
+				'noheader'   => true,
405
+			),
406
+			'edit_message_template'            => array(
407
+				'func'       => '_edit_message_template',
408
+				'capability' => 'ee_edit_message',
409
+				'obj_id'     => $grp_id,
410
+			),
411
+			'preview_message'                  => array(
412
+				'func'               => '_preview_message',
413
+				'capability'         => 'ee_read_message',
414
+				'obj_id'             => $grp_id,
415
+				'noheader'           => true,
416
+				'headers_sent_route' => 'display_preview_message',
417
+			),
418
+			'display_preview_message'          => array(
419
+				'func'       => '_display_preview_message',
420
+				'capability' => 'ee_read_message',
421
+				'obj_id'     => $grp_id,
422
+			),
423
+			'insert_message_template'          => array(
424
+				'func'       => '_insert_or_update_message_template',
425
+				'capability' => 'ee_edit_messages',
426
+				'args'       => array('new_template' => true),
427
+				'noheader'   => true,
428
+			),
429
+			'update_message_template'          => array(
430
+				'func'       => '_insert_or_update_message_template',
431
+				'capability' => 'ee_edit_message',
432
+				'obj_id'     => $grp_id,
433
+				'args'       => array('new_template' => false),
434
+				'noheader'   => true,
435
+			),
436
+			'trash_message_template'           => array(
437
+				'func'       => '_trash_or_restore_message_template',
438
+				'capability' => 'ee_delete_message',
439
+				'obj_id'     => $grp_id,
440
+				'args'       => array('trash' => true, 'all' => true),
441
+				'noheader'   => true,
442
+			),
443
+			'trash_message_template_context'   => array(
444
+				'func'       => '_trash_or_restore_message_template',
445
+				'capability' => 'ee_delete_message',
446
+				'obj_id'     => $grp_id,
447
+				'args'       => array('trash' => true),
448
+				'noheader'   => true,
449
+			),
450
+			'restore_message_template'         => array(
451
+				'func'       => '_trash_or_restore_message_template',
452
+				'capability' => 'ee_delete_message',
453
+				'obj_id'     => $grp_id,
454
+				'args'       => array('trash' => false, 'all' => true),
455
+				'noheader'   => true,
456
+			),
457
+			'restore_message_template_context' => array(
458
+				'func'       => '_trash_or_restore_message_template',
459
+				'capability' => 'ee_delete_message',
460
+				'obj_id'     => $grp_id,
461
+				'args'       => array('trash' => false),
462
+				'noheader'   => true,
463
+			),
464
+			'delete_message_template'          => array(
465
+				'func'       => '_delete_message_template',
466
+				'capability' => 'ee_delete_message',
467
+				'obj_id'     => $grp_id,
468
+				'noheader'   => true,
469
+			),
470
+			'reset_to_default'                 => array(
471
+				'func'       => '_reset_to_default_template',
472
+				'capability' => 'ee_edit_message',
473
+				'obj_id'     => $grp_id,
474
+				'noheader'   => true,
475
+			),
476
+			'settings'                         => array(
477
+				'func'       => '_settings',
478
+				'capability' => 'manage_options',
479
+			),
480
+			'update_global_settings'           => array(
481
+				'func'       => '_update_global_settings',
482
+				'capability' => 'manage_options',
483
+				'noheader'   => true,
484
+			),
485
+			'generate_now'                     => array(
486
+				'func'       => '_generate_now',
487
+				'capability' => 'ee_send_message',
488
+				'noheader'   => true,
489
+			),
490
+			'generate_and_send_now'            => array(
491
+				'func'       => '_generate_and_send_now',
492
+				'capability' => 'ee_send_message',
493
+				'noheader'   => true,
494
+			),
495
+			'queue_for_resending'              => array(
496
+				'func'       => '_queue_for_resending',
497
+				'capability' => 'ee_send_message',
498
+				'noheader'   => true,
499
+			),
500
+			'send_now'                         => array(
501
+				'func'       => '_send_now',
502
+				'capability' => 'ee_send_message',
503
+				'noheader'   => true,
504
+			),
505
+			'delete_ee_message'                => array(
506
+				'func'       => '_delete_ee_messages',
507
+				'capability' => 'ee_delete_messages',
508
+				'noheader'   => true,
509
+			),
510
+			'delete_ee_messages'               => array(
511
+				'func'       => '_delete_ee_messages',
512
+				'capability' => 'ee_delete_messages',
513
+				'noheader'   => true,
514
+				'obj_id'     => $msg_id,
515
+			),
516
+		);
517
+	}
518
+
519
+
520
+	protected function _set_page_config()
521
+	{
522
+		$this->_page_config = array(
523
+			'default'                  => array(
524
+				'nav'           => array(
525
+					'label' => esc_html__('Message Activity', 'event_espresso'),
526
+					'order' => 10,
527
+				),
528
+				'list_table'    => 'EE_Message_List_Table',
529
+				// 'qtips' => array( 'EE_Message_List_Table_Tips' ),
530
+				'require_nonce' => false,
531
+			),
532
+			'global_mtps'              => array(
533
+				'nav'           => array(
534
+					'label' => esc_html__('Default Message Templates', 'event_espresso'),
535
+					'order' => 20,
536
+				),
537
+				'list_table'    => 'Messages_Template_List_Table',
538
+				'help_tabs'     => array(
539
+					'messages_overview_help_tab'                                => array(
540
+						'title'    => esc_html__('Messages Overview', 'event_espresso'),
541
+						'filename' => 'messages_overview',
542
+					),
543
+					'messages_overview_messages_table_column_headings_help_tab' => array(
544
+						'title'    => esc_html__('Messages Table Column Headings', 'event_espresso'),
545
+						'filename' => 'messages_overview_table_column_headings',
546
+					),
547
+					'messages_overview_messages_filters_help_tab'               => array(
548
+						'title'    => esc_html__('Message Filters', 'event_espresso'),
549
+						'filename' => 'messages_overview_filters',
550
+					),
551
+					'messages_overview_messages_views_help_tab'                 => array(
552
+						'title'    => esc_html__('Message Views', 'event_espresso'),
553
+						'filename' => 'messages_overview_views',
554
+					),
555
+					'message_overview_message_types_help_tab'                   => array(
556
+						'title'    => esc_html__('Message Types', 'event_espresso'),
557
+						'filename' => 'messages_overview_types',
558
+					),
559
+					'messages_overview_messengers_help_tab'                     => array(
560
+						'title'    => esc_html__('Messengers', 'event_espresso'),
561
+						'filename' => 'messages_overview_messengers',
562
+					),
563
+				),
564
+				'help_tour'     => array('Messages_Overview_Help_Tour'),
565
+				'require_nonce' => false,
566
+			),
567
+			'custom_mtps'              => array(
568
+				'nav'           => array(
569
+					'label' => esc_html__('Custom Message Templates', 'event_espresso'),
570
+					'order' => 30,
571
+				),
572
+				'help_tabs'     => array(),
573
+				'help_tour'     => array(),
574
+				'require_nonce' => false,
575
+			),
576
+			'add_new_message_template' => array(
577
+				'nav'           => array(
578
+					'label'      => esc_html__('Add New Message Templates', 'event_espresso'),
579
+					'order'      => 5,
580
+					'persistent' => false,
581
+				),
582
+				'require_nonce' => false,
583
+			),
584
+			'edit_message_template'    => array(
585
+				'labels'        => array(
586
+					'buttons'    => array(
587
+						'reset' => esc_html__('Reset Templates', 'event_espresso'),
588
+					),
589
+					'publishbox' => esc_html__('Update Actions', 'event_espresso'),
590
+				),
591
+				'nav'           => array(
592
+					'label'      => esc_html__('Edit Message Templates', 'event_espresso'),
593
+					'order'      => 5,
594
+					'persistent' => false,
595
+					'url'        => '',
596
+				),
597
+				'metaboxes'     => array('_publish_post_box', '_register_edit_meta_boxes'),
598
+				'has_metaboxes' => true,
599
+				'help_tour'     => array('Message_Templates_Edit_Help_Tour'),
600
+				'help_tabs'     => array(
601
+					'edit_message_template'            => array(
602
+						'title'    => esc_html__('Message Template Editor', 'event_espresso'),
603
+						'callback' => 'edit_message_template_help_tab',
604
+					),
605
+					'message_templates_help_tab'       => array(
606
+						'title'    => esc_html__('Message Templates', 'event_espresso'),
607
+						'filename' => 'messages_templates',
608
+					),
609
+					'message_template_shortcodes'      => array(
610
+						'title'    => esc_html__('Message Shortcodes', 'event_espresso'),
611
+						'callback' => 'message_template_shortcodes_help_tab',
612
+					),
613
+					'message_preview_help_tab'         => array(
614
+						'title'    => esc_html__('Message Preview', 'event_espresso'),
615
+						'filename' => 'messages_preview',
616
+					),
617
+					'messages_overview_other_help_tab' => array(
618
+						'title'    => esc_html__('Messages Other', 'event_espresso'),
619
+						'filename' => 'messages_overview_other',
620
+					),
621
+				),
622
+				'require_nonce' => false,
623
+			),
624
+			'display_preview_message'  => array(
625
+				'nav'           => array(
626
+					'label'      => esc_html__('Message Preview', 'event_espresso'),
627
+					'order'      => 5,
628
+					'url'        => '',
629
+					'persistent' => false,
630
+				),
631
+				'help_tabs'     => array(
632
+					'preview_message' => array(
633
+						'title'    => esc_html__('About Previews', 'event_espresso'),
634
+						'callback' => 'preview_message_help_tab',
635
+					),
636
+				),
637
+				'require_nonce' => false,
638
+			),
639
+			'settings'                 => array(
640
+				'nav'           => array(
641
+					'label' => esc_html__('Settings', 'event_espresso'),
642
+					'order' => 40,
643
+				),
644
+				'metaboxes'     => array('_messages_settings_metaboxes'),
645
+				'help_tabs'     => array(
646
+					'messages_settings_help_tab'               => array(
647
+						'title'    => esc_html__('Messages Settings', 'event_espresso'),
648
+						'filename' => 'messages_settings',
649
+					),
650
+					'messages_settings_message_types_help_tab' => array(
651
+						'title'    => esc_html__('Activating / Deactivating Message Types', 'event_espresso'),
652
+						'filename' => 'messages_settings_message_types',
653
+					),
654
+					'messages_settings_messengers_help_tab'    => array(
655
+						'title'    => esc_html__('Activating / Deactivating Messengers', 'event_espresso'),
656
+						'filename' => 'messages_settings_messengers',
657
+					),
658
+				),
659
+				'help_tour'     => array('Messages_Settings_Help_Tour'),
660
+				'require_nonce' => false,
661
+			),
662
+		);
663
+	}
664
+
665
+
666
+	protected function _add_screen_options()
667
+	{
668
+		// todo
669
+	}
670
+
671
+
672
+	protected function _add_screen_options_global_mtps()
673
+	{
674
+		/**
675
+		 * Note: the reason for the value swap here on $this->_admin_page_title is because $this->_per_page_screen_options
676
+		 * uses the $_admin_page_title property and we want different outputs in the different spots.
677
+		 */
678
+		$page_title = $this->_admin_page_title;
679
+		$this->_admin_page_title = esc_html__('Global Message Templates', 'event_espresso');
680
+		$this->_per_page_screen_option();
681
+		$this->_admin_page_title = $page_title;
682
+	}
683
+
684
+
685
+	protected function _add_screen_options_default()
686
+	{
687
+		$this->_admin_page_title = esc_html__('Message Activity', 'event_espresso');
688
+		$this->_per_page_screen_option();
689
+	}
690
+
691
+
692
+	// none of the below group are currently used for Messages
693
+	protected function _add_feature_pointers()
694
+	{
695
+	}
696
+
697
+	public function admin_init()
698
+	{
699
+	}
700
+
701
+	public function admin_notices()
702
+	{
703
+	}
704
+
705
+	public function admin_footer_scripts()
706
+	{
707
+	}
708
+
709
+
710
+	public function messages_help_tab()
711
+	{
712
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_help_tab.template.php');
713
+	}
714
+
715
+
716
+	public function messengers_help_tab()
717
+	{
718
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messenger_help_tab.template.php');
719
+	}
720
+
721
+
722
+	public function message_types_help_tab()
723
+	{
724
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_type_help_tab.template.php');
725
+	}
726
+
727
+
728
+	public function messages_overview_help_tab()
729
+	{
730
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_overview_help_tab.template.php');
731
+	}
732
+
733
+
734
+	public function message_templates_help_tab()
735
+	{
736
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_templates_help_tab.template.php');
737
+	}
738
+
739
+
740
+	public function edit_message_template_help_tab()
741
+	{
742
+		$args['img1'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/editor.png' . '" alt="'
743
+						. esc_attr__('Editor Title', 'event_espresso')
744
+						. '" />';
745
+		$args['img2'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/switch-context.png' . '" alt="'
746
+						. esc_attr__('Context Switcher and Preview', 'event_espresso')
747
+						. '" />';
748
+		$args['img3'] = '<img class="left" src="' . EE_MSG_ASSETS_URL . 'images/form-fields.png' . '" alt="'
749
+						. esc_attr__('Message Template Form Fields', 'event_espresso')
750
+						. '" />';
751
+		$args['img4'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/shortcodes-metabox.png' . '" alt="'
752
+						. esc_attr__('Shortcodes Metabox', 'event_espresso')
753
+						. '" />';
754
+		$args['img5'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/publish-meta-box.png' . '" alt="'
755
+						. esc_attr__('Publish Metabox', 'event_espresso')
756
+						. '" />';
757
+		EEH_Template::display_template(
758
+			EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_templates_editor_help_tab.template.php',
759
+			$args
760
+		);
761
+	}
762
+
763
+
764
+	public function message_template_shortcodes_help_tab()
765
+	{
766
+		$this->_set_shortcodes();
767
+		$args['shortcodes'] = $this->_shortcodes;
768
+		EEH_Template::display_template(
769
+			EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_shortcodes_help_tab.template.php',
770
+			$args
771
+		);
772
+	}
773
+
774
+
775
+	public function preview_message_help_tab()
776
+	{
777
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_preview_help_tab.template.php');
778
+	}
779
+
780
+
781
+	public function settings_help_tab()
782
+	{
783
+		$args['img1'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-active.png'
784
+						. '" alt="' . esc_attr__('Active Email Tab', 'event_espresso') . '" />';
785
+		$args['img2'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-inactive.png'
786
+						. '" alt="' . esc_attr__('Inactive Email Tab', 'event_espresso') . '" />';
787
+		$args['img3'] = '<div class="switch">'
788
+						. '<input class="ee-on-off-toggle ee-toggle-round-flat"'
789
+						. ' type="checkbox" checked="checked">'
790
+						. '<label for="ee-on-off-toggle-on"></label>'
791
+						. '</div>';
792
+		$args['img4'] = '<div class="switch">'
793
+						. '<input class="ee-on-off-toggle ee-toggle-round-flat"'
794
+						. ' type="checkbox">'
795
+						. '<label for="ee-on-off-toggle-on"></label>'
796
+						. '</div>';
797
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_settings_help_tab.template.php', $args);
798
+	}
799
+
800
+
801
+	public function load_scripts_styles()
802
+	{
803
+		wp_register_style('espresso_ee_msg', EE_MSG_ASSETS_URL . 'ee_message_admin.css', EVENT_ESPRESSO_VERSION);
804
+		wp_enqueue_style('espresso_ee_msg');
805
+
806
+		wp_register_script(
807
+			'ee-messages-settings',
808
+			EE_MSG_ASSETS_URL . 'ee-messages-settings.js',
809
+			array('jquery-ui-droppable', 'ee-serialize-full-array'),
810
+			EVENT_ESPRESSO_VERSION,
811
+			true
812
+		);
813
+		wp_register_script(
814
+			'ee-msg-list-table-js',
815
+			EE_MSG_ASSETS_URL . 'ee_message_admin_list_table.js',
816
+			array('ee-dialog'),
817
+			EVENT_ESPRESSO_VERSION
818
+		);
819
+	}
820
+
821
+
822
+	public function load_scripts_styles_default()
823
+	{
824
+		wp_enqueue_script('ee-msg-list-table-js');
825
+	}
826
+
827
+
828
+	public function wp_editor_css($mce_css)
829
+	{
830
+		// if we're on the edit_message_template route
831
+		if ($this->_req_action === 'edit_message_template' && $this->_active_messenger instanceof EE_messenger) {
832
+			$message_type_name = $this->_active_message_type_name;
833
+
834
+			// we're going to REPLACE the existing mce css
835
+			// we need to get the css file location from the active messenger
836
+			$mce_css = $this->_active_messenger->get_variation(
837
+				$this->_template_pack,
838
+				$message_type_name,
839
+				true,
840
+				'wpeditor',
841
+				$this->_variation
842
+			);
843
+		}
844
+
845
+		return $mce_css;
846
+	}
847
+
848
+
849
+	public function load_scripts_styles_edit_message_template()
850
+	{
851
+
852
+		$this->_set_shortcodes();
853
+
854
+		EE_Registry::$i18n_js_strings['confirm_default_reset'] = sprintf(
855
+			esc_html__(
856
+				'Are you sure you want to reset the %s %s message templates?  Remember continuing will reset the templates for all contexts in this messenger and message type group.',
857
+				'event_espresso'
858
+			),
859
+			$this->_message_template_group->messenger_obj()->label['singular'],
860
+			$this->_message_template_group->message_type_obj()->label['singular']
861
+		);
862
+		EE_Registry::$i18n_js_strings['confirm_switch_template_pack'] = esc_html__(
863
+			'Switching the template pack for a messages template will reset the content for the template so the new layout is loaded.  Any custom content in the existing template will be lost. Are you sure you wish to do this?',
864
+			'event_espresso'
865
+		);
866
+		EE_Registry::$i18n_js_strings['server_error'] = esc_html__(
867
+			'An unknown error occurred on the server while attempting to process your request. Please refresh the page and try again or contact support.',
868
+			'event_espresso'
869
+		);
870
+
871
+		wp_register_script(
872
+			'ee_msgs_edit_js',
873
+			EE_MSG_ASSETS_URL . 'ee_message_editor.js',
874
+			array('jquery'),
875
+			EVENT_ESPRESSO_VERSION
876
+		);
877
+
878
+		wp_enqueue_script('ee_admin_js');
879
+		wp_enqueue_script('ee_msgs_edit_js');
880
+
881
+		// add in special css for tiny_mce
882
+		add_filter('mce_css', array($this, 'wp_editor_css'));
883
+	}
884
+
885
+
886
+	public function load_scripts_styles_display_preview_message()
887
+	{
888
+
889
+		$this->_set_message_template_group();
890
+
891
+		if (isset($this->_req_data['messenger'])) {
892
+			$this->_active_messenger = $this->_message_resource_manager->get_active_messenger(
893
+				$this->_req_data['messenger']
894
+			);
895
+		}
896
+
897
+		$message_type_name = isset($this->_req_data['message_type']) ? $this->_req_data['message_type'] : '';
898
+
899
+
900
+		wp_enqueue_style(
901
+			'espresso_preview_css',
902
+			$this->_active_messenger->get_variation(
903
+				$this->_template_pack,
904
+				$message_type_name,
905
+				true,
906
+				'preview',
907
+				$this->_variation
908
+			)
909
+		);
910
+	}
911
+
912
+
913
+	public function load_scripts_styles_settings()
914
+	{
915
+		wp_register_style(
916
+			'ee-message-settings',
917
+			EE_MSG_ASSETS_URL . 'ee_message_settings.css',
918
+			array(),
919
+			EVENT_ESPRESSO_VERSION
920
+		);
921
+		wp_enqueue_style('ee-text-links');
922
+		wp_enqueue_style('ee-message-settings');
923
+		wp_enqueue_script('ee-messages-settings');
924
+	}
925
+
926
+
927
+	/**
928
+	 * set views array for List Table
929
+	 */
930
+	public function _set_list_table_views_global_mtps()
931
+	{
932
+		$this->_views = array(
933
+			'in_use' => array(
934
+				'slug'  => 'in_use',
935
+				'label' => esc_html__('In Use', 'event_espresso'),
936
+				'count' => 0,
937
+			),
938
+		);
939
+	}
940
+
941
+
942
+	/**
943
+	 * Set views array for the Custom Template List Table
944
+	 */
945
+	public function _set_list_table_views_custom_mtps()
946
+	{
947
+		$this->_set_list_table_views_global_mtps();
948
+		$this->_views['in_use']['bulk_action'] = array(
949
+			'trash_message_template' => esc_html__('Move to Trash', 'event_espresso'),
950
+		);
951
+	}
952
+
953
+
954
+	/**
955
+	 * set views array for message queue list table
956
+	 *
957
+	 * @throws InvalidDataTypeException
958
+	 * @throws InvalidInterfaceException
959
+	 * @throws InvalidArgumentException
960
+	 * @throws EE_Error
961
+	 * @throws ReflectionException
962
+	 */
963
+	public function _set_list_table_views_default()
964
+	{
965
+		EE_Registry::instance()->load_helper('Template');
966
+
967
+		$common_bulk_actions = EE_Registry::instance()->CAP->current_user_can(
968
+			'ee_send_message',
969
+			'message_list_table_bulk_actions'
970
+		)
971
+			? array(
972
+				'generate_now'          => esc_html__('Generate Now', 'event_espresso'),
973
+				'generate_and_send_now' => esc_html__('Generate and Send Now', 'event_espresso'),
974
+				'queue_for_resending'   => esc_html__('Queue for Resending', 'event_espresso'),
975
+				'send_now'              => esc_html__('Send Now', 'event_espresso'),
976
+			)
977
+			: array();
978
+
979
+		$delete_bulk_action = EE_Registry::instance()->CAP->current_user_can(
980
+			'ee_delete_messages',
981
+			'message_list_table_bulk_actions'
982
+		)
983
+			? array('delete_ee_messages' => esc_html__('Delete Messages', 'event_espresso'))
984
+			: array();
985
+
986
+
987
+		$this->_views = array(
988
+			'all' => array(
989
+				'slug'        => 'all',
990
+				'label'       => esc_html__('All', 'event_espresso'),
991
+				'count'       => 0,
992
+				'bulk_action' => array_merge($common_bulk_actions, $delete_bulk_action),
993
+			),
994
+		);
995
+
996
+
997
+		foreach (EEM_Message::instance()->all_statuses() as $status) {
998
+			if ($status === EEM_Message::status_debug_only && ! EEM_Message::debug()) {
999
+				continue;
1000
+			}
1001
+			$status_bulk_actions = $common_bulk_actions;
1002
+			// unset bulk actions not applying to status
1003
+			if (! empty($status_bulk_actions)) {
1004
+				switch ($status) {
1005
+					case EEM_Message::status_idle:
1006
+					case EEM_Message::status_resend:
1007
+						$status_bulk_actions['send_now'] = $common_bulk_actions['send_now'];
1008
+						break;
1009
+
1010
+					case EEM_Message::status_failed:
1011
+					case EEM_Message::status_debug_only:
1012
+					case EEM_Message::status_messenger_executing:
1013
+						$status_bulk_actions = array();
1014
+						break;
1015
+
1016
+					case EEM_Message::status_incomplete:
1017
+						unset($status_bulk_actions['queue_for_resending'], $status_bulk_actions['send_now']);
1018
+						break;
1019
+
1020
+					case EEM_Message::status_retry:
1021
+					case EEM_Message::status_sent:
1022
+						unset($status_bulk_actions['generate_now'], $status_bulk_actions['generate_and_send_now']);
1023
+						break;
1024
+				}
1025
+			}
1026
+
1027
+			// skip adding messenger executing status to views because it will be included with the Failed view.
1028
+			if ($status === EEM_Message::status_messenger_executing) {
1029
+				continue;
1030
+			}
1031
+
1032
+			$this->_views[ strtolower($status) ] = array(
1033
+				'slug'        => strtolower($status),
1034
+				'label'       => EEH_Template::pretty_status($status, false, 'sentence'),
1035
+				'count'       => 0,
1036
+				'bulk_action' => array_merge($status_bulk_actions, $delete_bulk_action),
1037
+			);
1038
+		}
1039
+	}
1040
+
1041
+
1042
+	protected function _ee_default_messages_overview_list_table()
1043
+	{
1044
+		$this->_admin_page_title = esc_html__('Default Message Templates', 'event_espresso');
1045
+		$this->display_admin_list_table_page_with_no_sidebar();
1046
+	}
1047
+
1048
+
1049
+	protected function _message_queue_list_table()
1050
+	{
1051
+		$this->_search_btn_label = esc_html__('Message Activity', 'event_espresso');
1052
+		$this->_template_args['per_column'] = 6;
1053
+		$this->_template_args['after_list_table'] = $this->_display_legend($this->_message_legend_items());
1054
+		$this->_template_args['before_list_table'] = '<h3>'
1055
+													 . EEM_Message::instance()->get_pretty_label_for_results()
1056
+													 . '</h3>';
1057
+		$this->display_admin_list_table_page_with_no_sidebar();
1058
+	}
1059
+
1060
+
1061
+	protected function _message_legend_items()
1062
+	{
1063
+
1064
+		$action_css_classes = EEH_MSG_Template::get_message_action_icons();
1065
+		$action_items = array();
1066
+
1067
+		foreach ($action_css_classes as $action_item => $action_details) {
1068
+			if ($action_item === 'see_notifications_for') {
1069
+				continue;
1070
+			}
1071
+			$action_items[ $action_item ] = array(
1072
+				'class' => $action_details['css_class'],
1073
+				'desc'  => $action_details['label'],
1074
+			);
1075
+		}
1076
+
1077
+		/** @type array $status_items status legend setup */
1078
+		$status_items = array(
1079
+			'sent_status'                => array(
1080
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_sent,
1081
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_sent, false, 'sentence'),
1082
+			),
1083
+			'idle_status'                => array(
1084
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_idle,
1085
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_idle, false, 'sentence'),
1086
+			),
1087
+			'failed_status'              => array(
1088
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_failed,
1089
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_failed, false, 'sentence'),
1090
+			),
1091
+			'messenger_executing_status' => array(
1092
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_messenger_executing,
1093
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_messenger_executing, false, 'sentence'),
1094
+			),
1095
+			'resend_status'              => array(
1096
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_resend,
1097
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_resend, false, 'sentence'),
1098
+			),
1099
+			'incomplete_status'          => array(
1100
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_incomplete,
1101
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_incomplete, false, 'sentence'),
1102
+			),
1103
+			'retry_status'               => array(
1104
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_retry,
1105
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_retry, false, 'sentence'),
1106
+			),
1107
+		);
1108
+		if (EEM_Message::debug()) {
1109
+			$status_items['debug_only_status'] = array(
1110
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_debug_only,
1111
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_debug_only, false, 'sentence'),
1112
+			);
1113
+		}
1114
+
1115
+		return array_merge($action_items, $status_items);
1116
+	}
1117
+
1118
+
1119
+	protected function _custom_mtps_preview()
1120
+	{
1121
+		$this->_admin_page_title = esc_html__('Custom Message Templates (Preview)', 'event_espresso');
1122
+		$this->_template_args['preview_img'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/custom_mtps_preview.png"'
1123
+											   . ' alt="' . esc_attr__(
1124
+												   'Preview Custom Message Templates screenshot',
1125
+												   'event_espresso'
1126
+											   ) . '" />';
1127
+		$this->_template_args['preview_text'] = '<strong>'
1128
+												. esc_html__(
1129
+													'Custom Message Templates is a feature that is only available in the premium version of Event Espresso 4 which is available with a support license purchase on EventEspresso.com. With the Custom Message Templates feature, you are able to create custom message templates and assign them on a per-event basis.',
1130
+													'event_espresso'
1131
+												)
1132
+												. '</strong>';
1133
+
1134
+		$this->display_admin_caf_preview_page('custom_message_types', false);
1135
+	}
1136
+
1137
+
1138
+	/**
1139
+	 * get_message_templates
1140
+	 * This gets all the message templates for listing on the overview list.
1141
+	 *
1142
+	 * @access public
1143
+	 * @param int    $perpage the amount of templates groups to show per page
1144
+	 * @param string $type    the current _view we're getting templates for
1145
+	 * @param bool   $count   return count?
1146
+	 * @param bool   $all     disregard any paging info (get all data);
1147
+	 * @param bool   $global  whether to return just global (true) or custom templates (false)
1148
+	 * @return array
1149
+	 * @throws EE_Error
1150
+	 * @throws InvalidArgumentException
1151
+	 * @throws InvalidDataTypeException
1152
+	 * @throws InvalidInterfaceException
1153
+	 */
1154
+	public function get_message_templates(
1155
+		$perpage = 10,
1156
+		$type = 'in_use',
1157
+		$count = false,
1158
+		$all = false,
1159
+		$global = true
1160
+	) {
1161
+
1162
+		$MTP = EEM_Message_Template_Group::instance();
1163
+
1164
+		$this->_req_data['orderby'] = empty($this->_req_data['orderby']) ? 'GRP_ID' : $this->_req_data['orderby'];
1165
+		$orderby = $this->_req_data['orderby'];
1166
+
1167
+		$order = (isset($this->_req_data['order']) && ! empty($this->_req_data['order']))
1168
+			? $this->_req_data['order']
1169
+			: 'ASC';
1170
+
1171
+		$current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged'])
1172
+			? $this->_req_data['paged']
1173
+			: 1;
1174
+		$per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage'])
1175
+			? $this->_req_data['perpage']
1176
+			: $perpage;
1177
+
1178
+		$offset = ($current_page - 1) * $per_page;
1179
+		$limit = $all ? null : array($offset, $per_page);
1180
+
1181
+
1182
+		// options will match what is in the _views array property
1183
+		switch ($type) {
1184
+			case 'in_use':
1185
+				$templates = $MTP->get_all_active_message_templates($orderby, $order, $limit, $count, $global, true);
1186
+				break;
1187
+			default:
1188
+				$templates = $MTP->get_all_trashed_grouped_message_templates($orderby, $order, $limit, $count, $global);
1189
+		}
1190
+
1191
+		return $templates;
1192
+	}
1193
+
1194
+
1195
+	/**
1196
+	 * filters etc might need a list of installed message_types
1197
+	 *
1198
+	 * @return array an array of message type objects
1199
+	 */
1200
+	public function get_installed_message_types()
1201
+	{
1202
+		$installed_message_types = $this->_message_resource_manager->installed_message_types();
1203
+		$installed = array();
1204
+
1205
+		foreach ($installed_message_types as $message_type) {
1206
+			$installed[ $message_type->name ] = $message_type;
1207
+		}
1208
+
1209
+		return $installed;
1210
+	}
1211
+
1212
+
1213
+	/**
1214
+	 * _add_message_template
1215
+	 *
1216
+	 * This is used when creating a custom template. All Custom Templates start based off another template.
1217
+	 *
1218
+	 * @param string $message_type
1219
+	 * @param string $messenger
1220
+	 * @param string $GRP_ID
1221
+	 *
1222
+	 * @throws EE_error
1223
+	 */
1224
+	protected function _add_message_template($message_type = '', $messenger = '', $GRP_ID = '')
1225
+	{
1226
+		// set values override any request data
1227
+		$message_type = ! empty($message_type) ? $message_type : '';
1228
+		$message_type = empty($message_type) && ! empty($this->_req_data['message_type'])
1229
+			? $this->_req_data['message_type']
1230
+			: $message_type;
1231
+
1232
+		$messenger = ! empty($messenger) ? $messenger : '';
1233
+		$messenger = empty($messenger) && ! empty($this->_req_data['messenger'])
1234
+			? $this->_req_data['messenger']
1235
+			: $messenger;
1236
+
1237
+		$GRP_ID = ! empty($GRP_ID) ? $GRP_ID : '';
1238
+		$GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : $GRP_ID;
1239
+
1240
+		// we need messenger and message type.  They should be coming from the event editor. If not here then return error
1241
+		if (empty($message_type) || empty($messenger)) {
1242
+			throw new EE_Error(
1243
+				esc_html__(
1244
+					'Sorry, but we can\'t create new templates because we\'re missing the messenger or message type',
1245
+					'event_espresso'
1246
+				)
1247
+			);
1248
+		}
1249
+
1250
+		// we need the GRP_ID for the template being used as the base for the new template
1251
+		if (empty($GRP_ID)) {
1252
+			throw new EE_Error(
1253
+				esc_html__(
1254
+					'In order to create a custom message template the GRP_ID of the template being used as a base is needed',
1255
+					'event_espresso'
1256
+				)
1257
+			);
1258
+		}
1259
+
1260
+		// let's just make sure the template gets generated!
1261
+
1262
+		// we need to reassign some variables for what the insert is expecting
1263
+		$this->_req_data['MTP_messenger'] = $messenger;
1264
+		$this->_req_data['MTP_message_type'] = $message_type;
1265
+		$this->_req_data['GRP_ID'] = $GRP_ID;
1266
+		$this->_insert_or_update_message_template(true);
1267
+	}
1268
+
1269
+
1270
+	/**
1271
+	 * public wrapper for the _add_message_template method
1272
+	 *
1273
+	 * @param string $message_type     message type slug
1274
+	 * @param string $messenger        messenger slug
1275
+	 * @param int    $GRP_ID           GRP_ID for the related message template group this new template will be based
1276
+	 *                                 off of.
1277
+	 * @throws EE_error
1278
+	 */
1279
+	public function add_message_template($message_type, $messenger, $GRP_ID)
1280
+	{
1281
+		$this->_add_message_template($message_type, $messenger, $GRP_ID);
1282
+	}
1283
+
1284
+
1285
+	/**
1286
+	 * _edit_message_template
1287
+	 *
1288
+	 * @access protected
1289
+	 * @return void
1290
+	 * @throws InvalidIdentifierException
1291
+	 * @throws DomainException
1292
+	 * @throws EE_Error
1293
+	 * @throws InvalidArgumentException
1294
+	 * @throws ReflectionException
1295
+	 * @throws InvalidDataTypeException
1296
+	 * @throws InvalidInterfaceException
1297
+	 */
1298
+	protected function _edit_message_template()
1299
+	{
1300
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1301
+		$template_fields = '';
1302
+		$sidebar_fields = '';
1303
+		// we filter the tinyMCE settings to remove the validation since message templates by their nature will not have
1304
+		// valid html in the templates.
1305
+		add_filter('tiny_mce_before_init', array($this, 'filter_tinymce_init'), 10, 2);
1306
+
1307
+		$GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id'])
1308
+			? absint($this->_req_data['id'])
1309
+			: false;
1310
+
1311
+		$this->_set_shortcodes(); // this also sets the _message_template property.
1312
+		$message_template_group = $this->_message_template_group;
1313
+		$c_label = $message_template_group->context_label();
1314
+		$c_config = $message_template_group->contexts_config();
1315
+
1316
+		reset($c_config);
1317
+		$context = isset($this->_req_data['context']) && ! empty($this->_req_data['context'])
1318
+			? strtolower($this->_req_data['context'])
1319
+			: key($c_config);
1320
+
1321
+
1322
+		if (empty($GRP_ID)) {
1323
+			$action = 'insert_message_template';
1324
+			$edit_message_template_form_url = add_query_arg(
1325
+				array('action' => $action, 'noheader' => true),
1326
+				EE_MSG_ADMIN_URL
1327
+			);
1328
+		} else {
1329
+			$action = 'update_message_template';
1330
+			$edit_message_template_form_url = add_query_arg(
1331
+				array('action' => $action, 'noheader' => true),
1332
+				EE_MSG_ADMIN_URL
1333
+			);
1334
+		}
1335
+
1336
+		// set active messenger for this view
1337
+		$this->_active_messenger = $this->_message_resource_manager->get_active_messenger(
1338
+			$message_template_group->messenger()
1339
+		);
1340
+		$this->_active_message_type_name = $message_template_group->message_type();
1341
+
1342
+
1343
+		// Do we have any validation errors?
1344
+		$validators = $this->_get_transient();
1345
+		$v_fields = ! empty($validators) ? array_keys($validators) : array();
1346
+
1347
+
1348
+		// we need to assemble the title from Various details
1349
+		$context_label = sprintf(
1350
+			esc_html__('(%s %s)', 'event_espresso'),
1351
+			$c_config[ $context ]['label'],
1352
+			ucwords($c_label['label'])
1353
+		);
1354
+
1355
+		$title = sprintf(
1356
+			esc_html__(' %s %s Template %s', 'event_espresso'),
1357
+			ucwords($message_template_group->messenger_obj()->label['singular']),
1358
+			ucwords($message_template_group->message_type_obj()->label['singular']),
1359
+			$context_label
1360
+		);
1361
+
1362
+		$this->_template_args['GRP_ID'] = $GRP_ID;
1363
+		$this->_template_args['message_template'] = $message_template_group;
1364
+		$this->_template_args['is_extra_fields'] = false;
1365
+
1366
+
1367
+		// let's get EEH_MSG_Template so we can get template form fields
1368
+		$template_field_structure = EEH_MSG_Template::get_fields(
1369
+			$message_template_group->messenger(),
1370
+			$message_template_group->message_type()
1371
+		);
1372
+
1373
+		if (! $template_field_structure) {
1374
+			$template_field_structure = false;
1375
+			$template_fields = esc_html__(
1376
+				'There was an error in assembling the fields for this display (you should see an error message)',
1377
+				'event_espresso'
1378
+			);
1379
+		}
1380
+
1381
+
1382
+		$message_templates = $message_template_group->context_templates();
1383
+
1384
+
1385
+		// if we have the extra key.. then we need to remove the content index from the template_field_structure as it
1386
+		// will get handled in the "extra" array.
1387
+		if (is_array($template_field_structure[ $context ]) && isset($template_field_structure[ $context ]['extra'])) {
1388
+			foreach ($template_field_structure[ $context ]['extra'] as $reference_field => $new_fields) {
1389
+				unset($template_field_structure[ $context ][ $reference_field ]);
1390
+			}
1391
+		}
1392
+
1393
+		// let's loop through the template_field_structure and actually assemble the input fields!
1394
+		if (! empty($template_field_structure)) {
1395
+			foreach ($template_field_structure[ $context ] as $template_field => $field_setup_array) {
1396
+				// if this is an 'extra' template field then we need to remove any existing fields that are keyed up in
1397
+				// the extra array and reset them.
1398
+				if ($template_field === 'extra') {
1399
+					$this->_template_args['is_extra_fields'] = true;
1400
+					foreach ($field_setup_array as $reference_field => $new_fields_array) {
1401
+						$message_template = $message_templates[ $context ][ $reference_field ];
1402
+						$content = $message_template instanceof EE_Message_Template
1403
+							? $message_template->get('MTP_content')
1404
+							: '';
1405
+						foreach ($new_fields_array as $extra_field => $extra_array) {
1406
+							// let's verify if we need this extra field via the shortcodes parameter.
1407
+							$continue = false;
1408
+							if (isset($extra_array['shortcodes_required'])) {
1409
+								foreach ((array) $extra_array['shortcodes_required'] as $shortcode) {
1410
+									if (! array_key_exists($shortcode, $this->_shortcodes)) {
1411
+										$continue = true;
1412
+									}
1413
+								}
1414
+								if ($continue) {
1415
+									continue;
1416
+								}
1417
+							}
1418
+
1419
+							$field_id = $reference_field
1420
+										. '-'
1421
+										. $extra_field
1422
+										. '-content';
1423
+							$template_form_fields[ $field_id ] = $extra_array;
1424
+							$template_form_fields[ $field_id ]['name'] = 'MTP_template_fields['
1425
+																		 . $reference_field
1426
+																		 . '][content]['
1427
+																		 . $extra_field . ']';
1428
+							$css_class = isset($extra_array['css_class'])
1429
+								? $extra_array['css_class']
1430
+								: '';
1431
+
1432
+							$template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
1433
+																			  && in_array($extra_field, $v_fields, true)
1434
+																			  &&
1435
+																			  (
1436
+																				  is_array($validators[ $extra_field ])
1437
+																				  && isset($validators[ $extra_field ]['msg'])
1438
+																			  )
1439
+								? 'validate-error ' . $css_class
1440
+								: $css_class;
1441
+
1442
+							$template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
1443
+																		  && isset($content[ $extra_field ])
1444
+								? $content[ $extra_field ]
1445
+								: '';
1446
+
1447
+							// do we have a validation error?  if we do then let's use that value instead
1448
+							$template_form_fields[ $field_id ]['value'] = isset($validators[ $extra_field ])
1449
+								? $validators[ $extra_field ]['value']
1450
+								: $template_form_fields[ $field_id ]['value'];
1451
+
1452
+
1453
+							$template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
1454
+
1455
+							// shortcode selector
1456
+							$field_name_to_use = $extra_field === 'main'
1457
+								? 'content'
1458
+								: $extra_field;
1459
+							$template_form_fields[ $field_id ]['append_content'] = $this->_get_shortcode_selector(
1460
+								$field_name_to_use,
1461
+								$field_id
1462
+							);
1463
+
1464
+							if (isset($extra_array['input']) && $extra_array['input'] === 'wp_editor') {
1465
+								// we want to decode the entities
1466
+								$template_form_fields[ $field_id ]['value'] = $template_form_fields[ $field_id ]['value'];
1467
+							}/**/
1468
+						}
1469
+						$templatefield_MTP_id = $reference_field . '-MTP_ID';
1470
+						$templatefield_templatename_id = $reference_field . '-name';
1471
+
1472
+						$template_form_fields[ $templatefield_MTP_id ] = array(
1473
+							'name'       => 'MTP_template_fields[' . $reference_field . '][MTP_ID]',
1474
+							'label'      => null,
1475
+							'input'      => 'hidden',
1476
+							'type'       => 'int',
1477
+							'required'   => false,
1478
+							'validation' => false,
1479
+							'value'      => ! empty($message_templates) ? $message_template->ID() : '',
1480
+							'css_class'  => '',
1481
+							'format'     => '%d',
1482
+							'db-col'     => 'MTP_ID',
1483
+						);
1484
+
1485
+						$template_form_fields[ $templatefield_templatename_id ] = array(
1486
+							'name'       => 'MTP_template_fields[' . $reference_field . '][name]',
1487
+							'label'      => null,
1488
+							'input'      => 'hidden',
1489
+							'type'       => 'string',
1490
+							'required'   => false,
1491
+							'validation' => true,
1492
+							'value'      => $reference_field,
1493
+							'css_class'  => '',
1494
+							'format'     => '%s',
1495
+							'db-col'     => 'MTP_template_field',
1496
+						);
1497
+					}
1498
+					continue; // skip the next stuff, we got the necessary fields here for this dataset.
1499
+				} else {
1500
+					$field_id = $template_field . '-content';
1501
+					$template_form_fields[ $field_id ] = $field_setup_array;
1502
+					$template_form_fields[ $field_id ]['name'] = 'MTP_template_fields[' . $template_field . '][content]';
1503
+					$message_template = isset($message_templates[ $context ][ $template_field ])
1504
+						? $message_templates[ $context ][ $template_field ]
1505
+						: null;
1506
+					$template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
1507
+																  && is_array($message_templates[ $context ])
1508
+																  && $message_template instanceof EE_Message_Template
1509
+						? $message_template->get('MTP_content')
1510
+						: '';
1511
+
1512
+					// do we have a validator error for this field?  if we do then we'll use that value instead
1513
+					$template_form_fields[ $field_id ]['value'] = isset($validators[ $template_field ])
1514
+						? $validators[ $template_field ]['value']
1515
+						: $template_form_fields[ $field_id ]['value'];
1516
+
1517
+
1518
+					$template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
1519
+					$css_class = isset($field_setup_array['css_class'])
1520
+						? $field_setup_array['css_class']
1521
+						: '';
1522
+					$template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
1523
+																	  && in_array($template_field, $v_fields, true)
1524
+																	  && isset($validators[ $template_field ]['msg'])
1525
+						? 'validate-error ' . $css_class
1526
+						: $css_class;
1527
+
1528
+					// shortcode selector
1529
+					$template_form_fields[ $field_id ]['append_content'] = $this->_get_shortcode_selector(
1530
+						$template_field,
1531
+						$field_id
1532
+					);
1533
+				}
1534
+
1535
+				// k took care of content field(s) now let's take care of others.
1536
+
1537
+				$templatefield_MTP_id = $template_field . '-MTP_ID';
1538
+				$templatefield_field_templatename_id = $template_field . '-name';
1539
+
1540
+				// foreach template field there are actually two form fields created
1541
+				$template_form_fields[ $templatefield_MTP_id ] = array(
1542
+					'name'       => 'MTP_template_fields[' . $template_field . '][MTP_ID]',
1543
+					'label'      => null,
1544
+					'input'      => 'hidden',
1545
+					'type'       => 'int',
1546
+					'required'   => false,
1547
+					'validation' => true,
1548
+					'value'      => $message_template instanceof EE_Message_Template ? $message_template->ID() : '',
1549
+					'css_class'  => '',
1550
+					'format'     => '%d',
1551
+					'db-col'     => 'MTP_ID',
1552
+				);
1553
+
1554
+				$template_form_fields[ $templatefield_field_templatename_id ] = array(
1555
+					'name'       => 'MTP_template_fields[' . $template_field . '][name]',
1556
+					'label'      => null,
1557
+					'input'      => 'hidden',
1558
+					'type'       => 'string',
1559
+					'required'   => false,
1560
+					'validation' => true,
1561
+					'value'      => $template_field,
1562
+					'css_class'  => '',
1563
+					'format'     => '%s',
1564
+					'db-col'     => 'MTP_template_field',
1565
+				);
1566
+			}
1567
+
1568
+			// add other fields
1569
+			$template_form_fields['ee-msg-current-context'] = array(
1570
+				'name'       => 'MTP_context',
1571
+				'label'      => null,
1572
+				'input'      => 'hidden',
1573
+				'type'       => 'string',
1574
+				'required'   => false,
1575
+				'validation' => true,
1576
+				'value'      => $context,
1577
+				'css_class'  => '',
1578
+				'format'     => '%s',
1579
+				'db-col'     => 'MTP_context',
1580
+			);
1581
+
1582
+			$template_form_fields['ee-msg-grp-id'] = array(
1583
+				'name'       => 'GRP_ID',
1584
+				'label'      => null,
1585
+				'input'      => 'hidden',
1586
+				'type'       => 'int',
1587
+				'required'   => false,
1588
+				'validation' => true,
1589
+				'value'      => $GRP_ID,
1590
+				'css_class'  => '',
1591
+				'format'     => '%d',
1592
+				'db-col'     => 'GRP_ID',
1593
+			);
1594
+
1595
+			$template_form_fields['ee-msg-messenger'] = array(
1596
+				'name'       => 'MTP_messenger',
1597
+				'label'      => null,
1598
+				'input'      => 'hidden',
1599
+				'type'       => 'string',
1600
+				'required'   => false,
1601
+				'validation' => true,
1602
+				'value'      => $message_template_group->messenger(),
1603
+				'css_class'  => '',
1604
+				'format'     => '%s',
1605
+				'db-col'     => 'MTP_messenger',
1606
+			);
1607
+
1608
+			$template_form_fields['ee-msg-message-type'] = array(
1609
+				'name'       => 'MTP_message_type',
1610
+				'label'      => null,
1611
+				'input'      => 'hidden',
1612
+				'type'       => 'string',
1613
+				'required'   => false,
1614
+				'validation' => true,
1615
+				'value'      => $message_template_group->message_type(),
1616
+				'css_class'  => '',
1617
+				'format'     => '%s',
1618
+				'db-col'     => 'MTP_message_type',
1619
+			);
1620
+
1621
+			$sidebar_form_fields['ee-msg-is-global'] = array(
1622
+				'name'       => 'MTP_is_global',
1623
+				'label'      => esc_html__('Global Template', 'event_espresso'),
1624
+				'input'      => 'hidden',
1625
+				'type'       => 'int',
1626
+				'required'   => false,
1627
+				'validation' => true,
1628
+				'value'      => $message_template_group->get('MTP_is_global'),
1629
+				'css_class'  => '',
1630
+				'format'     => '%d',
1631
+				'db-col'     => 'MTP_is_global',
1632
+			);
1633
+
1634
+			$sidebar_form_fields['ee-msg-is-override'] = array(
1635
+				'name'       => 'MTP_is_override',
1636
+				'label'      => esc_html__('Override all custom', 'event_espresso'),
1637
+				'input'      => $message_template_group->is_global() ? 'checkbox' : 'hidden',
1638
+				'type'       => 'int',
1639
+				'required'   => false,
1640
+				'validation' => true,
1641
+				'value'      => $message_template_group->get('MTP_is_override'),
1642
+				'css_class'  => '',
1643
+				'format'     => '%d',
1644
+				'db-col'     => 'MTP_is_override',
1645
+			);
1646
+
1647
+			$sidebar_form_fields['ee-msg-is-active'] = array(
1648
+				'name'       => 'MTP_is_active',
1649
+				'label'      => esc_html__('Active Template', 'event_espresso'),
1650
+				'input'      => 'hidden',
1651
+				'type'       => 'int',
1652
+				'required'   => false,
1653
+				'validation' => true,
1654
+				'value'      => $message_template_group->is_active(),
1655
+				'css_class'  => '',
1656
+				'format'     => '%d',
1657
+				'db-col'     => 'MTP_is_active',
1658
+			);
1659
+
1660
+			$sidebar_form_fields['ee-msg-deleted'] = array(
1661
+				'name'       => 'MTP_deleted',
1662
+				'label'      => null,
1663
+				'input'      => 'hidden',
1664
+				'type'       => 'int',
1665
+				'required'   => false,
1666
+				'validation' => true,
1667
+				'value'      => $message_template_group->get('MTP_deleted'),
1668
+				'css_class'  => '',
1669
+				'format'     => '%d',
1670
+				'db-col'     => 'MTP_deleted',
1671
+			);
1672
+			$sidebar_form_fields['ee-msg-author'] = array(
1673
+				'name'       => 'MTP_user_id',
1674
+				'label'      => esc_html__('Author', 'event_espresso'),
1675
+				'input'      => 'hidden',
1676
+				'type'       => 'int',
1677
+				'required'   => false,
1678
+				'validation' => false,
1679
+				'value'      => $message_template_group->user(),
1680
+				'format'     => '%d',
1681
+				'db-col'     => 'MTP_user_id',
1682
+			);
1683
+
1684
+			$sidebar_form_fields['ee-msg-route'] = array(
1685
+				'name'  => 'action',
1686
+				'input' => 'hidden',
1687
+				'type'  => 'string',
1688
+				'value' => $action,
1689
+			);
1690
+
1691
+			$sidebar_form_fields['ee-msg-id'] = array(
1692
+				'name'  => 'id',
1693
+				'input' => 'hidden',
1694
+				'type'  => 'int',
1695
+				'value' => $GRP_ID,
1696
+			);
1697
+			$sidebar_form_fields['ee-msg-evt-nonce'] = array(
1698
+				'name'  => $action . '_nonce',
1699
+				'input' => 'hidden',
1700
+				'type'  => 'string',
1701
+				'value' => wp_create_nonce($action . '_nonce'),
1702
+			);
1703
+
1704
+			if (isset($this->_req_data['template_switch']) && $this->_req_data['template_switch']) {
1705
+				$sidebar_form_fields['ee-msg-template-switch'] = array(
1706
+					'name'  => 'template_switch',
1707
+					'input' => 'hidden',
1708
+					'type'  => 'int',
1709
+					'value' => 1,
1710
+				);
1711
+			}
1712
+
1713
+
1714
+			$template_fields = $this->_generate_admin_form_fields($template_form_fields);
1715
+			$sidebar_fields = $this->_generate_admin_form_fields($sidebar_form_fields);
1716
+		} //end if ( !empty($template_field_structure) )
1717
+
1718
+		// set extra content for publish box
1719
+		$this->_template_args['publish_box_extra_content'] = $sidebar_fields;
1720
+		$this->_set_publish_post_box_vars(
1721
+			'id',
1722
+			$GRP_ID,
1723
+			false,
1724
+			add_query_arg(
1725
+				array('action' => 'global_mtps'),
1726
+				$this->_admin_base_url
1727
+			)
1728
+		);
1729
+
1730
+		// add preview button
1731
+		$preview_url = parent::add_query_args_and_nonce(
1732
+			array(
1733
+				'message_type' => $message_template_group->message_type(),
1734
+				'messenger'    => $message_template_group->messenger(),
1735
+				'context'      => $context,
1736
+				'GRP_ID'       => $GRP_ID,
1737
+				'action'       => 'preview_message',
1738
+			),
1739
+			$this->_admin_base_url
1740
+		);
1741
+		$preview_button = '<a href="' . $preview_url . '" class="button-secondary messages-preview-button">'
1742
+						  . esc_html__('Preview', 'event_espresso')
1743
+						  . '</a>';
1744
+
1745
+
1746
+		// setup context switcher
1747
+		$context_switcher_args = array(
1748
+			'page'    => 'espresso_messages',
1749
+			'action'  => 'edit_message_template',
1750
+			'id'      => $GRP_ID,
1751
+			'context' => $context,
1752
+			'extra'   => $preview_button,
1753
+		);
1754
+		$this->_set_context_switcher($message_template_group, $context_switcher_args);
1755
+
1756
+
1757
+		// main box
1758
+		$this->_template_args['template_fields'] = $template_fields;
1759
+		$this->_template_args['sidebar_box_id'] = 'details';
1760
+		$this->_template_args['action'] = $action;
1761
+		$this->_template_args['context'] = $context;
1762
+		$this->_template_args['edit_message_template_form_url'] = $edit_message_template_form_url;
1763
+		$this->_template_args['learn_more_about_message_templates_link'] =
1764
+			$this->_learn_more_about_message_templates_link();
1765
+
1766
+
1767
+		$this->_template_args['before_admin_page_content'] = $this->add_context_switcher();
1768
+		$this->_template_args['before_admin_page_content'] .= $this->add_active_context_element(
1769
+			$message_template_group,
1770
+			$context,
1771
+			$context_label
1772
+		);
1773
+		$this->_template_args['before_admin_page_content'] .= $this->_add_form_element_before();
1774
+		$this->_template_args['after_admin_page_content'] = $this->_add_form_element_after();
1775
+
1776
+		$this->_template_path = $this->_template_args['GRP_ID']
1777
+			? EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_edit_meta_box.template.php'
1778
+			: EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_add_meta_box.template.php';
1779
+
1780
+		// send along EE_Message_Template_Group object for further template use.
1781
+		$this->_template_args['MTP'] = $message_template_group;
1782
+
1783
+		$this->_template_args['admin_page_content'] = EEH_Template::display_template(
1784
+			$this->_template_path,
1785
+			$this->_template_args,
1786
+			true
1787
+		);
1788
+
1789
+
1790
+		// finally, let's set the admin_page title
1791
+		$this->_admin_page_title = sprintf(__('Editing %s', 'event_espresso'), $title);
1792
+
1793
+
1794
+		// we need to take care of setting the shortcodes property for use elsewhere.
1795
+		$this->_set_shortcodes();
1796
+
1797
+
1798
+		// final template wrapper
1799
+		$this->display_admin_page_with_sidebar();
1800
+	}
1801
+
1802
+
1803
+	public function filter_tinymce_init($mceInit, $editor_id)
1804
+	{
1805
+		return $mceInit;
1806
+	}
1807
+
1808
+
1809
+	public function add_context_switcher()
1810
+	{
1811
+		return $this->_context_switcher;
1812
+	}
1813
+
1814
+
1815
+	/**
1816
+	 * Adds the activation/deactivation toggle for the message template context.
1817
+	 *
1818
+	 * @param EE_Message_Template_Group $message_template_group
1819
+	 * @param string                    $context
1820
+	 * @param string                    $context_label
1821
+	 * @return string
1822
+	 * @throws DomainException
1823
+	 * @throws EE_Error
1824
+	 * @throws InvalidIdentifierException
1825
+	 */
1826
+	protected function add_active_context_element(
1827
+		EE_Message_Template_Group $message_template_group,
1828
+		$context,
1829
+		$context_label
1830
+	) {
1831
+		$template_args = array(
1832
+			'context'                   => $context,
1833
+			'nonce'                     => wp_create_nonce('activate_' . $context . '_toggle_nonce'),
1834
+			'is_active'                 => $message_template_group->is_context_active($context),
1835
+			'on_off_action'             => $message_template_group->is_context_active($context)
1836
+				? 'context-off'
1837
+				: 'context-on',
1838
+			'context_label'             => str_replace(array('(', ')'), '', $context_label),
1839
+			'message_template_group_id' => $message_template_group->ID(),
1840
+		);
1841
+		return EEH_Template::display_template(
1842
+			EE_MSG_TEMPLATE_PATH . 'ee_msg_editor_active_context_element.template.php',
1843
+			$template_args,
1844
+			true
1845
+		);
1846
+	}
1847
+
1848
+
1849
+	/**
1850
+	 * Ajax callback for `toggle_context_template` ajax action.
1851
+	 * Handles toggling the message context on or off.
1852
+	 *
1853
+	 * @throws EE_Error
1854
+	 * @throws InvalidArgumentException
1855
+	 * @throws InvalidDataTypeException
1856
+	 * @throws InvalidIdentifierException
1857
+	 * @throws InvalidInterfaceException
1858
+	 */
1859
+	public function toggle_context_template()
1860
+	{
1861
+		$success = true;
1862
+		// check for required data
1863
+		if (! isset(
1864
+			$this->_req_data['message_template_group_id'],
1865
+			$this->_req_data['context'],
1866
+			$this->_req_data['status']
1867
+		)) {
1868
+			EE_Error::add_error(
1869
+				esc_html__('Required data for doing this action is not available.', 'event_espresso'),
1870
+				__FILE__,
1871
+				__FUNCTION__,
1872
+				__LINE__
1873
+			);
1874
+			$success = false;
1875
+		}
1876
+
1877
+		$nonce = isset($this->_req_data['toggle_context_nonce'])
1878
+			? sanitize_text_field($this->_req_data['toggle_context_nonce'])
1879
+			: '';
1880
+		$nonce_ref = 'activate_' . $this->_req_data['context'] . '_toggle_nonce';
1881
+		$this->_verify_nonce($nonce, $nonce_ref);
1882
+		$status = $this->_req_data['status'];
1883
+		if ($status !== 'off' && $status !== 'on') {
1884
+			EE_Error::add_error(
1885
+				sprintf(
1886
+					esc_html__('The given status (%s) is not valid. Must be "off" or "on"', 'event_espresso'),
1887
+					$this->_req_data['status']
1888
+				),
1889
+				__FILE__,
1890
+				__FUNCTION__,
1891
+				__LINE__
1892
+			);
1893
+			$success = false;
1894
+		}
1895
+		$message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID(
1896
+			$this->_req_data['message_template_group_id']
1897
+		);
1898
+		if (! $message_template_group instanceof EE_Message_Template_Group) {
1899
+			EE_Error::add_error(
1900
+				sprintf(
1901
+					esc_html__(
1902
+						'Unable to change the active state because the given id "%1$d" does not match a valid "%2$s"',
1903
+						'event_espresso'
1904
+					),
1905
+					$this->_req_data['message_template_group_id'],
1906
+					'EE_Message_Template_Group'
1907
+				),
1908
+				__FILE__,
1909
+				__FUNCTION__,
1910
+				__LINE__
1911
+			);
1912
+			$success = false;
1913
+		}
1914
+		if ($success) {
1915
+			$success = $status === 'off'
1916
+				? $message_template_group->deactivate_context($this->_req_data['context'])
1917
+				: $message_template_group->activate_context($this->_req_data['context']);
1918
+		}
1919
+		$this->_template_args['success'] = $success;
1920
+		$this->_return_json();
1921
+	}
1922
+
1923
+
1924
+	public function _add_form_element_before()
1925
+	{
1926
+		return '<form method="post" action="'
1927
+			   . $this->_template_args["edit_message_template_form_url"]
1928
+			   . '" id="ee-msg-edit-frm">';
1929
+	}
1930
+
1931
+	public function _add_form_element_after()
1932
+	{
1933
+		return '</form>';
1934
+	}
1935
+
1936
+
1937
+	/**
1938
+	 * This executes switching the template pack for a message template.
1939
+	 *
1940
+	 * @since 4.5.0
1941
+	 * @throws EE_Error
1942
+	 * @throws InvalidDataTypeException
1943
+	 * @throws InvalidInterfaceException
1944
+	 * @throws InvalidArgumentException
1945
+	 * @throws ReflectionException
1946
+	 */
1947
+	public function switch_template_pack()
1948
+	{
1949
+		$GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
1950
+		$template_pack = ! empty($this->_req_data['template_pack']) ? $this->_req_data['template_pack'] : '';
1951
+
1952
+		// verify we have needed values.
1953
+		if (empty($GRP_ID) || empty($template_pack)) {
1954
+			$this->_template_args['error'] = true;
1955
+			EE_Error::add_error(
1956
+				esc_html__('The required date for switching templates is not available.', 'event_espresso'),
1957
+				__FILE__,
1958
+				__FUNCTION__,
1959
+				__LINE__
1960
+			);
1961
+		} else {
1962
+			// get template, set the new template_pack and then reset to default
1963
+			/** @type EE_Message_Template_Group $message_template_group */
1964
+			$message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID);
1965
+
1966
+			$message_template_group->set_template_pack_name($template_pack);
1967
+			$this->_req_data['msgr'] = $message_template_group->messenger();
1968
+			$this->_req_data['mt'] = $message_template_group->message_type();
1969
+
1970
+			$query_args = $this->_reset_to_default_template();
1971
+
1972
+			if (empty($query_args['id'])) {
1973
+				EE_Error::add_error(
1974
+					esc_html__(
1975
+						'Something went wrong with switching the template pack. Please try again or contact EE support',
1976
+						'event_espresso'
1977
+					),
1978
+					__FILE__,
1979
+					__FUNCTION__,
1980
+					__LINE__
1981
+				);
1982
+				$this->_template_args['error'] = true;
1983
+			} else {
1984
+				$template_label = $message_template_group->get_template_pack()->label;
1985
+				$template_pack_labels = $message_template_group->messenger_obj()->get_supports_labels();
1986
+				EE_Error::add_success(
1987
+					sprintf(
1988
+						esc_html__(
1989
+							'This message template has been successfully switched to use the %1$s %2$s.  Please wait while the page reloads with your new template.',
1990
+							'event_espresso'
1991
+						),
1992
+						$template_label,
1993
+						$template_pack_labels->template_pack
1994
+					)
1995
+				);
1996
+				// generate the redirect url for js.
1997
+				$url = self::add_query_args_and_nonce(
1998
+					$query_args,
1999
+					$this->_admin_base_url
2000
+				);
2001
+				$this->_template_args['data']['redirect_url'] = $url;
2002
+				$this->_template_args['success'] = true;
2003
+			}
2004
+
2005
+			$this->_return_json();
2006
+		}
2007
+	}
2008
+
2009
+
2010
+	/**
2011
+	 * This handles resetting the template for the given messenger/message_type so that users can start from scratch if
2012
+	 * they want.
2013
+	 *
2014
+	 * @access protected
2015
+	 * @return array|null
2016
+	 * @throws EE_Error
2017
+	 * @throws InvalidArgumentException
2018
+	 * @throws InvalidDataTypeException
2019
+	 * @throws InvalidInterfaceException
2020
+	 */
2021
+	protected function _reset_to_default_template()
2022
+	{
2023
+
2024
+		$templates = array();
2025
+		$GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
2026
+		// we need to make sure we've got the info we need.
2027
+		if (! isset($this->_req_data['msgr'], $this->_req_data['mt'], $this->_req_data['GRP_ID'])) {
2028
+			EE_Error::add_error(
2029
+				esc_html__(
2030
+					'In order to reset the template to its default we require the messenger, message type, and message template GRP_ID to know what is being reset.  At least one of these is missing.',
2031
+					'event_espresso'
2032
+				),
2033
+				__FILE__,
2034
+				__FUNCTION__,
2035
+				__LINE__
2036
+			);
2037
+		}
2038
+
2039
+		// all templates will be reset to whatever the defaults are
2040
+		// for the global template matching the messenger and message type.
2041
+		$success = ! empty($GRP_ID) ? true : false;
2042
+
2043
+		if ($success) {
2044
+			// let's first determine if the incoming template is a global template,
2045
+			// if it isn't then we need to get the global template matching messenger and message type.
2046
+			// $MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID( $GRP_ID );
2047
+
2048
+
2049
+			// note this is ONLY deleting the template fields (Message Template rows) NOT the message template group.
2050
+			$success = $this->_delete_mtp_permanently($GRP_ID, false);
2051
+
2052
+			if ($success) {
2053
+				// if successfully deleted, lets generate the new ones.
2054
+				// Note. We set GLOBAL to true, because resets on ANY template
2055
+				// will use the related global template defaults for regeneration.
2056
+				// This means that if a custom template is reset it resets to whatever the related global template is.
2057
+				// HOWEVER, we DO keep the template pack and template variation set
2058
+				// for the current custom template when resetting.
2059
+				$templates = $this->_generate_new_templates(
2060
+					$this->_req_data['msgr'],
2061
+					$this->_req_data['mt'],
2062
+					$GRP_ID,
2063
+					true
2064
+				);
2065
+			}
2066
+		}
2067
+
2068
+		// any error messages?
2069
+		if (! $success) {
2070
+			EE_Error::add_error(
2071
+				esc_html__(
2072
+					'Something went wrong with deleting existing templates. Unable to reset to default',
2073
+					'event_espresso'
2074
+				),
2075
+				__FILE__,
2076
+				__FUNCTION__,
2077
+				__LINE__
2078
+			);
2079
+		}
2080
+
2081
+		// all good, let's add a success message!
2082
+		if ($success && ! empty($templates)) {
2083
+			// the info for the template we generated is the first element in the returned array
2084
+			// $templates = $templates[0];
2085
+			EE_Error::overwrite_success();
2086
+			EE_Error::add_success(__('Templates have been reset to defaults.', 'event_espresso'));
2087
+		}
2088
+
2089
+
2090
+		$query_args = array(
2091
+			'id'      => isset($templates[0]['GRP_ID']) ? $templates[0]['GRP_ID'] : null,
2092
+			'context' => isset($templates[0]['MTP_context']) ? $templates[0]['MTP_context'] : null,
2093
+			'action'  => isset($templates[0]['GRP_ID']) ? 'edit_message_template' : 'global_mtps',
2094
+		);
2095
+
2096
+		// if called via ajax then we return query args otherwise redirect
2097
+		if (defined('DOING_AJAX') && DOING_AJAX) {
2098
+			return $query_args;
2099
+		} else {
2100
+			$this->_redirect_after_action(false, '', '', $query_args, true);
2101
+
2102
+			return null;
2103
+		}
2104
+	}
2105
+
2106
+
2107
+	/**
2108
+	 * Retrieve and set the message preview for display.
2109
+	 *
2110
+	 * @param bool $send if TRUE then we are doing an actual TEST send with the results of the preview.
2111
+	 * @return string
2112
+	 * @throws ReflectionException
2113
+	 * @throws EE_Error
2114
+	 * @throws InvalidArgumentException
2115
+	 * @throws InvalidDataTypeException
2116
+	 * @throws InvalidInterfaceException
2117
+	 */
2118
+	public function _preview_message($send = false)
2119
+	{
2120
+		// first make sure we've got the necessary parameters
2121
+		if (! isset(
2122
+			$this->_req_data['message_type'],
2123
+			$this->_req_data['messenger'],
2124
+			$this->_req_data['messenger'],
2125
+			$this->_req_data['GRP_ID']
2126
+		)) {
2127
+			EE_Error::add_error(
2128
+				esc_html__('Missing necessary parameters for displaying preview', 'event_espresso'),
2129
+				__FILE__,
2130
+				__FUNCTION__,
2131
+				__LINE__
2132
+			);
2133
+		}
2134
+
2135
+		EE_Registry::instance()->REQ->set('GRP_ID', $this->_req_data['GRP_ID']);
2136
+
2137
+
2138
+		// get the preview!
2139
+		$preview = EED_Messages::preview_message(
2140
+			$this->_req_data['message_type'],
2141
+			$this->_req_data['context'],
2142
+			$this->_req_data['messenger'],
2143
+			$send
2144
+		);
2145
+
2146
+		if ($send) {
2147
+			return $preview;
2148
+		}
2149
+
2150
+		// let's add a button to go back to the edit view
2151
+		$query_args = array(
2152
+			'id'      => $this->_req_data['GRP_ID'],
2153
+			'context' => $this->_req_data['context'],
2154
+			'action'  => 'edit_message_template',
2155
+		);
2156
+		$go_back_url = parent::add_query_args_and_nonce($query_args, $this->_admin_base_url);
2157
+		$preview_button = '<a href="'
2158
+						  . $go_back_url
2159
+						  . '" class="button-secondary messages-preview-go-back-button">'
2160
+						  . esc_html__('Go Back to Edit', 'event_espresso')
2161
+						  . '</a>';
2162
+		$message_types = $this->get_installed_message_types();
2163
+		$active_messenger = $this->_message_resource_manager->get_active_messenger(
2164
+			$this->_req_data['messenger']
2165
+		);
2166
+		$active_messenger_label = $active_messenger instanceof EE_messenger
2167
+			? ucwords($active_messenger->label['singular'])
2168
+			: esc_html__('Unknown Messenger', 'event_espresso');
2169
+		// let's provide a helpful title for context
2170
+		$preview_title = sprintf(
2171
+			esc_html__('Viewing Preview for %s %s Message Template', 'event_espresso'),
2172
+			$active_messenger_label,
2173
+			ucwords($message_types[ $this->_req_data['message_type'] ]->label['singular'])
2174
+		);
2175
+		if ( empty( $preview ) ) {
2176
+			$this->noEventsErrorMessage();
2177
+		}
2178
+		// setup display of preview.
2179
+		$this->_admin_page_title = $preview_title;
2180
+		$this->_template_args['admin_page_title'] = $preview_title;
2181
+		$this->_template_args['admin_page_content'] = $preview_button . '<br />' . $preview;
2182
+		$this->_template_args['data']['force_json'] = true;
2183
+
2184
+		return '';
2185
+	}
2186
+
2187
+
2188
+	/**
2189
+	 * Used to set an error if there are no events available for generating a preview/test send.
2190
+	 *
2191
+	 * @param bool $test_send  Whether the error should be generated for the context of a test send.
2192
+	 */
2193
+	protected function noEventsErrorMessage($test_send = false) {
2194
+		$events_url = parent::add_query_args_and_nonce(
2195
+			array(
2196
+				'action' => 'default',
2197
+				'page' => 'espresso_events',
2198
+			),
2199
+			admin_url( 'admin.php' )
2200
+		);
2201
+		$message = $test_send
2202
+			? __(
2203
+				'A test message could not be sent for this message template because there are no events created yet. The preview system uses actual events for generating the test message. %1$sGo see your events%2$s!',
2204
+				'event_espresso'
2205
+			)
2206
+			: __(
2207
+				'There is no preview for this message template available because there are no events created yet. The preview system uses actual events for generating the preview. %1$sGo see your events%2$s!',
2208
+				'event_espresso'
2209
+			);
2210
+
2211
+		EE_Error::add_attention(
2212
+			sprintf(
2213
+				$message,
2214
+				"<a href='{$events_url}'>",
2215
+				'</a>'
2216
+			)
2217
+		);
2218
+	}
2219
+
2220
+
2221
+	/**
2222
+	 * The initial _preview_message is on a no headers route.  It will optionally call this if necessary otherwise it
2223
+	 * gets called automatically.
2224
+	 *
2225
+	 * @since 4.5.0
2226
+	 *
2227
+	 * @return string
2228
+	 */
2229
+	protected function _display_preview_message()
2230
+	{
2231
+		$this->display_admin_page_with_no_sidebar();
2232
+	}
2233
+
2234
+
2235
+	/**
2236
+	 * registers metaboxes that should show up on the "edit_message_template" page
2237
+	 *
2238
+	 * @access protected
2239
+	 * @return void
2240
+	 */
2241
+	protected function _register_edit_meta_boxes()
2242
+	{
2243
+		add_meta_box(
2244
+			'mtp_valid_shortcodes',
2245
+			esc_html__('Valid Shortcodes', 'event_espresso'),
2246
+			array($this, 'shortcode_meta_box'),
2247
+			$this->_current_screen->id,
2248
+			'side',
2249
+			'default'
2250
+		);
2251
+		add_meta_box(
2252
+			'mtp_extra_actions',
2253
+			esc_html__('Extra Actions', 'event_espresso'),
2254
+			array($this, 'extra_actions_meta_box'),
2255
+			$this->_current_screen->id,
2256
+			'side',
2257
+			'high'
2258
+		);
2259
+		add_meta_box(
2260
+			'mtp_templates',
2261
+			esc_html__('Template Styles', 'event_espresso'),
2262
+			array($this, 'template_pack_meta_box'),
2263
+			$this->_current_screen->id,
2264
+			'side',
2265
+			'high'
2266
+		);
2267
+	}
2268
+
2269
+
2270
+	/**
2271
+	 * metabox content for all template pack and variation selection.
2272
+	 *
2273
+	 * @since 4.5.0
2274
+	 * @return string
2275
+	 * @throws DomainException
2276
+	 * @throws EE_Error
2277
+	 * @throws InvalidArgumentException
2278
+	 * @throws ReflectionException
2279
+	 * @throws InvalidDataTypeException
2280
+	 * @throws InvalidInterfaceException
2281
+	 */
2282
+	public function template_pack_meta_box()
2283
+	{
2284
+		$this->_set_message_template_group();
2285
+
2286
+		$tp_collection = EEH_MSG_Template::get_template_pack_collection();
2287
+
2288
+		$tp_select_values = array();
2289
+
2290
+		foreach ($tp_collection as $tp) {
2291
+			// only include template packs that support this messenger and message type!
2292
+			$supports = $tp->get_supports();
2293
+			if (! isset($supports[ $this->_message_template_group->messenger() ])
2294
+				|| ! in_array(
2295
+					$this->_message_template_group->message_type(),
2296
+					$supports[ $this->_message_template_group->messenger() ],
2297
+					true
2298
+				)
2299
+			) {
2300
+				// not supported
2301
+				continue;
2302
+			}
2303
+
2304
+			$tp_select_values[] = array(
2305
+				'text' => $tp->label,
2306
+				'id'   => $tp->dbref,
2307
+			);
2308
+		}
2309
+
2310
+		// if empty $tp_select_values then we make sure default is set because EVERY message type should be supported by
2311
+		// the default template pack.  This still allows for the odd template pack to override.
2312
+		if (empty($tp_select_values)) {
2313
+			$tp_select_values[] = array(
2314
+				'text' => esc_html__('Default', 'event_espresso'),
2315
+				'id'   => 'default',
2316
+			);
2317
+		}
2318
+
2319
+		// setup variation select values for the currently selected template.
2320
+		$variations = $this->_message_template_group->get_template_pack()->get_variations(
2321
+			$this->_message_template_group->messenger(),
2322
+			$this->_message_template_group->message_type()
2323
+		);
2324
+		$variations_select_values = array();
2325
+		foreach ($variations as $variation => $label) {
2326
+			$variations_select_values[] = array(
2327
+				'text' => $label,
2328
+				'id'   => $variation,
2329
+			);
2330
+		}
2331
+
2332
+		$template_pack_labels = $this->_message_template_group->messenger_obj()->get_supports_labels();
2333
+
2334
+		$template_args['template_packs_selector'] = EEH_Form_Fields::select_input(
2335
+			'MTP_template_pack',
2336
+			$tp_select_values,
2337
+			$this->_message_template_group->get_template_pack_name()
2338
+		);
2339
+		$template_args['variations_selector'] = EEH_Form_Fields::select_input(
2340
+			'MTP_template_variation',
2341
+			$variations_select_values,
2342
+			$this->_message_template_group->get_template_pack_variation()
2343
+		);
2344
+		$template_args['template_pack_label'] = $template_pack_labels->template_pack;
2345
+		$template_args['template_variation_label'] = $template_pack_labels->template_variation;
2346
+		$template_args['template_pack_description'] = $template_pack_labels->template_pack_description;
2347
+		$template_args['template_variation_description'] = $template_pack_labels->template_variation_description;
2348
+
2349
+		$template = EE_MSG_TEMPLATE_PATH . 'template_pack_and_variations_metabox.template.php';
2350
+
2351
+		EEH_Template::display_template($template, $template_args);
2352
+	}
2353
+
2354
+
2355
+	/**
2356
+	 * This meta box holds any extra actions related to Message Templates
2357
+	 * For now, this includes Resetting templates to defaults and sending a test email.
2358
+	 *
2359
+	 * @access  public
2360
+	 * @return void
2361
+	 * @throws EE_Error
2362
+	 */
2363
+	public function extra_actions_meta_box()
2364
+	{
2365
+		$template_form_fields = array();
2366
+
2367
+		$extra_args = array(
2368
+			'msgr'   => $this->_message_template_group->messenger(),
2369
+			'mt'     => $this->_message_template_group->message_type(),
2370
+			'GRP_ID' => $this->_message_template_group->GRP_ID(),
2371
+		);
2372
+		// first we need to see if there are any fields
2373
+		$fields = $this->_message_template_group->messenger_obj()->get_test_settings_fields();
2374
+
2375
+		if (! empty($fields)) {
2376
+			// yup there be fields
2377
+			foreach ($fields as $field => $config) {
2378
+				$field_id = $this->_message_template_group->messenger() . '_' . $field;
2379
+				$existing = $this->_message_template_group->messenger_obj()->get_existing_test_settings();
2380
+				$default = isset($config['default']) ? $config['default'] : '';
2381
+				$default = isset($config['value']) ? $config['value'] : $default;
2382
+
2383
+				// if type is hidden and the value is empty
2384
+				// something may have gone wrong so let's correct with the defaults
2385
+				$fix = $config['input'] === 'hidden'
2386
+					   && isset($existing[ $field ])
2387
+					   && empty($existing[ $field ])
2388
+					? $default
2389
+					: '';
2390
+				$existing[ $field ] = isset($existing[ $field ]) && empty($fix)
2391
+					? $existing[ $field ]
2392
+					: $fix;
2393
+
2394
+				$template_form_fields[ $field_id ] = array(
2395
+					'name'       => 'test_settings_fld[' . $field . ']',
2396
+					'label'      => $config['label'],
2397
+					'input'      => $config['input'],
2398
+					'type'       => $config['type'],
2399
+					'required'   => $config['required'],
2400
+					'validation' => $config['validation'],
2401
+					'value'      => isset($existing[ $field ]) ? $existing[ $field ] : $default,
2402
+					'css_class'  => $config['css_class'],
2403
+					'options'    => isset($config['options']) ? $config['options'] : array(),
2404
+					'default'    => $default,
2405
+					'format'     => $config['format'],
2406
+				);
2407
+			}
2408
+		}
2409
+
2410
+		$test_settings_fields = ! empty($template_form_fields)
2411
+			? $this->_generate_admin_form_fields($template_form_fields, 'string', 'ee_tst_settings_flds')
2412
+			: '';
2413
+
2414
+		$test_settings_html = '';
2415
+		// print out $test_settings_fields
2416
+		if (! empty($test_settings_fields)) {
2417
+			echo $test_settings_fields;
2418
+			$test_settings_html = '<input type="submit" class="button-primary mtp-test-button alignright" ';
2419
+			$test_settings_html .= 'name="test_button" value="';
2420
+			$test_settings_html .= esc_html__('Test Send', 'event_espresso');
2421
+			$test_settings_html .= '" /><div style="clear:both"></div>';
2422
+		}
2423
+
2424
+		// and button
2425
+		$test_settings_html .= '<p>'
2426
+							   . esc_html__('Need to reset this message type and start over?', 'event_espresso')
2427
+							   . '</p>';
2428
+		$test_settings_html .= '<div class="publishing-action alignright resetbutton">';
2429
+		$test_settings_html .= $this->get_action_link_or_button(
2430
+			'reset_to_default',
2431
+			'reset',
2432
+			$extra_args,
2433
+			'button-primary reset-default-button'
2434
+		);
2435
+		$test_settings_html .= '</div><div style="clear:both"></div>';
2436
+		echo $test_settings_html;
2437
+	}
2438
+
2439
+
2440
+	/**
2441
+	 * This returns the shortcode selector skeleton for a given context and field.
2442
+	 *
2443
+	 * @since 4.9.rc.000
2444
+	 * @param string $field           The name of the field retrieving shortcodes for.
2445
+	 * @param string $linked_input_id The css id of the input that the shortcodes get added to.
2446
+	 * @return string
2447
+	 * @throws DomainException
2448
+	 * @throws EE_Error
2449
+	 * @throws InvalidArgumentException
2450
+	 * @throws ReflectionException
2451
+	 * @throws InvalidDataTypeException
2452
+	 * @throws InvalidInterfaceException
2453
+	 */
2454
+	protected function _get_shortcode_selector($field, $linked_input_id)
2455
+	{
2456
+		$template_args = array(
2457
+			'shortcodes'      => $this->_get_shortcodes(array($field), true),
2458
+			'fieldname'       => $field,
2459
+			'linked_input_id' => $linked_input_id,
2460
+		);
2461
+
2462
+		return EEH_Template::display_template(
2463
+			EE_MSG_TEMPLATE_PATH . 'shortcode_selector_skeleton.template.php',
2464
+			$template_args,
2465
+			true
2466
+		);
2467
+	}
2468
+
2469
+
2470
+	/**
2471
+	 * This just takes care of returning the meta box content for shortcodes (only used on the edit message template
2472
+	 * page)
2473
+	 *
2474
+	 * @access public
2475
+	 * @return void
2476
+	 * @throws EE_Error
2477
+	 * @throws InvalidArgumentException
2478
+	 * @throws ReflectionException
2479
+	 * @throws InvalidDataTypeException
2480
+	 * @throws InvalidInterfaceException
2481
+	 */
2482
+	public function shortcode_meta_box()
2483
+	{
2484
+		$shortcodes = $this->_get_shortcodes(array(), false); // just make sure shortcodes property is set
2485
+		// $messenger = $this->_message_template_group->messenger_obj();
2486
+		// now let's set the content depending on the status of the shortcodes array
2487
+		if (empty($shortcodes)) {
2488
+			$content = '<p>' . esc_html__('There are no valid shortcodes available', 'event_espresso') . '</p>';
2489
+			echo $content;
2490
+		} else {
2491
+			// $alt = 0;
2492
+			?>
2493 2493
             <div style="float:right; margin-top:10px"><?php
2494
-                            echo $this->_get_help_tab_link('message_template_shortcodes');
2495
-                            ?></div>
2494
+							echo $this->_get_help_tab_link('message_template_shortcodes');
2495
+							?></div>
2496 2496
             <p class="small-text"><?php
2497
-                                  printf(
2498
-                                      esc_html__(
2499
-                                          'You can view the shortcodes usable in your template by clicking the %s icon next to each field.',
2500
-                                          'event_espresso'
2501
-                                      ),
2502
-                                      '<span class="dashicons dashicons-menu"></span>'
2503
-                                  );
2504
-                                ?>
2497
+								  printf(
2498
+									  esc_html__(
2499
+										  'You can view the shortcodes usable in your template by clicking the %s icon next to each field.',
2500
+										  'event_espresso'
2501
+									  ),
2502
+									  '<span class="dashicons dashicons-menu"></span>'
2503
+								  );
2504
+								?>
2505 2505
             </p>
2506 2506
             <?php
2507
-        }
2508
-    }
2509
-
2510
-
2511
-    /**
2512
-     * used to set the $_shortcodes property for when its needed elsewhere.
2513
-     *
2514
-     * @access protected
2515
-     * @return void
2516
-     * @throws EE_Error
2517
-     * @throws InvalidArgumentException
2518
-     * @throws ReflectionException
2519
-     * @throws InvalidDataTypeException
2520
-     * @throws InvalidInterfaceException
2521
-     */
2522
-    protected function _set_shortcodes()
2523
-    {
2524
-
2525
-        // no need to run this if the property is already set
2526
-        if (! empty($this->_shortcodes)) {
2527
-            return;
2528
-        }
2529
-
2530
-        $this->_shortcodes = $this->_get_shortcodes();
2531
-    }
2532
-
2533
-
2534
-    /**
2535
-     * get's all shortcodes for a given template group. (typically used by _set_shortcodes to set the $_shortcodes
2536
-     * property)
2537
-     *
2538
-     * @access  protected
2539
-     * @param  array   $fields include an array of specific field names that you want to be used to get the shortcodes
2540
-     *                         for. Defaults to all (for the given context)
2541
-     * @param  boolean $merged Whether to merge all the shortcodes into one list of unique shortcodes
2542
-     * @return array Shortcodes indexed by fieldname and the an array of shortcode/label pairs OR if merged is
2543
-     *                         true just an array of shortcode/label pairs.
2544
-     * @throws EE_Error
2545
-     * @throws InvalidArgumentException
2546
-     * @throws ReflectionException
2547
-     * @throws InvalidDataTypeException
2548
-     * @throws InvalidInterfaceException
2549
-     */
2550
-    protected function _get_shortcodes($fields = array(), $merged = true)
2551
-    {
2552
-        $this->_set_message_template_group();
2553
-
2554
-        // we need the messenger and message template to retrieve the valid shortcodes array.
2555
-        $GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id'])
2556
-            ? absint($this->_req_data['id'])
2557
-            : false;
2558
-        $context = isset($this->_req_data['context'])
2559
-            ? $this->_req_data['context']
2560
-            : key($this->_message_template_group->contexts_config());
2561
-
2562
-        return ! empty($GRP_ID) ? $this->_message_template_group->get_shortcodes($context, $fields, $merged) : array();
2563
-    }
2564
-
2565
-
2566
-    /**
2567
-     * This sets the _message_template property (containing the called message_template object)
2568
-     *
2569
-     * @access protected
2570
-     * @return void
2571
-     * @throws EE_Error
2572
-     * @throws InvalidArgumentException
2573
-     * @throws ReflectionException
2574
-     * @throws InvalidDataTypeException
2575
-     * @throws InvalidInterfaceException
2576
-     */
2577
-    protected function _set_message_template_group()
2578
-    {
2579
-
2580
-        if (! empty($this->_message_template_group)) {
2581
-            return;
2582
-        } //get out if this is already set.
2583
-
2584
-        $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? absint($this->_req_data['GRP_ID']) : false;
2585
-        $GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['id']) ? $this->_req_data['id'] : $GRP_ID;
2586
-
2587
-        // let's get the message templates
2588
-        $MTP = EEM_Message_Template_Group::instance();
2589
-
2590
-        if (empty($GRP_ID)) {
2591
-            $this->_message_template_group = $MTP->create_default_object();
2592
-        } else {
2593
-            $this->_message_template_group = $MTP->get_one_by_ID($GRP_ID);
2594
-        }
2595
-
2596
-        $this->_template_pack = $this->_message_template_group->get_template_pack();
2597
-        $this->_variation = $this->_message_template_group->get_template_pack_variation();
2598
-    }
2599
-
2600
-
2601
-    /**
2602
-     * sets up a context switcher for edit forms
2603
-     *
2604
-     * @access  protected
2605
-     * @param  EE_Message_Template_Group $template_group_object the template group object being displayed on the form
2606
-     * @param array                      $args                  various things the context switcher needs.
2607
-     * @throws EE_Error
2608
-     */
2609
-    protected function _set_context_switcher(EE_Message_Template_Group $template_group_object, $args)
2610
-    {
2611
-        $context_details = $template_group_object->contexts_config();
2612
-        $context_label = $template_group_object->context_label();
2613
-        ob_start();
2614
-        ?>
2507
+		}
2508
+	}
2509
+
2510
+
2511
+	/**
2512
+	 * used to set the $_shortcodes property for when its needed elsewhere.
2513
+	 *
2514
+	 * @access protected
2515
+	 * @return void
2516
+	 * @throws EE_Error
2517
+	 * @throws InvalidArgumentException
2518
+	 * @throws ReflectionException
2519
+	 * @throws InvalidDataTypeException
2520
+	 * @throws InvalidInterfaceException
2521
+	 */
2522
+	protected function _set_shortcodes()
2523
+	{
2524
+
2525
+		// no need to run this if the property is already set
2526
+		if (! empty($this->_shortcodes)) {
2527
+			return;
2528
+		}
2529
+
2530
+		$this->_shortcodes = $this->_get_shortcodes();
2531
+	}
2532
+
2533
+
2534
+	/**
2535
+	 * get's all shortcodes for a given template group. (typically used by _set_shortcodes to set the $_shortcodes
2536
+	 * property)
2537
+	 *
2538
+	 * @access  protected
2539
+	 * @param  array   $fields include an array of specific field names that you want to be used to get the shortcodes
2540
+	 *                         for. Defaults to all (for the given context)
2541
+	 * @param  boolean $merged Whether to merge all the shortcodes into one list of unique shortcodes
2542
+	 * @return array Shortcodes indexed by fieldname and the an array of shortcode/label pairs OR if merged is
2543
+	 *                         true just an array of shortcode/label pairs.
2544
+	 * @throws EE_Error
2545
+	 * @throws InvalidArgumentException
2546
+	 * @throws ReflectionException
2547
+	 * @throws InvalidDataTypeException
2548
+	 * @throws InvalidInterfaceException
2549
+	 */
2550
+	protected function _get_shortcodes($fields = array(), $merged = true)
2551
+	{
2552
+		$this->_set_message_template_group();
2553
+
2554
+		// we need the messenger and message template to retrieve the valid shortcodes array.
2555
+		$GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id'])
2556
+			? absint($this->_req_data['id'])
2557
+			: false;
2558
+		$context = isset($this->_req_data['context'])
2559
+			? $this->_req_data['context']
2560
+			: key($this->_message_template_group->contexts_config());
2561
+
2562
+		return ! empty($GRP_ID) ? $this->_message_template_group->get_shortcodes($context, $fields, $merged) : array();
2563
+	}
2564
+
2565
+
2566
+	/**
2567
+	 * This sets the _message_template property (containing the called message_template object)
2568
+	 *
2569
+	 * @access protected
2570
+	 * @return void
2571
+	 * @throws EE_Error
2572
+	 * @throws InvalidArgumentException
2573
+	 * @throws ReflectionException
2574
+	 * @throws InvalidDataTypeException
2575
+	 * @throws InvalidInterfaceException
2576
+	 */
2577
+	protected function _set_message_template_group()
2578
+	{
2579
+
2580
+		if (! empty($this->_message_template_group)) {
2581
+			return;
2582
+		} //get out if this is already set.
2583
+
2584
+		$GRP_ID = ! empty($this->_req_data['GRP_ID']) ? absint($this->_req_data['GRP_ID']) : false;
2585
+		$GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['id']) ? $this->_req_data['id'] : $GRP_ID;
2586
+
2587
+		// let's get the message templates
2588
+		$MTP = EEM_Message_Template_Group::instance();
2589
+
2590
+		if (empty($GRP_ID)) {
2591
+			$this->_message_template_group = $MTP->create_default_object();
2592
+		} else {
2593
+			$this->_message_template_group = $MTP->get_one_by_ID($GRP_ID);
2594
+		}
2595
+
2596
+		$this->_template_pack = $this->_message_template_group->get_template_pack();
2597
+		$this->_variation = $this->_message_template_group->get_template_pack_variation();
2598
+	}
2599
+
2600
+
2601
+	/**
2602
+	 * sets up a context switcher for edit forms
2603
+	 *
2604
+	 * @access  protected
2605
+	 * @param  EE_Message_Template_Group $template_group_object the template group object being displayed on the form
2606
+	 * @param array                      $args                  various things the context switcher needs.
2607
+	 * @throws EE_Error
2608
+	 */
2609
+	protected function _set_context_switcher(EE_Message_Template_Group $template_group_object, $args)
2610
+	{
2611
+		$context_details = $template_group_object->contexts_config();
2612
+		$context_label = $template_group_object->context_label();
2613
+		ob_start();
2614
+		?>
2615 2615
         <div class="ee-msg-switcher-container">
2616 2616
             <form method="get" action="<?php echo EE_MSG_ADMIN_URL; ?>" id="ee-msg-context-switcher-frm">
2617 2617
                 <?php
2618
-                foreach ($args as $name => $value) {
2619
-                    if ($name === 'context' || empty($value) || $name === 'extra') {
2620
-                        continue;
2621
-                    }
2622
-                    ?>
2618
+				foreach ($args as $name => $value) {
2619
+					if ($name === 'context' || empty($value) || $name === 'extra') {
2620
+						continue;
2621
+					}
2622
+					?>
2623 2623
                     <input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/>
2624 2624
                     <?php
2625
-                }
2626
-                // setup nonce_url
2627
-                wp_nonce_field($args['action'] . '_nonce', $args['action'] . '_nonce', false);
2628
-                ?>
2625
+				}
2626
+				// setup nonce_url
2627
+				wp_nonce_field($args['action'] . '_nonce', $args['action'] . '_nonce', false);
2628
+				?>
2629 2629
                 <select name="context">
2630 2630
                     <?php
2631
-                    $context_templates = $template_group_object->context_templates();
2632
-                    if (is_array($context_templates)) :
2633
-                        foreach ($context_templates as $context => $template_fields) :
2634
-                            $checked = ($context === $args['context']) ? 'selected="selected"' : '';
2635
-                            ?>
2631
+					$context_templates = $template_group_object->context_templates();
2632
+					if (is_array($context_templates)) :
2633
+						foreach ($context_templates as $context => $template_fields) :
2634
+							$checked = ($context === $args['context']) ? 'selected="selected"' : '';
2635
+							?>
2636 2636
                             <option value="<?php echo $context; ?>" <?php echo $checked; ?>>
2637 2637
                                 <?php echo $context_details[ $context ]['label']; ?>
2638 2638
                             </option>
2639 2639
                         <?php endforeach;
2640
-                    endif; ?>
2640
+					endif; ?>
2641 2641
                 </select>
2642 2642
                 <?php $button_text = sprintf(__('Switch %s', 'event_espresso'), ucwords($context_label['label'])); ?>
2643 2643
                 <input id="submit-msg-context-switcher-sbmt" class="button-secondary" type="submit"
@@ -2646,1925 +2646,1925 @@  discard block
 block discarded – undo
2646 2646
             <?php echo $args['extra']; ?>
2647 2647
         </div> <!-- end .ee-msg-switcher-container -->
2648 2648
         <?php
2649
-        $output = ob_get_contents();
2650
-        ob_clean();
2651
-        $this->_context_switcher = $output;
2652
-    }
2653
-
2654
-
2655
-    /**
2656
-     * utility for sanitizing new values coming in.
2657
-     * Note: this is only used when updating a context.
2658
-     *
2659
-     * @access protected
2660
-     *
2661
-     * @param int $index This helps us know which template field to select from the request array.
2662
-     *
2663
-     * @return array
2664
-     */
2665
-    protected function _set_message_template_column_values($index)
2666
-    {
2667
-        if (is_array($this->_req_data['MTP_template_fields'][ $index ]['content'])) {
2668
-            foreach ($this->_req_data['MTP_template_fields'][ $index ]['content'] as $field => $value) {
2669
-                $this->_req_data['MTP_template_fields'][ $index ]['content'][ $field ] = $value;
2670
-            }
2671
-        }
2672
-
2673
-
2674
-        $set_column_values = array(
2675
-            'MTP_ID'             => absint($this->_req_data['MTP_template_fields'][ $index ]['MTP_ID']),
2676
-            'GRP_ID'             => absint($this->_req_data['GRP_ID']),
2677
-            'MTP_user_id'        => absint($this->_req_data['MTP_user_id']),
2678
-            'MTP_messenger'      => strtolower($this->_req_data['MTP_messenger']),
2679
-            'MTP_message_type'   => strtolower($this->_req_data['MTP_message_type']),
2680
-            'MTP_template_field' => strtolower($this->_req_data['MTP_template_fields'][ $index ]['name']),
2681
-            'MTP_context'        => strtolower($this->_req_data['MTP_context']),
2682
-            'MTP_content'        => $this->_req_data['MTP_template_fields'][ $index ]['content'],
2683
-            'MTP_is_global'      => isset($this->_req_data['MTP_is_global'])
2684
-                ? absint($this->_req_data['MTP_is_global'])
2685
-                : 0,
2686
-            'MTP_is_override'    => isset($this->_req_data['MTP_is_override'])
2687
-                ? absint($this->_req_data['MTP_is_override'])
2688
-                : 0,
2689
-            'MTP_deleted'        => absint($this->_req_data['MTP_deleted']),
2690
-            'MTP_is_active'      => absint($this->_req_data['MTP_is_active']),
2691
-        );
2692
-
2693
-
2694
-        return $set_column_values;
2695
-    }
2696
-
2697
-
2698
-    protected function _insert_or_update_message_template($new = false)
2699
-    {
2700
-
2701
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
2702
-        $success = 0;
2703
-        $override = false;
2704
-
2705
-        // setup notices description
2706
-        $messenger_slug = ! empty($this->_req_data['MTP_messenger']) ? $this->_req_data['MTP_messenger'] : '';
2707
-
2708
-        // need the message type and messenger objects to be able to use the labels for the notices
2709
-        $messenger_object = $this->_message_resource_manager->get_messenger($messenger_slug);
2710
-        $messenger_label = $messenger_object instanceof EE_messenger
2711
-            ? ucwords($messenger_object->label['singular'])
2712
-            : '';
2713
-
2714
-        $message_type_slug = ! empty($this->_req_data['MTP_message_type'])
2715
-            ? $this->_req_data['MTP_message_type']
2716
-            : '';
2717
-        $message_type_object = $this->_message_resource_manager->get_message_type($message_type_slug);
2718
-
2719
-        $message_type_label = $message_type_object instanceof EE_message_type
2720
-            ? ucwords($message_type_object->label['singular'])
2721
-            : '';
2722
-
2723
-        $context_slug = ! empty($this->_req_data['MTP_context'])
2724
-            ? $this->_req_data['MTP_context']
2725
-            : '';
2726
-        $context = ucwords(str_replace('_', ' ', $context_slug));
2727
-
2728
-        $item_desc = $messenger_label && $message_type_label
2729
-            ? $messenger_label . ' ' . $message_type_label . ' ' . $context . ' '
2730
-            : '';
2731
-        $item_desc .= 'Message Template';
2732
-        $query_args = array();
2733
-        $edit_array = array();
2734
-        $action_desc = '';
2735
-
2736
-        // if this is "new" then we need to generate the default contexts for the selected messenger/message_type for
2737
-        // user to edit.
2738
-        if ($new) {
2739
-            $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
2740
-            if ($edit_array = $this->_generate_new_templates($messenger_slug, $message_type_slug, $GRP_ID)) {
2741
-                if (empty($edit_array)) {
2742
-                    $success = 0;
2743
-                } else {
2744
-                    $success = 1;
2745
-                    $edit_array = $edit_array[0];
2746
-                    $query_args = array(
2747
-                        'id'      => $edit_array['GRP_ID'],
2748
-                        'context' => $edit_array['MTP_context'],
2749
-                        'action'  => 'edit_message_template',
2750
-                    );
2751
-                }
2752
-            }
2753
-            $action_desc = 'created';
2754
-        } else {
2755
-            $MTPG = EEM_Message_Template_Group::instance();
2756
-            $MTP = EEM_Message_Template::instance();
2757
-
2758
-
2759
-            // run update for each template field in displayed context
2760
-            if (! isset($this->_req_data['MTP_template_fields']) && empty($this->_req_data['MTP_template_fields'])) {
2761
-                EE_Error::add_error(
2762
-                    esc_html__(
2763
-                        'There was a problem saving the template fields from the form because I didn\'t receive any actual template field data.',
2764
-                        'event_espresso'
2765
-                    ),
2766
-                    __FILE__,
2767
-                    __FUNCTION__,
2768
-                    __LINE__
2769
-                );
2770
-                $success = 0;
2771
-            } else {
2772
-                // first validate all fields!
2773
-                // this filter allows client code to add its own validation to the template fields as well.
2774
-                // returning an empty array means everything passed validation.
2775
-                // errors in validation should be represented in an array with the following shape:
2776
-                // array(
2777
-                //   'fieldname' => array(
2778
-                //          'msg' => 'error message'
2779
-                //          'value' => 'value for field producing error'
2780
-                // )
2781
-                $custom_validation = (array) apply_filters(
2782
-                    'FHEE__Messages_Admin_Page___insert_or_update_message_template__validates',
2783
-                    array(),
2784
-                    $this->_req_data['MTP_template_fields'],
2785
-                    $context_slug,
2786
-                    $messenger_slug,
2787
-                    $message_type_slug
2788
-                );
2789
-
2790
-                $system_validation = $MTPG->validate(
2791
-                    $this->_req_data['MTP_template_fields'],
2792
-                    $context_slug,
2793
-                    $messenger_slug,
2794
-                    $message_type_slug
2795
-                );
2796
-
2797
-                $system_validation = ! is_array($system_validation) && $system_validation ? array()
2798
-                    : $system_validation;
2799
-                $validates = array_merge($custom_validation, $system_validation);
2800
-
2801
-                // if $validate returned error messages (i.e. is_array()) then we need to process them and setup an
2802
-                // appropriate response. HMM, dang this isn't correct, $validates will ALWAYS be an array.
2803
-                //  WE need to make sure there is no actual error messages in validates.
2804
-                if (is_array($validates) && ! empty($validates)) {
2805
-                    // add the transient so when the form loads we know which fields to highlight
2806
-                    $this->_add_transient('edit_message_template', $validates);
2807
-
2808
-                    $success = 0;
2809
-
2810
-                    // setup notices
2811
-                    foreach ($validates as $field => $error) {
2812
-                        if (isset($error['msg'])) {
2813
-                            EE_Error::add_error($error['msg'], __FILE__, __FUNCTION__, __LINE__);
2814
-                        }
2815
-                    }
2816
-                } else {
2817
-                    $set_column_values = array();
2818
-                    foreach ($this->_req_data['MTP_template_fields'] as $template_field => $content) {
2819
-                        $set_column_values = $this->_set_message_template_column_values($template_field);
2820
-
2821
-                        $where_cols_n_values = array(
2822
-                            'MTP_ID' => $this->_req_data['MTP_template_fields'][ $template_field ]['MTP_ID'],
2823
-                        );
2824
-                        // if they aren't allowed to use all JS, restrict them to just posty-y tags
2825
-                        if (! current_user_can('unfiltered_html')) {
2826
-                            if (is_array($set_column_values['MTP_content'])) {
2827
-                                foreach ($set_column_values['MTP_content'] as $key => $value) {
2828
-                                    // remove slashes so wp_kses works properly (its wp_kses_stripslashes() function
2829
-                                    // only removes slashes from double-quotes, so attributes using single quotes always
2830
-                                    // appear invalid.) But currently the models expect slashed data, so after wp_kses
2831
-                                    // runs we need to re-slash the data. Sheesh. See
2832
-                                    // https://events.codebasehq.com/projects/event-espresso/tickets/11211#update-47321587
2833
-                                    $set_column_values['MTP_content'][ $key ] = addslashes(
2834
-                                        wp_kses(
2835
-                                            stripslashes($value),
2836
-                                            wp_kses_allowed_html('post')
2837
-                                        )
2838
-                                    );
2839
-                                }
2840
-                            } else {
2841
-                                $set_column_values['MTP_content'] = wp_kses(
2842
-                                    $set_column_values['MTP_content'],
2843
-                                    wp_kses_allowed_html('post')
2844
-                                );
2845
-                            }
2846
-                        }
2847
-                        $message_template_fields = array(
2848
-                            'GRP_ID'             => $set_column_values['GRP_ID'],
2849
-                            'MTP_template_field' => $set_column_values['MTP_template_field'],
2850
-                            'MTP_context'        => $set_column_values['MTP_context'],
2851
-                            'MTP_content'        => $set_column_values['MTP_content'],
2852
-                        );
2853
-                        if ($updated = $MTP->update($message_template_fields, array($where_cols_n_values))) {
2854
-                            if ($updated === false) {
2855
-                                EE_Error::add_error(
2856
-                                    sprintf(
2857
-                                        esc_html__('%s field was NOT updated for some reason', 'event_espresso'),
2858
-                                        $template_field
2859
-                                    ),
2860
-                                    __FILE__,
2861
-                                    __FUNCTION__,
2862
-                                    __LINE__
2863
-                                );
2864
-                            } else {
2865
-                                $success = 1;
2866
-                            }
2867
-                        } else {
2868
-                            // only do this logic if we don't have a MTP_ID for this field
2869
-                            if (empty($this->_req_data['MTP_template_fields'][ $template_field ]['MTP_ID'])) {
2870
-                                // this has already been through the template field validator and sanitized, so it will be
2871
-                                // safe to insert this field.  Why insert?  This typically happens when we introduce a new
2872
-                                // message template field in a messenger/message type and existing users don't have the
2873
-                                // default setup for it.
2874
-                                // @link https://events.codebasehq.com/projects/event-espresso/tickets/9465
2875
-                                $updated = $MTP->insert($message_template_fields);
2876
-                                if (! $updated || is_wp_error($updated)) {
2877
-                                    EE_Error::add_error(
2878
-                                        sprintf(
2879
-                                            esc_html__('%s field could not be updated.', 'event_espresso'),
2880
-                                            $template_field
2881
-                                        ),
2882
-                                        __FILE__,
2883
-                                        __FUNCTION__,
2884
-                                        __LINE__
2885
-                                    );
2886
-                                    $success = 0;
2887
-                                } else {
2888
-                                    $success = 1;
2889
-                                }
2890
-                            }
2891
-                        }
2892
-                        $action_desc = 'updated';
2893
-                    }
2894
-
2895
-                    // we can use the last set_column_values for the MTPG update (because its the same for all of these specific MTPs)
2896
-                    $mtpg_fields = array(
2897
-                        'MTP_user_id'      => $set_column_values['MTP_user_id'],
2898
-                        'MTP_messenger'    => $set_column_values['MTP_messenger'],
2899
-                        'MTP_message_type' => $set_column_values['MTP_message_type'],
2900
-                        'MTP_is_global'    => $set_column_values['MTP_is_global'],
2901
-                        'MTP_is_override'  => $set_column_values['MTP_is_override'],
2902
-                        'MTP_deleted'      => $set_column_values['MTP_deleted'],
2903
-                        'MTP_is_active'    => $set_column_values['MTP_is_active'],
2904
-                        'MTP_name'         => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_name'])
2905
-                            ? $this->_req_data['ee_msg_non_global_fields']['MTP_name']
2906
-                            : '',
2907
-                        'MTP_description'  => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_description'])
2908
-                            ? $this->_req_data['ee_msg_non_global_fields']['MTP_description']
2909
-                            : '',
2910
-                    );
2911
-
2912
-                    $mtpg_where = array('GRP_ID' => $set_column_values['GRP_ID']);
2913
-                    $updated = $MTPG->update($mtpg_fields, array($mtpg_where));
2914
-
2915
-                    if ($updated === false) {
2916
-                        EE_Error::add_error(
2917
-                            sprintf(
2918
-                                esc_html__(
2919
-                                    'The Message Template Group (%d) was NOT updated for some reason',
2920
-                                    'event_espresso'
2921
-                                ),
2922
-                                $set_column_values['GRP_ID']
2923
-                            ),
2924
-                            __FILE__,
2925
-                            __FUNCTION__,
2926
-                            __LINE__
2927
-                        );
2928
-                    } else {
2929
-                        // k now we need to ensure the template_pack and template_variation fields are set.
2930
-                        $template_pack = ! empty($this->_req_data['MTP_template_pack'])
2931
-                            ? $this->_req_data['MTP_template_pack']
2932
-                            : 'default';
2933
-
2934
-                        $template_variation = ! empty($this->_req_data['MTP_template_variation'])
2935
-                            ? $this->_req_data['MTP_template_variation']
2936
-                            : 'default';
2937
-
2938
-                        $mtpg_obj = $MTPG->get_one_by_ID($set_column_values['GRP_ID']);
2939
-                        if ($mtpg_obj instanceof EE_Message_Template_Group) {
2940
-                            $mtpg_obj->set_template_pack_name($template_pack);
2941
-                            $mtpg_obj->set_template_pack_variation($template_variation);
2942
-                        }
2943
-                        $success = 1;
2944
-                    }
2945
-                }
2946
-            }
2947
-        }
2948
-
2949
-        // we return things differently if doing ajax
2950
-        if (defined('DOING_AJAX') && DOING_AJAX) {
2951
-            $this->_template_args['success'] = $success;
2952
-            $this->_template_args['error'] = ! $success ? true : false;
2953
-            $this->_template_args['content'] = '';
2954
-            $this->_template_args['data'] = array(
2955
-                'grpID'        => $edit_array['GRP_ID'],
2956
-                'templateName' => $edit_array['template_name'],
2957
-            );
2958
-            if ($success) {
2959
-                EE_Error::overwrite_success();
2960
-                EE_Error::add_success(
2961
-                    esc_html__(
2962
-                        'The new template has been created and automatically selected for this event.  You can edit the new template by clicking the edit button.  Note before this template is assigned to this event, the event must be saved.',
2963
-                        'event_espresso'
2964
-                    )
2965
-                );
2966
-            }
2967
-
2968
-            $this->_return_json();
2969
-        }
2970
-
2971
-
2972
-        // was a test send triggered?
2973
-        if (isset($this->_req_data['test_button'])) {
2974
-            EE_Error::overwrite_success();
2975
-            $this->_do_test_send($context_slug, $messenger_slug, $message_type_slug);
2976
-            $override = true;
2977
-        }
2978
-
2979
-        if (empty($query_args)) {
2980
-            $query_args = array(
2981
-                'id'      => $this->_req_data['GRP_ID'],
2982
-                'context' => $context_slug,
2983
-                'action'  => 'edit_message_template',
2984
-            );
2985
-        }
2986
-
2987
-        $this->_redirect_after_action($success, $item_desc, $action_desc, $query_args, $override);
2988
-    }
2989
-
2990
-
2991
-    /**
2992
-     * processes a test send request to do an actual messenger delivery test for the given message template being tested
2993
-     *
2994
-     * @param  string $context      what context being tested
2995
-     * @param  string $messenger    messenger being tested
2996
-     * @param  string $message_type message type being tested
2997
-     * @throws EE_Error
2998
-     * @throws InvalidArgumentException
2999
-     * @throws InvalidDataTypeException
3000
-     * @throws InvalidInterfaceException
3001
-     */
3002
-    protected function _do_test_send($context, $messenger, $message_type)
3003
-    {
3004
-        // set things up for preview
3005
-        $this->_req_data['messenger'] = $messenger;
3006
-        $this->_req_data['message_type'] = $message_type;
3007
-        $this->_req_data['context'] = $context;
3008
-        $this->_req_data['GRP_ID'] = isset($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : '';
3009
-        $active_messenger = $this->_message_resource_manager->get_active_messenger($messenger);
3010
-
3011
-        // let's save any existing fields that might be required by the messenger
3012
-        if (isset($this->_req_data['test_settings_fld'])
3013
-            && $active_messenger instanceof EE_messenger
3014
-            && apply_filters(
3015
-                'FHEE__Messages_Admin_Page__do_test_send__set_existing_test_settings',
3016
-                true,
3017
-                $this->_req_data['test_settings_fld'],
3018
-                $active_messenger
3019
-            )
3020
-        ) {
3021
-            $active_messenger->set_existing_test_settings($this->_req_data['test_settings_fld']);
3022
-        }
3023
-
3024
-        /**
3025
-         * Use filter to add additional controls on whether message can send or not
3026
-         */
3027
-        if (apply_filters(
3028
-            'FHEE__Messages_Admin_Page__do_test_send__can_send',
3029
-            true,
3030
-            $context,
3031
-            $this->_req_data,
3032
-            $messenger,
3033
-            $message_type
3034
-        )) {
3035
-            if ( EEM_Event::instance()->count() > 0 ) {
3036
-                $success = $this->_preview_message(true);
3037
-                if ($success) {
3038
-                    EE_Error::add_success(__('Test message sent', 'event_espresso'));
3039
-                } else {
3040
-                    EE_Error::add_error(
3041
-                        esc_html__('The test message was not sent', 'event_espresso'),
3042
-                        __FILE__,
3043
-                        __FUNCTION__,
3044
-                        __LINE__
3045
-                    );
3046
-                }
3047
-            } else {
3048
-                $this->noEventsErrorMessage(true);
3049
-            }
3050
-        }
3051
-    }
3052
-
3053
-
3054
-    /**
3055
-     * _generate_new_templates
3056
-     * This will handle the messenger, message_type selection when "adding a new custom template" for an event and will
3057
-     * automatically create the defaults for the event.  The user would then be redirected to edit the default context
3058
-     * for the event.
3059
-     *
3060
-     *
3061
-     * @param  string $messenger     the messenger we are generating templates for
3062
-     * @param array   $message_types array of message types that the templates are generated for.
3063
-     * @param int     $GRP_ID        If this is a custom template being generated then a GRP_ID needs to be included to
3064
-     *                               indicate the message_template_group being used as the base.
3065
-     *
3066
-     * @param bool    $global
3067
-     *
3068
-     * @return array|bool array of data required for the redirect to the correct edit page or bool if
3069
-     *                               encountering problems.
3070
-     * @throws EE_Error
3071
-     */
3072
-    protected function _generate_new_templates($messenger, $message_types, $GRP_ID = 0, $global = false)
3073
-    {
3074
-
3075
-        // if no $message_types are given then that's okay... this may be a messenger that just adds shortcodes, so we
3076
-        // just don't generate any templates.
3077
-        if (empty($message_types)) {
3078
-            return true;
3079
-        }
3080
-
3081
-        return EEH_MSG_Template::generate_new_templates($messenger, $message_types, $GRP_ID, $global);
3082
-    }
3083
-
3084
-
3085
-    /**
3086
-     * [_trash_or_restore_message_template]
3087
-     *
3088
-     * @param  boolean $trash whether to move an item to trash/restore (TRUE) or restore it (FALSE)
3089
-     * @param boolean  $all   whether this is going to trash/restore all contexts within a template group (TRUE) OR just
3090
-     *                        an individual context (FALSE).
3091
-     * @return void
3092
-     * @throws EE_Error
3093
-     * @throws InvalidArgumentException
3094
-     * @throws InvalidDataTypeException
3095
-     * @throws InvalidInterfaceException
3096
-     */
3097
-    protected function _trash_or_restore_message_template($trash = true, $all = false)
3098
-    {
3099
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
3100
-        $MTP = EEM_Message_Template_Group::instance();
3101
-
3102
-        $success = 1;
3103
-
3104
-        // incoming GRP_IDs
3105
-        if ($all) {
3106
-            // Checkboxes
3107
-            if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3108
-                // if array has more than one element then success message should be plural.
3109
-                // todo: what about nonce?
3110
-                $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
3111
-
3112
-                // cycle through checkboxes
3113
-                while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) {
3114
-                    $trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID);
3115
-                    if (! $trashed_or_restored) {
3116
-                        $success = 0;
3117
-                    }
3118
-                }
3119
-            } else {
3120
-                // grab single GRP_ID and handle
3121
-                $GRP_ID = isset($this->_req_data['id']) ? absint($this->_req_data['id']) : 0;
3122
-                if (! empty($GRP_ID)) {
3123
-                    $trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID);
3124
-                    if (! $trashed_or_restored) {
3125
-                        $success = 0;
3126
-                    }
3127
-                } else {
3128
-                    $success = 0;
3129
-                }
3130
-            }
3131
-        }
3132
-
3133
-        $action_desc = $trash
3134
-            ? esc_html__('moved to the trash', 'event_espresso')
3135
-            : esc_html__('restored', 'event_espresso');
3136
-
3137
-        $action_desc = ! empty($this->_req_data['template_switch']) ? esc_html__('switched', 'event_espresso') : $action_desc;
3138
-
3139
-        $item_desc = $all ? _n(
3140
-            'Message Template Group',
3141
-            'Message Template Groups',
3142
-            $success,
3143
-            'event_espresso'
3144
-        ) : _n('Message Template Context', 'Message Template Contexts', $success, 'event_espresso');
3145
-
3146
-        $item_desc = ! empty($this->_req_data['template_switch']) ? _n(
3147
-            'template',
3148
-            'templates',
3149
-            $success,
3150
-            'event_espresso'
3151
-        ) : $item_desc;
3152
-
3153
-        $this->_redirect_after_action($success, $item_desc, $action_desc, array());
3154
-    }
3155
-
3156
-
3157
-    /**
3158
-     * [_delete_message_template]
3159
-     * NOTE: this handles not only the deletion of the groups but also all the templates belonging to that group.
3160
-     *
3161
-     * @return void
3162
-     * @throws EE_Error
3163
-     * @throws InvalidArgumentException
3164
-     * @throws InvalidDataTypeException
3165
-     * @throws InvalidInterfaceException
3166
-     */
3167
-    protected function _delete_message_template()
3168
-    {
3169
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
3170
-
3171
-        // checkboxes
3172
-        if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3173
-            // if array has more than one element then success message should be plural
3174
-            $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
3175
-
3176
-            // cycle through bulk action checkboxes
3177
-            while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) {
3178
-                $success = $this->_delete_mtp_permanently($GRP_ID);
3179
-            }
3180
-        } else {
3181
-            // grab single grp_id and delete
3182
-            $GRP_ID = absint($this->_req_data['id']);
3183
-            $success = $this->_delete_mtp_permanently($GRP_ID);
3184
-        }
3185
-
3186
-        $this->_redirect_after_action($success, 'Message Templates', 'deleted', array());
3187
-    }
3188
-
3189
-
3190
-    /**
3191
-     * helper for permanently deleting a mtP group and all related message_templates
3192
-     *
3193
-     * @param  int  $GRP_ID        The group being deleted
3194
-     * @param  bool $include_group whether to delete the Message Template Group as well.
3195
-     * @return bool boolean to indicate the success of the deletes or not.
3196
-     * @throws EE_Error
3197
-     * @throws InvalidArgumentException
3198
-     * @throws InvalidDataTypeException
3199
-     * @throws InvalidInterfaceException
3200
-     */
3201
-    private function _delete_mtp_permanently($GRP_ID, $include_group = true)
3202
-    {
3203
-        $success = 1;
3204
-        $MTPG = EEM_Message_Template_Group::instance();
3205
-        // first let's GET this group
3206
-        $MTG = $MTPG->get_one_by_ID($GRP_ID);
3207
-        // then delete permanently all the related Message Templates
3208
-        $deleted = $MTG->delete_related_permanently('Message_Template');
3209
-
3210
-        if ($deleted === 0) {
3211
-            $success = 0;
3212
-        }
3213
-
3214
-        // now delete permanently this particular group
3215
-
3216
-        if ($include_group && ! $MTG->delete_permanently()) {
3217
-            $success = 0;
3218
-        }
3219
-
3220
-        return $success;
3221
-    }
3222
-
3223
-
3224
-    /**
3225
-     *    _learn_more_about_message_templates_link
3226
-     *
3227
-     * @access protected
3228
-     * @return string
3229
-     */
3230
-    protected function _learn_more_about_message_templates_link()
3231
-    {
3232
-        return '<a class="hidden" style="margin:0 20px; cursor:pointer; font-size:12px;" >'
3233
-               . esc_html__('learn more about how message templates works', 'event_espresso')
3234
-               . '</a>';
3235
-    }
3236
-
3237
-
3238
-    /**
3239
-     * Used for setting up messenger/message type activation.  This loads up the initial view.  The rest is handled by
3240
-     * ajax and other routes.
3241
-     *
3242
-     * @return void
3243
-     * @throws DomainException
3244
-     */
3245
-    protected function _settings()
3246
-    {
3247
-
3248
-
3249
-        $this->_set_m_mt_settings();
3250
-
3251
-        $selected_messenger = isset($this->_req_data['selected_messenger'])
3252
-            ? $this->_req_data['selected_messenger']
3253
-            : 'email';
3254
-
3255
-        // let's setup the messenger tabs
3256
-        $this->_template_args['admin_page_header'] = EEH_Tabbed_Content::tab_text_links(
3257
-            $this->_m_mt_settings['messenger_tabs'],
3258
-            'messenger_links',
3259
-            '|',
3260
-            $selected_messenger
3261
-        );
3262
-        $this->_template_args['before_admin_page_content'] = '<div class="ui-widget ui-helper-clearfix">';
3263
-        $this->_template_args['after_admin_page_content'] = '</div><!-- end .ui-widget -->';
3264
-
3265
-        $this->display_admin_page_with_sidebar();
3266
-    }
3267
-
3268
-
3269
-    /**
3270
-     * This sets the $_m_mt_settings property for when needed (used on the Messages settings page)
3271
-     *
3272
-     * @access protected
3273
-     * @return void
3274
-     * @throws DomainException
3275
-     */
3276
-    protected function _set_m_mt_settings()
3277
-    {
3278
-        // first if this is already set then lets get out no need to regenerate data.
3279
-        if (! empty($this->_m_mt_settings)) {
3280
-            return;
3281
-        }
3282
-
3283
-        // get all installed messengers and message_types
3284
-        /** @type EE_messenger[] $messengers */
3285
-        $messengers = $this->_message_resource_manager->installed_messengers();
3286
-        /** @type EE_message_type[] $message_types */
3287
-        $message_types = $this->_message_resource_manager->installed_message_types();
3288
-
3289
-
3290
-        // assemble the array for the _tab_text_links helper
3291
-
3292
-        foreach ($messengers as $messenger) {
3293
-            $this->_m_mt_settings['messenger_tabs'][ $messenger->name ] = array(
3294
-                'label' => ucwords($messenger->label['singular']),
3295
-                'class' => $this->_message_resource_manager->is_messenger_active($messenger->name)
3296
-                    ? 'messenger-active'
3297
-                    : '',
3298
-                'href'  => $messenger->name,
3299
-                'title' => esc_html__('Modify this Messenger', 'event_espresso'),
3300
-                'slug'  => $messenger->name,
3301
-                'obj'   => $messenger,
3302
-            );
3303
-
3304
-
3305
-            $message_types_for_messenger = $messenger->get_valid_message_types();
3306
-
3307
-            foreach ($message_types as $message_type) {
3308
-                // first we need to verify that this message type is valid with this messenger. Cause if it isn't then
3309
-                // it shouldn't show in either the inactive OR active metabox.
3310
-                if (! in_array($message_type->name, $message_types_for_messenger, true)) {
3311
-                    continue;
3312
-                }
3313
-
3314
-                $a_or_i = $this->_message_resource_manager->is_message_type_active_for_messenger(
3315
-                    $messenger->name,
3316
-                    $message_type->name
3317
-                )
3318
-                    ? 'active'
3319
-                    : 'inactive';
3320
-
3321
-                $this->_m_mt_settings['message_type_tabs'][ $messenger->name ][ $a_or_i ][ $message_type->name ] = array(
3322
-                    'label'    => ucwords($message_type->label['singular']),
3323
-                    'class'    => 'message-type-' . $a_or_i,
3324
-                    'slug_id'  => $message_type->name . '-messagetype-' . $messenger->name,
3325
-                    'mt_nonce' => wp_create_nonce($message_type->name . '_nonce'),
3326
-                    'href'     => 'espresso_' . $message_type->name . '_message_type_settings',
3327
-                    'title'    => $a_or_i === 'active'
3328
-                        ? esc_html__('Drag this message type to the Inactive window to deactivate', 'event_espresso')
3329
-                        : esc_html__('Drag this message type to the messenger to activate', 'event_espresso'),
3330
-                    'content'  => $a_or_i === 'active'
3331
-                        ? $this->_message_type_settings_content($message_type, $messenger, true)
3332
-                        : $this->_message_type_settings_content($message_type, $messenger),
3333
-                    'slug'     => $message_type->name,
3334
-                    'active'   => $a_or_i === 'active',
3335
-                    'obj'      => $message_type,
3336
-                );
3337
-            }
3338
-        }
3339
-    }
3340
-
3341
-
3342
-    /**
3343
-     * This just prepares the content for the message type settings
3344
-     *
3345
-     * @param  EE_message_type $message_type The message type object
3346
-     * @param  EE_messenger    $messenger    The messenger object
3347
-     * @param  boolean         $active       Whether the message type is active or not
3348
-     * @return string html output for the content
3349
-     * @throws DomainException
3350
-     */
3351
-    protected function _message_type_settings_content($message_type, $messenger, $active = false)
3352
-    {
3353
-        // get message type fields
3354
-        $fields = $message_type->get_admin_settings_fields();
3355
-        $settings_template_args['template_form_fields'] = '';
3356
-
3357
-        if (! empty($fields) && $active) {
3358
-            $existing_settings = $message_type->get_existing_admin_settings($messenger->name);
3359
-            foreach ($fields as $fldname => $fldprops) {
3360
-                $field_id = $messenger->name . '-' . $message_type->name . '-' . $fldname;
3361
-                $template_form_field[ $field_id ] = array(
3362
-                    'name'       => 'message_type_settings[' . $fldname . ']',
3363
-                    'label'      => $fldprops['label'],
3364
-                    'input'      => $fldprops['field_type'],
3365
-                    'type'       => $fldprops['value_type'],
3366
-                    'required'   => $fldprops['required'],
3367
-                    'validation' => $fldprops['validation'],
3368
-                    'value'      => isset($existing_settings[ $fldname ])
3369
-                        ? $existing_settings[ $fldname ]
3370
-                        : $fldprops['default'],
3371
-                    'options'    => isset($fldprops['options'])
3372
-                        ? $fldprops['options']
3373
-                        : array(),
3374
-                    'default'    => isset($existing_settings[ $fldname ])
3375
-                        ? $existing_settings[ $fldname ]
3376
-                        : $fldprops['default'],
3377
-                    'css_class'  => 'no-drag',
3378
-                    'format'     => $fldprops['format'],
3379
-                );
3380
-            }
3381
-
3382
-
3383
-            $settings_template_args['template_form_fields'] = ! empty($template_form_field)
3384
-                ? $this->_generate_admin_form_fields(
3385
-                    $template_form_field,
3386
-                    'string',
3387
-                    'ee_mt_activate_form'
3388
-                )
3389
-                : '';
3390
-        }
3391
-
3392
-        $settings_template_args['description'] = $message_type->description;
3393
-        // we also need some hidden fields
3394
-        $settings_template_args['hidden_fields'] = array(
3395
-            'message_type_settings[messenger]'    => array(
3396
-                'type'  => 'hidden',
3397
-                'value' => $messenger->name,
3398
-            ),
3399
-            'message_type_settings[message_type]' => array(
3400
-                'type'  => 'hidden',
3401
-                'value' => $message_type->name,
3402
-            ),
3403
-            'type'                                => array(
3404
-                'type'  => 'hidden',
3405
-                'value' => 'message_type',
3406
-            ),
3407
-        );
3408
-
3409
-        $settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields(
3410
-            $settings_template_args['hidden_fields'],
3411
-            'array'
3412
-        );
3413
-        $settings_template_args['show_form'] = empty($settings_template_args['template_form_fields'])
3414
-            ? ' hidden'
3415
-            : '';
3416
-
3417
-
3418
-        $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_mt_settings_content.template.php';
3419
-        $content = EEH_Template::display_template($template, $settings_template_args, true);
3420
-
3421
-        return $content;
3422
-    }
3423
-
3424
-
3425
-    /**
3426
-     * Generate all the metaboxes for the message types and register them for the messages settings page.
3427
-     *
3428
-     * @access protected
3429
-     * @return void
3430
-     * @throws DomainException
3431
-     */
3432
-    protected function _messages_settings_metaboxes()
3433
-    {
3434
-        $this->_set_m_mt_settings();
3435
-        $m_boxes = $mt_boxes = array();
3436
-        $m_template_args = $mt_template_args = array();
3437
-
3438
-        $selected_messenger = isset($this->_req_data['selected_messenger'])
3439
-            ? $this->_req_data['selected_messenger']
3440
-            : 'email';
3441
-
3442
-        if (isset($this->_m_mt_settings['messenger_tabs'])) {
3443
-            foreach ($this->_m_mt_settings['messenger_tabs'] as $messenger => $tab_array) {
3444
-                $hide_on_message = $this->_message_resource_manager->is_messenger_active($messenger) ? '' : 'hidden';
3445
-                $hide_off_message = $this->_message_resource_manager->is_messenger_active($messenger) ? 'hidden' : '';
3446
-                // messenger meta boxes
3447
-                $active = $selected_messenger === $messenger;
3448
-                $active_mt_tabs = isset(
3449
-                    $this->_m_mt_settings['message_type_tabs'][ $messenger ]['active']
3450
-                )
3451
-                    ? $this->_m_mt_settings['message_type_tabs'][ $messenger ]['active']
3452
-                    : '';
3453
-                $m_boxes[ $messenger . '_a_box' ] = sprintf(
3454
-                    esc_html__('%s Settings', 'event_espresso'),
3455
-                    $tab_array['label']
3456
-                );
3457
-                $m_template_args[ $messenger . '_a_box' ] = array(
3458
-                    'active_message_types'   => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '',
3459
-                    'inactive_message_types' => isset(
3460
-                        $this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive']
3461
-                    )
3462
-                        ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive'])
3463
-                        : '',
3464
-                    'content'                => $this->_get_messenger_box_content($tab_array['obj']),
3465
-                    'hidden'                 => $active ? '' : ' hidden',
3466
-                    'hide_on_message'        => $hide_on_message,
3467
-                    'messenger'              => $messenger,
3468
-                    'active'                 => $active,
3469
-                );
3470
-                // message type meta boxes
3471
-                // (which is really just the inactive container for each messenger
3472
-                // showing inactive message types for that messenger)
3473
-                $mt_boxes[ $messenger . '_i_box' ] = esc_html__('Inactive Message Types', 'event_espresso');
3474
-                $mt_template_args[ $messenger . '_i_box' ] = array(
3475
-                    'active_message_types'   => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '',
3476
-                    'inactive_message_types' => isset(
3477
-                        $this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive']
3478
-                    )
3479
-                        ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive'])
3480
-                        : '',
3481
-                    'hidden'                 => $active ? '' : ' hidden',
3482
-                    'hide_on_message'        => $hide_on_message,
3483
-                    'hide_off_message'       => $hide_off_message,
3484
-                    'messenger'              => $messenger,
3485
-                    'active'                 => $active,
3486
-                );
3487
-            }
3488
-        }
3489
-
3490
-
3491
-        // register messenger metaboxes
3492
-        $m_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_mt_meta_box.template.php';
3493
-        foreach ($m_boxes as $box => $label) {
3494
-            $callback_args = array('template_path' => $m_template_path, 'template_args' => $m_template_args[ $box ]);
3495
-            $msgr = str_replace('_a_box', '', $box);
3496
-            add_meta_box(
3497
-                'espresso_' . $msgr . '_settings',
3498
-                $label,
3499
-                function ($post, $metabox) {
3500
-                    echo EEH_Template::display_template(
3501
-                        $metabox["args"]["template_path"],
3502
-                        $metabox["args"]["template_args"],
3503
-                        true
3504
-                    );
3505
-                },
3506
-                $this->_current_screen->id,
3507
-                'normal',
3508
-                'high',
3509
-                $callback_args
3510
-            );
3511
-        }
3512
-
3513
-        // register message type metaboxes
3514
-        $mt_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_meta_box.template.php';
3515
-        foreach ($mt_boxes as $box => $label) {
3516
-            $callback_args = array(
3517
-                'template_path' => $mt_template_path,
3518
-                'template_args' => $mt_template_args[ $box ],
3519
-            );
3520
-            $mt = str_replace('_i_box', '', $box);
3521
-            add_meta_box(
3522
-                'espresso_' . $mt . '_inactive_mts',
3523
-                $label,
3524
-                function ($post, $metabox) {
3525
-                    echo EEH_Template::display_template(
3526
-                        $metabox["args"]["template_path"],
3527
-                        $metabox["args"]["template_args"],
3528
-                        true
3529
-                    );
3530
-                },
3531
-                $this->_current_screen->id,
3532
-                'side',
3533
-                'high',
3534
-                $callback_args
3535
-            );
3536
-        }
3537
-
3538
-        // register metabox for global messages settings but only when on the main site.  On single site installs this
3539
-        // will always result in the metabox showing, on multisite installs the metabox will only show on the main site.
3540
-        if (is_main_site()) {
3541
-            add_meta_box(
3542
-                'espresso_global_message_settings',
3543
-                esc_html__('Global Message Settings', 'event_espresso'),
3544
-                array($this, 'global_messages_settings_metabox_content'),
3545
-                $this->_current_screen->id,
3546
-                'normal',
3547
-                'low',
3548
-                array()
3549
-            );
3550
-        }
3551
-    }
3552
-
3553
-
3554
-    /**
3555
-     *  This generates the content for the global messages settings metabox.
3556
-     *
3557
-     * @return string
3558
-     * @throws EE_Error
3559
-     * @throws InvalidArgumentException
3560
-     * @throws ReflectionException
3561
-     * @throws InvalidDataTypeException
3562
-     * @throws InvalidInterfaceException
3563
-     */
3564
-    public function global_messages_settings_metabox_content()
3565
-    {
3566
-        $form = $this->_generate_global_settings_form();
3567
-        echo $form->form_open(
3568
-            $this->add_query_args_and_nonce(array('action' => 'update_global_settings'), EE_MSG_ADMIN_URL),
3569
-            'POST'
3570
-        )
3571
-             . $form->get_html()
3572
-             . $form->form_close();
3573
-    }
3574
-
3575
-
3576
-    /**
3577
-     * This generates and returns the form object for the global messages settings.
3578
-     *
3579
-     * @return EE_Form_Section_Proper
3580
-     * @throws EE_Error
3581
-     * @throws InvalidArgumentException
3582
-     * @throws ReflectionException
3583
-     * @throws InvalidDataTypeException
3584
-     * @throws InvalidInterfaceException
3585
-     */
3586
-    protected function _generate_global_settings_form()
3587
-    {
3588
-        EE_Registry::instance()->load_helper('HTML');
3589
-        /** @var EE_Network_Core_Config $network_config */
3590
-        $network_config = EE_Registry::instance()->NET_CFG->core;
3591
-
3592
-        return new EE_Form_Section_Proper(
3593
-            array(
3594
-                'name'            => 'global_messages_settings',
3595
-                'html_id'         => 'global_messages_settings',
3596
-                'html_class'      => 'form-table',
3597
-                'layout_strategy' => new EE_Admin_Two_Column_Layout(),
3598
-                'subsections'     => apply_filters(
3599
-                    'FHEE__Messages_Admin_Page__global_messages_settings_metabox_content__form_subsections',
3600
-                    array(
3601
-                        'do_messages_on_same_request' => new EE_Select_Input(
3602
-                            array(
3603
-                                true  => esc_html__("On the same request", "event_espresso"),
3604
-                                false => esc_html__("On a separate request", "event_espresso"),
3605
-                            ),
3606
-                            array(
3607
-                                'default'         => $network_config->do_messages_on_same_request,
3608
-                                'html_label_text' => esc_html__(
3609
-                                    'Generate and send all messages:',
3610
-                                    'event_espresso'
3611
-                                ),
3612
-                                'html_help_text'  => esc_html__(
3613
-                                    'By default the messages system uses a more efficient means of processing messages on separate requests and utilizes the wp-cron scheduling system.  This makes things execute faster for people registering for your events.  However, if the wp-cron system is disabled on your site and there is no alternative in place, then you can change this so messages are always executed on the same request.',
3614
-                                    'event_espresso'
3615
-                                ),
3616
-                            )
3617
-                        ),
3618
-                        'delete_threshold'            => new EE_Select_Input(
3619
-                            array(
3620
-                                0  => esc_html__('Forever', 'event_espresso'),
3621
-                                3  => esc_html__('3 Months', 'event_espresso'),
3622
-                                6  => esc_html__('6 Months', 'event_espresso'),
3623
-                                9  => esc_html__('9 Months', 'event_espresso'),
3624
-                                12 => esc_html__('12 Months', 'event_espresso'),
3625
-                                24 => esc_html__('24 Months', 'event_espresso'),
3626
-                                36 => esc_html__('36 Months', 'event_espresso'),
3627
-                            ),
3628
-                            array(
3629
-                                'default'         => EE_Registry::instance()->CFG->messages->delete_threshold,
3630
-                                'html_label_text' => esc_html__('Cleanup of old messages:', 'event_espresso'),
3631
-                                'html_help_text'  => esc_html__(
3632
-                                    'You can control how long a record of processed messages is kept via this option.',
3633
-                                    'event_espresso'
3634
-                                ),
3635
-                            )
3636
-                        ),
3637
-                        'update_settings'             => new EE_Submit_Input(
3638
-                            array(
3639
-                                'default'         => esc_html__('Update', 'event_espresso'),
3640
-                                'html_label_text' => '&nbsp',
3641
-                            )
3642
-                        ),
3643
-                    )
3644
-                ),
3645
-            )
3646
-        );
3647
-    }
3648
-
3649
-
3650
-    /**
3651
-     * This handles updating the global settings set on the admin page.
3652
-     *
3653
-     * @throws EE_Error
3654
-     * @throws InvalidDataTypeException
3655
-     * @throws InvalidInterfaceException
3656
-     * @throws InvalidArgumentException
3657
-     * @throws ReflectionException
3658
-     */
3659
-    protected function _update_global_settings()
3660
-    {
3661
-        /** @var EE_Network_Core_Config $network_config */
3662
-        $network_config = EE_Registry::instance()->NET_CFG->core;
3663
-        $messages_config = EE_Registry::instance()->CFG->messages;
3664
-        $form = $this->_generate_global_settings_form();
3665
-        if ($form->was_submitted()) {
3666
-            $form->receive_form_submission();
3667
-            if ($form->is_valid()) {
3668
-                $valid_data = $form->valid_data();
3669
-                foreach ($valid_data as $property => $value) {
3670
-                    $setter = 'set_' . $property;
3671
-                    if (method_exists($network_config, $setter)) {
3672
-                        $network_config->{$setter}($value);
3673
-                    } elseif (property_exists($network_config, $property)
3674
-                        && $network_config->{$property} !== $value
3675
-                    ) {
3676
-                        $network_config->{$property} = $value;
3677
-                    } elseif (property_exists($messages_config, $property)
3678
-                        && $messages_config->{$property} !== $value
3679
-                    ) {
3680
-                        $messages_config->{$property} = $value;
3681
-                    }
3682
-                }
3683
-                // only update if the form submission was valid!
3684
-                EE_Registry::instance()->NET_CFG->update_config(true, false);
3685
-                EE_Registry::instance()->CFG->update_espresso_config();
3686
-                EE_Error::overwrite_success();
3687
-                EE_Error::add_success(__('Global message settings were updated', 'event_espresso'));
3688
-            }
3689
-        }
3690
-        $this->_redirect_after_action(0, '', '', array('action' => 'settings'), true);
3691
-    }
3692
-
3693
-
3694
-    /**
3695
-     * this prepares the messenger tabs that can be dragged in and out of messenger boxes to activate/deactivate
3696
-     *
3697
-     * @param  array $tab_array This is an array of message type tab details used to generate the tabs
3698
-     * @return string html formatted tabs
3699
-     * @throws DomainException
3700
-     */
3701
-    protected function _get_mt_tabs($tab_array)
3702
-    {
3703
-        $tab_array = (array) $tab_array;
3704
-        $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_mt_settings_tab_item.template.php';
3705
-        $tabs = '';
3706
-
3707
-        foreach ($tab_array as $tab) {
3708
-            $tabs .= EEH_Template::display_template($template, $tab, true);
3709
-        }
3710
-
3711
-        return $tabs;
3712
-    }
3713
-
3714
-
3715
-    /**
3716
-     * This prepares the content of the messenger meta box admin settings
3717
-     *
3718
-     * @param  EE_messenger $messenger The messenger we're setting up content for
3719
-     * @return string html formatted content
3720
-     * @throws DomainException
3721
-     */
3722
-    protected function _get_messenger_box_content(EE_messenger $messenger)
3723
-    {
3724
-
3725
-        $fields = $messenger->get_admin_settings_fields();
3726
-        $settings_template_args['template_form_fields'] = '';
3727
-
3728
-        // is $messenger active?
3729
-        $settings_template_args['active'] = $this->_message_resource_manager->is_messenger_active($messenger->name);
3730
-
3731
-
3732
-        if (! empty($fields)) {
3733
-            $existing_settings = $messenger->get_existing_admin_settings();
3734
-
3735
-            foreach ($fields as $fldname => $fldprops) {
3736
-                $field_id = $messenger->name . '-' . $fldname;
3737
-                $template_form_field[ $field_id ] = array(
3738
-                    'name'       => 'messenger_settings[' . $field_id . ']',
3739
-                    'label'      => $fldprops['label'],
3740
-                    'input'      => $fldprops['field_type'],
3741
-                    'type'       => $fldprops['value_type'],
3742
-                    'required'   => $fldprops['required'],
3743
-                    'validation' => $fldprops['validation'],
3744
-                    'value'      => isset($existing_settings[ $field_id ])
3745
-                        ? $existing_settings[ $field_id ]
3746
-                        : $fldprops['default'],
3747
-                    'css_class'  => '',
3748
-                    'format'     => $fldprops['format'],
3749
-                );
3750
-            }
3751
-
3752
-
3753
-            $settings_template_args['template_form_fields'] = ! empty($template_form_field)
3754
-                ? $this->_generate_admin_form_fields($template_form_field, 'string', 'ee_m_activate_form')
3755
-                : '';
3756
-        }
3757
-
3758
-        // we also need some hidden fields
3759
-        $settings_template_args['hidden_fields'] = array(
3760
-            'messenger_settings[messenger]' => array(
3761
-                'type'  => 'hidden',
3762
-                'value' => $messenger->name,
3763
-            ),
3764
-            'type'                          => array(
3765
-                'type'  => 'hidden',
3766
-                'value' => 'messenger',
3767
-            ),
3768
-        );
3769
-
3770
-        // make sure any active message types that are existing are included in the hidden fields
3771
-        if (isset($this->_m_mt_settings['message_type_tabs'][ $messenger->name ]['active'])) {
3772
-            foreach ($this->_m_mt_settings['message_type_tabs'][ $messenger->name ]['active'] as $mt => $values) {
3773
-                $settings_template_args['hidden_fields'][ 'messenger_settings[message_types][' . $mt . ']' ] = array(
3774
-                    'type'  => 'hidden',
3775
-                    'value' => $mt,
3776
-                );
3777
-            }
3778
-        }
3779
-        $settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields(
3780
-            $settings_template_args['hidden_fields'],
3781
-            'array'
3782
-        );
3783
-        $active = $this->_message_resource_manager->is_messenger_active($messenger->name);
3784
-
3785
-        $settings_template_args['messenger'] = $messenger->name;
3786
-        $settings_template_args['description'] = $messenger->description;
3787
-        $settings_template_args['show_hide_edit_form'] = $active ? '' : ' hidden';
3788
-
3789
-
3790
-        $settings_template_args['show_hide_edit_form'] = $this->_message_resource_manager->is_messenger_active(
3791
-            $messenger->name
3792
-        )
3793
-            ? $settings_template_args['show_hide_edit_form']
3794
-            : ' hidden';
3795
-
3796
-        $settings_template_args['show_hide_edit_form'] = empty($settings_template_args['template_form_fields'])
3797
-            ? ' hidden'
3798
-            : $settings_template_args['show_hide_edit_form'];
3799
-
3800
-
3801
-        $settings_template_args['on_off_action'] = $active ? 'messenger-off' : 'messenger-on';
3802
-        $settings_template_args['nonce'] = wp_create_nonce('activate_' . $messenger->name . '_toggle_nonce');
3803
-        $settings_template_args['on_off_status'] = $active ? true : false;
3804
-        $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_m_settings_content.template.php';
3805
-        $content = EEH_Template::display_template(
3806
-            $template,
3807
-            $settings_template_args,
3808
-            true
3809
-        );
3810
-
3811
-        return $content;
3812
-    }
3813
-
3814
-
3815
-    /**
3816
-     * used by ajax on the messages settings page to activate|deactivate the messenger
3817
-     *
3818
-     * @throws DomainException
3819
-     * @throws EE_Error
3820
-     * @throws InvalidDataTypeException
3821
-     * @throws InvalidInterfaceException
3822
-     * @throws InvalidArgumentException
3823
-     * @throws ReflectionException
3824
-     */
3825
-    public function activate_messenger_toggle()
3826
-    {
3827
-        $success = true;
3828
-        $this->_prep_default_response_for_messenger_or_message_type_toggle();
3829
-        // let's check that we have required data
3830
-        if (! isset($this->_req_data['messenger'])) {
3831
-            EE_Error::add_error(
3832
-                esc_html__('Messenger name needed to toggle activation. None given', 'event_espresso'),
3833
-                __FILE__,
3834
-                __FUNCTION__,
3835
-                __LINE__
3836
-            );
3837
-            $success = false;
3838
-        }
3839
-
3840
-        // do a nonce check here since we're not arriving via a normal route
3841
-        $nonce = isset($this->_req_data['activate_nonce'])
3842
-            ? sanitize_text_field($this->_req_data['activate_nonce'])
3843
-            : '';
3844
-        $nonce_ref = 'activate_' . $this->_req_data['messenger'] . '_toggle_nonce';
3845
-
3846
-        $this->_verify_nonce($nonce, $nonce_ref);
3847
-
3848
-
3849
-        if (! isset($this->_req_data['status'])) {
3850
-            EE_Error::add_error(
3851
-                esc_html__(
3852
-                    'Messenger status needed to know whether activation or deactivation is happening. No status is given',
3853
-                    'event_espresso'
3854
-                ),
3855
-                __FILE__,
3856
-                __FUNCTION__,
3857
-                __LINE__
3858
-            );
3859
-            $success = false;
3860
-        }
3861
-
3862
-        // do check to verify we have a valid status.
3863
-        $status = $this->_req_data['status'];
3864
-
3865
-        if ($status !== 'off' && $status !== 'on') {
3866
-            EE_Error::add_error(
3867
-                sprintf(
3868
-                    esc_html__('The given status (%s) is not valid. Must be "off" or "on"', 'event_espresso'),
3869
-                    $this->_req_data['status']
3870
-                ),
3871
-                __FILE__,
3872
-                __FUNCTION__,
3873
-                __LINE__
3874
-            );
3875
-            $success = false;
3876
-        }
3877
-
3878
-        if ($success) {
3879
-            // made it here?  Stop dawdling then!!
3880
-            $success = $status === 'off'
3881
-                ? $this->_deactivate_messenger($this->_req_data['messenger'])
3882
-                : $this->_activate_messenger($this->_req_data['messenger']);
3883
-        }
3884
-
3885
-        $this->_template_args['success'] = $success;
3886
-
3887
-        // no special instructions so let's just do the json return (which should automatically do all the special stuff).
3888
-        $this->_return_json();
3889
-    }
3890
-
3891
-
3892
-    /**
3893
-     * used by ajax from the messages settings page to activate|deactivate a message type
3894
-     *
3895
-     * @throws DomainException
3896
-     * @throws EE_Error
3897
-     * @throws ReflectionException
3898
-     * @throws InvalidDataTypeException
3899
-     * @throws InvalidInterfaceException
3900
-     * @throws InvalidArgumentException
3901
-     */
3902
-    public function activate_mt_toggle()
3903
-    {
3904
-        $success = true;
3905
-        $this->_prep_default_response_for_messenger_or_message_type_toggle();
3906
-
3907
-        // let's make sure we have the necessary data
3908
-        if (! isset($this->_req_data['message_type'])) {
3909
-            EE_Error::add_error(
3910
-                esc_html__('Message Type name needed to toggle activation. None given', 'event_espresso'),
3911
-                __FILE__,
3912
-                __FUNCTION__,
3913
-                __LINE__
3914
-            );
3915
-            $success = false;
3916
-        }
3917
-
3918
-        if (! isset($this->_req_data['messenger'])) {
3919
-            EE_Error::add_error(
3920
-                esc_html__('Messenger name needed to toggle activation. None given', 'event_espresso'),
3921
-                __FILE__,
3922
-                __FUNCTION__,
3923
-                __LINE__
3924
-            );
3925
-            $success = false;
3926
-        }
3927
-
3928
-        if (! isset($this->_req_data['status'])) {
3929
-            EE_Error::add_error(
3930
-                esc_html__(
3931
-                    'Messenger status needed to know whether activation or deactivation is happening. No status is given',
3932
-                    'event_espresso'
3933
-                ),
3934
-                __FILE__,
3935
-                __FUNCTION__,
3936
-                __LINE__
3937
-            );
3938
-            $success = false;
3939
-        }
3940
-
3941
-
3942
-        // do check to verify we have a valid status.
3943
-        $status = $this->_req_data['status'];
3944
-
3945
-        if ($status !== 'activate' && $status !== 'deactivate') {
3946
-            EE_Error::add_error(
3947
-                sprintf(
3948
-                    esc_html__('The given status (%s) is not valid. Must be "active" or "inactive"', 'event_espresso'),
3949
-                    $this->_req_data['status']
3950
-                ),
3951
-                __FILE__,
3952
-                __FUNCTION__,
3953
-                __LINE__
3954
-            );
3955
-            $success = false;
3956
-        }
3957
-
3958
-
3959
-        // do a nonce check here since we're not arriving via a normal route
3960
-        $nonce = isset($this->_req_data['mt_nonce']) ? sanitize_text_field($this->_req_data['mt_nonce']) : '';
3961
-        $nonce_ref = $this->_req_data['message_type'] . '_nonce';
3962
-
3963
-        $this->_verify_nonce($nonce, $nonce_ref);
3964
-
3965
-        if ($success) {
3966
-            // made it here? um, what are you waiting for then?
3967
-            $success = $status === 'deactivate'
3968
-                ? $this->_deactivate_message_type_for_messenger(
3969
-                    $this->_req_data['messenger'],
3970
-                    $this->_req_data['message_type']
3971
-                )
3972
-                : $this->_activate_message_type_for_messenger(
3973
-                    $this->_req_data['messenger'],
3974
-                    $this->_req_data['message_type']
3975
-                );
3976
-        }
3977
-
3978
-        $this->_template_args['success'] = $success;
3979
-        $this->_return_json();
3980
-    }
3981
-
3982
-
3983
-    /**
3984
-     * Takes care of processing activating a messenger and preparing the appropriate response.
3985
-     *
3986
-     * @param string $messenger_name The name of the messenger being activated
3987
-     * @return bool
3988
-     * @throws DomainException
3989
-     * @throws EE_Error
3990
-     * @throws InvalidArgumentException
3991
-     * @throws ReflectionException
3992
-     * @throws InvalidDataTypeException
3993
-     * @throws InvalidInterfaceException
3994
-     */
3995
-    protected function _activate_messenger($messenger_name)
3996
-    {
3997
-        /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
3998
-        $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
3999
-        $message_types_to_activate = $active_messenger instanceof EE_Messenger
4000
-            ? $active_messenger->get_default_message_types()
4001
-            : array();
4002
-
4003
-        // ensure is active
4004
-        $this->_message_resource_manager->activate_messenger($messenger_name, $message_types_to_activate);
4005
-
4006
-        // set response_data for reload
4007
-        foreach ($message_types_to_activate as $message_type_name) {
4008
-            /** @var EE_message_type $message_type */
4009
-            $message_type = $this->_message_resource_manager->get_message_type($message_type_name);
4010
-            if ($this->_message_resource_manager->is_message_type_active_for_messenger(
4011
-                $messenger_name,
4012
-                $message_type_name
4013
-            )
4014
-                && $message_type instanceof EE_message_type
4015
-            ) {
4016
-                $this->_template_args['data']['active_mts'][] = $message_type_name;
4017
-                if ($message_type->get_admin_settings_fields()) {
4018
-                    $this->_template_args['data']['mt_reload'][] = $message_type_name;
4019
-                }
4020
-            }
4021
-        }
4022
-
4023
-        // add success message for activating messenger
4024
-        return $this->_setup_response_message_for_activating_messenger_with_message_types($active_messenger);
4025
-    }
4026
-
4027
-
4028
-    /**
4029
-     * Takes care of processing deactivating a messenger and preparing the appropriate response.
4030
-     *
4031
-     * @param string $messenger_name The name of the messenger being activated
4032
-     * @return bool
4033
-     * @throws DomainException
4034
-     * @throws EE_Error
4035
-     * @throws InvalidArgumentException
4036
-     * @throws ReflectionException
4037
-     * @throws InvalidDataTypeException
4038
-     * @throws InvalidInterfaceException
4039
-     */
4040
-    protected function _deactivate_messenger($messenger_name)
4041
-    {
4042
-        /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4043
-        $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4044
-        $this->_message_resource_manager->deactivate_messenger($messenger_name);
4045
-
4046
-        return $this->_setup_response_message_for_deactivating_messenger_with_message_types($active_messenger);
4047
-    }
4048
-
4049
-
4050
-    /**
4051
-     * Takes care of processing activating a message type for a messenger and preparing the appropriate response.
4052
-     *
4053
-     * @param string $messenger_name    The name of the messenger the message type is being activated for.
4054
-     * @param string $message_type_name The name of the message type being activated for the messenger
4055
-     * @return bool
4056
-     * @throws DomainException
4057
-     * @throws EE_Error
4058
-     * @throws InvalidArgumentException
4059
-     * @throws ReflectionException
4060
-     * @throws InvalidDataTypeException
4061
-     * @throws InvalidInterfaceException
4062
-     */
4063
-    protected function _activate_message_type_for_messenger($messenger_name, $message_type_name)
4064
-    {
4065
-        /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4066
-        $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4067
-        /** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */
4068
-        $message_type_to_activate = $this->_message_resource_manager->get_message_type($message_type_name);
4069
-
4070
-        // ensure is active
4071
-        $this->_message_resource_manager->activate_messenger($messenger_name, $message_type_name);
4072
-
4073
-        // set response for load
4074
-        if ($this->_message_resource_manager->is_message_type_active_for_messenger(
4075
-            $messenger_name,
4076
-            $message_type_name
4077
-        )
4078
-        ) {
4079
-            $this->_template_args['data']['active_mts'][] = $message_type_name;
4080
-            if ($message_type_to_activate->get_admin_settings_fields()) {
4081
-                $this->_template_args['data']['mt_reload'][] = $message_type_name;
4082
-            }
4083
-        }
4084
-
4085
-        return $this->_setup_response_message_for_activating_messenger_with_message_types(
4086
-            $active_messenger,
4087
-            $message_type_to_activate
4088
-        );
4089
-    }
4090
-
4091
-
4092
-    /**
4093
-     * Takes care of processing deactivating a message type for a messenger and preparing the appropriate response.
4094
-     *
4095
-     * @param string $messenger_name    The name of the messenger the message type is being deactivated for.
4096
-     * @param string $message_type_name The name of the message type being deactivated for the messenger
4097
-     * @return bool
4098
-     * @throws DomainException
4099
-     * @throws EE_Error
4100
-     * @throws InvalidArgumentException
4101
-     * @throws ReflectionException
4102
-     * @throws InvalidDataTypeException
4103
-     * @throws InvalidInterfaceException
4104
-     */
4105
-    protected function _deactivate_message_type_for_messenger($messenger_name, $message_type_name)
4106
-    {
4107
-        /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4108
-        $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4109
-        /** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */
4110
-        $message_type_to_deactivate = $this->_message_resource_manager->get_message_type($message_type_name);
4111
-        $this->_message_resource_manager->deactivate_message_type_for_messenger($message_type_name, $messenger_name);
4112
-
4113
-        return $this->_setup_response_message_for_deactivating_messenger_with_message_types(
4114
-            $active_messenger,
4115
-            $message_type_to_deactivate
4116
-        );
4117
-    }
4118
-
4119
-
4120
-    /**
4121
-     * This just initializes the defaults for activating messenger and message type responses.
4122
-     */
4123
-    protected function _prep_default_response_for_messenger_or_message_type_toggle()
4124
-    {
4125
-        $this->_template_args['data']['active_mts'] = array();
4126
-        $this->_template_args['data']['mt_reload'] = array();
4127
-    }
4128
-
4129
-
4130
-    /**
4131
-     * Setup appropriate response for activating a messenger and/or message types
4132
-     *
4133
-     * @param EE_messenger         $messenger
4134
-     * @param EE_message_type|null $message_type
4135
-     * @return bool
4136
-     * @throws DomainException
4137
-     * @throws EE_Error
4138
-     * @throws InvalidArgumentException
4139
-     * @throws ReflectionException
4140
-     * @throws InvalidDataTypeException
4141
-     * @throws InvalidInterfaceException
4142
-     */
4143
-    protected function _setup_response_message_for_activating_messenger_with_message_types(
4144
-        $messenger,
4145
-        EE_Message_Type $message_type = null
4146
-    ) {
4147
-        // if $messenger isn't a valid messenger object then get out.
4148
-        if (! $messenger instanceof EE_Messenger) {
4149
-            EE_Error::add_error(
4150
-                esc_html__('The messenger being activated is not a valid messenger', 'event_espresso'),
4151
-                __FILE__,
4152
-                __FUNCTION__,
4153
-                __LINE__
4154
-            );
4155
-
4156
-            return false;
4157
-        }
4158
-        // activated
4159
-        if ($this->_template_args['data']['active_mts']) {
4160
-            EE_Error::overwrite_success();
4161
-            // activated a message type with the messenger
4162
-            if ($message_type instanceof EE_message_type) {
4163
-                EE_Error::add_success(
4164
-                    sprintf(
4165
-                        esc_html__(
4166
-                            '%s message type has been successfully activated with the %s messenger',
4167
-                            'event_espresso'
4168
-                        ),
4169
-                        ucwords($message_type->label['singular']),
4170
-                        ucwords($messenger->label['singular'])
4171
-                    )
4172
-                );
4173
-
4174
-                // if message type was invoice then let's make sure we activate the invoice payment method.
4175
-                if ($message_type->name === 'invoice') {
4176
-                    EE_Registry::instance()->load_lib('Payment_Method_Manager');
4177
-                    $pm = EE_Payment_Method_Manager::instance()->activate_a_payment_method_of_type('Invoice');
4178
-                    if ($pm instanceof EE_Payment_Method) {
4179
-                        EE_Error::add_attention(
4180
-                            esc_html__(
4181
-                                'Activating the invoice message type also automatically activates the invoice payment method.  If you do not wish the invoice payment method to be active, or to change its settings, visit the payment method admin page.',
4182
-                                'event_espresso'
4183
-                            )
4184
-                        );
4185
-                    }
4186
-                }
4187
-                // just toggles the entire messenger
4188
-            } else {
4189
-                EE_Error::add_success(
4190
-                    sprintf(
4191
-                        esc_html__('%s messenger has been successfully activated', 'event_espresso'),
4192
-                        ucwords($messenger->label['singular'])
4193
-                    )
4194
-                );
4195
-            }
4196
-
4197
-            return true;
4198
-
4199
-            // possible error condition. This will happen when our active_mts data is empty because it is validated for actual active
4200
-            // message types after the activation process.  However its possible some messengers don't HAVE any default_message_types
4201
-            // in which case we just give a success message for the messenger being successfully activated.
4202
-        } else {
4203
-            if (! $messenger->get_default_message_types()) {
4204
-                // messenger doesn't have any default message types so still a success.
4205
-                EE_Error::add_success(
4206
-                    sprintf(
4207
-                        esc_html__('%s messenger was successfully activated.', 'event_espresso'),
4208
-                        ucwords($messenger->label['singular'])
4209
-                    )
4210
-                );
4211
-
4212
-                return true;
4213
-            } else {
4214
-                EE_Error::add_error(
4215
-                    $message_type instanceof EE_message_type
4216
-                        ? sprintf(
4217
-                            esc_html__(
4218
-                                '%s message type was not successfully activated with the %s messenger',
4219
-                                'event_espresso'
4220
-                            ),
4221
-                            ucwords($message_type->label['singular']),
4222
-                            ucwords($messenger->label['singular'])
4223
-                        )
4224
-                        : sprintf(
4225
-                            esc_html__('%s messenger was not successfully activated', 'event_espresso'),
4226
-                            ucwords($messenger->label['singular'])
4227
-                        ),
4228
-                    __FILE__,
4229
-                    __FUNCTION__,
4230
-                    __LINE__
4231
-                );
4232
-
4233
-                return false;
4234
-            }
4235
-        }
4236
-    }
4237
-
4238
-
4239
-    /**
4240
-     * This sets up the appropriate response for deactivating a messenger and/or message type.
4241
-     *
4242
-     * @param EE_messenger         $messenger
4243
-     * @param EE_message_type|null $message_type
4244
-     * @return bool
4245
-     * @throws DomainException
4246
-     * @throws EE_Error
4247
-     * @throws InvalidArgumentException
4248
-     * @throws ReflectionException
4249
-     * @throws InvalidDataTypeException
4250
-     * @throws InvalidInterfaceException
4251
-     */
4252
-    protected function _setup_response_message_for_deactivating_messenger_with_message_types(
4253
-        $messenger,
4254
-        EE_message_type $message_type = null
4255
-    ) {
4256
-        EE_Error::overwrite_success();
4257
-
4258
-        // if $messenger isn't a valid messenger object then get out.
4259
-        if (! $messenger instanceof EE_Messenger) {
4260
-            EE_Error::add_error(
4261
-                esc_html__('The messenger being deactivated is not a valid messenger', 'event_espresso'),
4262
-                __FILE__,
4263
-                __FUNCTION__,
4264
-                __LINE__
4265
-            );
4266
-
4267
-            return false;
4268
-        }
4269
-
4270
-        if ($message_type instanceof EE_message_type) {
4271
-            $message_type_name = $message_type->name;
4272
-            EE_Error::add_success(
4273
-                sprintf(
4274
-                    esc_html__(
4275
-                        '%s message type has been successfully deactivated for the %s messenger.',
4276
-                        'event_espresso'
4277
-                    ),
4278
-                    ucwords($message_type->label['singular']),
4279
-                    ucwords($messenger->label['singular'])
4280
-                )
4281
-            );
4282
-        } else {
4283
-            $message_type_name = '';
4284
-            EE_Error::add_success(
4285
-                sprintf(
4286
-                    esc_html__('%s messenger has been successfully deactivated.', 'event_espresso'),
4287
-                    ucwords($messenger->label['singular'])
4288
-                )
4289
-            );
4290
-        }
4291
-
4292
-        // if messenger was html or message type was invoice then let's make sure we deactivate invoice payment method.
4293
-        if ($messenger->name === 'html' || $message_type_name === 'invoice') {
4294
-            EE_Registry::instance()->load_lib('Payment_Method_Manager');
4295
-            $count_updated = EE_Payment_Method_Manager::instance()->deactivate_payment_method('invoice');
4296
-            if ($count_updated > 0) {
4297
-                $msg = $message_type_name === 'invoice'
4298
-                    ? esc_html__(
4299
-                        'Deactivating the invoice message type also automatically deactivates the invoice payment method. In order for invoices to be generated the invoice message type must be active. If you completed this action by mistake, simply reactivate the invoice message type and then visit the payment methods admin page to reactivate the invoice payment method.',
4300
-                        'event_espresso'
4301
-                    )
4302
-                    : esc_html__(
4303
-                        'Deactivating the html messenger also automatically deactivates the invoice payment method.  In order for invoices to be generated the html messenger must be be active.  If you completed this action by mistake, simply reactivate the html messenger, then visit the payment methods admin page to reactivate the invoice payment method.',
4304
-                        'event_espresso'
4305
-                    );
4306
-                EE_Error::add_attention($msg);
4307
-            }
4308
-        }
4309
-
4310
-        return true;
4311
-    }
4312
-
4313
-
4314
-    /**
4315
-     * handles updating a message type form on messenger activation IF the message type has settings fields. (via ajax)
4316
-     *
4317
-     * @throws DomainException
4318
-     */
4319
-    public function update_mt_form()
4320
-    {
4321
-        if (! isset($this->_req_data['messenger']) || ! isset($this->_req_data['message_type'])) {
4322
-            EE_Error::add_error(
4323
-                esc_html__('Require message type or messenger to send an updated form', 'event_espresso'),
4324
-                __FILE__,
4325
-                __FUNCTION__,
4326
-                __LINE__
4327
-            );
4328
-            $this->_return_json();
4329
-        }
4330
-
4331
-        $message_types = $this->get_installed_message_types();
4332
-
4333
-        $message_type = $message_types[ $this->_req_data['message_type'] ];
4334
-        $messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']);
4335
-
4336
-        $content = $this->_message_type_settings_content(
4337
-            $message_type,
4338
-            $messenger,
4339
-            true
4340
-        );
4341
-        $this->_template_args['success'] = true;
4342
-        $this->_template_args['content'] = $content;
4343
-        $this->_return_json();
4344
-    }
4345
-
4346
-
4347
-    /**
4348
-     * this handles saving the settings for a messenger or message type
4349
-     *
4350
-     */
4351
-    public function save_settings()
4352
-    {
4353
-        if (! isset($this->_req_data['type'])) {
4354
-            EE_Error::add_error(
4355
-                esc_html__(
4356
-                    'Cannot save settings because type is unknown (messenger settings or messsage type settings?)',
4357
-                    'event_espresso'
4358
-                ),
4359
-                __FILE__,
4360
-                __FUNCTION__,
4361
-                __LINE__
4362
-            );
4363
-            $this->_template_args['error'] = true;
4364
-            $this->_return_json();
4365
-        }
4366
-
4367
-
4368
-        if ($this->_req_data['type'] === 'messenger') {
4369
-            // this should be an array.
4370
-            $settings = $this->_req_data['messenger_settings'];
4371
-            $messenger = $settings['messenger'];
4372
-            // let's setup the settings data
4373
-            foreach ($settings as $key => $value) {
4374
-                switch ($key) {
4375
-                    case 'messenger':
4376
-                        unset($settings['messenger']);
4377
-                        break;
4378
-                    case 'message_types':
4379
-                        unset($settings['message_types']);
4380
-                        break;
4381
-                    default:
4382
-                        $settings[ $key ] = $value;
4383
-                        break;
4384
-                }
4385
-            }
4386
-            $this->_message_resource_manager->add_settings_for_messenger($messenger, $settings);
4387
-        } elseif ($this->_req_data['type'] === 'message_type') {
4388
-            $settings = $this->_req_data['message_type_settings'];
4389
-            $messenger = $settings['messenger'];
4390
-            $message_type = $settings['message_type'];
4391
-
4392
-            foreach ($settings as $key => $value) {
4393
-                switch ($key) {
4394
-                    case 'messenger':
4395
-                        unset($settings['messenger']);
4396
-                        break;
4397
-                    case 'message_type':
4398
-                        unset($settings['message_type']);
4399
-                        break;
4400
-                    default:
4401
-                        $settings[ $key ] = $value;
4402
-                        break;
4403
-                }
4404
-            }
4405
-
4406
-            $this->_message_resource_manager->add_settings_for_message_type($messenger, $message_type, $settings);
4407
-        }
4408
-
4409
-        // okay we should have the data all setup.  Now we just update!
4410
-        $success = $this->_message_resource_manager->update_active_messengers_option();
4411
-
4412
-        if ($success) {
4413
-            EE_Error::add_success(__('Settings updated', 'event_espresso'));
4414
-        } else {
4415
-            EE_Error::add_error(
4416
-                esc_html__(
4417
-                    'Settings did not get updated',
4418
-                    'event_espresso'
4419
-                ),
4420
-                __FILE__,
4421
-                __FUNCTION__,
4422
-                __LINE__
4423
-            );
4424
-        }
4425
-
4426
-        $this->_template_args['success'] = $success;
4427
-        $this->_return_json();
4428
-    }
4429
-
4430
-
4431
-
4432
-
4433
-    /**  EE MESSAGE PROCESSING ACTIONS **/
4434
-
4435
-
4436
-    /**
4437
-     * This immediately generates any EE_Message ID's that are selected that are EEM_Message::status_incomplete
4438
-     * However, this does not send immediately, it just queues for sending.
4439
-     *
4440
-     * @since 4.9.0
4441
-     * @throws EE_Error
4442
-     * @throws InvalidDataTypeException
4443
-     * @throws InvalidInterfaceException
4444
-     * @throws InvalidArgumentException
4445
-     * @throws ReflectionException
4446
-     */
4447
-    protected function _generate_now()
4448
-    {
4449
-        EED_Messages::generate_now($this->_get_msg_ids_from_request());
4450
-        $this->_redirect_after_action(false, '', '', array(), true);
4451
-    }
4452
-
4453
-
4454
-    /**
4455
-     * This immediately generates AND sends any EE_Message's selected that are EEM_Message::status_incomplete or that
4456
-     * are EEM_Message::status_resend or EEM_Message::status_idle
4457
-     *
4458
-     * @since 4.9.0
4459
-     * @throws EE_Error
4460
-     * @throws InvalidDataTypeException
4461
-     * @throws InvalidInterfaceException
4462
-     * @throws InvalidArgumentException
4463
-     * @throws ReflectionException
4464
-     */
4465
-    protected function _generate_and_send_now()
4466
-    {
4467
-        EED_Messages::generate_and_send_now($this->_get_msg_ids_from_request());
4468
-        $this->_redirect_after_action(false, '', '', array(), true);
4469
-    }
4470
-
4471
-
4472
-    /**
4473
-     * This queues any EEM_Message::status_sent EE_Message ids in the request for resending.
4474
-     *
4475
-     * @since 4.9.0
4476
-     * @throws EE_Error
4477
-     * @throws InvalidDataTypeException
4478
-     * @throws InvalidInterfaceException
4479
-     * @throws InvalidArgumentException
4480
-     * @throws ReflectionException
4481
-     */
4482
-    protected function _queue_for_resending()
4483
-    {
4484
-        EED_Messages::queue_for_resending($this->_get_msg_ids_from_request());
4485
-        $this->_redirect_after_action(false, '', '', array(), true);
4486
-    }
4487
-
4488
-
4489
-    /**
4490
-     *  This sends immediately any EEM_Message::status_idle or EEM_Message::status_resend messages in the queue
4491
-     *
4492
-     * @since 4.9.0
4493
-     * @throws EE_Error
4494
-     * @throws InvalidDataTypeException
4495
-     * @throws InvalidInterfaceException
4496
-     * @throws InvalidArgumentException
4497
-     * @throws ReflectionException
4498
-     */
4499
-    protected function _send_now()
4500
-    {
4501
-        EED_Messages::send_now($this->_get_msg_ids_from_request());
4502
-        $this->_redirect_after_action(false, '', '', array(), true);
4503
-    }
4504
-
4505
-
4506
-    /**
4507
-     * Deletes EE_messages for IDs in the request.
4508
-     *
4509
-     * @since 4.9.0
4510
-     * @throws EE_Error
4511
-     * @throws InvalidDataTypeException
4512
-     * @throws InvalidInterfaceException
4513
-     * @throws InvalidArgumentException
4514
-     */
4515
-    protected function _delete_ee_messages()
4516
-    {
4517
-        $msg_ids = $this->_get_msg_ids_from_request();
4518
-        $deleted_count = 0;
4519
-        foreach ($msg_ids as $msg_id) {
4520
-            if (EEM_Message::instance()->delete_by_ID($msg_id)) {
4521
-                $deleted_count++;
4522
-            }
4523
-        }
4524
-        if ($deleted_count) {
4525
-            EE_Error::add_success(
4526
-                esc_html(
4527
-                    _n(
4528
-                        'Message successfully deleted',
4529
-                        'Messages successfully deleted',
4530
-                        $deleted_count,
4531
-                        'event_espresso'
4532
-                    )
4533
-                )
4534
-            );
4535
-            $this->_redirect_after_action(
4536
-                false,
4537
-                '',
4538
-                '',
4539
-                array(),
4540
-                true
4541
-            );
4542
-        } else {
4543
-            EE_Error::add_error(
4544
-                _n('The message was not deleted.', 'The messages were not deleted', count($msg_ids), 'event_espresso'),
4545
-                __FILE__,
4546
-                __FUNCTION__,
4547
-                __LINE__
4548
-            );
4549
-            $this->_redirect_after_action(false, '', '', array(), true);
4550
-        }
4551
-    }
4552
-
4553
-
4554
-    /**
4555
-     *  This looks for 'MSG_ID' key in the request and returns an array of MSG_ID's if present.
4556
-     *
4557
-     * @since 4.9.0
4558
-     * @return array
4559
-     */
4560
-    protected function _get_msg_ids_from_request()
4561
-    {
4562
-        if (! isset($this->_req_data['MSG_ID'])) {
4563
-            return array();
4564
-        }
4565
-
4566
-        return is_array($this->_req_data['MSG_ID'])
4567
-            ? array_keys($this->_req_data['MSG_ID'])
4568
-            : array($this->_req_data['MSG_ID']);
4569
-    }
2649
+		$output = ob_get_contents();
2650
+		ob_clean();
2651
+		$this->_context_switcher = $output;
2652
+	}
2653
+
2654
+
2655
+	/**
2656
+	 * utility for sanitizing new values coming in.
2657
+	 * Note: this is only used when updating a context.
2658
+	 *
2659
+	 * @access protected
2660
+	 *
2661
+	 * @param int $index This helps us know which template field to select from the request array.
2662
+	 *
2663
+	 * @return array
2664
+	 */
2665
+	protected function _set_message_template_column_values($index)
2666
+	{
2667
+		if (is_array($this->_req_data['MTP_template_fields'][ $index ]['content'])) {
2668
+			foreach ($this->_req_data['MTP_template_fields'][ $index ]['content'] as $field => $value) {
2669
+				$this->_req_data['MTP_template_fields'][ $index ]['content'][ $field ] = $value;
2670
+			}
2671
+		}
2672
+
2673
+
2674
+		$set_column_values = array(
2675
+			'MTP_ID'             => absint($this->_req_data['MTP_template_fields'][ $index ]['MTP_ID']),
2676
+			'GRP_ID'             => absint($this->_req_data['GRP_ID']),
2677
+			'MTP_user_id'        => absint($this->_req_data['MTP_user_id']),
2678
+			'MTP_messenger'      => strtolower($this->_req_data['MTP_messenger']),
2679
+			'MTP_message_type'   => strtolower($this->_req_data['MTP_message_type']),
2680
+			'MTP_template_field' => strtolower($this->_req_data['MTP_template_fields'][ $index ]['name']),
2681
+			'MTP_context'        => strtolower($this->_req_data['MTP_context']),
2682
+			'MTP_content'        => $this->_req_data['MTP_template_fields'][ $index ]['content'],
2683
+			'MTP_is_global'      => isset($this->_req_data['MTP_is_global'])
2684
+				? absint($this->_req_data['MTP_is_global'])
2685
+				: 0,
2686
+			'MTP_is_override'    => isset($this->_req_data['MTP_is_override'])
2687
+				? absint($this->_req_data['MTP_is_override'])
2688
+				: 0,
2689
+			'MTP_deleted'        => absint($this->_req_data['MTP_deleted']),
2690
+			'MTP_is_active'      => absint($this->_req_data['MTP_is_active']),
2691
+		);
2692
+
2693
+
2694
+		return $set_column_values;
2695
+	}
2696
+
2697
+
2698
+	protected function _insert_or_update_message_template($new = false)
2699
+	{
2700
+
2701
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
2702
+		$success = 0;
2703
+		$override = false;
2704
+
2705
+		// setup notices description
2706
+		$messenger_slug = ! empty($this->_req_data['MTP_messenger']) ? $this->_req_data['MTP_messenger'] : '';
2707
+
2708
+		// need the message type and messenger objects to be able to use the labels for the notices
2709
+		$messenger_object = $this->_message_resource_manager->get_messenger($messenger_slug);
2710
+		$messenger_label = $messenger_object instanceof EE_messenger
2711
+			? ucwords($messenger_object->label['singular'])
2712
+			: '';
2713
+
2714
+		$message_type_slug = ! empty($this->_req_data['MTP_message_type'])
2715
+			? $this->_req_data['MTP_message_type']
2716
+			: '';
2717
+		$message_type_object = $this->_message_resource_manager->get_message_type($message_type_slug);
2718
+
2719
+		$message_type_label = $message_type_object instanceof EE_message_type
2720
+			? ucwords($message_type_object->label['singular'])
2721
+			: '';
2722
+
2723
+		$context_slug = ! empty($this->_req_data['MTP_context'])
2724
+			? $this->_req_data['MTP_context']
2725
+			: '';
2726
+		$context = ucwords(str_replace('_', ' ', $context_slug));
2727
+
2728
+		$item_desc = $messenger_label && $message_type_label
2729
+			? $messenger_label . ' ' . $message_type_label . ' ' . $context . ' '
2730
+			: '';
2731
+		$item_desc .= 'Message Template';
2732
+		$query_args = array();
2733
+		$edit_array = array();
2734
+		$action_desc = '';
2735
+
2736
+		// if this is "new" then we need to generate the default contexts for the selected messenger/message_type for
2737
+		// user to edit.
2738
+		if ($new) {
2739
+			$GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
2740
+			if ($edit_array = $this->_generate_new_templates($messenger_slug, $message_type_slug, $GRP_ID)) {
2741
+				if (empty($edit_array)) {
2742
+					$success = 0;
2743
+				} else {
2744
+					$success = 1;
2745
+					$edit_array = $edit_array[0];
2746
+					$query_args = array(
2747
+						'id'      => $edit_array['GRP_ID'],
2748
+						'context' => $edit_array['MTP_context'],
2749
+						'action'  => 'edit_message_template',
2750
+					);
2751
+				}
2752
+			}
2753
+			$action_desc = 'created';
2754
+		} else {
2755
+			$MTPG = EEM_Message_Template_Group::instance();
2756
+			$MTP = EEM_Message_Template::instance();
2757
+
2758
+
2759
+			// run update for each template field in displayed context
2760
+			if (! isset($this->_req_data['MTP_template_fields']) && empty($this->_req_data['MTP_template_fields'])) {
2761
+				EE_Error::add_error(
2762
+					esc_html__(
2763
+						'There was a problem saving the template fields from the form because I didn\'t receive any actual template field data.',
2764
+						'event_espresso'
2765
+					),
2766
+					__FILE__,
2767
+					__FUNCTION__,
2768
+					__LINE__
2769
+				);
2770
+				$success = 0;
2771
+			} else {
2772
+				// first validate all fields!
2773
+				// this filter allows client code to add its own validation to the template fields as well.
2774
+				// returning an empty array means everything passed validation.
2775
+				// errors in validation should be represented in an array with the following shape:
2776
+				// array(
2777
+				//   'fieldname' => array(
2778
+				//          'msg' => 'error message'
2779
+				//          'value' => 'value for field producing error'
2780
+				// )
2781
+				$custom_validation = (array) apply_filters(
2782
+					'FHEE__Messages_Admin_Page___insert_or_update_message_template__validates',
2783
+					array(),
2784
+					$this->_req_data['MTP_template_fields'],
2785
+					$context_slug,
2786
+					$messenger_slug,
2787
+					$message_type_slug
2788
+				);
2789
+
2790
+				$system_validation = $MTPG->validate(
2791
+					$this->_req_data['MTP_template_fields'],
2792
+					$context_slug,
2793
+					$messenger_slug,
2794
+					$message_type_slug
2795
+				);
2796
+
2797
+				$system_validation = ! is_array($system_validation) && $system_validation ? array()
2798
+					: $system_validation;
2799
+				$validates = array_merge($custom_validation, $system_validation);
2800
+
2801
+				// if $validate returned error messages (i.e. is_array()) then we need to process them and setup an
2802
+				// appropriate response. HMM, dang this isn't correct, $validates will ALWAYS be an array.
2803
+				//  WE need to make sure there is no actual error messages in validates.
2804
+				if (is_array($validates) && ! empty($validates)) {
2805
+					// add the transient so when the form loads we know which fields to highlight
2806
+					$this->_add_transient('edit_message_template', $validates);
2807
+
2808
+					$success = 0;
2809
+
2810
+					// setup notices
2811
+					foreach ($validates as $field => $error) {
2812
+						if (isset($error['msg'])) {
2813
+							EE_Error::add_error($error['msg'], __FILE__, __FUNCTION__, __LINE__);
2814
+						}
2815
+					}
2816
+				} else {
2817
+					$set_column_values = array();
2818
+					foreach ($this->_req_data['MTP_template_fields'] as $template_field => $content) {
2819
+						$set_column_values = $this->_set_message_template_column_values($template_field);
2820
+
2821
+						$where_cols_n_values = array(
2822
+							'MTP_ID' => $this->_req_data['MTP_template_fields'][ $template_field ]['MTP_ID'],
2823
+						);
2824
+						// if they aren't allowed to use all JS, restrict them to just posty-y tags
2825
+						if (! current_user_can('unfiltered_html')) {
2826
+							if (is_array($set_column_values['MTP_content'])) {
2827
+								foreach ($set_column_values['MTP_content'] as $key => $value) {
2828
+									// remove slashes so wp_kses works properly (its wp_kses_stripslashes() function
2829
+									// only removes slashes from double-quotes, so attributes using single quotes always
2830
+									// appear invalid.) But currently the models expect slashed data, so after wp_kses
2831
+									// runs we need to re-slash the data. Sheesh. See
2832
+									// https://events.codebasehq.com/projects/event-espresso/tickets/11211#update-47321587
2833
+									$set_column_values['MTP_content'][ $key ] = addslashes(
2834
+										wp_kses(
2835
+											stripslashes($value),
2836
+											wp_kses_allowed_html('post')
2837
+										)
2838
+									);
2839
+								}
2840
+							} else {
2841
+								$set_column_values['MTP_content'] = wp_kses(
2842
+									$set_column_values['MTP_content'],
2843
+									wp_kses_allowed_html('post')
2844
+								);
2845
+							}
2846
+						}
2847
+						$message_template_fields = array(
2848
+							'GRP_ID'             => $set_column_values['GRP_ID'],
2849
+							'MTP_template_field' => $set_column_values['MTP_template_field'],
2850
+							'MTP_context'        => $set_column_values['MTP_context'],
2851
+							'MTP_content'        => $set_column_values['MTP_content'],
2852
+						);
2853
+						if ($updated = $MTP->update($message_template_fields, array($where_cols_n_values))) {
2854
+							if ($updated === false) {
2855
+								EE_Error::add_error(
2856
+									sprintf(
2857
+										esc_html__('%s field was NOT updated for some reason', 'event_espresso'),
2858
+										$template_field
2859
+									),
2860
+									__FILE__,
2861
+									__FUNCTION__,
2862
+									__LINE__
2863
+								);
2864
+							} else {
2865
+								$success = 1;
2866
+							}
2867
+						} else {
2868
+							// only do this logic if we don't have a MTP_ID for this field
2869
+							if (empty($this->_req_data['MTP_template_fields'][ $template_field ]['MTP_ID'])) {
2870
+								// this has already been through the template field validator and sanitized, so it will be
2871
+								// safe to insert this field.  Why insert?  This typically happens when we introduce a new
2872
+								// message template field in a messenger/message type and existing users don't have the
2873
+								// default setup for it.
2874
+								// @link https://events.codebasehq.com/projects/event-espresso/tickets/9465
2875
+								$updated = $MTP->insert($message_template_fields);
2876
+								if (! $updated || is_wp_error($updated)) {
2877
+									EE_Error::add_error(
2878
+										sprintf(
2879
+											esc_html__('%s field could not be updated.', 'event_espresso'),
2880
+											$template_field
2881
+										),
2882
+										__FILE__,
2883
+										__FUNCTION__,
2884
+										__LINE__
2885
+									);
2886
+									$success = 0;
2887
+								} else {
2888
+									$success = 1;
2889
+								}
2890
+							}
2891
+						}
2892
+						$action_desc = 'updated';
2893
+					}
2894
+
2895
+					// we can use the last set_column_values for the MTPG update (because its the same for all of these specific MTPs)
2896
+					$mtpg_fields = array(
2897
+						'MTP_user_id'      => $set_column_values['MTP_user_id'],
2898
+						'MTP_messenger'    => $set_column_values['MTP_messenger'],
2899
+						'MTP_message_type' => $set_column_values['MTP_message_type'],
2900
+						'MTP_is_global'    => $set_column_values['MTP_is_global'],
2901
+						'MTP_is_override'  => $set_column_values['MTP_is_override'],
2902
+						'MTP_deleted'      => $set_column_values['MTP_deleted'],
2903
+						'MTP_is_active'    => $set_column_values['MTP_is_active'],
2904
+						'MTP_name'         => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_name'])
2905
+							? $this->_req_data['ee_msg_non_global_fields']['MTP_name']
2906
+							: '',
2907
+						'MTP_description'  => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_description'])
2908
+							? $this->_req_data['ee_msg_non_global_fields']['MTP_description']
2909
+							: '',
2910
+					);
2911
+
2912
+					$mtpg_where = array('GRP_ID' => $set_column_values['GRP_ID']);
2913
+					$updated = $MTPG->update($mtpg_fields, array($mtpg_where));
2914
+
2915
+					if ($updated === false) {
2916
+						EE_Error::add_error(
2917
+							sprintf(
2918
+								esc_html__(
2919
+									'The Message Template Group (%d) was NOT updated for some reason',
2920
+									'event_espresso'
2921
+								),
2922
+								$set_column_values['GRP_ID']
2923
+							),
2924
+							__FILE__,
2925
+							__FUNCTION__,
2926
+							__LINE__
2927
+						);
2928
+					} else {
2929
+						// k now we need to ensure the template_pack and template_variation fields are set.
2930
+						$template_pack = ! empty($this->_req_data['MTP_template_pack'])
2931
+							? $this->_req_data['MTP_template_pack']
2932
+							: 'default';
2933
+
2934
+						$template_variation = ! empty($this->_req_data['MTP_template_variation'])
2935
+							? $this->_req_data['MTP_template_variation']
2936
+							: 'default';
2937
+
2938
+						$mtpg_obj = $MTPG->get_one_by_ID($set_column_values['GRP_ID']);
2939
+						if ($mtpg_obj instanceof EE_Message_Template_Group) {
2940
+							$mtpg_obj->set_template_pack_name($template_pack);
2941
+							$mtpg_obj->set_template_pack_variation($template_variation);
2942
+						}
2943
+						$success = 1;
2944
+					}
2945
+				}
2946
+			}
2947
+		}
2948
+
2949
+		// we return things differently if doing ajax
2950
+		if (defined('DOING_AJAX') && DOING_AJAX) {
2951
+			$this->_template_args['success'] = $success;
2952
+			$this->_template_args['error'] = ! $success ? true : false;
2953
+			$this->_template_args['content'] = '';
2954
+			$this->_template_args['data'] = array(
2955
+				'grpID'        => $edit_array['GRP_ID'],
2956
+				'templateName' => $edit_array['template_name'],
2957
+			);
2958
+			if ($success) {
2959
+				EE_Error::overwrite_success();
2960
+				EE_Error::add_success(
2961
+					esc_html__(
2962
+						'The new template has been created and automatically selected for this event.  You can edit the new template by clicking the edit button.  Note before this template is assigned to this event, the event must be saved.',
2963
+						'event_espresso'
2964
+					)
2965
+				);
2966
+			}
2967
+
2968
+			$this->_return_json();
2969
+		}
2970
+
2971
+
2972
+		// was a test send triggered?
2973
+		if (isset($this->_req_data['test_button'])) {
2974
+			EE_Error::overwrite_success();
2975
+			$this->_do_test_send($context_slug, $messenger_slug, $message_type_slug);
2976
+			$override = true;
2977
+		}
2978
+
2979
+		if (empty($query_args)) {
2980
+			$query_args = array(
2981
+				'id'      => $this->_req_data['GRP_ID'],
2982
+				'context' => $context_slug,
2983
+				'action'  => 'edit_message_template',
2984
+			);
2985
+		}
2986
+
2987
+		$this->_redirect_after_action($success, $item_desc, $action_desc, $query_args, $override);
2988
+	}
2989
+
2990
+
2991
+	/**
2992
+	 * processes a test send request to do an actual messenger delivery test for the given message template being tested
2993
+	 *
2994
+	 * @param  string $context      what context being tested
2995
+	 * @param  string $messenger    messenger being tested
2996
+	 * @param  string $message_type message type being tested
2997
+	 * @throws EE_Error
2998
+	 * @throws InvalidArgumentException
2999
+	 * @throws InvalidDataTypeException
3000
+	 * @throws InvalidInterfaceException
3001
+	 */
3002
+	protected function _do_test_send($context, $messenger, $message_type)
3003
+	{
3004
+		// set things up for preview
3005
+		$this->_req_data['messenger'] = $messenger;
3006
+		$this->_req_data['message_type'] = $message_type;
3007
+		$this->_req_data['context'] = $context;
3008
+		$this->_req_data['GRP_ID'] = isset($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : '';
3009
+		$active_messenger = $this->_message_resource_manager->get_active_messenger($messenger);
3010
+
3011
+		// let's save any existing fields that might be required by the messenger
3012
+		if (isset($this->_req_data['test_settings_fld'])
3013
+			&& $active_messenger instanceof EE_messenger
3014
+			&& apply_filters(
3015
+				'FHEE__Messages_Admin_Page__do_test_send__set_existing_test_settings',
3016
+				true,
3017
+				$this->_req_data['test_settings_fld'],
3018
+				$active_messenger
3019
+			)
3020
+		) {
3021
+			$active_messenger->set_existing_test_settings($this->_req_data['test_settings_fld']);
3022
+		}
3023
+
3024
+		/**
3025
+		 * Use filter to add additional controls on whether message can send or not
3026
+		 */
3027
+		if (apply_filters(
3028
+			'FHEE__Messages_Admin_Page__do_test_send__can_send',
3029
+			true,
3030
+			$context,
3031
+			$this->_req_data,
3032
+			$messenger,
3033
+			$message_type
3034
+		)) {
3035
+			if ( EEM_Event::instance()->count() > 0 ) {
3036
+				$success = $this->_preview_message(true);
3037
+				if ($success) {
3038
+					EE_Error::add_success(__('Test message sent', 'event_espresso'));
3039
+				} else {
3040
+					EE_Error::add_error(
3041
+						esc_html__('The test message was not sent', 'event_espresso'),
3042
+						__FILE__,
3043
+						__FUNCTION__,
3044
+						__LINE__
3045
+					);
3046
+				}
3047
+			} else {
3048
+				$this->noEventsErrorMessage(true);
3049
+			}
3050
+		}
3051
+	}
3052
+
3053
+
3054
+	/**
3055
+	 * _generate_new_templates
3056
+	 * This will handle the messenger, message_type selection when "adding a new custom template" for an event and will
3057
+	 * automatically create the defaults for the event.  The user would then be redirected to edit the default context
3058
+	 * for the event.
3059
+	 *
3060
+	 *
3061
+	 * @param  string $messenger     the messenger we are generating templates for
3062
+	 * @param array   $message_types array of message types that the templates are generated for.
3063
+	 * @param int     $GRP_ID        If this is a custom template being generated then a GRP_ID needs to be included to
3064
+	 *                               indicate the message_template_group being used as the base.
3065
+	 *
3066
+	 * @param bool    $global
3067
+	 *
3068
+	 * @return array|bool array of data required for the redirect to the correct edit page or bool if
3069
+	 *                               encountering problems.
3070
+	 * @throws EE_Error
3071
+	 */
3072
+	protected function _generate_new_templates($messenger, $message_types, $GRP_ID = 0, $global = false)
3073
+	{
3074
+
3075
+		// if no $message_types are given then that's okay... this may be a messenger that just adds shortcodes, so we
3076
+		// just don't generate any templates.
3077
+		if (empty($message_types)) {
3078
+			return true;
3079
+		}
3080
+
3081
+		return EEH_MSG_Template::generate_new_templates($messenger, $message_types, $GRP_ID, $global);
3082
+	}
3083
+
3084
+
3085
+	/**
3086
+	 * [_trash_or_restore_message_template]
3087
+	 *
3088
+	 * @param  boolean $trash whether to move an item to trash/restore (TRUE) or restore it (FALSE)
3089
+	 * @param boolean  $all   whether this is going to trash/restore all contexts within a template group (TRUE) OR just
3090
+	 *                        an individual context (FALSE).
3091
+	 * @return void
3092
+	 * @throws EE_Error
3093
+	 * @throws InvalidArgumentException
3094
+	 * @throws InvalidDataTypeException
3095
+	 * @throws InvalidInterfaceException
3096
+	 */
3097
+	protected function _trash_or_restore_message_template($trash = true, $all = false)
3098
+	{
3099
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
3100
+		$MTP = EEM_Message_Template_Group::instance();
3101
+
3102
+		$success = 1;
3103
+
3104
+		// incoming GRP_IDs
3105
+		if ($all) {
3106
+			// Checkboxes
3107
+			if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3108
+				// if array has more than one element then success message should be plural.
3109
+				// todo: what about nonce?
3110
+				$success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
3111
+
3112
+				// cycle through checkboxes
3113
+				while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) {
3114
+					$trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID);
3115
+					if (! $trashed_or_restored) {
3116
+						$success = 0;
3117
+					}
3118
+				}
3119
+			} else {
3120
+				// grab single GRP_ID and handle
3121
+				$GRP_ID = isset($this->_req_data['id']) ? absint($this->_req_data['id']) : 0;
3122
+				if (! empty($GRP_ID)) {
3123
+					$trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID);
3124
+					if (! $trashed_or_restored) {
3125
+						$success = 0;
3126
+					}
3127
+				} else {
3128
+					$success = 0;
3129
+				}
3130
+			}
3131
+		}
3132
+
3133
+		$action_desc = $trash
3134
+			? esc_html__('moved to the trash', 'event_espresso')
3135
+			: esc_html__('restored', 'event_espresso');
3136
+
3137
+		$action_desc = ! empty($this->_req_data['template_switch']) ? esc_html__('switched', 'event_espresso') : $action_desc;
3138
+
3139
+		$item_desc = $all ? _n(
3140
+			'Message Template Group',
3141
+			'Message Template Groups',
3142
+			$success,
3143
+			'event_espresso'
3144
+		) : _n('Message Template Context', 'Message Template Contexts', $success, 'event_espresso');
3145
+
3146
+		$item_desc = ! empty($this->_req_data['template_switch']) ? _n(
3147
+			'template',
3148
+			'templates',
3149
+			$success,
3150
+			'event_espresso'
3151
+		) : $item_desc;
3152
+
3153
+		$this->_redirect_after_action($success, $item_desc, $action_desc, array());
3154
+	}
3155
+
3156
+
3157
+	/**
3158
+	 * [_delete_message_template]
3159
+	 * NOTE: this handles not only the deletion of the groups but also all the templates belonging to that group.
3160
+	 *
3161
+	 * @return void
3162
+	 * @throws EE_Error
3163
+	 * @throws InvalidArgumentException
3164
+	 * @throws InvalidDataTypeException
3165
+	 * @throws InvalidInterfaceException
3166
+	 */
3167
+	protected function _delete_message_template()
3168
+	{
3169
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
3170
+
3171
+		// checkboxes
3172
+		if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3173
+			// if array has more than one element then success message should be plural
3174
+			$success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
3175
+
3176
+			// cycle through bulk action checkboxes
3177
+			while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) {
3178
+				$success = $this->_delete_mtp_permanently($GRP_ID);
3179
+			}
3180
+		} else {
3181
+			// grab single grp_id and delete
3182
+			$GRP_ID = absint($this->_req_data['id']);
3183
+			$success = $this->_delete_mtp_permanently($GRP_ID);
3184
+		}
3185
+
3186
+		$this->_redirect_after_action($success, 'Message Templates', 'deleted', array());
3187
+	}
3188
+
3189
+
3190
+	/**
3191
+	 * helper for permanently deleting a mtP group and all related message_templates
3192
+	 *
3193
+	 * @param  int  $GRP_ID        The group being deleted
3194
+	 * @param  bool $include_group whether to delete the Message Template Group as well.
3195
+	 * @return bool boolean to indicate the success of the deletes or not.
3196
+	 * @throws EE_Error
3197
+	 * @throws InvalidArgumentException
3198
+	 * @throws InvalidDataTypeException
3199
+	 * @throws InvalidInterfaceException
3200
+	 */
3201
+	private function _delete_mtp_permanently($GRP_ID, $include_group = true)
3202
+	{
3203
+		$success = 1;
3204
+		$MTPG = EEM_Message_Template_Group::instance();
3205
+		// first let's GET this group
3206
+		$MTG = $MTPG->get_one_by_ID($GRP_ID);
3207
+		// then delete permanently all the related Message Templates
3208
+		$deleted = $MTG->delete_related_permanently('Message_Template');
3209
+
3210
+		if ($deleted === 0) {
3211
+			$success = 0;
3212
+		}
3213
+
3214
+		// now delete permanently this particular group
3215
+
3216
+		if ($include_group && ! $MTG->delete_permanently()) {
3217
+			$success = 0;
3218
+		}
3219
+
3220
+		return $success;
3221
+	}
3222
+
3223
+
3224
+	/**
3225
+	 *    _learn_more_about_message_templates_link
3226
+	 *
3227
+	 * @access protected
3228
+	 * @return string
3229
+	 */
3230
+	protected function _learn_more_about_message_templates_link()
3231
+	{
3232
+		return '<a class="hidden" style="margin:0 20px; cursor:pointer; font-size:12px;" >'
3233
+			   . esc_html__('learn more about how message templates works', 'event_espresso')
3234
+			   . '</a>';
3235
+	}
3236
+
3237
+
3238
+	/**
3239
+	 * Used for setting up messenger/message type activation.  This loads up the initial view.  The rest is handled by
3240
+	 * ajax and other routes.
3241
+	 *
3242
+	 * @return void
3243
+	 * @throws DomainException
3244
+	 */
3245
+	protected function _settings()
3246
+	{
3247
+
3248
+
3249
+		$this->_set_m_mt_settings();
3250
+
3251
+		$selected_messenger = isset($this->_req_data['selected_messenger'])
3252
+			? $this->_req_data['selected_messenger']
3253
+			: 'email';
3254
+
3255
+		// let's setup the messenger tabs
3256
+		$this->_template_args['admin_page_header'] = EEH_Tabbed_Content::tab_text_links(
3257
+			$this->_m_mt_settings['messenger_tabs'],
3258
+			'messenger_links',
3259
+			'|',
3260
+			$selected_messenger
3261
+		);
3262
+		$this->_template_args['before_admin_page_content'] = '<div class="ui-widget ui-helper-clearfix">';
3263
+		$this->_template_args['after_admin_page_content'] = '</div><!-- end .ui-widget -->';
3264
+
3265
+		$this->display_admin_page_with_sidebar();
3266
+	}
3267
+
3268
+
3269
+	/**
3270
+	 * This sets the $_m_mt_settings property for when needed (used on the Messages settings page)
3271
+	 *
3272
+	 * @access protected
3273
+	 * @return void
3274
+	 * @throws DomainException
3275
+	 */
3276
+	protected function _set_m_mt_settings()
3277
+	{
3278
+		// first if this is already set then lets get out no need to regenerate data.
3279
+		if (! empty($this->_m_mt_settings)) {
3280
+			return;
3281
+		}
3282
+
3283
+		// get all installed messengers and message_types
3284
+		/** @type EE_messenger[] $messengers */
3285
+		$messengers = $this->_message_resource_manager->installed_messengers();
3286
+		/** @type EE_message_type[] $message_types */
3287
+		$message_types = $this->_message_resource_manager->installed_message_types();
3288
+
3289
+
3290
+		// assemble the array for the _tab_text_links helper
3291
+
3292
+		foreach ($messengers as $messenger) {
3293
+			$this->_m_mt_settings['messenger_tabs'][ $messenger->name ] = array(
3294
+				'label' => ucwords($messenger->label['singular']),
3295
+				'class' => $this->_message_resource_manager->is_messenger_active($messenger->name)
3296
+					? 'messenger-active'
3297
+					: '',
3298
+				'href'  => $messenger->name,
3299
+				'title' => esc_html__('Modify this Messenger', 'event_espresso'),
3300
+				'slug'  => $messenger->name,
3301
+				'obj'   => $messenger,
3302
+			);
3303
+
3304
+
3305
+			$message_types_for_messenger = $messenger->get_valid_message_types();
3306
+
3307
+			foreach ($message_types as $message_type) {
3308
+				// first we need to verify that this message type is valid with this messenger. Cause if it isn't then
3309
+				// it shouldn't show in either the inactive OR active metabox.
3310
+				if (! in_array($message_type->name, $message_types_for_messenger, true)) {
3311
+					continue;
3312
+				}
3313
+
3314
+				$a_or_i = $this->_message_resource_manager->is_message_type_active_for_messenger(
3315
+					$messenger->name,
3316
+					$message_type->name
3317
+				)
3318
+					? 'active'
3319
+					: 'inactive';
3320
+
3321
+				$this->_m_mt_settings['message_type_tabs'][ $messenger->name ][ $a_or_i ][ $message_type->name ] = array(
3322
+					'label'    => ucwords($message_type->label['singular']),
3323
+					'class'    => 'message-type-' . $a_or_i,
3324
+					'slug_id'  => $message_type->name . '-messagetype-' . $messenger->name,
3325
+					'mt_nonce' => wp_create_nonce($message_type->name . '_nonce'),
3326
+					'href'     => 'espresso_' . $message_type->name . '_message_type_settings',
3327
+					'title'    => $a_or_i === 'active'
3328
+						? esc_html__('Drag this message type to the Inactive window to deactivate', 'event_espresso')
3329
+						: esc_html__('Drag this message type to the messenger to activate', 'event_espresso'),
3330
+					'content'  => $a_or_i === 'active'
3331
+						? $this->_message_type_settings_content($message_type, $messenger, true)
3332
+						: $this->_message_type_settings_content($message_type, $messenger),
3333
+					'slug'     => $message_type->name,
3334
+					'active'   => $a_or_i === 'active',
3335
+					'obj'      => $message_type,
3336
+				);
3337
+			}
3338
+		}
3339
+	}
3340
+
3341
+
3342
+	/**
3343
+	 * This just prepares the content for the message type settings
3344
+	 *
3345
+	 * @param  EE_message_type $message_type The message type object
3346
+	 * @param  EE_messenger    $messenger    The messenger object
3347
+	 * @param  boolean         $active       Whether the message type is active or not
3348
+	 * @return string html output for the content
3349
+	 * @throws DomainException
3350
+	 */
3351
+	protected function _message_type_settings_content($message_type, $messenger, $active = false)
3352
+	{
3353
+		// get message type fields
3354
+		$fields = $message_type->get_admin_settings_fields();
3355
+		$settings_template_args['template_form_fields'] = '';
3356
+
3357
+		if (! empty($fields) && $active) {
3358
+			$existing_settings = $message_type->get_existing_admin_settings($messenger->name);
3359
+			foreach ($fields as $fldname => $fldprops) {
3360
+				$field_id = $messenger->name . '-' . $message_type->name . '-' . $fldname;
3361
+				$template_form_field[ $field_id ] = array(
3362
+					'name'       => 'message_type_settings[' . $fldname . ']',
3363
+					'label'      => $fldprops['label'],
3364
+					'input'      => $fldprops['field_type'],
3365
+					'type'       => $fldprops['value_type'],
3366
+					'required'   => $fldprops['required'],
3367
+					'validation' => $fldprops['validation'],
3368
+					'value'      => isset($existing_settings[ $fldname ])
3369
+						? $existing_settings[ $fldname ]
3370
+						: $fldprops['default'],
3371
+					'options'    => isset($fldprops['options'])
3372
+						? $fldprops['options']
3373
+						: array(),
3374
+					'default'    => isset($existing_settings[ $fldname ])
3375
+						? $existing_settings[ $fldname ]
3376
+						: $fldprops['default'],
3377
+					'css_class'  => 'no-drag',
3378
+					'format'     => $fldprops['format'],
3379
+				);
3380
+			}
3381
+
3382
+
3383
+			$settings_template_args['template_form_fields'] = ! empty($template_form_field)
3384
+				? $this->_generate_admin_form_fields(
3385
+					$template_form_field,
3386
+					'string',
3387
+					'ee_mt_activate_form'
3388
+				)
3389
+				: '';
3390
+		}
3391
+
3392
+		$settings_template_args['description'] = $message_type->description;
3393
+		// we also need some hidden fields
3394
+		$settings_template_args['hidden_fields'] = array(
3395
+			'message_type_settings[messenger]'    => array(
3396
+				'type'  => 'hidden',
3397
+				'value' => $messenger->name,
3398
+			),
3399
+			'message_type_settings[message_type]' => array(
3400
+				'type'  => 'hidden',
3401
+				'value' => $message_type->name,
3402
+			),
3403
+			'type'                                => array(
3404
+				'type'  => 'hidden',
3405
+				'value' => 'message_type',
3406
+			),
3407
+		);
3408
+
3409
+		$settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields(
3410
+			$settings_template_args['hidden_fields'],
3411
+			'array'
3412
+		);
3413
+		$settings_template_args['show_form'] = empty($settings_template_args['template_form_fields'])
3414
+			? ' hidden'
3415
+			: '';
3416
+
3417
+
3418
+		$template = EE_MSG_TEMPLATE_PATH . 'ee_msg_mt_settings_content.template.php';
3419
+		$content = EEH_Template::display_template($template, $settings_template_args, true);
3420
+
3421
+		return $content;
3422
+	}
3423
+
3424
+
3425
+	/**
3426
+	 * Generate all the metaboxes for the message types and register them for the messages settings page.
3427
+	 *
3428
+	 * @access protected
3429
+	 * @return void
3430
+	 * @throws DomainException
3431
+	 */
3432
+	protected function _messages_settings_metaboxes()
3433
+	{
3434
+		$this->_set_m_mt_settings();
3435
+		$m_boxes = $mt_boxes = array();
3436
+		$m_template_args = $mt_template_args = array();
3437
+
3438
+		$selected_messenger = isset($this->_req_data['selected_messenger'])
3439
+			? $this->_req_data['selected_messenger']
3440
+			: 'email';
3441
+
3442
+		if (isset($this->_m_mt_settings['messenger_tabs'])) {
3443
+			foreach ($this->_m_mt_settings['messenger_tabs'] as $messenger => $tab_array) {
3444
+				$hide_on_message = $this->_message_resource_manager->is_messenger_active($messenger) ? '' : 'hidden';
3445
+				$hide_off_message = $this->_message_resource_manager->is_messenger_active($messenger) ? 'hidden' : '';
3446
+				// messenger meta boxes
3447
+				$active = $selected_messenger === $messenger;
3448
+				$active_mt_tabs = isset(
3449
+					$this->_m_mt_settings['message_type_tabs'][ $messenger ]['active']
3450
+				)
3451
+					? $this->_m_mt_settings['message_type_tabs'][ $messenger ]['active']
3452
+					: '';
3453
+				$m_boxes[ $messenger . '_a_box' ] = sprintf(
3454
+					esc_html__('%s Settings', 'event_espresso'),
3455
+					$tab_array['label']
3456
+				);
3457
+				$m_template_args[ $messenger . '_a_box' ] = array(
3458
+					'active_message_types'   => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '',
3459
+					'inactive_message_types' => isset(
3460
+						$this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive']
3461
+					)
3462
+						? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive'])
3463
+						: '',
3464
+					'content'                => $this->_get_messenger_box_content($tab_array['obj']),
3465
+					'hidden'                 => $active ? '' : ' hidden',
3466
+					'hide_on_message'        => $hide_on_message,
3467
+					'messenger'              => $messenger,
3468
+					'active'                 => $active,
3469
+				);
3470
+				// message type meta boxes
3471
+				// (which is really just the inactive container for each messenger
3472
+				// showing inactive message types for that messenger)
3473
+				$mt_boxes[ $messenger . '_i_box' ] = esc_html__('Inactive Message Types', 'event_espresso');
3474
+				$mt_template_args[ $messenger . '_i_box' ] = array(
3475
+					'active_message_types'   => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '',
3476
+					'inactive_message_types' => isset(
3477
+						$this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive']
3478
+					)
3479
+						? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive'])
3480
+						: '',
3481
+					'hidden'                 => $active ? '' : ' hidden',
3482
+					'hide_on_message'        => $hide_on_message,
3483
+					'hide_off_message'       => $hide_off_message,
3484
+					'messenger'              => $messenger,
3485
+					'active'                 => $active,
3486
+				);
3487
+			}
3488
+		}
3489
+
3490
+
3491
+		// register messenger metaboxes
3492
+		$m_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_mt_meta_box.template.php';
3493
+		foreach ($m_boxes as $box => $label) {
3494
+			$callback_args = array('template_path' => $m_template_path, 'template_args' => $m_template_args[ $box ]);
3495
+			$msgr = str_replace('_a_box', '', $box);
3496
+			add_meta_box(
3497
+				'espresso_' . $msgr . '_settings',
3498
+				$label,
3499
+				function ($post, $metabox) {
3500
+					echo EEH_Template::display_template(
3501
+						$metabox["args"]["template_path"],
3502
+						$metabox["args"]["template_args"],
3503
+						true
3504
+					);
3505
+				},
3506
+				$this->_current_screen->id,
3507
+				'normal',
3508
+				'high',
3509
+				$callback_args
3510
+			);
3511
+		}
3512
+
3513
+		// register message type metaboxes
3514
+		$mt_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_meta_box.template.php';
3515
+		foreach ($mt_boxes as $box => $label) {
3516
+			$callback_args = array(
3517
+				'template_path' => $mt_template_path,
3518
+				'template_args' => $mt_template_args[ $box ],
3519
+			);
3520
+			$mt = str_replace('_i_box', '', $box);
3521
+			add_meta_box(
3522
+				'espresso_' . $mt . '_inactive_mts',
3523
+				$label,
3524
+				function ($post, $metabox) {
3525
+					echo EEH_Template::display_template(
3526
+						$metabox["args"]["template_path"],
3527
+						$metabox["args"]["template_args"],
3528
+						true
3529
+					);
3530
+				},
3531
+				$this->_current_screen->id,
3532
+				'side',
3533
+				'high',
3534
+				$callback_args
3535
+			);
3536
+		}
3537
+
3538
+		// register metabox for global messages settings but only when on the main site.  On single site installs this
3539
+		// will always result in the metabox showing, on multisite installs the metabox will only show on the main site.
3540
+		if (is_main_site()) {
3541
+			add_meta_box(
3542
+				'espresso_global_message_settings',
3543
+				esc_html__('Global Message Settings', 'event_espresso'),
3544
+				array($this, 'global_messages_settings_metabox_content'),
3545
+				$this->_current_screen->id,
3546
+				'normal',
3547
+				'low',
3548
+				array()
3549
+			);
3550
+		}
3551
+	}
3552
+
3553
+
3554
+	/**
3555
+	 *  This generates the content for the global messages settings metabox.
3556
+	 *
3557
+	 * @return string
3558
+	 * @throws EE_Error
3559
+	 * @throws InvalidArgumentException
3560
+	 * @throws ReflectionException
3561
+	 * @throws InvalidDataTypeException
3562
+	 * @throws InvalidInterfaceException
3563
+	 */
3564
+	public function global_messages_settings_metabox_content()
3565
+	{
3566
+		$form = $this->_generate_global_settings_form();
3567
+		echo $form->form_open(
3568
+			$this->add_query_args_and_nonce(array('action' => 'update_global_settings'), EE_MSG_ADMIN_URL),
3569
+			'POST'
3570
+		)
3571
+			 . $form->get_html()
3572
+			 . $form->form_close();
3573
+	}
3574
+
3575
+
3576
+	/**
3577
+	 * This generates and returns the form object for the global messages settings.
3578
+	 *
3579
+	 * @return EE_Form_Section_Proper
3580
+	 * @throws EE_Error
3581
+	 * @throws InvalidArgumentException
3582
+	 * @throws ReflectionException
3583
+	 * @throws InvalidDataTypeException
3584
+	 * @throws InvalidInterfaceException
3585
+	 */
3586
+	protected function _generate_global_settings_form()
3587
+	{
3588
+		EE_Registry::instance()->load_helper('HTML');
3589
+		/** @var EE_Network_Core_Config $network_config */
3590
+		$network_config = EE_Registry::instance()->NET_CFG->core;
3591
+
3592
+		return new EE_Form_Section_Proper(
3593
+			array(
3594
+				'name'            => 'global_messages_settings',
3595
+				'html_id'         => 'global_messages_settings',
3596
+				'html_class'      => 'form-table',
3597
+				'layout_strategy' => new EE_Admin_Two_Column_Layout(),
3598
+				'subsections'     => apply_filters(
3599
+					'FHEE__Messages_Admin_Page__global_messages_settings_metabox_content__form_subsections',
3600
+					array(
3601
+						'do_messages_on_same_request' => new EE_Select_Input(
3602
+							array(
3603
+								true  => esc_html__("On the same request", "event_espresso"),
3604
+								false => esc_html__("On a separate request", "event_espresso"),
3605
+							),
3606
+							array(
3607
+								'default'         => $network_config->do_messages_on_same_request,
3608
+								'html_label_text' => esc_html__(
3609
+									'Generate and send all messages:',
3610
+									'event_espresso'
3611
+								),
3612
+								'html_help_text'  => esc_html__(
3613
+									'By default the messages system uses a more efficient means of processing messages on separate requests and utilizes the wp-cron scheduling system.  This makes things execute faster for people registering for your events.  However, if the wp-cron system is disabled on your site and there is no alternative in place, then you can change this so messages are always executed on the same request.',
3614
+									'event_espresso'
3615
+								),
3616
+							)
3617
+						),
3618
+						'delete_threshold'            => new EE_Select_Input(
3619
+							array(
3620
+								0  => esc_html__('Forever', 'event_espresso'),
3621
+								3  => esc_html__('3 Months', 'event_espresso'),
3622
+								6  => esc_html__('6 Months', 'event_espresso'),
3623
+								9  => esc_html__('9 Months', 'event_espresso'),
3624
+								12 => esc_html__('12 Months', 'event_espresso'),
3625
+								24 => esc_html__('24 Months', 'event_espresso'),
3626
+								36 => esc_html__('36 Months', 'event_espresso'),
3627
+							),
3628
+							array(
3629
+								'default'         => EE_Registry::instance()->CFG->messages->delete_threshold,
3630
+								'html_label_text' => esc_html__('Cleanup of old messages:', 'event_espresso'),
3631
+								'html_help_text'  => esc_html__(
3632
+									'You can control how long a record of processed messages is kept via this option.',
3633
+									'event_espresso'
3634
+								),
3635
+							)
3636
+						),
3637
+						'update_settings'             => new EE_Submit_Input(
3638
+							array(
3639
+								'default'         => esc_html__('Update', 'event_espresso'),
3640
+								'html_label_text' => '&nbsp',
3641
+							)
3642
+						),
3643
+					)
3644
+				),
3645
+			)
3646
+		);
3647
+	}
3648
+
3649
+
3650
+	/**
3651
+	 * This handles updating the global settings set on the admin page.
3652
+	 *
3653
+	 * @throws EE_Error
3654
+	 * @throws InvalidDataTypeException
3655
+	 * @throws InvalidInterfaceException
3656
+	 * @throws InvalidArgumentException
3657
+	 * @throws ReflectionException
3658
+	 */
3659
+	protected function _update_global_settings()
3660
+	{
3661
+		/** @var EE_Network_Core_Config $network_config */
3662
+		$network_config = EE_Registry::instance()->NET_CFG->core;
3663
+		$messages_config = EE_Registry::instance()->CFG->messages;
3664
+		$form = $this->_generate_global_settings_form();
3665
+		if ($form->was_submitted()) {
3666
+			$form->receive_form_submission();
3667
+			if ($form->is_valid()) {
3668
+				$valid_data = $form->valid_data();
3669
+				foreach ($valid_data as $property => $value) {
3670
+					$setter = 'set_' . $property;
3671
+					if (method_exists($network_config, $setter)) {
3672
+						$network_config->{$setter}($value);
3673
+					} elseif (property_exists($network_config, $property)
3674
+						&& $network_config->{$property} !== $value
3675
+					) {
3676
+						$network_config->{$property} = $value;
3677
+					} elseif (property_exists($messages_config, $property)
3678
+						&& $messages_config->{$property} !== $value
3679
+					) {
3680
+						$messages_config->{$property} = $value;
3681
+					}
3682
+				}
3683
+				// only update if the form submission was valid!
3684
+				EE_Registry::instance()->NET_CFG->update_config(true, false);
3685
+				EE_Registry::instance()->CFG->update_espresso_config();
3686
+				EE_Error::overwrite_success();
3687
+				EE_Error::add_success(__('Global message settings were updated', 'event_espresso'));
3688
+			}
3689
+		}
3690
+		$this->_redirect_after_action(0, '', '', array('action' => 'settings'), true);
3691
+	}
3692
+
3693
+
3694
+	/**
3695
+	 * this prepares the messenger tabs that can be dragged in and out of messenger boxes to activate/deactivate
3696
+	 *
3697
+	 * @param  array $tab_array This is an array of message type tab details used to generate the tabs
3698
+	 * @return string html formatted tabs
3699
+	 * @throws DomainException
3700
+	 */
3701
+	protected function _get_mt_tabs($tab_array)
3702
+	{
3703
+		$tab_array = (array) $tab_array;
3704
+		$template = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_mt_settings_tab_item.template.php';
3705
+		$tabs = '';
3706
+
3707
+		foreach ($tab_array as $tab) {
3708
+			$tabs .= EEH_Template::display_template($template, $tab, true);
3709
+		}
3710
+
3711
+		return $tabs;
3712
+	}
3713
+
3714
+
3715
+	/**
3716
+	 * This prepares the content of the messenger meta box admin settings
3717
+	 *
3718
+	 * @param  EE_messenger $messenger The messenger we're setting up content for
3719
+	 * @return string html formatted content
3720
+	 * @throws DomainException
3721
+	 */
3722
+	protected function _get_messenger_box_content(EE_messenger $messenger)
3723
+	{
3724
+
3725
+		$fields = $messenger->get_admin_settings_fields();
3726
+		$settings_template_args['template_form_fields'] = '';
3727
+
3728
+		// is $messenger active?
3729
+		$settings_template_args['active'] = $this->_message_resource_manager->is_messenger_active($messenger->name);
3730
+
3731
+
3732
+		if (! empty($fields)) {
3733
+			$existing_settings = $messenger->get_existing_admin_settings();
3734
+
3735
+			foreach ($fields as $fldname => $fldprops) {
3736
+				$field_id = $messenger->name . '-' . $fldname;
3737
+				$template_form_field[ $field_id ] = array(
3738
+					'name'       => 'messenger_settings[' . $field_id . ']',
3739
+					'label'      => $fldprops['label'],
3740
+					'input'      => $fldprops['field_type'],
3741
+					'type'       => $fldprops['value_type'],
3742
+					'required'   => $fldprops['required'],
3743
+					'validation' => $fldprops['validation'],
3744
+					'value'      => isset($existing_settings[ $field_id ])
3745
+						? $existing_settings[ $field_id ]
3746
+						: $fldprops['default'],
3747
+					'css_class'  => '',
3748
+					'format'     => $fldprops['format'],
3749
+				);
3750
+			}
3751
+
3752
+
3753
+			$settings_template_args['template_form_fields'] = ! empty($template_form_field)
3754
+				? $this->_generate_admin_form_fields($template_form_field, 'string', 'ee_m_activate_form')
3755
+				: '';
3756
+		}
3757
+
3758
+		// we also need some hidden fields
3759
+		$settings_template_args['hidden_fields'] = array(
3760
+			'messenger_settings[messenger]' => array(
3761
+				'type'  => 'hidden',
3762
+				'value' => $messenger->name,
3763
+			),
3764
+			'type'                          => array(
3765
+				'type'  => 'hidden',
3766
+				'value' => 'messenger',
3767
+			),
3768
+		);
3769
+
3770
+		// make sure any active message types that are existing are included in the hidden fields
3771
+		if (isset($this->_m_mt_settings['message_type_tabs'][ $messenger->name ]['active'])) {
3772
+			foreach ($this->_m_mt_settings['message_type_tabs'][ $messenger->name ]['active'] as $mt => $values) {
3773
+				$settings_template_args['hidden_fields'][ 'messenger_settings[message_types][' . $mt . ']' ] = array(
3774
+					'type'  => 'hidden',
3775
+					'value' => $mt,
3776
+				);
3777
+			}
3778
+		}
3779
+		$settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields(
3780
+			$settings_template_args['hidden_fields'],
3781
+			'array'
3782
+		);
3783
+		$active = $this->_message_resource_manager->is_messenger_active($messenger->name);
3784
+
3785
+		$settings_template_args['messenger'] = $messenger->name;
3786
+		$settings_template_args['description'] = $messenger->description;
3787
+		$settings_template_args['show_hide_edit_form'] = $active ? '' : ' hidden';
3788
+
3789
+
3790
+		$settings_template_args['show_hide_edit_form'] = $this->_message_resource_manager->is_messenger_active(
3791
+			$messenger->name
3792
+		)
3793
+			? $settings_template_args['show_hide_edit_form']
3794
+			: ' hidden';
3795
+
3796
+		$settings_template_args['show_hide_edit_form'] = empty($settings_template_args['template_form_fields'])
3797
+			? ' hidden'
3798
+			: $settings_template_args['show_hide_edit_form'];
3799
+
3800
+
3801
+		$settings_template_args['on_off_action'] = $active ? 'messenger-off' : 'messenger-on';
3802
+		$settings_template_args['nonce'] = wp_create_nonce('activate_' . $messenger->name . '_toggle_nonce');
3803
+		$settings_template_args['on_off_status'] = $active ? true : false;
3804
+		$template = EE_MSG_TEMPLATE_PATH . 'ee_msg_m_settings_content.template.php';
3805
+		$content = EEH_Template::display_template(
3806
+			$template,
3807
+			$settings_template_args,
3808
+			true
3809
+		);
3810
+
3811
+		return $content;
3812
+	}
3813
+
3814
+
3815
+	/**
3816
+	 * used by ajax on the messages settings page to activate|deactivate the messenger
3817
+	 *
3818
+	 * @throws DomainException
3819
+	 * @throws EE_Error
3820
+	 * @throws InvalidDataTypeException
3821
+	 * @throws InvalidInterfaceException
3822
+	 * @throws InvalidArgumentException
3823
+	 * @throws ReflectionException
3824
+	 */
3825
+	public function activate_messenger_toggle()
3826
+	{
3827
+		$success = true;
3828
+		$this->_prep_default_response_for_messenger_or_message_type_toggle();
3829
+		// let's check that we have required data
3830
+		if (! isset($this->_req_data['messenger'])) {
3831
+			EE_Error::add_error(
3832
+				esc_html__('Messenger name needed to toggle activation. None given', 'event_espresso'),
3833
+				__FILE__,
3834
+				__FUNCTION__,
3835
+				__LINE__
3836
+			);
3837
+			$success = false;
3838
+		}
3839
+
3840
+		// do a nonce check here since we're not arriving via a normal route
3841
+		$nonce = isset($this->_req_data['activate_nonce'])
3842
+			? sanitize_text_field($this->_req_data['activate_nonce'])
3843
+			: '';
3844
+		$nonce_ref = 'activate_' . $this->_req_data['messenger'] . '_toggle_nonce';
3845
+
3846
+		$this->_verify_nonce($nonce, $nonce_ref);
3847
+
3848
+
3849
+		if (! isset($this->_req_data['status'])) {
3850
+			EE_Error::add_error(
3851
+				esc_html__(
3852
+					'Messenger status needed to know whether activation or deactivation is happening. No status is given',
3853
+					'event_espresso'
3854
+				),
3855
+				__FILE__,
3856
+				__FUNCTION__,
3857
+				__LINE__
3858
+			);
3859
+			$success = false;
3860
+		}
3861
+
3862
+		// do check to verify we have a valid status.
3863
+		$status = $this->_req_data['status'];
3864
+
3865
+		if ($status !== 'off' && $status !== 'on') {
3866
+			EE_Error::add_error(
3867
+				sprintf(
3868
+					esc_html__('The given status (%s) is not valid. Must be "off" or "on"', 'event_espresso'),
3869
+					$this->_req_data['status']
3870
+				),
3871
+				__FILE__,
3872
+				__FUNCTION__,
3873
+				__LINE__
3874
+			);
3875
+			$success = false;
3876
+		}
3877
+
3878
+		if ($success) {
3879
+			// made it here?  Stop dawdling then!!
3880
+			$success = $status === 'off'
3881
+				? $this->_deactivate_messenger($this->_req_data['messenger'])
3882
+				: $this->_activate_messenger($this->_req_data['messenger']);
3883
+		}
3884
+
3885
+		$this->_template_args['success'] = $success;
3886
+
3887
+		// no special instructions so let's just do the json return (which should automatically do all the special stuff).
3888
+		$this->_return_json();
3889
+	}
3890
+
3891
+
3892
+	/**
3893
+	 * used by ajax from the messages settings page to activate|deactivate a message type
3894
+	 *
3895
+	 * @throws DomainException
3896
+	 * @throws EE_Error
3897
+	 * @throws ReflectionException
3898
+	 * @throws InvalidDataTypeException
3899
+	 * @throws InvalidInterfaceException
3900
+	 * @throws InvalidArgumentException
3901
+	 */
3902
+	public function activate_mt_toggle()
3903
+	{
3904
+		$success = true;
3905
+		$this->_prep_default_response_for_messenger_or_message_type_toggle();
3906
+
3907
+		// let's make sure we have the necessary data
3908
+		if (! isset($this->_req_data['message_type'])) {
3909
+			EE_Error::add_error(
3910
+				esc_html__('Message Type name needed to toggle activation. None given', 'event_espresso'),
3911
+				__FILE__,
3912
+				__FUNCTION__,
3913
+				__LINE__
3914
+			);
3915
+			$success = false;
3916
+		}
3917
+
3918
+		if (! isset($this->_req_data['messenger'])) {
3919
+			EE_Error::add_error(
3920
+				esc_html__('Messenger name needed to toggle activation. None given', 'event_espresso'),
3921
+				__FILE__,
3922
+				__FUNCTION__,
3923
+				__LINE__
3924
+			);
3925
+			$success = false;
3926
+		}
3927
+
3928
+		if (! isset($this->_req_data['status'])) {
3929
+			EE_Error::add_error(
3930
+				esc_html__(
3931
+					'Messenger status needed to know whether activation or deactivation is happening. No status is given',
3932
+					'event_espresso'
3933
+				),
3934
+				__FILE__,
3935
+				__FUNCTION__,
3936
+				__LINE__
3937
+			);
3938
+			$success = false;
3939
+		}
3940
+
3941
+
3942
+		// do check to verify we have a valid status.
3943
+		$status = $this->_req_data['status'];
3944
+
3945
+		if ($status !== 'activate' && $status !== 'deactivate') {
3946
+			EE_Error::add_error(
3947
+				sprintf(
3948
+					esc_html__('The given status (%s) is not valid. Must be "active" or "inactive"', 'event_espresso'),
3949
+					$this->_req_data['status']
3950
+				),
3951
+				__FILE__,
3952
+				__FUNCTION__,
3953
+				__LINE__
3954
+			);
3955
+			$success = false;
3956
+		}
3957
+
3958
+
3959
+		// do a nonce check here since we're not arriving via a normal route
3960
+		$nonce = isset($this->_req_data['mt_nonce']) ? sanitize_text_field($this->_req_data['mt_nonce']) : '';
3961
+		$nonce_ref = $this->_req_data['message_type'] . '_nonce';
3962
+
3963
+		$this->_verify_nonce($nonce, $nonce_ref);
3964
+
3965
+		if ($success) {
3966
+			// made it here? um, what are you waiting for then?
3967
+			$success = $status === 'deactivate'
3968
+				? $this->_deactivate_message_type_for_messenger(
3969
+					$this->_req_data['messenger'],
3970
+					$this->_req_data['message_type']
3971
+				)
3972
+				: $this->_activate_message_type_for_messenger(
3973
+					$this->_req_data['messenger'],
3974
+					$this->_req_data['message_type']
3975
+				);
3976
+		}
3977
+
3978
+		$this->_template_args['success'] = $success;
3979
+		$this->_return_json();
3980
+	}
3981
+
3982
+
3983
+	/**
3984
+	 * Takes care of processing activating a messenger and preparing the appropriate response.
3985
+	 *
3986
+	 * @param string $messenger_name The name of the messenger being activated
3987
+	 * @return bool
3988
+	 * @throws DomainException
3989
+	 * @throws EE_Error
3990
+	 * @throws InvalidArgumentException
3991
+	 * @throws ReflectionException
3992
+	 * @throws InvalidDataTypeException
3993
+	 * @throws InvalidInterfaceException
3994
+	 */
3995
+	protected function _activate_messenger($messenger_name)
3996
+	{
3997
+		/** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
3998
+		$active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
3999
+		$message_types_to_activate = $active_messenger instanceof EE_Messenger
4000
+			? $active_messenger->get_default_message_types()
4001
+			: array();
4002
+
4003
+		// ensure is active
4004
+		$this->_message_resource_manager->activate_messenger($messenger_name, $message_types_to_activate);
4005
+
4006
+		// set response_data for reload
4007
+		foreach ($message_types_to_activate as $message_type_name) {
4008
+			/** @var EE_message_type $message_type */
4009
+			$message_type = $this->_message_resource_manager->get_message_type($message_type_name);
4010
+			if ($this->_message_resource_manager->is_message_type_active_for_messenger(
4011
+				$messenger_name,
4012
+				$message_type_name
4013
+			)
4014
+				&& $message_type instanceof EE_message_type
4015
+			) {
4016
+				$this->_template_args['data']['active_mts'][] = $message_type_name;
4017
+				if ($message_type->get_admin_settings_fields()) {
4018
+					$this->_template_args['data']['mt_reload'][] = $message_type_name;
4019
+				}
4020
+			}
4021
+		}
4022
+
4023
+		// add success message for activating messenger
4024
+		return $this->_setup_response_message_for_activating_messenger_with_message_types($active_messenger);
4025
+	}
4026
+
4027
+
4028
+	/**
4029
+	 * Takes care of processing deactivating a messenger and preparing the appropriate response.
4030
+	 *
4031
+	 * @param string $messenger_name The name of the messenger being activated
4032
+	 * @return bool
4033
+	 * @throws DomainException
4034
+	 * @throws EE_Error
4035
+	 * @throws InvalidArgumentException
4036
+	 * @throws ReflectionException
4037
+	 * @throws InvalidDataTypeException
4038
+	 * @throws InvalidInterfaceException
4039
+	 */
4040
+	protected function _deactivate_messenger($messenger_name)
4041
+	{
4042
+		/** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4043
+		$active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4044
+		$this->_message_resource_manager->deactivate_messenger($messenger_name);
4045
+
4046
+		return $this->_setup_response_message_for_deactivating_messenger_with_message_types($active_messenger);
4047
+	}
4048
+
4049
+
4050
+	/**
4051
+	 * Takes care of processing activating a message type for a messenger and preparing the appropriate response.
4052
+	 *
4053
+	 * @param string $messenger_name    The name of the messenger the message type is being activated for.
4054
+	 * @param string $message_type_name The name of the message type being activated for the messenger
4055
+	 * @return bool
4056
+	 * @throws DomainException
4057
+	 * @throws EE_Error
4058
+	 * @throws InvalidArgumentException
4059
+	 * @throws ReflectionException
4060
+	 * @throws InvalidDataTypeException
4061
+	 * @throws InvalidInterfaceException
4062
+	 */
4063
+	protected function _activate_message_type_for_messenger($messenger_name, $message_type_name)
4064
+	{
4065
+		/** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4066
+		$active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4067
+		/** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */
4068
+		$message_type_to_activate = $this->_message_resource_manager->get_message_type($message_type_name);
4069
+
4070
+		// ensure is active
4071
+		$this->_message_resource_manager->activate_messenger($messenger_name, $message_type_name);
4072
+
4073
+		// set response for load
4074
+		if ($this->_message_resource_manager->is_message_type_active_for_messenger(
4075
+			$messenger_name,
4076
+			$message_type_name
4077
+		)
4078
+		) {
4079
+			$this->_template_args['data']['active_mts'][] = $message_type_name;
4080
+			if ($message_type_to_activate->get_admin_settings_fields()) {
4081
+				$this->_template_args['data']['mt_reload'][] = $message_type_name;
4082
+			}
4083
+		}
4084
+
4085
+		return $this->_setup_response_message_for_activating_messenger_with_message_types(
4086
+			$active_messenger,
4087
+			$message_type_to_activate
4088
+		);
4089
+	}
4090
+
4091
+
4092
+	/**
4093
+	 * Takes care of processing deactivating a message type for a messenger and preparing the appropriate response.
4094
+	 *
4095
+	 * @param string $messenger_name    The name of the messenger the message type is being deactivated for.
4096
+	 * @param string $message_type_name The name of the message type being deactivated for the messenger
4097
+	 * @return bool
4098
+	 * @throws DomainException
4099
+	 * @throws EE_Error
4100
+	 * @throws InvalidArgumentException
4101
+	 * @throws ReflectionException
4102
+	 * @throws InvalidDataTypeException
4103
+	 * @throws InvalidInterfaceException
4104
+	 */
4105
+	protected function _deactivate_message_type_for_messenger($messenger_name, $message_type_name)
4106
+	{
4107
+		/** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4108
+		$active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4109
+		/** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */
4110
+		$message_type_to_deactivate = $this->_message_resource_manager->get_message_type($message_type_name);
4111
+		$this->_message_resource_manager->deactivate_message_type_for_messenger($message_type_name, $messenger_name);
4112
+
4113
+		return $this->_setup_response_message_for_deactivating_messenger_with_message_types(
4114
+			$active_messenger,
4115
+			$message_type_to_deactivate
4116
+		);
4117
+	}
4118
+
4119
+
4120
+	/**
4121
+	 * This just initializes the defaults for activating messenger and message type responses.
4122
+	 */
4123
+	protected function _prep_default_response_for_messenger_or_message_type_toggle()
4124
+	{
4125
+		$this->_template_args['data']['active_mts'] = array();
4126
+		$this->_template_args['data']['mt_reload'] = array();
4127
+	}
4128
+
4129
+
4130
+	/**
4131
+	 * Setup appropriate response for activating a messenger and/or message types
4132
+	 *
4133
+	 * @param EE_messenger         $messenger
4134
+	 * @param EE_message_type|null $message_type
4135
+	 * @return bool
4136
+	 * @throws DomainException
4137
+	 * @throws EE_Error
4138
+	 * @throws InvalidArgumentException
4139
+	 * @throws ReflectionException
4140
+	 * @throws InvalidDataTypeException
4141
+	 * @throws InvalidInterfaceException
4142
+	 */
4143
+	protected function _setup_response_message_for_activating_messenger_with_message_types(
4144
+		$messenger,
4145
+		EE_Message_Type $message_type = null
4146
+	) {
4147
+		// if $messenger isn't a valid messenger object then get out.
4148
+		if (! $messenger instanceof EE_Messenger) {
4149
+			EE_Error::add_error(
4150
+				esc_html__('The messenger being activated is not a valid messenger', 'event_espresso'),
4151
+				__FILE__,
4152
+				__FUNCTION__,
4153
+				__LINE__
4154
+			);
4155
+
4156
+			return false;
4157
+		}
4158
+		// activated
4159
+		if ($this->_template_args['data']['active_mts']) {
4160
+			EE_Error::overwrite_success();
4161
+			// activated a message type with the messenger
4162
+			if ($message_type instanceof EE_message_type) {
4163
+				EE_Error::add_success(
4164
+					sprintf(
4165
+						esc_html__(
4166
+							'%s message type has been successfully activated with the %s messenger',
4167
+							'event_espresso'
4168
+						),
4169
+						ucwords($message_type->label['singular']),
4170
+						ucwords($messenger->label['singular'])
4171
+					)
4172
+				);
4173
+
4174
+				// if message type was invoice then let's make sure we activate the invoice payment method.
4175
+				if ($message_type->name === 'invoice') {
4176
+					EE_Registry::instance()->load_lib('Payment_Method_Manager');
4177
+					$pm = EE_Payment_Method_Manager::instance()->activate_a_payment_method_of_type('Invoice');
4178
+					if ($pm instanceof EE_Payment_Method) {
4179
+						EE_Error::add_attention(
4180
+							esc_html__(
4181
+								'Activating the invoice message type also automatically activates the invoice payment method.  If you do not wish the invoice payment method to be active, or to change its settings, visit the payment method admin page.',
4182
+								'event_espresso'
4183
+							)
4184
+						);
4185
+					}
4186
+				}
4187
+				// just toggles the entire messenger
4188
+			} else {
4189
+				EE_Error::add_success(
4190
+					sprintf(
4191
+						esc_html__('%s messenger has been successfully activated', 'event_espresso'),
4192
+						ucwords($messenger->label['singular'])
4193
+					)
4194
+				);
4195
+			}
4196
+
4197
+			return true;
4198
+
4199
+			// possible error condition. This will happen when our active_mts data is empty because it is validated for actual active
4200
+			// message types after the activation process.  However its possible some messengers don't HAVE any default_message_types
4201
+			// in which case we just give a success message for the messenger being successfully activated.
4202
+		} else {
4203
+			if (! $messenger->get_default_message_types()) {
4204
+				// messenger doesn't have any default message types so still a success.
4205
+				EE_Error::add_success(
4206
+					sprintf(
4207
+						esc_html__('%s messenger was successfully activated.', 'event_espresso'),
4208
+						ucwords($messenger->label['singular'])
4209
+					)
4210
+				);
4211
+
4212
+				return true;
4213
+			} else {
4214
+				EE_Error::add_error(
4215
+					$message_type instanceof EE_message_type
4216
+						? sprintf(
4217
+							esc_html__(
4218
+								'%s message type was not successfully activated with the %s messenger',
4219
+								'event_espresso'
4220
+							),
4221
+							ucwords($message_type->label['singular']),
4222
+							ucwords($messenger->label['singular'])
4223
+						)
4224
+						: sprintf(
4225
+							esc_html__('%s messenger was not successfully activated', 'event_espresso'),
4226
+							ucwords($messenger->label['singular'])
4227
+						),
4228
+					__FILE__,
4229
+					__FUNCTION__,
4230
+					__LINE__
4231
+				);
4232
+
4233
+				return false;
4234
+			}
4235
+		}
4236
+	}
4237
+
4238
+
4239
+	/**
4240
+	 * This sets up the appropriate response for deactivating a messenger and/or message type.
4241
+	 *
4242
+	 * @param EE_messenger         $messenger
4243
+	 * @param EE_message_type|null $message_type
4244
+	 * @return bool
4245
+	 * @throws DomainException
4246
+	 * @throws EE_Error
4247
+	 * @throws InvalidArgumentException
4248
+	 * @throws ReflectionException
4249
+	 * @throws InvalidDataTypeException
4250
+	 * @throws InvalidInterfaceException
4251
+	 */
4252
+	protected function _setup_response_message_for_deactivating_messenger_with_message_types(
4253
+		$messenger,
4254
+		EE_message_type $message_type = null
4255
+	) {
4256
+		EE_Error::overwrite_success();
4257
+
4258
+		// if $messenger isn't a valid messenger object then get out.
4259
+		if (! $messenger instanceof EE_Messenger) {
4260
+			EE_Error::add_error(
4261
+				esc_html__('The messenger being deactivated is not a valid messenger', 'event_espresso'),
4262
+				__FILE__,
4263
+				__FUNCTION__,
4264
+				__LINE__
4265
+			);
4266
+
4267
+			return false;
4268
+		}
4269
+
4270
+		if ($message_type instanceof EE_message_type) {
4271
+			$message_type_name = $message_type->name;
4272
+			EE_Error::add_success(
4273
+				sprintf(
4274
+					esc_html__(
4275
+						'%s message type has been successfully deactivated for the %s messenger.',
4276
+						'event_espresso'
4277
+					),
4278
+					ucwords($message_type->label['singular']),
4279
+					ucwords($messenger->label['singular'])
4280
+				)
4281
+			);
4282
+		} else {
4283
+			$message_type_name = '';
4284
+			EE_Error::add_success(
4285
+				sprintf(
4286
+					esc_html__('%s messenger has been successfully deactivated.', 'event_espresso'),
4287
+					ucwords($messenger->label['singular'])
4288
+				)
4289
+			);
4290
+		}
4291
+
4292
+		// if messenger was html or message type was invoice then let's make sure we deactivate invoice payment method.
4293
+		if ($messenger->name === 'html' || $message_type_name === 'invoice') {
4294
+			EE_Registry::instance()->load_lib('Payment_Method_Manager');
4295
+			$count_updated = EE_Payment_Method_Manager::instance()->deactivate_payment_method('invoice');
4296
+			if ($count_updated > 0) {
4297
+				$msg = $message_type_name === 'invoice'
4298
+					? esc_html__(
4299
+						'Deactivating the invoice message type also automatically deactivates the invoice payment method. In order for invoices to be generated the invoice message type must be active. If you completed this action by mistake, simply reactivate the invoice message type and then visit the payment methods admin page to reactivate the invoice payment method.',
4300
+						'event_espresso'
4301
+					)
4302
+					: esc_html__(
4303
+						'Deactivating the html messenger also automatically deactivates the invoice payment method.  In order for invoices to be generated the html messenger must be be active.  If you completed this action by mistake, simply reactivate the html messenger, then visit the payment methods admin page to reactivate the invoice payment method.',
4304
+						'event_espresso'
4305
+					);
4306
+				EE_Error::add_attention($msg);
4307
+			}
4308
+		}
4309
+
4310
+		return true;
4311
+	}
4312
+
4313
+
4314
+	/**
4315
+	 * handles updating a message type form on messenger activation IF the message type has settings fields. (via ajax)
4316
+	 *
4317
+	 * @throws DomainException
4318
+	 */
4319
+	public function update_mt_form()
4320
+	{
4321
+		if (! isset($this->_req_data['messenger']) || ! isset($this->_req_data['message_type'])) {
4322
+			EE_Error::add_error(
4323
+				esc_html__('Require message type or messenger to send an updated form', 'event_espresso'),
4324
+				__FILE__,
4325
+				__FUNCTION__,
4326
+				__LINE__
4327
+			);
4328
+			$this->_return_json();
4329
+		}
4330
+
4331
+		$message_types = $this->get_installed_message_types();
4332
+
4333
+		$message_type = $message_types[ $this->_req_data['message_type'] ];
4334
+		$messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']);
4335
+
4336
+		$content = $this->_message_type_settings_content(
4337
+			$message_type,
4338
+			$messenger,
4339
+			true
4340
+		);
4341
+		$this->_template_args['success'] = true;
4342
+		$this->_template_args['content'] = $content;
4343
+		$this->_return_json();
4344
+	}
4345
+
4346
+
4347
+	/**
4348
+	 * this handles saving the settings for a messenger or message type
4349
+	 *
4350
+	 */
4351
+	public function save_settings()
4352
+	{
4353
+		if (! isset($this->_req_data['type'])) {
4354
+			EE_Error::add_error(
4355
+				esc_html__(
4356
+					'Cannot save settings because type is unknown (messenger settings or messsage type settings?)',
4357
+					'event_espresso'
4358
+				),
4359
+				__FILE__,
4360
+				__FUNCTION__,
4361
+				__LINE__
4362
+			);
4363
+			$this->_template_args['error'] = true;
4364
+			$this->_return_json();
4365
+		}
4366
+
4367
+
4368
+		if ($this->_req_data['type'] === 'messenger') {
4369
+			// this should be an array.
4370
+			$settings = $this->_req_data['messenger_settings'];
4371
+			$messenger = $settings['messenger'];
4372
+			// let's setup the settings data
4373
+			foreach ($settings as $key => $value) {
4374
+				switch ($key) {
4375
+					case 'messenger':
4376
+						unset($settings['messenger']);
4377
+						break;
4378
+					case 'message_types':
4379
+						unset($settings['message_types']);
4380
+						break;
4381
+					default:
4382
+						$settings[ $key ] = $value;
4383
+						break;
4384
+				}
4385
+			}
4386
+			$this->_message_resource_manager->add_settings_for_messenger($messenger, $settings);
4387
+		} elseif ($this->_req_data['type'] === 'message_type') {
4388
+			$settings = $this->_req_data['message_type_settings'];
4389
+			$messenger = $settings['messenger'];
4390
+			$message_type = $settings['message_type'];
4391
+
4392
+			foreach ($settings as $key => $value) {
4393
+				switch ($key) {
4394
+					case 'messenger':
4395
+						unset($settings['messenger']);
4396
+						break;
4397
+					case 'message_type':
4398
+						unset($settings['message_type']);
4399
+						break;
4400
+					default:
4401
+						$settings[ $key ] = $value;
4402
+						break;
4403
+				}
4404
+			}
4405
+
4406
+			$this->_message_resource_manager->add_settings_for_message_type($messenger, $message_type, $settings);
4407
+		}
4408
+
4409
+		// okay we should have the data all setup.  Now we just update!
4410
+		$success = $this->_message_resource_manager->update_active_messengers_option();
4411
+
4412
+		if ($success) {
4413
+			EE_Error::add_success(__('Settings updated', 'event_espresso'));
4414
+		} else {
4415
+			EE_Error::add_error(
4416
+				esc_html__(
4417
+					'Settings did not get updated',
4418
+					'event_espresso'
4419
+				),
4420
+				__FILE__,
4421
+				__FUNCTION__,
4422
+				__LINE__
4423
+			);
4424
+		}
4425
+
4426
+		$this->_template_args['success'] = $success;
4427
+		$this->_return_json();
4428
+	}
4429
+
4430
+
4431
+
4432
+
4433
+	/**  EE MESSAGE PROCESSING ACTIONS **/
4434
+
4435
+
4436
+	/**
4437
+	 * This immediately generates any EE_Message ID's that are selected that are EEM_Message::status_incomplete
4438
+	 * However, this does not send immediately, it just queues for sending.
4439
+	 *
4440
+	 * @since 4.9.0
4441
+	 * @throws EE_Error
4442
+	 * @throws InvalidDataTypeException
4443
+	 * @throws InvalidInterfaceException
4444
+	 * @throws InvalidArgumentException
4445
+	 * @throws ReflectionException
4446
+	 */
4447
+	protected function _generate_now()
4448
+	{
4449
+		EED_Messages::generate_now($this->_get_msg_ids_from_request());
4450
+		$this->_redirect_after_action(false, '', '', array(), true);
4451
+	}
4452
+
4453
+
4454
+	/**
4455
+	 * This immediately generates AND sends any EE_Message's selected that are EEM_Message::status_incomplete or that
4456
+	 * are EEM_Message::status_resend or EEM_Message::status_idle
4457
+	 *
4458
+	 * @since 4.9.0
4459
+	 * @throws EE_Error
4460
+	 * @throws InvalidDataTypeException
4461
+	 * @throws InvalidInterfaceException
4462
+	 * @throws InvalidArgumentException
4463
+	 * @throws ReflectionException
4464
+	 */
4465
+	protected function _generate_and_send_now()
4466
+	{
4467
+		EED_Messages::generate_and_send_now($this->_get_msg_ids_from_request());
4468
+		$this->_redirect_after_action(false, '', '', array(), true);
4469
+	}
4470
+
4471
+
4472
+	/**
4473
+	 * This queues any EEM_Message::status_sent EE_Message ids in the request for resending.
4474
+	 *
4475
+	 * @since 4.9.0
4476
+	 * @throws EE_Error
4477
+	 * @throws InvalidDataTypeException
4478
+	 * @throws InvalidInterfaceException
4479
+	 * @throws InvalidArgumentException
4480
+	 * @throws ReflectionException
4481
+	 */
4482
+	protected function _queue_for_resending()
4483
+	{
4484
+		EED_Messages::queue_for_resending($this->_get_msg_ids_from_request());
4485
+		$this->_redirect_after_action(false, '', '', array(), true);
4486
+	}
4487
+
4488
+
4489
+	/**
4490
+	 *  This sends immediately any EEM_Message::status_idle or EEM_Message::status_resend messages in the queue
4491
+	 *
4492
+	 * @since 4.9.0
4493
+	 * @throws EE_Error
4494
+	 * @throws InvalidDataTypeException
4495
+	 * @throws InvalidInterfaceException
4496
+	 * @throws InvalidArgumentException
4497
+	 * @throws ReflectionException
4498
+	 */
4499
+	protected function _send_now()
4500
+	{
4501
+		EED_Messages::send_now($this->_get_msg_ids_from_request());
4502
+		$this->_redirect_after_action(false, '', '', array(), true);
4503
+	}
4504
+
4505
+
4506
+	/**
4507
+	 * Deletes EE_messages for IDs in the request.
4508
+	 *
4509
+	 * @since 4.9.0
4510
+	 * @throws EE_Error
4511
+	 * @throws InvalidDataTypeException
4512
+	 * @throws InvalidInterfaceException
4513
+	 * @throws InvalidArgumentException
4514
+	 */
4515
+	protected function _delete_ee_messages()
4516
+	{
4517
+		$msg_ids = $this->_get_msg_ids_from_request();
4518
+		$deleted_count = 0;
4519
+		foreach ($msg_ids as $msg_id) {
4520
+			if (EEM_Message::instance()->delete_by_ID($msg_id)) {
4521
+				$deleted_count++;
4522
+			}
4523
+		}
4524
+		if ($deleted_count) {
4525
+			EE_Error::add_success(
4526
+				esc_html(
4527
+					_n(
4528
+						'Message successfully deleted',
4529
+						'Messages successfully deleted',
4530
+						$deleted_count,
4531
+						'event_espresso'
4532
+					)
4533
+				)
4534
+			);
4535
+			$this->_redirect_after_action(
4536
+				false,
4537
+				'',
4538
+				'',
4539
+				array(),
4540
+				true
4541
+			);
4542
+		} else {
4543
+			EE_Error::add_error(
4544
+				_n('The message was not deleted.', 'The messages were not deleted', count($msg_ids), 'event_espresso'),
4545
+				__FILE__,
4546
+				__FUNCTION__,
4547
+				__LINE__
4548
+			);
4549
+			$this->_redirect_after_action(false, '', '', array(), true);
4550
+		}
4551
+	}
4552
+
4553
+
4554
+	/**
4555
+	 *  This looks for 'MSG_ID' key in the request and returns an array of MSG_ID's if present.
4556
+	 *
4557
+	 * @since 4.9.0
4558
+	 * @return array
4559
+	 */
4560
+	protected function _get_msg_ids_from_request()
4561
+	{
4562
+		if (! isset($this->_req_data['MSG_ID'])) {
4563
+			return array();
4564
+		}
4565
+
4566
+		return is_array($this->_req_data['MSG_ID'])
4567
+			? array_keys($this->_req_data['MSG_ID'])
4568
+			: array($this->_req_data['MSG_ID']);
4569
+	}
4570 4570
 }
Please login to merge, or discard this patch.
Spacing   +213 added lines, -213 removed lines patch added patch discarded remove patch
@@ -141,8 +141,8 @@  discard block
 block discarded – undo
141 141
         $i = 1;
142 142
         foreach ($active_messengers as $active_messenger) {
143 143
             if ($active_messenger instanceof EE_Message) {
144
-                $m_values[ $i ]['id'] = $active_messenger->messenger();
145
-                $m_values[ $i ]['text'] = ucwords($active_messenger->messenger_label());
144
+                $m_values[$i]['id'] = $active_messenger->messenger();
145
+                $m_values[$i]['text'] = ucwords($active_messenger->messenger_label());
146 146
                 $i++;
147 147
             }
148 148
         }
@@ -178,8 +178,8 @@  discard block
 block discarded – undo
178 178
         $i = 1;
179 179
         foreach ($active_messages as $active_message) {
180 180
             if ($active_message instanceof EE_Message) {
181
-                $mt_values[ $i ]['id'] = $active_message->message_type();
182
-                $mt_values[ $i ]['text'] = ucwords($active_message->message_type_label());
181
+                $mt_values[$i]['id'] = $active_message->message_type();
182
+                $mt_values[$i]['text'] = ucwords($active_message->message_type_label());
183 183
                 $i++;
184 184
             }
185 185
         }
@@ -218,7 +218,7 @@  discard block
 block discarded – undo
218 218
                 if ($message_type instanceof EE_message_type) {
219 219
                     $message_type_contexts = $message_type->get_contexts();
220 220
                     foreach ($message_type_contexts as $context => $context_details) {
221
-                        $contexts[ $context ] = $context_details['label'];
221
+                        $contexts[$context] = $context_details['label'];
222 222
                     }
223 223
                 }
224 224
             }
@@ -709,53 +709,53 @@  discard block
 block discarded – undo
709 709
 
710 710
     public function messages_help_tab()
711 711
     {
712
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_help_tab.template.php');
712
+        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH.'ee_msg_messages_help_tab.template.php');
713 713
     }
714 714
 
715 715
 
716 716
     public function messengers_help_tab()
717 717
     {
718
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messenger_help_tab.template.php');
718
+        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH.'ee_msg_messenger_help_tab.template.php');
719 719
     }
720 720
 
721 721
 
722 722
     public function message_types_help_tab()
723 723
     {
724
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_type_help_tab.template.php');
724
+        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH.'ee_msg_message_type_help_tab.template.php');
725 725
     }
726 726
 
727 727
 
728 728
     public function messages_overview_help_tab()
729 729
     {
730
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_overview_help_tab.template.php');
730
+        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH.'ee_msg_overview_help_tab.template.php');
731 731
     }
732 732
 
733 733
 
734 734
     public function message_templates_help_tab()
735 735
     {
736
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_templates_help_tab.template.php');
736
+        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH.'ee_msg_message_templates_help_tab.template.php');
737 737
     }
738 738
 
739 739
 
740 740
     public function edit_message_template_help_tab()
741 741
     {
742
-        $args['img1'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/editor.png' . '" alt="'
742
+        $args['img1'] = '<img src="'.EE_MSG_ASSETS_URL.'images/editor.png'.'" alt="'
743 743
                         . esc_attr__('Editor Title', 'event_espresso')
744 744
                         . '" />';
745
-        $args['img2'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/switch-context.png' . '" alt="'
745
+        $args['img2'] = '<img src="'.EE_MSG_ASSETS_URL.'images/switch-context.png'.'" alt="'
746 746
                         . esc_attr__('Context Switcher and Preview', 'event_espresso')
747 747
                         . '" />';
748
-        $args['img3'] = '<img class="left" src="' . EE_MSG_ASSETS_URL . 'images/form-fields.png' . '" alt="'
748
+        $args['img3'] = '<img class="left" src="'.EE_MSG_ASSETS_URL.'images/form-fields.png'.'" alt="'
749 749
                         . esc_attr__('Message Template Form Fields', 'event_espresso')
750 750
                         . '" />';
751
-        $args['img4'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/shortcodes-metabox.png' . '" alt="'
751
+        $args['img4'] = '<img class="right" src="'.EE_MSG_ASSETS_URL.'images/shortcodes-metabox.png'.'" alt="'
752 752
                         . esc_attr__('Shortcodes Metabox', 'event_espresso')
753 753
                         . '" />';
754
-        $args['img5'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/publish-meta-box.png' . '" alt="'
754
+        $args['img5'] = '<img class="right" src="'.EE_MSG_ASSETS_URL.'images/publish-meta-box.png'.'" alt="'
755 755
                         . esc_attr__('Publish Metabox', 'event_espresso')
756 756
                         . '" />';
757 757
         EEH_Template::display_template(
758
-            EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_templates_editor_help_tab.template.php',
758
+            EE_MSG_TEMPLATE_PATH.'ee_msg_messages_templates_editor_help_tab.template.php',
759 759
             $args
760 760
         );
761 761
     }
@@ -766,7 +766,7 @@  discard block
 block discarded – undo
766 766
         $this->_set_shortcodes();
767 767
         $args['shortcodes'] = $this->_shortcodes;
768 768
         EEH_Template::display_template(
769
-            EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_shortcodes_help_tab.template.php',
769
+            EE_MSG_TEMPLATE_PATH.'ee_msg_messages_shortcodes_help_tab.template.php',
770 770
             $args
771 771
         );
772 772
     }
@@ -774,16 +774,16 @@  discard block
 block discarded – undo
774 774
 
775 775
     public function preview_message_help_tab()
776 776
     {
777
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_preview_help_tab.template.php');
777
+        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH.'ee_msg_preview_help_tab.template.php');
778 778
     }
779 779
 
780 780
 
781 781
     public function settings_help_tab()
782 782
     {
783
-        $args['img1'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-active.png'
784
-                        . '" alt="' . esc_attr__('Active Email Tab', 'event_espresso') . '" />';
785
-        $args['img2'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-inactive.png'
786
-                        . '" alt="' . esc_attr__('Inactive Email Tab', 'event_espresso') . '" />';
783
+        $args['img1'] = '<img class="inline-text" src="'.EE_MSG_ASSETS_URL.'images/email-tab-active.png'
784
+                        . '" alt="'.esc_attr__('Active Email Tab', 'event_espresso').'" />';
785
+        $args['img2'] = '<img class="inline-text" src="'.EE_MSG_ASSETS_URL.'images/email-tab-inactive.png'
786
+                        . '" alt="'.esc_attr__('Inactive Email Tab', 'event_espresso').'" />';
787 787
         $args['img3'] = '<div class="switch">'
788 788
                         . '<input class="ee-on-off-toggle ee-toggle-round-flat"'
789 789
                         . ' type="checkbox" checked="checked">'
@@ -794,25 +794,25 @@  discard block
 block discarded – undo
794 794
                         . ' type="checkbox">'
795 795
                         . '<label for="ee-on-off-toggle-on"></label>'
796 796
                         . '</div>';
797
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_settings_help_tab.template.php', $args);
797
+        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH.'ee_msg_messages_settings_help_tab.template.php', $args);
798 798
     }
799 799
 
800 800
 
801 801
     public function load_scripts_styles()
802 802
     {
803
-        wp_register_style('espresso_ee_msg', EE_MSG_ASSETS_URL . 'ee_message_admin.css', EVENT_ESPRESSO_VERSION);
803
+        wp_register_style('espresso_ee_msg', EE_MSG_ASSETS_URL.'ee_message_admin.css', EVENT_ESPRESSO_VERSION);
804 804
         wp_enqueue_style('espresso_ee_msg');
805 805
 
806 806
         wp_register_script(
807 807
             'ee-messages-settings',
808
-            EE_MSG_ASSETS_URL . 'ee-messages-settings.js',
808
+            EE_MSG_ASSETS_URL.'ee-messages-settings.js',
809 809
             array('jquery-ui-droppable', 'ee-serialize-full-array'),
810 810
             EVENT_ESPRESSO_VERSION,
811 811
             true
812 812
         );
813 813
         wp_register_script(
814 814
             'ee-msg-list-table-js',
815
-            EE_MSG_ASSETS_URL . 'ee_message_admin_list_table.js',
815
+            EE_MSG_ASSETS_URL.'ee_message_admin_list_table.js',
816 816
             array('ee-dialog'),
817 817
             EVENT_ESPRESSO_VERSION
818 818
         );
@@ -870,7 +870,7 @@  discard block
 block discarded – undo
870 870
 
871 871
         wp_register_script(
872 872
             'ee_msgs_edit_js',
873
-            EE_MSG_ASSETS_URL . 'ee_message_editor.js',
873
+            EE_MSG_ASSETS_URL.'ee_message_editor.js',
874 874
             array('jquery'),
875 875
             EVENT_ESPRESSO_VERSION
876 876
         );
@@ -914,7 +914,7 @@  discard block
 block discarded – undo
914 914
     {
915 915
         wp_register_style(
916 916
             'ee-message-settings',
917
-            EE_MSG_ASSETS_URL . 'ee_message_settings.css',
917
+            EE_MSG_ASSETS_URL.'ee_message_settings.css',
918 918
             array(),
919 919
             EVENT_ESPRESSO_VERSION
920 920
         );
@@ -1000,7 +1000,7 @@  discard block
 block discarded – undo
1000 1000
             }
1001 1001
             $status_bulk_actions = $common_bulk_actions;
1002 1002
             // unset bulk actions not applying to status
1003
-            if (! empty($status_bulk_actions)) {
1003
+            if ( ! empty($status_bulk_actions)) {
1004 1004
                 switch ($status) {
1005 1005
                     case EEM_Message::status_idle:
1006 1006
                     case EEM_Message::status_resend:
@@ -1029,7 +1029,7 @@  discard block
 block discarded – undo
1029 1029
                 continue;
1030 1030
             }
1031 1031
 
1032
-            $this->_views[ strtolower($status) ] = array(
1032
+            $this->_views[strtolower($status)] = array(
1033 1033
                 'slug'        => strtolower($status),
1034 1034
                 'label'       => EEH_Template::pretty_status($status, false, 'sentence'),
1035 1035
                 'count'       => 0,
@@ -1068,7 +1068,7 @@  discard block
 block discarded – undo
1068 1068
             if ($action_item === 'see_notifications_for') {
1069 1069
                 continue;
1070 1070
             }
1071
-            $action_items[ $action_item ] = array(
1071
+            $action_items[$action_item] = array(
1072 1072
                 'class' => $action_details['css_class'],
1073 1073
                 'desc'  => $action_details['label'],
1074 1074
             );
@@ -1077,37 +1077,37 @@  discard block
 block discarded – undo
1077 1077
         /** @type array $status_items status legend setup */
1078 1078
         $status_items = array(
1079 1079
             'sent_status'                => array(
1080
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_sent,
1080
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Message::status_sent,
1081 1081
                 'desc'  => EEH_Template::pretty_status(EEM_Message::status_sent, false, 'sentence'),
1082 1082
             ),
1083 1083
             'idle_status'                => array(
1084
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_idle,
1084
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Message::status_idle,
1085 1085
                 'desc'  => EEH_Template::pretty_status(EEM_Message::status_idle, false, 'sentence'),
1086 1086
             ),
1087 1087
             'failed_status'              => array(
1088
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_failed,
1088
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Message::status_failed,
1089 1089
                 'desc'  => EEH_Template::pretty_status(EEM_Message::status_failed, false, 'sentence'),
1090 1090
             ),
1091 1091
             'messenger_executing_status' => array(
1092
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_messenger_executing,
1092
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Message::status_messenger_executing,
1093 1093
                 'desc'  => EEH_Template::pretty_status(EEM_Message::status_messenger_executing, false, 'sentence'),
1094 1094
             ),
1095 1095
             'resend_status'              => array(
1096
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_resend,
1096
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Message::status_resend,
1097 1097
                 'desc'  => EEH_Template::pretty_status(EEM_Message::status_resend, false, 'sentence'),
1098 1098
             ),
1099 1099
             'incomplete_status'          => array(
1100
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_incomplete,
1100
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Message::status_incomplete,
1101 1101
                 'desc'  => EEH_Template::pretty_status(EEM_Message::status_incomplete, false, 'sentence'),
1102 1102
             ),
1103 1103
             'retry_status'               => array(
1104
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_retry,
1104
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Message::status_retry,
1105 1105
                 'desc'  => EEH_Template::pretty_status(EEM_Message::status_retry, false, 'sentence'),
1106 1106
             ),
1107 1107
         );
1108 1108
         if (EEM_Message::debug()) {
1109 1109
             $status_items['debug_only_status'] = array(
1110
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_debug_only,
1110
+                'class' => 'ee-status-legend ee-status-legend-'.EEM_Message::status_debug_only,
1111 1111
                 'desc'  => EEH_Template::pretty_status(EEM_Message::status_debug_only, false, 'sentence'),
1112 1112
             );
1113 1113
         }
@@ -1119,11 +1119,11 @@  discard block
 block discarded – undo
1119 1119
     protected function _custom_mtps_preview()
1120 1120
     {
1121 1121
         $this->_admin_page_title = esc_html__('Custom Message Templates (Preview)', 'event_espresso');
1122
-        $this->_template_args['preview_img'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/custom_mtps_preview.png"'
1123
-                                               . ' alt="' . esc_attr__(
1122
+        $this->_template_args['preview_img'] = '<img src="'.EE_MSG_ASSETS_URL.'images/custom_mtps_preview.png"'
1123
+                                               . ' alt="'.esc_attr__(
1124 1124
                                                    'Preview Custom Message Templates screenshot',
1125 1125
                                                    'event_espresso'
1126
-                                               ) . '" />';
1126
+                                               ).'" />';
1127 1127
         $this->_template_args['preview_text'] = '<strong>'
1128 1128
                                                 . esc_html__(
1129 1129
                                                     'Custom Message Templates is a feature that is only available in the premium version of Event Espresso 4 which is available with a support license purchase on EventEspresso.com. With the Custom Message Templates feature, you are able to create custom message templates and assign them on a per-event basis.',
@@ -1203,7 +1203,7 @@  discard block
 block discarded – undo
1203 1203
         $installed = array();
1204 1204
 
1205 1205
         foreach ($installed_message_types as $message_type) {
1206
-            $installed[ $message_type->name ] = $message_type;
1206
+            $installed[$message_type->name] = $message_type;
1207 1207
         }
1208 1208
 
1209 1209
         return $installed;
@@ -1348,7 +1348,7 @@  discard block
 block discarded – undo
1348 1348
         // we need to assemble the title from Various details
1349 1349
         $context_label = sprintf(
1350 1350
             esc_html__('(%s %s)', 'event_espresso'),
1351
-            $c_config[ $context ]['label'],
1351
+            $c_config[$context]['label'],
1352 1352
             ucwords($c_label['label'])
1353 1353
         );
1354 1354
 
@@ -1370,7 +1370,7 @@  discard block
 block discarded – undo
1370 1370
             $message_template_group->message_type()
1371 1371
         );
1372 1372
 
1373
-        if (! $template_field_structure) {
1373
+        if ( ! $template_field_structure) {
1374 1374
             $template_field_structure = false;
1375 1375
             $template_fields = esc_html__(
1376 1376
                 'There was an error in assembling the fields for this display (you should see an error message)',
@@ -1384,21 +1384,21 @@  discard block
 block discarded – undo
1384 1384
 
1385 1385
         // if we have the extra key.. then we need to remove the content index from the template_field_structure as it
1386 1386
         // will get handled in the "extra" array.
1387
-        if (is_array($template_field_structure[ $context ]) && isset($template_field_structure[ $context ]['extra'])) {
1388
-            foreach ($template_field_structure[ $context ]['extra'] as $reference_field => $new_fields) {
1389
-                unset($template_field_structure[ $context ][ $reference_field ]);
1387
+        if (is_array($template_field_structure[$context]) && isset($template_field_structure[$context]['extra'])) {
1388
+            foreach ($template_field_structure[$context]['extra'] as $reference_field => $new_fields) {
1389
+                unset($template_field_structure[$context][$reference_field]);
1390 1390
             }
1391 1391
         }
1392 1392
 
1393 1393
         // let's loop through the template_field_structure and actually assemble the input fields!
1394
-        if (! empty($template_field_structure)) {
1395
-            foreach ($template_field_structure[ $context ] as $template_field => $field_setup_array) {
1394
+        if ( ! empty($template_field_structure)) {
1395
+            foreach ($template_field_structure[$context] as $template_field => $field_setup_array) {
1396 1396
                 // if this is an 'extra' template field then we need to remove any existing fields that are keyed up in
1397 1397
                 // the extra array and reset them.
1398 1398
                 if ($template_field === 'extra') {
1399 1399
                     $this->_template_args['is_extra_fields'] = true;
1400 1400
                     foreach ($field_setup_array as $reference_field => $new_fields_array) {
1401
-                        $message_template = $message_templates[ $context ][ $reference_field ];
1401
+                        $message_template = $message_templates[$context][$reference_field];
1402 1402
                         $content = $message_template instanceof EE_Message_Template
1403 1403
                             ? $message_template->get('MTP_content')
1404 1404
                             : '';
@@ -1407,7 +1407,7 @@  discard block
 block discarded – undo
1407 1407
                             $continue = false;
1408 1408
                             if (isset($extra_array['shortcodes_required'])) {
1409 1409
                                 foreach ((array) $extra_array['shortcodes_required'] as $shortcode) {
1410
-                                    if (! array_key_exists($shortcode, $this->_shortcodes)) {
1410
+                                    if ( ! array_key_exists($shortcode, $this->_shortcodes)) {
1411 1411
                                         $continue = true;
1412 1412
                                     }
1413 1413
                                 }
@@ -1420,57 +1420,57 @@  discard block
 block discarded – undo
1420 1420
                                         . '-'
1421 1421
                                         . $extra_field
1422 1422
                                         . '-content';
1423
-                            $template_form_fields[ $field_id ] = $extra_array;
1424
-                            $template_form_fields[ $field_id ]['name'] = 'MTP_template_fields['
1423
+                            $template_form_fields[$field_id] = $extra_array;
1424
+                            $template_form_fields[$field_id]['name'] = 'MTP_template_fields['
1425 1425
                                                                          . $reference_field
1426 1426
                                                                          . '][content]['
1427
-                                                                         . $extra_field . ']';
1427
+                                                                         . $extra_field.']';
1428 1428
                             $css_class = isset($extra_array['css_class'])
1429 1429
                                 ? $extra_array['css_class']
1430 1430
                                 : '';
1431 1431
 
1432
-                            $template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
1432
+                            $template_form_fields[$field_id]['css_class'] = ! empty($v_fields)
1433 1433
                                                                               && in_array($extra_field, $v_fields, true)
1434 1434
                                                                               &&
1435 1435
                                                                               (
1436
-                                                                                  is_array($validators[ $extra_field ])
1437
-                                                                                  && isset($validators[ $extra_field ]['msg'])
1436
+                                                                                  is_array($validators[$extra_field])
1437
+                                                                                  && isset($validators[$extra_field]['msg'])
1438 1438
                                                                               )
1439
-                                ? 'validate-error ' . $css_class
1439
+                                ? 'validate-error '.$css_class
1440 1440
                                 : $css_class;
1441 1441
 
1442
-                            $template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
1443
-                                                                          && isset($content[ $extra_field ])
1444
-                                ? $content[ $extra_field ]
1442
+                            $template_form_fields[$field_id]['value'] = ! empty($message_templates)
1443
+                                                                          && isset($content[$extra_field])
1444
+                                ? $content[$extra_field]
1445 1445
                                 : '';
1446 1446
 
1447 1447
                             // do we have a validation error?  if we do then let's use that value instead
1448
-                            $template_form_fields[ $field_id ]['value'] = isset($validators[ $extra_field ])
1449
-                                ? $validators[ $extra_field ]['value']
1450
-                                : $template_form_fields[ $field_id ]['value'];
1448
+                            $template_form_fields[$field_id]['value'] = isset($validators[$extra_field])
1449
+                                ? $validators[$extra_field]['value']
1450
+                                : $template_form_fields[$field_id]['value'];
1451 1451
 
1452 1452
 
1453
-                            $template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
1453
+                            $template_form_fields[$field_id]['db-col'] = 'MTP_content';
1454 1454
 
1455 1455
                             // shortcode selector
1456 1456
                             $field_name_to_use = $extra_field === 'main'
1457 1457
                                 ? 'content'
1458 1458
                                 : $extra_field;
1459
-                            $template_form_fields[ $field_id ]['append_content'] = $this->_get_shortcode_selector(
1459
+                            $template_form_fields[$field_id]['append_content'] = $this->_get_shortcode_selector(
1460 1460
                                 $field_name_to_use,
1461 1461
                                 $field_id
1462 1462
                             );
1463 1463
 
1464 1464
                             if (isset($extra_array['input']) && $extra_array['input'] === 'wp_editor') {
1465 1465
                                 // we want to decode the entities
1466
-                                $template_form_fields[ $field_id ]['value'] = $template_form_fields[ $field_id ]['value'];
1466
+                                $template_form_fields[$field_id]['value'] = $template_form_fields[$field_id]['value'];
1467 1467
                             }/**/
1468 1468
                         }
1469
-                        $templatefield_MTP_id = $reference_field . '-MTP_ID';
1470
-                        $templatefield_templatename_id = $reference_field . '-name';
1469
+                        $templatefield_MTP_id = $reference_field.'-MTP_ID';
1470
+                        $templatefield_templatename_id = $reference_field.'-name';
1471 1471
 
1472
-                        $template_form_fields[ $templatefield_MTP_id ] = array(
1473
-                            'name'       => 'MTP_template_fields[' . $reference_field . '][MTP_ID]',
1472
+                        $template_form_fields[$templatefield_MTP_id] = array(
1473
+                            'name'       => 'MTP_template_fields['.$reference_field.'][MTP_ID]',
1474 1474
                             'label'      => null,
1475 1475
                             'input'      => 'hidden',
1476 1476
                             'type'       => 'int',
@@ -1482,8 +1482,8 @@  discard block
 block discarded – undo
1482 1482
                             'db-col'     => 'MTP_ID',
1483 1483
                         );
1484 1484
 
1485
-                        $template_form_fields[ $templatefield_templatename_id ] = array(
1486
-                            'name'       => 'MTP_template_fields[' . $reference_field . '][name]',
1485
+                        $template_form_fields[$templatefield_templatename_id] = array(
1486
+                            'name'       => 'MTP_template_fields['.$reference_field.'][name]',
1487 1487
                             'label'      => null,
1488 1488
                             'input'      => 'hidden',
1489 1489
                             'type'       => 'string',
@@ -1497,36 +1497,36 @@  discard block
 block discarded – undo
1497 1497
                     }
1498 1498
                     continue; // skip the next stuff, we got the necessary fields here for this dataset.
1499 1499
                 } else {
1500
-                    $field_id = $template_field . '-content';
1501
-                    $template_form_fields[ $field_id ] = $field_setup_array;
1502
-                    $template_form_fields[ $field_id ]['name'] = 'MTP_template_fields[' . $template_field . '][content]';
1503
-                    $message_template = isset($message_templates[ $context ][ $template_field ])
1504
-                        ? $message_templates[ $context ][ $template_field ]
1500
+                    $field_id = $template_field.'-content';
1501
+                    $template_form_fields[$field_id] = $field_setup_array;
1502
+                    $template_form_fields[$field_id]['name'] = 'MTP_template_fields['.$template_field.'][content]';
1503
+                    $message_template = isset($message_templates[$context][$template_field])
1504
+                        ? $message_templates[$context][$template_field]
1505 1505
                         : null;
1506
-                    $template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
1507
-                                                                  && is_array($message_templates[ $context ])
1506
+                    $template_form_fields[$field_id]['value'] = ! empty($message_templates)
1507
+                                                                  && is_array($message_templates[$context])
1508 1508
                                                                   && $message_template instanceof EE_Message_Template
1509 1509
                         ? $message_template->get('MTP_content')
1510 1510
                         : '';
1511 1511
 
1512 1512
                     // do we have a validator error for this field?  if we do then we'll use that value instead
1513
-                    $template_form_fields[ $field_id ]['value'] = isset($validators[ $template_field ])
1514
-                        ? $validators[ $template_field ]['value']
1515
-                        : $template_form_fields[ $field_id ]['value'];
1513
+                    $template_form_fields[$field_id]['value'] = isset($validators[$template_field])
1514
+                        ? $validators[$template_field]['value']
1515
+                        : $template_form_fields[$field_id]['value'];
1516 1516
 
1517 1517
 
1518
-                    $template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
1518
+                    $template_form_fields[$field_id]['db-col'] = 'MTP_content';
1519 1519
                     $css_class = isset($field_setup_array['css_class'])
1520 1520
                         ? $field_setup_array['css_class']
1521 1521
                         : '';
1522
-                    $template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
1522
+                    $template_form_fields[$field_id]['css_class'] = ! empty($v_fields)
1523 1523
                                                                       && in_array($template_field, $v_fields, true)
1524
-                                                                      && isset($validators[ $template_field ]['msg'])
1525
-                        ? 'validate-error ' . $css_class
1524
+                                                                      && isset($validators[$template_field]['msg'])
1525
+                        ? 'validate-error '.$css_class
1526 1526
                         : $css_class;
1527 1527
 
1528 1528
                     // shortcode selector
1529
-                    $template_form_fields[ $field_id ]['append_content'] = $this->_get_shortcode_selector(
1529
+                    $template_form_fields[$field_id]['append_content'] = $this->_get_shortcode_selector(
1530 1530
                         $template_field,
1531 1531
                         $field_id
1532 1532
                     );
@@ -1534,12 +1534,12 @@  discard block
 block discarded – undo
1534 1534
 
1535 1535
                 // k took care of content field(s) now let's take care of others.
1536 1536
 
1537
-                $templatefield_MTP_id = $template_field . '-MTP_ID';
1538
-                $templatefield_field_templatename_id = $template_field . '-name';
1537
+                $templatefield_MTP_id = $template_field.'-MTP_ID';
1538
+                $templatefield_field_templatename_id = $template_field.'-name';
1539 1539
 
1540 1540
                 // foreach template field there are actually two form fields created
1541
-                $template_form_fields[ $templatefield_MTP_id ] = array(
1542
-                    'name'       => 'MTP_template_fields[' . $template_field . '][MTP_ID]',
1541
+                $template_form_fields[$templatefield_MTP_id] = array(
1542
+                    'name'       => 'MTP_template_fields['.$template_field.'][MTP_ID]',
1543 1543
                     'label'      => null,
1544 1544
                     'input'      => 'hidden',
1545 1545
                     'type'       => 'int',
@@ -1551,8 +1551,8 @@  discard block
 block discarded – undo
1551 1551
                     'db-col'     => 'MTP_ID',
1552 1552
                 );
1553 1553
 
1554
-                $template_form_fields[ $templatefield_field_templatename_id ] = array(
1555
-                    'name'       => 'MTP_template_fields[' . $template_field . '][name]',
1554
+                $template_form_fields[$templatefield_field_templatename_id] = array(
1555
+                    'name'       => 'MTP_template_fields['.$template_field.'][name]',
1556 1556
                     'label'      => null,
1557 1557
                     'input'      => 'hidden',
1558 1558
                     'type'       => 'string',
@@ -1695,10 +1695,10 @@  discard block
 block discarded – undo
1695 1695
                 'value' => $GRP_ID,
1696 1696
             );
1697 1697
             $sidebar_form_fields['ee-msg-evt-nonce'] = array(
1698
-                'name'  => $action . '_nonce',
1698
+                'name'  => $action.'_nonce',
1699 1699
                 'input' => 'hidden',
1700 1700
                 'type'  => 'string',
1701
-                'value' => wp_create_nonce($action . '_nonce'),
1701
+                'value' => wp_create_nonce($action.'_nonce'),
1702 1702
             );
1703 1703
 
1704 1704
             if (isset($this->_req_data['template_switch']) && $this->_req_data['template_switch']) {
@@ -1738,7 +1738,7 @@  discard block
 block discarded – undo
1738 1738
             ),
1739 1739
             $this->_admin_base_url
1740 1740
         );
1741
-        $preview_button = '<a href="' . $preview_url . '" class="button-secondary messages-preview-button">'
1741
+        $preview_button = '<a href="'.$preview_url.'" class="button-secondary messages-preview-button">'
1742 1742
                           . esc_html__('Preview', 'event_espresso')
1743 1743
                           . '</a>';
1744 1744
 
@@ -1775,7 +1775,7 @@  discard block
 block discarded – undo
1775 1775
 
1776 1776
         $this->_template_path = $this->_template_args['GRP_ID']
1777 1777
             ? EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_edit_meta_box.template.php'
1778
-            : EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_add_meta_box.template.php';
1778
+            : EE_MSG_TEMPLATE_PATH.'ee_msg_details_main_add_meta_box.template.php';
1779 1779
 
1780 1780
         // send along EE_Message_Template_Group object for further template use.
1781 1781
         $this->_template_args['MTP'] = $message_template_group;
@@ -1830,7 +1830,7 @@  discard block
 block discarded – undo
1830 1830
     ) {
1831 1831
         $template_args = array(
1832 1832
             'context'                   => $context,
1833
-            'nonce'                     => wp_create_nonce('activate_' . $context . '_toggle_nonce'),
1833
+            'nonce'                     => wp_create_nonce('activate_'.$context.'_toggle_nonce'),
1834 1834
             'is_active'                 => $message_template_group->is_context_active($context),
1835 1835
             'on_off_action'             => $message_template_group->is_context_active($context)
1836 1836
                 ? 'context-off'
@@ -1839,7 +1839,7 @@  discard block
 block discarded – undo
1839 1839
             'message_template_group_id' => $message_template_group->ID(),
1840 1840
         );
1841 1841
         return EEH_Template::display_template(
1842
-            EE_MSG_TEMPLATE_PATH . 'ee_msg_editor_active_context_element.template.php',
1842
+            EE_MSG_TEMPLATE_PATH.'ee_msg_editor_active_context_element.template.php',
1843 1843
             $template_args,
1844 1844
             true
1845 1845
         );
@@ -1860,7 +1860,7 @@  discard block
 block discarded – undo
1860 1860
     {
1861 1861
         $success = true;
1862 1862
         // check for required data
1863
-        if (! isset(
1863
+        if ( ! isset(
1864 1864
             $this->_req_data['message_template_group_id'],
1865 1865
             $this->_req_data['context'],
1866 1866
             $this->_req_data['status']
@@ -1877,7 +1877,7 @@  discard block
 block discarded – undo
1877 1877
         $nonce = isset($this->_req_data['toggle_context_nonce'])
1878 1878
             ? sanitize_text_field($this->_req_data['toggle_context_nonce'])
1879 1879
             : '';
1880
-        $nonce_ref = 'activate_' . $this->_req_data['context'] . '_toggle_nonce';
1880
+        $nonce_ref = 'activate_'.$this->_req_data['context'].'_toggle_nonce';
1881 1881
         $this->_verify_nonce($nonce, $nonce_ref);
1882 1882
         $status = $this->_req_data['status'];
1883 1883
         if ($status !== 'off' && $status !== 'on') {
@@ -1895,7 +1895,7 @@  discard block
 block discarded – undo
1895 1895
         $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID(
1896 1896
             $this->_req_data['message_template_group_id']
1897 1897
         );
1898
-        if (! $message_template_group instanceof EE_Message_Template_Group) {
1898
+        if ( ! $message_template_group instanceof EE_Message_Template_Group) {
1899 1899
             EE_Error::add_error(
1900 1900
                 sprintf(
1901 1901
                     esc_html__(
@@ -2024,7 +2024,7 @@  discard block
 block discarded – undo
2024 2024
         $templates = array();
2025 2025
         $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
2026 2026
         // we need to make sure we've got the info we need.
2027
-        if (! isset($this->_req_data['msgr'], $this->_req_data['mt'], $this->_req_data['GRP_ID'])) {
2027
+        if ( ! isset($this->_req_data['msgr'], $this->_req_data['mt'], $this->_req_data['GRP_ID'])) {
2028 2028
             EE_Error::add_error(
2029 2029
                 esc_html__(
2030 2030
                     'In order to reset the template to its default we require the messenger, message type, and message template GRP_ID to know what is being reset.  At least one of these is missing.',
@@ -2066,7 +2066,7 @@  discard block
 block discarded – undo
2066 2066
         }
2067 2067
 
2068 2068
         // any error messages?
2069
-        if (! $success) {
2069
+        if ( ! $success) {
2070 2070
             EE_Error::add_error(
2071 2071
                 esc_html__(
2072 2072
                     'Something went wrong with deleting existing templates. Unable to reset to default',
@@ -2118,7 +2118,7 @@  discard block
 block discarded – undo
2118 2118
     public function _preview_message($send = false)
2119 2119
     {
2120 2120
         // first make sure we've got the necessary parameters
2121
-        if (! isset(
2121
+        if ( ! isset(
2122 2122
             $this->_req_data['message_type'],
2123 2123
             $this->_req_data['messenger'],
2124 2124
             $this->_req_data['messenger'],
@@ -2170,15 +2170,15 @@  discard block
 block discarded – undo
2170 2170
         $preview_title = sprintf(
2171 2171
             esc_html__('Viewing Preview for %s %s Message Template', 'event_espresso'),
2172 2172
             $active_messenger_label,
2173
-            ucwords($message_types[ $this->_req_data['message_type'] ]->label['singular'])
2173
+            ucwords($message_types[$this->_req_data['message_type']]->label['singular'])
2174 2174
         );
2175
-        if ( empty( $preview ) ) {
2175
+        if (empty($preview)) {
2176 2176
             $this->noEventsErrorMessage();
2177 2177
         }
2178 2178
         // setup display of preview.
2179 2179
         $this->_admin_page_title = $preview_title;
2180 2180
         $this->_template_args['admin_page_title'] = $preview_title;
2181
-        $this->_template_args['admin_page_content'] = $preview_button . '<br />' . $preview;
2181
+        $this->_template_args['admin_page_content'] = $preview_button.'<br />'.$preview;
2182 2182
         $this->_template_args['data']['force_json'] = true;
2183 2183
 
2184 2184
         return '';
@@ -2196,7 +2196,7 @@  discard block
 block discarded – undo
2196 2196
                 'action' => 'default',
2197 2197
                 'page' => 'espresso_events',
2198 2198
             ),
2199
-            admin_url( 'admin.php' )
2199
+            admin_url('admin.php')
2200 2200
         );
2201 2201
         $message = $test_send
2202 2202
             ? __(
@@ -2290,10 +2290,10 @@  discard block
 block discarded – undo
2290 2290
         foreach ($tp_collection as $tp) {
2291 2291
             // only include template packs that support this messenger and message type!
2292 2292
             $supports = $tp->get_supports();
2293
-            if (! isset($supports[ $this->_message_template_group->messenger() ])
2293
+            if ( ! isset($supports[$this->_message_template_group->messenger()])
2294 2294
                 || ! in_array(
2295 2295
                     $this->_message_template_group->message_type(),
2296
-                    $supports[ $this->_message_template_group->messenger() ],
2296
+                    $supports[$this->_message_template_group->messenger()],
2297 2297
                     true
2298 2298
                 )
2299 2299
             ) {
@@ -2346,7 +2346,7 @@  discard block
 block discarded – undo
2346 2346
         $template_args['template_pack_description'] = $template_pack_labels->template_pack_description;
2347 2347
         $template_args['template_variation_description'] = $template_pack_labels->template_variation_description;
2348 2348
 
2349
-        $template = EE_MSG_TEMPLATE_PATH . 'template_pack_and_variations_metabox.template.php';
2349
+        $template = EE_MSG_TEMPLATE_PATH.'template_pack_and_variations_metabox.template.php';
2350 2350
 
2351 2351
         EEH_Template::display_template($template, $template_args);
2352 2352
     }
@@ -2372,10 +2372,10 @@  discard block
 block discarded – undo
2372 2372
         // first we need to see if there are any fields
2373 2373
         $fields = $this->_message_template_group->messenger_obj()->get_test_settings_fields();
2374 2374
 
2375
-        if (! empty($fields)) {
2375
+        if ( ! empty($fields)) {
2376 2376
             // yup there be fields
2377 2377
             foreach ($fields as $field => $config) {
2378
-                $field_id = $this->_message_template_group->messenger() . '_' . $field;
2378
+                $field_id = $this->_message_template_group->messenger().'_'.$field;
2379 2379
                 $existing = $this->_message_template_group->messenger_obj()->get_existing_test_settings();
2380 2380
                 $default = isset($config['default']) ? $config['default'] : '';
2381 2381
                 $default = isset($config['value']) ? $config['value'] : $default;
@@ -2383,22 +2383,22 @@  discard block
 block discarded – undo
2383 2383
                 // if type is hidden and the value is empty
2384 2384
                 // something may have gone wrong so let's correct with the defaults
2385 2385
                 $fix = $config['input'] === 'hidden'
2386
-                       && isset($existing[ $field ])
2387
-                       && empty($existing[ $field ])
2386
+                       && isset($existing[$field])
2387
+                       && empty($existing[$field])
2388 2388
                     ? $default
2389 2389
                     : '';
2390
-                $existing[ $field ] = isset($existing[ $field ]) && empty($fix)
2391
-                    ? $existing[ $field ]
2390
+                $existing[$field] = isset($existing[$field]) && empty($fix)
2391
+                    ? $existing[$field]
2392 2392
                     : $fix;
2393 2393
 
2394
-                $template_form_fields[ $field_id ] = array(
2395
-                    'name'       => 'test_settings_fld[' . $field . ']',
2394
+                $template_form_fields[$field_id] = array(
2395
+                    'name'       => 'test_settings_fld['.$field.']',
2396 2396
                     'label'      => $config['label'],
2397 2397
                     'input'      => $config['input'],
2398 2398
                     'type'       => $config['type'],
2399 2399
                     'required'   => $config['required'],
2400 2400
                     'validation' => $config['validation'],
2401
-                    'value'      => isset($existing[ $field ]) ? $existing[ $field ] : $default,
2401
+                    'value'      => isset($existing[$field]) ? $existing[$field] : $default,
2402 2402
                     'css_class'  => $config['css_class'],
2403 2403
                     'options'    => isset($config['options']) ? $config['options'] : array(),
2404 2404
                     'default'    => $default,
@@ -2413,7 +2413,7 @@  discard block
 block discarded – undo
2413 2413
 
2414 2414
         $test_settings_html = '';
2415 2415
         // print out $test_settings_fields
2416
-        if (! empty($test_settings_fields)) {
2416
+        if ( ! empty($test_settings_fields)) {
2417 2417
             echo $test_settings_fields;
2418 2418
             $test_settings_html = '<input type="submit" class="button-primary mtp-test-button alignright" ';
2419 2419
             $test_settings_html .= 'name="test_button" value="';
@@ -2460,7 +2460,7 @@  discard block
 block discarded – undo
2460 2460
         );
2461 2461
 
2462 2462
         return EEH_Template::display_template(
2463
-            EE_MSG_TEMPLATE_PATH . 'shortcode_selector_skeleton.template.php',
2463
+            EE_MSG_TEMPLATE_PATH.'shortcode_selector_skeleton.template.php',
2464 2464
             $template_args,
2465 2465
             true
2466 2466
         );
@@ -2485,7 +2485,7 @@  discard block
 block discarded – undo
2485 2485
         // $messenger = $this->_message_template_group->messenger_obj();
2486 2486
         // now let's set the content depending on the status of the shortcodes array
2487 2487
         if (empty($shortcodes)) {
2488
-            $content = '<p>' . esc_html__('There are no valid shortcodes available', 'event_espresso') . '</p>';
2488
+            $content = '<p>'.esc_html__('There are no valid shortcodes available', 'event_espresso').'</p>';
2489 2489
             echo $content;
2490 2490
         } else {
2491 2491
             // $alt = 0;
@@ -2523,7 +2523,7 @@  discard block
 block discarded – undo
2523 2523
     {
2524 2524
 
2525 2525
         // no need to run this if the property is already set
2526
-        if (! empty($this->_shortcodes)) {
2526
+        if ( ! empty($this->_shortcodes)) {
2527 2527
             return;
2528 2528
         }
2529 2529
 
@@ -2577,7 +2577,7 @@  discard block
 block discarded – undo
2577 2577
     protected function _set_message_template_group()
2578 2578
     {
2579 2579
 
2580
-        if (! empty($this->_message_template_group)) {
2580
+        if ( ! empty($this->_message_template_group)) {
2581 2581
             return;
2582 2582
         } //get out if this is already set.
2583 2583
 
@@ -2624,7 +2624,7 @@  discard block
 block discarded – undo
2624 2624
                     <?php
2625 2625
                 }
2626 2626
                 // setup nonce_url
2627
-                wp_nonce_field($args['action'] . '_nonce', $args['action'] . '_nonce', false);
2627
+                wp_nonce_field($args['action'].'_nonce', $args['action'].'_nonce', false);
2628 2628
                 ?>
2629 2629
                 <select name="context">
2630 2630
                     <?php
@@ -2634,7 +2634,7 @@  discard block
 block discarded – undo
2634 2634
                             $checked = ($context === $args['context']) ? 'selected="selected"' : '';
2635 2635
                             ?>
2636 2636
                             <option value="<?php echo $context; ?>" <?php echo $checked; ?>>
2637
-                                <?php echo $context_details[ $context ]['label']; ?>
2637
+                                <?php echo $context_details[$context]['label']; ?>
2638 2638
                             </option>
2639 2639
                         <?php endforeach;
2640 2640
                     endif; ?>
@@ -2664,22 +2664,22 @@  discard block
 block discarded – undo
2664 2664
      */
2665 2665
     protected function _set_message_template_column_values($index)
2666 2666
     {
2667
-        if (is_array($this->_req_data['MTP_template_fields'][ $index ]['content'])) {
2668
-            foreach ($this->_req_data['MTP_template_fields'][ $index ]['content'] as $field => $value) {
2669
-                $this->_req_data['MTP_template_fields'][ $index ]['content'][ $field ] = $value;
2667
+        if (is_array($this->_req_data['MTP_template_fields'][$index]['content'])) {
2668
+            foreach ($this->_req_data['MTP_template_fields'][$index]['content'] as $field => $value) {
2669
+                $this->_req_data['MTP_template_fields'][$index]['content'][$field] = $value;
2670 2670
             }
2671 2671
         }
2672 2672
 
2673 2673
 
2674 2674
         $set_column_values = array(
2675
-            'MTP_ID'             => absint($this->_req_data['MTP_template_fields'][ $index ]['MTP_ID']),
2675
+            'MTP_ID'             => absint($this->_req_data['MTP_template_fields'][$index]['MTP_ID']),
2676 2676
             'GRP_ID'             => absint($this->_req_data['GRP_ID']),
2677 2677
             'MTP_user_id'        => absint($this->_req_data['MTP_user_id']),
2678 2678
             'MTP_messenger'      => strtolower($this->_req_data['MTP_messenger']),
2679 2679
             'MTP_message_type'   => strtolower($this->_req_data['MTP_message_type']),
2680
-            'MTP_template_field' => strtolower($this->_req_data['MTP_template_fields'][ $index ]['name']),
2680
+            'MTP_template_field' => strtolower($this->_req_data['MTP_template_fields'][$index]['name']),
2681 2681
             'MTP_context'        => strtolower($this->_req_data['MTP_context']),
2682
-            'MTP_content'        => $this->_req_data['MTP_template_fields'][ $index ]['content'],
2682
+            'MTP_content'        => $this->_req_data['MTP_template_fields'][$index]['content'],
2683 2683
             'MTP_is_global'      => isset($this->_req_data['MTP_is_global'])
2684 2684
                 ? absint($this->_req_data['MTP_is_global'])
2685 2685
                 : 0,
@@ -2726,7 +2726,7 @@  discard block
 block discarded – undo
2726 2726
         $context = ucwords(str_replace('_', ' ', $context_slug));
2727 2727
 
2728 2728
         $item_desc = $messenger_label && $message_type_label
2729
-            ? $messenger_label . ' ' . $message_type_label . ' ' . $context . ' '
2729
+            ? $messenger_label.' '.$message_type_label.' '.$context.' '
2730 2730
             : '';
2731 2731
         $item_desc .= 'Message Template';
2732 2732
         $query_args = array();
@@ -2757,7 +2757,7 @@  discard block
 block discarded – undo
2757 2757
 
2758 2758
 
2759 2759
             // run update for each template field in displayed context
2760
-            if (! isset($this->_req_data['MTP_template_fields']) && empty($this->_req_data['MTP_template_fields'])) {
2760
+            if ( ! isset($this->_req_data['MTP_template_fields']) && empty($this->_req_data['MTP_template_fields'])) {
2761 2761
                 EE_Error::add_error(
2762 2762
                     esc_html__(
2763 2763
                         'There was a problem saving the template fields from the form because I didn\'t receive any actual template field data.',
@@ -2819,10 +2819,10 @@  discard block
 block discarded – undo
2819 2819
                         $set_column_values = $this->_set_message_template_column_values($template_field);
2820 2820
 
2821 2821
                         $where_cols_n_values = array(
2822
-                            'MTP_ID' => $this->_req_data['MTP_template_fields'][ $template_field ]['MTP_ID'],
2822
+                            'MTP_ID' => $this->_req_data['MTP_template_fields'][$template_field]['MTP_ID'],
2823 2823
                         );
2824 2824
                         // if they aren't allowed to use all JS, restrict them to just posty-y tags
2825
-                        if (! current_user_can('unfiltered_html')) {
2825
+                        if ( ! current_user_can('unfiltered_html')) {
2826 2826
                             if (is_array($set_column_values['MTP_content'])) {
2827 2827
                                 foreach ($set_column_values['MTP_content'] as $key => $value) {
2828 2828
                                     // remove slashes so wp_kses works properly (its wp_kses_stripslashes() function
@@ -2830,7 +2830,7 @@  discard block
 block discarded – undo
2830 2830
                                     // appear invalid.) But currently the models expect slashed data, so after wp_kses
2831 2831
                                     // runs we need to re-slash the data. Sheesh. See
2832 2832
                                     // https://events.codebasehq.com/projects/event-espresso/tickets/11211#update-47321587
2833
-                                    $set_column_values['MTP_content'][ $key ] = addslashes(
2833
+                                    $set_column_values['MTP_content'][$key] = addslashes(
2834 2834
                                         wp_kses(
2835 2835
                                             stripslashes($value),
2836 2836
                                             wp_kses_allowed_html('post')
@@ -2866,14 +2866,14 @@  discard block
 block discarded – undo
2866 2866
                             }
2867 2867
                         } else {
2868 2868
                             // only do this logic if we don't have a MTP_ID for this field
2869
-                            if (empty($this->_req_data['MTP_template_fields'][ $template_field ]['MTP_ID'])) {
2869
+                            if (empty($this->_req_data['MTP_template_fields'][$template_field]['MTP_ID'])) {
2870 2870
                                 // this has already been through the template field validator and sanitized, so it will be
2871 2871
                                 // safe to insert this field.  Why insert?  This typically happens when we introduce a new
2872 2872
                                 // message template field in a messenger/message type and existing users don't have the
2873 2873
                                 // default setup for it.
2874 2874
                                 // @link https://events.codebasehq.com/projects/event-espresso/tickets/9465
2875 2875
                                 $updated = $MTP->insert($message_template_fields);
2876
-                                if (! $updated || is_wp_error($updated)) {
2876
+                                if ( ! $updated || is_wp_error($updated)) {
2877 2877
                                     EE_Error::add_error(
2878 2878
                                         sprintf(
2879 2879
                                             esc_html__('%s field could not be updated.', 'event_espresso'),
@@ -3032,7 +3032,7 @@  discard block
 block discarded – undo
3032 3032
             $messenger,
3033 3033
             $message_type
3034 3034
         )) {
3035
-            if ( EEM_Event::instance()->count() > 0 ) {
3035
+            if (EEM_Event::instance()->count() > 0) {
3036 3036
                 $success = $this->_preview_message(true);
3037 3037
                 if ($success) {
3038 3038
                     EE_Error::add_success(__('Test message sent', 'event_espresso'));
@@ -3104,7 +3104,7 @@  discard block
 block discarded – undo
3104 3104
         // incoming GRP_IDs
3105 3105
         if ($all) {
3106 3106
             // Checkboxes
3107
-            if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3107
+            if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3108 3108
                 // if array has more than one element then success message should be plural.
3109 3109
                 // todo: what about nonce?
3110 3110
                 $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
@@ -3112,16 +3112,16 @@  discard block
 block discarded – undo
3112 3112
                 // cycle through checkboxes
3113 3113
                 while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) {
3114 3114
                     $trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID);
3115
-                    if (! $trashed_or_restored) {
3115
+                    if ( ! $trashed_or_restored) {
3116 3116
                         $success = 0;
3117 3117
                     }
3118 3118
                 }
3119 3119
             } else {
3120 3120
                 // grab single GRP_ID and handle
3121 3121
                 $GRP_ID = isset($this->_req_data['id']) ? absint($this->_req_data['id']) : 0;
3122
-                if (! empty($GRP_ID)) {
3122
+                if ( ! empty($GRP_ID)) {
3123 3123
                     $trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID);
3124
-                    if (! $trashed_or_restored) {
3124
+                    if ( ! $trashed_or_restored) {
3125 3125
                         $success = 0;
3126 3126
                     }
3127 3127
                 } else {
@@ -3169,7 +3169,7 @@  discard block
 block discarded – undo
3169 3169
         do_action('AHEE_log', __FILE__, __FUNCTION__, '');
3170 3170
 
3171 3171
         // checkboxes
3172
-        if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3172
+        if ( ! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3173 3173
             // if array has more than one element then success message should be plural
3174 3174
             $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
3175 3175
 
@@ -3276,7 +3276,7 @@  discard block
 block discarded – undo
3276 3276
     protected function _set_m_mt_settings()
3277 3277
     {
3278 3278
         // first if this is already set then lets get out no need to regenerate data.
3279
-        if (! empty($this->_m_mt_settings)) {
3279
+        if ( ! empty($this->_m_mt_settings)) {
3280 3280
             return;
3281 3281
         }
3282 3282
 
@@ -3290,7 +3290,7 @@  discard block
 block discarded – undo
3290 3290
         // assemble the array for the _tab_text_links helper
3291 3291
 
3292 3292
         foreach ($messengers as $messenger) {
3293
-            $this->_m_mt_settings['messenger_tabs'][ $messenger->name ] = array(
3293
+            $this->_m_mt_settings['messenger_tabs'][$messenger->name] = array(
3294 3294
                 'label' => ucwords($messenger->label['singular']),
3295 3295
                 'class' => $this->_message_resource_manager->is_messenger_active($messenger->name)
3296 3296
                     ? 'messenger-active'
@@ -3307,7 +3307,7 @@  discard block
 block discarded – undo
3307 3307
             foreach ($message_types as $message_type) {
3308 3308
                 // first we need to verify that this message type is valid with this messenger. Cause if it isn't then
3309 3309
                 // it shouldn't show in either the inactive OR active metabox.
3310
-                if (! in_array($message_type->name, $message_types_for_messenger, true)) {
3310
+                if ( ! in_array($message_type->name, $message_types_for_messenger, true)) {
3311 3311
                     continue;
3312 3312
                 }
3313 3313
 
@@ -3318,12 +3318,12 @@  discard block
 block discarded – undo
3318 3318
                     ? 'active'
3319 3319
                     : 'inactive';
3320 3320
 
3321
-                $this->_m_mt_settings['message_type_tabs'][ $messenger->name ][ $a_or_i ][ $message_type->name ] = array(
3321
+                $this->_m_mt_settings['message_type_tabs'][$messenger->name][$a_or_i][$message_type->name] = array(
3322 3322
                     'label'    => ucwords($message_type->label['singular']),
3323
-                    'class'    => 'message-type-' . $a_or_i,
3324
-                    'slug_id'  => $message_type->name . '-messagetype-' . $messenger->name,
3325
-                    'mt_nonce' => wp_create_nonce($message_type->name . '_nonce'),
3326
-                    'href'     => 'espresso_' . $message_type->name . '_message_type_settings',
3323
+                    'class'    => 'message-type-'.$a_or_i,
3324
+                    'slug_id'  => $message_type->name.'-messagetype-'.$messenger->name,
3325
+                    'mt_nonce' => wp_create_nonce($message_type->name.'_nonce'),
3326
+                    'href'     => 'espresso_'.$message_type->name.'_message_type_settings',
3327 3327
                     'title'    => $a_or_i === 'active'
3328 3328
                         ? esc_html__('Drag this message type to the Inactive window to deactivate', 'event_espresso')
3329 3329
                         : esc_html__('Drag this message type to the messenger to activate', 'event_espresso'),
@@ -3354,25 +3354,25 @@  discard block
 block discarded – undo
3354 3354
         $fields = $message_type->get_admin_settings_fields();
3355 3355
         $settings_template_args['template_form_fields'] = '';
3356 3356
 
3357
-        if (! empty($fields) && $active) {
3357
+        if ( ! empty($fields) && $active) {
3358 3358
             $existing_settings = $message_type->get_existing_admin_settings($messenger->name);
3359 3359
             foreach ($fields as $fldname => $fldprops) {
3360
-                $field_id = $messenger->name . '-' . $message_type->name . '-' . $fldname;
3361
-                $template_form_field[ $field_id ] = array(
3362
-                    'name'       => 'message_type_settings[' . $fldname . ']',
3360
+                $field_id = $messenger->name.'-'.$message_type->name.'-'.$fldname;
3361
+                $template_form_field[$field_id] = array(
3362
+                    'name'       => 'message_type_settings['.$fldname.']',
3363 3363
                     'label'      => $fldprops['label'],
3364 3364
                     'input'      => $fldprops['field_type'],
3365 3365
                     'type'       => $fldprops['value_type'],
3366 3366
                     'required'   => $fldprops['required'],
3367 3367
                     'validation' => $fldprops['validation'],
3368
-                    'value'      => isset($existing_settings[ $fldname ])
3369
-                        ? $existing_settings[ $fldname ]
3368
+                    'value'      => isset($existing_settings[$fldname])
3369
+                        ? $existing_settings[$fldname]
3370 3370
                         : $fldprops['default'],
3371 3371
                     'options'    => isset($fldprops['options'])
3372 3372
                         ? $fldprops['options']
3373 3373
                         : array(),
3374
-                    'default'    => isset($existing_settings[ $fldname ])
3375
-                        ? $existing_settings[ $fldname ]
3374
+                    'default'    => isset($existing_settings[$fldname])
3375
+                        ? $existing_settings[$fldname]
3376 3376
                         : $fldprops['default'],
3377 3377
                     'css_class'  => 'no-drag',
3378 3378
                     'format'     => $fldprops['format'],
@@ -3415,7 +3415,7 @@  discard block
 block discarded – undo
3415 3415
             : '';
3416 3416
 
3417 3417
 
3418
-        $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_mt_settings_content.template.php';
3418
+        $template = EE_MSG_TEMPLATE_PATH.'ee_msg_mt_settings_content.template.php';
3419 3419
         $content = EEH_Template::display_template($template, $settings_template_args, true);
3420 3420
 
3421 3421
         return $content;
@@ -3446,20 +3446,20 @@  discard block
 block discarded – undo
3446 3446
                 // messenger meta boxes
3447 3447
                 $active = $selected_messenger === $messenger;
3448 3448
                 $active_mt_tabs = isset(
3449
-                    $this->_m_mt_settings['message_type_tabs'][ $messenger ]['active']
3449
+                    $this->_m_mt_settings['message_type_tabs'][$messenger]['active']
3450 3450
                 )
3451
-                    ? $this->_m_mt_settings['message_type_tabs'][ $messenger ]['active']
3451
+                    ? $this->_m_mt_settings['message_type_tabs'][$messenger]['active']
3452 3452
                     : '';
3453
-                $m_boxes[ $messenger . '_a_box' ] = sprintf(
3453
+                $m_boxes[$messenger.'_a_box'] = sprintf(
3454 3454
                     esc_html__('%s Settings', 'event_espresso'),
3455 3455
                     $tab_array['label']
3456 3456
                 );
3457
-                $m_template_args[ $messenger . '_a_box' ] = array(
3457
+                $m_template_args[$messenger.'_a_box'] = array(
3458 3458
                     'active_message_types'   => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '',
3459 3459
                     'inactive_message_types' => isset(
3460
-                        $this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive']
3460
+                        $this->_m_mt_settings['message_type_tabs'][$messenger]['inactive']
3461 3461
                     )
3462
-                        ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive'])
3462
+                        ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive'])
3463 3463
                         : '',
3464 3464
                     'content'                => $this->_get_messenger_box_content($tab_array['obj']),
3465 3465
                     'hidden'                 => $active ? '' : ' hidden',
@@ -3470,13 +3470,13 @@  discard block
 block discarded – undo
3470 3470
                 // message type meta boxes
3471 3471
                 // (which is really just the inactive container for each messenger
3472 3472
                 // showing inactive message types for that messenger)
3473
-                $mt_boxes[ $messenger . '_i_box' ] = esc_html__('Inactive Message Types', 'event_espresso');
3474
-                $mt_template_args[ $messenger . '_i_box' ] = array(
3473
+                $mt_boxes[$messenger.'_i_box'] = esc_html__('Inactive Message Types', 'event_espresso');
3474
+                $mt_template_args[$messenger.'_i_box'] = array(
3475 3475
                     'active_message_types'   => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '',
3476 3476
                     'inactive_message_types' => isset(
3477
-                        $this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive']
3477
+                        $this->_m_mt_settings['message_type_tabs'][$messenger]['inactive']
3478 3478
                     )
3479
-                        ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive'])
3479
+                        ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][$messenger]['inactive'])
3480 3480
                         : '',
3481 3481
                     'hidden'                 => $active ? '' : ' hidden',
3482 3482
                     'hide_on_message'        => $hide_on_message,
@@ -3489,14 +3489,14 @@  discard block
 block discarded – undo
3489 3489
 
3490 3490
 
3491 3491
         // register messenger metaboxes
3492
-        $m_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_mt_meta_box.template.php';
3492
+        $m_template_path = EE_MSG_TEMPLATE_PATH.'ee_msg_details_messenger_mt_meta_box.template.php';
3493 3493
         foreach ($m_boxes as $box => $label) {
3494
-            $callback_args = array('template_path' => $m_template_path, 'template_args' => $m_template_args[ $box ]);
3494
+            $callback_args = array('template_path' => $m_template_path, 'template_args' => $m_template_args[$box]);
3495 3495
             $msgr = str_replace('_a_box', '', $box);
3496 3496
             add_meta_box(
3497
-                'espresso_' . $msgr . '_settings',
3497
+                'espresso_'.$msgr.'_settings',
3498 3498
                 $label,
3499
-                function ($post, $metabox) {
3499
+                function($post, $metabox) {
3500 3500
                     echo EEH_Template::display_template(
3501 3501
                         $metabox["args"]["template_path"],
3502 3502
                         $metabox["args"]["template_args"],
@@ -3511,17 +3511,17 @@  discard block
 block discarded – undo
3511 3511
         }
3512 3512
 
3513 3513
         // register message type metaboxes
3514
-        $mt_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_meta_box.template.php';
3514
+        $mt_template_path = EE_MSG_TEMPLATE_PATH.'ee_msg_details_messenger_meta_box.template.php';
3515 3515
         foreach ($mt_boxes as $box => $label) {
3516 3516
             $callback_args = array(
3517 3517
                 'template_path' => $mt_template_path,
3518
-                'template_args' => $mt_template_args[ $box ],
3518
+                'template_args' => $mt_template_args[$box],
3519 3519
             );
3520 3520
             $mt = str_replace('_i_box', '', $box);
3521 3521
             add_meta_box(
3522
-                'espresso_' . $mt . '_inactive_mts',
3522
+                'espresso_'.$mt.'_inactive_mts',
3523 3523
                 $label,
3524
-                function ($post, $metabox) {
3524
+                function($post, $metabox) {
3525 3525
                     echo EEH_Template::display_template(
3526 3526
                         $metabox["args"]["template_path"],
3527 3527
                         $metabox["args"]["template_args"],
@@ -3667,7 +3667,7 @@  discard block
 block discarded – undo
3667 3667
             if ($form->is_valid()) {
3668 3668
                 $valid_data = $form->valid_data();
3669 3669
                 foreach ($valid_data as $property => $value) {
3670
-                    $setter = 'set_' . $property;
3670
+                    $setter = 'set_'.$property;
3671 3671
                     if (method_exists($network_config, $setter)) {
3672 3672
                         $network_config->{$setter}($value);
3673 3673
                     } elseif (property_exists($network_config, $property)
@@ -3701,7 +3701,7 @@  discard block
 block discarded – undo
3701 3701
     protected function _get_mt_tabs($tab_array)
3702 3702
     {
3703 3703
         $tab_array = (array) $tab_array;
3704
-        $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_mt_settings_tab_item.template.php';
3704
+        $template = EE_MSG_TEMPLATE_PATH.'ee_msg_details_mt_settings_tab_item.template.php';
3705 3705
         $tabs = '';
3706 3706
 
3707 3707
         foreach ($tab_array as $tab) {
@@ -3729,20 +3729,20 @@  discard block
 block discarded – undo
3729 3729
         $settings_template_args['active'] = $this->_message_resource_manager->is_messenger_active($messenger->name);
3730 3730
 
3731 3731
 
3732
-        if (! empty($fields)) {
3732
+        if ( ! empty($fields)) {
3733 3733
             $existing_settings = $messenger->get_existing_admin_settings();
3734 3734
 
3735 3735
             foreach ($fields as $fldname => $fldprops) {
3736
-                $field_id = $messenger->name . '-' . $fldname;
3737
-                $template_form_field[ $field_id ] = array(
3738
-                    'name'       => 'messenger_settings[' . $field_id . ']',
3736
+                $field_id = $messenger->name.'-'.$fldname;
3737
+                $template_form_field[$field_id] = array(
3738
+                    'name'       => 'messenger_settings['.$field_id.']',
3739 3739
                     'label'      => $fldprops['label'],
3740 3740
                     'input'      => $fldprops['field_type'],
3741 3741
                     'type'       => $fldprops['value_type'],
3742 3742
                     'required'   => $fldprops['required'],
3743 3743
                     'validation' => $fldprops['validation'],
3744
-                    'value'      => isset($existing_settings[ $field_id ])
3745
-                        ? $existing_settings[ $field_id ]
3744
+                    'value'      => isset($existing_settings[$field_id])
3745
+                        ? $existing_settings[$field_id]
3746 3746
                         : $fldprops['default'],
3747 3747
                     'css_class'  => '',
3748 3748
                     'format'     => $fldprops['format'],
@@ -3768,9 +3768,9 @@  discard block
 block discarded – undo
3768 3768
         );
3769 3769
 
3770 3770
         // make sure any active message types that are existing are included in the hidden fields
3771
-        if (isset($this->_m_mt_settings['message_type_tabs'][ $messenger->name ]['active'])) {
3772
-            foreach ($this->_m_mt_settings['message_type_tabs'][ $messenger->name ]['active'] as $mt => $values) {
3773
-                $settings_template_args['hidden_fields'][ 'messenger_settings[message_types][' . $mt . ']' ] = array(
3771
+        if (isset($this->_m_mt_settings['message_type_tabs'][$messenger->name]['active'])) {
3772
+            foreach ($this->_m_mt_settings['message_type_tabs'][$messenger->name]['active'] as $mt => $values) {
3773
+                $settings_template_args['hidden_fields']['messenger_settings[message_types]['.$mt.']'] = array(
3774 3774
                     'type'  => 'hidden',
3775 3775
                     'value' => $mt,
3776 3776
                 );
@@ -3799,9 +3799,9 @@  discard block
 block discarded – undo
3799 3799
 
3800 3800
 
3801 3801
         $settings_template_args['on_off_action'] = $active ? 'messenger-off' : 'messenger-on';
3802
-        $settings_template_args['nonce'] = wp_create_nonce('activate_' . $messenger->name . '_toggle_nonce');
3802
+        $settings_template_args['nonce'] = wp_create_nonce('activate_'.$messenger->name.'_toggle_nonce');
3803 3803
         $settings_template_args['on_off_status'] = $active ? true : false;
3804
-        $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_m_settings_content.template.php';
3804
+        $template = EE_MSG_TEMPLATE_PATH.'ee_msg_m_settings_content.template.php';
3805 3805
         $content = EEH_Template::display_template(
3806 3806
             $template,
3807 3807
             $settings_template_args,
@@ -3827,7 +3827,7 @@  discard block
 block discarded – undo
3827 3827
         $success = true;
3828 3828
         $this->_prep_default_response_for_messenger_or_message_type_toggle();
3829 3829
         // let's check that we have required data
3830
-        if (! isset($this->_req_data['messenger'])) {
3830
+        if ( ! isset($this->_req_data['messenger'])) {
3831 3831
             EE_Error::add_error(
3832 3832
                 esc_html__('Messenger name needed to toggle activation. None given', 'event_espresso'),
3833 3833
                 __FILE__,
@@ -3841,12 +3841,12 @@  discard block
 block discarded – undo
3841 3841
         $nonce = isset($this->_req_data['activate_nonce'])
3842 3842
             ? sanitize_text_field($this->_req_data['activate_nonce'])
3843 3843
             : '';
3844
-        $nonce_ref = 'activate_' . $this->_req_data['messenger'] . '_toggle_nonce';
3844
+        $nonce_ref = 'activate_'.$this->_req_data['messenger'].'_toggle_nonce';
3845 3845
 
3846 3846
         $this->_verify_nonce($nonce, $nonce_ref);
3847 3847
 
3848 3848
 
3849
-        if (! isset($this->_req_data['status'])) {
3849
+        if ( ! isset($this->_req_data['status'])) {
3850 3850
             EE_Error::add_error(
3851 3851
                 esc_html__(
3852 3852
                     'Messenger status needed to know whether activation or deactivation is happening. No status is given',
@@ -3905,7 +3905,7 @@  discard block
 block discarded – undo
3905 3905
         $this->_prep_default_response_for_messenger_or_message_type_toggle();
3906 3906
 
3907 3907
         // let's make sure we have the necessary data
3908
-        if (! isset($this->_req_data['message_type'])) {
3908
+        if ( ! isset($this->_req_data['message_type'])) {
3909 3909
             EE_Error::add_error(
3910 3910
                 esc_html__('Message Type name needed to toggle activation. None given', 'event_espresso'),
3911 3911
                 __FILE__,
@@ -3915,7 +3915,7 @@  discard block
 block discarded – undo
3915 3915
             $success = false;
3916 3916
         }
3917 3917
 
3918
-        if (! isset($this->_req_data['messenger'])) {
3918
+        if ( ! isset($this->_req_data['messenger'])) {
3919 3919
             EE_Error::add_error(
3920 3920
                 esc_html__('Messenger name needed to toggle activation. None given', 'event_espresso'),
3921 3921
                 __FILE__,
@@ -3925,7 +3925,7 @@  discard block
 block discarded – undo
3925 3925
             $success = false;
3926 3926
         }
3927 3927
 
3928
-        if (! isset($this->_req_data['status'])) {
3928
+        if ( ! isset($this->_req_data['status'])) {
3929 3929
             EE_Error::add_error(
3930 3930
                 esc_html__(
3931 3931
                     'Messenger status needed to know whether activation or deactivation is happening. No status is given',
@@ -3958,7 +3958,7 @@  discard block
 block discarded – undo
3958 3958
 
3959 3959
         // do a nonce check here since we're not arriving via a normal route
3960 3960
         $nonce = isset($this->_req_data['mt_nonce']) ? sanitize_text_field($this->_req_data['mt_nonce']) : '';
3961
-        $nonce_ref = $this->_req_data['message_type'] . '_nonce';
3961
+        $nonce_ref = $this->_req_data['message_type'].'_nonce';
3962 3962
 
3963 3963
         $this->_verify_nonce($nonce, $nonce_ref);
3964 3964
 
@@ -4145,7 +4145,7 @@  discard block
 block discarded – undo
4145 4145
         EE_Message_Type $message_type = null
4146 4146
     ) {
4147 4147
         // if $messenger isn't a valid messenger object then get out.
4148
-        if (! $messenger instanceof EE_Messenger) {
4148
+        if ( ! $messenger instanceof EE_Messenger) {
4149 4149
             EE_Error::add_error(
4150 4150
                 esc_html__('The messenger being activated is not a valid messenger', 'event_espresso'),
4151 4151
                 __FILE__,
@@ -4200,7 +4200,7 @@  discard block
 block discarded – undo
4200 4200
             // message types after the activation process.  However its possible some messengers don't HAVE any default_message_types
4201 4201
             // in which case we just give a success message for the messenger being successfully activated.
4202 4202
         } else {
4203
-            if (! $messenger->get_default_message_types()) {
4203
+            if ( ! $messenger->get_default_message_types()) {
4204 4204
                 // messenger doesn't have any default message types so still a success.
4205 4205
                 EE_Error::add_success(
4206 4206
                     sprintf(
@@ -4256,7 +4256,7 @@  discard block
 block discarded – undo
4256 4256
         EE_Error::overwrite_success();
4257 4257
 
4258 4258
         // if $messenger isn't a valid messenger object then get out.
4259
-        if (! $messenger instanceof EE_Messenger) {
4259
+        if ( ! $messenger instanceof EE_Messenger) {
4260 4260
             EE_Error::add_error(
4261 4261
                 esc_html__('The messenger being deactivated is not a valid messenger', 'event_espresso'),
4262 4262
                 __FILE__,
@@ -4318,7 +4318,7 @@  discard block
 block discarded – undo
4318 4318
      */
4319 4319
     public function update_mt_form()
4320 4320
     {
4321
-        if (! isset($this->_req_data['messenger']) || ! isset($this->_req_data['message_type'])) {
4321
+        if ( ! isset($this->_req_data['messenger']) || ! isset($this->_req_data['message_type'])) {
4322 4322
             EE_Error::add_error(
4323 4323
                 esc_html__('Require message type or messenger to send an updated form', 'event_espresso'),
4324 4324
                 __FILE__,
@@ -4330,7 +4330,7 @@  discard block
 block discarded – undo
4330 4330
 
4331 4331
         $message_types = $this->get_installed_message_types();
4332 4332
 
4333
-        $message_type = $message_types[ $this->_req_data['message_type'] ];
4333
+        $message_type = $message_types[$this->_req_data['message_type']];
4334 4334
         $messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']);
4335 4335
 
4336 4336
         $content = $this->_message_type_settings_content(
@@ -4350,7 +4350,7 @@  discard block
 block discarded – undo
4350 4350
      */
4351 4351
     public function save_settings()
4352 4352
     {
4353
-        if (! isset($this->_req_data['type'])) {
4353
+        if ( ! isset($this->_req_data['type'])) {
4354 4354
             EE_Error::add_error(
4355 4355
                 esc_html__(
4356 4356
                     'Cannot save settings because type is unknown (messenger settings or messsage type settings?)',
@@ -4379,7 +4379,7 @@  discard block
 block discarded – undo
4379 4379
                         unset($settings['message_types']);
4380 4380
                         break;
4381 4381
                     default:
4382
-                        $settings[ $key ] = $value;
4382
+                        $settings[$key] = $value;
4383 4383
                         break;
4384 4384
                 }
4385 4385
             }
@@ -4398,7 +4398,7 @@  discard block
 block discarded – undo
4398 4398
                         unset($settings['message_type']);
4399 4399
                         break;
4400 4400
                     default:
4401
-                        $settings[ $key ] = $value;
4401
+                        $settings[$key] = $value;
4402 4402
                         break;
4403 4403
                 }
4404 4404
             }
@@ -4559,7 +4559,7 @@  discard block
 block discarded – undo
4559 4559
      */
4560 4560
     protected function _get_msg_ids_from_request()
4561 4561
     {
4562
-        if (! isset($this->_req_data['MSG_ID'])) {
4562
+        if ( ! isset($this->_req_data['MSG_ID'])) {
4563 4563
             return array();
4564 4564
         }
4565 4565
 
Please login to merge, or discard this patch.