Completed
Branch FET/reg-form-builder/main (e2c78f)
by
unknown
15:46 queued 13:27
created
core/domain/services/registration/form/v1/CopyAttendeeInfoForm.php 2 patches
Indentation   +70 added lines, -70 removed lines patch added patch discarded remove patch
@@ -12,77 +12,77 @@
 block discarded – undo
12 12
 
13 13
 class CopyAttendeeInfoForm extends EE_Form_Section_Proper
14 14
 {
15
-    /**
16
-     * CopyAttendeeInfoForm constructor.
17
-     *
18
-     * @param EE_Registration[] $registrations
19
-     * @param string            $slug
20
-     * @throws EE_Error
21
-     * @throws ReflectionException
22
-     */
23
-    public function __construct(array $registrations, string $slug)
24
-    {
25
-        parent::__construct(
26
-            [
27
-                'subsections'     => $this->copyAttendeeInfoInputs($registrations),
28
-                'layout_strategy' => new EE_Template_Layout(
29
-                    [
30
-                        'layout_template_file'     => SPCO_REG_STEPS_PATH
31
-                                                      . $slug
32
-                                                      . '/copy_attendee_info.template.php',
33
-                        'begin_template_file'      => null,
34
-                        'input_template_file'      => null,
35
-                        'subsection_template_file' => null,
36
-                        'end_template_file'        => null,
37
-                    ]
38
-                ),
39
-            ]
40
-        );
41
-    }
15
+	/**
16
+	 * CopyAttendeeInfoForm constructor.
17
+	 *
18
+	 * @param EE_Registration[] $registrations
19
+	 * @param string            $slug
20
+	 * @throws EE_Error
21
+	 * @throws ReflectionException
22
+	 */
23
+	public function __construct(array $registrations, string $slug)
24
+	{
25
+		parent::__construct(
26
+			[
27
+				'subsections'     => $this->copyAttendeeInfoInputs($registrations),
28
+				'layout_strategy' => new EE_Template_Layout(
29
+					[
30
+						'layout_template_file'     => SPCO_REG_STEPS_PATH
31
+													  . $slug
32
+													  . '/copy_attendee_info.template.php',
33
+						'begin_template_file'      => null,
34
+						'input_template_file'      => null,
35
+						'subsection_template_file' => null,
36
+						'end_template_file'        => null,
37
+					]
38
+				),
39
+			]
40
+		);
41
+	}
42 42
 
43 43
 
44
-    /**
45
-     * @param array $registrations
46
-     * @return array
47
-     * @throws EE_Error
48
-     * @throws ReflectionException
49
-     */
50
-    private function copyAttendeeInfoInputs(array $registrations): array
51
-    {
52
-        $copy_attendee_info_inputs = [];
53
-        $prev_ticket               = null;
54
-        foreach ($registrations as $registration) {
55
-            // for all  attendees other than the primary attendee
56
-            if ($registration instanceof EE_Registration && ! $registration->is_primary_registrant()) {
57
-                // if this is a new ticket OR if this is the very first additional attendee after the primary attendee
58
-                if ($registration->ticket()->ID() !== $prev_ticket) {
59
-                    $item_name   = $registration->ticket()->name();
60
-                    $item_name   .= $registration->ticket()->description() !== ''
61
-                        ? ' - ' . $registration->ticket()->description()
62
-                        : '';
63
-                    $copy_attendee_info_inputs[ 'spco_copy_attendee_chk[ticket-' . $registration->ticket()->ID() . ']' ]
64
-                                 = new EE_Form_Section_HTML(
65
-                                     '<h6 class="spco-copy-attendee-event-hdr">' . $item_name . '</h6>'
66
-                                 );
67
-                    $prev_ticket = $registration->ticket()->ID();
68
-                }
44
+	/**
45
+	 * @param array $registrations
46
+	 * @return array
47
+	 * @throws EE_Error
48
+	 * @throws ReflectionException
49
+	 */
50
+	private function copyAttendeeInfoInputs(array $registrations): array
51
+	{
52
+		$copy_attendee_info_inputs = [];
53
+		$prev_ticket               = null;
54
+		foreach ($registrations as $registration) {
55
+			// for all  attendees other than the primary attendee
56
+			if ($registration instanceof EE_Registration && ! $registration->is_primary_registrant()) {
57
+				// if this is a new ticket OR if this is the very first additional attendee after the primary attendee
58
+				if ($registration->ticket()->ID() !== $prev_ticket) {
59
+					$item_name   = $registration->ticket()->name();
60
+					$item_name   .= $registration->ticket()->description() !== ''
61
+						? ' - ' . $registration->ticket()->description()
62
+						: '';
63
+					$copy_attendee_info_inputs[ 'spco_copy_attendee_chk[ticket-' . $registration->ticket()->ID() . ']' ]
64
+								 = new EE_Form_Section_HTML(
65
+									 '<h6 class="spco-copy-attendee-event-hdr">' . $item_name . '</h6>'
66
+								 );
67
+					$prev_ticket = $registration->ticket()->ID();
68
+				}
69 69
 
70
-                $copy_attendee_info_inputs[ 'spco_copy_attendee_chk[' . $registration->ID() . ']' ]
71
-                    = new EE_Checkbox_Multi_Input(
72
-                        [
73
-                        $registration->ID() => sprintf(
74
-                            esc_html_x('Attendee #%s', 'Attendee #123', 'event_espresso'),
75
-                            $registration->count()
76
-                        )
77
-                        ],
78
-                        [
79
-                            'html_id'                 => 'spco-copy-attendee-chk-' . $registration->reg_url_link(),
80
-                            'html_class'              => 'spco-copy-attendee-chk ee-do-not-validate',
81
-                            'display_html_label_text' => false,
82
-                        ]
83
-                    );
84
-            }
85
-        }
86
-        return $copy_attendee_info_inputs;
87
-    }
70
+				$copy_attendee_info_inputs[ 'spco_copy_attendee_chk[' . $registration->ID() . ']' ]
71
+					= new EE_Checkbox_Multi_Input(
72
+						[
73
+						$registration->ID() => sprintf(
74
+							esc_html_x('Attendee #%s', 'Attendee #123', 'event_espresso'),
75
+							$registration->count()
76
+						)
77
+						],
78
+						[
79
+							'html_id'                 => 'spco-copy-attendee-chk-' . $registration->reg_url_link(),
80
+							'html_class'              => 'spco-copy-attendee-chk ee-do-not-validate',
81
+							'display_html_label_text' => false,
82
+						]
83
+					);
84
+			}
85
+		}
86
+		return $copy_attendee_info_inputs;
87
+	}
88 88
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -56,18 +56,18 @@  discard block
 block discarded – undo
56 56
             if ($registration instanceof EE_Registration && ! $registration->is_primary_registrant()) {
57 57
                 // if this is a new ticket OR if this is the very first additional attendee after the primary attendee
58 58
                 if ($registration->ticket()->ID() !== $prev_ticket) {
59
-                    $item_name   = $registration->ticket()->name();
60
-                    $item_name   .= $registration->ticket()->description() !== ''
61
-                        ? ' - ' . $registration->ticket()->description()
59
+                    $item_name = $registration->ticket()->name();
60
+                    $item_name .= $registration->ticket()->description() !== ''
61
+                        ? ' - '.$registration->ticket()->description()
62 62
                         : '';
63
-                    $copy_attendee_info_inputs[ 'spco_copy_attendee_chk[ticket-' . $registration->ticket()->ID() . ']' ]
63
+                    $copy_attendee_info_inputs['spco_copy_attendee_chk[ticket-'.$registration->ticket()->ID().']']
64 64
                                  = new EE_Form_Section_HTML(
65
-                                     '<h6 class="spco-copy-attendee-event-hdr">' . $item_name . '</h6>'
65
+                                     '<h6 class="spco-copy-attendee-event-hdr">'.$item_name.'</h6>'
66 66
                                  );
67 67
                     $prev_ticket = $registration->ticket()->ID();
68 68
                 }
69 69
 
70
-                $copy_attendee_info_inputs[ 'spco_copy_attendee_chk[' . $registration->ID() . ']' ]
70
+                $copy_attendee_info_inputs['spco_copy_attendee_chk['.$registration->ID().']']
71 71
                     = new EE_Checkbox_Multi_Input(
72 72
                         [
73 73
                         $registration->ID() => sprintf(
@@ -76,7 +76,7 @@  discard block
 block discarded – undo
76 76
                         )
77 77
                         ],
78 78
                         [
79
-                            'html_id'                 => 'spco-copy-attendee-chk-' . $registration->reg_url_link(),
79
+                            'html_id'                 => 'spco-copy-attendee-chk-'.$registration->reg_url_link(),
80 80
                             'html_class'              => 'spco-copy-attendee-chk ee-do-not-validate',
81 81
                             'display_html_label_text' => false,
82 82
                         ]
Please login to merge, or discard this patch.
core/domain/services/registration/form/v1/RegForm.php 2 patches
Indentation   +220 added lines, -220 removed lines patch added patch discarded remove patch
@@ -32,251 +32,251 @@
 block discarded – undo
32 32
 class RegForm extends EE_Form_Section_Proper
33 33
 {
34 34
 
35
-    /**
36
-     * @var bool
37
-     */
38
-    private $print_copy_info = false;
35
+	/**
36
+	 * @var bool
37
+	 */
38
+	private $print_copy_info = false;
39 39
 
40
-    /**
41
-     * @var EE_Registration_Config
42
-     */
43
-    public $reg_config;
40
+	/**
41
+	 * @var EE_Registration_Config
42
+	 */
43
+	public $reg_config;
44 44
 
45
-    /**
46
-     * @var int
47
-     */
48
-    protected $reg_form_count = 0;
45
+	/**
46
+	 * @var int
47
+	 */
48
+	protected $reg_form_count = 0;
49 49
 
50
-    /**
51
-     * @var EE_SPCO_Reg_Step_Attendee_Information
52
-     */
53
-    public $reg_step;
50
+	/**
51
+	 * @var EE_SPCO_Reg_Step_Attendee_Information
52
+	 */
53
+	public $reg_step;
54 54
 
55
-    /**
56
-     * @var array
57
-     */
58
-    private $required_questions = [];
55
+	/**
56
+	 * @var array
57
+	 */
58
+	private $required_questions = [];
59 59
 
60
-    /**
61
-     * @var array
62
-     */
63
-    private $template_args = [];
60
+	/**
61
+	 * @var array
62
+	 */
63
+	private $template_args = [];
64 64
 
65 65
 
66
-    /**
67
-     * RegForm constructor.
68
-     *
69
-     * @param EE_SPCO_Reg_Step_Attendee_Information $reg_step
70
-     * @param EE_Registration_Config                $reg_config
71
-     * @throws ReflectionException
72
-     * @throws EE_Error
73
-     */
74
-    public function __construct(
75
-        EE_SPCO_Reg_Step_Attendee_Information $reg_step,
76
-        EE_Registration_Config $reg_config
77
-    ) {
78
-        $this->reg_step   = $reg_step;
79
-        $this->reg_config = $reg_config;
80
-        // setup some classes so that they are ready for loading during construction of other classes
81
-        LoaderFactory::getShared(CountryOptions::class, [$this->reg_step->checkout->action]);
82
-        LoaderFactory::getShared(StateOptions::class, [$this->reg_step->checkout->action]);
83
-        LoaderFactory::getShared(RegFormQuestionFactory::class, [[$this, 'addRequiredQuestion']]);
84
-        parent::__construct(
85
-            [
86
-                'name'            => $this->reg_step->reg_form_name(),
87
-                'html_id'         => $this->reg_step->reg_form_name(),
88
-                'subsections'     => $this->generateSubsections(),
89
-                'layout_strategy' => new EE_Template_Layout(
90
-                    [
91
-                        'layout_template_file' => $this->reg_step->template(), // layout_template
92
-                        'template_args'        => $this->template_args,
93
-                    ]
94
-                ),
95
-            ]
96
-        );
97
-    }
66
+	/**
67
+	 * RegForm constructor.
68
+	 *
69
+	 * @param EE_SPCO_Reg_Step_Attendee_Information $reg_step
70
+	 * @param EE_Registration_Config                $reg_config
71
+	 * @throws ReflectionException
72
+	 * @throws EE_Error
73
+	 */
74
+	public function __construct(
75
+		EE_SPCO_Reg_Step_Attendee_Information $reg_step,
76
+		EE_Registration_Config $reg_config
77
+	) {
78
+		$this->reg_step   = $reg_step;
79
+		$this->reg_config = $reg_config;
80
+		// setup some classes so that they are ready for loading during construction of other classes
81
+		LoaderFactory::getShared(CountryOptions::class, [$this->reg_step->checkout->action]);
82
+		LoaderFactory::getShared(StateOptions::class, [$this->reg_step->checkout->action]);
83
+		LoaderFactory::getShared(RegFormQuestionFactory::class, [[$this, 'addRequiredQuestion']]);
84
+		parent::__construct(
85
+			[
86
+				'name'            => $this->reg_step->reg_form_name(),
87
+				'html_id'         => $this->reg_step->reg_form_name(),
88
+				'subsections'     => $this->generateSubsections(),
89
+				'layout_strategy' => new EE_Template_Layout(
90
+					[
91
+						'layout_template_file' => $this->reg_step->template(), // layout_template
92
+						'template_args'        => $this->template_args,
93
+					]
94
+				),
95
+			]
96
+		);
97
+	}
98 98
 
99 99
 
100
-    /**
101
-     * @return void
102
-     */
103
-    public function enablePrintCopyInfo(): void
104
-    {
105
-        $this->print_copy_info = true;
106
-    }
100
+	/**
101
+	 * @return void
102
+	 */
103
+	public function enablePrintCopyInfo(): void
104
+	{
105
+		$this->print_copy_info = true;
106
+	}
107 107
 
108 108
 
109
-    /**
110
-     * @return bool
111
-     */
112
-    public function printCopyInfo(): bool
113
-    {
114
-        return $this->print_copy_info;
115
-    }
109
+	/**
110
+	 * @return bool
111
+	 */
112
+	public function printCopyInfo(): bool
113
+	{
114
+		return $this->print_copy_info;
115
+	}
116 116
 
117 117
 
118
-    /**
119
-     * @return int
120
-     */
121
-    public function regFormCount(): int
122
-    {
123
-        return $this->reg_form_count;
124
-    }
118
+	/**
119
+	 * @return int
120
+	 */
121
+	public function regFormCount(): int
122
+	{
123
+		return $this->reg_form_count;
124
+	}
125 125
 
126 126
 
127
-    /**
128
-     * @return array
129
-     */
130
-    public function requiredQuestions(): array
131
-    {
132
-        return $this->required_questions;
133
-    }
127
+	/**
128
+	 * @return array
129
+	 */
130
+	public function requiredQuestions(): array
131
+	{
132
+		return $this->required_questions;
133
+	}
134 134
 
135 135
 
136
-    /**
137
-     * @param string $identifier
138
-     * @param string $required_question
139
-     */
140
-    public function addRequiredQuestion(string $identifier, string $required_question): void
141
-    {
142
-        $this->required_questions[ $identifier ] = $required_question;
143
-    }
136
+	/**
137
+	 * @param string $identifier
138
+	 * @param string $required_question
139
+	 */
140
+	public function addRequiredQuestion(string $identifier, string $required_question): void
141
+	{
142
+		$this->required_questions[ $identifier ] = $required_question;
143
+	}
144 144
 
145 145
 
146
-    /**
147
-     * @return EE_Form_Section_Proper[]
148
-     * @throws DomainException
149
-     * @throws EE_Error
150
-     * @throws InvalidArgumentException
151
-     * @throws ReflectionException
152
-     * @throws EntityNotFoundException
153
-     * @throws InvalidDataTypeException
154
-     * @throws InvalidInterfaceException
155
-     */
156
-    private function generateSubsections(): array
157
-    {
158
-        // Init reg forms count.
159
-        $this->reg_form_count = 0;
146
+	/**
147
+	 * @return EE_Form_Section_Proper[]
148
+	 * @throws DomainException
149
+	 * @throws EE_Error
150
+	 * @throws InvalidArgumentException
151
+	 * @throws ReflectionException
152
+	 * @throws EntityNotFoundException
153
+	 * @throws InvalidDataTypeException
154
+	 * @throws InvalidInterfaceException
155
+	 */
156
+	private function generateSubsections(): array
157
+	{
158
+		// Init reg forms count.
159
+		$this->reg_form_count = 0;
160 160
 
161
-        $primary_registrant = null;
162
-        // autoload Line_Item_Display classes
163
-        EEH_Autoloader::register_line_item_display_autoloaders();
164
-        $Line_Item_Display = new EE_Line_Item_Display();
165
-        // calculate taxes
166
-        $Line_Item_Display->display_line_item(
167
-            $this->reg_step->checkout->cart->get_grand_total(),
168
-            ['set_tax_rate' => true]
169
-        );
170
-        $extra_inputs_section = $this->reg_step->reg_step_hidden_inputs();
171
-        $this->addPrivacyConsentCheckbox($extra_inputs_section);
172
-        $subsections = [
173
-            'default_hidden_inputs' => $extra_inputs_section,
174
-        ];
161
+		$primary_registrant = null;
162
+		// autoload Line_Item_Display classes
163
+		EEH_Autoloader::register_line_item_display_autoloaders();
164
+		$Line_Item_Display = new EE_Line_Item_Display();
165
+		// calculate taxes
166
+		$Line_Item_Display->display_line_item(
167
+			$this->reg_step->checkout->cart->get_grand_total(),
168
+			['set_tax_rate' => true]
169
+		);
170
+		$extra_inputs_section = $this->reg_step->reg_step_hidden_inputs();
171
+		$this->addPrivacyConsentCheckbox($extra_inputs_section);
172
+		$subsections = [
173
+			'default_hidden_inputs' => $extra_inputs_section,
174
+		];
175 175
 
176
-        $this->template_args = [
177
-            'revisit'       => $this->reg_step->checkout->revisit,
178
-            'registrations' => [],
179
-            'ticket_count'  => [],
180
-        ];
181
-        // grab the saved registrations from the transaction
182
-        $registrations = $this->reg_step->checkout->transaction->registrations(
183
-            $this->reg_step->checkout->reg_cache_where_params
184
-        );
185
-        if ($registrations) {
186
-            foreach ($registrations as $registration) {
187
-                // can this registration be processed during this visit ?
188
-                if (
189
-                    $registration instanceof EE_Registration
190
-                    && $this->reg_step->checkout->visit_allows_processing_of_this_registration($registration)
191
-                ) {
192
-                    $reg_url_link = $registration->reg_url_link();
193
-                    /** @var RegistrantForm $registrant_form */
194
-                    $registrant_form = LoaderFactory::getNew(
195
-                        RegistrantForm::class,
196
-                        [
197
-                            $registration,
198
-                            $this->reg_step->checkout->admin_request,
199
-                            $this->reg_config->copyAttendeeInfo(),
200
-                            [$this, 'enablePrintCopyInfo'],
201
-                        ]
202
-                    );
203
-                    // Increment the reg forms number if form is valid.
204
-                    if ($registrant_form->hasQuestions()) {
205
-                        $this->reg_form_count++;
206
-                        $subsections[ $reg_url_link ] = $registrant_form;
207
-                    } else {
208
-                        // or just add a blank section if there are no questions
209
-                        $subsections[ $reg_url_link ] = new EE_Form_Section_HTML();
210
-                    }
176
+		$this->template_args = [
177
+			'revisit'       => $this->reg_step->checkout->revisit,
178
+			'registrations' => [],
179
+			'ticket_count'  => [],
180
+		];
181
+		// grab the saved registrations from the transaction
182
+		$registrations = $this->reg_step->checkout->transaction->registrations(
183
+			$this->reg_step->checkout->reg_cache_where_params
184
+		);
185
+		if ($registrations) {
186
+			foreach ($registrations as $registration) {
187
+				// can this registration be processed during this visit ?
188
+				if (
189
+					$registration instanceof EE_Registration
190
+					&& $this->reg_step->checkout->visit_allows_processing_of_this_registration($registration)
191
+				) {
192
+					$reg_url_link = $registration->reg_url_link();
193
+					/** @var RegistrantForm $registrant_form */
194
+					$registrant_form = LoaderFactory::getNew(
195
+						RegistrantForm::class,
196
+						[
197
+							$registration,
198
+							$this->reg_step->checkout->admin_request,
199
+							$this->reg_config->copyAttendeeInfo(),
200
+							[$this, 'enablePrintCopyInfo'],
201
+						]
202
+					);
203
+					// Increment the reg forms number if form is valid.
204
+					if ($registrant_form->hasQuestions()) {
205
+						$this->reg_form_count++;
206
+						$subsections[ $reg_url_link ] = $registrant_form;
207
+					} else {
208
+						// or just add a blank section if there are no questions
209
+						$subsections[ $reg_url_link ] = new EE_Form_Section_HTML();
210
+					}
211 211
 
212
-                    $this->template_args['registrations'][ $reg_url_link ]                = $registration;
213
-                    $this->template_args['ticket_count'][ $registration->ticket()->ID() ] = isset(
214
-                        $this->template_args['ticket_count'][ $registration->ticket()->ID() ]
215
-                    )
216
-                        ? $this->template_args['ticket_count'][ $registration->ticket()->ID() ] + 1
217
-                        : 1;
218
-                    $ticket_line_item = EEH_Line_Item::get_line_items_by_object_type_and_IDs(
219
-                        $this->reg_step->checkout->cart->get_grand_total(),
220
-                        'Ticket',
221
-                        [$registration->ticket()->ID()]
222
-                    );
223
-                    $ticket_line_item = is_array($ticket_line_item)
224
-                        ? reset($ticket_line_item)
225
-                        : $ticket_line_item;
226
-                    $this->template_args['ticket_line_item'][ $registration->ticket()->ID() ] =
227
-                        $Line_Item_Display->display_line_item($ticket_line_item);
228
-                    if ($registration->is_primary_registrant()) {
229
-                        $primary_registrant = $reg_url_link;
230
-                    }
231
-                }
232
-            }
212
+					$this->template_args['registrations'][ $reg_url_link ]                = $registration;
213
+					$this->template_args['ticket_count'][ $registration->ticket()->ID() ] = isset(
214
+						$this->template_args['ticket_count'][ $registration->ticket()->ID() ]
215
+					)
216
+						? $this->template_args['ticket_count'][ $registration->ticket()->ID() ] + 1
217
+						: 1;
218
+					$ticket_line_item = EEH_Line_Item::get_line_items_by_object_type_and_IDs(
219
+						$this->reg_step->checkout->cart->get_grand_total(),
220
+						'Ticket',
221
+						[$registration->ticket()->ID()]
222
+					);
223
+					$ticket_line_item = is_array($ticket_line_item)
224
+						? reset($ticket_line_item)
225
+						: $ticket_line_item;
226
+					$this->template_args['ticket_line_item'][ $registration->ticket()->ID() ] =
227
+						$Line_Item_Display->display_line_item($ticket_line_item);
228
+					if ($registration->is_primary_registrant()) {
229
+						$primary_registrant = $reg_url_link;
230
+					}
231
+				}
232
+			}
233 233
 
234
-            if ($primary_registrant && count($registrations) > 1) {
235
-                if (
236
-                    isset($subsections[ $primary_registrant ])
237
-                    && $subsections[ $primary_registrant ] instanceof EE_Form_Section_Proper
238
-                ) {
239
-                    $copy_options['spco_copy_attendee_chk'] = $this->print_copy_info
240
-                        ? new CopyAttendeeInfoForm($registrations, $this->reg_step->slug())
241
-                        : new AutoCopyAttendeeInfoForm($this->reg_step->slug());
242
-                    $subsections[ $primary_registrant ]->add_subsections(
243
-                        $copy_options,
244
-                        'primary_registrant',
245
-                        false
246
-                    );
247
-                }
248
-            }
249
-        }
234
+			if ($primary_registrant && count($registrations) > 1) {
235
+				if (
236
+					isset($subsections[ $primary_registrant ])
237
+					&& $subsections[ $primary_registrant ] instanceof EE_Form_Section_Proper
238
+				) {
239
+					$copy_options['spco_copy_attendee_chk'] = $this->print_copy_info
240
+						? new CopyAttendeeInfoForm($registrations, $this->reg_step->slug())
241
+						: new AutoCopyAttendeeInfoForm($this->reg_step->slug());
242
+					$subsections[ $primary_registrant ]->add_subsections(
243
+						$copy_options,
244
+						'primary_registrant',
245
+						false
246
+					);
247
+				}
248
+			}
249
+		}
250 250
 
251
-        // Set the registration form template (default: one form per ticket details table).
252
-        // We decide the template to used based on the number of forms.
253
-        $template = $this->reg_form_count > 1
254
-            ? SPCO_REG_STEPS_PATH . $this->reg_step->slug() . '/attendee_info_main.template.php'
255
-            : SPCO_REG_STEPS_PATH . $this->reg_step->slug() . '/attendee_info_single.template.php';
256
-        $this->reg_step->setTemplate($template);
251
+		// Set the registration form template (default: one form per ticket details table).
252
+		// We decide the template to used based on the number of forms.
253
+		$template = $this->reg_form_count > 1
254
+			? SPCO_REG_STEPS_PATH . $this->reg_step->slug() . '/attendee_info_main.template.php'
255
+			: SPCO_REG_STEPS_PATH . $this->reg_step->slug() . '/attendee_info_single.template.php';
256
+		$this->reg_step->setTemplate($template);
257 257
 
258
-        return $subsections;
259
-    }
258
+		return $subsections;
259
+	}
260 260
 
261 261
 
262
-    /**
263
-     * @param EE_Form_Section_Proper $extra_inputs_section
264
-     * @throws EE_Error
265
-     */
266
-    private function addPrivacyConsentCheckbox(EE_Form_Section_Proper $extra_inputs_section)
267
-    {
268
-        // if this isn't a revisit, and they have the privacy consent box enabled, add it
269
-        if (! $this->reg_step->checkout->revisit && $this->reg_config->isConsentCheckboxEnabled()) {
270
-            $extra_inputs_section->add_subsections(
271
-                [
272
-                    'consent_box' => new PrivacyConsentCheckboxForm(
273
-                        $this->reg_step->slug(),
274
-                        $this->reg_config->getConsentCheckboxLabelText()
275
-                    )
276
-                ],
277
-                null,
278
-                false
279
-            );
280
-        }
281
-    }
262
+	/**
263
+	 * @param EE_Form_Section_Proper $extra_inputs_section
264
+	 * @throws EE_Error
265
+	 */
266
+	private function addPrivacyConsentCheckbox(EE_Form_Section_Proper $extra_inputs_section)
267
+	{
268
+		// if this isn't a revisit, and they have the privacy consent box enabled, add it
269
+		if (! $this->reg_step->checkout->revisit && $this->reg_config->isConsentCheckboxEnabled()) {
270
+			$extra_inputs_section->add_subsections(
271
+				[
272
+					'consent_box' => new PrivacyConsentCheckboxForm(
273
+						$this->reg_step->slug(),
274
+						$this->reg_config->getConsentCheckboxLabelText()
275
+					)
276
+				],
277
+				null,
278
+				false
279
+			);
280
+		}
281
+	}
282 282
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
      */
140 140
     public function addRequiredQuestion(string $identifier, string $required_question): void
141 141
     {
142
-        $this->required_questions[ $identifier ] = $required_question;
142
+        $this->required_questions[$identifier] = $required_question;
143 143
     }
144 144
 
145 145
 
@@ -203,17 +203,17 @@  discard block
 block discarded – undo
203 203
                     // Increment the reg forms number if form is valid.
204 204
                     if ($registrant_form->hasQuestions()) {
205 205
                         $this->reg_form_count++;
206
-                        $subsections[ $reg_url_link ] = $registrant_form;
206
+                        $subsections[$reg_url_link] = $registrant_form;
207 207
                     } else {
208 208
                         // or just add a blank section if there are no questions
209
-                        $subsections[ $reg_url_link ] = new EE_Form_Section_HTML();
209
+                        $subsections[$reg_url_link] = new EE_Form_Section_HTML();
210 210
                     }
211 211
 
212
-                    $this->template_args['registrations'][ $reg_url_link ]                = $registration;
213
-                    $this->template_args['ticket_count'][ $registration->ticket()->ID() ] = isset(
214
-                        $this->template_args['ticket_count'][ $registration->ticket()->ID() ]
212
+                    $this->template_args['registrations'][$reg_url_link]                = $registration;
213
+                    $this->template_args['ticket_count'][$registration->ticket()->ID()] = isset(
214
+                        $this->template_args['ticket_count'][$registration->ticket()->ID()]
215 215
                     )
216
-                        ? $this->template_args['ticket_count'][ $registration->ticket()->ID() ] + 1
216
+                        ? $this->template_args['ticket_count'][$registration->ticket()->ID()] + 1
217 217
                         : 1;
218 218
                     $ticket_line_item = EEH_Line_Item::get_line_items_by_object_type_and_IDs(
219 219
                         $this->reg_step->checkout->cart->get_grand_total(),
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
                     $ticket_line_item = is_array($ticket_line_item)
224 224
                         ? reset($ticket_line_item)
225 225
                         : $ticket_line_item;
226
-                    $this->template_args['ticket_line_item'][ $registration->ticket()->ID() ] =
226
+                    $this->template_args['ticket_line_item'][$registration->ticket()->ID()] =
227 227
                         $Line_Item_Display->display_line_item($ticket_line_item);
228 228
                     if ($registration->is_primary_registrant()) {
229 229
                         $primary_registrant = $reg_url_link;
@@ -233,13 +233,13 @@  discard block
 block discarded – undo
233 233
 
234 234
             if ($primary_registrant && count($registrations) > 1) {
235 235
                 if (
236
-                    isset($subsections[ $primary_registrant ])
237
-                    && $subsections[ $primary_registrant ] instanceof EE_Form_Section_Proper
236
+                    isset($subsections[$primary_registrant])
237
+                    && $subsections[$primary_registrant] instanceof EE_Form_Section_Proper
238 238
                 ) {
239 239
                     $copy_options['spco_copy_attendee_chk'] = $this->print_copy_info
240 240
                         ? new CopyAttendeeInfoForm($registrations, $this->reg_step->slug())
241 241
                         : new AutoCopyAttendeeInfoForm($this->reg_step->slug());
242
-                    $subsections[ $primary_registrant ]->add_subsections(
242
+                    $subsections[$primary_registrant]->add_subsections(
243 243
                         $copy_options,
244 244
                         'primary_registrant',
245 245
                         false
@@ -251,8 +251,8 @@  discard block
 block discarded – undo
251 251
         // Set the registration form template (default: one form per ticket details table).
252 252
         // We decide the template to used based on the number of forms.
253 253
         $template = $this->reg_form_count > 1
254
-            ? SPCO_REG_STEPS_PATH . $this->reg_step->slug() . '/attendee_info_main.template.php'
255
-            : SPCO_REG_STEPS_PATH . $this->reg_step->slug() . '/attendee_info_single.template.php';
254
+            ? SPCO_REG_STEPS_PATH . $this->reg_step->slug().'/attendee_info_main.template.php'
255
+            : SPCO_REG_STEPS_PATH.$this->reg_step->slug().'/attendee_info_single.template.php';
256 256
         $this->reg_step->setTemplate($template);
257 257
 
258 258
         return $subsections;
@@ -266,7 +266,7 @@  discard block
 block discarded – undo
266 266
     private function addPrivacyConsentCheckbox(EE_Form_Section_Proper $extra_inputs_section)
267 267
     {
268 268
         // if this isn't a revisit, and they have the privacy consent box enabled, add it
269
-        if (! $this->reg_step->checkout->revisit && $this->reg_config->isConsentCheckboxEnabled()) {
269
+        if ( ! $this->reg_step->checkout->revisit && $this->reg_config->isConsentCheckboxEnabled()) {
270 270
             $extra_inputs_section->add_subsections(
271 271
                 [
272 272
                     'consent_box' => new PrivacyConsentCheckboxForm(
Please login to merge, or discard this patch.
core/domain/services/admin/events/editor/EventEditorGraphQLData.php 1 patch
Indentation   +143 added lines, -143 removed lines patch added patch discarded remove patch
@@ -21,147 +21,147 @@
 block discarded – undo
21 21
 class EventEditorGraphQLData
22 22
 {
23 23
 
24
-    /**
25
-     * @var Event
26
-     */
27
-    protected $event;
28
-
29
-    /**
30
-     * @var Datetimes
31
-     */
32
-    protected $datetimes;
33
-
34
-    /**
35
-     * @var Prices
36
-     */
37
-    protected $prices;
38
-
39
-    /**
40
-     * @var PriceTypes
41
-     */
42
-    protected $price_types;
43
-
44
-    /**
45
-     * @var Tickets
46
-     */
47
-    protected $tickets;
48
-
49
-    /**
50
-     * @var EventEntityRelations
51
-     */
52
-    protected $relations;
53
-
54
-    /**
55
-     * @var EventManagerData
56
-     */
57
-    protected $managers;
58
-
59
-    /**
60
-     * @var NewEventDefaultEntities
61
-     */
62
-    protected $default_entities;
63
-
64
-    /**
65
-     * @var TicketMeta
66
-     */
67
-    protected $ticket_meta;
68
-
69
-    /**
70
-     * @var FormBuilder
71
-     */
72
-    protected $form_builder;
73
-
74
-
75
-    /**
76
-     * EventEditorGraphQLData constructor.
77
-     *
78
-     * @param Datetimes               $datetimes
79
-     * @param Event                   $event
80
-     * @param Prices                  $prices
81
-     * @param PriceTypes              $price_types
82
-     * @param Tickets                 $tickets
83
-     * @param EventEntityRelations    $relations
84
-     * @param EventManagerData        $managers
85
-     * @param NewEventDefaultEntities $default_entities
86
-     * @param TicketMeta              $ticket_meta
87
-     * @param FormBuilder             $form_builder
88
-     */
89
-    public function __construct(
90
-        Datetimes $datetimes,
91
-        Event $event,
92
-        Prices $prices,
93
-        PriceTypes $price_types,
94
-        Tickets $tickets,
95
-        EventEntityRelations $relations,
96
-        EventManagerData $managers,
97
-        NewEventDefaultEntities $default_entities,
98
-        TicketMeta $ticket_meta,
99
-        FormBuilder $form_builder
100
-    ) {
101
-        $this->datetimes        = $datetimes;
102
-        $this->event            = $event;
103
-        $this->default_entities = $default_entities;
104
-        $this->prices           = $prices;
105
-        $this->price_types      = $price_types;
106
-        $this->managers         = $managers;
107
-        $this->relations        = $relations;
108
-        $this->tickets          = $tickets;
109
-        $this->ticket_meta      = $ticket_meta;
110
-        $this->form_builder     = $form_builder;
111
-    }
112
-
113
-
114
-    /**
115
-     * @param int $eventId
116
-     * @return array
117
-     * @throws EE_Error
118
-     * @throws ReflectionException
119
-     * @since $VID:$
120
-     */
121
-    public function getData(int $eventId)
122
-    {
123
-        $event = $this->event->getData(['id' => $eventId]);
124
-        $datetimes = $this->datetimes->getData(['eventId' => $eventId]);
125
-        $eventManagers = $this->managers ->getData($eventId);
126
-
127
-        // Avoid undefined variable warning in PHP >= 7.3
128
-        $tickets = null;
129
-        $prices  = null;
130
-
131
-        if (empty($datetimes['nodes']) || (isset($_REQUEST['action']) && $_REQUEST['action'] === 'create_new')) {
132
-            $this->default_entities->getData($eventId);
133
-            $datetimes = $this->datetimes->getData(['eventId' => $eventId]);
134
-        }
135
-
136
-        $tickets = $this->tickets->getData([
137
-            'eventId'               => $eventId,
138
-            'includeDefaultTickets' => true,
139
-        ]);
140
-
141
-        $prices = $this->prices->getData([
142
-            'eventId'                     => $eventId,
143
-            'includeDefaultTicketsPrices' => true,
144
-            'includeDefaultPrices'        => true,
145
-        ]);
146
-
147
-        $priceTypes = $this->price_types->getData();
148
-
149
-        $relations = $this->relations->getData($eventId);
150
-
151
-        $ticketMeta = $this->ticket_meta->getData($eventId);
152
-
153
-        $formBuilder = $this->form_builder->getData($eventId);
154
-
155
-        return compact(
156
-            'datetimes',
157
-            'event',
158
-            'eventManagers',
159
-            'formBuilder',
160
-            'prices',
161
-            'priceTypes',
162
-            'relations',
163
-            'tickets',
164
-            'ticketMeta'
165
-        );
166
-    }
24
+	/**
25
+	 * @var Event
26
+	 */
27
+	protected $event;
28
+
29
+	/**
30
+	 * @var Datetimes
31
+	 */
32
+	protected $datetimes;
33
+
34
+	/**
35
+	 * @var Prices
36
+	 */
37
+	protected $prices;
38
+
39
+	/**
40
+	 * @var PriceTypes
41
+	 */
42
+	protected $price_types;
43
+
44
+	/**
45
+	 * @var Tickets
46
+	 */
47
+	protected $tickets;
48
+
49
+	/**
50
+	 * @var EventEntityRelations
51
+	 */
52
+	protected $relations;
53
+
54
+	/**
55
+	 * @var EventManagerData
56
+	 */
57
+	protected $managers;
58
+
59
+	/**
60
+	 * @var NewEventDefaultEntities
61
+	 */
62
+	protected $default_entities;
63
+
64
+	/**
65
+	 * @var TicketMeta
66
+	 */
67
+	protected $ticket_meta;
68
+
69
+	/**
70
+	 * @var FormBuilder
71
+	 */
72
+	protected $form_builder;
73
+
74
+
75
+	/**
76
+	 * EventEditorGraphQLData constructor.
77
+	 *
78
+	 * @param Datetimes               $datetimes
79
+	 * @param Event                   $event
80
+	 * @param Prices                  $prices
81
+	 * @param PriceTypes              $price_types
82
+	 * @param Tickets                 $tickets
83
+	 * @param EventEntityRelations    $relations
84
+	 * @param EventManagerData        $managers
85
+	 * @param NewEventDefaultEntities $default_entities
86
+	 * @param TicketMeta              $ticket_meta
87
+	 * @param FormBuilder             $form_builder
88
+	 */
89
+	public function __construct(
90
+		Datetimes $datetimes,
91
+		Event $event,
92
+		Prices $prices,
93
+		PriceTypes $price_types,
94
+		Tickets $tickets,
95
+		EventEntityRelations $relations,
96
+		EventManagerData $managers,
97
+		NewEventDefaultEntities $default_entities,
98
+		TicketMeta $ticket_meta,
99
+		FormBuilder $form_builder
100
+	) {
101
+		$this->datetimes        = $datetimes;
102
+		$this->event            = $event;
103
+		$this->default_entities = $default_entities;
104
+		$this->prices           = $prices;
105
+		$this->price_types      = $price_types;
106
+		$this->managers         = $managers;
107
+		$this->relations        = $relations;
108
+		$this->tickets          = $tickets;
109
+		$this->ticket_meta      = $ticket_meta;
110
+		$this->form_builder     = $form_builder;
111
+	}
112
+
113
+
114
+	/**
115
+	 * @param int $eventId
116
+	 * @return array
117
+	 * @throws EE_Error
118
+	 * @throws ReflectionException
119
+	 * @since $VID:$
120
+	 */
121
+	public function getData(int $eventId)
122
+	{
123
+		$event = $this->event->getData(['id' => $eventId]);
124
+		$datetimes = $this->datetimes->getData(['eventId' => $eventId]);
125
+		$eventManagers = $this->managers ->getData($eventId);
126
+
127
+		// Avoid undefined variable warning in PHP >= 7.3
128
+		$tickets = null;
129
+		$prices  = null;
130
+
131
+		if (empty($datetimes['nodes']) || (isset($_REQUEST['action']) && $_REQUEST['action'] === 'create_new')) {
132
+			$this->default_entities->getData($eventId);
133
+			$datetimes = $this->datetimes->getData(['eventId' => $eventId]);
134
+		}
135
+
136
+		$tickets = $this->tickets->getData([
137
+			'eventId'               => $eventId,
138
+			'includeDefaultTickets' => true,
139
+		]);
140
+
141
+		$prices = $this->prices->getData([
142
+			'eventId'                     => $eventId,
143
+			'includeDefaultTicketsPrices' => true,
144
+			'includeDefaultPrices'        => true,
145
+		]);
146
+
147
+		$priceTypes = $this->price_types->getData();
148
+
149
+		$relations = $this->relations->getData($eventId);
150
+
151
+		$ticketMeta = $this->ticket_meta->getData($eventId);
152
+
153
+		$formBuilder = $this->form_builder->getData($eventId);
154
+
155
+		return compact(
156
+			'datetimes',
157
+			'event',
158
+			'eventManagers',
159
+			'formBuilder',
160
+			'prices',
161
+			'priceTypes',
162
+			'relations',
163
+			'tickets',
164
+			'ticketMeta'
165
+		);
166
+	}
167 167
 }
Please login to merge, or discard this patch.
core/domain/services/admin/events/editor/FormBuilder.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -37,7 +37,7 @@  discard block
 block discarded – undo
37 37
             $UUID = $section->UUID();
38 38
 
39 39
             // Avoid duplicates, if any
40
-            $sections[ $UUID ] = [
40
+            $sections[$UUID] = [
41 41
                 'id'         => $UUID,
42 42
                 'appliesTo'  => GQLUtils::formatEnumKey($section->appliesTo()),
43 43
                 'attributes' => $section->attributes()->toJson(),
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
             $UUID = $element->UUID();
61 61
 
62 62
             // Avoid duplicates
63
-            $elements[ $UUID ] = [
63
+            $elements[$UUID] = [
64 64
                 'id'         => $UUID,
65 65
                 'adminOnly'  => $element->adminOnly(),
66 66
                 'attributes' => $element->attributes()->toJson(),
Please login to merge, or discard this patch.
Indentation   +70 added lines, -70 removed lines patch added patch discarded remove patch
@@ -23,81 +23,81 @@
 block discarded – undo
23 23
 {
24 24
 
25 25
 
26
-    /**
27
-     * @param int $eventId
28
-     * @return array
29
-     */
30
-    public function getData(int $eventId): array
31
-    {
32
-        /** @var EE_Event */
33
-        $event = EEM_Event::instance()->get_one_by_ID($eventId);
34
-        $topLevelSectionId = $event->registrationFormUuid();
26
+	/**
27
+	 * @param int $eventId
28
+	 * @return array
29
+	 */
30
+	public function getData(int $eventId): array
31
+	{
32
+		/** @var EE_Event */
33
+		$event = EEM_Event::instance()->get_one_by_ID($eventId);
34
+		$topLevelSectionId = $event->registrationFormUuid();
35 35
 
36
-        /** @var EE_Form_Section[] */
37
-        $form_sections = EEM_Form_Section::instance()->get_all([
38
-            [
39
-                'OR' => [
40
-                    'FSC_UUID'      => $topLevelSectionId, // top level form
41
-                    'FSC_belongsTo' => $topLevelSectionId, // child form sections
42
-                    'FSC_status'    => FormStatus::SHARED, // shared form sections
43
-                ]
44
-                ],
45
-            'order_by' => ['FSC_order' => 'ASC'],
46
-        ]);
36
+		/** @var EE_Form_Section[] */
37
+		$form_sections = EEM_Form_Section::instance()->get_all([
38
+			[
39
+				'OR' => [
40
+					'FSC_UUID'      => $topLevelSectionId, // top level form
41
+					'FSC_belongsTo' => $topLevelSectionId, // child form sections
42
+					'FSC_status'    => FormStatus::SHARED, // shared form sections
43
+				]
44
+				],
45
+			'order_by' => ['FSC_order' => 'ASC'],
46
+		]);
47 47
 
48
-        $sections = [];
49
-        foreach ($form_sections as $section) {
50
-            $UUID = $section->UUID();
48
+		$sections = [];
49
+		foreach ($form_sections as $section) {
50
+			$UUID = $section->UUID();
51 51
 
52
-            // Avoid duplicates, if any
53
-            $sections[ $UUID ] = [
54
-                'id'         => $UUID,
55
-                'appliesTo'  => GQLUtils::formatEnumKey($section->appliesTo()),
56
-                'attributes' => $section->attributes()->toJson(),
57
-                'belongsTo'  => $section->belongsTo(),
58
-                'isActive'   => $section->isActive(),
59
-                'isArchived' => $section->isArchived(),
60
-                'isDefault'  => $section->isDefault(),
61
-                'isShared'   => $section->isShared(),
62
-                'isTrashed'  => $section->isTrashed(),
63
-                'label'      => $section->label()->toJson(),
64
-                'order'      => $section->order(),
65
-                'status'     => GQLUtils::formatEnumKey($section->status()),
66
-            ];
67
-        }
52
+			// Avoid duplicates, if any
53
+			$sections[ $UUID ] = [
54
+				'id'         => $UUID,
55
+				'appliesTo'  => GQLUtils::formatEnumKey($section->appliesTo()),
56
+				'attributes' => $section->attributes()->toJson(),
57
+				'belongsTo'  => $section->belongsTo(),
58
+				'isActive'   => $section->isActive(),
59
+				'isArchived' => $section->isArchived(),
60
+				'isDefault'  => $section->isDefault(),
61
+				'isShared'   => $section->isShared(),
62
+				'isTrashed'  => $section->isTrashed(),
63
+				'label'      => $section->label()->toJson(),
64
+				'order'      => $section->order(),
65
+				'status'     => GQLUtils::formatEnumKey($section->status()),
66
+			];
67
+		}
68 68
 
69
-        /** @var EE_Form_Element[] */
70
-        $form_elements = EEM_Form_Element::instance()->get_all([
71
-            [
72
-                'FSC_UUID' => ['IN', array_keys($sections)]
73
-            ]
74
-        ]);
69
+		/** @var EE_Form_Element[] */
70
+		$form_elements = EEM_Form_Element::instance()->get_all([
71
+			[
72
+				'FSC_UUID' => ['IN', array_keys($sections)]
73
+			]
74
+		]);
75 75
 
76
-        $elements = [];
77
-        foreach ($form_elements as $element) {
78
-            $UUID = $element->UUID();
76
+		$elements = [];
77
+		foreach ($form_elements as $element) {
78
+			$UUID = $element->UUID();
79 79
 
80
-            // Avoid duplicates
81
-            $elements[ $UUID ] = [
82
-                'id'         => $UUID,
83
-                'adminOnly'  => $element->adminOnly(),
84
-                'attributes' => $element->attributes()->toJson(),
85
-                'belongsTo'  => $element->belongsTo(),
86
-                'helpText'   => $element->helpText()->toJson(),
87
-                'label'      => $element->label()->toJson(),
88
-                'mapsTo'     => $element->mapsTo(),
89
-                'options'    => $element->options()->toJson(),
90
-                'order'      => $element->order(),
91
-                'required'   => $element->required()->toJson(),
92
-                'status'     => GQLUtils::formatEnumKey($element->status()),
93
-                'type'       => GQLUtils::formatEnumKey($element->type()),
94
-            ];
95
-        }
80
+			// Avoid duplicates
81
+			$elements[ $UUID ] = [
82
+				'id'         => $UUID,
83
+				'adminOnly'  => $element->adminOnly(),
84
+				'attributes' => $element->attributes()->toJson(),
85
+				'belongsTo'  => $element->belongsTo(),
86
+				'helpText'   => $element->helpText()->toJson(),
87
+				'label'      => $element->label()->toJson(),
88
+				'mapsTo'     => $element->mapsTo(),
89
+				'options'    => $element->options()->toJson(),
90
+				'order'      => $element->order(),
91
+				'required'   => $element->required()->toJson(),
92
+				'status'     => GQLUtils::formatEnumKey($element->status()),
93
+				'type'       => GQLUtils::formatEnumKey($element->type()),
94
+			];
95
+		}
96 96
 
97
-        return [
98
-            'elements'          => array_values($elements),
99
-            'sections'          => array_values($sections),
100
-            'topLevelSectionId' => $event->registrationFormUuid()
101
-        ];
102
-    }
97
+		return [
98
+			'elements'          => array_values($elements),
99
+			'sections'          => array_values($sections),
100
+			'topLevelSectionId' => $event->registrationFormUuid()
101
+		];
102
+	}
103 103
 }
Please login to merge, or discard this patch.
core/domain/entities/routing/handlers/admin/EspressoEventEditor.php 1 patch
Indentation   +134 added lines, -134 removed lines patch added patch discarded remove patch
@@ -18,145 +18,145 @@
 block discarded – undo
18 18
 class EspressoEventEditor extends EspressoEventsAdmin
19 19
 {
20 20
 
21
-    /**
22
-     * returns true if the current request matches this route
23
-     *
24
-     * @return bool
25
-     * @since   $VID:$
26
-     */
27
-    public function matchesCurrentRequest(): bool
28
-    {
29
-        return parent::matchesCurrentRequest()
30
-               && $this->admin_config->useAdvancedEditor()
31
-               && (
32
-                   $this->request->getRequestParam('action') === 'create_new'
33
-                || $this->request->getRequestParam('action') === 'edit'
34
-               );
35
-    }
21
+	/**
22
+	 * returns true if the current request matches this route
23
+	 *
24
+	 * @return bool
25
+	 * @since   $VID:$
26
+	 */
27
+	public function matchesCurrentRequest(): bool
28
+	{
29
+		return parent::matchesCurrentRequest()
30
+			   && $this->admin_config->useAdvancedEditor()
31
+			   && (
32
+				   $this->request->getRequestParam('action') === 'create_new'
33
+				|| $this->request->getRequestParam('action') === 'edit'
34
+			   );
35
+	}
36 36
 
37 37
 
38
-    /**
39
-     * @since $VID:$
40
-     */
41
-    protected function registerDependencies()
42
-    {
43
-        $this->dependency_map->registerDependencies(
44
-            'EventEspresso\core\domain\services\admin\events\editor\EventEditorGraphQLData',
45
-            [
46
-                'EventEspresso\core\domain\entities\admin\GraphQLData\Datetimes'                 => EE_Dependency_Map::load_from_cache,
47
-                'EventEspresso\core\domain\entities\admin\GraphQLData\Event'                     => EE_Dependency_Map::load_from_cache,
48
-                'EventEspresso\core\domain\entities\admin\GraphQLData\Prices'                    => EE_Dependency_Map::load_from_cache,
49
-                'EventEspresso\core\domain\entities\admin\GraphQLData\PriceTypes'                => EE_Dependency_Map::load_from_cache,
50
-                'EventEspresso\core\domain\entities\admin\GraphQLData\Tickets'                   => EE_Dependency_Map::load_from_cache,
51
-                'EventEspresso\core\domain\services\admin\events\editor\NewEventDefaultEntities' => EE_Dependency_Map::load_from_cache,
52
-                '\EventEspresso\core\domain\services\admin\events\editor\EventManagerData'       => EE_Dependency_Map::load_from_cache,
53
-                'EventEspresso\core\domain\services\admin\events\editor\EventEntityRelations'    => EE_Dependency_Map::load_from_cache,
54
-                'EventEspresso\core\domain\services\admin\events\editor\TicketMeta'              => EE_Dependency_Map::load_from_cache,
55
-                'EventEspresso\core\domain\services\admin\events\editor\FormBuilder'             => EE_Dependency_Map::load_from_cache,
56
-            ]
57
-        );
58
-        $this->dependency_map->registerDependencies(
59
-            'EventEspresso\core\domain\services\admin\events\editor\EventEntityRelations',
60
-            [
61
-                'EEM_Datetime'                                         => EE_Dependency_Map::load_from_cache,
62
-                'EEM_Event'                                            => EE_Dependency_Map::load_from_cache,
63
-                'EEM_Price'                                            => EE_Dependency_Map::load_from_cache,
64
-                'EEM_Price_Type'                                       => EE_Dependency_Map::load_from_cache,
65
-                'EEM_Ticket'                                           => EE_Dependency_Map::load_from_cache,
66
-                'EventEspresso\core\domain\services\graphql\Utilities' => EE_Dependency_Map::load_from_cache,
67
-            ]
68
-        );
69
-        $this->dependency_map->registerDependencies(
70
-            'EventEspresso\core\domain\services\admin\events\editor\NewEventDefaultEntities',
71
-            [
72
-                'EventEspresso\core\domain\services\admin\entities\DefaultDatetimes' => EE_Dependency_Map::load_from_cache,
73
-                'EEM_Datetime'                                                       => EE_Dependency_Map::load_from_cache,
74
-                'EEM_Event'                                                          => EE_Dependency_Map::load_from_cache,
75
-                'EEM_Price'                                                          => EE_Dependency_Map::load_from_cache,
76
-                'EEM_Price_Type'                                                     => EE_Dependency_Map::load_from_cache,
77
-                'EEM_Ticket'                                                         => EE_Dependency_Map::load_from_cache,
78
-                'EventEspresso\core\domain\services\graphql\Utilities'               => EE_Dependency_Map::load_from_cache,
79
-            ]
80
-        );
81
-        $this->dependency_map->registerDependencies(
82
-            'EventEspresso\core\domain\services\admin\events\editor\TicketMeta',
83
-            ['EEM_Ticket' => EE_Dependency_Map::load_from_cache]
84
-        );
85
-        $this->dependency_map->registerDependencies(
86
-            'EventEspresso\core\domain\services\admin\entities\DefaultDatetimes',
87
-            [
88
-                'EventEspresso\core\domain\services\admin\entities\DefaultTickets' => EE_Dependency_Map::load_from_cache,
89
-                'EEM_Datetime'                                                     => EE_Dependency_Map::load_from_cache,
90
-            ]
91
-        );
92
-        $this->dependency_map->registerDependencies(
93
-            'EventEspresso\core\domain\services\admin\entities\DefaultTickets',
94
-            [
95
-                'EventEspresso\core\domain\services\admin\entities\DefaultPrices' => EE_Dependency_Map::load_from_cache,
96
-                'EEM_Ticket'                                                      => EE_Dependency_Map::load_from_cache,
97
-            ]
98
-        );
99
-        $this->dependency_map->registerDependencies(
100
-            'EventEspresso\core\domain\services\admin\entities\DefaultPrices',
101
-            [
102
-                'EEM_Price'      => EE_Dependency_Map::load_from_cache,
103
-                'EEM_Price_Type' => EE_Dependency_Map::load_from_cache,
104
-            ]
105
-        );
106
-        $this->dependency_map->registerDependencies(
107
-            'EventEspresso\core\domain\entities\routing\data_nodes\domains\EventEditor',
108
-            [
109
-                'EventEspresso\core\domain\services\admin\events\editor\EventEditorGraphQLData' => EE_Dependency_Map::load_from_cache,
110
-                'EventEspresso\core\services\json\JsonDataNodeValidator'                        => EE_Dependency_Map::load_from_cache,
111
-            ]
112
-        );
113
-        $this->dependency_map->registerDependencies(
114
-            'EventEspresso\core\domain\services\assets\EventEditorAssetManager',
115
-            [
116
-                'EventEspresso\core\domain\Domain'                   => EE_Dependency_Map::load_from_cache,
117
-                'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache,
118
-                'EventEspresso\core\services\assets\Registry'        => EE_Dependency_Map::load_from_cache,
119
-            ]
120
-        );
121
-        $this->dependency_map->registerDependencies(
122
-            'EventEspresso\core\domain\services\admin\events\editor\EventManagerData',
123
-            [
124
-                'EventEspresso\core\domain\entities\users\EventManagers' => EE_Dependency_Map::load_from_cache,
125
-                'EventEspresso\core\domain\services\graphql\Utilities'   => EE_Dependency_Map::load_from_cache,
126
-            ]
127
-        );
128
-    }
38
+	/**
39
+	 * @since $VID:$
40
+	 */
41
+	protected function registerDependencies()
42
+	{
43
+		$this->dependency_map->registerDependencies(
44
+			'EventEspresso\core\domain\services\admin\events\editor\EventEditorGraphQLData',
45
+			[
46
+				'EventEspresso\core\domain\entities\admin\GraphQLData\Datetimes'                 => EE_Dependency_Map::load_from_cache,
47
+				'EventEspresso\core\domain\entities\admin\GraphQLData\Event'                     => EE_Dependency_Map::load_from_cache,
48
+				'EventEspresso\core\domain\entities\admin\GraphQLData\Prices'                    => EE_Dependency_Map::load_from_cache,
49
+				'EventEspresso\core\domain\entities\admin\GraphQLData\PriceTypes'                => EE_Dependency_Map::load_from_cache,
50
+				'EventEspresso\core\domain\entities\admin\GraphQLData\Tickets'                   => EE_Dependency_Map::load_from_cache,
51
+				'EventEspresso\core\domain\services\admin\events\editor\NewEventDefaultEntities' => EE_Dependency_Map::load_from_cache,
52
+				'\EventEspresso\core\domain\services\admin\events\editor\EventManagerData'       => EE_Dependency_Map::load_from_cache,
53
+				'EventEspresso\core\domain\services\admin\events\editor\EventEntityRelations'    => EE_Dependency_Map::load_from_cache,
54
+				'EventEspresso\core\domain\services\admin\events\editor\TicketMeta'              => EE_Dependency_Map::load_from_cache,
55
+				'EventEspresso\core\domain\services\admin\events\editor\FormBuilder'             => EE_Dependency_Map::load_from_cache,
56
+			]
57
+		);
58
+		$this->dependency_map->registerDependencies(
59
+			'EventEspresso\core\domain\services\admin\events\editor\EventEntityRelations',
60
+			[
61
+				'EEM_Datetime'                                         => EE_Dependency_Map::load_from_cache,
62
+				'EEM_Event'                                            => EE_Dependency_Map::load_from_cache,
63
+				'EEM_Price'                                            => EE_Dependency_Map::load_from_cache,
64
+				'EEM_Price_Type'                                       => EE_Dependency_Map::load_from_cache,
65
+				'EEM_Ticket'                                           => EE_Dependency_Map::load_from_cache,
66
+				'EventEspresso\core\domain\services\graphql\Utilities' => EE_Dependency_Map::load_from_cache,
67
+			]
68
+		);
69
+		$this->dependency_map->registerDependencies(
70
+			'EventEspresso\core\domain\services\admin\events\editor\NewEventDefaultEntities',
71
+			[
72
+				'EventEspresso\core\domain\services\admin\entities\DefaultDatetimes' => EE_Dependency_Map::load_from_cache,
73
+				'EEM_Datetime'                                                       => EE_Dependency_Map::load_from_cache,
74
+				'EEM_Event'                                                          => EE_Dependency_Map::load_from_cache,
75
+				'EEM_Price'                                                          => EE_Dependency_Map::load_from_cache,
76
+				'EEM_Price_Type'                                                     => EE_Dependency_Map::load_from_cache,
77
+				'EEM_Ticket'                                                         => EE_Dependency_Map::load_from_cache,
78
+				'EventEspresso\core\domain\services\graphql\Utilities'               => EE_Dependency_Map::load_from_cache,
79
+			]
80
+		);
81
+		$this->dependency_map->registerDependencies(
82
+			'EventEspresso\core\domain\services\admin\events\editor\TicketMeta',
83
+			['EEM_Ticket' => EE_Dependency_Map::load_from_cache]
84
+		);
85
+		$this->dependency_map->registerDependencies(
86
+			'EventEspresso\core\domain\services\admin\entities\DefaultDatetimes',
87
+			[
88
+				'EventEspresso\core\domain\services\admin\entities\DefaultTickets' => EE_Dependency_Map::load_from_cache,
89
+				'EEM_Datetime'                                                     => EE_Dependency_Map::load_from_cache,
90
+			]
91
+		);
92
+		$this->dependency_map->registerDependencies(
93
+			'EventEspresso\core\domain\services\admin\entities\DefaultTickets',
94
+			[
95
+				'EventEspresso\core\domain\services\admin\entities\DefaultPrices' => EE_Dependency_Map::load_from_cache,
96
+				'EEM_Ticket'                                                      => EE_Dependency_Map::load_from_cache,
97
+			]
98
+		);
99
+		$this->dependency_map->registerDependencies(
100
+			'EventEspresso\core\domain\services\admin\entities\DefaultPrices',
101
+			[
102
+				'EEM_Price'      => EE_Dependency_Map::load_from_cache,
103
+				'EEM_Price_Type' => EE_Dependency_Map::load_from_cache,
104
+			]
105
+		);
106
+		$this->dependency_map->registerDependencies(
107
+			'EventEspresso\core\domain\entities\routing\data_nodes\domains\EventEditor',
108
+			[
109
+				'EventEspresso\core\domain\services\admin\events\editor\EventEditorGraphQLData' => EE_Dependency_Map::load_from_cache,
110
+				'EventEspresso\core\services\json\JsonDataNodeValidator'                        => EE_Dependency_Map::load_from_cache,
111
+			]
112
+		);
113
+		$this->dependency_map->registerDependencies(
114
+			'EventEspresso\core\domain\services\assets\EventEditorAssetManager',
115
+			[
116
+				'EventEspresso\core\domain\Domain'                   => EE_Dependency_Map::load_from_cache,
117
+				'EventEspresso\core\services\assets\AssetCollection' => EE_Dependency_Map::load_from_cache,
118
+				'EventEspresso\core\services\assets\Registry'        => EE_Dependency_Map::load_from_cache,
119
+			]
120
+		);
121
+		$this->dependency_map->registerDependencies(
122
+			'EventEspresso\core\domain\services\admin\events\editor\EventManagerData',
123
+			[
124
+				'EventEspresso\core\domain\entities\users\EventManagers' => EE_Dependency_Map::load_from_cache,
125
+				'EventEspresso\core\domain\services\graphql\Utilities'   => EE_Dependency_Map::load_from_cache,
126
+			]
127
+		);
128
+	}
129 129
 
130 130
 
131
-    /**
132
-     * @return string
133
-     */
134
-    protected function dataNodeClass(): string
135
-    {
136
-        return EventEditor::class;
137
-    }
131
+	/**
132
+	 * @return string
133
+	 */
134
+	protected function dataNodeClass(): string
135
+	{
136
+		return EventEditor::class;
137
+	}
138 138
 
139 139
 
140
-    /**
141
-     * implements logic required to run during request
142
-     *
143
-     * @return bool
144
-     * @since   $VID:$
145
-     */
146
-    protected function requestHandler(): bool
147
-    {
148
-        if (! class_exists('WPGraphQL')) {
149
-            require_once EE_THIRD_PARTY . 'wp-graphql/wp-graphql.php';
150
-        }
151
-        /** @var GraphQLManager $graphQL_manager */
152
-        $graphQL_manager = $this->loader->getShared('EventEspresso\core\services\graphql\GraphQLManager');
153
-        $graphQL_manager->init();
140
+	/**
141
+	 * implements logic required to run during request
142
+	 *
143
+	 * @return bool
144
+	 * @since   $VID:$
145
+	 */
146
+	protected function requestHandler(): bool
147
+	{
148
+		if (! class_exists('WPGraphQL')) {
149
+			require_once EE_THIRD_PARTY . 'wp-graphql/wp-graphql.php';
150
+		}
151
+		/** @var GraphQLManager $graphQL_manager */
152
+		$graphQL_manager = $this->loader->getShared('EventEspresso\core\services\graphql\GraphQLManager');
153
+		$graphQL_manager->init();
154 154
 
155
-        /** @var EventEditorAssetManager $asset_manager */
156
-        $asset_manager = $this->loader->getShared(
157
-            'EventEspresso\core\domain\services\assets\EventEditorAssetManager'
158
-        );
159
-        add_action('admin_enqueue_scripts', [$asset_manager, 'enqueueEventEditor']);
160
-        return true;
161
-    }
155
+		/** @var EventEditorAssetManager $asset_manager */
156
+		$asset_manager = $this->loader->getShared(
157
+			'EventEspresso\core\domain\services\assets\EventEditorAssetManager'
158
+		);
159
+		add_action('admin_enqueue_scripts', [$asset_manager, 'enqueueEventEditor']);
160
+		return true;
161
+	}
162 162
 }
Please login to merge, or discard this patch.
admin/extend/messages/espresso_events_Messages_Hooks_Extend.class.php 1 patch
Indentation   +265 added lines, -265 removed lines patch added patch discarded remove patch
@@ -12,274 +12,274 @@
 block discarded – undo
12 12
  */
13 13
 class espresso_events_Messages_Hooks_Extend extends espresso_events_Messages_Hooks
14 14
 {
15
-    /**
16
-     * espresso_events_Messages_Hooks_Extend constructor.
17
-     *
18
-     * @param \EE_Admin_Page $admin_page
19
-     */
20
-    public function __construct(EE_Admin_Page $admin_page)
21
-    {
22
-        /**
23
-         * Add cap restriction ... metaboxes should not show if user does not have the ability to edit_custom_messages
24
-         */
25
-        if (
26
-            ! EE_Registry::instance()->CAP->current_user_can(
27
-                'ee_edit_messages',
28
-                'messages_events_editor_metabox'
29
-            )
30
-        ) {
31
-            return;
32
-        }
33
-        add_filter(
34
-            'FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks',
35
-            array($this, 'caf_updates'),
36
-            10
37
-        );
38
-        add_action(
39
-            'AHEE__Extend_Events_Admin_Page___duplicate_event__after',
40
-            array($this, 'duplicate_custom_message_settings'),
41
-            10,
42
-            2
43
-        );
44
-        parent::__construct($admin_page);
45
-    }
46
-
47
-
48
-    /**
49
-     * extending the properties set in espresso_events_Messages_Hooks
50
-     *
51
-     * @access protected
52
-     * @return void
53
-     */
54
-    protected function _extend_properties()
55
-    {
56
-        define('EE_MSGS_EXTEND_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'messages/assets/');
57
-        $this->_ajax_func = array(
58
-            'ee_msgs_create_new_custom' => 'create_new_custom',
59
-        );
60
-        $this->_metaboxes = array(
61
-            0 => array(
62
-                'page_route' => array('edit', 'create_new'),
63
-                'func'       => 'messages_metabox',
64
-                'label'      => esc_html__('Notifications', 'event_espresso'),
65
-                'priority'   => 'high',
66
-            ),
67
-        );
68
-
69
-        // see explanation for layout in EE_Admin_Hooks
70
-        $this->_scripts_styles = array(
71
-            'registers' => array(
72
-                'events_msg_admin'     => array(
73
-                    'url'     => EE_MSGS_EXTEND_ASSETS_URL . 'events_messages_admin.js',
74
-                    'depends' => array('ee-dialog', 'ee-parse-uri', 'ee-serialize-full-array'),
75
-                ),
76
-                'events_msg_admin_css' => array(
77
-                    'url'  => EE_MSGS_EXTEND_ASSETS_URL . 'ee_msg_events_admin.css',
78
-                    'type' => 'css',
79
-                ),
80
-            ),
81
-            'enqueues'  => array(
82
-                'events_msg_admin'     => array('edit', 'create_new'),
83
-                'events_msg_admin_css' => array('edit', 'create_new'),
84
-            ),
85
-        );
86
-    }
87
-
88
-
89
-    public function caf_updates($update_callbacks)
90
-    {
91
-        $update_callbacks[] = array($this, 'attach_evt_message_templates');
92
-        return $update_callbacks;
93
-    }
94
-
95
-
96
-    /**
97
-     * Handles attaching Message Templates to the Event on save.
98
-     *
99
-     * @param  EE_Event $event EE event object
100
-     * @param  array    $data  The request data from the form
101
-     * @return bool success or fail
102
-     * @throws EE_Error
103
-     */
104
-    public function attach_evt_message_templates($event, $data)
105
-    {
106
-        // first we remove all existing relations on the Event for message types.
107
-        $event->_remove_relations('Message_Template_Group');
108
-        // now let's just loop through the selected templates and add relations!
109
-        if (isset($data['event_message_templates_relation'])) {
110
-            foreach ($data['event_message_templates_relation'] as $grp_ID) {
111
-                $event->_add_relation_to($grp_ID, 'Message_Template_Group');
112
-            }
113
-        }
114
-        // now save
115
-        return $event->save();
116
-    }
117
-
118
-
119
-    /**
120
-     * @param $event
121
-     * @param $callback_args
122
-     * @return string
123
-     * @throws \EE_Error
124
-     */
125
-    public function messages_metabox($event, $callback_args)
126
-    {
127
-        // let's get the active messengers (b/c messenger objects have the active message templates)
128
-        // convert 'evt_id' to 'EVT_ID'
129
-        $this->_req_data['EVT_ID'] = isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : null;
130
-        $this->_req_data['EVT_ID'] = isset($this->_req_data['post']) && empty($this->_req_data['EVT_ID'])
131
-            ? $this->_req_data['post']
132
-            : $this->_req_data['EVT_ID'];
133
-
134
-        $this->_req_data['EVT_ID'] = empty($this->_req_data['EVT_ID']) && isset($this->_req_data['evt_id'])
135
-            ? $this->_req_data['evt_id']
136
-            : $this->_req_data['EVT_ID'];
137
-        /** @type EE_Message_Resource_Manager $message_resource_manager */
138
-        $message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
139
-        $active_messengers = $message_resource_manager->active_messengers();
140
-        $tabs = array();
141
-
142
-        // empty messengers?
143
-        // Note message types will always have at least one available because every messenger has a default message type
144
-        // associated with it (payment) if no other message types are selected.
145
-        if (empty($active_messengers)) {
146
-            $msg_activate_url = EE_Admin_Page::add_query_args_and_nonce(
147
-                array('action' => 'settings'),
148
-                EE_MSG_ADMIN_URL
149
-            );
150
-            $error_msg = sprintf(
151
-                esc_html__(
152
-                    'There are no active messengers. So no notifications will go out for %1$sany%2$s events.  You will want to %3$sActivate a Messenger%4$s.',
153
-                    'event_espresso'
154
-                ),
155
-                '<strong>',
156
-                '</strong>',
157
-                '<a href="' . $msg_activate_url . '">',
158
-                '</a>'
159
-            );
160
-            $error_content = '<div class="error"><p>' . $error_msg . '</p></div>';
161
-            $internal_content = '<div id="messages-error"><p>' . $error_msg . '</p></div>';
162
-
163
-            echo $error_content;
164
-            echo $internal_content;
165
-            return '';
166
-        }
167
-
168
-        $event_id = isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : null;
169
-        // get content for active messengers
170
-        foreach ($active_messengers as $name => $messenger) {
171
-            // first check if there are any active message types for this messenger.
172
-            $active_mts = $message_resource_manager->get_active_message_types_for_messenger($name);
173
-            if (empty($active_mts)) {
174
-                continue;
175
-            }
176
-
177
-            $tab_content = $messenger->get_messenger_admin_page_content(
178
-                'events',
179
-                'edit',
180
-                array('event' => $event_id)
181
-            );
182
-
183
-            if (! empty($tab_content)) {
184
-                $tabs[ $name ] = $tab_content;
185
-            }
186
-        }
187
-
188
-
189
-        // we want this to be tabbed content so let's use the EEH_Tabbed_Content::display helper.
190
-        $tabbed_content = EEH_Tabbed_Content::display($tabs);
191
-        if ($tabbed_content instanceof WP_Error) {
192
-            $tabbed_content = $tabbed_content->get_error_message();
193
-        }
194
-
195
-        $notices = '
15
+	/**
16
+	 * espresso_events_Messages_Hooks_Extend constructor.
17
+	 *
18
+	 * @param \EE_Admin_Page $admin_page
19
+	 */
20
+	public function __construct(EE_Admin_Page $admin_page)
21
+	{
22
+		/**
23
+		 * Add cap restriction ... metaboxes should not show if user does not have the ability to edit_custom_messages
24
+		 */
25
+		if (
26
+			! EE_Registry::instance()->CAP->current_user_can(
27
+				'ee_edit_messages',
28
+				'messages_events_editor_metabox'
29
+			)
30
+		) {
31
+			return;
32
+		}
33
+		add_filter(
34
+			'FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks',
35
+			array($this, 'caf_updates'),
36
+			10
37
+		);
38
+		add_action(
39
+			'AHEE__Extend_Events_Admin_Page___duplicate_event__after',
40
+			array($this, 'duplicate_custom_message_settings'),
41
+			10,
42
+			2
43
+		);
44
+		parent::__construct($admin_page);
45
+	}
46
+
47
+
48
+	/**
49
+	 * extending the properties set in espresso_events_Messages_Hooks
50
+	 *
51
+	 * @access protected
52
+	 * @return void
53
+	 */
54
+	protected function _extend_properties()
55
+	{
56
+		define('EE_MSGS_EXTEND_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'messages/assets/');
57
+		$this->_ajax_func = array(
58
+			'ee_msgs_create_new_custom' => 'create_new_custom',
59
+		);
60
+		$this->_metaboxes = array(
61
+			0 => array(
62
+				'page_route' => array('edit', 'create_new'),
63
+				'func'       => 'messages_metabox',
64
+				'label'      => esc_html__('Notifications', 'event_espresso'),
65
+				'priority'   => 'high',
66
+			),
67
+		);
68
+
69
+		// see explanation for layout in EE_Admin_Hooks
70
+		$this->_scripts_styles = array(
71
+			'registers' => array(
72
+				'events_msg_admin'     => array(
73
+					'url'     => EE_MSGS_EXTEND_ASSETS_URL . 'events_messages_admin.js',
74
+					'depends' => array('ee-dialog', 'ee-parse-uri', 'ee-serialize-full-array'),
75
+				),
76
+				'events_msg_admin_css' => array(
77
+					'url'  => EE_MSGS_EXTEND_ASSETS_URL . 'ee_msg_events_admin.css',
78
+					'type' => 'css',
79
+				),
80
+			),
81
+			'enqueues'  => array(
82
+				'events_msg_admin'     => array('edit', 'create_new'),
83
+				'events_msg_admin_css' => array('edit', 'create_new'),
84
+			),
85
+		);
86
+	}
87
+
88
+
89
+	public function caf_updates($update_callbacks)
90
+	{
91
+		$update_callbacks[] = array($this, 'attach_evt_message_templates');
92
+		return $update_callbacks;
93
+	}
94
+
95
+
96
+	/**
97
+	 * Handles attaching Message Templates to the Event on save.
98
+	 *
99
+	 * @param  EE_Event $event EE event object
100
+	 * @param  array    $data  The request data from the form
101
+	 * @return bool success or fail
102
+	 * @throws EE_Error
103
+	 */
104
+	public function attach_evt_message_templates($event, $data)
105
+	{
106
+		// first we remove all existing relations on the Event for message types.
107
+		$event->_remove_relations('Message_Template_Group');
108
+		// now let's just loop through the selected templates and add relations!
109
+		if (isset($data['event_message_templates_relation'])) {
110
+			foreach ($data['event_message_templates_relation'] as $grp_ID) {
111
+				$event->_add_relation_to($grp_ID, 'Message_Template_Group');
112
+			}
113
+		}
114
+		// now save
115
+		return $event->save();
116
+	}
117
+
118
+
119
+	/**
120
+	 * @param $event
121
+	 * @param $callback_args
122
+	 * @return string
123
+	 * @throws \EE_Error
124
+	 */
125
+	public function messages_metabox($event, $callback_args)
126
+	{
127
+		// let's get the active messengers (b/c messenger objects have the active message templates)
128
+		// convert 'evt_id' to 'EVT_ID'
129
+		$this->_req_data['EVT_ID'] = isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : null;
130
+		$this->_req_data['EVT_ID'] = isset($this->_req_data['post']) && empty($this->_req_data['EVT_ID'])
131
+			? $this->_req_data['post']
132
+			: $this->_req_data['EVT_ID'];
133
+
134
+		$this->_req_data['EVT_ID'] = empty($this->_req_data['EVT_ID']) && isset($this->_req_data['evt_id'])
135
+			? $this->_req_data['evt_id']
136
+			: $this->_req_data['EVT_ID'];
137
+		/** @type EE_Message_Resource_Manager $message_resource_manager */
138
+		$message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager');
139
+		$active_messengers = $message_resource_manager->active_messengers();
140
+		$tabs = array();
141
+
142
+		// empty messengers?
143
+		// Note message types will always have at least one available because every messenger has a default message type
144
+		// associated with it (payment) if no other message types are selected.
145
+		if (empty($active_messengers)) {
146
+			$msg_activate_url = EE_Admin_Page::add_query_args_and_nonce(
147
+				array('action' => 'settings'),
148
+				EE_MSG_ADMIN_URL
149
+			);
150
+			$error_msg = sprintf(
151
+				esc_html__(
152
+					'There are no active messengers. So no notifications will go out for %1$sany%2$s events.  You will want to %3$sActivate a Messenger%4$s.',
153
+					'event_espresso'
154
+				),
155
+				'<strong>',
156
+				'</strong>',
157
+				'<a href="' . $msg_activate_url . '">',
158
+				'</a>'
159
+			);
160
+			$error_content = '<div class="error"><p>' . $error_msg . '</p></div>';
161
+			$internal_content = '<div id="messages-error"><p>' . $error_msg . '</p></div>';
162
+
163
+			echo $error_content;
164
+			echo $internal_content;
165
+			return '';
166
+		}
167
+
168
+		$event_id = isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : null;
169
+		// get content for active messengers
170
+		foreach ($active_messengers as $name => $messenger) {
171
+			// first check if there are any active message types for this messenger.
172
+			$active_mts = $message_resource_manager->get_active_message_types_for_messenger($name);
173
+			if (empty($active_mts)) {
174
+				continue;
175
+			}
176
+
177
+			$tab_content = $messenger->get_messenger_admin_page_content(
178
+				'events',
179
+				'edit',
180
+				array('event' => $event_id)
181
+			);
182
+
183
+			if (! empty($tab_content)) {
184
+				$tabs[ $name ] = $tab_content;
185
+			}
186
+		}
187
+
188
+
189
+		// we want this to be tabbed content so let's use the EEH_Tabbed_Content::display helper.
190
+		$tabbed_content = EEH_Tabbed_Content::display($tabs);
191
+		if ($tabbed_content instanceof WP_Error) {
192
+			$tabbed_content = $tabbed_content->get_error_message();
193
+		}
194
+
195
+		$notices = '
196 196
 	<div id="espresso-ajax-loading" class="ajax-loader-grey">
197 197
 		<span class="ee-spinner ee-spin"></span><span class="hidden">'
198
-                   . esc_html__('loading...', 'event_espresso')
199
-                   . '</span>
198
+				   . esc_html__('loading...', 'event_espresso')
199
+				   . '</span>
200 200
 	</div>
201 201
 	<div class="ee-notices"></div>';
202 202
 
203
-        if (defined('DOING_AJAX')) {
204
-            return $tabbed_content;
205
-        }
206
-
207
-        do_action('AHEE__espresso_events_Messages_Hooks_Extend__messages_metabox__before_content');
208
-        echo $notices . '<div class="messages-tabs-content">' . $tabbed_content . '</div>';
209
-        do_action('AHEE__espresso_events_Messages_Hooks_Extend__messages_metabox__after_content');
210
-    }
211
-
212
-
213
-    /**
214
-     * Ajax callback for ee_msgs_create_new_custom ajax request.
215
-     * Takes incoming GRP_ID and name and description values from ajax request
216
-     * to create a new custom template based off of the incoming GRP_ID.
217
-     *
218
-     * @access public
219
-     * @return string either an html string will be returned or a success message
220
-     * @throws EE_Error
221
-     */
222
-    public function create_new_custom()
223
-    {
224
-        if (! EE_Registry::instance()->CAP->current_user_can('ee_edit_messages', 'create_new_custom_ajax')) {
225
-            wp_die(__('You don\'t have privileges to do this action', 'event_espresso'));
226
-        }
227
-
228
-        // let's clean up the _POST global a bit for downstream usage of name and description.
229
-        $_POST['templateName'] = ! empty($this->_req_data['custom_template_args']['MTP_name'])
230
-            ? $this->_req_data['custom_template_args']['MTP_name']
231
-            : '';
232
-        $_POST['templateDescription'] = ! empty($this->_req_data['custom_template_args']['MTP_description'])
233
-            ? $this->_req_data['custom_template_args']['MTP_description']
234
-            : '';
235
-
236
-        // is this a template switch if so EE_Admin_Page child needs this object
237
-        $this->_page_object->set_hook_object($this);
238
-
239
-        $this->_page_object->add_message_template(
240
-            $this->_req_data['messageType'],
241
-            $this->_req_data['messenger'],
242
-            $this->_req_data['group_ID']
243
-        );
244
-    }
245
-
246
-
247
-    public function create_new_admin_footer()
248
-    {
249
-        $this->edit_admin_footer();
250
-    }
251
-
252
-
253
-    /**
254
-     * This is the dynamic method for this class
255
-     * that will end up hooking into the 'admin_footer' hook on the 'edit_event' route in the events page.
256
-     *
257
-     * @return string
258
-     * @throws DomainException
259
-     */
260
-    public function edit_admin_footer()
261
-    {
262
-        EEH_Template::display_template(
263
-            EE_CORE_CAF_ADMIN_EXTEND . 'messages/templates/create_custom_template_form.template.php'
264
-        );
265
-    }
266
-
267
-
268
-    /**
269
-     * Callback for AHEE__Extend_Events_Admin_Page___duplicate_event__after hook used to ensure new events duplicate
270
-     * the assigned custom message templates.
271
-     *
272
-     * @param EE_Event $new_event
273
-     * @param EE_Event $original_event
274
-     * @throws EE_Error
275
-     */
276
-    public function duplicate_custom_message_settings(EE_Event $new_event, EE_Event $original_event)
277
-    {
278
-        $message_template_groups = $original_event->get_many_related('Message_Template_Group');
279
-        foreach ($message_template_groups as $message_template_group) {
280
-            $new_event->_add_relation_to($message_template_group, 'Message_Template_Group');
281
-        }
282
-        // save new event
283
-        $new_event->save();
284
-    }
203
+		if (defined('DOING_AJAX')) {
204
+			return $tabbed_content;
205
+		}
206
+
207
+		do_action('AHEE__espresso_events_Messages_Hooks_Extend__messages_metabox__before_content');
208
+		echo $notices . '<div class="messages-tabs-content">' . $tabbed_content . '</div>';
209
+		do_action('AHEE__espresso_events_Messages_Hooks_Extend__messages_metabox__after_content');
210
+	}
211
+
212
+
213
+	/**
214
+	 * Ajax callback for ee_msgs_create_new_custom ajax request.
215
+	 * Takes incoming GRP_ID and name and description values from ajax request
216
+	 * to create a new custom template based off of the incoming GRP_ID.
217
+	 *
218
+	 * @access public
219
+	 * @return string either an html string will be returned or a success message
220
+	 * @throws EE_Error
221
+	 */
222
+	public function create_new_custom()
223
+	{
224
+		if (! EE_Registry::instance()->CAP->current_user_can('ee_edit_messages', 'create_new_custom_ajax')) {
225
+			wp_die(__('You don\'t have privileges to do this action', 'event_espresso'));
226
+		}
227
+
228
+		// let's clean up the _POST global a bit for downstream usage of name and description.
229
+		$_POST['templateName'] = ! empty($this->_req_data['custom_template_args']['MTP_name'])
230
+			? $this->_req_data['custom_template_args']['MTP_name']
231
+			: '';
232
+		$_POST['templateDescription'] = ! empty($this->_req_data['custom_template_args']['MTP_description'])
233
+			? $this->_req_data['custom_template_args']['MTP_description']
234
+			: '';
235
+
236
+		// is this a template switch if so EE_Admin_Page child needs this object
237
+		$this->_page_object->set_hook_object($this);
238
+
239
+		$this->_page_object->add_message_template(
240
+			$this->_req_data['messageType'],
241
+			$this->_req_data['messenger'],
242
+			$this->_req_data['group_ID']
243
+		);
244
+	}
245
+
246
+
247
+	public function create_new_admin_footer()
248
+	{
249
+		$this->edit_admin_footer();
250
+	}
251
+
252
+
253
+	/**
254
+	 * This is the dynamic method for this class
255
+	 * that will end up hooking into the 'admin_footer' hook on the 'edit_event' route in the events page.
256
+	 *
257
+	 * @return string
258
+	 * @throws DomainException
259
+	 */
260
+	public function edit_admin_footer()
261
+	{
262
+		EEH_Template::display_template(
263
+			EE_CORE_CAF_ADMIN_EXTEND . 'messages/templates/create_custom_template_form.template.php'
264
+		);
265
+	}
266
+
267
+
268
+	/**
269
+	 * Callback for AHEE__Extend_Events_Admin_Page___duplicate_event__after hook used to ensure new events duplicate
270
+	 * the assigned custom message templates.
271
+	 *
272
+	 * @param EE_Event $new_event
273
+	 * @param EE_Event $original_event
274
+	 * @throws EE_Error
275
+	 */
276
+	public function duplicate_custom_message_settings(EE_Event $new_event, EE_Event $original_event)
277
+	{
278
+		$message_template_groups = $original_event->get_many_related('Message_Template_Group');
279
+		foreach ($message_template_groups as $message_template_group) {
280
+			$new_event->_add_relation_to($message_template_group, 'Message_Template_Group');
281
+		}
282
+		// save new event
283
+		$new_event->save();
284
+	}
285 285
 }
Please login to merge, or discard this patch.
admin_pages/events/Events_Admin_Page.core.php 2 patches
Indentation   +2890 added lines, -2890 removed lines patch added patch discarded remove patch
@@ -17,2894 +17,2894 @@
 block discarded – undo
17 17
 class Events_Admin_Page extends EE_Admin_Page_CPT
18 18
 {
19 19
 
20
-    /**
21
-     * This will hold the event object for event_details screen.
22
-     *
23
-     * @access protected
24
-     * @var EE_Event $_event
25
-     */
26
-    protected $_event;
27
-
28
-
29
-    /**
30
-     * This will hold the category object for category_details screen.
31
-     *
32
-     * @var stdClass $_category
33
-     */
34
-    protected $_category;
35
-
36
-
37
-    /**
38
-     * This will hold the event model instance
39
-     *
40
-     * @var EEM_Event $_event_model
41
-     */
42
-    protected $_event_model;
43
-
44
-
45
-    /**
46
-     * @var EE_Event
47
-     */
48
-    protected $_cpt_model_obj = false;
49
-
50
-
51
-    /**
52
-     * @var NodeGroupDao
53
-     */
54
-    protected $model_obj_node_group_persister;
55
-
56
-
57
-    /**
58
-     * Initialize page props for this admin page group.
59
-     */
60
-    protected function _init_page_props()
61
-    {
62
-        $this->page_slug        = EVENTS_PG_SLUG;
63
-        $this->page_label       = EVENTS_LABEL;
64
-        $this->_admin_base_url  = EVENTS_ADMIN_URL;
65
-        $this->_admin_base_path = EVENTS_ADMIN;
66
-        $this->_cpt_model_names = [
67
-            'create_new' => 'EEM_Event',
68
-            'edit'       => 'EEM_Event',
69
-        ];
70
-        $this->_cpt_edit_routes = [
71
-            'espresso_events' => 'edit',
72
-        ];
73
-        add_action(
74
-            'AHEE__EE_Admin_Page_CPT__set_model_object__after_set_object',
75
-            [$this, 'verify_event_edit'],
76
-            10,
77
-            2
78
-        );
79
-    }
80
-
81
-
82
-    /**
83
-     * Sets the ajax hooks used for this admin page group.
84
-     */
85
-    protected function _ajax_hooks()
86
-    {
87
-        add_action('wp_ajax_ee_save_timezone_setting', [$this, 'save_timezonestring_setting']);
88
-    }
89
-
90
-
91
-    /**
92
-     * Sets the page properties for this admin page group.
93
-     */
94
-    protected function _define_page_props()
95
-    {
96
-        $this->_admin_page_title = EVENTS_LABEL;
97
-        $this->_labels           = [
98
-            'buttons'      => [
99
-                'add'             => esc_html__('Add New Event', 'event_espresso'),
100
-                'edit'            => esc_html__('Edit Event', 'event_espresso'),
101
-                'delete'          => esc_html__('Delete Event', 'event_espresso'),
102
-                'add_category'    => esc_html__('Add New Category', 'event_espresso'),
103
-                'edit_category'   => esc_html__('Edit Category', 'event_espresso'),
104
-                'delete_category' => esc_html__('Delete Category', 'event_espresso'),
105
-            ],
106
-            'editor_title' => [
107
-                'espresso_events' => esc_html__('Enter event title here', 'event_espresso'),
108
-            ],
109
-            'publishbox'   => [
110
-                'create_new'        => esc_html__('Save New Event', 'event_espresso'),
111
-                'edit'              => esc_html__('Update Event', 'event_espresso'),
112
-                'add_category'      => esc_html__('Save New Category', 'event_espresso'),
113
-                'edit_category'     => esc_html__('Update Category', 'event_espresso'),
114
-                'template_settings' => esc_html__('Update Settings', 'event_espresso'),
115
-            ],
116
-        ];
117
-    }
118
-
119
-
120
-    /**
121
-     * Sets the page routes property for this admin page group.
122
-     */
123
-    protected function _set_page_routes()
124
-    {
125
-        // load formatter helper
126
-        // load field generator helper
127
-        // is there a evt_id in the request?
128
-        $evt_id             = ! empty($this->_req_data['EVT_ID']) && ! is_array($this->_req_data['EVT_ID'])
129
-            ? $this->_req_data['EVT_ID']
130
-            : 0;
131
-        $evt_id             = ! empty($this->_req_data['post']) ? $this->_req_data['post'] : $evt_id;
132
-        $this->_page_routes = [
133
-            'default'                       => [
134
-                'func'       => '_events_overview_list_table',
135
-                'capability' => 'ee_read_events',
136
-            ],
137
-            'create_new'                    => [
138
-                'func'       => '_create_new_cpt_item',
139
-                'capability' => 'ee_edit_events',
140
-            ],
141
-            'edit'                          => [
142
-                'func'       => '_edit_cpt_item',
143
-                'capability' => 'ee_edit_event',
144
-                'obj_id'     => $evt_id,
145
-            ],
146
-            'copy_event'                    => [
147
-                'func'       => '_copy_events',
148
-                'capability' => 'ee_edit_event',
149
-                'obj_id'     => $evt_id,
150
-                'noheader'   => true,
151
-            ],
152
-            'trash_event'                   => [
153
-                'func'       => '_trash_or_restore_event',
154
-                'args'       => ['event_status' => 'trash'],
155
-                'capability' => 'ee_delete_event',
156
-                'obj_id'     => $evt_id,
157
-                'noheader'   => true,
158
-            ],
159
-            'trash_events'                  => [
160
-                'func'       => '_trash_or_restore_events',
161
-                'args'       => ['event_status' => 'trash'],
162
-                'capability' => 'ee_delete_events',
163
-                'noheader'   => true,
164
-            ],
165
-            'restore_event'                 => [
166
-                'func'       => '_trash_or_restore_event',
167
-                'args'       => ['event_status' => 'draft'],
168
-                'capability' => 'ee_delete_event',
169
-                'obj_id'     => $evt_id,
170
-                'noheader'   => true,
171
-            ],
172
-            'restore_events'                => [
173
-                'func'       => '_trash_or_restore_events',
174
-                'args'       => ['event_status' => 'draft'],
175
-                'capability' => 'ee_delete_events',
176
-                'noheader'   => true,
177
-            ],
178
-            'delete_event'                  => [
179
-                'func'       => '_delete_event',
180
-                'capability' => 'ee_delete_event',
181
-                'obj_id'     => $evt_id,
182
-                'noheader'   => true,
183
-            ],
184
-            'delete_events'                 => [
185
-                'func'       => '_delete_events',
186
-                'capability' => 'ee_delete_events',
187
-                'noheader'   => true,
188
-            ],
189
-            'view_report'                   => [
190
-                'func'       => '_view_report',
191
-                'capability' => 'ee_edit_events',
192
-            ],
193
-            'default_event_settings'        => [
194
-                'func'       => '_default_event_settings',
195
-                'capability' => 'manage_options',
196
-            ],
197
-            'update_default_event_settings' => [
198
-                'func'       => '_update_default_event_settings',
199
-                'capability' => 'manage_options',
200
-                'noheader'   => true,
201
-            ],
202
-            'template_settings'             => [
203
-                'func'       => '_template_settings',
204
-                'capability' => 'manage_options',
205
-            ],
206
-            // event category tab related
207
-            'add_category'                  => [
208
-                'func'       => '_category_details',
209
-                'capability' => 'ee_edit_event_category',
210
-                'args'       => ['add'],
211
-            ],
212
-            'edit_category'                 => [
213
-                'func'       => '_category_details',
214
-                'capability' => 'ee_edit_event_category',
215
-                'args'       => ['edit'],
216
-            ],
217
-            'delete_categories'             => [
218
-                'func'       => '_delete_categories',
219
-                'capability' => 'ee_delete_event_category',
220
-                'noheader'   => true,
221
-            ],
222
-            'delete_category'               => [
223
-                'func'       => '_delete_categories',
224
-                'capability' => 'ee_delete_event_category',
225
-                'noheader'   => true,
226
-            ],
227
-            'insert_category'               => [
228
-                'func'       => '_insert_or_update_category',
229
-                'args'       => ['new_category' => true],
230
-                'capability' => 'ee_edit_event_category',
231
-                'noheader'   => true,
232
-            ],
233
-            'update_category'               => [
234
-                'func'       => '_insert_or_update_category',
235
-                'args'       => ['new_category' => false],
236
-                'capability' => 'ee_edit_event_category',
237
-                'noheader'   => true,
238
-            ],
239
-            'category_list'                 => [
240
-                'func'       => '_category_list_table',
241
-                'capability' => 'ee_manage_event_categories',
242
-            ],
243
-            'preview_deletion'              => [
244
-                'func'       => 'previewDeletion',
245
-                'capability' => 'ee_delete_events',
246
-            ],
247
-            'confirm_deletion'              => [
248
-                'func'       => 'confirmDeletion',
249
-                'capability' => 'ee_delete_events',
250
-                'noheader'   => true,
251
-            ],
252
-        ];
253
-    }
254
-
255
-
256
-    /**
257
-     * Set the _page_config property for this admin page group.
258
-     */
259
-    protected function _set_page_config()
260
-    {
261
-        $this->_page_config = [
262
-            'default'                => [
263
-                'nav'           => [
264
-                    'label' => esc_html__('Overview', 'event_espresso'),
265
-                    'order' => 10,
266
-                ],
267
-                'list_table'    => 'Events_Admin_List_Table',
268
-                'help_tabs'     => [
269
-                    'events_overview_help_tab'                       => [
270
-                        'title'    => esc_html__('Events Overview', 'event_espresso'),
271
-                        'filename' => 'events_overview',
272
-                    ],
273
-                    'events_overview_table_column_headings_help_tab' => [
274
-                        'title'    => esc_html__('Events Overview Table Column Headings', 'event_espresso'),
275
-                        'filename' => 'events_overview_table_column_headings',
276
-                    ],
277
-                    'events_overview_filters_help_tab'               => [
278
-                        'title'    => esc_html__('Events Overview Filters', 'event_espresso'),
279
-                        'filename' => 'events_overview_filters',
280
-                    ],
281
-                    'events_overview_view_help_tab'                  => [
282
-                        'title'    => esc_html__('Events Overview Views', 'event_espresso'),
283
-                        'filename' => 'events_overview_views',
284
-                    ],
285
-                    'events_overview_other_help_tab'                 => [
286
-                        'title'    => esc_html__('Events Overview Other', 'event_espresso'),
287
-                        'filename' => 'events_overview_other',
288
-                    ],
289
-                ],
290
-                // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
291
-                // 'help_tour'     => [
292
-                //     'Event_Overview_Help_Tour',
293
-                //     // 'New_Features_Test_Help_Tour' for testing multiple help tour
294
-                // ],
295
-                'require_nonce' => false,
296
-                'qtips'         => ['EE_Event_List_Table_Tips'],
297
-            ],
298
-            'create_new'             => [
299
-                'nav'           => [
300
-                    'label'      => esc_html__('Add Event', 'event_espresso'),
301
-                    'order'      => 5,
302
-                    'persistent' => false,
303
-                ],
304
-                'metaboxes'     => ['_register_event_editor_meta_boxes'],
305
-                'help_tabs'     => [
306
-                    'event_editor_help_tab'                            => [
307
-                        'title'    => esc_html__('Event Editor', 'event_espresso'),
308
-                        'filename' => 'event_editor',
309
-                    ],
310
-                    'event_editor_title_richtexteditor_help_tab'       => [
311
-                        'title'    => esc_html__('Event Title & Rich Text Editor', 'event_espresso'),
312
-                        'filename' => 'event_editor_title_richtexteditor',
313
-                    ],
314
-                    'event_editor_venue_details_help_tab'              => [
315
-                        'title'    => esc_html__('Event Venue Details', 'event_espresso'),
316
-                        'filename' => 'event_editor_venue_details',
317
-                    ],
318
-                    'event_editor_event_datetimes_help_tab'            => [
319
-                        'title'    => esc_html__('Event Datetimes', 'event_espresso'),
320
-                        'filename' => 'event_editor_event_datetimes',
321
-                    ],
322
-                    'event_editor_event_tickets_help_tab'              => [
323
-                        'title'    => esc_html__('Event Tickets', 'event_espresso'),
324
-                        'filename' => 'event_editor_event_tickets',
325
-                    ],
326
-                    'event_editor_event_registration_options_help_tab' => [
327
-                        'title'    => esc_html__('Event Registration Options', 'event_espresso'),
328
-                        'filename' => 'event_editor_event_registration_options',
329
-                    ],
330
-                    'event_editor_tags_categories_help_tab'            => [
331
-                        'title'    => esc_html__('Event Tags & Categories', 'event_espresso'),
332
-                        'filename' => 'event_editor_tags_categories',
333
-                    ],
334
-                    'event_editor_questions_registrants_help_tab'      => [
335
-                        'title'    => esc_html__('Questions for Registrants', 'event_espresso'),
336
-                        'filename' => 'event_editor_questions_registrants',
337
-                    ],
338
-                    'event_editor_save_new_event_help_tab'             => [
339
-                        'title'    => esc_html__('Save New Event', 'event_espresso'),
340
-                        'filename' => 'event_editor_save_new_event',
341
-                    ],
342
-                    'event_editor_other_help_tab'                      => [
343
-                        'title'    => esc_html__('Event Other', 'event_espresso'),
344
-                        'filename' => 'event_editor_other',
345
-                    ],
346
-                ],
347
-                // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
348
-                // 'help_tour'     => [
349
-                //     'Event_Editor_Help_Tour',
350
-                // ],
351
-                'qtips'         => ['EE_Event_Editor_Decaf_Tips'],
352
-                'require_nonce' => false,
353
-            ],
354
-            'edit'                   => [
355
-                'nav'           => [
356
-                    'label'      => esc_html__('Edit Event', 'event_espresso'),
357
-                    'order'      => 5,
358
-                    'persistent' => false,
359
-                    'url'        => isset($this->_req_data['post'])
360
-                        ? EE_Admin_Page::add_query_args_and_nonce(
361
-                            ['post' => $this->_req_data['post'], 'action' => 'edit'],
362
-                            $this->_current_page_view_url
363
-                        )
364
-                        : $this->_admin_base_url,
365
-                ],
366
-                'metaboxes'     => ['_register_event_editor_meta_boxes'],
367
-                'help_tabs'     => [
368
-                    'event_editor_help_tab'                            => [
369
-                        'title'    => esc_html__('Event Editor', 'event_espresso'),
370
-                        'filename' => 'event_editor',
371
-                    ],
372
-                    'event_editor_title_richtexteditor_help_tab'       => [
373
-                        'title'    => esc_html__('Event Title & Rich Text Editor', 'event_espresso'),
374
-                        'filename' => 'event_editor_title_richtexteditor',
375
-                    ],
376
-                    'event_editor_venue_details_help_tab'              => [
377
-                        'title'    => esc_html__('Event Venue Details', 'event_espresso'),
378
-                        'filename' => 'event_editor_venue_details',
379
-                    ],
380
-                    'event_editor_event_datetimes_help_tab'            => [
381
-                        'title'    => esc_html__('Event Datetimes', 'event_espresso'),
382
-                        'filename' => 'event_editor_event_datetimes',
383
-                    ],
384
-                    'event_editor_event_tickets_help_tab'              => [
385
-                        'title'    => esc_html__('Event Tickets', 'event_espresso'),
386
-                        'filename' => 'event_editor_event_tickets',
387
-                    ],
388
-                    'event_editor_event_registration_options_help_tab' => [
389
-                        'title'    => esc_html__('Event Registration Options', 'event_espresso'),
390
-                        'filename' => 'event_editor_event_registration_options',
391
-                    ],
392
-                    'event_editor_tags_categories_help_tab'            => [
393
-                        'title'    => esc_html__('Event Tags & Categories', 'event_espresso'),
394
-                        'filename' => 'event_editor_tags_categories',
395
-                    ],
396
-                    'event_editor_questions_registrants_help_tab'      => [
397
-                        'title'    => esc_html__('Questions for Registrants', 'event_espresso'),
398
-                        'filename' => 'event_editor_questions_registrants',
399
-                    ],
400
-                    'event_editor_save_new_event_help_tab'             => [
401
-                        'title'    => esc_html__('Save New Event', 'event_espresso'),
402
-                        'filename' => 'event_editor_save_new_event',
403
-                    ],
404
-                    'event_editor_other_help_tab'                      => [
405
-                        'title'    => esc_html__('Event Other', 'event_espresso'),
406
-                        'filename' => 'event_editor_other',
407
-                    ],
408
-                ],
409
-                'require_nonce' => false,
410
-            ],
411
-            'default_event_settings' => [
412
-                'nav'           => [
413
-                    'label' => esc_html__('Default Settings', 'event_espresso'),
414
-                    'order' => 40,
415
-                ],
416
-                'metaboxes'     => array_merge($this->_default_espresso_metaboxes, ['_publish_post_box']),
417
-                'labels'        => [
418
-                    'publishbox' => esc_html__('Update Settings', 'event_espresso'),
419
-                ],
420
-                'help_tabs'     => [
421
-                    'default_settings_help_tab'        => [
422
-                        'title'    => esc_html__('Default Event Settings', 'event_espresso'),
423
-                        'filename' => 'events_default_settings',
424
-                    ],
425
-                    'default_settings_status_help_tab' => [
426
-                        'title'    => esc_html__('Default Registration Status', 'event_espresso'),
427
-                        'filename' => 'events_default_settings_status',
428
-                    ],
429
-                    'default_maximum_tickets_help_tab' => [
430
-                        'title'    => esc_html__('Default Maximum Tickets Per Order', 'event_espresso'),
431
-                        'filename' => 'events_default_settings_max_tickets',
432
-                    ],
433
-                ],
434
-                // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
435
-                // 'help_tour'     => ['Event_Default_Settings_Help_Tour'],
436
-                'require_nonce' => false,
437
-            ],
438
-            // template settings
439
-            'template_settings'      => [
440
-                'nav'           => [
441
-                    'label' => esc_html__('Templates', 'event_espresso'),
442
-                    'order' => 30,
443
-                ],
444
-                'metaboxes'     => $this->_default_espresso_metaboxes,
445
-                'help_tabs'     => [
446
-                    'general_settings_templates_help_tab' => [
447
-                        'title'    => esc_html__('Templates', 'event_espresso'),
448
-                        'filename' => 'general_settings_templates',
449
-                    ],
450
-                ],
451
-                // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
452
-                // 'help_tour'     => ['Templates_Help_Tour'],
453
-                'require_nonce' => false,
454
-            ],
455
-            // event category stuff
456
-            'add_category'           => [
457
-                'nav'           => [
458
-                    'label'      => esc_html__('Add Category', 'event_espresso'),
459
-                    'order'      => 15,
460
-                    'persistent' => false,
461
-                ],
462
-                'help_tabs'     => [
463
-                    'add_category_help_tab' => [
464
-                        'title'    => esc_html__('Add New Event Category', 'event_espresso'),
465
-                        'filename' => 'events_add_category',
466
-                    ],
467
-                ],
468
-                // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
469
-                // 'help_tour'     => ['Event_Add_Category_Help_Tour'],
470
-                'metaboxes'     => ['_publish_post_box'],
471
-                'require_nonce' => false,
472
-            ],
473
-            'edit_category'          => [
474
-                'nav'           => [
475
-                    'label'      => esc_html__('Edit Category', 'event_espresso'),
476
-                    'order'      => 15,
477
-                    'persistent' => false,
478
-                    'url'        => isset($this->_req_data['EVT_CAT_ID'])
479
-                        ? add_query_arg(
480
-                            ['EVT_CAT_ID' => $this->_req_data['EVT_CAT_ID']],
481
-                            $this->_current_page_view_url
482
-                        )
483
-                        : $this->_admin_base_url,
484
-                ],
485
-                'help_tabs'     => [
486
-                    'edit_category_help_tab' => [
487
-                        'title'    => esc_html__('Edit Event Category', 'event_espresso'),
488
-                        'filename' => 'events_edit_category',
489
-                    ],
490
-                ],
491
-                /*'help_tour' => ['Event_Edit_Category_Help_Tour'],*/
492
-                'metaboxes'     => ['_publish_post_box'],
493
-                'require_nonce' => false,
494
-            ],
495
-            'category_list'          => [
496
-                'nav'           => [
497
-                    'label' => esc_html__('Categories', 'event_espresso'),
498
-                    'order' => 20,
499
-                ],
500
-                'list_table'    => 'Event_Categories_Admin_List_Table',
501
-                'help_tabs'     => [
502
-                    'events_categories_help_tab'                       => [
503
-                        'title'    => esc_html__('Event Categories', 'event_espresso'),
504
-                        'filename' => 'events_categories',
505
-                    ],
506
-                    'events_categories_table_column_headings_help_tab' => [
507
-                        'title'    => esc_html__('Event Categories Table Column Headings', 'event_espresso'),
508
-                        'filename' => 'events_categories_table_column_headings',
509
-                    ],
510
-                    'events_categories_view_help_tab'                  => [
511
-                        'title'    => esc_html__('Event Categories Views', 'event_espresso'),
512
-                        'filename' => 'events_categories_views',
513
-                    ],
514
-                    'events_categories_other_help_tab'                 => [
515
-                        'title'    => esc_html__('Event Categories Other', 'event_espresso'),
516
-                        'filename' => 'events_categories_other',
517
-                    ],
518
-                ],
519
-                // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
520
-                // 'help_tour'     => [
521
-                //     'Event_Categories_Help_Tour',
522
-                // ],
523
-                'metaboxes'     => $this->_default_espresso_metaboxes,
524
-                'require_nonce' => false,
525
-            ],
526
-            'preview_deletion'       => [
527
-                'nav'           => [
528
-                    'label'      => esc_html__('Preview Deletion', 'event_espresso'),
529
-                    'order'      => 15,
530
-                    'persistent' => false,
531
-                    'url'        => '',
532
-                ],
533
-                'require_nonce' => false,
534
-            ],
535
-        ];
536
-        // only load EE_Event_Editor_Decaf_Tips if domain is not caffeinated
537
-        $domain = $this->loader->getShared('EventEspresso\core\domain\Domain');
538
-        if (! $domain->isCaffeinated()) {
539
-            $this->_page_config['create_new']['qtips'] = ['EE_Event_Editor_Decaf_Tips'];
540
-            $this->_page_config['edit']['qtips']       = ['EE_Event_Editor_Decaf_Tips'];
541
-        }
542
-    }
543
-
544
-
545
-    /**
546
-     * Used to register any global screen options if necessary for every route in this admin page group.
547
-     */
548
-    protected function _add_screen_options()
549
-    {
550
-    }
551
-
552
-
553
-    /**
554
-     * Implementing the screen options for the 'default' route.
555
-     *
556
-     * @throws InvalidArgumentException
557
-     * @throws InvalidDataTypeException
558
-     * @throws InvalidInterfaceException
559
-     */
560
-    protected function _add_screen_options_default()
561
-    {
562
-        $this->_per_page_screen_option();
563
-    }
564
-
565
-
566
-    /**
567
-     * Implementing screen options for the category list route.
568
-     *
569
-     * @throws InvalidArgumentException
570
-     * @throws InvalidDataTypeException
571
-     * @throws InvalidInterfaceException
572
-     */
573
-    protected function _add_screen_options_category_list()
574
-    {
575
-        $page_title              = $this->_admin_page_title;
576
-        $this->_admin_page_title = esc_html__('Categories', 'event_espresso');
577
-        $this->_per_page_screen_option();
578
-        $this->_admin_page_title = $page_title;
579
-    }
580
-
581
-
582
-    /**
583
-     * Used to register any global feature pointers for the admin page group.
584
-     */
585
-    protected function _add_feature_pointers()
586
-    {
587
-    }
588
-
589
-
590
-    /**
591
-     * Registers and enqueues any global scripts and styles for the entire admin page group.
592
-     */
593
-    public function load_scripts_styles()
594
-    {
595
-        wp_register_style(
596
-            'events-admin-css',
597
-            EVENTS_ASSETS_URL . 'events-admin-page.css',
598
-            [],
599
-            EVENT_ESPRESSO_VERSION
600
-        );
601
-        wp_register_style(
602
-            'ee-cat-admin',
603
-            EVENTS_ASSETS_URL . 'ee-cat-admin.css',
604
-            [],
605
-            EVENT_ESPRESSO_VERSION
606
-        );
607
-        wp_enqueue_style('events-admin-css');
608
-        wp_enqueue_style('ee-cat-admin');
609
-        // scripts
610
-        wp_register_script(
611
-            'event_editor_js',
612
-            EVENTS_ASSETS_URL . 'event_editor.js',
613
-            ['ee_admin_js', 'jquery-ui-slider', 'jquery-ui-timepicker-addon'],
614
-            EVENT_ESPRESSO_VERSION,
615
-            true
616
-        );
617
-    }
618
-
619
-
620
-    /**
621
-     * Enqueuing scripts and styles specific to this view
622
-     */
623
-    public function load_scripts_styles_create_new()
624
-    {
625
-        $this->load_scripts_styles_edit();
626
-    }
627
-
628
-
629
-    /**
630
-     * Enqueuing scripts and styles specific to this view
631
-     */
632
-    public function load_scripts_styles_edit()
633
-    {
634
-        // styles
635
-        wp_enqueue_style('espresso-ui-theme');
636
-        wp_register_style(
637
-            'event-editor-css',
638
-            EVENTS_ASSETS_URL . 'event-editor.css',
639
-            ['ee-admin-css'],
640
-            EVENT_ESPRESSO_VERSION
641
-        );
642
-        wp_enqueue_style('event-editor-css');
643
-        // scripts
644
-        if (! $this->admin_config->useAdvancedEditor()) {
645
-            wp_register_script(
646
-                'event-datetime-metabox',
647
-                EVENTS_ASSETS_URL . 'event-datetime-metabox.js',
648
-                ['event_editor_js', 'ee-datepicker'],
649
-                EVENT_ESPRESSO_VERSION
650
-            );
651
-            wp_enqueue_script('event-datetime-metabox');
652
-        }
653
-    }
654
-
655
-
656
-    /**
657
-     * Populating the _views property for the category list table view.
658
-     */
659
-    protected function _set_list_table_views_category_list()
660
-    {
661
-        $this->_views = [
662
-            'all' => [
663
-                'slug'        => 'all',
664
-                'label'       => esc_html__('All', 'event_espresso'),
665
-                'count'       => 0,
666
-                'bulk_action' => [
667
-                    'delete_categories' => esc_html__('Delete Permanently', 'event_espresso'),
668
-                ],
669
-            ],
670
-        ];
671
-    }
672
-
673
-
674
-    /**
675
-     * For adding anything that fires on the admin_init hook for any route within this admin page group.
676
-     */
677
-    public function admin_init()
678
-    {
679
-        EE_Registry::$i18n_js_strings['image_confirm'] = esc_html__(
680
-            'Do you really want to delete this image? Please remember to update your event to complete the removal.',
681
-            'event_espresso'
682
-        );
683
-    }
684
-
685
-
686
-    /**
687
-     * For adding anything that should be triggered on the admin_notices hook for any route within this admin page
688
-     * group.
689
-     */
690
-    public function admin_notices()
691
-    {
692
-    }
693
-
694
-
695
-    /**
696
-     * For adding anything that should be triggered on the `admin_print_footer_scripts` hook for any route within
697
-     * this admin page group.
698
-     */
699
-    public function admin_footer_scripts()
700
-    {
701
-    }
702
-
703
-
704
-    /**
705
-     * Call this function to verify if an event is public and has tickets for sale.  If it does, then we need to show a
706
-     * warning (via EE_Error::add_error());
707
-     *
708
-     * @param EE_Event $event Event object
709
-     * @param string   $req_type
710
-     * @return void
711
-     * @throws EE_Error
712
-     * @access public
713
-     */
714
-    public function verify_event_edit($event = null, $req_type = '')
715
-    {
716
-        // don't need to do this when processing
717
-        if (! empty($req_type)) {
718
-            return;
719
-        }
720
-        // no event?
721
-        if (! $event instanceof EE_Event) {
722
-            $event = $this->_cpt_model_obj;
723
-        }
724
-        // STILL no event?
725
-        if (! $event instanceof EE_Event) {
726
-            return;
727
-        }
728
-        $orig_status = $event->status();
729
-        // first check if event is active.
730
-        if (
731
-            $orig_status === EEM_Event::cancelled
732
-            || $orig_status === EEM_Event::postponed
733
-            || $event->is_expired()
734
-            || $event->is_inactive()
735
-        ) {
736
-            return;
737
-        }
738
-        // made it here so it IS active... next check that any of the tickets are sold.
739
-        if ($event->is_sold_out(true)) {
740
-            if ($orig_status !== EEM_Event::sold_out && $event->status() !== $orig_status) {
741
-                EE_Error::add_attention(
742
-                    sprintf(
743
-                        esc_html__(
744
-                            'Please note that the Event Status has automatically been changed to %s because there are no more spaces available for this event.  However, this change is not permanent until you update the event.  You can change the status back to something else before updating if you wish.',
745
-                            'event_espresso'
746
-                        ),
747
-                        EEH_Template::pretty_status(EEM_Event::sold_out, false, 'sentence')
748
-                    )
749
-                );
750
-            }
751
-            return;
752
-        }
753
-        if ($orig_status === EEM_Event::sold_out) {
754
-            EE_Error::add_attention(
755
-                sprintf(
756
-                    esc_html__(
757
-                        'Please note that the Event Status has automatically been changed to %s because more spaces have become available for this event, most likely due to abandoned transactions freeing up reserved tickets.  However, this change is not permanent until you update the event. If you wish, you can change the status back to something else before updating.',
758
-                        'event_espresso'
759
-                    ),
760
-                    EEH_Template::pretty_status($event->status(), false, 'sentence')
761
-                )
762
-            );
763
-        }
764
-        // now we need to determine if the event has any tickets on sale.  If not then we dont' show the error
765
-        if (! $event->tickets_on_sale()) {
766
-            return;
767
-        }
768
-        // made it here so show warning
769
-        $this->_edit_event_warning();
770
-    }
771
-
772
-
773
-    /**
774
-     * This is the text used for when an event is being edited that is public and has tickets for sale.
775
-     * When needed, hook this into a EE_Error::add_error() notice.
776
-     *
777
-     * @access protected
778
-     * @return void
779
-     */
780
-    protected function _edit_event_warning()
781
-    {
782
-        // we don't want to add warnings during these requests
783
-        if (isset($this->_req_data['action']) && $this->_req_data['action'] === 'editpost') {
784
-            return;
785
-        }
786
-        EE_Error::add_attention(
787
-            sprintf(
788
-                esc_html__(
789
-                    'Your event is open for registration. Making changes may disrupt any transactions in progress. %sLearn more%s',
790
-                    'event_espresso'
791
-                ),
792
-                '<a class="espresso-help-tab-lnk">',
793
-                '</a>'
794
-            )
795
-        );
796
-    }
797
-
798
-
799
-    /**
800
-     * When a user is creating a new event, notify them if they haven't set their timezone.
801
-     * Otherwise, do the normal logic
802
-     *
803
-     * @return void
804
-     * @throws EE_Error
805
-     * @throws InvalidArgumentException
806
-     * @throws InvalidDataTypeException
807
-     * @throws InvalidInterfaceException
808
-     */
809
-    protected function _create_new_cpt_item()
810
-    {
811
-        $has_timezone_string = get_option('timezone_string');
812
-        // only nag them about setting their timezone if it's their first event, and they haven't already done it
813
-        if (! $has_timezone_string && ! EEM_Event::instance()->exists([])) {
814
-            EE_Error::add_attention(
815
-                sprintf(
816
-                    __(
817
-                        'Your website\'s timezone is currently set to a UTC offset. We recommend updating your timezone to a city or region near you before you create an event. Change your timezone now:%1$s%2$s%3$sChange Timezone%4$s',
818
-                        'event_espresso'
819
-                    ),
820
-                    '<br>',
821
-                    '<select id="timezone_string" name="timezone_string" aria-describedby="timezone-description">'
822
-                    . EEH_DTT_Helper::wp_timezone_choice('', EEH_DTT_Helper::get_user_locale())
823
-                    . '</select>',
824
-                    '<button class="button button-secondary timezone-submit">',
825
-                    '</button><span class="spinner"></span>'
826
-                ),
827
-                __FILE__,
828
-                __FUNCTION__,
829
-                __LINE__
830
-            );
831
-        }
832
-        parent::_create_new_cpt_item();
833
-    }
834
-
835
-
836
-    /**
837
-     * Sets the _views property for the default route in this admin page group.
838
-     */
839
-    protected function _set_list_table_views_default()
840
-    {
841
-        $this->_views = [
842
-            'all'   => [
843
-                'slug'        => 'all',
844
-                'label'       => esc_html__('View All Events', 'event_espresso'),
845
-                'count'       => 0,
846
-                'bulk_action' => [
847
-                    'trash_events' => esc_html__('Move to Trash', 'event_espresso'),
848
-                ],
849
-            ],
850
-            'draft' => [
851
-                'slug'        => 'draft',
852
-                'label'       => esc_html__('Draft', 'event_espresso'),
853
-                'count'       => 0,
854
-                'bulk_action' => [
855
-                    'trash_events' => esc_html__('Move to Trash', 'event_espresso'),
856
-                ],
857
-            ],
858
-        ];
859
-        if (EE_Registry::instance()->CAP->current_user_can('ee_delete_events', 'espresso_events_trash_events')) {
860
-            $this->_views['trash'] = [
861
-                'slug'        => 'trash',
862
-                'label'       => esc_html__('Trash', 'event_espresso'),
863
-                'count'       => 0,
864
-                'bulk_action' => [
865
-                    'restore_events' => esc_html__('Restore From Trash', 'event_espresso'),
866
-                    'delete_events'  => esc_html__('Delete Permanently', 'event_espresso'),
867
-                ],
868
-            ];
869
-        }
870
-    }
871
-
872
-
873
-    /**
874
-     * Provides the legend item array for the default list table view.
875
-     *
876
-     * @return array
877
-     */
878
-    protected function _event_legend_items()
879
-    {
880
-        $items    = [
881
-            'view_details'   => [
882
-                'class' => 'dashicons dashicons-search',
883
-                'desc'  => esc_html__('View Event', 'event_espresso'),
884
-            ],
885
-            'edit_event'     => [
886
-                'class' => 'ee-icon ee-icon-calendar-edit',
887
-                'desc'  => esc_html__('Edit Event Details', 'event_espresso'),
888
-            ],
889
-            'view_attendees' => [
890
-                'class' => 'dashicons dashicons-groups',
891
-                'desc'  => esc_html__('View Registrations for Event', 'event_espresso'),
892
-            ],
893
-        ];
894
-        $items    = apply_filters('FHEE__Events_Admin_Page___event_legend_items__items', $items);
895
-        $statuses = [
896
-            'sold_out_status'  => [
897
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::sold_out,
898
-                'desc'  => EEH_Template::pretty_status(EE_Datetime::sold_out, false, 'sentence'),
899
-            ],
900
-            'active_status'    => [
901
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::active,
902
-                'desc'  => EEH_Template::pretty_status(EE_Datetime::active, false, 'sentence'),
903
-            ],
904
-            'upcoming_status'  => [
905
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::upcoming,
906
-                'desc'  => EEH_Template::pretty_status(EE_Datetime::upcoming, false, 'sentence'),
907
-            ],
908
-            'postponed_status' => [
909
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::postponed,
910
-                'desc'  => EEH_Template::pretty_status(EE_Datetime::postponed, false, 'sentence'),
911
-            ],
912
-            'cancelled_status' => [
913
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::cancelled,
914
-                'desc'  => EEH_Template::pretty_status(EE_Datetime::cancelled, false, 'sentence'),
915
-            ],
916
-            'expired_status'   => [
917
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::expired,
918
-                'desc'  => EEH_Template::pretty_status(EE_Datetime::expired, false, 'sentence'),
919
-            ],
920
-            'inactive_status'  => [
921
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::inactive,
922
-                'desc'  => EEH_Template::pretty_status(EE_Datetime::inactive, false, 'sentence'),
923
-            ],
924
-        ];
925
-        $statuses = apply_filters('FHEE__Events_Admin_Page__event_legend_items__statuses', $statuses);
926
-        return array_merge($items, $statuses);
927
-    }
928
-
929
-
930
-    /**
931
-     * @return EEM_Event
932
-     * @throws EE_Error
933
-     * @throws InvalidArgumentException
934
-     * @throws InvalidDataTypeException
935
-     * @throws InvalidInterfaceException
936
-     * @throws ReflectionException
937
-     */
938
-    private function _event_model()
939
-    {
940
-        if (! $this->_event_model instanceof EEM_Event) {
941
-            $this->_event_model = EE_Registry::instance()->load_model('Event');
942
-        }
943
-        return $this->_event_model;
944
-    }
945
-
946
-
947
-    /**
948
-     * Adds extra buttons to the WP CPT permalink field row.
949
-     * Method is called from parent and is hooked into the wp 'get_sample_permalink_html' filter.
950
-     *
951
-     * @param string $return    the current html
952
-     * @param int    $id        the post id for the page
953
-     * @param string $new_title What the title is
954
-     * @param string $new_slug  what the slug is
955
-     * @return string            The new html string for the permalink area
956
-     */
957
-    public function extra_permalink_field_buttons($return, $id, $new_title, $new_slug)
958
-    {
959
-        // make sure this is only when editing
960
-        if (! empty($id)) {
961
-            $post   = get_post($id);
962
-            $return .= '<a class="button button-small" onclick="prompt(\'Shortcode:\', jQuery(\'#shortcode\').val()); return false;" href="#"  tabindex="-1">'
963
-                       . esc_html__('Shortcode', 'event_espresso')
964
-                       . '</a> ';
965
-            $return .= '<input id="shortcode" type="hidden" value="[ESPRESSO_TICKET_SELECTOR event_id='
966
-                       . $post->ID
967
-                       . ']">';
968
-        }
969
-        return $return;
970
-    }
971
-
972
-
973
-    /**
974
-     * _events_overview_list_table
975
-     * This contains the logic for showing the events_overview list
976
-     *
977
-     * @access protected
978
-     * @return void
979
-     * @throws DomainException
980
-     * @throws EE_Error
981
-     * @throws InvalidArgumentException
982
-     * @throws InvalidDataTypeException
983
-     * @throws InvalidInterfaceException
984
-     */
985
-    protected function _events_overview_list_table()
986
-    {
987
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
988
-        $this->_template_args['after_list_table']                           =
989
-            ! empty($this->_template_args['after_list_table'])
990
-                ? (array) $this->_template_args['after_list_table']
991
-                : [];
992
-        $this->_template_args['after_list_table']['view_event_list_button'] = EEH_HTML::br()
993
-            . EEH_Template::get_button_or_link(
994
-                get_post_type_archive_link('espresso_events'),
995
-                esc_html__('View Event Archive Page', 'event_espresso'),
996
-                'button'
997
-            );
998
-        $this->_template_args['after_list_table']['legend']                 =
999
-            $this->_display_legend($this->_event_legend_items());
1000
-        $this->_admin_page_title                                            .= ' '
1001
-            . $this->get_action_link_or_button(
1002
-                'create_new',
1003
-                'add',
1004
-                [],
1005
-                'add-new-h2'
1006
-            );
1007
-        $this->display_admin_list_table_page_with_no_sidebar();
1008
-    }
1009
-
1010
-
1011
-    /**
1012
-     * this allows for extra misc actions in the default WP publish box
1013
-     *
1014
-     * @return void
1015
-     * @throws DomainException
1016
-     * @throws EE_Error
1017
-     * @throws InvalidArgumentException
1018
-     * @throws InvalidDataTypeException
1019
-     * @throws InvalidInterfaceException
1020
-     * @throws ReflectionException
1021
-     */
1022
-    public function extra_misc_actions_publish_box()
1023
-    {
1024
-        $this->_generate_publish_box_extra_content();
1025
-    }
1026
-
1027
-
1028
-    /**
1029
-     * This is hooked into the WordPress do_action('save_post') hook and runs after the custom post type has been
1030
-     * saved.
1031
-     * Typically you would use this to save any additional data.
1032
-     * Keep in mind also that "save_post" runs on EVERY post update to the database.
1033
-     * ALSO very important.  When a post transitions from scheduled to published,
1034
-     * the save_post action is fired but you will NOT have any _POST data containing any extra info you may have from
1035
-     * other meta saves. So MAKE sure that you handle this accordingly.
1036
-     *
1037
-     * @access protected
1038
-     * @abstract
1039
-     * @param string $post_id The ID of the cpt that was saved (so you can link relationally)
1040
-     * @param object $post    The post object of the cpt that was saved.
1041
-     * @return void
1042
-     * @throws EE_Error
1043
-     * @throws InvalidArgumentException
1044
-     * @throws InvalidDataTypeException
1045
-     * @throws InvalidInterfaceException
1046
-     * @throws ReflectionException
1047
-     */
1048
-    protected function _insert_update_cpt_item($post_id, $post)
1049
-    {
1050
-        if ($post instanceof WP_Post && $post->post_type !== 'espresso_events') {
1051
-            // get out we're not processing an event save.
1052
-            return;
1053
-        }
1054
-        $event_values = [
1055
-            'EVT_member_only'     => ! empty($this->_req_data['member_only']) ? 1 : 0,
1056
-            'EVT_allow_overflow'  => ! empty($this->_req_data['EVT_allow_overflow']) ? 1 : 0,
1057
-            'EVT_timezone_string' => ! empty($this->_req_data['timezone_string'])
1058
-                ? sanitize_text_field($this->_req_data['timezone_string'])
1059
-                : null,
1060
-        ];
1061
-        /** @var FeatureFlags $flags */
1062
-        $flags = $this->loader->getShared('EventEspresso\core\domain\services\capabilities\FeatureFlags');
1063
-        // check if the new EDTR reg options meta box is being used, and if so, don't run updates for legacy version
1064
-        if (! $this->admin_config->useAdvancedEditor() || ! $flags->featureAllowed('use_reg_options_meta_box')) {
1065
-            $event_values['EVT_display_ticket_selector']     =
1066
-                ! empty($this->_req_data['display_ticket_selector'])
1067
-                    ? 1
1068
-                    : 0;
1069
-            $event_values['EVT_additional_limit']            = min(
1070
-                apply_filters('FHEE__EE_Events_Admin__insert_update_cpt_item__EVT_additional_limit_max', 255),
1071
-                ! empty($this->_req_data['additional_limit'])
1072
-                    ? absint($this->_req_data['additional_limit'])
1073
-                    : null
1074
-            );
1075
-            $event_values['EVT_default_registration_status'] =
1076
-                ! empty($this->_req_data['EVT_default_registration_status'])
1077
-                    ? sanitize_text_field($this->_req_data['EVT_default_registration_status'])
1078
-                    : EE_Registry::instance()->CFG->registration->default_STS_ID;
1079
-            $event_values['EVT_external_URL']                = ! empty($this->_req_data['externalURL'])
1080
-                ? esc_url_raw($this->_req_data['externalURL'])
1081
-                : null;
1082
-            $event_values['EVT_phone']                       = ! empty($this->_req_data['event_phone'])
1083
-                ? sanitize_text_field($this->_req_data['event_phone'])
1084
-                : null;
1085
-        }
1086
-        // update event
1087
-        $success = $this->_event_model()->update_by_ID($event_values, $post_id);
1088
-        // get event_object for other metaboxes...
1089
-        // though it would seem to make sense to just use $this->_event_model()->get_one_by_ID( $post_id )..
1090
-        // i have to setup where conditions to override the filters in the model that filter out autodraft
1091
-        // and inherit statuses so we GET the inherit id!
1092
-        $get_one_where = [
1093
-            $this->_event_model()->primary_key_name() => $post_id,
1094
-            'OR'                                      => [
1095
-                'status'   => $post->post_status,
1096
-                // if trying to "Publish" a sold out event, it's status will get switched back to "sold_out" in the db,
1097
-                // but the returned object here has a status of "publish", so use the original post status as well
1098
-                'status*1' => $this->_req_data['original_post_status'],
1099
-            ],
1100
-        ];
1101
-        $event         = $this->_event_model()->get_one([$get_one_where]);
1102
-        // the following are default callbacks for event attachment updates
1103
-        // that can be overridden by caffeinated functionality and/or addons.
1104
-        $event_update_callbacks = apply_filters(
1105
-            'FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks',
1106
-            [
1107
-                [$this, '_default_venue_update'],
1108
-                [$this, '_default_tickets_update'],
1109
-            ]
1110
-        );
1111
-        $att_success            = true;
1112
-        foreach ($event_update_callbacks as $e_callback) {
1113
-            $_success = is_callable($e_callback)
1114
-                ? $e_callback($event, $this->_req_data)
1115
-                : false;
1116
-            // if ANY of these updates fail then we want the appropriate global error message
1117
-            $att_success = ! $att_success ? $att_success : $_success;
1118
-        }
1119
-        // any errors?
1120
-        if ($success && false === $att_success) {
1121
-            EE_Error::add_error(
1122
-                esc_html__(
1123
-                    'Event Details saved successfully but something went wrong with saving attachments.',
1124
-                    'event_espresso'
1125
-                ),
1126
-                __FILE__,
1127
-                __FUNCTION__,
1128
-                __LINE__
1129
-            );
1130
-        } elseif ($success === false) {
1131
-            EE_Error::add_error(
1132
-                esc_html__('Event Details did not save successfully.', 'event_espresso'),
1133
-                __FILE__,
1134
-                __FUNCTION__,
1135
-                __LINE__
1136
-            );
1137
-        }
1138
-    }
1139
-
1140
-
1141
-    /**
1142
-     * @param int $post_id
1143
-     * @param int $revision_id
1144
-     * @throws EE_Error
1145
-     * @throws InvalidArgumentException
1146
-     * @throws InvalidDataTypeException
1147
-     * @throws InvalidInterfaceException
1148
-     * @throws ReflectionException
1149
-     * @see parent::restore_item()
1150
-     */
1151
-    protected function _restore_cpt_item($post_id, $revision_id)
1152
-    {
1153
-        // copy existing event meta to new post
1154
-        $post_evt = $this->_event_model()->get_one_by_ID($post_id);
1155
-        if ($post_evt instanceof EE_Event) {
1156
-            // meta revision restore
1157
-            $post_evt->restore_revision($revision_id);
1158
-            // related objs restore
1159
-            $post_evt->restore_revision($revision_id, ['Venue', 'Datetime', 'Price']);
1160
-        }
1161
-    }
1162
-
1163
-
1164
-    /**
1165
-     * Attach the venue to the Event
1166
-     *
1167
-     * @param EE_Event $evtobj Event Object to add the venue to
1168
-     * @param array    $data   The request data from the form
1169
-     * @return bool           Success or fail.
1170
-     * @throws EE_Error
1171
-     * @throws InvalidArgumentException
1172
-     * @throws InvalidDataTypeException
1173
-     * @throws InvalidInterfaceException
1174
-     * @throws ReflectionException
1175
-     */
1176
-    protected function _default_venue_update(EE_Event $evtobj, $data)
1177
-    {
1178
-        require_once(EE_MODELS . 'EEM_Venue.model.php');
1179
-        $venue_model   = EE_Registry::instance()->load_model('Venue');
1180
-        $rows_affected = null;
1181
-        $venue_id      = ! empty($data['venue_id']) ? $data['venue_id'] : null;
1182
-        // very important.  If we don't have a venue name...
1183
-        // then we'll get out because not necessary to create empty venue
1184
-        if (empty($data['venue_title'])) {
1185
-            return false;
1186
-        }
1187
-        $venue_array = [
1188
-            'VNU_wp_user'         => $evtobj->get('EVT_wp_user'),
1189
-            'VNU_name'            => ! empty($data['venue_title']) ? $data['venue_title'] : null,
1190
-            'VNU_desc'            => ! empty($data['venue_description']) ? $data['venue_description'] : null,
1191
-            'VNU_identifier'      => ! empty($data['venue_identifier']) ? $data['venue_identifier'] : null,
1192
-            'VNU_short_desc'      => ! empty($data['venue_short_description']) ? $data['venue_short_description']
1193
-                : null,
1194
-            'VNU_address'         => ! empty($data['address']) ? $data['address'] : null,
1195
-            'VNU_address2'        => ! empty($data['address2']) ? $data['address2'] : null,
1196
-            'VNU_city'            => ! empty($data['city']) ? $data['city'] : null,
1197
-            'STA_ID'              => ! empty($data['state']) ? $data['state'] : null,
1198
-            'CNT_ISO'             => ! empty($data['countries']) ? $data['countries'] : null,
1199
-            'VNU_zip'             => ! empty($data['zip']) ? $data['zip'] : null,
1200
-            'VNU_phone'           => ! empty($data['venue_phone']) ? $data['venue_phone'] : null,
1201
-            'VNU_capacity'        => ! empty($data['venue_capacity']) ? $data['venue_capacity'] : null,
1202
-            'VNU_url'             => ! empty($data['venue_url']) ? $data['venue_url'] : null,
1203
-            'VNU_virtual_phone'   => ! empty($data['virtual_phone']) ? $data['virtual_phone'] : null,
1204
-            'VNU_virtual_url'     => ! empty($data['virtual_url']) ? $data['virtual_url'] : null,
1205
-            'VNU_enable_for_gmap' => isset($data['enable_for_gmap']) ? 1 : 0,
1206
-            'status'              => 'publish',
1207
-        ];
1208
-        // if we've got the venue_id then we're just updating the existing venue so let's do that and then get out.
1209
-        if (! empty($venue_id)) {
1210
-            $update_where  = [$venue_model->primary_key_name() => $venue_id];
1211
-            $rows_affected = $venue_model->update($venue_array, [$update_where]);
1212
-            // we've gotta make sure that the venue is always attached to a revision..
1213
-            // add_relation_to should take care of making sure that the relation is already present.
1214
-            $evtobj->_add_relation_to($venue_id, 'Venue');
1215
-            return $rows_affected > 0;
1216
-        }
1217
-        // we insert the venue
1218
-        $venue_id = $venue_model->insert($venue_array);
1219
-        $evtobj->_add_relation_to($venue_id, 'Venue');
1220
-        return ! empty($venue_id);
1221
-        // when we have the ancestor come in it's already been handled by the revision save.
1222
-    }
1223
-
1224
-
1225
-    /**
1226
-     * Handles saving everything related to Tickets (datetimes, tickets, prices)
1227
-     *
1228
-     * @param EE_Event $evtobj The Event object we're attaching data to
1229
-     * @param array    $data   The request data from the form
1230
-     * @return array
1231
-     * @throws EE_Error
1232
-     * @throws InvalidArgumentException
1233
-     * @throws InvalidDataTypeException
1234
-     * @throws InvalidInterfaceException
1235
-     * @throws ReflectionException
1236
-     * @throws Exception
1237
-     */
1238
-    protected function _default_tickets_update(EE_Event $evtobj, $data)
1239
-    {
1240
-        if ($this->admin_config->useAdvancedEditor()) {
1241
-            return [];
1242
-        }
1243
-        $success               = true;
1244
-        $saved_dtt             = null;
1245
-        $saved_tickets         = [];
1246
-        $incoming_date_formats = ['Y-m-d', 'h:i a'];
1247
-        foreach ($data['edit_event_datetimes'] as $row => $dtt) {
1248
-            // trim all values to ensure any excess whitespace is removed.
1249
-            $dtt                = array_map('trim', $dtt);
1250
-            $dtt['DTT_EVT_end'] = isset($dtt['DTT_EVT_end']) && ! empty($dtt['DTT_EVT_end']) ? $dtt['DTT_EVT_end']
1251
-                : $dtt['DTT_EVT_start'];
1252
-            $datetime_values    = [
1253
-                'DTT_ID'        => ! empty($dtt['DTT_ID']) ? $dtt['DTT_ID'] : null,
1254
-                'DTT_EVT_start' => $dtt['DTT_EVT_start'],
1255
-                'DTT_EVT_end'   => $dtt['DTT_EVT_end'],
1256
-                'DTT_reg_limit' => empty($dtt['DTT_reg_limit']) ? EE_INF : $dtt['DTT_reg_limit'],
1257
-                'DTT_order'     => $row,
1258
-            ];
1259
-            // if we have an id then let's get existing object first and then set the new values.
1260
-            //  Otherwise we instantiate a new object for save.
1261
-            if (! empty($dtt['DTT_ID'])) {
1262
-                $DTM = EE_Registry::instance()
1263
-                                  ->load_model('Datetime', [$evtobj->get_timezone()])
1264
-                                  ->get_one_by_ID($dtt['DTT_ID']);
1265
-                $DTM->set_date_format($incoming_date_formats[0]);
1266
-                $DTM->set_time_format($incoming_date_formats[1]);
1267
-                foreach ($datetime_values as $field => $value) {
1268
-                    $DTM->set($field, $value);
1269
-                }
1270
-                // make sure the $dtt_id here is saved in case after the add_relation_to() the autosave replaces it.
1271
-                // We need to do this so we dont' TRASH the parent DTT.
1272
-                $saved_dtts[ $DTM->ID() ] = $DTM;
1273
-            } else {
1274
-                $DTM = EE_Registry::instance()->load_class(
1275
-                    'Datetime',
1276
-                    [$datetime_values, $evtobj->get_timezone(), $incoming_date_formats],
1277
-                    false,
1278
-                    false
1279
-                );
1280
-                foreach ($datetime_values as $field => $value) {
1281
-                    $DTM->set($field, $value);
1282
-                }
1283
-            }
1284
-            $DTM->save();
1285
-            $DTT = $evtobj->_add_relation_to($DTM, 'Datetime');
1286
-            // load DTT helper
1287
-            // before going any further make sure our dates are setup correctly
1288
-            // so that the end date is always equal or greater than the start date.
1289
-            if ($DTT->get_raw('DTT_EVT_start') > $DTT->get_raw('DTT_EVT_end')) {
1290
-                $DTT->set('DTT_EVT_end', $DTT->get('DTT_EVT_start'));
1291
-                $DTT = EEH_DTT_Helper::date_time_add($DTT, 'DTT_EVT_end', 'days');
1292
-                $DTT->save();
1293
-            }
1294
-            // now we got to make sure we add the new DTT_ID to the $saved_dtts array
1295
-            //  because it is possible there was a new one created for the autosave.
1296
-            $saved_dtt = $DTT;
1297
-            $success   = ! $success ? $success : $DTT;
1298
-            // if ANY of these updates fail then we want the appropriate global error message.
1299
-            // //todo this is actually sucky we need a better error message but this is what it is for now.
1300
-        }
1301
-        // no dtts get deleted so we don't do any of that logic here.
1302
-        // update tickets next
1303
-        $old_tickets = isset($data['ticket_IDs']) ? explode(',', $data['ticket_IDs']) : [];
1304
-        foreach ($data['edit_tickets'] as $row => $tkt) {
1305
-            $incoming_date_formats = ['Y-m-d', 'h:i a'];
1306
-            $update_prices         = false;
1307
-            $ticket_price          = isset($data['edit_prices'][ $row ][1]['PRC_amount'])
1308
-                ? $data['edit_prices'][ $row ][1]['PRC_amount']
1309
-                : 0;
1310
-            // trim inputs to ensure any excess whitespace is removed.
1311
-            $tkt = array_map('trim', $tkt);
1312
-            if (empty($tkt['TKT_start_date'])) {
1313
-                // let's use now in the set timezone.
1314
-                $now                   = new DateTime('now', new DateTimeZone($evtobj->get_timezone()));
1315
-                $tkt['TKT_start_date'] = $now->format($incoming_date_formats[0] . ' ' . $incoming_date_formats[1]);
1316
-            }
1317
-            if (empty($tkt['TKT_end_date'])) {
1318
-                // use the start date of the first datetime
1319
-                $dtt                 = $evtobj->first_datetime();
1320
-                $tkt['TKT_end_date'] = $dtt->start_date_and_time(
1321
-                    $incoming_date_formats[0],
1322
-                    $incoming_date_formats[1]
1323
-                );
1324
-            }
1325
-            $TKT_values = [
1326
-                'TKT_ID'          => ! empty($tkt['TKT_ID']) ? $tkt['TKT_ID'] : null,
1327
-                'TTM_ID'          => ! empty($tkt['TTM_ID']) ? $tkt['TTM_ID'] : 0,
1328
-                'TKT_name'        => ! empty($tkt['TKT_name']) ? $tkt['TKT_name'] : '',
1329
-                'TKT_description' => ! empty($tkt['TKT_description']) ? $tkt['TKT_description'] : '',
1330
-                'TKT_start_date'  => $tkt['TKT_start_date'],
1331
-                'TKT_end_date'    => $tkt['TKT_end_date'],
1332
-                'TKT_qty'         => ! isset($tkt['TKT_qty']) || $tkt['TKT_qty'] === '' ? EE_INF : $tkt['TKT_qty'],
1333
-                'TKT_uses'        => ! isset($tkt['TKT_uses']) || $tkt['TKT_uses'] === '' ? EE_INF : $tkt['TKT_uses'],
1334
-                'TKT_min'         => empty($tkt['TKT_min']) ? 0 : $tkt['TKT_min'],
1335
-                'TKT_max'         => empty($tkt['TKT_max']) ? EE_INF : $tkt['TKT_max'],
1336
-                'TKT_row'         => $row,
1337
-                'TKT_order'       => isset($tkt['TKT_order']) ? $tkt['TKT_order'] : $row,
1338
-                'TKT_price'       => $ticket_price,
1339
-            ];
1340
-            // if this is a default TKT, then we need to set the TKT_ID to 0 and update accordingly,
1341
-            // which means in turn that the prices will become new prices as well.
1342
-            if (isset($tkt['TKT_is_default']) && $tkt['TKT_is_default']) {
1343
-                $TKT_values['TKT_ID']         = 0;
1344
-                $TKT_values['TKT_is_default'] = 0;
1345
-                $TKT_values['TKT_price']      = $ticket_price;
1346
-                $update_prices                = true;
1347
-            }
1348
-            // if we have a TKT_ID then we need to get that existing TKT_obj and update it
1349
-            // we actually do our saves a head of doing any add_relations to because its entirely possible
1350
-            // that this ticket didn't removed or added to any datetime in the session but DID have it's items modified.
1351
-            // keep in mind that if the TKT has been sold (and we have changed pricing information),
1352
-            // then we won't be updating the tkt but instead a new tkt will be created and the old one archived.
1353
-            if (! empty($tkt['TKT_ID'])) {
1354
-                $TKT = EE_Registry::instance()
1355
-                                  ->load_model('Ticket', [$evtobj->get_timezone()])
1356
-                                  ->get_one_by_ID($tkt['TKT_ID']);
1357
-                if ($TKT instanceof EE_Ticket) {
1358
-                    $ticket_sold = $TKT->count_related(
1359
-                        'Registration',
1360
-                        [
1361
-                            [
1362
-                                'STS_ID' => [
1363
-                                    'NOT IN',
1364
-                                    [EEM_Registration::status_id_incomplete],
1365
-                                ],
1366
-                            ],
1367
-                        ]
1368
-                    ) > 0;
1369
-                    // let's just check the total price for the existing ticket and determine if it matches the new
1370
-                    // total price.  if they are different then we create a new ticket (if tickets sold)
1371
-                    // if they aren't different then we go ahead and modify existing ticket.
1372
-                    $create_new_TKT = $ticket_sold
1373
-                                      && ! $TKT->deleted()
1374
-                                      && EEH_Money::compare_floats(
1375
-                                          $ticket_price,
1376
-                                          $TKT->get('TKT_price'),
1377
-                                          '!=='
1378
-                                      );
1379
-                    $TKT->set_date_format($incoming_date_formats[0]);
1380
-                    $TKT->set_time_format($incoming_date_formats[1]);
1381
-                    // set new values
1382
-                    foreach ($TKT_values as $field => $value) {
1383
-                        if ($field === 'TKT_qty') {
1384
-                            $TKT->set_qty($value);
1385
-                        } else {
1386
-                            $TKT->set($field, $value);
1387
-                        }
1388
-                    }
1389
-                    // if $create_new_TKT is false then we can safely update the existing ticket.
1390
-                    //  Otherwise we have to create a new ticket.
1391
-                    if ($create_new_TKT) {
1392
-                        // archive the old ticket first
1393
-                        $TKT->set('TKT_deleted', 1);
1394
-                        $TKT->save();
1395
-                        // make sure this ticket is still recorded in our saved_tkts
1396
-                        // so we don't run it through the regular trash routine.
1397
-                        $saved_tickets[ $TKT->ID() ] = $TKT;
1398
-                        // create new ticket that's a copy of the existing except a new id of course
1399
-                        // (and not archived) AND has the new TKT_price associated with it.
1400
-                        $TKT = clone $TKT;
1401
-                        $TKT->set('TKT_ID', 0);
1402
-                        $TKT->set('TKT_deleted', 0);
1403
-                        $TKT->set('TKT_price', $ticket_price);
1404
-                        $TKT->set('TKT_sold', 0);
1405
-                        // now we need to make sure that $new prices are created as well and attached to new ticket.
1406
-                        $update_prices = true;
1407
-                    }
1408
-                    // make sure price is set if it hasn't been already
1409
-                    $TKT->set('TKT_price', $ticket_price);
1410
-                }
1411
-            } else {
1412
-                // no TKT_id so a new TKT
1413
-                $TKT_values['TKT_price'] = $ticket_price;
1414
-                $TKT                     = EE_Registry::instance()->load_class('Ticket', [$TKT_values], false, false);
1415
-                if ($TKT instanceof EE_Ticket) {
1416
-                    // need to reset values to properly account for the date formats
1417
-                    $TKT->set_date_format($incoming_date_formats[0]);
1418
-                    $TKT->set_time_format($incoming_date_formats[1]);
1419
-                    $TKT->set_timezone($evtobj->get_timezone());
1420
-                    // set new values
1421
-                    foreach ($TKT_values as $field => $value) {
1422
-                        if ($field === 'TKT_qty') {
1423
-                            $TKT->set_qty($value);
1424
-                        } else {
1425
-                            $TKT->set($field, $value);
1426
-                        }
1427
-                    }
1428
-                    $update_prices = true;
1429
-                }
1430
-            }
1431
-            // cap ticket qty by datetime reg limits
1432
-            $TKT->set_qty(min($TKT->qty(), $TKT->qty('reg_limit')));
1433
-            // update ticket.
1434
-            $TKT->save();
1435
-            // before going any further make sure our dates are setup correctly
1436
-            // so that the end date is always equal or greater than the start date.
1437
-            if ($TKT->get_raw('TKT_start_date') > $TKT->get_raw('TKT_end_date')) {
1438
-                $TKT->set('TKT_end_date', $TKT->get('TKT_start_date'));
1439
-                $TKT = EEH_DTT_Helper::date_time_add($TKT, 'TKT_end_date', 'days');
1440
-                $TKT->save();
1441
-            }
1442
-            // initially let's add the ticket to the dtt
1443
-            $saved_dtt->_add_relation_to($TKT, 'Ticket');
1444
-            $saved_tickets[ $TKT->ID() ] = $TKT;
1445
-            // add prices to ticket
1446
-            $this->_add_prices_to_ticket($data['edit_prices'][ $row ], $TKT, $update_prices);
1447
-        }
1448
-        // however now we need to handle permanently deleting tickets via the ui.
1449
-        //  Keep in mind that the ui does not allow deleting/archiving tickets that have ticket sold.
1450
-        //  However, it does allow for deleting tickets that have no tickets sold,
1451
-        // in which case we want to get rid of permanently because there is no need to save in db.
1452
-        $old_tickets     = isset($old_tickets[0]) && $old_tickets[0] === '' ? [] : $old_tickets;
1453
-        $tickets_removed = array_diff($old_tickets, array_keys($saved_tickets));
1454
-        foreach ($tickets_removed as $id) {
1455
-            $id = absint($id);
1456
-            // get the ticket for this id
1457
-            $tkt_to_remove = EE_Registry::instance()->load_model('Ticket')->get_one_by_ID($id);
1458
-            // need to get all the related datetimes on this ticket and remove from every single one of them
1459
-            // (remember this process can ONLY kick off if there are NO tkts_sold)
1460
-            $dtts = $tkt_to_remove->get_many_related('Datetime');
1461
-            foreach ($dtts as $dtt) {
1462
-                $tkt_to_remove->_remove_relation_to($dtt, 'Datetime');
1463
-            }
1464
-            // need to do the same for prices (except these prices can also be deleted because again,
1465
-            // tickets can only be trashed if they don't have any TKTs sold (otherwise they are just archived))
1466
-            $tkt_to_remove->delete_related_permanently('Price');
1467
-            // finally let's delete this ticket
1468
-            // (which should not be blocked at this point b/c we've removed all our relationships)
1469
-            $tkt_to_remove->delete_permanently();
1470
-        }
1471
-        return [$saved_dtt, $saved_tickets];
1472
-    }
1473
-
1474
-
1475
-    /**
1476
-     * This attaches a list of given prices to a ticket.
1477
-     * Note we dont' have to worry about ever removing relationships (or archiving prices)
1478
-     * because if there is a change in price information on a ticket, a new ticket is created anyways
1479
-     * so the archived ticket will retain the old price info and prices are automatically "archived" via the ticket.
1480
-     *
1481
-     * @access  private
1482
-     * @param array     $prices     Array of prices from the form.
1483
-     * @param EE_Ticket $ticket     EE_Ticket object that prices are being attached to.
1484
-     * @param bool      $new_prices Whether attach existing incoming prices or create new ones.
1485
-     * @return  void
1486
-     * @throws EE_Error
1487
-     * @throws InvalidArgumentException
1488
-     * @throws InvalidDataTypeException
1489
-     * @throws InvalidInterfaceException
1490
-     * @throws ReflectionException
1491
-     */
1492
-    private function _add_prices_to_ticket($prices, EE_Ticket $ticket, $new_prices = false)
1493
-    {
1494
-        foreach ($prices as $row => $prc) {
1495
-            $PRC_values = [
1496
-                'PRC_ID'         => ! empty($prc['PRC_ID']) ? $prc['PRC_ID'] : null,
1497
-                'PRT_ID'         => ! empty($prc['PRT_ID']) ? $prc['PRT_ID'] : null,
1498
-                'PRC_amount'     => ! empty($prc['PRC_amount']) ? $prc['PRC_amount'] : 0,
1499
-                'PRC_name'       => ! empty($prc['PRC_name']) ? $prc['PRC_name'] : '',
1500
-                'PRC_desc'       => ! empty($prc['PRC_desc']) ? $prc['PRC_desc'] : '',
1501
-                'PRC_is_default' => 0, // make sure prices are NOT set as default from this context
1502
-                'PRC_order'      => $row,
1503
-            ];
1504
-            if ($new_prices || empty($PRC_values['PRC_ID'])) {
1505
-                $PRC_values['PRC_ID'] = 0;
1506
-                $PRC                  = EE_Registry::instance()->load_class('Price', [$PRC_values], false, false);
1507
-            } else {
1508
-                $PRC = EE_Registry::instance()->load_model('Price')->get_one_by_ID($prc['PRC_ID']);
1509
-                // update this price with new values
1510
-                foreach ($PRC_values as $field => $newprc) {
1511
-                    $PRC->set($field, $newprc);
1512
-                }
1513
-                $PRC->save();
1514
-            }
1515
-            $ticket->_add_relation_to($PRC, 'Price');
1516
-        }
1517
-    }
1518
-
1519
-
1520
-    /**
1521
-     * Add in our autosave ajax handlers
1522
-     *
1523
-     */
1524
-    protected function _ee_autosave_create_new()
1525
-    {
1526
-    }
1527
-
1528
-
1529
-    /**
1530
-     * More autosave handlers.
1531
-     */
1532
-    protected function _ee_autosave_edit()
1533
-    {
1534
-    }
1535
-
1536
-
1537
-    /**
1538
-     *    _generate_publish_box_extra_content
1539
-     *
1540
-     * @throws DomainException
1541
-     * @throws EE_Error
1542
-     * @throws InvalidArgumentException
1543
-     * @throws InvalidDataTypeException
1544
-     * @throws InvalidInterfaceException
1545
-     * @throws ReflectionException
1546
-     */
1547
-    private function _generate_publish_box_extra_content()
1548
-    {
1549
-        // load formatter helper
1550
-        // args for getting related registrations
1551
-        $approved_query_args        = [
1552
-            [
1553
-                'REG_deleted' => 0,
1554
-                'STS_ID'      => EEM_Registration::status_id_approved,
1555
-            ],
1556
-        ];
1557
-        $not_approved_query_args    = [
1558
-            [
1559
-                'REG_deleted' => 0,
1560
-                'STS_ID'      => EEM_Registration::status_id_not_approved,
1561
-            ],
1562
-        ];
1563
-        $pending_payment_query_args = [
1564
-            [
1565
-                'REG_deleted' => 0,
1566
-                'STS_ID'      => EEM_Registration::status_id_pending_payment,
1567
-            ],
1568
-        ];
1569
-        // publish box
1570
-        $publish_box_extra_args = [
1571
-            'view_approved_reg_url'        => add_query_arg(
1572
-                [
1573
-                    'action'      => 'default',
1574
-                    'event_id'    => $this->_cpt_model_obj->ID(),
1575
-                    '_reg_status' => EEM_Registration::status_id_approved,
1576
-                ],
1577
-                REG_ADMIN_URL
1578
-            ),
1579
-            'view_not_approved_reg_url'    => add_query_arg(
1580
-                [
1581
-                    'action'      => 'default',
1582
-                    'event_id'    => $this->_cpt_model_obj->ID(),
1583
-                    '_reg_status' => EEM_Registration::status_id_not_approved,
1584
-                ],
1585
-                REG_ADMIN_URL
1586
-            ),
1587
-            'view_pending_payment_reg_url' => add_query_arg(
1588
-                [
1589
-                    'action'      => 'default',
1590
-                    'event_id'    => $this->_cpt_model_obj->ID(),
1591
-                    '_reg_status' => EEM_Registration::status_id_pending_payment,
1592
-                ],
1593
-                REG_ADMIN_URL
1594
-            ),
1595
-            'approved_regs'                => $this->_cpt_model_obj->count_related(
1596
-                'Registration',
1597
-                $approved_query_args
1598
-            ),
1599
-            'not_approved_regs'            => $this->_cpt_model_obj->count_related(
1600
-                'Registration',
1601
-                $not_approved_query_args
1602
-            ),
1603
-            'pending_payment_regs'         => $this->_cpt_model_obj->count_related(
1604
-                'Registration',
1605
-                $pending_payment_query_args
1606
-            ),
1607
-            'misc_pub_section_class'       => apply_filters(
1608
-                'FHEE_Events_Admin_Page___generate_publish_box_extra_content__misc_pub_section_class',
1609
-                'misc-pub-section'
1610
-            ),
1611
-        ];
1612
-        ob_start();
1613
-        do_action(
1614
-            'AHEE__Events_Admin_Page___generate_publish_box_extra_content__event_editor_overview_add',
1615
-            $this->_cpt_model_obj
1616
-        );
1617
-        $publish_box_extra_args['event_editor_overview_add'] = ob_get_clean();
1618
-        // load template
1619
-        EEH_Template::display_template(
1620
-            EVENTS_TEMPLATE_PATH . 'event_publish_box_extras.template.php',
1621
-            $publish_box_extra_args
1622
-        );
1623
-    }
1624
-
1625
-
1626
-    /**
1627
-     * @return EE_Event
1628
-     */
1629
-    public function get_event_object()
1630
-    {
1631
-        return $this->_cpt_model_obj;
1632
-    }
1633
-
1634
-
1635
-
1636
-
1637
-    /** METABOXES * */
1638
-    /**
1639
-     * _register_event_editor_meta_boxes
1640
-     * add all metaboxes related to the event_editor
1641
-     *
1642
-     * @return void
1643
-     * @throws EE_Error
1644
-     * @throws InvalidArgumentException
1645
-     * @throws InvalidDataTypeException
1646
-     * @throws InvalidInterfaceException
1647
-     * @throws ReflectionException
1648
-     */
1649
-    protected function _register_event_editor_meta_boxes()
1650
-    {
1651
-        $this->verify_cpt_object();
1652
-        $use_advanced_editor = $this->admin_config->useAdvancedEditor();
1653
-        /** @var FeatureFlags $flags */
1654
-        $flags = $this->loader->getShared('EventEspresso\core\domain\services\capabilities\FeatureFlags');
1655
-        // check if the new EDTR reg options meta box is being used, and if so, don't load the legacy version
1656
-        if (! $use_advanced_editor || ! $flags->featureAllowed('use_reg_options_meta_box')) {
1657
-            add_meta_box(
1658
-                'espresso_event_editor_event_options',
1659
-                esc_html__('Event Registration Options', 'event_espresso'),
1660
-                [$this, 'registration_options_meta_box'],
1661
-                $this->page_slug,
1662
-                'side'
1663
-            );
1664
-        }
1665
-        if (! $use_advanced_editor) {
1666
-            add_meta_box(
1667
-                'espresso_event_editor_tickets',
1668
-                esc_html__('Event Datetime & Ticket', 'event_espresso'),
1669
-                [$this, 'ticket_metabox'],
1670
-                $this->page_slug,
1671
-                'normal',
1672
-                'high'
1673
-            );
1674
-        } else {
1675
-            if ($flags->featureAllowed('use_reg_options_meta_box')) {
1676
-                add_action(
1677
-                    'add_meta_boxes_espresso_events',
1678
-                    function () {
1679
-                        global $current_screen;
1680
-                        remove_meta_box('authordiv', $current_screen, 'normal');
1681
-                    },
1682
-                    99
1683
-                );
1684
-            }
1685
-        }
1686
-        // NOTE: if you're looking for other metaboxes in here,
1687
-        // where a metabox has a related management page in the admin
1688
-        // you will find it setup in the related management page's "_Hooks" file.
1689
-        // i.e. messages metabox is found in "espresso_events_Messages_Hooks.class.php".
1690
-    }
1691
-
1692
-
1693
-    /**
1694
-     * @throws DomainException
1695
-     * @throws EE_Error
1696
-     * @throws InvalidArgumentException
1697
-     * @throws InvalidDataTypeException
1698
-     * @throws InvalidInterfaceException
1699
-     * @throws ReflectionException
1700
-     */
1701
-    public function ticket_metabox()
1702
-    {
1703
-        $existing_datetime_ids = $existing_ticket_ids = [];
1704
-        // defaults for template args
1705
-        $template_args = [
1706
-            'existing_datetime_ids'    => '',
1707
-            'event_datetime_help_link' => '',
1708
-            'ticket_options_help_link' => '',
1709
-            'time'                     => null,
1710
-            'ticket_rows'              => '',
1711
-            'existing_ticket_ids'      => '',
1712
-            'total_ticket_rows'        => 1,
1713
-            'ticket_js_structure'      => '',
1714
-            'trash_icon'               => 'ee-lock-icon',
1715
-            'disabled'                 => '',
1716
-        ];
1717
-        $event_id      = is_object($this->_cpt_model_obj) ? $this->_cpt_model_obj->ID() : null;
1718
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1719
-        /**
1720
-         * 1. Start with retrieving Datetimes
1721
-         * 2. Fore each datetime get related tickets
1722
-         * 3. For each ticket get related prices
1723
-         */
1724
-        $times = EE_Registry::instance()->load_model('Datetime')->get_all_event_dates($event_id);
1725
-        /** @type EE_Datetime $first_datetime */
1726
-        $first_datetime = reset($times);
1727
-        // do we get related tickets?
1728
-        if (
1729
-            $first_datetime instanceof EE_Datetime
1730
-            && $first_datetime->ID() !== 0
1731
-        ) {
1732
-            $existing_datetime_ids[] = $first_datetime->get('DTT_ID');
1733
-            $template_args['time']   = $first_datetime;
1734
-            $related_tickets         = $first_datetime->tickets(
1735
-                [
1736
-                    ['OR' => ['TKT_deleted' => 1, 'TKT_deleted*' => 0]],
1737
-                    'default_where_conditions' => 'none',
1738
-                ]
1739
-            );
1740
-            if (! empty($related_tickets)) {
1741
-                $template_args['total_ticket_rows'] = count($related_tickets);
1742
-                $row                                = 0;
1743
-                foreach ($related_tickets as $ticket) {
1744
-                    $existing_ticket_ids[]        = $ticket->get('TKT_ID');
1745
-                    $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket, false, $row);
1746
-                    $row++;
1747
-                }
1748
-            } else {
1749
-                $template_args['total_ticket_rows'] = 1;
1750
-                /** @type EE_Ticket $ticket */
1751
-                $ticket                       = EE_Registry::instance()->load_model('Ticket')->create_default_object();
1752
-                $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket);
1753
-            }
1754
-        } else {
1755
-            $template_args['time'] = $times[0];
1756
-            /** @type EE_Ticket $ticket */
1757
-            $ticket                       = EE_Registry::instance()->load_model('Ticket')->get_all_default_tickets();
1758
-            $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket[1]);
1759
-            // NOTE: we're just sending the first default row
1760
-            // (decaf can't manage default tickets so this should be sufficient);
1761
-        }
1762
-        $template_args['event_datetime_help_link'] = $this->_get_help_tab_link(
1763
-            'event_editor_event_datetimes_help_tab'
1764
-        );
1765
-        $template_args['ticket_options_help_link'] = $this->_get_help_tab_link('ticket_options_info');
1766
-        $template_args['existing_datetime_ids']    = implode(',', $existing_datetime_ids);
1767
-        $template_args['existing_ticket_ids']      = implode(',', $existing_ticket_ids);
1768
-        $template_args['ticket_js_structure']      = $this->_get_ticket_row(
1769
-            EE_Registry::instance()->load_model('Ticket')->create_default_object(),
1770
-            true
1771
-        );
1772
-        $template                                  = apply_filters(
1773
-            'FHEE__Events_Admin_Page__ticket_metabox__template',
1774
-            EVENTS_TEMPLATE_PATH . 'event_tickets_metabox_main.template.php'
1775
-        );
1776
-        EEH_Template::display_template($template, $template_args);
1777
-    }
1778
-
1779
-
1780
-    /**
1781
-     * Setup an individual ticket form for the decaf event editor page
1782
-     *
1783
-     * @access private
1784
-     * @param EE_Ticket $ticket   the ticket object
1785
-     * @param boolean   $skeleton whether we're generating a skeleton for js manipulation
1786
-     * @param int       $row
1787
-     * @return string generated html for the ticket row.
1788
-     * @throws DomainException
1789
-     * @throws EE_Error
1790
-     * @throws InvalidArgumentException
1791
-     * @throws InvalidDataTypeException
1792
-     * @throws InvalidInterfaceException
1793
-     * @throws ReflectionException
1794
-     */
1795
-    private function _get_ticket_row($ticket, $skeleton = false, $row = 0)
1796
-    {
1797
-        $template_args = [
1798
-            'tkt_status_class'    => ' tkt-status-' . $ticket->ticket_status(),
1799
-            'tkt_archive_class'   => $ticket->ticket_status() === EE_Ticket::archived && ! $skeleton ? ' tkt-archived'
1800
-                : '',
1801
-            'ticketrow'           => $skeleton ? 'TICKETNUM' : $row,
1802
-            'TKT_ID'              => $ticket->get('TKT_ID'),
1803
-            'TKT_name'            => $ticket->get('TKT_name'),
1804
-            'TKT_start_date'      => $skeleton ? '' : $ticket->get_date('TKT_start_date', 'Y-m-d h:i a'),
1805
-            'TKT_end_date'        => $skeleton ? '' : $ticket->get_date('TKT_end_date', 'Y-m-d h:i a'),
1806
-            'TKT_is_default'      => $ticket->get('TKT_is_default'),
1807
-            'TKT_qty'             => $ticket->get_pretty('TKT_qty', 'input'),
1808
-            'edit_ticketrow_name' => $skeleton ? 'TICKETNAMEATTR' : 'edit_tickets',
1809
-            'TKT_sold'            => $skeleton ? 0 : $ticket->get('TKT_sold'),
1810
-            'trash_icon'          => ($skeleton || (! empty($ticket) && ! $ticket->get('TKT_deleted')))
1811
-                                     && (! empty($ticket) && $ticket->get('TKT_sold') === 0)
1812
-                ? 'trash-icon dashicons dashicons-post-trash clickable' : 'ee-lock-icon',
1813
-            'disabled'            => $skeleton || (! empty($ticket) && ! $ticket->get('TKT_deleted')) ? ''
1814
-                : ' disabled=disabled',
1815
-        ];
1816
-        $price         = $ticket->ID() !== 0
1817
-            ? $ticket->get_first_related('Price', ['default_where_conditions' => 'none'])
1818
-            : EE_Registry::instance()->load_model('Price')->create_default_object();
1819
-        $price_args    = [
1820
-            'price_currency_symbol' => EE_Registry::instance()->CFG->currency->sign,
1821
-            'PRC_amount'            => $price->get('PRC_amount'),
1822
-            'PRT_ID'                => $price->get('PRT_ID'),
1823
-            'PRC_ID'                => $price->get('PRC_ID'),
1824
-            'PRC_is_default'        => $price->get('PRC_is_default'),
1825
-        ];
1826
-        // make sure we have default start and end dates if skeleton
1827
-        // handle rows that should NOT be empty
1828
-        if (empty($template_args['TKT_start_date'])) {
1829
-            // if empty then the start date will be now.
1830
-            $template_args['TKT_start_date'] = date('Y-m-d h:i a', current_time('timestamp'));
1831
-        }
1832
-        if (empty($template_args['TKT_end_date'])) {
1833
-            // get the earliest datetime (if present);
1834
-            $earliest_dtt = $this->_cpt_model_obj->ID() > 0
1835
-                ? $this->_cpt_model_obj->get_first_related(
1836
-                    'Datetime',
1837
-                    ['order_by' => ['DTT_EVT_start' => 'ASC']]
1838
-                )
1839
-                : null;
1840
-            if (! empty($earliest_dtt)) {
1841
-                $template_args['TKT_end_date'] = $earliest_dtt->get_datetime('DTT_EVT_start', 'Y-m-d', 'h:i a');
1842
-            } else {
1843
-                $template_args['TKT_end_date'] = date(
1844
-                    'Y-m-d h:i a',
1845
-                    mktime(0, 0, 0, date('m'), date('d') + 7, date('Y'))
1846
-                );
1847
-            }
1848
-        }
1849
-        $template_args = array_merge($template_args, $price_args);
1850
-        $template      = apply_filters(
1851
-            'FHEE__Events_Admin_Page__get_ticket_row__template',
1852
-            EVENTS_TEMPLATE_PATH . 'event_tickets_metabox_ticket_row.template.php',
1853
-            $ticket
1854
-        );
1855
-        return EEH_Template::display_template($template, $template_args, true);
1856
-    }
1857
-
1858
-
1859
-    /**
1860
-     * @throws DomainException
1861
-     * @throws EE_Error
1862
-     */
1863
-    public function registration_options_meta_box()
1864
-    {
1865
-        $yes_no_values             = [
1866
-            ['id' => true, 'text' => esc_html__('Yes', 'event_espresso')],
1867
-            ['id' => false, 'text' => esc_html__('No', 'event_espresso')],
1868
-        ];
1869
-        $default_reg_status_values = EEM_Registration::reg_status_array(
1870
-            [
1871
-                EEM_Registration::status_id_cancelled,
1872
-                EEM_Registration::status_id_declined,
1873
-                EEM_Registration::status_id_incomplete,
1874
-            ],
1875
-            true
1876
-        );
1877
-        // $template_args['is_active_select'] = EEH_Form_Fields::select_input('is_active', $yes_no_values, $this->_cpt_model_obj->is_active());
1878
-        $template_args['_event']                          = $this->_cpt_model_obj;
1879
-        $template_args['active_status']                   = $this->_cpt_model_obj->pretty_active_status(false);
1880
-        $template_args['additional_limit']                = $this->_cpt_model_obj->additional_limit();
1881
-        $template_args['default_registration_status']     = EEH_Form_Fields::select_input(
1882
-            'default_reg_status',
1883
-            $default_reg_status_values,
1884
-            $this->_cpt_model_obj->default_registration_status()
1885
-        );
1886
-        $template_args['display_description']             = EEH_Form_Fields::select_input(
1887
-            'display_desc',
1888
-            $yes_no_values,
1889
-            $this->_cpt_model_obj->display_description()
1890
-        );
1891
-        $template_args['display_ticket_selector']         = EEH_Form_Fields::select_input(
1892
-            'display_ticket_selector',
1893
-            $yes_no_values,
1894
-            $this->_cpt_model_obj->display_ticket_selector(),
1895
-            '',
1896
-            '',
1897
-            false
1898
-        );
1899
-        $template_args['additional_registration_options'] = apply_filters(
1900
-            'FHEE__Events_Admin_Page__registration_options_meta_box__additional_registration_options',
1901
-            '',
1902
-            $template_args,
1903
-            $yes_no_values,
1904
-            $default_reg_status_values
1905
-        );
1906
-        EEH_Template::display_template(
1907
-            EVENTS_TEMPLATE_PATH . 'event_registration_options.template.php',
1908
-            $template_args
1909
-        );
1910
-    }
1911
-
1912
-
1913
-    /**
1914
-     * _get_events()
1915
-     * This method simply returns all the events (for the given _view and paging)
1916
-     *
1917
-     * @access public
1918
-     * @param int  $per_page     count of items per page (20 default);
1919
-     * @param int  $current_page what is the current page being viewed.
1920
-     * @param bool $count        if TRUE then we just return a count of ALL events matching the given _view.
1921
-     *                           If FALSE then we return an array of event objects
1922
-     *                           that match the given _view and paging parameters.
1923
-     * @return array|int an array of event objects.
1924
-     * @throws EE_Error
1925
-     * @throws InvalidArgumentException
1926
-     * @throws InvalidDataTypeException
1927
-     * @throws InvalidInterfaceException
1928
-     * @throws ReflectionException
1929
-     * @throws Exception
1930
-     * @throws Exception
1931
-     * @throws Exception
1932
-     */
1933
-    public function get_events($per_page = 10, $current_page = 1, $count = false)
1934
-    {
1935
-        $EEME    = $this->_event_model();
1936
-        $offset  = ($current_page - 1) * $per_page;
1937
-        $limit   = $count ? null : $offset . ',' . $per_page;
1938
-        $orderby = isset($this->_req_data['orderby']) ? $this->_req_data['orderby'] : 'EVT_ID';
1939
-        $order   = isset($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC';
1940
-        if (isset($this->_req_data['month_range'])) {
1941
-            $pieces = explode(' ', $this->_req_data['month_range'], 3);
1942
-            // simulate the FIRST day of the month, that fixes issues for months like February
1943
-            // where PHP doesn't know what to assume for date.
1944
-            // @see https://events.codebasehq.com/projects/event-espresso/tickets/10437
1945
-            $month_r = ! empty($pieces[0]) ? date('m', EEH_DTT_Helper::first_of_month_timestamp($pieces[0])) : '';
1946
-            $year_r  = ! empty($pieces[1]) ? $pieces[1] : '';
1947
-        }
1948
-        $where  = [];
1949
-        $status = isset($this->_req_data['status']) ? $this->_req_data['status'] : null;
1950
-        // determine what post_status our condition will have for the query.
1951
-        switch ($status) {
1952
-            case 'month':
1953
-            case 'today':
1954
-            case null:
1955
-            case 'all':
1956
-                break;
1957
-            case 'draft':
1958
-                $where['status'] = ['IN', ['draft', 'auto-draft']];
1959
-                break;
1960
-            default:
1961
-                $where['status'] = $status;
1962
-        }
1963
-        // categories?
1964
-        $category = isset($this->_req_data['EVT_CAT']) && $this->_req_data['EVT_CAT'] > 0
1965
-            ? $this->_req_data['EVT_CAT'] : null;
1966
-        if (! empty($category)) {
1967
-            $where['Term_Taxonomy.taxonomy'] = EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY;
1968
-            $where['Term_Taxonomy.term_id']  = $category;
1969
-        }
1970
-        // date where conditions
1971
-        $start_formats = EEM_Datetime::instance()->get_formats_for('DTT_EVT_start');
1972
-        if (isset($this->_req_data['month_range']) && $this->_req_data['month_range'] !== '') {
1973
-            $DateTime = new DateTime(
1974
-                $year_r . '-' . $month_r . '-01 00:00:00',
1975
-                new DateTimeZone('UTC')
1976
-            );
1977
-            $start    = $DateTime->getTimestamp();
1978
-            // set the datetime to be the end of the month
1979
-            $DateTime->setDate(
1980
-                $year_r,
1981
-                $month_r,
1982
-                $DateTime->format('t')
1983
-            )->setTime(23, 59, 59);
1984
-            $end                             = $DateTime->getTimestamp();
1985
-            $where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]];
1986
-        } elseif (isset($this->_req_data['status']) && $this->_req_data['status'] === 'today') {
1987
-            $DateTime                        =
1988
-                new DateTime('now', new DateTimeZone(EEM_Event::instance()->get_timezone()));
1989
-            $start                           = $DateTime->setTime(0, 0, 0)->format(implode(' ', $start_formats));
1990
-            $end                             = $DateTime->setTime(23, 59, 59)->format(implode(' ', $start_formats));
1991
-            $where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]];
1992
-        } elseif (isset($this->_req_data['status']) && $this->_req_data['status'] === 'month') {
1993
-            $now                             = date('Y-m-01');
1994
-            $DateTime                        =
1995
-                new DateTime($now, new DateTimeZone(EEM_Event::instance()->get_timezone()));
1996
-            $start                           = $DateTime->setTime(0, 0, 0)->format(implode(' ', $start_formats));
1997
-            $end                             = $DateTime->setDate(date('Y'), date('m'), $DateTime->format('t'))
1998
-                                                        ->setTime(23, 59, 59)
1999
-                                                        ->format(implode(' ', $start_formats));
2000
-            $where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]];
2001
-        }
2002
-        if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_events', 'get_events')) {
2003
-            $where['EVT_wp_user'] = get_current_user_id();
2004
-        } elseif (
2005
-            ! isset($where['status'])
2006
-            && ! EE_Registry::instance()->CAP->current_user_can('ee_read_private_events', 'get_events')
2007
-        ) {
2008
-            $where['OR'] = [
2009
-                'status*restrict_private' => ['!=', 'private'],
2010
-                'AND'                     => [
2011
-                    'status*inclusive' => ['=', 'private'],
2012
-                    'EVT_wp_user'      => get_current_user_id(),
2013
-                ],
2014
-            ];
2015
-        }
2016
-
2017
-        if (
2018
-            isset($this->_req_data['EVT_wp_user'])
2019
-            && (int) $this->_req_data['EVT_wp_user'] !== (int) get_current_user_id()
2020
-            && EE_Registry::instance()->CAP->current_user_can('ee_read_others_events', 'get_events')
2021
-        ) {
2022
-            $where['EVT_wp_user'] = $this->_req_data['EVT_wp_user'];
2023
-        }
2024
-        // search query handling
2025
-        if (isset($this->_req_data['s'])) {
2026
-            $search_string = '%' . $this->_req_data['s'] . '%';
2027
-            $where['OR']   = [
2028
-                'EVT_name'       => ['LIKE', $search_string],
2029
-                'EVT_desc'       => ['LIKE', $search_string],
2030
-                'EVT_short_desc' => ['LIKE', $search_string],
2031
-            ];
2032
-        }
2033
-        // filter events by venue.
2034
-        if (isset($this->_req_data['venue']) && ! empty($this->_req_data['venue'])) {
2035
-            $where['Venue.VNU_ID'] = absint($this->_req_data['venue']);
2036
-        }
2037
-        $where        = apply_filters('FHEE__Events_Admin_Page__get_events__where', $where, $this->_req_data);
2038
-        $query_params = apply_filters(
2039
-            'FHEE__Events_Admin_Page__get_events__query_params',
2040
-            [
2041
-                $where,
2042
-                'limit'    => $limit,
2043
-                'order_by' => $orderby,
2044
-                'order'    => $order,
2045
-                'group_by' => 'EVT_ID',
2046
-            ],
2047
-            $this->_req_data
2048
-        );
2049
-
2050
-        // let's first check if we have special requests coming in.
2051
-        if (isset($this->_req_data['active_status'])) {
2052
-            switch ($this->_req_data['active_status']) {
2053
-                case 'upcoming':
2054
-                    return $EEME->get_upcoming_events($query_params, $count);
2055
-                case 'expired':
2056
-                    return $EEME->get_expired_events($query_params, $count);
2057
-                case 'active':
2058
-                    return $EEME->get_active_events($query_params, $count);
2059
-                case 'inactive':
2060
-                    return $EEME->get_inactive_events($query_params, $count);
2061
-            }
2062
-        }
2063
-
2064
-        return $count ? $EEME->count([$where], 'EVT_ID', true) : $EEME->get_all($query_params);
2065
-    }
2066
-
2067
-
2068
-    /**
2069
-     * handling for WordPress CPT actions (trash, restore, delete)
2070
-     *
2071
-     * @param string $post_id
2072
-     * @throws EE_Error
2073
-     * @throws InvalidArgumentException
2074
-     * @throws InvalidDataTypeException
2075
-     * @throws InvalidInterfaceException
2076
-     * @throws ReflectionException
2077
-     */
2078
-    public function trash_cpt_item($post_id)
2079
-    {
2080
-        $this->_req_data['EVT_ID'] = $post_id;
2081
-        $this->_trash_or_restore_event('trash', false);
2082
-    }
2083
-
2084
-
2085
-    /**
2086
-     * @param string $post_id
2087
-     * @throws EE_Error
2088
-     * @throws InvalidArgumentException
2089
-     * @throws InvalidDataTypeException
2090
-     * @throws InvalidInterfaceException
2091
-     * @throws ReflectionException
2092
-     */
2093
-    public function restore_cpt_item($post_id)
2094
-    {
2095
-        $this->_req_data['EVT_ID'] = $post_id;
2096
-        $this->_trash_or_restore_event('draft', false);
2097
-    }
2098
-
2099
-
2100
-    /**
2101
-     * @param string $post_id
2102
-     * @throws EE_Error
2103
-     * @throws InvalidArgumentException
2104
-     * @throws InvalidDataTypeException
2105
-     * @throws InvalidInterfaceException
2106
-     * @throws ReflectionException
2107
-     */
2108
-    public function delete_cpt_item($post_id)
2109
-    {
2110
-        throw new EE_Error(
2111
-            esc_html__(
2112
-                'Please contact Event Espresso support with the details of the steps taken to produce this error.',
2113
-                'event_espresso'
2114
-            )
2115
-        );
2116
-        $this->_req_data['EVT_ID'] = $post_id;
2117
-        $this->_delete_event();
2118
-    }
2119
-
2120
-
2121
-    /**
2122
-     * _trash_or_restore_event
2123
-     *
2124
-     * @access protected
2125
-     * @param string $event_status
2126
-     * @param bool   $redirect_after
2127
-     * @throws EE_Error
2128
-     * @throws InvalidArgumentException
2129
-     * @throws InvalidDataTypeException
2130
-     * @throws InvalidInterfaceException
2131
-     * @throws ReflectionException
2132
-     */
2133
-    protected function _trash_or_restore_event($event_status = 'trash', $redirect_after = true)
2134
-    {
2135
-        // determine the event id and set to array.
2136
-        $EVT_ID = isset($this->_req_data['EVT_ID']) ? absint($this->_req_data['EVT_ID']) : false;
2137
-        // loop thru events
2138
-        if ($EVT_ID) {
2139
-            // clean status
2140
-            $event_status = sanitize_key($event_status);
2141
-            // grab status
2142
-            if (! empty($event_status)) {
2143
-                $success = $this->_change_event_status($EVT_ID, $event_status);
2144
-            } else {
2145
-                $success = false;
2146
-                $msg     = esc_html__(
2147
-                    'An error occurred. The event could not be moved to the trash because a valid event status was not not supplied.',
2148
-                    'event_espresso'
2149
-                );
2150
-                EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2151
-            }
2152
-        } else {
2153
-            $success = false;
2154
-            $msg     = esc_html__(
2155
-                'An error occurred. The event could not be moved to the trash because a valid event ID was not not supplied.',
2156
-                'event_espresso'
2157
-            );
2158
-            EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2159
-        }
2160
-        $action = $event_status === 'trash' ? 'moved to the trash' : 'restored from the trash';
2161
-        if ($redirect_after) {
2162
-            $this->_redirect_after_action($success, 'Event', $action, ['action' => 'default']);
2163
-        }
2164
-    }
2165
-
2166
-
2167
-    /**
2168
-     * _trash_or_restore_events
2169
-     *
2170
-     * @access protected
2171
-     * @param string $event_status
2172
-     * @return void
2173
-     * @throws EE_Error
2174
-     * @throws InvalidArgumentException
2175
-     * @throws InvalidDataTypeException
2176
-     * @throws InvalidInterfaceException
2177
-     * @throws ReflectionException
2178
-     */
2179
-    protected function _trash_or_restore_events($event_status = 'trash')
2180
-    {
2181
-        // clean status
2182
-        $event_status = sanitize_key($event_status);
2183
-        // grab status
2184
-        if (! empty($event_status)) {
2185
-            $success = true;
2186
-            // determine the event id and set to array.
2187
-            $EVT_IDs = isset($this->_req_data['EVT_IDs']) ? (array) $this->_req_data['EVT_IDs'] : [];
2188
-            // loop thru events
2189
-            foreach ($EVT_IDs as $EVT_ID) {
2190
-                if ($EVT_ID = absint($EVT_ID)) {
2191
-                    $results = $this->_change_event_status($EVT_ID, $event_status);
2192
-                    $success = $results !== false ? $success : false;
2193
-                } else {
2194
-                    $msg = sprintf(
2195
-                        esc_html__(
2196
-                            'An error occurred. Event #%d could not be moved to the trash because a valid event ID was not not supplied.',
2197
-                            'event_espresso'
2198
-                        ),
2199
-                        $EVT_ID
2200
-                    );
2201
-                    EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2202
-                    $success = false;
2203
-                }
2204
-            }
2205
-        } else {
2206
-            $success = false;
2207
-            $msg     = esc_html__(
2208
-                'An error occurred. The event could not be moved to the trash because a valid event status was not not supplied.',
2209
-                'event_espresso'
2210
-            );
2211
-            EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2212
-        }
2213
-        // in order to force a pluralized result message we need to send back a success status greater than 1
2214
-        $success = $success ? 2 : false;
2215
-        $action  = $event_status === 'trash' ? 'moved to the trash' : 'restored from the trash';
2216
-        $this->_redirect_after_action($success, 'Events', $action, ['action' => 'default']);
2217
-    }
2218
-
2219
-
2220
-    /**
2221
-     * _trash_or_restore_events
2222
-     *
2223
-     * @access  private
2224
-     * @param int    $EVT_ID
2225
-     * @param string $event_status
2226
-     * @return bool
2227
-     * @throws EE_Error
2228
-     * @throws InvalidArgumentException
2229
-     * @throws InvalidDataTypeException
2230
-     * @throws InvalidInterfaceException
2231
-     * @throws ReflectionException
2232
-     */
2233
-    private function _change_event_status($EVT_ID = 0, $event_status = '')
2234
-    {
2235
-        // grab event id
2236
-        if (! $EVT_ID) {
2237
-            $msg = esc_html__(
2238
-                'An error occurred. No Event ID or an invalid Event ID was received.',
2239
-                'event_espresso'
2240
-            );
2241
-            EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2242
-            return false;
2243
-        }
2244
-        $this->_cpt_model_obj = EEM_Event::instance()->get_one_by_ID($EVT_ID);
2245
-        // clean status
2246
-        $event_status = sanitize_key($event_status);
2247
-        // grab status
2248
-        if (empty($event_status)) {
2249
-            $msg = esc_html__(
2250
-                'An error occurred. No Event Status or an invalid Event Status was received.',
2251
-                'event_espresso'
2252
-            );
2253
-            EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2254
-            return false;
2255
-        }
2256
-        // was event trashed or restored ?
2257
-        switch ($event_status) {
2258
-            case 'draft':
2259
-                $action = 'restored from the trash';
2260
-                $hook   = 'AHEE_event_restored_from_trash';
2261
-                break;
2262
-            case 'trash':
2263
-                $action = 'moved to the trash';
2264
-                $hook   = 'AHEE_event_moved_to_trash';
2265
-                break;
2266
-            default:
2267
-                $action = 'updated';
2268
-                $hook   = false;
2269
-        }
2270
-        // use class to change status
2271
-        $this->_cpt_model_obj->set_status($event_status);
2272
-        $success = $this->_cpt_model_obj->save();
2273
-        if ($success === false) {
2274
-            $msg = sprintf(esc_html__('An error occurred. The event could not be %s.', 'event_espresso'), $action);
2275
-            EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2276
-            return false;
2277
-        }
2278
-        if ($hook) {
2279
-            do_action($hook);
2280
-        }
2281
-        return true;
2282
-    }
2283
-
2284
-
2285
-    /**
2286
-     * _delete_event
2287
-     *
2288
-     * @throws InvalidArgumentException
2289
-     * @throws InvalidDataTypeException
2290
-     * @throws InvalidInterfaceException
2291
-     */
2292
-    protected function _delete_event()
2293
-    {
2294
-        $this->generateDeletionPreview(isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : []);
2295
-    }
2296
-
2297
-
2298
-    /**
2299
-     * Gets the tree traversal batch persister.
2300
-     *
2301
-     * @return NodeGroupDao
2302
-     * @throws InvalidArgumentException
2303
-     * @throws InvalidDataTypeException
2304
-     * @throws InvalidInterfaceException
2305
-     * @since 4.10.12.p
2306
-     */
2307
-    protected function getModelObjNodeGroupPersister()
2308
-    {
2309
-        if (! $this->model_obj_node_group_persister instanceof NodeGroupDao) {
2310
-            $this->model_obj_node_group_persister =
2311
-                $this->getLoader()->load('\EventEspresso\core\services\orm\tree_traversal\NodeGroupDao');
2312
-        }
2313
-        return $this->model_obj_node_group_persister;
2314
-    }
2315
-
2316
-
2317
-    /**
2318
-     * _delete_events
2319
-     *
2320
-     * @access protected
2321
-     * @return void
2322
-     * @throws InvalidArgumentException
2323
-     * @throws InvalidDataTypeException
2324
-     * @throws InvalidInterfaceException
2325
-     */
2326
-    protected function _delete_events()
2327
-    {
2328
-        $this->generateDeletionPreview(isset($this->_req_data['EVT_IDs']) ? (array) $this->_req_data['EVT_IDs'] : []);
2329
-    }
2330
-
2331
-
2332
-    protected function generateDeletionPreview($event_ids)
2333
-    {
2334
-        $event_ids = (array) $event_ids;
2335
-        // Set a code we can use to reference this deletion task in the batch jobs and preview page.
2336
-        $deletion_job_code = $this->getModelObjNodeGroupPersister()->generateGroupCode();
2337
-        $return_url        = EE_Admin_Page::add_query_args_and_nonce(
2338
-            [
2339
-                'action'            => 'preview_deletion',
2340
-                'deletion_job_code' => $deletion_job_code,
2341
-            ],
2342
-            $this->_admin_base_url
2343
-        );
2344
-        $event_ids         = array_map(
2345
-            'intval',
2346
-            $event_ids
2347
-        );
2348
-
2349
-        EEH_URL::safeRedirectAndExit(
2350
-            EE_Admin_Page::add_query_args_and_nonce(
2351
-                [
2352
-                    'page'              => 'espresso_batch',
2353
-                    'batch'             => EED_Batch::batch_job,
2354
-                    'EVT_IDs'           => $event_ids,
2355
-                    'deletion_job_code' => $deletion_job_code,
2356
-                    'job_handler'       => urlencode('EventEspressoBatchRequest\JobHandlers\PreviewEventDeletion'),
2357
-                    'return_url'        => urlencode($return_url),
2358
-                ],
2359
-                admin_url()
2360
-            )
2361
-        );
2362
-    }
2363
-
2364
-
2365
-    /**
2366
-     * Checks for a POST submission
2367
-     *
2368
-     * @since 4.10.12.p
2369
-     */
2370
-    protected function confirmDeletion()
2371
-    {
2372
-        $deletion_redirect_logic =
2373
-            $this->getLoader()->getShared('\EventEspresso\core\domain\services\admin\events\data\ConfirmDeletion');
2374
-        $deletion_redirect_logic->handle($this->get_request_data(), $this->admin_base_url());
2375
-    }
2376
-
2377
-
2378
-    /**
2379
-     * A page for users to preview what exactly will be deleted, and confirm they want to delete it.
2380
-     *
2381
-     * @throws EE_Error
2382
-     * @since 4.10.12.p
2383
-     */
2384
-    protected function previewDeletion()
2385
-    {
2386
-        $preview_deletion_logic =
2387
-            $this->getLoader()->getShared('\EventEspresso\core\domain\services\admin\events\data\PreviewDeletion');
2388
-        $this->set_template_args($preview_deletion_logic->handle($this->get_request_data(), $this->admin_base_url()));
2389
-        $this->display_admin_page_with_no_sidebar();
2390
-    }
2391
-
2392
-
2393
-    /**
2394
-     * get total number of events
2395
-     *
2396
-     * @access public
2397
-     * @return int
2398
-     * @throws EE_Error
2399
-     * @throws InvalidArgumentException
2400
-     * @throws InvalidDataTypeException
2401
-     * @throws InvalidInterfaceException
2402
-     */
2403
-    public function total_events()
2404
-    {
2405
-        return EEM_Event::instance()->count(['caps' => 'read_admin'], 'EVT_ID', true);
2406
-    }
2407
-
2408
-
2409
-    /**
2410
-     * get total number of draft events
2411
-     *
2412
-     * @access public
2413
-     * @return int
2414
-     * @throws EE_Error
2415
-     * @throws InvalidArgumentException
2416
-     * @throws InvalidDataTypeException
2417
-     * @throws InvalidInterfaceException
2418
-     */
2419
-    public function total_events_draft()
2420
-    {
2421
-        $where = [
2422
-            'status' => ['IN', ['draft', 'auto-draft']],
2423
-        ];
2424
-        return EEM_Event::instance()->count([$where, 'caps' => 'read_admin'], 'EVT_ID', true);
2425
-    }
2426
-
2427
-
2428
-    /**
2429
-     * get total number of trashed events
2430
-     *
2431
-     * @access public
2432
-     * @return int
2433
-     * @throws EE_Error
2434
-     * @throws InvalidArgumentException
2435
-     * @throws InvalidDataTypeException
2436
-     * @throws InvalidInterfaceException
2437
-     */
2438
-    public function total_trashed_events()
2439
-    {
2440
-        $where = [
2441
-            'status' => 'trash',
2442
-        ];
2443
-        return EEM_Event::instance()->count([$where, 'caps' => 'read_admin'], 'EVT_ID', true);
2444
-    }
2445
-
2446
-
2447
-    /**
2448
-     *    _default_event_settings
2449
-     *    This generates the Default Settings Tab
2450
-     *
2451
-     * @return void
2452
-     * @throws DomainException
2453
-     * @throws EE_Error
2454
-     * @throws InvalidArgumentException
2455
-     * @throws InvalidDataTypeException
2456
-     * @throws InvalidInterfaceException
2457
-     */
2458
-    protected function _default_event_settings()
2459
-    {
2460
-        $this->_set_add_edit_form_tags('update_default_event_settings');
2461
-        $this->_set_publish_post_box_vars(null, false, false, null, false);
2462
-        $this->_template_args['admin_page_content'] = $this->_default_event_settings_form()->get_html();
2463
-        $this->display_admin_page_with_sidebar();
2464
-    }
2465
-
2466
-
2467
-    /**
2468
-     * Return the form for event settings.
2469
-     *
2470
-     * @return EE_Form_Section_Proper
2471
-     * @throws EE_Error
2472
-     */
2473
-    protected function _default_event_settings_form()
2474
-    {
2475
-        $registration_config              = EE_Registry::instance()->CFG->registration;
2476
-        $registration_stati_for_selection = EEM_Registration::reg_status_array(
2477
-        // exclude
2478
-            [
2479
-                EEM_Registration::status_id_cancelled,
2480
-                EEM_Registration::status_id_declined,
2481
-                EEM_Registration::status_id_incomplete,
2482
-                EEM_Registration::status_id_wait_list,
2483
-            ],
2484
-            true
2485
-        );
2486
-        return new EE_Form_Section_Proper(
2487
-            [
2488
-                'name'            => 'update_default_event_settings',
2489
-                'html_id'         => 'update_default_event_settings',
2490
-                'html_class'      => 'form-table',
2491
-                'layout_strategy' => new EE_Admin_Two_Column_Layout(),
2492
-                'subsections'     => apply_filters(
2493
-                    'FHEE__Events_Admin_Page___default_event_settings_form__form_subsections',
2494
-                    [
2495
-                        'default_reg_status'  => new EE_Select_Input(
2496
-                            $registration_stati_for_selection,
2497
-                            [
2498
-                                'default'         => isset($registration_config->default_STS_ID)
2499
-                                                     && array_key_exists(
2500
-                                                         $registration_config->default_STS_ID,
2501
-                                                         $registration_stati_for_selection
2502
-                                                     )
2503
-                                    ? sanitize_text_field($registration_config->default_STS_ID)
2504
-                                    : EEM_Registration::status_id_pending_payment,
2505
-                                'html_label_text' => esc_html__('Default Registration Status', 'event_espresso')
2506
-                                                        . EEH_Template::get_help_tab_link(
2507
-                                                            'default_settings_status_help_tab'
2508
-                                                        ),
2509
-                                'html_help_text'  => esc_html__(
2510
-                                    'This setting allows you to preselect what the default registration status setting is when creating an event.  Note that changing this setting does NOT retroactively apply it to existing events.',
2511
-                                    'event_espresso'
2512
-                                ),
2513
-                            ]
2514
-                        ),
2515
-                        'default_max_tickets' => new EE_Integer_Input(
2516
-                            [
2517
-                                'default'         => isset($registration_config->default_maximum_number_of_tickets)
2518
-                                    ? $registration_config->default_maximum_number_of_tickets
2519
-                                    : EEM_Event::get_default_additional_limit(),
2520
-                                'html_label_text' => esc_html__(
2521
-                                    'Default Maximum Tickets Allowed Per Order:',
2522
-                                    'event_espresso'
2523
-                                )
2524
-                                . EEH_Template::get_help_tab_link(
2525
-                                    'default_maximum_tickets_help_tab"'
2526
-                                ),
2527
-                                'html_help_text'  => esc_html__(
2528
-                                    'This setting allows you to indicate what will be the default for the maximum number of tickets per order when creating new events.',
2529
-                                    'event_espresso'
2530
-                                ),
2531
-                            ]
2532
-                        ),
2533
-                    ]
2534
-                ),
2535
-            ]
2536
-        );
2537
-    }
2538
-
2539
-
2540
-    /**
2541
-     * @return void
2542
-     * @throws EE_Error
2543
-     * @throws InvalidArgumentException
2544
-     * @throws InvalidDataTypeException
2545
-     * @throws InvalidInterfaceException
2546
-     */
2547
-    protected function _update_default_event_settings()
2548
-    {
2549
-        $form = $this->_default_event_settings_form();
2550
-        if ($form->was_submitted()) {
2551
-            $form->receive_form_submission();
2552
-            if ($form->is_valid()) {
2553
-                $registration_config = EE_Registry::instance()->CFG->registration;
2554
-                $valid_data          = $form->valid_data();
2555
-                if (isset($valid_data['default_reg_status'])) {
2556
-                    $registration_config->default_STS_ID = $valid_data['default_reg_status'];
2557
-                }
2558
-                if (isset($valid_data['default_max_tickets'])) {
2559
-                    $registration_config->default_maximum_number_of_tickets = $valid_data['default_max_tickets'];
2560
-                }
2561
-                do_action(
2562
-                    'AHEE__Events_Admin_Page___update_default_event_settings',
2563
-                    $valid_data,
2564
-                    EE_Registry::instance()->CFG,
2565
-                    $this
2566
-                );
2567
-                // update because data was valid!
2568
-                EE_Registry::instance()->CFG->update_espresso_config();
2569
-                EE_Error::overwrite_success();
2570
-                EE_Error::add_success(
2571
-                    __('Default Event Settings were updated', 'event_espresso')
2572
-                );
2573
-            }
2574
-        }
2575
-        $this->_redirect_after_action(0, '', '', ['action' => 'default_event_settings'], true);
2576
-    }
2577
-
2578
-
2579
-    /*************        Templates        *************/
2580
-    protected function _template_settings()
2581
-    {
2582
-        $this->_admin_page_title              = esc_html__('Template Settings (Preview)', 'event_espresso');
2583
-        $this->_template_args['preview_img']  = '<img src="'
2584
-                                                . EVENTS_ASSETS_URL
2585
-                                                . '/images/'
2586
-                                                . 'caffeinated_template_features.jpg" alt="'
2587
-                                                . esc_attr__('Template Settings Preview screenshot', 'event_espresso')
2588
-                                                . '" />';
2589
-        $this->_template_args['preview_text'] = '<strong>'
2590
-                                                . esc_html__(
2591
-                                                    'Template Settings 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. Template Settings allow you to configure some of the appearance options for both the Event List and Event Details pages.',
2592
-                                                    'event_espresso'
2593
-                                                ) . '</strong>';
2594
-        $this->display_admin_caf_preview_page('template_settings_tab');
2595
-    }
2596
-
2597
-
2598
-    /** Event Category Stuff **/
2599
-    /**
2600
-     * set the _category property with the category object for the loaded page.
2601
-     *
2602
-     * @access private
2603
-     * @return void
2604
-     */
2605
-    private function _set_category_object()
2606
-    {
2607
-        if (isset($this->_category->id) && ! empty($this->_category->id)) {
2608
-            return;
2609
-        } //already have the category object so get out.
2610
-        // set default category object
2611
-        $this->_set_empty_category_object();
2612
-        // only set if we've got an id
2613
-        if (! isset($this->_req_data['EVT_CAT_ID'])) {
2614
-            return;
2615
-        }
2616
-        $category_id = absint($this->_req_data['EVT_CAT_ID']);
2617
-        $term        = get_term($category_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY);
2618
-        if (! empty($term)) {
2619
-            $this->_category->category_name       = $term->name;
2620
-            $this->_category->category_identifier = $term->slug;
2621
-            $this->_category->category_desc       = $term->description;
2622
-            $this->_category->id                  = $term->term_id;
2623
-            $this->_category->parent              = $term->parent;
2624
-        }
2625
-    }
2626
-
2627
-
2628
-    /**
2629
-     * Clears out category properties.
2630
-     */
2631
-    private function _set_empty_category_object()
2632
-    {
2633
-        $this->_category                = new stdClass();
2634
-        $this->_category->category_name = $this->_category->category_identifier = $this->_category->category_desc = '';
2635
-        $this->_category->id            = $this->_category->parent = 0;
2636
-    }
2637
-
2638
-
2639
-    /**
2640
-     * @throws DomainException
2641
-     * @throws EE_Error
2642
-     * @throws InvalidArgumentException
2643
-     * @throws InvalidDataTypeException
2644
-     * @throws InvalidInterfaceException
2645
-     */
2646
-    protected function _category_list_table()
2647
-    {
2648
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
2649
-        $this->_search_btn_label = esc_html__('Categories', 'event_espresso');
2650
-        $this->_admin_page_title .= ' ';
2651
-        $this->_admin_page_title .= $this->get_action_link_or_button(
2652
-            'add_category',
2653
-            'add_category',
2654
-            [],
2655
-            'add-new-h2'
2656
-        );
2657
-        $this->display_admin_list_table_page_with_sidebar();
2658
-    }
2659
-
2660
-
2661
-    /**
2662
-     * Output category details view.
2663
-     *
2664
-     * @param string $view
2665
-     * @throws DomainException
2666
-     * @throws EE_Error
2667
-     * @throws InvalidArgumentException
2668
-     * @throws InvalidDataTypeException
2669
-     * @throws InvalidInterfaceException
2670
-     */
2671
-    protected function _category_details($view)
2672
-    {
2673
-        // load formatter helper
2674
-        // load field generator helper
2675
-        $route = $view === 'edit' ? 'update_category' : 'insert_category';
2676
-        $this->_set_add_edit_form_tags($route);
2677
-        $this->_set_category_object();
2678
-        $id            = ! empty($this->_category->id) ? $this->_category->id : '';
2679
-        $delete_action = 'delete_category';
2680
-        // custom redirect
2681
-        $redirect = EE_Admin_Page::add_query_args_and_nonce(
2682
-            ['action' => 'category_list'],
2683
-            $this->_admin_base_url
2684
-        );
2685
-        $this->_set_publish_post_box_vars('EVT_CAT_ID', $id, $delete_action, $redirect);
2686
-        // take care of contents
2687
-        $this->_template_args['admin_page_content'] = $this->_category_details_content();
2688
-        $this->display_admin_page_with_sidebar();
2689
-    }
2690
-
2691
-
2692
-    /**
2693
-     * Output category details content.
2694
-     *
2695
-     * @throws DomainException
2696
-     */
2697
-    protected function _category_details_content()
2698
-    {
2699
-        $editor_args['category_desc'] = [
2700
-            'type'          => 'wp_editor',
2701
-            'value'         => EEH_Formatter::admin_format_content($this->_category->category_desc),
2702
-            'class'         => 'my_editor_custom',
2703
-            'wpeditor_args' => ['media_buttons' => false],
2704
-        ];
2705
-        $_wp_editor                   = $this->_generate_admin_form_fields($editor_args, 'array');
2706
-        $all_terms                    = get_terms(
2707
-            [EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY],
2708
-            ['hide_empty' => 0, 'exclude' => [$this->_category->id]]
2709
-        );
2710
-        // setup category select for term parents.
2711
-        $category_select_values[] = [
2712
-            'text' => esc_html__('No Parent', 'event_espresso'),
2713
-            'id'   => 0,
2714
-        ];
2715
-        foreach ($all_terms as $term) {
2716
-            $category_select_values[] = [
2717
-                'text' => $term->name,
2718
-                'id'   => $term->term_id,
2719
-            ];
2720
-        }
2721
-        $category_select = EEH_Form_Fields::select_input(
2722
-            'category_parent',
2723
-            $category_select_values,
2724
-            $this->_category->parent
2725
-        );
2726
-        $template_args   = [
2727
-            'category'                 => $this->_category,
2728
-            'category_select'          => $category_select,
2729
-            'unique_id_info_help_link' => $this->_get_help_tab_link('unique_id_info'),
2730
-            'category_desc_editor'     => $_wp_editor['category_desc']['field'],
2731
-            'disable'                  => '',
2732
-            'disabled_message'         => false,
2733
-        ];
2734
-        $template        = EVENTS_TEMPLATE_PATH . 'event_category_details.template.php';
2735
-        return EEH_Template::display_template($template, $template_args, true);
2736
-    }
2737
-
2738
-
2739
-    /**
2740
-     * Handles deleting categories.
2741
-     */
2742
-    protected function _delete_categories()
2743
-    {
2744
-        $cat_ids = isset($this->_req_data['EVT_CAT_ID']) ? (array) $this->_req_data['EVT_CAT_ID']
2745
-            : (array) $this->_req_data['category_id'];
2746
-        foreach ($cat_ids as $cat_id) {
2747
-            $this->_delete_category($cat_id);
2748
-        }
2749
-        // doesn't matter what page we're coming from... we're going to the same place after delete.
2750
-        $query_args = [
2751
-            'action' => 'category_list',
2752
-        ];
2753
-        $this->_redirect_after_action(0, '', '', $query_args);
2754
-    }
2755
-
2756
-
2757
-    /**
2758
-     * Handles deleting specific category.
2759
-     *
2760
-     * @param int $cat_id
2761
-     */
2762
-    protected function _delete_category($cat_id)
2763
-    {
2764
-        $cat_id = absint($cat_id);
2765
-        wp_delete_term($cat_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY);
2766
-    }
2767
-
2768
-
2769
-    /**
2770
-     * Handles triggering the update or insertion of a new category.
2771
-     *
2772
-     * @param bool $new_category true means we're triggering the insert of a new category.
2773
-     * @throws EE_Error
2774
-     * @throws InvalidArgumentException
2775
-     * @throws InvalidDataTypeException
2776
-     * @throws InvalidInterfaceException
2777
-     */
2778
-    protected function _insert_or_update_category($new_category)
2779
-    {
2780
-        $cat_id  = $new_category ? $this->_insert_category() : $this->_insert_category(true);
2781
-        $success = 0; // we already have a success message so lets not send another.
2782
-        if ($cat_id) {
2783
-            $query_args = [
2784
-                'action'     => 'edit_category',
2785
-                'EVT_CAT_ID' => $cat_id,
2786
-            ];
2787
-        } else {
2788
-            $query_args = ['action' => 'add_category'];
2789
-        }
2790
-        $this->_redirect_after_action($success, '', '', $query_args, true);
2791
-    }
2792
-
2793
-
2794
-    /**
2795
-     * Inserts or updates category
2796
-     *
2797
-     * @param bool $update (true indicates we're updating a category).
2798
-     * @return bool|mixed|string
2799
-     */
2800
-    private function _insert_category($update = false)
2801
-    {
2802
-        $cat_id          = $update ? $this->_req_data['EVT_CAT_ID'] : '';
2803
-        $category_name   = isset($this->_req_data['category_name']) ? $this->_req_data['category_name'] : '';
2804
-        $category_desc   = isset($this->_req_data['category_desc']) ? $this->_req_data['category_desc'] : '';
2805
-        $category_parent = isset($this->_req_data['category_parent']) ? $this->_req_data['category_parent'] : 0;
2806
-        if (empty($category_name)) {
2807
-            $msg = esc_html__('You must add a name for the category.', 'event_espresso');
2808
-            EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2809
-            return false;
2810
-        }
2811
-        $term_args = [
2812
-            'name'        => $category_name,
2813
-            'description' => $category_desc,
2814
-            'parent'      => $category_parent,
2815
-        ];
2816
-        // was the category_identifier input disabled?
2817
-        if (isset($this->_req_data['category_identifier'])) {
2818
-            $term_args['slug'] = $this->_req_data['category_identifier'];
2819
-        }
2820
-        $insert_ids = $update
2821
-            ? wp_update_term($cat_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY, $term_args)
2822
-            : wp_insert_term($category_name, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY, $term_args);
2823
-        if (! is_array($insert_ids)) {
2824
-            $msg = esc_html__(
2825
-                'An error occurred and the category has not been saved to the database.',
2826
-                'event_espresso'
2827
-            );
2828
-            EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2829
-        } else {
2830
-            $cat_id = $insert_ids['term_id'];
2831
-            $msg    = sprintf(esc_html__('The category %s was successfully saved', 'event_espresso'), $category_name);
2832
-            EE_Error::add_success($msg);
2833
-        }
2834
-        return $cat_id;
2835
-    }
2836
-
2837
-
2838
-    /**
2839
-     * Gets categories or count of categories matching the arguments in the request.
2840
-     *
2841
-     * @param int  $per_page
2842
-     * @param int  $current_page
2843
-     * @param bool $count
2844
-     * @return EE_Base_Class[]|EE_Term_Taxonomy[]|int
2845
-     * @throws EE_Error
2846
-     * @throws InvalidArgumentException
2847
-     * @throws InvalidDataTypeException
2848
-     * @throws InvalidInterfaceException
2849
-     */
2850
-    public function get_categories($per_page = 10, $current_page = 1, $count = false)
2851
-    {
2852
-        // testing term stuff
2853
-        $orderby = isset($this->_req_data['orderby']) ? $this->_req_data['orderby'] : 'Term.term_id';
2854
-        $order   = isset($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC';
2855
-        $limit   = ($current_page - 1) * $per_page;
2856
-        $where   = ['taxonomy' => EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY];
2857
-        if (isset($this->_req_data['s'])) {
2858
-            $sstr        = '%' . $this->_req_data['s'] . '%';
2859
-            $where['OR'] = [
2860
-                'Term.name'   => ['LIKE', $sstr],
2861
-                'description' => ['LIKE', $sstr],
2862
-            ];
2863
-        }
2864
-        $query_params = [
2865
-            $where,
2866
-            'order_by'   => [$orderby => $order],
2867
-            'limit'      => $limit . ',' . $per_page,
2868
-            'force_join' => ['Term'],
2869
-        ];
2870
-        return $count
2871
-            ? EEM_Term_Taxonomy::instance()->count($query_params, 'term_id')
2872
-            : EEM_Term_Taxonomy::instance()->get_all($query_params);
2873
-    }
2874
-
2875
-    /* end category stuff */
2876
-    /**************/
2877
-
2878
-
2879
-    /**
2880
-     * Callback for the `ee_save_timezone_setting` ajax action.
2881
-     *
2882
-     * @throws EE_Error
2883
-     * @throws InvalidArgumentException
2884
-     * @throws InvalidDataTypeException
2885
-     * @throws InvalidInterfaceException
2886
-     */
2887
-    public function save_timezonestring_setting()
2888
-    {
2889
-        $timezone_string = isset($this->_req_data['timezone_selected'])
2890
-            ? $this->_req_data['timezone_selected']
2891
-            : '';
2892
-        if (empty($timezone_string) || ! EEH_DTT_Helper::validate_timezone($timezone_string, false)) {
2893
-            EE_Error::add_error(
2894
-                esc_html__('An invalid timezone string submitted.', 'event_espresso'),
2895
-                __FILE__,
2896
-                __FUNCTION__,
2897
-                __LINE__
2898
-            );
2899
-            $this->_template_args['error'] = true;
2900
-            $this->_return_json();
2901
-        }
2902
-
2903
-        update_option('timezone_string', $timezone_string);
2904
-        EE_Error::add_success(
2905
-            esc_html__('Your timezone string was updated.', 'event_espresso')
2906
-        );
2907
-        $this->_template_args['success'] = true;
2908
-        $this->_return_json(true, ['action' => 'create_new']);
2909
-    }
20
+	/**
21
+	 * This will hold the event object for event_details screen.
22
+	 *
23
+	 * @access protected
24
+	 * @var EE_Event $_event
25
+	 */
26
+	protected $_event;
27
+
28
+
29
+	/**
30
+	 * This will hold the category object for category_details screen.
31
+	 *
32
+	 * @var stdClass $_category
33
+	 */
34
+	protected $_category;
35
+
36
+
37
+	/**
38
+	 * This will hold the event model instance
39
+	 *
40
+	 * @var EEM_Event $_event_model
41
+	 */
42
+	protected $_event_model;
43
+
44
+
45
+	/**
46
+	 * @var EE_Event
47
+	 */
48
+	protected $_cpt_model_obj = false;
49
+
50
+
51
+	/**
52
+	 * @var NodeGroupDao
53
+	 */
54
+	protected $model_obj_node_group_persister;
55
+
56
+
57
+	/**
58
+	 * Initialize page props for this admin page group.
59
+	 */
60
+	protected function _init_page_props()
61
+	{
62
+		$this->page_slug        = EVENTS_PG_SLUG;
63
+		$this->page_label       = EVENTS_LABEL;
64
+		$this->_admin_base_url  = EVENTS_ADMIN_URL;
65
+		$this->_admin_base_path = EVENTS_ADMIN;
66
+		$this->_cpt_model_names = [
67
+			'create_new' => 'EEM_Event',
68
+			'edit'       => 'EEM_Event',
69
+		];
70
+		$this->_cpt_edit_routes = [
71
+			'espresso_events' => 'edit',
72
+		];
73
+		add_action(
74
+			'AHEE__EE_Admin_Page_CPT__set_model_object__after_set_object',
75
+			[$this, 'verify_event_edit'],
76
+			10,
77
+			2
78
+		);
79
+	}
80
+
81
+
82
+	/**
83
+	 * Sets the ajax hooks used for this admin page group.
84
+	 */
85
+	protected function _ajax_hooks()
86
+	{
87
+		add_action('wp_ajax_ee_save_timezone_setting', [$this, 'save_timezonestring_setting']);
88
+	}
89
+
90
+
91
+	/**
92
+	 * Sets the page properties for this admin page group.
93
+	 */
94
+	protected function _define_page_props()
95
+	{
96
+		$this->_admin_page_title = EVENTS_LABEL;
97
+		$this->_labels           = [
98
+			'buttons'      => [
99
+				'add'             => esc_html__('Add New Event', 'event_espresso'),
100
+				'edit'            => esc_html__('Edit Event', 'event_espresso'),
101
+				'delete'          => esc_html__('Delete Event', 'event_espresso'),
102
+				'add_category'    => esc_html__('Add New Category', 'event_espresso'),
103
+				'edit_category'   => esc_html__('Edit Category', 'event_espresso'),
104
+				'delete_category' => esc_html__('Delete Category', 'event_espresso'),
105
+			],
106
+			'editor_title' => [
107
+				'espresso_events' => esc_html__('Enter event title here', 'event_espresso'),
108
+			],
109
+			'publishbox'   => [
110
+				'create_new'        => esc_html__('Save New Event', 'event_espresso'),
111
+				'edit'              => esc_html__('Update Event', 'event_espresso'),
112
+				'add_category'      => esc_html__('Save New Category', 'event_espresso'),
113
+				'edit_category'     => esc_html__('Update Category', 'event_espresso'),
114
+				'template_settings' => esc_html__('Update Settings', 'event_espresso'),
115
+			],
116
+		];
117
+	}
118
+
119
+
120
+	/**
121
+	 * Sets the page routes property for this admin page group.
122
+	 */
123
+	protected function _set_page_routes()
124
+	{
125
+		// load formatter helper
126
+		// load field generator helper
127
+		// is there a evt_id in the request?
128
+		$evt_id             = ! empty($this->_req_data['EVT_ID']) && ! is_array($this->_req_data['EVT_ID'])
129
+			? $this->_req_data['EVT_ID']
130
+			: 0;
131
+		$evt_id             = ! empty($this->_req_data['post']) ? $this->_req_data['post'] : $evt_id;
132
+		$this->_page_routes = [
133
+			'default'                       => [
134
+				'func'       => '_events_overview_list_table',
135
+				'capability' => 'ee_read_events',
136
+			],
137
+			'create_new'                    => [
138
+				'func'       => '_create_new_cpt_item',
139
+				'capability' => 'ee_edit_events',
140
+			],
141
+			'edit'                          => [
142
+				'func'       => '_edit_cpt_item',
143
+				'capability' => 'ee_edit_event',
144
+				'obj_id'     => $evt_id,
145
+			],
146
+			'copy_event'                    => [
147
+				'func'       => '_copy_events',
148
+				'capability' => 'ee_edit_event',
149
+				'obj_id'     => $evt_id,
150
+				'noheader'   => true,
151
+			],
152
+			'trash_event'                   => [
153
+				'func'       => '_trash_or_restore_event',
154
+				'args'       => ['event_status' => 'trash'],
155
+				'capability' => 'ee_delete_event',
156
+				'obj_id'     => $evt_id,
157
+				'noheader'   => true,
158
+			],
159
+			'trash_events'                  => [
160
+				'func'       => '_trash_or_restore_events',
161
+				'args'       => ['event_status' => 'trash'],
162
+				'capability' => 'ee_delete_events',
163
+				'noheader'   => true,
164
+			],
165
+			'restore_event'                 => [
166
+				'func'       => '_trash_or_restore_event',
167
+				'args'       => ['event_status' => 'draft'],
168
+				'capability' => 'ee_delete_event',
169
+				'obj_id'     => $evt_id,
170
+				'noheader'   => true,
171
+			],
172
+			'restore_events'                => [
173
+				'func'       => '_trash_or_restore_events',
174
+				'args'       => ['event_status' => 'draft'],
175
+				'capability' => 'ee_delete_events',
176
+				'noheader'   => true,
177
+			],
178
+			'delete_event'                  => [
179
+				'func'       => '_delete_event',
180
+				'capability' => 'ee_delete_event',
181
+				'obj_id'     => $evt_id,
182
+				'noheader'   => true,
183
+			],
184
+			'delete_events'                 => [
185
+				'func'       => '_delete_events',
186
+				'capability' => 'ee_delete_events',
187
+				'noheader'   => true,
188
+			],
189
+			'view_report'                   => [
190
+				'func'       => '_view_report',
191
+				'capability' => 'ee_edit_events',
192
+			],
193
+			'default_event_settings'        => [
194
+				'func'       => '_default_event_settings',
195
+				'capability' => 'manage_options',
196
+			],
197
+			'update_default_event_settings' => [
198
+				'func'       => '_update_default_event_settings',
199
+				'capability' => 'manage_options',
200
+				'noheader'   => true,
201
+			],
202
+			'template_settings'             => [
203
+				'func'       => '_template_settings',
204
+				'capability' => 'manage_options',
205
+			],
206
+			// event category tab related
207
+			'add_category'                  => [
208
+				'func'       => '_category_details',
209
+				'capability' => 'ee_edit_event_category',
210
+				'args'       => ['add'],
211
+			],
212
+			'edit_category'                 => [
213
+				'func'       => '_category_details',
214
+				'capability' => 'ee_edit_event_category',
215
+				'args'       => ['edit'],
216
+			],
217
+			'delete_categories'             => [
218
+				'func'       => '_delete_categories',
219
+				'capability' => 'ee_delete_event_category',
220
+				'noheader'   => true,
221
+			],
222
+			'delete_category'               => [
223
+				'func'       => '_delete_categories',
224
+				'capability' => 'ee_delete_event_category',
225
+				'noheader'   => true,
226
+			],
227
+			'insert_category'               => [
228
+				'func'       => '_insert_or_update_category',
229
+				'args'       => ['new_category' => true],
230
+				'capability' => 'ee_edit_event_category',
231
+				'noheader'   => true,
232
+			],
233
+			'update_category'               => [
234
+				'func'       => '_insert_or_update_category',
235
+				'args'       => ['new_category' => false],
236
+				'capability' => 'ee_edit_event_category',
237
+				'noheader'   => true,
238
+			],
239
+			'category_list'                 => [
240
+				'func'       => '_category_list_table',
241
+				'capability' => 'ee_manage_event_categories',
242
+			],
243
+			'preview_deletion'              => [
244
+				'func'       => 'previewDeletion',
245
+				'capability' => 'ee_delete_events',
246
+			],
247
+			'confirm_deletion'              => [
248
+				'func'       => 'confirmDeletion',
249
+				'capability' => 'ee_delete_events',
250
+				'noheader'   => true,
251
+			],
252
+		];
253
+	}
254
+
255
+
256
+	/**
257
+	 * Set the _page_config property for this admin page group.
258
+	 */
259
+	protected function _set_page_config()
260
+	{
261
+		$this->_page_config = [
262
+			'default'                => [
263
+				'nav'           => [
264
+					'label' => esc_html__('Overview', 'event_espresso'),
265
+					'order' => 10,
266
+				],
267
+				'list_table'    => 'Events_Admin_List_Table',
268
+				'help_tabs'     => [
269
+					'events_overview_help_tab'                       => [
270
+						'title'    => esc_html__('Events Overview', 'event_espresso'),
271
+						'filename' => 'events_overview',
272
+					],
273
+					'events_overview_table_column_headings_help_tab' => [
274
+						'title'    => esc_html__('Events Overview Table Column Headings', 'event_espresso'),
275
+						'filename' => 'events_overview_table_column_headings',
276
+					],
277
+					'events_overview_filters_help_tab'               => [
278
+						'title'    => esc_html__('Events Overview Filters', 'event_espresso'),
279
+						'filename' => 'events_overview_filters',
280
+					],
281
+					'events_overview_view_help_tab'                  => [
282
+						'title'    => esc_html__('Events Overview Views', 'event_espresso'),
283
+						'filename' => 'events_overview_views',
284
+					],
285
+					'events_overview_other_help_tab'                 => [
286
+						'title'    => esc_html__('Events Overview Other', 'event_espresso'),
287
+						'filename' => 'events_overview_other',
288
+					],
289
+				],
290
+				// disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
291
+				// 'help_tour'     => [
292
+				//     'Event_Overview_Help_Tour',
293
+				//     // 'New_Features_Test_Help_Tour' for testing multiple help tour
294
+				// ],
295
+				'require_nonce' => false,
296
+				'qtips'         => ['EE_Event_List_Table_Tips'],
297
+			],
298
+			'create_new'             => [
299
+				'nav'           => [
300
+					'label'      => esc_html__('Add Event', 'event_espresso'),
301
+					'order'      => 5,
302
+					'persistent' => false,
303
+				],
304
+				'metaboxes'     => ['_register_event_editor_meta_boxes'],
305
+				'help_tabs'     => [
306
+					'event_editor_help_tab'                            => [
307
+						'title'    => esc_html__('Event Editor', 'event_espresso'),
308
+						'filename' => 'event_editor',
309
+					],
310
+					'event_editor_title_richtexteditor_help_tab'       => [
311
+						'title'    => esc_html__('Event Title & Rich Text Editor', 'event_espresso'),
312
+						'filename' => 'event_editor_title_richtexteditor',
313
+					],
314
+					'event_editor_venue_details_help_tab'              => [
315
+						'title'    => esc_html__('Event Venue Details', 'event_espresso'),
316
+						'filename' => 'event_editor_venue_details',
317
+					],
318
+					'event_editor_event_datetimes_help_tab'            => [
319
+						'title'    => esc_html__('Event Datetimes', 'event_espresso'),
320
+						'filename' => 'event_editor_event_datetimes',
321
+					],
322
+					'event_editor_event_tickets_help_tab'              => [
323
+						'title'    => esc_html__('Event Tickets', 'event_espresso'),
324
+						'filename' => 'event_editor_event_tickets',
325
+					],
326
+					'event_editor_event_registration_options_help_tab' => [
327
+						'title'    => esc_html__('Event Registration Options', 'event_espresso'),
328
+						'filename' => 'event_editor_event_registration_options',
329
+					],
330
+					'event_editor_tags_categories_help_tab'            => [
331
+						'title'    => esc_html__('Event Tags & Categories', 'event_espresso'),
332
+						'filename' => 'event_editor_tags_categories',
333
+					],
334
+					'event_editor_questions_registrants_help_tab'      => [
335
+						'title'    => esc_html__('Questions for Registrants', 'event_espresso'),
336
+						'filename' => 'event_editor_questions_registrants',
337
+					],
338
+					'event_editor_save_new_event_help_tab'             => [
339
+						'title'    => esc_html__('Save New Event', 'event_espresso'),
340
+						'filename' => 'event_editor_save_new_event',
341
+					],
342
+					'event_editor_other_help_tab'                      => [
343
+						'title'    => esc_html__('Event Other', 'event_espresso'),
344
+						'filename' => 'event_editor_other',
345
+					],
346
+				],
347
+				// disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
348
+				// 'help_tour'     => [
349
+				//     'Event_Editor_Help_Tour',
350
+				// ],
351
+				'qtips'         => ['EE_Event_Editor_Decaf_Tips'],
352
+				'require_nonce' => false,
353
+			],
354
+			'edit'                   => [
355
+				'nav'           => [
356
+					'label'      => esc_html__('Edit Event', 'event_espresso'),
357
+					'order'      => 5,
358
+					'persistent' => false,
359
+					'url'        => isset($this->_req_data['post'])
360
+						? EE_Admin_Page::add_query_args_and_nonce(
361
+							['post' => $this->_req_data['post'], 'action' => 'edit'],
362
+							$this->_current_page_view_url
363
+						)
364
+						: $this->_admin_base_url,
365
+				],
366
+				'metaboxes'     => ['_register_event_editor_meta_boxes'],
367
+				'help_tabs'     => [
368
+					'event_editor_help_tab'                            => [
369
+						'title'    => esc_html__('Event Editor', 'event_espresso'),
370
+						'filename' => 'event_editor',
371
+					],
372
+					'event_editor_title_richtexteditor_help_tab'       => [
373
+						'title'    => esc_html__('Event Title & Rich Text Editor', 'event_espresso'),
374
+						'filename' => 'event_editor_title_richtexteditor',
375
+					],
376
+					'event_editor_venue_details_help_tab'              => [
377
+						'title'    => esc_html__('Event Venue Details', 'event_espresso'),
378
+						'filename' => 'event_editor_venue_details',
379
+					],
380
+					'event_editor_event_datetimes_help_tab'            => [
381
+						'title'    => esc_html__('Event Datetimes', 'event_espresso'),
382
+						'filename' => 'event_editor_event_datetimes',
383
+					],
384
+					'event_editor_event_tickets_help_tab'              => [
385
+						'title'    => esc_html__('Event Tickets', 'event_espresso'),
386
+						'filename' => 'event_editor_event_tickets',
387
+					],
388
+					'event_editor_event_registration_options_help_tab' => [
389
+						'title'    => esc_html__('Event Registration Options', 'event_espresso'),
390
+						'filename' => 'event_editor_event_registration_options',
391
+					],
392
+					'event_editor_tags_categories_help_tab'            => [
393
+						'title'    => esc_html__('Event Tags & Categories', 'event_espresso'),
394
+						'filename' => 'event_editor_tags_categories',
395
+					],
396
+					'event_editor_questions_registrants_help_tab'      => [
397
+						'title'    => esc_html__('Questions for Registrants', 'event_espresso'),
398
+						'filename' => 'event_editor_questions_registrants',
399
+					],
400
+					'event_editor_save_new_event_help_tab'             => [
401
+						'title'    => esc_html__('Save New Event', 'event_espresso'),
402
+						'filename' => 'event_editor_save_new_event',
403
+					],
404
+					'event_editor_other_help_tab'                      => [
405
+						'title'    => esc_html__('Event Other', 'event_espresso'),
406
+						'filename' => 'event_editor_other',
407
+					],
408
+				],
409
+				'require_nonce' => false,
410
+			],
411
+			'default_event_settings' => [
412
+				'nav'           => [
413
+					'label' => esc_html__('Default Settings', 'event_espresso'),
414
+					'order' => 40,
415
+				],
416
+				'metaboxes'     => array_merge($this->_default_espresso_metaboxes, ['_publish_post_box']),
417
+				'labels'        => [
418
+					'publishbox' => esc_html__('Update Settings', 'event_espresso'),
419
+				],
420
+				'help_tabs'     => [
421
+					'default_settings_help_tab'        => [
422
+						'title'    => esc_html__('Default Event Settings', 'event_espresso'),
423
+						'filename' => 'events_default_settings',
424
+					],
425
+					'default_settings_status_help_tab' => [
426
+						'title'    => esc_html__('Default Registration Status', 'event_espresso'),
427
+						'filename' => 'events_default_settings_status',
428
+					],
429
+					'default_maximum_tickets_help_tab' => [
430
+						'title'    => esc_html__('Default Maximum Tickets Per Order', 'event_espresso'),
431
+						'filename' => 'events_default_settings_max_tickets',
432
+					],
433
+				],
434
+				// disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
435
+				// 'help_tour'     => ['Event_Default_Settings_Help_Tour'],
436
+				'require_nonce' => false,
437
+			],
438
+			// template settings
439
+			'template_settings'      => [
440
+				'nav'           => [
441
+					'label' => esc_html__('Templates', 'event_espresso'),
442
+					'order' => 30,
443
+				],
444
+				'metaboxes'     => $this->_default_espresso_metaboxes,
445
+				'help_tabs'     => [
446
+					'general_settings_templates_help_tab' => [
447
+						'title'    => esc_html__('Templates', 'event_espresso'),
448
+						'filename' => 'general_settings_templates',
449
+					],
450
+				],
451
+				// disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
452
+				// 'help_tour'     => ['Templates_Help_Tour'],
453
+				'require_nonce' => false,
454
+			],
455
+			// event category stuff
456
+			'add_category'           => [
457
+				'nav'           => [
458
+					'label'      => esc_html__('Add Category', 'event_espresso'),
459
+					'order'      => 15,
460
+					'persistent' => false,
461
+				],
462
+				'help_tabs'     => [
463
+					'add_category_help_tab' => [
464
+						'title'    => esc_html__('Add New Event Category', 'event_espresso'),
465
+						'filename' => 'events_add_category',
466
+					],
467
+				],
468
+				// disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
469
+				// 'help_tour'     => ['Event_Add_Category_Help_Tour'],
470
+				'metaboxes'     => ['_publish_post_box'],
471
+				'require_nonce' => false,
472
+			],
473
+			'edit_category'          => [
474
+				'nav'           => [
475
+					'label'      => esc_html__('Edit Category', 'event_espresso'),
476
+					'order'      => 15,
477
+					'persistent' => false,
478
+					'url'        => isset($this->_req_data['EVT_CAT_ID'])
479
+						? add_query_arg(
480
+							['EVT_CAT_ID' => $this->_req_data['EVT_CAT_ID']],
481
+							$this->_current_page_view_url
482
+						)
483
+						: $this->_admin_base_url,
484
+				],
485
+				'help_tabs'     => [
486
+					'edit_category_help_tab' => [
487
+						'title'    => esc_html__('Edit Event Category', 'event_espresso'),
488
+						'filename' => 'events_edit_category',
489
+					],
490
+				],
491
+				/*'help_tour' => ['Event_Edit_Category_Help_Tour'],*/
492
+				'metaboxes'     => ['_publish_post_box'],
493
+				'require_nonce' => false,
494
+			],
495
+			'category_list'          => [
496
+				'nav'           => [
497
+					'label' => esc_html__('Categories', 'event_espresso'),
498
+					'order' => 20,
499
+				],
500
+				'list_table'    => 'Event_Categories_Admin_List_Table',
501
+				'help_tabs'     => [
502
+					'events_categories_help_tab'                       => [
503
+						'title'    => esc_html__('Event Categories', 'event_espresso'),
504
+						'filename' => 'events_categories',
505
+					],
506
+					'events_categories_table_column_headings_help_tab' => [
507
+						'title'    => esc_html__('Event Categories Table Column Headings', 'event_espresso'),
508
+						'filename' => 'events_categories_table_column_headings',
509
+					],
510
+					'events_categories_view_help_tab'                  => [
511
+						'title'    => esc_html__('Event Categories Views', 'event_espresso'),
512
+						'filename' => 'events_categories_views',
513
+					],
514
+					'events_categories_other_help_tab'                 => [
515
+						'title'    => esc_html__('Event Categories Other', 'event_espresso'),
516
+						'filename' => 'events_categories_other',
517
+					],
518
+				],
519
+				// disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
520
+				// 'help_tour'     => [
521
+				//     'Event_Categories_Help_Tour',
522
+				// ],
523
+				'metaboxes'     => $this->_default_espresso_metaboxes,
524
+				'require_nonce' => false,
525
+			],
526
+			'preview_deletion'       => [
527
+				'nav'           => [
528
+					'label'      => esc_html__('Preview Deletion', 'event_espresso'),
529
+					'order'      => 15,
530
+					'persistent' => false,
531
+					'url'        => '',
532
+				],
533
+				'require_nonce' => false,
534
+			],
535
+		];
536
+		// only load EE_Event_Editor_Decaf_Tips if domain is not caffeinated
537
+		$domain = $this->loader->getShared('EventEspresso\core\domain\Domain');
538
+		if (! $domain->isCaffeinated()) {
539
+			$this->_page_config['create_new']['qtips'] = ['EE_Event_Editor_Decaf_Tips'];
540
+			$this->_page_config['edit']['qtips']       = ['EE_Event_Editor_Decaf_Tips'];
541
+		}
542
+	}
543
+
544
+
545
+	/**
546
+	 * Used to register any global screen options if necessary for every route in this admin page group.
547
+	 */
548
+	protected function _add_screen_options()
549
+	{
550
+	}
551
+
552
+
553
+	/**
554
+	 * Implementing the screen options for the 'default' route.
555
+	 *
556
+	 * @throws InvalidArgumentException
557
+	 * @throws InvalidDataTypeException
558
+	 * @throws InvalidInterfaceException
559
+	 */
560
+	protected function _add_screen_options_default()
561
+	{
562
+		$this->_per_page_screen_option();
563
+	}
564
+
565
+
566
+	/**
567
+	 * Implementing screen options for the category list route.
568
+	 *
569
+	 * @throws InvalidArgumentException
570
+	 * @throws InvalidDataTypeException
571
+	 * @throws InvalidInterfaceException
572
+	 */
573
+	protected function _add_screen_options_category_list()
574
+	{
575
+		$page_title              = $this->_admin_page_title;
576
+		$this->_admin_page_title = esc_html__('Categories', 'event_espresso');
577
+		$this->_per_page_screen_option();
578
+		$this->_admin_page_title = $page_title;
579
+	}
580
+
581
+
582
+	/**
583
+	 * Used to register any global feature pointers for the admin page group.
584
+	 */
585
+	protected function _add_feature_pointers()
586
+	{
587
+	}
588
+
589
+
590
+	/**
591
+	 * Registers and enqueues any global scripts and styles for the entire admin page group.
592
+	 */
593
+	public function load_scripts_styles()
594
+	{
595
+		wp_register_style(
596
+			'events-admin-css',
597
+			EVENTS_ASSETS_URL . 'events-admin-page.css',
598
+			[],
599
+			EVENT_ESPRESSO_VERSION
600
+		);
601
+		wp_register_style(
602
+			'ee-cat-admin',
603
+			EVENTS_ASSETS_URL . 'ee-cat-admin.css',
604
+			[],
605
+			EVENT_ESPRESSO_VERSION
606
+		);
607
+		wp_enqueue_style('events-admin-css');
608
+		wp_enqueue_style('ee-cat-admin');
609
+		// scripts
610
+		wp_register_script(
611
+			'event_editor_js',
612
+			EVENTS_ASSETS_URL . 'event_editor.js',
613
+			['ee_admin_js', 'jquery-ui-slider', 'jquery-ui-timepicker-addon'],
614
+			EVENT_ESPRESSO_VERSION,
615
+			true
616
+		);
617
+	}
618
+
619
+
620
+	/**
621
+	 * Enqueuing scripts and styles specific to this view
622
+	 */
623
+	public function load_scripts_styles_create_new()
624
+	{
625
+		$this->load_scripts_styles_edit();
626
+	}
627
+
628
+
629
+	/**
630
+	 * Enqueuing scripts and styles specific to this view
631
+	 */
632
+	public function load_scripts_styles_edit()
633
+	{
634
+		// styles
635
+		wp_enqueue_style('espresso-ui-theme');
636
+		wp_register_style(
637
+			'event-editor-css',
638
+			EVENTS_ASSETS_URL . 'event-editor.css',
639
+			['ee-admin-css'],
640
+			EVENT_ESPRESSO_VERSION
641
+		);
642
+		wp_enqueue_style('event-editor-css');
643
+		// scripts
644
+		if (! $this->admin_config->useAdvancedEditor()) {
645
+			wp_register_script(
646
+				'event-datetime-metabox',
647
+				EVENTS_ASSETS_URL . 'event-datetime-metabox.js',
648
+				['event_editor_js', 'ee-datepicker'],
649
+				EVENT_ESPRESSO_VERSION
650
+			);
651
+			wp_enqueue_script('event-datetime-metabox');
652
+		}
653
+	}
654
+
655
+
656
+	/**
657
+	 * Populating the _views property for the category list table view.
658
+	 */
659
+	protected function _set_list_table_views_category_list()
660
+	{
661
+		$this->_views = [
662
+			'all' => [
663
+				'slug'        => 'all',
664
+				'label'       => esc_html__('All', 'event_espresso'),
665
+				'count'       => 0,
666
+				'bulk_action' => [
667
+					'delete_categories' => esc_html__('Delete Permanently', 'event_espresso'),
668
+				],
669
+			],
670
+		];
671
+	}
672
+
673
+
674
+	/**
675
+	 * For adding anything that fires on the admin_init hook for any route within this admin page group.
676
+	 */
677
+	public function admin_init()
678
+	{
679
+		EE_Registry::$i18n_js_strings['image_confirm'] = esc_html__(
680
+			'Do you really want to delete this image? Please remember to update your event to complete the removal.',
681
+			'event_espresso'
682
+		);
683
+	}
684
+
685
+
686
+	/**
687
+	 * For adding anything that should be triggered on the admin_notices hook for any route within this admin page
688
+	 * group.
689
+	 */
690
+	public function admin_notices()
691
+	{
692
+	}
693
+
694
+
695
+	/**
696
+	 * For adding anything that should be triggered on the `admin_print_footer_scripts` hook for any route within
697
+	 * this admin page group.
698
+	 */
699
+	public function admin_footer_scripts()
700
+	{
701
+	}
702
+
703
+
704
+	/**
705
+	 * Call this function to verify if an event is public and has tickets for sale.  If it does, then we need to show a
706
+	 * warning (via EE_Error::add_error());
707
+	 *
708
+	 * @param EE_Event $event Event object
709
+	 * @param string   $req_type
710
+	 * @return void
711
+	 * @throws EE_Error
712
+	 * @access public
713
+	 */
714
+	public function verify_event_edit($event = null, $req_type = '')
715
+	{
716
+		// don't need to do this when processing
717
+		if (! empty($req_type)) {
718
+			return;
719
+		}
720
+		// no event?
721
+		if (! $event instanceof EE_Event) {
722
+			$event = $this->_cpt_model_obj;
723
+		}
724
+		// STILL no event?
725
+		if (! $event instanceof EE_Event) {
726
+			return;
727
+		}
728
+		$orig_status = $event->status();
729
+		// first check if event is active.
730
+		if (
731
+			$orig_status === EEM_Event::cancelled
732
+			|| $orig_status === EEM_Event::postponed
733
+			|| $event->is_expired()
734
+			|| $event->is_inactive()
735
+		) {
736
+			return;
737
+		}
738
+		// made it here so it IS active... next check that any of the tickets are sold.
739
+		if ($event->is_sold_out(true)) {
740
+			if ($orig_status !== EEM_Event::sold_out && $event->status() !== $orig_status) {
741
+				EE_Error::add_attention(
742
+					sprintf(
743
+						esc_html__(
744
+							'Please note that the Event Status has automatically been changed to %s because there are no more spaces available for this event.  However, this change is not permanent until you update the event.  You can change the status back to something else before updating if you wish.',
745
+							'event_espresso'
746
+						),
747
+						EEH_Template::pretty_status(EEM_Event::sold_out, false, 'sentence')
748
+					)
749
+				);
750
+			}
751
+			return;
752
+		}
753
+		if ($orig_status === EEM_Event::sold_out) {
754
+			EE_Error::add_attention(
755
+				sprintf(
756
+					esc_html__(
757
+						'Please note that the Event Status has automatically been changed to %s because more spaces have become available for this event, most likely due to abandoned transactions freeing up reserved tickets.  However, this change is not permanent until you update the event. If you wish, you can change the status back to something else before updating.',
758
+						'event_espresso'
759
+					),
760
+					EEH_Template::pretty_status($event->status(), false, 'sentence')
761
+				)
762
+			);
763
+		}
764
+		// now we need to determine if the event has any tickets on sale.  If not then we dont' show the error
765
+		if (! $event->tickets_on_sale()) {
766
+			return;
767
+		}
768
+		// made it here so show warning
769
+		$this->_edit_event_warning();
770
+	}
771
+
772
+
773
+	/**
774
+	 * This is the text used for when an event is being edited that is public and has tickets for sale.
775
+	 * When needed, hook this into a EE_Error::add_error() notice.
776
+	 *
777
+	 * @access protected
778
+	 * @return void
779
+	 */
780
+	protected function _edit_event_warning()
781
+	{
782
+		// we don't want to add warnings during these requests
783
+		if (isset($this->_req_data['action']) && $this->_req_data['action'] === 'editpost') {
784
+			return;
785
+		}
786
+		EE_Error::add_attention(
787
+			sprintf(
788
+				esc_html__(
789
+					'Your event is open for registration. Making changes may disrupt any transactions in progress. %sLearn more%s',
790
+					'event_espresso'
791
+				),
792
+				'<a class="espresso-help-tab-lnk">',
793
+				'</a>'
794
+			)
795
+		);
796
+	}
797
+
798
+
799
+	/**
800
+	 * When a user is creating a new event, notify them if they haven't set their timezone.
801
+	 * Otherwise, do the normal logic
802
+	 *
803
+	 * @return void
804
+	 * @throws EE_Error
805
+	 * @throws InvalidArgumentException
806
+	 * @throws InvalidDataTypeException
807
+	 * @throws InvalidInterfaceException
808
+	 */
809
+	protected function _create_new_cpt_item()
810
+	{
811
+		$has_timezone_string = get_option('timezone_string');
812
+		// only nag them about setting their timezone if it's their first event, and they haven't already done it
813
+		if (! $has_timezone_string && ! EEM_Event::instance()->exists([])) {
814
+			EE_Error::add_attention(
815
+				sprintf(
816
+					__(
817
+						'Your website\'s timezone is currently set to a UTC offset. We recommend updating your timezone to a city or region near you before you create an event. Change your timezone now:%1$s%2$s%3$sChange Timezone%4$s',
818
+						'event_espresso'
819
+					),
820
+					'<br>',
821
+					'<select id="timezone_string" name="timezone_string" aria-describedby="timezone-description">'
822
+					. EEH_DTT_Helper::wp_timezone_choice('', EEH_DTT_Helper::get_user_locale())
823
+					. '</select>',
824
+					'<button class="button button-secondary timezone-submit">',
825
+					'</button><span class="spinner"></span>'
826
+				),
827
+				__FILE__,
828
+				__FUNCTION__,
829
+				__LINE__
830
+			);
831
+		}
832
+		parent::_create_new_cpt_item();
833
+	}
834
+
835
+
836
+	/**
837
+	 * Sets the _views property for the default route in this admin page group.
838
+	 */
839
+	protected function _set_list_table_views_default()
840
+	{
841
+		$this->_views = [
842
+			'all'   => [
843
+				'slug'        => 'all',
844
+				'label'       => esc_html__('View All Events', 'event_espresso'),
845
+				'count'       => 0,
846
+				'bulk_action' => [
847
+					'trash_events' => esc_html__('Move to Trash', 'event_espresso'),
848
+				],
849
+			],
850
+			'draft' => [
851
+				'slug'        => 'draft',
852
+				'label'       => esc_html__('Draft', 'event_espresso'),
853
+				'count'       => 0,
854
+				'bulk_action' => [
855
+					'trash_events' => esc_html__('Move to Trash', 'event_espresso'),
856
+				],
857
+			],
858
+		];
859
+		if (EE_Registry::instance()->CAP->current_user_can('ee_delete_events', 'espresso_events_trash_events')) {
860
+			$this->_views['trash'] = [
861
+				'slug'        => 'trash',
862
+				'label'       => esc_html__('Trash', 'event_espresso'),
863
+				'count'       => 0,
864
+				'bulk_action' => [
865
+					'restore_events' => esc_html__('Restore From Trash', 'event_espresso'),
866
+					'delete_events'  => esc_html__('Delete Permanently', 'event_espresso'),
867
+				],
868
+			];
869
+		}
870
+	}
871
+
872
+
873
+	/**
874
+	 * Provides the legend item array for the default list table view.
875
+	 *
876
+	 * @return array
877
+	 */
878
+	protected function _event_legend_items()
879
+	{
880
+		$items    = [
881
+			'view_details'   => [
882
+				'class' => 'dashicons dashicons-search',
883
+				'desc'  => esc_html__('View Event', 'event_espresso'),
884
+			],
885
+			'edit_event'     => [
886
+				'class' => 'ee-icon ee-icon-calendar-edit',
887
+				'desc'  => esc_html__('Edit Event Details', 'event_espresso'),
888
+			],
889
+			'view_attendees' => [
890
+				'class' => 'dashicons dashicons-groups',
891
+				'desc'  => esc_html__('View Registrations for Event', 'event_espresso'),
892
+			],
893
+		];
894
+		$items    = apply_filters('FHEE__Events_Admin_Page___event_legend_items__items', $items);
895
+		$statuses = [
896
+			'sold_out_status'  => [
897
+				'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::sold_out,
898
+				'desc'  => EEH_Template::pretty_status(EE_Datetime::sold_out, false, 'sentence'),
899
+			],
900
+			'active_status'    => [
901
+				'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::active,
902
+				'desc'  => EEH_Template::pretty_status(EE_Datetime::active, false, 'sentence'),
903
+			],
904
+			'upcoming_status'  => [
905
+				'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::upcoming,
906
+				'desc'  => EEH_Template::pretty_status(EE_Datetime::upcoming, false, 'sentence'),
907
+			],
908
+			'postponed_status' => [
909
+				'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::postponed,
910
+				'desc'  => EEH_Template::pretty_status(EE_Datetime::postponed, false, 'sentence'),
911
+			],
912
+			'cancelled_status' => [
913
+				'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::cancelled,
914
+				'desc'  => EEH_Template::pretty_status(EE_Datetime::cancelled, false, 'sentence'),
915
+			],
916
+			'expired_status'   => [
917
+				'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::expired,
918
+				'desc'  => EEH_Template::pretty_status(EE_Datetime::expired, false, 'sentence'),
919
+			],
920
+			'inactive_status'  => [
921
+				'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::inactive,
922
+				'desc'  => EEH_Template::pretty_status(EE_Datetime::inactive, false, 'sentence'),
923
+			],
924
+		];
925
+		$statuses = apply_filters('FHEE__Events_Admin_Page__event_legend_items__statuses', $statuses);
926
+		return array_merge($items, $statuses);
927
+	}
928
+
929
+
930
+	/**
931
+	 * @return EEM_Event
932
+	 * @throws EE_Error
933
+	 * @throws InvalidArgumentException
934
+	 * @throws InvalidDataTypeException
935
+	 * @throws InvalidInterfaceException
936
+	 * @throws ReflectionException
937
+	 */
938
+	private function _event_model()
939
+	{
940
+		if (! $this->_event_model instanceof EEM_Event) {
941
+			$this->_event_model = EE_Registry::instance()->load_model('Event');
942
+		}
943
+		return $this->_event_model;
944
+	}
945
+
946
+
947
+	/**
948
+	 * Adds extra buttons to the WP CPT permalink field row.
949
+	 * Method is called from parent and is hooked into the wp 'get_sample_permalink_html' filter.
950
+	 *
951
+	 * @param string $return    the current html
952
+	 * @param int    $id        the post id for the page
953
+	 * @param string $new_title What the title is
954
+	 * @param string $new_slug  what the slug is
955
+	 * @return string            The new html string for the permalink area
956
+	 */
957
+	public function extra_permalink_field_buttons($return, $id, $new_title, $new_slug)
958
+	{
959
+		// make sure this is only when editing
960
+		if (! empty($id)) {
961
+			$post   = get_post($id);
962
+			$return .= '<a class="button button-small" onclick="prompt(\'Shortcode:\', jQuery(\'#shortcode\').val()); return false;" href="#"  tabindex="-1">'
963
+					   . esc_html__('Shortcode', 'event_espresso')
964
+					   . '</a> ';
965
+			$return .= '<input id="shortcode" type="hidden" value="[ESPRESSO_TICKET_SELECTOR event_id='
966
+					   . $post->ID
967
+					   . ']">';
968
+		}
969
+		return $return;
970
+	}
971
+
972
+
973
+	/**
974
+	 * _events_overview_list_table
975
+	 * This contains the logic for showing the events_overview list
976
+	 *
977
+	 * @access protected
978
+	 * @return void
979
+	 * @throws DomainException
980
+	 * @throws EE_Error
981
+	 * @throws InvalidArgumentException
982
+	 * @throws InvalidDataTypeException
983
+	 * @throws InvalidInterfaceException
984
+	 */
985
+	protected function _events_overview_list_table()
986
+	{
987
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
988
+		$this->_template_args['after_list_table']                           =
989
+			! empty($this->_template_args['after_list_table'])
990
+				? (array) $this->_template_args['after_list_table']
991
+				: [];
992
+		$this->_template_args['after_list_table']['view_event_list_button'] = EEH_HTML::br()
993
+			. EEH_Template::get_button_or_link(
994
+				get_post_type_archive_link('espresso_events'),
995
+				esc_html__('View Event Archive Page', 'event_espresso'),
996
+				'button'
997
+			);
998
+		$this->_template_args['after_list_table']['legend']                 =
999
+			$this->_display_legend($this->_event_legend_items());
1000
+		$this->_admin_page_title                                            .= ' '
1001
+			. $this->get_action_link_or_button(
1002
+				'create_new',
1003
+				'add',
1004
+				[],
1005
+				'add-new-h2'
1006
+			);
1007
+		$this->display_admin_list_table_page_with_no_sidebar();
1008
+	}
1009
+
1010
+
1011
+	/**
1012
+	 * this allows for extra misc actions in the default WP publish box
1013
+	 *
1014
+	 * @return void
1015
+	 * @throws DomainException
1016
+	 * @throws EE_Error
1017
+	 * @throws InvalidArgumentException
1018
+	 * @throws InvalidDataTypeException
1019
+	 * @throws InvalidInterfaceException
1020
+	 * @throws ReflectionException
1021
+	 */
1022
+	public function extra_misc_actions_publish_box()
1023
+	{
1024
+		$this->_generate_publish_box_extra_content();
1025
+	}
1026
+
1027
+
1028
+	/**
1029
+	 * This is hooked into the WordPress do_action('save_post') hook and runs after the custom post type has been
1030
+	 * saved.
1031
+	 * Typically you would use this to save any additional data.
1032
+	 * Keep in mind also that "save_post" runs on EVERY post update to the database.
1033
+	 * ALSO very important.  When a post transitions from scheduled to published,
1034
+	 * the save_post action is fired but you will NOT have any _POST data containing any extra info you may have from
1035
+	 * other meta saves. So MAKE sure that you handle this accordingly.
1036
+	 *
1037
+	 * @access protected
1038
+	 * @abstract
1039
+	 * @param string $post_id The ID of the cpt that was saved (so you can link relationally)
1040
+	 * @param object $post    The post object of the cpt that was saved.
1041
+	 * @return void
1042
+	 * @throws EE_Error
1043
+	 * @throws InvalidArgumentException
1044
+	 * @throws InvalidDataTypeException
1045
+	 * @throws InvalidInterfaceException
1046
+	 * @throws ReflectionException
1047
+	 */
1048
+	protected function _insert_update_cpt_item($post_id, $post)
1049
+	{
1050
+		if ($post instanceof WP_Post && $post->post_type !== 'espresso_events') {
1051
+			// get out we're not processing an event save.
1052
+			return;
1053
+		}
1054
+		$event_values = [
1055
+			'EVT_member_only'     => ! empty($this->_req_data['member_only']) ? 1 : 0,
1056
+			'EVT_allow_overflow'  => ! empty($this->_req_data['EVT_allow_overflow']) ? 1 : 0,
1057
+			'EVT_timezone_string' => ! empty($this->_req_data['timezone_string'])
1058
+				? sanitize_text_field($this->_req_data['timezone_string'])
1059
+				: null,
1060
+		];
1061
+		/** @var FeatureFlags $flags */
1062
+		$flags = $this->loader->getShared('EventEspresso\core\domain\services\capabilities\FeatureFlags');
1063
+		// check if the new EDTR reg options meta box is being used, and if so, don't run updates for legacy version
1064
+		if (! $this->admin_config->useAdvancedEditor() || ! $flags->featureAllowed('use_reg_options_meta_box')) {
1065
+			$event_values['EVT_display_ticket_selector']     =
1066
+				! empty($this->_req_data['display_ticket_selector'])
1067
+					? 1
1068
+					: 0;
1069
+			$event_values['EVT_additional_limit']            = min(
1070
+				apply_filters('FHEE__EE_Events_Admin__insert_update_cpt_item__EVT_additional_limit_max', 255),
1071
+				! empty($this->_req_data['additional_limit'])
1072
+					? absint($this->_req_data['additional_limit'])
1073
+					: null
1074
+			);
1075
+			$event_values['EVT_default_registration_status'] =
1076
+				! empty($this->_req_data['EVT_default_registration_status'])
1077
+					? sanitize_text_field($this->_req_data['EVT_default_registration_status'])
1078
+					: EE_Registry::instance()->CFG->registration->default_STS_ID;
1079
+			$event_values['EVT_external_URL']                = ! empty($this->_req_data['externalURL'])
1080
+				? esc_url_raw($this->_req_data['externalURL'])
1081
+				: null;
1082
+			$event_values['EVT_phone']                       = ! empty($this->_req_data['event_phone'])
1083
+				? sanitize_text_field($this->_req_data['event_phone'])
1084
+				: null;
1085
+		}
1086
+		// update event
1087
+		$success = $this->_event_model()->update_by_ID($event_values, $post_id);
1088
+		// get event_object for other metaboxes...
1089
+		// though it would seem to make sense to just use $this->_event_model()->get_one_by_ID( $post_id )..
1090
+		// i have to setup where conditions to override the filters in the model that filter out autodraft
1091
+		// and inherit statuses so we GET the inherit id!
1092
+		$get_one_where = [
1093
+			$this->_event_model()->primary_key_name() => $post_id,
1094
+			'OR'                                      => [
1095
+				'status'   => $post->post_status,
1096
+				// if trying to "Publish" a sold out event, it's status will get switched back to "sold_out" in the db,
1097
+				// but the returned object here has a status of "publish", so use the original post status as well
1098
+				'status*1' => $this->_req_data['original_post_status'],
1099
+			],
1100
+		];
1101
+		$event         = $this->_event_model()->get_one([$get_one_where]);
1102
+		// the following are default callbacks for event attachment updates
1103
+		// that can be overridden by caffeinated functionality and/or addons.
1104
+		$event_update_callbacks = apply_filters(
1105
+			'FHEE__Events_Admin_Page___insert_update_cpt_item__event_update_callbacks',
1106
+			[
1107
+				[$this, '_default_venue_update'],
1108
+				[$this, '_default_tickets_update'],
1109
+			]
1110
+		);
1111
+		$att_success            = true;
1112
+		foreach ($event_update_callbacks as $e_callback) {
1113
+			$_success = is_callable($e_callback)
1114
+				? $e_callback($event, $this->_req_data)
1115
+				: false;
1116
+			// if ANY of these updates fail then we want the appropriate global error message
1117
+			$att_success = ! $att_success ? $att_success : $_success;
1118
+		}
1119
+		// any errors?
1120
+		if ($success && false === $att_success) {
1121
+			EE_Error::add_error(
1122
+				esc_html__(
1123
+					'Event Details saved successfully but something went wrong with saving attachments.',
1124
+					'event_espresso'
1125
+				),
1126
+				__FILE__,
1127
+				__FUNCTION__,
1128
+				__LINE__
1129
+			);
1130
+		} elseif ($success === false) {
1131
+			EE_Error::add_error(
1132
+				esc_html__('Event Details did not save successfully.', 'event_espresso'),
1133
+				__FILE__,
1134
+				__FUNCTION__,
1135
+				__LINE__
1136
+			);
1137
+		}
1138
+	}
1139
+
1140
+
1141
+	/**
1142
+	 * @param int $post_id
1143
+	 * @param int $revision_id
1144
+	 * @throws EE_Error
1145
+	 * @throws InvalidArgumentException
1146
+	 * @throws InvalidDataTypeException
1147
+	 * @throws InvalidInterfaceException
1148
+	 * @throws ReflectionException
1149
+	 * @see parent::restore_item()
1150
+	 */
1151
+	protected function _restore_cpt_item($post_id, $revision_id)
1152
+	{
1153
+		// copy existing event meta to new post
1154
+		$post_evt = $this->_event_model()->get_one_by_ID($post_id);
1155
+		if ($post_evt instanceof EE_Event) {
1156
+			// meta revision restore
1157
+			$post_evt->restore_revision($revision_id);
1158
+			// related objs restore
1159
+			$post_evt->restore_revision($revision_id, ['Venue', 'Datetime', 'Price']);
1160
+		}
1161
+	}
1162
+
1163
+
1164
+	/**
1165
+	 * Attach the venue to the Event
1166
+	 *
1167
+	 * @param EE_Event $evtobj Event Object to add the venue to
1168
+	 * @param array    $data   The request data from the form
1169
+	 * @return bool           Success or fail.
1170
+	 * @throws EE_Error
1171
+	 * @throws InvalidArgumentException
1172
+	 * @throws InvalidDataTypeException
1173
+	 * @throws InvalidInterfaceException
1174
+	 * @throws ReflectionException
1175
+	 */
1176
+	protected function _default_venue_update(EE_Event $evtobj, $data)
1177
+	{
1178
+		require_once(EE_MODELS . 'EEM_Venue.model.php');
1179
+		$venue_model   = EE_Registry::instance()->load_model('Venue');
1180
+		$rows_affected = null;
1181
+		$venue_id      = ! empty($data['venue_id']) ? $data['venue_id'] : null;
1182
+		// very important.  If we don't have a venue name...
1183
+		// then we'll get out because not necessary to create empty venue
1184
+		if (empty($data['venue_title'])) {
1185
+			return false;
1186
+		}
1187
+		$venue_array = [
1188
+			'VNU_wp_user'         => $evtobj->get('EVT_wp_user'),
1189
+			'VNU_name'            => ! empty($data['venue_title']) ? $data['venue_title'] : null,
1190
+			'VNU_desc'            => ! empty($data['venue_description']) ? $data['venue_description'] : null,
1191
+			'VNU_identifier'      => ! empty($data['venue_identifier']) ? $data['venue_identifier'] : null,
1192
+			'VNU_short_desc'      => ! empty($data['venue_short_description']) ? $data['venue_short_description']
1193
+				: null,
1194
+			'VNU_address'         => ! empty($data['address']) ? $data['address'] : null,
1195
+			'VNU_address2'        => ! empty($data['address2']) ? $data['address2'] : null,
1196
+			'VNU_city'            => ! empty($data['city']) ? $data['city'] : null,
1197
+			'STA_ID'              => ! empty($data['state']) ? $data['state'] : null,
1198
+			'CNT_ISO'             => ! empty($data['countries']) ? $data['countries'] : null,
1199
+			'VNU_zip'             => ! empty($data['zip']) ? $data['zip'] : null,
1200
+			'VNU_phone'           => ! empty($data['venue_phone']) ? $data['venue_phone'] : null,
1201
+			'VNU_capacity'        => ! empty($data['venue_capacity']) ? $data['venue_capacity'] : null,
1202
+			'VNU_url'             => ! empty($data['venue_url']) ? $data['venue_url'] : null,
1203
+			'VNU_virtual_phone'   => ! empty($data['virtual_phone']) ? $data['virtual_phone'] : null,
1204
+			'VNU_virtual_url'     => ! empty($data['virtual_url']) ? $data['virtual_url'] : null,
1205
+			'VNU_enable_for_gmap' => isset($data['enable_for_gmap']) ? 1 : 0,
1206
+			'status'              => 'publish',
1207
+		];
1208
+		// if we've got the venue_id then we're just updating the existing venue so let's do that and then get out.
1209
+		if (! empty($venue_id)) {
1210
+			$update_where  = [$venue_model->primary_key_name() => $venue_id];
1211
+			$rows_affected = $venue_model->update($venue_array, [$update_where]);
1212
+			// we've gotta make sure that the venue is always attached to a revision..
1213
+			// add_relation_to should take care of making sure that the relation is already present.
1214
+			$evtobj->_add_relation_to($venue_id, 'Venue');
1215
+			return $rows_affected > 0;
1216
+		}
1217
+		// we insert the venue
1218
+		$venue_id = $venue_model->insert($venue_array);
1219
+		$evtobj->_add_relation_to($venue_id, 'Venue');
1220
+		return ! empty($venue_id);
1221
+		// when we have the ancestor come in it's already been handled by the revision save.
1222
+	}
1223
+
1224
+
1225
+	/**
1226
+	 * Handles saving everything related to Tickets (datetimes, tickets, prices)
1227
+	 *
1228
+	 * @param EE_Event $evtobj The Event object we're attaching data to
1229
+	 * @param array    $data   The request data from the form
1230
+	 * @return array
1231
+	 * @throws EE_Error
1232
+	 * @throws InvalidArgumentException
1233
+	 * @throws InvalidDataTypeException
1234
+	 * @throws InvalidInterfaceException
1235
+	 * @throws ReflectionException
1236
+	 * @throws Exception
1237
+	 */
1238
+	protected function _default_tickets_update(EE_Event $evtobj, $data)
1239
+	{
1240
+		if ($this->admin_config->useAdvancedEditor()) {
1241
+			return [];
1242
+		}
1243
+		$success               = true;
1244
+		$saved_dtt             = null;
1245
+		$saved_tickets         = [];
1246
+		$incoming_date_formats = ['Y-m-d', 'h:i a'];
1247
+		foreach ($data['edit_event_datetimes'] as $row => $dtt) {
1248
+			// trim all values to ensure any excess whitespace is removed.
1249
+			$dtt                = array_map('trim', $dtt);
1250
+			$dtt['DTT_EVT_end'] = isset($dtt['DTT_EVT_end']) && ! empty($dtt['DTT_EVT_end']) ? $dtt['DTT_EVT_end']
1251
+				: $dtt['DTT_EVT_start'];
1252
+			$datetime_values    = [
1253
+				'DTT_ID'        => ! empty($dtt['DTT_ID']) ? $dtt['DTT_ID'] : null,
1254
+				'DTT_EVT_start' => $dtt['DTT_EVT_start'],
1255
+				'DTT_EVT_end'   => $dtt['DTT_EVT_end'],
1256
+				'DTT_reg_limit' => empty($dtt['DTT_reg_limit']) ? EE_INF : $dtt['DTT_reg_limit'],
1257
+				'DTT_order'     => $row,
1258
+			];
1259
+			// if we have an id then let's get existing object first and then set the new values.
1260
+			//  Otherwise we instantiate a new object for save.
1261
+			if (! empty($dtt['DTT_ID'])) {
1262
+				$DTM = EE_Registry::instance()
1263
+								  ->load_model('Datetime', [$evtobj->get_timezone()])
1264
+								  ->get_one_by_ID($dtt['DTT_ID']);
1265
+				$DTM->set_date_format($incoming_date_formats[0]);
1266
+				$DTM->set_time_format($incoming_date_formats[1]);
1267
+				foreach ($datetime_values as $field => $value) {
1268
+					$DTM->set($field, $value);
1269
+				}
1270
+				// make sure the $dtt_id here is saved in case after the add_relation_to() the autosave replaces it.
1271
+				// We need to do this so we dont' TRASH the parent DTT.
1272
+				$saved_dtts[ $DTM->ID() ] = $DTM;
1273
+			} else {
1274
+				$DTM = EE_Registry::instance()->load_class(
1275
+					'Datetime',
1276
+					[$datetime_values, $evtobj->get_timezone(), $incoming_date_formats],
1277
+					false,
1278
+					false
1279
+				);
1280
+				foreach ($datetime_values as $field => $value) {
1281
+					$DTM->set($field, $value);
1282
+				}
1283
+			}
1284
+			$DTM->save();
1285
+			$DTT = $evtobj->_add_relation_to($DTM, 'Datetime');
1286
+			// load DTT helper
1287
+			// before going any further make sure our dates are setup correctly
1288
+			// so that the end date is always equal or greater than the start date.
1289
+			if ($DTT->get_raw('DTT_EVT_start') > $DTT->get_raw('DTT_EVT_end')) {
1290
+				$DTT->set('DTT_EVT_end', $DTT->get('DTT_EVT_start'));
1291
+				$DTT = EEH_DTT_Helper::date_time_add($DTT, 'DTT_EVT_end', 'days');
1292
+				$DTT->save();
1293
+			}
1294
+			// now we got to make sure we add the new DTT_ID to the $saved_dtts array
1295
+			//  because it is possible there was a new one created for the autosave.
1296
+			$saved_dtt = $DTT;
1297
+			$success   = ! $success ? $success : $DTT;
1298
+			// if ANY of these updates fail then we want the appropriate global error message.
1299
+			// //todo this is actually sucky we need a better error message but this is what it is for now.
1300
+		}
1301
+		// no dtts get deleted so we don't do any of that logic here.
1302
+		// update tickets next
1303
+		$old_tickets = isset($data['ticket_IDs']) ? explode(',', $data['ticket_IDs']) : [];
1304
+		foreach ($data['edit_tickets'] as $row => $tkt) {
1305
+			$incoming_date_formats = ['Y-m-d', 'h:i a'];
1306
+			$update_prices         = false;
1307
+			$ticket_price          = isset($data['edit_prices'][ $row ][1]['PRC_amount'])
1308
+				? $data['edit_prices'][ $row ][1]['PRC_amount']
1309
+				: 0;
1310
+			// trim inputs to ensure any excess whitespace is removed.
1311
+			$tkt = array_map('trim', $tkt);
1312
+			if (empty($tkt['TKT_start_date'])) {
1313
+				// let's use now in the set timezone.
1314
+				$now                   = new DateTime('now', new DateTimeZone($evtobj->get_timezone()));
1315
+				$tkt['TKT_start_date'] = $now->format($incoming_date_formats[0] . ' ' . $incoming_date_formats[1]);
1316
+			}
1317
+			if (empty($tkt['TKT_end_date'])) {
1318
+				// use the start date of the first datetime
1319
+				$dtt                 = $evtobj->first_datetime();
1320
+				$tkt['TKT_end_date'] = $dtt->start_date_and_time(
1321
+					$incoming_date_formats[0],
1322
+					$incoming_date_formats[1]
1323
+				);
1324
+			}
1325
+			$TKT_values = [
1326
+				'TKT_ID'          => ! empty($tkt['TKT_ID']) ? $tkt['TKT_ID'] : null,
1327
+				'TTM_ID'          => ! empty($tkt['TTM_ID']) ? $tkt['TTM_ID'] : 0,
1328
+				'TKT_name'        => ! empty($tkt['TKT_name']) ? $tkt['TKT_name'] : '',
1329
+				'TKT_description' => ! empty($tkt['TKT_description']) ? $tkt['TKT_description'] : '',
1330
+				'TKT_start_date'  => $tkt['TKT_start_date'],
1331
+				'TKT_end_date'    => $tkt['TKT_end_date'],
1332
+				'TKT_qty'         => ! isset($tkt['TKT_qty']) || $tkt['TKT_qty'] === '' ? EE_INF : $tkt['TKT_qty'],
1333
+				'TKT_uses'        => ! isset($tkt['TKT_uses']) || $tkt['TKT_uses'] === '' ? EE_INF : $tkt['TKT_uses'],
1334
+				'TKT_min'         => empty($tkt['TKT_min']) ? 0 : $tkt['TKT_min'],
1335
+				'TKT_max'         => empty($tkt['TKT_max']) ? EE_INF : $tkt['TKT_max'],
1336
+				'TKT_row'         => $row,
1337
+				'TKT_order'       => isset($tkt['TKT_order']) ? $tkt['TKT_order'] : $row,
1338
+				'TKT_price'       => $ticket_price,
1339
+			];
1340
+			// if this is a default TKT, then we need to set the TKT_ID to 0 and update accordingly,
1341
+			// which means in turn that the prices will become new prices as well.
1342
+			if (isset($tkt['TKT_is_default']) && $tkt['TKT_is_default']) {
1343
+				$TKT_values['TKT_ID']         = 0;
1344
+				$TKT_values['TKT_is_default'] = 0;
1345
+				$TKT_values['TKT_price']      = $ticket_price;
1346
+				$update_prices                = true;
1347
+			}
1348
+			// if we have a TKT_ID then we need to get that existing TKT_obj and update it
1349
+			// we actually do our saves a head of doing any add_relations to because its entirely possible
1350
+			// that this ticket didn't removed or added to any datetime in the session but DID have it's items modified.
1351
+			// keep in mind that if the TKT has been sold (and we have changed pricing information),
1352
+			// then we won't be updating the tkt but instead a new tkt will be created and the old one archived.
1353
+			if (! empty($tkt['TKT_ID'])) {
1354
+				$TKT = EE_Registry::instance()
1355
+								  ->load_model('Ticket', [$evtobj->get_timezone()])
1356
+								  ->get_one_by_ID($tkt['TKT_ID']);
1357
+				if ($TKT instanceof EE_Ticket) {
1358
+					$ticket_sold = $TKT->count_related(
1359
+						'Registration',
1360
+						[
1361
+							[
1362
+								'STS_ID' => [
1363
+									'NOT IN',
1364
+									[EEM_Registration::status_id_incomplete],
1365
+								],
1366
+							],
1367
+						]
1368
+					) > 0;
1369
+					// let's just check the total price for the existing ticket and determine if it matches the new
1370
+					// total price.  if they are different then we create a new ticket (if tickets sold)
1371
+					// if they aren't different then we go ahead and modify existing ticket.
1372
+					$create_new_TKT = $ticket_sold
1373
+									  && ! $TKT->deleted()
1374
+									  && EEH_Money::compare_floats(
1375
+										  $ticket_price,
1376
+										  $TKT->get('TKT_price'),
1377
+										  '!=='
1378
+									  );
1379
+					$TKT->set_date_format($incoming_date_formats[0]);
1380
+					$TKT->set_time_format($incoming_date_formats[1]);
1381
+					// set new values
1382
+					foreach ($TKT_values as $field => $value) {
1383
+						if ($field === 'TKT_qty') {
1384
+							$TKT->set_qty($value);
1385
+						} else {
1386
+							$TKT->set($field, $value);
1387
+						}
1388
+					}
1389
+					// if $create_new_TKT is false then we can safely update the existing ticket.
1390
+					//  Otherwise we have to create a new ticket.
1391
+					if ($create_new_TKT) {
1392
+						// archive the old ticket first
1393
+						$TKT->set('TKT_deleted', 1);
1394
+						$TKT->save();
1395
+						// make sure this ticket is still recorded in our saved_tkts
1396
+						// so we don't run it through the regular trash routine.
1397
+						$saved_tickets[ $TKT->ID() ] = $TKT;
1398
+						// create new ticket that's a copy of the existing except a new id of course
1399
+						// (and not archived) AND has the new TKT_price associated with it.
1400
+						$TKT = clone $TKT;
1401
+						$TKT->set('TKT_ID', 0);
1402
+						$TKT->set('TKT_deleted', 0);
1403
+						$TKT->set('TKT_price', $ticket_price);
1404
+						$TKT->set('TKT_sold', 0);
1405
+						// now we need to make sure that $new prices are created as well and attached to new ticket.
1406
+						$update_prices = true;
1407
+					}
1408
+					// make sure price is set if it hasn't been already
1409
+					$TKT->set('TKT_price', $ticket_price);
1410
+				}
1411
+			} else {
1412
+				// no TKT_id so a new TKT
1413
+				$TKT_values['TKT_price'] = $ticket_price;
1414
+				$TKT                     = EE_Registry::instance()->load_class('Ticket', [$TKT_values], false, false);
1415
+				if ($TKT instanceof EE_Ticket) {
1416
+					// need to reset values to properly account for the date formats
1417
+					$TKT->set_date_format($incoming_date_formats[0]);
1418
+					$TKT->set_time_format($incoming_date_formats[1]);
1419
+					$TKT->set_timezone($evtobj->get_timezone());
1420
+					// set new values
1421
+					foreach ($TKT_values as $field => $value) {
1422
+						if ($field === 'TKT_qty') {
1423
+							$TKT->set_qty($value);
1424
+						} else {
1425
+							$TKT->set($field, $value);
1426
+						}
1427
+					}
1428
+					$update_prices = true;
1429
+				}
1430
+			}
1431
+			// cap ticket qty by datetime reg limits
1432
+			$TKT->set_qty(min($TKT->qty(), $TKT->qty('reg_limit')));
1433
+			// update ticket.
1434
+			$TKT->save();
1435
+			// before going any further make sure our dates are setup correctly
1436
+			// so that the end date is always equal or greater than the start date.
1437
+			if ($TKT->get_raw('TKT_start_date') > $TKT->get_raw('TKT_end_date')) {
1438
+				$TKT->set('TKT_end_date', $TKT->get('TKT_start_date'));
1439
+				$TKT = EEH_DTT_Helper::date_time_add($TKT, 'TKT_end_date', 'days');
1440
+				$TKT->save();
1441
+			}
1442
+			// initially let's add the ticket to the dtt
1443
+			$saved_dtt->_add_relation_to($TKT, 'Ticket');
1444
+			$saved_tickets[ $TKT->ID() ] = $TKT;
1445
+			// add prices to ticket
1446
+			$this->_add_prices_to_ticket($data['edit_prices'][ $row ], $TKT, $update_prices);
1447
+		}
1448
+		// however now we need to handle permanently deleting tickets via the ui.
1449
+		//  Keep in mind that the ui does not allow deleting/archiving tickets that have ticket sold.
1450
+		//  However, it does allow for deleting tickets that have no tickets sold,
1451
+		// in which case we want to get rid of permanently because there is no need to save in db.
1452
+		$old_tickets     = isset($old_tickets[0]) && $old_tickets[0] === '' ? [] : $old_tickets;
1453
+		$tickets_removed = array_diff($old_tickets, array_keys($saved_tickets));
1454
+		foreach ($tickets_removed as $id) {
1455
+			$id = absint($id);
1456
+			// get the ticket for this id
1457
+			$tkt_to_remove = EE_Registry::instance()->load_model('Ticket')->get_one_by_ID($id);
1458
+			// need to get all the related datetimes on this ticket and remove from every single one of them
1459
+			// (remember this process can ONLY kick off if there are NO tkts_sold)
1460
+			$dtts = $tkt_to_remove->get_many_related('Datetime');
1461
+			foreach ($dtts as $dtt) {
1462
+				$tkt_to_remove->_remove_relation_to($dtt, 'Datetime');
1463
+			}
1464
+			// need to do the same for prices (except these prices can also be deleted because again,
1465
+			// tickets can only be trashed if they don't have any TKTs sold (otherwise they are just archived))
1466
+			$tkt_to_remove->delete_related_permanently('Price');
1467
+			// finally let's delete this ticket
1468
+			// (which should not be blocked at this point b/c we've removed all our relationships)
1469
+			$tkt_to_remove->delete_permanently();
1470
+		}
1471
+		return [$saved_dtt, $saved_tickets];
1472
+	}
1473
+
1474
+
1475
+	/**
1476
+	 * This attaches a list of given prices to a ticket.
1477
+	 * Note we dont' have to worry about ever removing relationships (or archiving prices)
1478
+	 * because if there is a change in price information on a ticket, a new ticket is created anyways
1479
+	 * so the archived ticket will retain the old price info and prices are automatically "archived" via the ticket.
1480
+	 *
1481
+	 * @access  private
1482
+	 * @param array     $prices     Array of prices from the form.
1483
+	 * @param EE_Ticket $ticket     EE_Ticket object that prices are being attached to.
1484
+	 * @param bool      $new_prices Whether attach existing incoming prices or create new ones.
1485
+	 * @return  void
1486
+	 * @throws EE_Error
1487
+	 * @throws InvalidArgumentException
1488
+	 * @throws InvalidDataTypeException
1489
+	 * @throws InvalidInterfaceException
1490
+	 * @throws ReflectionException
1491
+	 */
1492
+	private function _add_prices_to_ticket($prices, EE_Ticket $ticket, $new_prices = false)
1493
+	{
1494
+		foreach ($prices as $row => $prc) {
1495
+			$PRC_values = [
1496
+				'PRC_ID'         => ! empty($prc['PRC_ID']) ? $prc['PRC_ID'] : null,
1497
+				'PRT_ID'         => ! empty($prc['PRT_ID']) ? $prc['PRT_ID'] : null,
1498
+				'PRC_amount'     => ! empty($prc['PRC_amount']) ? $prc['PRC_amount'] : 0,
1499
+				'PRC_name'       => ! empty($prc['PRC_name']) ? $prc['PRC_name'] : '',
1500
+				'PRC_desc'       => ! empty($prc['PRC_desc']) ? $prc['PRC_desc'] : '',
1501
+				'PRC_is_default' => 0, // make sure prices are NOT set as default from this context
1502
+				'PRC_order'      => $row,
1503
+			];
1504
+			if ($new_prices || empty($PRC_values['PRC_ID'])) {
1505
+				$PRC_values['PRC_ID'] = 0;
1506
+				$PRC                  = EE_Registry::instance()->load_class('Price', [$PRC_values], false, false);
1507
+			} else {
1508
+				$PRC = EE_Registry::instance()->load_model('Price')->get_one_by_ID($prc['PRC_ID']);
1509
+				// update this price with new values
1510
+				foreach ($PRC_values as $field => $newprc) {
1511
+					$PRC->set($field, $newprc);
1512
+				}
1513
+				$PRC->save();
1514
+			}
1515
+			$ticket->_add_relation_to($PRC, 'Price');
1516
+		}
1517
+	}
1518
+
1519
+
1520
+	/**
1521
+	 * Add in our autosave ajax handlers
1522
+	 *
1523
+	 */
1524
+	protected function _ee_autosave_create_new()
1525
+	{
1526
+	}
1527
+
1528
+
1529
+	/**
1530
+	 * More autosave handlers.
1531
+	 */
1532
+	protected function _ee_autosave_edit()
1533
+	{
1534
+	}
1535
+
1536
+
1537
+	/**
1538
+	 *    _generate_publish_box_extra_content
1539
+	 *
1540
+	 * @throws DomainException
1541
+	 * @throws EE_Error
1542
+	 * @throws InvalidArgumentException
1543
+	 * @throws InvalidDataTypeException
1544
+	 * @throws InvalidInterfaceException
1545
+	 * @throws ReflectionException
1546
+	 */
1547
+	private function _generate_publish_box_extra_content()
1548
+	{
1549
+		// load formatter helper
1550
+		// args for getting related registrations
1551
+		$approved_query_args        = [
1552
+			[
1553
+				'REG_deleted' => 0,
1554
+				'STS_ID'      => EEM_Registration::status_id_approved,
1555
+			],
1556
+		];
1557
+		$not_approved_query_args    = [
1558
+			[
1559
+				'REG_deleted' => 0,
1560
+				'STS_ID'      => EEM_Registration::status_id_not_approved,
1561
+			],
1562
+		];
1563
+		$pending_payment_query_args = [
1564
+			[
1565
+				'REG_deleted' => 0,
1566
+				'STS_ID'      => EEM_Registration::status_id_pending_payment,
1567
+			],
1568
+		];
1569
+		// publish box
1570
+		$publish_box_extra_args = [
1571
+			'view_approved_reg_url'        => add_query_arg(
1572
+				[
1573
+					'action'      => 'default',
1574
+					'event_id'    => $this->_cpt_model_obj->ID(),
1575
+					'_reg_status' => EEM_Registration::status_id_approved,
1576
+				],
1577
+				REG_ADMIN_URL
1578
+			),
1579
+			'view_not_approved_reg_url'    => add_query_arg(
1580
+				[
1581
+					'action'      => 'default',
1582
+					'event_id'    => $this->_cpt_model_obj->ID(),
1583
+					'_reg_status' => EEM_Registration::status_id_not_approved,
1584
+				],
1585
+				REG_ADMIN_URL
1586
+			),
1587
+			'view_pending_payment_reg_url' => add_query_arg(
1588
+				[
1589
+					'action'      => 'default',
1590
+					'event_id'    => $this->_cpt_model_obj->ID(),
1591
+					'_reg_status' => EEM_Registration::status_id_pending_payment,
1592
+				],
1593
+				REG_ADMIN_URL
1594
+			),
1595
+			'approved_regs'                => $this->_cpt_model_obj->count_related(
1596
+				'Registration',
1597
+				$approved_query_args
1598
+			),
1599
+			'not_approved_regs'            => $this->_cpt_model_obj->count_related(
1600
+				'Registration',
1601
+				$not_approved_query_args
1602
+			),
1603
+			'pending_payment_regs'         => $this->_cpt_model_obj->count_related(
1604
+				'Registration',
1605
+				$pending_payment_query_args
1606
+			),
1607
+			'misc_pub_section_class'       => apply_filters(
1608
+				'FHEE_Events_Admin_Page___generate_publish_box_extra_content__misc_pub_section_class',
1609
+				'misc-pub-section'
1610
+			),
1611
+		];
1612
+		ob_start();
1613
+		do_action(
1614
+			'AHEE__Events_Admin_Page___generate_publish_box_extra_content__event_editor_overview_add',
1615
+			$this->_cpt_model_obj
1616
+		);
1617
+		$publish_box_extra_args['event_editor_overview_add'] = ob_get_clean();
1618
+		// load template
1619
+		EEH_Template::display_template(
1620
+			EVENTS_TEMPLATE_PATH . 'event_publish_box_extras.template.php',
1621
+			$publish_box_extra_args
1622
+		);
1623
+	}
1624
+
1625
+
1626
+	/**
1627
+	 * @return EE_Event
1628
+	 */
1629
+	public function get_event_object()
1630
+	{
1631
+		return $this->_cpt_model_obj;
1632
+	}
1633
+
1634
+
1635
+
1636
+
1637
+	/** METABOXES * */
1638
+	/**
1639
+	 * _register_event_editor_meta_boxes
1640
+	 * add all metaboxes related to the event_editor
1641
+	 *
1642
+	 * @return void
1643
+	 * @throws EE_Error
1644
+	 * @throws InvalidArgumentException
1645
+	 * @throws InvalidDataTypeException
1646
+	 * @throws InvalidInterfaceException
1647
+	 * @throws ReflectionException
1648
+	 */
1649
+	protected function _register_event_editor_meta_boxes()
1650
+	{
1651
+		$this->verify_cpt_object();
1652
+		$use_advanced_editor = $this->admin_config->useAdvancedEditor();
1653
+		/** @var FeatureFlags $flags */
1654
+		$flags = $this->loader->getShared('EventEspresso\core\domain\services\capabilities\FeatureFlags');
1655
+		// check if the new EDTR reg options meta box is being used, and if so, don't load the legacy version
1656
+		if (! $use_advanced_editor || ! $flags->featureAllowed('use_reg_options_meta_box')) {
1657
+			add_meta_box(
1658
+				'espresso_event_editor_event_options',
1659
+				esc_html__('Event Registration Options', 'event_espresso'),
1660
+				[$this, 'registration_options_meta_box'],
1661
+				$this->page_slug,
1662
+				'side'
1663
+			);
1664
+		}
1665
+		if (! $use_advanced_editor) {
1666
+			add_meta_box(
1667
+				'espresso_event_editor_tickets',
1668
+				esc_html__('Event Datetime & Ticket', 'event_espresso'),
1669
+				[$this, 'ticket_metabox'],
1670
+				$this->page_slug,
1671
+				'normal',
1672
+				'high'
1673
+			);
1674
+		} else {
1675
+			if ($flags->featureAllowed('use_reg_options_meta_box')) {
1676
+				add_action(
1677
+					'add_meta_boxes_espresso_events',
1678
+					function () {
1679
+						global $current_screen;
1680
+						remove_meta_box('authordiv', $current_screen, 'normal');
1681
+					},
1682
+					99
1683
+				);
1684
+			}
1685
+		}
1686
+		// NOTE: if you're looking for other metaboxes in here,
1687
+		// where a metabox has a related management page in the admin
1688
+		// you will find it setup in the related management page's "_Hooks" file.
1689
+		// i.e. messages metabox is found in "espresso_events_Messages_Hooks.class.php".
1690
+	}
1691
+
1692
+
1693
+	/**
1694
+	 * @throws DomainException
1695
+	 * @throws EE_Error
1696
+	 * @throws InvalidArgumentException
1697
+	 * @throws InvalidDataTypeException
1698
+	 * @throws InvalidInterfaceException
1699
+	 * @throws ReflectionException
1700
+	 */
1701
+	public function ticket_metabox()
1702
+	{
1703
+		$existing_datetime_ids = $existing_ticket_ids = [];
1704
+		// defaults for template args
1705
+		$template_args = [
1706
+			'existing_datetime_ids'    => '',
1707
+			'event_datetime_help_link' => '',
1708
+			'ticket_options_help_link' => '',
1709
+			'time'                     => null,
1710
+			'ticket_rows'              => '',
1711
+			'existing_ticket_ids'      => '',
1712
+			'total_ticket_rows'        => 1,
1713
+			'ticket_js_structure'      => '',
1714
+			'trash_icon'               => 'ee-lock-icon',
1715
+			'disabled'                 => '',
1716
+		];
1717
+		$event_id      = is_object($this->_cpt_model_obj) ? $this->_cpt_model_obj->ID() : null;
1718
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1719
+		/**
1720
+		 * 1. Start with retrieving Datetimes
1721
+		 * 2. Fore each datetime get related tickets
1722
+		 * 3. For each ticket get related prices
1723
+		 */
1724
+		$times = EE_Registry::instance()->load_model('Datetime')->get_all_event_dates($event_id);
1725
+		/** @type EE_Datetime $first_datetime */
1726
+		$first_datetime = reset($times);
1727
+		// do we get related tickets?
1728
+		if (
1729
+			$first_datetime instanceof EE_Datetime
1730
+			&& $first_datetime->ID() !== 0
1731
+		) {
1732
+			$existing_datetime_ids[] = $first_datetime->get('DTT_ID');
1733
+			$template_args['time']   = $first_datetime;
1734
+			$related_tickets         = $first_datetime->tickets(
1735
+				[
1736
+					['OR' => ['TKT_deleted' => 1, 'TKT_deleted*' => 0]],
1737
+					'default_where_conditions' => 'none',
1738
+				]
1739
+			);
1740
+			if (! empty($related_tickets)) {
1741
+				$template_args['total_ticket_rows'] = count($related_tickets);
1742
+				$row                                = 0;
1743
+				foreach ($related_tickets as $ticket) {
1744
+					$existing_ticket_ids[]        = $ticket->get('TKT_ID');
1745
+					$template_args['ticket_rows'] .= $this->_get_ticket_row($ticket, false, $row);
1746
+					$row++;
1747
+				}
1748
+			} else {
1749
+				$template_args['total_ticket_rows'] = 1;
1750
+				/** @type EE_Ticket $ticket */
1751
+				$ticket                       = EE_Registry::instance()->load_model('Ticket')->create_default_object();
1752
+				$template_args['ticket_rows'] .= $this->_get_ticket_row($ticket);
1753
+			}
1754
+		} else {
1755
+			$template_args['time'] = $times[0];
1756
+			/** @type EE_Ticket $ticket */
1757
+			$ticket                       = EE_Registry::instance()->load_model('Ticket')->get_all_default_tickets();
1758
+			$template_args['ticket_rows'] .= $this->_get_ticket_row($ticket[1]);
1759
+			// NOTE: we're just sending the first default row
1760
+			// (decaf can't manage default tickets so this should be sufficient);
1761
+		}
1762
+		$template_args['event_datetime_help_link'] = $this->_get_help_tab_link(
1763
+			'event_editor_event_datetimes_help_tab'
1764
+		);
1765
+		$template_args['ticket_options_help_link'] = $this->_get_help_tab_link('ticket_options_info');
1766
+		$template_args['existing_datetime_ids']    = implode(',', $existing_datetime_ids);
1767
+		$template_args['existing_ticket_ids']      = implode(',', $existing_ticket_ids);
1768
+		$template_args['ticket_js_structure']      = $this->_get_ticket_row(
1769
+			EE_Registry::instance()->load_model('Ticket')->create_default_object(),
1770
+			true
1771
+		);
1772
+		$template                                  = apply_filters(
1773
+			'FHEE__Events_Admin_Page__ticket_metabox__template',
1774
+			EVENTS_TEMPLATE_PATH . 'event_tickets_metabox_main.template.php'
1775
+		);
1776
+		EEH_Template::display_template($template, $template_args);
1777
+	}
1778
+
1779
+
1780
+	/**
1781
+	 * Setup an individual ticket form for the decaf event editor page
1782
+	 *
1783
+	 * @access private
1784
+	 * @param EE_Ticket $ticket   the ticket object
1785
+	 * @param boolean   $skeleton whether we're generating a skeleton for js manipulation
1786
+	 * @param int       $row
1787
+	 * @return string generated html for the ticket row.
1788
+	 * @throws DomainException
1789
+	 * @throws EE_Error
1790
+	 * @throws InvalidArgumentException
1791
+	 * @throws InvalidDataTypeException
1792
+	 * @throws InvalidInterfaceException
1793
+	 * @throws ReflectionException
1794
+	 */
1795
+	private function _get_ticket_row($ticket, $skeleton = false, $row = 0)
1796
+	{
1797
+		$template_args = [
1798
+			'tkt_status_class'    => ' tkt-status-' . $ticket->ticket_status(),
1799
+			'tkt_archive_class'   => $ticket->ticket_status() === EE_Ticket::archived && ! $skeleton ? ' tkt-archived'
1800
+				: '',
1801
+			'ticketrow'           => $skeleton ? 'TICKETNUM' : $row,
1802
+			'TKT_ID'              => $ticket->get('TKT_ID'),
1803
+			'TKT_name'            => $ticket->get('TKT_name'),
1804
+			'TKT_start_date'      => $skeleton ? '' : $ticket->get_date('TKT_start_date', 'Y-m-d h:i a'),
1805
+			'TKT_end_date'        => $skeleton ? '' : $ticket->get_date('TKT_end_date', 'Y-m-d h:i a'),
1806
+			'TKT_is_default'      => $ticket->get('TKT_is_default'),
1807
+			'TKT_qty'             => $ticket->get_pretty('TKT_qty', 'input'),
1808
+			'edit_ticketrow_name' => $skeleton ? 'TICKETNAMEATTR' : 'edit_tickets',
1809
+			'TKT_sold'            => $skeleton ? 0 : $ticket->get('TKT_sold'),
1810
+			'trash_icon'          => ($skeleton || (! empty($ticket) && ! $ticket->get('TKT_deleted')))
1811
+									 && (! empty($ticket) && $ticket->get('TKT_sold') === 0)
1812
+				? 'trash-icon dashicons dashicons-post-trash clickable' : 'ee-lock-icon',
1813
+			'disabled'            => $skeleton || (! empty($ticket) && ! $ticket->get('TKT_deleted')) ? ''
1814
+				: ' disabled=disabled',
1815
+		];
1816
+		$price         = $ticket->ID() !== 0
1817
+			? $ticket->get_first_related('Price', ['default_where_conditions' => 'none'])
1818
+			: EE_Registry::instance()->load_model('Price')->create_default_object();
1819
+		$price_args    = [
1820
+			'price_currency_symbol' => EE_Registry::instance()->CFG->currency->sign,
1821
+			'PRC_amount'            => $price->get('PRC_amount'),
1822
+			'PRT_ID'                => $price->get('PRT_ID'),
1823
+			'PRC_ID'                => $price->get('PRC_ID'),
1824
+			'PRC_is_default'        => $price->get('PRC_is_default'),
1825
+		];
1826
+		// make sure we have default start and end dates if skeleton
1827
+		// handle rows that should NOT be empty
1828
+		if (empty($template_args['TKT_start_date'])) {
1829
+			// if empty then the start date will be now.
1830
+			$template_args['TKT_start_date'] = date('Y-m-d h:i a', current_time('timestamp'));
1831
+		}
1832
+		if (empty($template_args['TKT_end_date'])) {
1833
+			// get the earliest datetime (if present);
1834
+			$earliest_dtt = $this->_cpt_model_obj->ID() > 0
1835
+				? $this->_cpt_model_obj->get_first_related(
1836
+					'Datetime',
1837
+					['order_by' => ['DTT_EVT_start' => 'ASC']]
1838
+				)
1839
+				: null;
1840
+			if (! empty($earliest_dtt)) {
1841
+				$template_args['TKT_end_date'] = $earliest_dtt->get_datetime('DTT_EVT_start', 'Y-m-d', 'h:i a');
1842
+			} else {
1843
+				$template_args['TKT_end_date'] = date(
1844
+					'Y-m-d h:i a',
1845
+					mktime(0, 0, 0, date('m'), date('d') + 7, date('Y'))
1846
+				);
1847
+			}
1848
+		}
1849
+		$template_args = array_merge($template_args, $price_args);
1850
+		$template      = apply_filters(
1851
+			'FHEE__Events_Admin_Page__get_ticket_row__template',
1852
+			EVENTS_TEMPLATE_PATH . 'event_tickets_metabox_ticket_row.template.php',
1853
+			$ticket
1854
+		);
1855
+		return EEH_Template::display_template($template, $template_args, true);
1856
+	}
1857
+
1858
+
1859
+	/**
1860
+	 * @throws DomainException
1861
+	 * @throws EE_Error
1862
+	 */
1863
+	public function registration_options_meta_box()
1864
+	{
1865
+		$yes_no_values             = [
1866
+			['id' => true, 'text' => esc_html__('Yes', 'event_espresso')],
1867
+			['id' => false, 'text' => esc_html__('No', 'event_espresso')],
1868
+		];
1869
+		$default_reg_status_values = EEM_Registration::reg_status_array(
1870
+			[
1871
+				EEM_Registration::status_id_cancelled,
1872
+				EEM_Registration::status_id_declined,
1873
+				EEM_Registration::status_id_incomplete,
1874
+			],
1875
+			true
1876
+		);
1877
+		// $template_args['is_active_select'] = EEH_Form_Fields::select_input('is_active', $yes_no_values, $this->_cpt_model_obj->is_active());
1878
+		$template_args['_event']                          = $this->_cpt_model_obj;
1879
+		$template_args['active_status']                   = $this->_cpt_model_obj->pretty_active_status(false);
1880
+		$template_args['additional_limit']                = $this->_cpt_model_obj->additional_limit();
1881
+		$template_args['default_registration_status']     = EEH_Form_Fields::select_input(
1882
+			'default_reg_status',
1883
+			$default_reg_status_values,
1884
+			$this->_cpt_model_obj->default_registration_status()
1885
+		);
1886
+		$template_args['display_description']             = EEH_Form_Fields::select_input(
1887
+			'display_desc',
1888
+			$yes_no_values,
1889
+			$this->_cpt_model_obj->display_description()
1890
+		);
1891
+		$template_args['display_ticket_selector']         = EEH_Form_Fields::select_input(
1892
+			'display_ticket_selector',
1893
+			$yes_no_values,
1894
+			$this->_cpt_model_obj->display_ticket_selector(),
1895
+			'',
1896
+			'',
1897
+			false
1898
+		);
1899
+		$template_args['additional_registration_options'] = apply_filters(
1900
+			'FHEE__Events_Admin_Page__registration_options_meta_box__additional_registration_options',
1901
+			'',
1902
+			$template_args,
1903
+			$yes_no_values,
1904
+			$default_reg_status_values
1905
+		);
1906
+		EEH_Template::display_template(
1907
+			EVENTS_TEMPLATE_PATH . 'event_registration_options.template.php',
1908
+			$template_args
1909
+		);
1910
+	}
1911
+
1912
+
1913
+	/**
1914
+	 * _get_events()
1915
+	 * This method simply returns all the events (for the given _view and paging)
1916
+	 *
1917
+	 * @access public
1918
+	 * @param int  $per_page     count of items per page (20 default);
1919
+	 * @param int  $current_page what is the current page being viewed.
1920
+	 * @param bool $count        if TRUE then we just return a count of ALL events matching the given _view.
1921
+	 *                           If FALSE then we return an array of event objects
1922
+	 *                           that match the given _view and paging parameters.
1923
+	 * @return array|int an array of event objects.
1924
+	 * @throws EE_Error
1925
+	 * @throws InvalidArgumentException
1926
+	 * @throws InvalidDataTypeException
1927
+	 * @throws InvalidInterfaceException
1928
+	 * @throws ReflectionException
1929
+	 * @throws Exception
1930
+	 * @throws Exception
1931
+	 * @throws Exception
1932
+	 */
1933
+	public function get_events($per_page = 10, $current_page = 1, $count = false)
1934
+	{
1935
+		$EEME    = $this->_event_model();
1936
+		$offset  = ($current_page - 1) * $per_page;
1937
+		$limit   = $count ? null : $offset . ',' . $per_page;
1938
+		$orderby = isset($this->_req_data['orderby']) ? $this->_req_data['orderby'] : 'EVT_ID';
1939
+		$order   = isset($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC';
1940
+		if (isset($this->_req_data['month_range'])) {
1941
+			$pieces = explode(' ', $this->_req_data['month_range'], 3);
1942
+			// simulate the FIRST day of the month, that fixes issues for months like February
1943
+			// where PHP doesn't know what to assume for date.
1944
+			// @see https://events.codebasehq.com/projects/event-espresso/tickets/10437
1945
+			$month_r = ! empty($pieces[0]) ? date('m', EEH_DTT_Helper::first_of_month_timestamp($pieces[0])) : '';
1946
+			$year_r  = ! empty($pieces[1]) ? $pieces[1] : '';
1947
+		}
1948
+		$where  = [];
1949
+		$status = isset($this->_req_data['status']) ? $this->_req_data['status'] : null;
1950
+		// determine what post_status our condition will have for the query.
1951
+		switch ($status) {
1952
+			case 'month':
1953
+			case 'today':
1954
+			case null:
1955
+			case 'all':
1956
+				break;
1957
+			case 'draft':
1958
+				$where['status'] = ['IN', ['draft', 'auto-draft']];
1959
+				break;
1960
+			default:
1961
+				$where['status'] = $status;
1962
+		}
1963
+		// categories?
1964
+		$category = isset($this->_req_data['EVT_CAT']) && $this->_req_data['EVT_CAT'] > 0
1965
+			? $this->_req_data['EVT_CAT'] : null;
1966
+		if (! empty($category)) {
1967
+			$where['Term_Taxonomy.taxonomy'] = EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY;
1968
+			$where['Term_Taxonomy.term_id']  = $category;
1969
+		}
1970
+		// date where conditions
1971
+		$start_formats = EEM_Datetime::instance()->get_formats_for('DTT_EVT_start');
1972
+		if (isset($this->_req_data['month_range']) && $this->_req_data['month_range'] !== '') {
1973
+			$DateTime = new DateTime(
1974
+				$year_r . '-' . $month_r . '-01 00:00:00',
1975
+				new DateTimeZone('UTC')
1976
+			);
1977
+			$start    = $DateTime->getTimestamp();
1978
+			// set the datetime to be the end of the month
1979
+			$DateTime->setDate(
1980
+				$year_r,
1981
+				$month_r,
1982
+				$DateTime->format('t')
1983
+			)->setTime(23, 59, 59);
1984
+			$end                             = $DateTime->getTimestamp();
1985
+			$where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]];
1986
+		} elseif (isset($this->_req_data['status']) && $this->_req_data['status'] === 'today') {
1987
+			$DateTime                        =
1988
+				new DateTime('now', new DateTimeZone(EEM_Event::instance()->get_timezone()));
1989
+			$start                           = $DateTime->setTime(0, 0, 0)->format(implode(' ', $start_formats));
1990
+			$end                             = $DateTime->setTime(23, 59, 59)->format(implode(' ', $start_formats));
1991
+			$where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]];
1992
+		} elseif (isset($this->_req_data['status']) && $this->_req_data['status'] === 'month') {
1993
+			$now                             = date('Y-m-01');
1994
+			$DateTime                        =
1995
+				new DateTime($now, new DateTimeZone(EEM_Event::instance()->get_timezone()));
1996
+			$start                           = $DateTime->setTime(0, 0, 0)->format(implode(' ', $start_formats));
1997
+			$end                             = $DateTime->setDate(date('Y'), date('m'), $DateTime->format('t'))
1998
+														->setTime(23, 59, 59)
1999
+														->format(implode(' ', $start_formats));
2000
+			$where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]];
2001
+		}
2002
+		if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_events', 'get_events')) {
2003
+			$where['EVT_wp_user'] = get_current_user_id();
2004
+		} elseif (
2005
+			! isset($where['status'])
2006
+			&& ! EE_Registry::instance()->CAP->current_user_can('ee_read_private_events', 'get_events')
2007
+		) {
2008
+			$where['OR'] = [
2009
+				'status*restrict_private' => ['!=', 'private'],
2010
+				'AND'                     => [
2011
+					'status*inclusive' => ['=', 'private'],
2012
+					'EVT_wp_user'      => get_current_user_id(),
2013
+				],
2014
+			];
2015
+		}
2016
+
2017
+		if (
2018
+			isset($this->_req_data['EVT_wp_user'])
2019
+			&& (int) $this->_req_data['EVT_wp_user'] !== (int) get_current_user_id()
2020
+			&& EE_Registry::instance()->CAP->current_user_can('ee_read_others_events', 'get_events')
2021
+		) {
2022
+			$where['EVT_wp_user'] = $this->_req_data['EVT_wp_user'];
2023
+		}
2024
+		// search query handling
2025
+		if (isset($this->_req_data['s'])) {
2026
+			$search_string = '%' . $this->_req_data['s'] . '%';
2027
+			$where['OR']   = [
2028
+				'EVT_name'       => ['LIKE', $search_string],
2029
+				'EVT_desc'       => ['LIKE', $search_string],
2030
+				'EVT_short_desc' => ['LIKE', $search_string],
2031
+			];
2032
+		}
2033
+		// filter events by venue.
2034
+		if (isset($this->_req_data['venue']) && ! empty($this->_req_data['venue'])) {
2035
+			$where['Venue.VNU_ID'] = absint($this->_req_data['venue']);
2036
+		}
2037
+		$where        = apply_filters('FHEE__Events_Admin_Page__get_events__where', $where, $this->_req_data);
2038
+		$query_params = apply_filters(
2039
+			'FHEE__Events_Admin_Page__get_events__query_params',
2040
+			[
2041
+				$where,
2042
+				'limit'    => $limit,
2043
+				'order_by' => $orderby,
2044
+				'order'    => $order,
2045
+				'group_by' => 'EVT_ID',
2046
+			],
2047
+			$this->_req_data
2048
+		);
2049
+
2050
+		// let's first check if we have special requests coming in.
2051
+		if (isset($this->_req_data['active_status'])) {
2052
+			switch ($this->_req_data['active_status']) {
2053
+				case 'upcoming':
2054
+					return $EEME->get_upcoming_events($query_params, $count);
2055
+				case 'expired':
2056
+					return $EEME->get_expired_events($query_params, $count);
2057
+				case 'active':
2058
+					return $EEME->get_active_events($query_params, $count);
2059
+				case 'inactive':
2060
+					return $EEME->get_inactive_events($query_params, $count);
2061
+			}
2062
+		}
2063
+
2064
+		return $count ? $EEME->count([$where], 'EVT_ID', true) : $EEME->get_all($query_params);
2065
+	}
2066
+
2067
+
2068
+	/**
2069
+	 * handling for WordPress CPT actions (trash, restore, delete)
2070
+	 *
2071
+	 * @param string $post_id
2072
+	 * @throws EE_Error
2073
+	 * @throws InvalidArgumentException
2074
+	 * @throws InvalidDataTypeException
2075
+	 * @throws InvalidInterfaceException
2076
+	 * @throws ReflectionException
2077
+	 */
2078
+	public function trash_cpt_item($post_id)
2079
+	{
2080
+		$this->_req_data['EVT_ID'] = $post_id;
2081
+		$this->_trash_or_restore_event('trash', false);
2082
+	}
2083
+
2084
+
2085
+	/**
2086
+	 * @param string $post_id
2087
+	 * @throws EE_Error
2088
+	 * @throws InvalidArgumentException
2089
+	 * @throws InvalidDataTypeException
2090
+	 * @throws InvalidInterfaceException
2091
+	 * @throws ReflectionException
2092
+	 */
2093
+	public function restore_cpt_item($post_id)
2094
+	{
2095
+		$this->_req_data['EVT_ID'] = $post_id;
2096
+		$this->_trash_or_restore_event('draft', false);
2097
+	}
2098
+
2099
+
2100
+	/**
2101
+	 * @param string $post_id
2102
+	 * @throws EE_Error
2103
+	 * @throws InvalidArgumentException
2104
+	 * @throws InvalidDataTypeException
2105
+	 * @throws InvalidInterfaceException
2106
+	 * @throws ReflectionException
2107
+	 */
2108
+	public function delete_cpt_item($post_id)
2109
+	{
2110
+		throw new EE_Error(
2111
+			esc_html__(
2112
+				'Please contact Event Espresso support with the details of the steps taken to produce this error.',
2113
+				'event_espresso'
2114
+			)
2115
+		);
2116
+		$this->_req_data['EVT_ID'] = $post_id;
2117
+		$this->_delete_event();
2118
+	}
2119
+
2120
+
2121
+	/**
2122
+	 * _trash_or_restore_event
2123
+	 *
2124
+	 * @access protected
2125
+	 * @param string $event_status
2126
+	 * @param bool   $redirect_after
2127
+	 * @throws EE_Error
2128
+	 * @throws InvalidArgumentException
2129
+	 * @throws InvalidDataTypeException
2130
+	 * @throws InvalidInterfaceException
2131
+	 * @throws ReflectionException
2132
+	 */
2133
+	protected function _trash_or_restore_event($event_status = 'trash', $redirect_after = true)
2134
+	{
2135
+		// determine the event id and set to array.
2136
+		$EVT_ID = isset($this->_req_data['EVT_ID']) ? absint($this->_req_data['EVT_ID']) : false;
2137
+		// loop thru events
2138
+		if ($EVT_ID) {
2139
+			// clean status
2140
+			$event_status = sanitize_key($event_status);
2141
+			// grab status
2142
+			if (! empty($event_status)) {
2143
+				$success = $this->_change_event_status($EVT_ID, $event_status);
2144
+			} else {
2145
+				$success = false;
2146
+				$msg     = esc_html__(
2147
+					'An error occurred. The event could not be moved to the trash because a valid event status was not not supplied.',
2148
+					'event_espresso'
2149
+				);
2150
+				EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2151
+			}
2152
+		} else {
2153
+			$success = false;
2154
+			$msg     = esc_html__(
2155
+				'An error occurred. The event could not be moved to the trash because a valid event ID was not not supplied.',
2156
+				'event_espresso'
2157
+			);
2158
+			EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2159
+		}
2160
+		$action = $event_status === 'trash' ? 'moved to the trash' : 'restored from the trash';
2161
+		if ($redirect_after) {
2162
+			$this->_redirect_after_action($success, 'Event', $action, ['action' => 'default']);
2163
+		}
2164
+	}
2165
+
2166
+
2167
+	/**
2168
+	 * _trash_or_restore_events
2169
+	 *
2170
+	 * @access protected
2171
+	 * @param string $event_status
2172
+	 * @return void
2173
+	 * @throws EE_Error
2174
+	 * @throws InvalidArgumentException
2175
+	 * @throws InvalidDataTypeException
2176
+	 * @throws InvalidInterfaceException
2177
+	 * @throws ReflectionException
2178
+	 */
2179
+	protected function _trash_or_restore_events($event_status = 'trash')
2180
+	{
2181
+		// clean status
2182
+		$event_status = sanitize_key($event_status);
2183
+		// grab status
2184
+		if (! empty($event_status)) {
2185
+			$success = true;
2186
+			// determine the event id and set to array.
2187
+			$EVT_IDs = isset($this->_req_data['EVT_IDs']) ? (array) $this->_req_data['EVT_IDs'] : [];
2188
+			// loop thru events
2189
+			foreach ($EVT_IDs as $EVT_ID) {
2190
+				if ($EVT_ID = absint($EVT_ID)) {
2191
+					$results = $this->_change_event_status($EVT_ID, $event_status);
2192
+					$success = $results !== false ? $success : false;
2193
+				} else {
2194
+					$msg = sprintf(
2195
+						esc_html__(
2196
+							'An error occurred. Event #%d could not be moved to the trash because a valid event ID was not not supplied.',
2197
+							'event_espresso'
2198
+						),
2199
+						$EVT_ID
2200
+					);
2201
+					EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2202
+					$success = false;
2203
+				}
2204
+			}
2205
+		} else {
2206
+			$success = false;
2207
+			$msg     = esc_html__(
2208
+				'An error occurred. The event could not be moved to the trash because a valid event status was not not supplied.',
2209
+				'event_espresso'
2210
+			);
2211
+			EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2212
+		}
2213
+		// in order to force a pluralized result message we need to send back a success status greater than 1
2214
+		$success = $success ? 2 : false;
2215
+		$action  = $event_status === 'trash' ? 'moved to the trash' : 'restored from the trash';
2216
+		$this->_redirect_after_action($success, 'Events', $action, ['action' => 'default']);
2217
+	}
2218
+
2219
+
2220
+	/**
2221
+	 * _trash_or_restore_events
2222
+	 *
2223
+	 * @access  private
2224
+	 * @param int    $EVT_ID
2225
+	 * @param string $event_status
2226
+	 * @return bool
2227
+	 * @throws EE_Error
2228
+	 * @throws InvalidArgumentException
2229
+	 * @throws InvalidDataTypeException
2230
+	 * @throws InvalidInterfaceException
2231
+	 * @throws ReflectionException
2232
+	 */
2233
+	private function _change_event_status($EVT_ID = 0, $event_status = '')
2234
+	{
2235
+		// grab event id
2236
+		if (! $EVT_ID) {
2237
+			$msg = esc_html__(
2238
+				'An error occurred. No Event ID or an invalid Event ID was received.',
2239
+				'event_espresso'
2240
+			);
2241
+			EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2242
+			return false;
2243
+		}
2244
+		$this->_cpt_model_obj = EEM_Event::instance()->get_one_by_ID($EVT_ID);
2245
+		// clean status
2246
+		$event_status = sanitize_key($event_status);
2247
+		// grab status
2248
+		if (empty($event_status)) {
2249
+			$msg = esc_html__(
2250
+				'An error occurred. No Event Status or an invalid Event Status was received.',
2251
+				'event_espresso'
2252
+			);
2253
+			EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2254
+			return false;
2255
+		}
2256
+		// was event trashed or restored ?
2257
+		switch ($event_status) {
2258
+			case 'draft':
2259
+				$action = 'restored from the trash';
2260
+				$hook   = 'AHEE_event_restored_from_trash';
2261
+				break;
2262
+			case 'trash':
2263
+				$action = 'moved to the trash';
2264
+				$hook   = 'AHEE_event_moved_to_trash';
2265
+				break;
2266
+			default:
2267
+				$action = 'updated';
2268
+				$hook   = false;
2269
+		}
2270
+		// use class to change status
2271
+		$this->_cpt_model_obj->set_status($event_status);
2272
+		$success = $this->_cpt_model_obj->save();
2273
+		if ($success === false) {
2274
+			$msg = sprintf(esc_html__('An error occurred. The event could not be %s.', 'event_espresso'), $action);
2275
+			EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2276
+			return false;
2277
+		}
2278
+		if ($hook) {
2279
+			do_action($hook);
2280
+		}
2281
+		return true;
2282
+	}
2283
+
2284
+
2285
+	/**
2286
+	 * _delete_event
2287
+	 *
2288
+	 * @throws InvalidArgumentException
2289
+	 * @throws InvalidDataTypeException
2290
+	 * @throws InvalidInterfaceException
2291
+	 */
2292
+	protected function _delete_event()
2293
+	{
2294
+		$this->generateDeletionPreview(isset($this->_req_data['EVT_ID']) ? $this->_req_data['EVT_ID'] : []);
2295
+	}
2296
+
2297
+
2298
+	/**
2299
+	 * Gets the tree traversal batch persister.
2300
+	 *
2301
+	 * @return NodeGroupDao
2302
+	 * @throws InvalidArgumentException
2303
+	 * @throws InvalidDataTypeException
2304
+	 * @throws InvalidInterfaceException
2305
+	 * @since 4.10.12.p
2306
+	 */
2307
+	protected function getModelObjNodeGroupPersister()
2308
+	{
2309
+		if (! $this->model_obj_node_group_persister instanceof NodeGroupDao) {
2310
+			$this->model_obj_node_group_persister =
2311
+				$this->getLoader()->load('\EventEspresso\core\services\orm\tree_traversal\NodeGroupDao');
2312
+		}
2313
+		return $this->model_obj_node_group_persister;
2314
+	}
2315
+
2316
+
2317
+	/**
2318
+	 * _delete_events
2319
+	 *
2320
+	 * @access protected
2321
+	 * @return void
2322
+	 * @throws InvalidArgumentException
2323
+	 * @throws InvalidDataTypeException
2324
+	 * @throws InvalidInterfaceException
2325
+	 */
2326
+	protected function _delete_events()
2327
+	{
2328
+		$this->generateDeletionPreview(isset($this->_req_data['EVT_IDs']) ? (array) $this->_req_data['EVT_IDs'] : []);
2329
+	}
2330
+
2331
+
2332
+	protected function generateDeletionPreview($event_ids)
2333
+	{
2334
+		$event_ids = (array) $event_ids;
2335
+		// Set a code we can use to reference this deletion task in the batch jobs and preview page.
2336
+		$deletion_job_code = $this->getModelObjNodeGroupPersister()->generateGroupCode();
2337
+		$return_url        = EE_Admin_Page::add_query_args_and_nonce(
2338
+			[
2339
+				'action'            => 'preview_deletion',
2340
+				'deletion_job_code' => $deletion_job_code,
2341
+			],
2342
+			$this->_admin_base_url
2343
+		);
2344
+		$event_ids         = array_map(
2345
+			'intval',
2346
+			$event_ids
2347
+		);
2348
+
2349
+		EEH_URL::safeRedirectAndExit(
2350
+			EE_Admin_Page::add_query_args_and_nonce(
2351
+				[
2352
+					'page'              => 'espresso_batch',
2353
+					'batch'             => EED_Batch::batch_job,
2354
+					'EVT_IDs'           => $event_ids,
2355
+					'deletion_job_code' => $deletion_job_code,
2356
+					'job_handler'       => urlencode('EventEspressoBatchRequest\JobHandlers\PreviewEventDeletion'),
2357
+					'return_url'        => urlencode($return_url),
2358
+				],
2359
+				admin_url()
2360
+			)
2361
+		);
2362
+	}
2363
+
2364
+
2365
+	/**
2366
+	 * Checks for a POST submission
2367
+	 *
2368
+	 * @since 4.10.12.p
2369
+	 */
2370
+	protected function confirmDeletion()
2371
+	{
2372
+		$deletion_redirect_logic =
2373
+			$this->getLoader()->getShared('\EventEspresso\core\domain\services\admin\events\data\ConfirmDeletion');
2374
+		$deletion_redirect_logic->handle($this->get_request_data(), $this->admin_base_url());
2375
+	}
2376
+
2377
+
2378
+	/**
2379
+	 * A page for users to preview what exactly will be deleted, and confirm they want to delete it.
2380
+	 *
2381
+	 * @throws EE_Error
2382
+	 * @since 4.10.12.p
2383
+	 */
2384
+	protected function previewDeletion()
2385
+	{
2386
+		$preview_deletion_logic =
2387
+			$this->getLoader()->getShared('\EventEspresso\core\domain\services\admin\events\data\PreviewDeletion');
2388
+		$this->set_template_args($preview_deletion_logic->handle($this->get_request_data(), $this->admin_base_url()));
2389
+		$this->display_admin_page_with_no_sidebar();
2390
+	}
2391
+
2392
+
2393
+	/**
2394
+	 * get total number of events
2395
+	 *
2396
+	 * @access public
2397
+	 * @return int
2398
+	 * @throws EE_Error
2399
+	 * @throws InvalidArgumentException
2400
+	 * @throws InvalidDataTypeException
2401
+	 * @throws InvalidInterfaceException
2402
+	 */
2403
+	public function total_events()
2404
+	{
2405
+		return EEM_Event::instance()->count(['caps' => 'read_admin'], 'EVT_ID', true);
2406
+	}
2407
+
2408
+
2409
+	/**
2410
+	 * get total number of draft events
2411
+	 *
2412
+	 * @access public
2413
+	 * @return int
2414
+	 * @throws EE_Error
2415
+	 * @throws InvalidArgumentException
2416
+	 * @throws InvalidDataTypeException
2417
+	 * @throws InvalidInterfaceException
2418
+	 */
2419
+	public function total_events_draft()
2420
+	{
2421
+		$where = [
2422
+			'status' => ['IN', ['draft', 'auto-draft']],
2423
+		];
2424
+		return EEM_Event::instance()->count([$where, 'caps' => 'read_admin'], 'EVT_ID', true);
2425
+	}
2426
+
2427
+
2428
+	/**
2429
+	 * get total number of trashed events
2430
+	 *
2431
+	 * @access public
2432
+	 * @return int
2433
+	 * @throws EE_Error
2434
+	 * @throws InvalidArgumentException
2435
+	 * @throws InvalidDataTypeException
2436
+	 * @throws InvalidInterfaceException
2437
+	 */
2438
+	public function total_trashed_events()
2439
+	{
2440
+		$where = [
2441
+			'status' => 'trash',
2442
+		];
2443
+		return EEM_Event::instance()->count([$where, 'caps' => 'read_admin'], 'EVT_ID', true);
2444
+	}
2445
+
2446
+
2447
+	/**
2448
+	 *    _default_event_settings
2449
+	 *    This generates the Default Settings Tab
2450
+	 *
2451
+	 * @return void
2452
+	 * @throws DomainException
2453
+	 * @throws EE_Error
2454
+	 * @throws InvalidArgumentException
2455
+	 * @throws InvalidDataTypeException
2456
+	 * @throws InvalidInterfaceException
2457
+	 */
2458
+	protected function _default_event_settings()
2459
+	{
2460
+		$this->_set_add_edit_form_tags('update_default_event_settings');
2461
+		$this->_set_publish_post_box_vars(null, false, false, null, false);
2462
+		$this->_template_args['admin_page_content'] = $this->_default_event_settings_form()->get_html();
2463
+		$this->display_admin_page_with_sidebar();
2464
+	}
2465
+
2466
+
2467
+	/**
2468
+	 * Return the form for event settings.
2469
+	 *
2470
+	 * @return EE_Form_Section_Proper
2471
+	 * @throws EE_Error
2472
+	 */
2473
+	protected function _default_event_settings_form()
2474
+	{
2475
+		$registration_config              = EE_Registry::instance()->CFG->registration;
2476
+		$registration_stati_for_selection = EEM_Registration::reg_status_array(
2477
+		// exclude
2478
+			[
2479
+				EEM_Registration::status_id_cancelled,
2480
+				EEM_Registration::status_id_declined,
2481
+				EEM_Registration::status_id_incomplete,
2482
+				EEM_Registration::status_id_wait_list,
2483
+			],
2484
+			true
2485
+		);
2486
+		return new EE_Form_Section_Proper(
2487
+			[
2488
+				'name'            => 'update_default_event_settings',
2489
+				'html_id'         => 'update_default_event_settings',
2490
+				'html_class'      => 'form-table',
2491
+				'layout_strategy' => new EE_Admin_Two_Column_Layout(),
2492
+				'subsections'     => apply_filters(
2493
+					'FHEE__Events_Admin_Page___default_event_settings_form__form_subsections',
2494
+					[
2495
+						'default_reg_status'  => new EE_Select_Input(
2496
+							$registration_stati_for_selection,
2497
+							[
2498
+								'default'         => isset($registration_config->default_STS_ID)
2499
+													 && array_key_exists(
2500
+														 $registration_config->default_STS_ID,
2501
+														 $registration_stati_for_selection
2502
+													 )
2503
+									? sanitize_text_field($registration_config->default_STS_ID)
2504
+									: EEM_Registration::status_id_pending_payment,
2505
+								'html_label_text' => esc_html__('Default Registration Status', 'event_espresso')
2506
+														. EEH_Template::get_help_tab_link(
2507
+															'default_settings_status_help_tab'
2508
+														),
2509
+								'html_help_text'  => esc_html__(
2510
+									'This setting allows you to preselect what the default registration status setting is when creating an event.  Note that changing this setting does NOT retroactively apply it to existing events.',
2511
+									'event_espresso'
2512
+								),
2513
+							]
2514
+						),
2515
+						'default_max_tickets' => new EE_Integer_Input(
2516
+							[
2517
+								'default'         => isset($registration_config->default_maximum_number_of_tickets)
2518
+									? $registration_config->default_maximum_number_of_tickets
2519
+									: EEM_Event::get_default_additional_limit(),
2520
+								'html_label_text' => esc_html__(
2521
+									'Default Maximum Tickets Allowed Per Order:',
2522
+									'event_espresso'
2523
+								)
2524
+								. EEH_Template::get_help_tab_link(
2525
+									'default_maximum_tickets_help_tab"'
2526
+								),
2527
+								'html_help_text'  => esc_html__(
2528
+									'This setting allows you to indicate what will be the default for the maximum number of tickets per order when creating new events.',
2529
+									'event_espresso'
2530
+								),
2531
+							]
2532
+						),
2533
+					]
2534
+				),
2535
+			]
2536
+		);
2537
+	}
2538
+
2539
+
2540
+	/**
2541
+	 * @return void
2542
+	 * @throws EE_Error
2543
+	 * @throws InvalidArgumentException
2544
+	 * @throws InvalidDataTypeException
2545
+	 * @throws InvalidInterfaceException
2546
+	 */
2547
+	protected function _update_default_event_settings()
2548
+	{
2549
+		$form = $this->_default_event_settings_form();
2550
+		if ($form->was_submitted()) {
2551
+			$form->receive_form_submission();
2552
+			if ($form->is_valid()) {
2553
+				$registration_config = EE_Registry::instance()->CFG->registration;
2554
+				$valid_data          = $form->valid_data();
2555
+				if (isset($valid_data['default_reg_status'])) {
2556
+					$registration_config->default_STS_ID = $valid_data['default_reg_status'];
2557
+				}
2558
+				if (isset($valid_data['default_max_tickets'])) {
2559
+					$registration_config->default_maximum_number_of_tickets = $valid_data['default_max_tickets'];
2560
+				}
2561
+				do_action(
2562
+					'AHEE__Events_Admin_Page___update_default_event_settings',
2563
+					$valid_data,
2564
+					EE_Registry::instance()->CFG,
2565
+					$this
2566
+				);
2567
+				// update because data was valid!
2568
+				EE_Registry::instance()->CFG->update_espresso_config();
2569
+				EE_Error::overwrite_success();
2570
+				EE_Error::add_success(
2571
+					__('Default Event Settings were updated', 'event_espresso')
2572
+				);
2573
+			}
2574
+		}
2575
+		$this->_redirect_after_action(0, '', '', ['action' => 'default_event_settings'], true);
2576
+	}
2577
+
2578
+
2579
+	/*************        Templates        *************/
2580
+	protected function _template_settings()
2581
+	{
2582
+		$this->_admin_page_title              = esc_html__('Template Settings (Preview)', 'event_espresso');
2583
+		$this->_template_args['preview_img']  = '<img src="'
2584
+												. EVENTS_ASSETS_URL
2585
+												. '/images/'
2586
+												. 'caffeinated_template_features.jpg" alt="'
2587
+												. esc_attr__('Template Settings Preview screenshot', 'event_espresso')
2588
+												. '" />';
2589
+		$this->_template_args['preview_text'] = '<strong>'
2590
+												. esc_html__(
2591
+													'Template Settings 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. Template Settings allow you to configure some of the appearance options for both the Event List and Event Details pages.',
2592
+													'event_espresso'
2593
+												) . '</strong>';
2594
+		$this->display_admin_caf_preview_page('template_settings_tab');
2595
+	}
2596
+
2597
+
2598
+	/** Event Category Stuff **/
2599
+	/**
2600
+	 * set the _category property with the category object for the loaded page.
2601
+	 *
2602
+	 * @access private
2603
+	 * @return void
2604
+	 */
2605
+	private function _set_category_object()
2606
+	{
2607
+		if (isset($this->_category->id) && ! empty($this->_category->id)) {
2608
+			return;
2609
+		} //already have the category object so get out.
2610
+		// set default category object
2611
+		$this->_set_empty_category_object();
2612
+		// only set if we've got an id
2613
+		if (! isset($this->_req_data['EVT_CAT_ID'])) {
2614
+			return;
2615
+		}
2616
+		$category_id = absint($this->_req_data['EVT_CAT_ID']);
2617
+		$term        = get_term($category_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY);
2618
+		if (! empty($term)) {
2619
+			$this->_category->category_name       = $term->name;
2620
+			$this->_category->category_identifier = $term->slug;
2621
+			$this->_category->category_desc       = $term->description;
2622
+			$this->_category->id                  = $term->term_id;
2623
+			$this->_category->parent              = $term->parent;
2624
+		}
2625
+	}
2626
+
2627
+
2628
+	/**
2629
+	 * Clears out category properties.
2630
+	 */
2631
+	private function _set_empty_category_object()
2632
+	{
2633
+		$this->_category                = new stdClass();
2634
+		$this->_category->category_name = $this->_category->category_identifier = $this->_category->category_desc = '';
2635
+		$this->_category->id            = $this->_category->parent = 0;
2636
+	}
2637
+
2638
+
2639
+	/**
2640
+	 * @throws DomainException
2641
+	 * @throws EE_Error
2642
+	 * @throws InvalidArgumentException
2643
+	 * @throws InvalidDataTypeException
2644
+	 * @throws InvalidInterfaceException
2645
+	 */
2646
+	protected function _category_list_table()
2647
+	{
2648
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
2649
+		$this->_search_btn_label = esc_html__('Categories', 'event_espresso');
2650
+		$this->_admin_page_title .= ' ';
2651
+		$this->_admin_page_title .= $this->get_action_link_or_button(
2652
+			'add_category',
2653
+			'add_category',
2654
+			[],
2655
+			'add-new-h2'
2656
+		);
2657
+		$this->display_admin_list_table_page_with_sidebar();
2658
+	}
2659
+
2660
+
2661
+	/**
2662
+	 * Output category details view.
2663
+	 *
2664
+	 * @param string $view
2665
+	 * @throws DomainException
2666
+	 * @throws EE_Error
2667
+	 * @throws InvalidArgumentException
2668
+	 * @throws InvalidDataTypeException
2669
+	 * @throws InvalidInterfaceException
2670
+	 */
2671
+	protected function _category_details($view)
2672
+	{
2673
+		// load formatter helper
2674
+		// load field generator helper
2675
+		$route = $view === 'edit' ? 'update_category' : 'insert_category';
2676
+		$this->_set_add_edit_form_tags($route);
2677
+		$this->_set_category_object();
2678
+		$id            = ! empty($this->_category->id) ? $this->_category->id : '';
2679
+		$delete_action = 'delete_category';
2680
+		// custom redirect
2681
+		$redirect = EE_Admin_Page::add_query_args_and_nonce(
2682
+			['action' => 'category_list'],
2683
+			$this->_admin_base_url
2684
+		);
2685
+		$this->_set_publish_post_box_vars('EVT_CAT_ID', $id, $delete_action, $redirect);
2686
+		// take care of contents
2687
+		$this->_template_args['admin_page_content'] = $this->_category_details_content();
2688
+		$this->display_admin_page_with_sidebar();
2689
+	}
2690
+
2691
+
2692
+	/**
2693
+	 * Output category details content.
2694
+	 *
2695
+	 * @throws DomainException
2696
+	 */
2697
+	protected function _category_details_content()
2698
+	{
2699
+		$editor_args['category_desc'] = [
2700
+			'type'          => 'wp_editor',
2701
+			'value'         => EEH_Formatter::admin_format_content($this->_category->category_desc),
2702
+			'class'         => 'my_editor_custom',
2703
+			'wpeditor_args' => ['media_buttons' => false],
2704
+		];
2705
+		$_wp_editor                   = $this->_generate_admin_form_fields($editor_args, 'array');
2706
+		$all_terms                    = get_terms(
2707
+			[EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY],
2708
+			['hide_empty' => 0, 'exclude' => [$this->_category->id]]
2709
+		);
2710
+		// setup category select for term parents.
2711
+		$category_select_values[] = [
2712
+			'text' => esc_html__('No Parent', 'event_espresso'),
2713
+			'id'   => 0,
2714
+		];
2715
+		foreach ($all_terms as $term) {
2716
+			$category_select_values[] = [
2717
+				'text' => $term->name,
2718
+				'id'   => $term->term_id,
2719
+			];
2720
+		}
2721
+		$category_select = EEH_Form_Fields::select_input(
2722
+			'category_parent',
2723
+			$category_select_values,
2724
+			$this->_category->parent
2725
+		);
2726
+		$template_args   = [
2727
+			'category'                 => $this->_category,
2728
+			'category_select'          => $category_select,
2729
+			'unique_id_info_help_link' => $this->_get_help_tab_link('unique_id_info'),
2730
+			'category_desc_editor'     => $_wp_editor['category_desc']['field'],
2731
+			'disable'                  => '',
2732
+			'disabled_message'         => false,
2733
+		];
2734
+		$template        = EVENTS_TEMPLATE_PATH . 'event_category_details.template.php';
2735
+		return EEH_Template::display_template($template, $template_args, true);
2736
+	}
2737
+
2738
+
2739
+	/**
2740
+	 * Handles deleting categories.
2741
+	 */
2742
+	protected function _delete_categories()
2743
+	{
2744
+		$cat_ids = isset($this->_req_data['EVT_CAT_ID']) ? (array) $this->_req_data['EVT_CAT_ID']
2745
+			: (array) $this->_req_data['category_id'];
2746
+		foreach ($cat_ids as $cat_id) {
2747
+			$this->_delete_category($cat_id);
2748
+		}
2749
+		// doesn't matter what page we're coming from... we're going to the same place after delete.
2750
+		$query_args = [
2751
+			'action' => 'category_list',
2752
+		];
2753
+		$this->_redirect_after_action(0, '', '', $query_args);
2754
+	}
2755
+
2756
+
2757
+	/**
2758
+	 * Handles deleting specific category.
2759
+	 *
2760
+	 * @param int $cat_id
2761
+	 */
2762
+	protected function _delete_category($cat_id)
2763
+	{
2764
+		$cat_id = absint($cat_id);
2765
+		wp_delete_term($cat_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY);
2766
+	}
2767
+
2768
+
2769
+	/**
2770
+	 * Handles triggering the update or insertion of a new category.
2771
+	 *
2772
+	 * @param bool $new_category true means we're triggering the insert of a new category.
2773
+	 * @throws EE_Error
2774
+	 * @throws InvalidArgumentException
2775
+	 * @throws InvalidDataTypeException
2776
+	 * @throws InvalidInterfaceException
2777
+	 */
2778
+	protected function _insert_or_update_category($new_category)
2779
+	{
2780
+		$cat_id  = $new_category ? $this->_insert_category() : $this->_insert_category(true);
2781
+		$success = 0; // we already have a success message so lets not send another.
2782
+		if ($cat_id) {
2783
+			$query_args = [
2784
+				'action'     => 'edit_category',
2785
+				'EVT_CAT_ID' => $cat_id,
2786
+			];
2787
+		} else {
2788
+			$query_args = ['action' => 'add_category'];
2789
+		}
2790
+		$this->_redirect_after_action($success, '', '', $query_args, true);
2791
+	}
2792
+
2793
+
2794
+	/**
2795
+	 * Inserts or updates category
2796
+	 *
2797
+	 * @param bool $update (true indicates we're updating a category).
2798
+	 * @return bool|mixed|string
2799
+	 */
2800
+	private function _insert_category($update = false)
2801
+	{
2802
+		$cat_id          = $update ? $this->_req_data['EVT_CAT_ID'] : '';
2803
+		$category_name   = isset($this->_req_data['category_name']) ? $this->_req_data['category_name'] : '';
2804
+		$category_desc   = isset($this->_req_data['category_desc']) ? $this->_req_data['category_desc'] : '';
2805
+		$category_parent = isset($this->_req_data['category_parent']) ? $this->_req_data['category_parent'] : 0;
2806
+		if (empty($category_name)) {
2807
+			$msg = esc_html__('You must add a name for the category.', 'event_espresso');
2808
+			EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2809
+			return false;
2810
+		}
2811
+		$term_args = [
2812
+			'name'        => $category_name,
2813
+			'description' => $category_desc,
2814
+			'parent'      => $category_parent,
2815
+		];
2816
+		// was the category_identifier input disabled?
2817
+		if (isset($this->_req_data['category_identifier'])) {
2818
+			$term_args['slug'] = $this->_req_data['category_identifier'];
2819
+		}
2820
+		$insert_ids = $update
2821
+			? wp_update_term($cat_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY, $term_args)
2822
+			: wp_insert_term($category_name, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY, $term_args);
2823
+		if (! is_array($insert_ids)) {
2824
+			$msg = esc_html__(
2825
+				'An error occurred and the category has not been saved to the database.',
2826
+				'event_espresso'
2827
+			);
2828
+			EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
2829
+		} else {
2830
+			$cat_id = $insert_ids['term_id'];
2831
+			$msg    = sprintf(esc_html__('The category %s was successfully saved', 'event_espresso'), $category_name);
2832
+			EE_Error::add_success($msg);
2833
+		}
2834
+		return $cat_id;
2835
+	}
2836
+
2837
+
2838
+	/**
2839
+	 * Gets categories or count of categories matching the arguments in the request.
2840
+	 *
2841
+	 * @param int  $per_page
2842
+	 * @param int  $current_page
2843
+	 * @param bool $count
2844
+	 * @return EE_Base_Class[]|EE_Term_Taxonomy[]|int
2845
+	 * @throws EE_Error
2846
+	 * @throws InvalidArgumentException
2847
+	 * @throws InvalidDataTypeException
2848
+	 * @throws InvalidInterfaceException
2849
+	 */
2850
+	public function get_categories($per_page = 10, $current_page = 1, $count = false)
2851
+	{
2852
+		// testing term stuff
2853
+		$orderby = isset($this->_req_data['orderby']) ? $this->_req_data['orderby'] : 'Term.term_id';
2854
+		$order   = isset($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC';
2855
+		$limit   = ($current_page - 1) * $per_page;
2856
+		$where   = ['taxonomy' => EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY];
2857
+		if (isset($this->_req_data['s'])) {
2858
+			$sstr        = '%' . $this->_req_data['s'] . '%';
2859
+			$where['OR'] = [
2860
+				'Term.name'   => ['LIKE', $sstr],
2861
+				'description' => ['LIKE', $sstr],
2862
+			];
2863
+		}
2864
+		$query_params = [
2865
+			$where,
2866
+			'order_by'   => [$orderby => $order],
2867
+			'limit'      => $limit . ',' . $per_page,
2868
+			'force_join' => ['Term'],
2869
+		];
2870
+		return $count
2871
+			? EEM_Term_Taxonomy::instance()->count($query_params, 'term_id')
2872
+			: EEM_Term_Taxonomy::instance()->get_all($query_params);
2873
+	}
2874
+
2875
+	/* end category stuff */
2876
+	/**************/
2877
+
2878
+
2879
+	/**
2880
+	 * Callback for the `ee_save_timezone_setting` ajax action.
2881
+	 *
2882
+	 * @throws EE_Error
2883
+	 * @throws InvalidArgumentException
2884
+	 * @throws InvalidDataTypeException
2885
+	 * @throws InvalidInterfaceException
2886
+	 */
2887
+	public function save_timezonestring_setting()
2888
+	{
2889
+		$timezone_string = isset($this->_req_data['timezone_selected'])
2890
+			? $this->_req_data['timezone_selected']
2891
+			: '';
2892
+		if (empty($timezone_string) || ! EEH_DTT_Helper::validate_timezone($timezone_string, false)) {
2893
+			EE_Error::add_error(
2894
+				esc_html__('An invalid timezone string submitted.', 'event_espresso'),
2895
+				__FILE__,
2896
+				__FUNCTION__,
2897
+				__LINE__
2898
+			);
2899
+			$this->_template_args['error'] = true;
2900
+			$this->_return_json();
2901
+		}
2902
+
2903
+		update_option('timezone_string', $timezone_string);
2904
+		EE_Error::add_success(
2905
+			esc_html__('Your timezone string was updated.', 'event_espresso')
2906
+		);
2907
+		$this->_template_args['success'] = true;
2908
+		$this->_return_json(true, ['action' => 'create_new']);
2909
+	}
2910 2910
 }
Please login to merge, or discard this patch.
Spacing   +84 added lines, -84 removed lines patch added patch discarded remove patch
@@ -535,7 +535,7 @@  discard block
 block discarded – undo
535 535
         ];
536 536
         // only load EE_Event_Editor_Decaf_Tips if domain is not caffeinated
537 537
         $domain = $this->loader->getShared('EventEspresso\core\domain\Domain');
538
-        if (! $domain->isCaffeinated()) {
538
+        if ( ! $domain->isCaffeinated()) {
539 539
             $this->_page_config['create_new']['qtips'] = ['EE_Event_Editor_Decaf_Tips'];
540 540
             $this->_page_config['edit']['qtips']       = ['EE_Event_Editor_Decaf_Tips'];
541 541
         }
@@ -594,13 +594,13 @@  discard block
 block discarded – undo
594 594
     {
595 595
         wp_register_style(
596 596
             'events-admin-css',
597
-            EVENTS_ASSETS_URL . 'events-admin-page.css',
597
+            EVENTS_ASSETS_URL.'events-admin-page.css',
598 598
             [],
599 599
             EVENT_ESPRESSO_VERSION
600 600
         );
601 601
         wp_register_style(
602 602
             'ee-cat-admin',
603
-            EVENTS_ASSETS_URL . 'ee-cat-admin.css',
603
+            EVENTS_ASSETS_URL.'ee-cat-admin.css',
604 604
             [],
605 605
             EVENT_ESPRESSO_VERSION
606 606
         );
@@ -609,7 +609,7 @@  discard block
 block discarded – undo
609 609
         // scripts
610 610
         wp_register_script(
611 611
             'event_editor_js',
612
-            EVENTS_ASSETS_URL . 'event_editor.js',
612
+            EVENTS_ASSETS_URL.'event_editor.js',
613 613
             ['ee_admin_js', 'jquery-ui-slider', 'jquery-ui-timepicker-addon'],
614 614
             EVENT_ESPRESSO_VERSION,
615 615
             true
@@ -635,16 +635,16 @@  discard block
 block discarded – undo
635 635
         wp_enqueue_style('espresso-ui-theme');
636 636
         wp_register_style(
637 637
             'event-editor-css',
638
-            EVENTS_ASSETS_URL . 'event-editor.css',
638
+            EVENTS_ASSETS_URL.'event-editor.css',
639 639
             ['ee-admin-css'],
640 640
             EVENT_ESPRESSO_VERSION
641 641
         );
642 642
         wp_enqueue_style('event-editor-css');
643 643
         // scripts
644
-        if (! $this->admin_config->useAdvancedEditor()) {
644
+        if ( ! $this->admin_config->useAdvancedEditor()) {
645 645
             wp_register_script(
646 646
                 'event-datetime-metabox',
647
-                EVENTS_ASSETS_URL . 'event-datetime-metabox.js',
647
+                EVENTS_ASSETS_URL.'event-datetime-metabox.js',
648 648
                 ['event_editor_js', 'ee-datepicker'],
649 649
                 EVENT_ESPRESSO_VERSION
650 650
             );
@@ -714,15 +714,15 @@  discard block
 block discarded – undo
714 714
     public function verify_event_edit($event = null, $req_type = '')
715 715
     {
716 716
         // don't need to do this when processing
717
-        if (! empty($req_type)) {
717
+        if ( ! empty($req_type)) {
718 718
             return;
719 719
         }
720 720
         // no event?
721
-        if (! $event instanceof EE_Event) {
721
+        if ( ! $event instanceof EE_Event) {
722 722
             $event = $this->_cpt_model_obj;
723 723
         }
724 724
         // STILL no event?
725
-        if (! $event instanceof EE_Event) {
725
+        if ( ! $event instanceof EE_Event) {
726 726
             return;
727 727
         }
728 728
         $orig_status = $event->status();
@@ -762,7 +762,7 @@  discard block
 block discarded – undo
762 762
             );
763 763
         }
764 764
         // now we need to determine if the event has any tickets on sale.  If not then we dont' show the error
765
-        if (! $event->tickets_on_sale()) {
765
+        if ( ! $event->tickets_on_sale()) {
766 766
             return;
767 767
         }
768 768
         // made it here so show warning
@@ -810,7 +810,7 @@  discard block
 block discarded – undo
810 810
     {
811 811
         $has_timezone_string = get_option('timezone_string');
812 812
         // only nag them about setting their timezone if it's their first event, and they haven't already done it
813
-        if (! $has_timezone_string && ! EEM_Event::instance()->exists([])) {
813
+        if ( ! $has_timezone_string && ! EEM_Event::instance()->exists([])) {
814 814
             EE_Error::add_attention(
815 815
                 sprintf(
816 816
                     __(
@@ -877,7 +877,7 @@  discard block
 block discarded – undo
877 877
      */
878 878
     protected function _event_legend_items()
879 879
     {
880
-        $items    = [
880
+        $items = [
881 881
             'view_details'   => [
882 882
                 'class' => 'dashicons dashicons-search',
883 883
                 'desc'  => esc_html__('View Event', 'event_espresso'),
@@ -894,31 +894,31 @@  discard block
 block discarded – undo
894 894
         $items    = apply_filters('FHEE__Events_Admin_Page___event_legend_items__items', $items);
895 895
         $statuses = [
896 896
             'sold_out_status'  => [
897
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::sold_out,
897
+                'class' => 'ee-status-legend ee-status-legend-'.EE_Datetime::sold_out,
898 898
                 'desc'  => EEH_Template::pretty_status(EE_Datetime::sold_out, false, 'sentence'),
899 899
             ],
900 900
             'active_status'    => [
901
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::active,
901
+                'class' => 'ee-status-legend ee-status-legend-'.EE_Datetime::active,
902 902
                 'desc'  => EEH_Template::pretty_status(EE_Datetime::active, false, 'sentence'),
903 903
             ],
904 904
             'upcoming_status'  => [
905
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::upcoming,
905
+                'class' => 'ee-status-legend ee-status-legend-'.EE_Datetime::upcoming,
906 906
                 'desc'  => EEH_Template::pretty_status(EE_Datetime::upcoming, false, 'sentence'),
907 907
             ],
908 908
             'postponed_status' => [
909
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::postponed,
909
+                'class' => 'ee-status-legend ee-status-legend-'.EE_Datetime::postponed,
910 910
                 'desc'  => EEH_Template::pretty_status(EE_Datetime::postponed, false, 'sentence'),
911 911
             ],
912 912
             'cancelled_status' => [
913
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::cancelled,
913
+                'class' => 'ee-status-legend ee-status-legend-'.EE_Datetime::cancelled,
914 914
                 'desc'  => EEH_Template::pretty_status(EE_Datetime::cancelled, false, 'sentence'),
915 915
             ],
916 916
             'expired_status'   => [
917
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::expired,
917
+                'class' => 'ee-status-legend ee-status-legend-'.EE_Datetime::expired,
918 918
                 'desc'  => EEH_Template::pretty_status(EE_Datetime::expired, false, 'sentence'),
919 919
             ],
920 920
             'inactive_status'  => [
921
-                'class' => 'ee-status-legend ee-status-legend-' . EE_Datetime::inactive,
921
+                'class' => 'ee-status-legend ee-status-legend-'.EE_Datetime::inactive,
922 922
                 'desc'  => EEH_Template::pretty_status(EE_Datetime::inactive, false, 'sentence'),
923 923
             ],
924 924
         ];
@@ -937,7 +937,7 @@  discard block
 block discarded – undo
937 937
      */
938 938
     private function _event_model()
939 939
     {
940
-        if (! $this->_event_model instanceof EEM_Event) {
940
+        if ( ! $this->_event_model instanceof EEM_Event) {
941 941
             $this->_event_model = EE_Registry::instance()->load_model('Event');
942 942
         }
943 943
         return $this->_event_model;
@@ -957,8 +957,8 @@  discard block
 block discarded – undo
957 957
     public function extra_permalink_field_buttons($return, $id, $new_title, $new_slug)
958 958
     {
959 959
         // make sure this is only when editing
960
-        if (! empty($id)) {
961
-            $post   = get_post($id);
960
+        if ( ! empty($id)) {
961
+            $post = get_post($id);
962 962
             $return .= '<a class="button button-small" onclick="prompt(\'Shortcode:\', jQuery(\'#shortcode\').val()); return false;" href="#"  tabindex="-1">'
963 963
                        . esc_html__('Shortcode', 'event_espresso')
964 964
                        . '</a> ';
@@ -985,7 +985,7 @@  discard block
 block discarded – undo
985 985
     protected function _events_overview_list_table()
986 986
     {
987 987
         do_action('AHEE_log', __FILE__, __FUNCTION__, '');
988
-        $this->_template_args['after_list_table']                           =
988
+        $this->_template_args['after_list_table'] =
989 989
             ! empty($this->_template_args['after_list_table'])
990 990
                 ? (array) $this->_template_args['after_list_table']
991 991
                 : [];
@@ -995,9 +995,9 @@  discard block
 block discarded – undo
995 995
                 esc_html__('View Event Archive Page', 'event_espresso'),
996 996
                 'button'
997 997
             );
998
-        $this->_template_args['after_list_table']['legend']                 =
998
+        $this->_template_args['after_list_table']['legend'] =
999 999
             $this->_display_legend($this->_event_legend_items());
1000
-        $this->_admin_page_title                                            .= ' '
1000
+        $this->_admin_page_title .= ' '
1001 1001
             . $this->get_action_link_or_button(
1002 1002
                 'create_new',
1003 1003
                 'add',
@@ -1061,12 +1061,12 @@  discard block
 block discarded – undo
1061 1061
         /** @var FeatureFlags $flags */
1062 1062
         $flags = $this->loader->getShared('EventEspresso\core\domain\services\capabilities\FeatureFlags');
1063 1063
         // check if the new EDTR reg options meta box is being used, and if so, don't run updates for legacy version
1064
-        if (! $this->admin_config->useAdvancedEditor() || ! $flags->featureAllowed('use_reg_options_meta_box')) {
1065
-            $event_values['EVT_display_ticket_selector']     =
1064
+        if ( ! $this->admin_config->useAdvancedEditor() || ! $flags->featureAllowed('use_reg_options_meta_box')) {
1065
+            $event_values['EVT_display_ticket_selector'] =
1066 1066
                 ! empty($this->_req_data['display_ticket_selector'])
1067 1067
                     ? 1
1068 1068
                     : 0;
1069
-            $event_values['EVT_additional_limit']            = min(
1069
+            $event_values['EVT_additional_limit'] = min(
1070 1070
                 apply_filters('FHEE__EE_Events_Admin__insert_update_cpt_item__EVT_additional_limit_max', 255),
1071 1071
                 ! empty($this->_req_data['additional_limit'])
1072 1072
                     ? absint($this->_req_data['additional_limit'])
@@ -1098,7 +1098,7 @@  discard block
 block discarded – undo
1098 1098
                 'status*1' => $this->_req_data['original_post_status'],
1099 1099
             ],
1100 1100
         ];
1101
-        $event         = $this->_event_model()->get_one([$get_one_where]);
1101
+        $event = $this->_event_model()->get_one([$get_one_where]);
1102 1102
         // the following are default callbacks for event attachment updates
1103 1103
         // that can be overridden by caffeinated functionality and/or addons.
1104 1104
         $event_update_callbacks = apply_filters(
@@ -1108,7 +1108,7 @@  discard block
 block discarded – undo
1108 1108
                 [$this, '_default_tickets_update'],
1109 1109
             ]
1110 1110
         );
1111
-        $att_success            = true;
1111
+        $att_success = true;
1112 1112
         foreach ($event_update_callbacks as $e_callback) {
1113 1113
             $_success = is_callable($e_callback)
1114 1114
                 ? $e_callback($event, $this->_req_data)
@@ -1175,7 +1175,7 @@  discard block
 block discarded – undo
1175 1175
      */
1176 1176
     protected function _default_venue_update(EE_Event $evtobj, $data)
1177 1177
     {
1178
-        require_once(EE_MODELS . 'EEM_Venue.model.php');
1178
+        require_once(EE_MODELS.'EEM_Venue.model.php');
1179 1179
         $venue_model   = EE_Registry::instance()->load_model('Venue');
1180 1180
         $rows_affected = null;
1181 1181
         $venue_id      = ! empty($data['venue_id']) ? $data['venue_id'] : null;
@@ -1206,7 +1206,7 @@  discard block
 block discarded – undo
1206 1206
             'status'              => 'publish',
1207 1207
         ];
1208 1208
         // if we've got the venue_id then we're just updating the existing venue so let's do that and then get out.
1209
-        if (! empty($venue_id)) {
1209
+        if ( ! empty($venue_id)) {
1210 1210
             $update_where  = [$venue_model->primary_key_name() => $venue_id];
1211 1211
             $rows_affected = $venue_model->update($venue_array, [$update_where]);
1212 1212
             // we've gotta make sure that the venue is always attached to a revision..
@@ -1258,7 +1258,7 @@  discard block
 block discarded – undo
1258 1258
             ];
1259 1259
             // if we have an id then let's get existing object first and then set the new values.
1260 1260
             //  Otherwise we instantiate a new object for save.
1261
-            if (! empty($dtt['DTT_ID'])) {
1261
+            if ( ! empty($dtt['DTT_ID'])) {
1262 1262
                 $DTM = EE_Registry::instance()
1263 1263
                                   ->load_model('Datetime', [$evtobj->get_timezone()])
1264 1264
                                   ->get_one_by_ID($dtt['DTT_ID']);
@@ -1269,7 +1269,7 @@  discard block
 block discarded – undo
1269 1269
                 }
1270 1270
                 // make sure the $dtt_id here is saved in case after the add_relation_to() the autosave replaces it.
1271 1271
                 // We need to do this so we dont' TRASH the parent DTT.
1272
-                $saved_dtts[ $DTM->ID() ] = $DTM;
1272
+                $saved_dtts[$DTM->ID()] = $DTM;
1273 1273
             } else {
1274 1274
                 $DTM = EE_Registry::instance()->load_class(
1275 1275
                     'Datetime',
@@ -1304,15 +1304,15 @@  discard block
 block discarded – undo
1304 1304
         foreach ($data['edit_tickets'] as $row => $tkt) {
1305 1305
             $incoming_date_formats = ['Y-m-d', 'h:i a'];
1306 1306
             $update_prices         = false;
1307
-            $ticket_price          = isset($data['edit_prices'][ $row ][1]['PRC_amount'])
1308
-                ? $data['edit_prices'][ $row ][1]['PRC_amount']
1307
+            $ticket_price          = isset($data['edit_prices'][$row][1]['PRC_amount'])
1308
+                ? $data['edit_prices'][$row][1]['PRC_amount']
1309 1309
                 : 0;
1310 1310
             // trim inputs to ensure any excess whitespace is removed.
1311 1311
             $tkt = array_map('trim', $tkt);
1312 1312
             if (empty($tkt['TKT_start_date'])) {
1313 1313
                 // let's use now in the set timezone.
1314 1314
                 $now                   = new DateTime('now', new DateTimeZone($evtobj->get_timezone()));
1315
-                $tkt['TKT_start_date'] = $now->format($incoming_date_formats[0] . ' ' . $incoming_date_formats[1]);
1315
+                $tkt['TKT_start_date'] = $now->format($incoming_date_formats[0].' '.$incoming_date_formats[1]);
1316 1316
             }
1317 1317
             if (empty($tkt['TKT_end_date'])) {
1318 1318
                 // use the start date of the first datetime
@@ -1350,7 +1350,7 @@  discard block
 block discarded – undo
1350 1350
             // that this ticket didn't removed or added to any datetime in the session but DID have it's items modified.
1351 1351
             // keep in mind that if the TKT has been sold (and we have changed pricing information),
1352 1352
             // then we won't be updating the tkt but instead a new tkt will be created and the old one archived.
1353
-            if (! empty($tkt['TKT_ID'])) {
1353
+            if ( ! empty($tkt['TKT_ID'])) {
1354 1354
                 $TKT = EE_Registry::instance()
1355 1355
                                   ->load_model('Ticket', [$evtobj->get_timezone()])
1356 1356
                                   ->get_one_by_ID($tkt['TKT_ID']);
@@ -1394,7 +1394,7 @@  discard block
 block discarded – undo
1394 1394
                         $TKT->save();
1395 1395
                         // make sure this ticket is still recorded in our saved_tkts
1396 1396
                         // so we don't run it through the regular trash routine.
1397
-                        $saved_tickets[ $TKT->ID() ] = $TKT;
1397
+                        $saved_tickets[$TKT->ID()] = $TKT;
1398 1398
                         // create new ticket that's a copy of the existing except a new id of course
1399 1399
                         // (and not archived) AND has the new TKT_price associated with it.
1400 1400
                         $TKT = clone $TKT;
@@ -1441,9 +1441,9 @@  discard block
 block discarded – undo
1441 1441
             }
1442 1442
             // initially let's add the ticket to the dtt
1443 1443
             $saved_dtt->_add_relation_to($TKT, 'Ticket');
1444
-            $saved_tickets[ $TKT->ID() ] = $TKT;
1444
+            $saved_tickets[$TKT->ID()] = $TKT;
1445 1445
             // add prices to ticket
1446
-            $this->_add_prices_to_ticket($data['edit_prices'][ $row ], $TKT, $update_prices);
1446
+            $this->_add_prices_to_ticket($data['edit_prices'][$row], $TKT, $update_prices);
1447 1447
         }
1448 1448
         // however now we need to handle permanently deleting tickets via the ui.
1449 1449
         //  Keep in mind that the ui does not allow deleting/archiving tickets that have ticket sold.
@@ -1548,13 +1548,13 @@  discard block
 block discarded – undo
1548 1548
     {
1549 1549
         // load formatter helper
1550 1550
         // args for getting related registrations
1551
-        $approved_query_args        = [
1551
+        $approved_query_args = [
1552 1552
             [
1553 1553
                 'REG_deleted' => 0,
1554 1554
                 'STS_ID'      => EEM_Registration::status_id_approved,
1555 1555
             ],
1556 1556
         ];
1557
-        $not_approved_query_args    = [
1557
+        $not_approved_query_args = [
1558 1558
             [
1559 1559
                 'REG_deleted' => 0,
1560 1560
                 'STS_ID'      => EEM_Registration::status_id_not_approved,
@@ -1617,7 +1617,7 @@  discard block
 block discarded – undo
1617 1617
         $publish_box_extra_args['event_editor_overview_add'] = ob_get_clean();
1618 1618
         // load template
1619 1619
         EEH_Template::display_template(
1620
-            EVENTS_TEMPLATE_PATH . 'event_publish_box_extras.template.php',
1620
+            EVENTS_TEMPLATE_PATH.'event_publish_box_extras.template.php',
1621 1621
             $publish_box_extra_args
1622 1622
         );
1623 1623
     }
@@ -1653,7 +1653,7 @@  discard block
 block discarded – undo
1653 1653
         /** @var FeatureFlags $flags */
1654 1654
         $flags = $this->loader->getShared('EventEspresso\core\domain\services\capabilities\FeatureFlags');
1655 1655
         // check if the new EDTR reg options meta box is being used, and if so, don't load the legacy version
1656
-        if (! $use_advanced_editor || ! $flags->featureAllowed('use_reg_options_meta_box')) {
1656
+        if ( ! $use_advanced_editor || ! $flags->featureAllowed('use_reg_options_meta_box')) {
1657 1657
             add_meta_box(
1658 1658
                 'espresso_event_editor_event_options',
1659 1659
                 esc_html__('Event Registration Options', 'event_espresso'),
@@ -1662,7 +1662,7 @@  discard block
 block discarded – undo
1662 1662
                 'side'
1663 1663
             );
1664 1664
         }
1665
-        if (! $use_advanced_editor) {
1665
+        if ( ! $use_advanced_editor) {
1666 1666
             add_meta_box(
1667 1667
                 'espresso_event_editor_tickets',
1668 1668
                 esc_html__('Event Datetime & Ticket', 'event_espresso'),
@@ -1675,7 +1675,7 @@  discard block
 block discarded – undo
1675 1675
             if ($flags->featureAllowed('use_reg_options_meta_box')) {
1676 1676
                 add_action(
1677 1677
                     'add_meta_boxes_espresso_events',
1678
-                    function () {
1678
+                    function() {
1679 1679
                         global $current_screen;
1680 1680
                         remove_meta_box('authordiv', $current_screen, 'normal');
1681 1681
                     },
@@ -1714,7 +1714,7 @@  discard block
 block discarded – undo
1714 1714
             'trash_icon'               => 'ee-lock-icon',
1715 1715
             'disabled'                 => '',
1716 1716
         ];
1717
-        $event_id      = is_object($this->_cpt_model_obj) ? $this->_cpt_model_obj->ID() : null;
1717
+        $event_id = is_object($this->_cpt_model_obj) ? $this->_cpt_model_obj->ID() : null;
1718 1718
         do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1719 1719
         /**
1720 1720
          * 1. Start with retrieving Datetimes
@@ -1737,24 +1737,24 @@  discard block
 block discarded – undo
1737 1737
                     'default_where_conditions' => 'none',
1738 1738
                 ]
1739 1739
             );
1740
-            if (! empty($related_tickets)) {
1740
+            if ( ! empty($related_tickets)) {
1741 1741
                 $template_args['total_ticket_rows'] = count($related_tickets);
1742 1742
                 $row                                = 0;
1743 1743
                 foreach ($related_tickets as $ticket) {
1744
-                    $existing_ticket_ids[]        = $ticket->get('TKT_ID');
1744
+                    $existing_ticket_ids[] = $ticket->get('TKT_ID');
1745 1745
                     $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket, false, $row);
1746 1746
                     $row++;
1747 1747
                 }
1748 1748
             } else {
1749 1749
                 $template_args['total_ticket_rows'] = 1;
1750 1750
                 /** @type EE_Ticket $ticket */
1751
-                $ticket                       = EE_Registry::instance()->load_model('Ticket')->create_default_object();
1751
+                $ticket = EE_Registry::instance()->load_model('Ticket')->create_default_object();
1752 1752
                 $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket);
1753 1753
             }
1754 1754
         } else {
1755 1755
             $template_args['time'] = $times[0];
1756 1756
             /** @type EE_Ticket $ticket */
1757
-            $ticket                       = EE_Registry::instance()->load_model('Ticket')->get_all_default_tickets();
1757
+            $ticket = EE_Registry::instance()->load_model('Ticket')->get_all_default_tickets();
1758 1758
             $template_args['ticket_rows'] .= $this->_get_ticket_row($ticket[1]);
1759 1759
             // NOTE: we're just sending the first default row
1760 1760
             // (decaf can't manage default tickets so this should be sufficient);
@@ -1769,9 +1769,9 @@  discard block
 block discarded – undo
1769 1769
             EE_Registry::instance()->load_model('Ticket')->create_default_object(),
1770 1770
             true
1771 1771
         );
1772
-        $template                                  = apply_filters(
1772
+        $template = apply_filters(
1773 1773
             'FHEE__Events_Admin_Page__ticket_metabox__template',
1774
-            EVENTS_TEMPLATE_PATH . 'event_tickets_metabox_main.template.php'
1774
+            EVENTS_TEMPLATE_PATH.'event_tickets_metabox_main.template.php'
1775 1775
         );
1776 1776
         EEH_Template::display_template($template, $template_args);
1777 1777
     }
@@ -1795,7 +1795,7 @@  discard block
 block discarded – undo
1795 1795
     private function _get_ticket_row($ticket, $skeleton = false, $row = 0)
1796 1796
     {
1797 1797
         $template_args = [
1798
-            'tkt_status_class'    => ' tkt-status-' . $ticket->ticket_status(),
1798
+            'tkt_status_class'    => ' tkt-status-'.$ticket->ticket_status(),
1799 1799
             'tkt_archive_class'   => $ticket->ticket_status() === EE_Ticket::archived && ! $skeleton ? ' tkt-archived'
1800 1800
                 : '',
1801 1801
             'ticketrow'           => $skeleton ? 'TICKETNUM' : $row,
@@ -1807,10 +1807,10 @@  discard block
 block discarded – undo
1807 1807
             'TKT_qty'             => $ticket->get_pretty('TKT_qty', 'input'),
1808 1808
             'edit_ticketrow_name' => $skeleton ? 'TICKETNAMEATTR' : 'edit_tickets',
1809 1809
             'TKT_sold'            => $skeleton ? 0 : $ticket->get('TKT_sold'),
1810
-            'trash_icon'          => ($skeleton || (! empty($ticket) && ! $ticket->get('TKT_deleted')))
1811
-                                     && (! empty($ticket) && $ticket->get('TKT_sold') === 0)
1810
+            'trash_icon'          => ($skeleton || ( ! empty($ticket) && ! $ticket->get('TKT_deleted')))
1811
+                                     && ( ! empty($ticket) && $ticket->get('TKT_sold') === 0)
1812 1812
                 ? 'trash-icon dashicons dashicons-post-trash clickable' : 'ee-lock-icon',
1813
-            'disabled'            => $skeleton || (! empty($ticket) && ! $ticket->get('TKT_deleted')) ? ''
1813
+            'disabled'            => $skeleton || ( ! empty($ticket) && ! $ticket->get('TKT_deleted')) ? ''
1814 1814
                 : ' disabled=disabled',
1815 1815
         ];
1816 1816
         $price         = $ticket->ID() !== 0
@@ -1837,7 +1837,7 @@  discard block
 block discarded – undo
1837 1837
                     ['order_by' => ['DTT_EVT_start' => 'ASC']]
1838 1838
                 )
1839 1839
                 : null;
1840
-            if (! empty($earliest_dtt)) {
1840
+            if ( ! empty($earliest_dtt)) {
1841 1841
                 $template_args['TKT_end_date'] = $earliest_dtt->get_datetime('DTT_EVT_start', 'Y-m-d', 'h:i a');
1842 1842
             } else {
1843 1843
                 $template_args['TKT_end_date'] = date(
@@ -1849,7 +1849,7 @@  discard block
 block discarded – undo
1849 1849
         $template_args = array_merge($template_args, $price_args);
1850 1850
         $template      = apply_filters(
1851 1851
             'FHEE__Events_Admin_Page__get_ticket_row__template',
1852
-            EVENTS_TEMPLATE_PATH . 'event_tickets_metabox_ticket_row.template.php',
1852
+            EVENTS_TEMPLATE_PATH.'event_tickets_metabox_ticket_row.template.php',
1853 1853
             $ticket
1854 1854
         );
1855 1855
         return EEH_Template::display_template($template, $template_args, true);
@@ -1862,7 +1862,7 @@  discard block
 block discarded – undo
1862 1862
      */
1863 1863
     public function registration_options_meta_box()
1864 1864
     {
1865
-        $yes_no_values             = [
1865
+        $yes_no_values = [
1866 1866
             ['id' => true, 'text' => esc_html__('Yes', 'event_espresso')],
1867 1867
             ['id' => false, 'text' => esc_html__('No', 'event_espresso')],
1868 1868
         ];
@@ -1883,12 +1883,12 @@  discard block
 block discarded – undo
1883 1883
             $default_reg_status_values,
1884 1884
             $this->_cpt_model_obj->default_registration_status()
1885 1885
         );
1886
-        $template_args['display_description']             = EEH_Form_Fields::select_input(
1886
+        $template_args['display_description'] = EEH_Form_Fields::select_input(
1887 1887
             'display_desc',
1888 1888
             $yes_no_values,
1889 1889
             $this->_cpt_model_obj->display_description()
1890 1890
         );
1891
-        $template_args['display_ticket_selector']         = EEH_Form_Fields::select_input(
1891
+        $template_args['display_ticket_selector'] = EEH_Form_Fields::select_input(
1892 1892
             'display_ticket_selector',
1893 1893
             $yes_no_values,
1894 1894
             $this->_cpt_model_obj->display_ticket_selector(),
@@ -1904,7 +1904,7 @@  discard block
 block discarded – undo
1904 1904
             $default_reg_status_values
1905 1905
         );
1906 1906
         EEH_Template::display_template(
1907
-            EVENTS_TEMPLATE_PATH . 'event_registration_options.template.php',
1907
+            EVENTS_TEMPLATE_PATH.'event_registration_options.template.php',
1908 1908
             $template_args
1909 1909
         );
1910 1910
     }
@@ -1934,7 +1934,7 @@  discard block
 block discarded – undo
1934 1934
     {
1935 1935
         $EEME    = $this->_event_model();
1936 1936
         $offset  = ($current_page - 1) * $per_page;
1937
-        $limit   = $count ? null : $offset . ',' . $per_page;
1937
+        $limit   = $count ? null : $offset.','.$per_page;
1938 1938
         $orderby = isset($this->_req_data['orderby']) ? $this->_req_data['orderby'] : 'EVT_ID';
1939 1939
         $order   = isset($this->_req_data['order']) ? $this->_req_data['order'] : 'DESC';
1940 1940
         if (isset($this->_req_data['month_range'])) {
@@ -1963,7 +1963,7 @@  discard block
 block discarded – undo
1963 1963
         // categories?
1964 1964
         $category = isset($this->_req_data['EVT_CAT']) && $this->_req_data['EVT_CAT'] > 0
1965 1965
             ? $this->_req_data['EVT_CAT'] : null;
1966
-        if (! empty($category)) {
1966
+        if ( ! empty($category)) {
1967 1967
             $where['Term_Taxonomy.taxonomy'] = EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY;
1968 1968
             $where['Term_Taxonomy.term_id']  = $category;
1969 1969
         }
@@ -1971,10 +1971,10 @@  discard block
 block discarded – undo
1971 1971
         $start_formats = EEM_Datetime::instance()->get_formats_for('DTT_EVT_start');
1972 1972
         if (isset($this->_req_data['month_range']) && $this->_req_data['month_range'] !== '') {
1973 1973
             $DateTime = new DateTime(
1974
-                $year_r . '-' . $month_r . '-01 00:00:00',
1974
+                $year_r.'-'.$month_r.'-01 00:00:00',
1975 1975
                 new DateTimeZone('UTC')
1976 1976
             );
1977
-            $start    = $DateTime->getTimestamp();
1977
+            $start = $DateTime->getTimestamp();
1978 1978
             // set the datetime to be the end of the month
1979 1979
             $DateTime->setDate(
1980 1980
                 $year_r,
@@ -1999,7 +1999,7 @@  discard block
 block discarded – undo
1999 1999
                                                         ->format(implode(' ', $start_formats));
2000 2000
             $where['Datetime.DTT_EVT_start'] = ['BETWEEN', [$start, $end]];
2001 2001
         }
2002
-        if (! EE_Registry::instance()->CAP->current_user_can('ee_read_others_events', 'get_events')) {
2002
+        if ( ! EE_Registry::instance()->CAP->current_user_can('ee_read_others_events', 'get_events')) {
2003 2003
             $where['EVT_wp_user'] = get_current_user_id();
2004 2004
         } elseif (
2005 2005
             ! isset($where['status'])
@@ -2023,7 +2023,7 @@  discard block
 block discarded – undo
2023 2023
         }
2024 2024
         // search query handling
2025 2025
         if (isset($this->_req_data['s'])) {
2026
-            $search_string = '%' . $this->_req_data['s'] . '%';
2026
+            $search_string = '%'.$this->_req_data['s'].'%';
2027 2027
             $where['OR']   = [
2028 2028
                 'EVT_name'       => ['LIKE', $search_string],
2029 2029
                 'EVT_desc'       => ['LIKE', $search_string],
@@ -2139,7 +2139,7 @@  discard block
 block discarded – undo
2139 2139
             // clean status
2140 2140
             $event_status = sanitize_key($event_status);
2141 2141
             // grab status
2142
-            if (! empty($event_status)) {
2142
+            if ( ! empty($event_status)) {
2143 2143
                 $success = $this->_change_event_status($EVT_ID, $event_status);
2144 2144
             } else {
2145 2145
                 $success = false;
@@ -2181,7 +2181,7 @@  discard block
 block discarded – undo
2181 2181
         // clean status
2182 2182
         $event_status = sanitize_key($event_status);
2183 2183
         // grab status
2184
-        if (! empty($event_status)) {
2184
+        if ( ! empty($event_status)) {
2185 2185
             $success = true;
2186 2186
             // determine the event id and set to array.
2187 2187
             $EVT_IDs = isset($this->_req_data['EVT_IDs']) ? (array) $this->_req_data['EVT_IDs'] : [];
@@ -2233,7 +2233,7 @@  discard block
 block discarded – undo
2233 2233
     private function _change_event_status($EVT_ID = 0, $event_status = '')
2234 2234
     {
2235 2235
         // grab event id
2236
-        if (! $EVT_ID) {
2236
+        if ( ! $EVT_ID) {
2237 2237
             $msg = esc_html__(
2238 2238
                 'An error occurred. No Event ID or an invalid Event ID was received.',
2239 2239
                 'event_espresso'
@@ -2306,7 +2306,7 @@  discard block
 block discarded – undo
2306 2306
      */
2307 2307
     protected function getModelObjNodeGroupPersister()
2308 2308
     {
2309
-        if (! $this->model_obj_node_group_persister instanceof NodeGroupDao) {
2309
+        if ( ! $this->model_obj_node_group_persister instanceof NodeGroupDao) {
2310 2310
             $this->model_obj_node_group_persister =
2311 2311
                 $this->getLoader()->load('\EventEspresso\core\services\orm\tree_traversal\NodeGroupDao');
2312 2312
         }
@@ -2341,7 +2341,7 @@  discard block
 block discarded – undo
2341 2341
             ],
2342 2342
             $this->_admin_base_url
2343 2343
         );
2344
-        $event_ids         = array_map(
2344
+        $event_ids = array_map(
2345 2345
             'intval',
2346 2346
             $event_ids
2347 2347
         );
@@ -2590,7 +2590,7 @@  discard block
 block discarded – undo
2590 2590
                                                 . esc_html__(
2591 2591
                                                     'Template Settings 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. Template Settings allow you to configure some of the appearance options for both the Event List and Event Details pages.',
2592 2592
                                                     'event_espresso'
2593
-                                                ) . '</strong>';
2593
+                                                ).'</strong>';
2594 2594
         $this->display_admin_caf_preview_page('template_settings_tab');
2595 2595
     }
2596 2596
 
@@ -2610,12 +2610,12 @@  discard block
 block discarded – undo
2610 2610
         // set default category object
2611 2611
         $this->_set_empty_category_object();
2612 2612
         // only set if we've got an id
2613
-        if (! isset($this->_req_data['EVT_CAT_ID'])) {
2613
+        if ( ! isset($this->_req_data['EVT_CAT_ID'])) {
2614 2614
             return;
2615 2615
         }
2616 2616
         $category_id = absint($this->_req_data['EVT_CAT_ID']);
2617 2617
         $term        = get_term($category_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY);
2618
-        if (! empty($term)) {
2618
+        if ( ! empty($term)) {
2619 2619
             $this->_category->category_name       = $term->name;
2620 2620
             $this->_category->category_identifier = $term->slug;
2621 2621
             $this->_category->category_desc       = $term->description;
@@ -2723,7 +2723,7 @@  discard block
 block discarded – undo
2723 2723
             $category_select_values,
2724 2724
             $this->_category->parent
2725 2725
         );
2726
-        $template_args   = [
2726
+        $template_args = [
2727 2727
             'category'                 => $this->_category,
2728 2728
             'category_select'          => $category_select,
2729 2729
             'unique_id_info_help_link' => $this->_get_help_tab_link('unique_id_info'),
@@ -2731,7 +2731,7 @@  discard block
 block discarded – undo
2731 2731
             'disable'                  => '',
2732 2732
             'disabled_message'         => false,
2733 2733
         ];
2734
-        $template        = EVENTS_TEMPLATE_PATH . 'event_category_details.template.php';
2734
+        $template = EVENTS_TEMPLATE_PATH.'event_category_details.template.php';
2735 2735
         return EEH_Template::display_template($template, $template_args, true);
2736 2736
     }
2737 2737
 
@@ -2820,7 +2820,7 @@  discard block
 block discarded – undo
2820 2820
         $insert_ids = $update
2821 2821
             ? wp_update_term($cat_id, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY, $term_args)
2822 2822
             : wp_insert_term($category_name, EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY, $term_args);
2823
-        if (! is_array($insert_ids)) {
2823
+        if ( ! is_array($insert_ids)) {
2824 2824
             $msg = esc_html__(
2825 2825
                 'An error occurred and the category has not been saved to the database.',
2826 2826
                 'event_espresso'
@@ -2855,7 +2855,7 @@  discard block
 block discarded – undo
2855 2855
         $limit   = ($current_page - 1) * $per_page;
2856 2856
         $where   = ['taxonomy' => EEM_CPT_Base::EVENT_CATEGORY_TAXONOMY];
2857 2857
         if (isset($this->_req_data['s'])) {
2858
-            $sstr        = '%' . $this->_req_data['s'] . '%';
2858
+            $sstr        = '%'.$this->_req_data['s'].'%';
2859 2859
             $where['OR'] = [
2860 2860
                 'Term.name'   => ['LIKE', $sstr],
2861 2861
                 'description' => ['LIKE', $sstr],
@@ -2864,7 +2864,7 @@  discard block
 block discarded – undo
2864 2864
         $query_params = [
2865 2865
             $where,
2866 2866
             'order_by'   => [$orderby => $order],
2867
-            'limit'      => $limit . ',' . $per_page,
2867
+            'limit'      => $limit.','.$per_page,
2868 2868
             'force_join' => ['Term'],
2869 2869
         ];
2870 2870
         return $count
Please login to merge, or discard this patch.
admin_pages/messages/Messages_Admin_Page.core.php 1 patch
Indentation   +4555 added lines, -4555 removed lines patch added patch discarded remove patch
@@ -19,2650 +19,2650 @@  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 (
243
-            empty($messenger_options)
244
-            || ! is_array($messenger_options)
245
-            || count($messenger_options) === 1
246
-        ) {
247
-            return '';
248
-        }
249
-        // merge in default
250
-        $messenger_options = array_merge(
251
-            array('none_selected' => esc_html__('Show All Messengers', 'event_espresso')),
252
-            $messenger_options
253
-        );
254
-        $input = new EE_Select_Input(
255
-            $messenger_options,
256
-            array(
257
-                'html_name'  => 'ee_messenger_filter_by',
258
-                'html_id'    => 'ee_messenger_filter_by',
259
-                'html_class' => 'wide',
260
-                'default'    => isset($this->_req_data['ee_messenger_filter_by'])
261
-                    ? sanitize_title($this->_req_data['ee_messenger_filter_by'])
262
-                    : 'none_selected',
263
-            )
264
-        );
265
-
266
-        return $input->get_html_for_input();
267
-    }
268
-
269
-
270
-    /**
271
-     * Generate select input with provided message type options array.
272
-     *
273
-     * @param array $message_type_options Array of message types indexed by message type slug, and values are the
274
-     *                                    message type labels
275
-     * @return string
276
-     * @throws EE_Error
277
-     */
278
-    public function get_message_types_select_input($message_type_options)
279
-    {
280
-        // if empty or count of options is 1 then just return an empty string
281
-        if (
282
-            empty($message_type_options)
283
-            || ! is_array($message_type_options)
284
-            || count($message_type_options) === 1
285
-        ) {
286
-            return '';
287
-        }
288
-        // merge in default
289
-        $message_type_options = array_merge(
290
-            array('none_selected' => esc_html__('Show All Message Types', 'event_espresso')),
291
-            $message_type_options
292
-        );
293
-        $input = new EE_Select_Input(
294
-            $message_type_options,
295
-            array(
296
-                'html_name'  => 'ee_message_type_filter_by',
297
-                'html_id'    => 'ee_message_type_filter_by',
298
-                'html_class' => 'wide',
299
-                'default'    => isset($this->_req_data['ee_message_type_filter_by'])
300
-                    ? sanitize_title($this->_req_data['ee_message_type_filter_by'])
301
-                    : 'none_selected',
302
-            )
303
-        );
304
-
305
-        return $input->get_html_for_input();
306
-    }
307
-
308
-
309
-    /**
310
-     * Generate select input with provide message type contexts array.
311
-     *
312
-     * @param array $context_options Array of message type contexts indexed by context slug, and values are the
313
-     *                               context label.
314
-     * @return string
315
-     * @throws EE_Error
316
-     */
317
-    public function get_contexts_for_message_types_select_input($context_options)
318
-    {
319
-        // if empty or count of options is one then just return empty string
320
-        if (
321
-            empty($context_options)
322
-            || ! is_array($context_options)
323
-            || count($context_options) === 1
324
-        ) {
325
-            return '';
326
-        }
327
-        // merge in default
328
-        $context_options = array_merge(
329
-            array('none_selected' => esc_html__('Show all Contexts', 'event_espresso')),
330
-            $context_options
331
-        );
332
-        $input = new EE_Select_Input(
333
-            $context_options,
334
-            array(
335
-                'html_name'  => 'ee_context_filter_by',
336
-                'html_id'    => 'ee_context_filter_by',
337
-                'html_class' => 'wide',
338
-                'default'    => isset($this->_req_data['ee_context_filter_by'])
339
-                    ? sanitize_title($this->_req_data['ee_context_filter_by'])
340
-                    : 'none_selected',
341
-            )
342
-        );
343
-
344
-        return $input->get_html_for_input();
345
-    }
346
-
347
-
348
-    protected function _ajax_hooks()
349
-    {
350
-        add_action('wp_ajax_activate_messenger', array($this, 'activate_messenger_toggle'));
351
-        add_action('wp_ajax_activate_mt', array($this, 'activate_mt_toggle'));
352
-        add_action('wp_ajax_ee_msgs_save_settings', array($this, 'save_settings'));
353
-        add_action('wp_ajax_ee_msgs_update_mt_form', array($this, 'update_mt_form'));
354
-        add_action('wp_ajax_switch_template_pack', array($this, 'switch_template_pack'));
355
-        add_action('wp_ajax_toggle_context_template', array($this, 'toggle_context_template'));
356
-    }
357
-
358
-
359
-    protected function _define_page_props()
360
-    {
361
-        $this->_admin_page_title = $this->page_label;
362
-        $this->_labels = array(
363
-            'buttons'    => array(
364
-                'add'    => esc_html__('Add New Message Template', 'event_espresso'),
365
-                'edit'   => esc_html__('Edit Message Template', 'event_espresso'),
366
-                'delete' => esc_html__('Delete Message Template', 'event_espresso'),
367
-            ),
368
-            'publishbox' => esc_html__('Update Actions', 'event_espresso'),
369
-        );
370
-    }
371
-
372
-
373
-    /**
374
-     *        an array for storing key => value pairs of request actions and their corresponding methods
375
-     *
376
-     * @access protected
377
-     * @return void
378
-     */
379
-    protected function _set_page_routes()
380
-    {
381
-        $grp_id = ! empty($this->_req_data['GRP_ID']) && ! is_array($this->_req_data['GRP_ID'])
382
-            ? $this->_req_data['GRP_ID']
383
-            : 0;
384
-        $grp_id = empty($grp_id) && ! empty($this->_req_data['id'])
385
-            ? $this->_req_data['id']
386
-            : $grp_id;
387
-        $msg_id = ! empty($this->_req_data['MSG_ID']) && ! is_array($this->_req_data['MSG_ID'])
388
-            ? $this->_req_data['MSG_ID']
389
-            : 0;
390
-
391
-        $this->_page_routes = array(
392
-            'default'                          => array(
393
-                'func'       => '_message_queue_list_table',
394
-                'capability' => 'ee_read_global_messages',
395
-            ),
396
-            'global_mtps'                      => array(
397
-                'func'       => '_ee_default_messages_overview_list_table',
398
-                'capability' => 'ee_read_global_messages',
399
-            ),
400
-            'custom_mtps'                      => array(
401
-                'func'       => '_custom_mtps_preview',
402
-                'capability' => 'ee_read_messages',
403
-            ),
404
-            'add_new_message_template'         => array(
405
-                'func'       => '_add_message_template',
406
-                'capability' => 'ee_edit_messages',
407
-                'noheader'   => true,
408
-            ),
409
-            'edit_message_template'            => array(
410
-                'func'       => '_edit_message_template',
411
-                'capability' => 'ee_edit_message',
412
-                'obj_id'     => $grp_id,
413
-            ),
414
-            'preview_message'                  => array(
415
-                'func'               => '_preview_message',
416
-                'capability'         => 'ee_read_message',
417
-                'obj_id'             => $grp_id,
418
-                'noheader'           => true,
419
-                'headers_sent_route' => 'display_preview_message',
420
-            ),
421
-            'display_preview_message'          => array(
422
-                'func'       => '_display_preview_message',
423
-                'capability' => 'ee_read_message',
424
-                'obj_id'     => $grp_id,
425
-            ),
426
-            'insert_message_template'          => array(
427
-                'func'       => '_insert_or_update_message_template',
428
-                'capability' => 'ee_edit_messages',
429
-                'args'       => array('new_template' => true),
430
-                'noheader'   => true,
431
-            ),
432
-            'update_message_template'          => array(
433
-                'func'       => '_insert_or_update_message_template',
434
-                'capability' => 'ee_edit_message',
435
-                'obj_id'     => $grp_id,
436
-                'args'       => array('new_template' => false),
437
-                'noheader'   => true,
438
-            ),
439
-            'trash_message_template'           => array(
440
-                'func'       => '_trash_or_restore_message_template',
441
-                'capability' => 'ee_delete_message',
442
-                'obj_id'     => $grp_id,
443
-                'args'       => array('trash' => true, 'all' => true),
444
-                'noheader'   => true,
445
-            ),
446
-            'trash_message_template_context'   => array(
447
-                'func'       => '_trash_or_restore_message_template',
448
-                'capability' => 'ee_delete_message',
449
-                'obj_id'     => $grp_id,
450
-                'args'       => array('trash' => true),
451
-                'noheader'   => true,
452
-            ),
453
-            'restore_message_template'         => array(
454
-                'func'       => '_trash_or_restore_message_template',
455
-                'capability' => 'ee_delete_message',
456
-                'obj_id'     => $grp_id,
457
-                'args'       => array('trash' => false, 'all' => true),
458
-                'noheader'   => true,
459
-            ),
460
-            'restore_message_template_context' => array(
461
-                'func'       => '_trash_or_restore_message_template',
462
-                'capability' => 'ee_delete_message',
463
-                'obj_id'     => $grp_id,
464
-                'args'       => array('trash' => false),
465
-                'noheader'   => true,
466
-            ),
467
-            'delete_message_template'          => array(
468
-                'func'       => '_delete_message_template',
469
-                'capability' => 'ee_delete_message',
470
-                'obj_id'     => $grp_id,
471
-                'noheader'   => true,
472
-            ),
473
-            'reset_to_default'                 => array(
474
-                'func'       => '_reset_to_default_template',
475
-                'capability' => 'ee_edit_message',
476
-                'obj_id'     => $grp_id,
477
-                'noheader'   => true,
478
-            ),
479
-            'settings'                         => array(
480
-                'func'       => '_settings',
481
-                'capability' => 'manage_options',
482
-            ),
483
-            'update_global_settings'           => array(
484
-                'func'       => '_update_global_settings',
485
-                'capability' => 'manage_options',
486
-                'noheader'   => true,
487
-            ),
488
-            'generate_now'                     => array(
489
-                'func'       => '_generate_now',
490
-                'capability' => 'ee_send_message',
491
-                'noheader'   => true,
492
-            ),
493
-            'generate_and_send_now'            => array(
494
-                'func'       => '_generate_and_send_now',
495
-                'capability' => 'ee_send_message',
496
-                'noheader'   => true,
497
-            ),
498
-            'queue_for_resending'              => array(
499
-                'func'       => '_queue_for_resending',
500
-                'capability' => 'ee_send_message',
501
-                'noheader'   => true,
502
-            ),
503
-            'send_now'                         => array(
504
-                'func'       => '_send_now',
505
-                'capability' => 'ee_send_message',
506
-                'noheader'   => true,
507
-            ),
508
-            'delete_ee_message'                => array(
509
-                'func'       => '_delete_ee_messages',
510
-                'capability' => 'ee_delete_messages',
511
-                'noheader'   => true,
512
-            ),
513
-            'delete_ee_messages'               => array(
514
-                'func'       => '_delete_ee_messages',
515
-                'capability' => 'ee_delete_messages',
516
-                'noheader'   => true,
517
-                'obj_id'     => $msg_id,
518
-            ),
519
-        );
520
-    }
521
-
522
-
523
-    protected function _set_page_config()
524
-    {
525
-        $this->_page_config = array(
526
-            'default'                  => array(
527
-                'nav'           => array(
528
-                    'label' => esc_html__('Message Activity', 'event_espresso'),
529
-                    'order' => 10,
530
-                ),
531
-                'list_table'    => 'EE_Message_List_Table',
532
-                // 'qtips' => array( 'EE_Message_List_Table_Tips' ),
533
-                'require_nonce' => false,
534
-            ),
535
-            'global_mtps'              => array(
536
-                'nav'           => array(
537
-                    'label' => esc_html__('Default Message Templates', 'event_espresso'),
538
-                    'order' => 20,
539
-                ),
540
-                'list_table'    => 'Messages_Template_List_Table',
541
-                'help_tabs'     => array(
542
-                    'messages_overview_help_tab'                                => array(
543
-                        'title'    => esc_html__('Messages Overview', 'event_espresso'),
544
-                        'filename' => 'messages_overview',
545
-                    ),
546
-                    'messages_overview_messages_table_column_headings_help_tab' => array(
547
-                        'title'    => esc_html__('Messages Table Column Headings', 'event_espresso'),
548
-                        'filename' => 'messages_overview_table_column_headings',
549
-                    ),
550
-                    'messages_overview_messages_filters_help_tab'               => array(
551
-                        'title'    => esc_html__('Message Filters', 'event_espresso'),
552
-                        'filename' => 'messages_overview_filters',
553
-                    ),
554
-                    'messages_overview_messages_views_help_tab'                 => array(
555
-                        'title'    => esc_html__('Message Views', 'event_espresso'),
556
-                        'filename' => 'messages_overview_views',
557
-                    ),
558
-                    'message_overview_message_types_help_tab'                   => array(
559
-                        'title'    => esc_html__('Message Types', 'event_espresso'),
560
-                        'filename' => 'messages_overview_types',
561
-                    ),
562
-                    'messages_overview_messengers_help_tab'                     => array(
563
-                        'title'    => esc_html__('Messengers', 'event_espresso'),
564
-                        'filename' => 'messages_overview_messengers',
565
-                    ),
566
-                ),
567
-                // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
568
-                // 'help_tour'     => array('Messages_Overview_Help_Tour'),
569
-                'require_nonce' => false,
570
-            ),
571
-            'custom_mtps'              => array(
572
-                'nav'           => array(
573
-                    'label' => esc_html__('Custom Message Templates', 'event_espresso'),
574
-                    'order' => 30,
575
-                ),
576
-                'help_tabs'     => array(),
577
-                // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
578
-                // 'help_tour'     => array(),
579
-                'require_nonce' => false,
580
-            ),
581
-            'add_new_message_template' => array(
582
-                'nav'           => array(
583
-                    'label'      => esc_html__('Add New Message Templates', 'event_espresso'),
584
-                    'order'      => 5,
585
-                    'persistent' => false,
586
-                ),
587
-                'require_nonce' => false,
588
-            ),
589
-            'edit_message_template'    => array(
590
-                'labels'        => array(
591
-                    'buttons'    => array(
592
-                        'reset' => esc_html__('Reset Templates', 'event_espresso'),
593
-                    ),
594
-                    'publishbox' => esc_html__('Update Actions', 'event_espresso'),
595
-                ),
596
-                'nav'           => array(
597
-                    'label'      => esc_html__('Edit Message Templates', 'event_espresso'),
598
-                    'order'      => 5,
599
-                    'persistent' => false,
600
-                    'url'        => '',
601
-                ),
602
-                'metaboxes'     => array('_publish_post_box', '_register_edit_meta_boxes'),
603
-                'has_metaboxes' => true,
604
-                // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
605
-                // 'help_tour'     => array('Message_Templates_Edit_Help_Tour'),
606
-                'help_tabs'     => array(
607
-                    'edit_message_template'            => array(
608
-                        'title'    => esc_html__('Message Template Editor', 'event_espresso'),
609
-                        'callback' => 'edit_message_template_help_tab',
610
-                    ),
611
-                    'message_templates_help_tab'       => array(
612
-                        'title'    => esc_html__('Message Templates', 'event_espresso'),
613
-                        'filename' => 'messages_templates',
614
-                    ),
615
-                    'message_template_shortcodes'      => array(
616
-                        'title'    => esc_html__('Message Shortcodes', 'event_espresso'),
617
-                        'callback' => 'message_template_shortcodes_help_tab',
618
-                    ),
619
-                    'message_preview_help_tab'         => array(
620
-                        'title'    => esc_html__('Message Preview', 'event_espresso'),
621
-                        'filename' => 'messages_preview',
622
-                    ),
623
-                    'messages_overview_other_help_tab' => array(
624
-                        'title'    => esc_html__('Messages Other', 'event_espresso'),
625
-                        'filename' => 'messages_overview_other',
626
-                    ),
627
-                ),
628
-                'require_nonce' => false,
629
-            ),
630
-            'display_preview_message'  => array(
631
-                'nav'           => array(
632
-                    'label'      => esc_html__('Message Preview', 'event_espresso'),
633
-                    'order'      => 5,
634
-                    'url'        => '',
635
-                    'persistent' => false,
636
-                ),
637
-                'help_tabs'     => array(
638
-                    'preview_message' => array(
639
-                        'title'    => esc_html__('About Previews', 'event_espresso'),
640
-                        'callback' => 'preview_message_help_tab',
641
-                    ),
642
-                ),
643
-                'require_nonce' => false,
644
-            ),
645
-            'settings'                 => array(
646
-                'nav'           => array(
647
-                    'label' => esc_html__('Settings', 'event_espresso'),
648
-                    'order' => 40,
649
-                ),
650
-                'metaboxes'     => array('_messages_settings_metaboxes'),
651
-                'help_tabs'     => array(
652
-                    'messages_settings_help_tab'               => array(
653
-                        'title'    => esc_html__('Messages Settings', 'event_espresso'),
654
-                        'filename' => 'messages_settings',
655
-                    ),
656
-                    'messages_settings_message_types_help_tab' => array(
657
-                        'title'    => esc_html__('Activating / Deactivating Message Types', 'event_espresso'),
658
-                        'filename' => 'messages_settings_message_types',
659
-                    ),
660
-                    'messages_settings_messengers_help_tab'    => array(
661
-                        'title'    => esc_html__('Activating / Deactivating Messengers', 'event_espresso'),
662
-                        'filename' => 'messages_settings_messengers',
663
-                    ),
664
-                ),
665
-                // disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
666
-                // 'help_tour'     => array('Messages_Settings_Help_Tour'),
667
-                'require_nonce' => false,
668
-            ),
669
-        );
670
-    }
671
-
672
-
673
-    protected function _add_screen_options()
674
-    {
675
-        // todo
676
-    }
677
-
678
-
679
-    protected function _add_screen_options_global_mtps()
680
-    {
681
-        /**
682
-         * Note: the reason for the value swap here on $this->_admin_page_title is because $this->_per_page_screen_options
683
-         * uses the $_admin_page_title property and we want different outputs in the different spots.
684
-         */
685
-        $page_title = $this->_admin_page_title;
686
-        $this->_admin_page_title = esc_html__('Global Message Templates', 'event_espresso');
687
-        $this->_per_page_screen_option();
688
-        $this->_admin_page_title = $page_title;
689
-    }
690
-
691
-
692
-    protected function _add_screen_options_default()
693
-    {
694
-        $this->_admin_page_title = esc_html__('Message Activity', 'event_espresso');
695
-        $this->_per_page_screen_option();
696
-    }
697
-
698
-
699
-    // none of the below group are currently used for Messages
700
-    protected function _add_feature_pointers()
701
-    {
702
-    }
703
-
704
-    public function admin_init()
705
-    {
706
-    }
707
-
708
-    public function admin_notices()
709
-    {
710
-    }
711
-
712
-    public function admin_footer_scripts()
713
-    {
714
-    }
715
-
716
-
717
-    public function messages_help_tab()
718
-    {
719
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_help_tab.template.php');
720
-    }
721
-
722
-
723
-    public function messengers_help_tab()
724
-    {
725
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messenger_help_tab.template.php');
726
-    }
727
-
728
-
729
-    public function message_types_help_tab()
730
-    {
731
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_type_help_tab.template.php');
732
-    }
733
-
734
-
735
-    public function messages_overview_help_tab()
736
-    {
737
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_overview_help_tab.template.php');
738
-    }
739
-
740
-
741
-    public function message_templates_help_tab()
742
-    {
743
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_templates_help_tab.template.php');
744
-    }
745
-
746
-
747
-    public function edit_message_template_help_tab()
748
-    {
749
-        $args['img1'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/editor.png' . '" alt="'
750
-                        . esc_attr__('Editor Title', 'event_espresso')
751
-                        . '" />';
752
-        $args['img2'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/switch-context.png' . '" alt="'
753
-                        . esc_attr__('Context Switcher and Preview', 'event_espresso')
754
-                        . '" />';
755
-        $args['img3'] = '<img class="left" src="' . EE_MSG_ASSETS_URL . 'images/form-fields.png' . '" alt="'
756
-                        . esc_attr__('Message Template Form Fields', 'event_espresso')
757
-                        . '" />';
758
-        $args['img4'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/shortcodes-metabox.png' . '" alt="'
759
-                        . esc_attr__('Shortcodes Metabox', 'event_espresso')
760
-                        . '" />';
761
-        $args['img5'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/publish-meta-box.png' . '" alt="'
762
-                        . esc_attr__('Publish Metabox', 'event_espresso')
763
-                        . '" />';
764
-        EEH_Template::display_template(
765
-            EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_templates_editor_help_tab.template.php',
766
-            $args
767
-        );
768
-    }
769
-
770
-
771
-    public function message_template_shortcodes_help_tab()
772
-    {
773
-        $this->_set_shortcodes();
774
-        $args['shortcodes'] = $this->_shortcodes;
775
-        EEH_Template::display_template(
776
-            EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_shortcodes_help_tab.template.php',
777
-            $args
778
-        );
779
-    }
780
-
781
-
782
-    public function preview_message_help_tab()
783
-    {
784
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_preview_help_tab.template.php');
785
-    }
786
-
787
-
788
-    public function settings_help_tab()
789
-    {
790
-        $args['img1'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-active.png'
791
-                        . '" alt="' . esc_attr__('Active Email Tab', 'event_espresso') . '" />';
792
-        $args['img2'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-inactive.png'
793
-                        . '" alt="' . esc_attr__('Inactive Email Tab', 'event_espresso') . '" />';
794
-        $args['img3'] = '<div class="switch">'
795
-                        . '<input class="ee-on-off-toggle ee-toggle-round-flat"'
796
-                        . ' type="checkbox" checked="checked">'
797
-                        . '<label for="ee-on-off-toggle-on"></label>'
798
-                        . '</div>';
799
-        $args['img4'] = '<div class="switch">'
800
-                        . '<input class="ee-on-off-toggle ee-toggle-round-flat"'
801
-                        . ' type="checkbox">'
802
-                        . '<label for="ee-on-off-toggle-on"></label>'
803
-                        . '</div>';
804
-        EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_settings_help_tab.template.php', $args);
805
-    }
806
-
807
-
808
-    public function load_scripts_styles()
809
-    {
810
-        wp_register_style('espresso_ee_msg', EE_MSG_ASSETS_URL . 'ee_message_admin.css', EVENT_ESPRESSO_VERSION);
811
-        wp_enqueue_style('espresso_ee_msg');
812
-
813
-        wp_register_script(
814
-            'ee-messages-settings',
815
-            EE_MSG_ASSETS_URL . 'ee-messages-settings.js',
816
-            array('jquery-ui-droppable', 'ee-serialize-full-array'),
817
-            EVENT_ESPRESSO_VERSION,
818
-            true
819
-        );
820
-        wp_register_script(
821
-            'ee-msg-list-table-js',
822
-            EE_MSG_ASSETS_URL . 'ee_message_admin_list_table.js',
823
-            array('ee-dialog'),
824
-            EVENT_ESPRESSO_VERSION
825
-        );
826
-    }
827
-
828
-
829
-    public function load_scripts_styles_default()
830
-    {
831
-        wp_enqueue_script('ee-msg-list-table-js');
832
-    }
833
-
834
-
835
-    public function wp_editor_css($mce_css)
836
-    {
837
-        // if we're on the edit_message_template route
838
-        if ($this->_req_action === 'edit_message_template' && $this->_active_messenger instanceof EE_messenger) {
839
-            $message_type_name = $this->_active_message_type_name;
840
-
841
-            // we're going to REPLACE the existing mce css
842
-            // we need to get the css file location from the active messenger
843
-            $mce_css = $this->_active_messenger->get_variation(
844
-                $this->_template_pack,
845
-                $message_type_name,
846
-                true,
847
-                'wpeditor',
848
-                $this->_variation
849
-            );
850
-        }
851
-
852
-        return $mce_css;
853
-    }
854
-
855
-
856
-    public function load_scripts_styles_edit_message_template()
857
-    {
858
-
859
-        $this->_set_shortcodes();
860
-
861
-        EE_Registry::$i18n_js_strings['confirm_default_reset'] = sprintf(
862
-            esc_html__(
863
-                '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.',
864
-                'event_espresso'
865
-            ),
866
-            $this->_message_template_group->messenger_obj()->label['singular'],
867
-            $this->_message_template_group->message_type_obj()->label['singular']
868
-        );
869
-        EE_Registry::$i18n_js_strings['confirm_switch_template_pack'] = esc_html__(
870
-            '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?',
871
-            'event_espresso'
872
-        );
873
-        EE_Registry::$i18n_js_strings['server_error'] = esc_html__(
874
-            'An unknown error occurred on the server while attempting to process your request. Please refresh the page and try again or contact support.',
875
-            'event_espresso'
876
-        );
877
-
878
-        wp_register_script(
879
-            'ee_msgs_edit_js',
880
-            EE_MSG_ASSETS_URL . 'ee_message_editor.js',
881
-            array('jquery'),
882
-            EVENT_ESPRESSO_VERSION
883
-        );
884
-
885
-        wp_enqueue_script('ee_admin_js');
886
-        wp_enqueue_script('ee_msgs_edit_js');
887
-
888
-        // add in special css for tiny_mce
889
-        add_filter('mce_css', array($this, 'wp_editor_css'));
890
-    }
891
-
892
-
893
-    public function load_scripts_styles_display_preview_message()
894
-    {
895
-
896
-        $this->_set_message_template_group();
897
-
898
-        if (isset($this->_req_data['messenger'])) {
899
-            $this->_active_messenger = $this->_message_resource_manager->get_active_messenger(
900
-                $this->_req_data['messenger']
901
-            );
902
-        }
903
-
904
-        $message_type_name = isset($this->_req_data['message_type']) ? $this->_req_data['message_type'] : '';
905
-
906
-
907
-        wp_enqueue_style(
908
-            'espresso_preview_css',
909
-            $this->_active_messenger->get_variation(
910
-                $this->_template_pack,
911
-                $message_type_name,
912
-                true,
913
-                'preview',
914
-                $this->_variation
915
-            )
916
-        );
917
-    }
918
-
919
-
920
-    public function load_scripts_styles_settings()
921
-    {
922
-        wp_register_style(
923
-            'ee-message-settings',
924
-            EE_MSG_ASSETS_URL . 'ee_message_settings.css',
925
-            array(),
926
-            EVENT_ESPRESSO_VERSION
927
-        );
928
-        wp_enqueue_style('ee-text-links');
929
-        wp_enqueue_style('ee-message-settings');
930
-        wp_enqueue_script('ee-messages-settings');
931
-    }
932
-
933
-
934
-    /**
935
-     * set views array for List Table
936
-     */
937
-    public function _set_list_table_views_global_mtps()
938
-    {
939
-        $this->_views = array(
940
-            'in_use' => array(
941
-                'slug'  => 'in_use',
942
-                'label' => esc_html__('In Use', 'event_espresso'),
943
-                'count' => 0,
944
-            ),
945
-        );
946
-    }
947
-
948
-
949
-    /**
950
-     * Set views array for the Custom Template List Table
951
-     */
952
-    public function _set_list_table_views_custom_mtps()
953
-    {
954
-        $this->_set_list_table_views_global_mtps();
955
-        $this->_views['in_use']['bulk_action'] = array(
956
-            'trash_message_template' => esc_html__('Move to Trash', 'event_espresso'),
957
-        );
958
-    }
959
-
960
-
961
-    /**
962
-     * set views array for message queue list table
963
-     *
964
-     * @throws InvalidDataTypeException
965
-     * @throws InvalidInterfaceException
966
-     * @throws InvalidArgumentException
967
-     * @throws EE_Error
968
-     * @throws ReflectionException
969
-     */
970
-    public function _set_list_table_views_default()
971
-    {
972
-        EE_Registry::instance()->load_helper('Template');
973
-
974
-        $common_bulk_actions = EE_Registry::instance()->CAP->current_user_can(
975
-            'ee_send_message',
976
-            'message_list_table_bulk_actions'
977
-        )
978
-            ? array(
979
-                'generate_now'          => esc_html__('Generate Now', 'event_espresso'),
980
-                'generate_and_send_now' => esc_html__('Generate and Send Now', 'event_espresso'),
981
-                'queue_for_resending'   => esc_html__('Queue for Resending', 'event_espresso'),
982
-                'send_now'              => esc_html__('Send Now', 'event_espresso'),
983
-            )
984
-            : array();
985
-
986
-        $delete_bulk_action = EE_Registry::instance()->CAP->current_user_can(
987
-            'ee_delete_messages',
988
-            'message_list_table_bulk_actions'
989
-        )
990
-            ? array('delete_ee_messages' => esc_html__('Delete Messages', 'event_espresso'))
991
-            : array();
992
-
993
-
994
-        $this->_views = array(
995
-            'all' => array(
996
-                'slug'        => 'all',
997
-                'label'       => esc_html__('All', 'event_espresso'),
998
-                'count'       => 0,
999
-                'bulk_action' => array_merge($common_bulk_actions, $delete_bulk_action),
1000
-            ),
1001
-        );
1002
-
1003
-
1004
-        foreach (EEM_Message::instance()->all_statuses() as $status) {
1005
-            if ($status === EEM_Message::status_debug_only && ! EEM_Message::debug()) {
1006
-                continue;
1007
-            }
1008
-            $status_bulk_actions = $common_bulk_actions;
1009
-            // unset bulk actions not applying to status
1010
-            if (! empty($status_bulk_actions)) {
1011
-                switch ($status) {
1012
-                    case EEM_Message::status_idle:
1013
-                    case EEM_Message::status_resend:
1014
-                        $status_bulk_actions['send_now'] = $common_bulk_actions['send_now'];
1015
-                        break;
1016
-
1017
-                    case EEM_Message::status_failed:
1018
-                    case EEM_Message::status_debug_only:
1019
-                    case EEM_Message::status_messenger_executing:
1020
-                        $status_bulk_actions = array();
1021
-                        break;
1022
-
1023
-                    case EEM_Message::status_incomplete:
1024
-                        unset($status_bulk_actions['queue_for_resending'], $status_bulk_actions['send_now']);
1025
-                        break;
1026
-
1027
-                    case EEM_Message::status_retry:
1028
-                    case EEM_Message::status_sent:
1029
-                        unset($status_bulk_actions['generate_now'], $status_bulk_actions['generate_and_send_now']);
1030
-                        break;
1031
-                }
1032
-            }
1033
-
1034
-            // skip adding messenger executing status to views because it will be included with the Failed view.
1035
-            if ($status === EEM_Message::status_messenger_executing) {
1036
-                continue;
1037
-            }
1038
-
1039
-            $this->_views[ strtolower($status) ] = array(
1040
-                'slug'        => strtolower($status),
1041
-                'label'       => EEH_Template::pretty_status($status, false, 'sentence'),
1042
-                'count'       => 0,
1043
-                'bulk_action' => array_merge($status_bulk_actions, $delete_bulk_action),
1044
-            );
1045
-        }
1046
-    }
1047
-
1048
-
1049
-    protected function _ee_default_messages_overview_list_table()
1050
-    {
1051
-        $this->_admin_page_title = esc_html__('Default Message Templates', 'event_espresso');
1052
-        $this->display_admin_list_table_page_with_no_sidebar();
1053
-    }
1054
-
1055
-
1056
-    protected function _message_queue_list_table()
1057
-    {
1058
-        $this->_search_btn_label = esc_html__('Message Activity', 'event_espresso');
1059
-        $this->_template_args['per_column'] = 6;
1060
-        $this->_template_args['after_list_table'] = $this->_display_legend($this->_message_legend_items());
1061
-        $this->_template_args['before_list_table'] = '<h3>'
1062
-                                                     . EEM_Message::instance()->get_pretty_label_for_results()
1063
-                                                     . '</h3>';
1064
-        $this->display_admin_list_table_page_with_no_sidebar();
1065
-    }
1066
-
1067
-
1068
-    protected function _message_legend_items()
1069
-    {
1070
-
1071
-        $action_css_classes = EEH_MSG_Template::get_message_action_icons();
1072
-        $action_items = array();
1073
-
1074
-        foreach ($action_css_classes as $action_item => $action_details) {
1075
-            if ($action_item === 'see_notifications_for') {
1076
-                continue;
1077
-            }
1078
-            $action_items[ $action_item ] = array(
1079
-                'class' => $action_details['css_class'],
1080
-                'desc'  => $action_details['label'],
1081
-            );
1082
-        }
1083
-
1084
-        /** @type array $status_items status legend setup */
1085
-        $status_items = array(
1086
-            'incomplete_status'          => array(
1087
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_incomplete,
1088
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_incomplete, false, 'sentence'),
1089
-            ),
1090
-            'idle_status'                => array(
1091
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_idle,
1092
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_idle, false, 'sentence'),
1093
-            ),
1094
-            'resend_status'              => array(
1095
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_resend,
1096
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_resend, false, 'sentence'),
1097
-            ),
1098
-            'messenger_executing_status' => array(
1099
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_messenger_executing,
1100
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_messenger_executing, false, 'sentence'),
1101
-            ),
1102
-            'sent_status'                => array(
1103
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_sent,
1104
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_sent, false, 'sentence'),
1105
-            ),
1106
-            'retry_status'               => array(
1107
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_retry,
1108
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_retry, false, 'sentence'),
1109
-            ),
1110
-            'failed_status'              => array(
1111
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_failed,
1112
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_failed, false, 'sentence'),
1113
-            ),
1114
-        );
1115
-        if (EEM_Message::debug()) {
1116
-            $status_items['debug_only_status'] = array(
1117
-                'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_debug_only,
1118
-                'desc'  => EEH_Template::pretty_status(EEM_Message::status_debug_only, false, 'sentence'),
1119
-            );
1120
-        }
1121
-
1122
-        return array_merge($action_items, $status_items);
1123
-    }
1124
-
1125
-
1126
-    protected function _custom_mtps_preview()
1127
-    {
1128
-        $this->_admin_page_title = esc_html__('Custom Message Templates (Preview)', 'event_espresso');
1129
-        $this->_template_args['preview_img'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/custom_mtps_preview.png"'
1130
-                                               . ' alt="' . esc_attr__(
1131
-                                                   'Preview Custom Message Templates screenshot',
1132
-                                                   'event_espresso'
1133
-                                               ) . '" />';
1134
-        $this->_template_args['preview_text'] = '<strong>'
1135
-                                                . esc_html__(
1136
-                                                    '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.',
1137
-                                                    'event_espresso'
1138
-                                                )
1139
-                                                . '</strong>';
1140
-
1141
-        $this->display_admin_caf_preview_page('custom_message_types', false);
1142
-    }
1143
-
1144
-
1145
-    /**
1146
-     * get_message_templates
1147
-     * This gets all the message templates for listing on the overview list.
1148
-     *
1149
-     * @access public
1150
-     * @param int    $perpage the amount of templates groups to show per page
1151
-     * @param string $type    the current _view we're getting templates for
1152
-     * @param bool   $count   return count?
1153
-     * @param bool   $all     disregard any paging info (get all data);
1154
-     * @param bool   $global  whether to return just global (true) or custom templates (false)
1155
-     * @return array
1156
-     * @throws EE_Error
1157
-     * @throws InvalidArgumentException
1158
-     * @throws InvalidDataTypeException
1159
-     * @throws InvalidInterfaceException
1160
-     */
1161
-    public function get_message_templates(
1162
-        $perpage = 10,
1163
-        $type = 'in_use',
1164
-        $count = false,
1165
-        $all = false,
1166
-        $global = true
1167
-    ) {
1168
-
1169
-        $MTP = EEM_Message_Template_Group::instance();
1170
-
1171
-        $this->_req_data['orderby'] = empty($this->_req_data['orderby']) ? 'GRP_ID' : $this->_req_data['orderby'];
1172
-        $orderby = $this->_req_data['orderby'];
1173
-
1174
-        $order = (isset($this->_req_data['order']) && ! empty($this->_req_data['order']))
1175
-            ? $this->_req_data['order']
1176
-            : 'ASC';
1177
-
1178
-        $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged'])
1179
-            ? $this->_req_data['paged']
1180
-            : 1;
1181
-        $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage'])
1182
-            ? $this->_req_data['perpage']
1183
-            : $perpage;
1184
-
1185
-        $offset = ($current_page - 1) * $per_page;
1186
-        $limit = $all ? null : array($offset, $per_page);
1187
-
1188
-
1189
-        // options will match what is in the _views array property
1190
-        switch ($type) {
1191
-            case 'in_use':
1192
-                $templates = $MTP->get_all_active_message_templates($orderby, $order, $limit, $count, $global, true);
1193
-                break;
1194
-            default:
1195
-                $templates = $MTP->get_all_trashed_grouped_message_templates($orderby, $order, $limit, $count, $global);
1196
-        }
1197
-
1198
-        return $templates;
1199
-    }
1200
-
1201
-
1202
-    /**
1203
-     * filters etc might need a list of installed message_types
1204
-     *
1205
-     * @return array an array of message type objects
1206
-     */
1207
-    public function get_installed_message_types()
1208
-    {
1209
-        $installed_message_types = $this->_message_resource_manager->installed_message_types();
1210
-        $installed = array();
1211
-
1212
-        foreach ($installed_message_types as $message_type) {
1213
-            $installed[ $message_type->name ] = $message_type;
1214
-        }
1215
-
1216
-        return $installed;
1217
-    }
1218
-
1219
-
1220
-    /**
1221
-     * _add_message_template
1222
-     *
1223
-     * This is used when creating a custom template. All Custom Templates start based off another template.
1224
-     *
1225
-     * @param string $message_type
1226
-     * @param string $messenger
1227
-     * @param string $GRP_ID
1228
-     *
1229
-     * @throws EE_error
1230
-     */
1231
-    protected function _add_message_template($message_type = '', $messenger = '', $GRP_ID = '')
1232
-    {
1233
-        // set values override any request data
1234
-        $message_type = ! empty($message_type) ? $message_type : '';
1235
-        $message_type = empty($message_type) && ! empty($this->_req_data['message_type'])
1236
-            ? $this->_req_data['message_type']
1237
-            : $message_type;
1238
-
1239
-        $messenger = ! empty($messenger) ? $messenger : '';
1240
-        $messenger = empty($messenger) && ! empty($this->_req_data['messenger'])
1241
-            ? $this->_req_data['messenger']
1242
-            : $messenger;
1243
-
1244
-        $GRP_ID = ! empty($GRP_ID) ? $GRP_ID : '';
1245
-        $GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : $GRP_ID;
1246
-
1247
-        // we need messenger and message type.  They should be coming from the event editor. If not here then return error
1248
-        if (empty($message_type) || empty($messenger)) {
1249
-            throw new EE_Error(
1250
-                esc_html__(
1251
-                    'Sorry, but we can\'t create new templates because we\'re missing the messenger or message type',
1252
-                    'event_espresso'
1253
-                )
1254
-            );
1255
-        }
1256
-
1257
-        // we need the GRP_ID for the template being used as the base for the new template
1258
-        if (empty($GRP_ID)) {
1259
-            throw new EE_Error(
1260
-                esc_html__(
1261
-                    'In order to create a custom message template the GRP_ID of the template being used as a base is needed',
1262
-                    'event_espresso'
1263
-                )
1264
-            );
1265
-        }
1266
-
1267
-        // let's just make sure the template gets generated!
1268
-
1269
-        // we need to reassign some variables for what the insert is expecting
1270
-        $this->_req_data['MTP_messenger'] = $messenger;
1271
-        $this->_req_data['MTP_message_type'] = $message_type;
1272
-        $this->_req_data['GRP_ID'] = $GRP_ID;
1273
-        $this->_insert_or_update_message_template(true);
1274
-    }
1275
-
1276
-
1277
-    /**
1278
-     * public wrapper for the _add_message_template method
1279
-     *
1280
-     * @param string $message_type     message type slug
1281
-     * @param string $messenger        messenger slug
1282
-     * @param int    $GRP_ID           GRP_ID for the related message template group this new template will be based
1283
-     *                                 off of.
1284
-     * @throws EE_error
1285
-     */
1286
-    public function add_message_template($message_type, $messenger, $GRP_ID)
1287
-    {
1288
-        $this->_add_message_template($message_type, $messenger, $GRP_ID);
1289
-    }
1290
-
1291
-
1292
-    /**
1293
-     * _edit_message_template
1294
-     *
1295
-     * @access protected
1296
-     * @return void
1297
-     * @throws InvalidIdentifierException
1298
-     * @throws DomainException
1299
-     * @throws EE_Error
1300
-     * @throws InvalidArgumentException
1301
-     * @throws ReflectionException
1302
-     * @throws InvalidDataTypeException
1303
-     * @throws InvalidInterfaceException
1304
-     */
1305
-    protected function _edit_message_template()
1306
-    {
1307
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1308
-        $template_fields = '';
1309
-        $sidebar_fields = '';
1310
-        // we filter the tinyMCE settings to remove the validation since message templates by their nature will not have
1311
-        // valid html in the templates.
1312
-        add_filter('tiny_mce_before_init', array($this, 'filter_tinymce_init'), 10, 2);
1313
-
1314
-        $GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id'])
1315
-            ? absint($this->_req_data['id'])
1316
-            : false;
1317
-
1318
-        $EVT_ID = isset($this->_req_data['evt_id']) && ! empty($this->_req_data['evt_id'])
1319
-        ? absint($this->_req_data['evt_id'])
1320
-        : false;
1321
-
1322
-        $this->_set_shortcodes(); // this also sets the _message_template property.
1323
-        $message_template_group = $this->_message_template_group;
1324
-        $c_label = $message_template_group->context_label();
1325
-        $c_config = $message_template_group->contexts_config();
1326
-
1327
-        reset($c_config);
1328
-        $context = isset($this->_req_data['context']) && ! empty($this->_req_data['context'])
1329
-            ? strtolower($this->_req_data['context'])
1330
-            : key($c_config);
1331
-
1332
-
1333
-        if (empty($GRP_ID)) {
1334
-            $action = 'insert_message_template';
1335
-            $edit_message_template_form_url = add_query_arg(
1336
-                array('action' => $action, 'noheader' => true),
1337
-                EE_MSG_ADMIN_URL
1338
-            );
1339
-        } else {
1340
-            $action = 'update_message_template';
1341
-            $edit_message_template_form_url = add_query_arg(
1342
-                array('action' => $action, 'noheader' => true),
1343
-                EE_MSG_ADMIN_URL
1344
-            );
1345
-        }
1346
-
1347
-        // set active messenger for this view
1348
-        $this->_active_messenger = $this->_message_resource_manager->get_active_messenger(
1349
-            $message_template_group->messenger()
1350
-        );
1351
-        $this->_active_message_type_name = $message_template_group->message_type();
1352
-
1353
-
1354
-        // Do we have any validation errors?
1355
-        $validators = $this->_get_transient();
1356
-        $v_fields = ! empty($validators) ? array_keys($validators) : array();
1357
-
1358
-
1359
-        // we need to assemble the title from Various details
1360
-        $context_label = sprintf(
1361
-            esc_html__('(%s %s)', 'event_espresso'),
1362
-            $c_config[ $context ]['label'],
1363
-            ucwords($c_label['label'])
1364
-        );
1365
-
1366
-        $title = sprintf(
1367
-            esc_html__(' %s %s Template %s', 'event_espresso'),
1368
-            ucwords($message_template_group->messenger_obj()->label['singular']),
1369
-            ucwords($message_template_group->message_type_obj()->label['singular']),
1370
-            $context_label
1371
-        );
1372
-
1373
-        $this->_template_args['GRP_ID'] = $GRP_ID;
1374
-        $this->_template_args['message_template'] = $message_template_group;
1375
-        $this->_template_args['is_extra_fields'] = false;
1376
-
1377
-
1378
-        // let's get EEH_MSG_Template so we can get template form fields
1379
-        $template_field_structure = EEH_MSG_Template::get_fields(
1380
-            $message_template_group->messenger(),
1381
-            $message_template_group->message_type()
1382
-        );
1383
-
1384
-        if (! $template_field_structure) {
1385
-            $template_field_structure = false;
1386
-            $template_fields = esc_html__(
1387
-                'There was an error in assembling the fields for this display (you should see an error message)',
1388
-                'event_espresso'
1389
-            );
1390
-        }
1391
-
1392
-
1393
-        $message_templates = $message_template_group->context_templates();
1394
-
1395
-
1396
-        // if we have the extra key.. then we need to remove the content index from the template_field_structure as it
1397
-        // will get handled in the "extra" array.
1398
-        if (is_array($template_field_structure[ $context ]) && isset($template_field_structure[ $context ]['extra'])) {
1399
-            foreach ($template_field_structure[ $context ]['extra'] as $reference_field => $new_fields) {
1400
-                unset($template_field_structure[ $context ][ $reference_field ]);
1401
-            }
1402
-        }
1403
-
1404
-        // let's loop through the template_field_structure and actually assemble the input fields!
1405
-        if (! empty($template_field_structure)) {
1406
-            foreach ($template_field_structure[ $context ] as $template_field => $field_setup_array) {
1407
-                // if this is an 'extra' template field then we need to remove any existing fields that are keyed up in
1408
-                // the extra array and reset them.
1409
-                if ($template_field === 'extra') {
1410
-                    $this->_template_args['is_extra_fields'] = true;
1411
-                    foreach ($field_setup_array as $reference_field => $new_fields_array) {
1412
-                        $message_template = $message_templates[ $context ][ $reference_field ];
1413
-                        $content = $message_template instanceof EE_Message_Template
1414
-                            ? $message_template->get('MTP_content')
1415
-                            : '';
1416
-                        foreach ($new_fields_array as $extra_field => $extra_array) {
1417
-                            // let's verify if we need this extra field via the shortcodes parameter.
1418
-                            $continue = false;
1419
-                            if (isset($extra_array['shortcodes_required'])) {
1420
-                                foreach ((array) $extra_array['shortcodes_required'] as $shortcode) {
1421
-                                    if (! array_key_exists($shortcode, $this->_shortcodes)) {
1422
-                                        $continue = true;
1423
-                                    }
1424
-                                }
1425
-                                if ($continue) {
1426
-                                    continue;
1427
-                                }
1428
-                            }
1429
-
1430
-                            $field_id = $reference_field
1431
-                                        . '-'
1432
-                                        . $extra_field
1433
-                                        . '-content';
1434
-                            $template_form_fields[ $field_id ] = $extra_array;
1435
-                            $template_form_fields[ $field_id ]['name'] = 'MTP_template_fields['
1436
-                                                                         . $reference_field
1437
-                                                                         . '][content]['
1438
-                                                                         . $extra_field . ']';
1439
-                            $css_class = isset($extra_array['css_class'])
1440
-                                ? $extra_array['css_class']
1441
-                                : '';
1442
-
1443
-                            $template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
1444
-                                                                              && in_array($extra_field, $v_fields, true)
1445
-                                                                              &&
1446
-                                                                              (
1447
-                                                                                  is_array($validators[ $extra_field ])
1448
-                                                                                  && isset($validators[ $extra_field ]['msg'])
1449
-                                                                              )
1450
-                                ? 'validate-error ' . $css_class
1451
-                                : $css_class;
1452
-
1453
-                            $template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
1454
-                                                                          && isset($content[ $extra_field ])
1455
-                                ? $content[ $extra_field ]
1456
-                                : '';
1457
-
1458
-                            // do we have a validation error?  if we do then let's use that value instead
1459
-                            $template_form_fields[ $field_id ]['value'] = isset($validators[ $extra_field ])
1460
-                                ? $validators[ $extra_field ]['value']
1461
-                                : $template_form_fields[ $field_id ]['value'];
1462
-
1463
-
1464
-                            $template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
1465
-
1466
-                            // shortcode selector
1467
-                            $field_name_to_use = $extra_field === 'main'
1468
-                                ? 'content'
1469
-                                : $extra_field;
1470
-                            $template_form_fields[ $field_id ]['append_content'] = $this->_get_shortcode_selector(
1471
-                                $field_name_to_use,
1472
-                                $field_id
1473
-                            );
1474
-
1475
-                            if (isset($extra_array['input']) && $extra_array['input'] === 'wp_editor') {
1476
-                                // we want to decode the entities
1477
-                                $template_form_fields[ $field_id ]['value'] = $template_form_fields[ $field_id ]['value'];
1478
-                            }/**/
1479
-                        }
1480
-                        $templatefield_MTP_id = $reference_field . '-MTP_ID';
1481
-                        $templatefield_templatename_id = $reference_field . '-name';
1482
-
1483
-                        $template_form_fields[ $templatefield_MTP_id ] = array(
1484
-                            'name'       => 'MTP_template_fields[' . $reference_field . '][MTP_ID]',
1485
-                            'label'      => null,
1486
-                            'input'      => 'hidden',
1487
-                            'type'       => 'int',
1488
-                            'required'   => false,
1489
-                            'validation' => false,
1490
-                            'value'      => ! empty($message_templates) ? $message_template->ID() : '',
1491
-                            'css_class'  => '',
1492
-                            'format'     => '%d',
1493
-                            'db-col'     => 'MTP_ID',
1494
-                        );
1495
-
1496
-                        $template_form_fields[ $templatefield_templatename_id ] = array(
1497
-                            'name'       => 'MTP_template_fields[' . $reference_field . '][name]',
1498
-                            'label'      => null,
1499
-                            'input'      => 'hidden',
1500
-                            'type'       => 'string',
1501
-                            'required'   => false,
1502
-                            'validation' => true,
1503
-                            'value'      => $reference_field,
1504
-                            'css_class'  => '',
1505
-                            'format'     => '%s',
1506
-                            'db-col'     => 'MTP_template_field',
1507
-                        );
1508
-                    }
1509
-                    continue; // skip the next stuff, we got the necessary fields here for this dataset.
1510
-                } else {
1511
-                    $field_id = $template_field . '-content';
1512
-                    $template_form_fields[ $field_id ] = $field_setup_array;
1513
-                    $template_form_fields[ $field_id ]['name'] = 'MTP_template_fields[' . $template_field . '][content]';
1514
-                    $message_template = isset($message_templates[ $context ][ $template_field ])
1515
-                        ? $message_templates[ $context ][ $template_field ]
1516
-                        : null;
1517
-                    $template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
1518
-                                                                  && is_array($message_templates[ $context ])
1519
-                                                                  && $message_template instanceof EE_Message_Template
1520
-                        ? $message_template->get('MTP_content')
1521
-                        : '';
1522
-
1523
-                    // do we have a validator error for this field?  if we do then we'll use that value instead
1524
-                    $template_form_fields[ $field_id ]['value'] = isset($validators[ $template_field ])
1525
-                        ? $validators[ $template_field ]['value']
1526
-                        : $template_form_fields[ $field_id ]['value'];
1527
-
1528
-
1529
-                    $template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
1530
-                    $css_class = isset($field_setup_array['css_class'])
1531
-                        ? $field_setup_array['css_class']
1532
-                        : '';
1533
-                    $template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
1534
-                                                                      && in_array($template_field, $v_fields, true)
1535
-                                                                      && isset($validators[ $template_field ]['msg'])
1536
-                        ? 'validate-error ' . $css_class
1537
-                        : $css_class;
1538
-
1539
-                    // shortcode selector
1540
-                    $template_form_fields[ $field_id ]['append_content'] = $this->_get_shortcode_selector(
1541
-                        $template_field,
1542
-                        $field_id
1543
-                    );
1544
-                }
1545
-
1546
-                // k took care of content field(s) now let's take care of others.
1547
-
1548
-                $templatefield_MTP_id = $template_field . '-MTP_ID';
1549
-                $templatefield_field_templatename_id = $template_field . '-name';
1550
-
1551
-                // foreach template field there are actually two form fields created
1552
-                $template_form_fields[ $templatefield_MTP_id ] = array(
1553
-                    'name'       => 'MTP_template_fields[' . $template_field . '][MTP_ID]',
1554
-                    'label'      => null,
1555
-                    'input'      => 'hidden',
1556
-                    'type'       => 'int',
1557
-                    'required'   => false,
1558
-                    'validation' => true,
1559
-                    'value'      => $message_template instanceof EE_Message_Template ? $message_template->ID() : '',
1560
-                    'css_class'  => '',
1561
-                    'format'     => '%d',
1562
-                    'db-col'     => 'MTP_ID',
1563
-                );
1564
-
1565
-                $template_form_fields[ $templatefield_field_templatename_id ] = array(
1566
-                    'name'       => 'MTP_template_fields[' . $template_field . '][name]',
1567
-                    'label'      => null,
1568
-                    'input'      => 'hidden',
1569
-                    'type'       => 'string',
1570
-                    'required'   => false,
1571
-                    'validation' => true,
1572
-                    'value'      => $template_field,
1573
-                    'css_class'  => '',
1574
-                    'format'     => '%s',
1575
-                    'db-col'     => 'MTP_template_field',
1576
-                );
1577
-            }
1578
-
1579
-            // add other fields
1580
-            $template_form_fields['ee-msg-current-context'] = array(
1581
-                'name'       => 'MTP_context',
1582
-                'label'      => null,
1583
-                'input'      => 'hidden',
1584
-                'type'       => 'string',
1585
-                'required'   => false,
1586
-                'validation' => true,
1587
-                'value'      => $context,
1588
-                'css_class'  => '',
1589
-                'format'     => '%s',
1590
-                'db-col'     => 'MTP_context',
1591
-            );
1592
-
1593
-            $template_form_fields['ee-msg-grp-id'] = array(
1594
-                'name'       => 'GRP_ID',
1595
-                'label'      => null,
1596
-                'input'      => 'hidden',
1597
-                'type'       => 'int',
1598
-                'required'   => false,
1599
-                'validation' => true,
1600
-                'value'      => $GRP_ID,
1601
-                'css_class'  => '',
1602
-                'format'     => '%d',
1603
-                'db-col'     => 'GRP_ID',
1604
-            );
1605
-
1606
-            $template_form_fields['ee-msg-messenger'] = array(
1607
-                'name'       => 'MTP_messenger',
1608
-                'label'      => null,
1609
-                'input'      => 'hidden',
1610
-                'type'       => 'string',
1611
-                'required'   => false,
1612
-                'validation' => true,
1613
-                'value'      => $message_template_group->messenger(),
1614
-                'css_class'  => '',
1615
-                'format'     => '%s',
1616
-                'db-col'     => 'MTP_messenger',
1617
-            );
1618
-
1619
-            $template_form_fields['ee-msg-message-type'] = array(
1620
-                'name'       => 'MTP_message_type',
1621
-                'label'      => null,
1622
-                'input'      => 'hidden',
1623
-                'type'       => 'string',
1624
-                'required'   => false,
1625
-                'validation' => true,
1626
-                'value'      => $message_template_group->message_type(),
1627
-                'css_class'  => '',
1628
-                'format'     => '%s',
1629
-                'db-col'     => 'MTP_message_type',
1630
-            );
1631
-
1632
-            $sidebar_form_fields['ee-msg-is-global'] = array(
1633
-                'name'       => 'MTP_is_global',
1634
-                'label'      => esc_html__('Global Template', 'event_espresso'),
1635
-                'input'      => 'hidden',
1636
-                'type'       => 'int',
1637
-                'required'   => false,
1638
-                'validation' => true,
1639
-                'value'      => $message_template_group->get('MTP_is_global'),
1640
-                'css_class'  => '',
1641
-                'format'     => '%d',
1642
-                'db-col'     => 'MTP_is_global',
1643
-            );
1644
-
1645
-            $sidebar_form_fields['ee-msg-is-override'] = array(
1646
-                'name'       => 'MTP_is_override',
1647
-                'label'      => esc_html__('Override all custom', 'event_espresso'),
1648
-                'input'      => $message_template_group->is_global() ? 'checkbox' : 'hidden',
1649
-                'type'       => 'int',
1650
-                'required'   => false,
1651
-                'validation' => true,
1652
-                'value'      => $message_template_group->get('MTP_is_override'),
1653
-                'css_class'  => '',
1654
-                'format'     => '%d',
1655
-                'db-col'     => 'MTP_is_override',
1656
-            );
1657
-
1658
-            $sidebar_form_fields['ee-msg-is-active'] = array(
1659
-                'name'       => 'MTP_is_active',
1660
-                'label'      => esc_html__('Active Template', 'event_espresso'),
1661
-                'input'      => 'hidden',
1662
-                'type'       => 'int',
1663
-                'required'   => false,
1664
-                'validation' => true,
1665
-                'value'      => $message_template_group->is_active(),
1666
-                'css_class'  => '',
1667
-                'format'     => '%d',
1668
-                'db-col'     => 'MTP_is_active',
1669
-            );
1670
-
1671
-            $sidebar_form_fields['ee-msg-deleted'] = array(
1672
-                'name'       => 'MTP_deleted',
1673
-                'label'      => null,
1674
-                'input'      => 'hidden',
1675
-                'type'       => 'int',
1676
-                'required'   => false,
1677
-                'validation' => true,
1678
-                'value'      => $message_template_group->get('MTP_deleted'),
1679
-                'css_class'  => '',
1680
-                'format'     => '%d',
1681
-                'db-col'     => 'MTP_deleted',
1682
-            );
1683
-            $sidebar_form_fields['ee-msg-author'] = array(
1684
-                'name'       => 'MTP_user_id',
1685
-                'label'      => esc_html__('Author', 'event_espresso'),
1686
-                'input'      => 'hidden',
1687
-                'type'       => 'int',
1688
-                'required'   => false,
1689
-                'validation' => false,
1690
-                'value'      => $message_template_group->user(),
1691
-                'format'     => '%d',
1692
-                'db-col'     => 'MTP_user_id',
1693
-            );
1694
-
1695
-            $sidebar_form_fields['ee-msg-route'] = array(
1696
-                'name'  => 'action',
1697
-                'input' => 'hidden',
1698
-                'type'  => 'string',
1699
-                'value' => $action,
1700
-            );
1701
-
1702
-            $sidebar_form_fields['ee-msg-id'] = array(
1703
-                'name'  => 'id',
1704
-                'input' => 'hidden',
1705
-                'type'  => 'int',
1706
-                'value' => $GRP_ID,
1707
-            );
1708
-            $sidebar_form_fields['ee-msg-evt-nonce'] = array(
1709
-                'name'  => $action . '_nonce',
1710
-                'input' => 'hidden',
1711
-                'type'  => 'string',
1712
-                'value' => wp_create_nonce($action . '_nonce'),
1713
-            );
1714
-
1715
-            if (isset($this->_req_data['template_switch']) && $this->_req_data['template_switch']) {
1716
-                $sidebar_form_fields['ee-msg-template-switch'] = array(
1717
-                    'name'  => 'template_switch',
1718
-                    'input' => 'hidden',
1719
-                    'type'  => 'int',
1720
-                    'value' => 1,
1721
-                );
1722
-            }
1723
-
1724
-
1725
-            $template_fields = $this->_generate_admin_form_fields($template_form_fields);
1726
-            $sidebar_fields = $this->_generate_admin_form_fields($sidebar_form_fields);
1727
-        } //end if ( !empty($template_field_structure) )
1728
-
1729
-        // set extra content for publish box
1730
-        $this->_template_args['publish_box_extra_content'] = $sidebar_fields;
1731
-        $this->_set_publish_post_box_vars(
1732
-            'id',
1733
-            $GRP_ID,
1734
-            false,
1735
-            add_query_arg(
1736
-                array('action' => 'global_mtps'),
1737
-                $this->_admin_base_url
1738
-            )
1739
-        );
1740
-
1741
-        // add preview button
1742
-        $preview_url = parent::add_query_args_and_nonce(
1743
-            array(
1744
-                'message_type' => $message_template_group->message_type(),
1745
-                'messenger'    => $message_template_group->messenger(),
1746
-                'context'      => $context,
1747
-                'GRP_ID'       => $GRP_ID,
1748
-                'evt_id'       => $EVT_ID,
1749
-                'action'       => 'preview_message',
1750
-            ),
1751
-            $this->_admin_base_url
1752
-        );
1753
-        $preview_button = '<a href="' . $preview_url . '" class="button-secondary messages-preview-button">'
1754
-                          . esc_html__('Preview', 'event_espresso')
1755
-                          . '</a>';
1756
-
1757
-
1758
-        // setup context switcher
1759
-        $context_switcher_args = array(
1760
-            'page'    => 'espresso_messages',
1761
-            'action'  => 'edit_message_template',
1762
-            'id'      => $GRP_ID,
1763
-            'evt_id'  => $EVT_ID,
1764
-            'context' => $context,
1765
-            'extra'   => $preview_button,
1766
-        );
1767
-        $this->_set_context_switcher($message_template_group, $context_switcher_args);
1768
-
1769
-
1770
-        // main box
1771
-        $this->_template_args['template_fields'] = $template_fields;
1772
-        $this->_template_args['sidebar_box_id'] = 'details';
1773
-        $this->_template_args['action'] = $action;
1774
-        $this->_template_args['context'] = $context;
1775
-        $this->_template_args['edit_message_template_form_url'] = $edit_message_template_form_url;
1776
-        $this->_template_args['learn_more_about_message_templates_link'] =
1777
-            $this->_learn_more_about_message_templates_link();
1778
-
1779
-
1780
-        $this->_template_args['before_admin_page_content'] = $this->add_context_switcher();
1781
-        $this->_template_args['before_admin_page_content'] .= $this->add_active_context_element(
1782
-            $message_template_group,
1783
-            $context,
1784
-            $context_label
1785
-        );
1786
-        $this->_template_args['before_admin_page_content'] .= $this->_add_form_element_before();
1787
-        $this->_template_args['after_admin_page_content'] = $this->_add_form_element_after();
1788
-
1789
-        $this->_template_path = $this->_template_args['GRP_ID']
1790
-            ? EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_edit_meta_box.template.php'
1791
-            : EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_add_meta_box.template.php';
1792
-
1793
-        // send along EE_Message_Template_Group object for further template use.
1794
-        $this->_template_args['MTP'] = $message_template_group;
1795
-
1796
-        $this->_template_args['admin_page_content'] = EEH_Template::display_template(
1797
-            $this->_template_path,
1798
-            $this->_template_args,
1799
-            true
1800
-        );
1801
-
1802
-
1803
-        // finally, let's set the admin_page title
1804
-        $this->_admin_page_title = sprintf(__('Editing %s', 'event_espresso'), $title);
1805
-
1806
-
1807
-        // we need to take care of setting the shortcodes property for use elsewhere.
1808
-        $this->_set_shortcodes();
1809
-
1810
-
1811
-        // final template wrapper
1812
-        $this->display_admin_page_with_sidebar();
1813
-    }
1814
-
1815
-
1816
-    public function filter_tinymce_init($mceInit, $editor_id)
1817
-    {
1818
-        return $mceInit;
1819
-    }
1820
-
1821
-
1822
-    public function add_context_switcher()
1823
-    {
1824
-        return $this->_context_switcher;
1825
-    }
1826
-
1827
-
1828
-    /**
1829
-     * Adds the activation/deactivation toggle for the message template context.
1830
-     *
1831
-     * @param EE_Message_Template_Group $message_template_group
1832
-     * @param string                    $context
1833
-     * @param string                    $context_label
1834
-     * @return string
1835
-     * @throws DomainException
1836
-     * @throws EE_Error
1837
-     * @throws InvalidIdentifierException
1838
-     */
1839
-    protected function add_active_context_element(
1840
-        EE_Message_Template_Group $message_template_group,
1841
-        $context,
1842
-        $context_label
1843
-    ) {
1844
-        $template_args = array(
1845
-            'context'                   => $context,
1846
-            'nonce'                     => wp_create_nonce('activate_' . $context . '_toggle_nonce'),
1847
-            'is_active'                 => $message_template_group->is_context_active($context),
1848
-            'on_off_action'             => $message_template_group->is_context_active($context)
1849
-                ? 'context-off'
1850
-                : 'context-on',
1851
-            'context_label'             => str_replace(array('(', ')'), '', $context_label),
1852
-            'message_template_group_id' => $message_template_group->ID(),
1853
-        );
1854
-        return EEH_Template::display_template(
1855
-            EE_MSG_TEMPLATE_PATH . 'ee_msg_editor_active_context_element.template.php',
1856
-            $template_args,
1857
-            true
1858
-        );
1859
-    }
1860
-
1861
-
1862
-    /**
1863
-     * Ajax callback for `toggle_context_template` ajax action.
1864
-     * Handles toggling the message context on or off.
1865
-     *
1866
-     * @throws EE_Error
1867
-     * @throws InvalidArgumentException
1868
-     * @throws InvalidDataTypeException
1869
-     * @throws InvalidIdentifierException
1870
-     * @throws InvalidInterfaceException
1871
-     */
1872
-    public function toggle_context_template()
1873
-    {
1874
-        $success = true;
1875
-        // check for required data
1876
-        if (
1877
-            ! isset(
1878
-                $this->_req_data['message_template_group_id'],
1879
-                $this->_req_data['context'],
1880
-                $this->_req_data['status']
1881
-            )
1882
-        ) {
1883
-            EE_Error::add_error(
1884
-                esc_html__('Required data for doing this action is not available.', 'event_espresso'),
1885
-                __FILE__,
1886
-                __FUNCTION__,
1887
-                __LINE__
1888
-            );
1889
-            $success = false;
1890
-        }
1891
-
1892
-        $nonce = isset($this->_req_data['toggle_context_nonce'])
1893
-            ? sanitize_text_field($this->_req_data['toggle_context_nonce'])
1894
-            : '';
1895
-        $nonce_ref = 'activate_' . $this->_req_data['context'] . '_toggle_nonce';
1896
-        $this->_verify_nonce($nonce, $nonce_ref);
1897
-        $status = $this->_req_data['status'];
1898
-        if ($status !== 'off' && $status !== 'on') {
1899
-            EE_Error::add_error(
1900
-                sprintf(
1901
-                    esc_html__('The given status (%s) is not valid. Must be "off" or "on"', 'event_espresso'),
1902
-                    $this->_req_data['status']
1903
-                ),
1904
-                __FILE__,
1905
-                __FUNCTION__,
1906
-                __LINE__
1907
-            );
1908
-            $success = false;
1909
-        }
1910
-        $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID(
1911
-            $this->_req_data['message_template_group_id']
1912
-        );
1913
-        if (! $message_template_group instanceof EE_Message_Template_Group) {
1914
-            EE_Error::add_error(
1915
-                sprintf(
1916
-                    esc_html__(
1917
-                        'Unable to change the active state because the given id "%1$d" does not match a valid "%2$s"',
1918
-                        'event_espresso'
1919
-                    ),
1920
-                    $this->_req_data['message_template_group_id'],
1921
-                    'EE_Message_Template_Group'
1922
-                ),
1923
-                __FILE__,
1924
-                __FUNCTION__,
1925
-                __LINE__
1926
-            );
1927
-            $success = false;
1928
-        }
1929
-        if ($success) {
1930
-            $success = $status === 'off'
1931
-                ? $message_template_group->deactivate_context($this->_req_data['context'])
1932
-                : $message_template_group->activate_context($this->_req_data['context']);
1933
-        }
1934
-        $this->_template_args['success'] = $success;
1935
-        $this->_return_json();
1936
-    }
1937
-
1938
-
1939
-    public function _add_form_element_before()
1940
-    {
1941
-        return '<form method="post" action="'
1942
-               . $this->_template_args["edit_message_template_form_url"]
1943
-               . '" id="ee-msg-edit-frm">';
1944
-    }
1945
-
1946
-    public function _add_form_element_after()
1947
-    {
1948
-        return '</form>';
1949
-    }
1950
-
1951
-
1952
-    /**
1953
-     * This executes switching the template pack for a message template.
1954
-     *
1955
-     * @since 4.5.0
1956
-     * @throws EE_Error
1957
-     * @throws InvalidDataTypeException
1958
-     * @throws InvalidInterfaceException
1959
-     * @throws InvalidArgumentException
1960
-     * @throws ReflectionException
1961
-     */
1962
-    public function switch_template_pack()
1963
-    {
1964
-        $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
1965
-        $template_pack = ! empty($this->_req_data['template_pack']) ? $this->_req_data['template_pack'] : '';
1966
-
1967
-        // verify we have needed values.
1968
-        if (empty($GRP_ID) || empty($template_pack)) {
1969
-            $this->_template_args['error'] = true;
1970
-            EE_Error::add_error(
1971
-                esc_html__('The required date for switching templates is not available.', 'event_espresso'),
1972
-                __FILE__,
1973
-                __FUNCTION__,
1974
-                __LINE__
1975
-            );
1976
-        } else {
1977
-            // get template, set the new template_pack and then reset to default
1978
-            /** @type EE_Message_Template_Group $message_template_group */
1979
-            $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID);
1980
-
1981
-            $message_template_group->set_template_pack_name($template_pack);
1982
-            $this->_req_data['msgr'] = $message_template_group->messenger();
1983
-            $this->_req_data['mt'] = $message_template_group->message_type();
1984
-
1985
-            $query_args = $this->_reset_to_default_template();
1986
-
1987
-            if (empty($query_args['id'])) {
1988
-                EE_Error::add_error(
1989
-                    esc_html__(
1990
-                        'Something went wrong with switching the template pack. Please try again or contact EE support',
1991
-                        'event_espresso'
1992
-                    ),
1993
-                    __FILE__,
1994
-                    __FUNCTION__,
1995
-                    __LINE__
1996
-                );
1997
-                $this->_template_args['error'] = true;
1998
-            } else {
1999
-                $template_label = $message_template_group->get_template_pack()->label;
2000
-                $template_pack_labels = $message_template_group->messenger_obj()->get_supports_labels();
2001
-                EE_Error::add_success(
2002
-                    sprintf(
2003
-                        esc_html__(
2004
-                            'This message template has been successfully switched to use the %1$s %2$s.  Please wait while the page reloads with your new template.',
2005
-                            'event_espresso'
2006
-                        ),
2007
-                        $template_label,
2008
-                        $template_pack_labels->template_pack
2009
-                    )
2010
-                );
2011
-                // generate the redirect url for js.
2012
-                $url = self::add_query_args_and_nonce(
2013
-                    $query_args,
2014
-                    $this->_admin_base_url
2015
-                );
2016
-                $this->_template_args['data']['redirect_url'] = $url;
2017
-                $this->_template_args['success'] = true;
2018
-            }
2019
-
2020
-            $this->_return_json();
2021
-        }
2022
-    }
2023
-
2024
-
2025
-    /**
2026
-     * This handles resetting the template for the given messenger/message_type so that users can start from scratch if
2027
-     * they want.
2028
-     *
2029
-     * @access protected
2030
-     * @return array|null
2031
-     * @throws EE_Error
2032
-     * @throws InvalidArgumentException
2033
-     * @throws InvalidDataTypeException
2034
-     * @throws InvalidInterfaceException
2035
-     */
2036
-    protected function _reset_to_default_template()
2037
-    {
2038
-
2039
-        $templates = array();
2040
-        $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
2041
-        // we need to make sure we've got the info we need.
2042
-        if (! isset($this->_req_data['msgr'], $this->_req_data['mt'], $this->_req_data['GRP_ID'])) {
2043
-            EE_Error::add_error(
2044
-                esc_html__(
2045
-                    '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.',
2046
-                    'event_espresso'
2047
-                ),
2048
-                __FILE__,
2049
-                __FUNCTION__,
2050
-                __LINE__
2051
-            );
2052
-        }
2053
-
2054
-        // all templates will be reset to whatever the defaults are
2055
-        // for the global template matching the messenger and message type.
2056
-        $success = ! empty($GRP_ID) ? true : false;
2057
-
2058
-        if ($success) {
2059
-            // let's first determine if the incoming template is a global template,
2060
-            // if it isn't then we need to get the global template matching messenger and message type.
2061
-            // $MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID( $GRP_ID );
2062
-
2063
-
2064
-            // note this is ONLY deleting the template fields (Message Template rows) NOT the message template group.
2065
-            $success = $this->_delete_mtp_permanently($GRP_ID, false);
2066
-
2067
-            if ($success) {
2068
-                // if successfully deleted, lets generate the new ones.
2069
-                // Note. We set GLOBAL to true, because resets on ANY template
2070
-                // will use the related global template defaults for regeneration.
2071
-                // This means that if a custom template is reset it resets to whatever the related global template is.
2072
-                // HOWEVER, we DO keep the template pack and template variation set
2073
-                // for the current custom template when resetting.
2074
-                $templates = $this->_generate_new_templates(
2075
-                    $this->_req_data['msgr'],
2076
-                    $this->_req_data['mt'],
2077
-                    $GRP_ID,
2078
-                    true
2079
-                );
2080
-            }
2081
-        }
2082
-
2083
-        // any error messages?
2084
-        if (! $success) {
2085
-            EE_Error::add_error(
2086
-                esc_html__(
2087
-                    'Something went wrong with deleting existing templates. Unable to reset to default',
2088
-                    'event_espresso'
2089
-                ),
2090
-                __FILE__,
2091
-                __FUNCTION__,
2092
-                __LINE__
2093
-            );
2094
-        }
2095
-
2096
-        // all good, let's add a success message!
2097
-        if ($success && ! empty($templates)) {
2098
-            // the info for the template we generated is the first element in the returned array
2099
-            // $templates = $templates[0];
2100
-            EE_Error::overwrite_success();
2101
-            EE_Error::add_success(__('Templates have been reset to defaults.', 'event_espresso'));
2102
-        }
2103
-
2104
-
2105
-        $query_args = array(
2106
-            'id'      => isset($templates[0]['GRP_ID']) ? $templates[0]['GRP_ID'] : null,
2107
-            'context' => isset($templates[0]['MTP_context']) ? $templates[0]['MTP_context'] : null,
2108
-            'action'  => isset($templates[0]['GRP_ID']) ? 'edit_message_template' : 'global_mtps',
2109
-        );
2110
-
2111
-        // if called via ajax then we return query args otherwise redirect
2112
-        if (defined('DOING_AJAX') && DOING_AJAX) {
2113
-            return $query_args;
2114
-        } else {
2115
-            $this->_redirect_after_action(false, '', '', $query_args, true);
2116
-
2117
-            return null;
2118
-        }
2119
-    }
2120
-
2121
-
2122
-    /**
2123
-     * Retrieve and set the message preview for display.
2124
-     *
2125
-     * @param bool $send if TRUE then we are doing an actual TEST send with the results of the preview.
2126
-     * @return string
2127
-     * @throws ReflectionException
2128
-     * @throws EE_Error
2129
-     * @throws InvalidArgumentException
2130
-     * @throws InvalidDataTypeException
2131
-     * @throws InvalidInterfaceException
2132
-     */
2133
-    public function _preview_message($send = false)
2134
-    {
2135
-        // first make sure we've got the necessary parameters
2136
-        if (
2137
-            ! isset(
2138
-                $this->_req_data['message_type'],
2139
-                $this->_req_data['messenger'],
2140
-                $this->_req_data['messenger'],
2141
-                $this->_req_data['GRP_ID']
2142
-            )
2143
-        ) {
2144
-            EE_Error::add_error(
2145
-                esc_html__('Missing necessary parameters for displaying preview', 'event_espresso'),
2146
-                __FILE__,
2147
-                __FUNCTION__,
2148
-                __LINE__
2149
-            );
2150
-        }
2151
-
2152
-        EE_Registry::instance()->REQ->set('GRP_ID', $this->_req_data['GRP_ID']);
2153
-
2154
-        // if we have an evt_id set on the request, use it.
2155
-        $EVT_ID = isset($this->_req_data['evt_id']) && ! empty($this->_req_data['evt_id'])
2156
-        ? absint($this->_req_data['evt_id'])
2157
-        : false;
2158
-
2159
-
2160
-        // get the preview!
2161
-        $preview = EED_Messages::preview_message(
2162
-            $this->_req_data['message_type'],
2163
-            $this->_req_data['context'],
2164
-            $this->_req_data['messenger'],
2165
-            $send
2166
-        );
2167
-
2168
-        if ($send) {
2169
-            return $preview;
2170
-        }
2171
-
2172
-        // let's add a button to go back to the edit view
2173
-        $query_args = array(
2174
-            'id'      => $this->_req_data['GRP_ID'],
2175
-            'evt_id'  => $EVT_ID,
2176
-            'context' => $this->_req_data['context'],
2177
-            'action'  => 'edit_message_template',
2178
-        );
2179
-        $go_back_url = parent::add_query_args_and_nonce($query_args, $this->_admin_base_url);
2180
-        $preview_button = '<a href="'
2181
-                          . $go_back_url
2182
-                          . '" class="button-secondary messages-preview-go-back-button">'
2183
-                          . esc_html__('Go Back to Edit', 'event_espresso')
2184
-                          . '</a>';
2185
-        $message_types = $this->get_installed_message_types();
2186
-        $active_messenger = $this->_message_resource_manager->get_active_messenger(
2187
-            $this->_req_data['messenger']
2188
-        );
2189
-        $active_messenger_label = $active_messenger instanceof EE_messenger
2190
-            ? ucwords($active_messenger->label['singular'])
2191
-            : esc_html__('Unknown Messenger', 'event_espresso');
2192
-        // let's provide a helpful title for context
2193
-        $preview_title = sprintf(
2194
-            esc_html__('Viewing Preview for %s %s Message Template', 'event_espresso'),
2195
-            $active_messenger_label,
2196
-            ucwords($message_types[ $this->_req_data['message_type'] ]->label['singular'])
2197
-        );
2198
-        if (empty($preview)) {
2199
-            $this->noEventsErrorMessage();
2200
-        }
2201
-        // setup display of preview.
2202
-        $this->_admin_page_title = $preview_title;
2203
-        $this->_template_args['admin_page_title'] = $preview_title;
2204
-        $this->_template_args['admin_page_content'] = $preview_button . '<br />' . $preview;
2205
-        $this->_template_args['data']['force_json'] = true;
2206
-
2207
-        return '';
2208
-    }
2209
-
2210
-
2211
-    /**
2212
-     * Used to set an error if there are no events available for generating a preview/test send.
2213
-     *
2214
-     * @param bool $test_send  Whether the error should be generated for the context of a test send.
2215
-     */
2216
-    protected function noEventsErrorMessage($test_send = false)
2217
-    {
2218
-        $events_url = parent::add_query_args_and_nonce(
2219
-            array(
2220
-                'action' => 'default',
2221
-                'page'   => 'espresso_events',
2222
-            ),
2223
-            admin_url('admin.php')
2224
-        );
2225
-        $message = $test_send
2226
-            ? __(
2227
-                '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!',
2228
-                'event_espresso'
2229
-            )
2230
-            : __(
2231
-                '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!',
2232
-                'event_espresso'
2233
-            );
2234
-
2235
-        EE_Error::add_attention(
2236
-            sprintf(
2237
-                $message,
2238
-                "<a href='{$events_url}'>",
2239
-                '</a>'
2240
-            )
2241
-        );
2242
-    }
2243
-
2244
-
2245
-    /**
2246
-     * The initial _preview_message is on a no headers route.  It will optionally call this if necessary otherwise it
2247
-     * gets called automatically.
2248
-     *
2249
-     * @since 4.5.0
2250
-     *
2251
-     * @return string
2252
-     */
2253
-    protected function _display_preview_message()
2254
-    {
2255
-        $this->display_admin_page_with_no_sidebar();
2256
-    }
2257
-
2258
-
2259
-    /**
2260
-     * registers metaboxes that should show up on the "edit_message_template" page
2261
-     *
2262
-     * @access protected
2263
-     * @return void
2264
-     */
2265
-    protected function _register_edit_meta_boxes()
2266
-    {
2267
-        add_meta_box(
2268
-            'mtp_valid_shortcodes',
2269
-            esc_html__('Valid Shortcodes', 'event_espresso'),
2270
-            array($this, 'shortcode_meta_box'),
2271
-            $this->_current_screen->id,
2272
-            'side',
2273
-            'default'
2274
-        );
2275
-        add_meta_box(
2276
-            'mtp_extra_actions',
2277
-            esc_html__('Extra Actions', 'event_espresso'),
2278
-            array($this, 'extra_actions_meta_box'),
2279
-            $this->_current_screen->id,
2280
-            'side',
2281
-            'high'
2282
-        );
2283
-        add_meta_box(
2284
-            'mtp_templates',
2285
-            esc_html__('Template Styles', 'event_espresso'),
2286
-            array($this, 'template_pack_meta_box'),
2287
-            $this->_current_screen->id,
2288
-            'side',
2289
-            'high'
2290
-        );
2291
-    }
2292
-
2293
-
2294
-    /**
2295
-     * metabox content for all template pack and variation selection.
2296
-     *
2297
-     * @since 4.5.0
2298
-     * @return string
2299
-     * @throws DomainException
2300
-     * @throws EE_Error
2301
-     * @throws InvalidArgumentException
2302
-     * @throws ReflectionException
2303
-     * @throws InvalidDataTypeException
2304
-     * @throws InvalidInterfaceException
2305
-     */
2306
-    public function template_pack_meta_box()
2307
-    {
2308
-        $this->_set_message_template_group();
2309
-
2310
-        $tp_collection = EEH_MSG_Template::get_template_pack_collection();
2311
-
2312
-        $tp_select_values = array();
2313
-
2314
-        foreach ($tp_collection as $tp) {
2315
-            // only include template packs that support this messenger and message type!
2316
-            $supports = $tp->get_supports();
2317
-            if (
2318
-                ! isset($supports[ $this->_message_template_group->messenger() ])
2319
-                || ! in_array(
2320
-                    $this->_message_template_group->message_type(),
2321
-                    $supports[ $this->_message_template_group->messenger() ],
2322
-                    true
2323
-                )
2324
-            ) {
2325
-                // not supported
2326
-                continue;
2327
-            }
2328
-
2329
-            $tp_select_values[] = array(
2330
-                'text' => $tp->label,
2331
-                'id'   => $tp->dbref,
2332
-            );
2333
-        }
2334
-
2335
-        // if empty $tp_select_values then we make sure default is set because EVERY message type should be supported by
2336
-        // the default template pack.  This still allows for the odd template pack to override.
2337
-        if (empty($tp_select_values)) {
2338
-            $tp_select_values[] = array(
2339
-                'text' => esc_html__('Default', 'event_espresso'),
2340
-                'id'   => 'default',
2341
-            );
2342
-        }
2343
-
2344
-        // setup variation select values for the currently selected template.
2345
-        $variations = $this->_message_template_group->get_template_pack()->get_variations(
2346
-            $this->_message_template_group->messenger(),
2347
-            $this->_message_template_group->message_type()
2348
-        );
2349
-        $variations_select_values = array();
2350
-        foreach ($variations as $variation => $label) {
2351
-            $variations_select_values[] = array(
2352
-                'text' => $label,
2353
-                'id'   => $variation,
2354
-            );
2355
-        }
2356
-
2357
-        $template_pack_labels = $this->_message_template_group->messenger_obj()->get_supports_labels();
2358
-
2359
-        $template_args['template_packs_selector'] = EEH_Form_Fields::select_input(
2360
-            'MTP_template_pack',
2361
-            $tp_select_values,
2362
-            $this->_message_template_group->get_template_pack_name()
2363
-        );
2364
-        $template_args['variations_selector'] = EEH_Form_Fields::select_input(
2365
-            'MTP_template_variation',
2366
-            $variations_select_values,
2367
-            $this->_message_template_group->get_template_pack_variation()
2368
-        );
2369
-        $template_args['template_pack_label'] = $template_pack_labels->template_pack;
2370
-        $template_args['template_variation_label'] = $template_pack_labels->template_variation;
2371
-        $template_args['template_pack_description'] = $template_pack_labels->template_pack_description;
2372
-        $template_args['template_variation_description'] = $template_pack_labels->template_variation_description;
2373
-
2374
-        $template = EE_MSG_TEMPLATE_PATH . 'template_pack_and_variations_metabox.template.php';
2375
-
2376
-        EEH_Template::display_template($template, $template_args);
2377
-    }
2378
-
2379
-
2380
-    /**
2381
-     * This meta box holds any extra actions related to Message Templates
2382
-     * For now, this includes Resetting templates to defaults and sending a test email.
2383
-     *
2384
-     * @access  public
2385
-     * @return void
2386
-     * @throws EE_Error
2387
-     */
2388
-    public function extra_actions_meta_box()
2389
-    {
2390
-        $template_form_fields = array();
2391
-
2392
-        $extra_args = array(
2393
-            'msgr'   => $this->_message_template_group->messenger(),
2394
-            'mt'     => $this->_message_template_group->message_type(),
2395
-            'GRP_ID' => $this->_message_template_group->GRP_ID(),
2396
-        );
2397
-        // first we need to see if there are any fields
2398
-        $fields = $this->_message_template_group->messenger_obj()->get_test_settings_fields();
2399
-
2400
-        if (! empty($fields)) {
2401
-            // yup there be fields
2402
-            foreach ($fields as $field => $config) {
2403
-                $field_id = $this->_message_template_group->messenger() . '_' . $field;
2404
-                $existing = $this->_message_template_group->messenger_obj()->get_existing_test_settings();
2405
-                $default = isset($config['default']) ? $config['default'] : '';
2406
-                $default = isset($config['value']) ? $config['value'] : $default;
2407
-
2408
-                // if type is hidden and the value is empty
2409
-                // something may have gone wrong so let's correct with the defaults
2410
-                $fix = $config['input'] === 'hidden'
2411
-                       && isset($existing[ $field ])
2412
-                       && empty($existing[ $field ])
2413
-                    ? $default
2414
-                    : '';
2415
-                $existing[ $field ] = isset($existing[ $field ]) && empty($fix)
2416
-                    ? $existing[ $field ]
2417
-                    : $fix;
2418
-
2419
-                $template_form_fields[ $field_id ] = array(
2420
-                    'name'       => 'test_settings_fld[' . $field . ']',
2421
-                    'label'      => $config['label'],
2422
-                    'input'      => $config['input'],
2423
-                    'type'       => $config['type'],
2424
-                    'required'   => $config['required'],
2425
-                    'validation' => $config['validation'],
2426
-                    'value'      => isset($existing[ $field ]) ? $existing[ $field ] : $default,
2427
-                    'css_class'  => $config['css_class'],
2428
-                    'options'    => isset($config['options']) ? $config['options'] : array(),
2429
-                    'default'    => $default,
2430
-                    'format'     => $config['format'],
2431
-                );
2432
-            }
2433
-        }
2434
-
2435
-        $test_settings_fields = ! empty($template_form_fields)
2436
-            ? $this->_generate_admin_form_fields($template_form_fields, 'string', 'ee_tst_settings_flds')
2437
-            : '';
2438
-
2439
-        $test_settings_html = '';
2440
-        // print out $test_settings_fields
2441
-        if (! empty($test_settings_fields)) {
2442
-            echo $test_settings_fields;
2443
-            $test_settings_html = '<input type="submit" class="button-primary mtp-test-button alignright" ';
2444
-            $test_settings_html .= 'name="test_button" value="';
2445
-            $test_settings_html .= esc_html__('Test Send', 'event_espresso');
2446
-            $test_settings_html .= '" /><div style="clear:both"></div>';
2447
-        }
2448
-
2449
-        // and button
2450
-        $test_settings_html .= '<p>'
2451
-                               . esc_html__('Need to reset this message type and start over?', 'event_espresso')
2452
-                               . '</p>';
2453
-        $test_settings_html .= '<div class="publishing-action alignright resetbutton">';
2454
-        $test_settings_html .= $this->get_action_link_or_button(
2455
-            'reset_to_default',
2456
-            'reset',
2457
-            $extra_args,
2458
-            'button-primary reset-default-button'
2459
-        );
2460
-        $test_settings_html .= '</div><div style="clear:both"></div>';
2461
-        echo $test_settings_html;
2462
-    }
2463
-
2464
-
2465
-    /**
2466
-     * This returns the shortcode selector skeleton for a given context and field.
2467
-     *
2468
-     * @since 4.9.rc.000
2469
-     * @param string $field           The name of the field retrieving shortcodes for.
2470
-     * @param string $linked_input_id The css id of the input that the shortcodes get added to.
2471
-     * @return string
2472
-     * @throws DomainException
2473
-     * @throws EE_Error
2474
-     * @throws InvalidArgumentException
2475
-     * @throws ReflectionException
2476
-     * @throws InvalidDataTypeException
2477
-     * @throws InvalidInterfaceException
2478
-     */
2479
-    protected function _get_shortcode_selector($field, $linked_input_id)
2480
-    {
2481
-        $template_args = array(
2482
-            'shortcodes'      => $this->_get_shortcodes(array($field), true),
2483
-            'fieldname'       => $field,
2484
-            'linked_input_id' => $linked_input_id,
2485
-        );
2486
-
2487
-        return EEH_Template::display_template(
2488
-            EE_MSG_TEMPLATE_PATH . 'shortcode_selector_skeleton.template.php',
2489
-            $template_args,
2490
-            true
2491
-        );
2492
-    }
2493
-
2494
-
2495
-    /**
2496
-     * This just takes care of returning the meta box content for shortcodes (only used on the edit message template
2497
-     * page)
2498
-     *
2499
-     * @access public
2500
-     * @return void
2501
-     * @throws EE_Error
2502
-     * @throws InvalidArgumentException
2503
-     * @throws ReflectionException
2504
-     * @throws InvalidDataTypeException
2505
-     * @throws InvalidInterfaceException
2506
-     */
2507
-    public function shortcode_meta_box()
2508
-    {
2509
-        $shortcodes = $this->_get_shortcodes(array(), false); // just make sure shortcodes property is set
2510
-        // $messenger = $this->_message_template_group->messenger_obj();
2511
-        // now let's set the content depending on the status of the shortcodes array
2512
-        if (empty($shortcodes)) {
2513
-            $content = '<p>' . esc_html__('There are no valid shortcodes available', 'event_espresso') . '</p>';
2514
-            echo $content;
2515
-        } else {
2516
-            // $alt = 0;
2517
-            ?>
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 (
243
+			empty($messenger_options)
244
+			|| ! is_array($messenger_options)
245
+			|| count($messenger_options) === 1
246
+		) {
247
+			return '';
248
+		}
249
+		// merge in default
250
+		$messenger_options = array_merge(
251
+			array('none_selected' => esc_html__('Show All Messengers', 'event_espresso')),
252
+			$messenger_options
253
+		);
254
+		$input = new EE_Select_Input(
255
+			$messenger_options,
256
+			array(
257
+				'html_name'  => 'ee_messenger_filter_by',
258
+				'html_id'    => 'ee_messenger_filter_by',
259
+				'html_class' => 'wide',
260
+				'default'    => isset($this->_req_data['ee_messenger_filter_by'])
261
+					? sanitize_title($this->_req_data['ee_messenger_filter_by'])
262
+					: 'none_selected',
263
+			)
264
+		);
265
+
266
+		return $input->get_html_for_input();
267
+	}
268
+
269
+
270
+	/**
271
+	 * Generate select input with provided message type options array.
272
+	 *
273
+	 * @param array $message_type_options Array of message types indexed by message type slug, and values are the
274
+	 *                                    message type labels
275
+	 * @return string
276
+	 * @throws EE_Error
277
+	 */
278
+	public function get_message_types_select_input($message_type_options)
279
+	{
280
+		// if empty or count of options is 1 then just return an empty string
281
+		if (
282
+			empty($message_type_options)
283
+			|| ! is_array($message_type_options)
284
+			|| count($message_type_options) === 1
285
+		) {
286
+			return '';
287
+		}
288
+		// merge in default
289
+		$message_type_options = array_merge(
290
+			array('none_selected' => esc_html__('Show All Message Types', 'event_espresso')),
291
+			$message_type_options
292
+		);
293
+		$input = new EE_Select_Input(
294
+			$message_type_options,
295
+			array(
296
+				'html_name'  => 'ee_message_type_filter_by',
297
+				'html_id'    => 'ee_message_type_filter_by',
298
+				'html_class' => 'wide',
299
+				'default'    => isset($this->_req_data['ee_message_type_filter_by'])
300
+					? sanitize_title($this->_req_data['ee_message_type_filter_by'])
301
+					: 'none_selected',
302
+			)
303
+		);
304
+
305
+		return $input->get_html_for_input();
306
+	}
307
+
308
+
309
+	/**
310
+	 * Generate select input with provide message type contexts array.
311
+	 *
312
+	 * @param array $context_options Array of message type contexts indexed by context slug, and values are the
313
+	 *                               context label.
314
+	 * @return string
315
+	 * @throws EE_Error
316
+	 */
317
+	public function get_contexts_for_message_types_select_input($context_options)
318
+	{
319
+		// if empty or count of options is one then just return empty string
320
+		if (
321
+			empty($context_options)
322
+			|| ! is_array($context_options)
323
+			|| count($context_options) === 1
324
+		) {
325
+			return '';
326
+		}
327
+		// merge in default
328
+		$context_options = array_merge(
329
+			array('none_selected' => esc_html__('Show all Contexts', 'event_espresso')),
330
+			$context_options
331
+		);
332
+		$input = new EE_Select_Input(
333
+			$context_options,
334
+			array(
335
+				'html_name'  => 'ee_context_filter_by',
336
+				'html_id'    => 'ee_context_filter_by',
337
+				'html_class' => 'wide',
338
+				'default'    => isset($this->_req_data['ee_context_filter_by'])
339
+					? sanitize_title($this->_req_data['ee_context_filter_by'])
340
+					: 'none_selected',
341
+			)
342
+		);
343
+
344
+		return $input->get_html_for_input();
345
+	}
346
+
347
+
348
+	protected function _ajax_hooks()
349
+	{
350
+		add_action('wp_ajax_activate_messenger', array($this, 'activate_messenger_toggle'));
351
+		add_action('wp_ajax_activate_mt', array($this, 'activate_mt_toggle'));
352
+		add_action('wp_ajax_ee_msgs_save_settings', array($this, 'save_settings'));
353
+		add_action('wp_ajax_ee_msgs_update_mt_form', array($this, 'update_mt_form'));
354
+		add_action('wp_ajax_switch_template_pack', array($this, 'switch_template_pack'));
355
+		add_action('wp_ajax_toggle_context_template', array($this, 'toggle_context_template'));
356
+	}
357
+
358
+
359
+	protected function _define_page_props()
360
+	{
361
+		$this->_admin_page_title = $this->page_label;
362
+		$this->_labels = array(
363
+			'buttons'    => array(
364
+				'add'    => esc_html__('Add New Message Template', 'event_espresso'),
365
+				'edit'   => esc_html__('Edit Message Template', 'event_espresso'),
366
+				'delete' => esc_html__('Delete Message Template', 'event_espresso'),
367
+			),
368
+			'publishbox' => esc_html__('Update Actions', 'event_espresso'),
369
+		);
370
+	}
371
+
372
+
373
+	/**
374
+	 *        an array for storing key => value pairs of request actions and their corresponding methods
375
+	 *
376
+	 * @access protected
377
+	 * @return void
378
+	 */
379
+	protected function _set_page_routes()
380
+	{
381
+		$grp_id = ! empty($this->_req_data['GRP_ID']) && ! is_array($this->_req_data['GRP_ID'])
382
+			? $this->_req_data['GRP_ID']
383
+			: 0;
384
+		$grp_id = empty($grp_id) && ! empty($this->_req_data['id'])
385
+			? $this->_req_data['id']
386
+			: $grp_id;
387
+		$msg_id = ! empty($this->_req_data['MSG_ID']) && ! is_array($this->_req_data['MSG_ID'])
388
+			? $this->_req_data['MSG_ID']
389
+			: 0;
390
+
391
+		$this->_page_routes = array(
392
+			'default'                          => array(
393
+				'func'       => '_message_queue_list_table',
394
+				'capability' => 'ee_read_global_messages',
395
+			),
396
+			'global_mtps'                      => array(
397
+				'func'       => '_ee_default_messages_overview_list_table',
398
+				'capability' => 'ee_read_global_messages',
399
+			),
400
+			'custom_mtps'                      => array(
401
+				'func'       => '_custom_mtps_preview',
402
+				'capability' => 'ee_read_messages',
403
+			),
404
+			'add_new_message_template'         => array(
405
+				'func'       => '_add_message_template',
406
+				'capability' => 'ee_edit_messages',
407
+				'noheader'   => true,
408
+			),
409
+			'edit_message_template'            => array(
410
+				'func'       => '_edit_message_template',
411
+				'capability' => 'ee_edit_message',
412
+				'obj_id'     => $grp_id,
413
+			),
414
+			'preview_message'                  => array(
415
+				'func'               => '_preview_message',
416
+				'capability'         => 'ee_read_message',
417
+				'obj_id'             => $grp_id,
418
+				'noheader'           => true,
419
+				'headers_sent_route' => 'display_preview_message',
420
+			),
421
+			'display_preview_message'          => array(
422
+				'func'       => '_display_preview_message',
423
+				'capability' => 'ee_read_message',
424
+				'obj_id'     => $grp_id,
425
+			),
426
+			'insert_message_template'          => array(
427
+				'func'       => '_insert_or_update_message_template',
428
+				'capability' => 'ee_edit_messages',
429
+				'args'       => array('new_template' => true),
430
+				'noheader'   => true,
431
+			),
432
+			'update_message_template'          => array(
433
+				'func'       => '_insert_or_update_message_template',
434
+				'capability' => 'ee_edit_message',
435
+				'obj_id'     => $grp_id,
436
+				'args'       => array('new_template' => false),
437
+				'noheader'   => true,
438
+			),
439
+			'trash_message_template'           => array(
440
+				'func'       => '_trash_or_restore_message_template',
441
+				'capability' => 'ee_delete_message',
442
+				'obj_id'     => $grp_id,
443
+				'args'       => array('trash' => true, 'all' => true),
444
+				'noheader'   => true,
445
+			),
446
+			'trash_message_template_context'   => array(
447
+				'func'       => '_trash_or_restore_message_template',
448
+				'capability' => 'ee_delete_message',
449
+				'obj_id'     => $grp_id,
450
+				'args'       => array('trash' => true),
451
+				'noheader'   => true,
452
+			),
453
+			'restore_message_template'         => array(
454
+				'func'       => '_trash_or_restore_message_template',
455
+				'capability' => 'ee_delete_message',
456
+				'obj_id'     => $grp_id,
457
+				'args'       => array('trash' => false, 'all' => true),
458
+				'noheader'   => true,
459
+			),
460
+			'restore_message_template_context' => array(
461
+				'func'       => '_trash_or_restore_message_template',
462
+				'capability' => 'ee_delete_message',
463
+				'obj_id'     => $grp_id,
464
+				'args'       => array('trash' => false),
465
+				'noheader'   => true,
466
+			),
467
+			'delete_message_template'          => array(
468
+				'func'       => '_delete_message_template',
469
+				'capability' => 'ee_delete_message',
470
+				'obj_id'     => $grp_id,
471
+				'noheader'   => true,
472
+			),
473
+			'reset_to_default'                 => array(
474
+				'func'       => '_reset_to_default_template',
475
+				'capability' => 'ee_edit_message',
476
+				'obj_id'     => $grp_id,
477
+				'noheader'   => true,
478
+			),
479
+			'settings'                         => array(
480
+				'func'       => '_settings',
481
+				'capability' => 'manage_options',
482
+			),
483
+			'update_global_settings'           => array(
484
+				'func'       => '_update_global_settings',
485
+				'capability' => 'manage_options',
486
+				'noheader'   => true,
487
+			),
488
+			'generate_now'                     => array(
489
+				'func'       => '_generate_now',
490
+				'capability' => 'ee_send_message',
491
+				'noheader'   => true,
492
+			),
493
+			'generate_and_send_now'            => array(
494
+				'func'       => '_generate_and_send_now',
495
+				'capability' => 'ee_send_message',
496
+				'noheader'   => true,
497
+			),
498
+			'queue_for_resending'              => array(
499
+				'func'       => '_queue_for_resending',
500
+				'capability' => 'ee_send_message',
501
+				'noheader'   => true,
502
+			),
503
+			'send_now'                         => array(
504
+				'func'       => '_send_now',
505
+				'capability' => 'ee_send_message',
506
+				'noheader'   => true,
507
+			),
508
+			'delete_ee_message'                => array(
509
+				'func'       => '_delete_ee_messages',
510
+				'capability' => 'ee_delete_messages',
511
+				'noheader'   => true,
512
+			),
513
+			'delete_ee_messages'               => array(
514
+				'func'       => '_delete_ee_messages',
515
+				'capability' => 'ee_delete_messages',
516
+				'noheader'   => true,
517
+				'obj_id'     => $msg_id,
518
+			),
519
+		);
520
+	}
521
+
522
+
523
+	protected function _set_page_config()
524
+	{
525
+		$this->_page_config = array(
526
+			'default'                  => array(
527
+				'nav'           => array(
528
+					'label' => esc_html__('Message Activity', 'event_espresso'),
529
+					'order' => 10,
530
+				),
531
+				'list_table'    => 'EE_Message_List_Table',
532
+				// 'qtips' => array( 'EE_Message_List_Table_Tips' ),
533
+				'require_nonce' => false,
534
+			),
535
+			'global_mtps'              => array(
536
+				'nav'           => array(
537
+					'label' => esc_html__('Default Message Templates', 'event_espresso'),
538
+					'order' => 20,
539
+				),
540
+				'list_table'    => 'Messages_Template_List_Table',
541
+				'help_tabs'     => array(
542
+					'messages_overview_help_tab'                                => array(
543
+						'title'    => esc_html__('Messages Overview', 'event_espresso'),
544
+						'filename' => 'messages_overview',
545
+					),
546
+					'messages_overview_messages_table_column_headings_help_tab' => array(
547
+						'title'    => esc_html__('Messages Table Column Headings', 'event_espresso'),
548
+						'filename' => 'messages_overview_table_column_headings',
549
+					),
550
+					'messages_overview_messages_filters_help_tab'               => array(
551
+						'title'    => esc_html__('Message Filters', 'event_espresso'),
552
+						'filename' => 'messages_overview_filters',
553
+					),
554
+					'messages_overview_messages_views_help_tab'                 => array(
555
+						'title'    => esc_html__('Message Views', 'event_espresso'),
556
+						'filename' => 'messages_overview_views',
557
+					),
558
+					'message_overview_message_types_help_tab'                   => array(
559
+						'title'    => esc_html__('Message Types', 'event_espresso'),
560
+						'filename' => 'messages_overview_types',
561
+					),
562
+					'messages_overview_messengers_help_tab'                     => array(
563
+						'title'    => esc_html__('Messengers', 'event_espresso'),
564
+						'filename' => 'messages_overview_messengers',
565
+					),
566
+				),
567
+				// disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
568
+				// 'help_tour'     => array('Messages_Overview_Help_Tour'),
569
+				'require_nonce' => false,
570
+			),
571
+			'custom_mtps'              => array(
572
+				'nav'           => array(
573
+					'label' => esc_html__('Custom Message Templates', 'event_espresso'),
574
+					'order' => 30,
575
+				),
576
+				'help_tabs'     => array(),
577
+				// disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
578
+				// 'help_tour'     => array(),
579
+				'require_nonce' => false,
580
+			),
581
+			'add_new_message_template' => array(
582
+				'nav'           => array(
583
+					'label'      => esc_html__('Add New Message Templates', 'event_espresso'),
584
+					'order'      => 5,
585
+					'persistent' => false,
586
+				),
587
+				'require_nonce' => false,
588
+			),
589
+			'edit_message_template'    => array(
590
+				'labels'        => array(
591
+					'buttons'    => array(
592
+						'reset' => esc_html__('Reset Templates', 'event_espresso'),
593
+					),
594
+					'publishbox' => esc_html__('Update Actions', 'event_espresso'),
595
+				),
596
+				'nav'           => array(
597
+					'label'      => esc_html__('Edit Message Templates', 'event_espresso'),
598
+					'order'      => 5,
599
+					'persistent' => false,
600
+					'url'        => '',
601
+				),
602
+				'metaboxes'     => array('_publish_post_box', '_register_edit_meta_boxes'),
603
+				'has_metaboxes' => true,
604
+				// disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
605
+				// 'help_tour'     => array('Message_Templates_Edit_Help_Tour'),
606
+				'help_tabs'     => array(
607
+					'edit_message_template'            => array(
608
+						'title'    => esc_html__('Message Template Editor', 'event_espresso'),
609
+						'callback' => 'edit_message_template_help_tab',
610
+					),
611
+					'message_templates_help_tab'       => array(
612
+						'title'    => esc_html__('Message Templates', 'event_espresso'),
613
+						'filename' => 'messages_templates',
614
+					),
615
+					'message_template_shortcodes'      => array(
616
+						'title'    => esc_html__('Message Shortcodes', 'event_espresso'),
617
+						'callback' => 'message_template_shortcodes_help_tab',
618
+					),
619
+					'message_preview_help_tab'         => array(
620
+						'title'    => esc_html__('Message Preview', 'event_espresso'),
621
+						'filename' => 'messages_preview',
622
+					),
623
+					'messages_overview_other_help_tab' => array(
624
+						'title'    => esc_html__('Messages Other', 'event_espresso'),
625
+						'filename' => 'messages_overview_other',
626
+					),
627
+				),
628
+				'require_nonce' => false,
629
+			),
630
+			'display_preview_message'  => array(
631
+				'nav'           => array(
632
+					'label'      => esc_html__('Message Preview', 'event_espresso'),
633
+					'order'      => 5,
634
+					'url'        => '',
635
+					'persistent' => false,
636
+				),
637
+				'help_tabs'     => array(
638
+					'preview_message' => array(
639
+						'title'    => esc_html__('About Previews', 'event_espresso'),
640
+						'callback' => 'preview_message_help_tab',
641
+					),
642
+				),
643
+				'require_nonce' => false,
644
+			),
645
+			'settings'                 => array(
646
+				'nav'           => array(
647
+					'label' => esc_html__('Settings', 'event_espresso'),
648
+					'order' => 40,
649
+				),
650
+				'metaboxes'     => array('_messages_settings_metaboxes'),
651
+				'help_tabs'     => array(
652
+					'messages_settings_help_tab'               => array(
653
+						'title'    => esc_html__('Messages Settings', 'event_espresso'),
654
+						'filename' => 'messages_settings',
655
+					),
656
+					'messages_settings_message_types_help_tab' => array(
657
+						'title'    => esc_html__('Activating / Deactivating Message Types', 'event_espresso'),
658
+						'filename' => 'messages_settings_message_types',
659
+					),
660
+					'messages_settings_messengers_help_tab'    => array(
661
+						'title'    => esc_html__('Activating / Deactivating Messengers', 'event_espresso'),
662
+						'filename' => 'messages_settings_messengers',
663
+					),
664
+				),
665
+				// disabled temporarily. see: https://github.com/eventespresso/eventsmart.com-website/issues/836
666
+				// 'help_tour'     => array('Messages_Settings_Help_Tour'),
667
+				'require_nonce' => false,
668
+			),
669
+		);
670
+	}
671
+
672
+
673
+	protected function _add_screen_options()
674
+	{
675
+		// todo
676
+	}
677
+
678
+
679
+	protected function _add_screen_options_global_mtps()
680
+	{
681
+		/**
682
+		 * Note: the reason for the value swap here on $this->_admin_page_title is because $this->_per_page_screen_options
683
+		 * uses the $_admin_page_title property and we want different outputs in the different spots.
684
+		 */
685
+		$page_title = $this->_admin_page_title;
686
+		$this->_admin_page_title = esc_html__('Global Message Templates', 'event_espresso');
687
+		$this->_per_page_screen_option();
688
+		$this->_admin_page_title = $page_title;
689
+	}
690
+
691
+
692
+	protected function _add_screen_options_default()
693
+	{
694
+		$this->_admin_page_title = esc_html__('Message Activity', 'event_espresso');
695
+		$this->_per_page_screen_option();
696
+	}
697
+
698
+
699
+	// none of the below group are currently used for Messages
700
+	protected function _add_feature_pointers()
701
+	{
702
+	}
703
+
704
+	public function admin_init()
705
+	{
706
+	}
707
+
708
+	public function admin_notices()
709
+	{
710
+	}
711
+
712
+	public function admin_footer_scripts()
713
+	{
714
+	}
715
+
716
+
717
+	public function messages_help_tab()
718
+	{
719
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_help_tab.template.php');
720
+	}
721
+
722
+
723
+	public function messengers_help_tab()
724
+	{
725
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messenger_help_tab.template.php');
726
+	}
727
+
728
+
729
+	public function message_types_help_tab()
730
+	{
731
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_type_help_tab.template.php');
732
+	}
733
+
734
+
735
+	public function messages_overview_help_tab()
736
+	{
737
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_overview_help_tab.template.php');
738
+	}
739
+
740
+
741
+	public function message_templates_help_tab()
742
+	{
743
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_message_templates_help_tab.template.php');
744
+	}
745
+
746
+
747
+	public function edit_message_template_help_tab()
748
+	{
749
+		$args['img1'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/editor.png' . '" alt="'
750
+						. esc_attr__('Editor Title', 'event_espresso')
751
+						. '" />';
752
+		$args['img2'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/switch-context.png' . '" alt="'
753
+						. esc_attr__('Context Switcher and Preview', 'event_espresso')
754
+						. '" />';
755
+		$args['img3'] = '<img class="left" src="' . EE_MSG_ASSETS_URL . 'images/form-fields.png' . '" alt="'
756
+						. esc_attr__('Message Template Form Fields', 'event_espresso')
757
+						. '" />';
758
+		$args['img4'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/shortcodes-metabox.png' . '" alt="'
759
+						. esc_attr__('Shortcodes Metabox', 'event_espresso')
760
+						. '" />';
761
+		$args['img5'] = '<img class="right" src="' . EE_MSG_ASSETS_URL . 'images/publish-meta-box.png' . '" alt="'
762
+						. esc_attr__('Publish Metabox', 'event_espresso')
763
+						. '" />';
764
+		EEH_Template::display_template(
765
+			EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_templates_editor_help_tab.template.php',
766
+			$args
767
+		);
768
+	}
769
+
770
+
771
+	public function message_template_shortcodes_help_tab()
772
+	{
773
+		$this->_set_shortcodes();
774
+		$args['shortcodes'] = $this->_shortcodes;
775
+		EEH_Template::display_template(
776
+			EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_shortcodes_help_tab.template.php',
777
+			$args
778
+		);
779
+	}
780
+
781
+
782
+	public function preview_message_help_tab()
783
+	{
784
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_preview_help_tab.template.php');
785
+	}
786
+
787
+
788
+	public function settings_help_tab()
789
+	{
790
+		$args['img1'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-active.png'
791
+						. '" alt="' . esc_attr__('Active Email Tab', 'event_espresso') . '" />';
792
+		$args['img2'] = '<img class="inline-text" src="' . EE_MSG_ASSETS_URL . 'images/email-tab-inactive.png'
793
+						. '" alt="' . esc_attr__('Inactive Email Tab', 'event_espresso') . '" />';
794
+		$args['img3'] = '<div class="switch">'
795
+						. '<input class="ee-on-off-toggle ee-toggle-round-flat"'
796
+						. ' type="checkbox" checked="checked">'
797
+						. '<label for="ee-on-off-toggle-on"></label>'
798
+						. '</div>';
799
+		$args['img4'] = '<div class="switch">'
800
+						. '<input class="ee-on-off-toggle ee-toggle-round-flat"'
801
+						. ' type="checkbox">'
802
+						. '<label for="ee-on-off-toggle-on"></label>'
803
+						. '</div>';
804
+		EEH_Template::display_template(EE_MSG_TEMPLATE_PATH . 'ee_msg_messages_settings_help_tab.template.php', $args);
805
+	}
806
+
807
+
808
+	public function load_scripts_styles()
809
+	{
810
+		wp_register_style('espresso_ee_msg', EE_MSG_ASSETS_URL . 'ee_message_admin.css', EVENT_ESPRESSO_VERSION);
811
+		wp_enqueue_style('espresso_ee_msg');
812
+
813
+		wp_register_script(
814
+			'ee-messages-settings',
815
+			EE_MSG_ASSETS_URL . 'ee-messages-settings.js',
816
+			array('jquery-ui-droppable', 'ee-serialize-full-array'),
817
+			EVENT_ESPRESSO_VERSION,
818
+			true
819
+		);
820
+		wp_register_script(
821
+			'ee-msg-list-table-js',
822
+			EE_MSG_ASSETS_URL . 'ee_message_admin_list_table.js',
823
+			array('ee-dialog'),
824
+			EVENT_ESPRESSO_VERSION
825
+		);
826
+	}
827
+
828
+
829
+	public function load_scripts_styles_default()
830
+	{
831
+		wp_enqueue_script('ee-msg-list-table-js');
832
+	}
833
+
834
+
835
+	public function wp_editor_css($mce_css)
836
+	{
837
+		// if we're on the edit_message_template route
838
+		if ($this->_req_action === 'edit_message_template' && $this->_active_messenger instanceof EE_messenger) {
839
+			$message_type_name = $this->_active_message_type_name;
840
+
841
+			// we're going to REPLACE the existing mce css
842
+			// we need to get the css file location from the active messenger
843
+			$mce_css = $this->_active_messenger->get_variation(
844
+				$this->_template_pack,
845
+				$message_type_name,
846
+				true,
847
+				'wpeditor',
848
+				$this->_variation
849
+			);
850
+		}
851
+
852
+		return $mce_css;
853
+	}
854
+
855
+
856
+	public function load_scripts_styles_edit_message_template()
857
+	{
858
+
859
+		$this->_set_shortcodes();
860
+
861
+		EE_Registry::$i18n_js_strings['confirm_default_reset'] = sprintf(
862
+			esc_html__(
863
+				'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.',
864
+				'event_espresso'
865
+			),
866
+			$this->_message_template_group->messenger_obj()->label['singular'],
867
+			$this->_message_template_group->message_type_obj()->label['singular']
868
+		);
869
+		EE_Registry::$i18n_js_strings['confirm_switch_template_pack'] = esc_html__(
870
+			'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?',
871
+			'event_espresso'
872
+		);
873
+		EE_Registry::$i18n_js_strings['server_error'] = esc_html__(
874
+			'An unknown error occurred on the server while attempting to process your request. Please refresh the page and try again or contact support.',
875
+			'event_espresso'
876
+		);
877
+
878
+		wp_register_script(
879
+			'ee_msgs_edit_js',
880
+			EE_MSG_ASSETS_URL . 'ee_message_editor.js',
881
+			array('jquery'),
882
+			EVENT_ESPRESSO_VERSION
883
+		);
884
+
885
+		wp_enqueue_script('ee_admin_js');
886
+		wp_enqueue_script('ee_msgs_edit_js');
887
+
888
+		// add in special css for tiny_mce
889
+		add_filter('mce_css', array($this, 'wp_editor_css'));
890
+	}
891
+
892
+
893
+	public function load_scripts_styles_display_preview_message()
894
+	{
895
+
896
+		$this->_set_message_template_group();
897
+
898
+		if (isset($this->_req_data['messenger'])) {
899
+			$this->_active_messenger = $this->_message_resource_manager->get_active_messenger(
900
+				$this->_req_data['messenger']
901
+			);
902
+		}
903
+
904
+		$message_type_name = isset($this->_req_data['message_type']) ? $this->_req_data['message_type'] : '';
905
+
906
+
907
+		wp_enqueue_style(
908
+			'espresso_preview_css',
909
+			$this->_active_messenger->get_variation(
910
+				$this->_template_pack,
911
+				$message_type_name,
912
+				true,
913
+				'preview',
914
+				$this->_variation
915
+			)
916
+		);
917
+	}
918
+
919
+
920
+	public function load_scripts_styles_settings()
921
+	{
922
+		wp_register_style(
923
+			'ee-message-settings',
924
+			EE_MSG_ASSETS_URL . 'ee_message_settings.css',
925
+			array(),
926
+			EVENT_ESPRESSO_VERSION
927
+		);
928
+		wp_enqueue_style('ee-text-links');
929
+		wp_enqueue_style('ee-message-settings');
930
+		wp_enqueue_script('ee-messages-settings');
931
+	}
932
+
933
+
934
+	/**
935
+	 * set views array for List Table
936
+	 */
937
+	public function _set_list_table_views_global_mtps()
938
+	{
939
+		$this->_views = array(
940
+			'in_use' => array(
941
+				'slug'  => 'in_use',
942
+				'label' => esc_html__('In Use', 'event_espresso'),
943
+				'count' => 0,
944
+			),
945
+		);
946
+	}
947
+
948
+
949
+	/**
950
+	 * Set views array for the Custom Template List Table
951
+	 */
952
+	public function _set_list_table_views_custom_mtps()
953
+	{
954
+		$this->_set_list_table_views_global_mtps();
955
+		$this->_views['in_use']['bulk_action'] = array(
956
+			'trash_message_template' => esc_html__('Move to Trash', 'event_espresso'),
957
+		);
958
+	}
959
+
960
+
961
+	/**
962
+	 * set views array for message queue list table
963
+	 *
964
+	 * @throws InvalidDataTypeException
965
+	 * @throws InvalidInterfaceException
966
+	 * @throws InvalidArgumentException
967
+	 * @throws EE_Error
968
+	 * @throws ReflectionException
969
+	 */
970
+	public function _set_list_table_views_default()
971
+	{
972
+		EE_Registry::instance()->load_helper('Template');
973
+
974
+		$common_bulk_actions = EE_Registry::instance()->CAP->current_user_can(
975
+			'ee_send_message',
976
+			'message_list_table_bulk_actions'
977
+		)
978
+			? array(
979
+				'generate_now'          => esc_html__('Generate Now', 'event_espresso'),
980
+				'generate_and_send_now' => esc_html__('Generate and Send Now', 'event_espresso'),
981
+				'queue_for_resending'   => esc_html__('Queue for Resending', 'event_espresso'),
982
+				'send_now'              => esc_html__('Send Now', 'event_espresso'),
983
+			)
984
+			: array();
985
+
986
+		$delete_bulk_action = EE_Registry::instance()->CAP->current_user_can(
987
+			'ee_delete_messages',
988
+			'message_list_table_bulk_actions'
989
+		)
990
+			? array('delete_ee_messages' => esc_html__('Delete Messages', 'event_espresso'))
991
+			: array();
992
+
993
+
994
+		$this->_views = array(
995
+			'all' => array(
996
+				'slug'        => 'all',
997
+				'label'       => esc_html__('All', 'event_espresso'),
998
+				'count'       => 0,
999
+				'bulk_action' => array_merge($common_bulk_actions, $delete_bulk_action),
1000
+			),
1001
+		);
1002
+
1003
+
1004
+		foreach (EEM_Message::instance()->all_statuses() as $status) {
1005
+			if ($status === EEM_Message::status_debug_only && ! EEM_Message::debug()) {
1006
+				continue;
1007
+			}
1008
+			$status_bulk_actions = $common_bulk_actions;
1009
+			// unset bulk actions not applying to status
1010
+			if (! empty($status_bulk_actions)) {
1011
+				switch ($status) {
1012
+					case EEM_Message::status_idle:
1013
+					case EEM_Message::status_resend:
1014
+						$status_bulk_actions['send_now'] = $common_bulk_actions['send_now'];
1015
+						break;
1016
+
1017
+					case EEM_Message::status_failed:
1018
+					case EEM_Message::status_debug_only:
1019
+					case EEM_Message::status_messenger_executing:
1020
+						$status_bulk_actions = array();
1021
+						break;
1022
+
1023
+					case EEM_Message::status_incomplete:
1024
+						unset($status_bulk_actions['queue_for_resending'], $status_bulk_actions['send_now']);
1025
+						break;
1026
+
1027
+					case EEM_Message::status_retry:
1028
+					case EEM_Message::status_sent:
1029
+						unset($status_bulk_actions['generate_now'], $status_bulk_actions['generate_and_send_now']);
1030
+						break;
1031
+				}
1032
+			}
1033
+
1034
+			// skip adding messenger executing status to views because it will be included with the Failed view.
1035
+			if ($status === EEM_Message::status_messenger_executing) {
1036
+				continue;
1037
+			}
1038
+
1039
+			$this->_views[ strtolower($status) ] = array(
1040
+				'slug'        => strtolower($status),
1041
+				'label'       => EEH_Template::pretty_status($status, false, 'sentence'),
1042
+				'count'       => 0,
1043
+				'bulk_action' => array_merge($status_bulk_actions, $delete_bulk_action),
1044
+			);
1045
+		}
1046
+	}
1047
+
1048
+
1049
+	protected function _ee_default_messages_overview_list_table()
1050
+	{
1051
+		$this->_admin_page_title = esc_html__('Default Message Templates', 'event_espresso');
1052
+		$this->display_admin_list_table_page_with_no_sidebar();
1053
+	}
1054
+
1055
+
1056
+	protected function _message_queue_list_table()
1057
+	{
1058
+		$this->_search_btn_label = esc_html__('Message Activity', 'event_espresso');
1059
+		$this->_template_args['per_column'] = 6;
1060
+		$this->_template_args['after_list_table'] = $this->_display_legend($this->_message_legend_items());
1061
+		$this->_template_args['before_list_table'] = '<h3>'
1062
+													 . EEM_Message::instance()->get_pretty_label_for_results()
1063
+													 . '</h3>';
1064
+		$this->display_admin_list_table_page_with_no_sidebar();
1065
+	}
1066
+
1067
+
1068
+	protected function _message_legend_items()
1069
+	{
1070
+
1071
+		$action_css_classes = EEH_MSG_Template::get_message_action_icons();
1072
+		$action_items = array();
1073
+
1074
+		foreach ($action_css_classes as $action_item => $action_details) {
1075
+			if ($action_item === 'see_notifications_for') {
1076
+				continue;
1077
+			}
1078
+			$action_items[ $action_item ] = array(
1079
+				'class' => $action_details['css_class'],
1080
+				'desc'  => $action_details['label'],
1081
+			);
1082
+		}
1083
+
1084
+		/** @type array $status_items status legend setup */
1085
+		$status_items = array(
1086
+			'incomplete_status'          => array(
1087
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_incomplete,
1088
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_incomplete, false, 'sentence'),
1089
+			),
1090
+			'idle_status'                => array(
1091
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_idle,
1092
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_idle, false, 'sentence'),
1093
+			),
1094
+			'resend_status'              => array(
1095
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_resend,
1096
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_resend, false, 'sentence'),
1097
+			),
1098
+			'messenger_executing_status' => array(
1099
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_messenger_executing,
1100
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_messenger_executing, false, 'sentence'),
1101
+			),
1102
+			'sent_status'                => array(
1103
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_sent,
1104
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_sent, false, 'sentence'),
1105
+			),
1106
+			'retry_status'               => array(
1107
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_retry,
1108
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_retry, false, 'sentence'),
1109
+			),
1110
+			'failed_status'              => array(
1111
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_failed,
1112
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_failed, false, 'sentence'),
1113
+			),
1114
+		);
1115
+		if (EEM_Message::debug()) {
1116
+			$status_items['debug_only_status'] = array(
1117
+				'class' => 'ee-status-legend ee-status-legend-' . EEM_Message::status_debug_only,
1118
+				'desc'  => EEH_Template::pretty_status(EEM_Message::status_debug_only, false, 'sentence'),
1119
+			);
1120
+		}
1121
+
1122
+		return array_merge($action_items, $status_items);
1123
+	}
1124
+
1125
+
1126
+	protected function _custom_mtps_preview()
1127
+	{
1128
+		$this->_admin_page_title = esc_html__('Custom Message Templates (Preview)', 'event_espresso');
1129
+		$this->_template_args['preview_img'] = '<img src="' . EE_MSG_ASSETS_URL . 'images/custom_mtps_preview.png"'
1130
+											   . ' alt="' . esc_attr__(
1131
+												   'Preview Custom Message Templates screenshot',
1132
+												   'event_espresso'
1133
+											   ) . '" />';
1134
+		$this->_template_args['preview_text'] = '<strong>'
1135
+												. esc_html__(
1136
+													'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.',
1137
+													'event_espresso'
1138
+												)
1139
+												. '</strong>';
1140
+
1141
+		$this->display_admin_caf_preview_page('custom_message_types', false);
1142
+	}
1143
+
1144
+
1145
+	/**
1146
+	 * get_message_templates
1147
+	 * This gets all the message templates for listing on the overview list.
1148
+	 *
1149
+	 * @access public
1150
+	 * @param int    $perpage the amount of templates groups to show per page
1151
+	 * @param string $type    the current _view we're getting templates for
1152
+	 * @param bool   $count   return count?
1153
+	 * @param bool   $all     disregard any paging info (get all data);
1154
+	 * @param bool   $global  whether to return just global (true) or custom templates (false)
1155
+	 * @return array
1156
+	 * @throws EE_Error
1157
+	 * @throws InvalidArgumentException
1158
+	 * @throws InvalidDataTypeException
1159
+	 * @throws InvalidInterfaceException
1160
+	 */
1161
+	public function get_message_templates(
1162
+		$perpage = 10,
1163
+		$type = 'in_use',
1164
+		$count = false,
1165
+		$all = false,
1166
+		$global = true
1167
+	) {
1168
+
1169
+		$MTP = EEM_Message_Template_Group::instance();
1170
+
1171
+		$this->_req_data['orderby'] = empty($this->_req_data['orderby']) ? 'GRP_ID' : $this->_req_data['orderby'];
1172
+		$orderby = $this->_req_data['orderby'];
1173
+
1174
+		$order = (isset($this->_req_data['order']) && ! empty($this->_req_data['order']))
1175
+			? $this->_req_data['order']
1176
+			: 'ASC';
1177
+
1178
+		$current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged'])
1179
+			? $this->_req_data['paged']
1180
+			: 1;
1181
+		$per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage'])
1182
+			? $this->_req_data['perpage']
1183
+			: $perpage;
1184
+
1185
+		$offset = ($current_page - 1) * $per_page;
1186
+		$limit = $all ? null : array($offset, $per_page);
1187
+
1188
+
1189
+		// options will match what is in the _views array property
1190
+		switch ($type) {
1191
+			case 'in_use':
1192
+				$templates = $MTP->get_all_active_message_templates($orderby, $order, $limit, $count, $global, true);
1193
+				break;
1194
+			default:
1195
+				$templates = $MTP->get_all_trashed_grouped_message_templates($orderby, $order, $limit, $count, $global);
1196
+		}
1197
+
1198
+		return $templates;
1199
+	}
1200
+
1201
+
1202
+	/**
1203
+	 * filters etc might need a list of installed message_types
1204
+	 *
1205
+	 * @return array an array of message type objects
1206
+	 */
1207
+	public function get_installed_message_types()
1208
+	{
1209
+		$installed_message_types = $this->_message_resource_manager->installed_message_types();
1210
+		$installed = array();
1211
+
1212
+		foreach ($installed_message_types as $message_type) {
1213
+			$installed[ $message_type->name ] = $message_type;
1214
+		}
1215
+
1216
+		return $installed;
1217
+	}
1218
+
1219
+
1220
+	/**
1221
+	 * _add_message_template
1222
+	 *
1223
+	 * This is used when creating a custom template. All Custom Templates start based off another template.
1224
+	 *
1225
+	 * @param string $message_type
1226
+	 * @param string $messenger
1227
+	 * @param string $GRP_ID
1228
+	 *
1229
+	 * @throws EE_error
1230
+	 */
1231
+	protected function _add_message_template($message_type = '', $messenger = '', $GRP_ID = '')
1232
+	{
1233
+		// set values override any request data
1234
+		$message_type = ! empty($message_type) ? $message_type : '';
1235
+		$message_type = empty($message_type) && ! empty($this->_req_data['message_type'])
1236
+			? $this->_req_data['message_type']
1237
+			: $message_type;
1238
+
1239
+		$messenger = ! empty($messenger) ? $messenger : '';
1240
+		$messenger = empty($messenger) && ! empty($this->_req_data['messenger'])
1241
+			? $this->_req_data['messenger']
1242
+			: $messenger;
1243
+
1244
+		$GRP_ID = ! empty($GRP_ID) ? $GRP_ID : '';
1245
+		$GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : $GRP_ID;
1246
+
1247
+		// we need messenger and message type.  They should be coming from the event editor. If not here then return error
1248
+		if (empty($message_type) || empty($messenger)) {
1249
+			throw new EE_Error(
1250
+				esc_html__(
1251
+					'Sorry, but we can\'t create new templates because we\'re missing the messenger or message type',
1252
+					'event_espresso'
1253
+				)
1254
+			);
1255
+		}
1256
+
1257
+		// we need the GRP_ID for the template being used as the base for the new template
1258
+		if (empty($GRP_ID)) {
1259
+			throw new EE_Error(
1260
+				esc_html__(
1261
+					'In order to create a custom message template the GRP_ID of the template being used as a base is needed',
1262
+					'event_espresso'
1263
+				)
1264
+			);
1265
+		}
1266
+
1267
+		// let's just make sure the template gets generated!
1268
+
1269
+		// we need to reassign some variables for what the insert is expecting
1270
+		$this->_req_data['MTP_messenger'] = $messenger;
1271
+		$this->_req_data['MTP_message_type'] = $message_type;
1272
+		$this->_req_data['GRP_ID'] = $GRP_ID;
1273
+		$this->_insert_or_update_message_template(true);
1274
+	}
1275
+
1276
+
1277
+	/**
1278
+	 * public wrapper for the _add_message_template method
1279
+	 *
1280
+	 * @param string $message_type     message type slug
1281
+	 * @param string $messenger        messenger slug
1282
+	 * @param int    $GRP_ID           GRP_ID for the related message template group this new template will be based
1283
+	 *                                 off of.
1284
+	 * @throws EE_error
1285
+	 */
1286
+	public function add_message_template($message_type, $messenger, $GRP_ID)
1287
+	{
1288
+		$this->_add_message_template($message_type, $messenger, $GRP_ID);
1289
+	}
1290
+
1291
+
1292
+	/**
1293
+	 * _edit_message_template
1294
+	 *
1295
+	 * @access protected
1296
+	 * @return void
1297
+	 * @throws InvalidIdentifierException
1298
+	 * @throws DomainException
1299
+	 * @throws EE_Error
1300
+	 * @throws InvalidArgumentException
1301
+	 * @throws ReflectionException
1302
+	 * @throws InvalidDataTypeException
1303
+	 * @throws InvalidInterfaceException
1304
+	 */
1305
+	protected function _edit_message_template()
1306
+	{
1307
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1308
+		$template_fields = '';
1309
+		$sidebar_fields = '';
1310
+		// we filter the tinyMCE settings to remove the validation since message templates by their nature will not have
1311
+		// valid html in the templates.
1312
+		add_filter('tiny_mce_before_init', array($this, 'filter_tinymce_init'), 10, 2);
1313
+
1314
+		$GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id'])
1315
+			? absint($this->_req_data['id'])
1316
+			: false;
1317
+
1318
+		$EVT_ID = isset($this->_req_data['evt_id']) && ! empty($this->_req_data['evt_id'])
1319
+		? absint($this->_req_data['evt_id'])
1320
+		: false;
1321
+
1322
+		$this->_set_shortcodes(); // this also sets the _message_template property.
1323
+		$message_template_group = $this->_message_template_group;
1324
+		$c_label = $message_template_group->context_label();
1325
+		$c_config = $message_template_group->contexts_config();
1326
+
1327
+		reset($c_config);
1328
+		$context = isset($this->_req_data['context']) && ! empty($this->_req_data['context'])
1329
+			? strtolower($this->_req_data['context'])
1330
+			: key($c_config);
1331
+
1332
+
1333
+		if (empty($GRP_ID)) {
1334
+			$action = 'insert_message_template';
1335
+			$edit_message_template_form_url = add_query_arg(
1336
+				array('action' => $action, 'noheader' => true),
1337
+				EE_MSG_ADMIN_URL
1338
+			);
1339
+		} else {
1340
+			$action = 'update_message_template';
1341
+			$edit_message_template_form_url = add_query_arg(
1342
+				array('action' => $action, 'noheader' => true),
1343
+				EE_MSG_ADMIN_URL
1344
+			);
1345
+		}
1346
+
1347
+		// set active messenger for this view
1348
+		$this->_active_messenger = $this->_message_resource_manager->get_active_messenger(
1349
+			$message_template_group->messenger()
1350
+		);
1351
+		$this->_active_message_type_name = $message_template_group->message_type();
1352
+
1353
+
1354
+		// Do we have any validation errors?
1355
+		$validators = $this->_get_transient();
1356
+		$v_fields = ! empty($validators) ? array_keys($validators) : array();
1357
+
1358
+
1359
+		// we need to assemble the title from Various details
1360
+		$context_label = sprintf(
1361
+			esc_html__('(%s %s)', 'event_espresso'),
1362
+			$c_config[ $context ]['label'],
1363
+			ucwords($c_label['label'])
1364
+		);
1365
+
1366
+		$title = sprintf(
1367
+			esc_html__(' %s %s Template %s', 'event_espresso'),
1368
+			ucwords($message_template_group->messenger_obj()->label['singular']),
1369
+			ucwords($message_template_group->message_type_obj()->label['singular']),
1370
+			$context_label
1371
+		);
1372
+
1373
+		$this->_template_args['GRP_ID'] = $GRP_ID;
1374
+		$this->_template_args['message_template'] = $message_template_group;
1375
+		$this->_template_args['is_extra_fields'] = false;
1376
+
1377
+
1378
+		// let's get EEH_MSG_Template so we can get template form fields
1379
+		$template_field_structure = EEH_MSG_Template::get_fields(
1380
+			$message_template_group->messenger(),
1381
+			$message_template_group->message_type()
1382
+		);
1383
+
1384
+		if (! $template_field_structure) {
1385
+			$template_field_structure = false;
1386
+			$template_fields = esc_html__(
1387
+				'There was an error in assembling the fields for this display (you should see an error message)',
1388
+				'event_espresso'
1389
+			);
1390
+		}
1391
+
1392
+
1393
+		$message_templates = $message_template_group->context_templates();
1394
+
1395
+
1396
+		// if we have the extra key.. then we need to remove the content index from the template_field_structure as it
1397
+		// will get handled in the "extra" array.
1398
+		if (is_array($template_field_structure[ $context ]) && isset($template_field_structure[ $context ]['extra'])) {
1399
+			foreach ($template_field_structure[ $context ]['extra'] as $reference_field => $new_fields) {
1400
+				unset($template_field_structure[ $context ][ $reference_field ]);
1401
+			}
1402
+		}
1403
+
1404
+		// let's loop through the template_field_structure and actually assemble the input fields!
1405
+		if (! empty($template_field_structure)) {
1406
+			foreach ($template_field_structure[ $context ] as $template_field => $field_setup_array) {
1407
+				// if this is an 'extra' template field then we need to remove any existing fields that are keyed up in
1408
+				// the extra array and reset them.
1409
+				if ($template_field === 'extra') {
1410
+					$this->_template_args['is_extra_fields'] = true;
1411
+					foreach ($field_setup_array as $reference_field => $new_fields_array) {
1412
+						$message_template = $message_templates[ $context ][ $reference_field ];
1413
+						$content = $message_template instanceof EE_Message_Template
1414
+							? $message_template->get('MTP_content')
1415
+							: '';
1416
+						foreach ($new_fields_array as $extra_field => $extra_array) {
1417
+							// let's verify if we need this extra field via the shortcodes parameter.
1418
+							$continue = false;
1419
+							if (isset($extra_array['shortcodes_required'])) {
1420
+								foreach ((array) $extra_array['shortcodes_required'] as $shortcode) {
1421
+									if (! array_key_exists($shortcode, $this->_shortcodes)) {
1422
+										$continue = true;
1423
+									}
1424
+								}
1425
+								if ($continue) {
1426
+									continue;
1427
+								}
1428
+							}
1429
+
1430
+							$field_id = $reference_field
1431
+										. '-'
1432
+										. $extra_field
1433
+										. '-content';
1434
+							$template_form_fields[ $field_id ] = $extra_array;
1435
+							$template_form_fields[ $field_id ]['name'] = 'MTP_template_fields['
1436
+																		 . $reference_field
1437
+																		 . '][content]['
1438
+																		 . $extra_field . ']';
1439
+							$css_class = isset($extra_array['css_class'])
1440
+								? $extra_array['css_class']
1441
+								: '';
1442
+
1443
+							$template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
1444
+																			  && in_array($extra_field, $v_fields, true)
1445
+																			  &&
1446
+																			  (
1447
+																				  is_array($validators[ $extra_field ])
1448
+																				  && isset($validators[ $extra_field ]['msg'])
1449
+																			  )
1450
+								? 'validate-error ' . $css_class
1451
+								: $css_class;
1452
+
1453
+							$template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
1454
+																		  && isset($content[ $extra_field ])
1455
+								? $content[ $extra_field ]
1456
+								: '';
1457
+
1458
+							// do we have a validation error?  if we do then let's use that value instead
1459
+							$template_form_fields[ $field_id ]['value'] = isset($validators[ $extra_field ])
1460
+								? $validators[ $extra_field ]['value']
1461
+								: $template_form_fields[ $field_id ]['value'];
1462
+
1463
+
1464
+							$template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
1465
+
1466
+							// shortcode selector
1467
+							$field_name_to_use = $extra_field === 'main'
1468
+								? 'content'
1469
+								: $extra_field;
1470
+							$template_form_fields[ $field_id ]['append_content'] = $this->_get_shortcode_selector(
1471
+								$field_name_to_use,
1472
+								$field_id
1473
+							);
1474
+
1475
+							if (isset($extra_array['input']) && $extra_array['input'] === 'wp_editor') {
1476
+								// we want to decode the entities
1477
+								$template_form_fields[ $field_id ]['value'] = $template_form_fields[ $field_id ]['value'];
1478
+							}/**/
1479
+						}
1480
+						$templatefield_MTP_id = $reference_field . '-MTP_ID';
1481
+						$templatefield_templatename_id = $reference_field . '-name';
1482
+
1483
+						$template_form_fields[ $templatefield_MTP_id ] = array(
1484
+							'name'       => 'MTP_template_fields[' . $reference_field . '][MTP_ID]',
1485
+							'label'      => null,
1486
+							'input'      => 'hidden',
1487
+							'type'       => 'int',
1488
+							'required'   => false,
1489
+							'validation' => false,
1490
+							'value'      => ! empty($message_templates) ? $message_template->ID() : '',
1491
+							'css_class'  => '',
1492
+							'format'     => '%d',
1493
+							'db-col'     => 'MTP_ID',
1494
+						);
1495
+
1496
+						$template_form_fields[ $templatefield_templatename_id ] = array(
1497
+							'name'       => 'MTP_template_fields[' . $reference_field . '][name]',
1498
+							'label'      => null,
1499
+							'input'      => 'hidden',
1500
+							'type'       => 'string',
1501
+							'required'   => false,
1502
+							'validation' => true,
1503
+							'value'      => $reference_field,
1504
+							'css_class'  => '',
1505
+							'format'     => '%s',
1506
+							'db-col'     => 'MTP_template_field',
1507
+						);
1508
+					}
1509
+					continue; // skip the next stuff, we got the necessary fields here for this dataset.
1510
+				} else {
1511
+					$field_id = $template_field . '-content';
1512
+					$template_form_fields[ $field_id ] = $field_setup_array;
1513
+					$template_form_fields[ $field_id ]['name'] = 'MTP_template_fields[' . $template_field . '][content]';
1514
+					$message_template = isset($message_templates[ $context ][ $template_field ])
1515
+						? $message_templates[ $context ][ $template_field ]
1516
+						: null;
1517
+					$template_form_fields[ $field_id ]['value'] = ! empty($message_templates)
1518
+																  && is_array($message_templates[ $context ])
1519
+																  && $message_template instanceof EE_Message_Template
1520
+						? $message_template->get('MTP_content')
1521
+						: '';
1522
+
1523
+					// do we have a validator error for this field?  if we do then we'll use that value instead
1524
+					$template_form_fields[ $field_id ]['value'] = isset($validators[ $template_field ])
1525
+						? $validators[ $template_field ]['value']
1526
+						: $template_form_fields[ $field_id ]['value'];
1527
+
1528
+
1529
+					$template_form_fields[ $field_id ]['db-col'] = 'MTP_content';
1530
+					$css_class = isset($field_setup_array['css_class'])
1531
+						? $field_setup_array['css_class']
1532
+						: '';
1533
+					$template_form_fields[ $field_id ]['css_class'] = ! empty($v_fields)
1534
+																	  && in_array($template_field, $v_fields, true)
1535
+																	  && isset($validators[ $template_field ]['msg'])
1536
+						? 'validate-error ' . $css_class
1537
+						: $css_class;
1538
+
1539
+					// shortcode selector
1540
+					$template_form_fields[ $field_id ]['append_content'] = $this->_get_shortcode_selector(
1541
+						$template_field,
1542
+						$field_id
1543
+					);
1544
+				}
1545
+
1546
+				// k took care of content field(s) now let's take care of others.
1547
+
1548
+				$templatefield_MTP_id = $template_field . '-MTP_ID';
1549
+				$templatefield_field_templatename_id = $template_field . '-name';
1550
+
1551
+				// foreach template field there are actually two form fields created
1552
+				$template_form_fields[ $templatefield_MTP_id ] = array(
1553
+					'name'       => 'MTP_template_fields[' . $template_field . '][MTP_ID]',
1554
+					'label'      => null,
1555
+					'input'      => 'hidden',
1556
+					'type'       => 'int',
1557
+					'required'   => false,
1558
+					'validation' => true,
1559
+					'value'      => $message_template instanceof EE_Message_Template ? $message_template->ID() : '',
1560
+					'css_class'  => '',
1561
+					'format'     => '%d',
1562
+					'db-col'     => 'MTP_ID',
1563
+				);
1564
+
1565
+				$template_form_fields[ $templatefield_field_templatename_id ] = array(
1566
+					'name'       => 'MTP_template_fields[' . $template_field . '][name]',
1567
+					'label'      => null,
1568
+					'input'      => 'hidden',
1569
+					'type'       => 'string',
1570
+					'required'   => false,
1571
+					'validation' => true,
1572
+					'value'      => $template_field,
1573
+					'css_class'  => '',
1574
+					'format'     => '%s',
1575
+					'db-col'     => 'MTP_template_field',
1576
+				);
1577
+			}
1578
+
1579
+			// add other fields
1580
+			$template_form_fields['ee-msg-current-context'] = array(
1581
+				'name'       => 'MTP_context',
1582
+				'label'      => null,
1583
+				'input'      => 'hidden',
1584
+				'type'       => 'string',
1585
+				'required'   => false,
1586
+				'validation' => true,
1587
+				'value'      => $context,
1588
+				'css_class'  => '',
1589
+				'format'     => '%s',
1590
+				'db-col'     => 'MTP_context',
1591
+			);
1592
+
1593
+			$template_form_fields['ee-msg-grp-id'] = array(
1594
+				'name'       => 'GRP_ID',
1595
+				'label'      => null,
1596
+				'input'      => 'hidden',
1597
+				'type'       => 'int',
1598
+				'required'   => false,
1599
+				'validation' => true,
1600
+				'value'      => $GRP_ID,
1601
+				'css_class'  => '',
1602
+				'format'     => '%d',
1603
+				'db-col'     => 'GRP_ID',
1604
+			);
1605
+
1606
+			$template_form_fields['ee-msg-messenger'] = array(
1607
+				'name'       => 'MTP_messenger',
1608
+				'label'      => null,
1609
+				'input'      => 'hidden',
1610
+				'type'       => 'string',
1611
+				'required'   => false,
1612
+				'validation' => true,
1613
+				'value'      => $message_template_group->messenger(),
1614
+				'css_class'  => '',
1615
+				'format'     => '%s',
1616
+				'db-col'     => 'MTP_messenger',
1617
+			);
1618
+
1619
+			$template_form_fields['ee-msg-message-type'] = array(
1620
+				'name'       => 'MTP_message_type',
1621
+				'label'      => null,
1622
+				'input'      => 'hidden',
1623
+				'type'       => 'string',
1624
+				'required'   => false,
1625
+				'validation' => true,
1626
+				'value'      => $message_template_group->message_type(),
1627
+				'css_class'  => '',
1628
+				'format'     => '%s',
1629
+				'db-col'     => 'MTP_message_type',
1630
+			);
1631
+
1632
+			$sidebar_form_fields['ee-msg-is-global'] = array(
1633
+				'name'       => 'MTP_is_global',
1634
+				'label'      => esc_html__('Global Template', 'event_espresso'),
1635
+				'input'      => 'hidden',
1636
+				'type'       => 'int',
1637
+				'required'   => false,
1638
+				'validation' => true,
1639
+				'value'      => $message_template_group->get('MTP_is_global'),
1640
+				'css_class'  => '',
1641
+				'format'     => '%d',
1642
+				'db-col'     => 'MTP_is_global',
1643
+			);
1644
+
1645
+			$sidebar_form_fields['ee-msg-is-override'] = array(
1646
+				'name'       => 'MTP_is_override',
1647
+				'label'      => esc_html__('Override all custom', 'event_espresso'),
1648
+				'input'      => $message_template_group->is_global() ? 'checkbox' : 'hidden',
1649
+				'type'       => 'int',
1650
+				'required'   => false,
1651
+				'validation' => true,
1652
+				'value'      => $message_template_group->get('MTP_is_override'),
1653
+				'css_class'  => '',
1654
+				'format'     => '%d',
1655
+				'db-col'     => 'MTP_is_override',
1656
+			);
1657
+
1658
+			$sidebar_form_fields['ee-msg-is-active'] = array(
1659
+				'name'       => 'MTP_is_active',
1660
+				'label'      => esc_html__('Active Template', 'event_espresso'),
1661
+				'input'      => 'hidden',
1662
+				'type'       => 'int',
1663
+				'required'   => false,
1664
+				'validation' => true,
1665
+				'value'      => $message_template_group->is_active(),
1666
+				'css_class'  => '',
1667
+				'format'     => '%d',
1668
+				'db-col'     => 'MTP_is_active',
1669
+			);
1670
+
1671
+			$sidebar_form_fields['ee-msg-deleted'] = array(
1672
+				'name'       => 'MTP_deleted',
1673
+				'label'      => null,
1674
+				'input'      => 'hidden',
1675
+				'type'       => 'int',
1676
+				'required'   => false,
1677
+				'validation' => true,
1678
+				'value'      => $message_template_group->get('MTP_deleted'),
1679
+				'css_class'  => '',
1680
+				'format'     => '%d',
1681
+				'db-col'     => 'MTP_deleted',
1682
+			);
1683
+			$sidebar_form_fields['ee-msg-author'] = array(
1684
+				'name'       => 'MTP_user_id',
1685
+				'label'      => esc_html__('Author', 'event_espresso'),
1686
+				'input'      => 'hidden',
1687
+				'type'       => 'int',
1688
+				'required'   => false,
1689
+				'validation' => false,
1690
+				'value'      => $message_template_group->user(),
1691
+				'format'     => '%d',
1692
+				'db-col'     => 'MTP_user_id',
1693
+			);
1694
+
1695
+			$sidebar_form_fields['ee-msg-route'] = array(
1696
+				'name'  => 'action',
1697
+				'input' => 'hidden',
1698
+				'type'  => 'string',
1699
+				'value' => $action,
1700
+			);
1701
+
1702
+			$sidebar_form_fields['ee-msg-id'] = array(
1703
+				'name'  => 'id',
1704
+				'input' => 'hidden',
1705
+				'type'  => 'int',
1706
+				'value' => $GRP_ID,
1707
+			);
1708
+			$sidebar_form_fields['ee-msg-evt-nonce'] = array(
1709
+				'name'  => $action . '_nonce',
1710
+				'input' => 'hidden',
1711
+				'type'  => 'string',
1712
+				'value' => wp_create_nonce($action . '_nonce'),
1713
+			);
1714
+
1715
+			if (isset($this->_req_data['template_switch']) && $this->_req_data['template_switch']) {
1716
+				$sidebar_form_fields['ee-msg-template-switch'] = array(
1717
+					'name'  => 'template_switch',
1718
+					'input' => 'hidden',
1719
+					'type'  => 'int',
1720
+					'value' => 1,
1721
+				);
1722
+			}
1723
+
1724
+
1725
+			$template_fields = $this->_generate_admin_form_fields($template_form_fields);
1726
+			$sidebar_fields = $this->_generate_admin_form_fields($sidebar_form_fields);
1727
+		} //end if ( !empty($template_field_structure) )
1728
+
1729
+		// set extra content for publish box
1730
+		$this->_template_args['publish_box_extra_content'] = $sidebar_fields;
1731
+		$this->_set_publish_post_box_vars(
1732
+			'id',
1733
+			$GRP_ID,
1734
+			false,
1735
+			add_query_arg(
1736
+				array('action' => 'global_mtps'),
1737
+				$this->_admin_base_url
1738
+			)
1739
+		);
1740
+
1741
+		// add preview button
1742
+		$preview_url = parent::add_query_args_and_nonce(
1743
+			array(
1744
+				'message_type' => $message_template_group->message_type(),
1745
+				'messenger'    => $message_template_group->messenger(),
1746
+				'context'      => $context,
1747
+				'GRP_ID'       => $GRP_ID,
1748
+				'evt_id'       => $EVT_ID,
1749
+				'action'       => 'preview_message',
1750
+			),
1751
+			$this->_admin_base_url
1752
+		);
1753
+		$preview_button = '<a href="' . $preview_url . '" class="button-secondary messages-preview-button">'
1754
+						  . esc_html__('Preview', 'event_espresso')
1755
+						  . '</a>';
1756
+
1757
+
1758
+		// setup context switcher
1759
+		$context_switcher_args = array(
1760
+			'page'    => 'espresso_messages',
1761
+			'action'  => 'edit_message_template',
1762
+			'id'      => $GRP_ID,
1763
+			'evt_id'  => $EVT_ID,
1764
+			'context' => $context,
1765
+			'extra'   => $preview_button,
1766
+		);
1767
+		$this->_set_context_switcher($message_template_group, $context_switcher_args);
1768
+
1769
+
1770
+		// main box
1771
+		$this->_template_args['template_fields'] = $template_fields;
1772
+		$this->_template_args['sidebar_box_id'] = 'details';
1773
+		$this->_template_args['action'] = $action;
1774
+		$this->_template_args['context'] = $context;
1775
+		$this->_template_args['edit_message_template_form_url'] = $edit_message_template_form_url;
1776
+		$this->_template_args['learn_more_about_message_templates_link'] =
1777
+			$this->_learn_more_about_message_templates_link();
1778
+
1779
+
1780
+		$this->_template_args['before_admin_page_content'] = $this->add_context_switcher();
1781
+		$this->_template_args['before_admin_page_content'] .= $this->add_active_context_element(
1782
+			$message_template_group,
1783
+			$context,
1784
+			$context_label
1785
+		);
1786
+		$this->_template_args['before_admin_page_content'] .= $this->_add_form_element_before();
1787
+		$this->_template_args['after_admin_page_content'] = $this->_add_form_element_after();
1788
+
1789
+		$this->_template_path = $this->_template_args['GRP_ID']
1790
+			? EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_edit_meta_box.template.php'
1791
+			: EE_MSG_TEMPLATE_PATH . 'ee_msg_details_main_add_meta_box.template.php';
1792
+
1793
+		// send along EE_Message_Template_Group object for further template use.
1794
+		$this->_template_args['MTP'] = $message_template_group;
1795
+
1796
+		$this->_template_args['admin_page_content'] = EEH_Template::display_template(
1797
+			$this->_template_path,
1798
+			$this->_template_args,
1799
+			true
1800
+		);
1801
+
1802
+
1803
+		// finally, let's set the admin_page title
1804
+		$this->_admin_page_title = sprintf(__('Editing %s', 'event_espresso'), $title);
1805
+
1806
+
1807
+		// we need to take care of setting the shortcodes property for use elsewhere.
1808
+		$this->_set_shortcodes();
1809
+
1810
+
1811
+		// final template wrapper
1812
+		$this->display_admin_page_with_sidebar();
1813
+	}
1814
+
1815
+
1816
+	public function filter_tinymce_init($mceInit, $editor_id)
1817
+	{
1818
+		return $mceInit;
1819
+	}
1820
+
1821
+
1822
+	public function add_context_switcher()
1823
+	{
1824
+		return $this->_context_switcher;
1825
+	}
1826
+
1827
+
1828
+	/**
1829
+	 * Adds the activation/deactivation toggle for the message template context.
1830
+	 *
1831
+	 * @param EE_Message_Template_Group $message_template_group
1832
+	 * @param string                    $context
1833
+	 * @param string                    $context_label
1834
+	 * @return string
1835
+	 * @throws DomainException
1836
+	 * @throws EE_Error
1837
+	 * @throws InvalidIdentifierException
1838
+	 */
1839
+	protected function add_active_context_element(
1840
+		EE_Message_Template_Group $message_template_group,
1841
+		$context,
1842
+		$context_label
1843
+	) {
1844
+		$template_args = array(
1845
+			'context'                   => $context,
1846
+			'nonce'                     => wp_create_nonce('activate_' . $context . '_toggle_nonce'),
1847
+			'is_active'                 => $message_template_group->is_context_active($context),
1848
+			'on_off_action'             => $message_template_group->is_context_active($context)
1849
+				? 'context-off'
1850
+				: 'context-on',
1851
+			'context_label'             => str_replace(array('(', ')'), '', $context_label),
1852
+			'message_template_group_id' => $message_template_group->ID(),
1853
+		);
1854
+		return EEH_Template::display_template(
1855
+			EE_MSG_TEMPLATE_PATH . 'ee_msg_editor_active_context_element.template.php',
1856
+			$template_args,
1857
+			true
1858
+		);
1859
+	}
1860
+
1861
+
1862
+	/**
1863
+	 * Ajax callback for `toggle_context_template` ajax action.
1864
+	 * Handles toggling the message context on or off.
1865
+	 *
1866
+	 * @throws EE_Error
1867
+	 * @throws InvalidArgumentException
1868
+	 * @throws InvalidDataTypeException
1869
+	 * @throws InvalidIdentifierException
1870
+	 * @throws InvalidInterfaceException
1871
+	 */
1872
+	public function toggle_context_template()
1873
+	{
1874
+		$success = true;
1875
+		// check for required data
1876
+		if (
1877
+			! isset(
1878
+				$this->_req_data['message_template_group_id'],
1879
+				$this->_req_data['context'],
1880
+				$this->_req_data['status']
1881
+			)
1882
+		) {
1883
+			EE_Error::add_error(
1884
+				esc_html__('Required data for doing this action is not available.', 'event_espresso'),
1885
+				__FILE__,
1886
+				__FUNCTION__,
1887
+				__LINE__
1888
+			);
1889
+			$success = false;
1890
+		}
1891
+
1892
+		$nonce = isset($this->_req_data['toggle_context_nonce'])
1893
+			? sanitize_text_field($this->_req_data['toggle_context_nonce'])
1894
+			: '';
1895
+		$nonce_ref = 'activate_' . $this->_req_data['context'] . '_toggle_nonce';
1896
+		$this->_verify_nonce($nonce, $nonce_ref);
1897
+		$status = $this->_req_data['status'];
1898
+		if ($status !== 'off' && $status !== 'on') {
1899
+			EE_Error::add_error(
1900
+				sprintf(
1901
+					esc_html__('The given status (%s) is not valid. Must be "off" or "on"', 'event_espresso'),
1902
+					$this->_req_data['status']
1903
+				),
1904
+				__FILE__,
1905
+				__FUNCTION__,
1906
+				__LINE__
1907
+			);
1908
+			$success = false;
1909
+		}
1910
+		$message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID(
1911
+			$this->_req_data['message_template_group_id']
1912
+		);
1913
+		if (! $message_template_group instanceof EE_Message_Template_Group) {
1914
+			EE_Error::add_error(
1915
+				sprintf(
1916
+					esc_html__(
1917
+						'Unable to change the active state because the given id "%1$d" does not match a valid "%2$s"',
1918
+						'event_espresso'
1919
+					),
1920
+					$this->_req_data['message_template_group_id'],
1921
+					'EE_Message_Template_Group'
1922
+				),
1923
+				__FILE__,
1924
+				__FUNCTION__,
1925
+				__LINE__
1926
+			);
1927
+			$success = false;
1928
+		}
1929
+		if ($success) {
1930
+			$success = $status === 'off'
1931
+				? $message_template_group->deactivate_context($this->_req_data['context'])
1932
+				: $message_template_group->activate_context($this->_req_data['context']);
1933
+		}
1934
+		$this->_template_args['success'] = $success;
1935
+		$this->_return_json();
1936
+	}
1937
+
1938
+
1939
+	public function _add_form_element_before()
1940
+	{
1941
+		return '<form method="post" action="'
1942
+			   . $this->_template_args["edit_message_template_form_url"]
1943
+			   . '" id="ee-msg-edit-frm">';
1944
+	}
1945
+
1946
+	public function _add_form_element_after()
1947
+	{
1948
+		return '</form>';
1949
+	}
1950
+
1951
+
1952
+	/**
1953
+	 * This executes switching the template pack for a message template.
1954
+	 *
1955
+	 * @since 4.5.0
1956
+	 * @throws EE_Error
1957
+	 * @throws InvalidDataTypeException
1958
+	 * @throws InvalidInterfaceException
1959
+	 * @throws InvalidArgumentException
1960
+	 * @throws ReflectionException
1961
+	 */
1962
+	public function switch_template_pack()
1963
+	{
1964
+		$GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
1965
+		$template_pack = ! empty($this->_req_data['template_pack']) ? $this->_req_data['template_pack'] : '';
1966
+
1967
+		// verify we have needed values.
1968
+		if (empty($GRP_ID) || empty($template_pack)) {
1969
+			$this->_template_args['error'] = true;
1970
+			EE_Error::add_error(
1971
+				esc_html__('The required date for switching templates is not available.', 'event_espresso'),
1972
+				__FILE__,
1973
+				__FUNCTION__,
1974
+				__LINE__
1975
+			);
1976
+		} else {
1977
+			// get template, set the new template_pack and then reset to default
1978
+			/** @type EE_Message_Template_Group $message_template_group */
1979
+			$message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID);
1980
+
1981
+			$message_template_group->set_template_pack_name($template_pack);
1982
+			$this->_req_data['msgr'] = $message_template_group->messenger();
1983
+			$this->_req_data['mt'] = $message_template_group->message_type();
1984
+
1985
+			$query_args = $this->_reset_to_default_template();
1986
+
1987
+			if (empty($query_args['id'])) {
1988
+				EE_Error::add_error(
1989
+					esc_html__(
1990
+						'Something went wrong with switching the template pack. Please try again or contact EE support',
1991
+						'event_espresso'
1992
+					),
1993
+					__FILE__,
1994
+					__FUNCTION__,
1995
+					__LINE__
1996
+				);
1997
+				$this->_template_args['error'] = true;
1998
+			} else {
1999
+				$template_label = $message_template_group->get_template_pack()->label;
2000
+				$template_pack_labels = $message_template_group->messenger_obj()->get_supports_labels();
2001
+				EE_Error::add_success(
2002
+					sprintf(
2003
+						esc_html__(
2004
+							'This message template has been successfully switched to use the %1$s %2$s.  Please wait while the page reloads with your new template.',
2005
+							'event_espresso'
2006
+						),
2007
+						$template_label,
2008
+						$template_pack_labels->template_pack
2009
+					)
2010
+				);
2011
+				// generate the redirect url for js.
2012
+				$url = self::add_query_args_and_nonce(
2013
+					$query_args,
2014
+					$this->_admin_base_url
2015
+				);
2016
+				$this->_template_args['data']['redirect_url'] = $url;
2017
+				$this->_template_args['success'] = true;
2018
+			}
2019
+
2020
+			$this->_return_json();
2021
+		}
2022
+	}
2023
+
2024
+
2025
+	/**
2026
+	 * This handles resetting the template for the given messenger/message_type so that users can start from scratch if
2027
+	 * they want.
2028
+	 *
2029
+	 * @access protected
2030
+	 * @return array|null
2031
+	 * @throws EE_Error
2032
+	 * @throws InvalidArgumentException
2033
+	 * @throws InvalidDataTypeException
2034
+	 * @throws InvalidInterfaceException
2035
+	 */
2036
+	protected function _reset_to_default_template()
2037
+	{
2038
+
2039
+		$templates = array();
2040
+		$GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
2041
+		// we need to make sure we've got the info we need.
2042
+		if (! isset($this->_req_data['msgr'], $this->_req_data['mt'], $this->_req_data['GRP_ID'])) {
2043
+			EE_Error::add_error(
2044
+				esc_html__(
2045
+					'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.',
2046
+					'event_espresso'
2047
+				),
2048
+				__FILE__,
2049
+				__FUNCTION__,
2050
+				__LINE__
2051
+			);
2052
+		}
2053
+
2054
+		// all templates will be reset to whatever the defaults are
2055
+		// for the global template matching the messenger and message type.
2056
+		$success = ! empty($GRP_ID) ? true : false;
2057
+
2058
+		if ($success) {
2059
+			// let's first determine if the incoming template is a global template,
2060
+			// if it isn't then we need to get the global template matching messenger and message type.
2061
+			// $MTPG = EEM_Message_Template_Group::instance()->get_one_by_ID( $GRP_ID );
2062
+
2063
+
2064
+			// note this is ONLY deleting the template fields (Message Template rows) NOT the message template group.
2065
+			$success = $this->_delete_mtp_permanently($GRP_ID, false);
2066
+
2067
+			if ($success) {
2068
+				// if successfully deleted, lets generate the new ones.
2069
+				// Note. We set GLOBAL to true, because resets on ANY template
2070
+				// will use the related global template defaults for regeneration.
2071
+				// This means that if a custom template is reset it resets to whatever the related global template is.
2072
+				// HOWEVER, we DO keep the template pack and template variation set
2073
+				// for the current custom template when resetting.
2074
+				$templates = $this->_generate_new_templates(
2075
+					$this->_req_data['msgr'],
2076
+					$this->_req_data['mt'],
2077
+					$GRP_ID,
2078
+					true
2079
+				);
2080
+			}
2081
+		}
2082
+
2083
+		// any error messages?
2084
+		if (! $success) {
2085
+			EE_Error::add_error(
2086
+				esc_html__(
2087
+					'Something went wrong with deleting existing templates. Unable to reset to default',
2088
+					'event_espresso'
2089
+				),
2090
+				__FILE__,
2091
+				__FUNCTION__,
2092
+				__LINE__
2093
+			);
2094
+		}
2095
+
2096
+		// all good, let's add a success message!
2097
+		if ($success && ! empty($templates)) {
2098
+			// the info for the template we generated is the first element in the returned array
2099
+			// $templates = $templates[0];
2100
+			EE_Error::overwrite_success();
2101
+			EE_Error::add_success(__('Templates have been reset to defaults.', 'event_espresso'));
2102
+		}
2103
+
2104
+
2105
+		$query_args = array(
2106
+			'id'      => isset($templates[0]['GRP_ID']) ? $templates[0]['GRP_ID'] : null,
2107
+			'context' => isset($templates[0]['MTP_context']) ? $templates[0]['MTP_context'] : null,
2108
+			'action'  => isset($templates[0]['GRP_ID']) ? 'edit_message_template' : 'global_mtps',
2109
+		);
2110
+
2111
+		// if called via ajax then we return query args otherwise redirect
2112
+		if (defined('DOING_AJAX') && DOING_AJAX) {
2113
+			return $query_args;
2114
+		} else {
2115
+			$this->_redirect_after_action(false, '', '', $query_args, true);
2116
+
2117
+			return null;
2118
+		}
2119
+	}
2120
+
2121
+
2122
+	/**
2123
+	 * Retrieve and set the message preview for display.
2124
+	 *
2125
+	 * @param bool $send if TRUE then we are doing an actual TEST send with the results of the preview.
2126
+	 * @return string
2127
+	 * @throws ReflectionException
2128
+	 * @throws EE_Error
2129
+	 * @throws InvalidArgumentException
2130
+	 * @throws InvalidDataTypeException
2131
+	 * @throws InvalidInterfaceException
2132
+	 */
2133
+	public function _preview_message($send = false)
2134
+	{
2135
+		// first make sure we've got the necessary parameters
2136
+		if (
2137
+			! isset(
2138
+				$this->_req_data['message_type'],
2139
+				$this->_req_data['messenger'],
2140
+				$this->_req_data['messenger'],
2141
+				$this->_req_data['GRP_ID']
2142
+			)
2143
+		) {
2144
+			EE_Error::add_error(
2145
+				esc_html__('Missing necessary parameters for displaying preview', 'event_espresso'),
2146
+				__FILE__,
2147
+				__FUNCTION__,
2148
+				__LINE__
2149
+			);
2150
+		}
2151
+
2152
+		EE_Registry::instance()->REQ->set('GRP_ID', $this->_req_data['GRP_ID']);
2153
+
2154
+		// if we have an evt_id set on the request, use it.
2155
+		$EVT_ID = isset($this->_req_data['evt_id']) && ! empty($this->_req_data['evt_id'])
2156
+		? absint($this->_req_data['evt_id'])
2157
+		: false;
2158
+
2159
+
2160
+		// get the preview!
2161
+		$preview = EED_Messages::preview_message(
2162
+			$this->_req_data['message_type'],
2163
+			$this->_req_data['context'],
2164
+			$this->_req_data['messenger'],
2165
+			$send
2166
+		);
2167
+
2168
+		if ($send) {
2169
+			return $preview;
2170
+		}
2171
+
2172
+		// let's add a button to go back to the edit view
2173
+		$query_args = array(
2174
+			'id'      => $this->_req_data['GRP_ID'],
2175
+			'evt_id'  => $EVT_ID,
2176
+			'context' => $this->_req_data['context'],
2177
+			'action'  => 'edit_message_template',
2178
+		);
2179
+		$go_back_url = parent::add_query_args_and_nonce($query_args, $this->_admin_base_url);
2180
+		$preview_button = '<a href="'
2181
+						  . $go_back_url
2182
+						  . '" class="button-secondary messages-preview-go-back-button">'
2183
+						  . esc_html__('Go Back to Edit', 'event_espresso')
2184
+						  . '</a>';
2185
+		$message_types = $this->get_installed_message_types();
2186
+		$active_messenger = $this->_message_resource_manager->get_active_messenger(
2187
+			$this->_req_data['messenger']
2188
+		);
2189
+		$active_messenger_label = $active_messenger instanceof EE_messenger
2190
+			? ucwords($active_messenger->label['singular'])
2191
+			: esc_html__('Unknown Messenger', 'event_espresso');
2192
+		// let's provide a helpful title for context
2193
+		$preview_title = sprintf(
2194
+			esc_html__('Viewing Preview for %s %s Message Template', 'event_espresso'),
2195
+			$active_messenger_label,
2196
+			ucwords($message_types[ $this->_req_data['message_type'] ]->label['singular'])
2197
+		);
2198
+		if (empty($preview)) {
2199
+			$this->noEventsErrorMessage();
2200
+		}
2201
+		// setup display of preview.
2202
+		$this->_admin_page_title = $preview_title;
2203
+		$this->_template_args['admin_page_title'] = $preview_title;
2204
+		$this->_template_args['admin_page_content'] = $preview_button . '<br />' . $preview;
2205
+		$this->_template_args['data']['force_json'] = true;
2206
+
2207
+		return '';
2208
+	}
2209
+
2210
+
2211
+	/**
2212
+	 * Used to set an error if there are no events available for generating a preview/test send.
2213
+	 *
2214
+	 * @param bool $test_send  Whether the error should be generated for the context of a test send.
2215
+	 */
2216
+	protected function noEventsErrorMessage($test_send = false)
2217
+	{
2218
+		$events_url = parent::add_query_args_and_nonce(
2219
+			array(
2220
+				'action' => 'default',
2221
+				'page'   => 'espresso_events',
2222
+			),
2223
+			admin_url('admin.php')
2224
+		);
2225
+		$message = $test_send
2226
+			? __(
2227
+				'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!',
2228
+				'event_espresso'
2229
+			)
2230
+			: __(
2231
+				'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!',
2232
+				'event_espresso'
2233
+			);
2234
+
2235
+		EE_Error::add_attention(
2236
+			sprintf(
2237
+				$message,
2238
+				"<a href='{$events_url}'>",
2239
+				'</a>'
2240
+			)
2241
+		);
2242
+	}
2243
+
2244
+
2245
+	/**
2246
+	 * The initial _preview_message is on a no headers route.  It will optionally call this if necessary otherwise it
2247
+	 * gets called automatically.
2248
+	 *
2249
+	 * @since 4.5.0
2250
+	 *
2251
+	 * @return string
2252
+	 */
2253
+	protected function _display_preview_message()
2254
+	{
2255
+		$this->display_admin_page_with_no_sidebar();
2256
+	}
2257
+
2258
+
2259
+	/**
2260
+	 * registers metaboxes that should show up on the "edit_message_template" page
2261
+	 *
2262
+	 * @access protected
2263
+	 * @return void
2264
+	 */
2265
+	protected function _register_edit_meta_boxes()
2266
+	{
2267
+		add_meta_box(
2268
+			'mtp_valid_shortcodes',
2269
+			esc_html__('Valid Shortcodes', 'event_espresso'),
2270
+			array($this, 'shortcode_meta_box'),
2271
+			$this->_current_screen->id,
2272
+			'side',
2273
+			'default'
2274
+		);
2275
+		add_meta_box(
2276
+			'mtp_extra_actions',
2277
+			esc_html__('Extra Actions', 'event_espresso'),
2278
+			array($this, 'extra_actions_meta_box'),
2279
+			$this->_current_screen->id,
2280
+			'side',
2281
+			'high'
2282
+		);
2283
+		add_meta_box(
2284
+			'mtp_templates',
2285
+			esc_html__('Template Styles', 'event_espresso'),
2286
+			array($this, 'template_pack_meta_box'),
2287
+			$this->_current_screen->id,
2288
+			'side',
2289
+			'high'
2290
+		);
2291
+	}
2292
+
2293
+
2294
+	/**
2295
+	 * metabox content for all template pack and variation selection.
2296
+	 *
2297
+	 * @since 4.5.0
2298
+	 * @return string
2299
+	 * @throws DomainException
2300
+	 * @throws EE_Error
2301
+	 * @throws InvalidArgumentException
2302
+	 * @throws ReflectionException
2303
+	 * @throws InvalidDataTypeException
2304
+	 * @throws InvalidInterfaceException
2305
+	 */
2306
+	public function template_pack_meta_box()
2307
+	{
2308
+		$this->_set_message_template_group();
2309
+
2310
+		$tp_collection = EEH_MSG_Template::get_template_pack_collection();
2311
+
2312
+		$tp_select_values = array();
2313
+
2314
+		foreach ($tp_collection as $tp) {
2315
+			// only include template packs that support this messenger and message type!
2316
+			$supports = $tp->get_supports();
2317
+			if (
2318
+				! isset($supports[ $this->_message_template_group->messenger() ])
2319
+				|| ! in_array(
2320
+					$this->_message_template_group->message_type(),
2321
+					$supports[ $this->_message_template_group->messenger() ],
2322
+					true
2323
+				)
2324
+			) {
2325
+				// not supported
2326
+				continue;
2327
+			}
2328
+
2329
+			$tp_select_values[] = array(
2330
+				'text' => $tp->label,
2331
+				'id'   => $tp->dbref,
2332
+			);
2333
+		}
2334
+
2335
+		// if empty $tp_select_values then we make sure default is set because EVERY message type should be supported by
2336
+		// the default template pack.  This still allows for the odd template pack to override.
2337
+		if (empty($tp_select_values)) {
2338
+			$tp_select_values[] = array(
2339
+				'text' => esc_html__('Default', 'event_espresso'),
2340
+				'id'   => 'default',
2341
+			);
2342
+		}
2343
+
2344
+		// setup variation select values for the currently selected template.
2345
+		$variations = $this->_message_template_group->get_template_pack()->get_variations(
2346
+			$this->_message_template_group->messenger(),
2347
+			$this->_message_template_group->message_type()
2348
+		);
2349
+		$variations_select_values = array();
2350
+		foreach ($variations as $variation => $label) {
2351
+			$variations_select_values[] = array(
2352
+				'text' => $label,
2353
+				'id'   => $variation,
2354
+			);
2355
+		}
2356
+
2357
+		$template_pack_labels = $this->_message_template_group->messenger_obj()->get_supports_labels();
2358
+
2359
+		$template_args['template_packs_selector'] = EEH_Form_Fields::select_input(
2360
+			'MTP_template_pack',
2361
+			$tp_select_values,
2362
+			$this->_message_template_group->get_template_pack_name()
2363
+		);
2364
+		$template_args['variations_selector'] = EEH_Form_Fields::select_input(
2365
+			'MTP_template_variation',
2366
+			$variations_select_values,
2367
+			$this->_message_template_group->get_template_pack_variation()
2368
+		);
2369
+		$template_args['template_pack_label'] = $template_pack_labels->template_pack;
2370
+		$template_args['template_variation_label'] = $template_pack_labels->template_variation;
2371
+		$template_args['template_pack_description'] = $template_pack_labels->template_pack_description;
2372
+		$template_args['template_variation_description'] = $template_pack_labels->template_variation_description;
2373
+
2374
+		$template = EE_MSG_TEMPLATE_PATH . 'template_pack_and_variations_metabox.template.php';
2375
+
2376
+		EEH_Template::display_template($template, $template_args);
2377
+	}
2378
+
2379
+
2380
+	/**
2381
+	 * This meta box holds any extra actions related to Message Templates
2382
+	 * For now, this includes Resetting templates to defaults and sending a test email.
2383
+	 *
2384
+	 * @access  public
2385
+	 * @return void
2386
+	 * @throws EE_Error
2387
+	 */
2388
+	public function extra_actions_meta_box()
2389
+	{
2390
+		$template_form_fields = array();
2391
+
2392
+		$extra_args = array(
2393
+			'msgr'   => $this->_message_template_group->messenger(),
2394
+			'mt'     => $this->_message_template_group->message_type(),
2395
+			'GRP_ID' => $this->_message_template_group->GRP_ID(),
2396
+		);
2397
+		// first we need to see if there are any fields
2398
+		$fields = $this->_message_template_group->messenger_obj()->get_test_settings_fields();
2399
+
2400
+		if (! empty($fields)) {
2401
+			// yup there be fields
2402
+			foreach ($fields as $field => $config) {
2403
+				$field_id = $this->_message_template_group->messenger() . '_' . $field;
2404
+				$existing = $this->_message_template_group->messenger_obj()->get_existing_test_settings();
2405
+				$default = isset($config['default']) ? $config['default'] : '';
2406
+				$default = isset($config['value']) ? $config['value'] : $default;
2407
+
2408
+				// if type is hidden and the value is empty
2409
+				// something may have gone wrong so let's correct with the defaults
2410
+				$fix = $config['input'] === 'hidden'
2411
+					   && isset($existing[ $field ])
2412
+					   && empty($existing[ $field ])
2413
+					? $default
2414
+					: '';
2415
+				$existing[ $field ] = isset($existing[ $field ]) && empty($fix)
2416
+					? $existing[ $field ]
2417
+					: $fix;
2418
+
2419
+				$template_form_fields[ $field_id ] = array(
2420
+					'name'       => 'test_settings_fld[' . $field . ']',
2421
+					'label'      => $config['label'],
2422
+					'input'      => $config['input'],
2423
+					'type'       => $config['type'],
2424
+					'required'   => $config['required'],
2425
+					'validation' => $config['validation'],
2426
+					'value'      => isset($existing[ $field ]) ? $existing[ $field ] : $default,
2427
+					'css_class'  => $config['css_class'],
2428
+					'options'    => isset($config['options']) ? $config['options'] : array(),
2429
+					'default'    => $default,
2430
+					'format'     => $config['format'],
2431
+				);
2432
+			}
2433
+		}
2434
+
2435
+		$test_settings_fields = ! empty($template_form_fields)
2436
+			? $this->_generate_admin_form_fields($template_form_fields, 'string', 'ee_tst_settings_flds')
2437
+			: '';
2438
+
2439
+		$test_settings_html = '';
2440
+		// print out $test_settings_fields
2441
+		if (! empty($test_settings_fields)) {
2442
+			echo $test_settings_fields;
2443
+			$test_settings_html = '<input type="submit" class="button-primary mtp-test-button alignright" ';
2444
+			$test_settings_html .= 'name="test_button" value="';
2445
+			$test_settings_html .= esc_html__('Test Send', 'event_espresso');
2446
+			$test_settings_html .= '" /><div style="clear:both"></div>';
2447
+		}
2448
+
2449
+		// and button
2450
+		$test_settings_html .= '<p>'
2451
+							   . esc_html__('Need to reset this message type and start over?', 'event_espresso')
2452
+							   . '</p>';
2453
+		$test_settings_html .= '<div class="publishing-action alignright resetbutton">';
2454
+		$test_settings_html .= $this->get_action_link_or_button(
2455
+			'reset_to_default',
2456
+			'reset',
2457
+			$extra_args,
2458
+			'button-primary reset-default-button'
2459
+		);
2460
+		$test_settings_html .= '</div><div style="clear:both"></div>';
2461
+		echo $test_settings_html;
2462
+	}
2463
+
2464
+
2465
+	/**
2466
+	 * This returns the shortcode selector skeleton for a given context and field.
2467
+	 *
2468
+	 * @since 4.9.rc.000
2469
+	 * @param string $field           The name of the field retrieving shortcodes for.
2470
+	 * @param string $linked_input_id The css id of the input that the shortcodes get added to.
2471
+	 * @return string
2472
+	 * @throws DomainException
2473
+	 * @throws EE_Error
2474
+	 * @throws InvalidArgumentException
2475
+	 * @throws ReflectionException
2476
+	 * @throws InvalidDataTypeException
2477
+	 * @throws InvalidInterfaceException
2478
+	 */
2479
+	protected function _get_shortcode_selector($field, $linked_input_id)
2480
+	{
2481
+		$template_args = array(
2482
+			'shortcodes'      => $this->_get_shortcodes(array($field), true),
2483
+			'fieldname'       => $field,
2484
+			'linked_input_id' => $linked_input_id,
2485
+		);
2486
+
2487
+		return EEH_Template::display_template(
2488
+			EE_MSG_TEMPLATE_PATH . 'shortcode_selector_skeleton.template.php',
2489
+			$template_args,
2490
+			true
2491
+		);
2492
+	}
2493
+
2494
+
2495
+	/**
2496
+	 * This just takes care of returning the meta box content for shortcodes (only used on the edit message template
2497
+	 * page)
2498
+	 *
2499
+	 * @access public
2500
+	 * @return void
2501
+	 * @throws EE_Error
2502
+	 * @throws InvalidArgumentException
2503
+	 * @throws ReflectionException
2504
+	 * @throws InvalidDataTypeException
2505
+	 * @throws InvalidInterfaceException
2506
+	 */
2507
+	public function shortcode_meta_box()
2508
+	{
2509
+		$shortcodes = $this->_get_shortcodes(array(), false); // just make sure shortcodes property is set
2510
+		// $messenger = $this->_message_template_group->messenger_obj();
2511
+		// now let's set the content depending on the status of the shortcodes array
2512
+		if (empty($shortcodes)) {
2513
+			$content = '<p>' . esc_html__('There are no valid shortcodes available', 'event_espresso') . '</p>';
2514
+			echo $content;
2515
+		} else {
2516
+			// $alt = 0;
2517
+			?>
2518 2518
             <div style="float:right; margin-top:10px"><?php
2519
-                            echo $this->_get_help_tab_link('message_template_shortcodes');
2520
-            ?></div>
2519
+							echo $this->_get_help_tab_link('message_template_shortcodes');
2520
+			?></div>
2521 2521
             <p class="small-text"><?php
2522
-                                  printf(
2523
-                                      esc_html__(
2524
-                                          'You can view the shortcodes usable in your template by clicking the %s icon next to each field.',
2525
-                                          'event_espresso'
2526
-                                      ),
2527
-                                      '<span class="dashicons dashicons-menu"></span>'
2528
-                                  );
2529
-                                    ?>
2522
+								  printf(
2523
+									  esc_html__(
2524
+										  'You can view the shortcodes usable in your template by clicking the %s icon next to each field.',
2525
+										  'event_espresso'
2526
+									  ),
2527
+									  '<span class="dashicons dashicons-menu"></span>'
2528
+								  );
2529
+									?>
2530 2530
             </p>
2531 2531
             <?php
2532
-        }
2533
-    }
2534
-
2535
-
2536
-    /**
2537
-     * used to set the $_shortcodes property for when its needed elsewhere.
2538
-     *
2539
-     * @access protected
2540
-     * @return void
2541
-     * @throws EE_Error
2542
-     * @throws InvalidArgumentException
2543
-     * @throws ReflectionException
2544
-     * @throws InvalidDataTypeException
2545
-     * @throws InvalidInterfaceException
2546
-     */
2547
-    protected function _set_shortcodes()
2548
-    {
2549
-
2550
-        // no need to run this if the property is already set
2551
-        if (! empty($this->_shortcodes)) {
2552
-            return;
2553
-        }
2554
-
2555
-        $this->_shortcodes = $this->_get_shortcodes();
2556
-    }
2557
-
2558
-
2559
-    /**
2560
-     * get's all shortcodes for a given template group. (typically used by _set_shortcodes to set the $_shortcodes
2561
-     * property)
2562
-     *
2563
-     * @access  protected
2564
-     * @param  array   $fields include an array of specific field names that you want to be used to get the shortcodes
2565
-     *                         for. Defaults to all (for the given context)
2566
-     * @param  boolean $merged Whether to merge all the shortcodes into one list of unique shortcodes
2567
-     * @return array Shortcodes indexed by fieldname and the an array of shortcode/label pairs OR if merged is
2568
-     *                         true just an array of shortcode/label pairs.
2569
-     * @throws EE_Error
2570
-     * @throws InvalidArgumentException
2571
-     * @throws ReflectionException
2572
-     * @throws InvalidDataTypeException
2573
-     * @throws InvalidInterfaceException
2574
-     */
2575
-    protected function _get_shortcodes($fields = array(), $merged = true)
2576
-    {
2577
-        $this->_set_message_template_group();
2578
-
2579
-        // we need the messenger and message template to retrieve the valid shortcodes array.
2580
-        $GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id'])
2581
-            ? absint($this->_req_data['id'])
2582
-            : false;
2583
-        $context = isset($this->_req_data['context'])
2584
-            ? $this->_req_data['context']
2585
-            : key($this->_message_template_group->contexts_config());
2586
-
2587
-        return ! empty($GRP_ID) ? $this->_message_template_group->get_shortcodes($context, $fields, $merged) : array();
2588
-    }
2589
-
2590
-
2591
-    /**
2592
-     * This sets the _message_template property (containing the called message_template object)
2593
-     *
2594
-     * @access protected
2595
-     * @return void
2596
-     * @throws EE_Error
2597
-     * @throws InvalidArgumentException
2598
-     * @throws ReflectionException
2599
-     * @throws InvalidDataTypeException
2600
-     * @throws InvalidInterfaceException
2601
-     */
2602
-    protected function _set_message_template_group()
2603
-    {
2604
-
2605
-        if (! empty($this->_message_template_group)) {
2606
-            return;
2607
-        } //get out if this is already set.
2608
-
2609
-        $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? absint($this->_req_data['GRP_ID']) : false;
2610
-        $GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['id']) ? $this->_req_data['id'] : $GRP_ID;
2611
-
2612
-        // let's get the message templates
2613
-        $MTP = EEM_Message_Template_Group::instance();
2614
-
2615
-        if (empty($GRP_ID)) {
2616
-            $this->_message_template_group = $MTP->create_default_object();
2617
-        } else {
2618
-            $this->_message_template_group = $MTP->get_one_by_ID($GRP_ID);
2619
-        }
2620
-
2621
-        $this->_template_pack = $this->_message_template_group->get_template_pack();
2622
-        $this->_variation = $this->_message_template_group->get_template_pack_variation();
2623
-    }
2624
-
2625
-
2626
-    /**
2627
-     * sets up a context switcher for edit forms
2628
-     *
2629
-     * @access  protected
2630
-     * @param  EE_Message_Template_Group $template_group_object the template group object being displayed on the form
2631
-     * @param array                      $args                  various things the context switcher needs.
2632
-     * @throws EE_Error
2633
-     */
2634
-    protected function _set_context_switcher(EE_Message_Template_Group $template_group_object, $args)
2635
-    {
2636
-        $context_details = $template_group_object->contexts_config();
2637
-        $context_label = $template_group_object->context_label();
2638
-        ob_start();
2639
-        ?>
2532
+		}
2533
+	}
2534
+
2535
+
2536
+	/**
2537
+	 * used to set the $_shortcodes property for when its needed elsewhere.
2538
+	 *
2539
+	 * @access protected
2540
+	 * @return void
2541
+	 * @throws EE_Error
2542
+	 * @throws InvalidArgumentException
2543
+	 * @throws ReflectionException
2544
+	 * @throws InvalidDataTypeException
2545
+	 * @throws InvalidInterfaceException
2546
+	 */
2547
+	protected function _set_shortcodes()
2548
+	{
2549
+
2550
+		// no need to run this if the property is already set
2551
+		if (! empty($this->_shortcodes)) {
2552
+			return;
2553
+		}
2554
+
2555
+		$this->_shortcodes = $this->_get_shortcodes();
2556
+	}
2557
+
2558
+
2559
+	/**
2560
+	 * get's all shortcodes for a given template group. (typically used by _set_shortcodes to set the $_shortcodes
2561
+	 * property)
2562
+	 *
2563
+	 * @access  protected
2564
+	 * @param  array   $fields include an array of specific field names that you want to be used to get the shortcodes
2565
+	 *                         for. Defaults to all (for the given context)
2566
+	 * @param  boolean $merged Whether to merge all the shortcodes into one list of unique shortcodes
2567
+	 * @return array Shortcodes indexed by fieldname and the an array of shortcode/label pairs OR if merged is
2568
+	 *                         true just an array of shortcode/label pairs.
2569
+	 * @throws EE_Error
2570
+	 * @throws InvalidArgumentException
2571
+	 * @throws ReflectionException
2572
+	 * @throws InvalidDataTypeException
2573
+	 * @throws InvalidInterfaceException
2574
+	 */
2575
+	protected function _get_shortcodes($fields = array(), $merged = true)
2576
+	{
2577
+		$this->_set_message_template_group();
2578
+
2579
+		// we need the messenger and message template to retrieve the valid shortcodes array.
2580
+		$GRP_ID = isset($this->_req_data['id']) && ! empty($this->_req_data['id'])
2581
+			? absint($this->_req_data['id'])
2582
+			: false;
2583
+		$context = isset($this->_req_data['context'])
2584
+			? $this->_req_data['context']
2585
+			: key($this->_message_template_group->contexts_config());
2586
+
2587
+		return ! empty($GRP_ID) ? $this->_message_template_group->get_shortcodes($context, $fields, $merged) : array();
2588
+	}
2589
+
2590
+
2591
+	/**
2592
+	 * This sets the _message_template property (containing the called message_template object)
2593
+	 *
2594
+	 * @access protected
2595
+	 * @return void
2596
+	 * @throws EE_Error
2597
+	 * @throws InvalidArgumentException
2598
+	 * @throws ReflectionException
2599
+	 * @throws InvalidDataTypeException
2600
+	 * @throws InvalidInterfaceException
2601
+	 */
2602
+	protected function _set_message_template_group()
2603
+	{
2604
+
2605
+		if (! empty($this->_message_template_group)) {
2606
+			return;
2607
+		} //get out if this is already set.
2608
+
2609
+		$GRP_ID = ! empty($this->_req_data['GRP_ID']) ? absint($this->_req_data['GRP_ID']) : false;
2610
+		$GRP_ID = empty($GRP_ID) && ! empty($this->_req_data['id']) ? $this->_req_data['id'] : $GRP_ID;
2611
+
2612
+		// let's get the message templates
2613
+		$MTP = EEM_Message_Template_Group::instance();
2614
+
2615
+		if (empty($GRP_ID)) {
2616
+			$this->_message_template_group = $MTP->create_default_object();
2617
+		} else {
2618
+			$this->_message_template_group = $MTP->get_one_by_ID($GRP_ID);
2619
+		}
2620
+
2621
+		$this->_template_pack = $this->_message_template_group->get_template_pack();
2622
+		$this->_variation = $this->_message_template_group->get_template_pack_variation();
2623
+	}
2624
+
2625
+
2626
+	/**
2627
+	 * sets up a context switcher for edit forms
2628
+	 *
2629
+	 * @access  protected
2630
+	 * @param  EE_Message_Template_Group $template_group_object the template group object being displayed on the form
2631
+	 * @param array                      $args                  various things the context switcher needs.
2632
+	 * @throws EE_Error
2633
+	 */
2634
+	protected function _set_context_switcher(EE_Message_Template_Group $template_group_object, $args)
2635
+	{
2636
+		$context_details = $template_group_object->contexts_config();
2637
+		$context_label = $template_group_object->context_label();
2638
+		ob_start();
2639
+		?>
2640 2640
         <div class="ee-msg-switcher-container">
2641 2641
             <form method="get" action="<?php echo EE_MSG_ADMIN_URL; ?>" id="ee-msg-context-switcher-frm">
2642 2642
                 <?php
2643
-                foreach ($args as $name => $value) {
2644
-                    if ($name === 'context' || empty($value) || $name === 'extra') {
2645
-                        continue;
2646
-                    }
2647
-                    ?>
2643
+				foreach ($args as $name => $value) {
2644
+					if ($name === 'context' || empty($value) || $name === 'extra') {
2645
+						continue;
2646
+					}
2647
+					?>
2648 2648
                     <input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/>
2649 2649
                     <?php
2650
-                }
2651
-                // setup nonce_url
2652
-                wp_nonce_field($args['action'] . '_nonce', $args['action'] . '_nonce', false);
2653
-                ?>
2650
+				}
2651
+				// setup nonce_url
2652
+				wp_nonce_field($args['action'] . '_nonce', $args['action'] . '_nonce', false);
2653
+				?>
2654 2654
                 <select name="context">
2655 2655
                     <?php
2656
-                    $context_templates = $template_group_object->context_templates();
2657
-                    if (is_array($context_templates)) :
2658
-                        foreach ($context_templates as $context => $template_fields) :
2659
-                            $checked = ($context === $args['context']) ? 'selected="selected"' : '';
2660
-                            ?>
2656
+					$context_templates = $template_group_object->context_templates();
2657
+					if (is_array($context_templates)) :
2658
+						foreach ($context_templates as $context => $template_fields) :
2659
+							$checked = ($context === $args['context']) ? 'selected="selected"' : '';
2660
+							?>
2661 2661
                             <option value="<?php echo $context; ?>" <?php echo $checked; ?>>
2662 2662
                                 <?php echo $context_details[ $context ]['label']; ?>
2663 2663
                             </option>
2664 2664
                         <?php endforeach;
2665
-                    endif; ?>
2665
+					endif; ?>
2666 2666
                 </select>
2667 2667
                 <?php $button_text = sprintf(__('Switch %s', 'event_espresso'), ucwords($context_label['label'])); ?>
2668 2668
                 <input id="submit-msg-context-switcher-sbmt" class="button-secondary" type="submit"
@@ -2671,1930 +2671,1930 @@  discard block
 block discarded – undo
2671 2671
             <?php echo $args['extra']; ?>
2672 2672
         </div> <!-- end .ee-msg-switcher-container -->
2673 2673
         <?php
2674
-        $output = ob_get_contents();
2675
-        ob_clean();
2676
-        $this->_context_switcher = $output;
2677
-    }
2678
-
2679
-
2680
-    /**
2681
-     * utility for sanitizing new values coming in.
2682
-     * Note: this is only used when updating a context.
2683
-     *
2684
-     * @access protected
2685
-     *
2686
-     * @param int $index This helps us know which template field to select from the request array.
2687
-     *
2688
-     * @return array
2689
-     */
2690
-    protected function _set_message_template_column_values($index)
2691
-    {
2692
-        if (is_array($this->_req_data['MTP_template_fields'][ $index ]['content'])) {
2693
-            foreach ($this->_req_data['MTP_template_fields'][ $index ]['content'] as $field => $value) {
2694
-                $this->_req_data['MTP_template_fields'][ $index ]['content'][ $field ] = $value;
2695
-            }
2696
-        }
2697
-
2698
-
2699
-        $set_column_values = array(
2700
-            'MTP_ID'             => absint($this->_req_data['MTP_template_fields'][ $index ]['MTP_ID']),
2701
-            'GRP_ID'             => absint($this->_req_data['GRP_ID']),
2702
-            'MTP_user_id'        => absint($this->_req_data['MTP_user_id']),
2703
-            'MTP_messenger'      => strtolower($this->_req_data['MTP_messenger']),
2704
-            'MTP_message_type'   => strtolower($this->_req_data['MTP_message_type']),
2705
-            'MTP_template_field' => strtolower($this->_req_data['MTP_template_fields'][ $index ]['name']),
2706
-            'MTP_context'        => strtolower($this->_req_data['MTP_context']),
2707
-            'MTP_content'        => $this->_req_data['MTP_template_fields'][ $index ]['content'],
2708
-            'MTP_is_global'      => isset($this->_req_data['MTP_is_global'])
2709
-                ? absint($this->_req_data['MTP_is_global'])
2710
-                : 0,
2711
-            'MTP_is_override'    => isset($this->_req_data['MTP_is_override'])
2712
-                ? absint($this->_req_data['MTP_is_override'])
2713
-                : 0,
2714
-            'MTP_deleted'        => absint($this->_req_data['MTP_deleted']),
2715
-            'MTP_is_active'      => absint($this->_req_data['MTP_is_active']),
2716
-        );
2717
-
2718
-
2719
-        return $set_column_values;
2720
-    }
2721
-
2722
-
2723
-    protected function _insert_or_update_message_template($new = false)
2724
-    {
2725
-        $success = 0;
2726
-        $override = false;
2727
-
2728
-        // setup notices description
2729
-        $messenger_slug = ! empty($this->_req_data['MTP_messenger']) ? $this->_req_data['MTP_messenger'] : '';
2730
-
2731
-        // need the message type and messenger objects to be able to use the labels for the notices
2732
-        $messenger_object = $this->_message_resource_manager->get_messenger($messenger_slug);
2733
-        $messenger_label = $messenger_object instanceof EE_messenger
2734
-            ? ucwords($messenger_object->label['singular'])
2735
-            : '';
2736
-
2737
-        $message_type_slug = ! empty($this->_req_data['MTP_message_type'])
2738
-            ? $this->_req_data['MTP_message_type']
2739
-            : '';
2740
-        $message_type_object = $this->_message_resource_manager->get_message_type($message_type_slug);
2741
-
2742
-        $message_type_label = $message_type_object instanceof EE_message_type
2743
-            ? ucwords($message_type_object->label['singular'])
2744
-            : '';
2745
-
2746
-        $context_slug = ! empty($this->_req_data['MTP_context'])
2747
-            ? $this->_req_data['MTP_context']
2748
-            : '';
2749
-        $context = ucwords(str_replace('_', ' ', $context_slug));
2750
-
2751
-        $item_desc = $messenger_label && $message_type_label
2752
-            ? $messenger_label . ' ' . $message_type_label . ' ' . $context . ' '
2753
-            : '';
2754
-        $item_desc .= 'Message Template';
2755
-        $query_args = array();
2756
-        $edit_array = array();
2757
-        $action_desc = '';
2758
-
2759
-        // if this is "new" then we need to generate the default contexts for the selected messenger/message_type for
2760
-        // user to edit.
2761
-        if ($new) {
2762
-            $GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
2763
-            if ($edit_array = $this->_generate_new_templates($messenger_slug, $message_type_slug, $GRP_ID)) {
2764
-                if (empty($edit_array)) {
2765
-                    $success = 0;
2766
-                } else {
2767
-                    $success = 1;
2768
-                    $edit_array = $edit_array[0];
2769
-                    $query_args = array(
2770
-                        'id'      => $edit_array['GRP_ID'],
2771
-                        'context' => $edit_array['MTP_context'],
2772
-                        'action'  => 'edit_message_template',
2773
-                    );
2774
-                }
2775
-            }
2776
-            $action_desc = 'created';
2777
-        } else {
2778
-            $MTPG = EEM_Message_Template_Group::instance();
2779
-            $MTP = EEM_Message_Template::instance();
2780
-
2781
-
2782
-            // run update for each template field in displayed context
2783
-            if (! isset($this->_req_data['MTP_template_fields']) && empty($this->_req_data['MTP_template_fields'])) {
2784
-                EE_Error::add_error(
2785
-                    esc_html__(
2786
-                        'There was a problem saving the template fields from the form because I didn\'t receive any actual template field data.',
2787
-                        'event_espresso'
2788
-                    ),
2789
-                    __FILE__,
2790
-                    __FUNCTION__,
2791
-                    __LINE__
2792
-                );
2793
-                $success = 0;
2794
-            } else {
2795
-                // first validate all fields!
2796
-                // this filter allows client code to add its own validation to the template fields as well.
2797
-                // returning an empty array means everything passed validation.
2798
-                // errors in validation should be represented in an array with the following shape:
2799
-                // array(
2800
-                //   'fieldname' => array(
2801
-                //          'msg' => 'error message'
2802
-                //          'value' => 'value for field producing error'
2803
-                // )
2804
-                $custom_validation = (array) apply_filters(
2805
-                    'FHEE__Messages_Admin_Page___insert_or_update_message_template__validates',
2806
-                    array(),
2807
-                    $this->_req_data['MTP_template_fields'],
2808
-                    $context_slug,
2809
-                    $messenger_slug,
2810
-                    $message_type_slug
2811
-                );
2812
-
2813
-                $system_validation = $MTPG->validate(
2814
-                    $this->_req_data['MTP_template_fields'],
2815
-                    $context_slug,
2816
-                    $messenger_slug,
2817
-                    $message_type_slug
2818
-                );
2819
-
2820
-                $system_validation = ! is_array($system_validation) && $system_validation ? array()
2821
-                    : $system_validation;
2822
-                $validates = array_merge($custom_validation, $system_validation);
2823
-
2824
-                // if $validate returned error messages (i.e. is_array()) then we need to process them and setup an
2825
-                // appropriate response. HMM, dang this isn't correct, $validates will ALWAYS be an array.
2826
-                //  WE need to make sure there is no actual error messages in validates.
2827
-                if (is_array($validates) && ! empty($validates)) {
2828
-                    // add the transient so when the form loads we know which fields to highlight
2829
-                    $this->_add_transient('edit_message_template', $validates);
2830
-
2831
-                    $success = 0;
2832
-
2833
-                    // setup notices
2834
-                    foreach ($validates as $field => $error) {
2835
-                        if (isset($error['msg'])) {
2836
-                            EE_Error::add_error($error['msg'], __FILE__, __FUNCTION__, __LINE__);
2837
-                        }
2838
-                    }
2839
-                } else {
2840
-                    $set_column_values = array();
2841
-                    foreach ($this->_req_data['MTP_template_fields'] as $template_field => $content) {
2842
-                        $set_column_values = $this->_set_message_template_column_values($template_field);
2843
-
2844
-                        $where_cols_n_values = array(
2845
-                            'MTP_ID' => $this->_req_data['MTP_template_fields'][ $template_field ]['MTP_ID'],
2846
-                        );
2847
-                        // if they aren't allowed to use all JS, restrict them to just posty-y tags
2848
-                        if (! current_user_can('unfiltered_html')) {
2849
-                            if (is_array($set_column_values['MTP_content'])) {
2850
-                                foreach ($set_column_values['MTP_content'] as $key => $value) {
2851
-                                    // remove slashes so wp_kses works properly (its wp_kses_stripslashes() function
2852
-                                    // only removes slashes from double-quotes, so attributes using single quotes always
2853
-                                    // appear invalid.) But currently the models expect slashed data, so after wp_kses
2854
-                                    // runs we need to re-slash the data. Sheesh. See
2855
-                                    // https://events.codebasehq.com/projects/event-espresso/tickets/11211#update-47321587
2856
-                                    $set_column_values['MTP_content'][ $key ] = addslashes(
2857
-                                        wp_kses(
2858
-                                            stripslashes($value),
2859
-                                            wp_kses_allowed_html('post')
2860
-                                        )
2861
-                                    );
2862
-                                }
2863
-                            } else {
2864
-                                $set_column_values['MTP_content'] = wp_kses(
2865
-                                    $set_column_values['MTP_content'],
2866
-                                    wp_kses_allowed_html('post')
2867
-                                );
2868
-                            }
2869
-                        }
2870
-                        $message_template_fields = array(
2871
-                            'GRP_ID'             => $set_column_values['GRP_ID'],
2872
-                            'MTP_template_field' => $set_column_values['MTP_template_field'],
2873
-                            'MTP_context'        => $set_column_values['MTP_context'],
2874
-                            'MTP_content'        => $set_column_values['MTP_content'],
2875
-                        );
2876
-                        if ($updated = $MTP->update($message_template_fields, array($where_cols_n_values))) {
2877
-                            if ($updated === false) {
2878
-                                EE_Error::add_error(
2879
-                                    sprintf(
2880
-                                        esc_html__('%s field was NOT updated for some reason', 'event_espresso'),
2881
-                                        $template_field
2882
-                                    ),
2883
-                                    __FILE__,
2884
-                                    __FUNCTION__,
2885
-                                    __LINE__
2886
-                                );
2887
-                            } else {
2888
-                                $success = 1;
2889
-                            }
2890
-                        } else {
2891
-                            // only do this logic if we don't have a MTP_ID for this field
2892
-                            if (empty($this->_req_data['MTP_template_fields'][ $template_field ]['MTP_ID'])) {
2893
-                                // this has already been through the template field validator and sanitized, so it will be
2894
-                                // safe to insert this field.  Why insert?  This typically happens when we introduce a new
2895
-                                // message template field in a messenger/message type and existing users don't have the
2896
-                                // default setup for it.
2897
-                                // @link https://events.codebasehq.com/projects/event-espresso/tickets/9465
2898
-                                $updated = $MTP->insert($message_template_fields);
2899
-                                if (! $updated || is_wp_error($updated)) {
2900
-                                    EE_Error::add_error(
2901
-                                        sprintf(
2902
-                                            esc_html__('%s field could not be updated.', 'event_espresso'),
2903
-                                            $template_field
2904
-                                        ),
2905
-                                        __FILE__,
2906
-                                        __FUNCTION__,
2907
-                                        __LINE__
2908
-                                    );
2909
-                                    $success = 0;
2910
-                                } else {
2911
-                                    $success = 1;
2912
-                                }
2913
-                            }
2914
-                        }
2915
-                        $action_desc = 'updated';
2916
-                    }
2917
-
2918
-                    // we can use the last set_column_values for the MTPG update (because its the same for all of these specific MTPs)
2919
-                    $mtpg_fields = array(
2920
-                        'MTP_user_id'      => $set_column_values['MTP_user_id'],
2921
-                        'MTP_messenger'    => $set_column_values['MTP_messenger'],
2922
-                        'MTP_message_type' => $set_column_values['MTP_message_type'],
2923
-                        'MTP_is_global'    => $set_column_values['MTP_is_global'],
2924
-                        'MTP_is_override'  => $set_column_values['MTP_is_override'],
2925
-                        'MTP_deleted'      => $set_column_values['MTP_deleted'],
2926
-                        'MTP_is_active'    => $set_column_values['MTP_is_active'],
2927
-                        'MTP_name'         => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_name'])
2928
-                            ? $this->_req_data['ee_msg_non_global_fields']['MTP_name']
2929
-                            : '',
2930
-                        'MTP_description'  => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_description'])
2931
-                            ? $this->_req_data['ee_msg_non_global_fields']['MTP_description']
2932
-                            : '',
2933
-                    );
2934
-
2935
-                    $mtpg_where = array('GRP_ID' => $set_column_values['GRP_ID']);
2936
-                    $updated = $MTPG->update($mtpg_fields, array($mtpg_where));
2937
-
2938
-                    if ($updated === false) {
2939
-                        EE_Error::add_error(
2940
-                            sprintf(
2941
-                                esc_html__(
2942
-                                    'The Message Template Group (%d) was NOT updated for some reason',
2943
-                                    'event_espresso'
2944
-                                ),
2945
-                                $set_column_values['GRP_ID']
2946
-                            ),
2947
-                            __FILE__,
2948
-                            __FUNCTION__,
2949
-                            __LINE__
2950
-                        );
2951
-                    } else {
2952
-                        // k now we need to ensure the template_pack and template_variation fields are set.
2953
-                        $template_pack = ! empty($this->_req_data['MTP_template_pack'])
2954
-                            ? $this->_req_data['MTP_template_pack']
2955
-                            : 'default';
2956
-
2957
-                        $template_variation = ! empty($this->_req_data['MTP_template_variation'])
2958
-                            ? $this->_req_data['MTP_template_variation']
2959
-                            : 'default';
2960
-
2961
-                        $mtpg_obj = $MTPG->get_one_by_ID($set_column_values['GRP_ID']);
2962
-                        if ($mtpg_obj instanceof EE_Message_Template_Group) {
2963
-                            $mtpg_obj->set_template_pack_name($template_pack);
2964
-                            $mtpg_obj->set_template_pack_variation($template_variation);
2965
-                        }
2966
-                        $success = 1;
2967
-                    }
2968
-                }
2969
-            }
2970
-        }
2971
-
2972
-        // we return things differently if doing ajax
2973
-        if (defined('DOING_AJAX') && DOING_AJAX) {
2974
-            $this->_template_args['success'] = $success;
2975
-            $this->_template_args['error'] = ! $success ? true : false;
2976
-            $this->_template_args['content'] = '';
2977
-            $this->_template_args['data'] = array(
2978
-                'grpID'        => $edit_array['GRP_ID'],
2979
-                'templateName' => $edit_array['template_name'],
2980
-            );
2981
-            if ($success) {
2982
-                EE_Error::overwrite_success();
2983
-                EE_Error::add_success(
2984
-                    esc_html__(
2985
-                        '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.',
2986
-                        'event_espresso'
2987
-                    )
2988
-                );
2989
-            }
2990
-
2991
-            $this->_return_json();
2992
-        }
2993
-
2994
-
2995
-        // was a test send triggered?
2996
-        if (isset($this->_req_data['test_button'])) {
2997
-            EE_Error::overwrite_success();
2998
-            $this->_do_test_send($context_slug, $messenger_slug, $message_type_slug);
2999
-            $override = true;
3000
-        }
3001
-
3002
-        if (empty($query_args)) {
3003
-            $query_args = array(
3004
-                'id'      => $this->_req_data['GRP_ID'],
3005
-                'context' => $context_slug,
3006
-                'action'  => 'edit_message_template',
3007
-            );
3008
-        }
3009
-
3010
-        $this->_redirect_after_action($success, $item_desc, $action_desc, $query_args, $override);
3011
-    }
3012
-
3013
-
3014
-    /**
3015
-     * processes a test send request to do an actual messenger delivery test for the given message template being tested
3016
-     *
3017
-     * @param  string $context      what context being tested
3018
-     * @param  string $messenger    messenger being tested
3019
-     * @param  string $message_type message type being tested
3020
-     * @throws EE_Error
3021
-     * @throws InvalidArgumentException
3022
-     * @throws InvalidDataTypeException
3023
-     * @throws InvalidInterfaceException
3024
-     */
3025
-    protected function _do_test_send($context, $messenger, $message_type)
3026
-    {
3027
-        // set things up for preview
3028
-        $this->_req_data['messenger'] = $messenger;
3029
-        $this->_req_data['message_type'] = $message_type;
3030
-        $this->_req_data['context'] = $context;
3031
-        $this->_req_data['GRP_ID'] = isset($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : '';
3032
-        $active_messenger = $this->_message_resource_manager->get_active_messenger($messenger);
3033
-
3034
-        // let's save any existing fields that might be required by the messenger
3035
-        if (
3036
-            isset($this->_req_data['test_settings_fld'])
3037
-            && $active_messenger instanceof EE_messenger
3038
-            && apply_filters(
3039
-                'FHEE__Messages_Admin_Page__do_test_send__set_existing_test_settings',
3040
-                true,
3041
-                $this->_req_data['test_settings_fld'],
3042
-                $active_messenger
3043
-            )
3044
-        ) {
3045
-            $active_messenger->set_existing_test_settings($this->_req_data['test_settings_fld']);
3046
-        }
3047
-
3048
-        /**
3049
-         * Use filter to add additional controls on whether message can send or not
3050
-         */
3051
-        if (
3052
-            apply_filters(
3053
-                'FHEE__Messages_Admin_Page__do_test_send__can_send',
3054
-                true,
3055
-                $context,
3056
-                $this->_req_data,
3057
-                $messenger,
3058
-                $message_type
3059
-            )
3060
-        ) {
3061
-            if (EEM_Event::instance()->count() > 0) {
3062
-                $success = $this->_preview_message(true);
3063
-                if ($success) {
3064
-                    EE_Error::add_success(__('Test message sent', 'event_espresso'));
3065
-                } else {
3066
-                    EE_Error::add_error(
3067
-                        esc_html__('The test message was not sent', 'event_espresso'),
3068
-                        __FILE__,
3069
-                        __FUNCTION__,
3070
-                        __LINE__
3071
-                    );
3072
-                }
3073
-            } else {
3074
-                $this->noEventsErrorMessage(true);
3075
-            }
3076
-        }
3077
-    }
3078
-
3079
-
3080
-    /**
3081
-     * _generate_new_templates
3082
-     * This will handle the messenger, message_type selection when "adding a new custom template" for an event and will
3083
-     * automatically create the defaults for the event.  The user would then be redirected to edit the default context
3084
-     * for the event.
3085
-     *
3086
-     *
3087
-     * @param  string $messenger     the messenger we are generating templates for
3088
-     * @param array   $message_types array of message types that the templates are generated for.
3089
-     * @param int     $GRP_ID        If this is a custom template being generated then a GRP_ID needs to be included to
3090
-     *                               indicate the message_template_group being used as the base.
3091
-     *
3092
-     * @param bool    $global
3093
-     *
3094
-     * @return array|bool array of data required for the redirect to the correct edit page or bool if
3095
-     *                               encountering problems.
3096
-     * @throws EE_Error
3097
-     */
3098
-    protected function _generate_new_templates($messenger, $message_types, $GRP_ID = 0, $global = false)
3099
-    {
3100
-
3101
-        // if no $message_types are given then that's okay... this may be a messenger that just adds shortcodes, so we
3102
-        // just don't generate any templates.
3103
-        if (empty($message_types)) {
3104
-            return true;
3105
-        }
3106
-
3107
-        return EEH_MSG_Template::generate_new_templates($messenger, $message_types, $GRP_ID, $global);
3108
-    }
3109
-
3110
-
3111
-    /**
3112
-     * [_trash_or_restore_message_template]
3113
-     *
3114
-     * @param  boolean $trash whether to move an item to trash/restore (TRUE) or restore it (FALSE)
3115
-     * @param boolean  $all   whether this is going to trash/restore all contexts within a template group (TRUE) OR just
3116
-     *                        an individual context (FALSE).
3117
-     * @return void
3118
-     * @throws EE_Error
3119
-     * @throws InvalidArgumentException
3120
-     * @throws InvalidDataTypeException
3121
-     * @throws InvalidInterfaceException
3122
-     */
3123
-    protected function _trash_or_restore_message_template($trash = true, $all = false)
3124
-    {
3125
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
3126
-        $MTP = EEM_Message_Template_Group::instance();
3127
-
3128
-        $success = 1;
3129
-
3130
-        // incoming GRP_IDs
3131
-        if ($all) {
3132
-            // Checkboxes
3133
-            if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3134
-                // if array has more than one element then success message should be plural.
3135
-                // todo: what about nonce?
3136
-                $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
3137
-
3138
-                // cycle through checkboxes
3139
-                while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) {
3140
-                    $trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID);
3141
-                    if (! $trashed_or_restored) {
3142
-                        $success = 0;
3143
-                    }
3144
-                }
3145
-            } else {
3146
-                // grab single GRP_ID and handle
3147
-                $GRP_ID = isset($this->_req_data['id']) ? absint($this->_req_data['id']) : 0;
3148
-                if (! empty($GRP_ID)) {
3149
-                    $trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID);
3150
-                    if (! $trashed_or_restored) {
3151
-                        $success = 0;
3152
-                    }
3153
-                } else {
3154
-                    $success = 0;
3155
-                }
3156
-            }
3157
-        }
3158
-
3159
-        $action_desc = $trash
3160
-            ? esc_html__('moved to the trash', 'event_espresso')
3161
-            : esc_html__('restored', 'event_espresso');
3162
-
3163
-        $action_desc = ! empty($this->_req_data['template_switch']) ? esc_html__('switched', 'event_espresso') : $action_desc;
3164
-
3165
-        $item_desc = $all ? _n(
3166
-            'Message Template Group',
3167
-            'Message Template Groups',
3168
-            $success,
3169
-            'event_espresso'
3170
-        ) : _n('Message Template Context', 'Message Template Contexts', $success, 'event_espresso');
3171
-
3172
-        $item_desc = ! empty($this->_req_data['template_switch']) ? _n(
3173
-            'template',
3174
-            'templates',
3175
-            $success,
3176
-            'event_espresso'
3177
-        ) : $item_desc;
3178
-
3179
-        $this->_redirect_after_action($success, $item_desc, $action_desc, array());
3180
-    }
3181
-
3182
-
3183
-    /**
3184
-     * [_delete_message_template]
3185
-     * NOTE: this handles not only the deletion of the groups but also all the templates belonging to that group.
3186
-     *
3187
-     * @return void
3188
-     * @throws EE_Error
3189
-     * @throws InvalidArgumentException
3190
-     * @throws InvalidDataTypeException
3191
-     * @throws InvalidInterfaceException
3192
-     */
3193
-    protected function _delete_message_template()
3194
-    {
3195
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
3196
-
3197
-        // checkboxes
3198
-        if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3199
-            // if array has more than one element then success message should be plural
3200
-            $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
3201
-
3202
-            // cycle through bulk action checkboxes
3203
-            while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) {
3204
-                $success = $this->_delete_mtp_permanently($GRP_ID);
3205
-            }
3206
-        } else {
3207
-            // grab single grp_id and delete
3208
-            $GRP_ID = absint($this->_req_data['id']);
3209
-            $success = $this->_delete_mtp_permanently($GRP_ID);
3210
-        }
3211
-
3212
-        $this->_redirect_after_action($success, 'Message Templates', 'deleted', array());
3213
-    }
3214
-
3215
-
3216
-    /**
3217
-     * helper for permanently deleting a mtP group and all related message_templates
3218
-     *
3219
-     * @param  int  $GRP_ID        The group being deleted
3220
-     * @param  bool $include_group whether to delete the Message Template Group as well.
3221
-     * @return bool boolean to indicate the success of the deletes or not.
3222
-     * @throws EE_Error
3223
-     * @throws InvalidArgumentException
3224
-     * @throws InvalidDataTypeException
3225
-     * @throws InvalidInterfaceException
3226
-     */
3227
-    private function _delete_mtp_permanently($GRP_ID, $include_group = true)
3228
-    {
3229
-        $success = 1;
3230
-        $MTPG = EEM_Message_Template_Group::instance();
3231
-        // first let's GET this group
3232
-        $MTG = $MTPG->get_one_by_ID($GRP_ID);
3233
-        // then delete permanently all the related Message Templates
3234
-        $deleted = $MTG->delete_related_permanently('Message_Template');
3235
-
3236
-        if ($deleted === 0) {
3237
-            $success = 0;
3238
-        }
3239
-
3240
-        // now delete permanently this particular group
3241
-
3242
-        if ($include_group && ! $MTG->delete_permanently()) {
3243
-            $success = 0;
3244
-        }
3245
-
3246
-        return $success;
3247
-    }
3248
-
3249
-
3250
-    /**
3251
-     *    _learn_more_about_message_templates_link
3252
-     *
3253
-     * @access protected
3254
-     * @return string
3255
-     */
3256
-    protected function _learn_more_about_message_templates_link()
3257
-    {
3258
-        return '<a class="hidden" style="margin:0 20px; cursor:pointer; font-size:12px;" >'
3259
-               . esc_html__('learn more about how message templates works', 'event_espresso')
3260
-               . '</a>';
3261
-    }
3262
-
3263
-
3264
-    /**
3265
-     * Used for setting up messenger/message type activation.  This loads up the initial view.  The rest is handled by
3266
-     * ajax and other routes.
3267
-     *
3268
-     * @return void
3269
-     * @throws DomainException
3270
-     */
3271
-    protected function _settings()
3272
-    {
3273
-
3274
-
3275
-        $this->_set_m_mt_settings();
3276
-
3277
-        $selected_messenger = isset($this->_req_data['selected_messenger'])
3278
-            ? $this->_req_data['selected_messenger']
3279
-            : 'email';
3280
-
3281
-        // let's setup the messenger tabs
3282
-        $this->_template_args['admin_page_header'] = EEH_Tabbed_Content::tab_text_links(
3283
-            $this->_m_mt_settings['messenger_tabs'],
3284
-            'messenger_links',
3285
-            '|',
3286
-            $selected_messenger
3287
-        );
3288
-        $this->_template_args['before_admin_page_content'] = '<div class="ui-widget ui-helper-clearfix">';
3289
-        $this->_template_args['after_admin_page_content'] = '</div><!-- end .ui-widget -->';
3290
-
3291
-        $this->display_admin_page_with_sidebar();
3292
-    }
3293
-
3294
-
3295
-    /**
3296
-     * This sets the $_m_mt_settings property for when needed (used on the Messages settings page)
3297
-     *
3298
-     * @access protected
3299
-     * @return void
3300
-     * @throws DomainException
3301
-     */
3302
-    protected function _set_m_mt_settings()
3303
-    {
3304
-        // first if this is already set then lets get out no need to regenerate data.
3305
-        if (! empty($this->_m_mt_settings)) {
3306
-            return;
3307
-        }
3308
-
3309
-        // get all installed messengers and message_types
3310
-        /** @type EE_messenger[] $messengers */
3311
-        $messengers = $this->_message_resource_manager->installed_messengers();
3312
-        /** @type EE_message_type[] $message_types */
3313
-        $message_types = $this->_message_resource_manager->installed_message_types();
3314
-
3315
-
3316
-        // assemble the array for the _tab_text_links helper
3317
-
3318
-        foreach ($messengers as $messenger) {
3319
-            $this->_m_mt_settings['messenger_tabs'][ $messenger->name ] = array(
3320
-                'label' => ucwords($messenger->label['singular']),
3321
-                'class' => $this->_message_resource_manager->is_messenger_active($messenger->name)
3322
-                    ? 'messenger-active'
3323
-                    : '',
3324
-                'href'  => $messenger->name,
3325
-                'title' => esc_html__('Modify this Messenger', 'event_espresso'),
3326
-                'slug'  => $messenger->name,
3327
-                'obj'   => $messenger,
3328
-            );
3329
-
3330
-
3331
-            $message_types_for_messenger = $messenger->get_valid_message_types();
3332
-
3333
-            foreach ($message_types as $message_type) {
3334
-                // first we need to verify that this message type is valid with this messenger. Cause if it isn't then
3335
-                // it shouldn't show in either the inactive OR active metabox.
3336
-                if (! in_array($message_type->name, $message_types_for_messenger, true)) {
3337
-                    continue;
3338
-                }
3339
-
3340
-                $a_or_i = $this->_message_resource_manager->is_message_type_active_for_messenger(
3341
-                    $messenger->name,
3342
-                    $message_type->name
3343
-                )
3344
-                    ? 'active'
3345
-                    : 'inactive';
3346
-
3347
-                $this->_m_mt_settings['message_type_tabs'][ $messenger->name ][ $a_or_i ][ $message_type->name ] = array(
3348
-                    'label'    => ucwords($message_type->label['singular']),
3349
-                    'class'    => 'message-type-' . $a_or_i,
3350
-                    'slug_id'  => $message_type->name . '-messagetype-' . $messenger->name,
3351
-                    'mt_nonce' => wp_create_nonce($message_type->name . '_nonce'),
3352
-                    'href'     => 'espresso_' . $message_type->name . '_message_type_settings',
3353
-                    'title'    => $a_or_i === 'active'
3354
-                        ? esc_html__('Drag this message type to the Inactive window to deactivate', 'event_espresso')
3355
-                        : esc_html__('Drag this message type to the messenger to activate', 'event_espresso'),
3356
-                    'content'  => $a_or_i === 'active'
3357
-                        ? $this->_message_type_settings_content($message_type, $messenger, true)
3358
-                        : $this->_message_type_settings_content($message_type, $messenger),
3359
-                    'slug'     => $message_type->name,
3360
-                    'active'   => $a_or_i === 'active',
3361
-                    'obj'      => $message_type,
3362
-                );
3363
-            }
3364
-        }
3365
-    }
3366
-
3367
-
3368
-    /**
3369
-     * This just prepares the content for the message type settings
3370
-     *
3371
-     * @param  EE_message_type $message_type The message type object
3372
-     * @param  EE_messenger    $messenger    The messenger object
3373
-     * @param  boolean         $active       Whether the message type is active or not
3374
-     * @return string html output for the content
3375
-     * @throws DomainException
3376
-     */
3377
-    protected function _message_type_settings_content($message_type, $messenger, $active = false)
3378
-    {
3379
-        // get message type fields
3380
-        $fields = $message_type->get_admin_settings_fields();
3381
-        $settings_template_args['template_form_fields'] = '';
3382
-
3383
-        if (! empty($fields) && $active) {
3384
-            $existing_settings = $message_type->get_existing_admin_settings($messenger->name);
3385
-            foreach ($fields as $fldname => $fldprops) {
3386
-                $field_id = $messenger->name . '-' . $message_type->name . '-' . $fldname;
3387
-                $template_form_field[ $field_id ] = array(
3388
-                    'name'       => 'message_type_settings[' . $fldname . ']',
3389
-                    'label'      => $fldprops['label'],
3390
-                    'input'      => $fldprops['field_type'],
3391
-                    'type'       => $fldprops['value_type'],
3392
-                    'required'   => $fldprops['required'],
3393
-                    'validation' => $fldprops['validation'],
3394
-                    'value'      => isset($existing_settings[ $fldname ])
3395
-                        ? $existing_settings[ $fldname ]
3396
-                        : $fldprops['default'],
3397
-                    'options'    => isset($fldprops['options'])
3398
-                        ? $fldprops['options']
3399
-                        : array(),
3400
-                    'default'    => isset($existing_settings[ $fldname ])
3401
-                        ? $existing_settings[ $fldname ]
3402
-                        : $fldprops['default'],
3403
-                    'css_class'  => 'no-drag',
3404
-                    'format'     => $fldprops['format'],
3405
-                );
3406
-            }
3407
-
3408
-
3409
-            $settings_template_args['template_form_fields'] = ! empty($template_form_field)
3410
-                ? $this->_generate_admin_form_fields(
3411
-                    $template_form_field,
3412
-                    'string',
3413
-                    'ee_mt_activate_form'
3414
-                )
3415
-                : '';
3416
-        }
3417
-
3418
-        $settings_template_args['description'] = $message_type->description;
3419
-        // we also need some hidden fields
3420
-        $settings_template_args['hidden_fields'] = array(
3421
-            'message_type_settings[messenger]'    => array(
3422
-                'type'  => 'hidden',
3423
-                'value' => $messenger->name,
3424
-            ),
3425
-            'message_type_settings[message_type]' => array(
3426
-                'type'  => 'hidden',
3427
-                'value' => $message_type->name,
3428
-            ),
3429
-            'type'                                => array(
3430
-                'type'  => 'hidden',
3431
-                'value' => 'message_type',
3432
-            ),
3433
-        );
3434
-
3435
-        $settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields(
3436
-            $settings_template_args['hidden_fields'],
3437
-            'array'
3438
-        );
3439
-        $settings_template_args['show_form'] = empty($settings_template_args['template_form_fields'])
3440
-            ? ' hidden'
3441
-            : '';
3442
-
3443
-
3444
-        $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_mt_settings_content.template.php';
3445
-        $content = EEH_Template::display_template($template, $settings_template_args, true);
3446
-
3447
-        return $content;
3448
-    }
3449
-
3450
-
3451
-    /**
3452
-     * Generate all the metaboxes for the message types and register them for the messages settings page.
3453
-     *
3454
-     * @access protected
3455
-     * @return void
3456
-     * @throws DomainException
3457
-     */
3458
-    protected function _messages_settings_metaboxes()
3459
-    {
3460
-        $this->_set_m_mt_settings();
3461
-        $m_boxes = $mt_boxes = array();
3462
-        $m_template_args = $mt_template_args = array();
3463
-
3464
-        $selected_messenger = isset($this->_req_data['selected_messenger'])
3465
-            ? $this->_req_data['selected_messenger']
3466
-            : 'email';
3467
-
3468
-        if (isset($this->_m_mt_settings['messenger_tabs'])) {
3469
-            foreach ($this->_m_mt_settings['messenger_tabs'] as $messenger => $tab_array) {
3470
-                $hide_on_message = $this->_message_resource_manager->is_messenger_active($messenger) ? '' : 'hidden';
3471
-                $hide_off_message = $this->_message_resource_manager->is_messenger_active($messenger) ? 'hidden' : '';
3472
-                // messenger meta boxes
3473
-                $active = $selected_messenger === $messenger;
3474
-                $active_mt_tabs = isset(
3475
-                    $this->_m_mt_settings['message_type_tabs'][ $messenger ]['active']
3476
-                )
3477
-                    ? $this->_m_mt_settings['message_type_tabs'][ $messenger ]['active']
3478
-                    : '';
3479
-                $m_boxes[ $messenger . '_a_box' ] = sprintf(
3480
-                    esc_html__('%s Settings', 'event_espresso'),
3481
-                    $tab_array['label']
3482
-                );
3483
-                $m_template_args[ $messenger . '_a_box' ] = array(
3484
-                    'active_message_types'   => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '',
3485
-                    'inactive_message_types' => isset(
3486
-                        $this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive']
3487
-                    )
3488
-                        ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive'])
3489
-                        : '',
3490
-                    'content'                => $this->_get_messenger_box_content($tab_array['obj']),
3491
-                    'hidden'                 => $active ? '' : ' hidden',
3492
-                    'hide_on_message'        => $hide_on_message,
3493
-                    'messenger'              => $messenger,
3494
-                    'active'                 => $active,
3495
-                );
3496
-                // message type meta boxes
3497
-                // (which is really just the inactive container for each messenger
3498
-                // showing inactive message types for that messenger)
3499
-                $mt_boxes[ $messenger . '_i_box' ] = esc_html__('Inactive Message Types', 'event_espresso');
3500
-                $mt_template_args[ $messenger . '_i_box' ] = array(
3501
-                    'active_message_types'   => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '',
3502
-                    'inactive_message_types' => isset(
3503
-                        $this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive']
3504
-                    )
3505
-                        ? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive'])
3506
-                        : '',
3507
-                    'hidden'                 => $active ? '' : ' hidden',
3508
-                    'hide_on_message'        => $hide_on_message,
3509
-                    'hide_off_message'       => $hide_off_message,
3510
-                    'messenger'              => $messenger,
3511
-                    'active'                 => $active,
3512
-                );
3513
-            }
3514
-        }
3515
-
3516
-
3517
-        // register messenger metaboxes
3518
-        $m_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_mt_meta_box.template.php';
3519
-        foreach ($m_boxes as $box => $label) {
3520
-            $callback_args = array('template_path' => $m_template_path, 'template_args' => $m_template_args[ $box ]);
3521
-            $msgr = str_replace('_a_box', '', $box);
3522
-            add_meta_box(
3523
-                'espresso_' . $msgr . '_settings',
3524
-                $label,
3525
-                function ($post, $metabox) {
3526
-                    echo EEH_Template::display_template(
3527
-                        $metabox["args"]["template_path"],
3528
-                        $metabox["args"]["template_args"],
3529
-                        true
3530
-                    );
3531
-                },
3532
-                $this->_current_screen->id,
3533
-                'normal',
3534
-                'high',
3535
-                $callback_args
3536
-            );
3537
-        }
3538
-
3539
-        // register message type metaboxes
3540
-        $mt_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_meta_box.template.php';
3541
-        foreach ($mt_boxes as $box => $label) {
3542
-            $callback_args = array(
3543
-                'template_path' => $mt_template_path,
3544
-                'template_args' => $mt_template_args[ $box ],
3545
-            );
3546
-            $mt = str_replace('_i_box', '', $box);
3547
-            add_meta_box(
3548
-                'espresso_' . $mt . '_inactive_mts',
3549
-                $label,
3550
-                function ($post, $metabox) {
3551
-                    echo EEH_Template::display_template(
3552
-                        $metabox["args"]["template_path"],
3553
-                        $metabox["args"]["template_args"],
3554
-                        true
3555
-                    );
3556
-                },
3557
-                $this->_current_screen->id,
3558
-                'side',
3559
-                'high',
3560
-                $callback_args
3561
-            );
3562
-        }
3563
-
3564
-        // register metabox for global messages settings but only when on the main site.  On single site installs this
3565
-        // will always result in the metabox showing, on multisite installs the metabox will only show on the main site.
3566
-        if (is_main_site()) {
3567
-            add_meta_box(
3568
-                'espresso_global_message_settings',
3569
-                esc_html__('Global Message Settings', 'event_espresso'),
3570
-                array($this, 'global_messages_settings_metabox_content'),
3571
-                $this->_current_screen->id,
3572
-                'normal',
3573
-                'low',
3574
-                array()
3575
-            );
3576
-        }
3577
-    }
3578
-
3579
-
3580
-    /**
3581
-     *  This generates the content for the global messages settings metabox.
3582
-     *
3583
-     * @return string
3584
-     * @throws EE_Error
3585
-     * @throws InvalidArgumentException
3586
-     * @throws ReflectionException
3587
-     * @throws InvalidDataTypeException
3588
-     * @throws InvalidInterfaceException
3589
-     */
3590
-    public function global_messages_settings_metabox_content()
3591
-    {
3592
-        $form = $this->_generate_global_settings_form();
3593
-        echo $form->form_open(
3594
-            $this->add_query_args_and_nonce(array('action' => 'update_global_settings'), EE_MSG_ADMIN_URL),
3595
-            'POST'
3596
-        )
3597
-             . $form->get_html()
3598
-             . $form->form_close();
3599
-    }
3600
-
3601
-
3602
-    /**
3603
-     * This generates and returns the form object for the global messages settings.
3604
-     *
3605
-     * @return EE_Form_Section_Proper
3606
-     * @throws EE_Error
3607
-     * @throws InvalidArgumentException
3608
-     * @throws ReflectionException
3609
-     * @throws InvalidDataTypeException
3610
-     * @throws InvalidInterfaceException
3611
-     */
3612
-    protected function _generate_global_settings_form()
3613
-    {
3614
-        EE_Registry::instance()->load_helper('HTML');
3615
-        /** @var EE_Network_Core_Config $network_config */
3616
-        $network_config = EE_Registry::instance()->NET_CFG->core;
3617
-
3618
-        return new EE_Form_Section_Proper(
3619
-            array(
3620
-                'name'            => 'global_messages_settings',
3621
-                'html_id'         => 'global_messages_settings',
3622
-                'html_class'      => 'form-table',
3623
-                'layout_strategy' => new EE_Admin_Two_Column_Layout(),
3624
-                'subsections'     => apply_filters(
3625
-                    'FHEE__Messages_Admin_Page__global_messages_settings_metabox_content__form_subsections',
3626
-                    array(
3627
-                        'do_messages_on_same_request' => new EE_Select_Input(
3628
-                            array(
3629
-                                true  => esc_html__("On the same request", "event_espresso"),
3630
-                                false => esc_html__("On a separate request", "event_espresso"),
3631
-                            ),
3632
-                            array(
3633
-                                'default'         => $network_config->do_messages_on_same_request,
3634
-                                'html_label_text' => esc_html__(
3635
-                                    'Generate and send all messages:',
3636
-                                    'event_espresso'
3637
-                                ),
3638
-                                'html_help_text'  => esc_html__(
3639
-                                    '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.',
3640
-                                    'event_espresso'
3641
-                                ),
3642
-                            )
3643
-                        ),
3644
-                        'delete_threshold'            => new EE_Select_Input(
3645
-                            array(
3646
-                                0  => esc_html__('Forever', 'event_espresso'),
3647
-                                3  => esc_html__('3 Months', 'event_espresso'),
3648
-                                6  => esc_html__('6 Months', 'event_espresso'),
3649
-                                9  => esc_html__('9 Months', 'event_espresso'),
3650
-                                12 => esc_html__('12 Months', 'event_espresso'),
3651
-                                24 => esc_html__('24 Months', 'event_espresso'),
3652
-                                36 => esc_html__('36 Months', 'event_espresso'),
3653
-                            ),
3654
-                            array(
3655
-                                'default'         => EE_Registry::instance()->CFG->messages->delete_threshold,
3656
-                                'html_label_text' => esc_html__('Cleanup of old messages:', 'event_espresso'),
3657
-                                'html_help_text'  => esc_html__(
3658
-                                    'You can control how long a record of processed messages is kept via this option.',
3659
-                                    'event_espresso'
3660
-                                ),
3661
-                            )
3662
-                        ),
3663
-                        'update_settings'             => new EE_Submit_Input(
3664
-                            array(
3665
-                                'default'         => esc_html__('Update', 'event_espresso'),
3666
-                                'html_label_text' => '&nbsp',
3667
-                            )
3668
-                        ),
3669
-                    )
3670
-                ),
3671
-            )
3672
-        );
3673
-    }
3674
-
3675
-
3676
-    /**
3677
-     * This handles updating the global settings set on the admin page.
3678
-     *
3679
-     * @throws EE_Error
3680
-     * @throws InvalidDataTypeException
3681
-     * @throws InvalidInterfaceException
3682
-     * @throws InvalidArgumentException
3683
-     * @throws ReflectionException
3684
-     */
3685
-    protected function _update_global_settings()
3686
-    {
3687
-        /** @var EE_Network_Core_Config $network_config */
3688
-        $network_config = EE_Registry::instance()->NET_CFG->core;
3689
-        $messages_config = EE_Registry::instance()->CFG->messages;
3690
-        $form = $this->_generate_global_settings_form();
3691
-        if ($form->was_submitted()) {
3692
-            $form->receive_form_submission();
3693
-            if ($form->is_valid()) {
3694
-                $valid_data = $form->valid_data();
3695
-                foreach ($valid_data as $property => $value) {
3696
-                    $setter = 'set_' . $property;
3697
-                    if (method_exists($network_config, $setter)) {
3698
-                        $network_config->{$setter}($value);
3699
-                    } elseif (
3700
-                        property_exists($network_config, $property)
3701
-                        && $network_config->{$property} !== $value
3702
-                    ) {
3703
-                        $network_config->{$property} = $value;
3704
-                    } elseif (
3705
-                        property_exists($messages_config, $property)
3706
-                        && $messages_config->{$property} !== $value
3707
-                    ) {
3708
-                        $messages_config->{$property} = $value;
3709
-                    }
3710
-                }
3711
-                // only update if the form submission was valid!
3712
-                EE_Registry::instance()->NET_CFG->update_config(true, false);
3713
-                EE_Registry::instance()->CFG->update_espresso_config();
3714
-                EE_Error::overwrite_success();
3715
-                EE_Error::add_success(__('Global message settings were updated', 'event_espresso'));
3716
-            }
3717
-        }
3718
-        $this->_redirect_after_action(0, '', '', array('action' => 'settings'), true);
3719
-    }
3720
-
3721
-
3722
-    /**
3723
-     * this prepares the messenger tabs that can be dragged in and out of messenger boxes to activate/deactivate
3724
-     *
3725
-     * @param  array $tab_array This is an array of message type tab details used to generate the tabs
3726
-     * @return string html formatted tabs
3727
-     * @throws DomainException
3728
-     */
3729
-    protected function _get_mt_tabs($tab_array)
3730
-    {
3731
-        $tab_array = (array) $tab_array;
3732
-        $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_mt_settings_tab_item.template.php';
3733
-        $tabs = '';
3734
-
3735
-        foreach ($tab_array as $tab) {
3736
-            $tabs .= EEH_Template::display_template($template, $tab, true);
3737
-        }
3738
-
3739
-        return $tabs;
3740
-    }
3741
-
3742
-
3743
-    /**
3744
-     * This prepares the content of the messenger meta box admin settings
3745
-     *
3746
-     * @param  EE_messenger $messenger The messenger we're setting up content for
3747
-     * @return string html formatted content
3748
-     * @throws DomainException
3749
-     */
3750
-    protected function _get_messenger_box_content(EE_messenger $messenger)
3751
-    {
3752
-
3753
-        $fields = $messenger->get_admin_settings_fields();
3754
-        $settings_template_args['template_form_fields'] = '';
3755
-
3756
-        // is $messenger active?
3757
-        $settings_template_args['active'] = $this->_message_resource_manager->is_messenger_active($messenger->name);
3758
-
3759
-
3760
-        if (! empty($fields)) {
3761
-            $existing_settings = $messenger->get_existing_admin_settings();
3762
-
3763
-            foreach ($fields as $fldname => $fldprops) {
3764
-                $field_id = $messenger->name . '-' . $fldname;
3765
-                $template_form_field[ $field_id ] = array(
3766
-                    'name'       => 'messenger_settings[' . $field_id . ']',
3767
-                    'label'      => $fldprops['label'],
3768
-                    'input'      => $fldprops['field_type'],
3769
-                    'type'       => $fldprops['value_type'],
3770
-                    'required'   => $fldprops['required'],
3771
-                    'validation' => $fldprops['validation'],
3772
-                    'value'      => isset($existing_settings[ $field_id ])
3773
-                        ? $existing_settings[ $field_id ]
3774
-                        : $fldprops['default'],
3775
-                    'css_class'  => '',
3776
-                    'format'     => $fldprops['format'],
3777
-                );
3778
-            }
3779
-
3780
-
3781
-            $settings_template_args['template_form_fields'] = ! empty($template_form_field)
3782
-                ? $this->_generate_admin_form_fields($template_form_field, 'string', 'ee_m_activate_form')
3783
-                : '';
3784
-        }
3785
-
3786
-        // we also need some hidden fields
3787
-        $settings_template_args['hidden_fields'] = array(
3788
-            'messenger_settings[messenger]' => array(
3789
-                'type'  => 'hidden',
3790
-                'value' => $messenger->name,
3791
-            ),
3792
-            'type'                          => array(
3793
-                'type'  => 'hidden',
3794
-                'value' => 'messenger',
3795
-            ),
3796
-        );
3797
-
3798
-        // make sure any active message types that are existing are included in the hidden fields
3799
-        if (isset($this->_m_mt_settings['message_type_tabs'][ $messenger->name ]['active'])) {
3800
-            foreach ($this->_m_mt_settings['message_type_tabs'][ $messenger->name ]['active'] as $mt => $values) {
3801
-                $settings_template_args['hidden_fields'][ 'messenger_settings[message_types][' . $mt . ']' ] = array(
3802
-                    'type'  => 'hidden',
3803
-                    'value' => $mt,
3804
-                );
3805
-            }
3806
-        }
3807
-        $settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields(
3808
-            $settings_template_args['hidden_fields'],
3809
-            'array'
3810
-        );
3811
-        $active = $this->_message_resource_manager->is_messenger_active($messenger->name);
3812
-
3813
-        $settings_template_args['messenger'] = $messenger->name;
3814
-        $settings_template_args['description'] = $messenger->description;
3815
-        $settings_template_args['show_hide_edit_form'] = $active ? '' : ' hidden';
3816
-
3817
-
3818
-        $settings_template_args['show_hide_edit_form'] = $this->_message_resource_manager->is_messenger_active(
3819
-            $messenger->name
3820
-        )
3821
-            ? $settings_template_args['show_hide_edit_form']
3822
-            : ' hidden';
3823
-
3824
-        $settings_template_args['show_hide_edit_form'] = empty($settings_template_args['template_form_fields'])
3825
-            ? ' hidden'
3826
-            : $settings_template_args['show_hide_edit_form'];
3827
-
3828
-
3829
-        $settings_template_args['on_off_action'] = $active ? 'messenger-off' : 'messenger-on';
3830
-        $settings_template_args['nonce'] = wp_create_nonce('activate_' . $messenger->name . '_toggle_nonce');
3831
-        $settings_template_args['on_off_status'] = $active ? true : false;
3832
-        $template = EE_MSG_TEMPLATE_PATH . 'ee_msg_m_settings_content.template.php';
3833
-        $content = EEH_Template::display_template(
3834
-            $template,
3835
-            $settings_template_args,
3836
-            true
3837
-        );
3838
-
3839
-        return $content;
3840
-    }
3841
-
3842
-
3843
-    /**
3844
-     * used by ajax on the messages settings page to activate|deactivate the messenger
3845
-     *
3846
-     * @throws DomainException
3847
-     * @throws EE_Error
3848
-     * @throws InvalidDataTypeException
3849
-     * @throws InvalidInterfaceException
3850
-     * @throws InvalidArgumentException
3851
-     * @throws ReflectionException
3852
-     */
3853
-    public function activate_messenger_toggle()
3854
-    {
3855
-        $success = true;
3856
-        $this->_prep_default_response_for_messenger_or_message_type_toggle();
3857
-        // let's check that we have required data
3858
-        if (! isset($this->_req_data['messenger'])) {
3859
-            EE_Error::add_error(
3860
-                esc_html__('Messenger name needed to toggle activation. None given', 'event_espresso'),
3861
-                __FILE__,
3862
-                __FUNCTION__,
3863
-                __LINE__
3864
-            );
3865
-            $success = false;
3866
-        }
3867
-
3868
-        // do a nonce check here since we're not arriving via a normal route
3869
-        $nonce = isset($this->_req_data['activate_nonce'])
3870
-            ? sanitize_text_field($this->_req_data['activate_nonce'])
3871
-            : '';
3872
-        $nonce_ref = 'activate_' . $this->_req_data['messenger'] . '_toggle_nonce';
3873
-
3874
-        $this->_verify_nonce($nonce, $nonce_ref);
3875
-
3876
-
3877
-        if (! isset($this->_req_data['status'])) {
3878
-            EE_Error::add_error(
3879
-                esc_html__(
3880
-                    'Messenger status needed to know whether activation or deactivation is happening. No status is given',
3881
-                    'event_espresso'
3882
-                ),
3883
-                __FILE__,
3884
-                __FUNCTION__,
3885
-                __LINE__
3886
-            );
3887
-            $success = false;
3888
-        }
3889
-
3890
-        // do check to verify we have a valid status.
3891
-        $status = $this->_req_data['status'];
3892
-
3893
-        if ($status !== 'off' && $status !== 'on') {
3894
-            EE_Error::add_error(
3895
-                sprintf(
3896
-                    esc_html__('The given status (%s) is not valid. Must be "off" or "on"', 'event_espresso'),
3897
-                    $this->_req_data['status']
3898
-                ),
3899
-                __FILE__,
3900
-                __FUNCTION__,
3901
-                __LINE__
3902
-            );
3903
-            $success = false;
3904
-        }
3905
-
3906
-        if ($success) {
3907
-            // made it here?  Stop dawdling then!!
3908
-            $success = $status === 'off'
3909
-                ? $this->_deactivate_messenger($this->_req_data['messenger'])
3910
-                : $this->_activate_messenger($this->_req_data['messenger']);
3911
-        }
3912
-
3913
-        $this->_template_args['success'] = $success;
3914
-
3915
-        // no special instructions so let's just do the json return (which should automatically do all the special stuff).
3916
-        $this->_return_json();
3917
-    }
3918
-
3919
-
3920
-    /**
3921
-     * used by ajax from the messages settings page to activate|deactivate a message type
3922
-     *
3923
-     * @throws DomainException
3924
-     * @throws EE_Error
3925
-     * @throws ReflectionException
3926
-     * @throws InvalidDataTypeException
3927
-     * @throws InvalidInterfaceException
3928
-     * @throws InvalidArgumentException
3929
-     */
3930
-    public function activate_mt_toggle()
3931
-    {
3932
-        $success = true;
3933
-        $this->_prep_default_response_for_messenger_or_message_type_toggle();
3934
-
3935
-        // let's make sure we have the necessary data
3936
-        if (! isset($this->_req_data['message_type'])) {
3937
-            EE_Error::add_error(
3938
-                esc_html__('Message Type name needed to toggle activation. None given', 'event_espresso'),
3939
-                __FILE__,
3940
-                __FUNCTION__,
3941
-                __LINE__
3942
-            );
3943
-            $success = false;
3944
-        }
3945
-
3946
-        if (! isset($this->_req_data['messenger'])) {
3947
-            EE_Error::add_error(
3948
-                esc_html__('Messenger name needed to toggle activation. None given', 'event_espresso'),
3949
-                __FILE__,
3950
-                __FUNCTION__,
3951
-                __LINE__
3952
-            );
3953
-            $success = false;
3954
-        }
3955
-
3956
-        if (! isset($this->_req_data['status'])) {
3957
-            EE_Error::add_error(
3958
-                esc_html__(
3959
-                    'Messenger status needed to know whether activation or deactivation is happening. No status is given',
3960
-                    'event_espresso'
3961
-                ),
3962
-                __FILE__,
3963
-                __FUNCTION__,
3964
-                __LINE__
3965
-            );
3966
-            $success = false;
3967
-        }
3968
-
3969
-
3970
-        // do check to verify we have a valid status.
3971
-        $status = $this->_req_data['status'];
3972
-
3973
-        if ($status !== 'activate' && $status !== 'deactivate') {
3974
-            EE_Error::add_error(
3975
-                sprintf(
3976
-                    esc_html__('The given status (%s) is not valid. Must be "active" or "inactive"', 'event_espresso'),
3977
-                    $this->_req_data['status']
3978
-                ),
3979
-                __FILE__,
3980
-                __FUNCTION__,
3981
-                __LINE__
3982
-            );
3983
-            $success = false;
3984
-        }
3985
-
3986
-
3987
-        // do a nonce check here since we're not arriving via a normal route
3988
-        $nonce = isset($this->_req_data['mt_nonce']) ? sanitize_text_field($this->_req_data['mt_nonce']) : '';
3989
-        $nonce_ref = $this->_req_data['message_type'] . '_nonce';
3990
-
3991
-        $this->_verify_nonce($nonce, $nonce_ref);
3992
-
3993
-        if ($success) {
3994
-            // made it here? um, what are you waiting for then?
3995
-            $success = $status === 'deactivate'
3996
-                ? $this->_deactivate_message_type_for_messenger(
3997
-                    $this->_req_data['messenger'],
3998
-                    $this->_req_data['message_type']
3999
-                )
4000
-                : $this->_activate_message_type_for_messenger(
4001
-                    $this->_req_data['messenger'],
4002
-                    $this->_req_data['message_type']
4003
-                );
4004
-        }
4005
-
4006
-        $this->_template_args['success'] = $success;
4007
-        $this->_return_json();
4008
-    }
4009
-
4010
-
4011
-    /**
4012
-     * Takes care of processing activating a messenger and preparing the appropriate response.
4013
-     *
4014
-     * @param string $messenger_name The name of the messenger being activated
4015
-     * @return bool
4016
-     * @throws DomainException
4017
-     * @throws EE_Error
4018
-     * @throws InvalidArgumentException
4019
-     * @throws ReflectionException
4020
-     * @throws InvalidDataTypeException
4021
-     * @throws InvalidInterfaceException
4022
-     */
4023
-    protected function _activate_messenger($messenger_name)
4024
-    {
4025
-        /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4026
-        $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4027
-        $message_types_to_activate = $active_messenger instanceof EE_Messenger
4028
-            ? $active_messenger->get_default_message_types()
4029
-            : array();
4030
-
4031
-        // ensure is active
4032
-        $this->_message_resource_manager->activate_messenger($messenger_name, $message_types_to_activate);
4033
-
4034
-        // set response_data for reload
4035
-        foreach ($message_types_to_activate as $message_type_name) {
4036
-            /** @var EE_message_type $message_type */
4037
-            $message_type = $this->_message_resource_manager->get_message_type($message_type_name);
4038
-            if (
4039
-                $this->_message_resource_manager->is_message_type_active_for_messenger(
4040
-                    $messenger_name,
4041
-                    $message_type_name
4042
-                )
4043
-                && $message_type instanceof EE_message_type
4044
-            ) {
4045
-                $this->_template_args['data']['active_mts'][] = $message_type_name;
4046
-                if ($message_type->get_admin_settings_fields()) {
4047
-                    $this->_template_args['data']['mt_reload'][] = $message_type_name;
4048
-                }
4049
-            }
4050
-        }
4051
-
4052
-        // add success message for activating messenger
4053
-        return $this->_setup_response_message_for_activating_messenger_with_message_types($active_messenger);
4054
-    }
4055
-
4056
-
4057
-    /**
4058
-     * Takes care of processing deactivating a messenger and preparing the appropriate response.
4059
-     *
4060
-     * @param string $messenger_name The name of the messenger being activated
4061
-     * @return bool
4062
-     * @throws DomainException
4063
-     * @throws EE_Error
4064
-     * @throws InvalidArgumentException
4065
-     * @throws ReflectionException
4066
-     * @throws InvalidDataTypeException
4067
-     * @throws InvalidInterfaceException
4068
-     */
4069
-    protected function _deactivate_messenger($messenger_name)
4070
-    {
4071
-        /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4072
-        $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4073
-        $this->_message_resource_manager->deactivate_messenger($messenger_name);
4074
-
4075
-        return $this->_setup_response_message_for_deactivating_messenger_with_message_types($active_messenger);
4076
-    }
4077
-
4078
-
4079
-    /**
4080
-     * Takes care of processing activating a message type for a messenger and preparing the appropriate response.
4081
-     *
4082
-     * @param string $messenger_name    The name of the messenger the message type is being activated for.
4083
-     * @param string $message_type_name The name of the message type being activated for the messenger
4084
-     * @return bool
4085
-     * @throws DomainException
4086
-     * @throws EE_Error
4087
-     * @throws InvalidArgumentException
4088
-     * @throws ReflectionException
4089
-     * @throws InvalidDataTypeException
4090
-     * @throws InvalidInterfaceException
4091
-     */
4092
-    protected function _activate_message_type_for_messenger($messenger_name, $message_type_name)
4093
-    {
4094
-        /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4095
-        $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4096
-        /** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */
4097
-        $message_type_to_activate = $this->_message_resource_manager->get_message_type($message_type_name);
4098
-
4099
-        // ensure is active
4100
-        $this->_message_resource_manager->activate_messenger($messenger_name, $message_type_name);
4101
-
4102
-        // set response for load
4103
-        if (
4104
-            $this->_message_resource_manager->is_message_type_active_for_messenger(
4105
-                $messenger_name,
4106
-                $message_type_name
4107
-            )
4108
-        ) {
4109
-            $this->_template_args['data']['active_mts'][] = $message_type_name;
4110
-            if ($message_type_to_activate->get_admin_settings_fields()) {
4111
-                $this->_template_args['data']['mt_reload'][] = $message_type_name;
4112
-            }
4113
-        }
4114
-
4115
-        return $this->_setup_response_message_for_activating_messenger_with_message_types(
4116
-            $active_messenger,
4117
-            $message_type_to_activate
4118
-        );
4119
-    }
4120
-
4121
-
4122
-    /**
4123
-     * Takes care of processing deactivating a message type for a messenger and preparing the appropriate response.
4124
-     *
4125
-     * @param string $messenger_name    The name of the messenger the message type is being deactivated for.
4126
-     * @param string $message_type_name The name of the message type being deactivated for the messenger
4127
-     * @return bool
4128
-     * @throws DomainException
4129
-     * @throws EE_Error
4130
-     * @throws InvalidArgumentException
4131
-     * @throws ReflectionException
4132
-     * @throws InvalidDataTypeException
4133
-     * @throws InvalidInterfaceException
4134
-     */
4135
-    protected function _deactivate_message_type_for_messenger($messenger_name, $message_type_name)
4136
-    {
4137
-        /** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4138
-        $active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4139
-        /** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */
4140
-        $message_type_to_deactivate = $this->_message_resource_manager->get_message_type($message_type_name);
4141
-        $this->_message_resource_manager->deactivate_message_type_for_messenger($message_type_name, $messenger_name);
4142
-
4143
-        return $this->_setup_response_message_for_deactivating_messenger_with_message_types(
4144
-            $active_messenger,
4145
-            $message_type_to_deactivate
4146
-        );
4147
-    }
4148
-
4149
-
4150
-    /**
4151
-     * This just initializes the defaults for activating messenger and message type responses.
4152
-     */
4153
-    protected function _prep_default_response_for_messenger_or_message_type_toggle()
4154
-    {
4155
-        $this->_template_args['data']['active_mts'] = array();
4156
-        $this->_template_args['data']['mt_reload'] = array();
4157
-    }
4158
-
4159
-
4160
-    /**
4161
-     * Setup appropriate response for activating a messenger and/or message types
4162
-     *
4163
-     * @param EE_messenger         $messenger
4164
-     * @param EE_message_type|null $message_type
4165
-     * @return bool
4166
-     * @throws DomainException
4167
-     * @throws EE_Error
4168
-     * @throws InvalidArgumentException
4169
-     * @throws ReflectionException
4170
-     * @throws InvalidDataTypeException
4171
-     * @throws InvalidInterfaceException
4172
-     */
4173
-    protected function _setup_response_message_for_activating_messenger_with_message_types(
4174
-        $messenger,
4175
-        EE_Message_Type $message_type = null
4176
-    ) {
4177
-        // if $messenger isn't a valid messenger object then get out.
4178
-        if (! $messenger instanceof EE_Messenger) {
4179
-            EE_Error::add_error(
4180
-                esc_html__('The messenger being activated is not a valid messenger', 'event_espresso'),
4181
-                __FILE__,
4182
-                __FUNCTION__,
4183
-                __LINE__
4184
-            );
4185
-
4186
-            return false;
4187
-        }
4188
-        // activated
4189
-        if ($this->_template_args['data']['active_mts']) {
4190
-            EE_Error::overwrite_success();
4191
-            // activated a message type with the messenger
4192
-            if ($message_type instanceof EE_message_type) {
4193
-                EE_Error::add_success(
4194
-                    sprintf(
4195
-                        esc_html__(
4196
-                            '%s message type has been successfully activated with the %s messenger',
4197
-                            'event_espresso'
4198
-                        ),
4199
-                        ucwords($message_type->label['singular']),
4200
-                        ucwords($messenger->label['singular'])
4201
-                    )
4202
-                );
4203
-
4204
-                // if message type was invoice then let's make sure we activate the invoice payment method.
4205
-                if ($message_type->name === 'invoice') {
4206
-                    EE_Registry::instance()->load_lib('Payment_Method_Manager');
4207
-                    $pm = EE_Payment_Method_Manager::instance()->activate_a_payment_method_of_type('Invoice');
4208
-                    if ($pm instanceof EE_Payment_Method) {
4209
-                        EE_Error::add_attention(
4210
-                            esc_html__(
4211
-                                '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.',
4212
-                                'event_espresso'
4213
-                            )
4214
-                        );
4215
-                    }
4216
-                }
4217
-                // just toggles the entire messenger
4218
-            } else {
4219
-                EE_Error::add_success(
4220
-                    sprintf(
4221
-                        esc_html__('%s messenger has been successfully activated', 'event_espresso'),
4222
-                        ucwords($messenger->label['singular'])
4223
-                    )
4224
-                );
4225
-            }
4226
-
4227
-            return true;
4228
-
4229
-            // possible error condition. This will happen when our active_mts data is empty because it is validated for actual active
4230
-            // message types after the activation process.  However its possible some messengers don't HAVE any default_message_types
4231
-            // in which case we just give a success message for the messenger being successfully activated.
4232
-        } else {
4233
-            if (! $messenger->get_default_message_types()) {
4234
-                // messenger doesn't have any default message types so still a success.
4235
-                EE_Error::add_success(
4236
-                    sprintf(
4237
-                        esc_html__('%s messenger was successfully activated.', 'event_espresso'),
4238
-                        ucwords($messenger->label['singular'])
4239
-                    )
4240
-                );
4241
-
4242
-                return true;
4243
-            } else {
4244
-                EE_Error::add_error(
4245
-                    $message_type instanceof EE_message_type
4246
-                        ? sprintf(
4247
-                            esc_html__(
4248
-                                '%s message type was not successfully activated with the %s messenger',
4249
-                                'event_espresso'
4250
-                            ),
4251
-                            ucwords($message_type->label['singular']),
4252
-                            ucwords($messenger->label['singular'])
4253
-                        )
4254
-                        : sprintf(
4255
-                            esc_html__('%s messenger was not successfully activated', 'event_espresso'),
4256
-                            ucwords($messenger->label['singular'])
4257
-                        ),
4258
-                    __FILE__,
4259
-                    __FUNCTION__,
4260
-                    __LINE__
4261
-                );
4262
-
4263
-                return false;
4264
-            }
4265
-        }
4266
-    }
4267
-
4268
-
4269
-    /**
4270
-     * This sets up the appropriate response for deactivating a messenger and/or message type.
4271
-     *
4272
-     * @param EE_messenger         $messenger
4273
-     * @param EE_message_type|null $message_type
4274
-     * @return bool
4275
-     * @throws DomainException
4276
-     * @throws EE_Error
4277
-     * @throws InvalidArgumentException
4278
-     * @throws ReflectionException
4279
-     * @throws InvalidDataTypeException
4280
-     * @throws InvalidInterfaceException
4281
-     */
4282
-    protected function _setup_response_message_for_deactivating_messenger_with_message_types(
4283
-        $messenger,
4284
-        EE_message_type $message_type = null
4285
-    ) {
4286
-        EE_Error::overwrite_success();
4287
-
4288
-        // if $messenger isn't a valid messenger object then get out.
4289
-        if (! $messenger instanceof EE_Messenger) {
4290
-            EE_Error::add_error(
4291
-                esc_html__('The messenger being deactivated is not a valid messenger', 'event_espresso'),
4292
-                __FILE__,
4293
-                __FUNCTION__,
4294
-                __LINE__
4295
-            );
4296
-
4297
-            return false;
4298
-        }
4299
-
4300
-        if ($message_type instanceof EE_message_type) {
4301
-            $message_type_name = $message_type->name;
4302
-            EE_Error::add_success(
4303
-                sprintf(
4304
-                    esc_html__(
4305
-                        '%s message type has been successfully deactivated for the %s messenger.',
4306
-                        'event_espresso'
4307
-                    ),
4308
-                    ucwords($message_type->label['singular']),
4309
-                    ucwords($messenger->label['singular'])
4310
-                )
4311
-            );
4312
-        } else {
4313
-            $message_type_name = '';
4314
-            EE_Error::add_success(
4315
-                sprintf(
4316
-                    esc_html__('%s messenger has been successfully deactivated.', 'event_espresso'),
4317
-                    ucwords($messenger->label['singular'])
4318
-                )
4319
-            );
4320
-        }
4321
-
4322
-        // if messenger was html or message type was invoice then let's make sure we deactivate invoice payment method.
4323
-        if ($messenger->name === 'html' || $message_type_name === 'invoice') {
4324
-            EE_Registry::instance()->load_lib('Payment_Method_Manager');
4325
-            $count_updated = EE_Payment_Method_Manager::instance()->deactivate_payment_method('invoice');
4326
-            if ($count_updated > 0) {
4327
-                $msg = $message_type_name === 'invoice'
4328
-                    ? esc_html__(
4329
-                        '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.',
4330
-                        'event_espresso'
4331
-                    )
4332
-                    : esc_html__(
4333
-                        '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.',
4334
-                        'event_espresso'
4335
-                    );
4336
-                EE_Error::add_attention($msg);
4337
-            }
4338
-        }
4339
-
4340
-        return true;
4341
-    }
4342
-
4343
-
4344
-    /**
4345
-     * handles updating a message type form on messenger activation IF the message type has settings fields. (via ajax)
4346
-     *
4347
-     * @throws DomainException
4348
-     */
4349
-    public function update_mt_form()
4350
-    {
4351
-        if (! isset($this->_req_data['messenger']) || ! isset($this->_req_data['message_type'])) {
4352
-            EE_Error::add_error(
4353
-                esc_html__('Require message type or messenger to send an updated form', 'event_espresso'),
4354
-                __FILE__,
4355
-                __FUNCTION__,
4356
-                __LINE__
4357
-            );
4358
-            $this->_return_json();
4359
-        }
4360
-
4361
-        $message_types = $this->get_installed_message_types();
4362
-
4363
-        $message_type = $message_types[ $this->_req_data['message_type'] ];
4364
-        $messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']);
4365
-
4366
-        $content = $this->_message_type_settings_content(
4367
-            $message_type,
4368
-            $messenger,
4369
-            true
4370
-        );
4371
-        $this->_template_args['success'] = true;
4372
-        $this->_template_args['content'] = $content;
4373
-        $this->_return_json();
4374
-    }
4375
-
4376
-
4377
-    /**
4378
-     * this handles saving the settings for a messenger or message type
4379
-     *
4380
-     */
4381
-    public function save_settings()
4382
-    {
4383
-        if (! isset($this->_req_data['type'])) {
4384
-            EE_Error::add_error(
4385
-                esc_html__(
4386
-                    'Cannot save settings because type is unknown (messenger settings or messsage type settings?)',
4387
-                    'event_espresso'
4388
-                ),
4389
-                __FILE__,
4390
-                __FUNCTION__,
4391
-                __LINE__
4392
-            );
4393
-            $this->_template_args['error'] = true;
4394
-            $this->_return_json();
4395
-        }
4396
-
4397
-
4398
-        if ($this->_req_data['type'] === 'messenger') {
4399
-            // this should be an array.
4400
-            $settings = $this->_req_data['messenger_settings'];
4401
-            $messenger = $settings['messenger'];
4402
-            // let's setup the settings data
4403
-            foreach ($settings as $key => $value) {
4404
-                switch ($key) {
4405
-                    case 'messenger':
4406
-                        unset($settings['messenger']);
4407
-                        break;
4408
-                    case 'message_types':
4409
-                        unset($settings['message_types']);
4410
-                        break;
4411
-                    default:
4412
-                        $settings[ $key ] = $value;
4413
-                        break;
4414
-                }
4415
-            }
4416
-            $this->_message_resource_manager->add_settings_for_messenger($messenger, $settings);
4417
-        } elseif ($this->_req_data['type'] === 'message_type') {
4418
-            $settings = $this->_req_data['message_type_settings'];
4419
-            $messenger = $settings['messenger'];
4420
-            $message_type = $settings['message_type'];
4421
-
4422
-            foreach ($settings as $key => $value) {
4423
-                switch ($key) {
4424
-                    case 'messenger':
4425
-                        unset($settings['messenger']);
4426
-                        break;
4427
-                    case 'message_type':
4428
-                        unset($settings['message_type']);
4429
-                        break;
4430
-                    default:
4431
-                        $settings[ $key ] = $value;
4432
-                        break;
4433
-                }
4434
-            }
4435
-
4436
-            $this->_message_resource_manager->add_settings_for_message_type($messenger, $message_type, $settings);
4437
-        }
4438
-
4439
-        // okay we should have the data all setup.  Now we just update!
4440
-        $success = $this->_message_resource_manager->update_active_messengers_option();
4441
-
4442
-        if ($success) {
4443
-            EE_Error::add_success(__('Settings updated', 'event_espresso'));
4444
-        } else {
4445
-            EE_Error::add_error(
4446
-                esc_html__(
4447
-                    'Settings did not get updated',
4448
-                    'event_espresso'
4449
-                ),
4450
-                __FILE__,
4451
-                __FUNCTION__,
4452
-                __LINE__
4453
-            );
4454
-        }
4455
-
4456
-        $this->_template_args['success'] = $success;
4457
-        $this->_return_json();
4458
-    }
4459
-
4460
-
4461
-
4462
-
4463
-    /**  EE MESSAGE PROCESSING ACTIONS **/
4464
-
4465
-
4466
-    /**
4467
-     * This immediately generates any EE_Message ID's that are selected that are EEM_Message::status_incomplete
4468
-     * However, this does not send immediately, it just queues for sending.
4469
-     *
4470
-     * @since 4.9.0
4471
-     * @throws EE_Error
4472
-     * @throws InvalidDataTypeException
4473
-     * @throws InvalidInterfaceException
4474
-     * @throws InvalidArgumentException
4475
-     * @throws ReflectionException
4476
-     */
4477
-    protected function _generate_now()
4478
-    {
4479
-        EED_Messages::generate_now($this->_get_msg_ids_from_request());
4480
-        $this->_redirect_after_action(false, '', '', array(), true);
4481
-    }
4482
-
4483
-
4484
-    /**
4485
-     * This immediately generates AND sends any EE_Message's selected that are EEM_Message::status_incomplete or that
4486
-     * are EEM_Message::status_resend or EEM_Message::status_idle
4487
-     *
4488
-     * @since 4.9.0
4489
-     * @throws EE_Error
4490
-     * @throws InvalidDataTypeException
4491
-     * @throws InvalidInterfaceException
4492
-     * @throws InvalidArgumentException
4493
-     * @throws ReflectionException
4494
-     */
4495
-    protected function _generate_and_send_now()
4496
-    {
4497
-        EED_Messages::generate_and_send_now($this->_get_msg_ids_from_request());
4498
-        $this->_redirect_after_action(false, '', '', array(), true);
4499
-    }
4500
-
4501
-
4502
-    /**
4503
-     * This queues any EEM_Message::status_sent EE_Message ids in the request for resending.
4504
-     *
4505
-     * @since 4.9.0
4506
-     * @throws EE_Error
4507
-     * @throws InvalidDataTypeException
4508
-     * @throws InvalidInterfaceException
4509
-     * @throws InvalidArgumentException
4510
-     * @throws ReflectionException
4511
-     */
4512
-    protected function _queue_for_resending()
4513
-    {
4514
-        EED_Messages::queue_for_resending($this->_get_msg_ids_from_request());
4515
-        $this->_redirect_after_action(false, '', '', array(), true);
4516
-    }
4517
-
4518
-
4519
-    /**
4520
-     *  This sends immediately any EEM_Message::status_idle or EEM_Message::status_resend messages in the queue
4521
-     *
4522
-     * @since 4.9.0
4523
-     * @throws EE_Error
4524
-     * @throws InvalidDataTypeException
4525
-     * @throws InvalidInterfaceException
4526
-     * @throws InvalidArgumentException
4527
-     * @throws ReflectionException
4528
-     */
4529
-    protected function _send_now()
4530
-    {
4531
-        EED_Messages::send_now($this->_get_msg_ids_from_request());
4532
-        $this->_redirect_after_action(false, '', '', array(), true);
4533
-    }
4534
-
4535
-
4536
-    /**
4537
-     * Deletes EE_messages for IDs in the request.
4538
-     *
4539
-     * @since 4.9.0
4540
-     * @throws EE_Error
4541
-     * @throws InvalidDataTypeException
4542
-     * @throws InvalidInterfaceException
4543
-     * @throws InvalidArgumentException
4544
-     */
4545
-    protected function _delete_ee_messages()
4546
-    {
4547
-        $msg_ids = $this->_get_msg_ids_from_request();
4548
-        $deleted_count = 0;
4549
-        foreach ($msg_ids as $msg_id) {
4550
-            if (EEM_Message::instance()->delete_by_ID($msg_id)) {
4551
-                $deleted_count++;
4552
-            }
4553
-        }
4554
-        if ($deleted_count) {
4555
-            EE_Error::add_success(
4556
-                esc_html(
4557
-                    _n(
4558
-                        'Message successfully deleted',
4559
-                        'Messages successfully deleted',
4560
-                        $deleted_count,
4561
-                        'event_espresso'
4562
-                    )
4563
-                )
4564
-            );
4565
-            $this->_redirect_after_action(
4566
-                false,
4567
-                '',
4568
-                '',
4569
-                array(),
4570
-                true
4571
-            );
4572
-        } else {
4573
-            EE_Error::add_error(
4574
-                _n('The message was not deleted.', 'The messages were not deleted', count($msg_ids), 'event_espresso'),
4575
-                __FILE__,
4576
-                __FUNCTION__,
4577
-                __LINE__
4578
-            );
4579
-            $this->_redirect_after_action(false, '', '', array(), true);
4580
-        }
4581
-    }
4582
-
4583
-
4584
-    /**
4585
-     *  This looks for 'MSG_ID' key in the request and returns an array of MSG_ID's if present.
4586
-     *
4587
-     * @since 4.9.0
4588
-     * @return array
4589
-     */
4590
-    protected function _get_msg_ids_from_request()
4591
-    {
4592
-        if (! isset($this->_req_data['MSG_ID'])) {
4593
-            return array();
4594
-        }
4595
-
4596
-        return is_array($this->_req_data['MSG_ID'])
4597
-            ? array_keys($this->_req_data['MSG_ID'])
4598
-            : array($this->_req_data['MSG_ID']);
4599
-    }
2674
+		$output = ob_get_contents();
2675
+		ob_clean();
2676
+		$this->_context_switcher = $output;
2677
+	}
2678
+
2679
+
2680
+	/**
2681
+	 * utility for sanitizing new values coming in.
2682
+	 * Note: this is only used when updating a context.
2683
+	 *
2684
+	 * @access protected
2685
+	 *
2686
+	 * @param int $index This helps us know which template field to select from the request array.
2687
+	 *
2688
+	 * @return array
2689
+	 */
2690
+	protected function _set_message_template_column_values($index)
2691
+	{
2692
+		if (is_array($this->_req_data['MTP_template_fields'][ $index ]['content'])) {
2693
+			foreach ($this->_req_data['MTP_template_fields'][ $index ]['content'] as $field => $value) {
2694
+				$this->_req_data['MTP_template_fields'][ $index ]['content'][ $field ] = $value;
2695
+			}
2696
+		}
2697
+
2698
+
2699
+		$set_column_values = array(
2700
+			'MTP_ID'             => absint($this->_req_data['MTP_template_fields'][ $index ]['MTP_ID']),
2701
+			'GRP_ID'             => absint($this->_req_data['GRP_ID']),
2702
+			'MTP_user_id'        => absint($this->_req_data['MTP_user_id']),
2703
+			'MTP_messenger'      => strtolower($this->_req_data['MTP_messenger']),
2704
+			'MTP_message_type'   => strtolower($this->_req_data['MTP_message_type']),
2705
+			'MTP_template_field' => strtolower($this->_req_data['MTP_template_fields'][ $index ]['name']),
2706
+			'MTP_context'        => strtolower($this->_req_data['MTP_context']),
2707
+			'MTP_content'        => $this->_req_data['MTP_template_fields'][ $index ]['content'],
2708
+			'MTP_is_global'      => isset($this->_req_data['MTP_is_global'])
2709
+				? absint($this->_req_data['MTP_is_global'])
2710
+				: 0,
2711
+			'MTP_is_override'    => isset($this->_req_data['MTP_is_override'])
2712
+				? absint($this->_req_data['MTP_is_override'])
2713
+				: 0,
2714
+			'MTP_deleted'        => absint($this->_req_data['MTP_deleted']),
2715
+			'MTP_is_active'      => absint($this->_req_data['MTP_is_active']),
2716
+		);
2717
+
2718
+
2719
+		return $set_column_values;
2720
+	}
2721
+
2722
+
2723
+	protected function _insert_or_update_message_template($new = false)
2724
+	{
2725
+		$success = 0;
2726
+		$override = false;
2727
+
2728
+		// setup notices description
2729
+		$messenger_slug = ! empty($this->_req_data['MTP_messenger']) ? $this->_req_data['MTP_messenger'] : '';
2730
+
2731
+		// need the message type and messenger objects to be able to use the labels for the notices
2732
+		$messenger_object = $this->_message_resource_manager->get_messenger($messenger_slug);
2733
+		$messenger_label = $messenger_object instanceof EE_messenger
2734
+			? ucwords($messenger_object->label['singular'])
2735
+			: '';
2736
+
2737
+		$message_type_slug = ! empty($this->_req_data['MTP_message_type'])
2738
+			? $this->_req_data['MTP_message_type']
2739
+			: '';
2740
+		$message_type_object = $this->_message_resource_manager->get_message_type($message_type_slug);
2741
+
2742
+		$message_type_label = $message_type_object instanceof EE_message_type
2743
+			? ucwords($message_type_object->label['singular'])
2744
+			: '';
2745
+
2746
+		$context_slug = ! empty($this->_req_data['MTP_context'])
2747
+			? $this->_req_data['MTP_context']
2748
+			: '';
2749
+		$context = ucwords(str_replace('_', ' ', $context_slug));
2750
+
2751
+		$item_desc = $messenger_label && $message_type_label
2752
+			? $messenger_label . ' ' . $message_type_label . ' ' . $context . ' '
2753
+			: '';
2754
+		$item_desc .= 'Message Template';
2755
+		$query_args = array();
2756
+		$edit_array = array();
2757
+		$action_desc = '';
2758
+
2759
+		// if this is "new" then we need to generate the default contexts for the selected messenger/message_type for
2760
+		// user to edit.
2761
+		if ($new) {
2762
+			$GRP_ID = ! empty($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : 0;
2763
+			if ($edit_array = $this->_generate_new_templates($messenger_slug, $message_type_slug, $GRP_ID)) {
2764
+				if (empty($edit_array)) {
2765
+					$success = 0;
2766
+				} else {
2767
+					$success = 1;
2768
+					$edit_array = $edit_array[0];
2769
+					$query_args = array(
2770
+						'id'      => $edit_array['GRP_ID'],
2771
+						'context' => $edit_array['MTP_context'],
2772
+						'action'  => 'edit_message_template',
2773
+					);
2774
+				}
2775
+			}
2776
+			$action_desc = 'created';
2777
+		} else {
2778
+			$MTPG = EEM_Message_Template_Group::instance();
2779
+			$MTP = EEM_Message_Template::instance();
2780
+
2781
+
2782
+			// run update for each template field in displayed context
2783
+			if (! isset($this->_req_data['MTP_template_fields']) && empty($this->_req_data['MTP_template_fields'])) {
2784
+				EE_Error::add_error(
2785
+					esc_html__(
2786
+						'There was a problem saving the template fields from the form because I didn\'t receive any actual template field data.',
2787
+						'event_espresso'
2788
+					),
2789
+					__FILE__,
2790
+					__FUNCTION__,
2791
+					__LINE__
2792
+				);
2793
+				$success = 0;
2794
+			} else {
2795
+				// first validate all fields!
2796
+				// this filter allows client code to add its own validation to the template fields as well.
2797
+				// returning an empty array means everything passed validation.
2798
+				// errors in validation should be represented in an array with the following shape:
2799
+				// array(
2800
+				//   'fieldname' => array(
2801
+				//          'msg' => 'error message'
2802
+				//          'value' => 'value for field producing error'
2803
+				// )
2804
+				$custom_validation = (array) apply_filters(
2805
+					'FHEE__Messages_Admin_Page___insert_or_update_message_template__validates',
2806
+					array(),
2807
+					$this->_req_data['MTP_template_fields'],
2808
+					$context_slug,
2809
+					$messenger_slug,
2810
+					$message_type_slug
2811
+				);
2812
+
2813
+				$system_validation = $MTPG->validate(
2814
+					$this->_req_data['MTP_template_fields'],
2815
+					$context_slug,
2816
+					$messenger_slug,
2817
+					$message_type_slug
2818
+				);
2819
+
2820
+				$system_validation = ! is_array($system_validation) && $system_validation ? array()
2821
+					: $system_validation;
2822
+				$validates = array_merge($custom_validation, $system_validation);
2823
+
2824
+				// if $validate returned error messages (i.e. is_array()) then we need to process them and setup an
2825
+				// appropriate response. HMM, dang this isn't correct, $validates will ALWAYS be an array.
2826
+				//  WE need to make sure there is no actual error messages in validates.
2827
+				if (is_array($validates) && ! empty($validates)) {
2828
+					// add the transient so when the form loads we know which fields to highlight
2829
+					$this->_add_transient('edit_message_template', $validates);
2830
+
2831
+					$success = 0;
2832
+
2833
+					// setup notices
2834
+					foreach ($validates as $field => $error) {
2835
+						if (isset($error['msg'])) {
2836
+							EE_Error::add_error($error['msg'], __FILE__, __FUNCTION__, __LINE__);
2837
+						}
2838
+					}
2839
+				} else {
2840
+					$set_column_values = array();
2841
+					foreach ($this->_req_data['MTP_template_fields'] as $template_field => $content) {
2842
+						$set_column_values = $this->_set_message_template_column_values($template_field);
2843
+
2844
+						$where_cols_n_values = array(
2845
+							'MTP_ID' => $this->_req_data['MTP_template_fields'][ $template_field ]['MTP_ID'],
2846
+						);
2847
+						// if they aren't allowed to use all JS, restrict them to just posty-y tags
2848
+						if (! current_user_can('unfiltered_html')) {
2849
+							if (is_array($set_column_values['MTP_content'])) {
2850
+								foreach ($set_column_values['MTP_content'] as $key => $value) {
2851
+									// remove slashes so wp_kses works properly (its wp_kses_stripslashes() function
2852
+									// only removes slashes from double-quotes, so attributes using single quotes always
2853
+									// appear invalid.) But currently the models expect slashed data, so after wp_kses
2854
+									// runs we need to re-slash the data. Sheesh. See
2855
+									// https://events.codebasehq.com/projects/event-espresso/tickets/11211#update-47321587
2856
+									$set_column_values['MTP_content'][ $key ] = addslashes(
2857
+										wp_kses(
2858
+											stripslashes($value),
2859
+											wp_kses_allowed_html('post')
2860
+										)
2861
+									);
2862
+								}
2863
+							} else {
2864
+								$set_column_values['MTP_content'] = wp_kses(
2865
+									$set_column_values['MTP_content'],
2866
+									wp_kses_allowed_html('post')
2867
+								);
2868
+							}
2869
+						}
2870
+						$message_template_fields = array(
2871
+							'GRP_ID'             => $set_column_values['GRP_ID'],
2872
+							'MTP_template_field' => $set_column_values['MTP_template_field'],
2873
+							'MTP_context'        => $set_column_values['MTP_context'],
2874
+							'MTP_content'        => $set_column_values['MTP_content'],
2875
+						);
2876
+						if ($updated = $MTP->update($message_template_fields, array($where_cols_n_values))) {
2877
+							if ($updated === false) {
2878
+								EE_Error::add_error(
2879
+									sprintf(
2880
+										esc_html__('%s field was NOT updated for some reason', 'event_espresso'),
2881
+										$template_field
2882
+									),
2883
+									__FILE__,
2884
+									__FUNCTION__,
2885
+									__LINE__
2886
+								);
2887
+							} else {
2888
+								$success = 1;
2889
+							}
2890
+						} else {
2891
+							// only do this logic if we don't have a MTP_ID for this field
2892
+							if (empty($this->_req_data['MTP_template_fields'][ $template_field ]['MTP_ID'])) {
2893
+								// this has already been through the template field validator and sanitized, so it will be
2894
+								// safe to insert this field.  Why insert?  This typically happens when we introduce a new
2895
+								// message template field in a messenger/message type and existing users don't have the
2896
+								// default setup for it.
2897
+								// @link https://events.codebasehq.com/projects/event-espresso/tickets/9465
2898
+								$updated = $MTP->insert($message_template_fields);
2899
+								if (! $updated || is_wp_error($updated)) {
2900
+									EE_Error::add_error(
2901
+										sprintf(
2902
+											esc_html__('%s field could not be updated.', 'event_espresso'),
2903
+											$template_field
2904
+										),
2905
+										__FILE__,
2906
+										__FUNCTION__,
2907
+										__LINE__
2908
+									);
2909
+									$success = 0;
2910
+								} else {
2911
+									$success = 1;
2912
+								}
2913
+							}
2914
+						}
2915
+						$action_desc = 'updated';
2916
+					}
2917
+
2918
+					// we can use the last set_column_values for the MTPG update (because its the same for all of these specific MTPs)
2919
+					$mtpg_fields = array(
2920
+						'MTP_user_id'      => $set_column_values['MTP_user_id'],
2921
+						'MTP_messenger'    => $set_column_values['MTP_messenger'],
2922
+						'MTP_message_type' => $set_column_values['MTP_message_type'],
2923
+						'MTP_is_global'    => $set_column_values['MTP_is_global'],
2924
+						'MTP_is_override'  => $set_column_values['MTP_is_override'],
2925
+						'MTP_deleted'      => $set_column_values['MTP_deleted'],
2926
+						'MTP_is_active'    => $set_column_values['MTP_is_active'],
2927
+						'MTP_name'         => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_name'])
2928
+							? $this->_req_data['ee_msg_non_global_fields']['MTP_name']
2929
+							: '',
2930
+						'MTP_description'  => ! empty($this->_req_data['ee_msg_non_global_fields']['MTP_description'])
2931
+							? $this->_req_data['ee_msg_non_global_fields']['MTP_description']
2932
+							: '',
2933
+					);
2934
+
2935
+					$mtpg_where = array('GRP_ID' => $set_column_values['GRP_ID']);
2936
+					$updated = $MTPG->update($mtpg_fields, array($mtpg_where));
2937
+
2938
+					if ($updated === false) {
2939
+						EE_Error::add_error(
2940
+							sprintf(
2941
+								esc_html__(
2942
+									'The Message Template Group (%d) was NOT updated for some reason',
2943
+									'event_espresso'
2944
+								),
2945
+								$set_column_values['GRP_ID']
2946
+							),
2947
+							__FILE__,
2948
+							__FUNCTION__,
2949
+							__LINE__
2950
+						);
2951
+					} else {
2952
+						// k now we need to ensure the template_pack and template_variation fields are set.
2953
+						$template_pack = ! empty($this->_req_data['MTP_template_pack'])
2954
+							? $this->_req_data['MTP_template_pack']
2955
+							: 'default';
2956
+
2957
+						$template_variation = ! empty($this->_req_data['MTP_template_variation'])
2958
+							? $this->_req_data['MTP_template_variation']
2959
+							: 'default';
2960
+
2961
+						$mtpg_obj = $MTPG->get_one_by_ID($set_column_values['GRP_ID']);
2962
+						if ($mtpg_obj instanceof EE_Message_Template_Group) {
2963
+							$mtpg_obj->set_template_pack_name($template_pack);
2964
+							$mtpg_obj->set_template_pack_variation($template_variation);
2965
+						}
2966
+						$success = 1;
2967
+					}
2968
+				}
2969
+			}
2970
+		}
2971
+
2972
+		// we return things differently if doing ajax
2973
+		if (defined('DOING_AJAX') && DOING_AJAX) {
2974
+			$this->_template_args['success'] = $success;
2975
+			$this->_template_args['error'] = ! $success ? true : false;
2976
+			$this->_template_args['content'] = '';
2977
+			$this->_template_args['data'] = array(
2978
+				'grpID'        => $edit_array['GRP_ID'],
2979
+				'templateName' => $edit_array['template_name'],
2980
+			);
2981
+			if ($success) {
2982
+				EE_Error::overwrite_success();
2983
+				EE_Error::add_success(
2984
+					esc_html__(
2985
+						'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.',
2986
+						'event_espresso'
2987
+					)
2988
+				);
2989
+			}
2990
+
2991
+			$this->_return_json();
2992
+		}
2993
+
2994
+
2995
+		// was a test send triggered?
2996
+		if (isset($this->_req_data['test_button'])) {
2997
+			EE_Error::overwrite_success();
2998
+			$this->_do_test_send($context_slug, $messenger_slug, $message_type_slug);
2999
+			$override = true;
3000
+		}
3001
+
3002
+		if (empty($query_args)) {
3003
+			$query_args = array(
3004
+				'id'      => $this->_req_data['GRP_ID'],
3005
+				'context' => $context_slug,
3006
+				'action'  => 'edit_message_template',
3007
+			);
3008
+		}
3009
+
3010
+		$this->_redirect_after_action($success, $item_desc, $action_desc, $query_args, $override);
3011
+	}
3012
+
3013
+
3014
+	/**
3015
+	 * processes a test send request to do an actual messenger delivery test for the given message template being tested
3016
+	 *
3017
+	 * @param  string $context      what context being tested
3018
+	 * @param  string $messenger    messenger being tested
3019
+	 * @param  string $message_type message type being tested
3020
+	 * @throws EE_Error
3021
+	 * @throws InvalidArgumentException
3022
+	 * @throws InvalidDataTypeException
3023
+	 * @throws InvalidInterfaceException
3024
+	 */
3025
+	protected function _do_test_send($context, $messenger, $message_type)
3026
+	{
3027
+		// set things up for preview
3028
+		$this->_req_data['messenger'] = $messenger;
3029
+		$this->_req_data['message_type'] = $message_type;
3030
+		$this->_req_data['context'] = $context;
3031
+		$this->_req_data['GRP_ID'] = isset($this->_req_data['GRP_ID']) ? $this->_req_data['GRP_ID'] : '';
3032
+		$active_messenger = $this->_message_resource_manager->get_active_messenger($messenger);
3033
+
3034
+		// let's save any existing fields that might be required by the messenger
3035
+		if (
3036
+			isset($this->_req_data['test_settings_fld'])
3037
+			&& $active_messenger instanceof EE_messenger
3038
+			&& apply_filters(
3039
+				'FHEE__Messages_Admin_Page__do_test_send__set_existing_test_settings',
3040
+				true,
3041
+				$this->_req_data['test_settings_fld'],
3042
+				$active_messenger
3043
+			)
3044
+		) {
3045
+			$active_messenger->set_existing_test_settings($this->_req_data['test_settings_fld']);
3046
+		}
3047
+
3048
+		/**
3049
+		 * Use filter to add additional controls on whether message can send or not
3050
+		 */
3051
+		if (
3052
+			apply_filters(
3053
+				'FHEE__Messages_Admin_Page__do_test_send__can_send',
3054
+				true,
3055
+				$context,
3056
+				$this->_req_data,
3057
+				$messenger,
3058
+				$message_type
3059
+			)
3060
+		) {
3061
+			if (EEM_Event::instance()->count() > 0) {
3062
+				$success = $this->_preview_message(true);
3063
+				if ($success) {
3064
+					EE_Error::add_success(__('Test message sent', 'event_espresso'));
3065
+				} else {
3066
+					EE_Error::add_error(
3067
+						esc_html__('The test message was not sent', 'event_espresso'),
3068
+						__FILE__,
3069
+						__FUNCTION__,
3070
+						__LINE__
3071
+					);
3072
+				}
3073
+			} else {
3074
+				$this->noEventsErrorMessage(true);
3075
+			}
3076
+		}
3077
+	}
3078
+
3079
+
3080
+	/**
3081
+	 * _generate_new_templates
3082
+	 * This will handle the messenger, message_type selection when "adding a new custom template" for an event and will
3083
+	 * automatically create the defaults for the event.  The user would then be redirected to edit the default context
3084
+	 * for the event.
3085
+	 *
3086
+	 *
3087
+	 * @param  string $messenger     the messenger we are generating templates for
3088
+	 * @param array   $message_types array of message types that the templates are generated for.
3089
+	 * @param int     $GRP_ID        If this is a custom template being generated then a GRP_ID needs to be included to
3090
+	 *                               indicate the message_template_group being used as the base.
3091
+	 *
3092
+	 * @param bool    $global
3093
+	 *
3094
+	 * @return array|bool array of data required for the redirect to the correct edit page or bool if
3095
+	 *                               encountering problems.
3096
+	 * @throws EE_Error
3097
+	 */
3098
+	protected function _generate_new_templates($messenger, $message_types, $GRP_ID = 0, $global = false)
3099
+	{
3100
+
3101
+		// if no $message_types are given then that's okay... this may be a messenger that just adds shortcodes, so we
3102
+		// just don't generate any templates.
3103
+		if (empty($message_types)) {
3104
+			return true;
3105
+		}
3106
+
3107
+		return EEH_MSG_Template::generate_new_templates($messenger, $message_types, $GRP_ID, $global);
3108
+	}
3109
+
3110
+
3111
+	/**
3112
+	 * [_trash_or_restore_message_template]
3113
+	 *
3114
+	 * @param  boolean $trash whether to move an item to trash/restore (TRUE) or restore it (FALSE)
3115
+	 * @param boolean  $all   whether this is going to trash/restore all contexts within a template group (TRUE) OR just
3116
+	 *                        an individual context (FALSE).
3117
+	 * @return void
3118
+	 * @throws EE_Error
3119
+	 * @throws InvalidArgumentException
3120
+	 * @throws InvalidDataTypeException
3121
+	 * @throws InvalidInterfaceException
3122
+	 */
3123
+	protected function _trash_or_restore_message_template($trash = true, $all = false)
3124
+	{
3125
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
3126
+		$MTP = EEM_Message_Template_Group::instance();
3127
+
3128
+		$success = 1;
3129
+
3130
+		// incoming GRP_IDs
3131
+		if ($all) {
3132
+			// Checkboxes
3133
+			if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3134
+				// if array has more than one element then success message should be plural.
3135
+				// todo: what about nonce?
3136
+				$success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
3137
+
3138
+				// cycle through checkboxes
3139
+				while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) {
3140
+					$trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID);
3141
+					if (! $trashed_or_restored) {
3142
+						$success = 0;
3143
+					}
3144
+				}
3145
+			} else {
3146
+				// grab single GRP_ID and handle
3147
+				$GRP_ID = isset($this->_req_data['id']) ? absint($this->_req_data['id']) : 0;
3148
+				if (! empty($GRP_ID)) {
3149
+					$trashed_or_restored = $trash ? $MTP->delete_by_ID($GRP_ID) : $MTP->restore_by_ID($GRP_ID);
3150
+					if (! $trashed_or_restored) {
3151
+						$success = 0;
3152
+					}
3153
+				} else {
3154
+					$success = 0;
3155
+				}
3156
+			}
3157
+		}
3158
+
3159
+		$action_desc = $trash
3160
+			? esc_html__('moved to the trash', 'event_espresso')
3161
+			: esc_html__('restored', 'event_espresso');
3162
+
3163
+		$action_desc = ! empty($this->_req_data['template_switch']) ? esc_html__('switched', 'event_espresso') : $action_desc;
3164
+
3165
+		$item_desc = $all ? _n(
3166
+			'Message Template Group',
3167
+			'Message Template Groups',
3168
+			$success,
3169
+			'event_espresso'
3170
+		) : _n('Message Template Context', 'Message Template Contexts', $success, 'event_espresso');
3171
+
3172
+		$item_desc = ! empty($this->_req_data['template_switch']) ? _n(
3173
+			'template',
3174
+			'templates',
3175
+			$success,
3176
+			'event_espresso'
3177
+		) : $item_desc;
3178
+
3179
+		$this->_redirect_after_action($success, $item_desc, $action_desc, array());
3180
+	}
3181
+
3182
+
3183
+	/**
3184
+	 * [_delete_message_template]
3185
+	 * NOTE: this handles not only the deletion of the groups but also all the templates belonging to that group.
3186
+	 *
3187
+	 * @return void
3188
+	 * @throws EE_Error
3189
+	 * @throws InvalidArgumentException
3190
+	 * @throws InvalidDataTypeException
3191
+	 * @throws InvalidInterfaceException
3192
+	 */
3193
+	protected function _delete_message_template()
3194
+	{
3195
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
3196
+
3197
+		// checkboxes
3198
+		if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
3199
+			// if array has more than one element then success message should be plural
3200
+			$success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
3201
+
3202
+			// cycle through bulk action checkboxes
3203
+			while (list($GRP_ID, $value) = each($this->_req_data['checkbox'])) {
3204
+				$success = $this->_delete_mtp_permanently($GRP_ID);
3205
+			}
3206
+		} else {
3207
+			// grab single grp_id and delete
3208
+			$GRP_ID = absint($this->_req_data['id']);
3209
+			$success = $this->_delete_mtp_permanently($GRP_ID);
3210
+		}
3211
+
3212
+		$this->_redirect_after_action($success, 'Message Templates', 'deleted', array());
3213
+	}
3214
+
3215
+
3216
+	/**
3217
+	 * helper for permanently deleting a mtP group and all related message_templates
3218
+	 *
3219
+	 * @param  int  $GRP_ID        The group being deleted
3220
+	 * @param  bool $include_group whether to delete the Message Template Group as well.
3221
+	 * @return bool boolean to indicate the success of the deletes or not.
3222
+	 * @throws EE_Error
3223
+	 * @throws InvalidArgumentException
3224
+	 * @throws InvalidDataTypeException
3225
+	 * @throws InvalidInterfaceException
3226
+	 */
3227
+	private function _delete_mtp_permanently($GRP_ID, $include_group = true)
3228
+	{
3229
+		$success = 1;
3230
+		$MTPG = EEM_Message_Template_Group::instance();
3231
+		// first let's GET this group
3232
+		$MTG = $MTPG->get_one_by_ID($GRP_ID);
3233
+		// then delete permanently all the related Message Templates
3234
+		$deleted = $MTG->delete_related_permanently('Message_Template');
3235
+
3236
+		if ($deleted === 0) {
3237
+			$success = 0;
3238
+		}
3239
+
3240
+		// now delete permanently this particular group
3241
+
3242
+		if ($include_group && ! $MTG->delete_permanently()) {
3243
+			$success = 0;
3244
+		}
3245
+
3246
+		return $success;
3247
+	}
3248
+
3249
+
3250
+	/**
3251
+	 *    _learn_more_about_message_templates_link
3252
+	 *
3253
+	 * @access protected
3254
+	 * @return string
3255
+	 */
3256
+	protected function _learn_more_about_message_templates_link()
3257
+	{
3258
+		return '<a class="hidden" style="margin:0 20px; cursor:pointer; font-size:12px;" >'
3259
+			   . esc_html__('learn more about how message templates works', 'event_espresso')
3260
+			   . '</a>';
3261
+	}
3262
+
3263
+
3264
+	/**
3265
+	 * Used for setting up messenger/message type activation.  This loads up the initial view.  The rest is handled by
3266
+	 * ajax and other routes.
3267
+	 *
3268
+	 * @return void
3269
+	 * @throws DomainException
3270
+	 */
3271
+	protected function _settings()
3272
+	{
3273
+
3274
+
3275
+		$this->_set_m_mt_settings();
3276
+
3277
+		$selected_messenger = isset($this->_req_data['selected_messenger'])
3278
+			? $this->_req_data['selected_messenger']
3279
+			: 'email';
3280
+
3281
+		// let's setup the messenger tabs
3282
+		$this->_template_args['admin_page_header'] = EEH_Tabbed_Content::tab_text_links(
3283
+			$this->_m_mt_settings['messenger_tabs'],
3284
+			'messenger_links',
3285
+			'|',
3286
+			$selected_messenger
3287
+		);
3288
+		$this->_template_args['before_admin_page_content'] = '<div class="ui-widget ui-helper-clearfix">';
3289
+		$this->_template_args['after_admin_page_content'] = '</div><!-- end .ui-widget -->';
3290
+
3291
+		$this->display_admin_page_with_sidebar();
3292
+	}
3293
+
3294
+
3295
+	/**
3296
+	 * This sets the $_m_mt_settings property for when needed (used on the Messages settings page)
3297
+	 *
3298
+	 * @access protected
3299
+	 * @return void
3300
+	 * @throws DomainException
3301
+	 */
3302
+	protected function _set_m_mt_settings()
3303
+	{
3304
+		// first if this is already set then lets get out no need to regenerate data.
3305
+		if (! empty($this->_m_mt_settings)) {
3306
+			return;
3307
+		}
3308
+
3309
+		// get all installed messengers and message_types
3310
+		/** @type EE_messenger[] $messengers */
3311
+		$messengers = $this->_message_resource_manager->installed_messengers();
3312
+		/** @type EE_message_type[] $message_types */
3313
+		$message_types = $this->_message_resource_manager->installed_message_types();
3314
+
3315
+
3316
+		// assemble the array for the _tab_text_links helper
3317
+
3318
+		foreach ($messengers as $messenger) {
3319
+			$this->_m_mt_settings['messenger_tabs'][ $messenger->name ] = array(
3320
+				'label' => ucwords($messenger->label['singular']),
3321
+				'class' => $this->_message_resource_manager->is_messenger_active($messenger->name)
3322
+					? 'messenger-active'
3323
+					: '',
3324
+				'href'  => $messenger->name,
3325
+				'title' => esc_html__('Modify this Messenger', 'event_espresso'),
3326
+				'slug'  => $messenger->name,
3327
+				'obj'   => $messenger,
3328
+			);
3329
+
3330
+
3331
+			$message_types_for_messenger = $messenger->get_valid_message_types();
3332
+
3333
+			foreach ($message_types as $message_type) {
3334
+				// first we need to verify that this message type is valid with this messenger. Cause if it isn't then
3335
+				// it shouldn't show in either the inactive OR active metabox.
3336
+				if (! in_array($message_type->name, $message_types_for_messenger, true)) {
3337
+					continue;
3338
+				}
3339
+
3340
+				$a_or_i = $this->_message_resource_manager->is_message_type_active_for_messenger(
3341
+					$messenger->name,
3342
+					$message_type->name
3343
+				)
3344
+					? 'active'
3345
+					: 'inactive';
3346
+
3347
+				$this->_m_mt_settings['message_type_tabs'][ $messenger->name ][ $a_or_i ][ $message_type->name ] = array(
3348
+					'label'    => ucwords($message_type->label['singular']),
3349
+					'class'    => 'message-type-' . $a_or_i,
3350
+					'slug_id'  => $message_type->name . '-messagetype-' . $messenger->name,
3351
+					'mt_nonce' => wp_create_nonce($message_type->name . '_nonce'),
3352
+					'href'     => 'espresso_' . $message_type->name . '_message_type_settings',
3353
+					'title'    => $a_or_i === 'active'
3354
+						? esc_html__('Drag this message type to the Inactive window to deactivate', 'event_espresso')
3355
+						: esc_html__('Drag this message type to the messenger to activate', 'event_espresso'),
3356
+					'content'  => $a_or_i === 'active'
3357
+						? $this->_message_type_settings_content($message_type, $messenger, true)
3358
+						: $this->_message_type_settings_content($message_type, $messenger),
3359
+					'slug'     => $message_type->name,
3360
+					'active'   => $a_or_i === 'active',
3361
+					'obj'      => $message_type,
3362
+				);
3363
+			}
3364
+		}
3365
+	}
3366
+
3367
+
3368
+	/**
3369
+	 * This just prepares the content for the message type settings
3370
+	 *
3371
+	 * @param  EE_message_type $message_type The message type object
3372
+	 * @param  EE_messenger    $messenger    The messenger object
3373
+	 * @param  boolean         $active       Whether the message type is active or not
3374
+	 * @return string html output for the content
3375
+	 * @throws DomainException
3376
+	 */
3377
+	protected function _message_type_settings_content($message_type, $messenger, $active = false)
3378
+	{
3379
+		// get message type fields
3380
+		$fields = $message_type->get_admin_settings_fields();
3381
+		$settings_template_args['template_form_fields'] = '';
3382
+
3383
+		if (! empty($fields) && $active) {
3384
+			$existing_settings = $message_type->get_existing_admin_settings($messenger->name);
3385
+			foreach ($fields as $fldname => $fldprops) {
3386
+				$field_id = $messenger->name . '-' . $message_type->name . '-' . $fldname;
3387
+				$template_form_field[ $field_id ] = array(
3388
+					'name'       => 'message_type_settings[' . $fldname . ']',
3389
+					'label'      => $fldprops['label'],
3390
+					'input'      => $fldprops['field_type'],
3391
+					'type'       => $fldprops['value_type'],
3392
+					'required'   => $fldprops['required'],
3393
+					'validation' => $fldprops['validation'],
3394
+					'value'      => isset($existing_settings[ $fldname ])
3395
+						? $existing_settings[ $fldname ]
3396
+						: $fldprops['default'],
3397
+					'options'    => isset($fldprops['options'])
3398
+						? $fldprops['options']
3399
+						: array(),
3400
+					'default'    => isset($existing_settings[ $fldname ])
3401
+						? $existing_settings[ $fldname ]
3402
+						: $fldprops['default'],
3403
+					'css_class'  => 'no-drag',
3404
+					'format'     => $fldprops['format'],
3405
+				);
3406
+			}
3407
+
3408
+
3409
+			$settings_template_args['template_form_fields'] = ! empty($template_form_field)
3410
+				? $this->_generate_admin_form_fields(
3411
+					$template_form_field,
3412
+					'string',
3413
+					'ee_mt_activate_form'
3414
+				)
3415
+				: '';
3416
+		}
3417
+
3418
+		$settings_template_args['description'] = $message_type->description;
3419
+		// we also need some hidden fields
3420
+		$settings_template_args['hidden_fields'] = array(
3421
+			'message_type_settings[messenger]'    => array(
3422
+				'type'  => 'hidden',
3423
+				'value' => $messenger->name,
3424
+			),
3425
+			'message_type_settings[message_type]' => array(
3426
+				'type'  => 'hidden',
3427
+				'value' => $message_type->name,
3428
+			),
3429
+			'type'                                => array(
3430
+				'type'  => 'hidden',
3431
+				'value' => 'message_type',
3432
+			),
3433
+		);
3434
+
3435
+		$settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields(
3436
+			$settings_template_args['hidden_fields'],
3437
+			'array'
3438
+		);
3439
+		$settings_template_args['show_form'] = empty($settings_template_args['template_form_fields'])
3440
+			? ' hidden'
3441
+			: '';
3442
+
3443
+
3444
+		$template = EE_MSG_TEMPLATE_PATH . 'ee_msg_mt_settings_content.template.php';
3445
+		$content = EEH_Template::display_template($template, $settings_template_args, true);
3446
+
3447
+		return $content;
3448
+	}
3449
+
3450
+
3451
+	/**
3452
+	 * Generate all the metaboxes for the message types and register them for the messages settings page.
3453
+	 *
3454
+	 * @access protected
3455
+	 * @return void
3456
+	 * @throws DomainException
3457
+	 */
3458
+	protected function _messages_settings_metaboxes()
3459
+	{
3460
+		$this->_set_m_mt_settings();
3461
+		$m_boxes = $mt_boxes = array();
3462
+		$m_template_args = $mt_template_args = array();
3463
+
3464
+		$selected_messenger = isset($this->_req_data['selected_messenger'])
3465
+			? $this->_req_data['selected_messenger']
3466
+			: 'email';
3467
+
3468
+		if (isset($this->_m_mt_settings['messenger_tabs'])) {
3469
+			foreach ($this->_m_mt_settings['messenger_tabs'] as $messenger => $tab_array) {
3470
+				$hide_on_message = $this->_message_resource_manager->is_messenger_active($messenger) ? '' : 'hidden';
3471
+				$hide_off_message = $this->_message_resource_manager->is_messenger_active($messenger) ? 'hidden' : '';
3472
+				// messenger meta boxes
3473
+				$active = $selected_messenger === $messenger;
3474
+				$active_mt_tabs = isset(
3475
+					$this->_m_mt_settings['message_type_tabs'][ $messenger ]['active']
3476
+				)
3477
+					? $this->_m_mt_settings['message_type_tabs'][ $messenger ]['active']
3478
+					: '';
3479
+				$m_boxes[ $messenger . '_a_box' ] = sprintf(
3480
+					esc_html__('%s Settings', 'event_espresso'),
3481
+					$tab_array['label']
3482
+				);
3483
+				$m_template_args[ $messenger . '_a_box' ] = array(
3484
+					'active_message_types'   => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '',
3485
+					'inactive_message_types' => isset(
3486
+						$this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive']
3487
+					)
3488
+						? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive'])
3489
+						: '',
3490
+					'content'                => $this->_get_messenger_box_content($tab_array['obj']),
3491
+					'hidden'                 => $active ? '' : ' hidden',
3492
+					'hide_on_message'        => $hide_on_message,
3493
+					'messenger'              => $messenger,
3494
+					'active'                 => $active,
3495
+				);
3496
+				// message type meta boxes
3497
+				// (which is really just the inactive container for each messenger
3498
+				// showing inactive message types for that messenger)
3499
+				$mt_boxes[ $messenger . '_i_box' ] = esc_html__('Inactive Message Types', 'event_espresso');
3500
+				$mt_template_args[ $messenger . '_i_box' ] = array(
3501
+					'active_message_types'   => ! empty($active_mt_tabs) ? $this->_get_mt_tabs($active_mt_tabs) : '',
3502
+					'inactive_message_types' => isset(
3503
+						$this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive']
3504
+					)
3505
+						? $this->_get_mt_tabs($this->_m_mt_settings['message_type_tabs'][ $messenger ]['inactive'])
3506
+						: '',
3507
+					'hidden'                 => $active ? '' : ' hidden',
3508
+					'hide_on_message'        => $hide_on_message,
3509
+					'hide_off_message'       => $hide_off_message,
3510
+					'messenger'              => $messenger,
3511
+					'active'                 => $active,
3512
+				);
3513
+			}
3514
+		}
3515
+
3516
+
3517
+		// register messenger metaboxes
3518
+		$m_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_mt_meta_box.template.php';
3519
+		foreach ($m_boxes as $box => $label) {
3520
+			$callback_args = array('template_path' => $m_template_path, 'template_args' => $m_template_args[ $box ]);
3521
+			$msgr = str_replace('_a_box', '', $box);
3522
+			add_meta_box(
3523
+				'espresso_' . $msgr . '_settings',
3524
+				$label,
3525
+				function ($post, $metabox) {
3526
+					echo EEH_Template::display_template(
3527
+						$metabox["args"]["template_path"],
3528
+						$metabox["args"]["template_args"],
3529
+						true
3530
+					);
3531
+				},
3532
+				$this->_current_screen->id,
3533
+				'normal',
3534
+				'high',
3535
+				$callback_args
3536
+			);
3537
+		}
3538
+
3539
+		// register message type metaboxes
3540
+		$mt_template_path = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_messenger_meta_box.template.php';
3541
+		foreach ($mt_boxes as $box => $label) {
3542
+			$callback_args = array(
3543
+				'template_path' => $mt_template_path,
3544
+				'template_args' => $mt_template_args[ $box ],
3545
+			);
3546
+			$mt = str_replace('_i_box', '', $box);
3547
+			add_meta_box(
3548
+				'espresso_' . $mt . '_inactive_mts',
3549
+				$label,
3550
+				function ($post, $metabox) {
3551
+					echo EEH_Template::display_template(
3552
+						$metabox["args"]["template_path"],
3553
+						$metabox["args"]["template_args"],
3554
+						true
3555
+					);
3556
+				},
3557
+				$this->_current_screen->id,
3558
+				'side',
3559
+				'high',
3560
+				$callback_args
3561
+			);
3562
+		}
3563
+
3564
+		// register metabox for global messages settings but only when on the main site.  On single site installs this
3565
+		// will always result in the metabox showing, on multisite installs the metabox will only show on the main site.
3566
+		if (is_main_site()) {
3567
+			add_meta_box(
3568
+				'espresso_global_message_settings',
3569
+				esc_html__('Global Message Settings', 'event_espresso'),
3570
+				array($this, 'global_messages_settings_metabox_content'),
3571
+				$this->_current_screen->id,
3572
+				'normal',
3573
+				'low',
3574
+				array()
3575
+			);
3576
+		}
3577
+	}
3578
+
3579
+
3580
+	/**
3581
+	 *  This generates the content for the global messages settings metabox.
3582
+	 *
3583
+	 * @return string
3584
+	 * @throws EE_Error
3585
+	 * @throws InvalidArgumentException
3586
+	 * @throws ReflectionException
3587
+	 * @throws InvalidDataTypeException
3588
+	 * @throws InvalidInterfaceException
3589
+	 */
3590
+	public function global_messages_settings_metabox_content()
3591
+	{
3592
+		$form = $this->_generate_global_settings_form();
3593
+		echo $form->form_open(
3594
+			$this->add_query_args_and_nonce(array('action' => 'update_global_settings'), EE_MSG_ADMIN_URL),
3595
+			'POST'
3596
+		)
3597
+			 . $form->get_html()
3598
+			 . $form->form_close();
3599
+	}
3600
+
3601
+
3602
+	/**
3603
+	 * This generates and returns the form object for the global messages settings.
3604
+	 *
3605
+	 * @return EE_Form_Section_Proper
3606
+	 * @throws EE_Error
3607
+	 * @throws InvalidArgumentException
3608
+	 * @throws ReflectionException
3609
+	 * @throws InvalidDataTypeException
3610
+	 * @throws InvalidInterfaceException
3611
+	 */
3612
+	protected function _generate_global_settings_form()
3613
+	{
3614
+		EE_Registry::instance()->load_helper('HTML');
3615
+		/** @var EE_Network_Core_Config $network_config */
3616
+		$network_config = EE_Registry::instance()->NET_CFG->core;
3617
+
3618
+		return new EE_Form_Section_Proper(
3619
+			array(
3620
+				'name'            => 'global_messages_settings',
3621
+				'html_id'         => 'global_messages_settings',
3622
+				'html_class'      => 'form-table',
3623
+				'layout_strategy' => new EE_Admin_Two_Column_Layout(),
3624
+				'subsections'     => apply_filters(
3625
+					'FHEE__Messages_Admin_Page__global_messages_settings_metabox_content__form_subsections',
3626
+					array(
3627
+						'do_messages_on_same_request' => new EE_Select_Input(
3628
+							array(
3629
+								true  => esc_html__("On the same request", "event_espresso"),
3630
+								false => esc_html__("On a separate request", "event_espresso"),
3631
+							),
3632
+							array(
3633
+								'default'         => $network_config->do_messages_on_same_request,
3634
+								'html_label_text' => esc_html__(
3635
+									'Generate and send all messages:',
3636
+									'event_espresso'
3637
+								),
3638
+								'html_help_text'  => esc_html__(
3639
+									'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.',
3640
+									'event_espresso'
3641
+								),
3642
+							)
3643
+						),
3644
+						'delete_threshold'            => new EE_Select_Input(
3645
+							array(
3646
+								0  => esc_html__('Forever', 'event_espresso'),
3647
+								3  => esc_html__('3 Months', 'event_espresso'),
3648
+								6  => esc_html__('6 Months', 'event_espresso'),
3649
+								9  => esc_html__('9 Months', 'event_espresso'),
3650
+								12 => esc_html__('12 Months', 'event_espresso'),
3651
+								24 => esc_html__('24 Months', 'event_espresso'),
3652
+								36 => esc_html__('36 Months', 'event_espresso'),
3653
+							),
3654
+							array(
3655
+								'default'         => EE_Registry::instance()->CFG->messages->delete_threshold,
3656
+								'html_label_text' => esc_html__('Cleanup of old messages:', 'event_espresso'),
3657
+								'html_help_text'  => esc_html__(
3658
+									'You can control how long a record of processed messages is kept via this option.',
3659
+									'event_espresso'
3660
+								),
3661
+							)
3662
+						),
3663
+						'update_settings'             => new EE_Submit_Input(
3664
+							array(
3665
+								'default'         => esc_html__('Update', 'event_espresso'),
3666
+								'html_label_text' => '&nbsp',
3667
+							)
3668
+						),
3669
+					)
3670
+				),
3671
+			)
3672
+		);
3673
+	}
3674
+
3675
+
3676
+	/**
3677
+	 * This handles updating the global settings set on the admin page.
3678
+	 *
3679
+	 * @throws EE_Error
3680
+	 * @throws InvalidDataTypeException
3681
+	 * @throws InvalidInterfaceException
3682
+	 * @throws InvalidArgumentException
3683
+	 * @throws ReflectionException
3684
+	 */
3685
+	protected function _update_global_settings()
3686
+	{
3687
+		/** @var EE_Network_Core_Config $network_config */
3688
+		$network_config = EE_Registry::instance()->NET_CFG->core;
3689
+		$messages_config = EE_Registry::instance()->CFG->messages;
3690
+		$form = $this->_generate_global_settings_form();
3691
+		if ($form->was_submitted()) {
3692
+			$form->receive_form_submission();
3693
+			if ($form->is_valid()) {
3694
+				$valid_data = $form->valid_data();
3695
+				foreach ($valid_data as $property => $value) {
3696
+					$setter = 'set_' . $property;
3697
+					if (method_exists($network_config, $setter)) {
3698
+						$network_config->{$setter}($value);
3699
+					} elseif (
3700
+						property_exists($network_config, $property)
3701
+						&& $network_config->{$property} !== $value
3702
+					) {
3703
+						$network_config->{$property} = $value;
3704
+					} elseif (
3705
+						property_exists($messages_config, $property)
3706
+						&& $messages_config->{$property} !== $value
3707
+					) {
3708
+						$messages_config->{$property} = $value;
3709
+					}
3710
+				}
3711
+				// only update if the form submission was valid!
3712
+				EE_Registry::instance()->NET_CFG->update_config(true, false);
3713
+				EE_Registry::instance()->CFG->update_espresso_config();
3714
+				EE_Error::overwrite_success();
3715
+				EE_Error::add_success(__('Global message settings were updated', 'event_espresso'));
3716
+			}
3717
+		}
3718
+		$this->_redirect_after_action(0, '', '', array('action' => 'settings'), true);
3719
+	}
3720
+
3721
+
3722
+	/**
3723
+	 * this prepares the messenger tabs that can be dragged in and out of messenger boxes to activate/deactivate
3724
+	 *
3725
+	 * @param  array $tab_array This is an array of message type tab details used to generate the tabs
3726
+	 * @return string html formatted tabs
3727
+	 * @throws DomainException
3728
+	 */
3729
+	protected function _get_mt_tabs($tab_array)
3730
+	{
3731
+		$tab_array = (array) $tab_array;
3732
+		$template = EE_MSG_TEMPLATE_PATH . 'ee_msg_details_mt_settings_tab_item.template.php';
3733
+		$tabs = '';
3734
+
3735
+		foreach ($tab_array as $tab) {
3736
+			$tabs .= EEH_Template::display_template($template, $tab, true);
3737
+		}
3738
+
3739
+		return $tabs;
3740
+	}
3741
+
3742
+
3743
+	/**
3744
+	 * This prepares the content of the messenger meta box admin settings
3745
+	 *
3746
+	 * @param  EE_messenger $messenger The messenger we're setting up content for
3747
+	 * @return string html formatted content
3748
+	 * @throws DomainException
3749
+	 */
3750
+	protected function _get_messenger_box_content(EE_messenger $messenger)
3751
+	{
3752
+
3753
+		$fields = $messenger->get_admin_settings_fields();
3754
+		$settings_template_args['template_form_fields'] = '';
3755
+
3756
+		// is $messenger active?
3757
+		$settings_template_args['active'] = $this->_message_resource_manager->is_messenger_active($messenger->name);
3758
+
3759
+
3760
+		if (! empty($fields)) {
3761
+			$existing_settings = $messenger->get_existing_admin_settings();
3762
+
3763
+			foreach ($fields as $fldname => $fldprops) {
3764
+				$field_id = $messenger->name . '-' . $fldname;
3765
+				$template_form_field[ $field_id ] = array(
3766
+					'name'       => 'messenger_settings[' . $field_id . ']',
3767
+					'label'      => $fldprops['label'],
3768
+					'input'      => $fldprops['field_type'],
3769
+					'type'       => $fldprops['value_type'],
3770
+					'required'   => $fldprops['required'],
3771
+					'validation' => $fldprops['validation'],
3772
+					'value'      => isset($existing_settings[ $field_id ])
3773
+						? $existing_settings[ $field_id ]
3774
+						: $fldprops['default'],
3775
+					'css_class'  => '',
3776
+					'format'     => $fldprops['format'],
3777
+				);
3778
+			}
3779
+
3780
+
3781
+			$settings_template_args['template_form_fields'] = ! empty($template_form_field)
3782
+				? $this->_generate_admin_form_fields($template_form_field, 'string', 'ee_m_activate_form')
3783
+				: '';
3784
+		}
3785
+
3786
+		// we also need some hidden fields
3787
+		$settings_template_args['hidden_fields'] = array(
3788
+			'messenger_settings[messenger]' => array(
3789
+				'type'  => 'hidden',
3790
+				'value' => $messenger->name,
3791
+			),
3792
+			'type'                          => array(
3793
+				'type'  => 'hidden',
3794
+				'value' => 'messenger',
3795
+			),
3796
+		);
3797
+
3798
+		// make sure any active message types that are existing are included in the hidden fields
3799
+		if (isset($this->_m_mt_settings['message_type_tabs'][ $messenger->name ]['active'])) {
3800
+			foreach ($this->_m_mt_settings['message_type_tabs'][ $messenger->name ]['active'] as $mt => $values) {
3801
+				$settings_template_args['hidden_fields'][ 'messenger_settings[message_types][' . $mt . ']' ] = array(
3802
+					'type'  => 'hidden',
3803
+					'value' => $mt,
3804
+				);
3805
+			}
3806
+		}
3807
+		$settings_template_args['hidden_fields'] = $this->_generate_admin_form_fields(
3808
+			$settings_template_args['hidden_fields'],
3809
+			'array'
3810
+		);
3811
+		$active = $this->_message_resource_manager->is_messenger_active($messenger->name);
3812
+
3813
+		$settings_template_args['messenger'] = $messenger->name;
3814
+		$settings_template_args['description'] = $messenger->description;
3815
+		$settings_template_args['show_hide_edit_form'] = $active ? '' : ' hidden';
3816
+
3817
+
3818
+		$settings_template_args['show_hide_edit_form'] = $this->_message_resource_manager->is_messenger_active(
3819
+			$messenger->name
3820
+		)
3821
+			? $settings_template_args['show_hide_edit_form']
3822
+			: ' hidden';
3823
+
3824
+		$settings_template_args['show_hide_edit_form'] = empty($settings_template_args['template_form_fields'])
3825
+			? ' hidden'
3826
+			: $settings_template_args['show_hide_edit_form'];
3827
+
3828
+
3829
+		$settings_template_args['on_off_action'] = $active ? 'messenger-off' : 'messenger-on';
3830
+		$settings_template_args['nonce'] = wp_create_nonce('activate_' . $messenger->name . '_toggle_nonce');
3831
+		$settings_template_args['on_off_status'] = $active ? true : false;
3832
+		$template = EE_MSG_TEMPLATE_PATH . 'ee_msg_m_settings_content.template.php';
3833
+		$content = EEH_Template::display_template(
3834
+			$template,
3835
+			$settings_template_args,
3836
+			true
3837
+		);
3838
+
3839
+		return $content;
3840
+	}
3841
+
3842
+
3843
+	/**
3844
+	 * used by ajax on the messages settings page to activate|deactivate the messenger
3845
+	 *
3846
+	 * @throws DomainException
3847
+	 * @throws EE_Error
3848
+	 * @throws InvalidDataTypeException
3849
+	 * @throws InvalidInterfaceException
3850
+	 * @throws InvalidArgumentException
3851
+	 * @throws ReflectionException
3852
+	 */
3853
+	public function activate_messenger_toggle()
3854
+	{
3855
+		$success = true;
3856
+		$this->_prep_default_response_for_messenger_or_message_type_toggle();
3857
+		// let's check that we have required data
3858
+		if (! isset($this->_req_data['messenger'])) {
3859
+			EE_Error::add_error(
3860
+				esc_html__('Messenger name needed to toggle activation. None given', 'event_espresso'),
3861
+				__FILE__,
3862
+				__FUNCTION__,
3863
+				__LINE__
3864
+			);
3865
+			$success = false;
3866
+		}
3867
+
3868
+		// do a nonce check here since we're not arriving via a normal route
3869
+		$nonce = isset($this->_req_data['activate_nonce'])
3870
+			? sanitize_text_field($this->_req_data['activate_nonce'])
3871
+			: '';
3872
+		$nonce_ref = 'activate_' . $this->_req_data['messenger'] . '_toggle_nonce';
3873
+
3874
+		$this->_verify_nonce($nonce, $nonce_ref);
3875
+
3876
+
3877
+		if (! isset($this->_req_data['status'])) {
3878
+			EE_Error::add_error(
3879
+				esc_html__(
3880
+					'Messenger status needed to know whether activation or deactivation is happening. No status is given',
3881
+					'event_espresso'
3882
+				),
3883
+				__FILE__,
3884
+				__FUNCTION__,
3885
+				__LINE__
3886
+			);
3887
+			$success = false;
3888
+		}
3889
+
3890
+		// do check to verify we have a valid status.
3891
+		$status = $this->_req_data['status'];
3892
+
3893
+		if ($status !== 'off' && $status !== 'on') {
3894
+			EE_Error::add_error(
3895
+				sprintf(
3896
+					esc_html__('The given status (%s) is not valid. Must be "off" or "on"', 'event_espresso'),
3897
+					$this->_req_data['status']
3898
+				),
3899
+				__FILE__,
3900
+				__FUNCTION__,
3901
+				__LINE__
3902
+			);
3903
+			$success = false;
3904
+		}
3905
+
3906
+		if ($success) {
3907
+			// made it here?  Stop dawdling then!!
3908
+			$success = $status === 'off'
3909
+				? $this->_deactivate_messenger($this->_req_data['messenger'])
3910
+				: $this->_activate_messenger($this->_req_data['messenger']);
3911
+		}
3912
+
3913
+		$this->_template_args['success'] = $success;
3914
+
3915
+		// no special instructions so let's just do the json return (which should automatically do all the special stuff).
3916
+		$this->_return_json();
3917
+	}
3918
+
3919
+
3920
+	/**
3921
+	 * used by ajax from the messages settings page to activate|deactivate a message type
3922
+	 *
3923
+	 * @throws DomainException
3924
+	 * @throws EE_Error
3925
+	 * @throws ReflectionException
3926
+	 * @throws InvalidDataTypeException
3927
+	 * @throws InvalidInterfaceException
3928
+	 * @throws InvalidArgumentException
3929
+	 */
3930
+	public function activate_mt_toggle()
3931
+	{
3932
+		$success = true;
3933
+		$this->_prep_default_response_for_messenger_or_message_type_toggle();
3934
+
3935
+		// let's make sure we have the necessary data
3936
+		if (! isset($this->_req_data['message_type'])) {
3937
+			EE_Error::add_error(
3938
+				esc_html__('Message Type name needed to toggle activation. None given', 'event_espresso'),
3939
+				__FILE__,
3940
+				__FUNCTION__,
3941
+				__LINE__
3942
+			);
3943
+			$success = false;
3944
+		}
3945
+
3946
+		if (! isset($this->_req_data['messenger'])) {
3947
+			EE_Error::add_error(
3948
+				esc_html__('Messenger name needed to toggle activation. None given', 'event_espresso'),
3949
+				__FILE__,
3950
+				__FUNCTION__,
3951
+				__LINE__
3952
+			);
3953
+			$success = false;
3954
+		}
3955
+
3956
+		if (! isset($this->_req_data['status'])) {
3957
+			EE_Error::add_error(
3958
+				esc_html__(
3959
+					'Messenger status needed to know whether activation or deactivation is happening. No status is given',
3960
+					'event_espresso'
3961
+				),
3962
+				__FILE__,
3963
+				__FUNCTION__,
3964
+				__LINE__
3965
+			);
3966
+			$success = false;
3967
+		}
3968
+
3969
+
3970
+		// do check to verify we have a valid status.
3971
+		$status = $this->_req_data['status'];
3972
+
3973
+		if ($status !== 'activate' && $status !== 'deactivate') {
3974
+			EE_Error::add_error(
3975
+				sprintf(
3976
+					esc_html__('The given status (%s) is not valid. Must be "active" or "inactive"', 'event_espresso'),
3977
+					$this->_req_data['status']
3978
+				),
3979
+				__FILE__,
3980
+				__FUNCTION__,
3981
+				__LINE__
3982
+			);
3983
+			$success = false;
3984
+		}
3985
+
3986
+
3987
+		// do a nonce check here since we're not arriving via a normal route
3988
+		$nonce = isset($this->_req_data['mt_nonce']) ? sanitize_text_field($this->_req_data['mt_nonce']) : '';
3989
+		$nonce_ref = $this->_req_data['message_type'] . '_nonce';
3990
+
3991
+		$this->_verify_nonce($nonce, $nonce_ref);
3992
+
3993
+		if ($success) {
3994
+			// made it here? um, what are you waiting for then?
3995
+			$success = $status === 'deactivate'
3996
+				? $this->_deactivate_message_type_for_messenger(
3997
+					$this->_req_data['messenger'],
3998
+					$this->_req_data['message_type']
3999
+				)
4000
+				: $this->_activate_message_type_for_messenger(
4001
+					$this->_req_data['messenger'],
4002
+					$this->_req_data['message_type']
4003
+				);
4004
+		}
4005
+
4006
+		$this->_template_args['success'] = $success;
4007
+		$this->_return_json();
4008
+	}
4009
+
4010
+
4011
+	/**
4012
+	 * Takes care of processing activating a messenger and preparing the appropriate response.
4013
+	 *
4014
+	 * @param string $messenger_name The name of the messenger being activated
4015
+	 * @return bool
4016
+	 * @throws DomainException
4017
+	 * @throws EE_Error
4018
+	 * @throws InvalidArgumentException
4019
+	 * @throws ReflectionException
4020
+	 * @throws InvalidDataTypeException
4021
+	 * @throws InvalidInterfaceException
4022
+	 */
4023
+	protected function _activate_messenger($messenger_name)
4024
+	{
4025
+		/** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4026
+		$active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4027
+		$message_types_to_activate = $active_messenger instanceof EE_Messenger
4028
+			? $active_messenger->get_default_message_types()
4029
+			: array();
4030
+
4031
+		// ensure is active
4032
+		$this->_message_resource_manager->activate_messenger($messenger_name, $message_types_to_activate);
4033
+
4034
+		// set response_data for reload
4035
+		foreach ($message_types_to_activate as $message_type_name) {
4036
+			/** @var EE_message_type $message_type */
4037
+			$message_type = $this->_message_resource_manager->get_message_type($message_type_name);
4038
+			if (
4039
+				$this->_message_resource_manager->is_message_type_active_for_messenger(
4040
+					$messenger_name,
4041
+					$message_type_name
4042
+				)
4043
+				&& $message_type instanceof EE_message_type
4044
+			) {
4045
+				$this->_template_args['data']['active_mts'][] = $message_type_name;
4046
+				if ($message_type->get_admin_settings_fields()) {
4047
+					$this->_template_args['data']['mt_reload'][] = $message_type_name;
4048
+				}
4049
+			}
4050
+		}
4051
+
4052
+		// add success message for activating messenger
4053
+		return $this->_setup_response_message_for_activating_messenger_with_message_types($active_messenger);
4054
+	}
4055
+
4056
+
4057
+	/**
4058
+	 * Takes care of processing deactivating a messenger and preparing the appropriate response.
4059
+	 *
4060
+	 * @param string $messenger_name The name of the messenger being activated
4061
+	 * @return bool
4062
+	 * @throws DomainException
4063
+	 * @throws EE_Error
4064
+	 * @throws InvalidArgumentException
4065
+	 * @throws ReflectionException
4066
+	 * @throws InvalidDataTypeException
4067
+	 * @throws InvalidInterfaceException
4068
+	 */
4069
+	protected function _deactivate_messenger($messenger_name)
4070
+	{
4071
+		/** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4072
+		$active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4073
+		$this->_message_resource_manager->deactivate_messenger($messenger_name);
4074
+
4075
+		return $this->_setup_response_message_for_deactivating_messenger_with_message_types($active_messenger);
4076
+	}
4077
+
4078
+
4079
+	/**
4080
+	 * Takes care of processing activating a message type for a messenger and preparing the appropriate response.
4081
+	 *
4082
+	 * @param string $messenger_name    The name of the messenger the message type is being activated for.
4083
+	 * @param string $message_type_name The name of the message type being activated for the messenger
4084
+	 * @return bool
4085
+	 * @throws DomainException
4086
+	 * @throws EE_Error
4087
+	 * @throws InvalidArgumentException
4088
+	 * @throws ReflectionException
4089
+	 * @throws InvalidDataTypeException
4090
+	 * @throws InvalidInterfaceException
4091
+	 */
4092
+	protected function _activate_message_type_for_messenger($messenger_name, $message_type_name)
4093
+	{
4094
+		/** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4095
+		$active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4096
+		/** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */
4097
+		$message_type_to_activate = $this->_message_resource_manager->get_message_type($message_type_name);
4098
+
4099
+		// ensure is active
4100
+		$this->_message_resource_manager->activate_messenger($messenger_name, $message_type_name);
4101
+
4102
+		// set response for load
4103
+		if (
4104
+			$this->_message_resource_manager->is_message_type_active_for_messenger(
4105
+				$messenger_name,
4106
+				$message_type_name
4107
+			)
4108
+		) {
4109
+			$this->_template_args['data']['active_mts'][] = $message_type_name;
4110
+			if ($message_type_to_activate->get_admin_settings_fields()) {
4111
+				$this->_template_args['data']['mt_reload'][] = $message_type_name;
4112
+			}
4113
+		}
4114
+
4115
+		return $this->_setup_response_message_for_activating_messenger_with_message_types(
4116
+			$active_messenger,
4117
+			$message_type_to_activate
4118
+		);
4119
+	}
4120
+
4121
+
4122
+	/**
4123
+	 * Takes care of processing deactivating a message type for a messenger and preparing the appropriate response.
4124
+	 *
4125
+	 * @param string $messenger_name    The name of the messenger the message type is being deactivated for.
4126
+	 * @param string $message_type_name The name of the message type being deactivated for the messenger
4127
+	 * @return bool
4128
+	 * @throws DomainException
4129
+	 * @throws EE_Error
4130
+	 * @throws InvalidArgumentException
4131
+	 * @throws ReflectionException
4132
+	 * @throws InvalidDataTypeException
4133
+	 * @throws InvalidInterfaceException
4134
+	 */
4135
+	protected function _deactivate_message_type_for_messenger($messenger_name, $message_type_name)
4136
+	{
4137
+		/** @var EE_messenger $active_messenger This will be present because it can't be toggled if it isn't */
4138
+		$active_messenger = $this->_message_resource_manager->get_messenger($messenger_name);
4139
+		/** @var EE_message_type $message_type_to_activate This will be present because it can't be toggled if it isn't */
4140
+		$message_type_to_deactivate = $this->_message_resource_manager->get_message_type($message_type_name);
4141
+		$this->_message_resource_manager->deactivate_message_type_for_messenger($message_type_name, $messenger_name);
4142
+
4143
+		return $this->_setup_response_message_for_deactivating_messenger_with_message_types(
4144
+			$active_messenger,
4145
+			$message_type_to_deactivate
4146
+		);
4147
+	}
4148
+
4149
+
4150
+	/**
4151
+	 * This just initializes the defaults for activating messenger and message type responses.
4152
+	 */
4153
+	protected function _prep_default_response_for_messenger_or_message_type_toggle()
4154
+	{
4155
+		$this->_template_args['data']['active_mts'] = array();
4156
+		$this->_template_args['data']['mt_reload'] = array();
4157
+	}
4158
+
4159
+
4160
+	/**
4161
+	 * Setup appropriate response for activating a messenger and/or message types
4162
+	 *
4163
+	 * @param EE_messenger         $messenger
4164
+	 * @param EE_message_type|null $message_type
4165
+	 * @return bool
4166
+	 * @throws DomainException
4167
+	 * @throws EE_Error
4168
+	 * @throws InvalidArgumentException
4169
+	 * @throws ReflectionException
4170
+	 * @throws InvalidDataTypeException
4171
+	 * @throws InvalidInterfaceException
4172
+	 */
4173
+	protected function _setup_response_message_for_activating_messenger_with_message_types(
4174
+		$messenger,
4175
+		EE_Message_Type $message_type = null
4176
+	) {
4177
+		// if $messenger isn't a valid messenger object then get out.
4178
+		if (! $messenger instanceof EE_Messenger) {
4179
+			EE_Error::add_error(
4180
+				esc_html__('The messenger being activated is not a valid messenger', 'event_espresso'),
4181
+				__FILE__,
4182
+				__FUNCTION__,
4183
+				__LINE__
4184
+			);
4185
+
4186
+			return false;
4187
+		}
4188
+		// activated
4189
+		if ($this->_template_args['data']['active_mts']) {
4190
+			EE_Error::overwrite_success();
4191
+			// activated a message type with the messenger
4192
+			if ($message_type instanceof EE_message_type) {
4193
+				EE_Error::add_success(
4194
+					sprintf(
4195
+						esc_html__(
4196
+							'%s message type has been successfully activated with the %s messenger',
4197
+							'event_espresso'
4198
+						),
4199
+						ucwords($message_type->label['singular']),
4200
+						ucwords($messenger->label['singular'])
4201
+					)
4202
+				);
4203
+
4204
+				// if message type was invoice then let's make sure we activate the invoice payment method.
4205
+				if ($message_type->name === 'invoice') {
4206
+					EE_Registry::instance()->load_lib('Payment_Method_Manager');
4207
+					$pm = EE_Payment_Method_Manager::instance()->activate_a_payment_method_of_type('Invoice');
4208
+					if ($pm instanceof EE_Payment_Method) {
4209
+						EE_Error::add_attention(
4210
+							esc_html__(
4211
+								'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.',
4212
+								'event_espresso'
4213
+							)
4214
+						);
4215
+					}
4216
+				}
4217
+				// just toggles the entire messenger
4218
+			} else {
4219
+				EE_Error::add_success(
4220
+					sprintf(
4221
+						esc_html__('%s messenger has been successfully activated', 'event_espresso'),
4222
+						ucwords($messenger->label['singular'])
4223
+					)
4224
+				);
4225
+			}
4226
+
4227
+			return true;
4228
+
4229
+			// possible error condition. This will happen when our active_mts data is empty because it is validated for actual active
4230
+			// message types after the activation process.  However its possible some messengers don't HAVE any default_message_types
4231
+			// in which case we just give a success message for the messenger being successfully activated.
4232
+		} else {
4233
+			if (! $messenger->get_default_message_types()) {
4234
+				// messenger doesn't have any default message types so still a success.
4235
+				EE_Error::add_success(
4236
+					sprintf(
4237
+						esc_html__('%s messenger was successfully activated.', 'event_espresso'),
4238
+						ucwords($messenger->label['singular'])
4239
+					)
4240
+				);
4241
+
4242
+				return true;
4243
+			} else {
4244
+				EE_Error::add_error(
4245
+					$message_type instanceof EE_message_type
4246
+						? sprintf(
4247
+							esc_html__(
4248
+								'%s message type was not successfully activated with the %s messenger',
4249
+								'event_espresso'
4250
+							),
4251
+							ucwords($message_type->label['singular']),
4252
+							ucwords($messenger->label['singular'])
4253
+						)
4254
+						: sprintf(
4255
+							esc_html__('%s messenger was not successfully activated', 'event_espresso'),
4256
+							ucwords($messenger->label['singular'])
4257
+						),
4258
+					__FILE__,
4259
+					__FUNCTION__,
4260
+					__LINE__
4261
+				);
4262
+
4263
+				return false;
4264
+			}
4265
+		}
4266
+	}
4267
+
4268
+
4269
+	/**
4270
+	 * This sets up the appropriate response for deactivating a messenger and/or message type.
4271
+	 *
4272
+	 * @param EE_messenger         $messenger
4273
+	 * @param EE_message_type|null $message_type
4274
+	 * @return bool
4275
+	 * @throws DomainException
4276
+	 * @throws EE_Error
4277
+	 * @throws InvalidArgumentException
4278
+	 * @throws ReflectionException
4279
+	 * @throws InvalidDataTypeException
4280
+	 * @throws InvalidInterfaceException
4281
+	 */
4282
+	protected function _setup_response_message_for_deactivating_messenger_with_message_types(
4283
+		$messenger,
4284
+		EE_message_type $message_type = null
4285
+	) {
4286
+		EE_Error::overwrite_success();
4287
+
4288
+		// if $messenger isn't a valid messenger object then get out.
4289
+		if (! $messenger instanceof EE_Messenger) {
4290
+			EE_Error::add_error(
4291
+				esc_html__('The messenger being deactivated is not a valid messenger', 'event_espresso'),
4292
+				__FILE__,
4293
+				__FUNCTION__,
4294
+				__LINE__
4295
+			);
4296
+
4297
+			return false;
4298
+		}
4299
+
4300
+		if ($message_type instanceof EE_message_type) {
4301
+			$message_type_name = $message_type->name;
4302
+			EE_Error::add_success(
4303
+				sprintf(
4304
+					esc_html__(
4305
+						'%s message type has been successfully deactivated for the %s messenger.',
4306
+						'event_espresso'
4307
+					),
4308
+					ucwords($message_type->label['singular']),
4309
+					ucwords($messenger->label['singular'])
4310
+				)
4311
+			);
4312
+		} else {
4313
+			$message_type_name = '';
4314
+			EE_Error::add_success(
4315
+				sprintf(
4316
+					esc_html__('%s messenger has been successfully deactivated.', 'event_espresso'),
4317
+					ucwords($messenger->label['singular'])
4318
+				)
4319
+			);
4320
+		}
4321
+
4322
+		// if messenger was html or message type was invoice then let's make sure we deactivate invoice payment method.
4323
+		if ($messenger->name === 'html' || $message_type_name === 'invoice') {
4324
+			EE_Registry::instance()->load_lib('Payment_Method_Manager');
4325
+			$count_updated = EE_Payment_Method_Manager::instance()->deactivate_payment_method('invoice');
4326
+			if ($count_updated > 0) {
4327
+				$msg = $message_type_name === 'invoice'
4328
+					? esc_html__(
4329
+						'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.',
4330
+						'event_espresso'
4331
+					)
4332
+					: esc_html__(
4333
+						'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.',
4334
+						'event_espresso'
4335
+					);
4336
+				EE_Error::add_attention($msg);
4337
+			}
4338
+		}
4339
+
4340
+		return true;
4341
+	}
4342
+
4343
+
4344
+	/**
4345
+	 * handles updating a message type form on messenger activation IF the message type has settings fields. (via ajax)
4346
+	 *
4347
+	 * @throws DomainException
4348
+	 */
4349
+	public function update_mt_form()
4350
+	{
4351
+		if (! isset($this->_req_data['messenger']) || ! isset($this->_req_data['message_type'])) {
4352
+			EE_Error::add_error(
4353
+				esc_html__('Require message type or messenger to send an updated form', 'event_espresso'),
4354
+				__FILE__,
4355
+				__FUNCTION__,
4356
+				__LINE__
4357
+			);
4358
+			$this->_return_json();
4359
+		}
4360
+
4361
+		$message_types = $this->get_installed_message_types();
4362
+
4363
+		$message_type = $message_types[ $this->_req_data['message_type'] ];
4364
+		$messenger = $this->_message_resource_manager->get_active_messenger($this->_req_data['messenger']);
4365
+
4366
+		$content = $this->_message_type_settings_content(
4367
+			$message_type,
4368
+			$messenger,
4369
+			true
4370
+		);
4371
+		$this->_template_args['success'] = true;
4372
+		$this->_template_args['content'] = $content;
4373
+		$this->_return_json();
4374
+	}
4375
+
4376
+
4377
+	/**
4378
+	 * this handles saving the settings for a messenger or message type
4379
+	 *
4380
+	 */
4381
+	public function save_settings()
4382
+	{
4383
+		if (! isset($this->_req_data['type'])) {
4384
+			EE_Error::add_error(
4385
+				esc_html__(
4386
+					'Cannot save settings because type is unknown (messenger settings or messsage type settings?)',
4387
+					'event_espresso'
4388
+				),
4389
+				__FILE__,
4390
+				__FUNCTION__,
4391
+				__LINE__
4392
+			);
4393
+			$this->_template_args['error'] = true;
4394
+			$this->_return_json();
4395
+		}
4396
+
4397
+
4398
+		if ($this->_req_data['type'] === 'messenger') {
4399
+			// this should be an array.
4400
+			$settings = $this->_req_data['messenger_settings'];
4401
+			$messenger = $settings['messenger'];
4402
+			// let's setup the settings data
4403
+			foreach ($settings as $key => $value) {
4404
+				switch ($key) {
4405
+					case 'messenger':
4406
+						unset($settings['messenger']);
4407
+						break;
4408
+					case 'message_types':
4409
+						unset($settings['message_types']);
4410
+						break;
4411
+					default:
4412
+						$settings[ $key ] = $value;
4413
+						break;
4414
+				}
4415
+			}
4416
+			$this->_message_resource_manager->add_settings_for_messenger($messenger, $settings);
4417
+		} elseif ($this->_req_data['type'] === 'message_type') {
4418
+			$settings = $this->_req_data['message_type_settings'];
4419
+			$messenger = $settings['messenger'];
4420
+			$message_type = $settings['message_type'];
4421
+
4422
+			foreach ($settings as $key => $value) {
4423
+				switch ($key) {
4424
+					case 'messenger':
4425
+						unset($settings['messenger']);
4426
+						break;
4427
+					case 'message_type':
4428
+						unset($settings['message_type']);
4429
+						break;
4430
+					default:
4431
+						$settings[ $key ] = $value;
4432
+						break;
4433
+				}
4434
+			}
4435
+
4436
+			$this->_message_resource_manager->add_settings_for_message_type($messenger, $message_type, $settings);
4437
+		}
4438
+
4439
+		// okay we should have the data all setup.  Now we just update!
4440
+		$success = $this->_message_resource_manager->update_active_messengers_option();
4441
+
4442
+		if ($success) {
4443
+			EE_Error::add_success(__('Settings updated', 'event_espresso'));
4444
+		} else {
4445
+			EE_Error::add_error(
4446
+				esc_html__(
4447
+					'Settings did not get updated',
4448
+					'event_espresso'
4449
+				),
4450
+				__FILE__,
4451
+				__FUNCTION__,
4452
+				__LINE__
4453
+			);
4454
+		}
4455
+
4456
+		$this->_template_args['success'] = $success;
4457
+		$this->_return_json();
4458
+	}
4459
+
4460
+
4461
+
4462
+
4463
+	/**  EE MESSAGE PROCESSING ACTIONS **/
4464
+
4465
+
4466
+	/**
4467
+	 * This immediately generates any EE_Message ID's that are selected that are EEM_Message::status_incomplete
4468
+	 * However, this does not send immediately, it just queues for sending.
4469
+	 *
4470
+	 * @since 4.9.0
4471
+	 * @throws EE_Error
4472
+	 * @throws InvalidDataTypeException
4473
+	 * @throws InvalidInterfaceException
4474
+	 * @throws InvalidArgumentException
4475
+	 * @throws ReflectionException
4476
+	 */
4477
+	protected function _generate_now()
4478
+	{
4479
+		EED_Messages::generate_now($this->_get_msg_ids_from_request());
4480
+		$this->_redirect_after_action(false, '', '', array(), true);
4481
+	}
4482
+
4483
+
4484
+	/**
4485
+	 * This immediately generates AND sends any EE_Message's selected that are EEM_Message::status_incomplete or that
4486
+	 * are EEM_Message::status_resend or EEM_Message::status_idle
4487
+	 *
4488
+	 * @since 4.9.0
4489
+	 * @throws EE_Error
4490
+	 * @throws InvalidDataTypeException
4491
+	 * @throws InvalidInterfaceException
4492
+	 * @throws InvalidArgumentException
4493
+	 * @throws ReflectionException
4494
+	 */
4495
+	protected function _generate_and_send_now()
4496
+	{
4497
+		EED_Messages::generate_and_send_now($this->_get_msg_ids_from_request());
4498
+		$this->_redirect_after_action(false, '', '', array(), true);
4499
+	}
4500
+
4501
+
4502
+	/**
4503
+	 * This queues any EEM_Message::status_sent EE_Message ids in the request for resending.
4504
+	 *
4505
+	 * @since 4.9.0
4506
+	 * @throws EE_Error
4507
+	 * @throws InvalidDataTypeException
4508
+	 * @throws InvalidInterfaceException
4509
+	 * @throws InvalidArgumentException
4510
+	 * @throws ReflectionException
4511
+	 */
4512
+	protected function _queue_for_resending()
4513
+	{
4514
+		EED_Messages::queue_for_resending($this->_get_msg_ids_from_request());
4515
+		$this->_redirect_after_action(false, '', '', array(), true);
4516
+	}
4517
+
4518
+
4519
+	/**
4520
+	 *  This sends immediately any EEM_Message::status_idle or EEM_Message::status_resend messages in the queue
4521
+	 *
4522
+	 * @since 4.9.0
4523
+	 * @throws EE_Error
4524
+	 * @throws InvalidDataTypeException
4525
+	 * @throws InvalidInterfaceException
4526
+	 * @throws InvalidArgumentException
4527
+	 * @throws ReflectionException
4528
+	 */
4529
+	protected function _send_now()
4530
+	{
4531
+		EED_Messages::send_now($this->_get_msg_ids_from_request());
4532
+		$this->_redirect_after_action(false, '', '', array(), true);
4533
+	}
4534
+
4535
+
4536
+	/**
4537
+	 * Deletes EE_messages for IDs in the request.
4538
+	 *
4539
+	 * @since 4.9.0
4540
+	 * @throws EE_Error
4541
+	 * @throws InvalidDataTypeException
4542
+	 * @throws InvalidInterfaceException
4543
+	 * @throws InvalidArgumentException
4544
+	 */
4545
+	protected function _delete_ee_messages()
4546
+	{
4547
+		$msg_ids = $this->_get_msg_ids_from_request();
4548
+		$deleted_count = 0;
4549
+		foreach ($msg_ids as $msg_id) {
4550
+			if (EEM_Message::instance()->delete_by_ID($msg_id)) {
4551
+				$deleted_count++;
4552
+			}
4553
+		}
4554
+		if ($deleted_count) {
4555
+			EE_Error::add_success(
4556
+				esc_html(
4557
+					_n(
4558
+						'Message successfully deleted',
4559
+						'Messages successfully deleted',
4560
+						$deleted_count,
4561
+						'event_espresso'
4562
+					)
4563
+				)
4564
+			);
4565
+			$this->_redirect_after_action(
4566
+				false,
4567
+				'',
4568
+				'',
4569
+				array(),
4570
+				true
4571
+			);
4572
+		} else {
4573
+			EE_Error::add_error(
4574
+				_n('The message was not deleted.', 'The messages were not deleted', count($msg_ids), 'event_espresso'),
4575
+				__FILE__,
4576
+				__FUNCTION__,
4577
+				__LINE__
4578
+			);
4579
+			$this->_redirect_after_action(false, '', '', array(), true);
4580
+		}
4581
+	}
4582
+
4583
+
4584
+	/**
4585
+	 *  This looks for 'MSG_ID' key in the request and returns an array of MSG_ID's if present.
4586
+	 *
4587
+	 * @since 4.9.0
4588
+	 * @return array
4589
+	 */
4590
+	protected function _get_msg_ids_from_request()
4591
+	{
4592
+		if (! isset($this->_req_data['MSG_ID'])) {
4593
+			return array();
4594
+		}
4595
+
4596
+		return is_array($this->_req_data['MSG_ID'])
4597
+			? array_keys($this->_req_data['MSG_ID'])
4598
+			: array($this->_req_data['MSG_ID']);
4599
+	}
4600 4600
 }
Please login to merge, or discard this patch.
core/db_classes/EE_Ticket.class.php 1 patch
Indentation   +1998 added lines, -1998 removed lines patch added patch discarded remove patch
@@ -14,2006 +14,2006 @@
 block discarded – undo
14 14
 class EE_Ticket extends EE_Soft_Delete_Base_Class implements EEI_Line_Item_Object, EEI_Event_Relation, EEI_Has_Icon
15 15
 {
16 16
 
17
-    /**
18
-     * TicKet Sold out:
19
-     * constant used by ticket_status() to indicate that a ticket is sold out
20
-     * and no longer available for purchases
21
-     */
22
-    const sold_out = 'TKS';
23
-
24
-    /**
25
-     * TicKet Expired:
26
-     * constant used by ticket_status() to indicate that a ticket is expired
27
-     * and no longer available for purchase
28
-     */
29
-    const expired = 'TKE';
30
-
31
-    /**
32
-     * TicKet Archived:
33
-     * constant used by ticket_status() to indicate that a ticket is archived
34
-     * and no longer available for purchase
35
-     */
36
-    const archived = 'TKA';
37
-
38
-    /**
39
-     * TicKet Pending:
40
-     * constant used by ticket_status() to indicate that a ticket is pending
41
-     * and is NOT YET available for purchase
42
-     */
43
-    const pending = 'TKP';
44
-
45
-    /**
46
-     * TicKet On sale:
47
-     * constant used by ticket_status() to indicate that a ticket is On Sale
48
-     * and IS available for purchase
49
-     */
50
-    const onsale = 'TKO';
51
-
52
-    /**
53
-     * extra meta key for tracking ticket reservations
54
-     *
55
-     * @type string
56
-     */
57
-    const META_KEY_TICKET_RESERVATIONS = 'ticket_reservations';
58
-
59
-    /**
60
-     * override of parent property
61
-     *
62
-     * @var EEM_Ticket
63
-     */
64
-    protected $_model;
65
-
66
-    /**
67
-     * cached result from method of the same name
68
-     *
69
-     * @var float $_ticket_total_with_taxes
70
-     */
71
-    private $_ticket_total_with_taxes;
72
-
73
-
74
-    /**
75
-     * @param array  $props_n_values          incoming values
76
-     * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
77
-     *                                        used.)
78
-     * @param array  $date_formats            incoming date_formats in an array where the first value is the
79
-     *                                        date_format and the second value is the time format
80
-     * @return EE_Ticket
81
-     * @throws EE_Error
82
-     * @throws ReflectionException
83
-     */
84
-    public static function new_instance($props_n_values = [], $timezone = null, $date_formats = [])
85
-    {
86
-        $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
87
-        return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
88
-    }
89
-
90
-
91
-    /**
92
-     * @param array  $props_n_values  incoming values from the database
93
-     * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
94
-     *                                the website will be used.
95
-     * @return EE_Ticket
96
-     * @throws EE_Error
97
-     * @throws ReflectionException
98
-     */
99
-    public static function new_instance_from_db($props_n_values = [], $timezone = null)
100
-    {
101
-        return new self($props_n_values, true, $timezone);
102
-    }
103
-
104
-
105
-    /**
106
-     * @return bool
107
-     * @throws EE_Error
108
-     * @throws ReflectionException
109
-     */
110
-    public function parent()
111
-    {
112
-        return $this->get('TKT_parent');
113
-    }
114
-
115
-
116
-    /**
117
-     * return if a ticket has quantities available for purchase
118
-     *
119
-     * @param int $DTT_ID the primary key for a particular datetime
120
-     * @return boolean
121
-     * @throws EE_Error
122
-     * @throws ReflectionException
123
-     */
124
-    public function available($DTT_ID = 0)
125
-    {
126
-        // are we checking availability for a particular datetime ?
127
-        if ($DTT_ID) {
128
-            // get that datetime object
129
-            $datetime = $this->get_first_related('Datetime', [['DTT_ID' => $DTT_ID]]);
130
-            // if  ticket sales for this datetime have exceeded the reg limit...
131
-            if ($datetime instanceof EE_Datetime && $datetime->sold_out()) {
132
-                return false;
133
-            }
134
-        }
135
-        // datetime is still open for registration, but is this ticket sold out ?
136
-        return $this->qty() < 1 || $this->qty() > $this->sold();
137
-    }
138
-
139
-
140
-    /**
141
-     * Using the start date and end date this method calculates whether the ticket is On Sale, Pending, or Expired
142
-     *
143
-     * @param bool        $display   true = we'll return a localized string, otherwise we just return the value of the
144
-     *                               relevant status const
145
-     * @param bool | null $remaining if it is already known that tickets are available, then simply pass a bool to save
146
-     *                               further processing
147
-     * @return mixed status int if the display string isn't requested
148
-     * @throws EE_Error
149
-     * @throws ReflectionException
150
-     */
151
-    public function ticket_status($display = false, $remaining = null)
152
-    {
153
-        $remaining = is_bool($remaining) ? $remaining : $this->is_remaining();
154
-        if (! $remaining) {
155
-            return $display ? EEH_Template::pretty_status(EE_Ticket::sold_out, false, 'sentence') : EE_Ticket::sold_out;
156
-        }
157
-        if ($this->get('TKT_deleted')) {
158
-            return $display ? EEH_Template::pretty_status(EE_Ticket::archived, false, 'sentence') : EE_Ticket::archived;
159
-        }
160
-        if ($this->is_expired()) {
161
-            return $display ? EEH_Template::pretty_status(EE_Ticket::expired, false, 'sentence') : EE_Ticket::expired;
162
-        }
163
-        if ($this->is_pending()) {
164
-            return $display ? EEH_Template::pretty_status(EE_Ticket::pending, false, 'sentence') : EE_Ticket::pending;
165
-        }
166
-        if ($this->is_on_sale()) {
167
-            return $display ? EEH_Template::pretty_status(EE_Ticket::onsale, false, 'sentence') : EE_Ticket::onsale;
168
-        }
169
-        return '';
170
-    }
171
-
172
-
173
-    /**
174
-     * The purpose of this method is to simply return a boolean for whether there are any tickets remaining for sale
175
-     * considering ALL the factors used for figuring that out.
176
-     *
177
-     * @access public
178
-     * @param int $DTT_ID if an int above 0 is included here then we get a specific dtt.
179
-     * @return boolean         true = tickets remaining, false not.
180
-     * @throws EE_Error
181
-     * @throws ReflectionException
182
-     */
183
-    public function is_remaining($DTT_ID = 0)
184
-    {
185
-        $num_remaining = $this->remaining($DTT_ID);
186
-        if ($num_remaining === 0) {
187
-            return false;
188
-        }
189
-        if ($num_remaining > 0 && $num_remaining < $this->min()) {
190
-            return false;
191
-        }
192
-        return true;
193
-    }
194
-
195
-
196
-    /**
197
-     * return the total number of tickets available for purchase
198
-     *
199
-     * @param int $DTT_ID  the primary key for a particular datetime.
200
-     *                     set to 0 for all related datetimes
201
-     * @return int
202
-     * @throws EE_Error
203
-     * @throws ReflectionException
204
-     */
205
-    public function remaining($DTT_ID = 0)
206
-    {
207
-        return $this->real_quantity_on_ticket('saleable', $DTT_ID);
208
-    }
209
-
210
-
211
-    /**
212
-     * Gets min
213
-     *
214
-     * @return int
215
-     * @throws EE_Error
216
-     * @throws ReflectionException
217
-     */
218
-    public function min()
219
-    {
220
-        return $this->get('TKT_min');
221
-    }
222
-
223
-
224
-    /**
225
-     * return if a ticket is no longer available cause its available dates have expired.
226
-     *
227
-     * @return boolean
228
-     * @throws EE_Error
229
-     * @throws ReflectionException
230
-     */
231
-    public function is_expired()
232
-    {
233
-        return ($this->get_raw('TKT_end_date') < time());
234
-    }
235
-
236
-
237
-    /**
238
-     * Return if a ticket is yet to go on sale or not
239
-     *
240
-     * @return boolean
241
-     * @throws EE_Error
242
-     * @throws ReflectionException
243
-     */
244
-    public function is_pending()
245
-    {
246
-        return ($this->get_raw('TKT_start_date') >= time());
247
-    }
248
-
249
-
250
-    /**
251
-     * Return if a ticket is on sale or not
252
-     *
253
-     * @return boolean
254
-     * @throws EE_Error
255
-     * @throws ReflectionException
256
-     */
257
-    public function is_on_sale()
258
-    {
259
-        return ($this->get_raw('TKT_start_date') <= time() && $this->get_raw('TKT_end_date') >= time());
260
-    }
261
-
262
-
263
-    /**
264
-     * This returns the chronologically last datetime that this ticket is associated with
265
-     *
266
-     * @param string $date_format
267
-     * @param string $conjunction - conjunction junction what's your function ? this string joins the start date with
268
-     *                            the end date ie: Jan 01 "to" Dec 31
269
-     * @return string
270
-     * @throws EE_Error
271
-     * @throws ReflectionException
272
-     */
273
-    public function date_range($date_format = '', $conjunction = ' - ')
274
-    {
275
-        $date_format = ! empty($date_format) ? $date_format : $this->_dt_frmt;
276
-        $first_date  = $this->first_datetime() instanceof EE_Datetime
277
-            ? $this->first_datetime()->get_i18n_datetime('DTT_EVT_start', $date_format)
278
-            : '';
279
-        $last_date   = $this->last_datetime() instanceof EE_Datetime
280
-            ? $this->last_datetime()->get_i18n_datetime('DTT_EVT_end', $date_format)
281
-            : '';
282
-
283
-        return $first_date && $last_date ? $first_date . $conjunction . $last_date : '';
284
-    }
285
-
286
-
287
-    /**
288
-     * This returns the chronologically first datetime that this ticket is associated with
289
-     *
290
-     * @return EE_Datetime
291
-     * @throws EE_Error
292
-     * @throws ReflectionException
293
-     */
294
-    public function first_datetime()
295
-    {
296
-        $datetimes = $this->datetimes(['limit' => 1]);
297
-        return reset($datetimes);
298
-    }
299
-
300
-
301
-    /**
302
-     * Gets all the datetimes this ticket can be used for attending.
303
-     * Unless otherwise specified, orders datetimes by start date.
304
-     *
305
-     * @param array $query_params @see
306
-     *                            https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
307
-     * @return EE_Datetime[]|EE_Base_Class[]
308
-     * @throws EE_Error
309
-     * @throws ReflectionException
310
-     */
311
-    public function datetimes($query_params = [])
312
-    {
313
-        if (! isset($query_params['order_by'])) {
314
-            $query_params['order_by']['DTT_order'] = 'ASC';
315
-        }
316
-        return $this->get_many_related('Datetime', $query_params);
317
-    }
318
-
319
-
320
-    /**
321
-     * This returns the chronologically last datetime that this ticket is associated with
322
-     *
323
-     * @return EE_Datetime
324
-     * @throws EE_Error
325
-     * @throws ReflectionException
326
-     */
327
-    public function last_datetime()
328
-    {
329
-        $datetimes = $this->datetimes(['limit' => 1, 'order_by' => ['DTT_EVT_start' => 'DESC']]);
330
-        return end($datetimes);
331
-    }
332
-
333
-
334
-    /**
335
-     * This returns the total tickets sold depending on the given parameters.
336
-     *
337
-     * @param string $what    Can be one of two options: 'ticket', 'datetime'.
338
-     *                        'ticket' = total ticket sales for all datetimes this ticket is related to
339
-     *                        'datetime' = total ticket sales for a specified datetime (required $dtt_id)
340
-     *                        'datetime' = total ticket sales in the datetime_ticket table.
341
-     *                        If $dtt_id is not given then we return an array of sales indexed by datetime.
342
-     *                        If $dtt_id IS given then we return the tickets sold for that given datetime.
343
-     * @param int    $dtt_id  [optional] include the dtt_id with $what = 'datetime'.
344
-     * @return mixed (array|int)          how many tickets have sold
345
-     * @throws EE_Error
346
-     * @throws ReflectionException
347
-     */
348
-    public function tickets_sold($what = 'ticket', $dtt_id = null)
349
-    {
350
-        $total        = 0;
351
-        $tickets_sold = $this->_all_tickets_sold();
352
-        switch ($what) {
353
-            case 'ticket':
354
-                return $tickets_sold['ticket'];
355
-                break;
356
-            case 'datetime':
357
-                if (empty($tickets_sold['datetime'])) {
358
-                    return $total;
359
-                }
360
-                if (! empty($dtt_id) && ! isset($tickets_sold['datetime'][ $dtt_id ])) {
361
-                    EE_Error::add_error(
362
-                        __(
363
-                            'You\'ve requested the amount of tickets sold for a given ticket and datetime, however there are no records for the datetime id you included.  Are you SURE that is a datetime related to this ticket?',
364
-                            'event_espresso'
365
-                        ),
366
-                        __FILE__,
367
-                        __FUNCTION__,
368
-                        __LINE__
369
-                    );
370
-                    return $total;
371
-                }
372
-                return empty($dtt_id) ? $tickets_sold['datetime'] : $tickets_sold['datetime'][ $dtt_id ];
373
-                break;
374
-            default:
375
-                return $total;
376
-        }
377
-    }
378
-
379
-
380
-    /**
381
-     * This returns an array indexed by datetime_id for tickets sold with this ticket.
382
-     *
383
-     * @return EE_Ticket[]
384
-     * @throws EE_Error
385
-     * @throws ReflectionException
386
-     */
387
-    protected function _all_tickets_sold()
388
-    {
389
-        $datetimes    = $this->get_many_related('Datetime');
390
-        $tickets_sold = [];
391
-        if (! empty($datetimes)) {
392
-            foreach ($datetimes as $datetime) {
393
-                $tickets_sold['datetime'][ $datetime->ID() ] = $datetime->get('DTT_sold');
394
-            }
395
-        }
396
-        // Tickets sold
397
-        $tickets_sold['ticket'] = $this->sold();
398
-        return $tickets_sold;
399
-    }
400
-
401
-
402
-    /**
403
-     * This returns the base price object for the ticket.
404
-     *
405
-     * @param bool $return_array whether to return as an array indexed by price id or just the object.
406
-     * @return EE_Price|EE_Base_Class|EE_Price[]|EE_Base_Class[]
407
-     * @throws EE_Error
408
-     * @throws ReflectionException
409
-     */
410
-    public function base_price($return_array = false)
411
-    {
412
-        $_where = ['Price_Type.PBT_ID' => EEM_Price_Type::base_type_base_price];
413
-        return $return_array
414
-            ? $this->get_many_related('Price', [$_where])
415
-            : $this->get_first_related('Price', [$_where]);
416
-    }
417
-
418
-
419
-    /**
420
-     * This returns ONLY the price modifiers for the ticket (i.e. no taxes or base price)
421
-     *
422
-     * @access public
423
-     * @return EE_Price[]
424
-     * @throws EE_Error
425
-     * @throws ReflectionException
426
-     */
427
-    public function price_modifiers()
428
-    {
429
-        $query_params = [
430
-            0 => [
431
-                'Price_Type.PBT_ID' => [
432
-                    'NOT IN',
433
-                    [EEM_Price_Type::base_type_base_price, EEM_Price_Type::base_type_tax],
434
-                ],
435
-            ],
436
-        ];
437
-        return $this->prices($query_params);
438
-    }
439
-
440
-
441
-    /**
442
-     * This returns ONLY the price modifiers for the ticket (i.e. no taxes or base price)
443
-     *
444
-     * @access public
445
-     * @return EE_Price[]
446
-     * @throws EE_Error
447
-     * @throws ReflectionException
448
-     */
449
-    public function tax_price_modifiers()
450
-    {
451
-        $query_params = [
452
-            0 => [
453
-                'Price_Type.PBT_ID' => EEM_Price_Type::base_type_tax,
454
-            ],
455
-        ];
456
-        return $this->prices($query_params);
457
-    }
458
-
459
-
460
-    /**
461
-     * Gets all the prices that combine to form the final price of this ticket
462
-     *
463
-     * @param array $query_params @see
464
-     *                            https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
465
-     * @return EE_Price[]|EE_Base_Class[]
466
-     * @throws EE_Error
467
-     * @throws ReflectionException
468
-     */
469
-    public function prices($query_params = [])
470
-    {
471
-        return $this->get_many_related('Price', $query_params);
472
-    }
473
-
474
-
475
-    /**
476
-     * Gets all the ticket datetimes (ie, relations between datetimes and tickets)
477
-     *
478
-     * @param array $query_params @see
479
-     *                            https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
480
-     * @return EE_Datetime_Ticket|EE_Base_Class[]
481
-     * @throws EE_Error
482
-     * @throws ReflectionException
483
-     */
484
-    public function datetime_tickets($query_params = [])
485
-    {
486
-        return $this->get_many_related('Datetime_Ticket', $query_params);
487
-    }
488
-
489
-
490
-    /**
491
-     * Gets all the datetimes from the db ordered by DTT_order
492
-     *
493
-     * @param boolean $show_expired
494
-     * @param boolean $show_deleted
495
-     * @return EE_Datetime[]
496
-     * @throws EE_Error
497
-     * @throws ReflectionException
498
-     */
499
-    public function datetimes_ordered($show_expired = true, $show_deleted = false)
500
-    {
501
-        return EEM_Datetime::instance($this->_timezone)->get_datetimes_for_ticket_ordered_by_DTT_order(
502
-            $this->ID(),
503
-            $show_expired,
504
-            $show_deleted
505
-        );
506
-    }
507
-
508
-
509
-    /**
510
-     * Gets ID
511
-     *
512
-     * @return int
513
-     * @throws EE_Error
514
-     * @throws ReflectionException
515
-     */
516
-    public function ID()
517
-    {
518
-        return $this->get('TKT_ID');
519
-    }
520
-
521
-
522
-    /**
523
-     * get the author of the ticket.
524
-     *
525
-     * @return int
526
-     * @throws EE_Error
527
-     * @throws ReflectionException
528
-     * @since 4.5.0
529
-     */
530
-    public function wp_user()
531
-    {
532
-        return $this->get('TKT_wp_user');
533
-    }
534
-
535
-
536
-    /**
537
-     * Gets the template for the ticket
538
-     *
539
-     * @return EE_Ticket_Template|EE_Base_Class
540
-     * @throws EE_Error
541
-     * @throws ReflectionException
542
-     */
543
-    public function template()
544
-    {
545
-        return $this->get_first_related('Ticket_Template');
546
-    }
547
-
548
-
549
-    /**
550
-     * Simply returns an array of EE_Price objects that are taxes.
551
-     *
552
-     * @return EE_Price[]
553
-     * @throws EE_Error
554
-     */
555
-    public function get_ticket_taxes_for_admin()
556
-    {
557
-        return EE_Taxes::get_taxes_for_admin();
558
-    }
559
-
560
-
561
-    /**
562
-     * @return float
563
-     * @throws EE_Error
564
-     * @throws ReflectionException
565
-     */
566
-    public function ticket_price()
567
-    {
568
-        return $this->get('TKT_price');
569
-    }
570
-
571
-
572
-    /**
573
-     * @return mixed
574
-     * @throws EE_Error
575
-     * @throws ReflectionException
576
-     */
577
-    public function pretty_price()
578
-    {
579
-        return $this->get_pretty('TKT_price');
580
-    }
581
-
582
-
583
-    /**
584
-     * @return bool
585
-     * @throws EE_Error
586
-     * @throws ReflectionException
587
-     */
588
-    public function is_free()
589
-    {
590
-        return $this->get_ticket_total_with_taxes() === (float) 0;
591
-    }
592
-
593
-
594
-    /**
595
-     * get_ticket_total_with_taxes
596
-     *
597
-     * @param bool $no_cache
598
-     * @return float
599
-     * @throws EE_Error
600
-     * @throws ReflectionException
601
-     */
602
-    public function get_ticket_total_with_taxes($no_cache = false)
603
-    {
604
-        if ($this->_ticket_total_with_taxes === null || $no_cache) {
605
-            $this->_ticket_total_with_taxes = $this->get_ticket_subtotal() + $this->get_ticket_taxes_total_for_admin();
606
-        }
607
-        return (float) $this->_ticket_total_with_taxes;
608
-    }
609
-
610
-
611
-    /**
612
-     * @throws EE_Error
613
-     * @throws ReflectionException
614
-     */
615
-    public function ensure_TKT_Price_correct()
616
-    {
617
-        $this->set('TKT_price', EE_Taxes::get_subtotal_for_admin($this));
618
-        $this->save();
619
-    }
620
-
621
-
622
-    /**
623
-     * @return float
624
-     * @throws EE_Error
625
-     * @throws ReflectionException
626
-     */
627
-    public function get_ticket_subtotal()
628
-    {
629
-        return EE_Taxes::get_subtotal_for_admin($this);
630
-    }
631
-
632
-
633
-    /**
634
-     * Returns the total taxes applied to this ticket
635
-     *
636
-     * @return float
637
-     * @throws EE_Error
638
-     * @throws ReflectionException
639
-     */
640
-    public function get_ticket_taxes_total_for_admin()
641
-    {
642
-        return EE_Taxes::get_total_taxes_for_admin($this);
643
-    }
644
-
645
-
646
-    /**
647
-     * Sets name
648
-     *
649
-     * @param string $name
650
-     * @throws EE_Error
651
-     * @throws ReflectionException
652
-     */
653
-    public function set_name($name)
654
-    {
655
-        $this->set('TKT_name', $name);
656
-    }
657
-
658
-
659
-    /**
660
-     * Gets description
661
-     *
662
-     * @return string
663
-     * @throws EE_Error
664
-     * @throws ReflectionException
665
-     */
666
-    public function description()
667
-    {
668
-        return $this->get('TKT_description');
669
-    }
670
-
671
-
672
-    /**
673
-     * Sets description
674
-     *
675
-     * @param string $description
676
-     * @throws EE_Error
677
-     * @throws ReflectionException
678
-     */
679
-    public function set_description($description)
680
-    {
681
-        $this->set('TKT_description', $description);
682
-    }
683
-
684
-
685
-    /**
686
-     * Gets start_date
687
-     *
688
-     * @param string $date_format
689
-     * @param string $time_format
690
-     * @return string
691
-     * @throws EE_Error
692
-     * @throws ReflectionException
693
-     */
694
-    public function start_date($date_format = '', $time_format = '')
695
-    {
696
-        return $this->_get_datetime('TKT_start_date', $date_format, $time_format);
697
-    }
698
-
699
-
700
-    /**
701
-     * Sets start_date
702
-     *
703
-     * @param string $start_date
704
-     * @return void
705
-     * @throws EE_Error
706
-     * @throws ReflectionException
707
-     */
708
-    public function set_start_date($start_date)
709
-    {
710
-        $this->_set_date_time('B', $start_date, 'TKT_start_date');
711
-    }
712
-
713
-
714
-    /**
715
-     * Gets end_date
716
-     *
717
-     * @param string $date_format
718
-     * @param string $time_format
719
-     * @return string
720
-     * @throws EE_Error
721
-     * @throws ReflectionException
722
-     */
723
-    public function end_date($date_format = '', $time_format = '')
724
-    {
725
-        return $this->_get_datetime('TKT_end_date', $date_format, $time_format);
726
-    }
727
-
728
-
729
-    /**
730
-     * Sets end_date
731
-     *
732
-     * @param string $end_date
733
-     * @return void
734
-     * @throws EE_Error
735
-     * @throws ReflectionException
736
-     */
737
-    public function set_end_date($end_date)
738
-    {
739
-        $this->_set_date_time('B', $end_date, 'TKT_end_date');
740
-    }
741
-
742
-
743
-    /**
744
-     * Sets sell until time
745
-     *
746
-     * @param string $time a string representation of the sell until time (ex 9am or 7:30pm)
747
-     * @throws EE_Error
748
-     * @throws ReflectionException
749
-     * @since 4.5.0
750
-     */
751
-    public function set_end_time($time)
752
-    {
753
-        $this->_set_time_for($time, 'TKT_end_date');
754
-    }
755
-
756
-
757
-    /**
758
-     * Sets min
759
-     *
760
-     * @param int $min
761
-     * @return void
762
-     * @throws EE_Error
763
-     * @throws ReflectionException
764
-     */
765
-    public function set_min($min)
766
-    {
767
-        $this->set('TKT_min', $min);
768
-    }
769
-
770
-
771
-    /**
772
-     * Gets max
773
-     *
774
-     * @return int
775
-     * @throws EE_Error
776
-     * @throws ReflectionException
777
-     */
778
-    public function max()
779
-    {
780
-        return $this->get('TKT_max');
781
-    }
782
-
783
-
784
-    /**
785
-     * Sets max
786
-     *
787
-     * @param int $max
788
-     * @return void
789
-     * @throws EE_Error
790
-     * @throws ReflectionException
791
-     */
792
-    public function set_max($max)
793
-    {
794
-        $this->set('TKT_max', $max);
795
-    }
796
-
797
-
798
-    /**
799
-     * Sets price
800
-     *
801
-     * @param float $price
802
-     * @return void
803
-     * @throws EE_Error
804
-     * @throws ReflectionException
805
-     */
806
-    public function set_price($price)
807
-    {
808
-        $this->set('TKT_price', $price);
809
-    }
810
-
811
-
812
-    /**
813
-     * Gets sold
814
-     *
815
-     * @return int
816
-     * @throws EE_Error
817
-     * @throws ReflectionException
818
-     */
819
-    public function sold()
820
-    {
821
-        return $this->get_raw('TKT_sold');
822
-    }
823
-
824
-
825
-    /**
826
-     * Sets sold
827
-     *
828
-     * @param int $sold
829
-     * @return void
830
-     * @throws EE_Error
831
-     * @throws ReflectionException
832
-     */
833
-    public function set_sold($sold)
834
-    {
835
-        // sold can not go below zero
836
-        $sold = max(0, $sold);
837
-        $this->set('TKT_sold', $sold);
838
-    }
839
-
840
-
841
-    /**
842
-     * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its
843
-     * associated datetimes.
844
-     *
845
-     * @param int $qty
846
-     * @return boolean
847
-     * @throws EE_Error
848
-     * @throws InvalidArgumentException
849
-     * @throws InvalidDataTypeException
850
-     * @throws InvalidInterfaceException
851
-     * @throws ReflectionException
852
-     * @since 4.9.80.p
853
-     */
854
-    public function increaseSold($qty = 1)
855
-    {
856
-        $qty = absint($qty);
857
-        // increment sold and decrement reserved datetime quantities simultaneously
858
-        // don't worry about failures, because they must have already had a spot reserved
859
-        $this->increaseSoldForDatetimes($qty);
860
-        // Increment and decrement ticket quantities simultaneously
861
-        $success = $this->adjustNumericFieldsInDb(
862
-            [
863
-                'TKT_reserved' => $qty * -1,
864
-                'TKT_sold'     => $qty,
865
-            ]
866
-        );
867
-        do_action(
868
-            'AHEE__EE_Ticket__increase_sold',
869
-            $this,
870
-            $qty,
871
-            $this->sold(),
872
-            $success
873
-        );
874
-        return $success;
875
-    }
876
-
877
-
878
-    /**
879
-     * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty.
880
-     *
881
-     * @param int           $qty positive or negative. Positive means to increase sold counts (and decrease reserved
882
-     *                           counts), Negative means to decreases old counts (and increase reserved counts).
883
-     * @param EE_Datetime[] $datetimes
884
-     * @throws EE_Error
885
-     * @throws InvalidArgumentException
886
-     * @throws InvalidDataTypeException
887
-     * @throws InvalidInterfaceException
888
-     * @throws ReflectionException
889
-     * @since 4.9.80.p
890
-     */
891
-    protected function increaseSoldForDatetimes($qty, array $datetimes = [])
892
-    {
893
-        $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
894
-        foreach ($datetimes as $datetime) {
895
-            $datetime->increaseSold($qty);
896
-        }
897
-    }
898
-
899
-
900
-    /**
901
-     * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the
902
-     * DB and then updates the model objects.
903
-     * Does not affect the reserved counts.
904
-     *
905
-     * @param int $qty
906
-     * @return boolean
907
-     * @throws EE_Error
908
-     * @throws InvalidArgumentException
909
-     * @throws InvalidDataTypeException
910
-     * @throws InvalidInterfaceException
911
-     * @throws ReflectionException
912
-     * @since 4.9.80.p
913
-     */
914
-    public function decreaseSold($qty = 1)
915
-    {
916
-        $qty = absint($qty);
917
-        $this->decreaseSoldForDatetimes($qty);
918
-        $success = $this->adjustNumericFieldsInDb(
919
-            [
920
-                'TKT_sold' => $qty * -1,
921
-            ]
922
-        );
923
-        do_action(
924
-            'AHEE__EE_Ticket__decrease_sold',
925
-            $this,
926
-            $qty,
927
-            $this->sold(),
928
-            $success
929
-        );
930
-        return $success;
931
-    }
932
-
933
-
934
-    /**
935
-     * Decreases sold on related datetimes
936
-     *
937
-     * @param int           $qty
938
-     * @param EE_Datetime[] $datetimes
939
-     * @return void
940
-     * @throws EE_Error
941
-     * @throws InvalidArgumentException
942
-     * @throws InvalidDataTypeException
943
-     * @throws InvalidInterfaceException
944
-     * @throws ReflectionException
945
-     * @since 4.9.80.p
946
-     */
947
-    protected function decreaseSoldForDatetimes($qty = 1, array $datetimes = [])
948
-    {
949
-        $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
950
-        if (is_array($datetimes)) {
951
-            foreach ($datetimes as $datetime) {
952
-                if ($datetime instanceof EE_Datetime) {
953
-                    $datetime->decreaseSold($qty);
954
-                }
955
-            }
956
-        }
957
-    }
958
-
959
-
960
-    /**
961
-     * Gets qty of reserved tickets
962
-     *
963
-     * @return int
964
-     * @throws EE_Error
965
-     * @throws ReflectionException
966
-     */
967
-    public function reserved()
968
-    {
969
-        return $this->get_raw('TKT_reserved');
970
-    }
971
-
972
-
973
-    /**
974
-     * Sets reserved
975
-     *
976
-     * @param int $reserved
977
-     * @return void
978
-     * @throws EE_Error
979
-     * @throws ReflectionException
980
-     */
981
-    public function set_reserved($reserved)
982
-    {
983
-        // reserved can not go below zero
984
-        $reserved = max(0, (int) $reserved);
985
-        $this->set('TKT_reserved', $reserved);
986
-    }
987
-
988
-
989
-    /**
990
-     * Increments reserved by amount passed by $qty, and persists it immediately to the database.
991
-     *
992
-     * @param int    $qty
993
-     * @param string $source
994
-     * @return bool whether we successfully reserved the ticket or not.
995
-     * @throws EE_Error
996
-     * @throws InvalidArgumentException
997
-     * @throws ReflectionException
998
-     * @throws InvalidDataTypeException
999
-     * @throws InvalidInterfaceException
1000
-     * @since 4.9.80.p
1001
-     */
1002
-    public function increaseReserved($qty = 1, $source = 'unknown')
1003
-    {
1004
-        $qty = absint($qty);
1005
-        do_action(
1006
-            'AHEE__EE_Ticket__increase_reserved__begin',
1007
-            $this,
1008
-            $qty,
1009
-            $source
1010
-        );
1011
-        $this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "{$qty} from {$source}");
1012
-        $success                         = false;
1013
-        $datetimes_adjusted_successfully = $this->increaseReservedForDatetimes($qty);
1014
-        if ($datetimes_adjusted_successfully) {
1015
-            $success = $this->incrementFieldConditionallyInDb(
1016
-                'TKT_reserved',
1017
-                'TKT_sold',
1018
-                'TKT_qty',
1019
-                $qty
1020
-            );
1021
-            if (! $success) {
1022
-                // The datetimes were successfully bumped, but not the
1023
-                // ticket. So we need to manually rollback the datetimes.
1024
-                $this->decreaseReservedForDatetimes($qty);
1025
-            }
1026
-        }
1027
-        do_action(
1028
-            'AHEE__EE_Ticket__increase_reserved',
1029
-            $this,
1030
-            $qty,
1031
-            $this->reserved(),
1032
-            $success
1033
-        );
1034
-        return $success;
1035
-    }
1036
-
1037
-
1038
-    /**
1039
-     * Increases reserved counts on related datetimes
1040
-     *
1041
-     * @param int           $qty
1042
-     * @param EE_Datetime[] $datetimes
1043
-     * @return boolean indicating success
1044
-     * @throws EE_Error
1045
-     * @throws InvalidArgumentException
1046
-     * @throws InvalidDataTypeException
1047
-     * @throws InvalidInterfaceException
1048
-     * @throws ReflectionException
1049
-     * @since 4.9.80.p
1050
-     */
1051
-    protected function increaseReservedForDatetimes($qty = 1, array $datetimes = [])
1052
-    {
1053
-        $datetimes         = ! empty($datetimes) ? $datetimes : $this->datetimes();
1054
-        $datetimes_updated = [];
1055
-        $limit_exceeded    = false;
1056
-        if (is_array($datetimes)) {
1057
-            foreach ($datetimes as $datetime) {
1058
-                if ($datetime instanceof EE_Datetime) {
1059
-                    if ($datetime->increaseReserved($qty)) {
1060
-                        $datetimes_updated[] = $datetime;
1061
-                    } else {
1062
-                        $limit_exceeded = true;
1063
-                        break;
1064
-                    }
1065
-                }
1066
-            }
1067
-            // If somewhere along the way we detected a datetime whose
1068
-            // limit was exceeded, do a manual rollback.
1069
-            if ($limit_exceeded) {
1070
-                $this->decreaseReservedForDatetimes($qty, $datetimes_updated);
1071
-                return false;
1072
-            }
1073
-        }
1074
-        return true;
1075
-    }
1076
-
1077
-
1078
-    /**
1079
-     * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database.
1080
-     *
1081
-     * @param int    $qty
1082
-     * @param bool   $adjust_datetimes
1083
-     * @param string $source
1084
-     * @return boolean
1085
-     * @throws EE_Error
1086
-     * @throws InvalidArgumentException
1087
-     * @throws ReflectionException
1088
-     * @throws InvalidDataTypeException
1089
-     * @throws InvalidInterfaceException
1090
-     * @since 4.9.80.p
1091
-     */
1092
-    public function decreaseReserved($qty = 1, $adjust_datetimes = true, $source = 'unknown')
1093
-    {
1094
-        $qty = absint($qty);
1095
-        $this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "-{$qty} from {$source}");
1096
-        if ($adjust_datetimes) {
1097
-            $this->decreaseReservedForDatetimes($qty);
1098
-        }
1099
-        $success = $this->adjustNumericFieldsInDb(
1100
-            [
1101
-                'TKT_reserved' => $qty * -1,
1102
-            ]
1103
-        );
1104
-        do_action(
1105
-            'AHEE__EE_Ticket__decrease_reserved',
1106
-            $this,
1107
-            $qty,
1108
-            $this->reserved(),
1109
-            $success
1110
-        );
1111
-        return $success;
1112
-    }
1113
-
1114
-
1115
-    /**
1116
-     * Decreases the reserved count on the specified datetimes.
1117
-     *
1118
-     * @param int           $qty
1119
-     * @param EE_Datetime[] $datetimes
1120
-     * @throws EE_Error
1121
-     * @throws InvalidArgumentException
1122
-     * @throws ReflectionException
1123
-     * @throws InvalidDataTypeException
1124
-     * @throws InvalidInterfaceException
1125
-     * @since 4.9.80.p
1126
-     */
1127
-    protected function decreaseReservedForDatetimes($qty = 1, array $datetimes = [])
1128
-    {
1129
-        $datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
1130
-        foreach ($datetimes as $datetime) {
1131
-            if ($datetime instanceof EE_Datetime) {
1132
-                $datetime->decreaseReserved($qty);
1133
-            }
1134
-        }
1135
-    }
1136
-
1137
-
1138
-    /**
1139
-     * Gets ticket quantity
1140
-     *
1141
-     * @param string $context     ticket quantity is somewhat subjective depending on the exact information sought
1142
-     *                            therefore $context can be one of three values: '', 'reg_limit', or 'saleable'
1143
-     *                            '' (default) quantity is the actual db value for TKT_qty, unaffected by other objects
1144
-     *                            REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes
1145
-     *                            SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and
1146
-     *                            is therefore the truest measure of tickets that can be purchased at the moment
1147
-     * @return int
1148
-     * @throws EE_Error
1149
-     * @throws ReflectionException
1150
-     */
1151
-    public function qty($context = '')
1152
-    {
1153
-        switch ($context) {
1154
-            case 'reg_limit':
1155
-                return $this->real_quantity_on_ticket();
1156
-            case 'saleable':
1157
-                return $this->real_quantity_on_ticket('saleable');
1158
-            default:
1159
-                return $this->get_raw('TKT_qty');
1160
-        }
1161
-    }
1162
-
1163
-
1164
-    /**
1165
-     * Gets ticket quantity
1166
-     *
1167
-     * @param string $context     ticket quantity is somewhat subjective depending on the exact information sought
1168
-     *                            therefore $context can be one of two values: 'reg_limit', or 'saleable'
1169
-     *                            REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes
1170
-     *                            SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and
1171
-     *                            is therefore the truest measure of tickets that can be purchased at the moment
1172
-     * @param int    $DTT_ID      the primary key for a particular datetime.
1173
-     *                            set to 0 for all related datetimes
1174
-     * @return int
1175
-     * @throws EE_Error
1176
-     * @throws ReflectionException
1177
-     */
1178
-    public function real_quantity_on_ticket($context = 'reg_limit', $DTT_ID = 0)
1179
-    {
1180
-        $raw = $this->get_raw('TKT_qty');
1181
-        // return immediately if it's zero
1182
-        if ($raw === 0) {
1183
-            return $raw;
1184
-        }
1185
-        // echo "\n\n<br />Ticket: " . $this->name() . '<br />';
1186
-        // ensure qty doesn't exceed raw value for THIS ticket
1187
-        $qty = min(EE_INF, $raw);
1188
-        // echo "\n . qty: " . $qty . '<br />';
1189
-        // calculate this ticket's total sales and reservations
1190
-        $sold_and_reserved_for_this_ticket = $this->sold() + $this->reserved();
1191
-        // echo "\n . sold: " . $this->sold() . '<br />';
1192
-        // echo "\n . reserved: " . $this->reserved() . '<br />';
1193
-        // echo "\n . sold_and_reserved_for_this_ticket: " . $sold_and_reserved_for_this_ticket . '<br />';
1194
-        // first we need to calculate the maximum number of tickets available for the datetime
1195
-        // do we want data for one datetime or all of them ?
1196
-        $query_params = $DTT_ID ? [['DTT_ID' => $DTT_ID]] : [];
1197
-        $datetimes    = $this->datetimes($query_params);
1198
-        if (is_array($datetimes) && ! empty($datetimes)) {
1199
-            foreach ($datetimes as $datetime) {
1200
-                if ($datetime instanceof EE_Datetime) {
1201
-                    $datetime->refresh_from_db();
1202
-                    // echo "\n . . datetime name: " . $datetime->name() . '<br />';
1203
-                    // echo "\n . . datetime ID: " . $datetime->ID() . '<br />';
1204
-                    // initialize with no restrictions for each datetime
1205
-                    // but adjust datetime qty based on datetime reg limit
1206
-                    $datetime_qty = min(EE_INF, $datetime->reg_limit());
1207
-                    // echo "\n . . . datetime reg_limit: " . $datetime->reg_limit() . '<br />';
1208
-                    // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1209
-                    // if we want the actual saleable amount, then we need to consider OTHER ticket sales
1210
-                    // and reservations for this datetime, that do NOT include sales and reservations
1211
-                    // for this ticket (so we add $this->sold() and $this->reserved() back in)
1212
-                    if ($context === 'saleable') {
1213
-                        $datetime_qty = max(
1214
-                            $datetime_qty - $datetime->sold_and_reserved() + $sold_and_reserved_for_this_ticket,
1215
-                            0
1216
-                        );
1217
-                        // echo "\n . . . datetime sold: " . $datetime->sold() . '<br />';
1218
-                        // echo "\n . . . datetime reserved: " . $datetime->reserved() . '<br />';
1219
-                        // echo "\n . . . datetime sold_and_reserved: " . $datetime->sold_and_reserved() . '<br />';
1220
-                        // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1221
-                        $datetime_qty = ! $datetime->sold_out() ? $datetime_qty : 0;
1222
-                        // echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1223
-                    }
1224
-                    $qty = min($datetime_qty, $qty);
1225
-                    // echo "\n . . qty: " . $qty . '<br />';
1226
-                }
1227
-            }
1228
-        }
1229
-        // NOW that we know the  maximum number of tickets available for the datetime
1230
-        // we can finally factor in the details for this specific ticket
1231
-        if ($qty > 0 && $context === 'saleable') {
1232
-            // and subtract the sales for THIS ticket
1233
-            $qty = max($qty - $sold_and_reserved_for_this_ticket, 0);
1234
-            // echo "\n . qty: " . $qty . '<br />';
1235
-        }
1236
-        // echo "\nFINAL QTY: " . $qty . "<br /><br />";
1237
-        return $qty;
1238
-    }
1239
-
1240
-
1241
-    /**
1242
-     * Sets qty - IMPORTANT!!! Does NOT allow QTY to be set higher than the lowest reg limit of any related datetimes
1243
-     *
1244
-     * @param int $qty
1245
-     * @return void
1246
-     * @throws EE_Error
1247
-     * @throws ReflectionException
1248
-     */
1249
-    public function set_qty($qty)
1250
-    {
1251
-        $datetimes = $this->datetimes();
1252
-        foreach ($datetimes as $datetime) {
1253
-            if ($datetime instanceof EE_Datetime) {
1254
-                $qty = min($qty, $datetime->reg_limit());
1255
-            }
1256
-        }
1257
-        $this->set('TKT_qty', $qty);
1258
-    }
1259
-
1260
-
1261
-    /**
1262
-     * Gets uses
1263
-     *
1264
-     * @return int
1265
-     * @throws EE_Error
1266
-     * @throws ReflectionException
1267
-     */
1268
-    public function uses()
1269
-    {
1270
-        return $this->get('TKT_uses');
1271
-    }
1272
-
1273
-
1274
-    /**
1275
-     * Sets uses
1276
-     *
1277
-     * @param int $uses
1278
-     * @return void
1279
-     * @throws EE_Error
1280
-     * @throws ReflectionException
1281
-     */
1282
-    public function set_uses($uses)
1283
-    {
1284
-        $this->set('TKT_uses', $uses);
1285
-    }
1286
-
1287
-
1288
-    /**
1289
-     * returns whether ticket is required or not.
1290
-     *
1291
-     * @return boolean
1292
-     * @throws EE_Error
1293
-     * @throws ReflectionException
1294
-     */
1295
-    public function required()
1296
-    {
1297
-        return $this->get('TKT_required');
1298
-    }
1299
-
1300
-
1301
-    /**
1302
-     * sets the TKT_required property
1303
-     *
1304
-     * @param boolean $required
1305
-     * @return void
1306
-     * @throws EE_Error
1307
-     * @throws ReflectionException
1308
-     */
1309
-    public function set_required($required)
1310
-    {
1311
-        $this->set('TKT_required', $required);
1312
-    }
1313
-
1314
-
1315
-    /**
1316
-     * Gets taxable
1317
-     *
1318
-     * @return boolean
1319
-     * @throws EE_Error
1320
-     * @throws ReflectionException
1321
-     */
1322
-    public function taxable()
1323
-    {
1324
-        return $this->get('TKT_taxable');
1325
-    }
1326
-
1327
-
1328
-    /**
1329
-     * Sets taxable
1330
-     *
1331
-     * @param boolean $taxable
1332
-     * @return void
1333
-     * @throws EE_Error
1334
-     * @throws ReflectionException
1335
-     */
1336
-    public function set_taxable($taxable)
1337
-    {
1338
-        $this->set('TKT_taxable', $taxable);
1339
-    }
1340
-
1341
-
1342
-    /**
1343
-     * Gets is_default
1344
-     *
1345
-     * @return boolean
1346
-     * @throws EE_Error
1347
-     * @throws ReflectionException
1348
-     */
1349
-    public function is_default()
1350
-    {
1351
-        return $this->get('TKT_is_default');
1352
-    }
1353
-
1354
-
1355
-    /**
1356
-     * Sets is_default
1357
-     *
1358
-     * @param boolean $is_default
1359
-     * @return void
1360
-     * @throws EE_Error
1361
-     * @throws ReflectionException
1362
-     */
1363
-    public function set_is_default($is_default)
1364
-    {
1365
-        $this->set('TKT_is_default', $is_default);
1366
-    }
1367
-
1368
-
1369
-    /**
1370
-     * Gets order
1371
-     *
1372
-     * @return int
1373
-     * @throws EE_Error
1374
-     * @throws ReflectionException
1375
-     */
1376
-    public function order()
1377
-    {
1378
-        return $this->get('TKT_order');
1379
-    }
1380
-
1381
-
1382
-    /**
1383
-     * Sets order
1384
-     *
1385
-     * @param int $order
1386
-     * @return void
1387
-     * @throws EE_Error
1388
-     * @throws ReflectionException
1389
-     */
1390
-    public function set_order($order)
1391
-    {
1392
-        $this->set('TKT_order', $order);
1393
-    }
1394
-
1395
-
1396
-    /**
1397
-     * Gets row
1398
-     *
1399
-     * @return int
1400
-     * @throws EE_Error
1401
-     * @throws ReflectionException
1402
-     */
1403
-    public function row()
1404
-    {
1405
-        return $this->get('TKT_row');
1406
-    }
1407
-
1408
-
1409
-    /**
1410
-     * Sets row
1411
-     *
1412
-     * @param int $row
1413
-     * @return void
1414
-     * @throws EE_Error
1415
-     * @throws ReflectionException
1416
-     */
1417
-    public function set_row($row)
1418
-    {
1419
-        $this->set('TKT_row', $row);
1420
-    }
1421
-
1422
-
1423
-    /**
1424
-     * Gets deleted
1425
-     *
1426
-     * @return boolean
1427
-     * @throws EE_Error
1428
-     * @throws ReflectionException
1429
-     */
1430
-    public function deleted()
1431
-    {
1432
-        return $this->get('TKT_deleted');
1433
-    }
1434
-
1435
-
1436
-    /**
1437
-     * Sets deleted
1438
-     *
1439
-     * @param boolean $deleted
1440
-     * @return void
1441
-     * @throws EE_Error
1442
-     * @throws ReflectionException
1443
-     */
1444
-    public function set_deleted($deleted)
1445
-    {
1446
-        $this->set('TKT_deleted', $deleted);
1447
-    }
1448
-
1449
-
1450
-    /**
1451
-     * Gets parent
1452
-     *
1453
-     * @return int
1454
-     * @throws EE_Error
1455
-     * @throws ReflectionException
1456
-     */
1457
-    public function parent_ID()
1458
-    {
1459
-        return $this->get('TKT_parent');
1460
-    }
1461
-
1462
-
1463
-    /**
1464
-     * Sets parent
1465
-     *
1466
-     * @param int $parent
1467
-     * @return void
1468
-     * @throws EE_Error
1469
-     * @throws ReflectionException
1470
-     */
1471
-    public function set_parent_ID($parent)
1472
-    {
1473
-        $this->set('TKT_parent', $parent);
1474
-    }
1475
-
1476
-
1477
-    /**
1478
-     * @return boolean
1479
-     * @throws EE_Error
1480
-     * @throws InvalidArgumentException
1481
-     * @throws InvalidDataTypeException
1482
-     * @throws InvalidInterfaceException
1483
-     * @throws ReflectionException
1484
-     */
1485
-    public function reverse_calculate()
1486
-    {
1487
-        return $this->get('TKT_reverse_calculate');
1488
-    }
1489
-
1490
-
1491
-    /**
1492
-     * @param boolean $reverse_calculate
1493
-     * @throws EE_Error
1494
-     * @throws InvalidArgumentException
1495
-     * @throws InvalidDataTypeException
1496
-     * @throws InvalidInterfaceException
1497
-     * @throws ReflectionException
1498
-     */
1499
-    public function set_reverse_calculate($reverse_calculate)
1500
-    {
1501
-        $this->set('TKT_reverse_calculate', $reverse_calculate);
1502
-    }
1503
-
1504
-
1505
-    /**
1506
-     * Gets a string which is handy for showing in gateways etc that describes the ticket.
1507
-     *
1508
-     * @return string
1509
-     * @throws EE_Error
1510
-     * @throws ReflectionException
1511
-     */
1512
-    public function name_and_info()
1513
-    {
1514
-        $times = [];
1515
-        foreach ($this->datetimes() as $datetime) {
1516
-            $times[] = $datetime->start_date_and_time();
1517
-        }
1518
-        return $this->name() . ' @ ' . implode(', ', $times) . ' for ' . $this->pretty_price();
1519
-    }
1520
-
1521
-
1522
-    /**
1523
-     * Gets name
1524
-     *
1525
-     * @return string
1526
-     * @throws EE_Error
1527
-     * @throws ReflectionException
1528
-     */
1529
-    public function name()
1530
-    {
1531
-        return $this->get('TKT_name');
1532
-    }
1533
-
1534
-
1535
-    /**
1536
-     * Gets price
1537
-     *
1538
-     * @return float
1539
-     * @throws EE_Error
1540
-     * @throws ReflectionException
1541
-     */
1542
-    public function price()
1543
-    {
1544
-        return $this->get('TKT_price');
1545
-    }
1546
-
1547
-
1548
-    /**
1549
-     * Gets all the registrations for this ticket
1550
-     *
1551
-     * @param array $query_params @see
1552
-     *                            https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1553
-     * @return EE_Registration[]|EE_Base_Class[]
1554
-     * @throws EE_Error
1555
-     * @throws ReflectionException
1556
-     */
1557
-    public function registrations($query_params = [])
1558
-    {
1559
-        return $this->get_many_related('Registration', $query_params);
1560
-    }
1561
-
1562
-
1563
-    /**
1564
-     * Updates the TKT_sold attribute (and saves) based on the number of APPROVED registrations for this ticket.
1565
-     *
1566
-     * @return int
1567
-     * @throws EE_Error
1568
-     * @throws ReflectionException
1569
-     */
1570
-    public function update_tickets_sold()
1571
-    {
1572
-        $count_regs_for_this_ticket = $this->count_registrations(
1573
-            [
1574
-                [
1575
-                    'STS_ID'      => EEM_Registration::status_id_approved,
1576
-                    'REG_deleted' => 0,
1577
-                ],
1578
-            ]
1579
-        );
1580
-        $this->set_sold($count_regs_for_this_ticket);
1581
-        $this->save();
1582
-        return $count_regs_for_this_ticket;
1583
-    }
1584
-
1585
-
1586
-    /**
1587
-     * Counts the registrations for this ticket
1588
-     *
1589
-     * @param array $query_params @see
1590
-     *                            https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1591
-     * @return int
1592
-     * @throws EE_Error
1593
-     * @throws ReflectionException
1594
-     */
1595
-    public function count_registrations($query_params = [])
1596
-    {
1597
-        return $this->count_related('Registration', $query_params);
1598
-    }
1599
-
1600
-
1601
-    /**
1602
-     * Implementation for EEI_Has_Icon interface method.
1603
-     *
1604
-     * @return string
1605
-     * @see EEI_Visual_Representation for comments
1606
-     */
1607
-    public function get_icon()
1608
-    {
1609
-        return '<span class="dashicons dashicons-tickets-alt"/>';
1610
-    }
1611
-
1612
-
1613
-    /**
1614
-     * Implementation of the EEI_Event_Relation interface method
1615
-     *
1616
-     * @return EE_Event
1617
-     * @throws EE_Error
1618
-     * @throws UnexpectedEntityException
1619
-     * @throws ReflectionException
1620
-     * @see EEI_Event_Relation for comments
1621
-     */
1622
-    public function get_related_event()
1623
-    {
1624
-        // get one datetime to use for getting the event
1625
-        $datetime = $this->first_datetime();
1626
-        if (! $datetime instanceof EE_Datetime) {
1627
-            throw new UnexpectedEntityException(
1628
-                $datetime,
1629
-                'EE_Datetime',
1630
-                sprintf(
1631
-                    __('The ticket (%s) is not associated with any valid datetimes.', 'event_espresso'),
1632
-                    $this->name()
1633
-                )
1634
-            );
1635
-        }
1636
-        $event = $datetime->event();
1637
-        if (! $event instanceof EE_Event) {
1638
-            throw new UnexpectedEntityException(
1639
-                $event,
1640
-                'EE_Event',
1641
-                sprintf(
1642
-                    __('The ticket (%s) is not associated with a valid event.', 'event_espresso'),
1643
-                    $this->name()
1644
-                )
1645
-            );
1646
-        }
1647
-        return $event;
1648
-    }
1649
-
1650
-
1651
-    /**
1652
-     * Implementation of the EEI_Event_Relation interface method
1653
-     *
1654
-     * @return string
1655
-     * @throws UnexpectedEntityException
1656
-     * @throws EE_Error
1657
-     * @throws ReflectionException
1658
-     * @see EEI_Event_Relation for comments
1659
-     */
1660
-    public function get_event_name()
1661
-    {
1662
-        $event = $this->get_related_event();
1663
-        return $event instanceof EE_Event ? $event->name() : '';
1664
-    }
1665
-
1666
-
1667
-    /**
1668
-     * Implementation of the EEI_Event_Relation interface method
1669
-     *
1670
-     * @return int
1671
-     * @throws UnexpectedEntityException
1672
-     * @throws EE_Error
1673
-     * @throws ReflectionException
1674
-     * @see EEI_Event_Relation for comments
1675
-     */
1676
-    public function get_event_ID()
1677
-    {
1678
-        $event = $this->get_related_event();
1679
-        return $event instanceof EE_Event ? $event->ID() : 0;
1680
-    }
1681
-
1682
-
1683
-    /**
1684
-     * This simply returns whether a ticket can be permanently deleted or not.
1685
-     * The criteria for determining this is whether the ticket has any related registrations.
1686
-     * If there are none then it can be permanently deleted.
1687
-     *
1688
-     * @return bool
1689
-     * @throws EE_Error
1690
-     * @throws ReflectionException
1691
-     */
1692
-    public function is_permanently_deleteable()
1693
-    {
1694
-        return $this->count_registrations() === 0;
1695
-    }
1696
-
1697
-
1698
-    /**
1699
-     * @return int
1700
-     * @throws EE_Error
1701
-     * @throws ReflectionException
1702
-     * @since   $VID:$
1703
-     */
1704
-    public function visibility(): int
1705
-    {
1706
-        return $this->get('TKT_visibility');
1707
-    }
1708
-
1709
-
1710
-    /**
1711
-     * @return int
1712
-     * @throws EE_Error
1713
-     * @throws ReflectionException
1714
-     * @since   $VID:$
1715
-     */
1716
-    public function isHidden(): int
1717
-    {
1718
-        return $this->visibility() === EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE;
1719
-    }
1720
-
1721
-
1722
-    /**
1723
-     * @return int
1724
-     * @throws EE_Error
1725
-     * @throws ReflectionException
1726
-     * @since   $VID:$
1727
-     */
1728
-    public function isNotHidden(): int
1729
-    {
1730
-        return $this->visibility() > EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE;
1731
-    }
1732
-
1733
-
1734
-    /**
1735
-     * @return int
1736
-     * @throws EE_Error
1737
-     * @throws ReflectionException
1738
-     * @since   $VID:$
1739
-     */
1740
-    public function isPublicOnly(): int
1741
-    {
1742
-        return $this->isNotHidden() && $this->visibility() <= EEM_Ticket::TICKET_VISIBILITY_PUBLIC_VALUE;
1743
-    }
1744
-
1745
-
1746
-    /**
1747
-     * @return int
1748
-     * @throws EE_Error
1749
-     * @throws ReflectionException
1750
-     * @since   $VID:$
1751
-     */
1752
-    public function isMembersOnly(): int
1753
-    {
1754
-        return $this->visibility() > EEM_Ticket::TICKET_VISIBILITY_PUBLIC_VALUE
1755
-               && $this->visibility() <= EEM_Ticket::TICKET_VISIBILITY_MEMBERS_ONLY_VALUE;
1756
-    }
1757
-
1758
-
1759
-    /**
1760
-     * @return int
1761
-     * @throws EE_Error
1762
-     * @throws ReflectionException
1763
-     * @since   $VID:$
1764
-     */
1765
-    public function isAdminsOnly(): int
1766
-    {
1767
-        return $this->visibility() > EEM_Ticket::TICKET_VISIBILITY_MEMBERS_ONLY_VALUE
1768
-               && $this->visibility() <= EEM_Ticket::TICKET_VISIBILITY_ADMINS_ONLY_VALUE;
1769
-    }
1770
-
1771
-
1772
-    /**
1773
-     * @return int
1774
-     * @throws EE_Error
1775
-     * @throws ReflectionException
1776
-     * @since   $VID:$
1777
-     */
1778
-    public function isAdminUiOnly(): int
1779
-    {
1780
-        return $this->visibility() > EEM_Ticket::TICKET_VISIBILITY_ADMINS_ONLY_VALUE
1781
-               && $this->visibility() <= EEM_Ticket::TICKET_VISIBILITY_ADMIN_UI_ONLY_VALUE;
1782
-    }
1783
-
1784
-
1785
-    /**
1786
-     * @param int $visibility
1787
-     * @throws EE_Error
1788
-     * @throws ReflectionException
1789
-     * @since   $VID:$
1790
-     */
1791
-    public function set_visibility(int $visibility)
1792
-    {
1793
-
1794
-        $ticket_visibility_options = $this->_model->ticketVisibilityOptions();
1795
-        $ticket_visibility         = -1;
1796
-        foreach ($ticket_visibility_options as $ticket_visibility_option) {
1797
-            if ($visibility === $ticket_visibility_option) {
1798
-                $ticket_visibility = $visibility;
1799
-            }
1800
-        }
1801
-        if ($ticket_visibility === -1) {
1802
-            throw new DomainException(
1803
-                sprintf(
1804
-                    esc_html__(
1805
-                        'The supplied ticket visibility setting of "%1$s" is not valid. It needs to match one of the keys in the following array:%2$s %3$s ',
1806
-                        'event_espresso'
1807
-                    ),
1808
-                    $visibility,
1809
-                    '<br />',
1810
-                    var_export($ticket_visibility_options, true)
1811
-                )
1812
-            );
1813
-        }
1814
-        $this->set('TKT_visibility', $ticket_visibility);
1815
-    }
1816
-
1817
-
1818
-    /*******************************************************************
17
+	/**
18
+	 * TicKet Sold out:
19
+	 * constant used by ticket_status() to indicate that a ticket is sold out
20
+	 * and no longer available for purchases
21
+	 */
22
+	const sold_out = 'TKS';
23
+
24
+	/**
25
+	 * TicKet Expired:
26
+	 * constant used by ticket_status() to indicate that a ticket is expired
27
+	 * and no longer available for purchase
28
+	 */
29
+	const expired = 'TKE';
30
+
31
+	/**
32
+	 * TicKet Archived:
33
+	 * constant used by ticket_status() to indicate that a ticket is archived
34
+	 * and no longer available for purchase
35
+	 */
36
+	const archived = 'TKA';
37
+
38
+	/**
39
+	 * TicKet Pending:
40
+	 * constant used by ticket_status() to indicate that a ticket is pending
41
+	 * and is NOT YET available for purchase
42
+	 */
43
+	const pending = 'TKP';
44
+
45
+	/**
46
+	 * TicKet On sale:
47
+	 * constant used by ticket_status() to indicate that a ticket is On Sale
48
+	 * and IS available for purchase
49
+	 */
50
+	const onsale = 'TKO';
51
+
52
+	/**
53
+	 * extra meta key for tracking ticket reservations
54
+	 *
55
+	 * @type string
56
+	 */
57
+	const META_KEY_TICKET_RESERVATIONS = 'ticket_reservations';
58
+
59
+	/**
60
+	 * override of parent property
61
+	 *
62
+	 * @var EEM_Ticket
63
+	 */
64
+	protected $_model;
65
+
66
+	/**
67
+	 * cached result from method of the same name
68
+	 *
69
+	 * @var float $_ticket_total_with_taxes
70
+	 */
71
+	private $_ticket_total_with_taxes;
72
+
73
+
74
+	/**
75
+	 * @param array  $props_n_values          incoming values
76
+	 * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
77
+	 *                                        used.)
78
+	 * @param array  $date_formats            incoming date_formats in an array where the first value is the
79
+	 *                                        date_format and the second value is the time format
80
+	 * @return EE_Ticket
81
+	 * @throws EE_Error
82
+	 * @throws ReflectionException
83
+	 */
84
+	public static function new_instance($props_n_values = [], $timezone = null, $date_formats = [])
85
+	{
86
+		$has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
87
+		return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
88
+	}
89
+
90
+
91
+	/**
92
+	 * @param array  $props_n_values  incoming values from the database
93
+	 * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
94
+	 *                                the website will be used.
95
+	 * @return EE_Ticket
96
+	 * @throws EE_Error
97
+	 * @throws ReflectionException
98
+	 */
99
+	public static function new_instance_from_db($props_n_values = [], $timezone = null)
100
+	{
101
+		return new self($props_n_values, true, $timezone);
102
+	}
103
+
104
+
105
+	/**
106
+	 * @return bool
107
+	 * @throws EE_Error
108
+	 * @throws ReflectionException
109
+	 */
110
+	public function parent()
111
+	{
112
+		return $this->get('TKT_parent');
113
+	}
114
+
115
+
116
+	/**
117
+	 * return if a ticket has quantities available for purchase
118
+	 *
119
+	 * @param int $DTT_ID the primary key for a particular datetime
120
+	 * @return boolean
121
+	 * @throws EE_Error
122
+	 * @throws ReflectionException
123
+	 */
124
+	public function available($DTT_ID = 0)
125
+	{
126
+		// are we checking availability for a particular datetime ?
127
+		if ($DTT_ID) {
128
+			// get that datetime object
129
+			$datetime = $this->get_first_related('Datetime', [['DTT_ID' => $DTT_ID]]);
130
+			// if  ticket sales for this datetime have exceeded the reg limit...
131
+			if ($datetime instanceof EE_Datetime && $datetime->sold_out()) {
132
+				return false;
133
+			}
134
+		}
135
+		// datetime is still open for registration, but is this ticket sold out ?
136
+		return $this->qty() < 1 || $this->qty() > $this->sold();
137
+	}
138
+
139
+
140
+	/**
141
+	 * Using the start date and end date this method calculates whether the ticket is On Sale, Pending, or Expired
142
+	 *
143
+	 * @param bool        $display   true = we'll return a localized string, otherwise we just return the value of the
144
+	 *                               relevant status const
145
+	 * @param bool | null $remaining if it is already known that tickets are available, then simply pass a bool to save
146
+	 *                               further processing
147
+	 * @return mixed status int if the display string isn't requested
148
+	 * @throws EE_Error
149
+	 * @throws ReflectionException
150
+	 */
151
+	public function ticket_status($display = false, $remaining = null)
152
+	{
153
+		$remaining = is_bool($remaining) ? $remaining : $this->is_remaining();
154
+		if (! $remaining) {
155
+			return $display ? EEH_Template::pretty_status(EE_Ticket::sold_out, false, 'sentence') : EE_Ticket::sold_out;
156
+		}
157
+		if ($this->get('TKT_deleted')) {
158
+			return $display ? EEH_Template::pretty_status(EE_Ticket::archived, false, 'sentence') : EE_Ticket::archived;
159
+		}
160
+		if ($this->is_expired()) {
161
+			return $display ? EEH_Template::pretty_status(EE_Ticket::expired, false, 'sentence') : EE_Ticket::expired;
162
+		}
163
+		if ($this->is_pending()) {
164
+			return $display ? EEH_Template::pretty_status(EE_Ticket::pending, false, 'sentence') : EE_Ticket::pending;
165
+		}
166
+		if ($this->is_on_sale()) {
167
+			return $display ? EEH_Template::pretty_status(EE_Ticket::onsale, false, 'sentence') : EE_Ticket::onsale;
168
+		}
169
+		return '';
170
+	}
171
+
172
+
173
+	/**
174
+	 * The purpose of this method is to simply return a boolean for whether there are any tickets remaining for sale
175
+	 * considering ALL the factors used for figuring that out.
176
+	 *
177
+	 * @access public
178
+	 * @param int $DTT_ID if an int above 0 is included here then we get a specific dtt.
179
+	 * @return boolean         true = tickets remaining, false not.
180
+	 * @throws EE_Error
181
+	 * @throws ReflectionException
182
+	 */
183
+	public function is_remaining($DTT_ID = 0)
184
+	{
185
+		$num_remaining = $this->remaining($DTT_ID);
186
+		if ($num_remaining === 0) {
187
+			return false;
188
+		}
189
+		if ($num_remaining > 0 && $num_remaining < $this->min()) {
190
+			return false;
191
+		}
192
+		return true;
193
+	}
194
+
195
+
196
+	/**
197
+	 * return the total number of tickets available for purchase
198
+	 *
199
+	 * @param int $DTT_ID  the primary key for a particular datetime.
200
+	 *                     set to 0 for all related datetimes
201
+	 * @return int
202
+	 * @throws EE_Error
203
+	 * @throws ReflectionException
204
+	 */
205
+	public function remaining($DTT_ID = 0)
206
+	{
207
+		return $this->real_quantity_on_ticket('saleable', $DTT_ID);
208
+	}
209
+
210
+
211
+	/**
212
+	 * Gets min
213
+	 *
214
+	 * @return int
215
+	 * @throws EE_Error
216
+	 * @throws ReflectionException
217
+	 */
218
+	public function min()
219
+	{
220
+		return $this->get('TKT_min');
221
+	}
222
+
223
+
224
+	/**
225
+	 * return if a ticket is no longer available cause its available dates have expired.
226
+	 *
227
+	 * @return boolean
228
+	 * @throws EE_Error
229
+	 * @throws ReflectionException
230
+	 */
231
+	public function is_expired()
232
+	{
233
+		return ($this->get_raw('TKT_end_date') < time());
234
+	}
235
+
236
+
237
+	/**
238
+	 * Return if a ticket is yet to go on sale or not
239
+	 *
240
+	 * @return boolean
241
+	 * @throws EE_Error
242
+	 * @throws ReflectionException
243
+	 */
244
+	public function is_pending()
245
+	{
246
+		return ($this->get_raw('TKT_start_date') >= time());
247
+	}
248
+
249
+
250
+	/**
251
+	 * Return if a ticket is on sale or not
252
+	 *
253
+	 * @return boolean
254
+	 * @throws EE_Error
255
+	 * @throws ReflectionException
256
+	 */
257
+	public function is_on_sale()
258
+	{
259
+		return ($this->get_raw('TKT_start_date') <= time() && $this->get_raw('TKT_end_date') >= time());
260
+	}
261
+
262
+
263
+	/**
264
+	 * This returns the chronologically last datetime that this ticket is associated with
265
+	 *
266
+	 * @param string $date_format
267
+	 * @param string $conjunction - conjunction junction what's your function ? this string joins the start date with
268
+	 *                            the end date ie: Jan 01 "to" Dec 31
269
+	 * @return string
270
+	 * @throws EE_Error
271
+	 * @throws ReflectionException
272
+	 */
273
+	public function date_range($date_format = '', $conjunction = ' - ')
274
+	{
275
+		$date_format = ! empty($date_format) ? $date_format : $this->_dt_frmt;
276
+		$first_date  = $this->first_datetime() instanceof EE_Datetime
277
+			? $this->first_datetime()->get_i18n_datetime('DTT_EVT_start', $date_format)
278
+			: '';
279
+		$last_date   = $this->last_datetime() instanceof EE_Datetime
280
+			? $this->last_datetime()->get_i18n_datetime('DTT_EVT_end', $date_format)
281
+			: '';
282
+
283
+		return $first_date && $last_date ? $first_date . $conjunction . $last_date : '';
284
+	}
285
+
286
+
287
+	/**
288
+	 * This returns the chronologically first datetime that this ticket is associated with
289
+	 *
290
+	 * @return EE_Datetime
291
+	 * @throws EE_Error
292
+	 * @throws ReflectionException
293
+	 */
294
+	public function first_datetime()
295
+	{
296
+		$datetimes = $this->datetimes(['limit' => 1]);
297
+		return reset($datetimes);
298
+	}
299
+
300
+
301
+	/**
302
+	 * Gets all the datetimes this ticket can be used for attending.
303
+	 * Unless otherwise specified, orders datetimes by start date.
304
+	 *
305
+	 * @param array $query_params @see
306
+	 *                            https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
307
+	 * @return EE_Datetime[]|EE_Base_Class[]
308
+	 * @throws EE_Error
309
+	 * @throws ReflectionException
310
+	 */
311
+	public function datetimes($query_params = [])
312
+	{
313
+		if (! isset($query_params['order_by'])) {
314
+			$query_params['order_by']['DTT_order'] = 'ASC';
315
+		}
316
+		return $this->get_many_related('Datetime', $query_params);
317
+	}
318
+
319
+
320
+	/**
321
+	 * This returns the chronologically last datetime that this ticket is associated with
322
+	 *
323
+	 * @return EE_Datetime
324
+	 * @throws EE_Error
325
+	 * @throws ReflectionException
326
+	 */
327
+	public function last_datetime()
328
+	{
329
+		$datetimes = $this->datetimes(['limit' => 1, 'order_by' => ['DTT_EVT_start' => 'DESC']]);
330
+		return end($datetimes);
331
+	}
332
+
333
+
334
+	/**
335
+	 * This returns the total tickets sold depending on the given parameters.
336
+	 *
337
+	 * @param string $what    Can be one of two options: 'ticket', 'datetime'.
338
+	 *                        'ticket' = total ticket sales for all datetimes this ticket is related to
339
+	 *                        'datetime' = total ticket sales for a specified datetime (required $dtt_id)
340
+	 *                        'datetime' = total ticket sales in the datetime_ticket table.
341
+	 *                        If $dtt_id is not given then we return an array of sales indexed by datetime.
342
+	 *                        If $dtt_id IS given then we return the tickets sold for that given datetime.
343
+	 * @param int    $dtt_id  [optional] include the dtt_id with $what = 'datetime'.
344
+	 * @return mixed (array|int)          how many tickets have sold
345
+	 * @throws EE_Error
346
+	 * @throws ReflectionException
347
+	 */
348
+	public function tickets_sold($what = 'ticket', $dtt_id = null)
349
+	{
350
+		$total        = 0;
351
+		$tickets_sold = $this->_all_tickets_sold();
352
+		switch ($what) {
353
+			case 'ticket':
354
+				return $tickets_sold['ticket'];
355
+				break;
356
+			case 'datetime':
357
+				if (empty($tickets_sold['datetime'])) {
358
+					return $total;
359
+				}
360
+				if (! empty($dtt_id) && ! isset($tickets_sold['datetime'][ $dtt_id ])) {
361
+					EE_Error::add_error(
362
+						__(
363
+							'You\'ve requested the amount of tickets sold for a given ticket and datetime, however there are no records for the datetime id you included.  Are you SURE that is a datetime related to this ticket?',
364
+							'event_espresso'
365
+						),
366
+						__FILE__,
367
+						__FUNCTION__,
368
+						__LINE__
369
+					);
370
+					return $total;
371
+				}
372
+				return empty($dtt_id) ? $tickets_sold['datetime'] : $tickets_sold['datetime'][ $dtt_id ];
373
+				break;
374
+			default:
375
+				return $total;
376
+		}
377
+	}
378
+
379
+
380
+	/**
381
+	 * This returns an array indexed by datetime_id for tickets sold with this ticket.
382
+	 *
383
+	 * @return EE_Ticket[]
384
+	 * @throws EE_Error
385
+	 * @throws ReflectionException
386
+	 */
387
+	protected function _all_tickets_sold()
388
+	{
389
+		$datetimes    = $this->get_many_related('Datetime');
390
+		$tickets_sold = [];
391
+		if (! empty($datetimes)) {
392
+			foreach ($datetimes as $datetime) {
393
+				$tickets_sold['datetime'][ $datetime->ID() ] = $datetime->get('DTT_sold');
394
+			}
395
+		}
396
+		// Tickets sold
397
+		$tickets_sold['ticket'] = $this->sold();
398
+		return $tickets_sold;
399
+	}
400
+
401
+
402
+	/**
403
+	 * This returns the base price object for the ticket.
404
+	 *
405
+	 * @param bool $return_array whether to return as an array indexed by price id or just the object.
406
+	 * @return EE_Price|EE_Base_Class|EE_Price[]|EE_Base_Class[]
407
+	 * @throws EE_Error
408
+	 * @throws ReflectionException
409
+	 */
410
+	public function base_price($return_array = false)
411
+	{
412
+		$_where = ['Price_Type.PBT_ID' => EEM_Price_Type::base_type_base_price];
413
+		return $return_array
414
+			? $this->get_many_related('Price', [$_where])
415
+			: $this->get_first_related('Price', [$_where]);
416
+	}
417
+
418
+
419
+	/**
420
+	 * This returns ONLY the price modifiers for the ticket (i.e. no taxes or base price)
421
+	 *
422
+	 * @access public
423
+	 * @return EE_Price[]
424
+	 * @throws EE_Error
425
+	 * @throws ReflectionException
426
+	 */
427
+	public function price_modifiers()
428
+	{
429
+		$query_params = [
430
+			0 => [
431
+				'Price_Type.PBT_ID' => [
432
+					'NOT IN',
433
+					[EEM_Price_Type::base_type_base_price, EEM_Price_Type::base_type_tax],
434
+				],
435
+			],
436
+		];
437
+		return $this->prices($query_params);
438
+	}
439
+
440
+
441
+	/**
442
+	 * This returns ONLY the price modifiers for the ticket (i.e. no taxes or base price)
443
+	 *
444
+	 * @access public
445
+	 * @return EE_Price[]
446
+	 * @throws EE_Error
447
+	 * @throws ReflectionException
448
+	 */
449
+	public function tax_price_modifiers()
450
+	{
451
+		$query_params = [
452
+			0 => [
453
+				'Price_Type.PBT_ID' => EEM_Price_Type::base_type_tax,
454
+			],
455
+		];
456
+		return $this->prices($query_params);
457
+	}
458
+
459
+
460
+	/**
461
+	 * Gets all the prices that combine to form the final price of this ticket
462
+	 *
463
+	 * @param array $query_params @see
464
+	 *                            https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
465
+	 * @return EE_Price[]|EE_Base_Class[]
466
+	 * @throws EE_Error
467
+	 * @throws ReflectionException
468
+	 */
469
+	public function prices($query_params = [])
470
+	{
471
+		return $this->get_many_related('Price', $query_params);
472
+	}
473
+
474
+
475
+	/**
476
+	 * Gets all the ticket datetimes (ie, relations between datetimes and tickets)
477
+	 *
478
+	 * @param array $query_params @see
479
+	 *                            https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
480
+	 * @return EE_Datetime_Ticket|EE_Base_Class[]
481
+	 * @throws EE_Error
482
+	 * @throws ReflectionException
483
+	 */
484
+	public function datetime_tickets($query_params = [])
485
+	{
486
+		return $this->get_many_related('Datetime_Ticket', $query_params);
487
+	}
488
+
489
+
490
+	/**
491
+	 * Gets all the datetimes from the db ordered by DTT_order
492
+	 *
493
+	 * @param boolean $show_expired
494
+	 * @param boolean $show_deleted
495
+	 * @return EE_Datetime[]
496
+	 * @throws EE_Error
497
+	 * @throws ReflectionException
498
+	 */
499
+	public function datetimes_ordered($show_expired = true, $show_deleted = false)
500
+	{
501
+		return EEM_Datetime::instance($this->_timezone)->get_datetimes_for_ticket_ordered_by_DTT_order(
502
+			$this->ID(),
503
+			$show_expired,
504
+			$show_deleted
505
+		);
506
+	}
507
+
508
+
509
+	/**
510
+	 * Gets ID
511
+	 *
512
+	 * @return int
513
+	 * @throws EE_Error
514
+	 * @throws ReflectionException
515
+	 */
516
+	public function ID()
517
+	{
518
+		return $this->get('TKT_ID');
519
+	}
520
+
521
+
522
+	/**
523
+	 * get the author of the ticket.
524
+	 *
525
+	 * @return int
526
+	 * @throws EE_Error
527
+	 * @throws ReflectionException
528
+	 * @since 4.5.0
529
+	 */
530
+	public function wp_user()
531
+	{
532
+		return $this->get('TKT_wp_user');
533
+	}
534
+
535
+
536
+	/**
537
+	 * Gets the template for the ticket
538
+	 *
539
+	 * @return EE_Ticket_Template|EE_Base_Class
540
+	 * @throws EE_Error
541
+	 * @throws ReflectionException
542
+	 */
543
+	public function template()
544
+	{
545
+		return $this->get_first_related('Ticket_Template');
546
+	}
547
+
548
+
549
+	/**
550
+	 * Simply returns an array of EE_Price objects that are taxes.
551
+	 *
552
+	 * @return EE_Price[]
553
+	 * @throws EE_Error
554
+	 */
555
+	public function get_ticket_taxes_for_admin()
556
+	{
557
+		return EE_Taxes::get_taxes_for_admin();
558
+	}
559
+
560
+
561
+	/**
562
+	 * @return float
563
+	 * @throws EE_Error
564
+	 * @throws ReflectionException
565
+	 */
566
+	public function ticket_price()
567
+	{
568
+		return $this->get('TKT_price');
569
+	}
570
+
571
+
572
+	/**
573
+	 * @return mixed
574
+	 * @throws EE_Error
575
+	 * @throws ReflectionException
576
+	 */
577
+	public function pretty_price()
578
+	{
579
+		return $this->get_pretty('TKT_price');
580
+	}
581
+
582
+
583
+	/**
584
+	 * @return bool
585
+	 * @throws EE_Error
586
+	 * @throws ReflectionException
587
+	 */
588
+	public function is_free()
589
+	{
590
+		return $this->get_ticket_total_with_taxes() === (float) 0;
591
+	}
592
+
593
+
594
+	/**
595
+	 * get_ticket_total_with_taxes
596
+	 *
597
+	 * @param bool $no_cache
598
+	 * @return float
599
+	 * @throws EE_Error
600
+	 * @throws ReflectionException
601
+	 */
602
+	public function get_ticket_total_with_taxes($no_cache = false)
603
+	{
604
+		if ($this->_ticket_total_with_taxes === null || $no_cache) {
605
+			$this->_ticket_total_with_taxes = $this->get_ticket_subtotal() + $this->get_ticket_taxes_total_for_admin();
606
+		}
607
+		return (float) $this->_ticket_total_with_taxes;
608
+	}
609
+
610
+
611
+	/**
612
+	 * @throws EE_Error
613
+	 * @throws ReflectionException
614
+	 */
615
+	public function ensure_TKT_Price_correct()
616
+	{
617
+		$this->set('TKT_price', EE_Taxes::get_subtotal_for_admin($this));
618
+		$this->save();
619
+	}
620
+
621
+
622
+	/**
623
+	 * @return float
624
+	 * @throws EE_Error
625
+	 * @throws ReflectionException
626
+	 */
627
+	public function get_ticket_subtotal()
628
+	{
629
+		return EE_Taxes::get_subtotal_for_admin($this);
630
+	}
631
+
632
+
633
+	/**
634
+	 * Returns the total taxes applied to this ticket
635
+	 *
636
+	 * @return float
637
+	 * @throws EE_Error
638
+	 * @throws ReflectionException
639
+	 */
640
+	public function get_ticket_taxes_total_for_admin()
641
+	{
642
+		return EE_Taxes::get_total_taxes_for_admin($this);
643
+	}
644
+
645
+
646
+	/**
647
+	 * Sets name
648
+	 *
649
+	 * @param string $name
650
+	 * @throws EE_Error
651
+	 * @throws ReflectionException
652
+	 */
653
+	public function set_name($name)
654
+	{
655
+		$this->set('TKT_name', $name);
656
+	}
657
+
658
+
659
+	/**
660
+	 * Gets description
661
+	 *
662
+	 * @return string
663
+	 * @throws EE_Error
664
+	 * @throws ReflectionException
665
+	 */
666
+	public function description()
667
+	{
668
+		return $this->get('TKT_description');
669
+	}
670
+
671
+
672
+	/**
673
+	 * Sets description
674
+	 *
675
+	 * @param string $description
676
+	 * @throws EE_Error
677
+	 * @throws ReflectionException
678
+	 */
679
+	public function set_description($description)
680
+	{
681
+		$this->set('TKT_description', $description);
682
+	}
683
+
684
+
685
+	/**
686
+	 * Gets start_date
687
+	 *
688
+	 * @param string $date_format
689
+	 * @param string $time_format
690
+	 * @return string
691
+	 * @throws EE_Error
692
+	 * @throws ReflectionException
693
+	 */
694
+	public function start_date($date_format = '', $time_format = '')
695
+	{
696
+		return $this->_get_datetime('TKT_start_date', $date_format, $time_format);
697
+	}
698
+
699
+
700
+	/**
701
+	 * Sets start_date
702
+	 *
703
+	 * @param string $start_date
704
+	 * @return void
705
+	 * @throws EE_Error
706
+	 * @throws ReflectionException
707
+	 */
708
+	public function set_start_date($start_date)
709
+	{
710
+		$this->_set_date_time('B', $start_date, 'TKT_start_date');
711
+	}
712
+
713
+
714
+	/**
715
+	 * Gets end_date
716
+	 *
717
+	 * @param string $date_format
718
+	 * @param string $time_format
719
+	 * @return string
720
+	 * @throws EE_Error
721
+	 * @throws ReflectionException
722
+	 */
723
+	public function end_date($date_format = '', $time_format = '')
724
+	{
725
+		return $this->_get_datetime('TKT_end_date', $date_format, $time_format);
726
+	}
727
+
728
+
729
+	/**
730
+	 * Sets end_date
731
+	 *
732
+	 * @param string $end_date
733
+	 * @return void
734
+	 * @throws EE_Error
735
+	 * @throws ReflectionException
736
+	 */
737
+	public function set_end_date($end_date)
738
+	{
739
+		$this->_set_date_time('B', $end_date, 'TKT_end_date');
740
+	}
741
+
742
+
743
+	/**
744
+	 * Sets sell until time
745
+	 *
746
+	 * @param string $time a string representation of the sell until time (ex 9am or 7:30pm)
747
+	 * @throws EE_Error
748
+	 * @throws ReflectionException
749
+	 * @since 4.5.0
750
+	 */
751
+	public function set_end_time($time)
752
+	{
753
+		$this->_set_time_for($time, 'TKT_end_date');
754
+	}
755
+
756
+
757
+	/**
758
+	 * Sets min
759
+	 *
760
+	 * @param int $min
761
+	 * @return void
762
+	 * @throws EE_Error
763
+	 * @throws ReflectionException
764
+	 */
765
+	public function set_min($min)
766
+	{
767
+		$this->set('TKT_min', $min);
768
+	}
769
+
770
+
771
+	/**
772
+	 * Gets max
773
+	 *
774
+	 * @return int
775
+	 * @throws EE_Error
776
+	 * @throws ReflectionException
777
+	 */
778
+	public function max()
779
+	{
780
+		return $this->get('TKT_max');
781
+	}
782
+
783
+
784
+	/**
785
+	 * Sets max
786
+	 *
787
+	 * @param int $max
788
+	 * @return void
789
+	 * @throws EE_Error
790
+	 * @throws ReflectionException
791
+	 */
792
+	public function set_max($max)
793
+	{
794
+		$this->set('TKT_max', $max);
795
+	}
796
+
797
+
798
+	/**
799
+	 * Sets price
800
+	 *
801
+	 * @param float $price
802
+	 * @return void
803
+	 * @throws EE_Error
804
+	 * @throws ReflectionException
805
+	 */
806
+	public function set_price($price)
807
+	{
808
+		$this->set('TKT_price', $price);
809
+	}
810
+
811
+
812
+	/**
813
+	 * Gets sold
814
+	 *
815
+	 * @return int
816
+	 * @throws EE_Error
817
+	 * @throws ReflectionException
818
+	 */
819
+	public function sold()
820
+	{
821
+		return $this->get_raw('TKT_sold');
822
+	}
823
+
824
+
825
+	/**
826
+	 * Sets sold
827
+	 *
828
+	 * @param int $sold
829
+	 * @return void
830
+	 * @throws EE_Error
831
+	 * @throws ReflectionException
832
+	 */
833
+	public function set_sold($sold)
834
+	{
835
+		// sold can not go below zero
836
+		$sold = max(0, $sold);
837
+		$this->set('TKT_sold', $sold);
838
+	}
839
+
840
+
841
+	/**
842
+	 * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its
843
+	 * associated datetimes.
844
+	 *
845
+	 * @param int $qty
846
+	 * @return boolean
847
+	 * @throws EE_Error
848
+	 * @throws InvalidArgumentException
849
+	 * @throws InvalidDataTypeException
850
+	 * @throws InvalidInterfaceException
851
+	 * @throws ReflectionException
852
+	 * @since 4.9.80.p
853
+	 */
854
+	public function increaseSold($qty = 1)
855
+	{
856
+		$qty = absint($qty);
857
+		// increment sold and decrement reserved datetime quantities simultaneously
858
+		// don't worry about failures, because they must have already had a spot reserved
859
+		$this->increaseSoldForDatetimes($qty);
860
+		// Increment and decrement ticket quantities simultaneously
861
+		$success = $this->adjustNumericFieldsInDb(
862
+			[
863
+				'TKT_reserved' => $qty * -1,
864
+				'TKT_sold'     => $qty,
865
+			]
866
+		);
867
+		do_action(
868
+			'AHEE__EE_Ticket__increase_sold',
869
+			$this,
870
+			$qty,
871
+			$this->sold(),
872
+			$success
873
+		);
874
+		return $success;
875
+	}
876
+
877
+
878
+	/**
879
+	 * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty.
880
+	 *
881
+	 * @param int           $qty positive or negative. Positive means to increase sold counts (and decrease reserved
882
+	 *                           counts), Negative means to decreases old counts (and increase reserved counts).
883
+	 * @param EE_Datetime[] $datetimes
884
+	 * @throws EE_Error
885
+	 * @throws InvalidArgumentException
886
+	 * @throws InvalidDataTypeException
887
+	 * @throws InvalidInterfaceException
888
+	 * @throws ReflectionException
889
+	 * @since 4.9.80.p
890
+	 */
891
+	protected function increaseSoldForDatetimes($qty, array $datetimes = [])
892
+	{
893
+		$datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
894
+		foreach ($datetimes as $datetime) {
895
+			$datetime->increaseSold($qty);
896
+		}
897
+	}
898
+
899
+
900
+	/**
901
+	 * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the
902
+	 * DB and then updates the model objects.
903
+	 * Does not affect the reserved counts.
904
+	 *
905
+	 * @param int $qty
906
+	 * @return boolean
907
+	 * @throws EE_Error
908
+	 * @throws InvalidArgumentException
909
+	 * @throws InvalidDataTypeException
910
+	 * @throws InvalidInterfaceException
911
+	 * @throws ReflectionException
912
+	 * @since 4.9.80.p
913
+	 */
914
+	public function decreaseSold($qty = 1)
915
+	{
916
+		$qty = absint($qty);
917
+		$this->decreaseSoldForDatetimes($qty);
918
+		$success = $this->adjustNumericFieldsInDb(
919
+			[
920
+				'TKT_sold' => $qty * -1,
921
+			]
922
+		);
923
+		do_action(
924
+			'AHEE__EE_Ticket__decrease_sold',
925
+			$this,
926
+			$qty,
927
+			$this->sold(),
928
+			$success
929
+		);
930
+		return $success;
931
+	}
932
+
933
+
934
+	/**
935
+	 * Decreases sold on related datetimes
936
+	 *
937
+	 * @param int           $qty
938
+	 * @param EE_Datetime[] $datetimes
939
+	 * @return void
940
+	 * @throws EE_Error
941
+	 * @throws InvalidArgumentException
942
+	 * @throws InvalidDataTypeException
943
+	 * @throws InvalidInterfaceException
944
+	 * @throws ReflectionException
945
+	 * @since 4.9.80.p
946
+	 */
947
+	protected function decreaseSoldForDatetimes($qty = 1, array $datetimes = [])
948
+	{
949
+		$datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
950
+		if (is_array($datetimes)) {
951
+			foreach ($datetimes as $datetime) {
952
+				if ($datetime instanceof EE_Datetime) {
953
+					$datetime->decreaseSold($qty);
954
+				}
955
+			}
956
+		}
957
+	}
958
+
959
+
960
+	/**
961
+	 * Gets qty of reserved tickets
962
+	 *
963
+	 * @return int
964
+	 * @throws EE_Error
965
+	 * @throws ReflectionException
966
+	 */
967
+	public function reserved()
968
+	{
969
+		return $this->get_raw('TKT_reserved');
970
+	}
971
+
972
+
973
+	/**
974
+	 * Sets reserved
975
+	 *
976
+	 * @param int $reserved
977
+	 * @return void
978
+	 * @throws EE_Error
979
+	 * @throws ReflectionException
980
+	 */
981
+	public function set_reserved($reserved)
982
+	{
983
+		// reserved can not go below zero
984
+		$reserved = max(0, (int) $reserved);
985
+		$this->set('TKT_reserved', $reserved);
986
+	}
987
+
988
+
989
+	/**
990
+	 * Increments reserved by amount passed by $qty, and persists it immediately to the database.
991
+	 *
992
+	 * @param int    $qty
993
+	 * @param string $source
994
+	 * @return bool whether we successfully reserved the ticket or not.
995
+	 * @throws EE_Error
996
+	 * @throws InvalidArgumentException
997
+	 * @throws ReflectionException
998
+	 * @throws InvalidDataTypeException
999
+	 * @throws InvalidInterfaceException
1000
+	 * @since 4.9.80.p
1001
+	 */
1002
+	public function increaseReserved($qty = 1, $source = 'unknown')
1003
+	{
1004
+		$qty = absint($qty);
1005
+		do_action(
1006
+			'AHEE__EE_Ticket__increase_reserved__begin',
1007
+			$this,
1008
+			$qty,
1009
+			$source
1010
+		);
1011
+		$this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "{$qty} from {$source}");
1012
+		$success                         = false;
1013
+		$datetimes_adjusted_successfully = $this->increaseReservedForDatetimes($qty);
1014
+		if ($datetimes_adjusted_successfully) {
1015
+			$success = $this->incrementFieldConditionallyInDb(
1016
+				'TKT_reserved',
1017
+				'TKT_sold',
1018
+				'TKT_qty',
1019
+				$qty
1020
+			);
1021
+			if (! $success) {
1022
+				// The datetimes were successfully bumped, but not the
1023
+				// ticket. So we need to manually rollback the datetimes.
1024
+				$this->decreaseReservedForDatetimes($qty);
1025
+			}
1026
+		}
1027
+		do_action(
1028
+			'AHEE__EE_Ticket__increase_reserved',
1029
+			$this,
1030
+			$qty,
1031
+			$this->reserved(),
1032
+			$success
1033
+		);
1034
+		return $success;
1035
+	}
1036
+
1037
+
1038
+	/**
1039
+	 * Increases reserved counts on related datetimes
1040
+	 *
1041
+	 * @param int           $qty
1042
+	 * @param EE_Datetime[] $datetimes
1043
+	 * @return boolean indicating success
1044
+	 * @throws EE_Error
1045
+	 * @throws InvalidArgumentException
1046
+	 * @throws InvalidDataTypeException
1047
+	 * @throws InvalidInterfaceException
1048
+	 * @throws ReflectionException
1049
+	 * @since 4.9.80.p
1050
+	 */
1051
+	protected function increaseReservedForDatetimes($qty = 1, array $datetimes = [])
1052
+	{
1053
+		$datetimes         = ! empty($datetimes) ? $datetimes : $this->datetimes();
1054
+		$datetimes_updated = [];
1055
+		$limit_exceeded    = false;
1056
+		if (is_array($datetimes)) {
1057
+			foreach ($datetimes as $datetime) {
1058
+				if ($datetime instanceof EE_Datetime) {
1059
+					if ($datetime->increaseReserved($qty)) {
1060
+						$datetimes_updated[] = $datetime;
1061
+					} else {
1062
+						$limit_exceeded = true;
1063
+						break;
1064
+					}
1065
+				}
1066
+			}
1067
+			// If somewhere along the way we detected a datetime whose
1068
+			// limit was exceeded, do a manual rollback.
1069
+			if ($limit_exceeded) {
1070
+				$this->decreaseReservedForDatetimes($qty, $datetimes_updated);
1071
+				return false;
1072
+			}
1073
+		}
1074
+		return true;
1075
+	}
1076
+
1077
+
1078
+	/**
1079
+	 * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database.
1080
+	 *
1081
+	 * @param int    $qty
1082
+	 * @param bool   $adjust_datetimes
1083
+	 * @param string $source
1084
+	 * @return boolean
1085
+	 * @throws EE_Error
1086
+	 * @throws InvalidArgumentException
1087
+	 * @throws ReflectionException
1088
+	 * @throws InvalidDataTypeException
1089
+	 * @throws InvalidInterfaceException
1090
+	 * @since 4.9.80.p
1091
+	 */
1092
+	public function decreaseReserved($qty = 1, $adjust_datetimes = true, $source = 'unknown')
1093
+	{
1094
+		$qty = absint($qty);
1095
+		$this->add_extra_meta(EE_Ticket::META_KEY_TICKET_RESERVATIONS, "-{$qty} from {$source}");
1096
+		if ($adjust_datetimes) {
1097
+			$this->decreaseReservedForDatetimes($qty);
1098
+		}
1099
+		$success = $this->adjustNumericFieldsInDb(
1100
+			[
1101
+				'TKT_reserved' => $qty * -1,
1102
+			]
1103
+		);
1104
+		do_action(
1105
+			'AHEE__EE_Ticket__decrease_reserved',
1106
+			$this,
1107
+			$qty,
1108
+			$this->reserved(),
1109
+			$success
1110
+		);
1111
+		return $success;
1112
+	}
1113
+
1114
+
1115
+	/**
1116
+	 * Decreases the reserved count on the specified datetimes.
1117
+	 *
1118
+	 * @param int           $qty
1119
+	 * @param EE_Datetime[] $datetimes
1120
+	 * @throws EE_Error
1121
+	 * @throws InvalidArgumentException
1122
+	 * @throws ReflectionException
1123
+	 * @throws InvalidDataTypeException
1124
+	 * @throws InvalidInterfaceException
1125
+	 * @since 4.9.80.p
1126
+	 */
1127
+	protected function decreaseReservedForDatetimes($qty = 1, array $datetimes = [])
1128
+	{
1129
+		$datetimes = ! empty($datetimes) ? $datetimes : $this->datetimes();
1130
+		foreach ($datetimes as $datetime) {
1131
+			if ($datetime instanceof EE_Datetime) {
1132
+				$datetime->decreaseReserved($qty);
1133
+			}
1134
+		}
1135
+	}
1136
+
1137
+
1138
+	/**
1139
+	 * Gets ticket quantity
1140
+	 *
1141
+	 * @param string $context     ticket quantity is somewhat subjective depending on the exact information sought
1142
+	 *                            therefore $context can be one of three values: '', 'reg_limit', or 'saleable'
1143
+	 *                            '' (default) quantity is the actual db value for TKT_qty, unaffected by other objects
1144
+	 *                            REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes
1145
+	 *                            SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and
1146
+	 *                            is therefore the truest measure of tickets that can be purchased at the moment
1147
+	 * @return int
1148
+	 * @throws EE_Error
1149
+	 * @throws ReflectionException
1150
+	 */
1151
+	public function qty($context = '')
1152
+	{
1153
+		switch ($context) {
1154
+			case 'reg_limit':
1155
+				return $this->real_quantity_on_ticket();
1156
+			case 'saleable':
1157
+				return $this->real_quantity_on_ticket('saleable');
1158
+			default:
1159
+				return $this->get_raw('TKT_qty');
1160
+		}
1161
+	}
1162
+
1163
+
1164
+	/**
1165
+	 * Gets ticket quantity
1166
+	 *
1167
+	 * @param string $context     ticket quantity is somewhat subjective depending on the exact information sought
1168
+	 *                            therefore $context can be one of two values: 'reg_limit', or 'saleable'
1169
+	 *                            REG LIMIT: caps qty based on DTT_reg_limit for ALL related datetimes
1170
+	 *                            SALEABLE: also considers datetime sold and returns zero if ANY DTT is sold out, and
1171
+	 *                            is therefore the truest measure of tickets that can be purchased at the moment
1172
+	 * @param int    $DTT_ID      the primary key for a particular datetime.
1173
+	 *                            set to 0 for all related datetimes
1174
+	 * @return int
1175
+	 * @throws EE_Error
1176
+	 * @throws ReflectionException
1177
+	 */
1178
+	public function real_quantity_on_ticket($context = 'reg_limit', $DTT_ID = 0)
1179
+	{
1180
+		$raw = $this->get_raw('TKT_qty');
1181
+		// return immediately if it's zero
1182
+		if ($raw === 0) {
1183
+			return $raw;
1184
+		}
1185
+		// echo "\n\n<br />Ticket: " . $this->name() . '<br />';
1186
+		// ensure qty doesn't exceed raw value for THIS ticket
1187
+		$qty = min(EE_INF, $raw);
1188
+		// echo "\n . qty: " . $qty . '<br />';
1189
+		// calculate this ticket's total sales and reservations
1190
+		$sold_and_reserved_for_this_ticket = $this->sold() + $this->reserved();
1191
+		// echo "\n . sold: " . $this->sold() . '<br />';
1192
+		// echo "\n . reserved: " . $this->reserved() . '<br />';
1193
+		// echo "\n . sold_and_reserved_for_this_ticket: " . $sold_and_reserved_for_this_ticket . '<br />';
1194
+		// first we need to calculate the maximum number of tickets available for the datetime
1195
+		// do we want data for one datetime or all of them ?
1196
+		$query_params = $DTT_ID ? [['DTT_ID' => $DTT_ID]] : [];
1197
+		$datetimes    = $this->datetimes($query_params);
1198
+		if (is_array($datetimes) && ! empty($datetimes)) {
1199
+			foreach ($datetimes as $datetime) {
1200
+				if ($datetime instanceof EE_Datetime) {
1201
+					$datetime->refresh_from_db();
1202
+					// echo "\n . . datetime name: " . $datetime->name() . '<br />';
1203
+					// echo "\n . . datetime ID: " . $datetime->ID() . '<br />';
1204
+					// initialize with no restrictions for each datetime
1205
+					// but adjust datetime qty based on datetime reg limit
1206
+					$datetime_qty = min(EE_INF, $datetime->reg_limit());
1207
+					// echo "\n . . . datetime reg_limit: " . $datetime->reg_limit() . '<br />';
1208
+					// echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1209
+					// if we want the actual saleable amount, then we need to consider OTHER ticket sales
1210
+					// and reservations for this datetime, that do NOT include sales and reservations
1211
+					// for this ticket (so we add $this->sold() and $this->reserved() back in)
1212
+					if ($context === 'saleable') {
1213
+						$datetime_qty = max(
1214
+							$datetime_qty - $datetime->sold_and_reserved() + $sold_and_reserved_for_this_ticket,
1215
+							0
1216
+						);
1217
+						// echo "\n . . . datetime sold: " . $datetime->sold() . '<br />';
1218
+						// echo "\n . . . datetime reserved: " . $datetime->reserved() . '<br />';
1219
+						// echo "\n . . . datetime sold_and_reserved: " . $datetime->sold_and_reserved() . '<br />';
1220
+						// echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1221
+						$datetime_qty = ! $datetime->sold_out() ? $datetime_qty : 0;
1222
+						// echo "\n . . . datetime_qty: " . $datetime_qty . '<br />';
1223
+					}
1224
+					$qty = min($datetime_qty, $qty);
1225
+					// echo "\n . . qty: " . $qty . '<br />';
1226
+				}
1227
+			}
1228
+		}
1229
+		// NOW that we know the  maximum number of tickets available for the datetime
1230
+		// we can finally factor in the details for this specific ticket
1231
+		if ($qty > 0 && $context === 'saleable') {
1232
+			// and subtract the sales for THIS ticket
1233
+			$qty = max($qty - $sold_and_reserved_for_this_ticket, 0);
1234
+			// echo "\n . qty: " . $qty . '<br />';
1235
+		}
1236
+		// echo "\nFINAL QTY: " . $qty . "<br /><br />";
1237
+		return $qty;
1238
+	}
1239
+
1240
+
1241
+	/**
1242
+	 * Sets qty - IMPORTANT!!! Does NOT allow QTY to be set higher than the lowest reg limit of any related datetimes
1243
+	 *
1244
+	 * @param int $qty
1245
+	 * @return void
1246
+	 * @throws EE_Error
1247
+	 * @throws ReflectionException
1248
+	 */
1249
+	public function set_qty($qty)
1250
+	{
1251
+		$datetimes = $this->datetimes();
1252
+		foreach ($datetimes as $datetime) {
1253
+			if ($datetime instanceof EE_Datetime) {
1254
+				$qty = min($qty, $datetime->reg_limit());
1255
+			}
1256
+		}
1257
+		$this->set('TKT_qty', $qty);
1258
+	}
1259
+
1260
+
1261
+	/**
1262
+	 * Gets uses
1263
+	 *
1264
+	 * @return int
1265
+	 * @throws EE_Error
1266
+	 * @throws ReflectionException
1267
+	 */
1268
+	public function uses()
1269
+	{
1270
+		return $this->get('TKT_uses');
1271
+	}
1272
+
1273
+
1274
+	/**
1275
+	 * Sets uses
1276
+	 *
1277
+	 * @param int $uses
1278
+	 * @return void
1279
+	 * @throws EE_Error
1280
+	 * @throws ReflectionException
1281
+	 */
1282
+	public function set_uses($uses)
1283
+	{
1284
+		$this->set('TKT_uses', $uses);
1285
+	}
1286
+
1287
+
1288
+	/**
1289
+	 * returns whether ticket is required or not.
1290
+	 *
1291
+	 * @return boolean
1292
+	 * @throws EE_Error
1293
+	 * @throws ReflectionException
1294
+	 */
1295
+	public function required()
1296
+	{
1297
+		return $this->get('TKT_required');
1298
+	}
1299
+
1300
+
1301
+	/**
1302
+	 * sets the TKT_required property
1303
+	 *
1304
+	 * @param boolean $required
1305
+	 * @return void
1306
+	 * @throws EE_Error
1307
+	 * @throws ReflectionException
1308
+	 */
1309
+	public function set_required($required)
1310
+	{
1311
+		$this->set('TKT_required', $required);
1312
+	}
1313
+
1314
+
1315
+	/**
1316
+	 * Gets taxable
1317
+	 *
1318
+	 * @return boolean
1319
+	 * @throws EE_Error
1320
+	 * @throws ReflectionException
1321
+	 */
1322
+	public function taxable()
1323
+	{
1324
+		return $this->get('TKT_taxable');
1325
+	}
1326
+
1327
+
1328
+	/**
1329
+	 * Sets taxable
1330
+	 *
1331
+	 * @param boolean $taxable
1332
+	 * @return void
1333
+	 * @throws EE_Error
1334
+	 * @throws ReflectionException
1335
+	 */
1336
+	public function set_taxable($taxable)
1337
+	{
1338
+		$this->set('TKT_taxable', $taxable);
1339
+	}
1340
+
1341
+
1342
+	/**
1343
+	 * Gets is_default
1344
+	 *
1345
+	 * @return boolean
1346
+	 * @throws EE_Error
1347
+	 * @throws ReflectionException
1348
+	 */
1349
+	public function is_default()
1350
+	{
1351
+		return $this->get('TKT_is_default');
1352
+	}
1353
+
1354
+
1355
+	/**
1356
+	 * Sets is_default
1357
+	 *
1358
+	 * @param boolean $is_default
1359
+	 * @return void
1360
+	 * @throws EE_Error
1361
+	 * @throws ReflectionException
1362
+	 */
1363
+	public function set_is_default($is_default)
1364
+	{
1365
+		$this->set('TKT_is_default', $is_default);
1366
+	}
1367
+
1368
+
1369
+	/**
1370
+	 * Gets order
1371
+	 *
1372
+	 * @return int
1373
+	 * @throws EE_Error
1374
+	 * @throws ReflectionException
1375
+	 */
1376
+	public function order()
1377
+	{
1378
+		return $this->get('TKT_order');
1379
+	}
1380
+
1381
+
1382
+	/**
1383
+	 * Sets order
1384
+	 *
1385
+	 * @param int $order
1386
+	 * @return void
1387
+	 * @throws EE_Error
1388
+	 * @throws ReflectionException
1389
+	 */
1390
+	public function set_order($order)
1391
+	{
1392
+		$this->set('TKT_order', $order);
1393
+	}
1394
+
1395
+
1396
+	/**
1397
+	 * Gets row
1398
+	 *
1399
+	 * @return int
1400
+	 * @throws EE_Error
1401
+	 * @throws ReflectionException
1402
+	 */
1403
+	public function row()
1404
+	{
1405
+		return $this->get('TKT_row');
1406
+	}
1407
+
1408
+
1409
+	/**
1410
+	 * Sets row
1411
+	 *
1412
+	 * @param int $row
1413
+	 * @return void
1414
+	 * @throws EE_Error
1415
+	 * @throws ReflectionException
1416
+	 */
1417
+	public function set_row($row)
1418
+	{
1419
+		$this->set('TKT_row', $row);
1420
+	}
1421
+
1422
+
1423
+	/**
1424
+	 * Gets deleted
1425
+	 *
1426
+	 * @return boolean
1427
+	 * @throws EE_Error
1428
+	 * @throws ReflectionException
1429
+	 */
1430
+	public function deleted()
1431
+	{
1432
+		return $this->get('TKT_deleted');
1433
+	}
1434
+
1435
+
1436
+	/**
1437
+	 * Sets deleted
1438
+	 *
1439
+	 * @param boolean $deleted
1440
+	 * @return void
1441
+	 * @throws EE_Error
1442
+	 * @throws ReflectionException
1443
+	 */
1444
+	public function set_deleted($deleted)
1445
+	{
1446
+		$this->set('TKT_deleted', $deleted);
1447
+	}
1448
+
1449
+
1450
+	/**
1451
+	 * Gets parent
1452
+	 *
1453
+	 * @return int
1454
+	 * @throws EE_Error
1455
+	 * @throws ReflectionException
1456
+	 */
1457
+	public function parent_ID()
1458
+	{
1459
+		return $this->get('TKT_parent');
1460
+	}
1461
+
1462
+
1463
+	/**
1464
+	 * Sets parent
1465
+	 *
1466
+	 * @param int $parent
1467
+	 * @return void
1468
+	 * @throws EE_Error
1469
+	 * @throws ReflectionException
1470
+	 */
1471
+	public function set_parent_ID($parent)
1472
+	{
1473
+		$this->set('TKT_parent', $parent);
1474
+	}
1475
+
1476
+
1477
+	/**
1478
+	 * @return boolean
1479
+	 * @throws EE_Error
1480
+	 * @throws InvalidArgumentException
1481
+	 * @throws InvalidDataTypeException
1482
+	 * @throws InvalidInterfaceException
1483
+	 * @throws ReflectionException
1484
+	 */
1485
+	public function reverse_calculate()
1486
+	{
1487
+		return $this->get('TKT_reverse_calculate');
1488
+	}
1489
+
1490
+
1491
+	/**
1492
+	 * @param boolean $reverse_calculate
1493
+	 * @throws EE_Error
1494
+	 * @throws InvalidArgumentException
1495
+	 * @throws InvalidDataTypeException
1496
+	 * @throws InvalidInterfaceException
1497
+	 * @throws ReflectionException
1498
+	 */
1499
+	public function set_reverse_calculate($reverse_calculate)
1500
+	{
1501
+		$this->set('TKT_reverse_calculate', $reverse_calculate);
1502
+	}
1503
+
1504
+
1505
+	/**
1506
+	 * Gets a string which is handy for showing in gateways etc that describes the ticket.
1507
+	 *
1508
+	 * @return string
1509
+	 * @throws EE_Error
1510
+	 * @throws ReflectionException
1511
+	 */
1512
+	public function name_and_info()
1513
+	{
1514
+		$times = [];
1515
+		foreach ($this->datetimes() as $datetime) {
1516
+			$times[] = $datetime->start_date_and_time();
1517
+		}
1518
+		return $this->name() . ' @ ' . implode(', ', $times) . ' for ' . $this->pretty_price();
1519
+	}
1520
+
1521
+
1522
+	/**
1523
+	 * Gets name
1524
+	 *
1525
+	 * @return string
1526
+	 * @throws EE_Error
1527
+	 * @throws ReflectionException
1528
+	 */
1529
+	public function name()
1530
+	{
1531
+		return $this->get('TKT_name');
1532
+	}
1533
+
1534
+
1535
+	/**
1536
+	 * Gets price
1537
+	 *
1538
+	 * @return float
1539
+	 * @throws EE_Error
1540
+	 * @throws ReflectionException
1541
+	 */
1542
+	public function price()
1543
+	{
1544
+		return $this->get('TKT_price');
1545
+	}
1546
+
1547
+
1548
+	/**
1549
+	 * Gets all the registrations for this ticket
1550
+	 *
1551
+	 * @param array $query_params @see
1552
+	 *                            https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1553
+	 * @return EE_Registration[]|EE_Base_Class[]
1554
+	 * @throws EE_Error
1555
+	 * @throws ReflectionException
1556
+	 */
1557
+	public function registrations($query_params = [])
1558
+	{
1559
+		return $this->get_many_related('Registration', $query_params);
1560
+	}
1561
+
1562
+
1563
+	/**
1564
+	 * Updates the TKT_sold attribute (and saves) based on the number of APPROVED registrations for this ticket.
1565
+	 *
1566
+	 * @return int
1567
+	 * @throws EE_Error
1568
+	 * @throws ReflectionException
1569
+	 */
1570
+	public function update_tickets_sold()
1571
+	{
1572
+		$count_regs_for_this_ticket = $this->count_registrations(
1573
+			[
1574
+				[
1575
+					'STS_ID'      => EEM_Registration::status_id_approved,
1576
+					'REG_deleted' => 0,
1577
+				],
1578
+			]
1579
+		);
1580
+		$this->set_sold($count_regs_for_this_ticket);
1581
+		$this->save();
1582
+		return $count_regs_for_this_ticket;
1583
+	}
1584
+
1585
+
1586
+	/**
1587
+	 * Counts the registrations for this ticket
1588
+	 *
1589
+	 * @param array $query_params @see
1590
+	 *                            https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md
1591
+	 * @return int
1592
+	 * @throws EE_Error
1593
+	 * @throws ReflectionException
1594
+	 */
1595
+	public function count_registrations($query_params = [])
1596
+	{
1597
+		return $this->count_related('Registration', $query_params);
1598
+	}
1599
+
1600
+
1601
+	/**
1602
+	 * Implementation for EEI_Has_Icon interface method.
1603
+	 *
1604
+	 * @return string
1605
+	 * @see EEI_Visual_Representation for comments
1606
+	 */
1607
+	public function get_icon()
1608
+	{
1609
+		return '<span class="dashicons dashicons-tickets-alt"/>';
1610
+	}
1611
+
1612
+
1613
+	/**
1614
+	 * Implementation of the EEI_Event_Relation interface method
1615
+	 *
1616
+	 * @return EE_Event
1617
+	 * @throws EE_Error
1618
+	 * @throws UnexpectedEntityException
1619
+	 * @throws ReflectionException
1620
+	 * @see EEI_Event_Relation for comments
1621
+	 */
1622
+	public function get_related_event()
1623
+	{
1624
+		// get one datetime to use for getting the event
1625
+		$datetime = $this->first_datetime();
1626
+		if (! $datetime instanceof EE_Datetime) {
1627
+			throw new UnexpectedEntityException(
1628
+				$datetime,
1629
+				'EE_Datetime',
1630
+				sprintf(
1631
+					__('The ticket (%s) is not associated with any valid datetimes.', 'event_espresso'),
1632
+					$this->name()
1633
+				)
1634
+			);
1635
+		}
1636
+		$event = $datetime->event();
1637
+		if (! $event instanceof EE_Event) {
1638
+			throw new UnexpectedEntityException(
1639
+				$event,
1640
+				'EE_Event',
1641
+				sprintf(
1642
+					__('The ticket (%s) is not associated with a valid event.', 'event_espresso'),
1643
+					$this->name()
1644
+				)
1645
+			);
1646
+		}
1647
+		return $event;
1648
+	}
1649
+
1650
+
1651
+	/**
1652
+	 * Implementation of the EEI_Event_Relation interface method
1653
+	 *
1654
+	 * @return string
1655
+	 * @throws UnexpectedEntityException
1656
+	 * @throws EE_Error
1657
+	 * @throws ReflectionException
1658
+	 * @see EEI_Event_Relation for comments
1659
+	 */
1660
+	public function get_event_name()
1661
+	{
1662
+		$event = $this->get_related_event();
1663
+		return $event instanceof EE_Event ? $event->name() : '';
1664
+	}
1665
+
1666
+
1667
+	/**
1668
+	 * Implementation of the EEI_Event_Relation interface method
1669
+	 *
1670
+	 * @return int
1671
+	 * @throws UnexpectedEntityException
1672
+	 * @throws EE_Error
1673
+	 * @throws ReflectionException
1674
+	 * @see EEI_Event_Relation for comments
1675
+	 */
1676
+	public function get_event_ID()
1677
+	{
1678
+		$event = $this->get_related_event();
1679
+		return $event instanceof EE_Event ? $event->ID() : 0;
1680
+	}
1681
+
1682
+
1683
+	/**
1684
+	 * This simply returns whether a ticket can be permanently deleted or not.
1685
+	 * The criteria for determining this is whether the ticket has any related registrations.
1686
+	 * If there are none then it can be permanently deleted.
1687
+	 *
1688
+	 * @return bool
1689
+	 * @throws EE_Error
1690
+	 * @throws ReflectionException
1691
+	 */
1692
+	public function is_permanently_deleteable()
1693
+	{
1694
+		return $this->count_registrations() === 0;
1695
+	}
1696
+
1697
+
1698
+	/**
1699
+	 * @return int
1700
+	 * @throws EE_Error
1701
+	 * @throws ReflectionException
1702
+	 * @since   $VID:$
1703
+	 */
1704
+	public function visibility(): int
1705
+	{
1706
+		return $this->get('TKT_visibility');
1707
+	}
1708
+
1709
+
1710
+	/**
1711
+	 * @return int
1712
+	 * @throws EE_Error
1713
+	 * @throws ReflectionException
1714
+	 * @since   $VID:$
1715
+	 */
1716
+	public function isHidden(): int
1717
+	{
1718
+		return $this->visibility() === EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE;
1719
+	}
1720
+
1721
+
1722
+	/**
1723
+	 * @return int
1724
+	 * @throws EE_Error
1725
+	 * @throws ReflectionException
1726
+	 * @since   $VID:$
1727
+	 */
1728
+	public function isNotHidden(): int
1729
+	{
1730
+		return $this->visibility() > EEM_Ticket::TICKET_VISIBILITY_NONE_VALUE;
1731
+	}
1732
+
1733
+
1734
+	/**
1735
+	 * @return int
1736
+	 * @throws EE_Error
1737
+	 * @throws ReflectionException
1738
+	 * @since   $VID:$
1739
+	 */
1740
+	public function isPublicOnly(): int
1741
+	{
1742
+		return $this->isNotHidden() && $this->visibility() <= EEM_Ticket::TICKET_VISIBILITY_PUBLIC_VALUE;
1743
+	}
1744
+
1745
+
1746
+	/**
1747
+	 * @return int
1748
+	 * @throws EE_Error
1749
+	 * @throws ReflectionException
1750
+	 * @since   $VID:$
1751
+	 */
1752
+	public function isMembersOnly(): int
1753
+	{
1754
+		return $this->visibility() > EEM_Ticket::TICKET_VISIBILITY_PUBLIC_VALUE
1755
+			   && $this->visibility() <= EEM_Ticket::TICKET_VISIBILITY_MEMBERS_ONLY_VALUE;
1756
+	}
1757
+
1758
+
1759
+	/**
1760
+	 * @return int
1761
+	 * @throws EE_Error
1762
+	 * @throws ReflectionException
1763
+	 * @since   $VID:$
1764
+	 */
1765
+	public function isAdminsOnly(): int
1766
+	{
1767
+		return $this->visibility() > EEM_Ticket::TICKET_VISIBILITY_MEMBERS_ONLY_VALUE
1768
+			   && $this->visibility() <= EEM_Ticket::TICKET_VISIBILITY_ADMINS_ONLY_VALUE;
1769
+	}
1770
+
1771
+
1772
+	/**
1773
+	 * @return int
1774
+	 * @throws EE_Error
1775
+	 * @throws ReflectionException
1776
+	 * @since   $VID:$
1777
+	 */
1778
+	public function isAdminUiOnly(): int
1779
+	{
1780
+		return $this->visibility() > EEM_Ticket::TICKET_VISIBILITY_ADMINS_ONLY_VALUE
1781
+			   && $this->visibility() <= EEM_Ticket::TICKET_VISIBILITY_ADMIN_UI_ONLY_VALUE;
1782
+	}
1783
+
1784
+
1785
+	/**
1786
+	 * @param int $visibility
1787
+	 * @throws EE_Error
1788
+	 * @throws ReflectionException
1789
+	 * @since   $VID:$
1790
+	 */
1791
+	public function set_visibility(int $visibility)
1792
+	{
1793
+
1794
+		$ticket_visibility_options = $this->_model->ticketVisibilityOptions();
1795
+		$ticket_visibility         = -1;
1796
+		foreach ($ticket_visibility_options as $ticket_visibility_option) {
1797
+			if ($visibility === $ticket_visibility_option) {
1798
+				$ticket_visibility = $visibility;
1799
+			}
1800
+		}
1801
+		if ($ticket_visibility === -1) {
1802
+			throw new DomainException(
1803
+				sprintf(
1804
+					esc_html__(
1805
+						'The supplied ticket visibility setting of "%1$s" is not valid. It needs to match one of the keys in the following array:%2$s %3$s ',
1806
+						'event_espresso'
1807
+					),
1808
+					$visibility,
1809
+					'<br />',
1810
+					var_export($ticket_visibility_options, true)
1811
+				)
1812
+			);
1813
+		}
1814
+		$this->set('TKT_visibility', $ticket_visibility);
1815
+	}
1816
+
1817
+
1818
+	/*******************************************************************
1819 1819
      ***********************  DEPRECATED METHODS  **********************
1820 1820
      *******************************************************************/
1821 1821
 
1822 1822
 
1823
-    /**
1824
-     * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its
1825
-     * associated datetimes.
1826
-     *
1827
-     * @param int $qty
1828
-     * @return void
1829
-     * @throws EE_Error
1830
-     * @throws InvalidArgumentException
1831
-     * @throws InvalidDataTypeException
1832
-     * @throws InvalidInterfaceException
1833
-     * @throws ReflectionException
1834
-     * @deprecated 4.9.80.p
1835
-     */
1836
-    public function increase_sold($qty = 1)
1837
-    {
1838
-        EE_Error::doing_it_wrong(
1839
-            __FUNCTION__,
1840
-            esc_html__('Please use EE_Ticket::increaseSold() instead', 'event_espresso'),
1841
-            '4.9.80.p',
1842
-            '5.0.0.p'
1843
-        );
1844
-        $this->increaseSold($qty);
1845
-    }
1846
-
1847
-
1848
-    /**
1849
-     * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty.
1850
-     *
1851
-     * @param int $qty positive or negative. Positive means to increase sold counts (and decrease reserved counts),
1852
-     *                 Negative means to decreases old counts (and increase reserved counts).
1853
-     * @throws EE_Error
1854
-     * @throws InvalidArgumentException
1855
-     * @throws InvalidDataTypeException
1856
-     * @throws InvalidInterfaceException
1857
-     * @throws ReflectionException
1858
-     * @deprecated 4.9.80.p
1859
-     */
1860
-    protected function _increase_sold_for_datetimes($qty)
1861
-    {
1862
-        EE_Error::doing_it_wrong(
1863
-            __FUNCTION__,
1864
-            esc_html__('Please use EE_Ticket::increaseSoldForDatetimes() instead', 'event_espresso'),
1865
-            '4.9.80.p',
1866
-            '5.0.0.p'
1867
-        );
1868
-        $this->increaseSoldForDatetimes($qty);
1869
-    }
1870
-
1871
-
1872
-    /**
1873
-     * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the
1874
-     * DB and then updates the model objects.
1875
-     * Does not affect the reserved counts.
1876
-     *
1877
-     * @param int $qty
1878
-     * @return void
1879
-     * @throws EE_Error
1880
-     * @throws InvalidArgumentException
1881
-     * @throws InvalidDataTypeException
1882
-     * @throws InvalidInterfaceException
1883
-     * @throws ReflectionException
1884
-     * @deprecated 4.9.80.p
1885
-     */
1886
-    public function decrease_sold($qty = 1)
1887
-    {
1888
-        EE_Error::doing_it_wrong(
1889
-            __FUNCTION__,
1890
-            esc_html__('Please use EE_Ticket::decreaseSold() instead', 'event_espresso'),
1891
-            '4.9.80.p',
1892
-            '5.0.0.p'
1893
-        );
1894
-        $this->decreaseSold($qty);
1895
-    }
1896
-
1897
-
1898
-    /**
1899
-     * Decreases sold on related datetimes
1900
-     *
1901
-     * @param int $qty
1902
-     * @return void
1903
-     * @throws EE_Error
1904
-     * @throws InvalidArgumentException
1905
-     * @throws InvalidDataTypeException
1906
-     * @throws InvalidInterfaceException
1907
-     * @throws ReflectionException
1908
-     * @deprecated 4.9.80.p
1909
-     */
1910
-    protected function _decrease_sold_for_datetimes($qty = 1)
1911
-    {
1912
-        EE_Error::doing_it_wrong(
1913
-            __FUNCTION__,
1914
-            esc_html__('Please use EE_Ticket::decreaseSoldForDatetimes() instead', 'event_espresso'),
1915
-            '4.9.80.p',
1916
-            '5.0.0.p'
1917
-        );
1918
-        $this->decreaseSoldForDatetimes($qty);
1919
-    }
1920
-
1921
-
1922
-    /**
1923
-     * Increments reserved by amount passed by $qty, and persists it immediately to the database.
1924
-     *
1925
-     * @param int    $qty
1926
-     * @param string $source
1927
-     * @return bool whether we successfully reserved the ticket or not.
1928
-     * @throws EE_Error
1929
-     * @throws InvalidArgumentException
1930
-     * @throws ReflectionException
1931
-     * @throws InvalidDataTypeException
1932
-     * @throws InvalidInterfaceException
1933
-     * @deprecated 4.9.80.p
1934
-     */
1935
-    public function increase_reserved($qty = 1, $source = 'unknown')
1936
-    {
1937
-        EE_Error::doing_it_wrong(
1938
-            __FUNCTION__,
1939
-            esc_html__('Please use EE_Ticket::increaseReserved() instead', 'event_espresso'),
1940
-            '4.9.80.p',
1941
-            '5.0.0.p'
1942
-        );
1943
-        return $this->increaseReserved($qty);
1944
-    }
1945
-
1946
-
1947
-    /**
1948
-     * Increases sold on related datetimes
1949
-     *
1950
-     * @param int $qty
1951
-     * @return boolean indicating success
1952
-     * @throws EE_Error
1953
-     * @throws InvalidArgumentException
1954
-     * @throws InvalidDataTypeException
1955
-     * @throws InvalidInterfaceException
1956
-     * @throws ReflectionException
1957
-     * @deprecated 4.9.80.p
1958
-     */
1959
-    protected function _increase_reserved_for_datetimes($qty = 1)
1960
-    {
1961
-        EE_Error::doing_it_wrong(
1962
-            __FUNCTION__,
1963
-            esc_html__('Please use EE_Ticket::increaseReservedForDatetimes() instead', 'event_espresso'),
1964
-            '4.9.80.p',
1965
-            '5.0.0.p'
1966
-        );
1967
-        return $this->increaseReservedForDatetimes($qty);
1968
-    }
1969
-
1970
-
1971
-    /**
1972
-     * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database.
1973
-     *
1974
-     * @param int    $qty
1975
-     * @param bool   $adjust_datetimes
1976
-     * @param string $source
1977
-     * @return void
1978
-     * @throws EE_Error
1979
-     * @throws InvalidArgumentException
1980
-     * @throws ReflectionException
1981
-     * @throws InvalidDataTypeException
1982
-     * @throws InvalidInterfaceException
1983
-     * @deprecated 4.9.80.p
1984
-     */
1985
-    public function decrease_reserved($qty = 1, $adjust_datetimes = true, $source = 'unknown')
1986
-    {
1987
-        EE_Error::doing_it_wrong(
1988
-            __FUNCTION__,
1989
-            esc_html__('Please use EE_Ticket::decreaseReserved() instead', 'event_espresso'),
1990
-            '4.9.80.p',
1991
-            '5.0.0.p'
1992
-        );
1993
-        $this->decreaseReserved($qty);
1994
-    }
1995
-
1996
-
1997
-    /**
1998
-     * Decreases reserved on related datetimes
1999
-     *
2000
-     * @param int $qty
2001
-     * @return void
2002
-     * @throws EE_Error
2003
-     * @throws InvalidArgumentException
2004
-     * @throws ReflectionException
2005
-     * @throws InvalidDataTypeException
2006
-     * @throws InvalidInterfaceException
2007
-     * @deprecated 4.9.80.p
2008
-     */
2009
-    protected function _decrease_reserved_for_datetimes($qty = 1)
2010
-    {
2011
-        EE_Error::doing_it_wrong(
2012
-            __FUNCTION__,
2013
-            esc_html__('Please use EE_Ticket::decreaseReservedForDatetimes() instead', 'event_espresso'),
2014
-            '4.9.80.p',
2015
-            '5.0.0.p'
2016
-        );
2017
-        $this->decreaseReservedForDatetimes($qty);
2018
-    }
1823
+	/**
1824
+	 * Increments sold by amount passed by $qty AND decrements the reserved count on both this ticket and its
1825
+	 * associated datetimes.
1826
+	 *
1827
+	 * @param int $qty
1828
+	 * @return void
1829
+	 * @throws EE_Error
1830
+	 * @throws InvalidArgumentException
1831
+	 * @throws InvalidDataTypeException
1832
+	 * @throws InvalidInterfaceException
1833
+	 * @throws ReflectionException
1834
+	 * @deprecated 4.9.80.p
1835
+	 */
1836
+	public function increase_sold($qty = 1)
1837
+	{
1838
+		EE_Error::doing_it_wrong(
1839
+			__FUNCTION__,
1840
+			esc_html__('Please use EE_Ticket::increaseSold() instead', 'event_espresso'),
1841
+			'4.9.80.p',
1842
+			'5.0.0.p'
1843
+		);
1844
+		$this->increaseSold($qty);
1845
+	}
1846
+
1847
+
1848
+	/**
1849
+	 * On each datetime related to this ticket, increases its sold count and decreases its reserved count by $qty.
1850
+	 *
1851
+	 * @param int $qty positive or negative. Positive means to increase sold counts (and decrease reserved counts),
1852
+	 *                 Negative means to decreases old counts (and increase reserved counts).
1853
+	 * @throws EE_Error
1854
+	 * @throws InvalidArgumentException
1855
+	 * @throws InvalidDataTypeException
1856
+	 * @throws InvalidInterfaceException
1857
+	 * @throws ReflectionException
1858
+	 * @deprecated 4.9.80.p
1859
+	 */
1860
+	protected function _increase_sold_for_datetimes($qty)
1861
+	{
1862
+		EE_Error::doing_it_wrong(
1863
+			__FUNCTION__,
1864
+			esc_html__('Please use EE_Ticket::increaseSoldForDatetimes() instead', 'event_espresso'),
1865
+			'4.9.80.p',
1866
+			'5.0.0.p'
1867
+		);
1868
+		$this->increaseSoldForDatetimes($qty);
1869
+	}
1870
+
1871
+
1872
+	/**
1873
+	 * Decrements (subtracts) sold by amount passed by $qty on both the ticket and its related datetimes directly in the
1874
+	 * DB and then updates the model objects.
1875
+	 * Does not affect the reserved counts.
1876
+	 *
1877
+	 * @param int $qty
1878
+	 * @return void
1879
+	 * @throws EE_Error
1880
+	 * @throws InvalidArgumentException
1881
+	 * @throws InvalidDataTypeException
1882
+	 * @throws InvalidInterfaceException
1883
+	 * @throws ReflectionException
1884
+	 * @deprecated 4.9.80.p
1885
+	 */
1886
+	public function decrease_sold($qty = 1)
1887
+	{
1888
+		EE_Error::doing_it_wrong(
1889
+			__FUNCTION__,
1890
+			esc_html__('Please use EE_Ticket::decreaseSold() instead', 'event_espresso'),
1891
+			'4.9.80.p',
1892
+			'5.0.0.p'
1893
+		);
1894
+		$this->decreaseSold($qty);
1895
+	}
1896
+
1897
+
1898
+	/**
1899
+	 * Decreases sold on related datetimes
1900
+	 *
1901
+	 * @param int $qty
1902
+	 * @return void
1903
+	 * @throws EE_Error
1904
+	 * @throws InvalidArgumentException
1905
+	 * @throws InvalidDataTypeException
1906
+	 * @throws InvalidInterfaceException
1907
+	 * @throws ReflectionException
1908
+	 * @deprecated 4.9.80.p
1909
+	 */
1910
+	protected function _decrease_sold_for_datetimes($qty = 1)
1911
+	{
1912
+		EE_Error::doing_it_wrong(
1913
+			__FUNCTION__,
1914
+			esc_html__('Please use EE_Ticket::decreaseSoldForDatetimes() instead', 'event_espresso'),
1915
+			'4.9.80.p',
1916
+			'5.0.0.p'
1917
+		);
1918
+		$this->decreaseSoldForDatetimes($qty);
1919
+	}
1920
+
1921
+
1922
+	/**
1923
+	 * Increments reserved by amount passed by $qty, and persists it immediately to the database.
1924
+	 *
1925
+	 * @param int    $qty
1926
+	 * @param string $source
1927
+	 * @return bool whether we successfully reserved the ticket or not.
1928
+	 * @throws EE_Error
1929
+	 * @throws InvalidArgumentException
1930
+	 * @throws ReflectionException
1931
+	 * @throws InvalidDataTypeException
1932
+	 * @throws InvalidInterfaceException
1933
+	 * @deprecated 4.9.80.p
1934
+	 */
1935
+	public function increase_reserved($qty = 1, $source = 'unknown')
1936
+	{
1937
+		EE_Error::doing_it_wrong(
1938
+			__FUNCTION__,
1939
+			esc_html__('Please use EE_Ticket::increaseReserved() instead', 'event_espresso'),
1940
+			'4.9.80.p',
1941
+			'5.0.0.p'
1942
+		);
1943
+		return $this->increaseReserved($qty);
1944
+	}
1945
+
1946
+
1947
+	/**
1948
+	 * Increases sold on related datetimes
1949
+	 *
1950
+	 * @param int $qty
1951
+	 * @return boolean indicating success
1952
+	 * @throws EE_Error
1953
+	 * @throws InvalidArgumentException
1954
+	 * @throws InvalidDataTypeException
1955
+	 * @throws InvalidInterfaceException
1956
+	 * @throws ReflectionException
1957
+	 * @deprecated 4.9.80.p
1958
+	 */
1959
+	protected function _increase_reserved_for_datetimes($qty = 1)
1960
+	{
1961
+		EE_Error::doing_it_wrong(
1962
+			__FUNCTION__,
1963
+			esc_html__('Please use EE_Ticket::increaseReservedForDatetimes() instead', 'event_espresso'),
1964
+			'4.9.80.p',
1965
+			'5.0.0.p'
1966
+		);
1967
+		return $this->increaseReservedForDatetimes($qty);
1968
+	}
1969
+
1970
+
1971
+	/**
1972
+	 * Decrements (subtracts) reserved by amount passed by $qty, and persists it immediately to the database.
1973
+	 *
1974
+	 * @param int    $qty
1975
+	 * @param bool   $adjust_datetimes
1976
+	 * @param string $source
1977
+	 * @return void
1978
+	 * @throws EE_Error
1979
+	 * @throws InvalidArgumentException
1980
+	 * @throws ReflectionException
1981
+	 * @throws InvalidDataTypeException
1982
+	 * @throws InvalidInterfaceException
1983
+	 * @deprecated 4.9.80.p
1984
+	 */
1985
+	public function decrease_reserved($qty = 1, $adjust_datetimes = true, $source = 'unknown')
1986
+	{
1987
+		EE_Error::doing_it_wrong(
1988
+			__FUNCTION__,
1989
+			esc_html__('Please use EE_Ticket::decreaseReserved() instead', 'event_espresso'),
1990
+			'4.9.80.p',
1991
+			'5.0.0.p'
1992
+		);
1993
+		$this->decreaseReserved($qty);
1994
+	}
1995
+
1996
+
1997
+	/**
1998
+	 * Decreases reserved on related datetimes
1999
+	 *
2000
+	 * @param int $qty
2001
+	 * @return void
2002
+	 * @throws EE_Error
2003
+	 * @throws InvalidArgumentException
2004
+	 * @throws ReflectionException
2005
+	 * @throws InvalidDataTypeException
2006
+	 * @throws InvalidInterfaceException
2007
+	 * @deprecated 4.9.80.p
2008
+	 */
2009
+	protected function _decrease_reserved_for_datetimes($qty = 1)
2010
+	{
2011
+		EE_Error::doing_it_wrong(
2012
+			__FUNCTION__,
2013
+			esc_html__('Please use EE_Ticket::decreaseReservedForDatetimes() instead', 'event_espresso'),
2014
+			'4.9.80.p',
2015
+			'5.0.0.p'
2016
+		);
2017
+		$this->decreaseReservedForDatetimes($qty);
2018
+	}
2019 2019
 }
Please login to merge, or discard this patch.