@@ -14,44 +14,44 @@ |
||
14 | 14 | class EE_Payment_message_type extends EE_Payment_Base_message_type |
15 | 15 | { |
16 | 16 | |
17 | - public function __construct() |
|
18 | - { |
|
17 | + public function __construct() |
|
18 | + { |
|
19 | 19 | |
20 | - // setup type details for reference |
|
21 | - $this->name = 'payment'; |
|
22 | - $this->description = esc_html__('This message type is used for all payment notification messages that go out including any manual payments entered by an event administrator.', 'event_espresso'); |
|
23 | - $this->label = array( |
|
24 | - 'singular' => esc_html__('payment received', 'event_espresso'), |
|
25 | - 'plural' => esc_html__('payments received', 'event_espresso') |
|
26 | - ); |
|
20 | + // setup type details for reference |
|
21 | + $this->name = 'payment'; |
|
22 | + $this->description = esc_html__('This message type is used for all payment notification messages that go out including any manual payments entered by an event administrator.', 'event_espresso'); |
|
23 | + $this->label = array( |
|
24 | + 'singular' => esc_html__('payment received', 'event_espresso'), |
|
25 | + 'plural' => esc_html__('payments received', 'event_espresso') |
|
26 | + ); |
|
27 | 27 | |
28 | - parent::__construct(); |
|
29 | - } |
|
28 | + parent::__construct(); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * _set_contexts |
|
33 | - * This sets up the contexts associated with the message_type |
|
34 | - * |
|
35 | - * @access protected |
|
36 | - * @return void |
|
37 | - */ |
|
38 | - protected function _set_contexts() |
|
39 | - { |
|
40 | - $this->_context_label = array( |
|
41 | - 'label' => esc_html__('recipient', 'event_espresso'), |
|
42 | - 'plural' => esc_html__('recipients', 'event_espresso'), |
|
43 | - 'description' => esc_html__('Recipient\'s are who will receive the template. You may want different payment details sent out depending on who the recipient is', 'event_espresso') |
|
44 | - ); |
|
31 | + /** |
|
32 | + * _set_contexts |
|
33 | + * This sets up the contexts associated with the message_type |
|
34 | + * |
|
35 | + * @access protected |
|
36 | + * @return void |
|
37 | + */ |
|
38 | + protected function _set_contexts() |
|
39 | + { |
|
40 | + $this->_context_label = array( |
|
41 | + 'label' => esc_html__('recipient', 'event_espresso'), |
|
42 | + 'plural' => esc_html__('recipients', 'event_espresso'), |
|
43 | + 'description' => esc_html__('Recipient\'s are who will receive the template. You may want different payment details sent out depending on who the recipient is', 'event_espresso') |
|
44 | + ); |
|
45 | 45 | |
46 | - $this->_contexts = array( |
|
47 | - 'admin' => array( |
|
48 | - 'label' => esc_html__('Event Admin', 'event_espresso'), |
|
49 | - 'description' => esc_html__('This template is what event administrators will receive on a successful payment', 'event_espresso') |
|
50 | - ), |
|
51 | - 'primary_attendee' => array( |
|
52 | - 'label' => esc_html__('Primary Registrant', 'event_espresso'), |
|
53 | - 'description' => esc_html__('This template is what the primary registrant (the person who made the main registration) will receive on successful payment', 'event_espresso') |
|
54 | - ) |
|
55 | - ); |
|
56 | - } |
|
46 | + $this->_contexts = array( |
|
47 | + 'admin' => array( |
|
48 | + 'label' => esc_html__('Event Admin', 'event_espresso'), |
|
49 | + 'description' => esc_html__('This template is what event administrators will receive on a successful payment', 'event_espresso') |
|
50 | + ), |
|
51 | + 'primary_attendee' => array( |
|
52 | + 'label' => esc_html__('Primary Registrant', 'event_espresso'), |
|
53 | + 'description' => esc_html__('This template is what the primary registrant (the person who made the main registration) will receive on successful payment', 'event_espresso') |
|
54 | + ) |
|
55 | + ); |
|
56 | + } |
|
57 | 57 | } |
@@ -14,60 +14,60 @@ |
||
14 | 14 | class EE_Not_Approved_Registration_message_type extends EE_Registration_Base_message_type |
15 | 15 | { |
16 | 16 | |
17 | - public function __construct() |
|
18 | - { |
|
19 | - $this->name = 'not_approved_registration'; |
|
20 | - $this->description = esc_html__('This message type is for messages sent to registrants when their registration is set to the not approved status.', 'event_espresso'); |
|
21 | - $this->label = array( |
|
22 | - 'singular' => esc_html__('not approved registration', 'event_espresso'), |
|
23 | - 'plural' => esc_html__('not approved registrations', 'event_espresso') |
|
24 | - ); |
|
17 | + public function __construct() |
|
18 | + { |
|
19 | + $this->name = 'not_approved_registration'; |
|
20 | + $this->description = esc_html__('This message type is for messages sent to registrants when their registration is set to the not approved status.', 'event_espresso'); |
|
21 | + $this->label = array( |
|
22 | + 'singular' => esc_html__('not approved registration', 'event_espresso'), |
|
23 | + 'plural' => esc_html__('not approved registrations', 'event_espresso') |
|
24 | + ); |
|
25 | 25 | |
26 | - $this->_master_templates = array( |
|
27 | - 'email' => 'registration' |
|
28 | - ); |
|
26 | + $this->_master_templates = array( |
|
27 | + 'email' => 'registration' |
|
28 | + ); |
|
29 | 29 | |
30 | - parent::__construct(); |
|
31 | - } |
|
30 | + parent::__construct(); |
|
31 | + } |
|
32 | 32 | |
33 | 33 | |
34 | 34 | |
35 | 35 | |
36 | - /** |
|
37 | - * _set_contexts |
|
38 | - * This sets up the contexts associated with the message_type |
|
39 | - * |
|
40 | - * @access protected |
|
41 | - * @return void |
|
42 | - */ |
|
43 | - protected function _set_contexts() |
|
44 | - { |
|
45 | - $this->_context_label = array( |
|
46 | - 'label' => esc_html__('recipient', 'event_espresso'), |
|
47 | - 'plural' => esc_html__('recipients', 'event_espresso'), |
|
48 | - 'description' => esc_html__('Recipient\'s are who will receive the template. You may want different registration details sent out depending on who the recipient is', 'event_espresso') |
|
49 | - ); |
|
36 | + /** |
|
37 | + * _set_contexts |
|
38 | + * This sets up the contexts associated with the message_type |
|
39 | + * |
|
40 | + * @access protected |
|
41 | + * @return void |
|
42 | + */ |
|
43 | + protected function _set_contexts() |
|
44 | + { |
|
45 | + $this->_context_label = array( |
|
46 | + 'label' => esc_html__('recipient', 'event_espresso'), |
|
47 | + 'plural' => esc_html__('recipients', 'event_espresso'), |
|
48 | + 'description' => esc_html__('Recipient\'s are who will receive the template. You may want different registration details sent out depending on who the recipient is', 'event_espresso') |
|
49 | + ); |
|
50 | 50 | |
51 | - $this->_contexts = array( |
|
52 | - 'admin' => array( |
|
53 | - 'label' => esc_html__('Event Admin', 'event_espresso'), |
|
54 | - 'description' => esc_html__('This template is what event administrators will receive when registration status is set to not approved.', 'event_espresso') |
|
55 | - ), |
|
56 | - 'primary_attendee' => array( |
|
57 | - 'label' => esc_html__('Primary Registrant', 'event_espresso'), |
|
58 | - 'description' => esc_html__('This template is what the primary registrant (the person who completed the initial transaction) will receive when the registration status is not approved.', 'event_espresso') |
|
59 | - ) |
|
60 | - ); |
|
61 | - } |
|
51 | + $this->_contexts = array( |
|
52 | + 'admin' => array( |
|
53 | + 'label' => esc_html__('Event Admin', 'event_espresso'), |
|
54 | + 'description' => esc_html__('This template is what event administrators will receive when registration status is set to not approved.', 'event_espresso') |
|
55 | + ), |
|
56 | + 'primary_attendee' => array( |
|
57 | + 'label' => esc_html__('Primary Registrant', 'event_espresso'), |
|
58 | + 'description' => esc_html__('This template is what the primary registrant (the person who completed the initial transaction) will receive when the registration status is not approved.', 'event_espresso') |
|
59 | + ) |
|
60 | + ); |
|
61 | + } |
|
62 | 62 | |
63 | 63 | |
64 | 64 | |
65 | - protected function _primary_attendee_addressees() |
|
66 | - { |
|
67 | - $cached = $this->_single_message; |
|
68 | - $this->_single_message = false; |
|
69 | - $addressees = parent::_primary_attendee_addressees(); |
|
70 | - $this->_single_message = $cached; |
|
71 | - return $addressees; |
|
72 | - } |
|
65 | + protected function _primary_attendee_addressees() |
|
66 | + { |
|
67 | + $cached = $this->_single_message; |
|
68 | + $this->_single_message = false; |
|
69 | + $addressees = parent::_primary_attendee_addressees(); |
|
70 | + $this->_single_message = $cached; |
|
71 | + return $addressees; |
|
72 | + } |
|
73 | 73 | } |
@@ -12,1122 +12,1122 @@ |
||
12 | 12 | class EE_Message_Resource_Manager |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * This option in the database is used to keep a record of message types that have been activated for a messenger |
|
17 | - * at some point in the history of the site. It is utilized by the implementation of the 'force' flag in |
|
18 | - * EE_Register_Message_Type. The force flag is an indication of whether a message type should be activated by |
|
19 | - * default when the message type is registered. However, if a user has explicitly deactivated a message type, then |
|
20 | - * the force flag is ignored. The method by which the code knows whether to ignore this flag is via this option. |
|
21 | - * Note, that this is NOT a historical record. Its entirely possible for a message type to have been activated for |
|
22 | - * a messenger and yet not have a record in this option. This occurs when a message type is inactivated through an |
|
23 | - * automated process (when an add-on registering the message type deactivates, or when some other code calls the |
|
24 | - * EE_Registery_Message_Type::deregister method) and the related record(s) is(are) removed from this option to ensure |
|
25 | - * the "force" flag is respected if that message type is later re-registered. |
|
26 | - * |
|
27 | - * This option should NOT be used to determine the current "active" state of a message type for a given messenger. |
|
28 | - * |
|
29 | - * The name of this option (and related methods/properties) is due to matching the original intended purpose for the |
|
30 | - * option that got superseded by later behaviour requirements. |
|
31 | - */ |
|
32 | - const HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME = 'ee_has_activated_messenger'; |
|
33 | - |
|
34 | - /** |
|
35 | - * @type boolean $_initialized |
|
36 | - */ |
|
37 | - protected $_initialized = false; |
|
38 | - |
|
39 | - /** |
|
40 | - * @type EE_Messenger_Collection $_messenger_collection_loader |
|
41 | - */ |
|
42 | - protected $_messenger_collection_loader; |
|
43 | - |
|
44 | - /** |
|
45 | - * @type EE_Message_Type_Collection $_message_type_collection_loader |
|
46 | - */ |
|
47 | - protected $_message_type_collection_loader; |
|
48 | - |
|
49 | - /** |
|
50 | - * @type EEM_Message_Template_Group $_message_template_group_model |
|
51 | - */ |
|
52 | - protected $_message_template_group_model; |
|
53 | - |
|
54 | - /** |
|
55 | - * @type EE_messenger[] |
|
56 | - */ |
|
57 | - protected $_installed_messengers = array(); |
|
58 | - |
|
59 | - /** |
|
60 | - * @type EE_message_type[] |
|
61 | - */ |
|
62 | - protected $_installed_message_types = array(); |
|
63 | - |
|
64 | - /** |
|
65 | - * Array of active messengers. |
|
66 | - * Format is this: |
|
67 | - * array( |
|
68 | - * 'messenger_name' => EE_messenger |
|
69 | - * ) |
|
70 | - * |
|
71 | - * @type EE_messenger[] |
|
72 | - */ |
|
73 | - protected $_active_messengers = array(); |
|
74 | - |
|
75 | - /** |
|
76 | - * Formatted array of active message types grouped per messenger. |
|
77 | - * Format is this: |
|
78 | - * array( |
|
79 | - * 'messenger_name' => array( |
|
80 | - * 'settings' => array( |
|
81 | - * '{messenger_name}-message_types' => array( |
|
82 | - * 'message_type_name' => array() //variable array of settings corresponding to message type. |
|
83 | - * ) |
|
84 | - * ) |
|
85 | - * ) |
|
86 | - * ) |
|
87 | - * |
|
88 | - * @type array |
|
89 | - */ |
|
90 | - protected $_active_message_types = array(); |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * This holds the array of messengers and their corresponding message types that have |
|
95 | - * been activated on a site at some point. This is an important record that helps the messages system |
|
96 | - * not accidentally reactivate something that was intentionally deactivated by a user. |
|
97 | - * |
|
98 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
99 | - * |
|
100 | - * @type array |
|
101 | - */ |
|
102 | - protected $_has_activated_messengers_and_message_types = array(); |
|
103 | - |
|
104 | - /** |
|
105 | - * An array of unique message type contexts across all active message types. |
|
106 | - * The array will be indexed by either 'slugs' or 'all'. |
|
107 | - * The slugs index contains an array indexed by unique context slugs with the latest label representation for that |
|
108 | - * slug. array( |
|
109 | - * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
110 | - * ); |
|
111 | - * The all index returns an array in this format: |
|
112 | - * array( |
|
113 | - * 'message_type_name' => array( |
|
114 | - * 'context_slug' => array( |
|
115 | - * 'label' => 'localized label for context', |
|
116 | - * 'description' => 'localized description for context' |
|
117 | - * ) |
|
118 | - * ) |
|
119 | - * ); |
|
120 | - * |
|
121 | - * @type array |
|
122 | - */ |
|
123 | - protected $_contexts = array(); |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * EE_Message_Resource_Manager constructor. |
|
128 | - * |
|
129 | - * @param \EE_Messenger_Collection_Loader $Messenger_Collection_Loader |
|
130 | - * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader |
|
131 | - * @param \EEM_Message_Template_Group $Message_Template_Group_Model |
|
132 | - */ |
|
133 | - public function __construct( |
|
134 | - EE_Messenger_Collection_Loader $Messenger_Collection_Loader, |
|
135 | - EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader, |
|
136 | - EEM_Message_Template_Group $Message_Template_Group_Model |
|
137 | - ) { |
|
138 | - $this->_messenger_collection_loader = $Messenger_Collection_Loader; |
|
139 | - $this->_message_type_collection_loader = $Message_Type_Collection_Loader; |
|
140 | - $this->_message_template_group_model = $Message_Template_Group_Model; |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * @return void |
|
146 | - */ |
|
147 | - protected function _initialize_collections() |
|
148 | - { |
|
149 | - if ($this->_initialized) { |
|
150 | - return; |
|
151 | - } |
|
152 | - $this->_initialized = true; |
|
153 | - $this->_messenger_collection_loader->load_messengers_from_folder(); |
|
154 | - $this->_message_type_collection_loader->load_message_types_from_folder(); |
|
155 | - $this->get_has_activated_messengers_option(true); |
|
156 | - $this->_set_active_messengers_and_message_types(); |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * @return EE_Messenger_Collection |
|
162 | - */ |
|
163 | - public function messenger_collection() |
|
164 | - { |
|
165 | - $this->_initialize_collections(); |
|
166 | - return $this->_messenger_collection_loader->messenger_collection(); |
|
167 | - } |
|
168 | - |
|
169 | - |
|
170 | - /** |
|
171 | - * @return EE_messenger[] |
|
172 | - */ |
|
173 | - public function active_messengers() |
|
174 | - { |
|
175 | - $this->_initialize_collections(); |
|
176 | - return $this->_active_messengers; |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * @param string $messenger_name |
|
182 | - * @return \EE_messenger |
|
183 | - */ |
|
184 | - public function get_messenger($messenger_name) |
|
185 | - { |
|
186 | - return $this->messenger_collection()->get_by_info($messenger_name); |
|
187 | - } |
|
188 | - |
|
189 | - |
|
190 | - /** |
|
191 | - * This returns the corresponding EE_messenger object for the given string if it is active. |
|
192 | - * |
|
193 | - * @param string $messenger |
|
194 | - * @return EE_messenger | null |
|
195 | - */ |
|
196 | - public function get_active_messenger($messenger) |
|
197 | - { |
|
198 | - $this->_initialize_collections(); |
|
199 | - return ! empty($this->_active_messengers[ $messenger ]) ? $this->_active_messengers[ $messenger ] : null; |
|
200 | - } |
|
201 | - |
|
202 | - |
|
203 | - /** |
|
204 | - * @return \EE_messenger[] |
|
205 | - */ |
|
206 | - public function installed_messengers() |
|
207 | - { |
|
208 | - if (empty($this->_installed_messengers)) { |
|
209 | - $this->_installed_messengers = array(); |
|
210 | - $this->messenger_collection()->rewind(); |
|
211 | - while ($this->messenger_collection()->valid()) { |
|
212 | - $this->_installed_messengers[ $this->messenger_collection()->current()->name ] = $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 = array(); |
|
322 | - if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
323 | - $settings = isset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings']) |
|
324 | - ? $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] |
|
325 | - : array(); |
|
326 | - } |
|
327 | - return $settings; |
|
328 | - } |
|
329 | - |
|
330 | - |
|
331 | - /** |
|
332 | - * Returns whether the given messenger name has active message types on it. |
|
333 | - * Infers whether the messenger is active or not as well. |
|
334 | - * |
|
335 | - * @param string $messenger_name |
|
336 | - * @return bool |
|
337 | - */ |
|
338 | - public function messenger_has_active_message_types($messenger_name) |
|
339 | - { |
|
340 | - $this->_initialize_collections(); |
|
341 | - return |
|
342 | - ! empty($this->_active_message_types[ $messenger_name ]) |
|
343 | - && ! empty($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ]); |
|
344 | - } |
|
345 | - |
|
346 | - |
|
347 | - /** |
|
348 | - * This checks the _active_message_types property for any active message types |
|
349 | - * that are present for the given messenger and returns them. |
|
350 | - * |
|
351 | - * @since 4.9.0 |
|
352 | - * @param string $messenger_name The messenger being checked |
|
353 | - * @return EE_message_type[]|array (empty array if no active_message_types) |
|
354 | - */ |
|
355 | - public function get_active_message_types_for_messenger($messenger_name) |
|
356 | - { |
|
357 | - $message_types = array(); |
|
358 | - if (! $this->messenger_has_active_message_types($messenger_name)) { |
|
359 | - return $message_types; |
|
360 | - } |
|
361 | - $installed_message_types = $this->installed_message_types(); |
|
362 | - foreach ($installed_message_types as $message_type_name => $message_type) { |
|
363 | - if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
364 | - $message_types[ $message_type_name ] = $message_type; |
|
365 | - } |
|
366 | - } |
|
367 | - return $message_types; |
|
368 | - } |
|
369 | - |
|
370 | - |
|
371 | - /** |
|
372 | - * This does NOT return the _active_message_types property but |
|
373 | - * simply returns an array of active message type names from that property. |
|
374 | - * (The _active_message_types property is indexed by messenger and active message_types per messenger). |
|
375 | - * |
|
376 | - * @return array message_type references (string) |
|
377 | - */ |
|
378 | - public function list_of_active_message_types() |
|
379 | - { |
|
380 | - $active_message_type_names = array(); |
|
381 | - $this->_initialize_collections(); |
|
382 | - foreach ($this->_active_message_types as $messenger => $messenger_settings) { |
|
383 | - if (! isset($messenger_settings['settings'][ $messenger . '-message_types' ])) { |
|
384 | - continue; |
|
385 | - } |
|
386 | - foreach ($messenger_settings['settings'][ $messenger . '-message_types' ] as $message_type_name => $message_type_config) { |
|
387 | - if (! in_array($message_type_name, $active_message_type_names)) { |
|
388 | - $active_message_type_names[] = $message_type_name; |
|
389 | - } |
|
390 | - } |
|
391 | - } |
|
392 | - return $active_message_type_names; |
|
393 | - } |
|
394 | - |
|
395 | - |
|
396 | - /** |
|
397 | - * Same as list_of_active_message_types() except this returns actual EE_message_type objects |
|
398 | - * |
|
399 | - * @since 4.9.0 |
|
400 | - * @return \EE_message_type[] |
|
401 | - */ |
|
402 | - public function get_active_message_type_objects() |
|
403 | - { |
|
404 | - $active_message_types = array(); |
|
405 | - $installed_message_types = $this->installed_message_types(); |
|
406 | - $active_message_type_names = $this->list_of_active_message_types(); |
|
407 | - foreach ($active_message_type_names as $active_message_type_name) { |
|
408 | - if (isset($installed_message_types[ $active_message_type_name ])) { |
|
409 | - $active_message_types[ $active_message_type_name ] = $installed_message_types[ $active_message_type_name ]; |
|
410 | - } |
|
411 | - } |
|
412 | - return $active_message_types; |
|
413 | - } |
|
414 | - |
|
415 | - |
|
416 | - /** |
|
417 | - * @return \EE_message_type[] |
|
418 | - */ |
|
419 | - public function installed_message_types() |
|
420 | - { |
|
421 | - if (empty($this->_installed_message_types)) { |
|
422 | - $this->message_type_collection()->rewind(); |
|
423 | - while ($this->message_type_collection()->valid()) { |
|
424 | - $this->_installed_message_types[ $this->message_type_collection()->current()->name ] = $this->message_type_collection()->current(); |
|
425 | - $this->message_type_collection()->next(); |
|
426 | - } |
|
427 | - } |
|
428 | - return $this->_installed_message_types; |
|
429 | - } |
|
430 | - |
|
431 | - |
|
432 | - /** |
|
433 | - * @param string $message_type_name |
|
434 | - * @return \EE_message_type |
|
435 | - * @throws EE_Error |
|
436 | - */ |
|
437 | - public function valid_message_type($message_type_name) |
|
438 | - { |
|
439 | - $message_type = $this->get_message_type($message_type_name); |
|
440 | - if ($message_type instanceof EE_message_type) { |
|
441 | - return $message_type; |
|
442 | - } |
|
443 | - throw new EE_Error( |
|
444 | - sprintf( |
|
445 | - esc_html__('The "%1$s" message type is either invalid or not installed', 'event_espresso'), |
|
446 | - $message_type_name |
|
447 | - ) |
|
448 | - ); |
|
449 | - } |
|
450 | - |
|
451 | - |
|
452 | - /** |
|
453 | - * valid_message_type_for_messenger |
|
454 | - * |
|
455 | - * @param EE_messenger $messenger |
|
456 | - * @param string $message_type_name |
|
457 | - * @return boolean |
|
458 | - * @throws EE_Error |
|
459 | - */ |
|
460 | - public function valid_message_type_for_messenger(EE_messenger $messenger, $message_type_name) |
|
461 | - { |
|
462 | - $valid_message_types = $messenger->get_valid_message_types(); |
|
463 | - if (! in_array($message_type_name, $valid_message_types)) { |
|
464 | - throw new EE_Error( |
|
465 | - sprintf( |
|
466 | - esc_html__( |
|
467 | - '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.', |
|
468 | - 'event_espresso' |
|
469 | - ), |
|
470 | - $message_type_name, |
|
471 | - __METHOD__, |
|
472 | - $messenger->name |
|
473 | - ) |
|
474 | - ); |
|
475 | - } |
|
476 | - return true; |
|
477 | - } |
|
478 | - |
|
479 | - |
|
480 | - /** |
|
481 | - * Used to return active messengers array stored in the wp options table. |
|
482 | - * If no value is present in the option then an empty array is returned. |
|
483 | - * |
|
484 | - * @param bool $reset If true then we ignore whether the option is cached on the _active_message_types |
|
485 | - * property and pull directly from the db. Otherwise whatever is currently on the |
|
486 | - * $_active_message_types property is pulled. |
|
487 | - * @return array |
|
488 | - */ |
|
489 | - public function get_active_messengers_option($reset = false) |
|
490 | - { |
|
491 | - if ($reset) { |
|
492 | - $this->_active_message_types = get_option('ee_active_messengers', array()); |
|
493 | - } |
|
494 | - return $this->_active_message_types; |
|
495 | - } |
|
496 | - |
|
497 | - |
|
498 | - /** |
|
499 | - * Used to update the active messengers array stored in the wp options table. |
|
500 | - * |
|
501 | - * @param array $active_messenger_settings Incoming data to save. If empty, then the internal cached property |
|
502 | - * representing this data is used. |
|
503 | - * @return bool FALSE if not updated, TRUE if updated. |
|
504 | - */ |
|
505 | - public function update_active_messengers_option($active_messenger_settings = array()) |
|
506 | - { |
|
507 | - $active_messenger_settings = empty($active_messenger_settings) ? $this->_active_message_types : $active_messenger_settings; |
|
508 | - // make sure _active_message_types is updated (this is the internal cache for the settings). |
|
509 | - $this->_active_message_types = $active_messenger_settings; |
|
510 | - return update_option('ee_active_messengers', $active_messenger_settings); |
|
511 | - } |
|
512 | - |
|
513 | - |
|
514 | - /** |
|
515 | - * Used to return has activated message types for messengers array stored in the wp options table. |
|
516 | - * If no value is present in the option then an empty array is returned. |
|
517 | - * The value is cached on the $_has_activated_messengers_and_message_types property for future calls. |
|
518 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
519 | - * |
|
520 | - * @param bool $reset Used to indicate that any cached value should be ignored. |
|
521 | - * @return array |
|
522 | - */ |
|
523 | - public function get_has_activated_messengers_option($reset = false) |
|
524 | - { |
|
525 | - if ($reset || empty($this->_has_activated_messengers_and_message_types)) { |
|
526 | - $this->_has_activated_messengers_and_message_types = get_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, array()); |
|
527 | - } |
|
528 | - return $this->_has_activated_messengers_and_message_types; |
|
529 | - } |
|
530 | - |
|
531 | - |
|
532 | - /** |
|
533 | - * Used to update the has activated option in the db. |
|
534 | - * |
|
535 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
536 | - * |
|
537 | - * @param array $has_activated_messengers Incoming data to save. If empty, then the internal cached property |
|
538 | - * representing this data is used. |
|
539 | - * @return bool FALSE if not updated, TRUE if updated. |
|
540 | - */ |
|
541 | - public function update_has_activated_messengers_option($has_activated_messengers = array()) |
|
542 | - { |
|
543 | - // make sure the option has been retrieved from first so we don't overwrite it accidentally. |
|
544 | - if (empty($has_activated_messengers) && empty($this->_has_activated_messengers_and_message_types)) { |
|
545 | - $this->get_has_activated_messengers_option(); |
|
546 | - } |
|
547 | - $has_activated_messengers = empty($has_activated_messengers) |
|
548 | - ? $this->_has_activated_messengers_and_message_types |
|
549 | - : $has_activated_messengers; |
|
550 | - return update_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, $has_activated_messengers); |
|
551 | - } |
|
552 | - |
|
553 | - |
|
554 | - /** |
|
555 | - * wrapper for _set_active_messengers_and_message_types() |
|
556 | - */ |
|
557 | - public function reset_active_messengers_and_message_types() |
|
558 | - { |
|
559 | - $this->_set_active_messengers_and_message_types(); |
|
560 | - } |
|
561 | - |
|
562 | - |
|
563 | - /** |
|
564 | - * Generate list of active messengers and message types from collection. |
|
565 | - * This sets up the active messengers from what is present in the database. |
|
566 | - */ |
|
567 | - protected function _set_active_messengers_and_message_types() |
|
568 | - { |
|
569 | - // echo "\n\n " . __LINE__ . ") " . __METHOD__ . "() \n"; |
|
570 | - // list of activated messengers as set via the admin |
|
571 | - // note calling `get_active_messengers_options` also initializes the _active_message_types property. |
|
572 | - $this->get_active_messengers_option(true); |
|
573 | - $this->ensure_messengers_are_active(array(), false, true); |
|
574 | - $this->update_active_messengers_option(); |
|
575 | - $this->update_has_activated_messengers_option(); |
|
576 | - } |
|
577 | - |
|
578 | - |
|
579 | - /** |
|
580 | - * Ensures that the specified messenger is currently active. |
|
581 | - * If not, activates it and its default message types. |
|
582 | - * |
|
583 | - * @param string $messenger_name |
|
584 | - * @param bool $update_option Whether to update the option in the db or not. |
|
585 | - * @return boolean true if either already active or successfully activated. |
|
586 | - */ |
|
587 | - public function ensure_messenger_is_active($messenger_name, $update_option = true) |
|
588 | - { |
|
589 | - if (! isset($this->_active_messengers[ $messenger_name ])) { |
|
590 | - try { |
|
591 | - $this->activate_messenger($messenger_name, array(), $update_option); |
|
592 | - } catch (EE_Error $e) { |
|
593 | - EE_Error::add_error( |
|
594 | - $e->getMessage(), |
|
595 | - __FILE__, |
|
596 | - __FUNCTION__, |
|
597 | - __LINE__ |
|
598 | - ); |
|
599 | - return false; |
|
600 | - } |
|
601 | - } |
|
602 | - return true; |
|
603 | - } |
|
604 | - |
|
605 | - |
|
606 | - /** |
|
607 | - * This ensures the given array of messenger names is active in the system. |
|
608 | - * Note, this method will not activate any NEW message types for the messenger when it is called. Instead, |
|
609 | - * it will automatically activate the default message types for the messenger if its not active. |
|
610 | - * |
|
611 | - * @param array $messenger_names Array of messenger names for messengers to be activated. If an empty array |
|
612 | - * (default) then will attempt to set the active messengers from the |
|
613 | - * activated_messengers option |
|
614 | - * (stored in $_active_message_types property). |
|
615 | - * @param bool $update_option Whether to update the related active messengers option. |
|
616 | - * @param bool $verify Whether to verify the messengers are installed before activating. Note if this is |
|
617 | - * set to true and a messenger is indicated as active, but is NOT installed, then it |
|
618 | - * will automatically be deactivated. |
|
619 | - */ |
|
620 | - public function ensure_messengers_are_active($messenger_names = array(), $update_option = true, $verify = false) |
|
621 | - { |
|
622 | - $messenger_names = empty($messenger_names) ? array_keys($this->_active_message_types) : $messenger_names; |
|
623 | - |
|
624 | - $not_installed = array(); |
|
625 | - foreach ($messenger_names as $messenger_name) { |
|
626 | - if ($verify && ! $this->messenger_collection()->has_by_name($messenger_name)) { |
|
627 | - $not_installed[] = $messenger_name; |
|
628 | - $this->deactivate_messenger($messenger_name); |
|
629 | - continue; |
|
630 | - } |
|
631 | - $this->ensure_messenger_is_active($messenger_name, $update_option); |
|
632 | - } |
|
633 | - |
|
634 | - if (! empty($not_installed)) { |
|
635 | - EE_Error::add_error( |
|
636 | - sprintf( |
|
637 | - esc_html__('The following messengers are either not installed or are invalid:%1$s %2$s', 'event_espresso'), |
|
638 | - '<br />', |
|
639 | - implode(', ', $not_installed) |
|
640 | - ), |
|
641 | - __FILE__, |
|
642 | - __FUNCTION__, |
|
643 | - __LINE__ |
|
644 | - ); |
|
645 | - } |
|
646 | - } |
|
647 | - |
|
648 | - |
|
649 | - /** |
|
650 | - * Ensures that the specified message type for the given messenger is currently active, if not activates it. |
|
651 | - * This ALSO ensures that the given messenger is active as well! |
|
652 | - * |
|
653 | - * @param string $message_type_name message type name. |
|
654 | - * @param $messenger_name |
|
655 | - * @param bool $update_option Whether to update the option in the db or not. |
|
656 | - * @return bool Returns true if already is active or if was activated successfully. |
|
657 | - * @throws EE_Error |
|
658 | - */ |
|
659 | - public function ensure_message_type_is_active($message_type_name, $messenger_name, $update_option = true) |
|
660 | - { |
|
661 | - // grab the messenger to work with. |
|
662 | - $messenger = $this->valid_messenger($messenger_name); |
|
663 | - if ($this->valid_message_type_for_messenger($messenger, $message_type_name)) { |
|
664 | - // ensure messenger is active (that's an inherent coupling between active message types and the |
|
665 | - // messenger they are being activated for. |
|
666 | - try { |
|
667 | - if (! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
668 | - // all is good so let's just get it active |
|
669 | - $this->activate_messenger($messenger, array($message_type_name), $update_option); |
|
670 | - } |
|
671 | - } catch (EE_Error $e) { |
|
672 | - EE_Error::add_error( |
|
673 | - $e->getMessage(), |
|
674 | - __FILE__, |
|
675 | - __FUNCTION__, |
|
676 | - __LINE__ |
|
677 | - ); |
|
678 | - return false; |
|
679 | - } |
|
680 | - } |
|
681 | - return true; |
|
682 | - } |
|
683 | - |
|
684 | - |
|
685 | - /** |
|
686 | - * This is a wrapper for `ensure_message_type_is_active` that will handle ensuring multiple message types for a |
|
687 | - * messenger are active in one go. |
|
688 | - * |
|
689 | - * @param array $message_type_names Array of message type names to ensure are active. |
|
690 | - * @param string $messenger_name The name of the messenger that the message types are to be activated on. |
|
691 | - * @param bool $update_option Whether to persist the activation to the database or not (default true). |
|
692 | - */ |
|
693 | - public function ensure_message_types_are_active($message_type_names, $messenger_name, $update_option = true) |
|
694 | - { |
|
695 | - $message_type_names = (array) $message_type_names; |
|
696 | - foreach ($message_type_names as $message_type_name) { |
|
697 | - // note, intentionally not updating option here because we're in a loop. |
|
698 | - // We'll follow the instructions of the incoming $update_option argument after the loop. |
|
699 | - $this->ensure_message_type_is_active($message_type_name, $messenger_name, false); |
|
700 | - } |
|
701 | - if ($update_option) { |
|
702 | - $this->update_active_messengers_option(); |
|
703 | - $this->update_has_activated_messengers_option(); |
|
704 | - } |
|
705 | - } |
|
706 | - |
|
707 | - |
|
708 | - /** |
|
709 | - * Activates the specified messenger. |
|
710 | - * |
|
711 | - * @param EE_messenger|string $messenger Instantiated EE_messenger OR messenger name if not already loaded! |
|
712 | - * @param array $message_type_names An array of message type names to activate with this messenger. |
|
713 | - * If included we do NOT setup the default message types |
|
714 | - * (assuming they are already setup.) |
|
715 | - * @param bool $update_active_messengers_option |
|
716 | - * @return array of generated templates |
|
717 | - * @throws EE_Error |
|
718 | - */ |
|
719 | - public function activate_messenger( |
|
720 | - $messenger, |
|
721 | - $message_type_names = array(), |
|
722 | - $update_active_messengers_option = true |
|
723 | - ) { |
|
724 | - $templates = array(); |
|
725 | - // grab the messenger to work with. |
|
726 | - $messenger = $messenger instanceof EE_messenger |
|
727 | - ? $messenger |
|
728 | - : $this->messenger_collection()->get_by_info($messenger); |
|
729 | - // it's inactive. Activate it. |
|
730 | - if ($messenger instanceof EE_messenger) { |
|
731 | - $this->_active_messengers[ $messenger->name ] = $messenger; |
|
732 | - // activate incoming message types set to be activated with messenger. |
|
733 | - $message_type_names = $this->_activate_message_types($messenger, $message_type_names); |
|
734 | - // setup any initial settings for the messenger if necessary. |
|
735 | - $this->add_settings_for_messenger($messenger->name); |
|
736 | - if ($update_active_messengers_option) { |
|
737 | - $this->update_active_messengers_option(); |
|
738 | - $this->update_has_activated_messengers_option(); |
|
739 | - } |
|
740 | - // generate new templates if necessary and ensure all related templates that are already in the database are |
|
741 | - // marked active. Note, this will also deactivate a message type for a messenger if the template |
|
742 | - // cannot be successfully created during its attempt (only happens for global template attempts). |
|
743 | - if (! empty($message_type_names)) { |
|
744 | - $templates = EEH_MSG_Template::generate_new_templates($messenger->name, $message_type_names, 0, true); |
|
745 | - EEH_MSG_Template::update_to_active(array($messenger->name), $message_type_names); |
|
746 | - } |
|
747 | - } |
|
748 | - return $templates; |
|
749 | - } |
|
750 | - |
|
751 | - |
|
752 | - /** |
|
753 | - * Activates given message types for the given EE_messenger object. |
|
754 | - * Note: (very important) This method does not persist the activation to the database. |
|
755 | - * See code implementing this method in this class for examples of how to persist. |
|
756 | - * |
|
757 | - * @param \EE_messenger $messenger |
|
758 | - * @param array $message_type_names |
|
759 | - * @return array |
|
760 | - */ |
|
761 | - protected function _activate_message_types(EE_messenger $messenger, $message_type_names = array()) |
|
762 | - { |
|
763 | - // If $message_type_names is empty, AND $this->_active_message_types is empty, then that means |
|
764 | - // things have never been initialized (which should happen on EEH_Activation::generate_message_templates). |
|
765 | - // So ONLY then do we need to actually grab defaults and cycle through them. Otherwise we |
|
766 | - // only override _active_message_types when an explicit array of $message_type_names has been provided. |
|
767 | - $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[ $messenger->name ]) |
|
768 | - ? $messenger->get_default_message_types() |
|
769 | - : (array) $message_type_names; |
|
770 | - |
|
771 | - // now we ALWAYS need to make sure that the messenger is active for the message types we're activating! |
|
772 | - if (! isset($this->_active_message_types[ $messenger->name ])) { |
|
773 | - $this->_active_message_types[ $messenger->name ]['settings'] = array(); |
|
774 | - } |
|
775 | - |
|
776 | - if ($message_type_names) { |
|
777 | - // cycle thru message types |
|
778 | - foreach ($message_type_names as $message_type_name) { |
|
779 | - // only register the message type as active IF it isn't already active |
|
780 | - // and if its actually installed. |
|
781 | - if ( |
|
782 | - ! $this->is_message_type_active_for_messenger($messenger->name, $message_type_name) |
|
783 | - ) { |
|
784 | - $this->add_settings_for_message_type($messenger->name, $message_type_name); |
|
785 | - $this->_set_messenger_has_activated_message_type( |
|
786 | - $messenger, |
|
787 | - $message_type_name |
|
788 | - ); |
|
789 | - } |
|
790 | - } |
|
791 | - } |
|
792 | - return $message_type_names; |
|
793 | - } |
|
794 | - |
|
795 | - |
|
796 | - /** |
|
797 | - * add_settings_for_message_type |
|
798 | - * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
799 | - * $this->update_active_messengers_option to persist. |
|
800 | - * |
|
801 | - * @param string $messenger_name The name of the messenger adding the settings for |
|
802 | - * @param string $message_type_name The name of the message type adding the settings for |
|
803 | - * @param array $new_settings Any new settings being set for the message type and messenger |
|
804 | - */ |
|
805 | - public function add_settings_for_message_type($messenger_name, $message_type_name, $new_settings = array()) |
|
806 | - { |
|
807 | - // get installed message type from collection |
|
808 | - $message_type = $this->message_type_collection()->get_by_info($message_type_name); |
|
809 | - $existing_settings = $this->get_message_type_settings_for_messenger($messenger_name, $message_type_name); |
|
810 | - // we need to setup any initial settings for message types |
|
811 | - if ($message_type instanceof EE_message_type) { |
|
812 | - $default_settings = $message_type->get_admin_settings_fields(); |
|
813 | - foreach ($default_settings as $field => $values) { |
|
814 | - if (isset($new_settings[ $field ])) { |
|
815 | - $existing_settings[ $field ] = $new_settings[ $field ]; |
|
816 | - continue; |
|
817 | - } |
|
818 | - if (! isset($existing_settings[ $field ])) { |
|
819 | - $existing_settings[ $field ] = $values['default']; |
|
820 | - } |
|
821 | - } |
|
822 | - } |
|
823 | - $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] = $existing_settings; |
|
824 | - } |
|
825 | - |
|
826 | - |
|
827 | - /** |
|
828 | - * Updates the internal cached _has_activated_messengers_and_message_types property with the given messenger |
|
829 | - * and message type. |
|
830 | - * |
|
831 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
832 | - * |
|
833 | - * @access protected |
|
834 | - * @param \EE_messenger $messenger |
|
835 | - * @param string $message_type_name |
|
836 | - */ |
|
837 | - protected function _set_messenger_has_activated_message_type(EE_messenger $messenger, $message_type_name) |
|
838 | - { |
|
839 | - |
|
840 | - // if _has_activated_messengers_and_message_types is empty then lets ensure its initialized |
|
841 | - if (empty($this->_has_activated_messengers_and_message_types)) { |
|
842 | - $this->get_has_activated_messengers_option(); |
|
843 | - } |
|
844 | - |
|
845 | - // make sure this messenger has a record in the has_activated array |
|
846 | - if (! isset($this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
847 | - $this->_has_activated_messengers_and_message_types[ $messenger->name ] = array(); |
|
848 | - } |
|
849 | - // check if message type has already been added |
|
850 | - if (! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
851 | - $this->_has_activated_messengers_and_message_types[ $messenger->name ][] = $message_type_name; |
|
852 | - } |
|
853 | - } |
|
854 | - |
|
855 | - |
|
856 | - /** |
|
857 | - * add_settings_for_messenger |
|
858 | - * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
859 | - * $this->update_active_messengers_option to persist. |
|
860 | - * |
|
861 | - * @param string $messenger_name The name of the messenger the settings is being added for. |
|
862 | - * @param array $new_settings An array of settings to update the existing settings. |
|
863 | - */ |
|
864 | - public function add_settings_for_messenger($messenger_name, $new_settings = array()) |
|
865 | - { |
|
866 | - $messenger = $this->get_messenger($messenger_name); |
|
867 | - if ($messenger instanceof EE_messenger) { |
|
868 | - $msgr_settings = $messenger->get_admin_settings_fields(); |
|
869 | - if (! empty($msgr_settings)) { |
|
870 | - foreach ($msgr_settings as $field => $value) { |
|
871 | - // is there a new setting for this? |
|
872 | - if (isset($new_settings[ $field ])) { |
|
873 | - $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $new_settings[ $field ]; |
|
874 | - continue; |
|
875 | - } |
|
876 | - // only set the default if it isn't already set. |
|
877 | - if (! isset($this->_active_message_types[ $messenger->name ]['settings'][ $field ])) { |
|
878 | - $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $value; |
|
879 | - } |
|
880 | - } |
|
881 | - } |
|
882 | - } |
|
883 | - } |
|
884 | - |
|
885 | - |
|
886 | - /** |
|
887 | - * deactivate_messenger |
|
888 | - * |
|
889 | - * @param string|EE_messenger $messenger_name name of messenger |
|
890 | - * @return void |
|
891 | - */ |
|
892 | - public function deactivate_messenger($messenger_name) |
|
893 | - { |
|
894 | - $this->_initialize_collections(); |
|
895 | - if ($messenger_name instanceof EE_messenger) { |
|
896 | - $messenger_name = $messenger_name->name; |
|
897 | - } |
|
898 | - unset($this->_active_messengers[ $messenger_name ]); |
|
899 | - unset($this->_active_message_types[ $messenger_name ]); |
|
900 | - $this->_message_template_group_model->deactivate_message_template_groups_for($messenger_name); |
|
901 | - $this->update_active_messengers_option(); |
|
902 | - } |
|
903 | - |
|
904 | - |
|
905 | - /** |
|
906 | - * Deactivates a message type (note this will deactivate across all messenger's it is active on. |
|
907 | - * |
|
908 | - * @param string $message_type_name name of message type being deactivated |
|
909 | - * @param bool $set_has_active_record By default we always record the has_active record when deactivating a message |
|
910 | - * type. However, this can be overridden if we don't want this set (usually when |
|
911 | - * this is called as a part of deregistration of a custom message type) |
|
912 | - */ |
|
913 | - public function deactivate_message_type($message_type_name, $set_has_active_record = true) |
|
914 | - { |
|
915 | - $this->_initialize_collections(); |
|
916 | - if ($message_type_name instanceof EE_message_type) { |
|
917 | - $message_type_name = $message_type_name->name; |
|
918 | - } |
|
919 | - foreach ($this->_active_message_types as $messenger_name => $settings) { |
|
920 | - unset( |
|
921 | - $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ] |
|
922 | - ); |
|
923 | - |
|
924 | - // we always record (even on deactivation) that a message type has been activated because there should at |
|
925 | - // least be a record in the "has_activated" option that it WAS active at one point. |
|
926 | - if ($set_has_active_record) { |
|
927 | - $messenger = $this->get_messenger($messenger_name); |
|
928 | - $this->_set_messenger_has_activated_message_type($messenger, $message_type_name); |
|
929 | - } |
|
930 | - } |
|
931 | - $this->_message_template_group_model->deactivate_message_template_groups_for('', $message_type_name); |
|
932 | - $this->update_active_messengers_option(); |
|
933 | - $this->update_has_activated_messengers_option(); |
|
934 | - } |
|
935 | - |
|
936 | - |
|
937 | - /** |
|
938 | - * Deactivates a message type for a specific messenger as opposed to all messengers. |
|
939 | - * |
|
940 | - * @param string $message_type_name Name of message type being deactivated. |
|
941 | - * @param string $messenger_name Name of messenger the message type is being deactivated for. |
|
942 | - */ |
|
943 | - public function deactivate_message_type_for_messenger($message_type_name, $messenger_name) |
|
944 | - { |
|
945 | - $this->_initialize_collections(); |
|
946 | - if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
947 | - unset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]); |
|
948 | - } |
|
949 | - $this->_message_template_group_model->deactivate_message_template_groups_for( |
|
950 | - array($messenger_name), |
|
951 | - array($message_type_name) |
|
952 | - ); |
|
953 | - $this->update_active_messengers_option(); |
|
954 | - } |
|
955 | - |
|
956 | - |
|
957 | - /** |
|
958 | - * Used to verify if a message can be sent for the given messenger and message type |
|
959 | - * and that it is a generating messenger (used for generating message templates). |
|
960 | - * |
|
961 | - * @param EE_messenger $messenger messenger used in trigger |
|
962 | - * @param EE_message_type $message_type message type used in trigger |
|
963 | - * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send. |
|
964 | - */ |
|
965 | - public function is_generating_messenger_and_active(EE_messenger $messenger, EE_message_type $message_type) |
|
966 | - { |
|
967 | - // get the $messengers the message type says it can be used with. |
|
968 | - foreach ($message_type->with_messengers() as $generating_messenger => $secondary_messengers) { |
|
969 | - if ( |
|
970 | - $messenger->name === $generating_messenger |
|
971 | - && $this->is_message_type_active_for_messenger($messenger->name, $message_type->name) |
|
972 | - ) { |
|
973 | - return true; |
|
974 | - } |
|
975 | - } |
|
976 | - return false; |
|
977 | - } |
|
978 | - |
|
979 | - |
|
980 | - /** |
|
981 | - * This returns all the contexts that are registered by all message types. |
|
982 | - * If $slugs_only is true, |
|
983 | - * then just an array indexed by unique context slugs with the latest label representation for that slug. |
|
984 | - * array( |
|
985 | - * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
986 | - * ); |
|
987 | - * If $slugs_only is false, then the format is: |
|
988 | - * array( |
|
989 | - * 'message_type_name' => array( |
|
990 | - * 'context_slug' => array( |
|
991 | - * 'label' => 'localized label for context', |
|
992 | - * 'description' => 'localized description for context' |
|
993 | - * ) |
|
994 | - * ) |
|
995 | - * ); |
|
996 | - * Keep in mind that although different message types may share the same context slugs, |
|
997 | - * it is possible that the context is described differently by the message type. |
|
998 | - * |
|
999 | - * @since 4.9.0 |
|
1000 | - * @param bool $slugs_only Whether to return an array of just slugs and labels (true) |
|
1001 | - * or all contexts indexed by message type. |
|
1002 | - * @return array |
|
1003 | - */ |
|
1004 | - public function get_all_contexts($slugs_only = true) |
|
1005 | - { |
|
1006 | - $key = $slugs_only ? 'slugs' : 'all'; |
|
1007 | - // check if contexts has been setup yet. |
|
1008 | - if (empty($this->_contexts[ $key ])) { |
|
1009 | - // So let's get all active message type objects and loop through to get all unique contexts |
|
1010 | - foreach ($this->get_active_message_type_objects() as $message_type) { |
|
1011 | - if ($message_type instanceof EE_message_type) { |
|
1012 | - $message_type_contexts = $message_type->get_contexts(); |
|
1013 | - if ($slugs_only) { |
|
1014 | - foreach ($message_type_contexts as $context => $context_details) { |
|
1015 | - $this->_contexts[ $key ][ $context ] = $context_details['label']; |
|
1016 | - } |
|
1017 | - } else { |
|
1018 | - $this->_contexts[ $key ][ $message_type->name ] = $message_type_contexts; |
|
1019 | - } |
|
1020 | - } |
|
1021 | - } |
|
1022 | - } |
|
1023 | - return ! empty($this->_contexts[ $key ]) ? $this->_contexts[ $key ] : array(); |
|
1024 | - } |
|
1025 | - |
|
1026 | - |
|
1027 | - /** |
|
1028 | - * This checks the internal record of what message types are considered "active" and verifies that |
|
1029 | - * there is an installed class definition for that message type. If the active message type does not have a |
|
1030 | - * corresponding accessible message type class then it will be deactivated from all messengers it is active on and |
|
1031 | - * any related message templates will be inactivated as well. |
|
1032 | - * |
|
1033 | - * @return bool true means all active message types are valid, false means at least one message type was |
|
1034 | - * deactivated. |
|
1035 | - */ |
|
1036 | - public function validate_active_message_types_are_installed() |
|
1037 | - { |
|
1038 | - $list_of_active_message_type_names = $this->list_of_active_message_types(); |
|
1039 | - $installed_message_types = $this->installed_message_types(); |
|
1040 | - $all_message_types_valid = true; |
|
1041 | - // loop through list of active message types and verify they are installed. |
|
1042 | - foreach ($list_of_active_message_type_names as $message_type_name) { |
|
1043 | - if (! isset($installed_message_types[ $message_type_name ])) { |
|
1044 | - $this->remove_message_type_has_been_activated_from_all_messengers( |
|
1045 | - $message_type_name, |
|
1046 | - true |
|
1047 | - ); |
|
1048 | - $this->deactivate_message_type($message_type_name, false); |
|
1049 | - $all_message_types_valid = false; |
|
1050 | - } |
|
1051 | - } |
|
1052 | - return $all_message_types_valid; |
|
1053 | - } |
|
1054 | - |
|
1055 | - |
|
1056 | - /** |
|
1057 | - * This method checks the `ee_has_activated_messenger` option to see if the message type has ever been |
|
1058 | - * activated for the given messenger. This can be called by client code on plugin updates etc to determine whether |
|
1059 | - * to attempt automatically reactivating message types that should be activated by default or not. |
|
1060 | - * |
|
1061 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1062 | - * |
|
1063 | - * @param $message_type_name |
|
1064 | - * @param $messenger_name |
|
1065 | - * @return bool |
|
1066 | - */ |
|
1067 | - public function has_message_type_been_activated_for_messenger($message_type_name, $messenger_name) |
|
1068 | - { |
|
1069 | - $has_activated = $this->get_has_activated_messengers_option(); |
|
1070 | - return isset($has_activated[ $messenger_name ]) |
|
1071 | - && in_array($message_type_name, $has_activated[ $messenger_name ]); |
|
1072 | - } |
|
1073 | - |
|
1074 | - |
|
1075 | - /** |
|
1076 | - * This method unsets a message type from the given messenger has activated option. |
|
1077 | - * |
|
1078 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1079 | - * |
|
1080 | - * @param string $message_type_name |
|
1081 | - * @param string $messenger_name |
|
1082 | - * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1083 | - * If it is currently active, then remove. Otherwise leave it alone. |
|
1084 | - */ |
|
1085 | - public function remove_message_type_has_been_activated_for_messenger( |
|
1086 | - $message_type_name, |
|
1087 | - $messenger_name, |
|
1088 | - $consider_current_state = false |
|
1089 | - ) { |
|
1090 | - if ( |
|
1091 | - $consider_current_state |
|
1092 | - && ! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
1093 | - ) { |
|
1094 | - // when consider current state is true, this means we don't want to change anything on the "has_activated" |
|
1095 | - // record if the message type is currently active for this messenger. This is used when we want to retain |
|
1096 | - // the record for user initiated inactivations of the message type. |
|
1097 | - return; |
|
1098 | - } |
|
1099 | - $has_activated = $this->get_has_activated_messengers_option(); |
|
1100 | - $key_for_message_type = isset($has_activated[ $messenger_name ]) |
|
1101 | - ? array_search($message_type_name, $has_activated[ $messenger_name ], true) |
|
1102 | - : false; |
|
1103 | - if ($key_for_message_type !== false) { |
|
1104 | - unset($has_activated[ $messenger_name ][ $key_for_message_type ]); |
|
1105 | - $this->update_has_activated_messengers_option($has_activated); |
|
1106 | - // reset the internal cached property |
|
1107 | - $this->get_has_activated_messengers_option(true); |
|
1108 | - } |
|
1109 | - } |
|
1110 | - |
|
1111 | - |
|
1112 | - /** |
|
1113 | - * Removes a message type active record from all messengers it is attached to. |
|
1114 | - * |
|
1115 | - * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1116 | - * |
|
1117 | - * @param $message_type_name |
|
1118 | - * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1119 | - * If it is currently active, then remove. Otherwise leave it alone. |
|
1120 | - */ |
|
1121 | - public function remove_message_type_has_been_activated_from_all_messengers( |
|
1122 | - $message_type_name, |
|
1123 | - $consider_current_state = false |
|
1124 | - ) { |
|
1125 | - foreach (array_keys($this->get_has_activated_messengers_option()) as $messenger_name) { |
|
1126 | - $this->remove_message_type_has_been_activated_for_messenger( |
|
1127 | - $message_type_name, |
|
1128 | - $messenger_name, |
|
1129 | - $consider_current_state |
|
1130 | - ); |
|
1131 | - } |
|
1132 | - } |
|
15 | + /** |
|
16 | + * This option in the database is used to keep a record of message types that have been activated for a messenger |
|
17 | + * at some point in the history of the site. It is utilized by the implementation of the 'force' flag in |
|
18 | + * EE_Register_Message_Type. The force flag is an indication of whether a message type should be activated by |
|
19 | + * default when the message type is registered. However, if a user has explicitly deactivated a message type, then |
|
20 | + * the force flag is ignored. The method by which the code knows whether to ignore this flag is via this option. |
|
21 | + * Note, that this is NOT a historical record. Its entirely possible for a message type to have been activated for |
|
22 | + * a messenger and yet not have a record in this option. This occurs when a message type is inactivated through an |
|
23 | + * automated process (when an add-on registering the message type deactivates, or when some other code calls the |
|
24 | + * EE_Registery_Message_Type::deregister method) and the related record(s) is(are) removed from this option to ensure |
|
25 | + * the "force" flag is respected if that message type is later re-registered. |
|
26 | + * |
|
27 | + * This option should NOT be used to determine the current "active" state of a message type for a given messenger. |
|
28 | + * |
|
29 | + * The name of this option (and related methods/properties) is due to matching the original intended purpose for the |
|
30 | + * option that got superseded by later behaviour requirements. |
|
31 | + */ |
|
32 | + const HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME = 'ee_has_activated_messenger'; |
|
33 | + |
|
34 | + /** |
|
35 | + * @type boolean $_initialized |
|
36 | + */ |
|
37 | + protected $_initialized = false; |
|
38 | + |
|
39 | + /** |
|
40 | + * @type EE_Messenger_Collection $_messenger_collection_loader |
|
41 | + */ |
|
42 | + protected $_messenger_collection_loader; |
|
43 | + |
|
44 | + /** |
|
45 | + * @type EE_Message_Type_Collection $_message_type_collection_loader |
|
46 | + */ |
|
47 | + protected $_message_type_collection_loader; |
|
48 | + |
|
49 | + /** |
|
50 | + * @type EEM_Message_Template_Group $_message_template_group_model |
|
51 | + */ |
|
52 | + protected $_message_template_group_model; |
|
53 | + |
|
54 | + /** |
|
55 | + * @type EE_messenger[] |
|
56 | + */ |
|
57 | + protected $_installed_messengers = array(); |
|
58 | + |
|
59 | + /** |
|
60 | + * @type EE_message_type[] |
|
61 | + */ |
|
62 | + protected $_installed_message_types = array(); |
|
63 | + |
|
64 | + /** |
|
65 | + * Array of active messengers. |
|
66 | + * Format is this: |
|
67 | + * array( |
|
68 | + * 'messenger_name' => EE_messenger |
|
69 | + * ) |
|
70 | + * |
|
71 | + * @type EE_messenger[] |
|
72 | + */ |
|
73 | + protected $_active_messengers = array(); |
|
74 | + |
|
75 | + /** |
|
76 | + * Formatted array of active message types grouped per messenger. |
|
77 | + * Format is this: |
|
78 | + * array( |
|
79 | + * 'messenger_name' => array( |
|
80 | + * 'settings' => array( |
|
81 | + * '{messenger_name}-message_types' => array( |
|
82 | + * 'message_type_name' => array() //variable array of settings corresponding to message type. |
|
83 | + * ) |
|
84 | + * ) |
|
85 | + * ) |
|
86 | + * ) |
|
87 | + * |
|
88 | + * @type array |
|
89 | + */ |
|
90 | + protected $_active_message_types = array(); |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * This holds the array of messengers and their corresponding message types that have |
|
95 | + * been activated on a site at some point. This is an important record that helps the messages system |
|
96 | + * not accidentally reactivate something that was intentionally deactivated by a user. |
|
97 | + * |
|
98 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
99 | + * |
|
100 | + * @type array |
|
101 | + */ |
|
102 | + protected $_has_activated_messengers_and_message_types = array(); |
|
103 | + |
|
104 | + /** |
|
105 | + * An array of unique message type contexts across all active message types. |
|
106 | + * The array will be indexed by either 'slugs' or 'all'. |
|
107 | + * The slugs index contains an array indexed by unique context slugs with the latest label representation for that |
|
108 | + * slug. array( |
|
109 | + * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
110 | + * ); |
|
111 | + * The all index returns an array in this format: |
|
112 | + * array( |
|
113 | + * 'message_type_name' => array( |
|
114 | + * 'context_slug' => array( |
|
115 | + * 'label' => 'localized label for context', |
|
116 | + * 'description' => 'localized description for context' |
|
117 | + * ) |
|
118 | + * ) |
|
119 | + * ); |
|
120 | + * |
|
121 | + * @type array |
|
122 | + */ |
|
123 | + protected $_contexts = array(); |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * EE_Message_Resource_Manager constructor. |
|
128 | + * |
|
129 | + * @param \EE_Messenger_Collection_Loader $Messenger_Collection_Loader |
|
130 | + * @param \EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader |
|
131 | + * @param \EEM_Message_Template_Group $Message_Template_Group_Model |
|
132 | + */ |
|
133 | + public function __construct( |
|
134 | + EE_Messenger_Collection_Loader $Messenger_Collection_Loader, |
|
135 | + EE_Message_Type_Collection_Loader $Message_Type_Collection_Loader, |
|
136 | + EEM_Message_Template_Group $Message_Template_Group_Model |
|
137 | + ) { |
|
138 | + $this->_messenger_collection_loader = $Messenger_Collection_Loader; |
|
139 | + $this->_message_type_collection_loader = $Message_Type_Collection_Loader; |
|
140 | + $this->_message_template_group_model = $Message_Template_Group_Model; |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * @return void |
|
146 | + */ |
|
147 | + protected function _initialize_collections() |
|
148 | + { |
|
149 | + if ($this->_initialized) { |
|
150 | + return; |
|
151 | + } |
|
152 | + $this->_initialized = true; |
|
153 | + $this->_messenger_collection_loader->load_messengers_from_folder(); |
|
154 | + $this->_message_type_collection_loader->load_message_types_from_folder(); |
|
155 | + $this->get_has_activated_messengers_option(true); |
|
156 | + $this->_set_active_messengers_and_message_types(); |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * @return EE_Messenger_Collection |
|
162 | + */ |
|
163 | + public function messenger_collection() |
|
164 | + { |
|
165 | + $this->_initialize_collections(); |
|
166 | + return $this->_messenger_collection_loader->messenger_collection(); |
|
167 | + } |
|
168 | + |
|
169 | + |
|
170 | + /** |
|
171 | + * @return EE_messenger[] |
|
172 | + */ |
|
173 | + public function active_messengers() |
|
174 | + { |
|
175 | + $this->_initialize_collections(); |
|
176 | + return $this->_active_messengers; |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * @param string $messenger_name |
|
182 | + * @return \EE_messenger |
|
183 | + */ |
|
184 | + public function get_messenger($messenger_name) |
|
185 | + { |
|
186 | + return $this->messenger_collection()->get_by_info($messenger_name); |
|
187 | + } |
|
188 | + |
|
189 | + |
|
190 | + /** |
|
191 | + * This returns the corresponding EE_messenger object for the given string if it is active. |
|
192 | + * |
|
193 | + * @param string $messenger |
|
194 | + * @return EE_messenger | null |
|
195 | + */ |
|
196 | + public function get_active_messenger($messenger) |
|
197 | + { |
|
198 | + $this->_initialize_collections(); |
|
199 | + return ! empty($this->_active_messengers[ $messenger ]) ? $this->_active_messengers[ $messenger ] : null; |
|
200 | + } |
|
201 | + |
|
202 | + |
|
203 | + /** |
|
204 | + * @return \EE_messenger[] |
|
205 | + */ |
|
206 | + public function installed_messengers() |
|
207 | + { |
|
208 | + if (empty($this->_installed_messengers)) { |
|
209 | + $this->_installed_messengers = array(); |
|
210 | + $this->messenger_collection()->rewind(); |
|
211 | + while ($this->messenger_collection()->valid()) { |
|
212 | + $this->_installed_messengers[ $this->messenger_collection()->current()->name ] = $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 = array(); |
|
322 | + if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
323 | + $settings = isset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings']) |
|
324 | + ? $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] |
|
325 | + : array(); |
|
326 | + } |
|
327 | + return $settings; |
|
328 | + } |
|
329 | + |
|
330 | + |
|
331 | + /** |
|
332 | + * Returns whether the given messenger name has active message types on it. |
|
333 | + * Infers whether the messenger is active or not as well. |
|
334 | + * |
|
335 | + * @param string $messenger_name |
|
336 | + * @return bool |
|
337 | + */ |
|
338 | + public function messenger_has_active_message_types($messenger_name) |
|
339 | + { |
|
340 | + $this->_initialize_collections(); |
|
341 | + return |
|
342 | + ! empty($this->_active_message_types[ $messenger_name ]) |
|
343 | + && ! empty($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ]); |
|
344 | + } |
|
345 | + |
|
346 | + |
|
347 | + /** |
|
348 | + * This checks the _active_message_types property for any active message types |
|
349 | + * that are present for the given messenger and returns them. |
|
350 | + * |
|
351 | + * @since 4.9.0 |
|
352 | + * @param string $messenger_name The messenger being checked |
|
353 | + * @return EE_message_type[]|array (empty array if no active_message_types) |
|
354 | + */ |
|
355 | + public function get_active_message_types_for_messenger($messenger_name) |
|
356 | + { |
|
357 | + $message_types = array(); |
|
358 | + if (! $this->messenger_has_active_message_types($messenger_name)) { |
|
359 | + return $message_types; |
|
360 | + } |
|
361 | + $installed_message_types = $this->installed_message_types(); |
|
362 | + foreach ($installed_message_types as $message_type_name => $message_type) { |
|
363 | + if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
364 | + $message_types[ $message_type_name ] = $message_type; |
|
365 | + } |
|
366 | + } |
|
367 | + return $message_types; |
|
368 | + } |
|
369 | + |
|
370 | + |
|
371 | + /** |
|
372 | + * This does NOT return the _active_message_types property but |
|
373 | + * simply returns an array of active message type names from that property. |
|
374 | + * (The _active_message_types property is indexed by messenger and active message_types per messenger). |
|
375 | + * |
|
376 | + * @return array message_type references (string) |
|
377 | + */ |
|
378 | + public function list_of_active_message_types() |
|
379 | + { |
|
380 | + $active_message_type_names = array(); |
|
381 | + $this->_initialize_collections(); |
|
382 | + foreach ($this->_active_message_types as $messenger => $messenger_settings) { |
|
383 | + if (! isset($messenger_settings['settings'][ $messenger . '-message_types' ])) { |
|
384 | + continue; |
|
385 | + } |
|
386 | + foreach ($messenger_settings['settings'][ $messenger . '-message_types' ] as $message_type_name => $message_type_config) { |
|
387 | + if (! in_array($message_type_name, $active_message_type_names)) { |
|
388 | + $active_message_type_names[] = $message_type_name; |
|
389 | + } |
|
390 | + } |
|
391 | + } |
|
392 | + return $active_message_type_names; |
|
393 | + } |
|
394 | + |
|
395 | + |
|
396 | + /** |
|
397 | + * Same as list_of_active_message_types() except this returns actual EE_message_type objects |
|
398 | + * |
|
399 | + * @since 4.9.0 |
|
400 | + * @return \EE_message_type[] |
|
401 | + */ |
|
402 | + public function get_active_message_type_objects() |
|
403 | + { |
|
404 | + $active_message_types = array(); |
|
405 | + $installed_message_types = $this->installed_message_types(); |
|
406 | + $active_message_type_names = $this->list_of_active_message_types(); |
|
407 | + foreach ($active_message_type_names as $active_message_type_name) { |
|
408 | + if (isset($installed_message_types[ $active_message_type_name ])) { |
|
409 | + $active_message_types[ $active_message_type_name ] = $installed_message_types[ $active_message_type_name ]; |
|
410 | + } |
|
411 | + } |
|
412 | + return $active_message_types; |
|
413 | + } |
|
414 | + |
|
415 | + |
|
416 | + /** |
|
417 | + * @return \EE_message_type[] |
|
418 | + */ |
|
419 | + public function installed_message_types() |
|
420 | + { |
|
421 | + if (empty($this->_installed_message_types)) { |
|
422 | + $this->message_type_collection()->rewind(); |
|
423 | + while ($this->message_type_collection()->valid()) { |
|
424 | + $this->_installed_message_types[ $this->message_type_collection()->current()->name ] = $this->message_type_collection()->current(); |
|
425 | + $this->message_type_collection()->next(); |
|
426 | + } |
|
427 | + } |
|
428 | + return $this->_installed_message_types; |
|
429 | + } |
|
430 | + |
|
431 | + |
|
432 | + /** |
|
433 | + * @param string $message_type_name |
|
434 | + * @return \EE_message_type |
|
435 | + * @throws EE_Error |
|
436 | + */ |
|
437 | + public function valid_message_type($message_type_name) |
|
438 | + { |
|
439 | + $message_type = $this->get_message_type($message_type_name); |
|
440 | + if ($message_type instanceof EE_message_type) { |
|
441 | + return $message_type; |
|
442 | + } |
|
443 | + throw new EE_Error( |
|
444 | + sprintf( |
|
445 | + esc_html__('The "%1$s" message type is either invalid or not installed', 'event_espresso'), |
|
446 | + $message_type_name |
|
447 | + ) |
|
448 | + ); |
|
449 | + } |
|
450 | + |
|
451 | + |
|
452 | + /** |
|
453 | + * valid_message_type_for_messenger |
|
454 | + * |
|
455 | + * @param EE_messenger $messenger |
|
456 | + * @param string $message_type_name |
|
457 | + * @return boolean |
|
458 | + * @throws EE_Error |
|
459 | + */ |
|
460 | + public function valid_message_type_for_messenger(EE_messenger $messenger, $message_type_name) |
|
461 | + { |
|
462 | + $valid_message_types = $messenger->get_valid_message_types(); |
|
463 | + if (! in_array($message_type_name, $valid_message_types)) { |
|
464 | + throw new EE_Error( |
|
465 | + sprintf( |
|
466 | + esc_html__( |
|
467 | + '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.', |
|
468 | + 'event_espresso' |
|
469 | + ), |
|
470 | + $message_type_name, |
|
471 | + __METHOD__, |
|
472 | + $messenger->name |
|
473 | + ) |
|
474 | + ); |
|
475 | + } |
|
476 | + return true; |
|
477 | + } |
|
478 | + |
|
479 | + |
|
480 | + /** |
|
481 | + * Used to return active messengers array stored in the wp options table. |
|
482 | + * If no value is present in the option then an empty array is returned. |
|
483 | + * |
|
484 | + * @param bool $reset If true then we ignore whether the option is cached on the _active_message_types |
|
485 | + * property and pull directly from the db. Otherwise whatever is currently on the |
|
486 | + * $_active_message_types property is pulled. |
|
487 | + * @return array |
|
488 | + */ |
|
489 | + public function get_active_messengers_option($reset = false) |
|
490 | + { |
|
491 | + if ($reset) { |
|
492 | + $this->_active_message_types = get_option('ee_active_messengers', array()); |
|
493 | + } |
|
494 | + return $this->_active_message_types; |
|
495 | + } |
|
496 | + |
|
497 | + |
|
498 | + /** |
|
499 | + * Used to update the active messengers array stored in the wp options table. |
|
500 | + * |
|
501 | + * @param array $active_messenger_settings Incoming data to save. If empty, then the internal cached property |
|
502 | + * representing this data is used. |
|
503 | + * @return bool FALSE if not updated, TRUE if updated. |
|
504 | + */ |
|
505 | + public function update_active_messengers_option($active_messenger_settings = array()) |
|
506 | + { |
|
507 | + $active_messenger_settings = empty($active_messenger_settings) ? $this->_active_message_types : $active_messenger_settings; |
|
508 | + // make sure _active_message_types is updated (this is the internal cache for the settings). |
|
509 | + $this->_active_message_types = $active_messenger_settings; |
|
510 | + return update_option('ee_active_messengers', $active_messenger_settings); |
|
511 | + } |
|
512 | + |
|
513 | + |
|
514 | + /** |
|
515 | + * Used to return has activated message types for messengers array stored in the wp options table. |
|
516 | + * If no value is present in the option then an empty array is returned. |
|
517 | + * The value is cached on the $_has_activated_messengers_and_message_types property for future calls. |
|
518 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
519 | + * |
|
520 | + * @param bool $reset Used to indicate that any cached value should be ignored. |
|
521 | + * @return array |
|
522 | + */ |
|
523 | + public function get_has_activated_messengers_option($reset = false) |
|
524 | + { |
|
525 | + if ($reset || empty($this->_has_activated_messengers_and_message_types)) { |
|
526 | + $this->_has_activated_messengers_and_message_types = get_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, array()); |
|
527 | + } |
|
528 | + return $this->_has_activated_messengers_and_message_types; |
|
529 | + } |
|
530 | + |
|
531 | + |
|
532 | + /** |
|
533 | + * Used to update the has activated option in the db. |
|
534 | + * |
|
535 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
536 | + * |
|
537 | + * @param array $has_activated_messengers Incoming data to save. If empty, then the internal cached property |
|
538 | + * representing this data is used. |
|
539 | + * @return bool FALSE if not updated, TRUE if updated. |
|
540 | + */ |
|
541 | + public function update_has_activated_messengers_option($has_activated_messengers = array()) |
|
542 | + { |
|
543 | + // make sure the option has been retrieved from first so we don't overwrite it accidentally. |
|
544 | + if (empty($has_activated_messengers) && empty($this->_has_activated_messengers_and_message_types)) { |
|
545 | + $this->get_has_activated_messengers_option(); |
|
546 | + } |
|
547 | + $has_activated_messengers = empty($has_activated_messengers) |
|
548 | + ? $this->_has_activated_messengers_and_message_types |
|
549 | + : $has_activated_messengers; |
|
550 | + return update_option(self::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME, $has_activated_messengers); |
|
551 | + } |
|
552 | + |
|
553 | + |
|
554 | + /** |
|
555 | + * wrapper for _set_active_messengers_and_message_types() |
|
556 | + */ |
|
557 | + public function reset_active_messengers_and_message_types() |
|
558 | + { |
|
559 | + $this->_set_active_messengers_and_message_types(); |
|
560 | + } |
|
561 | + |
|
562 | + |
|
563 | + /** |
|
564 | + * Generate list of active messengers and message types from collection. |
|
565 | + * This sets up the active messengers from what is present in the database. |
|
566 | + */ |
|
567 | + protected function _set_active_messengers_and_message_types() |
|
568 | + { |
|
569 | + // echo "\n\n " . __LINE__ . ") " . __METHOD__ . "() \n"; |
|
570 | + // list of activated messengers as set via the admin |
|
571 | + // note calling `get_active_messengers_options` also initializes the _active_message_types property. |
|
572 | + $this->get_active_messengers_option(true); |
|
573 | + $this->ensure_messengers_are_active(array(), false, true); |
|
574 | + $this->update_active_messengers_option(); |
|
575 | + $this->update_has_activated_messengers_option(); |
|
576 | + } |
|
577 | + |
|
578 | + |
|
579 | + /** |
|
580 | + * Ensures that the specified messenger is currently active. |
|
581 | + * If not, activates it and its default message types. |
|
582 | + * |
|
583 | + * @param string $messenger_name |
|
584 | + * @param bool $update_option Whether to update the option in the db or not. |
|
585 | + * @return boolean true if either already active or successfully activated. |
|
586 | + */ |
|
587 | + public function ensure_messenger_is_active($messenger_name, $update_option = true) |
|
588 | + { |
|
589 | + if (! isset($this->_active_messengers[ $messenger_name ])) { |
|
590 | + try { |
|
591 | + $this->activate_messenger($messenger_name, array(), $update_option); |
|
592 | + } catch (EE_Error $e) { |
|
593 | + EE_Error::add_error( |
|
594 | + $e->getMessage(), |
|
595 | + __FILE__, |
|
596 | + __FUNCTION__, |
|
597 | + __LINE__ |
|
598 | + ); |
|
599 | + return false; |
|
600 | + } |
|
601 | + } |
|
602 | + return true; |
|
603 | + } |
|
604 | + |
|
605 | + |
|
606 | + /** |
|
607 | + * This ensures the given array of messenger names is active in the system. |
|
608 | + * Note, this method will not activate any NEW message types for the messenger when it is called. Instead, |
|
609 | + * it will automatically activate the default message types for the messenger if its not active. |
|
610 | + * |
|
611 | + * @param array $messenger_names Array of messenger names for messengers to be activated. If an empty array |
|
612 | + * (default) then will attempt to set the active messengers from the |
|
613 | + * activated_messengers option |
|
614 | + * (stored in $_active_message_types property). |
|
615 | + * @param bool $update_option Whether to update the related active messengers option. |
|
616 | + * @param bool $verify Whether to verify the messengers are installed before activating. Note if this is |
|
617 | + * set to true and a messenger is indicated as active, but is NOT installed, then it |
|
618 | + * will automatically be deactivated. |
|
619 | + */ |
|
620 | + public function ensure_messengers_are_active($messenger_names = array(), $update_option = true, $verify = false) |
|
621 | + { |
|
622 | + $messenger_names = empty($messenger_names) ? array_keys($this->_active_message_types) : $messenger_names; |
|
623 | + |
|
624 | + $not_installed = array(); |
|
625 | + foreach ($messenger_names as $messenger_name) { |
|
626 | + if ($verify && ! $this->messenger_collection()->has_by_name($messenger_name)) { |
|
627 | + $not_installed[] = $messenger_name; |
|
628 | + $this->deactivate_messenger($messenger_name); |
|
629 | + continue; |
|
630 | + } |
|
631 | + $this->ensure_messenger_is_active($messenger_name, $update_option); |
|
632 | + } |
|
633 | + |
|
634 | + if (! empty($not_installed)) { |
|
635 | + EE_Error::add_error( |
|
636 | + sprintf( |
|
637 | + esc_html__('The following messengers are either not installed or are invalid:%1$s %2$s', 'event_espresso'), |
|
638 | + '<br />', |
|
639 | + implode(', ', $not_installed) |
|
640 | + ), |
|
641 | + __FILE__, |
|
642 | + __FUNCTION__, |
|
643 | + __LINE__ |
|
644 | + ); |
|
645 | + } |
|
646 | + } |
|
647 | + |
|
648 | + |
|
649 | + /** |
|
650 | + * Ensures that the specified message type for the given messenger is currently active, if not activates it. |
|
651 | + * This ALSO ensures that the given messenger is active as well! |
|
652 | + * |
|
653 | + * @param string $message_type_name message type name. |
|
654 | + * @param $messenger_name |
|
655 | + * @param bool $update_option Whether to update the option in the db or not. |
|
656 | + * @return bool Returns true if already is active or if was activated successfully. |
|
657 | + * @throws EE_Error |
|
658 | + */ |
|
659 | + public function ensure_message_type_is_active($message_type_name, $messenger_name, $update_option = true) |
|
660 | + { |
|
661 | + // grab the messenger to work with. |
|
662 | + $messenger = $this->valid_messenger($messenger_name); |
|
663 | + if ($this->valid_message_type_for_messenger($messenger, $message_type_name)) { |
|
664 | + // ensure messenger is active (that's an inherent coupling between active message types and the |
|
665 | + // messenger they are being activated for. |
|
666 | + try { |
|
667 | + if (! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
668 | + // all is good so let's just get it active |
|
669 | + $this->activate_messenger($messenger, array($message_type_name), $update_option); |
|
670 | + } |
|
671 | + } catch (EE_Error $e) { |
|
672 | + EE_Error::add_error( |
|
673 | + $e->getMessage(), |
|
674 | + __FILE__, |
|
675 | + __FUNCTION__, |
|
676 | + __LINE__ |
|
677 | + ); |
|
678 | + return false; |
|
679 | + } |
|
680 | + } |
|
681 | + return true; |
|
682 | + } |
|
683 | + |
|
684 | + |
|
685 | + /** |
|
686 | + * This is a wrapper for `ensure_message_type_is_active` that will handle ensuring multiple message types for a |
|
687 | + * messenger are active in one go. |
|
688 | + * |
|
689 | + * @param array $message_type_names Array of message type names to ensure are active. |
|
690 | + * @param string $messenger_name The name of the messenger that the message types are to be activated on. |
|
691 | + * @param bool $update_option Whether to persist the activation to the database or not (default true). |
|
692 | + */ |
|
693 | + public function ensure_message_types_are_active($message_type_names, $messenger_name, $update_option = true) |
|
694 | + { |
|
695 | + $message_type_names = (array) $message_type_names; |
|
696 | + foreach ($message_type_names as $message_type_name) { |
|
697 | + // note, intentionally not updating option here because we're in a loop. |
|
698 | + // We'll follow the instructions of the incoming $update_option argument after the loop. |
|
699 | + $this->ensure_message_type_is_active($message_type_name, $messenger_name, false); |
|
700 | + } |
|
701 | + if ($update_option) { |
|
702 | + $this->update_active_messengers_option(); |
|
703 | + $this->update_has_activated_messengers_option(); |
|
704 | + } |
|
705 | + } |
|
706 | + |
|
707 | + |
|
708 | + /** |
|
709 | + * Activates the specified messenger. |
|
710 | + * |
|
711 | + * @param EE_messenger|string $messenger Instantiated EE_messenger OR messenger name if not already loaded! |
|
712 | + * @param array $message_type_names An array of message type names to activate with this messenger. |
|
713 | + * If included we do NOT setup the default message types |
|
714 | + * (assuming they are already setup.) |
|
715 | + * @param bool $update_active_messengers_option |
|
716 | + * @return array of generated templates |
|
717 | + * @throws EE_Error |
|
718 | + */ |
|
719 | + public function activate_messenger( |
|
720 | + $messenger, |
|
721 | + $message_type_names = array(), |
|
722 | + $update_active_messengers_option = true |
|
723 | + ) { |
|
724 | + $templates = array(); |
|
725 | + // grab the messenger to work with. |
|
726 | + $messenger = $messenger instanceof EE_messenger |
|
727 | + ? $messenger |
|
728 | + : $this->messenger_collection()->get_by_info($messenger); |
|
729 | + // it's inactive. Activate it. |
|
730 | + if ($messenger instanceof EE_messenger) { |
|
731 | + $this->_active_messengers[ $messenger->name ] = $messenger; |
|
732 | + // activate incoming message types set to be activated with messenger. |
|
733 | + $message_type_names = $this->_activate_message_types($messenger, $message_type_names); |
|
734 | + // setup any initial settings for the messenger if necessary. |
|
735 | + $this->add_settings_for_messenger($messenger->name); |
|
736 | + if ($update_active_messengers_option) { |
|
737 | + $this->update_active_messengers_option(); |
|
738 | + $this->update_has_activated_messengers_option(); |
|
739 | + } |
|
740 | + // generate new templates if necessary and ensure all related templates that are already in the database are |
|
741 | + // marked active. Note, this will also deactivate a message type for a messenger if the template |
|
742 | + // cannot be successfully created during its attempt (only happens for global template attempts). |
|
743 | + if (! empty($message_type_names)) { |
|
744 | + $templates = EEH_MSG_Template::generate_new_templates($messenger->name, $message_type_names, 0, true); |
|
745 | + EEH_MSG_Template::update_to_active(array($messenger->name), $message_type_names); |
|
746 | + } |
|
747 | + } |
|
748 | + return $templates; |
|
749 | + } |
|
750 | + |
|
751 | + |
|
752 | + /** |
|
753 | + * Activates given message types for the given EE_messenger object. |
|
754 | + * Note: (very important) This method does not persist the activation to the database. |
|
755 | + * See code implementing this method in this class for examples of how to persist. |
|
756 | + * |
|
757 | + * @param \EE_messenger $messenger |
|
758 | + * @param array $message_type_names |
|
759 | + * @return array |
|
760 | + */ |
|
761 | + protected function _activate_message_types(EE_messenger $messenger, $message_type_names = array()) |
|
762 | + { |
|
763 | + // If $message_type_names is empty, AND $this->_active_message_types is empty, then that means |
|
764 | + // things have never been initialized (which should happen on EEH_Activation::generate_message_templates). |
|
765 | + // So ONLY then do we need to actually grab defaults and cycle through them. Otherwise we |
|
766 | + // only override _active_message_types when an explicit array of $message_type_names has been provided. |
|
767 | + $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[ $messenger->name ]) |
|
768 | + ? $messenger->get_default_message_types() |
|
769 | + : (array) $message_type_names; |
|
770 | + |
|
771 | + // now we ALWAYS need to make sure that the messenger is active for the message types we're activating! |
|
772 | + if (! isset($this->_active_message_types[ $messenger->name ])) { |
|
773 | + $this->_active_message_types[ $messenger->name ]['settings'] = array(); |
|
774 | + } |
|
775 | + |
|
776 | + if ($message_type_names) { |
|
777 | + // cycle thru message types |
|
778 | + foreach ($message_type_names as $message_type_name) { |
|
779 | + // only register the message type as active IF it isn't already active |
|
780 | + // and if its actually installed. |
|
781 | + if ( |
|
782 | + ! $this->is_message_type_active_for_messenger($messenger->name, $message_type_name) |
|
783 | + ) { |
|
784 | + $this->add_settings_for_message_type($messenger->name, $message_type_name); |
|
785 | + $this->_set_messenger_has_activated_message_type( |
|
786 | + $messenger, |
|
787 | + $message_type_name |
|
788 | + ); |
|
789 | + } |
|
790 | + } |
|
791 | + } |
|
792 | + return $message_type_names; |
|
793 | + } |
|
794 | + |
|
795 | + |
|
796 | + /** |
|
797 | + * add_settings_for_message_type |
|
798 | + * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
799 | + * $this->update_active_messengers_option to persist. |
|
800 | + * |
|
801 | + * @param string $messenger_name The name of the messenger adding the settings for |
|
802 | + * @param string $message_type_name The name of the message type adding the settings for |
|
803 | + * @param array $new_settings Any new settings being set for the message type and messenger |
|
804 | + */ |
|
805 | + public function add_settings_for_message_type($messenger_name, $message_type_name, $new_settings = array()) |
|
806 | + { |
|
807 | + // get installed message type from collection |
|
808 | + $message_type = $this->message_type_collection()->get_by_info($message_type_name); |
|
809 | + $existing_settings = $this->get_message_type_settings_for_messenger($messenger_name, $message_type_name); |
|
810 | + // we need to setup any initial settings for message types |
|
811 | + if ($message_type instanceof EE_message_type) { |
|
812 | + $default_settings = $message_type->get_admin_settings_fields(); |
|
813 | + foreach ($default_settings as $field => $values) { |
|
814 | + if (isset($new_settings[ $field ])) { |
|
815 | + $existing_settings[ $field ] = $new_settings[ $field ]; |
|
816 | + continue; |
|
817 | + } |
|
818 | + if (! isset($existing_settings[ $field ])) { |
|
819 | + $existing_settings[ $field ] = $values['default']; |
|
820 | + } |
|
821 | + } |
|
822 | + } |
|
823 | + $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] = $existing_settings; |
|
824 | + } |
|
825 | + |
|
826 | + |
|
827 | + /** |
|
828 | + * Updates the internal cached _has_activated_messengers_and_message_types property with the given messenger |
|
829 | + * and message type. |
|
830 | + * |
|
831 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
832 | + * |
|
833 | + * @access protected |
|
834 | + * @param \EE_messenger $messenger |
|
835 | + * @param string $message_type_name |
|
836 | + */ |
|
837 | + protected function _set_messenger_has_activated_message_type(EE_messenger $messenger, $message_type_name) |
|
838 | + { |
|
839 | + |
|
840 | + // if _has_activated_messengers_and_message_types is empty then lets ensure its initialized |
|
841 | + if (empty($this->_has_activated_messengers_and_message_types)) { |
|
842 | + $this->get_has_activated_messengers_option(); |
|
843 | + } |
|
844 | + |
|
845 | + // make sure this messenger has a record in the has_activated array |
|
846 | + if (! isset($this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
847 | + $this->_has_activated_messengers_and_message_types[ $messenger->name ] = array(); |
|
848 | + } |
|
849 | + // check if message type has already been added |
|
850 | + if (! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
851 | + $this->_has_activated_messengers_and_message_types[ $messenger->name ][] = $message_type_name; |
|
852 | + } |
|
853 | + } |
|
854 | + |
|
855 | + |
|
856 | + /** |
|
857 | + * add_settings_for_messenger |
|
858 | + * NOTE This does NOT automatically persist any settings to the db. Client code should call |
|
859 | + * $this->update_active_messengers_option to persist. |
|
860 | + * |
|
861 | + * @param string $messenger_name The name of the messenger the settings is being added for. |
|
862 | + * @param array $new_settings An array of settings to update the existing settings. |
|
863 | + */ |
|
864 | + public function add_settings_for_messenger($messenger_name, $new_settings = array()) |
|
865 | + { |
|
866 | + $messenger = $this->get_messenger($messenger_name); |
|
867 | + if ($messenger instanceof EE_messenger) { |
|
868 | + $msgr_settings = $messenger->get_admin_settings_fields(); |
|
869 | + if (! empty($msgr_settings)) { |
|
870 | + foreach ($msgr_settings as $field => $value) { |
|
871 | + // is there a new setting for this? |
|
872 | + if (isset($new_settings[ $field ])) { |
|
873 | + $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $new_settings[ $field ]; |
|
874 | + continue; |
|
875 | + } |
|
876 | + // only set the default if it isn't already set. |
|
877 | + if (! isset($this->_active_message_types[ $messenger->name ]['settings'][ $field ])) { |
|
878 | + $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $value; |
|
879 | + } |
|
880 | + } |
|
881 | + } |
|
882 | + } |
|
883 | + } |
|
884 | + |
|
885 | + |
|
886 | + /** |
|
887 | + * deactivate_messenger |
|
888 | + * |
|
889 | + * @param string|EE_messenger $messenger_name name of messenger |
|
890 | + * @return void |
|
891 | + */ |
|
892 | + public function deactivate_messenger($messenger_name) |
|
893 | + { |
|
894 | + $this->_initialize_collections(); |
|
895 | + if ($messenger_name instanceof EE_messenger) { |
|
896 | + $messenger_name = $messenger_name->name; |
|
897 | + } |
|
898 | + unset($this->_active_messengers[ $messenger_name ]); |
|
899 | + unset($this->_active_message_types[ $messenger_name ]); |
|
900 | + $this->_message_template_group_model->deactivate_message_template_groups_for($messenger_name); |
|
901 | + $this->update_active_messengers_option(); |
|
902 | + } |
|
903 | + |
|
904 | + |
|
905 | + /** |
|
906 | + * Deactivates a message type (note this will deactivate across all messenger's it is active on. |
|
907 | + * |
|
908 | + * @param string $message_type_name name of message type being deactivated |
|
909 | + * @param bool $set_has_active_record By default we always record the has_active record when deactivating a message |
|
910 | + * type. However, this can be overridden if we don't want this set (usually when |
|
911 | + * this is called as a part of deregistration of a custom message type) |
|
912 | + */ |
|
913 | + public function deactivate_message_type($message_type_name, $set_has_active_record = true) |
|
914 | + { |
|
915 | + $this->_initialize_collections(); |
|
916 | + if ($message_type_name instanceof EE_message_type) { |
|
917 | + $message_type_name = $message_type_name->name; |
|
918 | + } |
|
919 | + foreach ($this->_active_message_types as $messenger_name => $settings) { |
|
920 | + unset( |
|
921 | + $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ] |
|
922 | + ); |
|
923 | + |
|
924 | + // we always record (even on deactivation) that a message type has been activated because there should at |
|
925 | + // least be a record in the "has_activated" option that it WAS active at one point. |
|
926 | + if ($set_has_active_record) { |
|
927 | + $messenger = $this->get_messenger($messenger_name); |
|
928 | + $this->_set_messenger_has_activated_message_type($messenger, $message_type_name); |
|
929 | + } |
|
930 | + } |
|
931 | + $this->_message_template_group_model->deactivate_message_template_groups_for('', $message_type_name); |
|
932 | + $this->update_active_messengers_option(); |
|
933 | + $this->update_has_activated_messengers_option(); |
|
934 | + } |
|
935 | + |
|
936 | + |
|
937 | + /** |
|
938 | + * Deactivates a message type for a specific messenger as opposed to all messengers. |
|
939 | + * |
|
940 | + * @param string $message_type_name Name of message type being deactivated. |
|
941 | + * @param string $messenger_name Name of messenger the message type is being deactivated for. |
|
942 | + */ |
|
943 | + public function deactivate_message_type_for_messenger($message_type_name, $messenger_name) |
|
944 | + { |
|
945 | + $this->_initialize_collections(); |
|
946 | + if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
947 | + unset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]); |
|
948 | + } |
|
949 | + $this->_message_template_group_model->deactivate_message_template_groups_for( |
|
950 | + array($messenger_name), |
|
951 | + array($message_type_name) |
|
952 | + ); |
|
953 | + $this->update_active_messengers_option(); |
|
954 | + } |
|
955 | + |
|
956 | + |
|
957 | + /** |
|
958 | + * Used to verify if a message can be sent for the given messenger and message type |
|
959 | + * and that it is a generating messenger (used for generating message templates). |
|
960 | + * |
|
961 | + * @param EE_messenger $messenger messenger used in trigger |
|
962 | + * @param EE_message_type $message_type message type used in trigger |
|
963 | + * @return bool true is a generating messenger and can be sent OR FALSE meaning cannot send. |
|
964 | + */ |
|
965 | + public function is_generating_messenger_and_active(EE_messenger $messenger, EE_message_type $message_type) |
|
966 | + { |
|
967 | + // get the $messengers the message type says it can be used with. |
|
968 | + foreach ($message_type->with_messengers() as $generating_messenger => $secondary_messengers) { |
|
969 | + if ( |
|
970 | + $messenger->name === $generating_messenger |
|
971 | + && $this->is_message_type_active_for_messenger($messenger->name, $message_type->name) |
|
972 | + ) { |
|
973 | + return true; |
|
974 | + } |
|
975 | + } |
|
976 | + return false; |
|
977 | + } |
|
978 | + |
|
979 | + |
|
980 | + /** |
|
981 | + * This returns all the contexts that are registered by all message types. |
|
982 | + * If $slugs_only is true, |
|
983 | + * then just an array indexed by unique context slugs with the latest label representation for that slug. |
|
984 | + * array( |
|
985 | + * 'context_slug' => 'localized label for context obtained from latest message type in the loop'. |
|
986 | + * ); |
|
987 | + * If $slugs_only is false, then the format is: |
|
988 | + * array( |
|
989 | + * 'message_type_name' => array( |
|
990 | + * 'context_slug' => array( |
|
991 | + * 'label' => 'localized label for context', |
|
992 | + * 'description' => 'localized description for context' |
|
993 | + * ) |
|
994 | + * ) |
|
995 | + * ); |
|
996 | + * Keep in mind that although different message types may share the same context slugs, |
|
997 | + * it is possible that the context is described differently by the message type. |
|
998 | + * |
|
999 | + * @since 4.9.0 |
|
1000 | + * @param bool $slugs_only Whether to return an array of just slugs and labels (true) |
|
1001 | + * or all contexts indexed by message type. |
|
1002 | + * @return array |
|
1003 | + */ |
|
1004 | + public function get_all_contexts($slugs_only = true) |
|
1005 | + { |
|
1006 | + $key = $slugs_only ? 'slugs' : 'all'; |
|
1007 | + // check if contexts has been setup yet. |
|
1008 | + if (empty($this->_contexts[ $key ])) { |
|
1009 | + // So let's get all active message type objects and loop through to get all unique contexts |
|
1010 | + foreach ($this->get_active_message_type_objects() as $message_type) { |
|
1011 | + if ($message_type instanceof EE_message_type) { |
|
1012 | + $message_type_contexts = $message_type->get_contexts(); |
|
1013 | + if ($slugs_only) { |
|
1014 | + foreach ($message_type_contexts as $context => $context_details) { |
|
1015 | + $this->_contexts[ $key ][ $context ] = $context_details['label']; |
|
1016 | + } |
|
1017 | + } else { |
|
1018 | + $this->_contexts[ $key ][ $message_type->name ] = $message_type_contexts; |
|
1019 | + } |
|
1020 | + } |
|
1021 | + } |
|
1022 | + } |
|
1023 | + return ! empty($this->_contexts[ $key ]) ? $this->_contexts[ $key ] : array(); |
|
1024 | + } |
|
1025 | + |
|
1026 | + |
|
1027 | + /** |
|
1028 | + * This checks the internal record of what message types are considered "active" and verifies that |
|
1029 | + * there is an installed class definition for that message type. If the active message type does not have a |
|
1030 | + * corresponding accessible message type class then it will be deactivated from all messengers it is active on and |
|
1031 | + * any related message templates will be inactivated as well. |
|
1032 | + * |
|
1033 | + * @return bool true means all active message types are valid, false means at least one message type was |
|
1034 | + * deactivated. |
|
1035 | + */ |
|
1036 | + public function validate_active_message_types_are_installed() |
|
1037 | + { |
|
1038 | + $list_of_active_message_type_names = $this->list_of_active_message_types(); |
|
1039 | + $installed_message_types = $this->installed_message_types(); |
|
1040 | + $all_message_types_valid = true; |
|
1041 | + // loop through list of active message types and verify they are installed. |
|
1042 | + foreach ($list_of_active_message_type_names as $message_type_name) { |
|
1043 | + if (! isset($installed_message_types[ $message_type_name ])) { |
|
1044 | + $this->remove_message_type_has_been_activated_from_all_messengers( |
|
1045 | + $message_type_name, |
|
1046 | + true |
|
1047 | + ); |
|
1048 | + $this->deactivate_message_type($message_type_name, false); |
|
1049 | + $all_message_types_valid = false; |
|
1050 | + } |
|
1051 | + } |
|
1052 | + return $all_message_types_valid; |
|
1053 | + } |
|
1054 | + |
|
1055 | + |
|
1056 | + /** |
|
1057 | + * This method checks the `ee_has_activated_messenger` option to see if the message type has ever been |
|
1058 | + * activated for the given messenger. This can be called by client code on plugin updates etc to determine whether |
|
1059 | + * to attempt automatically reactivating message types that should be activated by default or not. |
|
1060 | + * |
|
1061 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1062 | + * |
|
1063 | + * @param $message_type_name |
|
1064 | + * @param $messenger_name |
|
1065 | + * @return bool |
|
1066 | + */ |
|
1067 | + public function has_message_type_been_activated_for_messenger($message_type_name, $messenger_name) |
|
1068 | + { |
|
1069 | + $has_activated = $this->get_has_activated_messengers_option(); |
|
1070 | + return isset($has_activated[ $messenger_name ]) |
|
1071 | + && in_array($message_type_name, $has_activated[ $messenger_name ]); |
|
1072 | + } |
|
1073 | + |
|
1074 | + |
|
1075 | + /** |
|
1076 | + * This method unsets a message type from the given messenger has activated option. |
|
1077 | + * |
|
1078 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1079 | + * |
|
1080 | + * @param string $message_type_name |
|
1081 | + * @param string $messenger_name |
|
1082 | + * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1083 | + * If it is currently active, then remove. Otherwise leave it alone. |
|
1084 | + */ |
|
1085 | + public function remove_message_type_has_been_activated_for_messenger( |
|
1086 | + $message_type_name, |
|
1087 | + $messenger_name, |
|
1088 | + $consider_current_state = false |
|
1089 | + ) { |
|
1090 | + if ( |
|
1091 | + $consider_current_state |
|
1092 | + && ! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name) |
|
1093 | + ) { |
|
1094 | + // when consider current state is true, this means we don't want to change anything on the "has_activated" |
|
1095 | + // record if the message type is currently active for this messenger. This is used when we want to retain |
|
1096 | + // the record for user initiated inactivations of the message type. |
|
1097 | + return; |
|
1098 | + } |
|
1099 | + $has_activated = $this->get_has_activated_messengers_option(); |
|
1100 | + $key_for_message_type = isset($has_activated[ $messenger_name ]) |
|
1101 | + ? array_search($message_type_name, $has_activated[ $messenger_name ], true) |
|
1102 | + : false; |
|
1103 | + if ($key_for_message_type !== false) { |
|
1104 | + unset($has_activated[ $messenger_name ][ $key_for_message_type ]); |
|
1105 | + $this->update_has_activated_messengers_option($has_activated); |
|
1106 | + // reset the internal cached property |
|
1107 | + $this->get_has_activated_messengers_option(true); |
|
1108 | + } |
|
1109 | + } |
|
1110 | + |
|
1111 | + |
|
1112 | + /** |
|
1113 | + * Removes a message type active record from all messengers it is attached to. |
|
1114 | + * |
|
1115 | + * @see phpdocs on EE_Message_Resource_Manager::HAS_ACTIVATED_MESSAGE_TYPE_FOR_MESSENGER_OPTION_NAME for more details. |
|
1116 | + * |
|
1117 | + * @param $message_type_name |
|
1118 | + * @param bool $consider_current_state Whether to consider whether the message type is currently active or not. |
|
1119 | + * If it is currently active, then remove. Otherwise leave it alone. |
|
1120 | + */ |
|
1121 | + public function remove_message_type_has_been_activated_from_all_messengers( |
|
1122 | + $message_type_name, |
|
1123 | + $consider_current_state = false |
|
1124 | + ) { |
|
1125 | + foreach (array_keys($this->get_has_activated_messengers_option()) as $messenger_name) { |
|
1126 | + $this->remove_message_type_has_been_activated_for_messenger( |
|
1127 | + $message_type_name, |
|
1128 | + $messenger_name, |
|
1129 | + $consider_current_state |
|
1130 | + ); |
|
1131 | + } |
|
1132 | + } |
|
1133 | 1133 | } |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | public function get_active_messenger($messenger) |
197 | 197 | { |
198 | 198 | $this->_initialize_collections(); |
199 | - return ! empty($this->_active_messengers[ $messenger ]) ? $this->_active_messengers[ $messenger ] : null; |
|
199 | + return ! empty($this->_active_messengers[$messenger]) ? $this->_active_messengers[$messenger] : null; |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | $this->_installed_messengers = array(); |
210 | 210 | $this->messenger_collection()->rewind(); |
211 | 211 | while ($this->messenger_collection()->valid()) { |
212 | - $this->_installed_messengers[ $this->messenger_collection()->current()->name ] = $this->messenger_collection()->current(); |
|
212 | + $this->_installed_messengers[$this->messenger_collection()->current()->name] = $this->messenger_collection()->current(); |
|
213 | 213 | $this->messenger_collection()->next(); |
214 | 214 | } |
215 | 215 | } |
@@ -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 | |
@@ -320,8 +320,8 @@ discard block |
||
320 | 320 | { |
321 | 321 | $settings = array(); |
322 | 322 | if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
323 | - $settings = isset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings']) |
|
324 | - ? $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] |
|
323 | + $settings = isset($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]['settings']) |
|
324 | + ? $this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]['settings'] |
|
325 | 325 | : array(); |
326 | 326 | } |
327 | 327 | return $settings; |
@@ -339,8 +339,8 @@ discard block |
||
339 | 339 | { |
340 | 340 | $this->_initialize_collections(); |
341 | 341 | return |
342 | - ! empty($this->_active_message_types[ $messenger_name ]) |
|
343 | - && ! empty($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ]); |
|
342 | + ! empty($this->_active_message_types[$messenger_name]) |
|
343 | + && ! empty($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types']); |
|
344 | 344 | } |
345 | 345 | |
346 | 346 | |
@@ -355,13 +355,13 @@ discard block |
||
355 | 355 | public function get_active_message_types_for_messenger($messenger_name) |
356 | 356 | { |
357 | 357 | $message_types = array(); |
358 | - if (! $this->messenger_has_active_message_types($messenger_name)) { |
|
358 | + if ( ! $this->messenger_has_active_message_types($messenger_name)) { |
|
359 | 359 | return $message_types; |
360 | 360 | } |
361 | 361 | $installed_message_types = $this->installed_message_types(); |
362 | 362 | foreach ($installed_message_types as $message_type_name => $message_type) { |
363 | 363 | if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
364 | - $message_types[ $message_type_name ] = $message_type; |
|
364 | + $message_types[$message_type_name] = $message_type; |
|
365 | 365 | } |
366 | 366 | } |
367 | 367 | return $message_types; |
@@ -380,11 +380,11 @@ discard block |
||
380 | 380 | $active_message_type_names = array(); |
381 | 381 | $this->_initialize_collections(); |
382 | 382 | foreach ($this->_active_message_types as $messenger => $messenger_settings) { |
383 | - if (! isset($messenger_settings['settings'][ $messenger . '-message_types' ])) { |
|
383 | + if ( ! isset($messenger_settings['settings'][$messenger.'-message_types'])) { |
|
384 | 384 | continue; |
385 | 385 | } |
386 | - foreach ($messenger_settings['settings'][ $messenger . '-message_types' ] as $message_type_name => $message_type_config) { |
|
387 | - if (! in_array($message_type_name, $active_message_type_names)) { |
|
386 | + foreach ($messenger_settings['settings'][$messenger.'-message_types'] as $message_type_name => $message_type_config) { |
|
387 | + if ( ! in_array($message_type_name, $active_message_type_names)) { |
|
388 | 388 | $active_message_type_names[] = $message_type_name; |
389 | 389 | } |
390 | 390 | } |
@@ -405,8 +405,8 @@ discard block |
||
405 | 405 | $installed_message_types = $this->installed_message_types(); |
406 | 406 | $active_message_type_names = $this->list_of_active_message_types(); |
407 | 407 | foreach ($active_message_type_names as $active_message_type_name) { |
408 | - if (isset($installed_message_types[ $active_message_type_name ])) { |
|
409 | - $active_message_types[ $active_message_type_name ] = $installed_message_types[ $active_message_type_name ]; |
|
408 | + if (isset($installed_message_types[$active_message_type_name])) { |
|
409 | + $active_message_types[$active_message_type_name] = $installed_message_types[$active_message_type_name]; |
|
410 | 410 | } |
411 | 411 | } |
412 | 412 | return $active_message_types; |
@@ -421,7 +421,7 @@ discard block |
||
421 | 421 | if (empty($this->_installed_message_types)) { |
422 | 422 | $this->message_type_collection()->rewind(); |
423 | 423 | while ($this->message_type_collection()->valid()) { |
424 | - $this->_installed_message_types[ $this->message_type_collection()->current()->name ] = $this->message_type_collection()->current(); |
|
424 | + $this->_installed_message_types[$this->message_type_collection()->current()->name] = $this->message_type_collection()->current(); |
|
425 | 425 | $this->message_type_collection()->next(); |
426 | 426 | } |
427 | 427 | } |
@@ -460,7 +460,7 @@ discard block |
||
460 | 460 | public function valid_message_type_for_messenger(EE_messenger $messenger, $message_type_name) |
461 | 461 | { |
462 | 462 | $valid_message_types = $messenger->get_valid_message_types(); |
463 | - if (! in_array($message_type_name, $valid_message_types)) { |
|
463 | + if ( ! in_array($message_type_name, $valid_message_types)) { |
|
464 | 464 | throw new EE_Error( |
465 | 465 | sprintf( |
466 | 466 | esc_html__( |
@@ -586,7 +586,7 @@ discard block |
||
586 | 586 | */ |
587 | 587 | public function ensure_messenger_is_active($messenger_name, $update_option = true) |
588 | 588 | { |
589 | - if (! isset($this->_active_messengers[ $messenger_name ])) { |
|
589 | + if ( ! isset($this->_active_messengers[$messenger_name])) { |
|
590 | 590 | try { |
591 | 591 | $this->activate_messenger($messenger_name, array(), $update_option); |
592 | 592 | } catch (EE_Error $e) { |
@@ -631,7 +631,7 @@ discard block |
||
631 | 631 | $this->ensure_messenger_is_active($messenger_name, $update_option); |
632 | 632 | } |
633 | 633 | |
634 | - if (! empty($not_installed)) { |
|
634 | + if ( ! empty($not_installed)) { |
|
635 | 635 | EE_Error::add_error( |
636 | 636 | sprintf( |
637 | 637 | esc_html__('The following messengers are either not installed or are invalid:%1$s %2$s', 'event_espresso'), |
@@ -664,7 +664,7 @@ discard block |
||
664 | 664 | // ensure messenger is active (that's an inherent coupling between active message types and the |
665 | 665 | // messenger they are being activated for. |
666 | 666 | try { |
667 | - if (! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
667 | + if ( ! $this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
|
668 | 668 | // all is good so let's just get it active |
669 | 669 | $this->activate_messenger($messenger, array($message_type_name), $update_option); |
670 | 670 | } |
@@ -728,7 +728,7 @@ discard block |
||
728 | 728 | : $this->messenger_collection()->get_by_info($messenger); |
729 | 729 | // it's inactive. Activate it. |
730 | 730 | if ($messenger instanceof EE_messenger) { |
731 | - $this->_active_messengers[ $messenger->name ] = $messenger; |
|
731 | + $this->_active_messengers[$messenger->name] = $messenger; |
|
732 | 732 | // activate incoming message types set to be activated with messenger. |
733 | 733 | $message_type_names = $this->_activate_message_types($messenger, $message_type_names); |
734 | 734 | // setup any initial settings for the messenger if necessary. |
@@ -740,7 +740,7 @@ discard block |
||
740 | 740 | // generate new templates if necessary and ensure all related templates that are already in the database are |
741 | 741 | // marked active. Note, this will also deactivate a message type for a messenger if the template |
742 | 742 | // cannot be successfully created during its attempt (only happens for global template attempts). |
743 | - if (! empty($message_type_names)) { |
|
743 | + if ( ! empty($message_type_names)) { |
|
744 | 744 | $templates = EEH_MSG_Template::generate_new_templates($messenger->name, $message_type_names, 0, true); |
745 | 745 | EEH_MSG_Template::update_to_active(array($messenger->name), $message_type_names); |
746 | 746 | } |
@@ -764,13 +764,13 @@ discard block |
||
764 | 764 | // things have never been initialized (which should happen on EEH_Activation::generate_message_templates). |
765 | 765 | // So ONLY then do we need to actually grab defaults and cycle through them. Otherwise we |
766 | 766 | // only override _active_message_types when an explicit array of $message_type_names has been provided. |
767 | - $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[ $messenger->name ]) |
|
767 | + $message_type_names = empty($message_type_names) && ! isset($this->_active_message_types[$messenger->name]) |
|
768 | 768 | ? $messenger->get_default_message_types() |
769 | 769 | : (array) $message_type_names; |
770 | 770 | |
771 | 771 | // now we ALWAYS need to make sure that the messenger is active for the message types we're activating! |
772 | - if (! isset($this->_active_message_types[ $messenger->name ])) { |
|
773 | - $this->_active_message_types[ $messenger->name ]['settings'] = array(); |
|
772 | + if ( ! isset($this->_active_message_types[$messenger->name])) { |
|
773 | + $this->_active_message_types[$messenger->name]['settings'] = array(); |
|
774 | 774 | } |
775 | 775 | |
776 | 776 | if ($message_type_names) { |
@@ -811,16 +811,16 @@ discard block |
||
811 | 811 | if ($message_type instanceof EE_message_type) { |
812 | 812 | $default_settings = $message_type->get_admin_settings_fields(); |
813 | 813 | foreach ($default_settings as $field => $values) { |
814 | - if (isset($new_settings[ $field ])) { |
|
815 | - $existing_settings[ $field ] = $new_settings[ $field ]; |
|
814 | + if (isset($new_settings[$field])) { |
|
815 | + $existing_settings[$field] = $new_settings[$field]; |
|
816 | 816 | continue; |
817 | 817 | } |
818 | - if (! isset($existing_settings[ $field ])) { |
|
819 | - $existing_settings[ $field ] = $values['default']; |
|
818 | + if ( ! isset($existing_settings[$field])) { |
|
819 | + $existing_settings[$field] = $values['default']; |
|
820 | 820 | } |
821 | 821 | } |
822 | 822 | } |
823 | - $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]['settings'] = $existing_settings; |
|
823 | + $this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]['settings'] = $existing_settings; |
|
824 | 824 | } |
825 | 825 | |
826 | 826 | |
@@ -843,12 +843,12 @@ discard block |
||
843 | 843 | } |
844 | 844 | |
845 | 845 | // make sure this messenger has a record in the has_activated array |
846 | - if (! isset($this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
847 | - $this->_has_activated_messengers_and_message_types[ $messenger->name ] = array(); |
|
846 | + if ( ! isset($this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
847 | + $this->_has_activated_messengers_and_message_types[$messenger->name] = array(); |
|
848 | 848 | } |
849 | 849 | // check if message type has already been added |
850 | - if (! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[ $messenger->name ])) { |
|
851 | - $this->_has_activated_messengers_and_message_types[ $messenger->name ][] = $message_type_name; |
|
850 | + if ( ! in_array($message_type_name, $this->_has_activated_messengers_and_message_types[$messenger->name])) { |
|
851 | + $this->_has_activated_messengers_and_message_types[$messenger->name][] = $message_type_name; |
|
852 | 852 | } |
853 | 853 | } |
854 | 854 | |
@@ -866,16 +866,16 @@ discard block |
||
866 | 866 | $messenger = $this->get_messenger($messenger_name); |
867 | 867 | if ($messenger instanceof EE_messenger) { |
868 | 868 | $msgr_settings = $messenger->get_admin_settings_fields(); |
869 | - if (! empty($msgr_settings)) { |
|
869 | + if ( ! empty($msgr_settings)) { |
|
870 | 870 | foreach ($msgr_settings as $field => $value) { |
871 | 871 | // is there a new setting for this? |
872 | - if (isset($new_settings[ $field ])) { |
|
873 | - $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $new_settings[ $field ]; |
|
872 | + if (isset($new_settings[$field])) { |
|
873 | + $this->_active_message_types[$messenger->name]['settings'][$field] = $new_settings[$field]; |
|
874 | 874 | continue; |
875 | 875 | } |
876 | 876 | // only set the default if it isn't already set. |
877 | - if (! isset($this->_active_message_types[ $messenger->name ]['settings'][ $field ])) { |
|
878 | - $this->_active_message_types[ $messenger->name ]['settings'][ $field ] = $value; |
|
877 | + if ( ! isset($this->_active_message_types[$messenger->name]['settings'][$field])) { |
|
878 | + $this->_active_message_types[$messenger->name]['settings'][$field] = $value; |
|
879 | 879 | } |
880 | 880 | } |
881 | 881 | } |
@@ -895,8 +895,8 @@ discard block |
||
895 | 895 | if ($messenger_name instanceof EE_messenger) { |
896 | 896 | $messenger_name = $messenger_name->name; |
897 | 897 | } |
898 | - unset($this->_active_messengers[ $messenger_name ]); |
|
899 | - unset($this->_active_message_types[ $messenger_name ]); |
|
898 | + unset($this->_active_messengers[$messenger_name]); |
|
899 | + unset($this->_active_message_types[$messenger_name]); |
|
900 | 900 | $this->_message_template_group_model->deactivate_message_template_groups_for($messenger_name); |
901 | 901 | $this->update_active_messengers_option(); |
902 | 902 | } |
@@ -918,7 +918,7 @@ discard block |
||
918 | 918 | } |
919 | 919 | foreach ($this->_active_message_types as $messenger_name => $settings) { |
920 | 920 | unset( |
921 | - $this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ] |
|
921 | + $this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name] |
|
922 | 922 | ); |
923 | 923 | |
924 | 924 | // we always record (even on deactivation) that a message type has been activated because there should at |
@@ -944,7 +944,7 @@ discard block |
||
944 | 944 | { |
945 | 945 | $this->_initialize_collections(); |
946 | 946 | if ($this->is_message_type_active_for_messenger($messenger_name, $message_type_name)) { |
947 | - unset($this->_active_message_types[ $messenger_name ]['settings'][ $messenger_name . '-message_types' ][ $message_type_name ]); |
|
947 | + unset($this->_active_message_types[$messenger_name]['settings'][$messenger_name.'-message_types'][$message_type_name]); |
|
948 | 948 | } |
949 | 949 | $this->_message_template_group_model->deactivate_message_template_groups_for( |
950 | 950 | array($messenger_name), |
@@ -1005,22 +1005,22 @@ discard block |
||
1005 | 1005 | { |
1006 | 1006 | $key = $slugs_only ? 'slugs' : 'all'; |
1007 | 1007 | // check if contexts has been setup yet. |
1008 | - if (empty($this->_contexts[ $key ])) { |
|
1008 | + if (empty($this->_contexts[$key])) { |
|
1009 | 1009 | // So let's get all active message type objects and loop through to get all unique contexts |
1010 | 1010 | foreach ($this->get_active_message_type_objects() as $message_type) { |
1011 | 1011 | if ($message_type instanceof EE_message_type) { |
1012 | 1012 | $message_type_contexts = $message_type->get_contexts(); |
1013 | 1013 | if ($slugs_only) { |
1014 | 1014 | foreach ($message_type_contexts as $context => $context_details) { |
1015 | - $this->_contexts[ $key ][ $context ] = $context_details['label']; |
|
1015 | + $this->_contexts[$key][$context] = $context_details['label']; |
|
1016 | 1016 | } |
1017 | 1017 | } else { |
1018 | - $this->_contexts[ $key ][ $message_type->name ] = $message_type_contexts; |
|
1018 | + $this->_contexts[$key][$message_type->name] = $message_type_contexts; |
|
1019 | 1019 | } |
1020 | 1020 | } |
1021 | 1021 | } |
1022 | 1022 | } |
1023 | - return ! empty($this->_contexts[ $key ]) ? $this->_contexts[ $key ] : array(); |
|
1023 | + return ! empty($this->_contexts[$key]) ? $this->_contexts[$key] : array(); |
|
1024 | 1024 | } |
1025 | 1025 | |
1026 | 1026 | |
@@ -1040,7 +1040,7 @@ discard block |
||
1040 | 1040 | $all_message_types_valid = true; |
1041 | 1041 | // loop through list of active message types and verify they are installed. |
1042 | 1042 | foreach ($list_of_active_message_type_names as $message_type_name) { |
1043 | - if (! isset($installed_message_types[ $message_type_name ])) { |
|
1043 | + if ( ! isset($installed_message_types[$message_type_name])) { |
|
1044 | 1044 | $this->remove_message_type_has_been_activated_from_all_messengers( |
1045 | 1045 | $message_type_name, |
1046 | 1046 | true |
@@ -1067,8 +1067,8 @@ discard block |
||
1067 | 1067 | public function has_message_type_been_activated_for_messenger($message_type_name, $messenger_name) |
1068 | 1068 | { |
1069 | 1069 | $has_activated = $this->get_has_activated_messengers_option(); |
1070 | - return isset($has_activated[ $messenger_name ]) |
|
1071 | - && in_array($message_type_name, $has_activated[ $messenger_name ]); |
|
1070 | + return isset($has_activated[$messenger_name]) |
|
1071 | + && in_array($message_type_name, $has_activated[$messenger_name]); |
|
1072 | 1072 | } |
1073 | 1073 | |
1074 | 1074 | |
@@ -1097,11 +1097,11 @@ discard block |
||
1097 | 1097 | return; |
1098 | 1098 | } |
1099 | 1099 | $has_activated = $this->get_has_activated_messengers_option(); |
1100 | - $key_for_message_type = isset($has_activated[ $messenger_name ]) |
|
1101 | - ? array_search($message_type_name, $has_activated[ $messenger_name ], true) |
|
1100 | + $key_for_message_type = isset($has_activated[$messenger_name]) |
|
1101 | + ? array_search($message_type_name, $has_activated[$messenger_name], true) |
|
1102 | 1102 | : false; |
1103 | 1103 | if ($key_for_message_type !== false) { |
1104 | - unset($has_activated[ $messenger_name ][ $key_for_message_type ]); |
|
1104 | + unset($has_activated[$messenger_name][$key_for_message_type]); |
|
1105 | 1105 | $this->update_has_activated_messengers_option($has_activated); |
1106 | 1106 | // reset the internal cached property |
1107 | 1107 | $this->get_has_activated_messengers_option(true); |
@@ -13,111 +13,111 @@ |
||
13 | 13 | { |
14 | 14 | |
15 | 15 | |
16 | - /** |
|
17 | - * Sending messenger |
|
18 | - * |
|
19 | - * @type EE_messenger | string |
|
20 | - */ |
|
21 | - protected $_sending_messenger = ''; |
|
22 | - |
|
23 | - |
|
24 | - /** |
|
25 | - * Holds the incoming token; |
|
26 | - * @type string |
|
27 | - */ |
|
28 | - public $token = ''; |
|
29 | - |
|
30 | - |
|
31 | - /** |
|
32 | - * Constructor |
|
33 | - * |
|
34 | - * @param string $sending_messenger_slug This is used to set what messenger is used to "send" |
|
35 | - * the EE_Message retrieved from the DB via the given token. |
|
36 | - * @param string $token This is a token for a Message that should already exist int the db. |
|
37 | - * This is then used to populate the properties in here. |
|
38 | - * @param EE_Message_Resource_Manager $message_resource_manager |
|
39 | - */ |
|
40 | - public function __construct($token, $sending_messenger_slug = 'html', EE_Message_Resource_Manager $message_resource_manager) |
|
41 | - { |
|
42 | - $this->token = $token; |
|
43 | - $this->_sending_messenger = $this->_set_sending_messenger($sending_messenger_slug, $message_resource_manager); |
|
44 | - $this->_message = $this->_generate_message(); |
|
45 | - // set params for parent from the message object |
|
46 | - parent::__construct( |
|
47 | - $this->_message->messenger(), |
|
48 | - $this->_message->message_type(), |
|
49 | - array(), |
|
50 | - $this->_message->context(), |
|
51 | - false |
|
52 | - ); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $sending_messenger_slug |
|
59 | - * @param \EE_Message_Resource_Manager $message_resource_manager |
|
60 | - * @return \EE_messenger | string |
|
61 | - */ |
|
62 | - protected function _set_sending_messenger( |
|
63 | - $sending_messenger_slug, |
|
64 | - EE_Message_Resource_Manager $message_resource_manager |
|
65 | - ) { |
|
66 | - $sending_messenger = $message_resource_manager->get_active_messenger($sending_messenger_slug); |
|
67 | - return $sending_messenger instanceof EE_messenger ? $sending_messenger : $sending_messenger_slug; |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - |
|
72 | - /** |
|
73 | - * @return EE_messenger |
|
74 | - */ |
|
75 | - public function sending_messenger() |
|
76 | - { |
|
77 | - return $this->_sending_messenger; |
|
78 | - } |
|
79 | - |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * generates an EE_Message using the supplied arguments and some defaults |
|
84 | - * |
|
85 | - * @param array $properties |
|
86 | - * @return EE_Message |
|
87 | - * @throws \EE_Error |
|
88 | - */ |
|
89 | - protected function _generate_message($properties = array()) |
|
90 | - { |
|
91 | - // a message was generated immediately but the parent class will call this again |
|
92 | - if ($this->_message instanceof EE_Message) { |
|
93 | - return $this->_message; |
|
94 | - } |
|
95 | - $message = EEM_Message::instance()->get_one_by_token($this->token); |
|
96 | - if (! $message instanceof EE_Message) { |
|
97 | - throw new EE_Error( |
|
98 | - sprintf( |
|
99 | - esc_html__('Unable to retrieve generated message from DB using given token: "%1$s"', 'event_espresso'), |
|
100 | - $this->token |
|
101 | - ) |
|
102 | - ); |
|
103 | - } |
|
104 | - $message->set_STS_ID(EEM_Message::status_idle); |
|
105 | - |
|
106 | - if (! $this->_sending_messenger instanceof EE_messenger) { |
|
107 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
108 | - $message->set_error_message( |
|
109 | - sprintf( |
|
110 | - esc_html__('Unable to send message because the "%1$s" messenger is not active or not installed', 'event_espresso'), |
|
111 | - $this->_sending_messenger |
|
112 | - ) |
|
113 | - ); |
|
114 | - } |
|
115 | - |
|
116 | - // set properties |
|
117 | - $this->_valid = true; |
|
118 | - $this->_messenger = $message->messenger_object(); |
|
119 | - $this->_message_type = $message->message_type_object(); |
|
120 | - $this->_send_now = $message->send_now(); |
|
121 | - return $message; |
|
122 | - } |
|
16 | + /** |
|
17 | + * Sending messenger |
|
18 | + * |
|
19 | + * @type EE_messenger | string |
|
20 | + */ |
|
21 | + protected $_sending_messenger = ''; |
|
22 | + |
|
23 | + |
|
24 | + /** |
|
25 | + * Holds the incoming token; |
|
26 | + * @type string |
|
27 | + */ |
|
28 | + public $token = ''; |
|
29 | + |
|
30 | + |
|
31 | + /** |
|
32 | + * Constructor |
|
33 | + * |
|
34 | + * @param string $sending_messenger_slug This is used to set what messenger is used to "send" |
|
35 | + * the EE_Message retrieved from the DB via the given token. |
|
36 | + * @param string $token This is a token for a Message that should already exist int the db. |
|
37 | + * This is then used to populate the properties in here. |
|
38 | + * @param EE_Message_Resource_Manager $message_resource_manager |
|
39 | + */ |
|
40 | + public function __construct($token, $sending_messenger_slug = 'html', EE_Message_Resource_Manager $message_resource_manager) |
|
41 | + { |
|
42 | + $this->token = $token; |
|
43 | + $this->_sending_messenger = $this->_set_sending_messenger($sending_messenger_slug, $message_resource_manager); |
|
44 | + $this->_message = $this->_generate_message(); |
|
45 | + // set params for parent from the message object |
|
46 | + parent::__construct( |
|
47 | + $this->_message->messenger(), |
|
48 | + $this->_message->message_type(), |
|
49 | + array(), |
|
50 | + $this->_message->context(), |
|
51 | + false |
|
52 | + ); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $sending_messenger_slug |
|
59 | + * @param \EE_Message_Resource_Manager $message_resource_manager |
|
60 | + * @return \EE_messenger | string |
|
61 | + */ |
|
62 | + protected function _set_sending_messenger( |
|
63 | + $sending_messenger_slug, |
|
64 | + EE_Message_Resource_Manager $message_resource_manager |
|
65 | + ) { |
|
66 | + $sending_messenger = $message_resource_manager->get_active_messenger($sending_messenger_slug); |
|
67 | + return $sending_messenger instanceof EE_messenger ? $sending_messenger : $sending_messenger_slug; |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + |
|
72 | + /** |
|
73 | + * @return EE_messenger |
|
74 | + */ |
|
75 | + public function sending_messenger() |
|
76 | + { |
|
77 | + return $this->_sending_messenger; |
|
78 | + } |
|
79 | + |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * generates an EE_Message using the supplied arguments and some defaults |
|
84 | + * |
|
85 | + * @param array $properties |
|
86 | + * @return EE_Message |
|
87 | + * @throws \EE_Error |
|
88 | + */ |
|
89 | + protected function _generate_message($properties = array()) |
|
90 | + { |
|
91 | + // a message was generated immediately but the parent class will call this again |
|
92 | + if ($this->_message instanceof EE_Message) { |
|
93 | + return $this->_message; |
|
94 | + } |
|
95 | + $message = EEM_Message::instance()->get_one_by_token($this->token); |
|
96 | + if (! $message instanceof EE_Message) { |
|
97 | + throw new EE_Error( |
|
98 | + sprintf( |
|
99 | + esc_html__('Unable to retrieve generated message from DB using given token: "%1$s"', 'event_espresso'), |
|
100 | + $this->token |
|
101 | + ) |
|
102 | + ); |
|
103 | + } |
|
104 | + $message->set_STS_ID(EEM_Message::status_idle); |
|
105 | + |
|
106 | + if (! $this->_sending_messenger instanceof EE_messenger) { |
|
107 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
108 | + $message->set_error_message( |
|
109 | + sprintf( |
|
110 | + esc_html__('Unable to send message because the "%1$s" messenger is not active or not installed', 'event_espresso'), |
|
111 | + $this->_sending_messenger |
|
112 | + ) |
|
113 | + ); |
|
114 | + } |
|
115 | + |
|
116 | + // set properties |
|
117 | + $this->_valid = true; |
|
118 | + $this->_messenger = $message->messenger_object(); |
|
119 | + $this->_message_type = $message->message_type_object(); |
|
120 | + $this->_send_now = $message->send_now(); |
|
121 | + return $message; |
|
122 | + } |
|
123 | 123 | } |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | return $this->_message; |
94 | 94 | } |
95 | 95 | $message = EEM_Message::instance()->get_one_by_token($this->token); |
96 | - if (! $message instanceof EE_Message) { |
|
96 | + if ( ! $message instanceof EE_Message) { |
|
97 | 97 | throw new EE_Error( |
98 | 98 | sprintf( |
99 | 99 | esc_html__('Unable to retrieve generated message from DB using given token: "%1$s"', 'event_espresso'), |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | } |
104 | 104 | $message->set_STS_ID(EEM_Message::status_idle); |
105 | 105 | |
106 | - if (! $this->_sending_messenger instanceof EE_messenger) { |
|
106 | + if ( ! $this->_sending_messenger instanceof EE_messenger) { |
|
107 | 107 | $message->set_STS_ID(EEM_Message::status_failed); |
108 | 108 | $message->set_error_message( |
109 | 109 | sprintf( |
@@ -14,126 +14,126 @@ |
||
14 | 14 | { |
15 | 15 | |
16 | 16 | |
17 | - /** |
|
18 | - * @see parent::get_priority() for documentation. |
|
19 | - * @return int |
|
20 | - */ |
|
21 | - public function get_priority() |
|
22 | - { |
|
23 | - return EEM_Message::priority_medium; |
|
24 | - } |
|
25 | - |
|
26 | - |
|
27 | - |
|
28 | - protected function _set_admin_pages() |
|
29 | - { |
|
30 | - $this->admin_registered_pages = array( |
|
31 | - 'events_edit' => true |
|
32 | - ); |
|
33 | - } |
|
34 | - |
|
35 | - |
|
36 | - protected function _get_admin_content_events_edit_for_messenger(EE_messenger $messenger) |
|
37 | - { |
|
38 | - // this is just a test |
|
39 | - return $this->name . ' Message Type for ' . $messenger->name . ' Messenger '; |
|
40 | - } |
|
41 | - |
|
42 | - |
|
43 | - |
|
44 | - |
|
45 | - protected function _set_data_handler() |
|
46 | - { |
|
47 | - if (is_array($this->_data)) { |
|
48 | - $data_type = reset($this->_data); |
|
49 | - |
|
50 | - if (is_array($data_type)) { |
|
51 | - // grab the first item and see if its a registration. |
|
52 | - $maybe_reg = isset($data_type[0]) && is_array($data_type[0]) ? reset($data_type[0]) : reset($data_type); |
|
53 | - if ($maybe_reg instanceof EE_Registration) { |
|
54 | - // is $data_type itself just an array of registrations? |
|
55 | - if (isset($data_type[1]) && $data_type[1] instanceof EE_Registration) { |
|
56 | - $regs = $data_type; |
|
57 | - } else { |
|
58 | - $regs = is_array($data_type[0]) ? $data_type[0] : array( $maybe_reg ); |
|
59 | - } |
|
60 | - |
|
61 | - foreach ($regs as $reg) { |
|
62 | - if ($reg instanceof EE_Registration) { |
|
63 | - $this->_regs_for_sending[] = $reg->ID(); |
|
64 | - } |
|
65 | - } |
|
66 | - $this->_data = isset($this->_data[1]) ? array( $maybe_reg->transaction(), null, $this->_data[1] ) : array( $maybe_reg->transaction() ); |
|
67 | - $this->_data_handler = 'Gateways'; |
|
68 | - } else { |
|
69 | - $this->_data_handler = 'Gateways'; |
|
70 | - } |
|
71 | - } else { |
|
72 | - $this->_data_handler = $data_type instanceof EE_Registration ? 'REG' : 'Gateways'; |
|
73 | - } |
|
74 | - } else { |
|
75 | - $this->_data_handler = $this->_data instanceof EE_Registration ? 'REG' : 'Gateways'; |
|
76 | - } |
|
77 | - |
|
78 | - $this->_single_message = $this->_data_handler == 'REG' ? true : false; |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - |
|
83 | - protected function _get_data_for_context($context, EE_Registration $registration, $id) |
|
84 | - { |
|
85 | - if ($context == 'admin') { |
|
86 | - // use the registration to get the transaction. |
|
87 | - $transaction = $registration->transaction(); |
|
88 | - |
|
89 | - // bail early if no transaction |
|
90 | - if (! $transaction instanceof EE_Transaction) { |
|
91 | - throw new EE_Error(esc_html__('The given registration does not have an associated transaction. Something is wrong.', 'event_espresso')); |
|
92 | - } |
|
93 | - |
|
94 | - $payment = !empty($id) ? EEM_Payment::instance()->get_one(array( array( 'PAY_ID' => $id, 'TXN_ID' => $transaction->ID() ) )) : 0; |
|
95 | - return array( $transaction, $payment ); |
|
96 | - } else { |
|
97 | - return $registration; |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * Setup admin settings for this message type. |
|
105 | - */ |
|
106 | - protected function _set_admin_settings_fields() |
|
107 | - { |
|
108 | - $this->_admin_settings_fields = array(); |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - |
|
113 | - |
|
114 | - |
|
115 | - /** |
|
116 | - * returns an array of addressee objects for event_admins |
|
117 | - * |
|
118 | - * @access protected |
|
119 | - * @return array array of EE_Messages_Addressee objects |
|
120 | - */ |
|
121 | - protected function _admin_addressees() |
|
122 | - { |
|
123 | - if ($this->_single_message) { |
|
124 | - return array(); |
|
125 | - } |
|
126 | - return parent::_admin_addressees(); |
|
127 | - } |
|
128 | - |
|
129 | - |
|
130 | - |
|
131 | - protected function _primary_attendee_addressees() |
|
132 | - { |
|
133 | - if ($this->_single_message) { |
|
134 | - return array(); |
|
135 | - } |
|
136 | - |
|
137 | - return parent::_primary_attendee_addressees(); |
|
138 | - } |
|
17 | + /** |
|
18 | + * @see parent::get_priority() for documentation. |
|
19 | + * @return int |
|
20 | + */ |
|
21 | + public function get_priority() |
|
22 | + { |
|
23 | + return EEM_Message::priority_medium; |
|
24 | + } |
|
25 | + |
|
26 | + |
|
27 | + |
|
28 | + protected function _set_admin_pages() |
|
29 | + { |
|
30 | + $this->admin_registered_pages = array( |
|
31 | + 'events_edit' => true |
|
32 | + ); |
|
33 | + } |
|
34 | + |
|
35 | + |
|
36 | + protected function _get_admin_content_events_edit_for_messenger(EE_messenger $messenger) |
|
37 | + { |
|
38 | + // this is just a test |
|
39 | + return $this->name . ' Message Type for ' . $messenger->name . ' Messenger '; |
|
40 | + } |
|
41 | + |
|
42 | + |
|
43 | + |
|
44 | + |
|
45 | + protected function _set_data_handler() |
|
46 | + { |
|
47 | + if (is_array($this->_data)) { |
|
48 | + $data_type = reset($this->_data); |
|
49 | + |
|
50 | + if (is_array($data_type)) { |
|
51 | + // grab the first item and see if its a registration. |
|
52 | + $maybe_reg = isset($data_type[0]) && is_array($data_type[0]) ? reset($data_type[0]) : reset($data_type); |
|
53 | + if ($maybe_reg instanceof EE_Registration) { |
|
54 | + // is $data_type itself just an array of registrations? |
|
55 | + if (isset($data_type[1]) && $data_type[1] instanceof EE_Registration) { |
|
56 | + $regs = $data_type; |
|
57 | + } else { |
|
58 | + $regs = is_array($data_type[0]) ? $data_type[0] : array( $maybe_reg ); |
|
59 | + } |
|
60 | + |
|
61 | + foreach ($regs as $reg) { |
|
62 | + if ($reg instanceof EE_Registration) { |
|
63 | + $this->_regs_for_sending[] = $reg->ID(); |
|
64 | + } |
|
65 | + } |
|
66 | + $this->_data = isset($this->_data[1]) ? array( $maybe_reg->transaction(), null, $this->_data[1] ) : array( $maybe_reg->transaction() ); |
|
67 | + $this->_data_handler = 'Gateways'; |
|
68 | + } else { |
|
69 | + $this->_data_handler = 'Gateways'; |
|
70 | + } |
|
71 | + } else { |
|
72 | + $this->_data_handler = $data_type instanceof EE_Registration ? 'REG' : 'Gateways'; |
|
73 | + } |
|
74 | + } else { |
|
75 | + $this->_data_handler = $this->_data instanceof EE_Registration ? 'REG' : 'Gateways'; |
|
76 | + } |
|
77 | + |
|
78 | + $this->_single_message = $this->_data_handler == 'REG' ? true : false; |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + |
|
83 | + protected function _get_data_for_context($context, EE_Registration $registration, $id) |
|
84 | + { |
|
85 | + if ($context == 'admin') { |
|
86 | + // use the registration to get the transaction. |
|
87 | + $transaction = $registration->transaction(); |
|
88 | + |
|
89 | + // bail early if no transaction |
|
90 | + if (! $transaction instanceof EE_Transaction) { |
|
91 | + throw new EE_Error(esc_html__('The given registration does not have an associated transaction. Something is wrong.', 'event_espresso')); |
|
92 | + } |
|
93 | + |
|
94 | + $payment = !empty($id) ? EEM_Payment::instance()->get_one(array( array( 'PAY_ID' => $id, 'TXN_ID' => $transaction->ID() ) )) : 0; |
|
95 | + return array( $transaction, $payment ); |
|
96 | + } else { |
|
97 | + return $registration; |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * Setup admin settings for this message type. |
|
105 | + */ |
|
106 | + protected function _set_admin_settings_fields() |
|
107 | + { |
|
108 | + $this->_admin_settings_fields = array(); |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + |
|
113 | + |
|
114 | + |
|
115 | + /** |
|
116 | + * returns an array of addressee objects for event_admins |
|
117 | + * |
|
118 | + * @access protected |
|
119 | + * @return array array of EE_Messages_Addressee objects |
|
120 | + */ |
|
121 | + protected function _admin_addressees() |
|
122 | + { |
|
123 | + if ($this->_single_message) { |
|
124 | + return array(); |
|
125 | + } |
|
126 | + return parent::_admin_addressees(); |
|
127 | + } |
|
128 | + |
|
129 | + |
|
130 | + |
|
131 | + protected function _primary_attendee_addressees() |
|
132 | + { |
|
133 | + if ($this->_single_message) { |
|
134 | + return array(); |
|
135 | + } |
|
136 | + |
|
137 | + return parent::_primary_attendee_addressees(); |
|
138 | + } |
|
139 | 139 | } |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | protected function _get_admin_content_events_edit_for_messenger(EE_messenger $messenger) |
37 | 37 | { |
38 | 38 | // this is just a test |
39 | - return $this->name . ' Message Type for ' . $messenger->name . ' Messenger '; |
|
39 | + return $this->name.' Message Type for '.$messenger->name.' Messenger '; |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | if (isset($data_type[1]) && $data_type[1] instanceof EE_Registration) { |
56 | 56 | $regs = $data_type; |
57 | 57 | } else { |
58 | - $regs = is_array($data_type[0]) ? $data_type[0] : array( $maybe_reg ); |
|
58 | + $regs = is_array($data_type[0]) ? $data_type[0] : array($maybe_reg); |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | foreach ($regs as $reg) { |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | $this->_regs_for_sending[] = $reg->ID(); |
64 | 64 | } |
65 | 65 | } |
66 | - $this->_data = isset($this->_data[1]) ? array( $maybe_reg->transaction(), null, $this->_data[1] ) : array( $maybe_reg->transaction() ); |
|
66 | + $this->_data = isset($this->_data[1]) ? array($maybe_reg->transaction(), null, $this->_data[1]) : array($maybe_reg->transaction()); |
|
67 | 67 | $this->_data_handler = 'Gateways'; |
68 | 68 | } else { |
69 | 69 | $this->_data_handler = 'Gateways'; |
@@ -82,17 +82,17 @@ discard block |
||
82 | 82 | |
83 | 83 | protected function _get_data_for_context($context, EE_Registration $registration, $id) |
84 | 84 | { |
85 | - if ($context == 'admin') { |
|
85 | + if ($context == 'admin') { |
|
86 | 86 | // use the registration to get the transaction. |
87 | 87 | $transaction = $registration->transaction(); |
88 | 88 | |
89 | 89 | // bail early if no transaction |
90 | - if (! $transaction instanceof EE_Transaction) { |
|
90 | + if ( ! $transaction instanceof EE_Transaction) { |
|
91 | 91 | throw new EE_Error(esc_html__('The given registration does not have an associated transaction. Something is wrong.', 'event_espresso')); |
92 | 92 | } |
93 | 93 | |
94 | - $payment = !empty($id) ? EEM_Payment::instance()->get_one(array( array( 'PAY_ID' => $id, 'TXN_ID' => $transaction->ID() ) )) : 0; |
|
95 | - return array( $transaction, $payment ); |
|
94 | + $payment = ! empty($id) ? EEM_Payment::instance()->get_one(array(array('PAY_ID' => $id, 'TXN_ID' => $transaction->ID()))) : 0; |
|
95 | + return array($transaction, $payment); |
|
96 | 96 | } else { |
97 | 97 | return $registration; |
98 | 98 | } |
@@ -19,106 +19,106 @@ |
||
19 | 19 | class AttendeesReport extends JobHandlerFile |
20 | 20 | { |
21 | 21 | |
22 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
23 | - public function create_job(JobParameters $job_parameters) |
|
24 | - { |
|
25 | - if (! \EE_Capabilities::instance()->current_user_can('ee_read_contacts', 'generating_report')) { |
|
26 | - throw new BatchRequestException( |
|
27 | - esc_html__('You do not have permission to view contacts', 'event_espresso') |
|
28 | - ); |
|
29 | - } |
|
30 | - $filepath = $this->create_file_from_job_with_name( |
|
31 | - $job_parameters->job_id(), |
|
32 | - esc_html__('contact-list-report.csv', 'event_espresso') |
|
33 | - ); |
|
34 | - $job_parameters->add_extra_data('filepath', $filepath); |
|
35 | - $job_parameters->set_job_size($this->count_units_to_process()); |
|
36 | - // we should also set the header columns |
|
37 | - $csv_data_for_row = $this->get_csv_data(0, 1); |
|
38 | - \EEH_Export::write_data_array_to_csv($filepath, $csv_data_for_row, true); |
|
39 | - // if we actually processed a row there, record it |
|
40 | - if ($job_parameters->job_size()) { |
|
41 | - $job_parameters->mark_processed(1); |
|
42 | - } |
|
43 | - return new JobStepResponse( |
|
44 | - $job_parameters, |
|
45 | - esc_html__('Contacts report started successfully...', 'event_espresso') |
|
46 | - ); |
|
47 | - } |
|
22 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
23 | + public function create_job(JobParameters $job_parameters) |
|
24 | + { |
|
25 | + if (! \EE_Capabilities::instance()->current_user_can('ee_read_contacts', 'generating_report')) { |
|
26 | + throw new BatchRequestException( |
|
27 | + esc_html__('You do not have permission to view contacts', 'event_espresso') |
|
28 | + ); |
|
29 | + } |
|
30 | + $filepath = $this->create_file_from_job_with_name( |
|
31 | + $job_parameters->job_id(), |
|
32 | + esc_html__('contact-list-report.csv', 'event_espresso') |
|
33 | + ); |
|
34 | + $job_parameters->add_extra_data('filepath', $filepath); |
|
35 | + $job_parameters->set_job_size($this->count_units_to_process()); |
|
36 | + // we should also set the header columns |
|
37 | + $csv_data_for_row = $this->get_csv_data(0, 1); |
|
38 | + \EEH_Export::write_data_array_to_csv($filepath, $csv_data_for_row, true); |
|
39 | + // if we actually processed a row there, record it |
|
40 | + if ($job_parameters->job_size()) { |
|
41 | + $job_parameters->mark_processed(1); |
|
42 | + } |
|
43 | + return new JobStepResponse( |
|
44 | + $job_parameters, |
|
45 | + esc_html__('Contacts report started successfully...', 'event_espresso') |
|
46 | + ); |
|
47 | + } |
|
48 | 48 | |
49 | 49 | |
50 | - public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
|
51 | - { |
|
52 | - $csv_data = $this->get_csv_data($job_parameters->units_processed(), $batch_size); |
|
53 | - \EEH_Export::write_data_array_to_csv( |
|
54 | - $job_parameters->extra_datum('filepath'), |
|
55 | - $csv_data, |
|
56 | - false |
|
57 | - ); |
|
58 | - $units_processed = count($csv_data); |
|
59 | - $job_parameters->mark_processed($units_processed); |
|
60 | - $extra_response_data = array( |
|
61 | - 'file_url' => '', |
|
62 | - ); |
|
63 | - if ($units_processed < $batch_size) { |
|
64 | - $job_parameters->set_status(JobParameters::status_complete); |
|
65 | - $extra_response_data['file_url'] = $this->get_url_to_file($job_parameters->extra_datum('filepath')); |
|
66 | - } |
|
67 | - return new JobStepResponse( |
|
68 | - $job_parameters, |
|
69 | - sprintf( |
|
70 | - esc_html__('Wrote %1$s rows to report CSV file...', 'event_espresso'), |
|
71 | - count($csv_data) |
|
72 | - ), |
|
73 | - $extra_response_data |
|
74 | - ); |
|
75 | - } |
|
50 | + public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
|
51 | + { |
|
52 | + $csv_data = $this->get_csv_data($job_parameters->units_processed(), $batch_size); |
|
53 | + \EEH_Export::write_data_array_to_csv( |
|
54 | + $job_parameters->extra_datum('filepath'), |
|
55 | + $csv_data, |
|
56 | + false |
|
57 | + ); |
|
58 | + $units_processed = count($csv_data); |
|
59 | + $job_parameters->mark_processed($units_processed); |
|
60 | + $extra_response_data = array( |
|
61 | + 'file_url' => '', |
|
62 | + ); |
|
63 | + if ($units_processed < $batch_size) { |
|
64 | + $job_parameters->set_status(JobParameters::status_complete); |
|
65 | + $extra_response_data['file_url'] = $this->get_url_to_file($job_parameters->extra_datum('filepath')); |
|
66 | + } |
|
67 | + return new JobStepResponse( |
|
68 | + $job_parameters, |
|
69 | + sprintf( |
|
70 | + esc_html__('Wrote %1$s rows to report CSV file...', 'event_espresso'), |
|
71 | + count($csv_data) |
|
72 | + ), |
|
73 | + $extra_response_data |
|
74 | + ); |
|
75 | + } |
|
76 | 76 | |
77 | 77 | |
78 | - public function cleanup_job(JobParameters $job_parameters) |
|
79 | - { |
|
80 | - $this->_file_helper->delete( |
|
81 | - \EEH_File::remove_filename_from_filepath($job_parameters->extra_datum('filepath')), |
|
82 | - true, |
|
83 | - 'd' |
|
84 | - ); |
|
85 | - return new JobStepResponse($job_parameters, esc_html__('Cleaned up temporary file', 'event_espresso')); |
|
86 | - } |
|
78 | + public function cleanup_job(JobParameters $job_parameters) |
|
79 | + { |
|
80 | + $this->_file_helper->delete( |
|
81 | + \EEH_File::remove_filename_from_filepath($job_parameters->extra_datum('filepath')), |
|
82 | + true, |
|
83 | + 'd' |
|
84 | + ); |
|
85 | + return new JobStepResponse($job_parameters, esc_html__('Cleaned up temporary file', 'event_espresso')); |
|
86 | + } |
|
87 | 87 | |
88 | - public function count_units_to_process() |
|
89 | - { |
|
90 | - return \EEM_Attendee::instance()->count(array('caps' => \EEM_Base::caps_read_admin)); |
|
91 | - } |
|
88 | + public function count_units_to_process() |
|
89 | + { |
|
90 | + return \EEM_Attendee::instance()->count(array('caps' => \EEM_Base::caps_read_admin)); |
|
91 | + } |
|
92 | 92 | |
93 | - public function get_csv_data($offset, $limit) |
|
94 | - { |
|
95 | - $attendee_rows = \EEM_Attendee::instance()->get_all_wpdb_results( |
|
96 | - array( |
|
97 | - 'limit' => array($offset, $limit), |
|
98 | - 'force_join' => array('State', 'Country'), |
|
99 | - 'caps' => \EEM_Base::caps_read_admin, |
|
100 | - ) |
|
101 | - ); |
|
102 | - $csv_data = array(); |
|
103 | - foreach ($attendee_rows as $attendee_row) { |
|
104 | - $csv_row = array(); |
|
105 | - foreach (\EEM_Attendee::instance()->field_settings() as $field_name => $field_obj) { |
|
106 | - if ($field_name == 'STA_ID') { |
|
107 | - $state_name_field = \EEM_State::instance()->field_settings_for('STA_name'); |
|
108 | - $csv_row[ esc_html__('State', 'event_espresso') ] = $attendee_row[ $state_name_field->get_qualified_column() ]; |
|
109 | - } elseif ($field_name == 'CNT_ISO') { |
|
110 | - $country_name_field = \EEM_Country::instance()->field_settings_for('CNT_name'); |
|
111 | - $csv_row[ esc_html__('Country', 'event_espresso') ] = $attendee_row[ $country_name_field->get_qualified_column() ]; |
|
112 | - } else { |
|
113 | - $csv_row[ wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES) ] = $attendee_row[ $field_obj->get_qualified_column() ]; |
|
114 | - } |
|
115 | - } |
|
116 | - $csv_data[] = apply_filters( |
|
117 | - 'FHEE___EventEspresso_core_libraries_batch_JobHandlers_AttendeesReport__get_csv_data__row', |
|
118 | - $csv_row, |
|
119 | - $attendee_row |
|
120 | - ); |
|
121 | - } |
|
122 | - return $csv_data; |
|
123 | - } |
|
93 | + public function get_csv_data($offset, $limit) |
|
94 | + { |
|
95 | + $attendee_rows = \EEM_Attendee::instance()->get_all_wpdb_results( |
|
96 | + array( |
|
97 | + 'limit' => array($offset, $limit), |
|
98 | + 'force_join' => array('State', 'Country'), |
|
99 | + 'caps' => \EEM_Base::caps_read_admin, |
|
100 | + ) |
|
101 | + ); |
|
102 | + $csv_data = array(); |
|
103 | + foreach ($attendee_rows as $attendee_row) { |
|
104 | + $csv_row = array(); |
|
105 | + foreach (\EEM_Attendee::instance()->field_settings() as $field_name => $field_obj) { |
|
106 | + if ($field_name == 'STA_ID') { |
|
107 | + $state_name_field = \EEM_State::instance()->field_settings_for('STA_name'); |
|
108 | + $csv_row[ esc_html__('State', 'event_espresso') ] = $attendee_row[ $state_name_field->get_qualified_column() ]; |
|
109 | + } elseif ($field_name == 'CNT_ISO') { |
|
110 | + $country_name_field = \EEM_Country::instance()->field_settings_for('CNT_name'); |
|
111 | + $csv_row[ esc_html__('Country', 'event_espresso') ] = $attendee_row[ $country_name_field->get_qualified_column() ]; |
|
112 | + } else { |
|
113 | + $csv_row[ wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES) ] = $attendee_row[ $field_obj->get_qualified_column() ]; |
|
114 | + } |
|
115 | + } |
|
116 | + $csv_data[] = apply_filters( |
|
117 | + 'FHEE___EventEspresso_core_libraries_batch_JobHandlers_AttendeesReport__get_csv_data__row', |
|
118 | + $csv_row, |
|
119 | + $attendee_row |
|
120 | + ); |
|
121 | + } |
|
122 | + return $csv_data; |
|
123 | + } |
|
124 | 124 | } |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
23 | 23 | public function create_job(JobParameters $job_parameters) |
24 | 24 | { |
25 | - if (! \EE_Capabilities::instance()->current_user_can('ee_read_contacts', 'generating_report')) { |
|
25 | + if ( ! \EE_Capabilities::instance()->current_user_can('ee_read_contacts', 'generating_report')) { |
|
26 | 26 | throw new BatchRequestException( |
27 | 27 | esc_html__('You do not have permission to view contacts', 'event_espresso') |
28 | 28 | ); |
@@ -105,12 +105,12 @@ discard block |
||
105 | 105 | foreach (\EEM_Attendee::instance()->field_settings() as $field_name => $field_obj) { |
106 | 106 | if ($field_name == 'STA_ID') { |
107 | 107 | $state_name_field = \EEM_State::instance()->field_settings_for('STA_name'); |
108 | - $csv_row[ esc_html__('State', 'event_espresso') ] = $attendee_row[ $state_name_field->get_qualified_column() ]; |
|
108 | + $csv_row[esc_html__('State', 'event_espresso')] = $attendee_row[$state_name_field->get_qualified_column()]; |
|
109 | 109 | } elseif ($field_name == 'CNT_ISO') { |
110 | 110 | $country_name_field = \EEM_Country::instance()->field_settings_for('CNT_name'); |
111 | - $csv_row[ esc_html__('Country', 'event_espresso') ] = $attendee_row[ $country_name_field->get_qualified_column() ]; |
|
111 | + $csv_row[esc_html__('Country', 'event_espresso')] = $attendee_row[$country_name_field->get_qualified_column()]; |
|
112 | 112 | } else { |
113 | - $csv_row[ wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES) ] = $attendee_row[ $field_obj->get_qualified_column() ]; |
|
113 | + $csv_row[wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES)] = $attendee_row[$field_obj->get_qualified_column()]; |
|
114 | 114 | } |
115 | 115 | } |
116 | 116 | $csv_data[] = apply_filters( |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | public function create_job(JobParameters $job_parameters) |
46 | 46 | { |
47 | 47 | $event_id = intval($job_parameters->request_datum('EVT_ID', '0')); |
48 | - if (! \EE_Capabilities::instance()->current_user_can('ee_read_registrations', 'generating_report')) { |
|
48 | + if ( ! \EE_Capabilities::instance()->current_user_can('ee_read_registrations', 'generating_report')) { |
|
49 | 49 | throw new BatchRequestException(esc_html__('You do not have permission to view registrations', 'event_espresso')); |
50 | 50 | } |
51 | 51 | $filepath = $this->create_file_from_job_with_name( |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $query_params['force_join'][] = 'Event'; |
83 | 83 | } |
84 | 84 | } |
85 | - if (! isset($query_params['force_join'])) { |
|
85 | + if ( ! isset($query_params['force_join'])) { |
|
86 | 86 | $query_params['force_join'] = array('Event', 'Transaction', 'Ticket', 'Attendee'); |
87 | 87 | } |
88 | 88 | $job_parameters->add_extra_data('query_params', $query_params); |
@@ -181,10 +181,10 @@ discard block |
||
181 | 181 | $question_where_params = array(); |
182 | 182 | foreach ($reg_where_params as $key => $val) { |
183 | 183 | if (\EEM_Registration::instance()->is_logic_query_param_key($key)) { |
184 | - $question_where_params[ $key ] = $this->_change_registration_where_params_to_question_where_params($val); |
|
184 | + $question_where_params[$key] = $this->_change_registration_where_params_to_question_where_params($val); |
|
185 | 185 | } else { |
186 | 186 | // it's a normal where condition |
187 | - $question_where_params[ 'Question_Group.Event.Registration.' . $key ] = $val; |
|
187 | + $question_where_params['Question_Group.Event.Registration.'.$key] = $val; |
|
188 | 188 | } |
189 | 189 | } |
190 | 190 | return $question_where_params; |
@@ -277,9 +277,9 @@ discard block |
||
277 | 277 | foreach ($registration_rows as $reg_row) { |
278 | 278 | if (is_array($reg_row)) { |
279 | 279 | $reg_csv_array = array(); |
280 | - if (! $event_id) { |
|
280 | + if ( ! $event_id) { |
|
281 | 281 | // get the event's name and Id |
282 | - $reg_csv_array[ (string) esc_html__('Event', 'event_espresso') ] = sprintf( |
|
282 | + $reg_csv_array[(string) esc_html__('Event', 'event_espresso')] = sprintf( |
|
283 | 283 | /* translators: 1: event name, 2: event ID */ |
284 | 284 | esc_html__('%1$s (%2$s)', 'event_espresso'), |
285 | 285 | EEH_Export::prepare_value_from_db_for_display( |
@@ -327,13 +327,13 @@ discard block |
||
327 | 327 | $value = EEH_Export::prepare_value_from_db_for_display( |
328 | 328 | $reg_model, |
329 | 329 | $field_name, |
330 | - $reg_row[ $field->get_qualified_column() ] |
|
330 | + $reg_row[$field->get_qualified_column()] |
|
331 | 331 | ); |
332 | 332 | } |
333 | - $reg_csv_array[ EEH_Export::get_column_name_for_field($field) ] = $value; |
|
333 | + $reg_csv_array[EEH_Export::get_column_name_for_field($field)] = $value; |
|
334 | 334 | if ($field_name == 'REG_final_price') { |
335 | 335 | // add a column named Currency after the final price |
336 | - $reg_csv_array[ (string) esc_html__("Currency", "event_espresso") ] = \EE_Config::instance()->currency->code; |
|
336 | + $reg_csv_array[(string) esc_html__("Currency", "event_espresso")] = \EE_Config::instance()->currency->code; |
|
337 | 337 | } |
338 | 338 | } |
339 | 339 | // get pretty status |
@@ -345,17 +345,17 @@ discard block |
||
345 | 345 | false, |
346 | 346 | 'sentence' |
347 | 347 | ); |
348 | - $reg_csv_array[ (string) esc_html__("Registration Status", 'event_espresso') ] = $stati[ $reg_row['Registration.STS_ID'] ]; |
|
348 | + $reg_csv_array[(string) esc_html__("Registration Status", 'event_espresso')] = $stati[$reg_row['Registration.STS_ID']]; |
|
349 | 349 | // get pretty transaction status |
350 | - $reg_csv_array[ (string) esc_html__("Transaction Status", 'event_espresso') ] = $stati[ $reg_row['TransactionTable.STS_ID'] ]; |
|
351 | - $reg_csv_array[ (string) esc_html__('Transaction Amount Due', 'event_espresso') ] = $is_primary_reg |
|
350 | + $reg_csv_array[(string) esc_html__("Transaction Status", 'event_espresso')] = $stati[$reg_row['TransactionTable.STS_ID']]; |
|
351 | + $reg_csv_array[(string) esc_html__('Transaction Amount Due', 'event_espresso')] = $is_primary_reg |
|
352 | 352 | ? EEH_Export::prepare_value_from_db_for_display( |
353 | 353 | EEM_Transaction::instance(), |
354 | 354 | 'TXN_total', |
355 | 355 | $reg_row['TransactionTable.TXN_total'], |
356 | 356 | 'localized_float' |
357 | 357 | ) : '0.00'; |
358 | - $reg_csv_array[ (string) esc_html__('Amount Paid', 'event_espresso') ] = $is_primary_reg |
|
358 | + $reg_csv_array[(string) esc_html__('Amount Paid', 'event_espresso')] = $is_primary_reg |
|
359 | 359 | ? EEH_Export::prepare_value_from_db_for_display( |
360 | 360 | EEM_Transaction::instance(), |
361 | 361 | 'TXN_paid', |
@@ -386,14 +386,14 @@ discard block |
||
386 | 386 | ? $payment_method_and_gateway_txn_id['payment_time'] : ''; |
387 | 387 | } |
388 | 388 | } |
389 | - $reg_csv_array[ (string) esc_html__('Payment Date(s)', 'event_espresso') ] = implode(',', $payment_times); |
|
390 | - $reg_csv_array[ (string) esc_html__('Payment Method(s)', 'event_espresso') ] = implode(",", $payment_methods); |
|
391 | - $reg_csv_array[ (string) esc_html__('Gateway Transaction ID(s)', 'event_espresso') ] = implode( |
|
389 | + $reg_csv_array[(string) esc_html__('Payment Date(s)', 'event_espresso')] = implode(',', $payment_times); |
|
390 | + $reg_csv_array[(string) esc_html__('Payment Method(s)', 'event_espresso')] = implode(",", $payment_methods); |
|
391 | + $reg_csv_array[(string) esc_html__('Gateway Transaction ID(s)', 'event_espresso')] = implode( |
|
392 | 392 | ',', |
393 | 393 | $gateway_txn_ids_etc |
394 | 394 | ); |
395 | 395 | // get whether or not the user has checked in |
396 | - $reg_csv_array[ (string) esc_html__("Check-Ins", "event_espresso") ] = $reg_model->count_related( |
|
396 | + $reg_csv_array[(string) esc_html__("Check-Ins", "event_espresso")] = $reg_model->count_related( |
|
397 | 397 | $reg_row['Registration.REG_ID'], |
398 | 398 | 'Checkin' |
399 | 399 | ); |
@@ -425,8 +425,8 @@ discard block |
||
425 | 425 | $ticket_name = esc_html__('Unknown', 'event_espresso'); |
426 | 426 | $datetimes_strings = array(esc_html__('Unknown', 'event_espresso')); |
427 | 427 | } |
428 | - $reg_csv_array[ (string) $ticket_model->field_settings_for('TKT_name')->get_nicename() ] = $ticket_name; |
|
429 | - $reg_csv_array[ (string) esc_html__("Datetimes of Ticket", "event_espresso") ] = implode(", ", $datetimes_strings); |
|
428 | + $reg_csv_array[(string) $ticket_model->field_settings_for('TKT_name')->get_nicename()] = $ticket_name; |
|
429 | + $reg_csv_array[(string) esc_html__("Datetimes of Ticket", "event_espresso")] = implode(", ", $datetimes_strings); |
|
430 | 430 | // get datetime(s) of registration |
431 | 431 | // add attendee columns |
432 | 432 | foreach ($att_fields_to_include as $att_field_name) { |
@@ -446,18 +446,18 @@ discard block |
||
446 | 446 | $value = EEH_Export::prepare_value_from_db_for_display( |
447 | 447 | EEM_Attendee::instance(), |
448 | 448 | $att_field_name, |
449 | - $reg_row[ $field_obj->get_qualified_column() ] |
|
449 | + $reg_row[$field_obj->get_qualified_column()] |
|
450 | 450 | ); |
451 | 451 | } |
452 | 452 | } else { |
453 | 453 | $value = ''; |
454 | 454 | } |
455 | - $reg_csv_array[ EEH_Export::get_column_name_for_field($field_obj) ] = $value; |
|
455 | + $reg_csv_array[EEH_Export::get_column_name_for_field($field_obj)] = $value; |
|
456 | 456 | } |
457 | 457 | // make sure each registration has the same questions in the same order |
458 | 458 | foreach ($question_labels as $question_label) { |
459 | - if (! isset($reg_csv_array[ $question_label ])) { |
|
460 | - $reg_csv_array[ $question_label ] = null; |
|
459 | + if ( ! isset($reg_csv_array[$question_label])) { |
|
460 | + $reg_csv_array[$question_label] = null; |
|
461 | 461 | } |
462 | 462 | } |
463 | 463 | $answers = EEM_Answer::instance()->get_all_wpdb_results(array( |
@@ -484,12 +484,12 @@ discard block |
||
484 | 484 | isset($answer_row['Question.QST_type']) |
485 | 485 | && $answer_row['Question.QST_type'] == EEM_Question::QST_type_state |
486 | 486 | ) { |
487 | - $reg_csv_array[ $question_label ] = EEM_State::instance()->get_state_name_by_ID( |
|
487 | + $reg_csv_array[$question_label] = EEM_State::instance()->get_state_name_by_ID( |
|
488 | 488 | $answer_row['Answer.ANS_value'] |
489 | 489 | ); |
490 | 490 | } else { |
491 | 491 | // this isn't for html, so don't show html entities |
492 | - $reg_csv_array[ $question_label ] = html_entity_decode( |
|
492 | + $reg_csv_array[$question_label] = html_entity_decode( |
|
493 | 493 | EEH_Export::prepare_value_from_db_for_display( |
494 | 494 | EEM_Answer::instance(), |
495 | 495 | 'ANS_value', |
@@ -524,7 +524,7 @@ discard block |
||
524 | 524 | $model = EE_Registry::instance()->load_model($model_name); |
525 | 525 | foreach ($field_list as $field_name) { |
526 | 526 | $field = $model->field_settings_for($field_name); |
527 | - $reg_csv_array[ EEH_Export::get_column_name_for_field($field) ] = null; |
|
527 | + $reg_csv_array[EEH_Export::get_column_name_for_field($field)] = null; |
|
528 | 528 | } |
529 | 529 | } |
530 | 530 | $registrations_csv_ready_array[] = $reg_csv_array; |
@@ -31,541 +31,541 @@ |
||
31 | 31 | */ |
32 | 32 | class RegistrationsReport extends JobHandlerFile |
33 | 33 | { |
34 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
35 | - // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
36 | - /** |
|
37 | - * Performs any necessary setup for starting the job. This is also a good |
|
38 | - * place to setup the $job_arguments which will be used for subsequent HTTP requests |
|
39 | - * when continue_job will be called |
|
40 | - * |
|
41 | - * @param JobParameters $job_parameters |
|
42 | - * @throws BatchRequestException |
|
43 | - * @return JobStepResponse |
|
44 | - */ |
|
45 | - public function create_job(JobParameters $job_parameters) |
|
46 | - { |
|
47 | - $event_id = intval($job_parameters->request_datum('EVT_ID', '0')); |
|
48 | - if (! \EE_Capabilities::instance()->current_user_can('ee_read_registrations', 'generating_report')) { |
|
49 | - throw new BatchRequestException(esc_html__('You do not have permission to view registrations', 'event_espresso')); |
|
50 | - } |
|
51 | - $filepath = $this->create_file_from_job_with_name( |
|
52 | - $job_parameters->job_id(), |
|
53 | - $this->get_filename($event_id) |
|
54 | - ); |
|
55 | - $job_parameters->add_extra_data('filepath', $filepath); |
|
56 | - if ($job_parameters->request_datum('use_filters', false)) { |
|
57 | - $query_params = maybe_unserialize($job_parameters->request_datum('filters', array())); |
|
58 | - } else { |
|
59 | - $query_params = apply_filters('FHEE__EE_Export__report_registration_for_event', array( |
|
60 | - array( |
|
61 | - 'OR' => array( |
|
62 | - // don't include registrations from failed or abandoned transactions... |
|
63 | - 'Transaction.STS_ID' => array( |
|
64 | - 'NOT IN', |
|
65 | - array( |
|
66 | - EEM_Transaction::failed_status_code, |
|
67 | - EEM_Transaction::abandoned_status_code, |
|
68 | - ), |
|
69 | - ), |
|
70 | - // unless the registration is approved, in which case include it regardless of transaction status |
|
71 | - 'STS_ID' => \EEM_Registration::status_id_approved, |
|
72 | - ), |
|
73 | - 'Ticket.TKT_deleted' => array('IN', array(true, false)), |
|
74 | - ), |
|
75 | - 'order_by' => array('Transaction.TXN_ID' => 'asc', 'REG_count' => 'asc'), |
|
76 | - 'force_join' => array('Transaction', 'Ticket', 'Attendee'), |
|
77 | - 'caps' => \EEM_Base::caps_read_admin, |
|
78 | - ), $event_id); |
|
79 | - if ($event_id) { |
|
80 | - $query_params[0]['EVT_ID'] = $event_id; |
|
81 | - } else { |
|
82 | - $query_params['force_join'][] = 'Event'; |
|
83 | - } |
|
84 | - } |
|
85 | - if (! isset($query_params['force_join'])) { |
|
86 | - $query_params['force_join'] = array('Event', 'Transaction', 'Ticket', 'Attendee'); |
|
87 | - } |
|
88 | - $job_parameters->add_extra_data('query_params', $query_params); |
|
89 | - $question_labels = $this->_get_question_labels($query_params); |
|
90 | - $job_parameters->add_extra_data('question_labels', $question_labels); |
|
91 | - $job_parameters->set_job_size( |
|
92 | - \EEM_Registration::instance()->count( |
|
93 | - array_diff_key( |
|
94 | - $query_params, |
|
95 | - array_flip( |
|
96 | - array('limit') |
|
97 | - ) |
|
98 | - ) |
|
99 | - ) |
|
100 | - ); |
|
101 | - // we should also set the header columns |
|
102 | - $csv_data_for_row = $this->get_csv_data_for( |
|
103 | - $event_id, |
|
104 | - 0, |
|
105 | - 1, |
|
106 | - $job_parameters->extra_datum('question_labels'), |
|
107 | - $job_parameters->extra_datum('query_params') |
|
108 | - ); |
|
109 | - EEH_Export::write_data_array_to_csv($filepath, $csv_data_for_row, true); |
|
110 | - // if we actually processed a row there, record it |
|
111 | - if ($job_parameters->job_size()) { |
|
112 | - $job_parameters->mark_processed(1); |
|
113 | - } |
|
114 | - return new JobStepResponse( |
|
115 | - $job_parameters, |
|
116 | - esc_html__('Registrations report started successfully...', 'event_espresso') |
|
117 | - ); |
|
118 | - } |
|
34 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
35 | + // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
36 | + /** |
|
37 | + * Performs any necessary setup for starting the job. This is also a good |
|
38 | + * place to setup the $job_arguments which will be used for subsequent HTTP requests |
|
39 | + * when continue_job will be called |
|
40 | + * |
|
41 | + * @param JobParameters $job_parameters |
|
42 | + * @throws BatchRequestException |
|
43 | + * @return JobStepResponse |
|
44 | + */ |
|
45 | + public function create_job(JobParameters $job_parameters) |
|
46 | + { |
|
47 | + $event_id = intval($job_parameters->request_datum('EVT_ID', '0')); |
|
48 | + if (! \EE_Capabilities::instance()->current_user_can('ee_read_registrations', 'generating_report')) { |
|
49 | + throw new BatchRequestException(esc_html__('You do not have permission to view registrations', 'event_espresso')); |
|
50 | + } |
|
51 | + $filepath = $this->create_file_from_job_with_name( |
|
52 | + $job_parameters->job_id(), |
|
53 | + $this->get_filename($event_id) |
|
54 | + ); |
|
55 | + $job_parameters->add_extra_data('filepath', $filepath); |
|
56 | + if ($job_parameters->request_datum('use_filters', false)) { |
|
57 | + $query_params = maybe_unserialize($job_parameters->request_datum('filters', array())); |
|
58 | + } else { |
|
59 | + $query_params = apply_filters('FHEE__EE_Export__report_registration_for_event', array( |
|
60 | + array( |
|
61 | + 'OR' => array( |
|
62 | + // don't include registrations from failed or abandoned transactions... |
|
63 | + 'Transaction.STS_ID' => array( |
|
64 | + 'NOT IN', |
|
65 | + array( |
|
66 | + EEM_Transaction::failed_status_code, |
|
67 | + EEM_Transaction::abandoned_status_code, |
|
68 | + ), |
|
69 | + ), |
|
70 | + // unless the registration is approved, in which case include it regardless of transaction status |
|
71 | + 'STS_ID' => \EEM_Registration::status_id_approved, |
|
72 | + ), |
|
73 | + 'Ticket.TKT_deleted' => array('IN', array(true, false)), |
|
74 | + ), |
|
75 | + 'order_by' => array('Transaction.TXN_ID' => 'asc', 'REG_count' => 'asc'), |
|
76 | + 'force_join' => array('Transaction', 'Ticket', 'Attendee'), |
|
77 | + 'caps' => \EEM_Base::caps_read_admin, |
|
78 | + ), $event_id); |
|
79 | + if ($event_id) { |
|
80 | + $query_params[0]['EVT_ID'] = $event_id; |
|
81 | + } else { |
|
82 | + $query_params['force_join'][] = 'Event'; |
|
83 | + } |
|
84 | + } |
|
85 | + if (! isset($query_params['force_join'])) { |
|
86 | + $query_params['force_join'] = array('Event', 'Transaction', 'Ticket', 'Attendee'); |
|
87 | + } |
|
88 | + $job_parameters->add_extra_data('query_params', $query_params); |
|
89 | + $question_labels = $this->_get_question_labels($query_params); |
|
90 | + $job_parameters->add_extra_data('question_labels', $question_labels); |
|
91 | + $job_parameters->set_job_size( |
|
92 | + \EEM_Registration::instance()->count( |
|
93 | + array_diff_key( |
|
94 | + $query_params, |
|
95 | + array_flip( |
|
96 | + array('limit') |
|
97 | + ) |
|
98 | + ) |
|
99 | + ) |
|
100 | + ); |
|
101 | + // we should also set the header columns |
|
102 | + $csv_data_for_row = $this->get_csv_data_for( |
|
103 | + $event_id, |
|
104 | + 0, |
|
105 | + 1, |
|
106 | + $job_parameters->extra_datum('question_labels'), |
|
107 | + $job_parameters->extra_datum('query_params') |
|
108 | + ); |
|
109 | + EEH_Export::write_data_array_to_csv($filepath, $csv_data_for_row, true); |
|
110 | + // if we actually processed a row there, record it |
|
111 | + if ($job_parameters->job_size()) { |
|
112 | + $job_parameters->mark_processed(1); |
|
113 | + } |
|
114 | + return new JobStepResponse( |
|
115 | + $job_parameters, |
|
116 | + esc_html__('Registrations report started successfully...', 'event_espresso') |
|
117 | + ); |
|
118 | + } |
|
119 | 119 | |
120 | 120 | |
121 | - /** |
|
122 | - * Gets the filename |
|
123 | - * |
|
124 | - * @return string |
|
125 | - */ |
|
126 | - protected function get_filename() |
|
127 | - { |
|
128 | - return apply_filters( |
|
129 | - 'FHEE__EventEspressoBatchRequest__JobHandlers__RegistrationsReport__get_filename', |
|
130 | - sprintf( |
|
131 | - "event-espresso-registrations-%s.csv", |
|
132 | - str_replace(array(':', ' '), '-', current_time('mysql')) |
|
133 | - ) |
|
134 | - ); |
|
135 | - } |
|
121 | + /** |
|
122 | + * Gets the filename |
|
123 | + * |
|
124 | + * @return string |
|
125 | + */ |
|
126 | + protected function get_filename() |
|
127 | + { |
|
128 | + return apply_filters( |
|
129 | + 'FHEE__EventEspressoBatchRequest__JobHandlers__RegistrationsReport__get_filename', |
|
130 | + sprintf( |
|
131 | + "event-espresso-registrations-%s.csv", |
|
132 | + str_replace(array(':', ' '), '-', current_time('mysql')) |
|
133 | + ) |
|
134 | + ); |
|
135 | + } |
|
136 | 136 | |
137 | 137 | |
138 | - /** |
|
139 | - * Gets the questions which are to be used for this report, so they |
|
140 | - * can be remembered for later |
|
141 | - * |
|
142 | - * @param array $registration_query_params |
|
143 | - * @return array question admin labels to be used for this report |
|
144 | - */ |
|
145 | - protected function _get_question_labels($registration_query_params) |
|
146 | - { |
|
147 | - $where = isset($registration_query_params[0]) ? $registration_query_params[0] : null; |
|
148 | - $question_query_params = array(); |
|
149 | - if ($where !== null) { |
|
150 | - $question_query_params = array( |
|
151 | - $this->_change_registration_where_params_to_question_where_params($registration_query_params[0]), |
|
152 | - ); |
|
153 | - } |
|
154 | - // Make sure it's not a system question |
|
155 | - $question_query_params[0]['OR*not-system-questions'] = [ |
|
156 | - 'QST_system' => '', |
|
157 | - 'QST_system*null' => ['IS_NULL'] |
|
158 | - ]; |
|
159 | - if ( |
|
160 | - apply_filters( |
|
161 | - 'FHEE__EventEspressoBatchRequest__JobHandlers__RegistrationsReport___get_question_labels__only_include_answered_questions', |
|
162 | - false, |
|
163 | - $registration_query_params |
|
164 | - ) |
|
165 | - ) { |
|
166 | - $question_query_params[0]['Answer.ANS_ID'] = array('IS_NOT_NULL'); |
|
167 | - } |
|
168 | - $question_query_params['group_by'] = array('QST_ID'); |
|
169 | - return array_unique(EEM_Question::instance()->get_col($question_query_params, 'QST_admin_label')); |
|
170 | - } |
|
138 | + /** |
|
139 | + * Gets the questions which are to be used for this report, so they |
|
140 | + * can be remembered for later |
|
141 | + * |
|
142 | + * @param array $registration_query_params |
|
143 | + * @return array question admin labels to be used for this report |
|
144 | + */ |
|
145 | + protected function _get_question_labels($registration_query_params) |
|
146 | + { |
|
147 | + $where = isset($registration_query_params[0]) ? $registration_query_params[0] : null; |
|
148 | + $question_query_params = array(); |
|
149 | + if ($where !== null) { |
|
150 | + $question_query_params = array( |
|
151 | + $this->_change_registration_where_params_to_question_where_params($registration_query_params[0]), |
|
152 | + ); |
|
153 | + } |
|
154 | + // Make sure it's not a system question |
|
155 | + $question_query_params[0]['OR*not-system-questions'] = [ |
|
156 | + 'QST_system' => '', |
|
157 | + 'QST_system*null' => ['IS_NULL'] |
|
158 | + ]; |
|
159 | + if ( |
|
160 | + apply_filters( |
|
161 | + 'FHEE__EventEspressoBatchRequest__JobHandlers__RegistrationsReport___get_question_labels__only_include_answered_questions', |
|
162 | + false, |
|
163 | + $registration_query_params |
|
164 | + ) |
|
165 | + ) { |
|
166 | + $question_query_params[0]['Answer.ANS_ID'] = array('IS_NOT_NULL'); |
|
167 | + } |
|
168 | + $question_query_params['group_by'] = array('QST_ID'); |
|
169 | + return array_unique(EEM_Question::instance()->get_col($question_query_params, 'QST_admin_label')); |
|
170 | + } |
|
171 | 171 | |
172 | 172 | |
173 | - /** |
|
174 | - * Takes where params meant for registrations and changes them to work for questions |
|
175 | - * |
|
176 | - * @param array $reg_where_params |
|
177 | - * @return array |
|
178 | - */ |
|
179 | - protected function _change_registration_where_params_to_question_where_params($reg_where_params) |
|
180 | - { |
|
181 | - $question_where_params = array(); |
|
182 | - foreach ($reg_where_params as $key => $val) { |
|
183 | - if (\EEM_Registration::instance()->is_logic_query_param_key($key)) { |
|
184 | - $question_where_params[ $key ] = $this->_change_registration_where_params_to_question_where_params($val); |
|
185 | - } else { |
|
186 | - // it's a normal where condition |
|
187 | - $question_where_params[ 'Question_Group.Event.Registration.' . $key ] = $val; |
|
188 | - } |
|
189 | - } |
|
190 | - return $question_where_params; |
|
191 | - } |
|
173 | + /** |
|
174 | + * Takes where params meant for registrations and changes them to work for questions |
|
175 | + * |
|
176 | + * @param array $reg_where_params |
|
177 | + * @return array |
|
178 | + */ |
|
179 | + protected function _change_registration_where_params_to_question_where_params($reg_where_params) |
|
180 | + { |
|
181 | + $question_where_params = array(); |
|
182 | + foreach ($reg_where_params as $key => $val) { |
|
183 | + if (\EEM_Registration::instance()->is_logic_query_param_key($key)) { |
|
184 | + $question_where_params[ $key ] = $this->_change_registration_where_params_to_question_where_params($val); |
|
185 | + } else { |
|
186 | + // it's a normal where condition |
|
187 | + $question_where_params[ 'Question_Group.Event.Registration.' . $key ] = $val; |
|
188 | + } |
|
189 | + } |
|
190 | + return $question_where_params; |
|
191 | + } |
|
192 | 192 | |
193 | 193 | |
194 | - /** |
|
195 | - * Performs another step of the job |
|
196 | - * |
|
197 | - * @param JobParameters $job_parameters |
|
198 | - * @param int $batch_size |
|
199 | - * @return JobStepResponse |
|
200 | - * @throws \EE_Error |
|
201 | - */ |
|
202 | - public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
|
203 | - { |
|
204 | - if ($job_parameters->units_processed() < $job_parameters->job_size()) { |
|
205 | - $csv_data = $this->get_csv_data_for( |
|
206 | - $job_parameters->request_datum('EVT_ID', '0'), |
|
207 | - $job_parameters->units_processed(), |
|
208 | - $batch_size, |
|
209 | - $job_parameters->extra_datum('question_labels'), |
|
210 | - $job_parameters->extra_datum('query_params') |
|
211 | - ); |
|
212 | - EEH_Export::write_data_array_to_csv($job_parameters->extra_datum('filepath'), $csv_data, false); |
|
213 | - $units_processed = count($csv_data); |
|
214 | - } else { |
|
215 | - $csv_data = array(); |
|
216 | - $units_processed = 0; |
|
217 | - } |
|
218 | - $job_parameters->mark_processed($units_processed); |
|
219 | - $extra_response_data = array( |
|
220 | - 'file_url' => '', |
|
221 | - ); |
|
222 | - if ($units_processed < $batch_size) { |
|
223 | - $job_parameters->set_status(JobParameters::status_complete); |
|
224 | - $extra_response_data['file_url'] = $this->get_url_to_file($job_parameters->extra_datum('filepath')); |
|
225 | - } |
|
194 | + /** |
|
195 | + * Performs another step of the job |
|
196 | + * |
|
197 | + * @param JobParameters $job_parameters |
|
198 | + * @param int $batch_size |
|
199 | + * @return JobStepResponse |
|
200 | + * @throws \EE_Error |
|
201 | + */ |
|
202 | + public function continue_job(JobParameters $job_parameters, $batch_size = 50) |
|
203 | + { |
|
204 | + if ($job_parameters->units_processed() < $job_parameters->job_size()) { |
|
205 | + $csv_data = $this->get_csv_data_for( |
|
206 | + $job_parameters->request_datum('EVT_ID', '0'), |
|
207 | + $job_parameters->units_processed(), |
|
208 | + $batch_size, |
|
209 | + $job_parameters->extra_datum('question_labels'), |
|
210 | + $job_parameters->extra_datum('query_params') |
|
211 | + ); |
|
212 | + EEH_Export::write_data_array_to_csv($job_parameters->extra_datum('filepath'), $csv_data, false); |
|
213 | + $units_processed = count($csv_data); |
|
214 | + } else { |
|
215 | + $csv_data = array(); |
|
216 | + $units_processed = 0; |
|
217 | + } |
|
218 | + $job_parameters->mark_processed($units_processed); |
|
219 | + $extra_response_data = array( |
|
220 | + 'file_url' => '', |
|
221 | + ); |
|
222 | + if ($units_processed < $batch_size) { |
|
223 | + $job_parameters->set_status(JobParameters::status_complete); |
|
224 | + $extra_response_data['file_url'] = $this->get_url_to_file($job_parameters->extra_datum('filepath')); |
|
225 | + } |
|
226 | 226 | |
227 | - return new JobStepResponse( |
|
228 | - $job_parameters, |
|
229 | - sprintf(esc_html__('Wrote %1$s rows to report CSV file...', 'event_espresso'), count((array) $csv_data)), |
|
230 | - $extra_response_data |
|
231 | - ); |
|
232 | - } |
|
227 | + return new JobStepResponse( |
|
228 | + $job_parameters, |
|
229 | + sprintf(esc_html__('Wrote %1$s rows to report CSV file...', 'event_espresso'), count((array) $csv_data)), |
|
230 | + $extra_response_data |
|
231 | + ); |
|
232 | + } |
|
233 | 233 | |
234 | 234 | |
235 | - /** |
|
236 | - * Gets the csv data for a batch of registrations |
|
237 | - * |
|
238 | - * @param int|null $event_id |
|
239 | - * @param int $offset |
|
240 | - * @param int $limit |
|
241 | - * @param array $question_labels the IDs for all the questions which were answered by someone in this selection |
|
242 | - * @param array $query_params for using where querying the model |
|
243 | - * @return array top-level keys are numeric, next-level keys are column headers |
|
244 | - * @throws \EE_Error |
|
245 | - */ |
|
246 | - public function get_csv_data_for($event_id, $offset, $limit, $question_labels, $query_params) |
|
247 | - { |
|
248 | - $reg_fields_to_include = array( |
|
249 | - 'TXN_ID', |
|
250 | - 'ATT_ID', |
|
251 | - 'REG_ID', |
|
252 | - 'REG_date', |
|
253 | - 'REG_code', |
|
254 | - 'REG_count', |
|
255 | - 'REG_final_price', |
|
256 | - ); |
|
257 | - $att_fields_to_include = array( |
|
258 | - 'ATT_fname', |
|
259 | - 'ATT_lname', |
|
260 | - 'ATT_email', |
|
261 | - 'ATT_address', |
|
262 | - 'ATT_address2', |
|
263 | - 'ATT_city', |
|
264 | - 'STA_ID', |
|
265 | - 'CNT_ISO', |
|
266 | - 'ATT_zip', |
|
267 | - 'ATT_phone', |
|
268 | - ); |
|
269 | - $registrations_csv_ready_array = array(); |
|
270 | - $reg_model = EE_Registry::instance()->load_model('Registration'); |
|
271 | - $query_params['limit'] = array($offset, $limit); |
|
272 | - $registration_rows = $reg_model->get_all_wpdb_results($query_params); |
|
273 | - $registration_ids = array(); |
|
274 | - foreach ($registration_rows as $reg_row) { |
|
275 | - $registration_ids[] = intval($reg_row['Registration.REG_ID']); |
|
276 | - } |
|
277 | - foreach ($registration_rows as $reg_row) { |
|
278 | - if (is_array($reg_row)) { |
|
279 | - $reg_csv_array = array(); |
|
280 | - if (! $event_id) { |
|
281 | - // get the event's name and Id |
|
282 | - $reg_csv_array[ (string) esc_html__('Event', 'event_espresso') ] = sprintf( |
|
283 | - /* translators: 1: event name, 2: event ID */ |
|
284 | - esc_html__('%1$s (%2$s)', 'event_espresso'), |
|
285 | - EEH_Export::prepare_value_from_db_for_display( |
|
286 | - EEM_Event::instance(), |
|
287 | - 'EVT_name', |
|
288 | - $reg_row['Event_CPT.post_title'] |
|
289 | - ), |
|
290 | - $reg_row['Event_CPT.ID'] |
|
291 | - ); |
|
292 | - } |
|
293 | - $is_primary_reg = $reg_row['Registration.REG_count'] == '1' ? true : false; |
|
294 | - /*@var $reg_row EE_Registration */ |
|
295 | - foreach ($reg_fields_to_include as $field_name) { |
|
296 | - $field = $reg_model->field_settings_for($field_name); |
|
297 | - if ($field_name == 'REG_final_price') { |
|
298 | - $value = EEH_Export::prepare_value_from_db_for_display( |
|
299 | - $reg_model, |
|
300 | - $field_name, |
|
301 | - $reg_row['Registration.REG_final_price'], |
|
302 | - 'localized_float' |
|
303 | - ); |
|
304 | - } elseif ($field_name == 'REG_count') { |
|
305 | - $value = sprintf( |
|
306 | - /* translators: 1: number of registration in group (REG_count), 2: registration group size (REG_group_size) */ |
|
307 | - esc_html__('%1$s of %2$s', 'event_espresso'), |
|
308 | - EEH_Export::prepare_value_from_db_for_display( |
|
309 | - $reg_model, |
|
310 | - 'REG_count', |
|
311 | - $reg_row['Registration.REG_count'] |
|
312 | - ), |
|
313 | - EEH_Export::prepare_value_from_db_for_display( |
|
314 | - $reg_model, |
|
315 | - 'REG_group_size', |
|
316 | - $reg_row['Registration.REG_group_size'] |
|
317 | - ) |
|
318 | - ); |
|
319 | - } elseif ($field_name == 'REG_date') { |
|
320 | - $value = EEH_Export::prepare_value_from_db_for_display( |
|
321 | - $reg_model, |
|
322 | - $field_name, |
|
323 | - $reg_row['Registration.REG_date'], |
|
324 | - 'no_html' |
|
325 | - ); |
|
326 | - } else { |
|
327 | - $value = EEH_Export::prepare_value_from_db_for_display( |
|
328 | - $reg_model, |
|
329 | - $field_name, |
|
330 | - $reg_row[ $field->get_qualified_column() ] |
|
331 | - ); |
|
332 | - } |
|
333 | - $reg_csv_array[ EEH_Export::get_column_name_for_field($field) ] = $value; |
|
334 | - if ($field_name == 'REG_final_price') { |
|
335 | - // add a column named Currency after the final price |
|
336 | - $reg_csv_array[ (string) esc_html__("Currency", "event_espresso") ] = \EE_Config::instance()->currency->code; |
|
337 | - } |
|
338 | - } |
|
339 | - // get pretty status |
|
340 | - $stati = EEM_Status::instance()->localized_status( |
|
341 | - array( |
|
342 | - $reg_row['Registration.STS_ID'] => esc_html__('unknown', 'event_espresso'), |
|
343 | - $reg_row['TransactionTable.STS_ID'] => esc_html__('unknown', 'event_espresso'), |
|
344 | - ), |
|
345 | - false, |
|
346 | - 'sentence' |
|
347 | - ); |
|
348 | - $reg_csv_array[ (string) esc_html__("Registration Status", 'event_espresso') ] = $stati[ $reg_row['Registration.STS_ID'] ]; |
|
349 | - // get pretty transaction status |
|
350 | - $reg_csv_array[ (string) esc_html__("Transaction Status", 'event_espresso') ] = $stati[ $reg_row['TransactionTable.STS_ID'] ]; |
|
351 | - $reg_csv_array[ (string) esc_html__('Transaction Amount Due', 'event_espresso') ] = $is_primary_reg |
|
352 | - ? EEH_Export::prepare_value_from_db_for_display( |
|
353 | - EEM_Transaction::instance(), |
|
354 | - 'TXN_total', |
|
355 | - $reg_row['TransactionTable.TXN_total'], |
|
356 | - 'localized_float' |
|
357 | - ) : '0.00'; |
|
358 | - $reg_csv_array[ (string) esc_html__('Amount Paid', 'event_espresso') ] = $is_primary_reg |
|
359 | - ? EEH_Export::prepare_value_from_db_for_display( |
|
360 | - EEM_Transaction::instance(), |
|
361 | - 'TXN_paid', |
|
362 | - $reg_row['TransactionTable.TXN_paid'], |
|
363 | - 'localized_float' |
|
364 | - ) : '0.00'; |
|
365 | - $payment_methods = array(); |
|
366 | - $gateway_txn_ids_etc = array(); |
|
367 | - $payment_times = array(); |
|
368 | - if ($is_primary_reg && $reg_row['TransactionTable.TXN_ID']) { |
|
369 | - $payments_info = EEM_Payment::instance()->get_all_wpdb_results( |
|
370 | - array( |
|
371 | - array( |
|
372 | - 'TXN_ID' => $reg_row['TransactionTable.TXN_ID'], |
|
373 | - 'STS_ID' => EEM_Payment::status_id_approved, |
|
374 | - ), |
|
375 | - 'force_join' => array('Payment_Method'), |
|
376 | - ), |
|
377 | - ARRAY_A, |
|
378 | - 'Payment_Method.PMD_admin_name as name, Payment.PAY_txn_id_chq_nmbr as gateway_txn_id, Payment.PAY_timestamp as payment_time' |
|
379 | - ); |
|
380 | - foreach ($payments_info as $payment_method_and_gateway_txn_id) { |
|
381 | - $payment_methods[] = isset($payment_method_and_gateway_txn_id['name']) |
|
382 | - ? $payment_method_and_gateway_txn_id['name'] : esc_html__('Unknown', 'event_espresso'); |
|
383 | - $gateway_txn_ids_etc[] = isset($payment_method_and_gateway_txn_id['gateway_txn_id']) |
|
384 | - ? $payment_method_and_gateway_txn_id['gateway_txn_id'] : ''; |
|
385 | - $payment_times[] = isset($payment_method_and_gateway_txn_id['payment_time']) |
|
386 | - ? $payment_method_and_gateway_txn_id['payment_time'] : ''; |
|
387 | - } |
|
388 | - } |
|
389 | - $reg_csv_array[ (string) esc_html__('Payment Date(s)', 'event_espresso') ] = implode(',', $payment_times); |
|
390 | - $reg_csv_array[ (string) esc_html__('Payment Method(s)', 'event_espresso') ] = implode(",", $payment_methods); |
|
391 | - $reg_csv_array[ (string) esc_html__('Gateway Transaction ID(s)', 'event_espresso') ] = implode( |
|
392 | - ',', |
|
393 | - $gateway_txn_ids_etc |
|
394 | - ); |
|
395 | - // get whether or not the user has checked in |
|
396 | - $reg_csv_array[ (string) esc_html__("Check-Ins", "event_espresso") ] = $reg_model->count_related( |
|
397 | - $reg_row['Registration.REG_ID'], |
|
398 | - 'Checkin' |
|
399 | - ); |
|
400 | - // get ticket of registration and its price |
|
401 | - $ticket_model = EE_Registry::instance()->load_model('Ticket'); |
|
402 | - if ($reg_row['Ticket.TKT_ID']) { |
|
403 | - $ticket_name = EEH_Export::prepare_value_from_db_for_display( |
|
404 | - $ticket_model, |
|
405 | - 'TKT_name', |
|
406 | - $reg_row['Ticket.TKT_name'] |
|
407 | - ); |
|
408 | - $datetimes_strings = array(); |
|
409 | - foreach ( |
|
410 | - EEM_Datetime::instance()->get_all_wpdb_results( |
|
411 | - array( |
|
412 | - array('Ticket.TKT_ID' => $reg_row['Ticket.TKT_ID']), |
|
413 | - 'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
414 | - 'default_where_conditions' => 'none', |
|
415 | - ) |
|
416 | - ) as $datetime |
|
417 | - ) { |
|
418 | - $datetimes_strings[] = EEH_Export::prepare_value_from_db_for_display( |
|
419 | - EEM_Datetime::instance(), |
|
420 | - 'DTT_EVT_start', |
|
421 | - $datetime['Datetime.DTT_EVT_start'] |
|
422 | - ); |
|
423 | - } |
|
424 | - } else { |
|
425 | - $ticket_name = esc_html__('Unknown', 'event_espresso'); |
|
426 | - $datetimes_strings = array(esc_html__('Unknown', 'event_espresso')); |
|
427 | - } |
|
428 | - $reg_csv_array[ (string) $ticket_model->field_settings_for('TKT_name')->get_nicename() ] = $ticket_name; |
|
429 | - $reg_csv_array[ (string) esc_html__("Datetimes of Ticket", "event_espresso") ] = implode(", ", $datetimes_strings); |
|
430 | - // get datetime(s) of registration |
|
431 | - // add attendee columns |
|
432 | - foreach ($att_fields_to_include as $att_field_name) { |
|
433 | - $field_obj = EEM_Attendee::instance()->field_settings_for($att_field_name); |
|
434 | - if ($reg_row['Attendee_CPT.ID']) { |
|
435 | - if ($att_field_name == 'STA_ID') { |
|
436 | - $value = EEM_State::instance()->get_var( |
|
437 | - array(array('STA_ID' => $reg_row['Attendee_Meta.STA_ID'])), |
|
438 | - 'STA_name' |
|
439 | - ); |
|
440 | - } elseif ($att_field_name == 'CNT_ISO') { |
|
441 | - $value = EEM_Country::instance()->get_var( |
|
442 | - array(array('CNT_ISO' => $reg_row['Attendee_Meta.CNT_ISO'])), |
|
443 | - 'CNT_name' |
|
444 | - ); |
|
445 | - } else { |
|
446 | - $value = EEH_Export::prepare_value_from_db_for_display( |
|
447 | - EEM_Attendee::instance(), |
|
448 | - $att_field_name, |
|
449 | - $reg_row[ $field_obj->get_qualified_column() ] |
|
450 | - ); |
|
451 | - } |
|
452 | - } else { |
|
453 | - $value = ''; |
|
454 | - } |
|
455 | - $reg_csv_array[ EEH_Export::get_column_name_for_field($field_obj) ] = $value; |
|
456 | - } |
|
457 | - // make sure each registration has the same questions in the same order |
|
458 | - foreach ($question_labels as $question_label) { |
|
459 | - if (! isset($reg_csv_array[ $question_label ])) { |
|
460 | - $reg_csv_array[ $question_label ] = null; |
|
461 | - } |
|
462 | - } |
|
463 | - $answers = EEM_Answer::instance()->get_all_wpdb_results(array( |
|
464 | - array('REG_ID' => $reg_row['Registration.REG_ID']), |
|
465 | - 'force_join' => array('Question'), |
|
466 | - )); |
|
467 | - // now fill out the questions THEY answered |
|
468 | - foreach ($answers as $answer_row) { |
|
469 | - if ($answer_row['Question.QST_system']) { |
|
470 | - // it's an answer to a system question. That was already displayed as part of the attendee |
|
471 | - // fields, so don't write it out again thanks. |
|
472 | - continue; |
|
473 | - } |
|
474 | - if ($answer_row['Question.QST_ID']) { |
|
475 | - $question_label = EEH_Export::prepare_value_from_db_for_display( |
|
476 | - EEM_Question::instance(), |
|
477 | - 'QST_admin_label', |
|
478 | - $answer_row['Question.QST_admin_label'] |
|
479 | - ); |
|
480 | - } else { |
|
481 | - $question_label = sprintf(esc_html__('Question $s', 'event_espresso'), $answer_row['Answer.QST_ID']); |
|
482 | - } |
|
483 | - if ( |
|
484 | - isset($answer_row['Question.QST_type']) |
|
485 | - && $answer_row['Question.QST_type'] == EEM_Question::QST_type_state |
|
486 | - ) { |
|
487 | - $reg_csv_array[ $question_label ] = EEM_State::instance()->get_state_name_by_ID( |
|
488 | - $answer_row['Answer.ANS_value'] |
|
489 | - ); |
|
490 | - } else { |
|
491 | - // this isn't for html, so don't show html entities |
|
492 | - $reg_csv_array[ $question_label ] = html_entity_decode( |
|
493 | - EEH_Export::prepare_value_from_db_for_display( |
|
494 | - EEM_Answer::instance(), |
|
495 | - 'ANS_value', |
|
496 | - $answer_row['Answer.ANS_value'] |
|
497 | - ) |
|
498 | - ); |
|
499 | - } |
|
500 | - } |
|
235 | + /** |
|
236 | + * Gets the csv data for a batch of registrations |
|
237 | + * |
|
238 | + * @param int|null $event_id |
|
239 | + * @param int $offset |
|
240 | + * @param int $limit |
|
241 | + * @param array $question_labels the IDs for all the questions which were answered by someone in this selection |
|
242 | + * @param array $query_params for using where querying the model |
|
243 | + * @return array top-level keys are numeric, next-level keys are column headers |
|
244 | + * @throws \EE_Error |
|
245 | + */ |
|
246 | + public function get_csv_data_for($event_id, $offset, $limit, $question_labels, $query_params) |
|
247 | + { |
|
248 | + $reg_fields_to_include = array( |
|
249 | + 'TXN_ID', |
|
250 | + 'ATT_ID', |
|
251 | + 'REG_ID', |
|
252 | + 'REG_date', |
|
253 | + 'REG_code', |
|
254 | + 'REG_count', |
|
255 | + 'REG_final_price', |
|
256 | + ); |
|
257 | + $att_fields_to_include = array( |
|
258 | + 'ATT_fname', |
|
259 | + 'ATT_lname', |
|
260 | + 'ATT_email', |
|
261 | + 'ATT_address', |
|
262 | + 'ATT_address2', |
|
263 | + 'ATT_city', |
|
264 | + 'STA_ID', |
|
265 | + 'CNT_ISO', |
|
266 | + 'ATT_zip', |
|
267 | + 'ATT_phone', |
|
268 | + ); |
|
269 | + $registrations_csv_ready_array = array(); |
|
270 | + $reg_model = EE_Registry::instance()->load_model('Registration'); |
|
271 | + $query_params['limit'] = array($offset, $limit); |
|
272 | + $registration_rows = $reg_model->get_all_wpdb_results($query_params); |
|
273 | + $registration_ids = array(); |
|
274 | + foreach ($registration_rows as $reg_row) { |
|
275 | + $registration_ids[] = intval($reg_row['Registration.REG_ID']); |
|
276 | + } |
|
277 | + foreach ($registration_rows as $reg_row) { |
|
278 | + if (is_array($reg_row)) { |
|
279 | + $reg_csv_array = array(); |
|
280 | + if (! $event_id) { |
|
281 | + // get the event's name and Id |
|
282 | + $reg_csv_array[ (string) esc_html__('Event', 'event_espresso') ] = sprintf( |
|
283 | + /* translators: 1: event name, 2: event ID */ |
|
284 | + esc_html__('%1$s (%2$s)', 'event_espresso'), |
|
285 | + EEH_Export::prepare_value_from_db_for_display( |
|
286 | + EEM_Event::instance(), |
|
287 | + 'EVT_name', |
|
288 | + $reg_row['Event_CPT.post_title'] |
|
289 | + ), |
|
290 | + $reg_row['Event_CPT.ID'] |
|
291 | + ); |
|
292 | + } |
|
293 | + $is_primary_reg = $reg_row['Registration.REG_count'] == '1' ? true : false; |
|
294 | + /*@var $reg_row EE_Registration */ |
|
295 | + foreach ($reg_fields_to_include as $field_name) { |
|
296 | + $field = $reg_model->field_settings_for($field_name); |
|
297 | + if ($field_name == 'REG_final_price') { |
|
298 | + $value = EEH_Export::prepare_value_from_db_for_display( |
|
299 | + $reg_model, |
|
300 | + $field_name, |
|
301 | + $reg_row['Registration.REG_final_price'], |
|
302 | + 'localized_float' |
|
303 | + ); |
|
304 | + } elseif ($field_name == 'REG_count') { |
|
305 | + $value = sprintf( |
|
306 | + /* translators: 1: number of registration in group (REG_count), 2: registration group size (REG_group_size) */ |
|
307 | + esc_html__('%1$s of %2$s', 'event_espresso'), |
|
308 | + EEH_Export::prepare_value_from_db_for_display( |
|
309 | + $reg_model, |
|
310 | + 'REG_count', |
|
311 | + $reg_row['Registration.REG_count'] |
|
312 | + ), |
|
313 | + EEH_Export::prepare_value_from_db_for_display( |
|
314 | + $reg_model, |
|
315 | + 'REG_group_size', |
|
316 | + $reg_row['Registration.REG_group_size'] |
|
317 | + ) |
|
318 | + ); |
|
319 | + } elseif ($field_name == 'REG_date') { |
|
320 | + $value = EEH_Export::prepare_value_from_db_for_display( |
|
321 | + $reg_model, |
|
322 | + $field_name, |
|
323 | + $reg_row['Registration.REG_date'], |
|
324 | + 'no_html' |
|
325 | + ); |
|
326 | + } else { |
|
327 | + $value = EEH_Export::prepare_value_from_db_for_display( |
|
328 | + $reg_model, |
|
329 | + $field_name, |
|
330 | + $reg_row[ $field->get_qualified_column() ] |
|
331 | + ); |
|
332 | + } |
|
333 | + $reg_csv_array[ EEH_Export::get_column_name_for_field($field) ] = $value; |
|
334 | + if ($field_name == 'REG_final_price') { |
|
335 | + // add a column named Currency after the final price |
|
336 | + $reg_csv_array[ (string) esc_html__("Currency", "event_espresso") ] = \EE_Config::instance()->currency->code; |
|
337 | + } |
|
338 | + } |
|
339 | + // get pretty status |
|
340 | + $stati = EEM_Status::instance()->localized_status( |
|
341 | + array( |
|
342 | + $reg_row['Registration.STS_ID'] => esc_html__('unknown', 'event_espresso'), |
|
343 | + $reg_row['TransactionTable.STS_ID'] => esc_html__('unknown', 'event_espresso'), |
|
344 | + ), |
|
345 | + false, |
|
346 | + 'sentence' |
|
347 | + ); |
|
348 | + $reg_csv_array[ (string) esc_html__("Registration Status", 'event_espresso') ] = $stati[ $reg_row['Registration.STS_ID'] ]; |
|
349 | + // get pretty transaction status |
|
350 | + $reg_csv_array[ (string) esc_html__("Transaction Status", 'event_espresso') ] = $stati[ $reg_row['TransactionTable.STS_ID'] ]; |
|
351 | + $reg_csv_array[ (string) esc_html__('Transaction Amount Due', 'event_espresso') ] = $is_primary_reg |
|
352 | + ? EEH_Export::prepare_value_from_db_for_display( |
|
353 | + EEM_Transaction::instance(), |
|
354 | + 'TXN_total', |
|
355 | + $reg_row['TransactionTable.TXN_total'], |
|
356 | + 'localized_float' |
|
357 | + ) : '0.00'; |
|
358 | + $reg_csv_array[ (string) esc_html__('Amount Paid', 'event_espresso') ] = $is_primary_reg |
|
359 | + ? EEH_Export::prepare_value_from_db_for_display( |
|
360 | + EEM_Transaction::instance(), |
|
361 | + 'TXN_paid', |
|
362 | + $reg_row['TransactionTable.TXN_paid'], |
|
363 | + 'localized_float' |
|
364 | + ) : '0.00'; |
|
365 | + $payment_methods = array(); |
|
366 | + $gateway_txn_ids_etc = array(); |
|
367 | + $payment_times = array(); |
|
368 | + if ($is_primary_reg && $reg_row['TransactionTable.TXN_ID']) { |
|
369 | + $payments_info = EEM_Payment::instance()->get_all_wpdb_results( |
|
370 | + array( |
|
371 | + array( |
|
372 | + 'TXN_ID' => $reg_row['TransactionTable.TXN_ID'], |
|
373 | + 'STS_ID' => EEM_Payment::status_id_approved, |
|
374 | + ), |
|
375 | + 'force_join' => array('Payment_Method'), |
|
376 | + ), |
|
377 | + ARRAY_A, |
|
378 | + 'Payment_Method.PMD_admin_name as name, Payment.PAY_txn_id_chq_nmbr as gateway_txn_id, Payment.PAY_timestamp as payment_time' |
|
379 | + ); |
|
380 | + foreach ($payments_info as $payment_method_and_gateway_txn_id) { |
|
381 | + $payment_methods[] = isset($payment_method_and_gateway_txn_id['name']) |
|
382 | + ? $payment_method_and_gateway_txn_id['name'] : esc_html__('Unknown', 'event_espresso'); |
|
383 | + $gateway_txn_ids_etc[] = isset($payment_method_and_gateway_txn_id['gateway_txn_id']) |
|
384 | + ? $payment_method_and_gateway_txn_id['gateway_txn_id'] : ''; |
|
385 | + $payment_times[] = isset($payment_method_and_gateway_txn_id['payment_time']) |
|
386 | + ? $payment_method_and_gateway_txn_id['payment_time'] : ''; |
|
387 | + } |
|
388 | + } |
|
389 | + $reg_csv_array[ (string) esc_html__('Payment Date(s)', 'event_espresso') ] = implode(',', $payment_times); |
|
390 | + $reg_csv_array[ (string) esc_html__('Payment Method(s)', 'event_espresso') ] = implode(",", $payment_methods); |
|
391 | + $reg_csv_array[ (string) esc_html__('Gateway Transaction ID(s)', 'event_espresso') ] = implode( |
|
392 | + ',', |
|
393 | + $gateway_txn_ids_etc |
|
394 | + ); |
|
395 | + // get whether or not the user has checked in |
|
396 | + $reg_csv_array[ (string) esc_html__("Check-Ins", "event_espresso") ] = $reg_model->count_related( |
|
397 | + $reg_row['Registration.REG_ID'], |
|
398 | + 'Checkin' |
|
399 | + ); |
|
400 | + // get ticket of registration and its price |
|
401 | + $ticket_model = EE_Registry::instance()->load_model('Ticket'); |
|
402 | + if ($reg_row['Ticket.TKT_ID']) { |
|
403 | + $ticket_name = EEH_Export::prepare_value_from_db_for_display( |
|
404 | + $ticket_model, |
|
405 | + 'TKT_name', |
|
406 | + $reg_row['Ticket.TKT_name'] |
|
407 | + ); |
|
408 | + $datetimes_strings = array(); |
|
409 | + foreach ( |
|
410 | + EEM_Datetime::instance()->get_all_wpdb_results( |
|
411 | + array( |
|
412 | + array('Ticket.TKT_ID' => $reg_row['Ticket.TKT_ID']), |
|
413 | + 'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
414 | + 'default_where_conditions' => 'none', |
|
415 | + ) |
|
416 | + ) as $datetime |
|
417 | + ) { |
|
418 | + $datetimes_strings[] = EEH_Export::prepare_value_from_db_for_display( |
|
419 | + EEM_Datetime::instance(), |
|
420 | + 'DTT_EVT_start', |
|
421 | + $datetime['Datetime.DTT_EVT_start'] |
|
422 | + ); |
|
423 | + } |
|
424 | + } else { |
|
425 | + $ticket_name = esc_html__('Unknown', 'event_espresso'); |
|
426 | + $datetimes_strings = array(esc_html__('Unknown', 'event_espresso')); |
|
427 | + } |
|
428 | + $reg_csv_array[ (string) $ticket_model->field_settings_for('TKT_name')->get_nicename() ] = $ticket_name; |
|
429 | + $reg_csv_array[ (string) esc_html__("Datetimes of Ticket", "event_espresso") ] = implode(", ", $datetimes_strings); |
|
430 | + // get datetime(s) of registration |
|
431 | + // add attendee columns |
|
432 | + foreach ($att_fields_to_include as $att_field_name) { |
|
433 | + $field_obj = EEM_Attendee::instance()->field_settings_for($att_field_name); |
|
434 | + if ($reg_row['Attendee_CPT.ID']) { |
|
435 | + if ($att_field_name == 'STA_ID') { |
|
436 | + $value = EEM_State::instance()->get_var( |
|
437 | + array(array('STA_ID' => $reg_row['Attendee_Meta.STA_ID'])), |
|
438 | + 'STA_name' |
|
439 | + ); |
|
440 | + } elseif ($att_field_name == 'CNT_ISO') { |
|
441 | + $value = EEM_Country::instance()->get_var( |
|
442 | + array(array('CNT_ISO' => $reg_row['Attendee_Meta.CNT_ISO'])), |
|
443 | + 'CNT_name' |
|
444 | + ); |
|
445 | + } else { |
|
446 | + $value = EEH_Export::prepare_value_from_db_for_display( |
|
447 | + EEM_Attendee::instance(), |
|
448 | + $att_field_name, |
|
449 | + $reg_row[ $field_obj->get_qualified_column() ] |
|
450 | + ); |
|
451 | + } |
|
452 | + } else { |
|
453 | + $value = ''; |
|
454 | + } |
|
455 | + $reg_csv_array[ EEH_Export::get_column_name_for_field($field_obj) ] = $value; |
|
456 | + } |
|
457 | + // make sure each registration has the same questions in the same order |
|
458 | + foreach ($question_labels as $question_label) { |
|
459 | + if (! isset($reg_csv_array[ $question_label ])) { |
|
460 | + $reg_csv_array[ $question_label ] = null; |
|
461 | + } |
|
462 | + } |
|
463 | + $answers = EEM_Answer::instance()->get_all_wpdb_results(array( |
|
464 | + array('REG_ID' => $reg_row['Registration.REG_ID']), |
|
465 | + 'force_join' => array('Question'), |
|
466 | + )); |
|
467 | + // now fill out the questions THEY answered |
|
468 | + foreach ($answers as $answer_row) { |
|
469 | + if ($answer_row['Question.QST_system']) { |
|
470 | + // it's an answer to a system question. That was already displayed as part of the attendee |
|
471 | + // fields, so don't write it out again thanks. |
|
472 | + continue; |
|
473 | + } |
|
474 | + if ($answer_row['Question.QST_ID']) { |
|
475 | + $question_label = EEH_Export::prepare_value_from_db_for_display( |
|
476 | + EEM_Question::instance(), |
|
477 | + 'QST_admin_label', |
|
478 | + $answer_row['Question.QST_admin_label'] |
|
479 | + ); |
|
480 | + } else { |
|
481 | + $question_label = sprintf(esc_html__('Question $s', 'event_espresso'), $answer_row['Answer.QST_ID']); |
|
482 | + } |
|
483 | + if ( |
|
484 | + isset($answer_row['Question.QST_type']) |
|
485 | + && $answer_row['Question.QST_type'] == EEM_Question::QST_type_state |
|
486 | + ) { |
|
487 | + $reg_csv_array[ $question_label ] = EEM_State::instance()->get_state_name_by_ID( |
|
488 | + $answer_row['Answer.ANS_value'] |
|
489 | + ); |
|
490 | + } else { |
|
491 | + // this isn't for html, so don't show html entities |
|
492 | + $reg_csv_array[ $question_label ] = html_entity_decode( |
|
493 | + EEH_Export::prepare_value_from_db_for_display( |
|
494 | + EEM_Answer::instance(), |
|
495 | + 'ANS_value', |
|
496 | + $answer_row['Answer.ANS_value'] |
|
497 | + ) |
|
498 | + ); |
|
499 | + } |
|
500 | + } |
|
501 | 501 | |
502 | - /** |
|
503 | - * Filter to change the contents of each row of the registrations report CSV file. |
|
504 | - * This can be used to add or remote columns from the CSV file, or change their values. |
|
505 | - * Note when using: all rows in the CSV should have the same columns. |
|
506 | - * @param array $reg_csv_array keys are the column names, values are their cell values |
|
507 | - * @param array $reg_row one entry from EEM_Registration::get_all_wpdb_results() |
|
508 | - */ |
|
509 | - $registrations_csv_ready_array[] = apply_filters( |
|
510 | - 'FHEE__EventEspressoBatchRequest__JobHandlers__RegistrationsReport__reg_csv_array', |
|
511 | - $reg_csv_array, |
|
512 | - $reg_row |
|
513 | - ); |
|
514 | - } |
|
515 | - } |
|
516 | - // if we couldn't export anything, we want to at least show the column headers |
|
517 | - if (empty($registrations_csv_ready_array)) { |
|
518 | - $reg_csv_array = array(); |
|
519 | - $model_and_fields_to_include = array( |
|
520 | - 'Registration' => $reg_fields_to_include, |
|
521 | - 'Attendee' => $att_fields_to_include, |
|
522 | - ); |
|
523 | - foreach ($model_and_fields_to_include as $model_name => $field_list) { |
|
524 | - $model = EE_Registry::instance()->load_model($model_name); |
|
525 | - foreach ($field_list as $field_name) { |
|
526 | - $field = $model->field_settings_for($field_name); |
|
527 | - $reg_csv_array[ EEH_Export::get_column_name_for_field($field) ] = null; |
|
528 | - } |
|
529 | - } |
|
530 | - $registrations_csv_ready_array[] = $reg_csv_array; |
|
531 | - } |
|
532 | - return $registrations_csv_ready_array; |
|
533 | - } |
|
502 | + /** |
|
503 | + * Filter to change the contents of each row of the registrations report CSV file. |
|
504 | + * This can be used to add or remote columns from the CSV file, or change their values. |
|
505 | + * Note when using: all rows in the CSV should have the same columns. |
|
506 | + * @param array $reg_csv_array keys are the column names, values are their cell values |
|
507 | + * @param array $reg_row one entry from EEM_Registration::get_all_wpdb_results() |
|
508 | + */ |
|
509 | + $registrations_csv_ready_array[] = apply_filters( |
|
510 | + 'FHEE__EventEspressoBatchRequest__JobHandlers__RegistrationsReport__reg_csv_array', |
|
511 | + $reg_csv_array, |
|
512 | + $reg_row |
|
513 | + ); |
|
514 | + } |
|
515 | + } |
|
516 | + // if we couldn't export anything, we want to at least show the column headers |
|
517 | + if (empty($registrations_csv_ready_array)) { |
|
518 | + $reg_csv_array = array(); |
|
519 | + $model_and_fields_to_include = array( |
|
520 | + 'Registration' => $reg_fields_to_include, |
|
521 | + 'Attendee' => $att_fields_to_include, |
|
522 | + ); |
|
523 | + foreach ($model_and_fields_to_include as $model_name => $field_list) { |
|
524 | + $model = EE_Registry::instance()->load_model($model_name); |
|
525 | + foreach ($field_list as $field_name) { |
|
526 | + $field = $model->field_settings_for($field_name); |
|
527 | + $reg_csv_array[ EEH_Export::get_column_name_for_field($field) ] = null; |
|
528 | + } |
|
529 | + } |
|
530 | + $registrations_csv_ready_array[] = $reg_csv_array; |
|
531 | + } |
|
532 | + return $registrations_csv_ready_array; |
|
533 | + } |
|
534 | 534 | |
535 | 535 | |
536 | - /** |
|
537 | - * Counts total unit to process |
|
538 | - * |
|
539 | - * @deprecated since 4.9.19 |
|
540 | - * @param int|array $event_id |
|
541 | - * @return int |
|
542 | - */ |
|
543 | - public function count_units_to_process($event_id) |
|
544 | - { |
|
545 | - // use the legacy filter |
|
546 | - if ($event_id) { |
|
547 | - $query_params[0]['EVT_ID'] = $event_id; |
|
548 | - } else { |
|
549 | - $query_params['force_join'][] = 'Event'; |
|
550 | - } |
|
551 | - return \EEM_Registration::instance()->count($query_params); |
|
552 | - } |
|
536 | + /** |
|
537 | + * Counts total unit to process |
|
538 | + * |
|
539 | + * @deprecated since 4.9.19 |
|
540 | + * @param int|array $event_id |
|
541 | + * @return int |
|
542 | + */ |
|
543 | + public function count_units_to_process($event_id) |
|
544 | + { |
|
545 | + // use the legacy filter |
|
546 | + if ($event_id) { |
|
547 | + $query_params[0]['EVT_ID'] = $event_id; |
|
548 | + } else { |
|
549 | + $query_params['force_join'][] = 'Event'; |
|
550 | + } |
|
551 | + return \EEM_Registration::instance()->count($query_params); |
|
552 | + } |
|
553 | 553 | |
554 | 554 | |
555 | - /** |
|
556 | - * Performs any clean-up logic when we know the job is completed. |
|
557 | - * In this case, we delete the temporary file |
|
558 | - * |
|
559 | - * @param JobParameters $job_parameters |
|
560 | - * @return boolean |
|
561 | - */ |
|
562 | - public function cleanup_job(JobParameters $job_parameters) |
|
563 | - { |
|
564 | - $this->_file_helper->delete( |
|
565 | - \EEH_File::remove_filename_from_filepath($job_parameters->extra_datum('filepath')), |
|
566 | - true, |
|
567 | - 'd' |
|
568 | - ); |
|
569 | - return new JobStepResponse($job_parameters, esc_html__('Cleaned up temporary file', 'event_espresso')); |
|
570 | - } |
|
555 | + /** |
|
556 | + * Performs any clean-up logic when we know the job is completed. |
|
557 | + * In this case, we delete the temporary file |
|
558 | + * |
|
559 | + * @param JobParameters $job_parameters |
|
560 | + * @return boolean |
|
561 | + */ |
|
562 | + public function cleanup_job(JobParameters $job_parameters) |
|
563 | + { |
|
564 | + $this->_file_helper->delete( |
|
565 | + \EEH_File::remove_filename_from_filepath($job_parameters->extra_datum('filepath')), |
|
566 | + true, |
|
567 | + 'd' |
|
568 | + ); |
|
569 | + return new JobStepResponse($job_parameters, esc_html__('Cleaned up temporary file', 'event_espresso')); |
|
570 | + } |
|
571 | 571 | } |
@@ -17,135 +17,135 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class JobHandlerFile extends JobHandler |
19 | 19 | { |
20 | - // phpcs:disable Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase |
|
21 | - const temp_folder_name = 'batch_temp_folder'; |
|
20 | + // phpcs:disable Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase |
|
21 | + const temp_folder_name = 'batch_temp_folder'; |
|
22 | 22 | |
23 | - // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
|
24 | - /** |
|
25 | - * @var \EEHI_File |
|
26 | - */ |
|
27 | - protected $_file_helper = null; |
|
23 | + // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
|
24 | + /** |
|
25 | + * @var \EEHI_File |
|
26 | + */ |
|
27 | + protected $_file_helper = null; |
|
28 | 28 | |
29 | 29 | |
30 | - /** |
|
31 | - * JobHandlerFile constructor. |
|
32 | - * |
|
33 | - * @param \EEHI_File|null $file_helper |
|
34 | - */ |
|
35 | - public function __construct(\EEHI_File $file_helper = null) |
|
36 | - { |
|
37 | - if (! $file_helper) { |
|
38 | - $this->_file_helper = new EEH_File(); |
|
39 | - } |
|
40 | - } |
|
30 | + /** |
|
31 | + * JobHandlerFile constructor. |
|
32 | + * |
|
33 | + * @param \EEHI_File|null $file_helper |
|
34 | + */ |
|
35 | + public function __construct(\EEHI_File $file_helper = null) |
|
36 | + { |
|
37 | + if (! $file_helper) { |
|
38 | + $this->_file_helper = new EEH_File(); |
|
39 | + } |
|
40 | + } |
|
41 | 41 | |
42 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
43 | - /** |
|
44 | - * Creates a file |
|
45 | - * |
|
46 | - * @param string $job_id |
|
47 | - * @param string $filename |
|
48 | - * @param string $filetype |
|
49 | - * @param string $bom initial content to place in the file. |
|
50 | - * @return string |
|
51 | - * @throws \EventEspressoBatchRequest\Helpers\BatchRequestException |
|
52 | - */ |
|
53 | - public function create_file_from_job_with_name( |
|
54 | - $job_id, |
|
55 | - $filename, |
|
56 | - $filetype = 'application/ms-excel', |
|
57 | - $bom = "\xEF\xBB\xBF" |
|
58 | - ) { |
|
59 | - $filepath = ''; |
|
60 | - try { |
|
61 | - $base_folder = $this->get_base_folder(); |
|
62 | - $success = $this->_file_helper->ensure_folder_exists_and_is_writable( |
|
63 | - $base_folder . JobHandlerFile::temp_folder_name |
|
64 | - ); |
|
65 | - if ($success) { |
|
66 | - $success = $this->_file_helper->ensure_folder_exists_and_is_writable( |
|
67 | - $base_folder . JobHandlerFile::temp_folder_name . '/' . $job_id |
|
68 | - ); |
|
69 | - } |
|
70 | - if ($success) { |
|
71 | - $filepath = $base_folder . JobHandlerFile::temp_folder_name . '/' . $job_id . '/' . $filename; |
|
72 | - $success = $this->_file_helper->ensure_file_exists_and_is_writable($filepath); |
|
73 | - } |
|
74 | - // let's add the .htaccess file so safari will open the file properly |
|
75 | - if ($success) { |
|
76 | - $extension = EEH_File::get_file_extension($filepath); |
|
77 | - EEH_File::write_to_file( |
|
78 | - $base_folder . JobHandlerFile::temp_folder_name . '/' . $job_id . '/.htaccess', |
|
79 | - 'AddType ' . $filetype . ' ' . $extension, |
|
80 | - '.htaccess' |
|
81 | - ); |
|
82 | - } |
|
83 | - /** |
|
84 | - * Filters what initial content will be added to the file. |
|
85 | - * @param string $return_value. By default it's whatever was pased into |
|
86 | - * JobHandlerFile::create_file_from_job_with_name() |
|
87 | - * @param string $filename |
|
88 | - * @param string $filetype default 'application/ms-excel' |
|
89 | - * @param string $filepath |
|
90 | - */ |
|
91 | - EEH_File::write_to_file( |
|
92 | - $filepath, |
|
93 | - apply_filters( |
|
94 | - 'FHEE__EE_CSV__begin_sending_csv__start_writing', |
|
95 | - $bom, |
|
96 | - $filename, |
|
97 | - $filetype, |
|
98 | - $filepath |
|
99 | - ) |
|
100 | - ); |
|
101 | - // those methods normally fail with an exception, but if not, let's do it |
|
102 | - if (! $success) { |
|
103 | - throw new \EE_Error(esc_html__('Could not create temporary file, an unknown error occurred', 'event_espresso')); |
|
104 | - } |
|
105 | - } catch (\EE_Error $e) { |
|
106 | - throw new BatchRequestException( |
|
107 | - sprintf( |
|
108 | - // phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment |
|
109 | - esc_html__('Could not create temporary file for job %1$s, because: %2$s ', 'event_espresso'), |
|
110 | - $job_id, |
|
111 | - $e->getMessage() |
|
112 | - ), |
|
113 | - 500, |
|
114 | - $e |
|
115 | - ); |
|
116 | - } |
|
117 | - return $filepath; |
|
118 | - } |
|
42 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
43 | + /** |
|
44 | + * Creates a file |
|
45 | + * |
|
46 | + * @param string $job_id |
|
47 | + * @param string $filename |
|
48 | + * @param string $filetype |
|
49 | + * @param string $bom initial content to place in the file. |
|
50 | + * @return string |
|
51 | + * @throws \EventEspressoBatchRequest\Helpers\BatchRequestException |
|
52 | + */ |
|
53 | + public function create_file_from_job_with_name( |
|
54 | + $job_id, |
|
55 | + $filename, |
|
56 | + $filetype = 'application/ms-excel', |
|
57 | + $bom = "\xEF\xBB\xBF" |
|
58 | + ) { |
|
59 | + $filepath = ''; |
|
60 | + try { |
|
61 | + $base_folder = $this->get_base_folder(); |
|
62 | + $success = $this->_file_helper->ensure_folder_exists_and_is_writable( |
|
63 | + $base_folder . JobHandlerFile::temp_folder_name |
|
64 | + ); |
|
65 | + if ($success) { |
|
66 | + $success = $this->_file_helper->ensure_folder_exists_and_is_writable( |
|
67 | + $base_folder . JobHandlerFile::temp_folder_name . '/' . $job_id |
|
68 | + ); |
|
69 | + } |
|
70 | + if ($success) { |
|
71 | + $filepath = $base_folder . JobHandlerFile::temp_folder_name . '/' . $job_id . '/' . $filename; |
|
72 | + $success = $this->_file_helper->ensure_file_exists_and_is_writable($filepath); |
|
73 | + } |
|
74 | + // let's add the .htaccess file so safari will open the file properly |
|
75 | + if ($success) { |
|
76 | + $extension = EEH_File::get_file_extension($filepath); |
|
77 | + EEH_File::write_to_file( |
|
78 | + $base_folder . JobHandlerFile::temp_folder_name . '/' . $job_id . '/.htaccess', |
|
79 | + 'AddType ' . $filetype . ' ' . $extension, |
|
80 | + '.htaccess' |
|
81 | + ); |
|
82 | + } |
|
83 | + /** |
|
84 | + * Filters what initial content will be added to the file. |
|
85 | + * @param string $return_value. By default it's whatever was pased into |
|
86 | + * JobHandlerFile::create_file_from_job_with_name() |
|
87 | + * @param string $filename |
|
88 | + * @param string $filetype default 'application/ms-excel' |
|
89 | + * @param string $filepath |
|
90 | + */ |
|
91 | + EEH_File::write_to_file( |
|
92 | + $filepath, |
|
93 | + apply_filters( |
|
94 | + 'FHEE__EE_CSV__begin_sending_csv__start_writing', |
|
95 | + $bom, |
|
96 | + $filename, |
|
97 | + $filetype, |
|
98 | + $filepath |
|
99 | + ) |
|
100 | + ); |
|
101 | + // those methods normally fail with an exception, but if not, let's do it |
|
102 | + if (! $success) { |
|
103 | + throw new \EE_Error(esc_html__('Could not create temporary file, an unknown error occurred', 'event_espresso')); |
|
104 | + } |
|
105 | + } catch (\EE_Error $e) { |
|
106 | + throw new BatchRequestException( |
|
107 | + sprintf( |
|
108 | + // phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment |
|
109 | + esc_html__('Could not create temporary file for job %1$s, because: %2$s ', 'event_espresso'), |
|
110 | + $job_id, |
|
111 | + $e->getMessage() |
|
112 | + ), |
|
113 | + 500, |
|
114 | + $e |
|
115 | + ); |
|
116 | + } |
|
117 | + return $filepath; |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * Gets the URL to download the file |
|
122 | - * |
|
123 | - * @param string $filepath |
|
124 | - * @return string url to file |
|
125 | - */ |
|
126 | - public function get_url_to_file($filepath) |
|
127 | - { |
|
128 | - return str_replace($this->get_base_folder(), $this->get_base_url(), $filepath); |
|
129 | - } |
|
120 | + /** |
|
121 | + * Gets the URL to download the file |
|
122 | + * |
|
123 | + * @param string $filepath |
|
124 | + * @return string url to file |
|
125 | + */ |
|
126 | + public function get_url_to_file($filepath) |
|
127 | + { |
|
128 | + return str_replace($this->get_base_folder(), $this->get_base_url(), $filepath); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Gets the folder which will contain the "batch_temp_folder" |
|
133 | - * |
|
134 | - * @return string |
|
135 | - */ |
|
136 | - public function get_base_folder() |
|
137 | - { |
|
138 | - return apply_filters( |
|
139 | - 'FHEE__EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandlerFile__get_base_folder', |
|
140 | - EVENT_ESPRESSO_UPLOAD_DIR |
|
141 | - ); |
|
142 | - } |
|
131 | + /** |
|
132 | + * Gets the folder which will contain the "batch_temp_folder" |
|
133 | + * |
|
134 | + * @return string |
|
135 | + */ |
|
136 | + public function get_base_folder() |
|
137 | + { |
|
138 | + return apply_filters( |
|
139 | + 'FHEE__EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandlerFile__get_base_folder', |
|
140 | + EVENT_ESPRESSO_UPLOAD_DIR |
|
141 | + ); |
|
142 | + } |
|
143 | 143 | |
144 | - public function get_base_url() |
|
145 | - { |
|
146 | - return apply_filters( |
|
147 | - 'FHEE__EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandlerFile__get_base_url', |
|
148 | - EVENT_ESPRESSO_UPLOAD_URL |
|
149 | - ); |
|
150 | - } |
|
144 | + public function get_base_url() |
|
145 | + { |
|
146 | + return apply_filters( |
|
147 | + 'FHEE__EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandlerFile__get_base_url', |
|
148 | + EVENT_ESPRESSO_UPLOAD_URL |
|
149 | + ); |
|
150 | + } |
|
151 | 151 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public function __construct(\EEHI_File $file_helper = null) |
36 | 36 | { |
37 | - if (! $file_helper) { |
|
37 | + if ( ! $file_helper) { |
|
38 | 38 | $this->_file_helper = new EEH_File(); |
39 | 39 | } |
40 | 40 | } |
@@ -60,23 +60,23 @@ discard block |
||
60 | 60 | try { |
61 | 61 | $base_folder = $this->get_base_folder(); |
62 | 62 | $success = $this->_file_helper->ensure_folder_exists_and_is_writable( |
63 | - $base_folder . JobHandlerFile::temp_folder_name |
|
63 | + $base_folder.JobHandlerFile::temp_folder_name |
|
64 | 64 | ); |
65 | 65 | if ($success) { |
66 | 66 | $success = $this->_file_helper->ensure_folder_exists_and_is_writable( |
67 | - $base_folder . JobHandlerFile::temp_folder_name . '/' . $job_id |
|
67 | + $base_folder.JobHandlerFile::temp_folder_name.'/'.$job_id |
|
68 | 68 | ); |
69 | 69 | } |
70 | 70 | if ($success) { |
71 | - $filepath = $base_folder . JobHandlerFile::temp_folder_name . '/' . $job_id . '/' . $filename; |
|
71 | + $filepath = $base_folder.JobHandlerFile::temp_folder_name.'/'.$job_id.'/'.$filename; |
|
72 | 72 | $success = $this->_file_helper->ensure_file_exists_and_is_writable($filepath); |
73 | 73 | } |
74 | 74 | // let's add the .htaccess file so safari will open the file properly |
75 | 75 | if ($success) { |
76 | 76 | $extension = EEH_File::get_file_extension($filepath); |
77 | 77 | EEH_File::write_to_file( |
78 | - $base_folder . JobHandlerFile::temp_folder_name . '/' . $job_id . '/.htaccess', |
|
79 | - 'AddType ' . $filetype . ' ' . $extension, |
|
78 | + $base_folder.JobHandlerFile::temp_folder_name.'/'.$job_id.'/.htaccess', |
|
79 | + 'AddType '.$filetype.' '.$extension, |
|
80 | 80 | '.htaccess' |
81 | 81 | ); |
82 | 82 | } |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | ) |
100 | 100 | ); |
101 | 101 | // those methods normally fail with an exception, but if not, let's do it |
102 | - if (! $success) { |
|
102 | + if ( ! $success) { |
|
103 | 103 | throw new \EE_Error(esc_html__('Could not create temporary file, an unknown error occurred', 'event_espresso')); |
104 | 104 | } |
105 | 105 | } catch (\EE_Error $e) { |
@@ -25,215 +25,215 @@ |
||
25 | 25 | */ |
26 | 26 | class BatchRequestProcessor |
27 | 27 | { |
28 | - // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
|
29 | - // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
30 | - /** |
|
31 | - * Current job's ID (if assigned) |
|
32 | - * |
|
33 | - * @var string|null |
|
34 | - */ |
|
35 | - protected $_job_id; |
|
28 | + // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
|
29 | + // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
30 | + /** |
|
31 | + * Current job's ID (if assigned) |
|
32 | + * |
|
33 | + * @var string|null |
|
34 | + */ |
|
35 | + protected $_job_id; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Current job's parameters |
|
39 | - * |
|
40 | - * @var JobParameters|null |
|
41 | - */ |
|
42 | - protected $_job_parameters; |
|
43 | - /** |
|
44 | - * @var LoaderInterface |
|
45 | - */ |
|
46 | - private $loader; |
|
37 | + /** |
|
38 | + * Current job's parameters |
|
39 | + * |
|
40 | + * @var JobParameters|null |
|
41 | + */ |
|
42 | + protected $_job_parameters; |
|
43 | + /** |
|
44 | + * @var LoaderInterface |
|
45 | + */ |
|
46 | + private $loader; |
|
47 | 47 | |
48 | - /** |
|
49 | - * BatchRequestProcessor constructor. |
|
50 | - * @param LoaderInterface $loader |
|
51 | - */ |
|
52 | - public function __construct(LoaderInterface $loader) |
|
53 | - { |
|
54 | - $this->loader = $loader; |
|
55 | - } |
|
56 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
57 | - /** |
|
58 | - * Creates a job for the specified batch handler class (which should be autoloaded) |
|
59 | - * and the specified request data |
|
60 | - * |
|
61 | - * @param string $batch_job_handler_class of an auto-loaded class implementing JobHandlerInterface |
|
62 | - * @param array $request_data to be used by the batch job handler |
|
63 | - * @return JobStepResponse |
|
64 | - */ |
|
65 | - public function create_job($batch_job_handler_class, $request_data) |
|
66 | - { |
|
67 | - try { |
|
68 | - $this->_job_id = wp_generate_password(15, false); |
|
69 | - $obj = $this->instantiate_batch_job_handler_from_classname($batch_job_handler_class); |
|
70 | - $this->_job_parameters = new JobParameters($this->_job_id, $batch_job_handler_class, $request_data); |
|
71 | - $response = $obj->create_job($this->_job_parameters); |
|
72 | - if (! $response instanceof JobStepResponse) { |
|
73 | - throw new BatchRequestException( |
|
74 | - sprintf( |
|
75 | - esc_html__( |
|
76 | - 'The class implementing JobHandlerInterface did not return a JobStepResponse when create_job was called with %1$s. It needs to return one or throw an Exception', |
|
77 | - 'event_espresso' |
|
78 | - ), |
|
79 | - wp_json_encode($request_data) |
|
80 | - ) |
|
81 | - ); |
|
82 | - } |
|
83 | - $success = $this->_job_parameters->save(true); |
|
84 | - if (! $success) { |
|
85 | - throw new BatchRequestException( |
|
86 | - sprintf( |
|
87 | - esc_html__( |
|
88 | - 'Could not save job %1$s to the Wordpress Options table. These were the arguments used: %2$s', |
|
89 | - 'event_espresso' |
|
90 | - ), |
|
91 | - $this->_job_id, |
|
92 | - wp_json_encode($request_data) |
|
93 | - ) |
|
94 | - ); |
|
95 | - } |
|
96 | - } catch (\Exception $e) { |
|
97 | - $response = $this->_get_error_response($e, 'create_job'); |
|
98 | - } |
|
99 | - return $response; |
|
100 | - } |
|
48 | + /** |
|
49 | + * BatchRequestProcessor constructor. |
|
50 | + * @param LoaderInterface $loader |
|
51 | + */ |
|
52 | + public function __construct(LoaderInterface $loader) |
|
53 | + { |
|
54 | + $this->loader = $loader; |
|
55 | + } |
|
56 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
57 | + /** |
|
58 | + * Creates a job for the specified batch handler class (which should be autoloaded) |
|
59 | + * and the specified request data |
|
60 | + * |
|
61 | + * @param string $batch_job_handler_class of an auto-loaded class implementing JobHandlerInterface |
|
62 | + * @param array $request_data to be used by the batch job handler |
|
63 | + * @return JobStepResponse |
|
64 | + */ |
|
65 | + public function create_job($batch_job_handler_class, $request_data) |
|
66 | + { |
|
67 | + try { |
|
68 | + $this->_job_id = wp_generate_password(15, false); |
|
69 | + $obj = $this->instantiate_batch_job_handler_from_classname($batch_job_handler_class); |
|
70 | + $this->_job_parameters = new JobParameters($this->_job_id, $batch_job_handler_class, $request_data); |
|
71 | + $response = $obj->create_job($this->_job_parameters); |
|
72 | + if (! $response instanceof JobStepResponse) { |
|
73 | + throw new BatchRequestException( |
|
74 | + sprintf( |
|
75 | + esc_html__( |
|
76 | + 'The class implementing JobHandlerInterface did not return a JobStepResponse when create_job was called with %1$s. It needs to return one or throw an Exception', |
|
77 | + 'event_espresso' |
|
78 | + ), |
|
79 | + wp_json_encode($request_data) |
|
80 | + ) |
|
81 | + ); |
|
82 | + } |
|
83 | + $success = $this->_job_parameters->save(true); |
|
84 | + if (! $success) { |
|
85 | + throw new BatchRequestException( |
|
86 | + sprintf( |
|
87 | + esc_html__( |
|
88 | + 'Could not save job %1$s to the Wordpress Options table. These were the arguments used: %2$s', |
|
89 | + 'event_espresso' |
|
90 | + ), |
|
91 | + $this->_job_id, |
|
92 | + wp_json_encode($request_data) |
|
93 | + ) |
|
94 | + ); |
|
95 | + } |
|
96 | + } catch (\Exception $e) { |
|
97 | + $response = $this->_get_error_response($e, 'create_job'); |
|
98 | + } |
|
99 | + return $response; |
|
100 | + } |
|
101 | 101 | |
102 | 102 | |
103 | - /** |
|
104 | - * Retrieves the job's arguments |
|
105 | - * |
|
106 | - * @param string $job_id |
|
107 | - * @param int $batch_size |
|
108 | - * @return JobStepResponse |
|
109 | - */ |
|
110 | - public function continue_job($job_id, $batch_size = 50) |
|
111 | - { |
|
112 | - try { |
|
113 | - $this->_job_id = $job_id; |
|
114 | - $batch_size = defined('EE_BATCHRUNNER_BATCH_SIZE') ? EE_BATCHRUNNER_BATCH_SIZE : $batch_size; |
|
115 | - // get the corresponding WordPress option for the job |
|
116 | - $this->_job_parameters = JobParameters::load($this->_job_id); |
|
117 | - $handler_obj = $this->instantiate_batch_job_handler_from_classname($this->_job_parameters->classname()); |
|
118 | - // continue it |
|
119 | - $response = $handler_obj->continue_job($this->_job_parameters, $batch_size); |
|
120 | - if (! $response instanceof JobStepResponse) { |
|
121 | - throw new BatchRequestException( |
|
122 | - sprintf( |
|
123 | - esc_html__( |
|
124 | - 'The class implementing JobHandlerInterface did not return a JobStepResponse when continue_job was called with job %1$s. It needs to return one or throw an Exception', |
|
125 | - 'event_espresso' |
|
126 | - ), |
|
127 | - $this->_job_id |
|
128 | - ) |
|
129 | - ); |
|
130 | - } |
|
131 | - $this->_job_parameters->save(); |
|
132 | - } catch (\Exception $e) { |
|
133 | - $response = $this->_get_error_response($e, 'continue_job'); |
|
134 | - } |
|
135 | - return $response; |
|
136 | - } |
|
103 | + /** |
|
104 | + * Retrieves the job's arguments |
|
105 | + * |
|
106 | + * @param string $job_id |
|
107 | + * @param int $batch_size |
|
108 | + * @return JobStepResponse |
|
109 | + */ |
|
110 | + public function continue_job($job_id, $batch_size = 50) |
|
111 | + { |
|
112 | + try { |
|
113 | + $this->_job_id = $job_id; |
|
114 | + $batch_size = defined('EE_BATCHRUNNER_BATCH_SIZE') ? EE_BATCHRUNNER_BATCH_SIZE : $batch_size; |
|
115 | + // get the corresponding WordPress option for the job |
|
116 | + $this->_job_parameters = JobParameters::load($this->_job_id); |
|
117 | + $handler_obj = $this->instantiate_batch_job_handler_from_classname($this->_job_parameters->classname()); |
|
118 | + // continue it |
|
119 | + $response = $handler_obj->continue_job($this->_job_parameters, $batch_size); |
|
120 | + if (! $response instanceof JobStepResponse) { |
|
121 | + throw new BatchRequestException( |
|
122 | + sprintf( |
|
123 | + esc_html__( |
|
124 | + 'The class implementing JobHandlerInterface did not return a JobStepResponse when continue_job was called with job %1$s. It needs to return one or throw an Exception', |
|
125 | + 'event_espresso' |
|
126 | + ), |
|
127 | + $this->_job_id |
|
128 | + ) |
|
129 | + ); |
|
130 | + } |
|
131 | + $this->_job_parameters->save(); |
|
132 | + } catch (\Exception $e) { |
|
133 | + $response = $this->_get_error_response($e, 'continue_job'); |
|
134 | + } |
|
135 | + return $response; |
|
136 | + } |
|
137 | 137 | |
138 | 138 | |
139 | - /** |
|
140 | - * Instantiates an object of type $classname, which implements |
|
141 | - * JobHandlerInterface |
|
142 | - * |
|
143 | - * @param string $classname |
|
144 | - * @return JobHandlerInterface |
|
145 | - * @throws BatchRequestException |
|
146 | - */ |
|
147 | - public function instantiate_batch_job_handler_from_classname($classname) |
|
148 | - { |
|
149 | - if (! class_exists($classname)) { |
|
150 | - throw new BatchRequestException( |
|
151 | - sprintf( |
|
152 | - esc_html__( |
|
153 | - 'The class %1$s does not exist, and so could not be used for running a job. It should implement JobHandlerInterface.', |
|
154 | - 'event_espresso' |
|
155 | - ), |
|
156 | - $classname |
|
157 | - ) |
|
158 | - ); |
|
159 | - } |
|
160 | - $obj = $this->loader->getNew($classname); |
|
161 | - if (! $obj instanceof JobHandlerInterface) { |
|
162 | - throw new BatchRequestException( |
|
163 | - sprintf( |
|
164 | - esc_html__( |
|
165 | - 'The class %1$s does not implement JobHandlerInterface and so could not be used for running a job', |
|
166 | - 'event_espresso' |
|
167 | - ), |
|
168 | - $classname |
|
169 | - ) |
|
170 | - ); |
|
171 | - } |
|
172 | - return $obj; |
|
173 | - } |
|
139 | + /** |
|
140 | + * Instantiates an object of type $classname, which implements |
|
141 | + * JobHandlerInterface |
|
142 | + * |
|
143 | + * @param string $classname |
|
144 | + * @return JobHandlerInterface |
|
145 | + * @throws BatchRequestException |
|
146 | + */ |
|
147 | + public function instantiate_batch_job_handler_from_classname($classname) |
|
148 | + { |
|
149 | + if (! class_exists($classname)) { |
|
150 | + throw new BatchRequestException( |
|
151 | + sprintf( |
|
152 | + esc_html__( |
|
153 | + 'The class %1$s does not exist, and so could not be used for running a job. It should implement JobHandlerInterface.', |
|
154 | + 'event_espresso' |
|
155 | + ), |
|
156 | + $classname |
|
157 | + ) |
|
158 | + ); |
|
159 | + } |
|
160 | + $obj = $this->loader->getNew($classname); |
|
161 | + if (! $obj instanceof JobHandlerInterface) { |
|
162 | + throw new BatchRequestException( |
|
163 | + sprintf( |
|
164 | + esc_html__( |
|
165 | + 'The class %1$s does not implement JobHandlerInterface and so could not be used for running a job', |
|
166 | + 'event_espresso' |
|
167 | + ), |
|
168 | + $classname |
|
169 | + ) |
|
170 | + ); |
|
171 | + } |
|
172 | + return $obj; |
|
173 | + } |
|
174 | 174 | |
175 | 175 | |
176 | - /** |
|
177 | - * Forces a job to be cleaned up |
|
178 | - * |
|
179 | - * @param string $job_id |
|
180 | - * @return JobStepResponse |
|
181 | - * @throws BatchRequestException |
|
182 | - */ |
|
183 | - public function cleanup_job($job_id) |
|
184 | - { |
|
185 | - try { |
|
186 | - $this->_job_id = $job_id; |
|
187 | - $job_parameters = JobParameters::load($this->_job_id); |
|
188 | - $handler_obj = $this->instantiate_batch_job_handler_from_classname($job_parameters->classname()); |
|
189 | - // continue it |
|
190 | - $response = $handler_obj->cleanup_job($job_parameters); |
|
191 | - if (! $response instanceof JobStepResponse) { |
|
192 | - throw new BatchRequestException( |
|
193 | - sprintf( |
|
194 | - esc_html__( |
|
195 | - 'The class implementing JobHandlerInterface did not return a JobStepResponse when cleanup_job was called with job %1$s. It needs to return one or throw an Exception', |
|
196 | - 'event_espresso' |
|
197 | - ), |
|
198 | - $this->_job_id |
|
199 | - ) |
|
200 | - ); |
|
201 | - } |
|
202 | - $job_parameters->set_status(JobParameters::status_cleaned_up); |
|
203 | - $job_parameters->delete(); |
|
204 | - return $response; |
|
205 | - } catch (\Exception $e) { |
|
206 | - $response = $this->_get_error_response($e, 'cleanup_job'); |
|
207 | - } |
|
208 | - return $response; |
|
209 | - } |
|
176 | + /** |
|
177 | + * Forces a job to be cleaned up |
|
178 | + * |
|
179 | + * @param string $job_id |
|
180 | + * @return JobStepResponse |
|
181 | + * @throws BatchRequestException |
|
182 | + */ |
|
183 | + public function cleanup_job($job_id) |
|
184 | + { |
|
185 | + try { |
|
186 | + $this->_job_id = $job_id; |
|
187 | + $job_parameters = JobParameters::load($this->_job_id); |
|
188 | + $handler_obj = $this->instantiate_batch_job_handler_from_classname($job_parameters->classname()); |
|
189 | + // continue it |
|
190 | + $response = $handler_obj->cleanup_job($job_parameters); |
|
191 | + if (! $response instanceof JobStepResponse) { |
|
192 | + throw new BatchRequestException( |
|
193 | + sprintf( |
|
194 | + esc_html__( |
|
195 | + 'The class implementing JobHandlerInterface did not return a JobStepResponse when cleanup_job was called with job %1$s. It needs to return one or throw an Exception', |
|
196 | + 'event_espresso' |
|
197 | + ), |
|
198 | + $this->_job_id |
|
199 | + ) |
|
200 | + ); |
|
201 | + } |
|
202 | + $job_parameters->set_status(JobParameters::status_cleaned_up); |
|
203 | + $job_parameters->delete(); |
|
204 | + return $response; |
|
205 | + } catch (\Exception $e) { |
|
206 | + $response = $this->_get_error_response($e, 'cleanup_job'); |
|
207 | + } |
|
208 | + return $response; |
|
209 | + } |
|
210 | 210 | |
211 | 211 | |
212 | - /** |
|
213 | - * Creates a valid JobStepResponse object from an exception and method name. |
|
214 | - * |
|
215 | - * @param \Exception $exception |
|
216 | - * @param string $method_name |
|
217 | - * @return JobStepResponse |
|
218 | - */ |
|
219 | - protected function _get_error_response(\Exception $exception, $method_name) |
|
220 | - { |
|
221 | - if (! $this->_job_parameters instanceof JobParameters) { |
|
222 | - $this->_job_parameters = new JobParameters($this->_job_id, esc_html__('__Unknown__', 'event_espresso'), array()); |
|
223 | - } |
|
224 | - $this->_job_parameters->set_status(JobParameters::status_error); |
|
225 | - return new JobStepResponse( |
|
226 | - $this->_job_parameters, |
|
227 | - sprintf( |
|
228 | - esc_html__( |
|
229 | - 'An exception of type %1$s occurred while running %2$s. Its message was %3$s and had trace %4$s', |
|
230 | - 'event_espresso' |
|
231 | - ), |
|
232 | - get_class($exception), |
|
233 | - 'BatchRunner::' . $method_name . '()', |
|
234 | - $exception->getMessage(), |
|
235 | - $exception->getTraceAsString() |
|
236 | - ) |
|
237 | - ); |
|
238 | - } |
|
212 | + /** |
|
213 | + * Creates a valid JobStepResponse object from an exception and method name. |
|
214 | + * |
|
215 | + * @param \Exception $exception |
|
216 | + * @param string $method_name |
|
217 | + * @return JobStepResponse |
|
218 | + */ |
|
219 | + protected function _get_error_response(\Exception $exception, $method_name) |
|
220 | + { |
|
221 | + if (! $this->_job_parameters instanceof JobParameters) { |
|
222 | + $this->_job_parameters = new JobParameters($this->_job_id, esc_html__('__Unknown__', 'event_espresso'), array()); |
|
223 | + } |
|
224 | + $this->_job_parameters->set_status(JobParameters::status_error); |
|
225 | + return new JobStepResponse( |
|
226 | + $this->_job_parameters, |
|
227 | + sprintf( |
|
228 | + esc_html__( |
|
229 | + 'An exception of type %1$s occurred while running %2$s. Its message was %3$s and had trace %4$s', |
|
230 | + 'event_espresso' |
|
231 | + ), |
|
232 | + get_class($exception), |
|
233 | + 'BatchRunner::' . $method_name . '()', |
|
234 | + $exception->getMessage(), |
|
235 | + $exception->getTraceAsString() |
|
236 | + ) |
|
237 | + ); |
|
238 | + } |
|
239 | 239 | } |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | $obj = $this->instantiate_batch_job_handler_from_classname($batch_job_handler_class); |
70 | 70 | $this->_job_parameters = new JobParameters($this->_job_id, $batch_job_handler_class, $request_data); |
71 | 71 | $response = $obj->create_job($this->_job_parameters); |
72 | - if (! $response instanceof JobStepResponse) { |
|
72 | + if ( ! $response instanceof JobStepResponse) { |
|
73 | 73 | throw new BatchRequestException( |
74 | 74 | sprintf( |
75 | 75 | esc_html__( |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | ); |
82 | 82 | } |
83 | 83 | $success = $this->_job_parameters->save(true); |
84 | - if (! $success) { |
|
84 | + if ( ! $success) { |
|
85 | 85 | throw new BatchRequestException( |
86 | 86 | sprintf( |
87 | 87 | esc_html__( |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | $handler_obj = $this->instantiate_batch_job_handler_from_classname($this->_job_parameters->classname()); |
118 | 118 | // continue it |
119 | 119 | $response = $handler_obj->continue_job($this->_job_parameters, $batch_size); |
120 | - if (! $response instanceof JobStepResponse) { |
|
120 | + if ( ! $response instanceof JobStepResponse) { |
|
121 | 121 | throw new BatchRequestException( |
122 | 122 | sprintf( |
123 | 123 | esc_html__( |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | */ |
147 | 147 | public function instantiate_batch_job_handler_from_classname($classname) |
148 | 148 | { |
149 | - if (! class_exists($classname)) { |
|
149 | + if ( ! class_exists($classname)) { |
|
150 | 150 | throw new BatchRequestException( |
151 | 151 | sprintf( |
152 | 152 | esc_html__( |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | ); |
159 | 159 | } |
160 | 160 | $obj = $this->loader->getNew($classname); |
161 | - if (! $obj instanceof JobHandlerInterface) { |
|
161 | + if ( ! $obj instanceof JobHandlerInterface) { |
|
162 | 162 | throw new BatchRequestException( |
163 | 163 | sprintf( |
164 | 164 | esc_html__( |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | $handler_obj = $this->instantiate_batch_job_handler_from_classname($job_parameters->classname()); |
189 | 189 | // continue it |
190 | 190 | $response = $handler_obj->cleanup_job($job_parameters); |
191 | - if (! $response instanceof JobStepResponse) { |
|
191 | + if ( ! $response instanceof JobStepResponse) { |
|
192 | 192 | throw new BatchRequestException( |
193 | 193 | sprintf( |
194 | 194 | esc_html__( |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | */ |
219 | 219 | protected function _get_error_response(\Exception $exception, $method_name) |
220 | 220 | { |
221 | - if (! $this->_job_parameters instanceof JobParameters) { |
|
221 | + if ( ! $this->_job_parameters instanceof JobParameters) { |
|
222 | 222 | $this->_job_parameters = new JobParameters($this->_job_id, esc_html__('__Unknown__', 'event_espresso'), array()); |
223 | 223 | } |
224 | 224 | $this->_job_parameters->set_status(JobParameters::status_error); |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | 'event_espresso' |
231 | 231 | ), |
232 | 232 | get_class($exception), |
233 | - 'BatchRunner::' . $method_name . '()', |
|
233 | + 'BatchRunner::'.$method_name.'()', |
|
234 | 234 | $exception->getMessage(), |
235 | 235 | $exception->getTraceAsString() |
236 | 236 | ) |