@@ -12,140 +12,140 @@ |
||
12 | 12 | */ |
13 | 13 | class EE_Message_Factory |
14 | 14 | { |
15 | - protected static ?EE_Message_Factory $_instance = null; |
|
16 | - |
|
17 | - protected EE_Message_Resource_Manager $_message_resource_manager; |
|
18 | - |
|
19 | - |
|
20 | - /** |
|
21 | - * EE_Message_Factory constructor. |
|
22 | - * |
|
23 | - * @param EE_Message_Resource_Manager $Message_Resource_Manager |
|
24 | - */ |
|
25 | - protected function __construct(EE_Message_Resource_Manager $Message_Resource_Manager) |
|
26 | - { |
|
27 | - $this->_message_resource_manager = $Message_Resource_Manager; |
|
28 | - } |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * @singleton method used to instantiate class object |
|
33 | - * @access public |
|
34 | - * @param EE_Message_Resource_Manager $Message_Resource_Manager |
|
35 | - * @return EE_Message_Factory instance |
|
36 | - */ |
|
37 | - public static function instance(EE_Message_Resource_Manager $Message_Resource_Manager): ?EE_Message_Factory |
|
38 | - { |
|
39 | - // check if class object is instantiated, and instantiated properly |
|
40 | - if (! self::$_instance instanceof EE_Message_Factory) { |
|
41 | - self::$_instance = new EE_Message_Factory($Message_Resource_Manager); |
|
42 | - } |
|
43 | - return self::$_instance; |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * @param array $props_n_values |
|
49 | - * @return EE_Message |
|
50 | - */ |
|
51 | - public static function create(array $props_n_values = []): EE_Message |
|
52 | - { |
|
53 | - /** @type EE_Message_Factory $Message_Factory */ |
|
54 | - $Message_Factory = LoaderFactory::getShared('EE_Message_Factory'); |
|
55 | - return $Message_Factory->_create($props_n_values); |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - /** |
|
60 | - * @param EE_Message $message |
|
61 | - * @return EE_Message |
|
62 | - */ |
|
63 | - public static function set_messenger_and_message_type(EE_Message $message): EE_Message |
|
64 | - { |
|
65 | - /** @type EE_Message_Factory $Message_Factory */ |
|
66 | - $Message_Factory = LoaderFactory::getShared('EE_Message_Factory'); |
|
67 | - return $Message_Factory->_set_messenger_and_message_type($message); |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * @param EE_Message $message |
|
73 | - * @return EE_Message |
|
74 | - */ |
|
75 | - public static function set_messenger(EE_Message $message): EE_Message |
|
76 | - { |
|
77 | - /** @type EE_Message_Factory $Message_Factory */ |
|
78 | - $Message_Factory = LoaderFactory::getShared('EE_Message_Factory'); |
|
79 | - return $Message_Factory->_set_messenger($message); |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * @param EE_Message $message |
|
85 | - * @return EE_Message |
|
86 | - */ |
|
87 | - public static function set_message_type(EE_Message $message): EE_Message |
|
88 | - { |
|
89 | - /** @type EE_Message_Factory $Message_Factory */ |
|
90 | - $Message_Factory = LoaderFactory::getShared('EE_Message_Factory'); |
|
91 | - return $Message_Factory->_set_message_type($message); |
|
92 | - } |
|
93 | - |
|
94 | - |
|
95 | - /** |
|
96 | - * @param array $props_n_values |
|
97 | - * @return EE_Message |
|
98 | - */ |
|
99 | - protected function _create(array $props_n_values = []): EE_Message |
|
100 | - { |
|
101 | - $new_instance = false; |
|
102 | - if (! empty($props_n_values['MSG_ID'])) { |
|
103 | - $message = EE_Message::new_instance_from_db($props_n_values); |
|
104 | - } else { |
|
105 | - $message = EE_Message::new_instance($props_n_values); |
|
106 | - $new_instance = true; |
|
107 | - } |
|
108 | - return $this->_set_messenger_and_message_type($message, $new_instance); |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * @param EE_Message $message |
|
114 | - * @param bool $new_instance Whether the message type was setup from the database (false) or not (true) |
|
115 | - * @return EE_Message |
|
116 | - */ |
|
117 | - protected function _set_messenger_and_message_type(EE_Message $message, bool $new_instance = false): EE_Message |
|
118 | - { |
|
119 | - $message = $this->_set_messenger($message); |
|
120 | - return $this->_set_message_type($message, $new_instance); |
|
121 | - } |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * @param EE_Message $message |
|
126 | - * @return EE_Message |
|
127 | - */ |
|
128 | - protected function _set_messenger(EE_Message $message): EE_Message |
|
129 | - { |
|
130 | - $messenger = $this->_message_resource_manager->get_messenger($message->messenger()); |
|
131 | - if ($messenger instanceof EE_messenger) { |
|
132 | - $message->set_messenger_object($messenger); |
|
133 | - } |
|
134 | - return $message; |
|
135 | - } |
|
136 | - |
|
137 | - |
|
138 | - /** |
|
139 | - * @param EE_Message $message |
|
140 | - * @param bool $new_instance Whether the message type was setup from the database (false) or not (true) |
|
141 | - * @return EE_Message |
|
142 | - */ |
|
143 | - protected function _set_message_type(EE_Message $message, bool $new_instance = false): EE_Message |
|
144 | - { |
|
145 | - $message_type = $this->_message_resource_manager->get_message_type($message->message_type()); |
|
146 | - if ($message_type instanceof EE_message_type) { |
|
147 | - $message->set_message_type_object($message_type, $new_instance); |
|
148 | - } |
|
149 | - return $message; |
|
150 | - } |
|
15 | + protected static ?EE_Message_Factory $_instance = null; |
|
16 | + |
|
17 | + protected EE_Message_Resource_Manager $_message_resource_manager; |
|
18 | + |
|
19 | + |
|
20 | + /** |
|
21 | + * EE_Message_Factory constructor. |
|
22 | + * |
|
23 | + * @param EE_Message_Resource_Manager $Message_Resource_Manager |
|
24 | + */ |
|
25 | + protected function __construct(EE_Message_Resource_Manager $Message_Resource_Manager) |
|
26 | + { |
|
27 | + $this->_message_resource_manager = $Message_Resource_Manager; |
|
28 | + } |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * @singleton method used to instantiate class object |
|
33 | + * @access public |
|
34 | + * @param EE_Message_Resource_Manager $Message_Resource_Manager |
|
35 | + * @return EE_Message_Factory instance |
|
36 | + */ |
|
37 | + public static function instance(EE_Message_Resource_Manager $Message_Resource_Manager): ?EE_Message_Factory |
|
38 | + { |
|
39 | + // check if class object is instantiated, and instantiated properly |
|
40 | + if (! self::$_instance instanceof EE_Message_Factory) { |
|
41 | + self::$_instance = new EE_Message_Factory($Message_Resource_Manager); |
|
42 | + } |
|
43 | + return self::$_instance; |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * @param array $props_n_values |
|
49 | + * @return EE_Message |
|
50 | + */ |
|
51 | + public static function create(array $props_n_values = []): EE_Message |
|
52 | + { |
|
53 | + /** @type EE_Message_Factory $Message_Factory */ |
|
54 | + $Message_Factory = LoaderFactory::getShared('EE_Message_Factory'); |
|
55 | + return $Message_Factory->_create($props_n_values); |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + /** |
|
60 | + * @param EE_Message $message |
|
61 | + * @return EE_Message |
|
62 | + */ |
|
63 | + public static function set_messenger_and_message_type(EE_Message $message): EE_Message |
|
64 | + { |
|
65 | + /** @type EE_Message_Factory $Message_Factory */ |
|
66 | + $Message_Factory = LoaderFactory::getShared('EE_Message_Factory'); |
|
67 | + return $Message_Factory->_set_messenger_and_message_type($message); |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * @param EE_Message $message |
|
73 | + * @return EE_Message |
|
74 | + */ |
|
75 | + public static function set_messenger(EE_Message $message): EE_Message |
|
76 | + { |
|
77 | + /** @type EE_Message_Factory $Message_Factory */ |
|
78 | + $Message_Factory = LoaderFactory::getShared('EE_Message_Factory'); |
|
79 | + return $Message_Factory->_set_messenger($message); |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * @param EE_Message $message |
|
85 | + * @return EE_Message |
|
86 | + */ |
|
87 | + public static function set_message_type(EE_Message $message): EE_Message |
|
88 | + { |
|
89 | + /** @type EE_Message_Factory $Message_Factory */ |
|
90 | + $Message_Factory = LoaderFactory::getShared('EE_Message_Factory'); |
|
91 | + return $Message_Factory->_set_message_type($message); |
|
92 | + } |
|
93 | + |
|
94 | + |
|
95 | + /** |
|
96 | + * @param array $props_n_values |
|
97 | + * @return EE_Message |
|
98 | + */ |
|
99 | + protected function _create(array $props_n_values = []): EE_Message |
|
100 | + { |
|
101 | + $new_instance = false; |
|
102 | + if (! empty($props_n_values['MSG_ID'])) { |
|
103 | + $message = EE_Message::new_instance_from_db($props_n_values); |
|
104 | + } else { |
|
105 | + $message = EE_Message::new_instance($props_n_values); |
|
106 | + $new_instance = true; |
|
107 | + } |
|
108 | + return $this->_set_messenger_and_message_type($message, $new_instance); |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * @param EE_Message $message |
|
114 | + * @param bool $new_instance Whether the message type was setup from the database (false) or not (true) |
|
115 | + * @return EE_Message |
|
116 | + */ |
|
117 | + protected function _set_messenger_and_message_type(EE_Message $message, bool $new_instance = false): EE_Message |
|
118 | + { |
|
119 | + $message = $this->_set_messenger($message); |
|
120 | + return $this->_set_message_type($message, $new_instance); |
|
121 | + } |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * @param EE_Message $message |
|
126 | + * @return EE_Message |
|
127 | + */ |
|
128 | + protected function _set_messenger(EE_Message $message): EE_Message |
|
129 | + { |
|
130 | + $messenger = $this->_message_resource_manager->get_messenger($message->messenger()); |
|
131 | + if ($messenger instanceof EE_messenger) { |
|
132 | + $message->set_messenger_object($messenger); |
|
133 | + } |
|
134 | + return $message; |
|
135 | + } |
|
136 | + |
|
137 | + |
|
138 | + /** |
|
139 | + * @param EE_Message $message |
|
140 | + * @param bool $new_instance Whether the message type was setup from the database (false) or not (true) |
|
141 | + * @return EE_Message |
|
142 | + */ |
|
143 | + protected function _set_message_type(EE_Message $message, bool $new_instance = false): EE_Message |
|
144 | + { |
|
145 | + $message_type = $this->_message_resource_manager->get_message_type($message->message_type()); |
|
146 | + if ($message_type instanceof EE_message_type) { |
|
147 | + $message->set_message_type_object($message_type, $new_instance); |
|
148 | + } |
|
149 | + return $message; |
|
150 | + } |
|
151 | 151 | } |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | public static function instance(EE_Message_Resource_Manager $Message_Resource_Manager): ?EE_Message_Factory |
38 | 38 | { |
39 | 39 | // check if class object is instantiated, and instantiated properly |
40 | - if (! self::$_instance instanceof EE_Message_Factory) { |
|
40 | + if ( ! self::$_instance instanceof EE_Message_Factory) { |
|
41 | 41 | self::$_instance = new EE_Message_Factory($Message_Resource_Manager); |
42 | 42 | } |
43 | 43 | return self::$_instance; |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | protected function _create(array $props_n_values = []): EE_Message |
100 | 100 | { |
101 | 101 | $new_instance = false; |
102 | - if (! empty($props_n_values['MSG_ID'])) { |
|
102 | + if ( ! empty($props_n_values['MSG_ID'])) { |
|
103 | 103 | $message = EE_Message::new_instance_from_db($props_n_values); |
104 | 104 | } else { |
105 | 105 | $message = EE_Message::new_instance($props_n_values); |
@@ -11,1139 +11,1139 @@ |
||
11 | 11 | */ |
12 | 12 | class EE_Message_Resource_Manager |
13 | 13 | { |
14 | - /** |
|
15 | - * This option in the database is used to keep a record of message types that have been activated for a messenger |
|
16 | - * at some point in the history of the site. It is utilized by the implementation of the 'force' flag in |
|
17 | - * EE_Register_Message_Type. The force flag is an indication of whether a message type should be activated by |
|
18 | - * default when the message type is registered. However, if a user has explicitly deactivated a message type, then |
|
19 | - * the force flag is ignored. The method by which the code knows whether to ignore this flag is via this option. |
|
20 | - * Note, that this is NOT a historical record. Its entirely possible for a message type to have been activated for |
|
21 | - * a messenger and yet not have a record in this option. This occurs when a message type is inactivated through an |
|
22 | - * automated process (when an add-on registering the message type deactivates, or when some other code calls the |
|
23 | - * EE_Registery_Message_Type::deregister method) and the related record(s) is(are) removed from this option to |
|
24 | - * ensure the "force" flag is respected if that message type is later re-registered. This option should NOT be used |
|
25 | - * to determine the current "active" state of a message type for a given messenger. The name of this option (and |
|
26 | - * related methods/properties) is due to matching the original intended purpose for the option that got superseded |
|
27 | - * by later behaviour requirements. |
|
28 | - */ |
|
29 | - const HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME = 'ee_has_activated_messenger'; |
|
30 | - |
|
31 | - /** |
|
32 | - * @type boolean $_initialized |
|
33 | - */ |
|
34 | - protected $_initialized = false; |
|
35 | - |
|
36 | - /** |
|
37 | - * @type EE_Messenger_Collection $_messenger_collection_loader |
|
38 | - */ |
|
39 | - protected $_messenger_collection_loader; |
|
40 | - |
|
41 | - /** |
|
42 | - * @type EE_Message_Type_Collection $_message_type_collection_loader |
|
43 | - */ |
|
44 | - protected $_message_type_collection_loader; |
|
45 | - |
|
46 | - /** |
|
47 | - * @type EEM_Message_Template_Group $_message_template_group_model |
|
48 | - */ |
|
49 | - protected $_message_template_group_model; |
|
50 | - |
|
51 | - /** |
|
52 | - * @type EE_messenger[] |
|
53 | - */ |
|
54 | - protected $_installed_messengers = []; |
|
55 | - |
|
56 | - /** |
|
57 | - * @type EE_message_type[] |
|
58 | - */ |
|
59 | - protected $_installed_message_types = []; |
|
60 | - |
|
61 | - /** |
|
62 | - * Array of active messengers. |
|
63 | - * Format is this: |
|
64 | - * array( |
|
65 | - * 'messenger_name' => EE_messenger |
|
66 | - * ) |
|
67 | - * |
|
68 | - * @type EE_messenger[] |
|
69 | - */ |
|
70 | - protected $_active_messengers = []; |
|
71 | - |
|
72 | - /** |
|
73 | - * Formatted array of active message types grouped per messenger. |
|
74 | - * Format is this: |
|
75 | - * array( |
|
76 | - * 'messenger_name' => array( |
|
77 | - * 'settings' => array( |
|
78 | - * '{messenger_name}-message_types' => array( |
|
79 | - * 'message_type_name' => array() //variable array of settings corresponding to message type. |
|
80 | - * ) |
|
81 | - * ) |
|
82 | - * ) |
|
83 | - * ) |
|
84 | - * |
|
85 | - * @type array |
|
86 | - */ |
|
87 | - protected $_active_message_types = []; |
|
88 | - |
|
89 | - |
|
90 | - /** |
|
91 | - * This holds the array of messengers and their corresponding message types that have |
|
92 | - * been activated on a site at some point. This is an important record that helps the messages system |
|
93 | - * not accidentally reactivate something that was intentionally deactivated by a user. |
|
94 | - * |
|
95 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
96 | - * details. |
|
97 | - * @type array |
|
98 | - */ |
|
99 | - protected $_has_activated_messengers_and_message_types = []; |
|
100 | - |
|
101 | - /** |
|
102 | - * An array of unique message type contexts across all active message types. |
|
103 | - * The array will be indexed by either 'slugs' or 'all'. |
|
104 | - * The slugs index contains an array indexed by unique context slugs with the latest label representation for that |
|
105 | - * slug. array( |
|
106 | - * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
107 | - * ); |
|
108 | - * The all index returns an array in this format: |
|
109 | - * array( |
|
110 | - * 'message_type_name' => array( |
|
111 | - * 'context_slug' => array( |
|
112 | - * 'label' => 'localized label for context', |
|
113 | - * 'description' => 'localized description for context' |
|
114 | - * ) |
|
115 | - * ) |
|
116 | - * ); |
|
117 | - * |
|
118 | - * @type array |
|
119 | - */ |
|
120 | - protected $_contexts = []; |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * EE_Message_Resource_Manager constructor. |
|
125 | - * |
|
126 | - * @param \EE_Messenger_Collection_Loader $Messenger_Collection_Loader |
|
127 | - * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader |
|
128 | - * @param \EEM_Message_Template_Group $Message_Template_Group_Model |
|
129 | - */ |
|
130 | - public function __construct( |
|
131 | - EE_Messenger_Collection_Loader $Messenger_Collection_Loader, |
|
132 | - EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader, |
|
133 | - EEM_Message_Template_Group $Message_Template_Group_Model |
|
134 | - ) { |
|
135 | - $this->_messenger_collection_loader = $Messenger_Collection_Loader; |
|
136 | - $this->_message_type_collection_loader = $Message_Type_Collection_Loader; |
|
137 | - $this->_message_template_group_model = $Message_Template_Group_Model; |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - /** |
|
142 | - * @return void |
|
143 | - */ |
|
144 | - protected function _initialize_collections() |
|
145 | - { |
|
146 | - if ($this->_initialized) { |
|
147 | - return; |
|
148 | - } |
|
149 | - $this->_initialized = true; |
|
150 | - $this->_messenger_collection_loader->load_messengers_from_folder(); |
|
151 | - $this->_message_type_collection_loader->load_message_types_from_folder(); |
|
152 | - $this->get_has_activated_messengers_option(true); |
|
153 | - $this->_set_active_messengers_and_message_types(); |
|
154 | - } |
|
155 | - |
|
156 | - |
|
157 | - /** |
|
158 | - * @return EE_Messenger_Collection |
|
159 | - */ |
|
160 | - public function messenger_collection() |
|
161 | - { |
|
162 | - $this->_initialize_collections(); |
|
163 | - return $this->_messenger_collection_loader->messenger_collection(); |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * @return EE_messenger[] |
|
169 | - */ |
|
170 | - public function active_messengers() |
|
171 | - { |
|
172 | - $this->_initialize_collections(); |
|
173 | - return $this->_active_messengers; |
|
174 | - } |
|
175 | - |
|
176 | - |
|
177 | - /** |
|
178 | - * @param string $messenger_name |
|
179 | - * @return \EE_messenger |
|
180 | - */ |
|
181 | - public function get_messenger($messenger_name) |
|
182 | - { |
|
183 | - return $this->messenger_collection()->get_by_info($messenger_name); |
|
184 | - } |
|
185 | - |
|
186 | - |
|
187 | - /** |
|
188 | - * This returns the corresponding EE_messenger object for the given string if it is active. |
|
189 | - * |
|
190 | - * @param string $messenger |
|
191 | - * @return EE_messenger | null |
|
192 | - */ |
|
193 | - public function get_active_messenger($messenger) |
|
194 | - { |
|
195 | - $this->_initialize_collections(); |
|
196 | - return ! empty($this->_active_messengers[ $messenger ]) |
|
197 | - ? $this->_active_messengers[ $messenger ] |
|
198 | - : null; |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - /** |
|
203 | - * @return \EE_messenger[] |
|
204 | - */ |
|
205 | - public function installed_messengers() |
|
206 | - { |
|
207 | - if (empty($this->_installed_messengers)) { |
|
208 | - $this->_installed_messengers = []; |
|
209 | - $this->messenger_collection()->rewind(); |
|
210 | - while ($this->messenger_collection()->valid()) { |
|
211 | - $this->_installed_messengers[ $this->messenger_collection()->current()->name ] = |
|
212 | - $this->messenger_collection()->current(); |
|
213 | - $this->messenger_collection()->next(); |
|
214 | - } |
|
215 | - } |
|
216 | - return $this->_installed_messengers; |
|
217 | - } |
|
218 | - |
|
219 | - |
|
220 | - /** |
|
221 | - * @param string $messenger_name |
|
222 | - * @return \EE_messenger |
|
223 | - * @throws EE_Error |
|
224 | - */ |
|
225 | - public function valid_messenger($messenger_name) |
|
226 | - { |
|
227 | - $messenger = $this->get_messenger($messenger_name); |
|
228 | - if ($messenger instanceof EE_messenger) { |
|
229 | - return $messenger; |
|
230 | - } |
|
231 | - throw new EE_Error( |
|
232 | - sprintf( |
|
233 | - esc_html__('The "%1$s" messenger is either invalid or not installed', 'event_espresso'), |
|
234 | - $messenger_name |
|
235 | - ) |
|
236 | - ); |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - /** |
|
241 | - * @return EE_Message_Type_Collection |
|
242 | - */ |
|
243 | - public function message_type_collection() |
|
244 | - { |
|
245 | - $this->_initialize_collections(); |
|
246 | - return $this->_message_type_collection_loader->message_type_collection(); |
|
247 | - } |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * @return array |
|
252 | - */ |
|
253 | - public function active_message_types() |
|
254 | - { |
|
255 | - $this->_initialize_collections(); |
|
256 | - return $this->_active_message_types; |
|
257 | - } |
|
258 | - |
|
259 | - |
|
260 | - /** |
|
261 | - * @param string $message_type_name |
|
262 | - * @return \EE_message_type |
|
263 | - */ |
|
264 | - public function get_message_type($message_type_name) |
|
265 | - { |
|
266 | - return $this->message_type_collection()->get_by_info($message_type_name); |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * This returns the EE_message_type from the active message types array ( if present ); |
|
272 | - * |
|
273 | - * @param string $messenger_name |
|
274 | - * @param string $message_type_name |
|
275 | - * @return \EE_message_type|null |
|
276 | - */ |
|
277 | - public function get_active_message_type_for_messenger($messenger_name, $message_type_name) |
|
278 | - { |
|
279 | - return $this->is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
280 | - ? $this->get_message_type($message_type_name) |
|
281 | - : null; |
|
282 | - } |
|
283 | - |
|
284 | - |
|
285 | - /** |
|
286 | - * Returns whether the given message type is active for the given messenger. |
|
287 | - * |
|
288 | - * @param string $messenger_name |
|
289 | - * @param string $message_type_name |
|
290 | - * @return bool |
|
291 | - */ |
|
292 | - public function is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
293 | - { |
|
294 | - $this->_initialize_collections(); |
|
295 | - return ! empty($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]); |
|
296 | - } |
|
297 | - |
|
298 | - |
|
299 | - /** |
|
300 | - * Returns whether the given messenger is active. |
|
301 | - * |
|
302 | - * @param string $messenger_name the name of the messenger to check if active. |
|
303 | - * @return bool |
|
304 | - */ |
|
305 | - public function is_messenger_active($messenger_name) |
|
306 | - { |
|
307 | - $this->_initialize_collections(); |
|
308 | - return ! empty($this->_active_message_types[ $messenger_name ]); |
|
309 | - } |
|
310 | - |
|
311 | - |
|
312 | - /** |
|
313 | - * This returns any settings that might be on a message type for a messenger |
|
314 | - * |
|
315 | - * @param string $messenger_name The slug of the messenger |
|
316 | - * @param string $message_type_name The slug of the message type getting the settings for. |
|
317 | - * @return array |
|
318 | - */ |
|
319 | - public function get_message_type_settings_for_messenger($messenger_name, $message_type_name) |
|
320 | - { |
|
321 | - $settings = []; |
|
322 | - if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
323 | - $settings = |
|
324 | - isset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings']) |
|
325 | - ? $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] |
|
326 | - : []; |
|
327 | - } |
|
328 | - return $settings; |
|
329 | - } |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * Returns whether the given messenger name has active message types on it. |
|
334 | - * Infers whether the messenger is active or not as well. |
|
335 | - * |
|
336 | - * @param string $messenger_name |
|
337 | - * @return bool |
|
338 | - */ |
|
339 | - public function messenger_has_active_message_types($messenger_name) |
|
340 | - { |
|
341 | - $this->_initialize_collections(); |
|
342 | - return |
|
343 | - ! empty($this->_active_message_types[ $messenger_name ]) |
|
344 | - && ! empty($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ]); |
|
345 | - } |
|
346 | - |
|
347 | - |
|
348 | - /** |
|
349 | - * This checks the _active_message_types property for any active message types |
|
350 | - * that are present for the given messenger and returns them. |
|
351 | - * |
|
352 | - * @param string $messenger_name The messenger being checked |
|
353 | - * @return EE_message_type[]|array (empty array if no active_message_types) |
|
354 | - * @since 4.9.0 |
|
355 | - */ |
|
356 | - public function get_active_message_types_for_messenger($messenger_name) |
|
357 | - { |
|
358 | - $message_types = []; |
|
359 | - if (! $this->messenger_has_active_message_types($messenger_name)) { |
|
360 | - return $message_types; |
|
361 | - } |
|
362 | - $installed_message_types = $this->installed_message_types(); |
|
363 | - foreach ($installed_message_types as $message_type_name => $message_type) { |
|
364 | - if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
365 | - $message_types[ $message_type_name ] = $message_type; |
|
366 | - } |
|
367 | - } |
|
368 | - return $message_types; |
|
369 | - } |
|
370 | - |
|
371 | - |
|
372 | - /** |
|
373 | - * This does NOT return the _active_message_types property but |
|
374 | - * simply returns an array of active message type names from that property. |
|
375 | - * (The _active_message_types property is indexed by messenger and active message_types per messenger). |
|
376 | - * |
|
377 | - * @return array message_type references (string) |
|
378 | - */ |
|
379 | - public function list_of_active_message_types() |
|
380 | - { |
|
381 | - $active_message_type_names = []; |
|
382 | - $this->_initialize_collections(); |
|
383 | - foreach ($this->_active_message_types as $messenger => $messenger_settings) { |
|
384 | - if (! isset($messenger_settings['settings'][ $messenger . '-message_types' ])) { |
|
385 | - continue; |
|
386 | - } |
|
387 | - foreach ($messenger_settings['settings'][ $messenger . '-message_types' ] as $message_type_name => $message_type_config) { |
|
388 | - if (! in_array($message_type_name, $active_message_type_names)) { |
|
389 | - $active_message_type_names[] = $message_type_name; |
|
390 | - } |
|
391 | - } |
|
392 | - } |
|
393 | - return $active_message_type_names; |
|
394 | - } |
|
395 | - |
|
396 | - |
|
397 | - /** |
|
398 | - * Same as list_of_active_message_types() except this returns actual EE_message_type objects |
|
399 | - * |
|
400 | - * @return \EE_message_type[] |
|
401 | - * @since 4.9.0 |
|
402 | - */ |
|
403 | - public function get_active_message_type_objects() |
|
404 | - { |
|
405 | - $active_message_types = []; |
|
406 | - $installed_message_types = $this->installed_message_types(); |
|
407 | - $active_message_type_names = $this->list_of_active_message_types(); |
|
408 | - foreach ($active_message_type_names as $active_message_type_name) { |
|
409 | - if (isset($installed_message_types[ $active_message_type_name ])) { |
|
410 | - $active_message_types[ $active_message_type_name ] = |
|
411 | - $installed_message_types[ $active_message_type_name ]; |
|
412 | - } |
|
413 | - } |
|
414 | - return $active_message_types; |
|
415 | - } |
|
416 | - |
|
417 | - |
|
418 | - /** |
|
419 | - * @return \EE_message_type[] |
|
420 | - */ |
|
421 | - public function installed_message_types() |
|
422 | - { |
|
423 | - if (empty($this->_installed_message_types)) { |
|
424 | - $this->message_type_collection()->rewind(); |
|
425 | - while ($this->message_type_collection()->valid()) { |
|
426 | - $this->_installed_message_types[ $this->message_type_collection()->current()->name ] = |
|
427 | - $this->message_type_collection()->current(); |
|
428 | - $this->message_type_collection()->next(); |
|
429 | - } |
|
430 | - } |
|
431 | - return $this->_installed_message_types; |
|
432 | - } |
|
433 | - |
|
434 | - |
|
435 | - /** |
|
436 | - * @param string $message_type_name |
|
437 | - * @return \EE_message_type |
|
438 | - * @throws EE_Error |
|
439 | - */ |
|
440 | - public function valid_message_type($message_type_name) |
|
441 | - { |
|
442 | - $message_type = $this->get_message_type($message_type_name); |
|
443 | - if ($message_type instanceof EE_message_type) { |
|
444 | - return $message_type; |
|
445 | - } |
|
446 | - throw new EE_Error( |
|
447 | - sprintf( |
|
448 | - esc_html__('The "%1$s" message type is either invalid or not installed', 'event_espresso'), |
|
449 | - $message_type_name |
|
450 | - ) |
|
451 | - ); |
|
452 | - } |
|
453 | - |
|
454 | - |
|
455 | - /** |
|
456 | - * valid_message_type_for_messenger |
|
457 | - * |
|
458 | - * @param EE_messenger $messenger |
|
459 | - * @param string $message_type_name |
|
460 | - * @return boolean |
|
461 | - * @throws EE_Error |
|
462 | - */ |
|
463 | - public function valid_message_type_for_messenger(EE_messenger $messenger, $message_type_name) |
|
464 | - { |
|
465 | - $valid_message_types = $messenger->get_valid_message_types(); |
|
466 | - if (! in_array($message_type_name, $valid_message_types)) { |
|
467 | - throw new EE_Error( |
|
468 | - sprintf( |
|
469 | - esc_html__( |
|
470 | - '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.', |
|
471 | - 'event_espresso' |
|
472 | - ), |
|
473 | - $message_type_name, |
|
474 | - __METHOD__, |
|
475 | - $messenger->name |
|
476 | - ) |
|
477 | - ); |
|
478 | - } |
|
479 | - return true; |
|
480 | - } |
|
481 | - |
|
482 | - |
|
483 | - /** |
|
484 | - * Used to return active messengers array stored in the wp options table. |
|
485 | - * If no value is present in the option then an empty array is returned. |
|
486 | - * |
|
487 | - * @param bool $reset If true then we ignore whether the option is cached on the _active_message_types |
|
488 | - * property and pull directly from the db. Otherwise whatever is currently on the |
|
489 | - * $_active_message_types property is pulled. |
|
490 | - * @return array |
|
491 | - */ |
|
492 | - public function get_active_messengers_option($reset = false) |
|
493 | - { |
|
494 | - if ($reset) { |
|
495 | - $this->_active_message_types = get_option('ee_active_messengers', []); |
|
496 | - } |
|
497 | - return $this->_active_message_types; |
|
498 | - } |
|
499 | - |
|
500 | - |
|
501 | - /** |
|
502 | - * Used to update the active messengers array stored in the wp options table. |
|
503 | - * |
|
504 | - * @param array $active_messenger_settings Incoming data to save. If empty, then the internal cached property |
|
505 | - * representing this data is used. |
|
506 | - * @return bool FALSE if not updated, TRUE if updated. |
|
507 | - */ |
|
508 | - public function update_active_messengers_option($active_messenger_settings = []) |
|
509 | - { |
|
510 | - $active_messenger_settings = empty($active_messenger_settings) |
|
511 | - ? $this->_active_message_types |
|
512 | - : $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 | - * |
|
524 | - * @param bool $reset Used to indicate that any cached value should be ignored. |
|
525 | - * @return array |
|
526 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
527 | - * details. |
|
528 | - */ |
|
529 | - public function get_has_activated_messengers_option($reset = false) |
|
530 | - { |
|
531 | - if ($reset || empty($this->_has_activated_messengers_and_message_types)) { |
|
532 | - $this->_has_activated_messengers_and_message_types = |
|
533 | - get_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, []); |
|
534 | - } |
|
535 | - return $this->_has_activated_messengers_and_message_types; |
|
536 | - } |
|
537 | - |
|
538 | - |
|
539 | - /** |
|
540 | - * Used to update the has activated option in the db. |
|
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 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
546 | - * details. |
|
547 | - */ |
|
548 | - public function update_has_activated_messengers_option($has_activated_messengers = []) |
|
549 | - { |
|
550 | - // make sure the option has been retrieved from first so we don't overwrite it accidentally. |
|
551 | - if (empty($has_activated_messengers) && empty($this->_has_activated_messengers_and_message_types)) { |
|
552 | - $this->get_has_activated_messengers_option(); |
|
553 | - } |
|
554 | - $has_activated_messengers = empty($has_activated_messengers) |
|
555 | - ? $this->_has_activated_messengers_and_message_types |
|
556 | - : $has_activated_messengers; |
|
557 | - return update_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, $has_activated_messengers); |
|
558 | - } |
|
559 | - |
|
560 | - |
|
561 | - /** |
|
562 | - * wrapper for _set_active_messengers_and_message_types() |
|
563 | - */ |
|
564 | - public function reset_active_messengers_and_message_types() |
|
565 | - { |
|
566 | - $this->_set_active_messengers_and_message_types(); |
|
567 | - } |
|
568 | - |
|
569 | - |
|
570 | - /** |
|
571 | - * Generate list of active messengers and message types from collection. |
|
572 | - * This sets up the active messengers from what is present in the database. |
|
573 | - */ |
|
574 | - protected function _set_active_messengers_and_message_types() |
|
575 | - { |
|
576 | - // echo "\n\n " . __LINE__ . ") " . __METHOD__ . "() \n"; |
|
577 | - // list of activated messengers as set via the admin |
|
578 | - // note calling `get_active_messengers_options` also initializes the _active_message_types property. |
|
579 | - $this->get_active_messengers_option(true); |
|
580 | - $this->ensure_messengers_are_active([], false, true); |
|
581 | - $this->update_active_messengers_option(); |
|
582 | - $this->update_has_activated_messengers_option(); |
|
583 | - } |
|
584 | - |
|
585 | - |
|
586 | - /** |
|
587 | - * Ensures that the specified messenger is currently active. |
|
588 | - * If not, activates it and its default message types. |
|
589 | - * |
|
590 | - * @param string $messenger_name |
|
591 | - * @param bool $update_option Whether to update the option in the db or not. |
|
592 | - * @return boolean true if either already active or successfully activated. |
|
593 | - */ |
|
594 | - public function ensure_messenger_is_active($messenger_name, $update_option = true) |
|
595 | - { |
|
596 | - if (! isset($this->_active_messengers[ $messenger_name ])) { |
|
597 | - try { |
|
598 | - $this->activate_messenger($messenger_name, [], $update_option); |
|
599 | - } catch (EE_Error $e) { |
|
600 | - EE_Error::add_error( |
|
601 | - $e->getMessage(), |
|
602 | - __FILE__, |
|
603 | - __FUNCTION__, |
|
604 | - __LINE__ |
|
605 | - ); |
|
606 | - return false; |
|
607 | - } |
|
608 | - } |
|
609 | - return true; |
|
610 | - } |
|
611 | - |
|
612 | - |
|
613 | - /** |
|
614 | - * This ensures the given array of messenger names is active in the system. |
|
615 | - * Note, this method will not activate any NEW message types for the messenger when it is called. Instead, |
|
616 | - * it will automatically activate the default message types for the messenger if its not active. |
|
617 | - * |
|
618 | - * @param array $messenger_names Array of messenger names for messengers to be activated. If an empty array |
|
619 | - * (default) then will attempt to set the active messengers from the |
|
620 | - * activated_messengers option |
|
621 | - * (stored in $_active_message_types property). |
|
622 | - * @param bool $update_option Whether to update the related active messengers option. |
|
623 | - * @param bool $verify Whether to verify the messengers are installed before activating. Note if this is |
|
624 | - * set to true and a messenger is indicated as active, but is NOT installed, then it |
|
625 | - * will automatically be deactivated. |
|
626 | - */ |
|
627 | - public function ensure_messengers_are_active($messenger_names = [], $update_option = true, $verify = false) |
|
628 | - { |
|
629 | - $messenger_names = empty($messenger_names) |
|
630 | - ? array_keys($this->_active_message_types) |
|
631 | - : $messenger_names; |
|
632 | - |
|
633 | - $not_installed = []; |
|
634 | - foreach ($messenger_names as $messenger_name) { |
|
635 | - if ($verify && ! $this->messenger_collection()->has_by_name($messenger_name)) { |
|
636 | - $not_installed[] = $messenger_name; |
|
637 | - $this->deactivate_messenger($messenger_name); |
|
638 | - continue; |
|
639 | - } |
|
640 | - $this->ensure_messenger_is_active($messenger_name, $update_option); |
|
641 | - } |
|
642 | - |
|
643 | - if (! empty($not_installed)) { |
|
644 | - EE_Error::add_error( |
|
645 | - sprintf( |
|
646 | - esc_html__('The following messengers are either not installed or are invalid:%1$s %2$s', |
|
647 | - 'event_espresso'), |
|
648 | - '<br />', |
|
649 | - implode(', ', $not_installed) |
|
650 | - ), |
|
651 | - __FILE__, |
|
652 | - __FUNCTION__, |
|
653 | - __LINE__ |
|
654 | - ); |
|
655 | - } |
|
656 | - } |
|
657 | - |
|
658 | - |
|
659 | - /** |
|
660 | - * Ensures that the specified message type for the given messenger is currently active, if not activates it. |
|
661 | - * This ALSO ensures that the given messenger is active as well! |
|
662 | - * |
|
663 | - * @param string $message_type_name message type name. |
|
664 | - * @param $messenger_name |
|
665 | - * @param bool $update_option Whether to update the option in the db or not. |
|
666 | - * @return bool Returns true if already is active or if was activated successfully. |
|
667 | - * @throws EE_Error |
|
668 | - */ |
|
669 | - public function ensure_message_type_is_active($message_type_name, $messenger_name, $update_option = true) |
|
670 | - { |
|
671 | - // grab the messenger to work with. |
|
672 | - $messenger = $this->valid_messenger($messenger_name); |
|
673 | - if ($this->valid_message_type_for_messenger($messenger, $message_type_name)) { |
|
674 | - // ensure messenger is active (that's an inherent coupling between active message types and the |
|
675 | - // messenger they are being activated for. |
|
676 | - try { |
|
677 | - if (! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
678 | - // all is good so let's just get it active |
|
679 | - $this->activate_messenger($messenger, [$message_type_name], $update_option); |
|
680 | - } |
|
681 | - } catch (EE_Error $e) { |
|
682 | - EE_Error::add_error( |
|
683 | - $e->getMessage(), |
|
684 | - __FILE__, |
|
685 | - __FUNCTION__, |
|
686 | - __LINE__ |
|
687 | - ); |
|
688 | - return false; |
|
689 | - } |
|
690 | - } |
|
691 | - return true; |
|
692 | - } |
|
693 | - |
|
694 | - |
|
695 | - /** |
|
696 | - * This is a wrapper for `ensure_message_type_is_active` that will handle ensuring multiple message types for a |
|
697 | - * messenger are active in one go. |
|
698 | - * |
|
699 | - * @param array $message_type_names Array of message type names to ensure are active. |
|
700 | - * @param string $messenger_name The name of the messenger that the message types are to be activated on. |
|
701 | - * @param bool $update_option Whether to persist the activation to the database or not (default true). |
|
702 | - */ |
|
703 | - public function ensure_message_types_are_active($message_type_names, $messenger_name, $update_option = true) |
|
704 | - { |
|
705 | - $message_type_names = (array) $message_type_names; |
|
706 | - foreach ($message_type_names as $message_type_name) { |
|
707 | - // note, intentionally not updating option here because we're in a loop. |
|
708 | - // We'll follow the instructions of the incoming $update_option argument after the loop. |
|
709 | - $this->ensure_message_type_is_active($message_type_name, $messenger_name, false); |
|
710 | - } |
|
711 | - if ($update_option) { |
|
712 | - $this->update_active_messengers_option(); |
|
713 | - $this->update_has_activated_messengers_option(); |
|
714 | - } |
|
715 | - } |
|
716 | - |
|
717 | - |
|
718 | - /** |
|
719 | - * Activates the specified messenger. |
|
720 | - * |
|
721 | - * @param EE_messenger|string $messenger Instantiated EE_messenger OR messenger name if not already loaded! |
|
722 | - * @param array $message_type_names An array of message type names to activate with this messenger. |
|
723 | - * If included we do NOT setup the default message types |
|
724 | - * (assuming they are already setup.) |
|
725 | - * @param bool $update_active_messengers_option |
|
726 | - * @return array of generated templates |
|
727 | - * @throws EE_Error |
|
728 | - */ |
|
729 | - public function activate_messenger( |
|
730 | - $messenger, |
|
731 | - $message_type_names = [], |
|
732 | - $update_active_messengers_option = true |
|
733 | - ) { |
|
734 | - $templates = []; |
|
735 | - // grab the messenger to work with. |
|
736 | - $messenger = $messenger instanceof EE_messenger |
|
737 | - ? $messenger |
|
738 | - : $this->messenger_collection()->get_by_info($messenger); |
|
739 | - // it's inactive. Activate it. |
|
740 | - if ($messenger instanceof EE_messenger) { |
|
741 | - $this->_active_messengers[ $messenger->name ] = $messenger; |
|
742 | - // activate incoming message types set to be activated with messenger. |
|
743 | - $message_type_names = $this->_activate_message_types($messenger, $message_type_names); |
|
744 | - // setup any initial settings for the messenger if necessary. |
|
745 | - $this->add_settings_for_messenger($messenger->name); |
|
746 | - if ($update_active_messengers_option) { |
|
747 | - $this->update_active_messengers_option(); |
|
748 | - $this->update_has_activated_messengers_option(); |
|
749 | - } |
|
750 | - // generate new templates if necessary and ensure all related templates that are already in the database are |
|
751 | - // marked active. Note, this will also deactivate a message type for a messenger if the template |
|
752 | - // cannot be successfully created during its attempt (only happens for global template attempts). |
|
753 | - if (! empty($message_type_names)) { |
|
754 | - $templates = EEH_MSG_Template::generate_new_templates($messenger->name, $message_type_names, 0, true); |
|
755 | - EEH_MSG_Template::update_to_active([$messenger->name], $message_type_names); |
|
756 | - } |
|
757 | - } |
|
758 | - return $templates; |
|
759 | - } |
|
760 | - |
|
761 | - |
|
762 | - /** |
|
763 | - * Activates given message types for the given EE_messenger object. |
|
764 | - * Note: (very important) This method does not persist the activation to the database. |
|
765 | - * See code implementing this method in this class for examples of how to persist. |
|
766 | - * |
|
767 | - * @param \EE_messenger $messenger |
|
768 | - * @param array $message_type_names |
|
769 | - * @return array |
|
770 | - */ |
|
771 | - protected function _activate_message_types(EE_messenger $messenger, $message_type_names = []) |
|
772 | - { |
|
773 | - // If $message_type_names is empty, AND $this->_active_message_types is empty, then that means |
|
774 | - // things have never been initialized (which should happen on EEH_Activation::generate_message_templates). |
|
775 | - // So ONLY then do we need to actually grab defaults and cycle through them. Otherwise we |
|
776 | - // only override _active_message_types when an explicit array of $message_type_names has been provided. |
|
777 | - $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[ $messenger->name ]) |
|
778 | - ? $messenger->get_default_message_types() |
|
779 | - : (array) $message_type_names; |
|
780 | - |
|
781 | - // now we ALWAYS need to make sure that the messenger is active for the message types we're activating! |
|
782 | - if (! isset($this->_active_message_types[ $messenger->name ])) { |
|
783 | - $this->_active_message_types[ $messenger->name ]['settings'] = []; |
|
784 | - } |
|
785 | - |
|
786 | - if ($message_type_names) { |
|
787 | - // cycle thru message types |
|
788 | - foreach ($message_type_names as $message_type_name) { |
|
789 | - // only register the message type as active IF it isn't already active |
|
790 | - // and if its actually installed. |
|
791 | - if ( |
|
792 | - ! $this->is_message_type_active_for_messenger($messenger->name, $message_type_name) |
|
793 | - ) { |
|
794 | - $this->add_settings_for_message_type($messenger->name, $message_type_name); |
|
795 | - $this->_set_messenger_has_activated_message_type( |
|
796 | - $messenger, |
|
797 | - $message_type_name |
|
798 | - ); |
|
799 | - } |
|
800 | - } |
|
801 | - } |
|
802 | - return $message_type_names; |
|
803 | - } |
|
804 | - |
|
805 | - |
|
806 | - /** |
|
807 | - * add_settings_for_message_type |
|
808 | - * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
809 | - * $this->update_active_messengers_option to persist. |
|
810 | - * |
|
811 | - * @param string $messenger_name The name of the messenger adding the settings for |
|
812 | - * @param string $message_type_name The name of the message type adding the settings for |
|
813 | - * @param array $new_settings Any new settings being set for the message type and messenger |
|
814 | - */ |
|
815 | - public function add_settings_for_message_type($messenger_name, $message_type_name, $new_settings = []) |
|
816 | - { |
|
817 | - // get installed message type from collection |
|
818 | - $message_type = $this->message_type_collection()->get_by_info($message_type_name); |
|
819 | - $existing_settings = $this->get_message_type_settings_for_messenger($messenger_name, $message_type_name); |
|
820 | - // we need to setup any initial settings for message types |
|
821 | - if ($message_type instanceof EE_message_type) { |
|
822 | - $default_settings = $message_type->get_admin_settings_fields(); |
|
823 | - foreach ($default_settings as $field => $values) { |
|
824 | - if (isset($new_settings[ $field ])) { |
|
825 | - $existing_settings[ $field ] = $new_settings[ $field ]; |
|
826 | - continue; |
|
827 | - } |
|
828 | - if (! isset($existing_settings[ $field ])) { |
|
829 | - $existing_settings[ $field ] = $values['default']; |
|
830 | - } |
|
831 | - } |
|
832 | - } |
|
833 | - $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] = |
|
834 | - $existing_settings; |
|
835 | - } |
|
836 | - |
|
837 | - |
|
838 | - /** |
|
839 | - * Updates the internal cached _has_activated_messengers_and_message_types property with the given messenger |
|
840 | - * and message type. |
|
841 | - * |
|
842 | - * @param \EE_messenger $messenger |
|
843 | - * @param string $message_type_name |
|
844 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
845 | - * details. |
|
846 | - * @access protected |
|
847 | - */ |
|
848 | - protected function _set_messenger_has_activated_message_type(EE_messenger $messenger, $message_type_name) |
|
849 | - { |
|
850 | - // if _has_activated_messengers_and_message_types is empty then lets ensure its initialized |
|
851 | - if (empty($this->_has_activated_messengers_and_message_types)) { |
|
852 | - $this->get_has_activated_messengers_option(); |
|
853 | - } |
|
854 | - |
|
855 | - // make sure this messenger has a record in the has_activated array |
|
856 | - if (! isset($this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
857 | - $this->_has_activated_messengers_and_message_types[ $messenger->name ] = []; |
|
858 | - } |
|
859 | - // check if message type has already been added |
|
860 | - if (! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
861 | - $this->_has_activated_messengers_and_message_types[ $messenger->name ][] = $message_type_name; |
|
862 | - } |
|
863 | - } |
|
864 | - |
|
865 | - |
|
866 | - /** |
|
867 | - * add_settings_for_messenger |
|
868 | - * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
869 | - * $this->update_active_messengers_option to persist. |
|
870 | - * |
|
871 | - * @param string $messenger_name The name of the messenger the settings is being added for. |
|
872 | - * @param array $new_settings An array of settings to update the existing settings. |
|
873 | - */ |
|
874 | - public function add_settings_for_messenger($messenger_name, $new_settings = []) |
|
875 | - { |
|
876 | - $messenger = $this->get_messenger($messenger_name); |
|
877 | - if ($messenger instanceof EE_messenger) { |
|
878 | - $msgr_settings = $messenger->get_admin_settings_fields(); |
|
879 | - if (! empty($msgr_settings)) { |
|
880 | - foreach ($msgr_settings as $field => $value) { |
|
881 | - // is there a new setting for this? |
|
882 | - if (isset($new_settings[ $field ])) { |
|
883 | - $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = |
|
884 | - $new_settings[ $field ]; |
|
885 | - continue; |
|
886 | - } |
|
887 | - // only set the default if it isn't already set. |
|
888 | - if (! isset($this->_active_message_types[ $messenger->name ]['settings'][ $field ])) { |
|
889 | - $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $value; |
|
890 | - } |
|
891 | - } |
|
892 | - } |
|
893 | - } |
|
894 | - } |
|
895 | - |
|
896 | - |
|
897 | - /** |
|
898 | - * deactivate_messenger |
|
899 | - * |
|
900 | - * @param string|EE_messenger $messenger_name name of messenger |
|
901 | - * @return void |
|
902 | - */ |
|
903 | - public function deactivate_messenger($messenger_name) |
|
904 | - { |
|
905 | - $this->_initialize_collections(); |
|
906 | - if ($messenger_name instanceof EE_messenger) { |
|
907 | - $messenger_name = $messenger_name->name; |
|
908 | - } |
|
909 | - unset($this->_active_messengers[ $messenger_name ]); |
|
910 | - unset($this->_active_message_types[ $messenger_name ]); |
|
911 | - $this->_message_template_group_model->deactivate_message_template_groups_for($messenger_name); |
|
912 | - $this->update_active_messengers_option(); |
|
913 | - } |
|
914 | - |
|
915 | - |
|
916 | - /** |
|
917 | - * Deactivates a message type (note this will deactivate across all messenger's it is active on. |
|
918 | - * |
|
919 | - * @param string $message_type_name name of message type being deactivated |
|
920 | - * @param bool $set_has_active_record By default we always record the has_active record when deactivating a |
|
921 | - * message type. However, this can be overridden if we don't want this set |
|
922 | - * (usually when this is called as a part of deregistration of a custom |
|
923 | - * message type) |
|
924 | - */ |
|
925 | - public function deactivate_message_type($message_type_name, $set_has_active_record = true) |
|
926 | - { |
|
927 | - $this->_initialize_collections(); |
|
928 | - if ($message_type_name instanceof EE_message_type) { |
|
929 | - $message_type_name = $message_type_name->name; |
|
930 | - } |
|
931 | - foreach ($this->_active_message_types as $messenger_name => $settings) { |
|
932 | - unset( |
|
933 | - $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ] |
|
934 | - ); |
|
935 | - |
|
936 | - // we always record (even on deactivation) that a message type has been activated because there should at |
|
937 | - // least be a record in the "has_activated" option that it WAS active at one point. |
|
938 | - if ($set_has_active_record) { |
|
939 | - $messenger = $this->get_messenger($messenger_name); |
|
940 | - $this->_set_messenger_has_activated_message_type($messenger, $message_type_name); |
|
941 | - } |
|
942 | - } |
|
943 | - $this->_message_template_group_model->deactivate_message_template_groups_for('', $message_type_name); |
|
944 | - $this->update_active_messengers_option(); |
|
945 | - $this->update_has_activated_messengers_option(); |
|
946 | - } |
|
947 | - |
|
948 | - |
|
949 | - /** |
|
950 | - * Deactivates a message type for a specific messenger as opposed to all messengers. |
|
951 | - * |
|
952 | - * @param string $message_type_name Name of message type being deactivated. |
|
953 | - * @param string $messenger_name Name of messenger the message type is being deactivated for. |
|
954 | - */ |
|
955 | - public function deactivate_message_type_for_messenger($message_type_name, $messenger_name) |
|
956 | - { |
|
957 | - $this->_initialize_collections(); |
|
958 | - if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
959 | - unset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]); |
|
960 | - } |
|
961 | - $this->_message_template_group_model->deactivate_message_template_groups_for( |
|
962 | - [$messenger_name], |
|
963 | - [$message_type_name] |
|
964 | - ); |
|
965 | - $this->update_active_messengers_option(); |
|
966 | - } |
|
967 | - |
|
968 | - |
|
969 | - /** |
|
970 | - * Used to verify if a message can be sent for the given messenger and message type |
|
971 | - * and that it is a generating messenger (used for generating message templates). |
|
972 | - * |
|
973 | - * @param EE_messenger $messenger messenger used in trigger |
|
974 | - * @param EE_message_type $message_type message type used in trigger |
|
975 | - * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send. |
|
976 | - */ |
|
977 | - public function is_generating_messenger_and_active(EE_messenger $messenger, EE_message_type $message_type) |
|
978 | - { |
|
979 | - // get the $messengers the message type says it can be used with. |
|
980 | - foreach ($message_type->with_messengers() as $generating_messenger => $secondary_messengers) { |
|
981 | - if ( |
|
982 | - $messenger->name === $generating_messenger |
|
983 | - && $this->is_message_type_active_for_messenger($messenger->name, $message_type->name) |
|
984 | - ) { |
|
985 | - return true; |
|
986 | - } |
|
987 | - } |
|
988 | - return false; |
|
989 | - } |
|
990 | - |
|
991 | - |
|
992 | - /** |
|
993 | - * This returns all the contexts that are registered by all message types. |
|
994 | - * If $slugs_only is true, |
|
995 | - * then just an array indexed by unique context slugs with the latest label representation for that slug. |
|
996 | - * array( |
|
997 | - * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
998 | - * ); |
|
999 | - * If $slugs_only is false, then the format is: |
|
1000 | - * array( |
|
1001 | - * 'message_type_name' => array( |
|
1002 | - * 'context_slug' => array( |
|
1003 | - * 'label' => 'localized label for context', |
|
1004 | - * 'description' => 'localized description for context' |
|
1005 | - * ) |
|
1006 | - * ) |
|
1007 | - * ); |
|
1008 | - * Keep in mind that although different message types may share the same context slugs, |
|
1009 | - * it is possible that the context is described differently by the message type. |
|
1010 | - * |
|
1011 | - * @param bool $slugs_only Whether to return an array of just slugs and labels (true) |
|
1012 | - * or all contexts indexed by message type. |
|
1013 | - * @return array |
|
1014 | - * @since 4.9.0 |
|
1015 | - */ |
|
1016 | - public function get_all_contexts($slugs_only = true) |
|
1017 | - { |
|
1018 | - $key = $slugs_only |
|
1019 | - ? 'slugs' |
|
1020 | - : 'all'; |
|
1021 | - // check if contexts has been setup yet. |
|
1022 | - if (empty($this->_contexts[ $key ])) { |
|
1023 | - // So let's get all active message type objects and loop through to get all unique contexts |
|
1024 | - foreach ($this->get_active_message_type_objects() as $message_type) { |
|
1025 | - if ($message_type instanceof EE_message_type) { |
|
1026 | - $message_type_contexts = $message_type->get_contexts(); |
|
1027 | - if ($slugs_only) { |
|
1028 | - foreach ($message_type_contexts as $context => $context_details) { |
|
1029 | - $this->_contexts[ $key ][ $context ] = $context_details['label']; |
|
1030 | - } |
|
1031 | - } else { |
|
1032 | - $this->_contexts[ $key ][ $message_type->name ] = $message_type_contexts; |
|
1033 | - } |
|
1034 | - } |
|
1035 | - } |
|
1036 | - } |
|
1037 | - return ! empty($this->_contexts[ $key ]) |
|
1038 | - ? $this->_contexts[ $key ] |
|
1039 | - : []; |
|
1040 | - } |
|
1041 | - |
|
1042 | - |
|
1043 | - /** |
|
1044 | - * This checks the internal record of what message types are considered "active" and verifies that |
|
1045 | - * there is an installed class definition for that message type. If the active message type does not have a |
|
1046 | - * corresponding accessible message type class then it will be deactivated from all messengers it is active on and |
|
1047 | - * any related message templates will be inactivated as well. |
|
1048 | - * |
|
1049 | - * @return bool true means all active message types are valid, false means at least one message type was |
|
1050 | - * deactivated. |
|
1051 | - */ |
|
1052 | - public function validate_active_message_types_are_installed() |
|
1053 | - { |
|
1054 | - $list_of_active_message_type_names = $this->list_of_active_message_types(); |
|
1055 | - $installed_message_types = $this->installed_message_types(); |
|
1056 | - $all_message_types_valid = true; |
|
1057 | - // loop through list of active message types and verify they are installed. |
|
1058 | - foreach ($list_of_active_message_type_names as $message_type_name) { |
|
1059 | - if (! isset($installed_message_types[ $message_type_name ])) { |
|
1060 | - $this->remove_message_type_has_been_activated_from_all_messengers( |
|
1061 | - $message_type_name, |
|
1062 | - true |
|
1063 | - ); |
|
1064 | - $this->deactivate_message_type($message_type_name, false); |
|
1065 | - $all_message_types_valid = false; |
|
1066 | - } |
|
1067 | - } |
|
1068 | - return $all_message_types_valid; |
|
1069 | - } |
|
1070 | - |
|
1071 | - |
|
1072 | - /** |
|
1073 | - * This method checks the `ee_has_activated_messenger` option to see if the message type has ever been |
|
1074 | - * activated for the given messenger. This can be called by client code on plugin updates etc to determine whether |
|
1075 | - * to attempt automatically reactivating message types that should be activated by default or not. |
|
1076 | - * |
|
1077 | - * @param $message_type_name |
|
1078 | - * @param $messenger_name |
|
1079 | - * @return bool |
|
1080 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
1081 | - * details. |
|
1082 | - */ |
|
1083 | - public function has_message_type_been_activated_for_messenger($message_type_name, $messenger_name) |
|
1084 | - { |
|
1085 | - $has_activated = $this->get_has_activated_messengers_option(); |
|
1086 | - return isset($has_activated[ $messenger_name ]) |
|
1087 | - && in_array($message_type_name, $has_activated[ $messenger_name ]); |
|
1088 | - } |
|
1089 | - |
|
1090 | - |
|
1091 | - /** |
|
1092 | - * This method unsets a message type from the given messenger has activated option. |
|
1093 | - * |
|
1094 | - * @param string $message_type_name |
|
1095 | - * @param string $messenger_name |
|
1096 | - * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1097 | - * If it is currently active, then remove. Otherwise leave it alone. |
|
1098 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
1099 | - * details. |
|
1100 | - */ |
|
1101 | - public function remove_message_type_has_been_activated_for_messenger( |
|
1102 | - $message_type_name, |
|
1103 | - $messenger_name, |
|
1104 | - $consider_current_state = false |
|
1105 | - ) { |
|
1106 | - if ( |
|
1107 | - $consider_current_state |
|
1108 | - && ! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
1109 | - ) { |
|
1110 | - // when consider current state is true, this means we don't want to change anything on the "has_activated" |
|
1111 | - // record if the message type is currently active for this messenger. This is used when we want to retain |
|
1112 | - // the record for user initiated inactivations of the message type. |
|
1113 | - return; |
|
1114 | - } |
|
1115 | - $has_activated = $this->get_has_activated_messengers_option(); |
|
1116 | - $key_for_message_type = isset($has_activated[ $messenger_name ]) |
|
1117 | - ? array_search($message_type_name, $has_activated[ $messenger_name ], true) |
|
1118 | - : false; |
|
1119 | - if ($key_for_message_type !== false) { |
|
1120 | - unset($has_activated[ $messenger_name ][ $key_for_message_type ]); |
|
1121 | - $this->update_has_activated_messengers_option($has_activated); |
|
1122 | - // reset the internal cached property |
|
1123 | - $this->get_has_activated_messengers_option(true); |
|
1124 | - } |
|
1125 | - } |
|
1126 | - |
|
1127 | - |
|
1128 | - /** |
|
1129 | - * Removes a message type active record from all messengers it is attached to. |
|
1130 | - * |
|
1131 | - * @param $message_type_name |
|
1132 | - * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1133 | - * If it is currently active, then remove. Otherwise leave it alone. |
|
1134 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
1135 | - * details. |
|
1136 | - */ |
|
1137 | - public function remove_message_type_has_been_activated_from_all_messengers( |
|
1138 | - $message_type_name, |
|
1139 | - $consider_current_state = false |
|
1140 | - ) { |
|
1141 | - foreach (array_keys($this->get_has_activated_messengers_option()) as $messenger_name) { |
|
1142 | - $this->remove_message_type_has_been_activated_for_messenger( |
|
1143 | - $message_type_name, |
|
1144 | - $messenger_name, |
|
1145 | - $consider_current_state |
|
1146 | - ); |
|
1147 | - } |
|
1148 | - } |
|
14 | + /** |
|
15 | + * This option in the database is used to keep a record of message types that have been activated for a messenger |
|
16 | + * at some point in the history of the site. It is utilized by the implementation of the 'force' flag in |
|
17 | + * EE_Register_Message_Type. The force flag is an indication of whether a message type should be activated by |
|
18 | + * default when the message type is registered. However, if a user has explicitly deactivated a message type, then |
|
19 | + * the force flag is ignored. The method by which the code knows whether to ignore this flag is via this option. |
|
20 | + * Note, that this is NOT a historical record. Its entirely possible for a message type to have been activated for |
|
21 | + * a messenger and yet not have a record in this option. This occurs when a message type is inactivated through an |
|
22 | + * automated process (when an add-on registering the message type deactivates, or when some other code calls the |
|
23 | + * EE_Registery_Message_Type::deregister method) and the related record(s) is(are) removed from this option to |
|
24 | + * ensure the "force" flag is respected if that message type is later re-registered. This option should NOT be used |
|
25 | + * to determine the current "active" state of a message type for a given messenger. The name of this option (and |
|
26 | + * related methods/properties) is due to matching the original intended purpose for the option that got superseded |
|
27 | + * by later behaviour requirements. |
|
28 | + */ |
|
29 | + const HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME = 'ee_has_activated_messenger'; |
|
30 | + |
|
31 | + /** |
|
32 | + * @type boolean $_initialized |
|
33 | + */ |
|
34 | + protected $_initialized = false; |
|
35 | + |
|
36 | + /** |
|
37 | + * @type EE_Messenger_Collection $_messenger_collection_loader |
|
38 | + */ |
|
39 | + protected $_messenger_collection_loader; |
|
40 | + |
|
41 | + /** |
|
42 | + * @type EE_Message_Type_Collection $_message_type_collection_loader |
|
43 | + */ |
|
44 | + protected $_message_type_collection_loader; |
|
45 | + |
|
46 | + /** |
|
47 | + * @type EEM_Message_Template_Group $_message_template_group_model |
|
48 | + */ |
|
49 | + protected $_message_template_group_model; |
|
50 | + |
|
51 | + /** |
|
52 | + * @type EE_messenger[] |
|
53 | + */ |
|
54 | + protected $_installed_messengers = []; |
|
55 | + |
|
56 | + /** |
|
57 | + * @type EE_message_type[] |
|
58 | + */ |
|
59 | + protected $_installed_message_types = []; |
|
60 | + |
|
61 | + /** |
|
62 | + * Array of active messengers. |
|
63 | + * Format is this: |
|
64 | + * array( |
|
65 | + * 'messenger_name' => EE_messenger |
|
66 | + * ) |
|
67 | + * |
|
68 | + * @type EE_messenger[] |
|
69 | + */ |
|
70 | + protected $_active_messengers = []; |
|
71 | + |
|
72 | + /** |
|
73 | + * Formatted array of active message types grouped per messenger. |
|
74 | + * Format is this: |
|
75 | + * array( |
|
76 | + * 'messenger_name' => array( |
|
77 | + * 'settings' => array( |
|
78 | + * '{messenger_name}-message_types' => array( |
|
79 | + * 'message_type_name' => array() //variable array of settings corresponding to message type. |
|
80 | + * ) |
|
81 | + * ) |
|
82 | + * ) |
|
83 | + * ) |
|
84 | + * |
|
85 | + * @type array |
|
86 | + */ |
|
87 | + protected $_active_message_types = []; |
|
88 | + |
|
89 | + |
|
90 | + /** |
|
91 | + * This holds the array of messengers and their corresponding message types that have |
|
92 | + * been activated on a site at some point. This is an important record that helps the messages system |
|
93 | + * not accidentally reactivate something that was intentionally deactivated by a user. |
|
94 | + * |
|
95 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
96 | + * details. |
|
97 | + * @type array |
|
98 | + */ |
|
99 | + protected $_has_activated_messengers_and_message_types = []; |
|
100 | + |
|
101 | + /** |
|
102 | + * An array of unique message type contexts across all active message types. |
|
103 | + * The array will be indexed by either 'slugs' or 'all'. |
|
104 | + * The slugs index contains an array indexed by unique context slugs with the latest label representation for that |
|
105 | + * slug. array( |
|
106 | + * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
107 | + * ); |
|
108 | + * The all index returns an array in this format: |
|
109 | + * array( |
|
110 | + * 'message_type_name' => array( |
|
111 | + * 'context_slug' => array( |
|
112 | + * 'label' => 'localized label for context', |
|
113 | + * 'description' => 'localized description for context' |
|
114 | + * ) |
|
115 | + * ) |
|
116 | + * ); |
|
117 | + * |
|
118 | + * @type array |
|
119 | + */ |
|
120 | + protected $_contexts = []; |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * EE_Message_Resource_Manager constructor. |
|
125 | + * |
|
126 | + * @param \EE_Messenger_Collection_Loader $Messenger_Collection_Loader |
|
127 | + * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader |
|
128 | + * @param \EEM_Message_Template_Group $Message_Template_Group_Model |
|
129 | + */ |
|
130 | + public function __construct( |
|
131 | + EE_Messenger_Collection_Loader $Messenger_Collection_Loader, |
|
132 | + EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader, |
|
133 | + EEM_Message_Template_Group $Message_Template_Group_Model |
|
134 | + ) { |
|
135 | + $this->_messenger_collection_loader = $Messenger_Collection_Loader; |
|
136 | + $this->_message_type_collection_loader = $Message_Type_Collection_Loader; |
|
137 | + $this->_message_template_group_model = $Message_Template_Group_Model; |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + /** |
|
142 | + * @return void |
|
143 | + */ |
|
144 | + protected function _initialize_collections() |
|
145 | + { |
|
146 | + if ($this->_initialized) { |
|
147 | + return; |
|
148 | + } |
|
149 | + $this->_initialized = true; |
|
150 | + $this->_messenger_collection_loader->load_messengers_from_folder(); |
|
151 | + $this->_message_type_collection_loader->load_message_types_from_folder(); |
|
152 | + $this->get_has_activated_messengers_option(true); |
|
153 | + $this->_set_active_messengers_and_message_types(); |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + /** |
|
158 | + * @return EE_Messenger_Collection |
|
159 | + */ |
|
160 | + public function messenger_collection() |
|
161 | + { |
|
162 | + $this->_initialize_collections(); |
|
163 | + return $this->_messenger_collection_loader->messenger_collection(); |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * @return EE_messenger[] |
|
169 | + */ |
|
170 | + public function active_messengers() |
|
171 | + { |
|
172 | + $this->_initialize_collections(); |
|
173 | + return $this->_active_messengers; |
|
174 | + } |
|
175 | + |
|
176 | + |
|
177 | + /** |
|
178 | + * @param string $messenger_name |
|
179 | + * @return \EE_messenger |
|
180 | + */ |
|
181 | + public function get_messenger($messenger_name) |
|
182 | + { |
|
183 | + return $this->messenger_collection()->get_by_info($messenger_name); |
|
184 | + } |
|
185 | + |
|
186 | + |
|
187 | + /** |
|
188 | + * This returns the corresponding EE_messenger object for the given string if it is active. |
|
189 | + * |
|
190 | + * @param string $messenger |
|
191 | + * @return EE_messenger | null |
|
192 | + */ |
|
193 | + public function get_active_messenger($messenger) |
|
194 | + { |
|
195 | + $this->_initialize_collections(); |
|
196 | + return ! empty($this->_active_messengers[ $messenger ]) |
|
197 | + ? $this->_active_messengers[ $messenger ] |
|
198 | + : null; |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + /** |
|
203 | + * @return \EE_messenger[] |
|
204 | + */ |
|
205 | + public function installed_messengers() |
|
206 | + { |
|
207 | + if (empty($this->_installed_messengers)) { |
|
208 | + $this->_installed_messengers = []; |
|
209 | + $this->messenger_collection()->rewind(); |
|
210 | + while ($this->messenger_collection()->valid()) { |
|
211 | + $this->_installed_messengers[ $this->messenger_collection()->current()->name ] = |
|
212 | + $this->messenger_collection()->current(); |
|
213 | + $this->messenger_collection()->next(); |
|
214 | + } |
|
215 | + } |
|
216 | + return $this->_installed_messengers; |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + /** |
|
221 | + * @param string $messenger_name |
|
222 | + * @return \EE_messenger |
|
223 | + * @throws EE_Error |
|
224 | + */ |
|
225 | + public function valid_messenger($messenger_name) |
|
226 | + { |
|
227 | + $messenger = $this->get_messenger($messenger_name); |
|
228 | + if ($messenger instanceof EE_messenger) { |
|
229 | + return $messenger; |
|
230 | + } |
|
231 | + throw new EE_Error( |
|
232 | + sprintf( |
|
233 | + esc_html__('The "%1$s" messenger is either invalid or not installed', 'event_espresso'), |
|
234 | + $messenger_name |
|
235 | + ) |
|
236 | + ); |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * @return EE_Message_Type_Collection |
|
242 | + */ |
|
243 | + public function message_type_collection() |
|
244 | + { |
|
245 | + $this->_initialize_collections(); |
|
246 | + return $this->_message_type_collection_loader->message_type_collection(); |
|
247 | + } |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * @return array |
|
252 | + */ |
|
253 | + public function active_message_types() |
|
254 | + { |
|
255 | + $this->_initialize_collections(); |
|
256 | + return $this->_active_message_types; |
|
257 | + } |
|
258 | + |
|
259 | + |
|
260 | + /** |
|
261 | + * @param string $message_type_name |
|
262 | + * @return \EE_message_type |
|
263 | + */ |
|
264 | + public function get_message_type($message_type_name) |
|
265 | + { |
|
266 | + return $this->message_type_collection()->get_by_info($message_type_name); |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * This returns the EE_message_type from the active message types array ( if present ); |
|
272 | + * |
|
273 | + * @param string $messenger_name |
|
274 | + * @param string $message_type_name |
|
275 | + * @return \EE_message_type|null |
|
276 | + */ |
|
277 | + public function get_active_message_type_for_messenger($messenger_name, $message_type_name) |
|
278 | + { |
|
279 | + return $this->is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
280 | + ? $this->get_message_type($message_type_name) |
|
281 | + : null; |
|
282 | + } |
|
283 | + |
|
284 | + |
|
285 | + /** |
|
286 | + * Returns whether the given message type is active for the given messenger. |
|
287 | + * |
|
288 | + * @param string $messenger_name |
|
289 | + * @param string $message_type_name |
|
290 | + * @return bool |
|
291 | + */ |
|
292 | + public function is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
293 | + { |
|
294 | + $this->_initialize_collections(); |
|
295 | + return ! empty($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]); |
|
296 | + } |
|
297 | + |
|
298 | + |
|
299 | + /** |
|
300 | + * Returns whether the given messenger is active. |
|
301 | + * |
|
302 | + * @param string $messenger_name the name of the messenger to check if active. |
|
303 | + * @return bool |
|
304 | + */ |
|
305 | + public function is_messenger_active($messenger_name) |
|
306 | + { |
|
307 | + $this->_initialize_collections(); |
|
308 | + return ! empty($this->_active_message_types[ $messenger_name ]); |
|
309 | + } |
|
310 | + |
|
311 | + |
|
312 | + /** |
|
313 | + * This returns any settings that might be on a message type for a messenger |
|
314 | + * |
|
315 | + * @param string $messenger_name The slug of the messenger |
|
316 | + * @param string $message_type_name The slug of the message type getting the settings for. |
|
317 | + * @return array |
|
318 | + */ |
|
319 | + public function get_message_type_settings_for_messenger($messenger_name, $message_type_name) |
|
320 | + { |
|
321 | + $settings = []; |
|
322 | + if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
323 | + $settings = |
|
324 | + isset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings']) |
|
325 | + ? $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] |
|
326 | + : []; |
|
327 | + } |
|
328 | + return $settings; |
|
329 | + } |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * Returns whether the given messenger name has active message types on it. |
|
334 | + * Infers whether the messenger is active or not as well. |
|
335 | + * |
|
336 | + * @param string $messenger_name |
|
337 | + * @return bool |
|
338 | + */ |
|
339 | + public function messenger_has_active_message_types($messenger_name) |
|
340 | + { |
|
341 | + $this->_initialize_collections(); |
|
342 | + return |
|
343 | + ! empty($this->_active_message_types[ $messenger_name ]) |
|
344 | + && ! empty($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ]); |
|
345 | + } |
|
346 | + |
|
347 | + |
|
348 | + /** |
|
349 | + * This checks the _active_message_types property for any active message types |
|
350 | + * that are present for the given messenger and returns them. |
|
351 | + * |
|
352 | + * @param string $messenger_name The messenger being checked |
|
353 | + * @return EE_message_type[]|array (empty array if no active_message_types) |
|
354 | + * @since 4.9.0 |
|
355 | + */ |
|
356 | + public function get_active_message_types_for_messenger($messenger_name) |
|
357 | + { |
|
358 | + $message_types = []; |
|
359 | + if (! $this->messenger_has_active_message_types($messenger_name)) { |
|
360 | + return $message_types; |
|
361 | + } |
|
362 | + $installed_message_types = $this->installed_message_types(); |
|
363 | + foreach ($installed_message_types as $message_type_name => $message_type) { |
|
364 | + if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
365 | + $message_types[ $message_type_name ] = $message_type; |
|
366 | + } |
|
367 | + } |
|
368 | + return $message_types; |
|
369 | + } |
|
370 | + |
|
371 | + |
|
372 | + /** |
|
373 | + * This does NOT return the _active_message_types property but |
|
374 | + * simply returns an array of active message type names from that property. |
|
375 | + * (The _active_message_types property is indexed by messenger and active message_types per messenger). |
|
376 | + * |
|
377 | + * @return array message_type references (string) |
|
378 | + */ |
|
379 | + public function list_of_active_message_types() |
|
380 | + { |
|
381 | + $active_message_type_names = []; |
|
382 | + $this->_initialize_collections(); |
|
383 | + foreach ($this->_active_message_types as $messenger => $messenger_settings) { |
|
384 | + if (! isset($messenger_settings['settings'][ $messenger . '-message_types' ])) { |
|
385 | + continue; |
|
386 | + } |
|
387 | + foreach ($messenger_settings['settings'][ $messenger . '-message_types' ] as $message_type_name => $message_type_config) { |
|
388 | + if (! in_array($message_type_name, $active_message_type_names)) { |
|
389 | + $active_message_type_names[] = $message_type_name; |
|
390 | + } |
|
391 | + } |
|
392 | + } |
|
393 | + return $active_message_type_names; |
|
394 | + } |
|
395 | + |
|
396 | + |
|
397 | + /** |
|
398 | + * Same as list_of_active_message_types() except this returns actual EE_message_type objects |
|
399 | + * |
|
400 | + * @return \EE_message_type[] |
|
401 | + * @since 4.9.0 |
|
402 | + */ |
|
403 | + public function get_active_message_type_objects() |
|
404 | + { |
|
405 | + $active_message_types = []; |
|
406 | + $installed_message_types = $this->installed_message_types(); |
|
407 | + $active_message_type_names = $this->list_of_active_message_types(); |
|
408 | + foreach ($active_message_type_names as $active_message_type_name) { |
|
409 | + if (isset($installed_message_types[ $active_message_type_name ])) { |
|
410 | + $active_message_types[ $active_message_type_name ] = |
|
411 | + $installed_message_types[ $active_message_type_name ]; |
|
412 | + } |
|
413 | + } |
|
414 | + return $active_message_types; |
|
415 | + } |
|
416 | + |
|
417 | + |
|
418 | + /** |
|
419 | + * @return \EE_message_type[] |
|
420 | + */ |
|
421 | + public function installed_message_types() |
|
422 | + { |
|
423 | + if (empty($this->_installed_message_types)) { |
|
424 | + $this->message_type_collection()->rewind(); |
|
425 | + while ($this->message_type_collection()->valid()) { |
|
426 | + $this->_installed_message_types[ $this->message_type_collection()->current()->name ] = |
|
427 | + $this->message_type_collection()->current(); |
|
428 | + $this->message_type_collection()->next(); |
|
429 | + } |
|
430 | + } |
|
431 | + return $this->_installed_message_types; |
|
432 | + } |
|
433 | + |
|
434 | + |
|
435 | + /** |
|
436 | + * @param string $message_type_name |
|
437 | + * @return \EE_message_type |
|
438 | + * @throws EE_Error |
|
439 | + */ |
|
440 | + public function valid_message_type($message_type_name) |
|
441 | + { |
|
442 | + $message_type = $this->get_message_type($message_type_name); |
|
443 | + if ($message_type instanceof EE_message_type) { |
|
444 | + return $message_type; |
|
445 | + } |
|
446 | + throw new EE_Error( |
|
447 | + sprintf( |
|
448 | + esc_html__('The "%1$s" message type is either invalid or not installed', 'event_espresso'), |
|
449 | + $message_type_name |
|
450 | + ) |
|
451 | + ); |
|
452 | + } |
|
453 | + |
|
454 | + |
|
455 | + /** |
|
456 | + * valid_message_type_for_messenger |
|
457 | + * |
|
458 | + * @param EE_messenger $messenger |
|
459 | + * @param string $message_type_name |
|
460 | + * @return boolean |
|
461 | + * @throws EE_Error |
|
462 | + */ |
|
463 | + public function valid_message_type_for_messenger(EE_messenger $messenger, $message_type_name) |
|
464 | + { |
|
465 | + $valid_message_types = $messenger->get_valid_message_types(); |
|
466 | + if (! in_array($message_type_name, $valid_message_types)) { |
|
467 | + throw new EE_Error( |
|
468 | + sprintf( |
|
469 | + esc_html__( |
|
470 | + '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.', |
|
471 | + 'event_espresso' |
|
472 | + ), |
|
473 | + $message_type_name, |
|
474 | + __METHOD__, |
|
475 | + $messenger->name |
|
476 | + ) |
|
477 | + ); |
|
478 | + } |
|
479 | + return true; |
|
480 | + } |
|
481 | + |
|
482 | + |
|
483 | + /** |
|
484 | + * Used to return active messengers array stored in the wp options table. |
|
485 | + * If no value is present in the option then an empty array is returned. |
|
486 | + * |
|
487 | + * @param bool $reset If true then we ignore whether the option is cached on the _active_message_types |
|
488 | + * property and pull directly from the db. Otherwise whatever is currently on the |
|
489 | + * $_active_message_types property is pulled. |
|
490 | + * @return array |
|
491 | + */ |
|
492 | + public function get_active_messengers_option($reset = false) |
|
493 | + { |
|
494 | + if ($reset) { |
|
495 | + $this->_active_message_types = get_option('ee_active_messengers', []); |
|
496 | + } |
|
497 | + return $this->_active_message_types; |
|
498 | + } |
|
499 | + |
|
500 | + |
|
501 | + /** |
|
502 | + * Used to update the active messengers array stored in the wp options table. |
|
503 | + * |
|
504 | + * @param array $active_messenger_settings Incoming data to save. If empty, then the internal cached property |
|
505 | + * representing this data is used. |
|
506 | + * @return bool FALSE if not updated, TRUE if updated. |
|
507 | + */ |
|
508 | + public function update_active_messengers_option($active_messenger_settings = []) |
|
509 | + { |
|
510 | + $active_messenger_settings = empty($active_messenger_settings) |
|
511 | + ? $this->_active_message_types |
|
512 | + : $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 | + * |
|
524 | + * @param bool $reset Used to indicate that any cached value should be ignored. |
|
525 | + * @return array |
|
526 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
527 | + * details. |
|
528 | + */ |
|
529 | + public function get_has_activated_messengers_option($reset = false) |
|
530 | + { |
|
531 | + if ($reset || empty($this->_has_activated_messengers_and_message_types)) { |
|
532 | + $this->_has_activated_messengers_and_message_types = |
|
533 | + get_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, []); |
|
534 | + } |
|
535 | + return $this->_has_activated_messengers_and_message_types; |
|
536 | + } |
|
537 | + |
|
538 | + |
|
539 | + /** |
|
540 | + * Used to update the has activated option in the db. |
|
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 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
546 | + * details. |
|
547 | + */ |
|
548 | + public function update_has_activated_messengers_option($has_activated_messengers = []) |
|
549 | + { |
|
550 | + // make sure the option has been retrieved from first so we don't overwrite it accidentally. |
|
551 | + if (empty($has_activated_messengers) && empty($this->_has_activated_messengers_and_message_types)) { |
|
552 | + $this->get_has_activated_messengers_option(); |
|
553 | + } |
|
554 | + $has_activated_messengers = empty($has_activated_messengers) |
|
555 | + ? $this->_has_activated_messengers_and_message_types |
|
556 | + : $has_activated_messengers; |
|
557 | + return update_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, $has_activated_messengers); |
|
558 | + } |
|
559 | + |
|
560 | + |
|
561 | + /** |
|
562 | + * wrapper for _set_active_messengers_and_message_types() |
|
563 | + */ |
|
564 | + public function reset_active_messengers_and_message_types() |
|
565 | + { |
|
566 | + $this->_set_active_messengers_and_message_types(); |
|
567 | + } |
|
568 | + |
|
569 | + |
|
570 | + /** |
|
571 | + * Generate list of active messengers and message types from collection. |
|
572 | + * This sets up the active messengers from what is present in the database. |
|
573 | + */ |
|
574 | + protected function _set_active_messengers_and_message_types() |
|
575 | + { |
|
576 | + // echo "\n\n " . __LINE__ . ") " . __METHOD__ . "() \n"; |
|
577 | + // list of activated messengers as set via the admin |
|
578 | + // note calling `get_active_messengers_options` also initializes the _active_message_types property. |
|
579 | + $this->get_active_messengers_option(true); |
|
580 | + $this->ensure_messengers_are_active([], false, true); |
|
581 | + $this->update_active_messengers_option(); |
|
582 | + $this->update_has_activated_messengers_option(); |
|
583 | + } |
|
584 | + |
|
585 | + |
|
586 | + /** |
|
587 | + * Ensures that the specified messenger is currently active. |
|
588 | + * If not, activates it and its default message types. |
|
589 | + * |
|
590 | + * @param string $messenger_name |
|
591 | + * @param bool $update_option Whether to update the option in the db or not. |
|
592 | + * @return boolean true if either already active or successfully activated. |
|
593 | + */ |
|
594 | + public function ensure_messenger_is_active($messenger_name, $update_option = true) |
|
595 | + { |
|
596 | + if (! isset($this->_active_messengers[ $messenger_name ])) { |
|
597 | + try { |
|
598 | + $this->activate_messenger($messenger_name, [], $update_option); |
|
599 | + } catch (EE_Error $e) { |
|
600 | + EE_Error::add_error( |
|
601 | + $e->getMessage(), |
|
602 | + __FILE__, |
|
603 | + __FUNCTION__, |
|
604 | + __LINE__ |
|
605 | + ); |
|
606 | + return false; |
|
607 | + } |
|
608 | + } |
|
609 | + return true; |
|
610 | + } |
|
611 | + |
|
612 | + |
|
613 | + /** |
|
614 | + * This ensures the given array of messenger names is active in the system. |
|
615 | + * Note, this method will not activate any NEW message types for the messenger when it is called. Instead, |
|
616 | + * it will automatically activate the default message types for the messenger if its not active. |
|
617 | + * |
|
618 | + * @param array $messenger_names Array of messenger names for messengers to be activated. If an empty array |
|
619 | + * (default) then will attempt to set the active messengers from the |
|
620 | + * activated_messengers option |
|
621 | + * (stored in $_active_message_types property). |
|
622 | + * @param bool $update_option Whether to update the related active messengers option. |
|
623 | + * @param bool $verify Whether to verify the messengers are installed before activating. Note if this is |
|
624 | + * set to true and a messenger is indicated as active, but is NOT installed, then it |
|
625 | + * will automatically be deactivated. |
|
626 | + */ |
|
627 | + public function ensure_messengers_are_active($messenger_names = [], $update_option = true, $verify = false) |
|
628 | + { |
|
629 | + $messenger_names = empty($messenger_names) |
|
630 | + ? array_keys($this->_active_message_types) |
|
631 | + : $messenger_names; |
|
632 | + |
|
633 | + $not_installed = []; |
|
634 | + foreach ($messenger_names as $messenger_name) { |
|
635 | + if ($verify && ! $this->messenger_collection()->has_by_name($messenger_name)) { |
|
636 | + $not_installed[] = $messenger_name; |
|
637 | + $this->deactivate_messenger($messenger_name); |
|
638 | + continue; |
|
639 | + } |
|
640 | + $this->ensure_messenger_is_active($messenger_name, $update_option); |
|
641 | + } |
|
642 | + |
|
643 | + if (! empty($not_installed)) { |
|
644 | + EE_Error::add_error( |
|
645 | + sprintf( |
|
646 | + esc_html__('The following messengers are either not installed or are invalid:%1$s %2$s', |
|
647 | + 'event_espresso'), |
|
648 | + '<br />', |
|
649 | + implode(', ', $not_installed) |
|
650 | + ), |
|
651 | + __FILE__, |
|
652 | + __FUNCTION__, |
|
653 | + __LINE__ |
|
654 | + ); |
|
655 | + } |
|
656 | + } |
|
657 | + |
|
658 | + |
|
659 | + /** |
|
660 | + * Ensures that the specified message type for the given messenger is currently active, if not activates it. |
|
661 | + * This ALSO ensures that the given messenger is active as well! |
|
662 | + * |
|
663 | + * @param string $message_type_name message type name. |
|
664 | + * @param $messenger_name |
|
665 | + * @param bool $update_option Whether to update the option in the db or not. |
|
666 | + * @return bool Returns true if already is active or if was activated successfully. |
|
667 | + * @throws EE_Error |
|
668 | + */ |
|
669 | + public function ensure_message_type_is_active($message_type_name, $messenger_name, $update_option = true) |
|
670 | + { |
|
671 | + // grab the messenger to work with. |
|
672 | + $messenger = $this->valid_messenger($messenger_name); |
|
673 | + if ($this->valid_message_type_for_messenger($messenger, $message_type_name)) { |
|
674 | + // ensure messenger is active (that's an inherent coupling between active message types and the |
|
675 | + // messenger they are being activated for. |
|
676 | + try { |
|
677 | + if (! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
678 | + // all is good so let's just get it active |
|
679 | + $this->activate_messenger($messenger, [$message_type_name], $update_option); |
|
680 | + } |
|
681 | + } catch (EE_Error $e) { |
|
682 | + EE_Error::add_error( |
|
683 | + $e->getMessage(), |
|
684 | + __FILE__, |
|
685 | + __FUNCTION__, |
|
686 | + __LINE__ |
|
687 | + ); |
|
688 | + return false; |
|
689 | + } |
|
690 | + } |
|
691 | + return true; |
|
692 | + } |
|
693 | + |
|
694 | + |
|
695 | + /** |
|
696 | + * This is a wrapper for `ensure_message_type_is_active` that will handle ensuring multiple message types for a |
|
697 | + * messenger are active in one go. |
|
698 | + * |
|
699 | + * @param array $message_type_names Array of message type names to ensure are active. |
|
700 | + * @param string $messenger_name The name of the messenger that the message types are to be activated on. |
|
701 | + * @param bool $update_option Whether to persist the activation to the database or not (default true). |
|
702 | + */ |
|
703 | + public function ensure_message_types_are_active($message_type_names, $messenger_name, $update_option = true) |
|
704 | + { |
|
705 | + $message_type_names = (array) $message_type_names; |
|
706 | + foreach ($message_type_names as $message_type_name) { |
|
707 | + // note, intentionally not updating option here because we're in a loop. |
|
708 | + // We'll follow the instructions of the incoming $update_option argument after the loop. |
|
709 | + $this->ensure_message_type_is_active($message_type_name, $messenger_name, false); |
|
710 | + } |
|
711 | + if ($update_option) { |
|
712 | + $this->update_active_messengers_option(); |
|
713 | + $this->update_has_activated_messengers_option(); |
|
714 | + } |
|
715 | + } |
|
716 | + |
|
717 | + |
|
718 | + /** |
|
719 | + * Activates the specified messenger. |
|
720 | + * |
|
721 | + * @param EE_messenger|string $messenger Instantiated EE_messenger OR messenger name if not already loaded! |
|
722 | + * @param array $message_type_names An array of message type names to activate with this messenger. |
|
723 | + * If included we do NOT setup the default message types |
|
724 | + * (assuming they are already setup.) |
|
725 | + * @param bool $update_active_messengers_option |
|
726 | + * @return array of generated templates |
|
727 | + * @throws EE_Error |
|
728 | + */ |
|
729 | + public function activate_messenger( |
|
730 | + $messenger, |
|
731 | + $message_type_names = [], |
|
732 | + $update_active_messengers_option = true |
|
733 | + ) { |
|
734 | + $templates = []; |
|
735 | + // grab the messenger to work with. |
|
736 | + $messenger = $messenger instanceof EE_messenger |
|
737 | + ? $messenger |
|
738 | + : $this->messenger_collection()->get_by_info($messenger); |
|
739 | + // it's inactive. Activate it. |
|
740 | + if ($messenger instanceof EE_messenger) { |
|
741 | + $this->_active_messengers[ $messenger->name ] = $messenger; |
|
742 | + // activate incoming message types set to be activated with messenger. |
|
743 | + $message_type_names = $this->_activate_message_types($messenger, $message_type_names); |
|
744 | + // setup any initial settings for the messenger if necessary. |
|
745 | + $this->add_settings_for_messenger($messenger->name); |
|
746 | + if ($update_active_messengers_option) { |
|
747 | + $this->update_active_messengers_option(); |
|
748 | + $this->update_has_activated_messengers_option(); |
|
749 | + } |
|
750 | + // generate new templates if necessary and ensure all related templates that are already in the database are |
|
751 | + // marked active. Note, this will also deactivate a message type for a messenger if the template |
|
752 | + // cannot be successfully created during its attempt (only happens for global template attempts). |
|
753 | + if (! empty($message_type_names)) { |
|
754 | + $templates = EEH_MSG_Template::generate_new_templates($messenger->name, $message_type_names, 0, true); |
|
755 | + EEH_MSG_Template::update_to_active([$messenger->name], $message_type_names); |
|
756 | + } |
|
757 | + } |
|
758 | + return $templates; |
|
759 | + } |
|
760 | + |
|
761 | + |
|
762 | + /** |
|
763 | + * Activates given message types for the given EE_messenger object. |
|
764 | + * Note: (very important) This method does not persist the activation to the database. |
|
765 | + * See code implementing this method in this class for examples of how to persist. |
|
766 | + * |
|
767 | + * @param \EE_messenger $messenger |
|
768 | + * @param array $message_type_names |
|
769 | + * @return array |
|
770 | + */ |
|
771 | + protected function _activate_message_types(EE_messenger $messenger, $message_type_names = []) |
|
772 | + { |
|
773 | + // If $message_type_names is empty, AND $this->_active_message_types is empty, then that means |
|
774 | + // things have never been initialized (which should happen on EEH_Activation::generate_message_templates). |
|
775 | + // So ONLY then do we need to actually grab defaults and cycle through them. Otherwise we |
|
776 | + // only override _active_message_types when an explicit array of $message_type_names has been provided. |
|
777 | + $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[ $messenger->name ]) |
|
778 | + ? $messenger->get_default_message_types() |
|
779 | + : (array) $message_type_names; |
|
780 | + |
|
781 | + // now we ALWAYS need to make sure that the messenger is active for the message types we're activating! |
|
782 | + if (! isset($this->_active_message_types[ $messenger->name ])) { |
|
783 | + $this->_active_message_types[ $messenger->name ]['settings'] = []; |
|
784 | + } |
|
785 | + |
|
786 | + if ($message_type_names) { |
|
787 | + // cycle thru message types |
|
788 | + foreach ($message_type_names as $message_type_name) { |
|
789 | + // only register the message type as active IF it isn't already active |
|
790 | + // and if its actually installed. |
|
791 | + if ( |
|
792 | + ! $this->is_message_type_active_for_messenger($messenger->name, $message_type_name) |
|
793 | + ) { |
|
794 | + $this->add_settings_for_message_type($messenger->name, $message_type_name); |
|
795 | + $this->_set_messenger_has_activated_message_type( |
|
796 | + $messenger, |
|
797 | + $message_type_name |
|
798 | + ); |
|
799 | + } |
|
800 | + } |
|
801 | + } |
|
802 | + return $message_type_names; |
|
803 | + } |
|
804 | + |
|
805 | + |
|
806 | + /** |
|
807 | + * add_settings_for_message_type |
|
808 | + * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
809 | + * $this->update_active_messengers_option to persist. |
|
810 | + * |
|
811 | + * @param string $messenger_name The name of the messenger adding the settings for |
|
812 | + * @param string $message_type_name The name of the message type adding the settings for |
|
813 | + * @param array $new_settings Any new settings being set for the message type and messenger |
|
814 | + */ |
|
815 | + public function add_settings_for_message_type($messenger_name, $message_type_name, $new_settings = []) |
|
816 | + { |
|
817 | + // get installed message type from collection |
|
818 | + $message_type = $this->message_type_collection()->get_by_info($message_type_name); |
|
819 | + $existing_settings = $this->get_message_type_settings_for_messenger($messenger_name, $message_type_name); |
|
820 | + // we need to setup any initial settings for message types |
|
821 | + if ($message_type instanceof EE_message_type) { |
|
822 | + $default_settings = $message_type->get_admin_settings_fields(); |
|
823 | + foreach ($default_settings as $field => $values) { |
|
824 | + if (isset($new_settings[ $field ])) { |
|
825 | + $existing_settings[ $field ] = $new_settings[ $field ]; |
|
826 | + continue; |
|
827 | + } |
|
828 | + if (! isset($existing_settings[ $field ])) { |
|
829 | + $existing_settings[ $field ] = $values['default']; |
|
830 | + } |
|
831 | + } |
|
832 | + } |
|
833 | + $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] = |
|
834 | + $existing_settings; |
|
835 | + } |
|
836 | + |
|
837 | + |
|
838 | + /** |
|
839 | + * Updates the internal cached _has_activated_messengers_and_message_types property with the given messenger |
|
840 | + * and message type. |
|
841 | + * |
|
842 | + * @param \EE_messenger $messenger |
|
843 | + * @param string $message_type_name |
|
844 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
845 | + * details. |
|
846 | + * @access protected |
|
847 | + */ |
|
848 | + protected function _set_messenger_has_activated_message_type(EE_messenger $messenger, $message_type_name) |
|
849 | + { |
|
850 | + // if _has_activated_messengers_and_message_types is empty then lets ensure its initialized |
|
851 | + if (empty($this->_has_activated_messengers_and_message_types)) { |
|
852 | + $this->get_has_activated_messengers_option(); |
|
853 | + } |
|
854 | + |
|
855 | + // make sure this messenger has a record in the has_activated array |
|
856 | + if (! isset($this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
857 | + $this->_has_activated_messengers_and_message_types[ $messenger->name ] = []; |
|
858 | + } |
|
859 | + // check if message type has already been added |
|
860 | + if (! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
861 | + $this->_has_activated_messengers_and_message_types[ $messenger->name ][] = $message_type_name; |
|
862 | + } |
|
863 | + } |
|
864 | + |
|
865 | + |
|
866 | + /** |
|
867 | + * add_settings_for_messenger |
|
868 | + * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
869 | + * $this->update_active_messengers_option to persist. |
|
870 | + * |
|
871 | + * @param string $messenger_name The name of the messenger the settings is being added for. |
|
872 | + * @param array $new_settings An array of settings to update the existing settings. |
|
873 | + */ |
|
874 | + public function add_settings_for_messenger($messenger_name, $new_settings = []) |
|
875 | + { |
|
876 | + $messenger = $this->get_messenger($messenger_name); |
|
877 | + if ($messenger instanceof EE_messenger) { |
|
878 | + $msgr_settings = $messenger->get_admin_settings_fields(); |
|
879 | + if (! empty($msgr_settings)) { |
|
880 | + foreach ($msgr_settings as $field => $value) { |
|
881 | + // is there a new setting for this? |
|
882 | + if (isset($new_settings[ $field ])) { |
|
883 | + $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = |
|
884 | + $new_settings[ $field ]; |
|
885 | + continue; |
|
886 | + } |
|
887 | + // only set the default if it isn't already set. |
|
888 | + if (! isset($this->_active_message_types[ $messenger->name ]['settings'][ $field ])) { |
|
889 | + $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $value; |
|
890 | + } |
|
891 | + } |
|
892 | + } |
|
893 | + } |
|
894 | + } |
|
895 | + |
|
896 | + |
|
897 | + /** |
|
898 | + * deactivate_messenger |
|
899 | + * |
|
900 | + * @param string|EE_messenger $messenger_name name of messenger |
|
901 | + * @return void |
|
902 | + */ |
|
903 | + public function deactivate_messenger($messenger_name) |
|
904 | + { |
|
905 | + $this->_initialize_collections(); |
|
906 | + if ($messenger_name instanceof EE_messenger) { |
|
907 | + $messenger_name = $messenger_name->name; |
|
908 | + } |
|
909 | + unset($this->_active_messengers[ $messenger_name ]); |
|
910 | + unset($this->_active_message_types[ $messenger_name ]); |
|
911 | + $this->_message_template_group_model->deactivate_message_template_groups_for($messenger_name); |
|
912 | + $this->update_active_messengers_option(); |
|
913 | + } |
|
914 | + |
|
915 | + |
|
916 | + /** |
|
917 | + * Deactivates a message type (note this will deactivate across all messenger's it is active on. |
|
918 | + * |
|
919 | + * @param string $message_type_name name of message type being deactivated |
|
920 | + * @param bool $set_has_active_record By default we always record the has_active record when deactivating a |
|
921 | + * message type. However, this can be overridden if we don't want this set |
|
922 | + * (usually when this is called as a part of deregistration of a custom |
|
923 | + * message type) |
|
924 | + */ |
|
925 | + public function deactivate_message_type($message_type_name, $set_has_active_record = true) |
|
926 | + { |
|
927 | + $this->_initialize_collections(); |
|
928 | + if ($message_type_name instanceof EE_message_type) { |
|
929 | + $message_type_name = $message_type_name->name; |
|
930 | + } |
|
931 | + foreach ($this->_active_message_types as $messenger_name => $settings) { |
|
932 | + unset( |
|
933 | + $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ] |
|
934 | + ); |
|
935 | + |
|
936 | + // we always record (even on deactivation) that a message type has been activated because there should at |
|
937 | + // least be a record in the "has_activated" option that it WAS active at one point. |
|
938 | + if ($set_has_active_record) { |
|
939 | + $messenger = $this->get_messenger($messenger_name); |
|
940 | + $this->_set_messenger_has_activated_message_type($messenger, $message_type_name); |
|
941 | + } |
|
942 | + } |
|
943 | + $this->_message_template_group_model->deactivate_message_template_groups_for('', $message_type_name); |
|
944 | + $this->update_active_messengers_option(); |
|
945 | + $this->update_has_activated_messengers_option(); |
|
946 | + } |
|
947 | + |
|
948 | + |
|
949 | + /** |
|
950 | + * Deactivates a message type for a specific messenger as opposed to all messengers. |
|
951 | + * |
|
952 | + * @param string $message_type_name Name of message type being deactivated. |
|
953 | + * @param string $messenger_name Name of messenger the message type is being deactivated for. |
|
954 | + */ |
|
955 | + public function deactivate_message_type_for_messenger($message_type_name, $messenger_name) |
|
956 | + { |
|
957 | + $this->_initialize_collections(); |
|
958 | + if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
959 | + unset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]); |
|
960 | + } |
|
961 | + $this->_message_template_group_model->deactivate_message_template_groups_for( |
|
962 | + [$messenger_name], |
|
963 | + [$message_type_name] |
|
964 | + ); |
|
965 | + $this->update_active_messengers_option(); |
|
966 | + } |
|
967 | + |
|
968 | + |
|
969 | + /** |
|
970 | + * Used to verify if a message can be sent for the given messenger and message type |
|
971 | + * and that it is a generating messenger (used for generating message templates). |
|
972 | + * |
|
973 | + * @param EE_messenger $messenger messenger used in trigger |
|
974 | + * @param EE_message_type $message_type message type used in trigger |
|
975 | + * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send. |
|
976 | + */ |
|
977 | + public function is_generating_messenger_and_active(EE_messenger $messenger, EE_message_type $message_type) |
|
978 | + { |
|
979 | + // get the $messengers the message type says it can be used with. |
|
980 | + foreach ($message_type->with_messengers() as $generating_messenger => $secondary_messengers) { |
|
981 | + if ( |
|
982 | + $messenger->name === $generating_messenger |
|
983 | + && $this->is_message_type_active_for_messenger($messenger->name, $message_type->name) |
|
984 | + ) { |
|
985 | + return true; |
|
986 | + } |
|
987 | + } |
|
988 | + return false; |
|
989 | + } |
|
990 | + |
|
991 | + |
|
992 | + /** |
|
993 | + * This returns all the contexts that are registered by all message types. |
|
994 | + * If $slugs_only is true, |
|
995 | + * then just an array indexed by unique context slugs with the latest label representation for that slug. |
|
996 | + * array( |
|
997 | + * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
998 | + * ); |
|
999 | + * If $slugs_only is false, then the format is: |
|
1000 | + * array( |
|
1001 | + * 'message_type_name' => array( |
|
1002 | + * 'context_slug' => array( |
|
1003 | + * 'label' => 'localized label for context', |
|
1004 | + * 'description' => 'localized description for context' |
|
1005 | + * ) |
|
1006 | + * ) |
|
1007 | + * ); |
|
1008 | + * Keep in mind that although different message types may share the same context slugs, |
|
1009 | + * it is possible that the context is described differently by the message type. |
|
1010 | + * |
|
1011 | + * @param bool $slugs_only Whether to return an array of just slugs and labels (true) |
|
1012 | + * or all contexts indexed by message type. |
|
1013 | + * @return array |
|
1014 | + * @since 4.9.0 |
|
1015 | + */ |
|
1016 | + public function get_all_contexts($slugs_only = true) |
|
1017 | + { |
|
1018 | + $key = $slugs_only |
|
1019 | + ? 'slugs' |
|
1020 | + : 'all'; |
|
1021 | + // check if contexts has been setup yet. |
|
1022 | + if (empty($this->_contexts[ $key ])) { |
|
1023 | + // So let's get all active message type objects and loop through to get all unique contexts |
|
1024 | + foreach ($this->get_active_message_type_objects() as $message_type) { |
|
1025 | + if ($message_type instanceof EE_message_type) { |
|
1026 | + $message_type_contexts = $message_type->get_contexts(); |
|
1027 | + if ($slugs_only) { |
|
1028 | + foreach ($message_type_contexts as $context => $context_details) { |
|
1029 | + $this->_contexts[ $key ][ $context ] = $context_details['label']; |
|
1030 | + } |
|
1031 | + } else { |
|
1032 | + $this->_contexts[ $key ][ $message_type->name ] = $message_type_contexts; |
|
1033 | + } |
|
1034 | + } |
|
1035 | + } |
|
1036 | + } |
|
1037 | + return ! empty($this->_contexts[ $key ]) |
|
1038 | + ? $this->_contexts[ $key ] |
|
1039 | + : []; |
|
1040 | + } |
|
1041 | + |
|
1042 | + |
|
1043 | + /** |
|
1044 | + * This checks the internal record of what message types are considered "active" and verifies that |
|
1045 | + * there is an installed class definition for that message type. If the active message type does not have a |
|
1046 | + * corresponding accessible message type class then it will be deactivated from all messengers it is active on and |
|
1047 | + * any related message templates will be inactivated as well. |
|
1048 | + * |
|
1049 | + * @return bool true means all active message types are valid, false means at least one message type was |
|
1050 | + * deactivated. |
|
1051 | + */ |
|
1052 | + public function validate_active_message_types_are_installed() |
|
1053 | + { |
|
1054 | + $list_of_active_message_type_names = $this->list_of_active_message_types(); |
|
1055 | + $installed_message_types = $this->installed_message_types(); |
|
1056 | + $all_message_types_valid = true; |
|
1057 | + // loop through list of active message types and verify they are installed. |
|
1058 | + foreach ($list_of_active_message_type_names as $message_type_name) { |
|
1059 | + if (! isset($installed_message_types[ $message_type_name ])) { |
|
1060 | + $this->remove_message_type_has_been_activated_from_all_messengers( |
|
1061 | + $message_type_name, |
|
1062 | + true |
|
1063 | + ); |
|
1064 | + $this->deactivate_message_type($message_type_name, false); |
|
1065 | + $all_message_types_valid = false; |
|
1066 | + } |
|
1067 | + } |
|
1068 | + return $all_message_types_valid; |
|
1069 | + } |
|
1070 | + |
|
1071 | + |
|
1072 | + /** |
|
1073 | + * This method checks the `ee_has_activated_messenger` option to see if the message type has ever been |
|
1074 | + * activated for the given messenger. This can be called by client code on plugin updates etc to determine whether |
|
1075 | + * to attempt automatically reactivating message types that should be activated by default or not. |
|
1076 | + * |
|
1077 | + * @param $message_type_name |
|
1078 | + * @param $messenger_name |
|
1079 | + * @return bool |
|
1080 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
1081 | + * details. |
|
1082 | + */ |
|
1083 | + public function has_message_type_been_activated_for_messenger($message_type_name, $messenger_name) |
|
1084 | + { |
|
1085 | + $has_activated = $this->get_has_activated_messengers_option(); |
|
1086 | + return isset($has_activated[ $messenger_name ]) |
|
1087 | + && in_array($message_type_name, $has_activated[ $messenger_name ]); |
|
1088 | + } |
|
1089 | + |
|
1090 | + |
|
1091 | + /** |
|
1092 | + * This method unsets a message type from the given messenger has activated option. |
|
1093 | + * |
|
1094 | + * @param string $message_type_name |
|
1095 | + * @param string $messenger_name |
|
1096 | + * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1097 | + * If it is currently active, then remove. Otherwise leave it alone. |
|
1098 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
1099 | + * details. |
|
1100 | + */ |
|
1101 | + public function remove_message_type_has_been_activated_for_messenger( |
|
1102 | + $message_type_name, |
|
1103 | + $messenger_name, |
|
1104 | + $consider_current_state = false |
|
1105 | + ) { |
|
1106 | + if ( |
|
1107 | + $consider_current_state |
|
1108 | + && ! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
1109 | + ) { |
|
1110 | + // when consider current state is true, this means we don't want to change anything on the "has_activated" |
|
1111 | + // record if the message type is currently active for this messenger. This is used when we want to retain |
|
1112 | + // the record for user initiated inactivations of the message type. |
|
1113 | + return; |
|
1114 | + } |
|
1115 | + $has_activated = $this->get_has_activated_messengers_option(); |
|
1116 | + $key_for_message_type = isset($has_activated[ $messenger_name ]) |
|
1117 | + ? array_search($message_type_name, $has_activated[ $messenger_name ], true) |
|
1118 | + : false; |
|
1119 | + if ($key_for_message_type !== false) { |
|
1120 | + unset($has_activated[ $messenger_name ][ $key_for_message_type ]); |
|
1121 | + $this->update_has_activated_messengers_option($has_activated); |
|
1122 | + // reset the internal cached property |
|
1123 | + $this->get_has_activated_messengers_option(true); |
|
1124 | + } |
|
1125 | + } |
|
1126 | + |
|
1127 | + |
|
1128 | + /** |
|
1129 | + * Removes a message type active record from all messengers it is attached to. |
|
1130 | + * |
|
1131 | + * @param $message_type_name |
|
1132 | + * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1133 | + * If it is currently active, then remove. Otherwise leave it alone. |
|
1134 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more |
|
1135 | + * details. |
|
1136 | + */ |
|
1137 | + public function remove_message_type_has_been_activated_from_all_messengers( |
|
1138 | + $message_type_name, |
|
1139 | + $consider_current_state = false |
|
1140 | + ) { |
|
1141 | + foreach (array_keys($this->get_has_activated_messengers_option()) as $messenger_name) { |
|
1142 | + $this->remove_message_type_has_been_activated_for_messenger( |
|
1143 | + $message_type_name, |
|
1144 | + $messenger_name, |
|
1145 | + $consider_current_state |
|
1146 | + ); |
|
1147 | + } |
|
1148 | + } |
|
1149 | 1149 | } |
@@ -193,8 +193,8 @@ discard block |
||
193 | 193 | public function get_active_messenger($messenger) |
194 | 194 | { |
195 | 195 | $this->_initialize_collections(); |
196 | - return ! empty($this->_active_messengers[ $messenger ]) |
|
197 | - ? $this->_active_messengers[ $messenger ] |
|
196 | + return ! empty($this->_active_messengers[$messenger]) |
|
197 | + ? $this->_active_messengers[$messenger] |
|
198 | 198 | : null; |
199 | 199 | } |
200 | 200 | |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | $this->_installed_messengers = []; |
209 | 209 | $this->messenger_collection()->rewind(); |
210 | 210 | while ($this->messenger_collection()->valid()) { |
211 | - $this->_installed_messengers[ $this->messenger_collection()->current()->name ] = |
|
211 | + $this->_installed_messengers[$this->messenger_collection()->current()->name] = |
|
212 | 212 | $this->messenger_collection()->current(); |
213 | 213 | $this->messenger_collection()->next(); |
214 | 214 | } |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | public function is_message_type_active_for_messenger($messenger_name, $message_type_name) |
293 | 293 | { |
294 | 294 | $this->_initialize_collections(); |
295 | - return ! empty($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]); |
|
295 | + return ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]); |
|
296 | 296 | } |
297 | 297 | |
298 | 298 | |
@@ -305,7 +305,7 @@ discard block |
||
305 | 305 | public function is_messenger_active($messenger_name) |
306 | 306 | { |
307 | 307 | $this->_initialize_collections(); |
308 | - return ! empty($this->_active_message_types[ $messenger_name ]); |
|
308 | + return ! empty($this->_active_message_types[$messenger_name]); |
|
309 | 309 | } |
310 | 310 | |
311 | 311 | |
@@ -321,8 +321,8 @@ discard block |
||
321 | 321 | $settings = []; |
322 | 322 | if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
323 | 323 | $settings = |
324 | - isset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings']) |
|
325 | - ? $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] |
|
324 | + isset($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]['settings']) |
|
325 | + ? $this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]['settings'] |
|
326 | 326 | : []; |
327 | 327 | } |
328 | 328 | return $settings; |
@@ -340,8 +340,8 @@ discard block |
||
340 | 340 | { |
341 | 341 | $this->_initialize_collections(); |
342 | 342 | return |
343 | - ! empty($this->_active_message_types[ $messenger_name ]) |
|
344 | - && ! empty($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ]); |
|
343 | + ! empty($this->_active_message_types[$messenger_name]) |
|
344 | + && ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types']); |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | |
@@ -356,13 +356,13 @@ discard block |
||
356 | 356 | public function get_active_message_types_for_messenger($messenger_name) |
357 | 357 | { |
358 | 358 | $message_types = []; |
359 | - if (! $this->messenger_has_active_message_types($messenger_name)) { |
|
359 | + if ( ! $this->messenger_has_active_message_types($messenger_name)) { |
|
360 | 360 | return $message_types; |
361 | 361 | } |
362 | 362 | $installed_message_types = $this->installed_message_types(); |
363 | 363 | foreach ($installed_message_types as $message_type_name => $message_type) { |
364 | 364 | if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
365 | - $message_types[ $message_type_name ] = $message_type; |
|
365 | + $message_types[$message_type_name] = $message_type; |
|
366 | 366 | } |
367 | 367 | } |
368 | 368 | return $message_types; |
@@ -381,11 +381,11 @@ discard block |
||
381 | 381 | $active_message_type_names = []; |
382 | 382 | $this->_initialize_collections(); |
383 | 383 | foreach ($this->_active_message_types as $messenger => $messenger_settings) { |
384 | - if (! isset($messenger_settings['settings'][ $messenger . '-message_types' ])) { |
|
384 | + if ( ! isset($messenger_settings['settings'][$messenger.'-message_types'])) { |
|
385 | 385 | continue; |
386 | 386 | } |
387 | - foreach ($messenger_settings['settings'][ $messenger . '-message_types' ] as $message_type_name => $message_type_config) { |
|
388 | - if (! in_array($message_type_name, $active_message_type_names)) { |
|
387 | + foreach ($messenger_settings['settings'][$messenger.'-message_types'] as $message_type_name => $message_type_config) { |
|
388 | + if ( ! in_array($message_type_name, $active_message_type_names)) { |
|
389 | 389 | $active_message_type_names[] = $message_type_name; |
390 | 390 | } |
391 | 391 | } |
@@ -406,9 +406,9 @@ discard block |
||
406 | 406 | $installed_message_types = $this->installed_message_types(); |
407 | 407 | $active_message_type_names = $this->list_of_active_message_types(); |
408 | 408 | foreach ($active_message_type_names as $active_message_type_name) { |
409 | - if (isset($installed_message_types[ $active_message_type_name ])) { |
|
410 | - $active_message_types[ $active_message_type_name ] = |
|
411 | - $installed_message_types[ $active_message_type_name ]; |
|
409 | + if (isset($installed_message_types[$active_message_type_name])) { |
|
410 | + $active_message_types[$active_message_type_name] = |
|
411 | + $installed_message_types[$active_message_type_name]; |
|
412 | 412 | } |
413 | 413 | } |
414 | 414 | return $active_message_types; |
@@ -423,7 +423,7 @@ discard block |
||
423 | 423 | if (empty($this->_installed_message_types)) { |
424 | 424 | $this->message_type_collection()->rewind(); |
425 | 425 | while ($this->message_type_collection()->valid()) { |
426 | - $this->_installed_message_types[ $this->message_type_collection()->current()->name ] = |
|
426 | + $this->_installed_message_types[$this->message_type_collection()->current()->name] = |
|
427 | 427 | $this->message_type_collection()->current(); |
428 | 428 | $this->message_type_collection()->next(); |
429 | 429 | } |
@@ -463,7 +463,7 @@ discard block |
||
463 | 463 | public function valid_message_type_for_messenger(EE_messenger $messenger, $message_type_name) |
464 | 464 | { |
465 | 465 | $valid_message_types = $messenger->get_valid_message_types(); |
466 | - if (! in_array($message_type_name, $valid_message_types)) { |
|
466 | + if ( ! in_array($message_type_name, $valid_message_types)) { |
|
467 | 467 | throw new EE_Error( |
468 | 468 | sprintf( |
469 | 469 | esc_html__( |
@@ -593,7 +593,7 @@ discard block |
||
593 | 593 | */ |
594 | 594 | public function ensure_messenger_is_active($messenger_name, $update_option = true) |
595 | 595 | { |
596 | - if (! isset($this->_active_messengers[ $messenger_name ])) { |
|
596 | + if ( ! isset($this->_active_messengers[$messenger_name])) { |
|
597 | 597 | try { |
598 | 598 | $this->activate_messenger($messenger_name, [], $update_option); |
599 | 599 | } catch (EE_Error $e) { |
@@ -640,7 +640,7 @@ discard block |
||
640 | 640 | $this->ensure_messenger_is_active($messenger_name, $update_option); |
641 | 641 | } |
642 | 642 | |
643 | - if (! empty($not_installed)) { |
|
643 | + if ( ! empty($not_installed)) { |
|
644 | 644 | EE_Error::add_error( |
645 | 645 | sprintf( |
646 | 646 | esc_html__('The following messengers are either not installed or are invalid:%1$s %2$s', |
@@ -674,7 +674,7 @@ discard block |
||
674 | 674 | // ensure messenger is active (that's an inherent coupling between active message types and the |
675 | 675 | // messenger they are being activated for. |
676 | 676 | try { |
677 | - if (! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
677 | + if ( ! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
678 | 678 | // all is good so let's just get it active |
679 | 679 | $this->activate_messenger($messenger, [$message_type_name], $update_option); |
680 | 680 | } |
@@ -738,7 +738,7 @@ discard block |
||
738 | 738 | : $this->messenger_collection()->get_by_info($messenger); |
739 | 739 | // it's inactive. Activate it. |
740 | 740 | if ($messenger instanceof EE_messenger) { |
741 | - $this->_active_messengers[ $messenger->name ] = $messenger; |
|
741 | + $this->_active_messengers[$messenger->name] = $messenger; |
|
742 | 742 | // activate incoming message types set to be activated with messenger. |
743 | 743 | $message_type_names = $this->_activate_message_types($messenger, $message_type_names); |
744 | 744 | // setup any initial settings for the messenger if necessary. |
@@ -750,7 +750,7 @@ discard block |
||
750 | 750 | // generate new templates if necessary and ensure all related templates that are already in the database are |
751 | 751 | // marked active. Note, this will also deactivate a message type for a messenger if the template |
752 | 752 | // cannot be successfully created during its attempt (only happens for global template attempts). |
753 | - if (! empty($message_type_names)) { |
|
753 | + if ( ! empty($message_type_names)) { |
|
754 | 754 | $templates = EEH_MSG_Template::generate_new_templates($messenger->name, $message_type_names, 0, true); |
755 | 755 | EEH_MSG_Template::update_to_active([$messenger->name], $message_type_names); |
756 | 756 | } |
@@ -774,13 +774,13 @@ discard block |
||
774 | 774 | // things have never been initialized (which should happen on EEH_Activation::generate_message_templates). |
775 | 775 | // So ONLY then do we need to actually grab defaults and cycle through them. Otherwise we |
776 | 776 | // only override _active_message_types when an explicit array of $message_type_names has been provided. |
777 | - $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[ $messenger->name ]) |
|
777 | + $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[$messenger->name]) |
|
778 | 778 | ? $messenger->get_default_message_types() |
779 | 779 | : (array) $message_type_names; |
780 | 780 | |
781 | 781 | // now we ALWAYS need to make sure that the messenger is active for the message types we're activating! |
782 | - if (! isset($this->_active_message_types[ $messenger->name ])) { |
|
783 | - $this->_active_message_types[ $messenger->name ]['settings'] = []; |
|
782 | + if ( ! isset($this->_active_message_types[$messenger->name])) { |
|
783 | + $this->_active_message_types[$messenger->name]['settings'] = []; |
|
784 | 784 | } |
785 | 785 | |
786 | 786 | if ($message_type_names) { |
@@ -821,16 +821,16 @@ discard block |
||
821 | 821 | if ($message_type instanceof EE_message_type) { |
822 | 822 | $default_settings = $message_type->get_admin_settings_fields(); |
823 | 823 | foreach ($default_settings as $field => $values) { |
824 | - if (isset($new_settings[ $field ])) { |
|
825 | - $existing_settings[ $field ] = $new_settings[ $field ]; |
|
824 | + if (isset($new_settings[$field])) { |
|
825 | + $existing_settings[$field] = $new_settings[$field]; |
|
826 | 826 | continue; |
827 | 827 | } |
828 | - if (! isset($existing_settings[ $field ])) { |
|
829 | - $existing_settings[ $field ] = $values['default']; |
|
828 | + if ( ! isset($existing_settings[$field])) { |
|
829 | + $existing_settings[$field] = $values['default']; |
|
830 | 830 | } |
831 | 831 | } |
832 | 832 | } |
833 | - $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] = |
|
833 | + $this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]['settings'] = |
|
834 | 834 | $existing_settings; |
835 | 835 | } |
836 | 836 | |
@@ -853,12 +853,12 @@ discard block |
||
853 | 853 | } |
854 | 854 | |
855 | 855 | // make sure this messenger has a record in the has_activated array |
856 | - if (! isset($this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
857 | - $this->_has_activated_messengers_and_message_types[ $messenger->name ] = []; |
|
856 | + if ( ! isset($this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
857 | + $this->_has_activated_messengers_and_message_types[$messenger->name] = []; |
|
858 | 858 | } |
859 | 859 | // check if message type has already been added |
860 | - if (! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
861 | - $this->_has_activated_messengers_and_message_types[ $messenger->name ][] = $message_type_name; |
|
860 | + if ( ! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
861 | + $this->_has_activated_messengers_and_message_types[$messenger->name][] = $message_type_name; |
|
862 | 862 | } |
863 | 863 | } |
864 | 864 | |
@@ -876,17 +876,17 @@ discard block |
||
876 | 876 | $messenger = $this->get_messenger($messenger_name); |
877 | 877 | if ($messenger instanceof EE_messenger) { |
878 | 878 | $msgr_settings = $messenger->get_admin_settings_fields(); |
879 | - if (! empty($msgr_settings)) { |
|
879 | + if ( ! empty($msgr_settings)) { |
|
880 | 880 | foreach ($msgr_settings as $field => $value) { |
881 | 881 | // is there a new setting for this? |
882 | - if (isset($new_settings[ $field ])) { |
|
883 | - $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = |
|
884 | - $new_settings[ $field ]; |
|
882 | + if (isset($new_settings[$field])) { |
|
883 | + $this->_active_message_types[$messenger->name]['settings'][$field] = |
|
884 | + $new_settings[$field]; |
|
885 | 885 | continue; |
886 | 886 | } |
887 | 887 | // only set the default if it isn't already set. |
888 | - if (! isset($this->_active_message_types[ $messenger->name ]['settings'][ $field ])) { |
|
889 | - $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $value; |
|
888 | + if ( ! isset($this->_active_message_types[$messenger->name]['settings'][$field])) { |
|
889 | + $this->_active_message_types[$messenger->name]['settings'][$field] = $value; |
|
890 | 890 | } |
891 | 891 | } |
892 | 892 | } |
@@ -906,8 +906,8 @@ discard block |
||
906 | 906 | if ($messenger_name instanceof EE_messenger) { |
907 | 907 | $messenger_name = $messenger_name->name; |
908 | 908 | } |
909 | - unset($this->_active_messengers[ $messenger_name ]); |
|
910 | - unset($this->_active_message_types[ $messenger_name ]); |
|
909 | + unset($this->_active_messengers[$messenger_name]); |
|
910 | + unset($this->_active_message_types[$messenger_name]); |
|
911 | 911 | $this->_message_template_group_model->deactivate_message_template_groups_for($messenger_name); |
912 | 912 | $this->update_active_messengers_option(); |
913 | 913 | } |
@@ -930,7 +930,7 @@ discard block |
||
930 | 930 | } |
931 | 931 | foreach ($this->_active_message_types as $messenger_name => $settings) { |
932 | 932 | unset( |
933 | - $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ] |
|
933 | + $this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name] |
|
934 | 934 | ); |
935 | 935 | |
936 | 936 | // we always record (even on deactivation) that a message type has been activated because there should at |
@@ -956,7 +956,7 @@ discard block |
||
956 | 956 | { |
957 | 957 | $this->_initialize_collections(); |
958 | 958 | if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
959 | - unset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]); |
|
959 | + unset($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]); |
|
960 | 960 | } |
961 | 961 | $this->_message_template_group_model->deactivate_message_template_groups_for( |
962 | 962 | [$messenger_name], |
@@ -1019,23 +1019,23 @@ discard block |
||
1019 | 1019 | ? 'slugs' |
1020 | 1020 | : 'all'; |
1021 | 1021 | // check if contexts has been setup yet. |
1022 | - if (empty($this->_contexts[ $key ])) { |
|
1022 | + if (empty($this->_contexts[$key])) { |
|
1023 | 1023 | // So let's get all active message type objects and loop through to get all unique contexts |
1024 | 1024 | foreach ($this->get_active_message_type_objects() as $message_type) { |
1025 | 1025 | if ($message_type instanceof EE_message_type) { |
1026 | 1026 | $message_type_contexts = $message_type->get_contexts(); |
1027 | 1027 | if ($slugs_only) { |
1028 | 1028 | foreach ($message_type_contexts as $context => $context_details) { |
1029 | - $this->_contexts[ $key ][ $context ] = $context_details['label']; |
|
1029 | + $this->_contexts[$key][$context] = $context_details['label']; |
|
1030 | 1030 | } |
1031 | 1031 | } else { |
1032 | - $this->_contexts[ $key ][ $message_type->name ] = $message_type_contexts; |
|
1032 | + $this->_contexts[$key][$message_type->name] = $message_type_contexts; |
|
1033 | 1033 | } |
1034 | 1034 | } |
1035 | 1035 | } |
1036 | 1036 | } |
1037 | - return ! empty($this->_contexts[ $key ]) |
|
1038 | - ? $this->_contexts[ $key ] |
|
1037 | + return ! empty($this->_contexts[$key]) |
|
1038 | + ? $this->_contexts[$key] |
|
1039 | 1039 | : []; |
1040 | 1040 | } |
1041 | 1041 | |
@@ -1056,7 +1056,7 @@ discard block |
||
1056 | 1056 | $all_message_types_valid = true; |
1057 | 1057 | // loop through list of active message types and verify they are installed. |
1058 | 1058 | foreach ($list_of_active_message_type_names as $message_type_name) { |
1059 | - if (! isset($installed_message_types[ $message_type_name ])) { |
|
1059 | + if ( ! isset($installed_message_types[$message_type_name])) { |
|
1060 | 1060 | $this->remove_message_type_has_been_activated_from_all_messengers( |
1061 | 1061 | $message_type_name, |
1062 | 1062 | true |
@@ -1083,8 +1083,8 @@ discard block |
||
1083 | 1083 | public function has_message_type_been_activated_for_messenger($message_type_name, $messenger_name) |
1084 | 1084 | { |
1085 | 1085 | $has_activated = $this->get_has_activated_messengers_option(); |
1086 | - return isset($has_activated[ $messenger_name ]) |
|
1087 | - && in_array($message_type_name, $has_activated[ $messenger_name ]); |
|
1086 | + return isset($has_activated[$messenger_name]) |
|
1087 | + && in_array($message_type_name, $has_activated[$messenger_name]); |
|
1088 | 1088 | } |
1089 | 1089 | |
1090 | 1090 | |
@@ -1113,11 +1113,11 @@ discard block |
||
1113 | 1113 | return; |
1114 | 1114 | } |
1115 | 1115 | $has_activated = $this->get_has_activated_messengers_option(); |
1116 | - $key_for_message_type = isset($has_activated[ $messenger_name ]) |
|
1117 | - ? array_search($message_type_name, $has_activated[ $messenger_name ], true) |
|
1116 | + $key_for_message_type = isset($has_activated[$messenger_name]) |
|
1117 | + ? array_search($message_type_name, $has_activated[$messenger_name], true) |
|
1118 | 1118 | : false; |
1119 | 1119 | if ($key_for_message_type !== false) { |
1120 | - unset($has_activated[ $messenger_name ][ $key_for_message_type ]); |
|
1120 | + unset($has_activated[$messenger_name][$key_for_message_type]); |
|
1121 | 1121 | $this->update_has_activated_messengers_option($has_activated); |
1122 | 1122 | // reset the internal cached property |
1123 | 1123 | $this->get_has_activated_messengers_option(true); |
@@ -15,984 +15,984 @@ |
||
15 | 15 | */ |
16 | 16 | class EE_Messages_Generator |
17 | 17 | { |
18 | - protected EE_Messages_Data_Handler_Collection $_data_handler_collection; |
|
19 | - |
|
20 | - protected EE_Message_Template_Group_Collection $_template_collection; |
|
21 | - |
|
22 | - /** |
|
23 | - * This will hold the data handler for the current EE_Message being generated. |
|
24 | - */ |
|
25 | - protected ?EE_Messages_incoming_data $_current_data_handler = null; |
|
26 | - |
|
27 | - /** |
|
28 | - * This holds the EE_Messages_Queue that contains the messages to generate. |
|
29 | - */ |
|
30 | - protected EE_Messages_Queue $_generation_queue; |
|
31 | - |
|
32 | - /** |
|
33 | - * This holds the EE_Messages_Queue that will store the generated EE_Message objects. |
|
34 | - */ |
|
35 | - protected EE_Messages_Queue $_ready_queue; |
|
36 | - |
|
37 | - /** |
|
38 | - * This is a container for any error messages that get created through the generation |
|
39 | - * process. |
|
40 | - */ |
|
41 | - protected array $_error_msg = []; |
|
42 | - |
|
43 | - /** |
|
44 | - * Flag used to set when the current EE_Message in the generation queue has been verified. |
|
45 | - */ |
|
46 | - protected bool $_verified = false; |
|
47 | - |
|
48 | - /** |
|
49 | - * This will hold the current messenger object corresponding with the current EE_Message in the generation queue. |
|
50 | - */ |
|
51 | - protected ?EE_messenger $_current_messenger = null; |
|
52 | - |
|
53 | - /** |
|
54 | - * This will hold the current message type object corresponding with the current EE_Message in the generation queue. |
|
55 | - */ |
|
56 | - protected ?EE_message_type $_current_message_type = null; |
|
57 | - |
|
58 | - protected EEH_Parse_Shortcodes $_shortcode_parser; |
|
59 | - |
|
60 | - |
|
61 | - /** |
|
62 | - * @param EE_Messages_Queue $generation_queue |
|
63 | - * @param EE_Messages_Queue $ready_queue |
|
64 | - * @param EE_Messages_Data_Handler_Collection $data_handler_collection |
|
65 | - * @param EE_Message_Template_Group_Collection $template_collection |
|
66 | - * @param EEH_Parse_Shortcodes $shortcode_parser |
|
67 | - */ |
|
68 | - public function __construct( |
|
69 | - EE_Messages_Queue $generation_queue, |
|
70 | - EE_Messages_Queue $ready_queue, |
|
71 | - EE_Messages_Data_Handler_Collection $data_handler_collection, |
|
72 | - EE_Message_Template_Group_Collection $template_collection, |
|
73 | - EEH_Parse_Shortcodes $shortcode_parser |
|
74 | - ) { |
|
75 | - $this->_generation_queue = $generation_queue; |
|
76 | - $this->_ready_queue = $ready_queue; |
|
77 | - $this->_data_handler_collection = $data_handler_collection; |
|
78 | - $this->_template_collection = $template_collection; |
|
79 | - $this->_shortcode_parser = $shortcode_parser; |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * @return EE_Messages_Queue |
|
85 | - */ |
|
86 | - public function generation_queue(): EE_Messages_Queue |
|
87 | - { |
|
88 | - return $this->_generation_queue; |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * This iterates through the provided queue and generates the EE_Message objects. |
|
94 | - * When iterating through the queue, the queued item that served as the base for generating other EE_Message |
|
95 | - * objects gets removed and the new EE_Message objects get added to a NEW queue. The NEW queue is then returned |
|
96 | - * for the caller to decide what to do with it. |
|
97 | - * |
|
98 | - * @param bool $save Whether to save the EE_Message objects in the new queue or just return. |
|
99 | - * @return EE_Messages_Queue The new queue for holding generated EE_Message objects. |
|
100 | - * @throws EE_Error |
|
101 | - * @throws InvalidArgumentException |
|
102 | - * @throws InvalidDataTypeException |
|
103 | - * @throws InvalidInterfaceException |
|
104 | - * @throws ReflectionException |
|
105 | - */ |
|
106 | - public function generate(bool $save = true): EE_Messages_Queue |
|
107 | - { |
|
108 | - // iterate through the messages in the queue, generate, and add to new queue. |
|
109 | - $this->_generation_queue->get_message_repository()->rewind(); |
|
110 | - |
|
111 | - while ($this->_generation_queue->get_message_repository()->valid()) { |
|
112 | - // reset "current" properties |
|
113 | - $this->_reset_current_properties(); |
|
114 | - |
|
115 | - $msg = $this->_generation_queue->get_message_repository()->current(); |
|
116 | - |
|
117 | - /** |
|
118 | - * need to get the next object and capture it for setting manually after deletes. The reason is that when |
|
119 | - * an object is removed from the repo then valid for the next object will fail. |
|
120 | - */ |
|
121 | - $this->_generation_queue->get_message_repository()->next(); |
|
122 | - $next_msg = $this->_generation_queue->get_message_repository()->valid() |
|
123 | - ? $this->_generation_queue->get_message_repository()->current() |
|
124 | - : null; |
|
125 | - // restore pointer to current item |
|
126 | - $this->_generation_queue->get_message_repository()->set_current($msg); |
|
127 | - |
|
128 | - // skip and delete if the current $msg is NOT incomplete (queued for generation) |
|
129 | - if ($msg->STS_ID() !== EEM_Message::status_incomplete) { |
|
130 | - // we keep this item in the db just remove from the repo. |
|
131 | - $this->_generation_queue->get_message_repository()->remove($msg); |
|
132 | - // next item |
|
133 | - $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
134 | - continue; |
|
135 | - } |
|
136 | - |
|
137 | - if ($this->_verify()) { |
|
138 | - // let's get generating! |
|
139 | - $this->_generate(); |
|
140 | - } |
|
141 | - |
|
142 | - // don't persist debug_only messages if the messages system is not in debug mode. |
|
143 | - if ( |
|
144 | - $msg->STS_ID() === EEM_Message::status_debug_only |
|
145 | - && ! EEM_Message::debug() |
|
146 | - ) { |
|
147 | - do_action( |
|
148 | - 'AHEE__EE_Messages_Generator__generate__before_debug_delete', |
|
149 | - $msg, |
|
150 | - $this->_error_msg, |
|
151 | - $this->_current_messenger, |
|
152 | - $this->_current_message_type, |
|
153 | - $this->_current_data_handler |
|
154 | - ); |
|
155 | - $this->_generation_queue->get_message_repository()->delete(); |
|
156 | - $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
157 | - continue; |
|
158 | - } |
|
159 | - |
|
160 | - // if there are error messages then let's set the status and the error message. |
|
161 | - if ($this->_error_msg) { |
|
162 | - // if the status is already debug only, then let's leave it at that. |
|
163 | - if ($msg->STS_ID() !== EEM_Message::status_debug_only) { |
|
164 | - $msg->set_STS_ID(EEM_Message::status_failed); |
|
165 | - } |
|
166 | - do_action( |
|
167 | - 'AHEE__EE_Messages_Generator__generate__processing_failed_message', |
|
168 | - $msg, |
|
169 | - $this->_error_msg, |
|
170 | - $this->_current_messenger, |
|
171 | - $this->_current_message_type, |
|
172 | - $this->_current_data_handler |
|
173 | - ); |
|
174 | - $msg->set_error_message( |
|
175 | - esc_html__('Message failed to generate for the following reasons: ', 'event_espresso') |
|
176 | - . "\n" |
|
177 | - . implode("\n", $this->_error_msg) |
|
178 | - ); |
|
179 | - $msg->set_modified(time()); |
|
180 | - } else { |
|
181 | - do_action( |
|
182 | - 'AHEE__EE_Messages_Generator__generate__before_successful_generated_message_delete', |
|
183 | - $msg, |
|
184 | - $this->_error_msg, |
|
185 | - $this->_current_messenger, |
|
186 | - $this->_current_message_type, |
|
187 | - $this->_current_data_handler |
|
188 | - ); |
|
189 | - // remove from db |
|
190 | - $this->_generation_queue->get_message_repository()->delete(); |
|
191 | - } |
|
192 | - // next item |
|
193 | - $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
194 | - } |
|
195 | - |
|
196 | - // generation queue is ALWAYS saved to record any errors in the generation process. |
|
197 | - $this->_generation_queue->save(); |
|
198 | - |
|
199 | - /** |
|
200 | - * save _ready_queue if flag set. |
|
201 | - * Note: The EE_Message objects have values set via the EE_Base_Class::set_field_or_extra_meta() method. This |
|
202 | - * means if a field was added that is not a valid database column. The EE_Message was already saved to the db |
|
203 | - * so a EE_Extra_Meta entry could be created and attached to the EE_Message. In those cases the save flag is |
|
204 | - * irrelevant. |
|
205 | - */ |
|
206 | - if ($save) { |
|
207 | - $this->_ready_queue->save(); |
|
208 | - } |
|
209 | - |
|
210 | - // final reset of properties |
|
211 | - $this->_reset_current_properties(); |
|
212 | - |
|
213 | - return $this->_ready_queue; |
|
214 | - } |
|
215 | - |
|
216 | - |
|
217 | - /** |
|
218 | - * This resets all the properties used for holding "current" values corresponding to the current EE_Message object |
|
219 | - * in the generation queue. |
|
220 | - */ |
|
221 | - protected function _reset_current_properties() |
|
222 | - { |
|
223 | - $this->_verified = false; |
|
224 | - // make sure any _data value in the current message type is reset |
|
225 | - if ($this->_current_message_type instanceof EE_message_type) { |
|
226 | - $this->_current_message_type->reset_data(); |
|
227 | - } |
|
228 | - $this->_current_messenger = $this->_current_message_type = $this->_current_data_handler = null; |
|
229 | - } |
|
230 | - |
|
231 | - |
|
232 | - /** |
|
233 | - * This proceeds with the actual generation of a message. By the time this is called, there should already be a |
|
234 | - * $_current_data_handler set and all incoming information should be validated for the current EE_Message in the |
|
235 | - * _generating_queue. |
|
236 | - * |
|
237 | - * @return bool Whether the message was successfully generated or not. |
|
238 | - * @throws EE_Error |
|
239 | - * @throws InvalidArgumentException |
|
240 | - * @throws InvalidDataTypeException |
|
241 | - * @throws InvalidInterfaceException |
|
242 | - * @throws ReflectionException |
|
243 | - */ |
|
244 | - protected function _generate(): bool |
|
245 | - { |
|
246 | - // double check verification has run and that everything is ready to work with (saves us having to validate |
|
247 | - // everything again). |
|
248 | - if (! $this->_verified) { |
|
249 | - return false; // get out because we don't have a valid setup to work with. |
|
250 | - } |
|
251 | - |
|
252 | - |
|
253 | - try { |
|
254 | - $addressees = $this->_current_message_type->get_addressees( |
|
255 | - $this->_current_data_handler, |
|
256 | - $this->_generation_queue->get_message_repository()->current()->context() |
|
257 | - ); |
|
258 | - } catch (EE_Error $e) { |
|
259 | - $this->_error_msg[] = $e->getMessage(); |
|
260 | - return false; |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - // if no addressees then get out because there is nothing to generation (possible bad data). |
|
265 | - if (! $this->_valid_addressees($addressees)) { |
|
266 | - do_action( |
|
267 | - 'AHEE__EE_Messages_Generator___generate__invalid_addressees', |
|
268 | - $this->_generation_queue->get_message_repository()->current(), |
|
269 | - $addressees, |
|
270 | - $this->_current_messenger, |
|
271 | - $this->_current_message_type, |
|
272 | - $this->_current_data_handler |
|
273 | - ); |
|
274 | - $this->_generation_queue->get_message_repository()->current()->set_STS_ID( |
|
275 | - EEM_Message::status_debug_only |
|
276 | - ); |
|
277 | - $this->_error_msg[] = esc_html__( |
|
278 | - 'This is not a critical error but an informational notice. Unable to generate messages EE_Messages_Addressee objects. There were no attendees prepared by the data handler. Sometimes this is because messages only get generated for certain registration statuses. For example, the ticket notice message type only goes to approved registrations.', |
|
279 | - 'event_espresso' |
|
280 | - ); |
|
281 | - return false; |
|
282 | - } |
|
283 | - |
|
284 | - $message_template_group = $this->_get_message_template_group(); |
|
285 | - |
|
286 | - // in the unlikely event there is no EE_Message_Template_Group available, get out! |
|
287 | - if (! $message_template_group instanceof EE_Message_Template_Group) { |
|
288 | - $this->_error_msg[] = esc_html__( |
|
289 | - 'Unable to get the Message Templates for the Message being generated. No message template group accessible.', |
|
290 | - 'event_espresso' |
|
291 | - ); |
|
292 | - return false; |
|
293 | - } |
|
294 | - |
|
295 | - // get formatted templates for using to parse and setup EE_Message objects. |
|
296 | - $templates = $this->_get_templates($message_template_group); |
|
297 | - |
|
298 | - |
|
299 | - // setup new EE_Message objects (and add to _ready_queue) |
|
300 | - return $this->_assemble_messages($addressees, $templates, $message_template_group); |
|
301 | - } |
|
302 | - |
|
303 | - |
|
304 | - /** |
|
305 | - * Retrieves the message template group being used for generating messages. |
|
306 | - * Note: this also utilizes the EE_Message_Template_Group_Collection to avoid having to hit the db multiple times. |
|
307 | - * |
|
308 | - * @return EE_Message_Template_Group|null |
|
309 | - * @throws EE_Error |
|
310 | - * @throws InvalidArgumentException |
|
311 | - * @throws InvalidDataTypeException |
|
312 | - * @throws InvalidInterfaceException |
|
313 | - * @throws ReflectionException |
|
314 | - */ |
|
315 | - protected function _get_message_template_group(): ?EE_Message_Template_Group |
|
316 | - { |
|
317 | - // first see if there is a specific message template group requested |
|
318 | - // (current message in the queue has a specific GRP_ID) |
|
319 | - $message_template_group = $this->_specific_message_template_group_from_queue(); |
|
320 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
321 | - return $message_template_group; |
|
322 | - } |
|
323 | - |
|
324 | - // get event_ids from the data handler so we can check to see |
|
325 | - // if there's already a message template group for them in the collection. |
|
326 | - $event_ids = $this->_get_event_ids_from_current_data_handler(); |
|
327 | - $message_template_group = $this->_template_collection->get_by_key( |
|
328 | - $this->_template_collection->getKey( |
|
329 | - $this->_current_messenger->name, |
|
330 | - $this->_current_message_type->name, |
|
331 | - $event_ids |
|
332 | - ) |
|
333 | - ); |
|
334 | - |
|
335 | - // if we have a message template group then no need to hit the database, just return it. |
|
336 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
337 | - return $message_template_group; |
|
338 | - } |
|
339 | - |
|
340 | - // get the global group first for this messenger and message type |
|
341 | - // to ensure there is no override set. |
|
342 | - $global_message_template_group = |
|
343 | - $this->_get_global_message_template_group_for_current_messenger_and_message_type(); |
|
344 | - |
|
345 | - if ( |
|
346 | - $global_message_template_group instanceof EE_Message_Template_Group |
|
347 | - && $global_message_template_group->get('MTP_is_override') |
|
348 | - ) { |
|
349 | - return $global_message_template_group; |
|
350 | - } |
|
351 | - |
|
352 | - // if we're still here, that means there was no message template group for the events in the collection and |
|
353 | - // the global message template group for the messenger and message type is not set for override. So next step |
|
354 | - // is to see if there is a common shared custom message template group for this set of events. |
|
355 | - $message_template_group = $this->_get_shared_message_template_for_events($event_ids); |
|
356 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
357 | - return $message_template_group; |
|
358 | - } |
|
359 | - |
|
360 | - // STILL here? Okay that means the fallback is to just use the global message template group for this event |
|
361 | - // set. So we'll cache the global group for this event set (so this logic doesn't have to be repeated in this |
|
362 | - // request) and return it. |
|
363 | - if ($global_message_template_group instanceof EE_Message_Template_Group) { |
|
364 | - $this->_template_collection->add( |
|
365 | - $global_message_template_group, |
|
366 | - $event_ids |
|
367 | - ); |
|
368 | - return $global_message_template_group; |
|
369 | - } |
|
370 | - |
|
371 | - // if we land here that means there's NO active message template group for this set. |
|
372 | - // TODO this will be a good target for some optimization down the road. Whenever there is no active message |
|
373 | - // template group for a given event set then cache that result so we don't repeat the logic. However, for now, |
|
374 | - // this should likely bit hit rarely enough that it's not a significant issue. |
|
375 | - return null; |
|
376 | - } |
|
377 | - |
|
378 | - |
|
379 | - /** |
|
380 | - * This checks the current message in the queue and determines if there is a specific Message Template Group |
|
381 | - * requested for that message. |
|
382 | - * |
|
383 | - * @return EE_Message_Template_Group|null |
|
384 | - * @throws EE_Error |
|
385 | - * @throws InvalidArgumentException |
|
386 | - * @throws InvalidDataTypeException |
|
387 | - * @throws InvalidInterfaceException |
|
388 | - * @throws ReflectionException |
|
389 | - */ |
|
390 | - protected function _specific_message_template_group_from_queue(): ?EE_Message_Template_Group |
|
391 | - { |
|
392 | - // is there a GRP_ID already on the EE_Message object? If there is, then a specific template has been requested |
|
393 | - // so let's use that. |
|
394 | - $GRP_ID = $this->_generation_queue->get_message_repository()->current()->GRP_ID(); |
|
395 | - |
|
396 | - if ($GRP_ID) { |
|
397 | - // attempt to retrieve from repo first |
|
398 | - $message_template_group = $this->_template_collection->get_by_ID($GRP_ID); |
|
399 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
400 | - return $message_template_group; // got it! |
|
401 | - } |
|
402 | - |
|
403 | - // nope don't have it yet. Get from DB then add to repo if its not here, then that means the current GRP_ID |
|
404 | - // is not valid, so we'll continue on in the code assuming there's NO GRP_ID. |
|
405 | - $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID); |
|
406 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
407 | - $this->_template_collection->add($message_template_group); |
|
408 | - return $message_template_group; |
|
409 | - } |
|
410 | - } |
|
411 | - return null; |
|
412 | - } |
|
413 | - |
|
414 | - |
|
415 | - /** |
|
416 | - * Returns whether the event ids passed in all share the same message template group for the current message type |
|
417 | - * and messenger. |
|
418 | - * |
|
419 | - * @param array $event_ids |
|
420 | - * @return bool true means they DO share the same message template group, false means they don't. |
|
421 | - * @throws EE_Error |
|
422 | - * @throws InvalidArgumentException |
|
423 | - * @throws InvalidDataTypeException |
|
424 | - * @throws InvalidInterfaceException |
|
425 | - * @throws ReflectionException |
|
426 | - */ |
|
427 | - protected function _queue_shares_same_message_template_group_for_events(array $event_ids): bool |
|
428 | - { |
|
429 | - foreach ($this->_current_data_handler->events as $event) { |
|
430 | - $event_ids[ $event['ID'] ] = $event['ID']; |
|
431 | - } |
|
432 | - $count_of_message_template_groups = EEM_Message_Template_Group::instance()->count( |
|
433 | - [ |
|
434 | - [ |
|
435 | - 'Event.EVT_ID' => ['IN', $event_ids], |
|
436 | - 'MTP_messenger' => $this->_current_messenger->name, |
|
437 | - 'MTP_message_type' => $this->_current_message_type->name, |
|
438 | - ], |
|
439 | - ], |
|
440 | - 'GRP_ID', |
|
441 | - true |
|
442 | - ); |
|
443 | - return $count_of_message_template_groups === 1; |
|
444 | - } |
|
445 | - |
|
446 | - |
|
447 | - /** |
|
448 | - * This will get the shared message template group for events that are in the current data handler but ONLY if |
|
449 | - * there's a single shared message template group among all the events. Otherwise it returns null. |
|
450 | - * |
|
451 | - * @param array $event_ids |
|
452 | - * @return EE_Message_Template_Group|null |
|
453 | - * @throws EE_Error |
|
454 | - * @throws InvalidArgumentException |
|
455 | - * @throws InvalidDataTypeException |
|
456 | - * @throws InvalidInterfaceException |
|
457 | - * @throws ReflectionException |
|
458 | - */ |
|
459 | - protected function _get_shared_message_template_for_events(array $event_ids): ?EE_Message_Template_Group |
|
460 | - { |
|
461 | - $message_template_group = null; |
|
462 | - if ($this->_queue_shares_same_message_template_group_for_events($event_ids)) { |
|
463 | - $message_template_group = EEM_Message_Template_Group::instance()->get_one( |
|
464 | - [ |
|
465 | - [ |
|
466 | - 'Event.EVT_ID' => ['IN', $event_ids], |
|
467 | - 'MTP_messenger' => $this->_current_messenger->name, |
|
468 | - 'MTP_message_type' => $this->_current_message_type->name, |
|
469 | - 'MTP_is_active' => true, |
|
470 | - ], |
|
471 | - 'group_by' => 'GRP_ID', |
|
472 | - ] |
|
473 | - ); |
|
474 | - // store this in the collection if its valid |
|
475 | - if ($message_template_group instanceof EE_Message_Template_Group) { |
|
476 | - $this->_template_collection->add( |
|
477 | - $message_template_group, |
|
478 | - $event_ids |
|
479 | - ); |
|
480 | - } |
|
481 | - } |
|
482 | - return $message_template_group; |
|
483 | - } |
|
484 | - |
|
485 | - |
|
486 | - /** |
|
487 | - * Retrieves the global message template group for the current messenger and message type. |
|
488 | - * |
|
489 | - * @return EE_Message_Template_Group|null |
|
490 | - * @throws EE_Error |
|
491 | - * @throws InvalidArgumentException |
|
492 | - * @throws InvalidDataTypeException |
|
493 | - * @throws InvalidInterfaceException |
|
494 | - * @throws ReflectionException |
|
495 | - */ |
|
496 | - protected function _get_global_message_template_group_for_current_messenger_and_message_type( |
|
497 | - ): ?EE_Message_Template_Group |
|
498 | - { |
|
499 | - // first check the collection (we use an array with 0 in it to represent global groups). |
|
500 | - $global_message_template_group = $this->_template_collection->get_by_key( |
|
501 | - $this->_template_collection->getKey( |
|
502 | - $this->_current_messenger->name, |
|
503 | - $this->_current_message_type->name, |
|
504 | - [0] |
|
505 | - ) |
|
506 | - ); |
|
507 | - |
|
508 | - // if we don't have a group lets hit the db. |
|
509 | - if (! $global_message_template_group instanceof EE_Message_Template_Group) { |
|
510 | - $global_message_template_group = EEM_Message_Template_Group::instance()->get_one( |
|
511 | - [ |
|
512 | - [ |
|
513 | - 'MTP_messenger' => $this->_current_messenger->name, |
|
514 | - 'MTP_message_type' => $this->_current_message_type->name, |
|
515 | - 'MTP_is_active' => true, |
|
516 | - 'MTP_is_global' => true, |
|
517 | - ], |
|
518 | - ] |
|
519 | - ); |
|
520 | - // if we have a group, add it to the collection. |
|
521 | - if ($global_message_template_group instanceof EE_Message_Template_Group) { |
|
522 | - $this->_template_collection->add( |
|
523 | - $global_message_template_group, |
|
524 | - [0] |
|
525 | - ); |
|
526 | - } |
|
527 | - } |
|
528 | - return $global_message_template_group; |
|
529 | - } |
|
530 | - |
|
531 | - |
|
532 | - /** |
|
533 | - * Returns an array of event ids for all the events within the current data handler. |
|
534 | - * |
|
535 | - * @return array |
|
536 | - */ |
|
537 | - protected function _get_event_ids_from_current_data_handler(): array |
|
538 | - { |
|
539 | - $event_ids = []; |
|
540 | - foreach ($this->_current_data_handler->events as $event) { |
|
541 | - $event_ids[ $event['ID'] ] = $event['ID']; |
|
542 | - } |
|
543 | - return $event_ids; |
|
544 | - } |
|
545 | - |
|
546 | - |
|
547 | - /** |
|
548 | - * Retrieves formatted array of template information for each context specific to the given |
|
549 | - * EE_Message_Template_Group |
|
550 | - * |
|
551 | - * @param EE_Message_Template_Group $message_template_group |
|
552 | - * @return array The returned array is in this structure: |
|
553 | - * array( |
|
554 | - * 'field_name' => array( |
|
555 | - * 'context' => 'content' |
|
556 | - * ) |
|
557 | - * ) |
|
558 | - * @throws EE_Error |
|
559 | - * @throws InvalidArgumentException |
|
560 | - * @throws InvalidDataTypeException |
|
561 | - * @throws InvalidInterfaceException |
|
562 | - * @throws ReflectionException |
|
563 | - */ |
|
564 | - protected function _get_templates(EE_Message_Template_Group $message_template_group): array |
|
565 | - { |
|
566 | - $templates = []; |
|
567 | - $context_templates = $message_template_group->context_templates(); |
|
568 | - foreach ($context_templates as $context => $template_fields) { |
|
569 | - foreach ($template_fields as $template_field => $template_obj) { |
|
570 | - if (! $template_obj instanceof EE_Message_Template) { |
|
571 | - continue; |
|
572 | - } |
|
573 | - $templates[ $template_field ][ $context ] = $template_obj->get('MTP_content'); |
|
574 | - } |
|
575 | - } |
|
576 | - return $templates; |
|
577 | - } |
|
578 | - |
|
579 | - |
|
580 | - /** |
|
581 | - * Assembles new fully generated EE_Message objects and adds to _ready_queue |
|
582 | - * |
|
583 | - * @param array $addressees Array of EE_Messages_Addressee objects indexed by message type |
|
584 | - * context. |
|
585 | - * @param array $templates formatted array of templates used for parsing data. |
|
586 | - * @param EE_Message_Template_Group $message_template_group |
|
587 | - * @return bool true if message generation went a-ok. false if some sort of exception occurred. Note: The |
|
588 | - * method will attempt to generate ALL EE_Message objects and add to |
|
589 | - * the _ready_queue. Successfully generated messages get added to the |
|
590 | - * queue with EEM_Message::status_idle, unsuccessfully generated |
|
591 | - * messages will get added to the queue as EEM_Message::status_failed. |
|
592 | - * Very rarely should "false" be returned from this method. |
|
593 | - * @throws EE_Error |
|
594 | - * @throws InvalidArgumentException |
|
595 | - * @throws InvalidDataTypeException |
|
596 | - * @throws InvalidIdentifierException |
|
597 | - * @throws InvalidInterfaceException |
|
598 | - * @throws ReflectionException |
|
599 | - */ |
|
600 | - protected function _assemble_messages( |
|
601 | - array $addressees, |
|
602 | - array $templates, |
|
603 | - EE_Message_Template_Group $message_template_group |
|
604 | - ): bool { |
|
605 | - // if templates are empty then get out because we can't generate anything. |
|
606 | - if (! $templates) { |
|
607 | - $this->_error_msg[] = esc_html__( |
|
608 | - 'Unable to assemble messages because there are no templates retrieved for generating the messages with', |
|
609 | - 'event_espresso' |
|
610 | - ); |
|
611 | - return false; |
|
612 | - } |
|
613 | - |
|
614 | - // We use this as the counter for generated messages because don't forget we may be executing this inside of a |
|
615 | - // generation_queue. So _ready_queue may have generated EE_Message objects already. |
|
616 | - $generated_count = 0; |
|
617 | - foreach ($addressees as $context => $recipients) { |
|
618 | - foreach ($recipients as $recipient) { |
|
619 | - $message = $this->_setup_message_object($context, $recipient, $templates, $message_template_group); |
|
620 | - if ($message instanceof EE_Message) { |
|
621 | - $this->_ready_queue->add( |
|
622 | - $message, |
|
623 | - [], |
|
624 | - $this->_generation_queue->get_message_repository()->is_preview(), |
|
625 | - $this->_generation_queue->get_message_repository()->is_test_send() |
|
626 | - ); |
|
627 | - $generated_count++; |
|
628 | - } |
|
629 | - |
|
630 | - // if the current MSG being generated is for a test send then we'll only use ONE message in the |
|
631 | - // generation. |
|
632 | - if ($this->_generation_queue->get_message_repository()->is_test_send()) { |
|
633 | - break 2; |
|
634 | - } |
|
635 | - } |
|
636 | - } |
|
637 | - |
|
638 | - // if there are no generated messages then something else fatal went wrong. |
|
639 | - return $generated_count > 0; |
|
640 | - } |
|
641 | - |
|
642 | - |
|
643 | - /** |
|
644 | - * @param string $context The context for the generated message. |
|
645 | - * @param EE_Messages_Addressee $recipient |
|
646 | - * @param array $templates formatted array of templates used for parsing data. |
|
647 | - * @param EE_Message_Template_Group $message_template_group |
|
648 | - * @return bool|EE_Message |
|
649 | - * @throws EE_Error |
|
650 | - * @throws InvalidArgumentException |
|
651 | - * @throws InvalidDataTypeException |
|
652 | - * @throws InvalidInterfaceException |
|
653 | - * @throws ReflectionException |
|
654 | - * @throws InvalidIdentifierException |
|
655 | - */ |
|
656 | - protected function _setup_message_object( |
|
657 | - string $context, |
|
658 | - EE_Messages_Addressee $recipient, |
|
659 | - array $templates, |
|
660 | - EE_Message_Template_Group $message_template_group |
|
661 | - ) { |
|
662 | - // stuff we already know |
|
663 | - $transaction_id = $recipient->txn instanceof EE_Transaction |
|
664 | - ? $recipient->txn->ID() |
|
665 | - : 0; |
|
666 | - $transaction_id = empty($transaction_id) && $this->_current_data_handler->txn instanceof EE_Transaction |
|
667 | - ? $this->_current_data_handler->txn->ID() |
|
668 | - : $transaction_id; |
|
669 | - $message_fields = [ |
|
670 | - 'GRP_ID' => $message_template_group->ID(), |
|
671 | - 'TXN_ID' => $transaction_id, |
|
672 | - 'MSG_messenger' => $this->_current_messenger->name, |
|
673 | - 'MSG_message_type' => $this->_current_message_type->name, |
|
674 | - 'MSG_context' => $context, |
|
675 | - ]; |
|
676 | - |
|
677 | - // recipient id and type should be on the EE_Messages_Addressee object but if this is empty, let's try to grab |
|
678 | - // the info from the att_obj found in the EE_Messages_Addressee object. |
|
679 | - if (empty($recipient->recipient_id) || empty($recipient->recipient_type)) { |
|
680 | - $message_fields['MSG_recipient_ID'] = $recipient->att_obj instanceof EE_Attendee |
|
681 | - ? $recipient->att_obj->ID() |
|
682 | - : 0; |
|
683 | - $message_fields['MSG_recipient_type'] = 'Attendee'; |
|
684 | - } else { |
|
685 | - $message_fields['MSG_recipient_ID'] = $recipient->recipient_id; |
|
686 | - $message_fields['MSG_recipient_type'] = $recipient->recipient_type; |
|
687 | - } |
|
688 | - $message = EE_Message_Factory::create($message_fields); |
|
689 | - |
|
690 | - // grab valid shortcodes for shortcode parser |
|
691 | - $mt_shortcodes = $this->_current_message_type->get_valid_shortcodes(); |
|
692 | - $m_shortcodes = $this->_current_messenger->get_valid_shortcodes(); |
|
693 | - |
|
694 | - // if the 'to' field is empty or the context is inactive we skip EXCEPT if we're previewing |
|
695 | - if ( |
|
696 | - ! $this->_generation_queue->get_message_repository()->is_preview() |
|
697 | - && ( |
|
698 | - (empty($templates['to'][ $context ]) && ! $this->_current_messenger->allow_empty_to_field()) |
|
699 | - || ! $message_template_group->is_context_active($context) |
|
700 | - ) |
|
701 | - ) { |
|
702 | - // we silently exit here and do NOT record a fail because the message is "turned off" by having no "to" |
|
703 | - // field. |
|
704 | - return false; |
|
705 | - } |
|
706 | - $error_msg = []; |
|
707 | - foreach ($templates as $field => $field_context) { |
|
708 | - $error_msg = []; |
|
709 | - // let's setup the valid shortcodes for the incoming context. |
|
710 | - $valid_shortcodes = $mt_shortcodes[ $context ]; |
|
711 | - // merge in valid shortcodes for the field. |
|
712 | - $shortcodes = $m_shortcodes[ $field ] ?? $valid_shortcodes; |
|
713 | - if (isset($field_context[ $context ])) { |
|
714 | - // prefix field. |
|
715 | - $column_name = 'MSG_' . $field; |
|
716 | - try { |
|
717 | - $content = $this->_shortcode_parser->parse_message_template( |
|
718 | - $field_context[ $context ], |
|
719 | - $recipient, |
|
720 | - $shortcodes, |
|
721 | - $this->_current_message_type, |
|
722 | - $this->_current_messenger, |
|
723 | - $message |
|
724 | - ); |
|
725 | - // the model field removes slashes when setting (usually necessary when the input is from the |
|
726 | - // request) but this value is from another model and has no slashes. So add them so it matches |
|
727 | - // what the field expected (otherwise slashes will have been stripped from this an extra time) |
|
728 | - $message->set_field_or_extra_meta($column_name, addslashes($content)); |
|
729 | - } catch (EE_Error $e) { |
|
730 | - $error_msg[] = sprintf( |
|
731 | - /* Translators: First place holder is message model field name. |
|
18 | + protected EE_Messages_Data_Handler_Collection $_data_handler_collection; |
|
19 | + |
|
20 | + protected EE_Message_Template_Group_Collection $_template_collection; |
|
21 | + |
|
22 | + /** |
|
23 | + * This will hold the data handler for the current EE_Message being generated. |
|
24 | + */ |
|
25 | + protected ?EE_Messages_incoming_data $_current_data_handler = null; |
|
26 | + |
|
27 | + /** |
|
28 | + * This holds the EE_Messages_Queue that contains the messages to generate. |
|
29 | + */ |
|
30 | + protected EE_Messages_Queue $_generation_queue; |
|
31 | + |
|
32 | + /** |
|
33 | + * This holds the EE_Messages_Queue that will store the generated EE_Message objects. |
|
34 | + */ |
|
35 | + protected EE_Messages_Queue $_ready_queue; |
|
36 | + |
|
37 | + /** |
|
38 | + * This is a container for any error messages that get created through the generation |
|
39 | + * process. |
|
40 | + */ |
|
41 | + protected array $_error_msg = []; |
|
42 | + |
|
43 | + /** |
|
44 | + * Flag used to set when the current EE_Message in the generation queue has been verified. |
|
45 | + */ |
|
46 | + protected bool $_verified = false; |
|
47 | + |
|
48 | + /** |
|
49 | + * This will hold the current messenger object corresponding with the current EE_Message in the generation queue. |
|
50 | + */ |
|
51 | + protected ?EE_messenger $_current_messenger = null; |
|
52 | + |
|
53 | + /** |
|
54 | + * This will hold the current message type object corresponding with the current EE_Message in the generation queue. |
|
55 | + */ |
|
56 | + protected ?EE_message_type $_current_message_type = null; |
|
57 | + |
|
58 | + protected EEH_Parse_Shortcodes $_shortcode_parser; |
|
59 | + |
|
60 | + |
|
61 | + /** |
|
62 | + * @param EE_Messages_Queue $generation_queue |
|
63 | + * @param EE_Messages_Queue $ready_queue |
|
64 | + * @param EE_Messages_Data_Handler_Collection $data_handler_collection |
|
65 | + * @param EE_Message_Template_Group_Collection $template_collection |
|
66 | + * @param EEH_Parse_Shortcodes $shortcode_parser |
|
67 | + */ |
|
68 | + public function __construct( |
|
69 | + EE_Messages_Queue $generation_queue, |
|
70 | + EE_Messages_Queue $ready_queue, |
|
71 | + EE_Messages_Data_Handler_Collection $data_handler_collection, |
|
72 | + EE_Message_Template_Group_Collection $template_collection, |
|
73 | + EEH_Parse_Shortcodes $shortcode_parser |
|
74 | + ) { |
|
75 | + $this->_generation_queue = $generation_queue; |
|
76 | + $this->_ready_queue = $ready_queue; |
|
77 | + $this->_data_handler_collection = $data_handler_collection; |
|
78 | + $this->_template_collection = $template_collection; |
|
79 | + $this->_shortcode_parser = $shortcode_parser; |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * @return EE_Messages_Queue |
|
85 | + */ |
|
86 | + public function generation_queue(): EE_Messages_Queue |
|
87 | + { |
|
88 | + return $this->_generation_queue; |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * This iterates through the provided queue and generates the EE_Message objects. |
|
94 | + * When iterating through the queue, the queued item that served as the base for generating other EE_Message |
|
95 | + * objects gets removed and the new EE_Message objects get added to a NEW queue. The NEW queue is then returned |
|
96 | + * for the caller to decide what to do with it. |
|
97 | + * |
|
98 | + * @param bool $save Whether to save the EE_Message objects in the new queue or just return. |
|
99 | + * @return EE_Messages_Queue The new queue for holding generated EE_Message objects. |
|
100 | + * @throws EE_Error |
|
101 | + * @throws InvalidArgumentException |
|
102 | + * @throws InvalidDataTypeException |
|
103 | + * @throws InvalidInterfaceException |
|
104 | + * @throws ReflectionException |
|
105 | + */ |
|
106 | + public function generate(bool $save = true): EE_Messages_Queue |
|
107 | + { |
|
108 | + // iterate through the messages in the queue, generate, and add to new queue. |
|
109 | + $this->_generation_queue->get_message_repository()->rewind(); |
|
110 | + |
|
111 | + while ($this->_generation_queue->get_message_repository()->valid()) { |
|
112 | + // reset "current" properties |
|
113 | + $this->_reset_current_properties(); |
|
114 | + |
|
115 | + $msg = $this->_generation_queue->get_message_repository()->current(); |
|
116 | + |
|
117 | + /** |
|
118 | + * need to get the next object and capture it for setting manually after deletes. The reason is that when |
|
119 | + * an object is removed from the repo then valid for the next object will fail. |
|
120 | + */ |
|
121 | + $this->_generation_queue->get_message_repository()->next(); |
|
122 | + $next_msg = $this->_generation_queue->get_message_repository()->valid() |
|
123 | + ? $this->_generation_queue->get_message_repository()->current() |
|
124 | + : null; |
|
125 | + // restore pointer to current item |
|
126 | + $this->_generation_queue->get_message_repository()->set_current($msg); |
|
127 | + |
|
128 | + // skip and delete if the current $msg is NOT incomplete (queued for generation) |
|
129 | + if ($msg->STS_ID() !== EEM_Message::status_incomplete) { |
|
130 | + // we keep this item in the db just remove from the repo. |
|
131 | + $this->_generation_queue->get_message_repository()->remove($msg); |
|
132 | + // next item |
|
133 | + $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
134 | + continue; |
|
135 | + } |
|
136 | + |
|
137 | + if ($this->_verify()) { |
|
138 | + // let's get generating! |
|
139 | + $this->_generate(); |
|
140 | + } |
|
141 | + |
|
142 | + // don't persist debug_only messages if the messages system is not in debug mode. |
|
143 | + if ( |
|
144 | + $msg->STS_ID() === EEM_Message::status_debug_only |
|
145 | + && ! EEM_Message::debug() |
|
146 | + ) { |
|
147 | + do_action( |
|
148 | + 'AHEE__EE_Messages_Generator__generate__before_debug_delete', |
|
149 | + $msg, |
|
150 | + $this->_error_msg, |
|
151 | + $this->_current_messenger, |
|
152 | + $this->_current_message_type, |
|
153 | + $this->_current_data_handler |
|
154 | + ); |
|
155 | + $this->_generation_queue->get_message_repository()->delete(); |
|
156 | + $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
157 | + continue; |
|
158 | + } |
|
159 | + |
|
160 | + // if there are error messages then let's set the status and the error message. |
|
161 | + if ($this->_error_msg) { |
|
162 | + // if the status is already debug only, then let's leave it at that. |
|
163 | + if ($msg->STS_ID() !== EEM_Message::status_debug_only) { |
|
164 | + $msg->set_STS_ID(EEM_Message::status_failed); |
|
165 | + } |
|
166 | + do_action( |
|
167 | + 'AHEE__EE_Messages_Generator__generate__processing_failed_message', |
|
168 | + $msg, |
|
169 | + $this->_error_msg, |
|
170 | + $this->_current_messenger, |
|
171 | + $this->_current_message_type, |
|
172 | + $this->_current_data_handler |
|
173 | + ); |
|
174 | + $msg->set_error_message( |
|
175 | + esc_html__('Message failed to generate for the following reasons: ', 'event_espresso') |
|
176 | + . "\n" |
|
177 | + . implode("\n", $this->_error_msg) |
|
178 | + ); |
|
179 | + $msg->set_modified(time()); |
|
180 | + } else { |
|
181 | + do_action( |
|
182 | + 'AHEE__EE_Messages_Generator__generate__before_successful_generated_message_delete', |
|
183 | + $msg, |
|
184 | + $this->_error_msg, |
|
185 | + $this->_current_messenger, |
|
186 | + $this->_current_message_type, |
|
187 | + $this->_current_data_handler |
|
188 | + ); |
|
189 | + // remove from db |
|
190 | + $this->_generation_queue->get_message_repository()->delete(); |
|
191 | + } |
|
192 | + // next item |
|
193 | + $this->_generation_queue->get_message_repository()->set_current($next_msg); |
|
194 | + } |
|
195 | + |
|
196 | + // generation queue is ALWAYS saved to record any errors in the generation process. |
|
197 | + $this->_generation_queue->save(); |
|
198 | + |
|
199 | + /** |
|
200 | + * save _ready_queue if flag set. |
|
201 | + * Note: The EE_Message objects have values set via the EE_Base_Class::set_field_or_extra_meta() method. This |
|
202 | + * means if a field was added that is not a valid database column. The EE_Message was already saved to the db |
|
203 | + * so a EE_Extra_Meta entry could be created and attached to the EE_Message. In those cases the save flag is |
|
204 | + * irrelevant. |
|
205 | + */ |
|
206 | + if ($save) { |
|
207 | + $this->_ready_queue->save(); |
|
208 | + } |
|
209 | + |
|
210 | + // final reset of properties |
|
211 | + $this->_reset_current_properties(); |
|
212 | + |
|
213 | + return $this->_ready_queue; |
|
214 | + } |
|
215 | + |
|
216 | + |
|
217 | + /** |
|
218 | + * This resets all the properties used for holding "current" values corresponding to the current EE_Message object |
|
219 | + * in the generation queue. |
|
220 | + */ |
|
221 | + protected function _reset_current_properties() |
|
222 | + { |
|
223 | + $this->_verified = false; |
|
224 | + // make sure any _data value in the current message type is reset |
|
225 | + if ($this->_current_message_type instanceof EE_message_type) { |
|
226 | + $this->_current_message_type->reset_data(); |
|
227 | + } |
|
228 | + $this->_current_messenger = $this->_current_message_type = $this->_current_data_handler = null; |
|
229 | + } |
|
230 | + |
|
231 | + |
|
232 | + /** |
|
233 | + * This proceeds with the actual generation of a message. By the time this is called, there should already be a |
|
234 | + * $_current_data_handler set and all incoming information should be validated for the current EE_Message in the |
|
235 | + * _generating_queue. |
|
236 | + * |
|
237 | + * @return bool Whether the message was successfully generated or not. |
|
238 | + * @throws EE_Error |
|
239 | + * @throws InvalidArgumentException |
|
240 | + * @throws InvalidDataTypeException |
|
241 | + * @throws InvalidInterfaceException |
|
242 | + * @throws ReflectionException |
|
243 | + */ |
|
244 | + protected function _generate(): bool |
|
245 | + { |
|
246 | + // double check verification has run and that everything is ready to work with (saves us having to validate |
|
247 | + // everything again). |
|
248 | + if (! $this->_verified) { |
|
249 | + return false; // get out because we don't have a valid setup to work with. |
|
250 | + } |
|
251 | + |
|
252 | + |
|
253 | + try { |
|
254 | + $addressees = $this->_current_message_type->get_addressees( |
|
255 | + $this->_current_data_handler, |
|
256 | + $this->_generation_queue->get_message_repository()->current()->context() |
|
257 | + ); |
|
258 | + } catch (EE_Error $e) { |
|
259 | + $this->_error_msg[] = $e->getMessage(); |
|
260 | + return false; |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + // if no addressees then get out because there is nothing to generation (possible bad data). |
|
265 | + if (! $this->_valid_addressees($addressees)) { |
|
266 | + do_action( |
|
267 | + 'AHEE__EE_Messages_Generator___generate__invalid_addressees', |
|
268 | + $this->_generation_queue->get_message_repository()->current(), |
|
269 | + $addressees, |
|
270 | + $this->_current_messenger, |
|
271 | + $this->_current_message_type, |
|
272 | + $this->_current_data_handler |
|
273 | + ); |
|
274 | + $this->_generation_queue->get_message_repository()->current()->set_STS_ID( |
|
275 | + EEM_Message::status_debug_only |
|
276 | + ); |
|
277 | + $this->_error_msg[] = esc_html__( |
|
278 | + 'This is not a critical error but an informational notice. Unable to generate messages EE_Messages_Addressee objects. There were no attendees prepared by the data handler. Sometimes this is because messages only get generated for certain registration statuses. For example, the ticket notice message type only goes to approved registrations.', |
|
279 | + 'event_espresso' |
|
280 | + ); |
|
281 | + return false; |
|
282 | + } |
|
283 | + |
|
284 | + $message_template_group = $this->_get_message_template_group(); |
|
285 | + |
|
286 | + // in the unlikely event there is no EE_Message_Template_Group available, get out! |
|
287 | + if (! $message_template_group instanceof EE_Message_Template_Group) { |
|
288 | + $this->_error_msg[] = esc_html__( |
|
289 | + 'Unable to get the Message Templates for the Message being generated. No message template group accessible.', |
|
290 | + 'event_espresso' |
|
291 | + ); |
|
292 | + return false; |
|
293 | + } |
|
294 | + |
|
295 | + // get formatted templates for using to parse and setup EE_Message objects. |
|
296 | + $templates = $this->_get_templates($message_template_group); |
|
297 | + |
|
298 | + |
|
299 | + // setup new EE_Message objects (and add to _ready_queue) |
|
300 | + return $this->_assemble_messages($addressees, $templates, $message_template_group); |
|
301 | + } |
|
302 | + |
|
303 | + |
|
304 | + /** |
|
305 | + * Retrieves the message template group being used for generating messages. |
|
306 | + * Note: this also utilizes the EE_Message_Template_Group_Collection to avoid having to hit the db multiple times. |
|
307 | + * |
|
308 | + * @return EE_Message_Template_Group|null |
|
309 | + * @throws EE_Error |
|
310 | + * @throws InvalidArgumentException |
|
311 | + * @throws InvalidDataTypeException |
|
312 | + * @throws InvalidInterfaceException |
|
313 | + * @throws ReflectionException |
|
314 | + */ |
|
315 | + protected function _get_message_template_group(): ?EE_Message_Template_Group |
|
316 | + { |
|
317 | + // first see if there is a specific message template group requested |
|
318 | + // (current message in the queue has a specific GRP_ID) |
|
319 | + $message_template_group = $this->_specific_message_template_group_from_queue(); |
|
320 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
321 | + return $message_template_group; |
|
322 | + } |
|
323 | + |
|
324 | + // get event_ids from the data handler so we can check to see |
|
325 | + // if there's already a message template group for them in the collection. |
|
326 | + $event_ids = $this->_get_event_ids_from_current_data_handler(); |
|
327 | + $message_template_group = $this->_template_collection->get_by_key( |
|
328 | + $this->_template_collection->getKey( |
|
329 | + $this->_current_messenger->name, |
|
330 | + $this->_current_message_type->name, |
|
331 | + $event_ids |
|
332 | + ) |
|
333 | + ); |
|
334 | + |
|
335 | + // if we have a message template group then no need to hit the database, just return it. |
|
336 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
337 | + return $message_template_group; |
|
338 | + } |
|
339 | + |
|
340 | + // get the global group first for this messenger and message type |
|
341 | + // to ensure there is no override set. |
|
342 | + $global_message_template_group = |
|
343 | + $this->_get_global_message_template_group_for_current_messenger_and_message_type(); |
|
344 | + |
|
345 | + if ( |
|
346 | + $global_message_template_group instanceof EE_Message_Template_Group |
|
347 | + && $global_message_template_group->get('MTP_is_override') |
|
348 | + ) { |
|
349 | + return $global_message_template_group; |
|
350 | + } |
|
351 | + |
|
352 | + // if we're still here, that means there was no message template group for the events in the collection and |
|
353 | + // the global message template group for the messenger and message type is not set for override. So next step |
|
354 | + // is to see if there is a common shared custom message template group for this set of events. |
|
355 | + $message_template_group = $this->_get_shared_message_template_for_events($event_ids); |
|
356 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
357 | + return $message_template_group; |
|
358 | + } |
|
359 | + |
|
360 | + // STILL here? Okay that means the fallback is to just use the global message template group for this event |
|
361 | + // set. So we'll cache the global group for this event set (so this logic doesn't have to be repeated in this |
|
362 | + // request) and return it. |
|
363 | + if ($global_message_template_group instanceof EE_Message_Template_Group) { |
|
364 | + $this->_template_collection->add( |
|
365 | + $global_message_template_group, |
|
366 | + $event_ids |
|
367 | + ); |
|
368 | + return $global_message_template_group; |
|
369 | + } |
|
370 | + |
|
371 | + // if we land here that means there's NO active message template group for this set. |
|
372 | + // TODO this will be a good target for some optimization down the road. Whenever there is no active message |
|
373 | + // template group for a given event set then cache that result so we don't repeat the logic. However, for now, |
|
374 | + // this should likely bit hit rarely enough that it's not a significant issue. |
|
375 | + return null; |
|
376 | + } |
|
377 | + |
|
378 | + |
|
379 | + /** |
|
380 | + * This checks the current message in the queue and determines if there is a specific Message Template Group |
|
381 | + * requested for that message. |
|
382 | + * |
|
383 | + * @return EE_Message_Template_Group|null |
|
384 | + * @throws EE_Error |
|
385 | + * @throws InvalidArgumentException |
|
386 | + * @throws InvalidDataTypeException |
|
387 | + * @throws InvalidInterfaceException |
|
388 | + * @throws ReflectionException |
|
389 | + */ |
|
390 | + protected function _specific_message_template_group_from_queue(): ?EE_Message_Template_Group |
|
391 | + { |
|
392 | + // is there a GRP_ID already on the EE_Message object? If there is, then a specific template has been requested |
|
393 | + // so let's use that. |
|
394 | + $GRP_ID = $this->_generation_queue->get_message_repository()->current()->GRP_ID(); |
|
395 | + |
|
396 | + if ($GRP_ID) { |
|
397 | + // attempt to retrieve from repo first |
|
398 | + $message_template_group = $this->_template_collection->get_by_ID($GRP_ID); |
|
399 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
400 | + return $message_template_group; // got it! |
|
401 | + } |
|
402 | + |
|
403 | + // nope don't have it yet. Get from DB then add to repo if its not here, then that means the current GRP_ID |
|
404 | + // is not valid, so we'll continue on in the code assuming there's NO GRP_ID. |
|
405 | + $message_template_group = EEM_Message_Template_Group::instance()->get_one_by_ID($GRP_ID); |
|
406 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
407 | + $this->_template_collection->add($message_template_group); |
|
408 | + return $message_template_group; |
|
409 | + } |
|
410 | + } |
|
411 | + return null; |
|
412 | + } |
|
413 | + |
|
414 | + |
|
415 | + /** |
|
416 | + * Returns whether the event ids passed in all share the same message template group for the current message type |
|
417 | + * and messenger. |
|
418 | + * |
|
419 | + * @param array $event_ids |
|
420 | + * @return bool true means they DO share the same message template group, false means they don't. |
|
421 | + * @throws EE_Error |
|
422 | + * @throws InvalidArgumentException |
|
423 | + * @throws InvalidDataTypeException |
|
424 | + * @throws InvalidInterfaceException |
|
425 | + * @throws ReflectionException |
|
426 | + */ |
|
427 | + protected function _queue_shares_same_message_template_group_for_events(array $event_ids): bool |
|
428 | + { |
|
429 | + foreach ($this->_current_data_handler->events as $event) { |
|
430 | + $event_ids[ $event['ID'] ] = $event['ID']; |
|
431 | + } |
|
432 | + $count_of_message_template_groups = EEM_Message_Template_Group::instance()->count( |
|
433 | + [ |
|
434 | + [ |
|
435 | + 'Event.EVT_ID' => ['IN', $event_ids], |
|
436 | + 'MTP_messenger' => $this->_current_messenger->name, |
|
437 | + 'MTP_message_type' => $this->_current_message_type->name, |
|
438 | + ], |
|
439 | + ], |
|
440 | + 'GRP_ID', |
|
441 | + true |
|
442 | + ); |
|
443 | + return $count_of_message_template_groups === 1; |
|
444 | + } |
|
445 | + |
|
446 | + |
|
447 | + /** |
|
448 | + * This will get the shared message template group for events that are in the current data handler but ONLY if |
|
449 | + * there's a single shared message template group among all the events. Otherwise it returns null. |
|
450 | + * |
|
451 | + * @param array $event_ids |
|
452 | + * @return EE_Message_Template_Group|null |
|
453 | + * @throws EE_Error |
|
454 | + * @throws InvalidArgumentException |
|
455 | + * @throws InvalidDataTypeException |
|
456 | + * @throws InvalidInterfaceException |
|
457 | + * @throws ReflectionException |
|
458 | + */ |
|
459 | + protected function _get_shared_message_template_for_events(array $event_ids): ?EE_Message_Template_Group |
|
460 | + { |
|
461 | + $message_template_group = null; |
|
462 | + if ($this->_queue_shares_same_message_template_group_for_events($event_ids)) { |
|
463 | + $message_template_group = EEM_Message_Template_Group::instance()->get_one( |
|
464 | + [ |
|
465 | + [ |
|
466 | + 'Event.EVT_ID' => ['IN', $event_ids], |
|
467 | + 'MTP_messenger' => $this->_current_messenger->name, |
|
468 | + 'MTP_message_type' => $this->_current_message_type->name, |
|
469 | + 'MTP_is_active' => true, |
|
470 | + ], |
|
471 | + 'group_by' => 'GRP_ID', |
|
472 | + ] |
|
473 | + ); |
|
474 | + // store this in the collection if its valid |
|
475 | + if ($message_template_group instanceof EE_Message_Template_Group) { |
|
476 | + $this->_template_collection->add( |
|
477 | + $message_template_group, |
|
478 | + $event_ids |
|
479 | + ); |
|
480 | + } |
|
481 | + } |
|
482 | + return $message_template_group; |
|
483 | + } |
|
484 | + |
|
485 | + |
|
486 | + /** |
|
487 | + * Retrieves the global message template group for the current messenger and message type. |
|
488 | + * |
|
489 | + * @return EE_Message_Template_Group|null |
|
490 | + * @throws EE_Error |
|
491 | + * @throws InvalidArgumentException |
|
492 | + * @throws InvalidDataTypeException |
|
493 | + * @throws InvalidInterfaceException |
|
494 | + * @throws ReflectionException |
|
495 | + */ |
|
496 | + protected function _get_global_message_template_group_for_current_messenger_and_message_type( |
|
497 | + ): ?EE_Message_Template_Group |
|
498 | + { |
|
499 | + // first check the collection (we use an array with 0 in it to represent global groups). |
|
500 | + $global_message_template_group = $this->_template_collection->get_by_key( |
|
501 | + $this->_template_collection->getKey( |
|
502 | + $this->_current_messenger->name, |
|
503 | + $this->_current_message_type->name, |
|
504 | + [0] |
|
505 | + ) |
|
506 | + ); |
|
507 | + |
|
508 | + // if we don't have a group lets hit the db. |
|
509 | + if (! $global_message_template_group instanceof EE_Message_Template_Group) { |
|
510 | + $global_message_template_group = EEM_Message_Template_Group::instance()->get_one( |
|
511 | + [ |
|
512 | + [ |
|
513 | + 'MTP_messenger' => $this->_current_messenger->name, |
|
514 | + 'MTP_message_type' => $this->_current_message_type->name, |
|
515 | + 'MTP_is_active' => true, |
|
516 | + 'MTP_is_global' => true, |
|
517 | + ], |
|
518 | + ] |
|
519 | + ); |
|
520 | + // if we have a group, add it to the collection. |
|
521 | + if ($global_message_template_group instanceof EE_Message_Template_Group) { |
|
522 | + $this->_template_collection->add( |
|
523 | + $global_message_template_group, |
|
524 | + [0] |
|
525 | + ); |
|
526 | + } |
|
527 | + } |
|
528 | + return $global_message_template_group; |
|
529 | + } |
|
530 | + |
|
531 | + |
|
532 | + /** |
|
533 | + * Returns an array of event ids for all the events within the current data handler. |
|
534 | + * |
|
535 | + * @return array |
|
536 | + */ |
|
537 | + protected function _get_event_ids_from_current_data_handler(): array |
|
538 | + { |
|
539 | + $event_ids = []; |
|
540 | + foreach ($this->_current_data_handler->events as $event) { |
|
541 | + $event_ids[ $event['ID'] ] = $event['ID']; |
|
542 | + } |
|
543 | + return $event_ids; |
|
544 | + } |
|
545 | + |
|
546 | + |
|
547 | + /** |
|
548 | + * Retrieves formatted array of template information for each context specific to the given |
|
549 | + * EE_Message_Template_Group |
|
550 | + * |
|
551 | + * @param EE_Message_Template_Group $message_template_group |
|
552 | + * @return array The returned array is in this structure: |
|
553 | + * array( |
|
554 | + * 'field_name' => array( |
|
555 | + * 'context' => 'content' |
|
556 | + * ) |
|
557 | + * ) |
|
558 | + * @throws EE_Error |
|
559 | + * @throws InvalidArgumentException |
|
560 | + * @throws InvalidDataTypeException |
|
561 | + * @throws InvalidInterfaceException |
|
562 | + * @throws ReflectionException |
|
563 | + */ |
|
564 | + protected function _get_templates(EE_Message_Template_Group $message_template_group): array |
|
565 | + { |
|
566 | + $templates = []; |
|
567 | + $context_templates = $message_template_group->context_templates(); |
|
568 | + foreach ($context_templates as $context => $template_fields) { |
|
569 | + foreach ($template_fields as $template_field => $template_obj) { |
|
570 | + if (! $template_obj instanceof EE_Message_Template) { |
|
571 | + continue; |
|
572 | + } |
|
573 | + $templates[ $template_field ][ $context ] = $template_obj->get('MTP_content'); |
|
574 | + } |
|
575 | + } |
|
576 | + return $templates; |
|
577 | + } |
|
578 | + |
|
579 | + |
|
580 | + /** |
|
581 | + * Assembles new fully generated EE_Message objects and adds to _ready_queue |
|
582 | + * |
|
583 | + * @param array $addressees Array of EE_Messages_Addressee objects indexed by message type |
|
584 | + * context. |
|
585 | + * @param array $templates formatted array of templates used for parsing data. |
|
586 | + * @param EE_Message_Template_Group $message_template_group |
|
587 | + * @return bool true if message generation went a-ok. false if some sort of exception occurred. Note: The |
|
588 | + * method will attempt to generate ALL EE_Message objects and add to |
|
589 | + * the _ready_queue. Successfully generated messages get added to the |
|
590 | + * queue with EEM_Message::status_idle, unsuccessfully generated |
|
591 | + * messages will get added to the queue as EEM_Message::status_failed. |
|
592 | + * Very rarely should "false" be returned from this method. |
|
593 | + * @throws EE_Error |
|
594 | + * @throws InvalidArgumentException |
|
595 | + * @throws InvalidDataTypeException |
|
596 | + * @throws InvalidIdentifierException |
|
597 | + * @throws InvalidInterfaceException |
|
598 | + * @throws ReflectionException |
|
599 | + */ |
|
600 | + protected function _assemble_messages( |
|
601 | + array $addressees, |
|
602 | + array $templates, |
|
603 | + EE_Message_Template_Group $message_template_group |
|
604 | + ): bool { |
|
605 | + // if templates are empty then get out because we can't generate anything. |
|
606 | + if (! $templates) { |
|
607 | + $this->_error_msg[] = esc_html__( |
|
608 | + 'Unable to assemble messages because there are no templates retrieved for generating the messages with', |
|
609 | + 'event_espresso' |
|
610 | + ); |
|
611 | + return false; |
|
612 | + } |
|
613 | + |
|
614 | + // We use this as the counter for generated messages because don't forget we may be executing this inside of a |
|
615 | + // generation_queue. So _ready_queue may have generated EE_Message objects already. |
|
616 | + $generated_count = 0; |
|
617 | + foreach ($addressees as $context => $recipients) { |
|
618 | + foreach ($recipients as $recipient) { |
|
619 | + $message = $this->_setup_message_object($context, $recipient, $templates, $message_template_group); |
|
620 | + if ($message instanceof EE_Message) { |
|
621 | + $this->_ready_queue->add( |
|
622 | + $message, |
|
623 | + [], |
|
624 | + $this->_generation_queue->get_message_repository()->is_preview(), |
|
625 | + $this->_generation_queue->get_message_repository()->is_test_send() |
|
626 | + ); |
|
627 | + $generated_count++; |
|
628 | + } |
|
629 | + |
|
630 | + // if the current MSG being generated is for a test send then we'll only use ONE message in the |
|
631 | + // generation. |
|
632 | + if ($this->_generation_queue->get_message_repository()->is_test_send()) { |
|
633 | + break 2; |
|
634 | + } |
|
635 | + } |
|
636 | + } |
|
637 | + |
|
638 | + // if there are no generated messages then something else fatal went wrong. |
|
639 | + return $generated_count > 0; |
|
640 | + } |
|
641 | + |
|
642 | + |
|
643 | + /** |
|
644 | + * @param string $context The context for the generated message. |
|
645 | + * @param EE_Messages_Addressee $recipient |
|
646 | + * @param array $templates formatted array of templates used for parsing data. |
|
647 | + * @param EE_Message_Template_Group $message_template_group |
|
648 | + * @return bool|EE_Message |
|
649 | + * @throws EE_Error |
|
650 | + * @throws InvalidArgumentException |
|
651 | + * @throws InvalidDataTypeException |
|
652 | + * @throws InvalidInterfaceException |
|
653 | + * @throws ReflectionException |
|
654 | + * @throws InvalidIdentifierException |
|
655 | + */ |
|
656 | + protected function _setup_message_object( |
|
657 | + string $context, |
|
658 | + EE_Messages_Addressee $recipient, |
|
659 | + array $templates, |
|
660 | + EE_Message_Template_Group $message_template_group |
|
661 | + ) { |
|
662 | + // stuff we already know |
|
663 | + $transaction_id = $recipient->txn instanceof EE_Transaction |
|
664 | + ? $recipient->txn->ID() |
|
665 | + : 0; |
|
666 | + $transaction_id = empty($transaction_id) && $this->_current_data_handler->txn instanceof EE_Transaction |
|
667 | + ? $this->_current_data_handler->txn->ID() |
|
668 | + : $transaction_id; |
|
669 | + $message_fields = [ |
|
670 | + 'GRP_ID' => $message_template_group->ID(), |
|
671 | + 'TXN_ID' => $transaction_id, |
|
672 | + 'MSG_messenger' => $this->_current_messenger->name, |
|
673 | + 'MSG_message_type' => $this->_current_message_type->name, |
|
674 | + 'MSG_context' => $context, |
|
675 | + ]; |
|
676 | + |
|
677 | + // recipient id and type should be on the EE_Messages_Addressee object but if this is empty, let's try to grab |
|
678 | + // the info from the att_obj found in the EE_Messages_Addressee object. |
|
679 | + if (empty($recipient->recipient_id) || empty($recipient->recipient_type)) { |
|
680 | + $message_fields['MSG_recipient_ID'] = $recipient->att_obj instanceof EE_Attendee |
|
681 | + ? $recipient->att_obj->ID() |
|
682 | + : 0; |
|
683 | + $message_fields['MSG_recipient_type'] = 'Attendee'; |
|
684 | + } else { |
|
685 | + $message_fields['MSG_recipient_ID'] = $recipient->recipient_id; |
|
686 | + $message_fields['MSG_recipient_type'] = $recipient->recipient_type; |
|
687 | + } |
|
688 | + $message = EE_Message_Factory::create($message_fields); |
|
689 | + |
|
690 | + // grab valid shortcodes for shortcode parser |
|
691 | + $mt_shortcodes = $this->_current_message_type->get_valid_shortcodes(); |
|
692 | + $m_shortcodes = $this->_current_messenger->get_valid_shortcodes(); |
|
693 | + |
|
694 | + // if the 'to' field is empty or the context is inactive we skip EXCEPT if we're previewing |
|
695 | + if ( |
|
696 | + ! $this->_generation_queue->get_message_repository()->is_preview() |
|
697 | + && ( |
|
698 | + (empty($templates['to'][ $context ]) && ! $this->_current_messenger->allow_empty_to_field()) |
|
699 | + || ! $message_template_group->is_context_active($context) |
|
700 | + ) |
|
701 | + ) { |
|
702 | + // we silently exit here and do NOT record a fail because the message is "turned off" by having no "to" |
|
703 | + // field. |
|
704 | + return false; |
|
705 | + } |
|
706 | + $error_msg = []; |
|
707 | + foreach ($templates as $field => $field_context) { |
|
708 | + $error_msg = []; |
|
709 | + // let's setup the valid shortcodes for the incoming context. |
|
710 | + $valid_shortcodes = $mt_shortcodes[ $context ]; |
|
711 | + // merge in valid shortcodes for the field. |
|
712 | + $shortcodes = $m_shortcodes[ $field ] ?? $valid_shortcodes; |
|
713 | + if (isset($field_context[ $context ])) { |
|
714 | + // prefix field. |
|
715 | + $column_name = 'MSG_' . $field; |
|
716 | + try { |
|
717 | + $content = $this->_shortcode_parser->parse_message_template( |
|
718 | + $field_context[ $context ], |
|
719 | + $recipient, |
|
720 | + $shortcodes, |
|
721 | + $this->_current_message_type, |
|
722 | + $this->_current_messenger, |
|
723 | + $message |
|
724 | + ); |
|
725 | + // the model field removes slashes when setting (usually necessary when the input is from the |
|
726 | + // request) but this value is from another model and has no slashes. So add them so it matches |
|
727 | + // what the field expected (otherwise slashes will have been stripped from this an extra time) |
|
728 | + $message->set_field_or_extra_meta($column_name, addslashes($content)); |
|
729 | + } catch (EE_Error $e) { |
|
730 | + $error_msg[] = sprintf( |
|
731 | + /* Translators: First place holder is message model field name. |
|
732 | 732 | * Second placeholder is exception error message */ |
733 | - esc_html__( |
|
734 | - 'There was a problem generating the content for the field %s: %s', |
|
735 | - 'event_espresso' |
|
736 | - ), |
|
737 | - $field, |
|
738 | - $e->getMessage() |
|
739 | - ); |
|
740 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
741 | - } |
|
742 | - } |
|
743 | - } |
|
744 | - |
|
745 | - if ($message->STS_ID() === EEM_Message::status_failed) { |
|
746 | - $error_msg = esc_html__('There were problems generating this message:', 'event_espresso') |
|
747 | - . "\n" |
|
748 | - . implode("\n", $error_msg); |
|
749 | - $message->set_error_message($error_msg); |
|
750 | - } else { |
|
751 | - $message->set_STS_ID(EEM_Message::status_idle); |
|
752 | - } |
|
753 | - return $message; |
|
754 | - } |
|
755 | - |
|
756 | - |
|
757 | - /** |
|
758 | - * This verifies that the incoming array has a EE_messenger object and a EE_message_type object and sets appropriate |
|
759 | - * error message if either is missing. |
|
760 | - * |
|
761 | - * @return bool true means there were no errors, false means there were errors. |
|
762 | - */ |
|
763 | - protected function _verify(): bool |
|
764 | - { |
|
765 | - // reset error message to an empty array. |
|
766 | - $this->_error_msg = []; |
|
767 | - // set the verified flag so we know everything has been validated. |
|
768 | - $this->_verified = $this->_validate_messenger_and_message_type() && $this->_validate_and_setup_data(); |
|
769 | - return $this->_verified; |
|
770 | - } |
|
771 | - |
|
772 | - |
|
773 | - /** |
|
774 | - * This accepts an array and validates that it is an array indexed by context with each value being an array of |
|
775 | - * EE_Messages_Addressee objects. |
|
776 | - * |
|
777 | - * @param array|null $addressees Keys correspond to contexts for the message type and values are |
|
778 | - * EE_Messages_Addressee[] |
|
779 | - * @return bool |
|
780 | - */ |
|
781 | - protected function _valid_addressees(?array $addressees): bool |
|
782 | - { |
|
783 | - if (! $addressees || ! is_array($addressees)) { |
|
784 | - return false; |
|
785 | - } |
|
786 | - |
|
787 | - foreach ($addressees as $addressee_array) { |
|
788 | - foreach ($addressee_array as $addressee) { |
|
789 | - if (! $addressee instanceof EE_Messages_Addressee) { |
|
790 | - return false; |
|
791 | - } |
|
792 | - } |
|
793 | - } |
|
794 | - return true; |
|
795 | - } |
|
796 | - |
|
797 | - |
|
798 | - /** |
|
799 | - * This validates the messenger, message type, and presences of generation data for the current EE_Message in the |
|
800 | - * queue. This process sets error messages if something is wrong. |
|
801 | - * |
|
802 | - * @return bool true is if there are no errors. false is if there is. |
|
803 | - */ |
|
804 | - protected function _validate_messenger_and_message_type(): bool |
|
805 | - { |
|
806 | - // first are there any existing error messages? If so then return. |
|
807 | - if ($this->_error_msg) { |
|
808 | - return false; |
|
809 | - } |
|
810 | - $message = $this->_generation_queue->get_message_repository()->current(); |
|
811 | - try { |
|
812 | - $this->_current_messenger = $message->valid_messenger(true) |
|
813 | - ? $message->messenger_object() |
|
814 | - : null; |
|
815 | - } catch (Exception $e) { |
|
816 | - $this->_error_msg[] = $e->getMessage(); |
|
817 | - } |
|
818 | - try { |
|
819 | - $this->_current_message_type = $message->valid_message_type(true) |
|
820 | - ? $message->message_type_object() |
|
821 | - : null; |
|
822 | - } catch (Exception $e) { |
|
823 | - $this->_error_msg[] = $e->getMessage(); |
|
824 | - } |
|
825 | - |
|
826 | - /** |
|
827 | - * Check if there is any generation data, but only if this is not for a preview. |
|
828 | - */ |
|
829 | - if ( |
|
830 | - ! $this->_generation_queue->get_message_repository()->get_generation_data() |
|
831 | - && ( |
|
832 | - ! $this->_generation_queue->get_message_repository()->is_preview() |
|
833 | - && $this->_generation_queue->get_message_repository()->get_data_handler() |
|
834 | - !== 'EE_Messages_Preview_incoming_data' |
|
835 | - ) |
|
836 | - ) { |
|
837 | - $this->_error_msg[] = esc_html__( |
|
838 | - 'There is no generation data for this message. Unable to generate.', |
|
839 | - 'event_espresso' |
|
840 | - ); |
|
841 | - } |
|
842 | - |
|
843 | - return empty($this->_error_msg); |
|
844 | - } |
|
845 | - |
|
846 | - |
|
847 | - /** |
|
848 | - * This method retrieves the expected data handler for the message type and validates the generation data for that |
|
849 | - * data handler. |
|
850 | - * |
|
851 | - * @return bool true means there are no errors. false means there were errors (and handler did not get setup). |
|
852 | - */ |
|
853 | - protected function _validate_and_setup_data(): bool |
|
854 | - { |
|
855 | - // First, are there any existing error messages? If so, return because if there were errors elsewhere this |
|
856 | - // can't be used anyways. |
|
857 | - if ($this->_error_msg) { |
|
858 | - return false; |
|
859 | - } |
|
860 | - |
|
861 | - $generation_data = $this->_generation_queue->get_message_repository()->get_generation_data(); |
|
862 | - |
|
863 | - /** @type EE_Messages_incoming_data $data_handler_class_name - well not really... just the class name actually */ |
|
864 | - $data_handler_class_name = $this->_generation_queue->get_message_repository()->get_data_handler() |
|
865 | - ? $this->_generation_queue->get_message_repository()->get_data_handler() |
|
866 | - : 'EE_Messages_' . $this->_current_message_type->get_data_handler($generation_data) . '_incoming_data'; |
|
867 | - |
|
868 | - // If this EE_Message is for a preview, then let's switch out to the preview data handler. |
|
869 | - if ($this->_generation_queue->get_message_repository()->is_preview()) { |
|
870 | - $data_handler_class_name = 'EE_Messages_Preview_incoming_data'; |
|
871 | - } |
|
872 | - |
|
873 | - // First get the class name for the data handler (and also verifies it exists. |
|
874 | - if (! class_exists($data_handler_class_name)) { |
|
875 | - $this->_error_msg[] = sprintf( |
|
876 | - /* Translators: Both placeholders are the names of php classes. */ |
|
877 | - esc_html__( |
|
878 | - 'The included data handler class name does not match any valid, accessible, "%1$s" classes. Looking for %2$s.', |
|
879 | - 'event_espresso' |
|
880 | - ), |
|
881 | - 'EE_Messages_incoming_data', |
|
882 | - $data_handler_class_name |
|
883 | - ); |
|
884 | - return false; |
|
885 | - } |
|
886 | - |
|
887 | - // convert generation_data for data_handler_instantiation. |
|
888 | - $generation_data = $data_handler_class_name::convert_data_from_persistent_storage($generation_data); |
|
889 | - |
|
890 | - // note, this may set error messages as well. |
|
891 | - $this->_set_data_handler($generation_data, $data_handler_class_name); |
|
892 | - |
|
893 | - return empty($this->_error_msg); |
|
894 | - } |
|
895 | - |
|
896 | - |
|
897 | - /** |
|
898 | - * Sets the $_current_data_handler property that is used for generating the current EE_Message in the queue, and |
|
899 | - * adds it to the _data repository. |
|
900 | - * |
|
901 | - * @param mixed $generating_data This is data expected by the instantiated data handler. |
|
902 | - * @param string $data_handler_class_name This is the reference string indicating what data handler is being |
|
903 | - * instantiated. |
|
904 | - */ |
|
905 | - protected function _set_data_handler($generating_data, string $data_handler_class_name) |
|
906 | - { |
|
907 | - // valid classname for the data handler. Now let's setup the key for the data handler repository to see if |
|
908 | - // there is already a ready data handler in the repository. |
|
909 | - $this->_current_data_handler = $this->_data_handler_collection->get_by_key( |
|
910 | - $this->_data_handler_collection->get_key( |
|
911 | - $data_handler_class_name, |
|
912 | - $generating_data |
|
913 | - ) |
|
914 | - ); |
|
915 | - if (! $this->_current_data_handler instanceof EE_Messages_incoming_data) { |
|
916 | - // no saved data_handler in the repo so let's set one up and add it to the repo. |
|
917 | - try { |
|
918 | - $this->_current_data_handler = new $data_handler_class_name($generating_data); |
|
919 | - $this->_data_handler_collection->add($this->_current_data_handler, $generating_data); |
|
920 | - } catch (Exception $e) { |
|
921 | - $this->_error_msg[] = $e->getMessage(); |
|
922 | - } |
|
923 | - } |
|
924 | - } |
|
925 | - |
|
926 | - |
|
927 | - /** |
|
928 | - * The queued EE_Message for generation does not save the data used for generation as objects |
|
929 | - * because serialization of those objects could be problematic if the data is saved to the db. |
|
930 | - * So this method calls the static method on the associated data_handler for the given message_type |
|
931 | - * and that preps the data for later instantiation when generating. |
|
932 | - * |
|
933 | - * @param EE_Message_To_Generate $message_to_generate |
|
934 | - * @param bool $preview Indicate whether this is being used for a preview or not. |
|
935 | - * @return mixed Prepped data for persisting to the queue. false is returned if unable to prep data. |
|
936 | - */ |
|
937 | - protected function _prepare_data_for_queue(EE_Message_To_Generate $message_to_generate, bool $preview) |
|
938 | - { |
|
939 | - if (! $message_to_generate->valid()) { |
|
940 | - return false; // unable to get the data because the info in the EE_Message_To_Generate class is invalid. |
|
941 | - } |
|
942 | - /** @type EE_Messages_incoming_data $data_handler - well not really... just the class name actually */ |
|
943 | - $data_handler = $message_to_generate->get_data_handler_class_name($preview); |
|
944 | - return $data_handler::convert_data_for_persistent_storage($message_to_generate->data()); |
|
945 | - } |
|
946 | - |
|
947 | - |
|
948 | - /** |
|
949 | - * This sets up a EEM_Message::status_incomplete EE_Message object and adds it to the generation queue. |
|
950 | - * |
|
951 | - * @param EE_Message_To_Generate $message_to_generate |
|
952 | - * @param bool $test_send Whether this is just a test send or not. Typically used for previews. |
|
953 | - * @return bool |
|
954 | - * @throws InvalidArgumentException |
|
955 | - * @throws InvalidDataTypeException |
|
956 | - * @throws InvalidInterfaceException |
|
957 | - */ |
|
958 | - public function create_and_add_message_to_queue( |
|
959 | - EE_Message_To_Generate $message_to_generate, |
|
960 | - bool $test_send = false |
|
961 | - ): bool { |
|
962 | - // prep data |
|
963 | - $data = $this->_prepare_data_for_queue($message_to_generate, $message_to_generate->preview()); |
|
964 | - $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
965 | - |
|
966 | - $message = $message_to_generate->get_EE_Message(); |
|
967 | - $GRP_ID = $request->getRequestParam('GRP_ID', 0, 'int'); |
|
968 | - |
|
969 | - $GRP_ID = apply_filters( |
|
970 | - 'FHEE__EE_Messages_Generator__create_and_add_message_to_queue_GRP_ID', |
|
971 | - $GRP_ID > 0 |
|
972 | - ? $GRP_ID |
|
973 | - : $message->GRP_ID(), |
|
974 | - $message, |
|
975 | - $message_to_generate, |
|
976 | - $test_send |
|
977 | - ); |
|
978 | - |
|
979 | - if ($GRP_ID > 0) { |
|
980 | - $message->set_GRP_ID($GRP_ID); |
|
981 | - } |
|
982 | - |
|
983 | - if ($data === false) { |
|
984 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
985 | - $message->set_error_message( |
|
986 | - esc_html__( |
|
987 | - 'Unable to prepare data for persistence to the database.', |
|
988 | - 'event_espresso' |
|
989 | - ) |
|
990 | - ); |
|
991 | - } else { |
|
992 | - // make sure that the data handler is cached on the message as well |
|
993 | - $data['data_handler_class_name'] = $message_to_generate->get_data_handler_class_name(); |
|
994 | - } |
|
995 | - |
|
996 | - return $this->_generation_queue->add($message, $data, $message_to_generate->preview(), $test_send); |
|
997 | - } |
|
733 | + esc_html__( |
|
734 | + 'There was a problem generating the content for the field %s: %s', |
|
735 | + 'event_espresso' |
|
736 | + ), |
|
737 | + $field, |
|
738 | + $e->getMessage() |
|
739 | + ); |
|
740 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
741 | + } |
|
742 | + } |
|
743 | + } |
|
744 | + |
|
745 | + if ($message->STS_ID() === EEM_Message::status_failed) { |
|
746 | + $error_msg = esc_html__('There were problems generating this message:', 'event_espresso') |
|
747 | + . "\n" |
|
748 | + . implode("\n", $error_msg); |
|
749 | + $message->set_error_message($error_msg); |
|
750 | + } else { |
|
751 | + $message->set_STS_ID(EEM_Message::status_idle); |
|
752 | + } |
|
753 | + return $message; |
|
754 | + } |
|
755 | + |
|
756 | + |
|
757 | + /** |
|
758 | + * This verifies that the incoming array has a EE_messenger object and a EE_message_type object and sets appropriate |
|
759 | + * error message if either is missing. |
|
760 | + * |
|
761 | + * @return bool true means there were no errors, false means there were errors. |
|
762 | + */ |
|
763 | + protected function _verify(): bool |
|
764 | + { |
|
765 | + // reset error message to an empty array. |
|
766 | + $this->_error_msg = []; |
|
767 | + // set the verified flag so we know everything has been validated. |
|
768 | + $this->_verified = $this->_validate_messenger_and_message_type() && $this->_validate_and_setup_data(); |
|
769 | + return $this->_verified; |
|
770 | + } |
|
771 | + |
|
772 | + |
|
773 | + /** |
|
774 | + * This accepts an array and validates that it is an array indexed by context with each value being an array of |
|
775 | + * EE_Messages_Addressee objects. |
|
776 | + * |
|
777 | + * @param array|null $addressees Keys correspond to contexts for the message type and values are |
|
778 | + * EE_Messages_Addressee[] |
|
779 | + * @return bool |
|
780 | + */ |
|
781 | + protected function _valid_addressees(?array $addressees): bool |
|
782 | + { |
|
783 | + if (! $addressees || ! is_array($addressees)) { |
|
784 | + return false; |
|
785 | + } |
|
786 | + |
|
787 | + foreach ($addressees as $addressee_array) { |
|
788 | + foreach ($addressee_array as $addressee) { |
|
789 | + if (! $addressee instanceof EE_Messages_Addressee) { |
|
790 | + return false; |
|
791 | + } |
|
792 | + } |
|
793 | + } |
|
794 | + return true; |
|
795 | + } |
|
796 | + |
|
797 | + |
|
798 | + /** |
|
799 | + * This validates the messenger, message type, and presences of generation data for the current EE_Message in the |
|
800 | + * queue. This process sets error messages if something is wrong. |
|
801 | + * |
|
802 | + * @return bool true is if there are no errors. false is if there is. |
|
803 | + */ |
|
804 | + protected function _validate_messenger_and_message_type(): bool |
|
805 | + { |
|
806 | + // first are there any existing error messages? If so then return. |
|
807 | + if ($this->_error_msg) { |
|
808 | + return false; |
|
809 | + } |
|
810 | + $message = $this->_generation_queue->get_message_repository()->current(); |
|
811 | + try { |
|
812 | + $this->_current_messenger = $message->valid_messenger(true) |
|
813 | + ? $message->messenger_object() |
|
814 | + : null; |
|
815 | + } catch (Exception $e) { |
|
816 | + $this->_error_msg[] = $e->getMessage(); |
|
817 | + } |
|
818 | + try { |
|
819 | + $this->_current_message_type = $message->valid_message_type(true) |
|
820 | + ? $message->message_type_object() |
|
821 | + : null; |
|
822 | + } catch (Exception $e) { |
|
823 | + $this->_error_msg[] = $e->getMessage(); |
|
824 | + } |
|
825 | + |
|
826 | + /** |
|
827 | + * Check if there is any generation data, but only if this is not for a preview. |
|
828 | + */ |
|
829 | + if ( |
|
830 | + ! $this->_generation_queue->get_message_repository()->get_generation_data() |
|
831 | + && ( |
|
832 | + ! $this->_generation_queue->get_message_repository()->is_preview() |
|
833 | + && $this->_generation_queue->get_message_repository()->get_data_handler() |
|
834 | + !== 'EE_Messages_Preview_incoming_data' |
|
835 | + ) |
|
836 | + ) { |
|
837 | + $this->_error_msg[] = esc_html__( |
|
838 | + 'There is no generation data for this message. Unable to generate.', |
|
839 | + 'event_espresso' |
|
840 | + ); |
|
841 | + } |
|
842 | + |
|
843 | + return empty($this->_error_msg); |
|
844 | + } |
|
845 | + |
|
846 | + |
|
847 | + /** |
|
848 | + * This method retrieves the expected data handler for the message type and validates the generation data for that |
|
849 | + * data handler. |
|
850 | + * |
|
851 | + * @return bool true means there are no errors. false means there were errors (and handler did not get setup). |
|
852 | + */ |
|
853 | + protected function _validate_and_setup_data(): bool |
|
854 | + { |
|
855 | + // First, are there any existing error messages? If so, return because if there were errors elsewhere this |
|
856 | + // can't be used anyways. |
|
857 | + if ($this->_error_msg) { |
|
858 | + return false; |
|
859 | + } |
|
860 | + |
|
861 | + $generation_data = $this->_generation_queue->get_message_repository()->get_generation_data(); |
|
862 | + |
|
863 | + /** @type EE_Messages_incoming_data $data_handler_class_name - well not really... just the class name actually */ |
|
864 | + $data_handler_class_name = $this->_generation_queue->get_message_repository()->get_data_handler() |
|
865 | + ? $this->_generation_queue->get_message_repository()->get_data_handler() |
|
866 | + : 'EE_Messages_' . $this->_current_message_type->get_data_handler($generation_data) . '_incoming_data'; |
|
867 | + |
|
868 | + // If this EE_Message is for a preview, then let's switch out to the preview data handler. |
|
869 | + if ($this->_generation_queue->get_message_repository()->is_preview()) { |
|
870 | + $data_handler_class_name = 'EE_Messages_Preview_incoming_data'; |
|
871 | + } |
|
872 | + |
|
873 | + // First get the class name for the data handler (and also verifies it exists. |
|
874 | + if (! class_exists($data_handler_class_name)) { |
|
875 | + $this->_error_msg[] = sprintf( |
|
876 | + /* Translators: Both placeholders are the names of php classes. */ |
|
877 | + esc_html__( |
|
878 | + 'The included data handler class name does not match any valid, accessible, "%1$s" classes. Looking for %2$s.', |
|
879 | + 'event_espresso' |
|
880 | + ), |
|
881 | + 'EE_Messages_incoming_data', |
|
882 | + $data_handler_class_name |
|
883 | + ); |
|
884 | + return false; |
|
885 | + } |
|
886 | + |
|
887 | + // convert generation_data for data_handler_instantiation. |
|
888 | + $generation_data = $data_handler_class_name::convert_data_from_persistent_storage($generation_data); |
|
889 | + |
|
890 | + // note, this may set error messages as well. |
|
891 | + $this->_set_data_handler($generation_data, $data_handler_class_name); |
|
892 | + |
|
893 | + return empty($this->_error_msg); |
|
894 | + } |
|
895 | + |
|
896 | + |
|
897 | + /** |
|
898 | + * Sets the $_current_data_handler property that is used for generating the current EE_Message in the queue, and |
|
899 | + * adds it to the _data repository. |
|
900 | + * |
|
901 | + * @param mixed $generating_data This is data expected by the instantiated data handler. |
|
902 | + * @param string $data_handler_class_name This is the reference string indicating what data handler is being |
|
903 | + * instantiated. |
|
904 | + */ |
|
905 | + protected function _set_data_handler($generating_data, string $data_handler_class_name) |
|
906 | + { |
|
907 | + // valid classname for the data handler. Now let's setup the key for the data handler repository to see if |
|
908 | + // there is already a ready data handler in the repository. |
|
909 | + $this->_current_data_handler = $this->_data_handler_collection->get_by_key( |
|
910 | + $this->_data_handler_collection->get_key( |
|
911 | + $data_handler_class_name, |
|
912 | + $generating_data |
|
913 | + ) |
|
914 | + ); |
|
915 | + if (! $this->_current_data_handler instanceof EE_Messages_incoming_data) { |
|
916 | + // no saved data_handler in the repo so let's set one up and add it to the repo. |
|
917 | + try { |
|
918 | + $this->_current_data_handler = new $data_handler_class_name($generating_data); |
|
919 | + $this->_data_handler_collection->add($this->_current_data_handler, $generating_data); |
|
920 | + } catch (Exception $e) { |
|
921 | + $this->_error_msg[] = $e->getMessage(); |
|
922 | + } |
|
923 | + } |
|
924 | + } |
|
925 | + |
|
926 | + |
|
927 | + /** |
|
928 | + * The queued EE_Message for generation does not save the data used for generation as objects |
|
929 | + * because serialization of those objects could be problematic if the data is saved to the db. |
|
930 | + * So this method calls the static method on the associated data_handler for the given message_type |
|
931 | + * and that preps the data for later instantiation when generating. |
|
932 | + * |
|
933 | + * @param EE_Message_To_Generate $message_to_generate |
|
934 | + * @param bool $preview Indicate whether this is being used for a preview or not. |
|
935 | + * @return mixed Prepped data for persisting to the queue. false is returned if unable to prep data. |
|
936 | + */ |
|
937 | + protected function _prepare_data_for_queue(EE_Message_To_Generate $message_to_generate, bool $preview) |
|
938 | + { |
|
939 | + if (! $message_to_generate->valid()) { |
|
940 | + return false; // unable to get the data because the info in the EE_Message_To_Generate class is invalid. |
|
941 | + } |
|
942 | + /** @type EE_Messages_incoming_data $data_handler - well not really... just the class name actually */ |
|
943 | + $data_handler = $message_to_generate->get_data_handler_class_name($preview); |
|
944 | + return $data_handler::convert_data_for_persistent_storage($message_to_generate->data()); |
|
945 | + } |
|
946 | + |
|
947 | + |
|
948 | + /** |
|
949 | + * This sets up a EEM_Message::status_incomplete EE_Message object and adds it to the generation queue. |
|
950 | + * |
|
951 | + * @param EE_Message_To_Generate $message_to_generate |
|
952 | + * @param bool $test_send Whether this is just a test send or not. Typically used for previews. |
|
953 | + * @return bool |
|
954 | + * @throws InvalidArgumentException |
|
955 | + * @throws InvalidDataTypeException |
|
956 | + * @throws InvalidInterfaceException |
|
957 | + */ |
|
958 | + public function create_and_add_message_to_queue( |
|
959 | + EE_Message_To_Generate $message_to_generate, |
|
960 | + bool $test_send = false |
|
961 | + ): bool { |
|
962 | + // prep data |
|
963 | + $data = $this->_prepare_data_for_queue($message_to_generate, $message_to_generate->preview()); |
|
964 | + $request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
965 | + |
|
966 | + $message = $message_to_generate->get_EE_Message(); |
|
967 | + $GRP_ID = $request->getRequestParam('GRP_ID', 0, 'int'); |
|
968 | + |
|
969 | + $GRP_ID = apply_filters( |
|
970 | + 'FHEE__EE_Messages_Generator__create_and_add_message_to_queue_GRP_ID', |
|
971 | + $GRP_ID > 0 |
|
972 | + ? $GRP_ID |
|
973 | + : $message->GRP_ID(), |
|
974 | + $message, |
|
975 | + $message_to_generate, |
|
976 | + $test_send |
|
977 | + ); |
|
978 | + |
|
979 | + if ($GRP_ID > 0) { |
|
980 | + $message->set_GRP_ID($GRP_ID); |
|
981 | + } |
|
982 | + |
|
983 | + if ($data === false) { |
|
984 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
985 | + $message->set_error_message( |
|
986 | + esc_html__( |
|
987 | + 'Unable to prepare data for persistence to the database.', |
|
988 | + 'event_espresso' |
|
989 | + ) |
|
990 | + ); |
|
991 | + } else { |
|
992 | + // make sure that the data handler is cached on the message as well |
|
993 | + $data['data_handler_class_name'] = $message_to_generate->get_data_handler_class_name(); |
|
994 | + } |
|
995 | + |
|
996 | + return $this->_generation_queue->add($message, $data, $message_to_generate->preview(), $test_send); |
|
997 | + } |
|
998 | 998 | } |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | { |
246 | 246 | // double check verification has run and that everything is ready to work with (saves us having to validate |
247 | 247 | // everything again). |
248 | - if (! $this->_verified) { |
|
248 | + if ( ! $this->_verified) { |
|
249 | 249 | return false; // get out because we don't have a valid setup to work with. |
250 | 250 | } |
251 | 251 | |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | |
263 | 263 | |
264 | 264 | // if no addressees then get out because there is nothing to generation (possible bad data). |
265 | - if (! $this->_valid_addressees($addressees)) { |
|
265 | + if ( ! $this->_valid_addressees($addressees)) { |
|
266 | 266 | do_action( |
267 | 267 | 'AHEE__EE_Messages_Generator___generate__invalid_addressees', |
268 | 268 | $this->_generation_queue->get_message_repository()->current(), |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | $message_template_group = $this->_get_message_template_group(); |
285 | 285 | |
286 | 286 | // in the unlikely event there is no EE_Message_Template_Group available, get out! |
287 | - if (! $message_template_group instanceof EE_Message_Template_Group) { |
|
287 | + if ( ! $message_template_group instanceof EE_Message_Template_Group) { |
|
288 | 288 | $this->_error_msg[] = esc_html__( |
289 | 289 | 'Unable to get the Message Templates for the Message being generated. No message template group accessible.', |
290 | 290 | 'event_espresso' |
@@ -397,7 +397,7 @@ discard block |
||
397 | 397 | // attempt to retrieve from repo first |
398 | 398 | $message_template_group = $this->_template_collection->get_by_ID($GRP_ID); |
399 | 399 | if ($message_template_group instanceof EE_Message_Template_Group) { |
400 | - return $message_template_group; // got it! |
|
400 | + return $message_template_group; // got it! |
|
401 | 401 | } |
402 | 402 | |
403 | 403 | // nope don't have it yet. Get from DB then add to repo if its not here, then that means the current GRP_ID |
@@ -427,7 +427,7 @@ discard block |
||
427 | 427 | protected function _queue_shares_same_message_template_group_for_events(array $event_ids): bool |
428 | 428 | { |
429 | 429 | foreach ($this->_current_data_handler->events as $event) { |
430 | - $event_ids[ $event['ID'] ] = $event['ID']; |
|
430 | + $event_ids[$event['ID']] = $event['ID']; |
|
431 | 431 | } |
432 | 432 | $count_of_message_template_groups = EEM_Message_Template_Group::instance()->count( |
433 | 433 | [ |
@@ -506,7 +506,7 @@ discard block |
||
506 | 506 | ); |
507 | 507 | |
508 | 508 | // if we don't have a group lets hit the db. |
509 | - if (! $global_message_template_group instanceof EE_Message_Template_Group) { |
|
509 | + if ( ! $global_message_template_group instanceof EE_Message_Template_Group) { |
|
510 | 510 | $global_message_template_group = EEM_Message_Template_Group::instance()->get_one( |
511 | 511 | [ |
512 | 512 | [ |
@@ -538,7 +538,7 @@ discard block |
||
538 | 538 | { |
539 | 539 | $event_ids = []; |
540 | 540 | foreach ($this->_current_data_handler->events as $event) { |
541 | - $event_ids[ $event['ID'] ] = $event['ID']; |
|
541 | + $event_ids[$event['ID']] = $event['ID']; |
|
542 | 542 | } |
543 | 543 | return $event_ids; |
544 | 544 | } |
@@ -567,10 +567,10 @@ discard block |
||
567 | 567 | $context_templates = $message_template_group->context_templates(); |
568 | 568 | foreach ($context_templates as $context => $template_fields) { |
569 | 569 | foreach ($template_fields as $template_field => $template_obj) { |
570 | - if (! $template_obj instanceof EE_Message_Template) { |
|
570 | + if ( ! $template_obj instanceof EE_Message_Template) { |
|
571 | 571 | continue; |
572 | 572 | } |
573 | - $templates[ $template_field ][ $context ] = $template_obj->get('MTP_content'); |
|
573 | + $templates[$template_field][$context] = $template_obj->get('MTP_content'); |
|
574 | 574 | } |
575 | 575 | } |
576 | 576 | return $templates; |
@@ -603,7 +603,7 @@ discard block |
||
603 | 603 | EE_Message_Template_Group $message_template_group |
604 | 604 | ): bool { |
605 | 605 | // if templates are empty then get out because we can't generate anything. |
606 | - if (! $templates) { |
|
606 | + if ( ! $templates) { |
|
607 | 607 | $this->_error_msg[] = esc_html__( |
608 | 608 | 'Unable to assemble messages because there are no templates retrieved for generating the messages with', |
609 | 609 | 'event_espresso' |
@@ -695,7 +695,7 @@ discard block |
||
695 | 695 | if ( |
696 | 696 | ! $this->_generation_queue->get_message_repository()->is_preview() |
697 | 697 | && ( |
698 | - (empty($templates['to'][ $context ]) && ! $this->_current_messenger->allow_empty_to_field()) |
|
698 | + (empty($templates['to'][$context]) && ! $this->_current_messenger->allow_empty_to_field()) |
|
699 | 699 | || ! $message_template_group->is_context_active($context) |
700 | 700 | ) |
701 | 701 | ) { |
@@ -707,15 +707,15 @@ discard block |
||
707 | 707 | foreach ($templates as $field => $field_context) { |
708 | 708 | $error_msg = []; |
709 | 709 | // let's setup the valid shortcodes for the incoming context. |
710 | - $valid_shortcodes = $mt_shortcodes[ $context ]; |
|
710 | + $valid_shortcodes = $mt_shortcodes[$context]; |
|
711 | 711 | // merge in valid shortcodes for the field. |
712 | - $shortcodes = $m_shortcodes[ $field ] ?? $valid_shortcodes; |
|
713 | - if (isset($field_context[ $context ])) { |
|
712 | + $shortcodes = $m_shortcodes[$field] ?? $valid_shortcodes; |
|
713 | + if (isset($field_context[$context])) { |
|
714 | 714 | // prefix field. |
715 | - $column_name = 'MSG_' . $field; |
|
715 | + $column_name = 'MSG_'.$field; |
|
716 | 716 | try { |
717 | 717 | $content = $this->_shortcode_parser->parse_message_template( |
718 | - $field_context[ $context ], |
|
718 | + $field_context[$context], |
|
719 | 719 | $recipient, |
720 | 720 | $shortcodes, |
721 | 721 | $this->_current_message_type, |
@@ -780,13 +780,13 @@ discard block |
||
780 | 780 | */ |
781 | 781 | protected function _valid_addressees(?array $addressees): bool |
782 | 782 | { |
783 | - if (! $addressees || ! is_array($addressees)) { |
|
783 | + if ( ! $addressees || ! is_array($addressees)) { |
|
784 | 784 | return false; |
785 | 785 | } |
786 | 786 | |
787 | 787 | foreach ($addressees as $addressee_array) { |
788 | 788 | foreach ($addressee_array as $addressee) { |
789 | - if (! $addressee instanceof EE_Messages_Addressee) { |
|
789 | + if ( ! $addressee instanceof EE_Messages_Addressee) { |
|
790 | 790 | return false; |
791 | 791 | } |
792 | 792 | } |
@@ -863,7 +863,7 @@ discard block |
||
863 | 863 | /** @type EE_Messages_incoming_data $data_handler_class_name - well not really... just the class name actually */ |
864 | 864 | $data_handler_class_name = $this->_generation_queue->get_message_repository()->get_data_handler() |
865 | 865 | ? $this->_generation_queue->get_message_repository()->get_data_handler() |
866 | - : 'EE_Messages_' . $this->_current_message_type->get_data_handler($generation_data) . '_incoming_data'; |
|
866 | + : 'EE_Messages_'.$this->_current_message_type->get_data_handler($generation_data).'_incoming_data'; |
|
867 | 867 | |
868 | 868 | // If this EE_Message is for a preview, then let's switch out to the preview data handler. |
869 | 869 | if ($this->_generation_queue->get_message_repository()->is_preview()) { |
@@ -871,7 +871,7 @@ discard block |
||
871 | 871 | } |
872 | 872 | |
873 | 873 | // First get the class name for the data handler (and also verifies it exists. |
874 | - if (! class_exists($data_handler_class_name)) { |
|
874 | + if ( ! class_exists($data_handler_class_name)) { |
|
875 | 875 | $this->_error_msg[] = sprintf( |
876 | 876 | /* Translators: Both placeholders are the names of php classes. */ |
877 | 877 | esc_html__( |
@@ -912,7 +912,7 @@ discard block |
||
912 | 912 | $generating_data |
913 | 913 | ) |
914 | 914 | ); |
915 | - if (! $this->_current_data_handler instanceof EE_Messages_incoming_data) { |
|
915 | + if ( ! $this->_current_data_handler instanceof EE_Messages_incoming_data) { |
|
916 | 916 | // no saved data_handler in the repo so let's set one up and add it to the repo. |
917 | 917 | try { |
918 | 918 | $this->_current_data_handler = new $data_handler_class_name($generating_data); |
@@ -936,7 +936,7 @@ discard block |
||
936 | 936 | */ |
937 | 937 | protected function _prepare_data_for_queue(EE_Message_To_Generate $message_to_generate, bool $preview) |
938 | 938 | { |
939 | - if (! $message_to_generate->valid()) { |
|
939 | + if ( ! $message_to_generate->valid()) { |
|
940 | 940 | return false; // unable to get the data because the info in the EE_Message_To_Generate class is invalid. |
941 | 941 | } |
942 | 942 | /** @type EE_Messages_incoming_data $data_handler - well not really... just the class name actually */ |
@@ -20,338 +20,338 @@ |
||
20 | 20 | */ |
21 | 21 | class Base |
22 | 22 | { |
23 | - /** |
|
24 | - * @deprecated use all-caps version |
|
25 | - */ |
|
26 | - // @codingStandardsIgnoreStart |
|
27 | - const header_prefix_for_ee = 'X-EE-'; |
|
28 | - // @codingStandardsIgnoreEnd |
|
29 | - |
|
30 | - const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
|
31 | - |
|
32 | - /** |
|
33 | - * @deprecated use all-caps version instead |
|
34 | - */ |
|
35 | - // @codingStandardsIgnoreStart |
|
36 | - const header_prefix_for_wp = 'X-WP-'; |
|
37 | - // @codingStandardsIgnoreEnd |
|
38 | - |
|
39 | - const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
|
40 | - |
|
41 | - /** |
|
42 | - * Contains debug info we'll send back in the response headers |
|
43 | - * |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - protected $debug_info = array(); |
|
47 | - |
|
48 | - /** |
|
49 | - * Indicates whether or not the API is in debug mode |
|
50 | - * |
|
51 | - * @var boolean |
|
52 | - */ |
|
53 | - protected $debug_mode = false; |
|
54 | - |
|
55 | - /** |
|
56 | - * Indicates the version that was requested |
|
57 | - * |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $requested_version; |
|
61 | - |
|
62 | - /** |
|
63 | - * flat array of headers to send in the response |
|
64 | - * |
|
65 | - * @var array |
|
66 | - */ |
|
67 | - protected $response_headers = array(); |
|
68 | - |
|
69 | - |
|
70 | - public function __construct() |
|
71 | - { |
|
72 | - $this->debug_mode = EED_Core_Rest_Api::debugMode(); |
|
73 | - // we are handling a REST request. Don't show a fancy HTML error message is any error comes up |
|
74 | - add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * Sets the version the user requested |
|
80 | - * |
|
81 | - * @param string $version eg '4.8' |
|
82 | - */ |
|
83 | - public function setRequestedVersion($version) |
|
84 | - { |
|
85 | - $this->requested_version = $version; |
|
86 | - } |
|
87 | - |
|
88 | - |
|
89 | - /** |
|
90 | - * Sets some debug info that we'll send back in headers |
|
91 | - * |
|
92 | - * @param string $key |
|
93 | - * @param string|array $info |
|
94 | - */ |
|
95 | - protected function setDebugInfo($key, $info) |
|
96 | - { |
|
97 | - $this->debug_info[ $key ] = $info; |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Sets headers for the response |
|
103 | - * |
|
104 | - * @param string $header_key , excluding the "X-EE-" part |
|
105 | - * @param array|string $value if an array, multiple headers will be added, one |
|
106 | - * for each key in the array |
|
107 | - * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
108 | - * the standard WP one |
|
109 | - */ |
|
110 | - protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
|
111 | - { |
|
112 | - if (is_array($value)) { |
|
113 | - foreach ($value as $value_key => $value_value) { |
|
114 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
115 | - } |
|
116 | - } else { |
|
117 | - $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
|
118 | - $this->response_headers[ $prefix . $header_key ] = $value; |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * Returns a flat array of headers to be added to the response |
|
125 | - * |
|
126 | - * @return array |
|
127 | - */ |
|
128 | - protected function getResponseHeaders() |
|
129 | - { |
|
130 | - return apply_filters( |
|
131 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
132 | - $this->response_headers, |
|
133 | - $this, |
|
134 | - $this->requested_version |
|
135 | - ); |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * Adds error notices from EE_Error onto the provided \WP_Error |
|
141 | - * |
|
142 | - * @param WP_Error $wp_error_response |
|
143 | - * @return WP_Error |
|
144 | - */ |
|
145 | - protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
|
146 | - { |
|
147 | - $notices_during_checkin = EE_Error::get_raw_notices(); |
|
148 | - if (! empty($notices_during_checkin['errors'])) { |
|
149 | - foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
150 | - $wp_error_response->add( |
|
151 | - sanitize_key($error_code), |
|
152 | - strip_tags($error_message) |
|
153 | - ); |
|
154 | - } |
|
155 | - } |
|
156 | - return $wp_error_response; |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * Sends a response, but also makes sure to attach headers that |
|
162 | - * are handy for debugging. |
|
163 | - * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
164 | - * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
165 | - * the response, others? |
|
166 | - * |
|
167 | - * @param array|WP_Error|Exception|RestException $response |
|
168 | - * @return WP_REST_Response |
|
169 | - */ |
|
170 | - public function sendResponse($response): WP_REST_Response |
|
171 | - { |
|
172 | - if ($response instanceof RestException) { |
|
173 | - $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); |
|
174 | - } |
|
175 | - if ($response instanceof Exception) { |
|
176 | - $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
177 | - $response = new WP_Error($code, $response->getMessage()); |
|
178 | - } |
|
179 | - if ($response instanceof WP_Error) { |
|
180 | - $response = $this->addEeErrorsToResponse($response); |
|
181 | - $rest_response = $this->createRestResponseFromWpError($response); |
|
182 | - } else { |
|
183 | - $rest_response = new WP_REST_Response($response, 200); |
|
184 | - } |
|
185 | - $headers = array(); |
|
186 | - if ($this->debug_mode && is_array($this->debug_info)) { |
|
187 | - foreach ($this->debug_info as $debug_key => $debug_info) { |
|
188 | - if (is_array($debug_info)) { |
|
189 | - $debug_info = wp_json_encode($debug_info); |
|
190 | - } |
|
191 | - $headers['X-EE4-Debug-' . ucwords(str_replace("\x20", '-', $debug_key))] = $debug_info; |
|
192 | - } |
|
193 | - } |
|
194 | - $headers = array_merge( |
|
195 | - $headers, |
|
196 | - $this->getResponseHeaders(), |
|
197 | - $this->getHeadersFromEeNotices() |
|
198 | - ); |
|
199 | - $rest_response->set_headers($headers); |
|
200 | - return $rest_response; |
|
201 | - } |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * Converts the \WP_Error into `WP_REST_Response. |
|
206 | - * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
207 | - * (which is protected) |
|
208 | - * |
|
209 | - * @param WP_Error $wp_error |
|
210 | - * @return WP_REST_Response |
|
211 | - */ |
|
212 | - protected function createRestResponseFromWpError(WP_Error $wp_error) |
|
213 | - { |
|
214 | - $error_data = $wp_error->get_error_data(); |
|
215 | - if (is_array($error_data) && isset($error_data['status'])) { |
|
216 | - $status = $error_data['status']; |
|
217 | - } else { |
|
218 | - $status = 500; |
|
219 | - } |
|
220 | - $errors = array(); |
|
221 | - foreach ((array) $wp_error->errors as $code => $messages) { |
|
222 | - foreach ((array) $messages as $message) { |
|
223 | - $errors[] = array( |
|
224 | - 'code' => $code, |
|
225 | - 'message' => $message, |
|
226 | - 'data' => $wp_error->get_error_data($code), |
|
227 | - ); |
|
228 | - } |
|
229 | - } |
|
230 | - $data = isset($errors[0]) ? $errors[0] : array(); |
|
231 | - if (count($errors) > 1) { |
|
232 | - // Remove the primary error. |
|
233 | - array_shift($errors); |
|
234 | - $data['additional_errors'] = $errors; |
|
235 | - } |
|
236 | - return new WP_REST_Response($data, $status); |
|
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - /** |
|
241 | - * Array of headers derived from EE success, attention, and error messages |
|
242 | - * |
|
243 | - * @return array |
|
244 | - */ |
|
245 | - protected function getHeadersFromEeNotices() |
|
246 | - { |
|
247 | - $headers = array(); |
|
248 | - $notices = EE_Error::get_raw_notices(); |
|
249 | - foreach ($notices as $notice_type => $sub_notices) { |
|
250 | - if (! is_array($sub_notices)) { |
|
251 | - continue; |
|
252 | - } |
|
253 | - foreach ($sub_notices as $notice_code => $sub_notice) { |
|
254 | - $headers[ 'X-EE4-Notices-' |
|
255 | - . EEH_Inflector::humanize($notice_type) |
|
256 | - . '[' |
|
257 | - . $notice_code |
|
258 | - . ']' ] = strip_tags((string) $sub_notice); |
|
259 | - } |
|
260 | - } |
|
261 | - return apply_filters( |
|
262 | - 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
263 | - $headers, |
|
264 | - $this->requested_version, |
|
265 | - $notices |
|
266 | - ); |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * Finds which version of the API was requested given the route, and returns it. |
|
272 | - * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
273 | - * "4.8.29". |
|
274 | - * We should know hte requested version in this model though, so if no route is |
|
275 | - * provided just use what we set earlier |
|
276 | - * |
|
277 | - * @param string $route |
|
278 | - * @return string |
|
279 | - */ |
|
280 | - public function getRequestedVersion($route = null) |
|
281 | - { |
|
282 | - if ($route === null) { |
|
283 | - return $this->requested_version; |
|
284 | - } |
|
285 | - $matches = $this->parseRoute( |
|
286 | - $route, |
|
287 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
288 | - array('version') |
|
289 | - ); |
|
290 | - if (isset($matches['version'])) { |
|
291 | - return $matches['version']; |
|
292 | - } else { |
|
293 | - return EED_Core_Rest_Api::latest_rest_api_version(); |
|
294 | - } |
|
295 | - } |
|
296 | - |
|
297 | - |
|
298 | - /** |
|
299 | - * Applies the regex to the route, then creates an array using the values of |
|
300 | - * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
301 | - * For example, if you call |
|
302 | - * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
303 | - * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
304 | - * |
|
305 | - * @param string $route |
|
306 | - * @param string $regex |
|
307 | - * @param array $match_keys EXCLUDING matching the entire regex |
|
308 | - * @return array where $match_keys are the keys (the first value of $match_keys |
|
309 | - * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
310 | - * array( 'model', 'id' ), will, if the regex is successful, will return |
|
311 | - * array( 'model' => 'foo', 'id' => 'bar' ) |
|
312 | - * @throws EE_Error if it couldn't be parsed |
|
313 | - */ |
|
314 | - public function parseRoute($route, $regex, $match_keys) |
|
315 | - { |
|
316 | - $indexed_matches = array(); |
|
317 | - $success = preg_match($regex, $route, $matches); |
|
318 | - if (is_array($matches)) { |
|
319 | - // skip the overall regex match. Who cares |
|
320 | - for ($i = 1; $i <= count($match_keys); $i++) { |
|
321 | - if (! isset($matches[ $i ])) { |
|
322 | - $success = false; |
|
323 | - } else { |
|
324 | - $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; |
|
325 | - } |
|
326 | - } |
|
327 | - } |
|
328 | - if (! $success) { |
|
329 | - throw new EE_Error( |
|
330 | - esc_html__('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
331 | - 'endpoint_parsing_error' |
|
332 | - ); |
|
333 | - } |
|
334 | - return $indexed_matches; |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - /** |
|
339 | - * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
|
340 | - * |
|
341 | - * @param \WP_REST_Request $request |
|
342 | - * @return array |
|
343 | - */ |
|
344 | - protected function getBodyParams(\WP_REST_Request $request) |
|
345 | - { |
|
346 | - // $request->get_params(); |
|
347 | - return array_merge( |
|
348 | - (array) $request->get_body_params(), |
|
349 | - (array) $request->get_json_params() |
|
350 | - ); |
|
351 | - // return array_diff_key( |
|
352 | - // $request->get_params(), |
|
353 | - // $request->get_url_params(), |
|
354 | - // $request->get_query_params() |
|
355 | - // ); |
|
356 | - } |
|
23 | + /** |
|
24 | + * @deprecated use all-caps version |
|
25 | + */ |
|
26 | + // @codingStandardsIgnoreStart |
|
27 | + const header_prefix_for_ee = 'X-EE-'; |
|
28 | + // @codingStandardsIgnoreEnd |
|
29 | + |
|
30 | + const HEADER_PREFIX_FOR_EE = 'X-EE-'; |
|
31 | + |
|
32 | + /** |
|
33 | + * @deprecated use all-caps version instead |
|
34 | + */ |
|
35 | + // @codingStandardsIgnoreStart |
|
36 | + const header_prefix_for_wp = 'X-WP-'; |
|
37 | + // @codingStandardsIgnoreEnd |
|
38 | + |
|
39 | + const HEADER_PREFIX_FOR_WP = 'X-WP-'; |
|
40 | + |
|
41 | + /** |
|
42 | + * Contains debug info we'll send back in the response headers |
|
43 | + * |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + protected $debug_info = array(); |
|
47 | + |
|
48 | + /** |
|
49 | + * Indicates whether or not the API is in debug mode |
|
50 | + * |
|
51 | + * @var boolean |
|
52 | + */ |
|
53 | + protected $debug_mode = false; |
|
54 | + |
|
55 | + /** |
|
56 | + * Indicates the version that was requested |
|
57 | + * |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $requested_version; |
|
61 | + |
|
62 | + /** |
|
63 | + * flat array of headers to send in the response |
|
64 | + * |
|
65 | + * @var array |
|
66 | + */ |
|
67 | + protected $response_headers = array(); |
|
68 | + |
|
69 | + |
|
70 | + public function __construct() |
|
71 | + { |
|
72 | + $this->debug_mode = EED_Core_Rest_Api::debugMode(); |
|
73 | + // we are handling a REST request. Don't show a fancy HTML error message is any error comes up |
|
74 | + add_filter('FHEE__EE_Error__get_error__show_normal_exceptions', '__return_true'); |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * Sets the version the user requested |
|
80 | + * |
|
81 | + * @param string $version eg '4.8' |
|
82 | + */ |
|
83 | + public function setRequestedVersion($version) |
|
84 | + { |
|
85 | + $this->requested_version = $version; |
|
86 | + } |
|
87 | + |
|
88 | + |
|
89 | + /** |
|
90 | + * Sets some debug info that we'll send back in headers |
|
91 | + * |
|
92 | + * @param string $key |
|
93 | + * @param string|array $info |
|
94 | + */ |
|
95 | + protected function setDebugInfo($key, $info) |
|
96 | + { |
|
97 | + $this->debug_info[ $key ] = $info; |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * Sets headers for the response |
|
103 | + * |
|
104 | + * @param string $header_key , excluding the "X-EE-" part |
|
105 | + * @param array|string $value if an array, multiple headers will be added, one |
|
106 | + * for each key in the array |
|
107 | + * @param boolean $use_ee_prefix whether to use the EE prefix on the header, or fallback to |
|
108 | + * the standard WP one |
|
109 | + */ |
|
110 | + protected function setResponseHeader($header_key, $value, $use_ee_prefix = true) |
|
111 | + { |
|
112 | + if (is_array($value)) { |
|
113 | + foreach ($value as $value_key => $value_value) { |
|
114 | + $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
115 | + } |
|
116 | + } else { |
|
117 | + $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
|
118 | + $this->response_headers[ $prefix . $header_key ] = $value; |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * Returns a flat array of headers to be added to the response |
|
125 | + * |
|
126 | + * @return array |
|
127 | + */ |
|
128 | + protected function getResponseHeaders() |
|
129 | + { |
|
130 | + return apply_filters( |
|
131 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_response_headers', |
|
132 | + $this->response_headers, |
|
133 | + $this, |
|
134 | + $this->requested_version |
|
135 | + ); |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * Adds error notices from EE_Error onto the provided \WP_Error |
|
141 | + * |
|
142 | + * @param WP_Error $wp_error_response |
|
143 | + * @return WP_Error |
|
144 | + */ |
|
145 | + protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
|
146 | + { |
|
147 | + $notices_during_checkin = EE_Error::get_raw_notices(); |
|
148 | + if (! empty($notices_during_checkin['errors'])) { |
|
149 | + foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
|
150 | + $wp_error_response->add( |
|
151 | + sanitize_key($error_code), |
|
152 | + strip_tags($error_message) |
|
153 | + ); |
|
154 | + } |
|
155 | + } |
|
156 | + return $wp_error_response; |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * Sends a response, but also makes sure to attach headers that |
|
162 | + * are handy for debugging. |
|
163 | + * Specifically, we assume folks will want to know what exactly was the DB query that got run, |
|
164 | + * what exactly was the Models query that got run, what capabilities came into play, what fields were omitted from |
|
165 | + * the response, others? |
|
166 | + * |
|
167 | + * @param array|WP_Error|Exception|RestException $response |
|
168 | + * @return WP_REST_Response |
|
169 | + */ |
|
170 | + public function sendResponse($response): WP_REST_Response |
|
171 | + { |
|
172 | + if ($response instanceof RestException) { |
|
173 | + $response = new WP_Error($response->getStringCode(), $response->getMessage(), $response->getData()); |
|
174 | + } |
|
175 | + if ($response instanceof Exception) { |
|
176 | + $code = $response->getCode() ? $response->getCode() : 'error_occurred'; |
|
177 | + $response = new WP_Error($code, $response->getMessage()); |
|
178 | + } |
|
179 | + if ($response instanceof WP_Error) { |
|
180 | + $response = $this->addEeErrorsToResponse($response); |
|
181 | + $rest_response = $this->createRestResponseFromWpError($response); |
|
182 | + } else { |
|
183 | + $rest_response = new WP_REST_Response($response, 200); |
|
184 | + } |
|
185 | + $headers = array(); |
|
186 | + if ($this->debug_mode && is_array($this->debug_info)) { |
|
187 | + foreach ($this->debug_info as $debug_key => $debug_info) { |
|
188 | + if (is_array($debug_info)) { |
|
189 | + $debug_info = wp_json_encode($debug_info); |
|
190 | + } |
|
191 | + $headers['X-EE4-Debug-' . ucwords(str_replace("\x20", '-', $debug_key))] = $debug_info; |
|
192 | + } |
|
193 | + } |
|
194 | + $headers = array_merge( |
|
195 | + $headers, |
|
196 | + $this->getResponseHeaders(), |
|
197 | + $this->getHeadersFromEeNotices() |
|
198 | + ); |
|
199 | + $rest_response->set_headers($headers); |
|
200 | + return $rest_response; |
|
201 | + } |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * Converts the \WP_Error into `WP_REST_Response. |
|
206 | + * Mostly this is just a copy-and-paste from \WP_REST_Server::error_to_response |
|
207 | + * (which is protected) |
|
208 | + * |
|
209 | + * @param WP_Error $wp_error |
|
210 | + * @return WP_REST_Response |
|
211 | + */ |
|
212 | + protected function createRestResponseFromWpError(WP_Error $wp_error) |
|
213 | + { |
|
214 | + $error_data = $wp_error->get_error_data(); |
|
215 | + if (is_array($error_data) && isset($error_data['status'])) { |
|
216 | + $status = $error_data['status']; |
|
217 | + } else { |
|
218 | + $status = 500; |
|
219 | + } |
|
220 | + $errors = array(); |
|
221 | + foreach ((array) $wp_error->errors as $code => $messages) { |
|
222 | + foreach ((array) $messages as $message) { |
|
223 | + $errors[] = array( |
|
224 | + 'code' => $code, |
|
225 | + 'message' => $message, |
|
226 | + 'data' => $wp_error->get_error_data($code), |
|
227 | + ); |
|
228 | + } |
|
229 | + } |
|
230 | + $data = isset($errors[0]) ? $errors[0] : array(); |
|
231 | + if (count($errors) > 1) { |
|
232 | + // Remove the primary error. |
|
233 | + array_shift($errors); |
|
234 | + $data['additional_errors'] = $errors; |
|
235 | + } |
|
236 | + return new WP_REST_Response($data, $status); |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * Array of headers derived from EE success, attention, and error messages |
|
242 | + * |
|
243 | + * @return array |
|
244 | + */ |
|
245 | + protected function getHeadersFromEeNotices() |
|
246 | + { |
|
247 | + $headers = array(); |
|
248 | + $notices = EE_Error::get_raw_notices(); |
|
249 | + foreach ($notices as $notice_type => $sub_notices) { |
|
250 | + if (! is_array($sub_notices)) { |
|
251 | + continue; |
|
252 | + } |
|
253 | + foreach ($sub_notices as $notice_code => $sub_notice) { |
|
254 | + $headers[ 'X-EE4-Notices-' |
|
255 | + . EEH_Inflector::humanize($notice_type) |
|
256 | + . '[' |
|
257 | + . $notice_code |
|
258 | + . ']' ] = strip_tags((string) $sub_notice); |
|
259 | + } |
|
260 | + } |
|
261 | + return apply_filters( |
|
262 | + 'FHEE__EventEspresso\core\libraries\rest_api\controllers\Base___get_headers_from_ee_notices__return', |
|
263 | + $headers, |
|
264 | + $this->requested_version, |
|
265 | + $notices |
|
266 | + ); |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * Finds which version of the API was requested given the route, and returns it. |
|
272 | + * eg in a request to "mysite.com/wp-json/ee/v4.8.29/events/123" this would return |
|
273 | + * "4.8.29". |
|
274 | + * We should know hte requested version in this model though, so if no route is |
|
275 | + * provided just use what we set earlier |
|
276 | + * |
|
277 | + * @param string $route |
|
278 | + * @return string |
|
279 | + */ |
|
280 | + public function getRequestedVersion($route = null) |
|
281 | + { |
|
282 | + if ($route === null) { |
|
283 | + return $this->requested_version; |
|
284 | + } |
|
285 | + $matches = $this->parseRoute( |
|
286 | + $route, |
|
287 | + '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
288 | + array('version') |
|
289 | + ); |
|
290 | + if (isset($matches['version'])) { |
|
291 | + return $matches['version']; |
|
292 | + } else { |
|
293 | + return EED_Core_Rest_Api::latest_rest_api_version(); |
|
294 | + } |
|
295 | + } |
|
296 | + |
|
297 | + |
|
298 | + /** |
|
299 | + * Applies the regex to the route, then creates an array using the values of |
|
300 | + * $match_keys as keys (but ignores the full pattern match). Returns the array of matches. |
|
301 | + * For example, if you call |
|
302 | + * parse_route( '/ee/v4.8/events', '~\/ee\/v([^/]*)\/(.*)~', array( 'version', 'model' ) ) |
|
303 | + * it will return array( 'version' => '4.8', 'model' => 'events' ) |
|
304 | + * |
|
305 | + * @param string $route |
|
306 | + * @param string $regex |
|
307 | + * @param array $match_keys EXCLUDING matching the entire regex |
|
308 | + * @return array where $match_keys are the keys (the first value of $match_keys |
|
309 | + * becomes the first key of the return value, etc. Eg passing in $match_keys of |
|
310 | + * array( 'model', 'id' ), will, if the regex is successful, will return |
|
311 | + * array( 'model' => 'foo', 'id' => 'bar' ) |
|
312 | + * @throws EE_Error if it couldn't be parsed |
|
313 | + */ |
|
314 | + public function parseRoute($route, $regex, $match_keys) |
|
315 | + { |
|
316 | + $indexed_matches = array(); |
|
317 | + $success = preg_match($regex, $route, $matches); |
|
318 | + if (is_array($matches)) { |
|
319 | + // skip the overall regex match. Who cares |
|
320 | + for ($i = 1; $i <= count($match_keys); $i++) { |
|
321 | + if (! isset($matches[ $i ])) { |
|
322 | + $success = false; |
|
323 | + } else { |
|
324 | + $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; |
|
325 | + } |
|
326 | + } |
|
327 | + } |
|
328 | + if (! $success) { |
|
329 | + throw new EE_Error( |
|
330 | + esc_html__('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
|
331 | + 'endpoint_parsing_error' |
|
332 | + ); |
|
333 | + } |
|
334 | + return $indexed_matches; |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + /** |
|
339 | + * Gets the body's params (either from JSON or parsed body), which EXCLUDES the GET params and URL params |
|
340 | + * |
|
341 | + * @param \WP_REST_Request $request |
|
342 | + * @return array |
|
343 | + */ |
|
344 | + protected function getBodyParams(\WP_REST_Request $request) |
|
345 | + { |
|
346 | + // $request->get_params(); |
|
347 | + return array_merge( |
|
348 | + (array) $request->get_body_params(), |
|
349 | + (array) $request->get_json_params() |
|
350 | + ); |
|
351 | + // return array_diff_key( |
|
352 | + // $request->get_params(), |
|
353 | + // $request->get_url_params(), |
|
354 | + // $request->get_query_params() |
|
355 | + // ); |
|
356 | + } |
|
357 | 357 | } |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | */ |
95 | 95 | protected function setDebugInfo($key, $info) |
96 | 96 | { |
97 | - $this->debug_info[ $key ] = $info; |
|
97 | + $this->debug_info[$key] = $info; |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | |
@@ -111,11 +111,11 @@ discard block |
||
111 | 111 | { |
112 | 112 | if (is_array($value)) { |
113 | 113 | foreach ($value as $value_key => $value_value) { |
114 | - $this->setResponseHeader($header_key . '[' . $value_key . ']', $value_value); |
|
114 | + $this->setResponseHeader($header_key.'['.$value_key.']', $value_value); |
|
115 | 115 | } |
116 | 116 | } else { |
117 | 117 | $prefix = $use_ee_prefix ? Base::HEADER_PREFIX_FOR_EE : Base::HEADER_PREFIX_FOR_WP; |
118 | - $this->response_headers[ $prefix . $header_key ] = $value; |
|
118 | + $this->response_headers[$prefix.$header_key] = $value; |
|
119 | 119 | } |
120 | 120 | } |
121 | 121 | |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | protected function addEeErrorsToResponse(WP_Error $wp_error_response) |
146 | 146 | { |
147 | 147 | $notices_during_checkin = EE_Error::get_raw_notices(); |
148 | - if (! empty($notices_during_checkin['errors'])) { |
|
148 | + if ( ! empty($notices_during_checkin['errors'])) { |
|
149 | 149 | foreach ($notices_during_checkin['errors'] as $error_code => $error_message) { |
150 | 150 | $wp_error_response->add( |
151 | 151 | sanitize_key($error_code), |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | if (is_array($debug_info)) { |
189 | 189 | $debug_info = wp_json_encode($debug_info); |
190 | 190 | } |
191 | - $headers['X-EE4-Debug-' . ucwords(str_replace("\x20", '-', $debug_key))] = $debug_info; |
|
191 | + $headers['X-EE4-Debug-'.ucwords(str_replace("\x20", '-', $debug_key))] = $debug_info; |
|
192 | 192 | } |
193 | 193 | } |
194 | 194 | $headers = array_merge( |
@@ -247,15 +247,15 @@ discard block |
||
247 | 247 | $headers = array(); |
248 | 248 | $notices = EE_Error::get_raw_notices(); |
249 | 249 | foreach ($notices as $notice_type => $sub_notices) { |
250 | - if (! is_array($sub_notices)) { |
|
250 | + if ( ! is_array($sub_notices)) { |
|
251 | 251 | continue; |
252 | 252 | } |
253 | 253 | foreach ($sub_notices as $notice_code => $sub_notice) { |
254 | - $headers[ 'X-EE4-Notices-' |
|
254 | + $headers['X-EE4-Notices-' |
|
255 | 255 | . EEH_Inflector::humanize($notice_type) |
256 | 256 | . '[' |
257 | 257 | . $notice_code |
258 | - . ']' ] = strip_tags((string) $sub_notice); |
|
258 | + . ']'] = strip_tags((string) $sub_notice); |
|
259 | 259 | } |
260 | 260 | } |
261 | 261 | return apply_filters( |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | } |
285 | 285 | $matches = $this->parseRoute( |
286 | 286 | $route, |
287 | - '~' . EED_Core_Rest_Api::ee_api_namespace_for_regex . '~', |
|
287 | + '~'.EED_Core_Rest_Api::ee_api_namespace_for_regex.'~', |
|
288 | 288 | array('version') |
289 | 289 | ); |
290 | 290 | if (isset($matches['version'])) { |
@@ -318,14 +318,14 @@ discard block |
||
318 | 318 | if (is_array($matches)) { |
319 | 319 | // skip the overall regex match. Who cares |
320 | 320 | for ($i = 1; $i <= count($match_keys); $i++) { |
321 | - if (! isset($matches[ $i ])) { |
|
321 | + if ( ! isset($matches[$i])) { |
|
322 | 322 | $success = false; |
323 | 323 | } else { |
324 | - $indexed_matches[ $match_keys[ $i - 1 ] ] = $matches[ $i ]; |
|
324 | + $indexed_matches[$match_keys[$i - 1]] = $matches[$i]; |
|
325 | 325 | } |
326 | 326 | } |
327 | 327 | } |
328 | - if (! $success) { |
|
328 | + if ( ! $success) { |
|
329 | 329 | throw new EE_Error( |
330 | 330 | esc_html__('We could not parse the URL. Please contact Event Espresso Support', 'event_espresso'), |
331 | 331 | 'endpoint_parsing_error' |
@@ -29,133 +29,133 @@ |
||
29 | 29 | */ |
30 | 30 | class EEE_Base_Class |
31 | 31 | { |
32 | - const extending_method_prefix = 'ext_'; |
|
32 | + const extending_method_prefix = 'ext_'; |
|
33 | 33 | |
34 | - const dynamic_callback_method_prefix = 'dynamic_callback_method_'; |
|
34 | + const dynamic_callback_method_prefix = 'dynamic_callback_method_'; |
|
35 | 35 | |
36 | - /** |
|
37 | - * The model name that is extended (not classname) |
|
38 | - * |
|
39 | - * @var string|null |
|
40 | - */ |
|
41 | - protected ?string $_model_name_extended = null; |
|
36 | + /** |
|
37 | + * The model name that is extended (not classname) |
|
38 | + * |
|
39 | + * @var string|null |
|
40 | + */ |
|
41 | + protected ?string $_model_name_extended = null; |
|
42 | 42 | |
43 | - /** |
|
44 | - * The model this extends |
|
45 | - * |
|
46 | - * @var EE_Base_Class|null |
|
47 | - */ |
|
48 | - protected $_ = null; |
|
43 | + /** |
|
44 | + * The model this extends |
|
45 | + * |
|
46 | + * @var EE_Base_Class|null |
|
47 | + */ |
|
48 | + protected $_ = null; |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * @throws EE_Error |
|
53 | - */ |
|
54 | - public function __construct() |
|
55 | - { |
|
56 | - if (! $this->_model_name_extended) { |
|
57 | - throw new EE_Error( |
|
58 | - esc_html__( |
|
59 | - "When declaring a class extension, you must define its _model_name_extended property. It should be a model name like 'Attendee' or 'Event'", |
|
60 | - "event_espresso" |
|
61 | - ) |
|
51 | + /** |
|
52 | + * @throws EE_Error |
|
53 | + */ |
|
54 | + public function __construct() |
|
55 | + { |
|
56 | + if (! $this->_model_name_extended) { |
|
57 | + throw new EE_Error( |
|
58 | + esc_html__( |
|
59 | + "When declaring a class extension, you must define its _model_name_extended property. It should be a model name like 'Attendee' or 'Event'", |
|
60 | + "event_espresso" |
|
61 | + ) |
|
62 | 62 | |
63 | - ); |
|
64 | - } |
|
65 | - if (did_action('AHEE__EE_' . $this->_model_name_extended . '__construct__end')) { |
|
66 | - throw new EE_Error( |
|
67 | - sprintf( |
|
68 | - esc_html__( |
|
69 | - "Hooked in model object extension '%s' too late! The model object %s has already been used!", |
|
70 | - "event_espresso" |
|
71 | - ), |
|
72 | - get_class($this), |
|
73 | - $this->_model_name_extended |
|
74 | - ) |
|
75 | - ); |
|
76 | - } |
|
77 | - $this->_register_extending_methods(); |
|
78 | - } |
|
63 | + ); |
|
64 | + } |
|
65 | + if (did_action('AHEE__EE_' . $this->_model_name_extended . '__construct__end')) { |
|
66 | + throw new EE_Error( |
|
67 | + sprintf( |
|
68 | + esc_html__( |
|
69 | + "Hooked in model object extension '%s' too late! The model object %s has already been used!", |
|
70 | + "event_espresso" |
|
71 | + ), |
|
72 | + get_class($this), |
|
73 | + $this->_model_name_extended |
|
74 | + ) |
|
75 | + ); |
|
76 | + } |
|
77 | + $this->_register_extending_methods(); |
|
78 | + } |
|
79 | 79 | |
80 | 80 | |
81 | - /** |
|
82 | - * scans the child of EEME_Base for functions starting with ext_, and magically makes them functions on the |
|
83 | - * model extended. (Internally uses filters, and the __call magic method) |
|
84 | - */ |
|
85 | - protected function _register_extending_methods() |
|
86 | - { |
|
87 | - $all_methods = get_class_methods(get_class($this)); |
|
88 | - foreach ($all_methods as $method_name) { |
|
89 | - if (strpos($method_name, self::extending_method_prefix) === 0) { |
|
90 | - $method_name_on_model = str_replace(self::extending_method_prefix, '', $method_name); |
|
91 | - $callback_name = "FHEE__EE_{$this->_model_name_extended}__$method_name_on_model"; |
|
92 | - add_filter( |
|
93 | - $callback_name, |
|
94 | - [$this, self::dynamic_callback_method_prefix . $method_name_on_model], |
|
95 | - 10, |
|
96 | - 10 |
|
97 | - ); |
|
98 | - } |
|
99 | - } |
|
100 | - } |
|
81 | + /** |
|
82 | + * scans the child of EEME_Base for functions starting with ext_, and magically makes them functions on the |
|
83 | + * model extended. (Internally uses filters, and the __call magic method) |
|
84 | + */ |
|
85 | + protected function _register_extending_methods() |
|
86 | + { |
|
87 | + $all_methods = get_class_methods(get_class($this)); |
|
88 | + foreach ($all_methods as $method_name) { |
|
89 | + if (strpos($method_name, self::extending_method_prefix) === 0) { |
|
90 | + $method_name_on_model = str_replace(self::extending_method_prefix, '', $method_name); |
|
91 | + $callback_name = "FHEE__EE_{$this->_model_name_extended}__$method_name_on_model"; |
|
92 | + add_filter( |
|
93 | + $callback_name, |
|
94 | + [$this, self::dynamic_callback_method_prefix . $method_name_on_model], |
|
95 | + 10, |
|
96 | + 10 |
|
97 | + ); |
|
98 | + } |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | 102 | |
103 | - /** |
|
104 | - * scans the child of EEME_Base for functions starting with ext_, and magically REMOVES them as functions on the |
|
105 | - * model extended. (Internally uses filters, and the __call magic method) |
|
106 | - */ |
|
107 | - public function deregister() |
|
108 | - { |
|
109 | - $all_methods = get_class_methods(get_class($this)); |
|
110 | - foreach ($all_methods as $method_name) { |
|
111 | - if (strpos($method_name, self::extending_method_prefix) === 0) { |
|
112 | - $method_name_on_model = str_replace(self::extending_method_prefix, '', $method_name); |
|
113 | - $callback_name = "FHEE__EE_{$this->_model_name_extended}__$method_name_on_model"; |
|
114 | - remove_filter( |
|
115 | - $callback_name, |
|
116 | - [$this, self::dynamic_callback_method_prefix . $method_name_on_model] |
|
117 | - ); |
|
118 | - } |
|
119 | - } |
|
120 | - } |
|
103 | + /** |
|
104 | + * scans the child of EEME_Base for functions starting with ext_, and magically REMOVES them as functions on the |
|
105 | + * model extended. (Internally uses filters, and the __call magic method) |
|
106 | + */ |
|
107 | + public function deregister() |
|
108 | + { |
|
109 | + $all_methods = get_class_methods(get_class($this)); |
|
110 | + foreach ($all_methods as $method_name) { |
|
111 | + if (strpos($method_name, self::extending_method_prefix) === 0) { |
|
112 | + $method_name_on_model = str_replace(self::extending_method_prefix, '', $method_name); |
|
113 | + $callback_name = "FHEE__EE_{$this->_model_name_extended}__$method_name_on_model"; |
|
114 | + remove_filter( |
|
115 | + $callback_name, |
|
116 | + [$this, self::dynamic_callback_method_prefix . $method_name_on_model] |
|
117 | + ); |
|
118 | + } |
|
119 | + } |
|
120 | + } |
|
121 | 121 | |
122 | 122 | |
123 | - /** |
|
124 | - * @throws EE_Error |
|
125 | - */ |
|
126 | - public function __call($callback_method_name, $args) |
|
127 | - { |
|
128 | - if (strpos($callback_method_name, self::dynamic_callback_method_prefix) === 0) { |
|
129 | - // it's a dynamic callback for a method name |
|
130 | - $method_called_on_model = str_replace(self::dynamic_callback_method_prefix, '', $callback_method_name); |
|
131 | - $model_called = $args[1]; |
|
132 | - // phpcs:disable WordPress.WP.I18n.SingleUnderscoreGetTextFunction |
|
133 | - $this->_ = $model_called; |
|
134 | - // phpcs:enable |
|
135 | - $args_provided_to_method_on_model = $args[2]; |
|
136 | - $extending_method = self::extending_method_prefix . $method_called_on_model; |
|
137 | - if (method_exists($this, $extending_method)) { |
|
138 | - return call_user_func_array([$this, $extending_method], $args_provided_to_method_on_model); |
|
139 | - } |
|
140 | - throw new EE_Error( |
|
141 | - sprintf( |
|
142 | - esc_html__( |
|
143 | - "An odd error occurred. Model '%s' had a method called on it that it didn't recognize. So it passed it onto the model extension '%s' (because it had a function named '%s' which should be able to handle it), but the function '%s' doesnt exist!)", |
|
144 | - "event_espresso" |
|
145 | - ), |
|
146 | - $this->_model_name_extended, |
|
147 | - get_class($this), |
|
148 | - $extending_method, |
|
149 | - $extending_method |
|
150 | - ) |
|
151 | - ); |
|
152 | - } |
|
153 | - throw new EE_Error( |
|
154 | - sprintf( |
|
155 | - esc_html__("There is no method named '%s' on '%s'", "event_espresso"), |
|
156 | - $callback_method_name, |
|
157 | - get_class($this) |
|
158 | - ) |
|
159 | - ); |
|
160 | - } |
|
123 | + /** |
|
124 | + * @throws EE_Error |
|
125 | + */ |
|
126 | + public function __call($callback_method_name, $args) |
|
127 | + { |
|
128 | + if (strpos($callback_method_name, self::dynamic_callback_method_prefix) === 0) { |
|
129 | + // it's a dynamic callback for a method name |
|
130 | + $method_called_on_model = str_replace(self::dynamic_callback_method_prefix, '', $callback_method_name); |
|
131 | + $model_called = $args[1]; |
|
132 | + // phpcs:disable WordPress.WP.I18n.SingleUnderscoreGetTextFunction |
|
133 | + $this->_ = $model_called; |
|
134 | + // phpcs:enable |
|
135 | + $args_provided_to_method_on_model = $args[2]; |
|
136 | + $extending_method = self::extending_method_prefix . $method_called_on_model; |
|
137 | + if (method_exists($this, $extending_method)) { |
|
138 | + return call_user_func_array([$this, $extending_method], $args_provided_to_method_on_model); |
|
139 | + } |
|
140 | + throw new EE_Error( |
|
141 | + sprintf( |
|
142 | + esc_html__( |
|
143 | + "An odd error occurred. Model '%s' had a method called on it that it didn't recognize. So it passed it onto the model extension '%s' (because it had a function named '%s' which should be able to handle it), but the function '%s' doesnt exist!)", |
|
144 | + "event_espresso" |
|
145 | + ), |
|
146 | + $this->_model_name_extended, |
|
147 | + get_class($this), |
|
148 | + $extending_method, |
|
149 | + $extending_method |
|
150 | + ) |
|
151 | + ); |
|
152 | + } |
|
153 | + throw new EE_Error( |
|
154 | + sprintf( |
|
155 | + esc_html__("There is no method named '%s' on '%s'", "event_espresso"), |
|
156 | + $callback_method_name, |
|
157 | + get_class($this) |
|
158 | + ) |
|
159 | + ); |
|
160 | + } |
|
161 | 161 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function __construct() |
55 | 55 | { |
56 | - if (! $this->_model_name_extended) { |
|
56 | + if ( ! $this->_model_name_extended) { |
|
57 | 57 | throw new EE_Error( |
58 | 58 | esc_html__( |
59 | 59 | "When declaring a class extension, you must define its _model_name_extended property. It should be a model name like 'Attendee' or 'Event'", |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | |
63 | 63 | ); |
64 | 64 | } |
65 | - if (did_action('AHEE__EE_' . $this->_model_name_extended . '__construct__end')) { |
|
65 | + if (did_action('AHEE__EE_'.$this->_model_name_extended.'__construct__end')) { |
|
66 | 66 | throw new EE_Error( |
67 | 67 | sprintf( |
68 | 68 | esc_html__( |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | $callback_name = "FHEE__EE_{$this->_model_name_extended}__$method_name_on_model"; |
92 | 92 | add_filter( |
93 | 93 | $callback_name, |
94 | - [$this, self::dynamic_callback_method_prefix . $method_name_on_model], |
|
94 | + [$this, self::dynamic_callback_method_prefix.$method_name_on_model], |
|
95 | 95 | 10, |
96 | 96 | 10 |
97 | 97 | ); |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | $callback_name = "FHEE__EE_{$this->_model_name_extended}__$method_name_on_model"; |
114 | 114 | remove_filter( |
115 | 115 | $callback_name, |
116 | - [$this, self::dynamic_callback_method_prefix . $method_name_on_model] |
|
116 | + [$this, self::dynamic_callback_method_prefix.$method_name_on_model] |
|
117 | 117 | ); |
118 | 118 | } |
119 | 119 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | $this->_ = $model_called; |
134 | 134 | // phpcs:enable |
135 | 135 | $args_provided_to_method_on_model = $args[2]; |
136 | - $extending_method = self::extending_method_prefix . $method_called_on_model; |
|
136 | + $extending_method = self::extending_method_prefix.$method_called_on_model; |
|
137 | 137 | if (method_exists($this, $extending_method)) { |
138 | 138 | return call_user_func_array([$this, $extending_method], $args_provided_to_method_on_model); |
139 | 139 | } |
@@ -10,131 +10,131 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Register_Admin_Page implements EEI_Plugin_API |
12 | 12 | { |
13 | - /** |
|
14 | - * Holds registered EE_Admin_Pages |
|
15 | - * |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - protected static $_ee_admin_page_registry = []; |
|
13 | + /** |
|
14 | + * Holds registered EE_Admin_Pages |
|
15 | + * |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + protected static $_ee_admin_page_registry = []; |
|
19 | 19 | |
20 | 20 | |
21 | - /** |
|
22 | - * The purpose of this method is to provide an easy way for addons to register their admin pages (using the EE |
|
23 | - * Admin Page loader system). |
|
24 | - * |
|
25 | - * @param string $addon_name This string represents the basename of the Admin |
|
26 | - * Page init. The init file must use this basename |
|
27 | - * in its name and class (i.e. |
|
28 | - * {page_basename}_Admin_Page_Init.core.php). |
|
29 | - * @param array $setup_args { An array of configuration options |
|
30 | - * that will be used in different circumstances |
|
31 | - * |
|
32 | - * @type string $page_path This is the path where the registered admin pages |
|
33 | - * reside ( used to setup autoloaders). |
|
34 | - * |
|
35 | - * } |
|
36 | - * @return bool |
|
37 | - * @throws EE_Error |
|
38 | - * @since 4.3.0 |
|
39 | - * |
|
40 | - */ |
|
41 | - public static function register(string $addon_name = '', array $setup_args = []): bool |
|
42 | - { |
|
21 | + /** |
|
22 | + * The purpose of this method is to provide an easy way for addons to register their admin pages (using the EE |
|
23 | + * Admin Page loader system). |
|
24 | + * |
|
25 | + * @param string $addon_name This string represents the basename of the Admin |
|
26 | + * Page init. The init file must use this basename |
|
27 | + * in its name and class (i.e. |
|
28 | + * {page_basename}_Admin_Page_Init.core.php). |
|
29 | + * @param array $setup_args { An array of configuration options |
|
30 | + * that will be used in different circumstances |
|
31 | + * |
|
32 | + * @type string $page_path This is the path where the registered admin pages |
|
33 | + * reside ( used to setup autoloaders). |
|
34 | + * |
|
35 | + * } |
|
36 | + * @return bool |
|
37 | + * @throws EE_Error |
|
38 | + * @since 4.3.0 |
|
39 | + * |
|
40 | + */ |
|
41 | + public static function register(string $addon_name = '', array $setup_args = []): bool |
|
42 | + { |
|
43 | 43 | |
44 | - // check that an admin_page has not already been registered with that name |
|
45 | - if (isset(self::$_ee_admin_page_registry[ $addon_name ])) { |
|
46 | - throw new EE_Error( |
|
47 | - sprintf( |
|
48 | - esc_html__( |
|
49 | - 'An Admin Page located at %1$s has already been registered and each Admin Page requires a unique name. |
|
44 | + // check that an admin_page has not already been registered with that name |
|
45 | + if (isset(self::$_ee_admin_page_registry[ $addon_name ])) { |
|
46 | + throw new EE_Error( |
|
47 | + sprintf( |
|
48 | + esc_html__( |
|
49 | + 'An Admin Page located at %1$s has already been registered and each Admin Page requires a unique name. |
|
50 | 50 | Error thrown while registering an Admin Page of the same name for the %2$s addon with the admin page path: %3$s."', |
51 | - 'event_espresso' |
|
52 | - ), |
|
53 | - '<pre>' . (self::$_ee_admin_page_registry[ $addon_name ]['page_path']) . '</pre>', |
|
54 | - $addon_name, |
|
55 | - '<pre>' . ($setup_args['page_path'] ?? '') . '</pre>' |
|
56 | - ) |
|
57 | - ); |
|
58 | - } |
|
51 | + 'event_espresso' |
|
52 | + ), |
|
53 | + '<pre>' . (self::$_ee_admin_page_registry[ $addon_name ]['page_path']) . '</pre>', |
|
54 | + $addon_name, |
|
55 | + '<pre>' . ($setup_args['page_path'] ?? '') . '</pre>' |
|
56 | + ) |
|
57 | + ); |
|
58 | + } |
|
59 | 59 | |
60 | - // required fields MUST be present, so let's make sure they are. |
|
61 | - if (empty($addon_name) || ! is_array($setup_args) || empty($setup_args['page_path'])) { |
|
62 | - throw new EE_Error( |
|
63 | - esc_html__( |
|
64 | - 'In order to register an Admin Page with EE_Register_Admin_Page::register(), you must include the "page_basename" (the class name of the page), and an array containing the following keys: "page_path" (the path where the registered admin pages reside)', |
|
65 | - 'event_espresso' |
|
66 | - ) |
|
67 | - ); |
|
68 | - } |
|
60 | + // required fields MUST be present, so let's make sure they are. |
|
61 | + if (empty($addon_name) || ! is_array($setup_args) || empty($setup_args['page_path'])) { |
|
62 | + throw new EE_Error( |
|
63 | + esc_html__( |
|
64 | + 'In order to register an Admin Page with EE_Register_Admin_Page::register(), you must include the "page_basename" (the class name of the page), and an array containing the following keys: "page_path" (the path where the registered admin pages reside)', |
|
65 | + 'event_espresso' |
|
66 | + ) |
|
67 | + ); |
|
68 | + } |
|
69 | 69 | |
70 | - // make sure we don't register twice |
|
71 | - if (isset(self::$_ee_admin_page_registry[ $addon_name ])) { |
|
72 | - return true; |
|
73 | - } |
|
70 | + // make sure we don't register twice |
|
71 | + if (isset(self::$_ee_admin_page_registry[ $addon_name ])) { |
|
72 | + return true; |
|
73 | + } |
|
74 | 74 | |
75 | - if (! did_action('AHEE__EE_System__load_espresso_addons') || did_action('AHEE__EE_Admin__loaded')) { |
|
76 | - EE_Error::doing_it_wrong( |
|
77 | - __METHOD__, |
|
78 | - sprintf( |
|
79 | - esc_html__( |
|
80 | - 'An attempt was made to register "%s" as an EE Admin page has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__load_controllers__load_admin_controllers" hook to register Admin pages.', |
|
81 | - 'event_espresso' |
|
82 | - ), |
|
83 | - $addon_name |
|
84 | - ), |
|
85 | - '4.3' |
|
86 | - ); |
|
87 | - } |
|
75 | + if (! did_action('AHEE__EE_System__load_espresso_addons') || did_action('AHEE__EE_Admin__loaded')) { |
|
76 | + EE_Error::doing_it_wrong( |
|
77 | + __METHOD__, |
|
78 | + sprintf( |
|
79 | + esc_html__( |
|
80 | + 'An attempt was made to register "%s" as an EE Admin page has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__load_controllers__load_admin_controllers" hook to register Admin pages.', |
|
81 | + 'event_espresso' |
|
82 | + ), |
|
83 | + $addon_name |
|
84 | + ), |
|
85 | + '4.3' |
|
86 | + ); |
|
87 | + } |
|
88 | 88 | |
89 | - // add incoming stuff to our registry property |
|
90 | - self::$_ee_admin_page_registry[ $addon_name ] = [ |
|
91 | - 'page_path' => $setup_args['page_path'], |
|
92 | - 'config' => $setup_args, |
|
93 | - ]; |
|
89 | + // add incoming stuff to our registry property |
|
90 | + self::$_ee_admin_page_registry[ $addon_name ] = [ |
|
91 | + 'page_path' => $setup_args['page_path'], |
|
92 | + 'config' => $setup_args, |
|
93 | + ]; |
|
94 | 94 | |
95 | - // add filters |
|
96 | - add_filter( |
|
97 | - 'FHEE__EE_Admin_Page_Loader__findAdminPages__admin_page_folders', |
|
98 | - ['EE_Register_Admin_Page', 'set_page_path'] |
|
99 | - ); |
|
95 | + // add filters |
|
96 | + add_filter( |
|
97 | + 'FHEE__EE_Admin_Page_Loader__findAdminPages__admin_page_folders', |
|
98 | + ['EE_Register_Admin_Page', 'set_page_path'] |
|
99 | + ); |
|
100 | 100 | |
101 | - return true; |
|
102 | - } |
|
101 | + return true; |
|
102 | + } |
|
103 | 103 | |
104 | 104 | |
105 | - /** |
|
106 | - * This deregisters a EE_Admin page that is already registered. Note, this MUST be loaded after the |
|
107 | - * page being deregistered is loaded. |
|
108 | - * |
|
109 | - * @param string $addon_name Use whatever string was used to register the admin page. |
|
110 | - * @return void |
|
111 | - * @since 4.3.0 |
|
112 | - * |
|
113 | - */ |
|
114 | - public static function deregister(string $addon_name = '') |
|
115 | - { |
|
116 | - unset(self::$_ee_admin_page_registry[ $addon_name ]); |
|
117 | - } |
|
105 | + /** |
|
106 | + * This deregisters a EE_Admin page that is already registered. Note, this MUST be loaded after the |
|
107 | + * page being deregistered is loaded. |
|
108 | + * |
|
109 | + * @param string $addon_name Use whatever string was used to register the admin page. |
|
110 | + * @return void |
|
111 | + * @since 4.3.0 |
|
112 | + * |
|
113 | + */ |
|
114 | + public static function deregister(string $addon_name = '') |
|
115 | + { |
|
116 | + unset(self::$_ee_admin_page_registry[ $addon_name ]); |
|
117 | + } |
|
118 | 118 | |
119 | 119 | |
120 | - /** |
|
121 | - * set_page_path |
|
122 | - * |
|
123 | - * @param array $paths |
|
124 | - * @return mixed |
|
125 | - * @throws EE_Error |
|
126 | - */ |
|
127 | - public static function set_page_path(array $paths): array |
|
128 | - { |
|
129 | - foreach (self::$_ee_admin_page_registry as $basename => $args) { |
|
130 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder($args['page_path']); |
|
131 | - $paths[ $basename ] = $args['page_path']; |
|
132 | - } |
|
133 | - return $paths; |
|
134 | - } |
|
120 | + /** |
|
121 | + * set_page_path |
|
122 | + * |
|
123 | + * @param array $paths |
|
124 | + * @return mixed |
|
125 | + * @throws EE_Error |
|
126 | + */ |
|
127 | + public static function set_page_path(array $paths): array |
|
128 | + { |
|
129 | + foreach (self::$_ee_admin_page_registry as $basename => $args) { |
|
130 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder($args['page_path']); |
|
131 | + $paths[ $basename ] = $args['page_path']; |
|
132 | + } |
|
133 | + return $paths; |
|
134 | + } |
|
135 | 135 | |
136 | - public static function reset(): void |
|
137 | - { |
|
138 | - self::$_ee_admin_page_registry = []; |
|
139 | - } |
|
136 | + public static function reset(): void |
|
137 | + { |
|
138 | + self::$_ee_admin_page_registry = []; |
|
139 | + } |
|
140 | 140 | } |
@@ -10,339 +10,339 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Register_Messages_Template_Variations implements EEI_Plugin_API |
12 | 12 | { |
13 | - /** |
|
14 | - * Holds values for registered variations |
|
15 | - * |
|
16 | - * @since 4.5.0 |
|
17 | - * |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - protected static $_registry = []; |
|
13 | + /** |
|
14 | + * Holds values for registered variations |
|
15 | + * |
|
16 | + * @since 4.5.0 |
|
17 | + * |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + protected static $_registry = []; |
|
21 | 21 | |
22 | 22 | |
23 | - /** |
|
24 | - * Used to register new variations |
|
25 | - * |
|
26 | - * Variations are attached to template packs and do not typically change any structural layout but merely tweak the |
|
27 | - * style of the layout. The most commonly known variation is css. CSS does not affect html structure just the |
|
28 | - * style of existing structure. |
|
29 | - * |
|
30 | - * It's important to remember that when variation files are loaded, the file structure looked for is: |
|
31 | - * '{$messenger}_{$messenger_variation_type}_{$variation_slug}.{$extension}'. |
|
32 | - * |
|
33 | - * - Every variation applies to specific messengers. That's why the variation file includes the messenger name |
|
34 | - * it. This ensures that if a template pack the variation is registered with supports multiple variations that |
|
35 | - * you can have the correct variation loaded. |
|
36 | - * - EE_messengers also implicitly define variation "types" which typically are the context in which a specific |
|
37 | - * variation is loaded. For instance the email messenger has: 'inline', which is the css added inline to the |
|
38 | - * email templates; 'preview', which is the same css only customized for when emails are previewed; and |
|
39 | - * 'wpeditor', which is the same css only customized so that it works with the wpeditor fields for templates to |
|
40 | - * give a accurate representation of the style in the wysiwyg editor. This means that for each variation, if |
|
41 | - * you want it to be accurately represented in various template contexts you need to have that relevant |
|
42 | - * variation file available. |
|
43 | - * - $variation_slug is simply the variation slug for that variation. |
|
44 | - * - $extension = whatever the extension is for the variation used for the messenger calling it. In MOST cases |
|
45 | - * messenger variations are .css files. Note: if your file names are not formatted correctly then they will NOT |
|
46 | - * be loaded. The EE messages template pack system will fallback to corresponding default template pack for the |
|
47 | - * given messenger or as a last resort (i.e. no default variation for the given messenger) will not load any |
|
48 | - * variation (so the template pack would be unstyled) |
|
49 | - * |
|
50 | - * @see /core/libraries/messages/defaults/default/variations/* for example variation files for the email and html |
|
51 | - * messengers. |
|
52 | - * |
|
53 | - * @param string $addon_name unique reference used to describe this variation registry. If |
|
54 | - * this ISN'T unique then this method will make it unique (and it |
|
55 | - * becomes harder to deregister). |
|
56 | - * @param array $setup_args { |
|
57 | - * an array of required values for registering the variations. |
|
58 | - * @type array $variations { |
|
59 | - * An array indexed by template_pack->dbref. and values are an array |
|
60 | - * indexed by messenger name and values are an array indexed by |
|
61 | - * message_type and values are an array indexed by variation_slug |
|
62 | - * and value is the localized label for the variation. Note this |
|
63 | - * api reserves the "default" variation name for the default |
|
64 | - * template pack so you can't register a default variation. Also, |
|
65 | - * try to use unique variation slugs to reference your variations |
|
66 | - * because this api checks if any existing variations are in place |
|
67 | - * with that name. If there are then subsequent variations for that |
|
68 | - * template pack with that same name will fail to register with a |
|
69 | - * persistent notice put up for the user. Required. |
|
70 | - * 'default' => array( |
|
71 | - * 'email' => array( |
|
72 | - * 'registration_approved' => array( |
|
73 | - * my_ee_addon_blue_lagoon' => esc_html__('Blue Lagoon', |
|
74 | - * 'text_domain'), |
|
75 | - * 'my_ee_addon_red_sunset' => esc_html__('Red Sunset', |
|
76 | - * 'text_domain') |
|
77 | - * ) |
|
78 | - * ) |
|
79 | - * ) |
|
80 | - * } |
|
81 | - * @type string $base_path The base path for where all your variations are found. Although |
|
82 | - * the full path to your variation files should include |
|
83 | - * '/variations/' in it, do not include the |
|
84 | - * 'variations/' in this. Required. |
|
85 | - * @type string $base_url The base url for where all your variations are found. See note |
|
86 | - * above about the 'variations/' string. Required. |
|
87 | - * } |
|
88 | - * } |
|
89 | - * |
|
90 | - * @throws EE_Error |
|
91 | - * @return bool |
|
92 | - */ |
|
93 | - public static function register(string $addon_name = '', array $setup_args = []): bool |
|
94 | - { |
|
23 | + /** |
|
24 | + * Used to register new variations |
|
25 | + * |
|
26 | + * Variations are attached to template packs and do not typically change any structural layout but merely tweak the |
|
27 | + * style of the layout. The most commonly known variation is css. CSS does not affect html structure just the |
|
28 | + * style of existing structure. |
|
29 | + * |
|
30 | + * It's important to remember that when variation files are loaded, the file structure looked for is: |
|
31 | + * '{$messenger}_{$messenger_variation_type}_{$variation_slug}.{$extension}'. |
|
32 | + * |
|
33 | + * - Every variation applies to specific messengers. That's why the variation file includes the messenger name |
|
34 | + * it. This ensures that if a template pack the variation is registered with supports multiple variations that |
|
35 | + * you can have the correct variation loaded. |
|
36 | + * - EE_messengers also implicitly define variation "types" which typically are the context in which a specific |
|
37 | + * variation is loaded. For instance the email messenger has: 'inline', which is the css added inline to the |
|
38 | + * email templates; 'preview', which is the same css only customized for when emails are previewed; and |
|
39 | + * 'wpeditor', which is the same css only customized so that it works with the wpeditor fields for templates to |
|
40 | + * give a accurate representation of the style in the wysiwyg editor. This means that for each variation, if |
|
41 | + * you want it to be accurately represented in various template contexts you need to have that relevant |
|
42 | + * variation file available. |
|
43 | + * - $variation_slug is simply the variation slug for that variation. |
|
44 | + * - $extension = whatever the extension is for the variation used for the messenger calling it. In MOST cases |
|
45 | + * messenger variations are .css files. Note: if your file names are not formatted correctly then they will NOT |
|
46 | + * be loaded. The EE messages template pack system will fallback to corresponding default template pack for the |
|
47 | + * given messenger or as a last resort (i.e. no default variation for the given messenger) will not load any |
|
48 | + * variation (so the template pack would be unstyled) |
|
49 | + * |
|
50 | + * @see /core/libraries/messages/defaults/default/variations/* for example variation files for the email and html |
|
51 | + * messengers. |
|
52 | + * |
|
53 | + * @param string $addon_name unique reference used to describe this variation registry. If |
|
54 | + * this ISN'T unique then this method will make it unique (and it |
|
55 | + * becomes harder to deregister). |
|
56 | + * @param array $setup_args { |
|
57 | + * an array of required values for registering the variations. |
|
58 | + * @type array $variations { |
|
59 | + * An array indexed by template_pack->dbref. and values are an array |
|
60 | + * indexed by messenger name and values are an array indexed by |
|
61 | + * message_type and values are an array indexed by variation_slug |
|
62 | + * and value is the localized label for the variation. Note this |
|
63 | + * api reserves the "default" variation name for the default |
|
64 | + * template pack so you can't register a default variation. Also, |
|
65 | + * try to use unique variation slugs to reference your variations |
|
66 | + * because this api checks if any existing variations are in place |
|
67 | + * with that name. If there are then subsequent variations for that |
|
68 | + * template pack with that same name will fail to register with a |
|
69 | + * persistent notice put up for the user. Required. |
|
70 | + * 'default' => array( |
|
71 | + * 'email' => array( |
|
72 | + * 'registration_approved' => array( |
|
73 | + * my_ee_addon_blue_lagoon' => esc_html__('Blue Lagoon', |
|
74 | + * 'text_domain'), |
|
75 | + * 'my_ee_addon_red_sunset' => esc_html__('Red Sunset', |
|
76 | + * 'text_domain') |
|
77 | + * ) |
|
78 | + * ) |
|
79 | + * ) |
|
80 | + * } |
|
81 | + * @type string $base_path The base path for where all your variations are found. Although |
|
82 | + * the full path to your variation files should include |
|
83 | + * '/variations/' in it, do not include the |
|
84 | + * 'variations/' in this. Required. |
|
85 | + * @type string $base_url The base url for where all your variations are found. See note |
|
86 | + * above about the 'variations/' string. Required. |
|
87 | + * } |
|
88 | + * } |
|
89 | + * |
|
90 | + * @throws EE_Error |
|
91 | + * @return bool |
|
92 | + */ |
|
93 | + public static function register(string $addon_name = '', array $setup_args = []): bool |
|
94 | + { |
|
95 | 95 | |
96 | - // check for required params |
|
97 | - if (empty($addon_name)) { |
|
98 | - throw new EE_Error( |
|
99 | - esc_html__( |
|
100 | - 'In order to register variations for a EE_Message_Template_Pack, you must include a value to reference the variations being registered', |
|
101 | - 'event_espresso' |
|
102 | - ) |
|
103 | - ); |
|
104 | - } |
|
96 | + // check for required params |
|
97 | + if (empty($addon_name)) { |
|
98 | + throw new EE_Error( |
|
99 | + esc_html__( |
|
100 | + 'In order to register variations for a EE_Message_Template_Pack, you must include a value to reference the variations being registered', |
|
101 | + 'event_espresso' |
|
102 | + ) |
|
103 | + ); |
|
104 | + } |
|
105 | 105 | |
106 | - if ( |
|
107 | - ! is_array($setup_args) |
|
108 | - || empty($setup_args['variations']) |
|
109 | - || empty($setup_args['base_path']) |
|
110 | - || empty($setup_args['base_url']) |
|
111 | - ) { |
|
112 | - throw new EE_Error( |
|
113 | - esc_html__( |
|
114 | - 'In order to register variations for a EE_Message_Template_Pack, you must include an array containing the following keys: "variations", "base_path", "base_url", "extension"', |
|
115 | - 'event_espresso' |
|
116 | - ) |
|
117 | - ); |
|
118 | - } |
|
106 | + if ( |
|
107 | + ! is_array($setup_args) |
|
108 | + || empty($setup_args['variations']) |
|
109 | + || empty($setup_args['base_path']) |
|
110 | + || empty($setup_args['base_url']) |
|
111 | + ) { |
|
112 | + throw new EE_Error( |
|
113 | + esc_html__( |
|
114 | + 'In order to register variations for a EE_Message_Template_Pack, you must include an array containing the following keys: "variations", "base_path", "base_url", "extension"', |
|
115 | + 'event_espresso' |
|
116 | + ) |
|
117 | + ); |
|
118 | + } |
|
119 | 119 | |
120 | - // make sure we don't register twice |
|
121 | - if (isset(self::$_registry[ $addon_name ])) { |
|
122 | - return true; |
|
123 | - } |
|
120 | + // make sure we don't register twice |
|
121 | + if (isset(self::$_registry[ $addon_name ])) { |
|
122 | + return true; |
|
123 | + } |
|
124 | 124 | |
125 | - // make sure variation ref is unique. |
|
126 | - if (isset(self::$_registry[ $addon_name ])) { |
|
127 | - $addon_name = uniqid() . '_' . $addon_name; |
|
128 | - } |
|
125 | + // make sure variation ref is unique. |
|
126 | + if (isset(self::$_registry[ $addon_name ])) { |
|
127 | + $addon_name = uniqid() . '_' . $addon_name; |
|
128 | + } |
|
129 | 129 | |
130 | 130 | |
131 | - // make sure this was called in the right place! |
|
132 | - if ( |
|
133 | - ! did_action('EE_Brewing_Regular___messages_caf') |
|
134 | - || did_action('AHEE__EE_System__perform_activations_upgrades_and_migrations') |
|
135 | - ) { |
|
136 | - EE_Error::doing_it_wrong( |
|
137 | - __METHOD__, |
|
138 | - sprintf( |
|
139 | - esc_html__( |
|
140 | - 'Messages Templates Variations given the reference "%s" has been attempted to be registered with the EE Messages Template Pack System. It may or may not work because it should be only called on the "EE_Brewing_Regular__messages_caf" hook.', |
|
141 | - 'event_espresso' |
|
142 | - ), |
|
143 | - $addon_name |
|
144 | - ), |
|
145 | - '4.5.0' |
|
146 | - ); |
|
147 | - } |
|
131 | + // make sure this was called in the right place! |
|
132 | + if ( |
|
133 | + ! did_action('EE_Brewing_Regular___messages_caf') |
|
134 | + || did_action('AHEE__EE_System__perform_activations_upgrades_and_migrations') |
|
135 | + ) { |
|
136 | + EE_Error::doing_it_wrong( |
|
137 | + __METHOD__, |
|
138 | + sprintf( |
|
139 | + esc_html__( |
|
140 | + 'Messages Templates Variations given the reference "%s" has been attempted to be registered with the EE Messages Template Pack System. It may or may not work because it should be only called on the "EE_Brewing_Regular__messages_caf" hook.', |
|
141 | + 'event_espresso' |
|
142 | + ), |
|
143 | + $addon_name |
|
144 | + ), |
|
145 | + '4.5.0' |
|
146 | + ); |
|
147 | + } |
|
148 | 148 | |
149 | - // validate/sanitize incoming args. |
|
150 | - $validated = [ |
|
151 | - 'variations' => (array) $setup_args['variations'], |
|
152 | - 'base_path' => (string) $setup_args['base_path'], |
|
153 | - 'base_url' => (string) $setup_args['base_url'], |
|
154 | - ]; |
|
149 | + // validate/sanitize incoming args. |
|
150 | + $validated = [ |
|
151 | + 'variations' => (array) $setup_args['variations'], |
|
152 | + 'base_path' => (string) $setup_args['base_path'], |
|
153 | + 'base_url' => (string) $setup_args['base_url'], |
|
154 | + ]; |
|
155 | 155 | |
156 | 156 | |
157 | - // check that no reserved variation names are in use and also checks if there are already existing variation names for a given template pack. The former will throw an error. The latter will remove the conflicting variation name but still register the others and will add EE_Error notice. |
|
158 | - $validated = self::_verify_variations($addon_name, $validated); |
|
159 | - self::$_registry[ $addon_name ] = $validated; |
|
157 | + // check that no reserved variation names are in use and also checks if there are already existing variation names for a given template pack. The former will throw an error. The latter will remove the conflicting variation name but still register the others and will add EE_Error notice. |
|
158 | + $validated = self::_verify_variations($addon_name, $validated); |
|
159 | + self::$_registry[ $addon_name ] = $validated; |
|
160 | 160 | |
161 | - add_filter( |
|
162 | - 'FHEE__EE_Messages_Template_Pack__get_variations', |
|
163 | - ['EE_Register_Messages_Template_Variations', 'get_variations'], |
|
164 | - 10, |
|
165 | - 4 |
|
166 | - ); |
|
167 | - add_filter( |
|
168 | - 'FHEE__EE_Messages_Template_Pack__get_variation', |
|
169 | - ['EE_Register_Messages_Template_Variations', 'get_variation'], |
|
170 | - 10, |
|
171 | - 8 |
|
172 | - ); |
|
173 | - return true; |
|
174 | - } |
|
161 | + add_filter( |
|
162 | + 'FHEE__EE_Messages_Template_Pack__get_variations', |
|
163 | + ['EE_Register_Messages_Template_Variations', 'get_variations'], |
|
164 | + 10, |
|
165 | + 4 |
|
166 | + ); |
|
167 | + add_filter( |
|
168 | + 'FHEE__EE_Messages_Template_Pack__get_variation', |
|
169 | + ['EE_Register_Messages_Template_Variations', 'get_variation'], |
|
170 | + 10, |
|
171 | + 8 |
|
172 | + ); |
|
173 | + return true; |
|
174 | + } |
|
175 | 175 | |
176 | 176 | |
177 | - /** |
|
178 | - * Cycles through the variations registered and makes sure there are no reserved variations being registered which |
|
179 | - * throws an error. Also checks if there is already a |
|
180 | - * |
|
181 | - * @param string $addon_name the reference for the variations being registered |
|
182 | - * @param array $validated_variations The variations setup array that's being registered (and verified). |
|
183 | - * @return array |
|
184 | - * @throws EE_Error |
|
185 | - * @since 4.5.0 |
|
186 | - * |
|
187 | - */ |
|
188 | - private static function _verify_variations(string $addon_name, array $validated_variations): array |
|
189 | - { |
|
190 | - foreach (self::$_registry as $settings) { |
|
191 | - foreach ($settings['variations'] as $messenger) { |
|
192 | - foreach ($messenger as $all_variations) { |
|
193 | - if (isset($all_variations['default'])) { |
|
194 | - throw new EE_Error( |
|
195 | - sprintf( |
|
196 | - esc_html__( |
|
197 | - 'Variations registered through the EE_Register_Messages_Template_Variations api cannot override the default variation for the default template. Please check the code registering variations with this reference, "%s" and modify.', |
|
198 | - 'event_espresso' |
|
199 | - ), |
|
200 | - $addon_name |
|
201 | - ) |
|
202 | - ); |
|
203 | - } |
|
204 | - } |
|
205 | - } |
|
206 | - } |
|
177 | + /** |
|
178 | + * Cycles through the variations registered and makes sure there are no reserved variations being registered which |
|
179 | + * throws an error. Also checks if there is already a |
|
180 | + * |
|
181 | + * @param string $addon_name the reference for the variations being registered |
|
182 | + * @param array $validated_variations The variations setup array that's being registered (and verified). |
|
183 | + * @return array |
|
184 | + * @throws EE_Error |
|
185 | + * @since 4.5.0 |
|
186 | + * |
|
187 | + */ |
|
188 | + private static function _verify_variations(string $addon_name, array $validated_variations): array |
|
189 | + { |
|
190 | + foreach (self::$_registry as $settings) { |
|
191 | + foreach ($settings['variations'] as $messenger) { |
|
192 | + foreach ($messenger as $all_variations) { |
|
193 | + if (isset($all_variations['default'])) { |
|
194 | + throw new EE_Error( |
|
195 | + sprintf( |
|
196 | + esc_html__( |
|
197 | + 'Variations registered through the EE_Register_Messages_Template_Variations api cannot override the default variation for the default template. Please check the code registering variations with this reference, "%s" and modify.', |
|
198 | + 'event_espresso' |
|
199 | + ), |
|
200 | + $addon_name |
|
201 | + ) |
|
202 | + ); |
|
203 | + } |
|
204 | + } |
|
205 | + } |
|
206 | + } |
|
207 | 207 | |
208 | - // is there already a variation registered with a given variation slug? |
|
209 | - foreach ($validated_variations['variations'] as $template_pack => $messenger) { |
|
210 | - foreach ($messenger as $message_type => $variations) { |
|
211 | - foreach ($variations as $slug => $label) { |
|
212 | - foreach (self::$_registry as $registered_var => $reg_settings) { |
|
213 | - if (isset($reg_settings['variations'][ $template_pack ][ $messenger ][ $message_type ][ $slug ])) { |
|
214 | - unset($validated_variations['variations'][ $template_pack ][ $messenger ][ $message_type ][ $slug ]); |
|
215 | - EE_Error::add_error( |
|
216 | - sprintf( |
|
217 | - esc_html__( |
|
218 | - 'Unable to register the %s variation for the %s template pack with the %s messenger and %s message_type because a variation with this slug was already registered for this template pack and messenger and message type by an addon using this key %s.', |
|
219 | - 'event_espresso' |
|
220 | - ), |
|
221 | - $label, |
|
222 | - $template_pack, |
|
223 | - $messenger, |
|
224 | - $message_type, |
|
225 | - $registered_var |
|
226 | - ) |
|
227 | - ); |
|
228 | - } |
|
229 | - } |
|
230 | - } |
|
231 | - } |
|
232 | - } |
|
233 | - return $validated_variations; |
|
234 | - } |
|
208 | + // is there already a variation registered with a given variation slug? |
|
209 | + foreach ($validated_variations['variations'] as $template_pack => $messenger) { |
|
210 | + foreach ($messenger as $message_type => $variations) { |
|
211 | + foreach ($variations as $slug => $label) { |
|
212 | + foreach (self::$_registry as $registered_var => $reg_settings) { |
|
213 | + if (isset($reg_settings['variations'][ $template_pack ][ $messenger ][ $message_type ][ $slug ])) { |
|
214 | + unset($validated_variations['variations'][ $template_pack ][ $messenger ][ $message_type ][ $slug ]); |
|
215 | + EE_Error::add_error( |
|
216 | + sprintf( |
|
217 | + esc_html__( |
|
218 | + 'Unable to register the %s variation for the %s template pack with the %s messenger and %s message_type because a variation with this slug was already registered for this template pack and messenger and message type by an addon using this key %s.', |
|
219 | + 'event_espresso' |
|
220 | + ), |
|
221 | + $label, |
|
222 | + $template_pack, |
|
223 | + $messenger, |
|
224 | + $message_type, |
|
225 | + $registered_var |
|
226 | + ) |
|
227 | + ); |
|
228 | + } |
|
229 | + } |
|
230 | + } |
|
231 | + } |
|
232 | + } |
|
233 | + return $validated_variations; |
|
234 | + } |
|
235 | 235 | |
236 | 236 | |
237 | - /** |
|
238 | - * Callback for the FHEE__EE_Messages_Template_Pack__get_variation filter to ensure registered variations are used. |
|
239 | - * |
|
240 | - * @param string $variation_path The path generated for the current variation |
|
241 | - * @param string $messenger The messenger the variation is for |
|
242 | - * @param string $message_type EE_message_type->name |
|
243 | - * @param string $type The type of variation being requested |
|
244 | - * @param string $variation The slug for the variation being requested |
|
245 | - * @param string $file_extension What the file extension is for the variation |
|
246 | - * @param bool $url Whether url or path is being returned. |
|
247 | - * @param EE_Messages_Template_Pack $template_pack |
|
248 | - * |
|
249 | - * @return string The path to the requested variation. |
|
250 | - * @since 4.5.0 |
|
251 | - * |
|
252 | - */ |
|
253 | - public static function get_variation( |
|
254 | - string $variation_path, |
|
255 | - string $messenger, |
|
256 | - string $message_type, |
|
257 | - string $type, |
|
258 | - string $variation, |
|
259 | - string $file_extension, |
|
260 | - bool $url, |
|
261 | - EE_Messages_Template_Pack $template_pack |
|
262 | - ): string { |
|
263 | - // so let's loop through our registered variations and then pull any details matching the request. |
|
264 | - foreach (self::$_registry as $registry_settings) { |
|
265 | - $base = $url ? $registry_settings['base_url'] : $registry_settings['base_path']; |
|
266 | - $file_string = $messenger . '_' . $type . '_' . $variation . $file_extension; |
|
267 | - // see if this file exists |
|
268 | - if (is_readable($registry_settings['base_path'] . $file_string)) { |
|
269 | - return $base . $file_string; |
|
270 | - } |
|
271 | - } |
|
237 | + /** |
|
238 | + * Callback for the FHEE__EE_Messages_Template_Pack__get_variation filter to ensure registered variations are used. |
|
239 | + * |
|
240 | + * @param string $variation_path The path generated for the current variation |
|
241 | + * @param string $messenger The messenger the variation is for |
|
242 | + * @param string $message_type EE_message_type->name |
|
243 | + * @param string $type The type of variation being requested |
|
244 | + * @param string $variation The slug for the variation being requested |
|
245 | + * @param string $file_extension What the file extension is for the variation |
|
246 | + * @param bool $url Whether url or path is being returned. |
|
247 | + * @param EE_Messages_Template_Pack $template_pack |
|
248 | + * |
|
249 | + * @return string The path to the requested variation. |
|
250 | + * @since 4.5.0 |
|
251 | + * |
|
252 | + */ |
|
253 | + public static function get_variation( |
|
254 | + string $variation_path, |
|
255 | + string $messenger, |
|
256 | + string $message_type, |
|
257 | + string $type, |
|
258 | + string $variation, |
|
259 | + string $file_extension, |
|
260 | + bool $url, |
|
261 | + EE_Messages_Template_Pack $template_pack |
|
262 | + ): string { |
|
263 | + // so let's loop through our registered variations and then pull any details matching the request. |
|
264 | + foreach (self::$_registry as $registry_settings) { |
|
265 | + $base = $url ? $registry_settings['base_url'] : $registry_settings['base_path']; |
|
266 | + $file_string = $messenger . '_' . $type . '_' . $variation . $file_extension; |
|
267 | + // see if this file exists |
|
268 | + if (is_readable($registry_settings['base_path'] . $file_string)) { |
|
269 | + return $base . $file_string; |
|
270 | + } |
|
271 | + } |
|
272 | 272 | |
273 | - // no match |
|
274 | - return $variation_path; |
|
275 | - } |
|
273 | + // no match |
|
274 | + return $variation_path; |
|
275 | + } |
|
276 | 276 | |
277 | 277 | |
278 | - /** |
|
279 | - * callback for the FHEE__EE_Messages_Template_Pack__get_variations filter. |
|
280 | - * |
|
281 | - * |
|
282 | - * @param array $variations The original contents for the template pack variations property. |
|
283 | - * @param string $messenger The messenger requesting the variations. |
|
284 | - * @param string $message_type |
|
285 | - * @param EE_Messages_Template_Pack $template_pack |
|
286 | - * |
|
287 | - * @return array new variations array (or existing one if nothing registered) |
|
288 | - * @since 4.5.0 |
|
289 | - * |
|
290 | - * @see $_variation property definition in EE_Messages_Template_Pack |
|
291 | - */ |
|
292 | - public static function get_variations( |
|
293 | - array $variations, |
|
294 | - string $messenger, |
|
295 | - string $message_type, |
|
296 | - EE_Messages_Template_Pack $template_pack |
|
297 | - ): array { |
|
298 | - // first let's check if we even have registered variations and get out early. |
|
299 | - if (empty(self::$_registry)) { |
|
300 | - return $variations; |
|
301 | - } |
|
302 | - $template_variations = []; |
|
303 | - // do we have any new variations for the given messenger, $message_type, and template packs |
|
304 | - foreach (self::$_registry as $registry_settings) { |
|
305 | - // allow for different conditions. |
|
306 | - if (empty($messenger)) { |
|
307 | - return array_merge($registry_settings['variations'], $variations); |
|
308 | - } |
|
309 | - if (empty($message_type)) { |
|
310 | - if (! empty($registry_settings['variations'][ $template_pack->dbref ][ $messenger ])) { |
|
311 | - return array_merge( |
|
312 | - $registry_settings['variations'][ $template_pack->dbref ][ $messenger ], |
|
313 | - $variations |
|
314 | - ); |
|
315 | - } |
|
316 | - } else { |
|
317 | - if (! empty($registry_settings['variations'][ $template_pack->dbref ][ $messenger ][ $message_type ])) { |
|
318 | - return array_merge( |
|
319 | - $registry_settings['variations'][ $template_pack->dbref ][ $messenger ][ $message_type ], |
|
320 | - $variations |
|
321 | - ); |
|
322 | - } |
|
323 | - } |
|
324 | - } |
|
325 | - return array_merge($variations, ...$template_variations); |
|
326 | - } |
|
278 | + /** |
|
279 | + * callback for the FHEE__EE_Messages_Template_Pack__get_variations filter. |
|
280 | + * |
|
281 | + * |
|
282 | + * @param array $variations The original contents for the template pack variations property. |
|
283 | + * @param string $messenger The messenger requesting the variations. |
|
284 | + * @param string $message_type |
|
285 | + * @param EE_Messages_Template_Pack $template_pack |
|
286 | + * |
|
287 | + * @return array new variations array (or existing one if nothing registered) |
|
288 | + * @since 4.5.0 |
|
289 | + * |
|
290 | + * @see $_variation property definition in EE_Messages_Template_Pack |
|
291 | + */ |
|
292 | + public static function get_variations( |
|
293 | + array $variations, |
|
294 | + string $messenger, |
|
295 | + string $message_type, |
|
296 | + EE_Messages_Template_Pack $template_pack |
|
297 | + ): array { |
|
298 | + // first let's check if we even have registered variations and get out early. |
|
299 | + if (empty(self::$_registry)) { |
|
300 | + return $variations; |
|
301 | + } |
|
302 | + $template_variations = []; |
|
303 | + // do we have any new variations for the given messenger, $message_type, and template packs |
|
304 | + foreach (self::$_registry as $registry_settings) { |
|
305 | + // allow for different conditions. |
|
306 | + if (empty($messenger)) { |
|
307 | + return array_merge($registry_settings['variations'], $variations); |
|
308 | + } |
|
309 | + if (empty($message_type)) { |
|
310 | + if (! empty($registry_settings['variations'][ $template_pack->dbref ][ $messenger ])) { |
|
311 | + return array_merge( |
|
312 | + $registry_settings['variations'][ $template_pack->dbref ][ $messenger ], |
|
313 | + $variations |
|
314 | + ); |
|
315 | + } |
|
316 | + } else { |
|
317 | + if (! empty($registry_settings['variations'][ $template_pack->dbref ][ $messenger ][ $message_type ])) { |
|
318 | + return array_merge( |
|
319 | + $registry_settings['variations'][ $template_pack->dbref ][ $messenger ][ $message_type ], |
|
320 | + $variations |
|
321 | + ); |
|
322 | + } |
|
323 | + } |
|
324 | + } |
|
325 | + return array_merge($variations, ...$template_variations); |
|
326 | + } |
|
327 | 327 | |
328 | 328 | |
329 | - /** |
|
330 | - * This deregisters a variation set that was previously registered with the given slug. |
|
331 | - * |
|
332 | - * @param string $addon_name The name for the variation set that was previously registered. |
|
333 | - * |
|
334 | - * @return void |
|
335 | - * @since 4.5.0 |
|
336 | - * |
|
337 | - */ |
|
338 | - public static function deregister(string $addon_name = '') |
|
339 | - { |
|
340 | - unset(self::$_registry[ $addon_name ]); |
|
341 | - } |
|
329 | + /** |
|
330 | + * This deregisters a variation set that was previously registered with the given slug. |
|
331 | + * |
|
332 | + * @param string $addon_name The name for the variation set that was previously registered. |
|
333 | + * |
|
334 | + * @return void |
|
335 | + * @since 4.5.0 |
|
336 | + * |
|
337 | + */ |
|
338 | + public static function deregister(string $addon_name = '') |
|
339 | + { |
|
340 | + unset(self::$_registry[ $addon_name ]); |
|
341 | + } |
|
342 | 342 | |
343 | 343 | |
344 | - public static function reset(): void |
|
345 | - { |
|
346 | - self::$_registry = []; |
|
347 | - } |
|
344 | + public static function reset(): void |
|
345 | + { |
|
346 | + self::$_registry = []; |
|
347 | + } |
|
348 | 348 | } |
@@ -118,13 +118,13 @@ discard block |
||
118 | 118 | } |
119 | 119 | |
120 | 120 | // make sure we don't register twice |
121 | - if (isset(self::$_registry[ $addon_name ])) { |
|
121 | + if (isset(self::$_registry[$addon_name])) { |
|
122 | 122 | return true; |
123 | 123 | } |
124 | 124 | |
125 | 125 | // make sure variation ref is unique. |
126 | - if (isset(self::$_registry[ $addon_name ])) { |
|
127 | - $addon_name = uniqid() . '_' . $addon_name; |
|
126 | + if (isset(self::$_registry[$addon_name])) { |
|
127 | + $addon_name = uniqid().'_'.$addon_name; |
|
128 | 128 | } |
129 | 129 | |
130 | 130 | |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | |
157 | 157 | // check that no reserved variation names are in use and also checks if there are already existing variation names for a given template pack. The former will throw an error. The latter will remove the conflicting variation name but still register the others and will add EE_Error notice. |
158 | 158 | $validated = self::_verify_variations($addon_name, $validated); |
159 | - self::$_registry[ $addon_name ] = $validated; |
|
159 | + self::$_registry[$addon_name] = $validated; |
|
160 | 160 | |
161 | 161 | add_filter( |
162 | 162 | 'FHEE__EE_Messages_Template_Pack__get_variations', |
@@ -210,8 +210,8 @@ discard block |
||
210 | 210 | foreach ($messenger as $message_type => $variations) { |
211 | 211 | foreach ($variations as $slug => $label) { |
212 | 212 | foreach (self::$_registry as $registered_var => $reg_settings) { |
213 | - if (isset($reg_settings['variations'][ $template_pack ][ $messenger ][ $message_type ][ $slug ])) { |
|
214 | - unset($validated_variations['variations'][ $template_pack ][ $messenger ][ $message_type ][ $slug ]); |
|
213 | + if (isset($reg_settings['variations'][$template_pack][$messenger][$message_type][$slug])) { |
|
214 | + unset($validated_variations['variations'][$template_pack][$messenger][$message_type][$slug]); |
|
215 | 215 | EE_Error::add_error( |
216 | 216 | sprintf( |
217 | 217 | esc_html__( |
@@ -263,10 +263,10 @@ discard block |
||
263 | 263 | // so let's loop through our registered variations and then pull any details matching the request. |
264 | 264 | foreach (self::$_registry as $registry_settings) { |
265 | 265 | $base = $url ? $registry_settings['base_url'] : $registry_settings['base_path']; |
266 | - $file_string = $messenger . '_' . $type . '_' . $variation . $file_extension; |
|
266 | + $file_string = $messenger.'_'.$type.'_'.$variation.$file_extension; |
|
267 | 267 | // see if this file exists |
268 | - if (is_readable($registry_settings['base_path'] . $file_string)) { |
|
269 | - return $base . $file_string; |
|
268 | + if (is_readable($registry_settings['base_path'].$file_string)) { |
|
269 | + return $base.$file_string; |
|
270 | 270 | } |
271 | 271 | } |
272 | 272 | |
@@ -307,16 +307,16 @@ discard block |
||
307 | 307 | return array_merge($registry_settings['variations'], $variations); |
308 | 308 | } |
309 | 309 | if (empty($message_type)) { |
310 | - if (! empty($registry_settings['variations'][ $template_pack->dbref ][ $messenger ])) { |
|
310 | + if ( ! empty($registry_settings['variations'][$template_pack->dbref][$messenger])) { |
|
311 | 311 | return array_merge( |
312 | - $registry_settings['variations'][ $template_pack->dbref ][ $messenger ], |
|
312 | + $registry_settings['variations'][$template_pack->dbref][$messenger], |
|
313 | 313 | $variations |
314 | 314 | ); |
315 | 315 | } |
316 | 316 | } else { |
317 | - if (! empty($registry_settings['variations'][ $template_pack->dbref ][ $messenger ][ $message_type ])) { |
|
317 | + if ( ! empty($registry_settings['variations'][$template_pack->dbref][$messenger][$message_type])) { |
|
318 | 318 | return array_merge( |
319 | - $registry_settings['variations'][ $template_pack->dbref ][ $messenger ][ $message_type ], |
|
319 | + $registry_settings['variations'][$template_pack->dbref][$messenger][$message_type], |
|
320 | 320 | $variations |
321 | 321 | ); |
322 | 322 | } |
@@ -337,7 +337,7 @@ discard block |
||
337 | 337 | */ |
338 | 338 | public static function deregister(string $addon_name = '') |
339 | 339 | { |
340 | - unset(self::$_registry[ $addon_name ]); |
|
340 | + unset(self::$_registry[$addon_name]); |
|
341 | 341 | } |
342 | 342 | |
343 | 343 |
@@ -14,104 +14,104 @@ |
||
14 | 14 | */ |
15 | 15 | class EE_Register_Module implements EEI_Plugin_API |
16 | 16 | { |
17 | - /** |
|
18 | - * Holds values for registered modules |
|
19 | - * |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - protected static $_settings = []; |
|
17 | + /** |
|
18 | + * Holds values for registered modules |
|
19 | + * |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + protected static $_settings = []; |
|
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * Method for registering new EED_Modules |
|
27 | - * |
|
28 | - * @param string $addon_name a unique identifier for this set of modules Required. |
|
29 | - * @param array $setup_args an array of full server paths to folders containing any EED_Modules, or to the |
|
30 | - * EED_Module files themselves Required. |
|
31 | - * @type array module_paths an array of full server paths to folders containing any EED_Modules, or to the |
|
32 | - * EED_Module files themselves |
|
33 | - * @return bool |
|
34 | - * @throws EE_Error |
|
35 | - * @since 4.3.0 |
|
36 | - */ |
|
37 | - public static function register(string $addon_name = '', array $setup_args = []): bool |
|
38 | - { |
|
39 | - // required fields MUST be present, so let's make sure they are. |
|
40 | - if (empty($addon_name) || ! is_array($setup_args) || empty($setup_args['module_paths'])) { |
|
41 | - throw new EE_Error( |
|
42 | - esc_html__( |
|
43 | - 'In order to register Modules with EE_Register_Module::register(), you must include a "module_id" (a unique identifier for this set of modules), and an array containing the following keys: "module_paths" (an array of full server paths to folders that contain modules, or to the module files themselves)', |
|
44 | - 'event_espresso' |
|
45 | - ) |
|
46 | - ); |
|
47 | - } |
|
25 | + /** |
|
26 | + * Method for registering new EED_Modules |
|
27 | + * |
|
28 | + * @param string $addon_name a unique identifier for this set of modules Required. |
|
29 | + * @param array $setup_args an array of full server paths to folders containing any EED_Modules, or to the |
|
30 | + * EED_Module files themselves Required. |
|
31 | + * @type array module_paths an array of full server paths to folders containing any EED_Modules, or to the |
|
32 | + * EED_Module files themselves |
|
33 | + * @return bool |
|
34 | + * @throws EE_Error |
|
35 | + * @since 4.3.0 |
|
36 | + */ |
|
37 | + public static function register(string $addon_name = '', array $setup_args = []): bool |
|
38 | + { |
|
39 | + // required fields MUST be present, so let's make sure they are. |
|
40 | + if (empty($addon_name) || ! is_array($setup_args) || empty($setup_args['module_paths'])) { |
|
41 | + throw new EE_Error( |
|
42 | + esc_html__( |
|
43 | + 'In order to register Modules with EE_Register_Module::register(), you must include a "module_id" (a unique identifier for this set of modules), and an array containing the following keys: "module_paths" (an array of full server paths to folders that contain modules, or to the module files themselves)', |
|
44 | + 'event_espresso' |
|
45 | + ) |
|
46 | + ); |
|
47 | + } |
|
48 | 48 | |
49 | - // make sure we don't register twice |
|
50 | - if (isset(self::$_settings[ $addon_name ])) { |
|
51 | - return true; |
|
52 | - } |
|
49 | + // make sure we don't register twice |
|
50 | + if (isset(self::$_settings[ $addon_name ])) { |
|
51 | + return true; |
|
52 | + } |
|
53 | 53 | |
54 | - // make sure this was called in the right place! |
|
55 | - if ( |
|
56 | - ! did_action('AHEE__EE_System__load_espresso_addons') |
|
57 | - || did_action('AHEE__EE_System__register_shortcodes_modules_and_widgets') |
|
58 | - ) { |
|
59 | - EE_Error::doing_it_wrong( |
|
60 | - __METHOD__, |
|
61 | - esc_html__( |
|
62 | - 'An attempt to register modules has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__register_shortcodes_modules_and_widgets" hook to register modules.', |
|
63 | - 'event_espresso' |
|
64 | - ), |
|
65 | - '4.3.0' |
|
66 | - ); |
|
67 | - } |
|
68 | - // setup $_settings array from incoming values. |
|
69 | - self::$_settings[ $addon_name ] = [ |
|
70 | - // array of full server paths to any EED_Modules used by the module |
|
71 | - 'module_paths' => isset($setup_args['module_paths']) ? (array) $setup_args['module_paths'] : [], |
|
72 | - ]; |
|
73 | - // add to list of modules to be registered |
|
74 | - add_filter( |
|
75 | - 'FHEE__EE_Config__register_modules__modules_to_register', |
|
76 | - ['EE_Register_Module', 'add_modules'] |
|
77 | - ); |
|
78 | - return true; |
|
79 | - } |
|
54 | + // make sure this was called in the right place! |
|
55 | + if ( |
|
56 | + ! did_action('AHEE__EE_System__load_espresso_addons') |
|
57 | + || did_action('AHEE__EE_System__register_shortcodes_modules_and_widgets') |
|
58 | + ) { |
|
59 | + EE_Error::doing_it_wrong( |
|
60 | + __METHOD__, |
|
61 | + esc_html__( |
|
62 | + 'An attempt to register modules has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__register_shortcodes_modules_and_widgets" hook to register modules.', |
|
63 | + 'event_espresso' |
|
64 | + ), |
|
65 | + '4.3.0' |
|
66 | + ); |
|
67 | + } |
|
68 | + // setup $_settings array from incoming values. |
|
69 | + self::$_settings[ $addon_name ] = [ |
|
70 | + // array of full server paths to any EED_Modules used by the module |
|
71 | + 'module_paths' => isset($setup_args['module_paths']) ? (array) $setup_args['module_paths'] : [], |
|
72 | + ]; |
|
73 | + // add to list of modules to be registered |
|
74 | + add_filter( |
|
75 | + 'FHEE__EE_Config__register_modules__modules_to_register', |
|
76 | + ['EE_Register_Module', 'add_modules'] |
|
77 | + ); |
|
78 | + return true; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | |
82 | - /** |
|
83 | - * Filters the list of modules to add ours. |
|
84 | - * and they're just full filepaths to FOLDERS containing a module class file. Eg. |
|
85 | - * array('espresso_monkey'=>'/public_html/wonder-site/wp-content/plugins/ee4/shortcodes/espresso_monkey'...) |
|
86 | - * |
|
87 | - * @param array $modules_to_register array of paths to all modules that require registering |
|
88 | - * @return array |
|
89 | - */ |
|
90 | - public static function add_modules(array $modules_to_register): array |
|
91 | - { |
|
92 | - $module_paths = []; |
|
93 | - foreach (self::$_settings as $settings) { |
|
94 | - $module_paths[] = $settings['module_paths']; |
|
95 | - } |
|
96 | - return array_merge($modules_to_register, ...$module_paths); |
|
97 | - } |
|
82 | + /** |
|
83 | + * Filters the list of modules to add ours. |
|
84 | + * and they're just full filepaths to FOLDERS containing a module class file. Eg. |
|
85 | + * array('espresso_monkey'=>'/public_html/wonder-site/wp-content/plugins/ee4/shortcodes/espresso_monkey'...) |
|
86 | + * |
|
87 | + * @param array $modules_to_register array of paths to all modules that require registering |
|
88 | + * @return array |
|
89 | + */ |
|
90 | + public static function add_modules(array $modules_to_register): array |
|
91 | + { |
|
92 | + $module_paths = []; |
|
93 | + foreach (self::$_settings as $settings) { |
|
94 | + $module_paths[] = $settings['module_paths']; |
|
95 | + } |
|
96 | + return array_merge($modules_to_register, ...$module_paths); |
|
97 | + } |
|
98 | 98 | |
99 | 99 | |
100 | - /** |
|
101 | - * This deregisters a module that was previously registered with a specific $addon_name. |
|
102 | - * |
|
103 | - * @param string $addon_name the name for the module that was previously registered |
|
104 | - * @return void |
|
105 | - * @since 4.3.0 |
|
106 | - */ |
|
107 | - public static function deregister(string $addon_name = '') |
|
108 | - { |
|
109 | - unset(self::$_settings[ $addon_name ]); |
|
110 | - } |
|
100 | + /** |
|
101 | + * This deregisters a module that was previously registered with a specific $addon_name. |
|
102 | + * |
|
103 | + * @param string $addon_name the name for the module that was previously registered |
|
104 | + * @return void |
|
105 | + * @since 4.3.0 |
|
106 | + */ |
|
107 | + public static function deregister(string $addon_name = '') |
|
108 | + { |
|
109 | + unset(self::$_settings[ $addon_name ]); |
|
110 | + } |
|
111 | 111 | |
112 | 112 | |
113 | - public static function reset(): void |
|
114 | - { |
|
115 | - self::$_settings = []; |
|
116 | - } |
|
113 | + public static function reset(): void |
|
114 | + { |
|
115 | + self::$_settings = []; |
|
116 | + } |
|
117 | 117 | } |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | } |
48 | 48 | |
49 | 49 | // make sure we don't register twice |
50 | - if (isset(self::$_settings[ $addon_name ])) { |
|
50 | + if (isset(self::$_settings[$addon_name])) { |
|
51 | 51 | return true; |
52 | 52 | } |
53 | 53 | |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | ); |
67 | 67 | } |
68 | 68 | // setup $_settings array from incoming values. |
69 | - self::$_settings[ $addon_name ] = [ |
|
69 | + self::$_settings[$addon_name] = [ |
|
70 | 70 | // array of full server paths to any EED_Modules used by the module |
71 | 71 | 'module_paths' => isset($setup_args['module_paths']) ? (array) $setup_args['module_paths'] : [], |
72 | 72 | ]; |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | */ |
107 | 107 | public static function deregister(string $addon_name = '') |
108 | 108 | { |
109 | - unset(self::$_settings[ $addon_name ]); |
|
109 | + unset(self::$_settings[$addon_name]); |
|
110 | 110 | } |
111 | 111 | |
112 | 112 |
@@ -14,111 +14,111 @@ |
||
14 | 14 | */ |
15 | 15 | class EE_Register_Data_Migration_Scripts implements EEI_Plugin_API |
16 | 16 | { |
17 | - /** |
|
18 | - * Holds values for registered DMSs |
|
19 | - * |
|
20 | - * @var array[][] |
|
21 | - */ |
|
22 | - protected static array $_settings = []; |
|
17 | + /** |
|
18 | + * Holds values for registered DMSs |
|
19 | + * |
|
20 | + * @var array[][] |
|
21 | + */ |
|
22 | + protected static array $_settings = []; |
|
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * Method for registering new Data Migration Scripts |
|
27 | - * |
|
28 | - * @param string $addon_name EE_Addon class name that this set of data migration scripts belongs to |
|
29 | - * If EE_Addon class is namespaced, then this needs to be the Fully Qualified Class Name |
|
30 | - * @param array $setup_args { |
|
31 | - * @type string $dms_paths an array of full server paths to folders that contain data migration scripts |
|
32 | - * } |
|
33 | - * @return bool |
|
34 | - * @throws EE_Error |
|
35 | - * @since 4.3.0 |
|
36 | - * @since 4.3.0 |
|
37 | - */ |
|
38 | - public static function register(string $addon_name = '', array $setup_args = []): bool |
|
39 | - { |
|
40 | - // required fields MUST be present, so let's make sure they are. |
|
41 | - if (empty($addon_name) || ! is_array($setup_args) || empty($setup_args['dms_paths'])) { |
|
42 | - throw new EE_Error( |
|
43 | - esc_html__( |
|
44 | - 'In order to register Data Migration Scripts with EE_Register_Data_Migration_Scripts::register(), you must include the EE_Addon class name (used as a unique identifier for this set of data migration scripts), and an array containing the following keys: "dms_paths" (an array of full server paths to folders that contain data migration scripts)', |
|
45 | - 'event_espresso' |
|
46 | - ) |
|
47 | - ); |
|
48 | - } |
|
49 | - // make sure we don't register twice |
|
50 | - if (isset(self::$_settings[ $addon_name ])) { |
|
51 | - return true; |
|
52 | - } |
|
53 | - // make sure this was called in the right place! |
|
54 | - if ( |
|
55 | - ! did_action('AHEE__EE_System__load_espresso_addons') |
|
56 | - || did_action('AHEE__EE_System___detect_if_activation_or_upgrade__begin') |
|
57 | - ) { |
|
58 | - EE_Error::doing_it_wrong( |
|
59 | - __METHOD__, |
|
60 | - esc_html__( |
|
61 | - 'An attempt to register Data Migration Scripts has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__load_espresso_addons" hook to register Data Migration Scripts.', |
|
62 | - 'event_espresso' |
|
63 | - ), |
|
64 | - '4.3.0' |
|
65 | - ); |
|
66 | - } |
|
67 | - // setup $_settings array from incoming values. |
|
68 | - self::$_settings[ $addon_name ] = ['dms_paths' => (array) $setup_args['dms_paths']]; |
|
69 | - // setup DMS |
|
70 | - $filters_set = has_filter( |
|
71 | - 'FHEE__EE_Data_Migration_Manager__get_data_migration_script_folders', |
|
72 | - ['EE_Register_Data_Migration_Scripts', 'add_data_migration_script_folders'] |
|
73 | - ); |
|
74 | - if (! $filters_set) { |
|
75 | - add_filter( |
|
76 | - 'FHEE__EE_Data_Migration_Manager__get_data_migration_script_folders', |
|
77 | - ['EE_Register_Data_Migration_Scripts', 'add_data_migration_script_folders'] |
|
78 | - ); |
|
79 | - } |
|
80 | - return true; |
|
81 | - } |
|
25 | + /** |
|
26 | + * Method for registering new Data Migration Scripts |
|
27 | + * |
|
28 | + * @param string $addon_name EE_Addon class name that this set of data migration scripts belongs to |
|
29 | + * If EE_Addon class is namespaced, then this needs to be the Fully Qualified Class Name |
|
30 | + * @param array $setup_args { |
|
31 | + * @type string $dms_paths an array of full server paths to folders that contain data migration scripts |
|
32 | + * } |
|
33 | + * @return bool |
|
34 | + * @throws EE_Error |
|
35 | + * @since 4.3.0 |
|
36 | + * @since 4.3.0 |
|
37 | + */ |
|
38 | + public static function register(string $addon_name = '', array $setup_args = []): bool |
|
39 | + { |
|
40 | + // required fields MUST be present, so let's make sure they are. |
|
41 | + if (empty($addon_name) || ! is_array($setup_args) || empty($setup_args['dms_paths'])) { |
|
42 | + throw new EE_Error( |
|
43 | + esc_html__( |
|
44 | + 'In order to register Data Migration Scripts with EE_Register_Data_Migration_Scripts::register(), you must include the EE_Addon class name (used as a unique identifier for this set of data migration scripts), and an array containing the following keys: "dms_paths" (an array of full server paths to folders that contain data migration scripts)', |
|
45 | + 'event_espresso' |
|
46 | + ) |
|
47 | + ); |
|
48 | + } |
|
49 | + // make sure we don't register twice |
|
50 | + if (isset(self::$_settings[ $addon_name ])) { |
|
51 | + return true; |
|
52 | + } |
|
53 | + // make sure this was called in the right place! |
|
54 | + if ( |
|
55 | + ! did_action('AHEE__EE_System__load_espresso_addons') |
|
56 | + || did_action('AHEE__EE_System___detect_if_activation_or_upgrade__begin') |
|
57 | + ) { |
|
58 | + EE_Error::doing_it_wrong( |
|
59 | + __METHOD__, |
|
60 | + esc_html__( |
|
61 | + 'An attempt to register Data Migration Scripts has failed because it was not registered at the correct time. Please use the "AHEE__EE_System__load_espresso_addons" hook to register Data Migration Scripts.', |
|
62 | + 'event_espresso' |
|
63 | + ), |
|
64 | + '4.3.0' |
|
65 | + ); |
|
66 | + } |
|
67 | + // setup $_settings array from incoming values. |
|
68 | + self::$_settings[ $addon_name ] = ['dms_paths' => (array) $setup_args['dms_paths']]; |
|
69 | + // setup DMS |
|
70 | + $filters_set = has_filter( |
|
71 | + 'FHEE__EE_Data_Migration_Manager__get_data_migration_script_folders', |
|
72 | + ['EE_Register_Data_Migration_Scripts', 'add_data_migration_script_folders'] |
|
73 | + ); |
|
74 | + if (! $filters_set) { |
|
75 | + add_filter( |
|
76 | + 'FHEE__EE_Data_Migration_Manager__get_data_migration_script_folders', |
|
77 | + ['EE_Register_Data_Migration_Scripts', 'add_data_migration_script_folders'] |
|
78 | + ); |
|
79 | + } |
|
80 | + return true; |
|
81 | + } |
|
82 | 82 | |
83 | 83 | |
84 | - /** |
|
85 | - * @param array $dms_paths |
|
86 | - * @return array |
|
87 | - */ |
|
88 | - public static function add_data_migration_script_folders(array $dms_paths = []): array |
|
89 | - { |
|
90 | - foreach (self::$_settings as $addon_name => $settings) { |
|
91 | - $wildcards = 0; |
|
92 | - foreach ($settings['dms_paths'] as $dms_path) { |
|
93 | - // since we are using the addon name for the array key |
|
94 | - // we need to ensure that the key is unique, |
|
95 | - // so if for some reason an addon has multiple dms paths, |
|
96 | - // we append one or more * to the classname |
|
97 | - // which will get stripped out later on |
|
98 | - $dms_paths[ $addon_name . str_repeat('*', $wildcards) ] = $dms_path; |
|
99 | - $wildcards++; |
|
100 | - } |
|
101 | - } |
|
102 | - return $dms_paths; |
|
103 | - } |
|
84 | + /** |
|
85 | + * @param array $dms_paths |
|
86 | + * @return array |
|
87 | + */ |
|
88 | + public static function add_data_migration_script_folders(array $dms_paths = []): array |
|
89 | + { |
|
90 | + foreach (self::$_settings as $addon_name => $settings) { |
|
91 | + $wildcards = 0; |
|
92 | + foreach ($settings['dms_paths'] as $dms_path) { |
|
93 | + // since we are using the addon name for the array key |
|
94 | + // we need to ensure that the key is unique, |
|
95 | + // so if for some reason an addon has multiple dms paths, |
|
96 | + // we append one or more * to the classname |
|
97 | + // which will get stripped out later on |
|
98 | + $dms_paths[ $addon_name . str_repeat('*', $wildcards) ] = $dms_path; |
|
99 | + $wildcards++; |
|
100 | + } |
|
101 | + } |
|
102 | + return $dms_paths; |
|
103 | + } |
|
104 | 104 | |
105 | 105 | |
106 | - /** |
|
107 | - * This deregisters a set of Data Migration Scripts that were previously registered with a specific dms_id |
|
108 | - * |
|
109 | - * @param string $addon_name EE_Addon class name that this set of data migration scripts belongs to |
|
110 | - * @return void |
|
111 | - * @since 4.3.0 |
|
112 | - * @since 4.3.0 |
|
113 | - */ |
|
114 | - public static function deregister(string $addon_name = '') |
|
115 | - { |
|
116 | - unset(self::$_settings[ $addon_name ]); |
|
117 | - } |
|
106 | + /** |
|
107 | + * This deregisters a set of Data Migration Scripts that were previously registered with a specific dms_id |
|
108 | + * |
|
109 | + * @param string $addon_name EE_Addon class name that this set of data migration scripts belongs to |
|
110 | + * @return void |
|
111 | + * @since 4.3.0 |
|
112 | + * @since 4.3.0 |
|
113 | + */ |
|
114 | + public static function deregister(string $addon_name = '') |
|
115 | + { |
|
116 | + unset(self::$_settings[ $addon_name ]); |
|
117 | + } |
|
118 | 118 | |
119 | 119 | |
120 | - public static function reset(): void |
|
121 | - { |
|
122 | - self::$_settings = []; |
|
123 | - } |
|
120 | + public static function reset(): void |
|
121 | + { |
|
122 | + self::$_settings = []; |
|
123 | + } |
|
124 | 124 | } |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | ); |
48 | 48 | } |
49 | 49 | // make sure we don't register twice |
50 | - if (isset(self::$_settings[ $addon_name ])) { |
|
50 | + if (isset(self::$_settings[$addon_name])) { |
|
51 | 51 | return true; |
52 | 52 | } |
53 | 53 | // make sure this was called in the right place! |
@@ -65,13 +65,13 @@ discard block |
||
65 | 65 | ); |
66 | 66 | } |
67 | 67 | // setup $_settings array from incoming values. |
68 | - self::$_settings[ $addon_name ] = ['dms_paths' => (array) $setup_args['dms_paths']]; |
|
68 | + self::$_settings[$addon_name] = ['dms_paths' => (array) $setup_args['dms_paths']]; |
|
69 | 69 | // setup DMS |
70 | 70 | $filters_set = has_filter( |
71 | 71 | 'FHEE__EE_Data_Migration_Manager__get_data_migration_script_folders', |
72 | 72 | ['EE_Register_Data_Migration_Scripts', 'add_data_migration_script_folders'] |
73 | 73 | ); |
74 | - if (! $filters_set) { |
|
74 | + if ( ! $filters_set) { |
|
75 | 75 | add_filter( |
76 | 76 | 'FHEE__EE_Data_Migration_Manager__get_data_migration_script_folders', |
77 | 77 | ['EE_Register_Data_Migration_Scripts', 'add_data_migration_script_folders'] |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | // so if for some reason an addon has multiple dms paths, |
96 | 96 | // we append one or more * to the classname |
97 | 97 | // which will get stripped out later on |
98 | - $dms_paths[ $addon_name . str_repeat('*', $wildcards) ] = $dms_path; |
|
98 | + $dms_paths[$addon_name.str_repeat('*', $wildcards)] = $dms_path; |
|
99 | 99 | $wildcards++; |
100 | 100 | } |
101 | 101 | } |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | */ |
114 | 114 | public static function deregister(string $addon_name = '') |
115 | 115 | { |
116 | - unset(self::$_settings[ $addon_name ]); |
|
116 | + unset(self::$_settings[$addon_name]); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 |