@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
4 | - exit('No direct script access allowed'); |
|
4 | + exit('No direct script access allowed'); |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | |
@@ -17,1117 +17,1117 @@ discard block |
||
17 | 17 | class EE_Message_Resource_Manager |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * This option in the database is used to keep a record of message types that have been activated for a messenger |
|
22 | - * at some point in the history of the site. It is utilized by the implementation of the 'force' flag in |
|
23 | - * EE_Register_Message_Type. The force flag is an indication of whether a message type should be activated by |
|
24 | - * default when the message type is registered. However, if a user has explicitly deactivated a message type, then |
|
25 | - * the force flag is ignored. The method by which the code knows whether to ignore this flag is via this option. |
|
26 | - * Note, that this is NOT a historical record. Its entirely possible for a message type to have been activated for |
|
27 | - * a messenger and yet not have a record in this option. This occurs when a message type is inactivated through an |
|
28 | - * automated process (when an add-on registering the message type deactivates, or when some other code calls the |
|
29 | - * EE_Registery_Message_Type::deregister method) and the related record(s) is(are) removed from this option to ensure |
|
30 | - * the "force" flag is respected if that message type is later re-registered. |
|
31 | - * |
|
32 | - * This option should NOT be used to determine the current "active" state of a message type for a given messenger. |
|
33 | - * |
|
34 | - * The name of this option (and related methods/properties) is due to matching the original intended purpose for the |
|
35 | - * option that got superseded by later behaviour requirements. |
|
36 | - */ |
|
37 | - const HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME = 'ee_has_activated_messenger'; |
|
38 | - |
|
39 | - /** |
|
40 | - * @type boolean $_initialized |
|
41 | - */ |
|
42 | - protected $_initialized = false; |
|
43 | - |
|
44 | - /** |
|
45 | - * @type EE_Messenger_Collection $_messenger_collection_loader |
|
46 | - */ |
|
47 | - protected $_messenger_collection_loader; |
|
48 | - |
|
49 | - /** |
|
50 | - * @type EE_Message_Type_Collection $_message_type_collection_loader |
|
51 | - */ |
|
52 | - protected $_message_type_collection_loader; |
|
53 | - |
|
54 | - /** |
|
55 | - * @type EEM_Message_Template_Group $_message_template_group_model |
|
56 | - */ |
|
57 | - protected $_message_template_group_model; |
|
58 | - |
|
59 | - /** |
|
60 | - * @type EE_messenger[] |
|
61 | - */ |
|
62 | - protected $_installed_messengers = array(); |
|
63 | - |
|
64 | - /** |
|
65 | - * @type EE_message_type[] |
|
66 | - */ |
|
67 | - protected $_installed_message_types = array(); |
|
68 | - |
|
69 | - /** |
|
70 | - * Array of active messengers. |
|
71 | - * Format is this: |
|
72 | - * array( |
|
73 | - * 'messenger_name' => EE_messenger |
|
74 | - * ) |
|
75 | - * |
|
76 | - * @type EE_messenger[] |
|
77 | - */ |
|
78 | - protected $_active_messengers = array(); |
|
79 | - |
|
80 | - /** |
|
81 | - * Formatted array of active message types grouped per messenger. |
|
82 | - * Format is this: |
|
83 | - * array( |
|
84 | - * 'messenger_name' => array( |
|
85 | - * 'settings' => array( |
|
86 | - * '{messenger_name}-message_types' => array( |
|
87 | - * 'message_type_name' => array() //variable array of settings corresponding to message type. |
|
88 | - * ) |
|
89 | - * ) |
|
90 | - * ) |
|
91 | - * ) |
|
92 | - * |
|
93 | - * @type array |
|
94 | - */ |
|
95 | - protected $_active_message_types = array(); |
|
96 | - |
|
97 | - |
|
98 | - /** |
|
99 | - * This holds the array of messengers and their corresponding message types that have |
|
100 | - * been activated on a site at some point. This is an important record that helps the messages system |
|
101 | - * not accidentally reactivate something that was intentionally deactivated by a user. |
|
102 | - * |
|
103 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
104 | - * |
|
105 | - * @type array |
|
106 | - */ |
|
107 | - protected $_has_activated_messengers_and_message_types = array(); |
|
108 | - |
|
109 | - /** |
|
110 | - * An array of unique message type contexts across all active message types. |
|
111 | - * The array will be indexed by either 'slugs' or 'all'. |
|
112 | - * The slugs index contains an array indexed by unique context slugs with the latest label representation for that |
|
113 | - * slug. array( |
|
114 | - * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
115 | - * ); |
|
116 | - * The all index returns an array in this format: |
|
117 | - * array( |
|
118 | - * 'message_type_name' => array( |
|
119 | - * 'context_slug' => array( |
|
120 | - * 'label' => 'localized label for context', |
|
121 | - * 'description' => 'localized description for context' |
|
122 | - * ) |
|
123 | - * ) |
|
124 | - * ); |
|
125 | - * |
|
126 | - * @type array |
|
127 | - */ |
|
128 | - protected $_contexts = array(); |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * EE_Message_Resource_Manager constructor. |
|
133 | - * |
|
134 | - * @param \EE_Messenger_Collection_Loader $Messenger_Collection_Loader |
|
135 | - * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader |
|
136 | - * @param \EEM_Message_Template_Group $Message_Template_Group_Model |
|
137 | - */ |
|
138 | - function __construct( |
|
139 | - EE_Messenger_Collection_Loader $Messenger_Collection_Loader, |
|
140 | - EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader, |
|
141 | - EEM_Message_Template_Group $Message_Template_Group_Model |
|
142 | - ) { |
|
143 | - $this->_messenger_collection_loader = $Messenger_Collection_Loader; |
|
144 | - $this->_message_type_collection_loader = $Message_Type_Collection_Loader; |
|
145 | - $this->_message_template_group_model = $Message_Template_Group_Model; |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * @return void |
|
151 | - */ |
|
152 | - protected function _initialize_collections() |
|
153 | - { |
|
154 | - if ($this->_initialized) { |
|
155 | - return; |
|
156 | - } |
|
157 | - $this->_initialized = true; |
|
158 | - $this->_messenger_collection_loader->load_messengers_from_folder(); |
|
159 | - $this->_message_type_collection_loader->load_message_types_from_folder(); |
|
160 | - $this->get_has_activated_messengers_option(true); |
|
161 | - $this->_set_active_messengers_and_message_types(); |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - /** |
|
166 | - * @return EE_Messenger_Collection |
|
167 | - */ |
|
168 | - public function messenger_collection() |
|
169 | - { |
|
170 | - $this->_initialize_collections(); |
|
171 | - return $this->_messenger_collection_loader->messenger_collection(); |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * @return EE_messenger[] |
|
177 | - */ |
|
178 | - public function active_messengers() |
|
179 | - { |
|
180 | - $this->_initialize_collections(); |
|
181 | - return $this->_active_messengers; |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * @param string $messenger_name |
|
187 | - * @return \EE_messenger |
|
188 | - */ |
|
189 | - public function get_messenger($messenger_name) |
|
190 | - { |
|
191 | - return $this->messenger_collection()->get_by_info($messenger_name); |
|
192 | - } |
|
193 | - |
|
194 | - |
|
195 | - /** |
|
196 | - * This returns the corresponding EE_messenger object for the given string if it is active. |
|
197 | - * |
|
198 | - * @param string $messenger |
|
199 | - * @return EE_messenger | null |
|
200 | - */ |
|
201 | - public function get_active_messenger($messenger) |
|
202 | - { |
|
203 | - $this->_initialize_collections(); |
|
204 | - return ! empty($this->_active_messengers[$messenger]) ? $this->_active_messengers[$messenger] : null; |
|
205 | - } |
|
206 | - |
|
207 | - |
|
208 | - /** |
|
209 | - * @return \EE_messenger[] |
|
210 | - */ |
|
211 | - public function installed_messengers() |
|
212 | - { |
|
213 | - if (empty($this->_installed_messengers)) { |
|
214 | - $this->_installed_messengers = array(); |
|
215 | - $this->messenger_collection()->rewind(); |
|
216 | - while ($this->messenger_collection()->valid()) { |
|
217 | - $this->_installed_messengers[$this->messenger_collection()->current()->name] = $this->messenger_collection()->current(); |
|
218 | - $this->messenger_collection()->next(); |
|
219 | - } |
|
220 | - } |
|
221 | - return $this->_installed_messengers; |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - /** |
|
226 | - * @param string $messenger_name |
|
227 | - * @return \EE_messenger |
|
228 | - * @throws \EE_Error |
|
229 | - */ |
|
230 | - public function valid_messenger($messenger_name) |
|
231 | - { |
|
232 | - $messenger = $this->get_messenger($messenger_name); |
|
233 | - if ($messenger instanceof EE_messenger) { |
|
234 | - return $messenger; |
|
235 | - } |
|
236 | - throw new EE_Error( |
|
237 | - sprintf( |
|
238 | - __('The "%1$s" messenger is either invalid or not installed', 'event_espresso'), |
|
239 | - $messenger_name |
|
240 | - ) |
|
241 | - ); |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * @return EE_Message_Type_Collection |
|
247 | - */ |
|
248 | - public function message_type_collection() |
|
249 | - { |
|
250 | - $this->_initialize_collections(); |
|
251 | - return $this->_message_type_collection_loader->message_type_collection(); |
|
252 | - } |
|
253 | - |
|
254 | - |
|
255 | - /** |
|
256 | - * @return array |
|
257 | - */ |
|
258 | - public function active_message_types() |
|
259 | - { |
|
260 | - $this->_initialize_collections(); |
|
261 | - return $this->_active_message_types; |
|
262 | - } |
|
263 | - |
|
264 | - |
|
265 | - /** |
|
266 | - * @param string $message_type_name |
|
267 | - * @return \EE_message_type |
|
268 | - */ |
|
269 | - public function get_message_type($message_type_name) |
|
270 | - { |
|
271 | - return $this->message_type_collection()->get_by_info($message_type_name); |
|
272 | - } |
|
273 | - |
|
274 | - |
|
275 | - /** |
|
276 | - * This returns the EE_message_type from the active message types array ( if present ); |
|
277 | - * |
|
278 | - * @param string $messenger_name |
|
279 | - * @param string $message_type_name |
|
280 | - * @return \EE_message_type|null |
|
281 | - */ |
|
282 | - public function get_active_message_type_for_messenger($messenger_name, $message_type_name) |
|
283 | - { |
|
284 | - return $this->is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
285 | - ? $this->get_message_type($message_type_name) |
|
286 | - : null; |
|
287 | - } |
|
288 | - |
|
289 | - |
|
290 | - /** |
|
291 | - * Returns whether the given message type is active for the given messenger. |
|
292 | - * |
|
293 | - * @param string $messenger_name |
|
294 | - * @param string $message_type_name |
|
295 | - * @return bool |
|
296 | - */ |
|
297 | - public function is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
298 | - { |
|
299 | - $this->_initialize_collections(); |
|
300 | - return ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]); |
|
301 | - } |
|
302 | - |
|
303 | - |
|
304 | - /** |
|
305 | - * Returns whether the given messenger is active. |
|
306 | - * |
|
307 | - * @param string $messenger_name the name of the messenger to check if active. |
|
308 | - * @return bool |
|
309 | - */ |
|
310 | - public function is_messenger_active($messenger_name) |
|
311 | - { |
|
312 | - $this->_initialize_collections(); |
|
313 | - return ! empty($this->_active_message_types[$messenger_name]); |
|
314 | - } |
|
315 | - |
|
316 | - |
|
317 | - /** |
|
318 | - * This returns any settings that might be on a message type for a messenger |
|
319 | - * |
|
320 | - * @param string $messenger_name The slug of the messenger |
|
321 | - * @param string $message_type_name The slug of the message type getting the settings for. |
|
322 | - * @return array |
|
323 | - */ |
|
324 | - public function get_message_type_settings_for_messenger($messenger_name, $message_type_name) |
|
325 | - { |
|
326 | - $settings = array(); |
|
327 | - if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
328 | - $settings = isset($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]['settings']) |
|
329 | - ? $this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]['settings'] |
|
330 | - : array(); |
|
331 | - } |
|
332 | - return $settings; |
|
333 | - } |
|
334 | - |
|
335 | - |
|
336 | - /** |
|
337 | - * Returns whether the given messenger name has active message types on it. |
|
338 | - * Infers whether the messenger is active or not as well. |
|
339 | - * |
|
340 | - * @param string $messenger_name |
|
341 | - * @return bool |
|
342 | - */ |
|
343 | - public function messenger_has_active_message_types($messenger_name) |
|
344 | - { |
|
345 | - $this->_initialize_collections(); |
|
346 | - return |
|
347 | - ! empty($this->_active_message_types[$messenger_name]) |
|
348 | - && ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types']); |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * This checks the _active_message_types property for any active message types |
|
354 | - * that are present for the given messenger and returns them. |
|
355 | - * |
|
356 | - * @since 4.9.0 |
|
357 | - * @param string $messenger_name The messenger being checked |
|
358 | - * @return EE_message_type[]|array (empty array if no active_message_types) |
|
359 | - */ |
|
360 | - public function get_active_message_types_for_messenger($messenger_name) |
|
361 | - { |
|
362 | - $message_types = array(); |
|
363 | - if (! $this->messenger_has_active_message_types($messenger_name)) { |
|
364 | - return $message_types; |
|
365 | - } |
|
366 | - $installed_message_types = $this->installed_message_types(); |
|
367 | - foreach ($installed_message_types as $message_type_name => $message_type) { |
|
368 | - if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
369 | - $message_types[$message_type_name] = $message_type; |
|
370 | - } |
|
371 | - } |
|
372 | - return $message_types; |
|
373 | - } |
|
374 | - |
|
375 | - |
|
376 | - /** |
|
377 | - * This does NOT return the _active_message_types property but |
|
378 | - * simply returns an array of active message type names from that property. |
|
379 | - * (The _active_message_types property is indexed by messenger and active message_types per messenger). |
|
380 | - * |
|
381 | - * @return array message_type references (string) |
|
382 | - */ |
|
383 | - public function list_of_active_message_types() |
|
384 | - { |
|
385 | - $active_message_type_names = array(); |
|
386 | - $this->_initialize_collections(); |
|
387 | - foreach ($this->_active_message_types as $messenger => $messenger_settings) { |
|
388 | - if (! isset($messenger_settings['settings'][$messenger . '-message_types'])) { |
|
389 | - continue; |
|
390 | - } |
|
391 | - foreach ($messenger_settings['settings'][$messenger . '-message_types'] as $message_type_name => $message_type_config) { |
|
392 | - if (! in_array($message_type_name, $active_message_type_names)) { |
|
393 | - $active_message_type_names[] = $message_type_name; |
|
394 | - } |
|
395 | - } |
|
396 | - } |
|
397 | - return $active_message_type_names; |
|
398 | - } |
|
399 | - |
|
400 | - |
|
401 | - /** |
|
402 | - * Same as list_of_active_message_types() except this returns actual EE_message_type objects |
|
403 | - * |
|
404 | - * @since 4.9.0 |
|
405 | - * @return \EE_message_type[] |
|
406 | - */ |
|
407 | - public function get_active_message_type_objects() |
|
408 | - { |
|
409 | - $active_message_types = array(); |
|
410 | - $installed_message_types = $this->installed_message_types(); |
|
411 | - $active_message_type_names = $this->list_of_active_message_types(); |
|
412 | - foreach ($active_message_type_names as $active_message_type_name) { |
|
413 | - if (isset($installed_message_types[$active_message_type_name])) { |
|
414 | - $active_message_types[$active_message_type_name] = $installed_message_types[$active_message_type_name]; |
|
415 | - } |
|
416 | - } |
|
417 | - return $active_message_types; |
|
418 | - } |
|
419 | - |
|
420 | - |
|
421 | - /** |
|
422 | - * @return \EE_message_type[] |
|
423 | - */ |
|
424 | - public function installed_message_types() |
|
425 | - { |
|
426 | - if (empty($this->_installed_message_types)) { |
|
427 | - $this->message_type_collection()->rewind(); |
|
428 | - while ($this->message_type_collection()->valid()) { |
|
429 | - $this->_installed_message_types[$this->message_type_collection()->current()->name] = $this->message_type_collection()->current(); |
|
430 | - $this->message_type_collection()->next(); |
|
431 | - } |
|
432 | - } |
|
433 | - return $this->_installed_message_types; |
|
434 | - } |
|
435 | - |
|
436 | - |
|
437 | - /** |
|
438 | - * @param string $message_type_name |
|
439 | - * @return \EE_message_type |
|
440 | - * @throws \EE_Error |
|
441 | - */ |
|
442 | - public function valid_message_type($message_type_name) |
|
443 | - { |
|
444 | - $message_type = $this->get_message_type($message_type_name); |
|
445 | - if ($message_type instanceof EE_message_type) { |
|
446 | - return $message_type; |
|
447 | - } |
|
448 | - throw new EE_Error( |
|
449 | - sprintf( |
|
450 | - __('The "%1$s" message type is either invalid or not installed', 'event_espresso'), |
|
451 | - $message_type_name |
|
452 | - ) |
|
453 | - ); |
|
454 | - } |
|
455 | - |
|
456 | - |
|
457 | - /** |
|
458 | - * valid_message_type_for_messenger |
|
459 | - * |
|
460 | - * @param EE_messenger $messenger |
|
461 | - * @param string $message_type_name |
|
462 | - * @return boolean |
|
463 | - * @throws \EE_Error |
|
464 | - */ |
|
465 | - public function valid_message_type_for_messenger(EE_messenger $messenger, $message_type_name) |
|
466 | - { |
|
467 | - $valid_message_types = $messenger->get_valid_message_types(); |
|
468 | - if (! in_array($message_type_name, $valid_message_types)) { |
|
469 | - throw new EE_Error( |
|
470 | - sprintf( |
|
471 | - __( |
|
472 | - 'The message type (%1$s) sent to "%2$s" is not valid for the "%3$s" messenger. Double-check the spelling and verify that message type has been registered as a valid type with the messenger.', |
|
473 | - 'event_espresso' |
|
474 | - ), |
|
475 | - $message_type_name, |
|
476 | - __METHOD__, |
|
477 | - $messenger->name |
|
478 | - ) |
|
479 | - ); |
|
480 | - } |
|
481 | - return true; |
|
482 | - } |
|
483 | - |
|
484 | - |
|
485 | - /** |
|
486 | - * Used to return active messengers array stored in the wp options table. |
|
487 | - * If no value is present in the option then an empty array is returned. |
|
488 | - * |
|
489 | - * @param bool $reset If true then we ignore whether the option is cached on the _active_message_types |
|
490 | - * property and pull directly from the db. Otherwise whatever is currently on the |
|
491 | - * $_active_message_types property is pulled. |
|
492 | - * @return array |
|
493 | - */ |
|
494 | - public function get_active_messengers_option($reset = false) |
|
495 | - { |
|
496 | - if ($reset) { |
|
497 | - $this->_active_message_types = get_option('ee_active_messengers', array()); |
|
498 | - } |
|
499 | - return $this->_active_message_types; |
|
500 | - } |
|
501 | - |
|
502 | - |
|
503 | - /** |
|
504 | - * Used to update the active messengers array stored in the wp options table. |
|
505 | - * |
|
506 | - * @param array $active_messenger_settings Incoming data to save. If empty, then the internal cached property |
|
507 | - * representing this data is used. |
|
508 | - * @return bool FALSE if not updated, TRUE if updated. |
|
509 | - */ |
|
510 | - public function update_active_messengers_option($active_messenger_settings = array()) |
|
511 | - { |
|
512 | - $active_messenger_settings = empty($active_messenger_settings) ? $this->_active_message_types : $active_messenger_settings; |
|
513 | - //make sure _active_message_types is updated (this is the internal cache for the settings). |
|
514 | - $this->_active_message_types = $active_messenger_settings; |
|
515 | - return update_option('ee_active_messengers', $active_messenger_settings); |
|
516 | - } |
|
517 | - |
|
518 | - |
|
519 | - /** |
|
520 | - * Used to return has activated message types for messengers array stored in the wp options table. |
|
521 | - * If no value is present in the option then an empty array is returned. |
|
522 | - * The value is cached on the $_has_activated_messengers_and_message_types property for future calls. |
|
523 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
524 | - * |
|
525 | - * @param bool $reset Used to indicate that any cached value should be ignored. |
|
526 | - * @return array |
|
527 | - */ |
|
528 | - public function get_has_activated_messengers_option($reset = false) |
|
529 | - { |
|
530 | - if ($reset || empty($this->_has_activated_messengers_and_message_types)) { |
|
531 | - $this->_has_activated_messengers_and_message_types = get_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, array()); |
|
532 | - } |
|
533 | - return $this->_has_activated_messengers_and_message_types; |
|
534 | - } |
|
535 | - |
|
536 | - |
|
537 | - /** |
|
538 | - * Used to update the has activated option in the db. |
|
539 | - * |
|
540 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
541 | - * |
|
542 | - * @param array $has_activated_messengers Incoming data to save. If empty, then the internal cached property |
|
543 | - * representing this data is used. |
|
544 | - * @return bool FALSE if not updated, TRUE if updated. |
|
545 | - */ |
|
546 | - public function update_has_activated_messengers_option($has_activated_messengers = array()) |
|
547 | - { |
|
548 | - //make sure the option has been retrieved from first so we don't overwrite it accidentally. |
|
549 | - if (empty($has_activated_messengers) && empty($this->_has_activated_messengers_and_message_types)) { |
|
550 | - $this->get_has_activated_messengers_option(); |
|
551 | - } |
|
552 | - $has_activated_messengers = empty($has_activated_messengers) |
|
553 | - ? $this->_has_activated_messengers_and_message_types |
|
554 | - : $has_activated_messengers; |
|
555 | - return update_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, $has_activated_messengers); |
|
556 | - } |
|
557 | - |
|
558 | - |
|
559 | - /** |
|
560 | - * wrapper for _set_active_messengers_and_message_types() |
|
561 | - */ |
|
562 | - public function reset_active_messengers_and_message_types() |
|
563 | - { |
|
564 | - $this->_set_active_messengers_and_message_types(); |
|
565 | - } |
|
566 | - |
|
567 | - |
|
568 | - /** |
|
569 | - * Generate list of active messengers and message types from collection. |
|
570 | - * This sets up the active messengers from what is present in the database. |
|
571 | - */ |
|
572 | - protected function _set_active_messengers_and_message_types() |
|
573 | - { |
|
574 | - //echo "\n\n " . __LINE__ . ") " . __METHOD__ . "() \n"; |
|
575 | - // list of activated messengers as set via the admin |
|
576 | - // note calling `get_active_messengers_options` also initializes the _active_message_types property. |
|
577 | - $this->get_active_messengers_option(true); |
|
578 | - $this->ensure_messengers_are_active(array(), false, true); |
|
579 | - $this->update_active_messengers_option(); |
|
580 | - $this->update_has_activated_messengers_option(); |
|
581 | - } |
|
582 | - |
|
583 | - |
|
584 | - /** |
|
585 | - * Ensures that the specified messenger is currently active. |
|
586 | - * If not, activates it and its default message types. |
|
587 | - * |
|
588 | - * @param string $messenger_name |
|
589 | - * @param bool $update_option Whether to update the option in the db or not. |
|
590 | - * @return boolean true if either already active or successfully activated. |
|
591 | - */ |
|
592 | - public function ensure_messenger_is_active($messenger_name, $update_option = true) |
|
593 | - { |
|
594 | - if (! isset($this->_active_messengers[$messenger_name])) { |
|
595 | - try { |
|
596 | - $this->activate_messenger($messenger_name, array(), $update_option); |
|
597 | - } catch (EE_Error $e) { |
|
598 | - EE_Error::add_error( |
|
599 | - $e->getMessage(), |
|
600 | - __FILE__, |
|
601 | - __FUNCTION__, |
|
602 | - __LINE__ |
|
603 | - ); |
|
604 | - return false; |
|
605 | - } |
|
606 | - } |
|
607 | - return true; |
|
608 | - } |
|
609 | - |
|
610 | - |
|
611 | - /** |
|
612 | - * This ensures the given array of messenger names is active in the system. |
|
613 | - * Note, this method will not activate any NEW message types for the messenger when it is called. Instead, |
|
614 | - * it will automatically activate the default message types for the messenger if its not active. |
|
615 | - * |
|
616 | - * @param array $messenger_names Array of messenger names for messengers to be activated. If an empty array |
|
617 | - * (default) then will attempt to set the active messengers from the |
|
618 | - * activated_messengers option |
|
619 | - * (stored in $_active_message_types property). |
|
620 | - * @param bool $update_option Whether to update the related active messengers option. |
|
621 | - * @param bool $verify Whether to verify the messengers are installed before activating. Note if this is |
|
622 | - * set to true and a messenger is indicated as active, but is NOT installed, then it |
|
623 | - * will automatically be deactivated. |
|
624 | - */ |
|
625 | - public function ensure_messengers_are_active($messenger_names = array(), $update_option = true, $verify = false) |
|
626 | - { |
|
627 | - $messenger_names = empty($messenger_names) ? array_keys($this->_active_message_types) : $messenger_names; |
|
628 | - |
|
629 | - $not_installed = array(); |
|
630 | - foreach ($messenger_names as $messenger_name) { |
|
631 | - if ($verify && ! $this->messenger_collection()->has_by_name($messenger_name)) { |
|
632 | - $not_installed[] = $messenger_name; |
|
633 | - $this->deactivate_messenger($messenger_name); |
|
634 | - continue; |
|
635 | - } |
|
636 | - $this->ensure_messenger_is_active($messenger_name, $update_option); |
|
637 | - } |
|
638 | - |
|
639 | - if (! empty($not_installed)) { |
|
640 | - EE_Error::add_error( |
|
641 | - sprintf( |
|
642 | - __('The following messengers are either not installed or are invalid:%1$s %2$s', 'event_espresso'), |
|
643 | - '<br />', |
|
644 | - implode(', ', $not_installed) |
|
645 | - ), |
|
646 | - __FILE__, __FUNCTION__, __LINE__ |
|
647 | - ); |
|
648 | - } |
|
649 | - } |
|
650 | - |
|
651 | - |
|
652 | - /** |
|
653 | - * Ensures that the specified message type for the given messenger is currently active, if not activates it. |
|
654 | - * This ALSO ensures that the given messenger is active as well! |
|
655 | - * |
|
656 | - * @param string $message_type_name message type name. |
|
657 | - * @param $messenger_name |
|
658 | - * @param bool $update_option Whether to update the option in the db or not. |
|
659 | - * @return bool Returns true if already is active or if was activated successfully. |
|
660 | - * @throws \EE_Error |
|
661 | - */ |
|
662 | - public function ensure_message_type_is_active($message_type_name, $messenger_name, $update_option = true) |
|
663 | - { |
|
664 | - // grab the messenger to work with. |
|
665 | - $messenger = $this->valid_messenger($messenger_name); |
|
666 | - if ($this->valid_message_type_for_messenger($messenger, $message_type_name)) { |
|
667 | - //ensure messenger is active (that's an inherent coupling between active message types and the |
|
668 | - //messenger they are being activated for. |
|
669 | - try { |
|
670 | - if (! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
671 | - //all is good so let's just get it active |
|
672 | - $this->activate_messenger($messenger_name, array($message_type_name), $update_option); |
|
673 | - } |
|
674 | - } catch (EE_Error $e) { |
|
675 | - EE_Error::add_error( |
|
676 | - $e->getMessage(), |
|
677 | - __FILE__, |
|
678 | - __FUNCTION__, |
|
679 | - __LINE__ |
|
680 | - ); |
|
681 | - return false; |
|
682 | - } |
|
683 | - } |
|
684 | - return true; |
|
685 | - } |
|
686 | - |
|
687 | - |
|
688 | - /** |
|
689 | - * This is a wrapper for `ensure_message_type_is_active` that will handle ensuring multiple message types for a |
|
690 | - * messenger are active in one go. |
|
691 | - * |
|
692 | - * @param array $message_type_names Array of message type names to ensure are active. |
|
693 | - * @param string $messenger_name The name of the messenger that the message types are to be activated on. |
|
694 | - * @param bool $update_option Whether to persist the activation to the database or not (default true). |
|
695 | - */ |
|
696 | - public function ensure_message_types_are_active($message_type_names, $messenger_name, $update_option = true) |
|
697 | - { |
|
698 | - $message_type_names = (array)$message_type_names; |
|
699 | - foreach ($message_type_names as $message_type_name) { |
|
700 | - // note, intentionally not updating option here because we're in a loop. |
|
701 | - // We'll follow the instructions of the incoming $update_option argument after the loop. |
|
702 | - $this->ensure_message_type_is_active($message_type_name, $messenger_name, false); |
|
703 | - } |
|
704 | - if ($update_option) { |
|
705 | - $this->update_active_messengers_option(); |
|
706 | - $this->update_has_activated_messengers_option(); |
|
707 | - } |
|
708 | - } |
|
709 | - |
|
710 | - |
|
711 | - /** |
|
712 | - * Activates the specified messenger. |
|
713 | - * |
|
714 | - * @param string $messenger_name |
|
715 | - * @param array $message_type_names An array of message type names to activate with this messenger. |
|
716 | - * If included we do NOT setup the default message types |
|
717 | - * (assuming they are already setup.) |
|
718 | - * @param bool $update_active_messengers_option |
|
719 | - * @return array of generated templates |
|
720 | - * @throws \EE_Error |
|
721 | - */ |
|
722 | - public function activate_messenger( |
|
723 | - $messenger_name, |
|
724 | - $message_type_names = array(), |
|
725 | - $update_active_messengers_option = true |
|
726 | - ) { |
|
727 | - $templates = array(); |
|
728 | - // grab the messenger to work with. |
|
729 | - $messenger = $this->messenger_collection()->get_by_info($messenger_name); |
|
730 | - // it's inactive. Activate it. |
|
731 | - if ($messenger instanceof EE_messenger) { |
|
732 | - $this->_active_messengers[$messenger->name] = $messenger; |
|
733 | - //activate incoming message types set to be activated with messenger. |
|
734 | - $message_type_names = $this->_activate_message_types($messenger, $message_type_names); |
|
735 | - // setup any initial settings for the messenger if necessary. |
|
736 | - $this->add_settings_for_messenger($messenger->name); |
|
737 | - if ($update_active_messengers_option) { |
|
738 | - $this->update_active_messengers_option(); |
|
739 | - $this->update_has_activated_messengers_option(); |
|
740 | - } |
|
741 | - //generate new templates if necessary and ensure all related templates that are already in the database are |
|
742 | - //marked active. Note, this will also deactivate a message type for a messenger if the template |
|
743 | - //cannot be successfully created during its attempt (only happens for global template attempts). |
|
744 | - if (! empty($message_type_names)) { |
|
745 | - $templates = EEH_MSG_Template::generate_new_templates($messenger->name, $message_type_names, 0, true); |
|
746 | - EEH_MSG_Template::update_to_active(array($messenger->name), $message_type_names); |
|
747 | - } |
|
748 | - } |
|
749 | - return $templates; |
|
750 | - } |
|
751 | - |
|
752 | - |
|
753 | - /** |
|
754 | - * Activates given message types for the given EE_messenger object. |
|
755 | - * Note: (very important) This method does not persist the activation to the database. |
|
756 | - * See code implementing this method in this class for examples of how to persist. |
|
757 | - * |
|
758 | - * @param \EE_messenger $messenger |
|
759 | - * @param array $message_type_names |
|
760 | - * @return array |
|
761 | - */ |
|
762 | - protected function _activate_message_types(EE_messenger $messenger, $message_type_names = array()) |
|
763 | - { |
|
764 | - //If $message_type_names is empty, AND $this->_active_message_types is empty, then that means |
|
765 | - //things have never been initialized (which should happen on EEH_Activation::generate_message_templates). |
|
766 | - //So ONLY then do we need to actually grab defaults and cycle through them. Otherwise we |
|
767 | - //only override _active_message_types when an explicit array of $message_type_names has been provided. |
|
768 | - $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[$messenger->name]) |
|
769 | - ? $messenger->get_default_message_types() |
|
770 | - : (array)$message_type_names; |
|
771 | - |
|
772 | - //now we ALWAYS need to make sure that the messenger is active for the message types we're activating! |
|
773 | - if (! isset($this->_active_message_types[$messenger->name])) { |
|
774 | - $this->_active_message_types[$messenger->name]['settings'] = array(); |
|
775 | - } |
|
776 | - |
|
777 | - if ($message_type_names) { |
|
778 | - // cycle thru message types |
|
779 | - foreach ($message_type_names as $message_type_name) { |
|
780 | - //only register the message type as active IF it isn't already active |
|
781 | - //and if its actually installed. |
|
782 | - if ( |
|
783 | - ! $this->is_message_type_active_for_messenger($messenger->name, $message_type_name) |
|
784 | - ) { |
|
785 | - $this->add_settings_for_message_type($messenger->name, $message_type_name); |
|
786 | - $this->_set_messenger_has_activated_message_type( |
|
787 | - $messenger, |
|
788 | - $message_type_name |
|
789 | - ); |
|
790 | - } |
|
791 | - } |
|
792 | - } |
|
793 | - return $message_type_names; |
|
794 | - } |
|
795 | - |
|
796 | - |
|
797 | - /** |
|
798 | - * add_settings_for_message_type |
|
799 | - * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
800 | - * $this->update_active_messengers_option to persist. |
|
801 | - * |
|
802 | - * @param string $messenger_name The name of the messenger adding the settings for |
|
803 | - * @param string $message_type_name The name of the message type adding the settings for |
|
804 | - * @param array $new_settings Any new settings being set for the message type and messenger |
|
805 | - */ |
|
806 | - public function add_settings_for_message_type($messenger_name, $message_type_name, $new_settings = array()) |
|
807 | - { |
|
808 | - // get installed message type from collection |
|
809 | - $message_type = $this->message_type_collection()->get_by_info($message_type_name); |
|
810 | - $existing_settings = $this->get_message_type_settings_for_messenger($messenger_name, $message_type_name); |
|
811 | - //we need to setup any initial settings for message types |
|
812 | - if ($message_type instanceof EE_message_type) { |
|
813 | - $default_settings = $message_type->get_admin_settings_fields(); |
|
814 | - foreach ($default_settings as $field => $values) { |
|
815 | - if (isset($new_settings[$field])) { |
|
816 | - $existing_settings[$field] = $new_settings[$field]; |
|
817 | - continue; |
|
818 | - } |
|
819 | - if (! isset($existing_settings[$field])) { |
|
820 | - $existing_settings[$field] = $values['default']; |
|
821 | - } |
|
822 | - } |
|
823 | - } |
|
824 | - $this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]['settings'] = $existing_settings; |
|
825 | - } |
|
826 | - |
|
827 | - |
|
828 | - /** |
|
829 | - * Updates the internal cached _has_activated_messengers_and_message_types property with the given messenger |
|
830 | - * and message type. |
|
831 | - * |
|
832 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
833 | - * |
|
834 | - * @access protected |
|
835 | - * @param \EE_messenger $messenger |
|
836 | - * @param string $message_type_name |
|
837 | - */ |
|
838 | - protected function _set_messenger_has_activated_message_type(EE_messenger $messenger, $message_type_name) |
|
839 | - { |
|
840 | - |
|
841 | - //if _has_activated_messengers_and_message_types is empty then lets ensure its initialized |
|
842 | - if (empty($this->_has_activated_messengers_and_message_types)) { |
|
843 | - $this->get_has_activated_messengers_option(); |
|
844 | - } |
|
845 | - |
|
846 | - // make sure this messenger has a record in the has_activated array |
|
847 | - if (! isset($this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
848 | - $this->_has_activated_messengers_and_message_types[$messenger->name] = array(); |
|
849 | - } |
|
850 | - // check if message type has already been added |
|
851 | - if (! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
852 | - $this->_has_activated_messengers_and_message_types[$messenger->name][] = $message_type_name; |
|
853 | - } |
|
854 | - } |
|
855 | - |
|
856 | - |
|
857 | - /** |
|
858 | - * add_settings_for_messenger |
|
859 | - * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
860 | - * $this->update_active_messengers_option to persist. |
|
861 | - * |
|
862 | - * @param string $messenger_name The name of the messenger the settings is being added for. |
|
863 | - * @param array $new_settings An array of settings to update the existing settings. |
|
864 | - */ |
|
865 | - public function add_settings_for_messenger($messenger_name, $new_settings = array()) |
|
866 | - { |
|
867 | - $messenger = $this->get_messenger($messenger_name); |
|
868 | - if ($messenger instanceof EE_messenger) { |
|
869 | - $msgr_settings = $messenger->get_admin_settings_fields(); |
|
870 | - if (! empty($msgr_settings)) { |
|
871 | - foreach ($msgr_settings as $field => $value) { |
|
872 | - //is there a new setting for this? |
|
873 | - if (isset($new_settings[$field])) { |
|
874 | - $this->_active_message_types[$messenger->name]['settings'][$field] = $new_settings[$field]; |
|
875 | - continue; |
|
876 | - } |
|
877 | - //only set the default if it isn't already set. |
|
878 | - if (! isset($this->_active_message_types[$messenger->name]['settings'][$field])) { |
|
879 | - $this->_active_message_types[$messenger->name]['settings'][$field] = $value; |
|
880 | - } |
|
881 | - } |
|
882 | - } |
|
883 | - } |
|
884 | - } |
|
885 | - |
|
886 | - |
|
887 | - /** |
|
888 | - * deactivate_messenger |
|
889 | - * |
|
890 | - * @param string|EE_messenger $messenger_name name of messenger |
|
891 | - * @return void |
|
892 | - */ |
|
893 | - public function deactivate_messenger($messenger_name) |
|
894 | - { |
|
895 | - $this->_initialize_collections(); |
|
896 | - if ($messenger_name instanceof EE_messenger) { |
|
897 | - $messenger_name = $messenger_name->name; |
|
898 | - } |
|
899 | - unset($this->_active_messengers[$messenger_name]); |
|
900 | - unset($this->_active_message_types[$messenger_name]); |
|
901 | - $this->_message_template_group_model->deactivate_message_template_groups_for($messenger_name); |
|
902 | - $this->update_active_messengers_option(); |
|
903 | - } |
|
904 | - |
|
905 | - |
|
906 | - /** |
|
907 | - * Deactivates a message type (note this will deactivate across all messenger's it is active on. |
|
908 | - * |
|
909 | - * @param string $message_type_name name of message type being deactivated |
|
910 | - * @param bool $set_has_active_record By default we always record the has_active record when deactivating a message |
|
911 | - * type. However, this can be overridden if we don't want this set (usually when |
|
912 | - * this is called as a part of deregistration of a custom message type) |
|
913 | - */ |
|
914 | - public function deactivate_message_type($message_type_name, $set_has_active_record = true) |
|
915 | - { |
|
916 | - $this->_initialize_collections(); |
|
917 | - if ($message_type_name instanceof EE_message_type) { |
|
918 | - $message_type_name = $message_type_name->name; |
|
919 | - } |
|
920 | - foreach ($this->_active_message_types as $messenger_name => $settings) { |
|
921 | - unset( |
|
922 | - $this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name] |
|
923 | - ); |
|
924 | - |
|
925 | - //we always record (even on deactivation) that a message type has been activated because there should at |
|
926 | - //least be a record in the "has_activated" option that it WAS active at one point. |
|
927 | - if ($set_has_active_record) { |
|
928 | - $messenger = $this->get_messenger($messenger_name); |
|
929 | - $this->_set_messenger_has_activated_message_type($messenger, $message_type_name); |
|
930 | - } |
|
931 | - } |
|
932 | - $this->_message_template_group_model->deactivate_message_template_groups_for('', $message_type_name); |
|
933 | - $this->update_active_messengers_option(); |
|
934 | - $this->update_has_activated_messengers_option(); |
|
935 | - } |
|
936 | - |
|
937 | - |
|
938 | - /** |
|
939 | - * Deactivates a message type for a specific messenger as opposed to all messengers. |
|
940 | - * |
|
941 | - * @param string $message_type_name Name of message type being deactivated. |
|
942 | - * @param string $messenger_name Name of messenger the message type is being deactivated for. |
|
943 | - */ |
|
944 | - public function deactivate_message_type_for_messenger($message_type_name, $messenger_name) |
|
945 | - { |
|
946 | - $this->_initialize_collections(); |
|
947 | - if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
948 | - unset($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]); |
|
949 | - } |
|
950 | - $this->_message_template_group_model->deactivate_message_template_groups_for(array($messenger_name), |
|
951 | - array($message_type_name)); |
|
952 | - $this->update_active_messengers_option(); |
|
953 | - } |
|
954 | - |
|
955 | - |
|
956 | - /** |
|
957 | - * Used to verify if a message can be sent for the given messenger and message type |
|
958 | - * and that it is a generating messenger (used for generating message templates). |
|
959 | - * |
|
960 | - * @param EE_messenger $messenger messenger used in trigger |
|
961 | - * @param EE_message_type $message_type message type used in trigger |
|
962 | - * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send. |
|
963 | - */ |
|
964 | - public function is_generating_messenger_and_active(EE_messenger $messenger, EE_message_type $message_type) |
|
965 | - { |
|
966 | - //get the $messengers the message type says it can be used with. |
|
967 | - foreach ($message_type->with_messengers() as $generating_messenger => $secondary_messengers) { |
|
968 | - if ( |
|
969 | - $messenger->name === $generating_messenger |
|
970 | - && $this->is_message_type_active_for_messenger($messenger->name, $message_type->name) |
|
971 | - ) { |
|
972 | - return true; |
|
973 | - } |
|
974 | - } |
|
975 | - return false; |
|
976 | - } |
|
977 | - |
|
978 | - |
|
979 | - /** |
|
980 | - * This returns all the contexts that are registered by all message types. |
|
981 | - * If $slugs_only is true, |
|
982 | - * then just an array indexed by unique context slugs with the latest label representation for that slug. |
|
983 | - * array( |
|
984 | - * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
985 | - * ); |
|
986 | - * If $slugs_only is false, then the format is: |
|
987 | - * array( |
|
988 | - * 'message_type_name' => array( |
|
989 | - * 'context_slug' => array( |
|
990 | - * 'label' => 'localized label for context', |
|
991 | - * 'description' => 'localized description for context' |
|
992 | - * ) |
|
993 | - * ) |
|
994 | - * ); |
|
995 | - * Keep in mind that although different message types may share the same context slugs, |
|
996 | - * it is possible that the context is described differently by the message type. |
|
997 | - * |
|
998 | - * @since 4.9.0 |
|
999 | - * @param bool $slugs_only Whether to return an array of just slugs and labels (true) |
|
1000 | - * or all contexts indexed by message type. |
|
1001 | - * @return array |
|
1002 | - */ |
|
1003 | - public function get_all_contexts($slugs_only = true) |
|
1004 | - { |
|
1005 | - $key = $slugs_only ? 'slugs' : 'all'; |
|
1006 | - // check if contexts has been setup yet. |
|
1007 | - if (empty($this->_contexts[$key])) { |
|
1008 | - // So let's get all active message type objects and loop through to get all unique contexts |
|
1009 | - foreach ($this->get_active_message_type_objects() as $message_type) { |
|
1010 | - if ($message_type instanceof EE_message_type) { |
|
1011 | - $message_type_contexts = $message_type->get_contexts(); |
|
1012 | - if ($slugs_only) { |
|
1013 | - foreach ($message_type_contexts as $context => $context_details) { |
|
1014 | - $this->_contexts[$key][$context] = $context_details['label']; |
|
1015 | - } |
|
1016 | - } else { |
|
1017 | - $this->_contexts[$key][$message_type->name] = $message_type_contexts; |
|
1018 | - } |
|
1019 | - } |
|
1020 | - } |
|
1021 | - } |
|
1022 | - return ! empty($this->_contexts[$key]) ? $this->_contexts[$key] : array(); |
|
1023 | - } |
|
1024 | - |
|
1025 | - |
|
1026 | - /** |
|
1027 | - * This checks the internal record of what message types are considered "active" and verifies that |
|
1028 | - * there is an installed class definition for that message type. If the active message type does not have a |
|
1029 | - * corresponding accessible message type class then it will be deactivated from all messengers it is active on and |
|
1030 | - * any related message templates will be inactivated as well. |
|
1031 | - * |
|
1032 | - * @return bool true means all active message types are valid, false means at least one message type was |
|
1033 | - * deactivated. |
|
1034 | - */ |
|
1035 | - public function validate_active_message_types_are_installed() |
|
1036 | - { |
|
1037 | - $list_of_active_message_type_names = $this->list_of_active_message_types(); |
|
1038 | - $installed_message_types = $this->installed_message_types(); |
|
1039 | - $all_message_types_valid = true; |
|
1040 | - //loop through list of active message types and verify they are installed. |
|
1041 | - foreach ($list_of_active_message_type_names as $message_type_name) { |
|
1042 | - if (! isset($installed_message_types[$message_type_name])) { |
|
1043 | - $this->remove_message_type_has_been_activated_from_all_messengers( |
|
1044 | - $message_type_name, |
|
1045 | - true |
|
1046 | - ); |
|
1047 | - $this->deactivate_message_type($message_type_name, false); |
|
1048 | - $all_message_types_valid = false; |
|
1049 | - } |
|
1050 | - } |
|
1051 | - return $all_message_types_valid; |
|
1052 | - } |
|
1053 | - |
|
1054 | - |
|
1055 | - /** |
|
1056 | - * This method checks the `ee_has_activated_messenger` option to see if the message type has ever been |
|
1057 | - * activated for the given messenger. This can be called by client code on plugin updates etc to determine whether |
|
1058 | - * to attempt automatically reactivating message types that should be activated by default or not. |
|
1059 | - * |
|
1060 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1061 | - * |
|
1062 | - * @param $message_type_name |
|
1063 | - * @param $messenger_name |
|
1064 | - * @return bool |
|
1065 | - */ |
|
1066 | - public function has_message_type_been_activated_for_messenger($message_type_name, $messenger_name) |
|
1067 | - { |
|
1068 | - $has_activated = $this->get_has_activated_messengers_option(); |
|
1069 | - return isset($has_activated[$messenger_name]) |
|
1070 | - && in_array($message_type_name, $has_activated[$messenger_name]); |
|
1071 | - } |
|
1072 | - |
|
1073 | - |
|
1074 | - /** |
|
1075 | - * This method unsets a message type from the given messenger has activated option. |
|
1076 | - * |
|
1077 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1078 | - * |
|
1079 | - * @param string $message_type_name |
|
1080 | - * @param string $messenger_name |
|
1081 | - * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1082 | - * If it is currently active, then remove. Otherwise leave it alone. |
|
1083 | - */ |
|
1084 | - public function remove_message_type_has_been_activated_for_messenger( |
|
1085 | - $message_type_name, |
|
1086 | - $messenger_name, |
|
1087 | - $consider_current_state = false |
|
1088 | - ) { |
|
1089 | - if ($consider_current_state |
|
1090 | - && ! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
1091 | - ) { |
|
1092 | - //when consider current state is true, this means we don't want to change anything on the "has_activated" |
|
1093 | - //record if the message type is currently active for this messenger. This is used when we want to retain |
|
1094 | - //the record for user initiated inactivations of the message type. |
|
1095 | - return; |
|
1096 | - } |
|
1097 | - $has_activated = $this->get_has_activated_messengers_option(); |
|
1098 | - $key_for_message_type = isset($has_activated[$messenger_name]) |
|
1099 | - ? array_search($message_type_name, $has_activated[$messenger_name], true) |
|
1100 | - : false; |
|
1101 | - if ($key_for_message_type !== false) { |
|
1102 | - unset($has_activated[$messenger_name][$key_for_message_type]); |
|
1103 | - $this->update_has_activated_messengers_option($has_activated); |
|
1104 | - //reset the internal cached property |
|
1105 | - $this->get_has_activated_messengers_option(true); |
|
1106 | - } |
|
1107 | - } |
|
1108 | - |
|
1109 | - |
|
1110 | - /** |
|
1111 | - * Removes a message type active record from all messengers it is attached to. |
|
1112 | - * |
|
1113 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1114 | - * |
|
1115 | - * @param $message_type_name |
|
1116 | - * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1117 | - * If it is currently active, then remove. Otherwise leave it alone. |
|
1118 | - */ |
|
1119 | - public function remove_message_type_has_been_activated_from_all_messengers( |
|
1120 | - $message_type_name, |
|
1121 | - $consider_current_state = false |
|
1122 | - ) { |
|
1123 | - foreach(array_keys($this->get_has_activated_messengers_option()) as $messenger_name) { |
|
1124 | - $this->remove_message_type_has_been_activated_for_messenger( |
|
1125 | - $message_type_name, |
|
1126 | - $messenger_name, |
|
1127 | - $consider_current_state |
|
1128 | - ); |
|
1129 | - } |
|
1130 | - } |
|
20 | + /** |
|
21 | + * This option in the database is used to keep a record of message types that have been activated for a messenger |
|
22 | + * at some point in the history of the site. It is utilized by the implementation of the 'force' flag in |
|
23 | + * EE_Register_Message_Type. The force flag is an indication of whether a message type should be activated by |
|
24 | + * default when the message type is registered. However, if a user has explicitly deactivated a message type, then |
|
25 | + * the force flag is ignored. The method by which the code knows whether to ignore this flag is via this option. |
|
26 | + * Note, that this is NOT a historical record. Its entirely possible for a message type to have been activated for |
|
27 | + * a messenger and yet not have a record in this option. This occurs when a message type is inactivated through an |
|
28 | + * automated process (when an add-on registering the message type deactivates, or when some other code calls the |
|
29 | + * EE_Registery_Message_Type::deregister method) and the related record(s) is(are) removed from this option to ensure |
|
30 | + * the "force" flag is respected if that message type is later re-registered. |
|
31 | + * |
|
32 | + * This option should NOT be used to determine the current "active" state of a message type for a given messenger. |
|
33 | + * |
|
34 | + * The name of this option (and related methods/properties) is due to matching the original intended purpose for the |
|
35 | + * option that got superseded by later behaviour requirements. |
|
36 | + */ |
|
37 | + const HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME = 'ee_has_activated_messenger'; |
|
38 | + |
|
39 | + /** |
|
40 | + * @type boolean $_initialized |
|
41 | + */ |
|
42 | + protected $_initialized = false; |
|
43 | + |
|
44 | + /** |
|
45 | + * @type EE_Messenger_Collection $_messenger_collection_loader |
|
46 | + */ |
|
47 | + protected $_messenger_collection_loader; |
|
48 | + |
|
49 | + /** |
|
50 | + * @type EE_Message_Type_Collection $_message_type_collection_loader |
|
51 | + */ |
|
52 | + protected $_message_type_collection_loader; |
|
53 | + |
|
54 | + /** |
|
55 | + * @type EEM_Message_Template_Group $_message_template_group_model |
|
56 | + */ |
|
57 | + protected $_message_template_group_model; |
|
58 | + |
|
59 | + /** |
|
60 | + * @type EE_messenger[] |
|
61 | + */ |
|
62 | + protected $_installed_messengers = array(); |
|
63 | + |
|
64 | + /** |
|
65 | + * @type EE_message_type[] |
|
66 | + */ |
|
67 | + protected $_installed_message_types = array(); |
|
68 | + |
|
69 | + /** |
|
70 | + * Array of active messengers. |
|
71 | + * Format is this: |
|
72 | + * array( |
|
73 | + * 'messenger_name' => EE_messenger |
|
74 | + * ) |
|
75 | + * |
|
76 | + * @type EE_messenger[] |
|
77 | + */ |
|
78 | + protected $_active_messengers = array(); |
|
79 | + |
|
80 | + /** |
|
81 | + * Formatted array of active message types grouped per messenger. |
|
82 | + * Format is this: |
|
83 | + * array( |
|
84 | + * 'messenger_name' => array( |
|
85 | + * 'settings' => array( |
|
86 | + * '{messenger_name}-message_types' => array( |
|
87 | + * 'message_type_name' => array() //variable array of settings corresponding to message type. |
|
88 | + * ) |
|
89 | + * ) |
|
90 | + * ) |
|
91 | + * ) |
|
92 | + * |
|
93 | + * @type array |
|
94 | + */ |
|
95 | + protected $_active_message_types = array(); |
|
96 | + |
|
97 | + |
|
98 | + /** |
|
99 | + * This holds the array of messengers and their corresponding message types that have |
|
100 | + * been activated on a site at some point. This is an important record that helps the messages system |
|
101 | + * not accidentally reactivate something that was intentionally deactivated by a user. |
|
102 | + * |
|
103 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
104 | + * |
|
105 | + * @type array |
|
106 | + */ |
|
107 | + protected $_has_activated_messengers_and_message_types = array(); |
|
108 | + |
|
109 | + /** |
|
110 | + * An array of unique message type contexts across all active message types. |
|
111 | + * The array will be indexed by either 'slugs' or 'all'. |
|
112 | + * The slugs index contains an array indexed by unique context slugs with the latest label representation for that |
|
113 | + * slug. array( |
|
114 | + * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
115 | + * ); |
|
116 | + * The all index returns an array in this format: |
|
117 | + * array( |
|
118 | + * 'message_type_name' => array( |
|
119 | + * 'context_slug' => array( |
|
120 | + * 'label' => 'localized label for context', |
|
121 | + * 'description' => 'localized description for context' |
|
122 | + * ) |
|
123 | + * ) |
|
124 | + * ); |
|
125 | + * |
|
126 | + * @type array |
|
127 | + */ |
|
128 | + protected $_contexts = array(); |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * EE_Message_Resource_Manager constructor. |
|
133 | + * |
|
134 | + * @param \EE_Messenger_Collection_Loader $Messenger_Collection_Loader |
|
135 | + * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader |
|
136 | + * @param \EEM_Message_Template_Group $Message_Template_Group_Model |
|
137 | + */ |
|
138 | + function __construct( |
|
139 | + EE_Messenger_Collection_Loader $Messenger_Collection_Loader, |
|
140 | + EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader, |
|
141 | + EEM_Message_Template_Group $Message_Template_Group_Model |
|
142 | + ) { |
|
143 | + $this->_messenger_collection_loader = $Messenger_Collection_Loader; |
|
144 | + $this->_message_type_collection_loader = $Message_Type_Collection_Loader; |
|
145 | + $this->_message_template_group_model = $Message_Template_Group_Model; |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * @return void |
|
151 | + */ |
|
152 | + protected function _initialize_collections() |
|
153 | + { |
|
154 | + if ($this->_initialized) { |
|
155 | + return; |
|
156 | + } |
|
157 | + $this->_initialized = true; |
|
158 | + $this->_messenger_collection_loader->load_messengers_from_folder(); |
|
159 | + $this->_message_type_collection_loader->load_message_types_from_folder(); |
|
160 | + $this->get_has_activated_messengers_option(true); |
|
161 | + $this->_set_active_messengers_and_message_types(); |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + /** |
|
166 | + * @return EE_Messenger_Collection |
|
167 | + */ |
|
168 | + public function messenger_collection() |
|
169 | + { |
|
170 | + $this->_initialize_collections(); |
|
171 | + return $this->_messenger_collection_loader->messenger_collection(); |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * @return EE_messenger[] |
|
177 | + */ |
|
178 | + public function active_messengers() |
|
179 | + { |
|
180 | + $this->_initialize_collections(); |
|
181 | + return $this->_active_messengers; |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * @param string $messenger_name |
|
187 | + * @return \EE_messenger |
|
188 | + */ |
|
189 | + public function get_messenger($messenger_name) |
|
190 | + { |
|
191 | + return $this->messenger_collection()->get_by_info($messenger_name); |
|
192 | + } |
|
193 | + |
|
194 | + |
|
195 | + /** |
|
196 | + * This returns the corresponding EE_messenger object for the given string if it is active. |
|
197 | + * |
|
198 | + * @param string $messenger |
|
199 | + * @return EE_messenger | null |
|
200 | + */ |
|
201 | + public function get_active_messenger($messenger) |
|
202 | + { |
|
203 | + $this->_initialize_collections(); |
|
204 | + return ! empty($this->_active_messengers[$messenger]) ? $this->_active_messengers[$messenger] : null; |
|
205 | + } |
|
206 | + |
|
207 | + |
|
208 | + /** |
|
209 | + * @return \EE_messenger[] |
|
210 | + */ |
|
211 | + public function installed_messengers() |
|
212 | + { |
|
213 | + if (empty($this->_installed_messengers)) { |
|
214 | + $this->_installed_messengers = array(); |
|
215 | + $this->messenger_collection()->rewind(); |
|
216 | + while ($this->messenger_collection()->valid()) { |
|
217 | + $this->_installed_messengers[$this->messenger_collection()->current()->name] = $this->messenger_collection()->current(); |
|
218 | + $this->messenger_collection()->next(); |
|
219 | + } |
|
220 | + } |
|
221 | + return $this->_installed_messengers; |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + /** |
|
226 | + * @param string $messenger_name |
|
227 | + * @return \EE_messenger |
|
228 | + * @throws \EE_Error |
|
229 | + */ |
|
230 | + public function valid_messenger($messenger_name) |
|
231 | + { |
|
232 | + $messenger = $this->get_messenger($messenger_name); |
|
233 | + if ($messenger instanceof EE_messenger) { |
|
234 | + return $messenger; |
|
235 | + } |
|
236 | + throw new EE_Error( |
|
237 | + sprintf( |
|
238 | + __('The "%1$s" messenger is either invalid or not installed', 'event_espresso'), |
|
239 | + $messenger_name |
|
240 | + ) |
|
241 | + ); |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * @return EE_Message_Type_Collection |
|
247 | + */ |
|
248 | + public function message_type_collection() |
|
249 | + { |
|
250 | + $this->_initialize_collections(); |
|
251 | + return $this->_message_type_collection_loader->message_type_collection(); |
|
252 | + } |
|
253 | + |
|
254 | + |
|
255 | + /** |
|
256 | + * @return array |
|
257 | + */ |
|
258 | + public function active_message_types() |
|
259 | + { |
|
260 | + $this->_initialize_collections(); |
|
261 | + return $this->_active_message_types; |
|
262 | + } |
|
263 | + |
|
264 | + |
|
265 | + /** |
|
266 | + * @param string $message_type_name |
|
267 | + * @return \EE_message_type |
|
268 | + */ |
|
269 | + public function get_message_type($message_type_name) |
|
270 | + { |
|
271 | + return $this->message_type_collection()->get_by_info($message_type_name); |
|
272 | + } |
|
273 | + |
|
274 | + |
|
275 | + /** |
|
276 | + * This returns the EE_message_type from the active message types array ( if present ); |
|
277 | + * |
|
278 | + * @param string $messenger_name |
|
279 | + * @param string $message_type_name |
|
280 | + * @return \EE_message_type|null |
|
281 | + */ |
|
282 | + public function get_active_message_type_for_messenger($messenger_name, $message_type_name) |
|
283 | + { |
|
284 | + return $this->is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
285 | + ? $this->get_message_type($message_type_name) |
|
286 | + : null; |
|
287 | + } |
|
288 | + |
|
289 | + |
|
290 | + /** |
|
291 | + * Returns whether the given message type is active for the given messenger. |
|
292 | + * |
|
293 | + * @param string $messenger_name |
|
294 | + * @param string $message_type_name |
|
295 | + * @return bool |
|
296 | + */ |
|
297 | + public function is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
298 | + { |
|
299 | + $this->_initialize_collections(); |
|
300 | + return ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]); |
|
301 | + } |
|
302 | + |
|
303 | + |
|
304 | + /** |
|
305 | + * Returns whether the given messenger is active. |
|
306 | + * |
|
307 | + * @param string $messenger_name the name of the messenger to check if active. |
|
308 | + * @return bool |
|
309 | + */ |
|
310 | + public function is_messenger_active($messenger_name) |
|
311 | + { |
|
312 | + $this->_initialize_collections(); |
|
313 | + return ! empty($this->_active_message_types[$messenger_name]); |
|
314 | + } |
|
315 | + |
|
316 | + |
|
317 | + /** |
|
318 | + * This returns any settings that might be on a message type for a messenger |
|
319 | + * |
|
320 | + * @param string $messenger_name The slug of the messenger |
|
321 | + * @param string $message_type_name The slug of the message type getting the settings for. |
|
322 | + * @return array |
|
323 | + */ |
|
324 | + public function get_message_type_settings_for_messenger($messenger_name, $message_type_name) |
|
325 | + { |
|
326 | + $settings = array(); |
|
327 | + if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
328 | + $settings = isset($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]['settings']) |
|
329 | + ? $this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]['settings'] |
|
330 | + : array(); |
|
331 | + } |
|
332 | + return $settings; |
|
333 | + } |
|
334 | + |
|
335 | + |
|
336 | + /** |
|
337 | + * Returns whether the given messenger name has active message types on it. |
|
338 | + * Infers whether the messenger is active or not as well. |
|
339 | + * |
|
340 | + * @param string $messenger_name |
|
341 | + * @return bool |
|
342 | + */ |
|
343 | + public function messenger_has_active_message_types($messenger_name) |
|
344 | + { |
|
345 | + $this->_initialize_collections(); |
|
346 | + return |
|
347 | + ! empty($this->_active_message_types[$messenger_name]) |
|
348 | + && ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types']); |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * This checks the _active_message_types property for any active message types |
|
354 | + * that are present for the given messenger and returns them. |
|
355 | + * |
|
356 | + * @since 4.9.0 |
|
357 | + * @param string $messenger_name The messenger being checked |
|
358 | + * @return EE_message_type[]|array (empty array if no active_message_types) |
|
359 | + */ |
|
360 | + public function get_active_message_types_for_messenger($messenger_name) |
|
361 | + { |
|
362 | + $message_types = array(); |
|
363 | + if (! $this->messenger_has_active_message_types($messenger_name)) { |
|
364 | + return $message_types; |
|
365 | + } |
|
366 | + $installed_message_types = $this->installed_message_types(); |
|
367 | + foreach ($installed_message_types as $message_type_name => $message_type) { |
|
368 | + if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
369 | + $message_types[$message_type_name] = $message_type; |
|
370 | + } |
|
371 | + } |
|
372 | + return $message_types; |
|
373 | + } |
|
374 | + |
|
375 | + |
|
376 | + /** |
|
377 | + * This does NOT return the _active_message_types property but |
|
378 | + * simply returns an array of active message type names from that property. |
|
379 | + * (The _active_message_types property is indexed by messenger and active message_types per messenger). |
|
380 | + * |
|
381 | + * @return array message_type references (string) |
|
382 | + */ |
|
383 | + public function list_of_active_message_types() |
|
384 | + { |
|
385 | + $active_message_type_names = array(); |
|
386 | + $this->_initialize_collections(); |
|
387 | + foreach ($this->_active_message_types as $messenger => $messenger_settings) { |
|
388 | + if (! isset($messenger_settings['settings'][$messenger . '-message_types'])) { |
|
389 | + continue; |
|
390 | + } |
|
391 | + foreach ($messenger_settings['settings'][$messenger . '-message_types'] as $message_type_name => $message_type_config) { |
|
392 | + if (! in_array($message_type_name, $active_message_type_names)) { |
|
393 | + $active_message_type_names[] = $message_type_name; |
|
394 | + } |
|
395 | + } |
|
396 | + } |
|
397 | + return $active_message_type_names; |
|
398 | + } |
|
399 | + |
|
400 | + |
|
401 | + /** |
|
402 | + * Same as list_of_active_message_types() except this returns actual EE_message_type objects |
|
403 | + * |
|
404 | + * @since 4.9.0 |
|
405 | + * @return \EE_message_type[] |
|
406 | + */ |
|
407 | + public function get_active_message_type_objects() |
|
408 | + { |
|
409 | + $active_message_types = array(); |
|
410 | + $installed_message_types = $this->installed_message_types(); |
|
411 | + $active_message_type_names = $this->list_of_active_message_types(); |
|
412 | + foreach ($active_message_type_names as $active_message_type_name) { |
|
413 | + if (isset($installed_message_types[$active_message_type_name])) { |
|
414 | + $active_message_types[$active_message_type_name] = $installed_message_types[$active_message_type_name]; |
|
415 | + } |
|
416 | + } |
|
417 | + return $active_message_types; |
|
418 | + } |
|
419 | + |
|
420 | + |
|
421 | + /** |
|
422 | + * @return \EE_message_type[] |
|
423 | + */ |
|
424 | + public function installed_message_types() |
|
425 | + { |
|
426 | + if (empty($this->_installed_message_types)) { |
|
427 | + $this->message_type_collection()->rewind(); |
|
428 | + while ($this->message_type_collection()->valid()) { |
|
429 | + $this->_installed_message_types[$this->message_type_collection()->current()->name] = $this->message_type_collection()->current(); |
|
430 | + $this->message_type_collection()->next(); |
|
431 | + } |
|
432 | + } |
|
433 | + return $this->_installed_message_types; |
|
434 | + } |
|
435 | + |
|
436 | + |
|
437 | + /** |
|
438 | + * @param string $message_type_name |
|
439 | + * @return \EE_message_type |
|
440 | + * @throws \EE_Error |
|
441 | + */ |
|
442 | + public function valid_message_type($message_type_name) |
|
443 | + { |
|
444 | + $message_type = $this->get_message_type($message_type_name); |
|
445 | + if ($message_type instanceof EE_message_type) { |
|
446 | + return $message_type; |
|
447 | + } |
|
448 | + throw new EE_Error( |
|
449 | + sprintf( |
|
450 | + __('The "%1$s" message type is either invalid or not installed', 'event_espresso'), |
|
451 | + $message_type_name |
|
452 | + ) |
|
453 | + ); |
|
454 | + } |
|
455 | + |
|
456 | + |
|
457 | + /** |
|
458 | + * valid_message_type_for_messenger |
|
459 | + * |
|
460 | + * @param EE_messenger $messenger |
|
461 | + * @param string $message_type_name |
|
462 | + * @return boolean |
|
463 | + * @throws \EE_Error |
|
464 | + */ |
|
465 | + public function valid_message_type_for_messenger(EE_messenger $messenger, $message_type_name) |
|
466 | + { |
|
467 | + $valid_message_types = $messenger->get_valid_message_types(); |
|
468 | + if (! in_array($message_type_name, $valid_message_types)) { |
|
469 | + throw new EE_Error( |
|
470 | + sprintf( |
|
471 | + __( |
|
472 | + 'The message type (%1$s) sent to "%2$s" is not valid for the "%3$s" messenger. Double-check the spelling and verify that message type has been registered as a valid type with the messenger.', |
|
473 | + 'event_espresso' |
|
474 | + ), |
|
475 | + $message_type_name, |
|
476 | + __METHOD__, |
|
477 | + $messenger->name |
|
478 | + ) |
|
479 | + ); |
|
480 | + } |
|
481 | + return true; |
|
482 | + } |
|
483 | + |
|
484 | + |
|
485 | + /** |
|
486 | + * Used to return active messengers array stored in the wp options table. |
|
487 | + * If no value is present in the option then an empty array is returned. |
|
488 | + * |
|
489 | + * @param bool $reset If true then we ignore whether the option is cached on the _active_message_types |
|
490 | + * property and pull directly from the db. Otherwise whatever is currently on the |
|
491 | + * $_active_message_types property is pulled. |
|
492 | + * @return array |
|
493 | + */ |
|
494 | + public function get_active_messengers_option($reset = false) |
|
495 | + { |
|
496 | + if ($reset) { |
|
497 | + $this->_active_message_types = get_option('ee_active_messengers', array()); |
|
498 | + } |
|
499 | + return $this->_active_message_types; |
|
500 | + } |
|
501 | + |
|
502 | + |
|
503 | + /** |
|
504 | + * Used to update the active messengers array stored in the wp options table. |
|
505 | + * |
|
506 | + * @param array $active_messenger_settings Incoming data to save. If empty, then the internal cached property |
|
507 | + * representing this data is used. |
|
508 | + * @return bool FALSE if not updated, TRUE if updated. |
|
509 | + */ |
|
510 | + public function update_active_messengers_option($active_messenger_settings = array()) |
|
511 | + { |
|
512 | + $active_messenger_settings = empty($active_messenger_settings) ? $this->_active_message_types : $active_messenger_settings; |
|
513 | + //make sure _active_message_types is updated (this is the internal cache for the settings). |
|
514 | + $this->_active_message_types = $active_messenger_settings; |
|
515 | + return update_option('ee_active_messengers', $active_messenger_settings); |
|
516 | + } |
|
517 | + |
|
518 | + |
|
519 | + /** |
|
520 | + * Used to return has activated message types for messengers array stored in the wp options table. |
|
521 | + * If no value is present in the option then an empty array is returned. |
|
522 | + * The value is cached on the $_has_activated_messengers_and_message_types property for future calls. |
|
523 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
524 | + * |
|
525 | + * @param bool $reset Used to indicate that any cached value should be ignored. |
|
526 | + * @return array |
|
527 | + */ |
|
528 | + public function get_has_activated_messengers_option($reset = false) |
|
529 | + { |
|
530 | + if ($reset || empty($this->_has_activated_messengers_and_message_types)) { |
|
531 | + $this->_has_activated_messengers_and_message_types = get_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, array()); |
|
532 | + } |
|
533 | + return $this->_has_activated_messengers_and_message_types; |
|
534 | + } |
|
535 | + |
|
536 | + |
|
537 | + /** |
|
538 | + * Used to update the has activated option in the db. |
|
539 | + * |
|
540 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
541 | + * |
|
542 | + * @param array $has_activated_messengers Incoming data to save. If empty, then the internal cached property |
|
543 | + * representing this data is used. |
|
544 | + * @return bool FALSE if not updated, TRUE if updated. |
|
545 | + */ |
|
546 | + public function update_has_activated_messengers_option($has_activated_messengers = array()) |
|
547 | + { |
|
548 | + //make sure the option has been retrieved from first so we don't overwrite it accidentally. |
|
549 | + if (empty($has_activated_messengers) && empty($this->_has_activated_messengers_and_message_types)) { |
|
550 | + $this->get_has_activated_messengers_option(); |
|
551 | + } |
|
552 | + $has_activated_messengers = empty($has_activated_messengers) |
|
553 | + ? $this->_has_activated_messengers_and_message_types |
|
554 | + : $has_activated_messengers; |
|
555 | + return update_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, $has_activated_messengers); |
|
556 | + } |
|
557 | + |
|
558 | + |
|
559 | + /** |
|
560 | + * wrapper for _set_active_messengers_and_message_types() |
|
561 | + */ |
|
562 | + public function reset_active_messengers_and_message_types() |
|
563 | + { |
|
564 | + $this->_set_active_messengers_and_message_types(); |
|
565 | + } |
|
566 | + |
|
567 | + |
|
568 | + /** |
|
569 | + * Generate list of active messengers and message types from collection. |
|
570 | + * This sets up the active messengers from what is present in the database. |
|
571 | + */ |
|
572 | + protected function _set_active_messengers_and_message_types() |
|
573 | + { |
|
574 | + //echo "\n\n " . __LINE__ . ") " . __METHOD__ . "() \n"; |
|
575 | + // list of activated messengers as set via the admin |
|
576 | + // note calling `get_active_messengers_options` also initializes the _active_message_types property. |
|
577 | + $this->get_active_messengers_option(true); |
|
578 | + $this->ensure_messengers_are_active(array(), false, true); |
|
579 | + $this->update_active_messengers_option(); |
|
580 | + $this->update_has_activated_messengers_option(); |
|
581 | + } |
|
582 | + |
|
583 | + |
|
584 | + /** |
|
585 | + * Ensures that the specified messenger is currently active. |
|
586 | + * If not, activates it and its default message types. |
|
587 | + * |
|
588 | + * @param string $messenger_name |
|
589 | + * @param bool $update_option Whether to update the option in the db or not. |
|
590 | + * @return boolean true if either already active or successfully activated. |
|
591 | + */ |
|
592 | + public function ensure_messenger_is_active($messenger_name, $update_option = true) |
|
593 | + { |
|
594 | + if (! isset($this->_active_messengers[$messenger_name])) { |
|
595 | + try { |
|
596 | + $this->activate_messenger($messenger_name, array(), $update_option); |
|
597 | + } catch (EE_Error $e) { |
|
598 | + EE_Error::add_error( |
|
599 | + $e->getMessage(), |
|
600 | + __FILE__, |
|
601 | + __FUNCTION__, |
|
602 | + __LINE__ |
|
603 | + ); |
|
604 | + return false; |
|
605 | + } |
|
606 | + } |
|
607 | + return true; |
|
608 | + } |
|
609 | + |
|
610 | + |
|
611 | + /** |
|
612 | + * This ensures the given array of messenger names is active in the system. |
|
613 | + * Note, this method will not activate any NEW message types for the messenger when it is called. Instead, |
|
614 | + * it will automatically activate the default message types for the messenger if its not active. |
|
615 | + * |
|
616 | + * @param array $messenger_names Array of messenger names for messengers to be activated. If an empty array |
|
617 | + * (default) then will attempt to set the active messengers from the |
|
618 | + * activated_messengers option |
|
619 | + * (stored in $_active_message_types property). |
|
620 | + * @param bool $update_option Whether to update the related active messengers option. |
|
621 | + * @param bool $verify Whether to verify the messengers are installed before activating. Note if this is |
|
622 | + * set to true and a messenger is indicated as active, but is NOT installed, then it |
|
623 | + * will automatically be deactivated. |
|
624 | + */ |
|
625 | + public function ensure_messengers_are_active($messenger_names = array(), $update_option = true, $verify = false) |
|
626 | + { |
|
627 | + $messenger_names = empty($messenger_names) ? array_keys($this->_active_message_types) : $messenger_names; |
|
628 | + |
|
629 | + $not_installed = array(); |
|
630 | + foreach ($messenger_names as $messenger_name) { |
|
631 | + if ($verify && ! $this->messenger_collection()->has_by_name($messenger_name)) { |
|
632 | + $not_installed[] = $messenger_name; |
|
633 | + $this->deactivate_messenger($messenger_name); |
|
634 | + continue; |
|
635 | + } |
|
636 | + $this->ensure_messenger_is_active($messenger_name, $update_option); |
|
637 | + } |
|
638 | + |
|
639 | + if (! empty($not_installed)) { |
|
640 | + EE_Error::add_error( |
|
641 | + sprintf( |
|
642 | + __('The following messengers are either not installed or are invalid:%1$s %2$s', 'event_espresso'), |
|
643 | + '<br />', |
|
644 | + implode(', ', $not_installed) |
|
645 | + ), |
|
646 | + __FILE__, __FUNCTION__, __LINE__ |
|
647 | + ); |
|
648 | + } |
|
649 | + } |
|
650 | + |
|
651 | + |
|
652 | + /** |
|
653 | + * Ensures that the specified message type for the given messenger is currently active, if not activates it. |
|
654 | + * This ALSO ensures that the given messenger is active as well! |
|
655 | + * |
|
656 | + * @param string $message_type_name message type name. |
|
657 | + * @param $messenger_name |
|
658 | + * @param bool $update_option Whether to update the option in the db or not. |
|
659 | + * @return bool Returns true if already is active or if was activated successfully. |
|
660 | + * @throws \EE_Error |
|
661 | + */ |
|
662 | + public function ensure_message_type_is_active($message_type_name, $messenger_name, $update_option = true) |
|
663 | + { |
|
664 | + // grab the messenger to work with. |
|
665 | + $messenger = $this->valid_messenger($messenger_name); |
|
666 | + if ($this->valid_message_type_for_messenger($messenger, $message_type_name)) { |
|
667 | + //ensure messenger is active (that's an inherent coupling between active message types and the |
|
668 | + //messenger they are being activated for. |
|
669 | + try { |
|
670 | + if (! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
671 | + //all is good so let's just get it active |
|
672 | + $this->activate_messenger($messenger_name, array($message_type_name), $update_option); |
|
673 | + } |
|
674 | + } catch (EE_Error $e) { |
|
675 | + EE_Error::add_error( |
|
676 | + $e->getMessage(), |
|
677 | + __FILE__, |
|
678 | + __FUNCTION__, |
|
679 | + __LINE__ |
|
680 | + ); |
|
681 | + return false; |
|
682 | + } |
|
683 | + } |
|
684 | + return true; |
|
685 | + } |
|
686 | + |
|
687 | + |
|
688 | + /** |
|
689 | + * This is a wrapper for `ensure_message_type_is_active` that will handle ensuring multiple message types for a |
|
690 | + * messenger are active in one go. |
|
691 | + * |
|
692 | + * @param array $message_type_names Array of message type names to ensure are active. |
|
693 | + * @param string $messenger_name The name of the messenger that the message types are to be activated on. |
|
694 | + * @param bool $update_option Whether to persist the activation to the database or not (default true). |
|
695 | + */ |
|
696 | + public function ensure_message_types_are_active($message_type_names, $messenger_name, $update_option = true) |
|
697 | + { |
|
698 | + $message_type_names = (array)$message_type_names; |
|
699 | + foreach ($message_type_names as $message_type_name) { |
|
700 | + // note, intentionally not updating option here because we're in a loop. |
|
701 | + // We'll follow the instructions of the incoming $update_option argument after the loop. |
|
702 | + $this->ensure_message_type_is_active($message_type_name, $messenger_name, false); |
|
703 | + } |
|
704 | + if ($update_option) { |
|
705 | + $this->update_active_messengers_option(); |
|
706 | + $this->update_has_activated_messengers_option(); |
|
707 | + } |
|
708 | + } |
|
709 | + |
|
710 | + |
|
711 | + /** |
|
712 | + * Activates the specified messenger. |
|
713 | + * |
|
714 | + * @param string $messenger_name |
|
715 | + * @param array $message_type_names An array of message type names to activate with this messenger. |
|
716 | + * If included we do NOT setup the default message types |
|
717 | + * (assuming they are already setup.) |
|
718 | + * @param bool $update_active_messengers_option |
|
719 | + * @return array of generated templates |
|
720 | + * @throws \EE_Error |
|
721 | + */ |
|
722 | + public function activate_messenger( |
|
723 | + $messenger_name, |
|
724 | + $message_type_names = array(), |
|
725 | + $update_active_messengers_option = true |
|
726 | + ) { |
|
727 | + $templates = array(); |
|
728 | + // grab the messenger to work with. |
|
729 | + $messenger = $this->messenger_collection()->get_by_info($messenger_name); |
|
730 | + // it's inactive. Activate it. |
|
731 | + if ($messenger instanceof EE_messenger) { |
|
732 | + $this->_active_messengers[$messenger->name] = $messenger; |
|
733 | + //activate incoming message types set to be activated with messenger. |
|
734 | + $message_type_names = $this->_activate_message_types($messenger, $message_type_names); |
|
735 | + // setup any initial settings for the messenger if necessary. |
|
736 | + $this->add_settings_for_messenger($messenger->name); |
|
737 | + if ($update_active_messengers_option) { |
|
738 | + $this->update_active_messengers_option(); |
|
739 | + $this->update_has_activated_messengers_option(); |
|
740 | + } |
|
741 | + //generate new templates if necessary and ensure all related templates that are already in the database are |
|
742 | + //marked active. Note, this will also deactivate a message type for a messenger if the template |
|
743 | + //cannot be successfully created during its attempt (only happens for global template attempts). |
|
744 | + if (! empty($message_type_names)) { |
|
745 | + $templates = EEH_MSG_Template::generate_new_templates($messenger->name, $message_type_names, 0, true); |
|
746 | + EEH_MSG_Template::update_to_active(array($messenger->name), $message_type_names); |
|
747 | + } |
|
748 | + } |
|
749 | + return $templates; |
|
750 | + } |
|
751 | + |
|
752 | + |
|
753 | + /** |
|
754 | + * Activates given message types for the given EE_messenger object. |
|
755 | + * Note: (very important) This method does not persist the activation to the database. |
|
756 | + * See code implementing this method in this class for examples of how to persist. |
|
757 | + * |
|
758 | + * @param \EE_messenger $messenger |
|
759 | + * @param array $message_type_names |
|
760 | + * @return array |
|
761 | + */ |
|
762 | + protected function _activate_message_types(EE_messenger $messenger, $message_type_names = array()) |
|
763 | + { |
|
764 | + //If $message_type_names is empty, AND $this->_active_message_types is empty, then that means |
|
765 | + //things have never been initialized (which should happen on EEH_Activation::generate_message_templates). |
|
766 | + //So ONLY then do we need to actually grab defaults and cycle through them. Otherwise we |
|
767 | + //only override _active_message_types when an explicit array of $message_type_names has been provided. |
|
768 | + $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[$messenger->name]) |
|
769 | + ? $messenger->get_default_message_types() |
|
770 | + : (array)$message_type_names; |
|
771 | + |
|
772 | + //now we ALWAYS need to make sure that the messenger is active for the message types we're activating! |
|
773 | + if (! isset($this->_active_message_types[$messenger->name])) { |
|
774 | + $this->_active_message_types[$messenger->name]['settings'] = array(); |
|
775 | + } |
|
776 | + |
|
777 | + if ($message_type_names) { |
|
778 | + // cycle thru message types |
|
779 | + foreach ($message_type_names as $message_type_name) { |
|
780 | + //only register the message type as active IF it isn't already active |
|
781 | + //and if its actually installed. |
|
782 | + if ( |
|
783 | + ! $this->is_message_type_active_for_messenger($messenger->name, $message_type_name) |
|
784 | + ) { |
|
785 | + $this->add_settings_for_message_type($messenger->name, $message_type_name); |
|
786 | + $this->_set_messenger_has_activated_message_type( |
|
787 | + $messenger, |
|
788 | + $message_type_name |
|
789 | + ); |
|
790 | + } |
|
791 | + } |
|
792 | + } |
|
793 | + return $message_type_names; |
|
794 | + } |
|
795 | + |
|
796 | + |
|
797 | + /** |
|
798 | + * add_settings_for_message_type |
|
799 | + * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
800 | + * $this->update_active_messengers_option to persist. |
|
801 | + * |
|
802 | + * @param string $messenger_name The name of the messenger adding the settings for |
|
803 | + * @param string $message_type_name The name of the message type adding the settings for |
|
804 | + * @param array $new_settings Any new settings being set for the message type and messenger |
|
805 | + */ |
|
806 | + public function add_settings_for_message_type($messenger_name, $message_type_name, $new_settings = array()) |
|
807 | + { |
|
808 | + // get installed message type from collection |
|
809 | + $message_type = $this->message_type_collection()->get_by_info($message_type_name); |
|
810 | + $existing_settings = $this->get_message_type_settings_for_messenger($messenger_name, $message_type_name); |
|
811 | + //we need to setup any initial settings for message types |
|
812 | + if ($message_type instanceof EE_message_type) { |
|
813 | + $default_settings = $message_type->get_admin_settings_fields(); |
|
814 | + foreach ($default_settings as $field => $values) { |
|
815 | + if (isset($new_settings[$field])) { |
|
816 | + $existing_settings[$field] = $new_settings[$field]; |
|
817 | + continue; |
|
818 | + } |
|
819 | + if (! isset($existing_settings[$field])) { |
|
820 | + $existing_settings[$field] = $values['default']; |
|
821 | + } |
|
822 | + } |
|
823 | + } |
|
824 | + $this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]['settings'] = $existing_settings; |
|
825 | + } |
|
826 | + |
|
827 | + |
|
828 | + /** |
|
829 | + * Updates the internal cached _has_activated_messengers_and_message_types property with the given messenger |
|
830 | + * and message type. |
|
831 | + * |
|
832 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
833 | + * |
|
834 | + * @access protected |
|
835 | + * @param \EE_messenger $messenger |
|
836 | + * @param string $message_type_name |
|
837 | + */ |
|
838 | + protected function _set_messenger_has_activated_message_type(EE_messenger $messenger, $message_type_name) |
|
839 | + { |
|
840 | + |
|
841 | + //if _has_activated_messengers_and_message_types is empty then lets ensure its initialized |
|
842 | + if (empty($this->_has_activated_messengers_and_message_types)) { |
|
843 | + $this->get_has_activated_messengers_option(); |
|
844 | + } |
|
845 | + |
|
846 | + // make sure this messenger has a record in the has_activated array |
|
847 | + if (! isset($this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
848 | + $this->_has_activated_messengers_and_message_types[$messenger->name] = array(); |
|
849 | + } |
|
850 | + // check if message type has already been added |
|
851 | + if (! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
852 | + $this->_has_activated_messengers_and_message_types[$messenger->name][] = $message_type_name; |
|
853 | + } |
|
854 | + } |
|
855 | + |
|
856 | + |
|
857 | + /** |
|
858 | + * add_settings_for_messenger |
|
859 | + * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
860 | + * $this->update_active_messengers_option to persist. |
|
861 | + * |
|
862 | + * @param string $messenger_name The name of the messenger the settings is being added for. |
|
863 | + * @param array $new_settings An array of settings to update the existing settings. |
|
864 | + */ |
|
865 | + public function add_settings_for_messenger($messenger_name, $new_settings = array()) |
|
866 | + { |
|
867 | + $messenger = $this->get_messenger($messenger_name); |
|
868 | + if ($messenger instanceof EE_messenger) { |
|
869 | + $msgr_settings = $messenger->get_admin_settings_fields(); |
|
870 | + if (! empty($msgr_settings)) { |
|
871 | + foreach ($msgr_settings as $field => $value) { |
|
872 | + //is there a new setting for this? |
|
873 | + if (isset($new_settings[$field])) { |
|
874 | + $this->_active_message_types[$messenger->name]['settings'][$field] = $new_settings[$field]; |
|
875 | + continue; |
|
876 | + } |
|
877 | + //only set the default if it isn't already set. |
|
878 | + if (! isset($this->_active_message_types[$messenger->name]['settings'][$field])) { |
|
879 | + $this->_active_message_types[$messenger->name]['settings'][$field] = $value; |
|
880 | + } |
|
881 | + } |
|
882 | + } |
|
883 | + } |
|
884 | + } |
|
885 | + |
|
886 | + |
|
887 | + /** |
|
888 | + * deactivate_messenger |
|
889 | + * |
|
890 | + * @param string|EE_messenger $messenger_name name of messenger |
|
891 | + * @return void |
|
892 | + */ |
|
893 | + public function deactivate_messenger($messenger_name) |
|
894 | + { |
|
895 | + $this->_initialize_collections(); |
|
896 | + if ($messenger_name instanceof EE_messenger) { |
|
897 | + $messenger_name = $messenger_name->name; |
|
898 | + } |
|
899 | + unset($this->_active_messengers[$messenger_name]); |
|
900 | + unset($this->_active_message_types[$messenger_name]); |
|
901 | + $this->_message_template_group_model->deactivate_message_template_groups_for($messenger_name); |
|
902 | + $this->update_active_messengers_option(); |
|
903 | + } |
|
904 | + |
|
905 | + |
|
906 | + /** |
|
907 | + * Deactivates a message type (note this will deactivate across all messenger's it is active on. |
|
908 | + * |
|
909 | + * @param string $message_type_name name of message type being deactivated |
|
910 | + * @param bool $set_has_active_record By default we always record the has_active record when deactivating a message |
|
911 | + * type. However, this can be overridden if we don't want this set (usually when |
|
912 | + * this is called as a part of deregistration of a custom message type) |
|
913 | + */ |
|
914 | + public function deactivate_message_type($message_type_name, $set_has_active_record = true) |
|
915 | + { |
|
916 | + $this->_initialize_collections(); |
|
917 | + if ($message_type_name instanceof EE_message_type) { |
|
918 | + $message_type_name = $message_type_name->name; |
|
919 | + } |
|
920 | + foreach ($this->_active_message_types as $messenger_name => $settings) { |
|
921 | + unset( |
|
922 | + $this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name] |
|
923 | + ); |
|
924 | + |
|
925 | + //we always record (even on deactivation) that a message type has been activated because there should at |
|
926 | + //least be a record in the "has_activated" option that it WAS active at one point. |
|
927 | + if ($set_has_active_record) { |
|
928 | + $messenger = $this->get_messenger($messenger_name); |
|
929 | + $this->_set_messenger_has_activated_message_type($messenger, $message_type_name); |
|
930 | + } |
|
931 | + } |
|
932 | + $this->_message_template_group_model->deactivate_message_template_groups_for('', $message_type_name); |
|
933 | + $this->update_active_messengers_option(); |
|
934 | + $this->update_has_activated_messengers_option(); |
|
935 | + } |
|
936 | + |
|
937 | + |
|
938 | + /** |
|
939 | + * Deactivates a message type for a specific messenger as opposed to all messengers. |
|
940 | + * |
|
941 | + * @param string $message_type_name Name of message type being deactivated. |
|
942 | + * @param string $messenger_name Name of messenger the message type is being deactivated for. |
|
943 | + */ |
|
944 | + public function deactivate_message_type_for_messenger($message_type_name, $messenger_name) |
|
945 | + { |
|
946 | + $this->_initialize_collections(); |
|
947 | + if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
948 | + unset($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]); |
|
949 | + } |
|
950 | + $this->_message_template_group_model->deactivate_message_template_groups_for(array($messenger_name), |
|
951 | + array($message_type_name)); |
|
952 | + $this->update_active_messengers_option(); |
|
953 | + } |
|
954 | + |
|
955 | + |
|
956 | + /** |
|
957 | + * Used to verify if a message can be sent for the given messenger and message type |
|
958 | + * and that it is a generating messenger (used for generating message templates). |
|
959 | + * |
|
960 | + * @param EE_messenger $messenger messenger used in trigger |
|
961 | + * @param EE_message_type $message_type message type used in trigger |
|
962 | + * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send. |
|
963 | + */ |
|
964 | + public function is_generating_messenger_and_active(EE_messenger $messenger, EE_message_type $message_type) |
|
965 | + { |
|
966 | + //get the $messengers the message type says it can be used with. |
|
967 | + foreach ($message_type->with_messengers() as $generating_messenger => $secondary_messengers) { |
|
968 | + if ( |
|
969 | + $messenger->name === $generating_messenger |
|
970 | + && $this->is_message_type_active_for_messenger($messenger->name, $message_type->name) |
|
971 | + ) { |
|
972 | + return true; |
|
973 | + } |
|
974 | + } |
|
975 | + return false; |
|
976 | + } |
|
977 | + |
|
978 | + |
|
979 | + /** |
|
980 | + * This returns all the contexts that are registered by all message types. |
|
981 | + * If $slugs_only is true, |
|
982 | + * then just an array indexed by unique context slugs with the latest label representation for that slug. |
|
983 | + * array( |
|
984 | + * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
985 | + * ); |
|
986 | + * If $slugs_only is false, then the format is: |
|
987 | + * array( |
|
988 | + * 'message_type_name' => array( |
|
989 | + * 'context_slug' => array( |
|
990 | + * 'label' => 'localized label for context', |
|
991 | + * 'description' => 'localized description for context' |
|
992 | + * ) |
|
993 | + * ) |
|
994 | + * ); |
|
995 | + * Keep in mind that although different message types may share the same context slugs, |
|
996 | + * it is possible that the context is described differently by the message type. |
|
997 | + * |
|
998 | + * @since 4.9.0 |
|
999 | + * @param bool $slugs_only Whether to return an array of just slugs and labels (true) |
|
1000 | + * or all contexts indexed by message type. |
|
1001 | + * @return array |
|
1002 | + */ |
|
1003 | + public function get_all_contexts($slugs_only = true) |
|
1004 | + { |
|
1005 | + $key = $slugs_only ? 'slugs' : 'all'; |
|
1006 | + // check if contexts has been setup yet. |
|
1007 | + if (empty($this->_contexts[$key])) { |
|
1008 | + // So let's get all active message type objects and loop through to get all unique contexts |
|
1009 | + foreach ($this->get_active_message_type_objects() as $message_type) { |
|
1010 | + if ($message_type instanceof EE_message_type) { |
|
1011 | + $message_type_contexts = $message_type->get_contexts(); |
|
1012 | + if ($slugs_only) { |
|
1013 | + foreach ($message_type_contexts as $context => $context_details) { |
|
1014 | + $this->_contexts[$key][$context] = $context_details['label']; |
|
1015 | + } |
|
1016 | + } else { |
|
1017 | + $this->_contexts[$key][$message_type->name] = $message_type_contexts; |
|
1018 | + } |
|
1019 | + } |
|
1020 | + } |
|
1021 | + } |
|
1022 | + return ! empty($this->_contexts[$key]) ? $this->_contexts[$key] : array(); |
|
1023 | + } |
|
1024 | + |
|
1025 | + |
|
1026 | + /** |
|
1027 | + * This checks the internal record of what message types are considered "active" and verifies that |
|
1028 | + * there is an installed class definition for that message type. If the active message type does not have a |
|
1029 | + * corresponding accessible message type class then it will be deactivated from all messengers it is active on and |
|
1030 | + * any related message templates will be inactivated as well. |
|
1031 | + * |
|
1032 | + * @return bool true means all active message types are valid, false means at least one message type was |
|
1033 | + * deactivated. |
|
1034 | + */ |
|
1035 | + public function validate_active_message_types_are_installed() |
|
1036 | + { |
|
1037 | + $list_of_active_message_type_names = $this->list_of_active_message_types(); |
|
1038 | + $installed_message_types = $this->installed_message_types(); |
|
1039 | + $all_message_types_valid = true; |
|
1040 | + //loop through list of active message types and verify they are installed. |
|
1041 | + foreach ($list_of_active_message_type_names as $message_type_name) { |
|
1042 | + if (! isset($installed_message_types[$message_type_name])) { |
|
1043 | + $this->remove_message_type_has_been_activated_from_all_messengers( |
|
1044 | + $message_type_name, |
|
1045 | + true |
|
1046 | + ); |
|
1047 | + $this->deactivate_message_type($message_type_name, false); |
|
1048 | + $all_message_types_valid = false; |
|
1049 | + } |
|
1050 | + } |
|
1051 | + return $all_message_types_valid; |
|
1052 | + } |
|
1053 | + |
|
1054 | + |
|
1055 | + /** |
|
1056 | + * This method checks the `ee_has_activated_messenger` option to see if the message type has ever been |
|
1057 | + * activated for the given messenger. This can be called by client code on plugin updates etc to determine whether |
|
1058 | + * to attempt automatically reactivating message types that should be activated by default or not. |
|
1059 | + * |
|
1060 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1061 | + * |
|
1062 | + * @param $message_type_name |
|
1063 | + * @param $messenger_name |
|
1064 | + * @return bool |
|
1065 | + */ |
|
1066 | + public function has_message_type_been_activated_for_messenger($message_type_name, $messenger_name) |
|
1067 | + { |
|
1068 | + $has_activated = $this->get_has_activated_messengers_option(); |
|
1069 | + return isset($has_activated[$messenger_name]) |
|
1070 | + && in_array($message_type_name, $has_activated[$messenger_name]); |
|
1071 | + } |
|
1072 | + |
|
1073 | + |
|
1074 | + /** |
|
1075 | + * This method unsets a message type from the given messenger has activated option. |
|
1076 | + * |
|
1077 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1078 | + * |
|
1079 | + * @param string $message_type_name |
|
1080 | + * @param string $messenger_name |
|
1081 | + * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1082 | + * If it is currently active, then remove. Otherwise leave it alone. |
|
1083 | + */ |
|
1084 | + public function remove_message_type_has_been_activated_for_messenger( |
|
1085 | + $message_type_name, |
|
1086 | + $messenger_name, |
|
1087 | + $consider_current_state = false |
|
1088 | + ) { |
|
1089 | + if ($consider_current_state |
|
1090 | + && ! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
1091 | + ) { |
|
1092 | + //when consider current state is true, this means we don't want to change anything on the "has_activated" |
|
1093 | + //record if the message type is currently active for this messenger. This is used when we want to retain |
|
1094 | + //the record for user initiated inactivations of the message type. |
|
1095 | + return; |
|
1096 | + } |
|
1097 | + $has_activated = $this->get_has_activated_messengers_option(); |
|
1098 | + $key_for_message_type = isset($has_activated[$messenger_name]) |
|
1099 | + ? array_search($message_type_name, $has_activated[$messenger_name], true) |
|
1100 | + : false; |
|
1101 | + if ($key_for_message_type !== false) { |
|
1102 | + unset($has_activated[$messenger_name][$key_for_message_type]); |
|
1103 | + $this->update_has_activated_messengers_option($has_activated); |
|
1104 | + //reset the internal cached property |
|
1105 | + $this->get_has_activated_messengers_option(true); |
|
1106 | + } |
|
1107 | + } |
|
1108 | + |
|
1109 | + |
|
1110 | + /** |
|
1111 | + * Removes a message type active record from all messengers it is attached to. |
|
1112 | + * |
|
1113 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1114 | + * |
|
1115 | + * @param $message_type_name |
|
1116 | + * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1117 | + * If it is currently active, then remove. Otherwise leave it alone. |
|
1118 | + */ |
|
1119 | + public function remove_message_type_has_been_activated_from_all_messengers( |
|
1120 | + $message_type_name, |
|
1121 | + $consider_current_state = false |
|
1122 | + ) { |
|
1123 | + foreach(array_keys($this->get_has_activated_messengers_option()) as $messenger_name) { |
|
1124 | + $this->remove_message_type_has_been_activated_for_messenger( |
|
1125 | + $message_type_name, |
|
1126 | + $messenger_name, |
|
1127 | + $consider_current_state |
|
1128 | + ); |
|
1129 | + } |
|
1130 | + } |
|
1131 | 1131 | } |
1132 | 1132 | // End of file EE_Message_Resource_Manager.lib.php |
1133 | 1133 | // Location: /EE_Message_Resource_Manager.lib.php |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
3 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
4 | 4 | exit('No direct script access allowed'); |
5 | 5 | } |
6 | 6 | |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | public function is_message_type_active_for_messenger($messenger_name, $message_type_name) |
298 | 298 | { |
299 | 299 | $this->_initialize_collections(); |
300 | - return ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]); |
|
300 | + return ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]); |
|
301 | 301 | } |
302 | 302 | |
303 | 303 | |
@@ -325,8 +325,8 @@ discard block |
||
325 | 325 | { |
326 | 326 | $settings = array(); |
327 | 327 | if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
328 | - $settings = isset($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]['settings']) |
|
329 | - ? $this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]['settings'] |
|
328 | + $settings = isset($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]['settings']) |
|
329 | + ? $this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]['settings'] |
|
330 | 330 | : array(); |
331 | 331 | } |
332 | 332 | return $settings; |
@@ -345,7 +345,7 @@ discard block |
||
345 | 345 | $this->_initialize_collections(); |
346 | 346 | return |
347 | 347 | ! empty($this->_active_message_types[$messenger_name]) |
348 | - && ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types']); |
|
348 | + && ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types']); |
|
349 | 349 | } |
350 | 350 | |
351 | 351 | |
@@ -360,7 +360,7 @@ discard block |
||
360 | 360 | public function get_active_message_types_for_messenger($messenger_name) |
361 | 361 | { |
362 | 362 | $message_types = array(); |
363 | - if (! $this->messenger_has_active_message_types($messenger_name)) { |
|
363 | + if ( ! $this->messenger_has_active_message_types($messenger_name)) { |
|
364 | 364 | return $message_types; |
365 | 365 | } |
366 | 366 | $installed_message_types = $this->installed_message_types(); |
@@ -385,11 +385,11 @@ discard block |
||
385 | 385 | $active_message_type_names = array(); |
386 | 386 | $this->_initialize_collections(); |
387 | 387 | foreach ($this->_active_message_types as $messenger => $messenger_settings) { |
388 | - if (! isset($messenger_settings['settings'][$messenger . '-message_types'])) { |
|
388 | + if ( ! isset($messenger_settings['settings'][$messenger.'-message_types'])) { |
|
389 | 389 | continue; |
390 | 390 | } |
391 | - foreach ($messenger_settings['settings'][$messenger . '-message_types'] as $message_type_name => $message_type_config) { |
|
392 | - if (! in_array($message_type_name, $active_message_type_names)) { |
|
391 | + foreach ($messenger_settings['settings'][$messenger.'-message_types'] as $message_type_name => $message_type_config) { |
|
392 | + if ( ! in_array($message_type_name, $active_message_type_names)) { |
|
393 | 393 | $active_message_type_names[] = $message_type_name; |
394 | 394 | } |
395 | 395 | } |
@@ -465,7 +465,7 @@ discard block |
||
465 | 465 | public function valid_message_type_for_messenger(EE_messenger $messenger, $message_type_name) |
466 | 466 | { |
467 | 467 | $valid_message_types = $messenger->get_valid_message_types(); |
468 | - if (! in_array($message_type_name, $valid_message_types)) { |
|
468 | + if ( ! in_array($message_type_name, $valid_message_types)) { |
|
469 | 469 | throw new EE_Error( |
470 | 470 | sprintf( |
471 | 471 | __( |
@@ -591,7 +591,7 @@ discard block |
||
591 | 591 | */ |
592 | 592 | public function ensure_messenger_is_active($messenger_name, $update_option = true) |
593 | 593 | { |
594 | - if (! isset($this->_active_messengers[$messenger_name])) { |
|
594 | + if ( ! isset($this->_active_messengers[$messenger_name])) { |
|
595 | 595 | try { |
596 | 596 | $this->activate_messenger($messenger_name, array(), $update_option); |
597 | 597 | } catch (EE_Error $e) { |
@@ -636,7 +636,7 @@ discard block |
||
636 | 636 | $this->ensure_messenger_is_active($messenger_name, $update_option); |
637 | 637 | } |
638 | 638 | |
639 | - if (! empty($not_installed)) { |
|
639 | + if ( ! empty($not_installed)) { |
|
640 | 640 | EE_Error::add_error( |
641 | 641 | sprintf( |
642 | 642 | __('The following messengers are either not installed or are invalid:%1$s %2$s', 'event_espresso'), |
@@ -667,7 +667,7 @@ discard block |
||
667 | 667 | //ensure messenger is active (that's an inherent coupling between active message types and the |
668 | 668 | //messenger they are being activated for. |
669 | 669 | try { |
670 | - if (! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
670 | + if ( ! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
671 | 671 | //all is good so let's just get it active |
672 | 672 | $this->activate_messenger($messenger_name, array($message_type_name), $update_option); |
673 | 673 | } |
@@ -695,7 +695,7 @@ discard block |
||
695 | 695 | */ |
696 | 696 | public function ensure_message_types_are_active($message_type_names, $messenger_name, $update_option = true) |
697 | 697 | { |
698 | - $message_type_names = (array)$message_type_names; |
|
698 | + $message_type_names = (array) $message_type_names; |
|
699 | 699 | foreach ($message_type_names as $message_type_name) { |
700 | 700 | // note, intentionally not updating option here because we're in a loop. |
701 | 701 | // We'll follow the instructions of the incoming $update_option argument after the loop. |
@@ -741,7 +741,7 @@ discard block |
||
741 | 741 | //generate new templates if necessary and ensure all related templates that are already in the database are |
742 | 742 | //marked active. Note, this will also deactivate a message type for a messenger if the template |
743 | 743 | //cannot be successfully created during its attempt (only happens for global template attempts). |
744 | - if (! empty($message_type_names)) { |
|
744 | + if ( ! empty($message_type_names)) { |
|
745 | 745 | $templates = EEH_MSG_Template::generate_new_templates($messenger->name, $message_type_names, 0, true); |
746 | 746 | EEH_MSG_Template::update_to_active(array($messenger->name), $message_type_names); |
747 | 747 | } |
@@ -767,10 +767,10 @@ discard block |
||
767 | 767 | //only override _active_message_types when an explicit array of $message_type_names has been provided. |
768 | 768 | $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[$messenger->name]) |
769 | 769 | ? $messenger->get_default_message_types() |
770 | - : (array)$message_type_names; |
|
770 | + : (array) $message_type_names; |
|
771 | 771 | |
772 | 772 | //now we ALWAYS need to make sure that the messenger is active for the message types we're activating! |
773 | - if (! isset($this->_active_message_types[$messenger->name])) { |
|
773 | + if ( ! isset($this->_active_message_types[$messenger->name])) { |
|
774 | 774 | $this->_active_message_types[$messenger->name]['settings'] = array(); |
775 | 775 | } |
776 | 776 | |
@@ -816,12 +816,12 @@ discard block |
||
816 | 816 | $existing_settings[$field] = $new_settings[$field]; |
817 | 817 | continue; |
818 | 818 | } |
819 | - if (! isset($existing_settings[$field])) { |
|
819 | + if ( ! isset($existing_settings[$field])) { |
|
820 | 820 | $existing_settings[$field] = $values['default']; |
821 | 821 | } |
822 | 822 | } |
823 | 823 | } |
824 | - $this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]['settings'] = $existing_settings; |
|
824 | + $this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]['settings'] = $existing_settings; |
|
825 | 825 | } |
826 | 826 | |
827 | 827 | |
@@ -844,11 +844,11 @@ discard block |
||
844 | 844 | } |
845 | 845 | |
846 | 846 | // make sure this messenger has a record in the has_activated array |
847 | - if (! isset($this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
847 | + if ( ! isset($this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
848 | 848 | $this->_has_activated_messengers_and_message_types[$messenger->name] = array(); |
849 | 849 | } |
850 | 850 | // check if message type has already been added |
851 | - if (! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
851 | + if ( ! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
852 | 852 | $this->_has_activated_messengers_and_message_types[$messenger->name][] = $message_type_name; |
853 | 853 | } |
854 | 854 | } |
@@ -867,7 +867,7 @@ discard block |
||
867 | 867 | $messenger = $this->get_messenger($messenger_name); |
868 | 868 | if ($messenger instanceof EE_messenger) { |
869 | 869 | $msgr_settings = $messenger->get_admin_settings_fields(); |
870 | - if (! empty($msgr_settings)) { |
|
870 | + if ( ! empty($msgr_settings)) { |
|
871 | 871 | foreach ($msgr_settings as $field => $value) { |
872 | 872 | //is there a new setting for this? |
873 | 873 | if (isset($new_settings[$field])) { |
@@ -875,7 +875,7 @@ discard block |
||
875 | 875 | continue; |
876 | 876 | } |
877 | 877 | //only set the default if it isn't already set. |
878 | - if (! isset($this->_active_message_types[$messenger->name]['settings'][$field])) { |
|
878 | + if ( ! isset($this->_active_message_types[$messenger->name]['settings'][$field])) { |
|
879 | 879 | $this->_active_message_types[$messenger->name]['settings'][$field] = $value; |
880 | 880 | } |
881 | 881 | } |
@@ -919,7 +919,7 @@ discard block |
||
919 | 919 | } |
920 | 920 | foreach ($this->_active_message_types as $messenger_name => $settings) { |
921 | 921 | unset( |
922 | - $this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name] |
|
922 | + $this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name] |
|
923 | 923 | ); |
924 | 924 | |
925 | 925 | //we always record (even on deactivation) that a message type has been activated because there should at |
@@ -945,7 +945,7 @@ discard block |
||
945 | 945 | { |
946 | 946 | $this->_initialize_collections(); |
947 | 947 | if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
948 | - unset($this->_active_message_types[$messenger_name]['settings'][$messenger_name . '-message_types'][$message_type_name]); |
|
948 | + unset($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]); |
|
949 | 949 | } |
950 | 950 | $this->_message_template_group_model->deactivate_message_template_groups_for(array($messenger_name), |
951 | 951 | array($message_type_name)); |
@@ -1039,7 +1039,7 @@ discard block |
||
1039 | 1039 | $all_message_types_valid = true; |
1040 | 1040 | //loop through list of active message types and verify they are installed. |
1041 | 1041 | foreach ($list_of_active_message_type_names as $message_type_name) { |
1042 | - if (! isset($installed_message_types[$message_type_name])) { |
|
1042 | + if ( ! isset($installed_message_types[$message_type_name])) { |
|
1043 | 1043 | $this->remove_message_type_has_been_activated_from_all_messengers( |
1044 | 1044 | $message_type_name, |
1045 | 1045 | true |
@@ -1120,7 +1120,7 @@ discard block |
||
1120 | 1120 | $message_type_name, |
1121 | 1121 | $consider_current_state = false |
1122 | 1122 | ) { |
1123 | - foreach(array_keys($this->get_has_activated_messengers_option()) as $messenger_name) { |
|
1123 | + foreach (array_keys($this->get_has_activated_messengers_option()) as $messenger_name) { |
|
1124 | 1124 | $this->remove_message_type_has_been_activated_for_messenger( |
1125 | 1125 | $message_type_name, |
1126 | 1126 | $messenger_name, |