@@ -171,9 +171,9 @@ discard block |
||
171 | 171 | 'order_by' => $this->_get_priority_orderby(), |
172 | 172 | 'limit' => $this->_batch_count, |
173 | 173 | ); |
174 | - $messages = EEM_Message::instance()->get_all($query_args); |
|
174 | + $messages = EEM_Message::instance()->get_all($query_args); |
|
175 | 175 | |
176 | - if (! $messages) { |
|
176 | + if ( ! $messages) { |
|
177 | 177 | return false; // nothing to generate |
178 | 178 | } |
179 | 179 | |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | |
228 | 228 | |
229 | 229 | // any to send? |
230 | - if (! $messages_to_send) { |
|
230 | + if ( ! $messages_to_send) { |
|
231 | 231 | $this->unlock_queue(EE_Messages_Queue::action_sending); |
232 | 232 | return false; |
233 | 233 | } |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | */ |
284 | 284 | protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
285 | 285 | { |
286 | - return '_ee_lock_' . $type; |
|
286 | + return '_ee_lock_'.$type; |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | */ |
356 | 356 | public function is_locked($type = EE_Messages_Queue::action_generating) |
357 | 357 | { |
358 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
358 | + if ( ! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
359 | 359 | return true; |
360 | 360 | } |
361 | 361 | $lock = (int) get_option($this->_get_lock_key($type), 0); |
@@ -505,7 +505,7 @@ discard block |
||
505 | 505 | /** @type EE_Message $message */ |
506 | 506 | $message = $this->_message_repository->current(); |
507 | 507 | // only process things that are queued for sending |
508 | - if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
508 | + if ( ! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
509 | 509 | $this->_message_repository->next(); |
510 | 510 | continue; |
511 | 511 | } |
@@ -515,13 +515,13 @@ discard block |
||
515 | 515 | continue; |
516 | 516 | } |
517 | 517 | // error checking |
518 | - if (! $message->valid_messenger()) { |
|
518 | + if ( ! $message->valid_messenger()) { |
|
519 | 519 | $error_messages[] = sprintf( |
520 | 520 | __('The %s messenger is not active at time of sending.', 'event_espresso'), |
521 | 521 | $message->messenger() |
522 | 522 | ); |
523 | 523 | } |
524 | - if (! $message->valid_message_type()) { |
|
524 | + if ( ! $message->valid_message_type()) { |
|
525 | 525 | $error_messages[] = sprintf( |
526 | 526 | __('The %s message type is not active at the time of sending.', 'event_espresso'), |
527 | 527 | $message->message_type() |
@@ -582,9 +582,9 @@ discard block |
||
582 | 582 | // send using messenger, but double check objects |
583 | 583 | if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
584 | 584 | // set hook for message type (but only if not using another messenger to send). |
585 | - if (! isset($this->_did_hook[ $message_type->name ])) { |
|
585 | + if ( ! isset($this->_did_hook[$message_type->name])) { |
|
586 | 586 | $message_type->do_messenger_hooks($messenger); |
587 | - $this->_did_hook[ $message_type->name ] = 1; |
|
587 | + $this->_did_hook[$message_type->name] = 1; |
|
588 | 588 | } |
589 | 589 | // if preview then use preview method |
590 | 590 | return $this->_message_repository->is_preview() |
@@ -635,7 +635,7 @@ discard block |
||
635 | 635 | $test_send |
636 | 636 | ) { |
637 | 637 | if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
638 | - if (! $test_send) { |
|
638 | + if ( ! $test_send) { |
|
639 | 639 | $message->set_content($preview); |
640 | 640 | } |
641 | 641 | $message->set_STS_ID(EEM_Message::status_sent); |
@@ -700,7 +700,7 @@ discard block |
||
700 | 700 | } |
701 | 701 | if (count($error_messages) > 0) { |
702 | 702 | $msg = __('Message was not executed successfully.', 'event_espresso'); |
703 | - $msg = $msg . "\n" . implode("\n", $error_messages); |
|
703 | + $msg = $msg."\n".implode("\n", $error_messages); |
|
704 | 704 | $message->set_error_message($msg); |
705 | 705 | } |
706 | 706 | } |
@@ -15,699 +15,699 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * @type string reference for sending action |
|
20 | - */ |
|
21 | - const action_sending = 'sending'; |
|
22 | - |
|
23 | - /** |
|
24 | - * @type string reference for generation action |
|
25 | - */ |
|
26 | - const action_generating = 'generation'; |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * @type EE_Message_Repository $_message_repository |
|
31 | - */ |
|
32 | - protected $_message_repository; |
|
33 | - |
|
34 | - /** |
|
35 | - * Sets the limit of how many messages are generated per process. |
|
36 | - * |
|
37 | - * @type int |
|
38 | - */ |
|
39 | - protected $_batch_count; |
|
40 | - |
|
41 | - |
|
42 | - /** |
|
43 | - * This is an array of cached queue items being stored in this object. |
|
44 | - * The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
|
45 | - * is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
|
46 | - * |
|
47 | - * @type EE_Message[] |
|
48 | - */ |
|
49 | - protected $_cached_queue_items; |
|
50 | - |
|
51 | - /** |
|
52 | - * Tracks the number of unsaved queue items. |
|
53 | - * |
|
54 | - * @type int |
|
55 | - */ |
|
56 | - protected $_unsaved_count = 0; |
|
57 | - |
|
58 | - /** |
|
59 | - * used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
|
60 | - * hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
|
61 | - * |
|
62 | - * @type array |
|
63 | - */ |
|
64 | - protected $_did_hook = array(); |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Constructor. |
|
69 | - * Setup all the initial properties and load a EE_Message_Repository. |
|
70 | - * |
|
71 | - * @param \EE_Message_Repository $message_repository |
|
72 | - */ |
|
73 | - public function __construct(EE_Message_Repository $message_repository) |
|
74 | - { |
|
75 | - $this->_batch_count = apply_filters('FHEE__EE_Messages_Queue___batch_count', 50); |
|
76 | - $this->_message_repository = $message_repository; |
|
77 | - } |
|
78 | - |
|
79 | - |
|
80 | - /** |
|
81 | - * Add a EE_Message object to the queue |
|
82 | - * |
|
83 | - * @param EE_Message $message |
|
84 | - * @param array $data This will be an array of data to attach to the object in the repository. If the |
|
85 | - * object is persisted, this data will be saved on an extra_meta object related to |
|
86 | - * EE_Message. |
|
87 | - * @param bool $preview Whether this EE_Message represents a preview or not. |
|
88 | - * @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
|
89 | - * use the messenger send method but typically is based on preview data. |
|
90 | - * @return bool Whether the message was successfully added to the repository or not. |
|
91 | - */ |
|
92 | - public function add(EE_Message $message, $data = array(), $preview = false, $test_send = false) |
|
93 | - { |
|
94 | - $data['preview'] = $preview; |
|
95 | - $data['test_send'] = $test_send; |
|
96 | - return $this->_message_repository->add($message, $data); |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
|
102 | - * |
|
103 | - * @param EE_Message $message The message to detach from the queue |
|
104 | - * @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
|
105 | - * @return bool |
|
106 | - */ |
|
107 | - public function remove(EE_Message $message, $persist = false) |
|
108 | - { |
|
109 | - if ($persist && $this->_message_repository->current() !== $message) { |
|
110 | - // get pointer on right message |
|
111 | - if ($this->_message_repository->has($message)) { |
|
112 | - $this->_message_repository->rewind(); |
|
113 | - while ($this->_message_repository->valid()) { |
|
114 | - if ($this->_message_repository->current() === $message) { |
|
115 | - break; |
|
116 | - } |
|
117 | - $this->_message_repository->next(); |
|
118 | - } |
|
119 | - } else { |
|
120 | - return false; |
|
121 | - } |
|
122 | - } |
|
123 | - return $persist ? $this->_message_repository->delete() : $this->_message_repository->remove($message); |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * Persists all queued EE_Message objects to the db. |
|
129 | - * |
|
130 | - * @param bool $do_hooks_only @see EE_Message_Repository::saveAll |
|
131 | - * @return array @see EE_Messages_Repository::saveAll() for return values. |
|
132 | - */ |
|
133 | - public function save($do_hooks_only = false) |
|
134 | - { |
|
135 | - return $this->_message_repository->saveAll($do_hooks_only); |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * @return EE_Message_Repository |
|
141 | - */ |
|
142 | - public function get_message_repository() |
|
143 | - { |
|
144 | - return $this->_message_repository; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * This does the following things: |
|
150 | - * 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return |
|
151 | - * false). |
|
152 | - * 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
|
153 | - * 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
|
154 | - * Note: Callers should make sure they release the lock otherwise batch generation will be prevented from |
|
155 | - * continuing. The lock is on a transient that is set to expire after one hour as a fallback in case locks are not |
|
156 | - * removed. |
|
157 | - * |
|
158 | - * @return bool true if successfully retrieved batch, false no batch ready. |
|
159 | - */ |
|
160 | - public function get_batch_to_generate() |
|
161 | - { |
|
162 | - if ($this->is_locked(EE_Messages_Queue::action_generating)) { |
|
163 | - return false; |
|
164 | - } |
|
165 | - |
|
166 | - // lock batch generation to prevent race conditions. |
|
167 | - $this->lock_queue(EE_Messages_Queue::action_generating); |
|
168 | - |
|
169 | - $query_args = array( |
|
170 | - // key 0 = where conditions |
|
171 | - 0 => array('STS_ID' => EEM_Message::status_incomplete), |
|
172 | - 'order_by' => $this->_get_priority_orderby(), |
|
173 | - 'limit' => $this->_batch_count, |
|
174 | - ); |
|
175 | - $messages = EEM_Message::instance()->get_all($query_args); |
|
176 | - |
|
177 | - if (! $messages) { |
|
178 | - return false; // nothing to generate |
|
179 | - } |
|
180 | - |
|
181 | - foreach ($messages as $message) { |
|
182 | - if ($message instanceof EE_Message) { |
|
183 | - $data = $message->all_extra_meta_array(); |
|
184 | - $this->add($message, $data); |
|
185 | - } |
|
186 | - } |
|
187 | - return true; |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * This does the following things: |
|
193 | - * 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return |
|
194 | - * false). |
|
195 | - * 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then |
|
196 | - * return false. |
|
197 | - * 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
|
198 | - * 3. On success or unsuccessful send, sets status appropriately. |
|
199 | - * 4. Saves messages via the queue |
|
200 | - * 5. Releases lock. |
|
201 | - * |
|
202 | - * @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not |
|
203 | - * necessarily mean that all messages were successfully sent. It just means that this method |
|
204 | - * successfully completed. On true, client may want to call $this->count_STS_in_queue( |
|
205 | - * EEM_Message::status_failed ) to see if any failed EE_Message objects. Each failed message object |
|
206 | - * will also have a saved error message on it to assist with notifying user. |
|
207 | - */ |
|
208 | - public function get_to_send_batch_and_send() |
|
209 | - { |
|
210 | - $rate_limit = $this->get_rate_limit(); |
|
211 | - if ( |
|
212 | - $rate_limit < 1 |
|
213 | - || $this->is_locked(EE_Messages_Queue::action_sending) |
|
214 | - ) { |
|
215 | - return false; |
|
216 | - } |
|
217 | - |
|
218 | - $this->lock_queue(EE_Messages_Queue::action_sending); |
|
219 | - |
|
220 | - $batch = $this->_batch_count < $rate_limit ? $this->_batch_count : $rate_limit; |
|
221 | - |
|
222 | - $query_args = array( |
|
223 | - // key 0 = where conditions |
|
224 | - 0 => array('STS_ID' => array('IN', EEM_Message::instance()->stati_indicating_to_send())), |
|
225 | - 'order_by' => $this->_get_priority_orderby(), |
|
226 | - 'limit' => $batch, |
|
227 | - ); |
|
228 | - |
|
229 | - $messages_to_send = EEM_Message::instance()->get_all($query_args); |
|
230 | - |
|
231 | - |
|
232 | - // any to send? |
|
233 | - if (! $messages_to_send) { |
|
234 | - $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
235 | - return false; |
|
236 | - } |
|
237 | - |
|
238 | - $queue_count = 0; |
|
239 | - |
|
240 | - // add to queue. |
|
241 | - foreach ($messages_to_send as $message) { |
|
242 | - if ($message instanceof EE_Message) { |
|
243 | - $queue_count++; |
|
244 | - $this->add($message); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - // send messages (this also updates the rate limit) |
|
249 | - $this->execute(); |
|
250 | - |
|
251 | - // release lock |
|
252 | - $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
253 | - // update rate limit |
|
254 | - $this->set_rate_limit($queue_count); |
|
255 | - return true; |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * Locks the queue so that no other queues can call the "batch" methods. |
|
261 | - * |
|
262 | - * @param string $type The type of queue being locked. |
|
263 | - */ |
|
264 | - public function lock_queue($type = EE_Messages_Queue::action_generating) |
|
265 | - { |
|
266 | - update_option($this->_get_lock_key($type), $this->_get_lock_expiry($type)); |
|
267 | - } |
|
268 | - |
|
269 | - |
|
270 | - /** |
|
271 | - * Unlocks the queue so that batch methods can be used. |
|
272 | - * |
|
273 | - * @param string $type The type of queue being unlocked. |
|
274 | - */ |
|
275 | - public function unlock_queue($type = EE_Messages_Queue::action_generating) |
|
276 | - { |
|
277 | - delete_option($this->_get_lock_key($type)); |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * Retrieve the key used for the lock transient. |
|
283 | - * |
|
284 | - * @param string $type The type of lock. |
|
285 | - * @return string |
|
286 | - */ |
|
287 | - protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
|
288 | - { |
|
289 | - return '_ee_lock_' . $type; |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - /** |
|
294 | - * Retrieve the expiry time for the lock transient. |
|
295 | - * |
|
296 | - * @param string $type The type of lock |
|
297 | - * @return int time to expiry in seconds. |
|
298 | - */ |
|
299 | - protected function _get_lock_expiry($type = EE_Messages_Queue::action_generating) |
|
300 | - { |
|
301 | - return time() + (int) apply_filters('FHEE__EE_Messages_Queue__lock_expiry', HOUR_IN_SECONDS, $type); |
|
302 | - } |
|
303 | - |
|
304 | - |
|
305 | - /** |
|
306 | - * Returns the key used for rate limit transient. |
|
307 | - * |
|
308 | - * @return string |
|
309 | - */ |
|
310 | - protected function _get_rate_limit_key() |
|
311 | - { |
|
312 | - return '_ee_rate_limit'; |
|
313 | - } |
|
314 | - |
|
315 | - |
|
316 | - /** |
|
317 | - * Returns the rate limit expiry time. |
|
318 | - * |
|
319 | - * @return int |
|
320 | - */ |
|
321 | - protected function _get_rate_limit_expiry() |
|
322 | - { |
|
323 | - return time() + (int) apply_filters('FHEE__EE_Messages_Queue__rate_limit_expiry', HOUR_IN_SECONDS); |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * Returns the default rate limit for sending messages. |
|
329 | - * |
|
330 | - * @return int |
|
331 | - */ |
|
332 | - protected function _default_rate_limit() |
|
333 | - { |
|
334 | - return (int) apply_filters('FHEE__EE_Messages_Queue___rate_limit', 200); |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - /** |
|
339 | - * Return the orderby array for priority. |
|
340 | - * |
|
341 | - * @return array |
|
342 | - */ |
|
343 | - protected function _get_priority_orderby() |
|
344 | - { |
|
345 | - return array( |
|
346 | - 'MSG_priority' => 'ASC', |
|
347 | - 'MSG_modified' => 'DESC', |
|
348 | - ); |
|
349 | - } |
|
350 | - |
|
351 | - |
|
352 | - /** |
|
353 | - * Returns whether batch methods are "locked" or not, and if models an currently be used to query the database. |
|
354 | - * Return true when batch methods should not be used; returns false when they can be. |
|
355 | - * |
|
356 | - * @param string $type The type of lock being checked for. |
|
357 | - * @return bool |
|
358 | - */ |
|
359 | - public function is_locked($type = EE_Messages_Queue::action_generating) |
|
360 | - { |
|
361 | - if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
362 | - return true; |
|
363 | - } |
|
364 | - $lock = (int) get_option($this->_get_lock_key($type), 0); |
|
365 | - /** |
|
366 | - * This filters the default is_locked behaviour. |
|
367 | - */ |
|
368 | - $is_locked = filter_var( |
|
369 | - apply_filters( |
|
370 | - 'FHEE__EE_Messages_Queue__is_locked', |
|
371 | - $lock > time(), |
|
372 | - $this |
|
373 | - ), |
|
374 | - FILTER_VALIDATE_BOOLEAN |
|
375 | - ); |
|
376 | - |
|
377 | - /** |
|
378 | - * @see usage of this filter in EE_Messages_Queue::initiate_request_by_priority() method. |
|
379 | - * Also implemented here because messages processed on the same request should not have any locks applied. |
|
380 | - */ |
|
381 | - if ( |
|
382 | - apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
383 | - || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
384 | - ) { |
|
385 | - $is_locked = false; |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - return $is_locked; |
|
390 | - } |
|
391 | - |
|
392 | - |
|
393 | - /** |
|
394 | - * Retrieves the rate limit that may be cached as a transient. |
|
395 | - * If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
|
396 | - * |
|
397 | - * @param bool $return_expiry If true then return the expiry time not the rate_limit. |
|
398 | - * @return int |
|
399 | - */ |
|
400 | - protected function get_rate_limit($return_expiry = false) |
|
401 | - { |
|
402 | - $stored_rate_info = get_option($this->_get_rate_limit_key(), array()); |
|
403 | - $rate_limit = isset($stored_rate_info[0]) |
|
404 | - ? (int) $stored_rate_info[0] |
|
405 | - : 0; |
|
406 | - $expiry = isset($stored_rate_info[1]) |
|
407 | - ? (int) $stored_rate_info[1] |
|
408 | - : 0; |
|
409 | - // set the default for tracking? |
|
410 | - if (empty($stored_rate_info) || time() > $expiry) { |
|
411 | - $expiry = $this->_get_rate_limit_expiry(); |
|
412 | - $rate_limit = $this->_default_rate_limit(); |
|
413 | - update_option($this->_get_rate_limit_key(), array($rate_limit, $expiry)); |
|
414 | - } |
|
415 | - return $return_expiry ? $expiry : $rate_limit; |
|
416 | - } |
|
417 | - |
|
418 | - |
|
419 | - /** |
|
420 | - * This updates existing rate limit with the new limit which is the old minus the batch. |
|
421 | - * |
|
422 | - * @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
|
423 | - */ |
|
424 | - protected function set_rate_limit($batch_completed) |
|
425 | - { |
|
426 | - // first get the most up to date rate limit (in case its expired and reset) |
|
427 | - $rate_limit = $this->get_rate_limit(); |
|
428 | - $expiry = $this->get_rate_limit(true); |
|
429 | - $new_limit = $rate_limit - $batch_completed; |
|
430 | - // updating the transient option directly to avoid resetting the expiry. |
|
431 | - |
|
432 | - update_option($this->_get_rate_limit_key(), array($new_limit, $expiry)); |
|
433 | - } |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
|
438 | - * If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
|
439 | - * Note: Keep in mind that there is the possibility that the request will not execute if there is already another |
|
440 | - * request running on a queue for the given task. |
|
441 | - * |
|
442 | - * @param string $task This indicates what type of request is going to be initiated. |
|
443 | - * @param int $priority This indicates the priority that triggers initiating the request. |
|
444 | - */ |
|
445 | - public function initiate_request_by_priority($task = 'generate', $priority = EEM_Message::priority_high) |
|
446 | - { |
|
447 | - // determine what status is matched with the priority as part of the trigger conditions. |
|
448 | - $status = $task == 'generate' |
|
449 | - ? EEM_Message::status_incomplete |
|
450 | - : EEM_Message::instance()->stati_indicating_to_send(); |
|
451 | - // always make sure we save because either this will get executed immediately on a separate request |
|
452 | - // or remains in the queue for the regularly scheduled queue batch. |
|
453 | - $this->save(); |
|
454 | - /** |
|
455 | - * This filter/option allows users to override processing of messages on separate requests and instead have everything |
|
456 | - * happen on the same request. If this is utilized remember: |
|
457 | - * - message priorities don't matter |
|
458 | - * - existing unprocessed messages in the queue will not get processed unless manually triggered. |
|
459 | - * - things will be perceived to take longer to happen for end users (i.e. registrations) because of the additional |
|
460 | - * processing happening on the same request. |
|
461 | - * - any race condition protection (locks) are removed because they don't apply when things are processed on |
|
462 | - * the same request. |
|
463 | - */ |
|
464 | - if ( |
|
465 | - apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
466 | - || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
467 | - ) { |
|
468 | - $messages_processor = EE_Registry::instance()->load_lib('Messages_Processor'); |
|
469 | - if ($messages_processor instanceof EE_Messages_Processor) { |
|
470 | - return $messages_processor->process_immediately_from_queue($this); |
|
471 | - } |
|
472 | - // if we get here then that means the messages processor couldn't be loaded so messages will just remain |
|
473 | - // queued for manual triggering by end user. |
|
474 | - } |
|
475 | - |
|
476 | - if ($this->_message_repository->count_by_priority_and_status($priority, $status)) { |
|
477 | - EE_Messages_Scheduler::initiate_scheduled_non_blocking_request($task); |
|
478 | - } |
|
479 | - } |
|
480 | - |
|
481 | - |
|
482 | - /** |
|
483 | - * Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
|
484 | - * |
|
485 | - * @param bool $save Used to indicate whether to save the message queue after sending |
|
486 | - * (default will save). |
|
487 | - * @param mixed $sending_messenger (optional) When the sending messenger is different than |
|
488 | - * what is on the EE_Message object in the queue. |
|
489 | - * For instance, showing the browser view of an email message, |
|
490 | - * or giving a pdf generated view of an html document. |
|
491 | - * This should be an instance of EE_messenger but if you call this |
|
492 | - * method |
|
493 | - * intending it to be a sending messenger but a valid one could not be |
|
494 | - * retrieved then send in an instance of EE_Error that contains the |
|
495 | - * related error message. |
|
496 | - * @param bool|int $by_priority When set, this indicates that only messages |
|
497 | - * matching the given priority should be executed. |
|
498 | - * @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
|
499 | - * Also, if the messenger is an request type messenger (or a preview), |
|
500 | - * its entirely possible that the messenger will exit before |
|
501 | - */ |
|
502 | - public function execute($save = true, $sending_messenger = null, $by_priority = false) |
|
503 | - { |
|
504 | - $messages_sent = 0; |
|
505 | - $this->_did_hook = array(); |
|
506 | - $this->_message_repository->rewind(); |
|
507 | - |
|
508 | - while ($this->_message_repository->valid()) { |
|
509 | - $error_messages = array(); |
|
510 | - /** @type EE_Message $message */ |
|
511 | - $message = $this->_message_repository->current(); |
|
512 | - // only process things that are queued for sending |
|
513 | - if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
514 | - $this->_message_repository->next(); |
|
515 | - continue; |
|
516 | - } |
|
517 | - // if $by_priority is set and does not match then continue; |
|
518 | - if ($by_priority && $by_priority != $message->priority()) { |
|
519 | - $this->_message_repository->next(); |
|
520 | - continue; |
|
521 | - } |
|
522 | - // error checking |
|
523 | - if (! $message->valid_messenger()) { |
|
524 | - $error_messages[] = sprintf( |
|
525 | - __('The %s messenger is not active at time of sending.', 'event_espresso'), |
|
526 | - $message->messenger() |
|
527 | - ); |
|
528 | - } |
|
529 | - if (! $message->valid_message_type()) { |
|
530 | - $error_messages[] = sprintf( |
|
531 | - __('The %s message type is not active at the time of sending.', 'event_espresso'), |
|
532 | - $message->message_type() |
|
533 | - ); |
|
534 | - } |
|
535 | - // if there was supposed to be a sending messenger for this message, but it was invalid/inactive, |
|
536 | - // then it will instead be an EE_Error object, so let's check for that |
|
537 | - if ($sending_messenger instanceof EE_Error) { |
|
538 | - $error_messages[] = $sending_messenger->getMessage(); |
|
539 | - } |
|
540 | - // if there are no errors, then let's process the message |
|
541 | - if (empty($error_messages)) { |
|
542 | - if ($save) { |
|
543 | - $message->set_messenger_is_executing(); |
|
544 | - } |
|
545 | - if ($this->_process_message($message, $sending_messenger)) { |
|
546 | - $messages_sent++; |
|
547 | - } |
|
548 | - } |
|
549 | - $this->_set_error_message($message, $error_messages); |
|
550 | - // add modified time |
|
551 | - $message->set_modified(time()); |
|
552 | - // we save each message after its processed to make sure its status persists in case PHP times-out or runs |
|
553 | - // out of memory. @see https://events.codebasehq.com/projects/event-espresso/tickets/10281 |
|
554 | - if ($save) { |
|
555 | - $message->save(); |
|
556 | - } |
|
557 | - |
|
558 | - $this->_message_repository->next(); |
|
559 | - } |
|
560 | - if ($save) { |
|
561 | - $this->save(true); |
|
562 | - } |
|
563 | - return $messages_sent; |
|
564 | - } |
|
565 | - |
|
566 | - |
|
567 | - /** |
|
568 | - * _process_message |
|
569 | - * |
|
570 | - * @param EE_Message $message |
|
571 | - * @param mixed $sending_messenger (optional) |
|
572 | - * @return bool |
|
573 | - */ |
|
574 | - protected function _process_message(EE_Message $message, $sending_messenger = null) |
|
575 | - { |
|
576 | - // these *should* have been validated in the execute() method above |
|
577 | - $messenger = $message->messenger_object(); |
|
578 | - $message_type = $message->message_type_object(); |
|
579 | - // do actions for sending messenger if it differs from generating messenger and swap values. |
|
580 | - if ( |
|
581 | - $sending_messenger instanceof EE_messenger |
|
582 | - && $messenger instanceof EE_messenger |
|
583 | - && $sending_messenger->name != $messenger->name |
|
584 | - ) { |
|
585 | - $messenger->do_secondary_messenger_hooks($sending_messenger->name); |
|
586 | - $messenger = $sending_messenger; |
|
587 | - } |
|
588 | - // send using messenger, but double check objects |
|
589 | - if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
|
590 | - // set hook for message type (but only if not using another messenger to send). |
|
591 | - if (! isset($this->_did_hook[ $message_type->name ])) { |
|
592 | - $message_type->do_messenger_hooks($messenger); |
|
593 | - $this->_did_hook[ $message_type->name ] = 1; |
|
594 | - } |
|
595 | - // if preview then use preview method |
|
596 | - return $this->_message_repository->is_preview() |
|
597 | - ? $this->_do_preview($message, $messenger, $message_type, $this->_message_repository->is_test_send()) |
|
598 | - : $this->_do_send($message, $messenger, $message_type); |
|
599 | - } |
|
600 | - return false; |
|
601 | - } |
|
602 | - |
|
603 | - |
|
604 | - /** |
|
605 | - * The intention of this method is to count how many EE_Message objects |
|
606 | - * are in the queue with a given status. |
|
607 | - * Example usage: |
|
608 | - * After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed |
|
609 | - * sends by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
|
610 | - * |
|
611 | - * @param array|string $status Stati to check for in queue |
|
612 | - * @return int Count of EE_Message's matching the given status. |
|
613 | - */ |
|
614 | - public function count_STS_in_queue($status) |
|
615 | - { |
|
616 | - $count = 0; |
|
617 | - $status = is_array($status) ? $status : array($status); |
|
618 | - $this->_message_repository->rewind(); |
|
619 | - foreach ($this->_message_repository as $message) { |
|
620 | - if (in_array($message->STS_ID(), $status)) { |
|
621 | - $count++; |
|
622 | - } |
|
623 | - } |
|
624 | - return $count; |
|
625 | - } |
|
626 | - |
|
627 | - |
|
628 | - /** |
|
629 | - * Executes the get_preview method on the provided messenger. |
|
630 | - * |
|
631 | - * @param EE_Message $message |
|
632 | - * @param EE_messenger $messenger |
|
633 | - * @param EE_message_type $message_type |
|
634 | - * @param $test_send |
|
635 | - * @return bool true means all went well, false means, not so much. |
|
636 | - */ |
|
637 | - protected function _do_preview( |
|
638 | - EE_Message $message, |
|
639 | - EE_messenger $messenger, |
|
640 | - EE_message_type $message_type, |
|
641 | - $test_send |
|
642 | - ) { |
|
643 | - if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
|
644 | - if (! $test_send) { |
|
645 | - $message->set_content($preview); |
|
646 | - } |
|
647 | - $message->set_STS_ID(EEM_Message::status_sent); |
|
648 | - return true; |
|
649 | - } else { |
|
650 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
651 | - return false; |
|
652 | - } |
|
653 | - } |
|
654 | - |
|
655 | - |
|
656 | - /** |
|
657 | - * Executes the send method on the provided messenger |
|
658 | - * EE_Messengers are expected to: |
|
659 | - * - return true if the send was successful. |
|
660 | - * - return false if the send was unsuccessful but can be tried again. |
|
661 | - * - throw an Exception if the send was unsuccessful and cannot be tried again. |
|
662 | - * |
|
663 | - * @param EE_Message $message |
|
664 | - * @param EE_messenger $messenger |
|
665 | - * @param EE_message_type $message_type |
|
666 | - * @return bool true means all went well, false means, not so much. |
|
667 | - */ |
|
668 | - protected function _do_send(EE_Message $message, EE_messenger $messenger, EE_message_type $message_type) |
|
669 | - { |
|
670 | - try { |
|
671 | - if ($messenger->send_message($message, $message_type)) { |
|
672 | - $message->set_STS_ID(EEM_Message::status_sent); |
|
673 | - return true; |
|
674 | - } else { |
|
675 | - $message->set_STS_ID(EEM_Message::status_retry); |
|
676 | - return false; |
|
677 | - } |
|
678 | - } catch (SendMessageException $e) { |
|
679 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
680 | - $message->set_error_message($e->getMessage()); |
|
681 | - return false; |
|
682 | - } |
|
683 | - } |
|
684 | - |
|
685 | - |
|
686 | - /** |
|
687 | - * This sets any necessary error messages on the message object and its status to failed. |
|
688 | - * |
|
689 | - * @param EE_Message $message |
|
690 | - * @param array $error_messages the response from the messenger. |
|
691 | - */ |
|
692 | - protected function _set_error_message(EE_Message $message, $error_messages) |
|
693 | - { |
|
694 | - $error_messages = (array) $error_messages; |
|
695 | - if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_failed_sending())) { |
|
696 | - $notices = EE_Error::has_notices(); |
|
697 | - $error_messages[] = __( |
|
698 | - 'Messenger and Message Type were valid and active, but the messenger send method failed.', |
|
699 | - 'event_espresso' |
|
700 | - ); |
|
701 | - if ($notices === 1) { |
|
702 | - $notices = EE_Error::get_vanilla_notices(); |
|
703 | - $notices['errors'] = isset($notices['errors']) ? $notices['errors'] : array(); |
|
704 | - $error_messages[] = implode("\n", $notices['errors']); |
|
705 | - } |
|
706 | - } |
|
707 | - if (count($error_messages) > 0) { |
|
708 | - $msg = __('Message was not executed successfully.', 'event_espresso'); |
|
709 | - $msg = $msg . "\n" . implode("\n", $error_messages); |
|
710 | - $message->set_error_message($msg); |
|
711 | - } |
|
712 | - } |
|
18 | + /** |
|
19 | + * @type string reference for sending action |
|
20 | + */ |
|
21 | + const action_sending = 'sending'; |
|
22 | + |
|
23 | + /** |
|
24 | + * @type string reference for generation action |
|
25 | + */ |
|
26 | + const action_generating = 'generation'; |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * @type EE_Message_Repository $_message_repository |
|
31 | + */ |
|
32 | + protected $_message_repository; |
|
33 | + |
|
34 | + /** |
|
35 | + * Sets the limit of how many messages are generated per process. |
|
36 | + * |
|
37 | + * @type int |
|
38 | + */ |
|
39 | + protected $_batch_count; |
|
40 | + |
|
41 | + |
|
42 | + /** |
|
43 | + * This is an array of cached queue items being stored in this object. |
|
44 | + * The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
|
45 | + * is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
|
46 | + * |
|
47 | + * @type EE_Message[] |
|
48 | + */ |
|
49 | + protected $_cached_queue_items; |
|
50 | + |
|
51 | + /** |
|
52 | + * Tracks the number of unsaved queue items. |
|
53 | + * |
|
54 | + * @type int |
|
55 | + */ |
|
56 | + protected $_unsaved_count = 0; |
|
57 | + |
|
58 | + /** |
|
59 | + * used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
|
60 | + * hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
|
61 | + * |
|
62 | + * @type array |
|
63 | + */ |
|
64 | + protected $_did_hook = array(); |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Constructor. |
|
69 | + * Setup all the initial properties and load a EE_Message_Repository. |
|
70 | + * |
|
71 | + * @param \EE_Message_Repository $message_repository |
|
72 | + */ |
|
73 | + public function __construct(EE_Message_Repository $message_repository) |
|
74 | + { |
|
75 | + $this->_batch_count = apply_filters('FHEE__EE_Messages_Queue___batch_count', 50); |
|
76 | + $this->_message_repository = $message_repository; |
|
77 | + } |
|
78 | + |
|
79 | + |
|
80 | + /** |
|
81 | + * Add a EE_Message object to the queue |
|
82 | + * |
|
83 | + * @param EE_Message $message |
|
84 | + * @param array $data This will be an array of data to attach to the object in the repository. If the |
|
85 | + * object is persisted, this data will be saved on an extra_meta object related to |
|
86 | + * EE_Message. |
|
87 | + * @param bool $preview Whether this EE_Message represents a preview or not. |
|
88 | + * @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
|
89 | + * use the messenger send method but typically is based on preview data. |
|
90 | + * @return bool Whether the message was successfully added to the repository or not. |
|
91 | + */ |
|
92 | + public function add(EE_Message $message, $data = array(), $preview = false, $test_send = false) |
|
93 | + { |
|
94 | + $data['preview'] = $preview; |
|
95 | + $data['test_send'] = $test_send; |
|
96 | + return $this->_message_repository->add($message, $data); |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
|
102 | + * |
|
103 | + * @param EE_Message $message The message to detach from the queue |
|
104 | + * @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
|
105 | + * @return bool |
|
106 | + */ |
|
107 | + public function remove(EE_Message $message, $persist = false) |
|
108 | + { |
|
109 | + if ($persist && $this->_message_repository->current() !== $message) { |
|
110 | + // get pointer on right message |
|
111 | + if ($this->_message_repository->has($message)) { |
|
112 | + $this->_message_repository->rewind(); |
|
113 | + while ($this->_message_repository->valid()) { |
|
114 | + if ($this->_message_repository->current() === $message) { |
|
115 | + break; |
|
116 | + } |
|
117 | + $this->_message_repository->next(); |
|
118 | + } |
|
119 | + } else { |
|
120 | + return false; |
|
121 | + } |
|
122 | + } |
|
123 | + return $persist ? $this->_message_repository->delete() : $this->_message_repository->remove($message); |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * Persists all queued EE_Message objects to the db. |
|
129 | + * |
|
130 | + * @param bool $do_hooks_only @see EE_Message_Repository::saveAll |
|
131 | + * @return array @see EE_Messages_Repository::saveAll() for return values. |
|
132 | + */ |
|
133 | + public function save($do_hooks_only = false) |
|
134 | + { |
|
135 | + return $this->_message_repository->saveAll($do_hooks_only); |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * @return EE_Message_Repository |
|
141 | + */ |
|
142 | + public function get_message_repository() |
|
143 | + { |
|
144 | + return $this->_message_repository; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * This does the following things: |
|
150 | + * 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return |
|
151 | + * false). |
|
152 | + * 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
|
153 | + * 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
|
154 | + * Note: Callers should make sure they release the lock otherwise batch generation will be prevented from |
|
155 | + * continuing. The lock is on a transient that is set to expire after one hour as a fallback in case locks are not |
|
156 | + * removed. |
|
157 | + * |
|
158 | + * @return bool true if successfully retrieved batch, false no batch ready. |
|
159 | + */ |
|
160 | + public function get_batch_to_generate() |
|
161 | + { |
|
162 | + if ($this->is_locked(EE_Messages_Queue::action_generating)) { |
|
163 | + return false; |
|
164 | + } |
|
165 | + |
|
166 | + // lock batch generation to prevent race conditions. |
|
167 | + $this->lock_queue(EE_Messages_Queue::action_generating); |
|
168 | + |
|
169 | + $query_args = array( |
|
170 | + // key 0 = where conditions |
|
171 | + 0 => array('STS_ID' => EEM_Message::status_incomplete), |
|
172 | + 'order_by' => $this->_get_priority_orderby(), |
|
173 | + 'limit' => $this->_batch_count, |
|
174 | + ); |
|
175 | + $messages = EEM_Message::instance()->get_all($query_args); |
|
176 | + |
|
177 | + if (! $messages) { |
|
178 | + return false; // nothing to generate |
|
179 | + } |
|
180 | + |
|
181 | + foreach ($messages as $message) { |
|
182 | + if ($message instanceof EE_Message) { |
|
183 | + $data = $message->all_extra_meta_array(); |
|
184 | + $this->add($message, $data); |
|
185 | + } |
|
186 | + } |
|
187 | + return true; |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * This does the following things: |
|
193 | + * 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return |
|
194 | + * false). |
|
195 | + * 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then |
|
196 | + * return false. |
|
197 | + * 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
|
198 | + * 3. On success or unsuccessful send, sets status appropriately. |
|
199 | + * 4. Saves messages via the queue |
|
200 | + * 5. Releases lock. |
|
201 | + * |
|
202 | + * @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not |
|
203 | + * necessarily mean that all messages were successfully sent. It just means that this method |
|
204 | + * successfully completed. On true, client may want to call $this->count_STS_in_queue( |
|
205 | + * EEM_Message::status_failed ) to see if any failed EE_Message objects. Each failed message object |
|
206 | + * will also have a saved error message on it to assist with notifying user. |
|
207 | + */ |
|
208 | + public function get_to_send_batch_and_send() |
|
209 | + { |
|
210 | + $rate_limit = $this->get_rate_limit(); |
|
211 | + if ( |
|
212 | + $rate_limit < 1 |
|
213 | + || $this->is_locked(EE_Messages_Queue::action_sending) |
|
214 | + ) { |
|
215 | + return false; |
|
216 | + } |
|
217 | + |
|
218 | + $this->lock_queue(EE_Messages_Queue::action_sending); |
|
219 | + |
|
220 | + $batch = $this->_batch_count < $rate_limit ? $this->_batch_count : $rate_limit; |
|
221 | + |
|
222 | + $query_args = array( |
|
223 | + // key 0 = where conditions |
|
224 | + 0 => array('STS_ID' => array('IN', EEM_Message::instance()->stati_indicating_to_send())), |
|
225 | + 'order_by' => $this->_get_priority_orderby(), |
|
226 | + 'limit' => $batch, |
|
227 | + ); |
|
228 | + |
|
229 | + $messages_to_send = EEM_Message::instance()->get_all($query_args); |
|
230 | + |
|
231 | + |
|
232 | + // any to send? |
|
233 | + if (! $messages_to_send) { |
|
234 | + $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
235 | + return false; |
|
236 | + } |
|
237 | + |
|
238 | + $queue_count = 0; |
|
239 | + |
|
240 | + // add to queue. |
|
241 | + foreach ($messages_to_send as $message) { |
|
242 | + if ($message instanceof EE_Message) { |
|
243 | + $queue_count++; |
|
244 | + $this->add($message); |
|
245 | + } |
|
246 | + } |
|
247 | + |
|
248 | + // send messages (this also updates the rate limit) |
|
249 | + $this->execute(); |
|
250 | + |
|
251 | + // release lock |
|
252 | + $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
253 | + // update rate limit |
|
254 | + $this->set_rate_limit($queue_count); |
|
255 | + return true; |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * Locks the queue so that no other queues can call the "batch" methods. |
|
261 | + * |
|
262 | + * @param string $type The type of queue being locked. |
|
263 | + */ |
|
264 | + public function lock_queue($type = EE_Messages_Queue::action_generating) |
|
265 | + { |
|
266 | + update_option($this->_get_lock_key($type), $this->_get_lock_expiry($type)); |
|
267 | + } |
|
268 | + |
|
269 | + |
|
270 | + /** |
|
271 | + * Unlocks the queue so that batch methods can be used. |
|
272 | + * |
|
273 | + * @param string $type The type of queue being unlocked. |
|
274 | + */ |
|
275 | + public function unlock_queue($type = EE_Messages_Queue::action_generating) |
|
276 | + { |
|
277 | + delete_option($this->_get_lock_key($type)); |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * Retrieve the key used for the lock transient. |
|
283 | + * |
|
284 | + * @param string $type The type of lock. |
|
285 | + * @return string |
|
286 | + */ |
|
287 | + protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
|
288 | + { |
|
289 | + return '_ee_lock_' . $type; |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + /** |
|
294 | + * Retrieve the expiry time for the lock transient. |
|
295 | + * |
|
296 | + * @param string $type The type of lock |
|
297 | + * @return int time to expiry in seconds. |
|
298 | + */ |
|
299 | + protected function _get_lock_expiry($type = EE_Messages_Queue::action_generating) |
|
300 | + { |
|
301 | + return time() + (int) apply_filters('FHEE__EE_Messages_Queue__lock_expiry', HOUR_IN_SECONDS, $type); |
|
302 | + } |
|
303 | + |
|
304 | + |
|
305 | + /** |
|
306 | + * Returns the key used for rate limit transient. |
|
307 | + * |
|
308 | + * @return string |
|
309 | + */ |
|
310 | + protected function _get_rate_limit_key() |
|
311 | + { |
|
312 | + return '_ee_rate_limit'; |
|
313 | + } |
|
314 | + |
|
315 | + |
|
316 | + /** |
|
317 | + * Returns the rate limit expiry time. |
|
318 | + * |
|
319 | + * @return int |
|
320 | + */ |
|
321 | + protected function _get_rate_limit_expiry() |
|
322 | + { |
|
323 | + return time() + (int) apply_filters('FHEE__EE_Messages_Queue__rate_limit_expiry', HOUR_IN_SECONDS); |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * Returns the default rate limit for sending messages. |
|
329 | + * |
|
330 | + * @return int |
|
331 | + */ |
|
332 | + protected function _default_rate_limit() |
|
333 | + { |
|
334 | + return (int) apply_filters('FHEE__EE_Messages_Queue___rate_limit', 200); |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + /** |
|
339 | + * Return the orderby array for priority. |
|
340 | + * |
|
341 | + * @return array |
|
342 | + */ |
|
343 | + protected function _get_priority_orderby() |
|
344 | + { |
|
345 | + return array( |
|
346 | + 'MSG_priority' => 'ASC', |
|
347 | + 'MSG_modified' => 'DESC', |
|
348 | + ); |
|
349 | + } |
|
350 | + |
|
351 | + |
|
352 | + /** |
|
353 | + * Returns whether batch methods are "locked" or not, and if models an currently be used to query the database. |
|
354 | + * Return true when batch methods should not be used; returns false when they can be. |
|
355 | + * |
|
356 | + * @param string $type The type of lock being checked for. |
|
357 | + * @return bool |
|
358 | + */ |
|
359 | + public function is_locked($type = EE_Messages_Queue::action_generating) |
|
360 | + { |
|
361 | + if (! EE_Maintenance_Mode::instance()->models_can_query()) { |
|
362 | + return true; |
|
363 | + } |
|
364 | + $lock = (int) get_option($this->_get_lock_key($type), 0); |
|
365 | + /** |
|
366 | + * This filters the default is_locked behaviour. |
|
367 | + */ |
|
368 | + $is_locked = filter_var( |
|
369 | + apply_filters( |
|
370 | + 'FHEE__EE_Messages_Queue__is_locked', |
|
371 | + $lock > time(), |
|
372 | + $this |
|
373 | + ), |
|
374 | + FILTER_VALIDATE_BOOLEAN |
|
375 | + ); |
|
376 | + |
|
377 | + /** |
|
378 | + * @see usage of this filter in EE_Messages_Queue::initiate_request_by_priority() method. |
|
379 | + * Also implemented here because messages processed on the same request should not have any locks applied. |
|
380 | + */ |
|
381 | + if ( |
|
382 | + apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
383 | + || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
384 | + ) { |
|
385 | + $is_locked = false; |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + return $is_locked; |
|
390 | + } |
|
391 | + |
|
392 | + |
|
393 | + /** |
|
394 | + * Retrieves the rate limit that may be cached as a transient. |
|
395 | + * If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
|
396 | + * |
|
397 | + * @param bool $return_expiry If true then return the expiry time not the rate_limit. |
|
398 | + * @return int |
|
399 | + */ |
|
400 | + protected function get_rate_limit($return_expiry = false) |
|
401 | + { |
|
402 | + $stored_rate_info = get_option($this->_get_rate_limit_key(), array()); |
|
403 | + $rate_limit = isset($stored_rate_info[0]) |
|
404 | + ? (int) $stored_rate_info[0] |
|
405 | + : 0; |
|
406 | + $expiry = isset($stored_rate_info[1]) |
|
407 | + ? (int) $stored_rate_info[1] |
|
408 | + : 0; |
|
409 | + // set the default for tracking? |
|
410 | + if (empty($stored_rate_info) || time() > $expiry) { |
|
411 | + $expiry = $this->_get_rate_limit_expiry(); |
|
412 | + $rate_limit = $this->_default_rate_limit(); |
|
413 | + update_option($this->_get_rate_limit_key(), array($rate_limit, $expiry)); |
|
414 | + } |
|
415 | + return $return_expiry ? $expiry : $rate_limit; |
|
416 | + } |
|
417 | + |
|
418 | + |
|
419 | + /** |
|
420 | + * This updates existing rate limit with the new limit which is the old minus the batch. |
|
421 | + * |
|
422 | + * @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
|
423 | + */ |
|
424 | + protected function set_rate_limit($batch_completed) |
|
425 | + { |
|
426 | + // first get the most up to date rate limit (in case its expired and reset) |
|
427 | + $rate_limit = $this->get_rate_limit(); |
|
428 | + $expiry = $this->get_rate_limit(true); |
|
429 | + $new_limit = $rate_limit - $batch_completed; |
|
430 | + // updating the transient option directly to avoid resetting the expiry. |
|
431 | + |
|
432 | + update_option($this->_get_rate_limit_key(), array($new_limit, $expiry)); |
|
433 | + } |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
|
438 | + * If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
|
439 | + * Note: Keep in mind that there is the possibility that the request will not execute if there is already another |
|
440 | + * request running on a queue for the given task. |
|
441 | + * |
|
442 | + * @param string $task This indicates what type of request is going to be initiated. |
|
443 | + * @param int $priority This indicates the priority that triggers initiating the request. |
|
444 | + */ |
|
445 | + public function initiate_request_by_priority($task = 'generate', $priority = EEM_Message::priority_high) |
|
446 | + { |
|
447 | + // determine what status is matched with the priority as part of the trigger conditions. |
|
448 | + $status = $task == 'generate' |
|
449 | + ? EEM_Message::status_incomplete |
|
450 | + : EEM_Message::instance()->stati_indicating_to_send(); |
|
451 | + // always make sure we save because either this will get executed immediately on a separate request |
|
452 | + // or remains in the queue for the regularly scheduled queue batch. |
|
453 | + $this->save(); |
|
454 | + /** |
|
455 | + * This filter/option allows users to override processing of messages on separate requests and instead have everything |
|
456 | + * happen on the same request. If this is utilized remember: |
|
457 | + * - message priorities don't matter |
|
458 | + * - existing unprocessed messages in the queue will not get processed unless manually triggered. |
|
459 | + * - things will be perceived to take longer to happen for end users (i.e. registrations) because of the additional |
|
460 | + * processing happening on the same request. |
|
461 | + * - any race condition protection (locks) are removed because they don't apply when things are processed on |
|
462 | + * the same request. |
|
463 | + */ |
|
464 | + if ( |
|
465 | + apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
466 | + || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
467 | + ) { |
|
468 | + $messages_processor = EE_Registry::instance()->load_lib('Messages_Processor'); |
|
469 | + if ($messages_processor instanceof EE_Messages_Processor) { |
|
470 | + return $messages_processor->process_immediately_from_queue($this); |
|
471 | + } |
|
472 | + // if we get here then that means the messages processor couldn't be loaded so messages will just remain |
|
473 | + // queued for manual triggering by end user. |
|
474 | + } |
|
475 | + |
|
476 | + if ($this->_message_repository->count_by_priority_and_status($priority, $status)) { |
|
477 | + EE_Messages_Scheduler::initiate_scheduled_non_blocking_request($task); |
|
478 | + } |
|
479 | + } |
|
480 | + |
|
481 | + |
|
482 | + /** |
|
483 | + * Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
|
484 | + * |
|
485 | + * @param bool $save Used to indicate whether to save the message queue after sending |
|
486 | + * (default will save). |
|
487 | + * @param mixed $sending_messenger (optional) When the sending messenger is different than |
|
488 | + * what is on the EE_Message object in the queue. |
|
489 | + * For instance, showing the browser view of an email message, |
|
490 | + * or giving a pdf generated view of an html document. |
|
491 | + * This should be an instance of EE_messenger but if you call this |
|
492 | + * method |
|
493 | + * intending it to be a sending messenger but a valid one could not be |
|
494 | + * retrieved then send in an instance of EE_Error that contains the |
|
495 | + * related error message. |
|
496 | + * @param bool|int $by_priority When set, this indicates that only messages |
|
497 | + * matching the given priority should be executed. |
|
498 | + * @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
|
499 | + * Also, if the messenger is an request type messenger (or a preview), |
|
500 | + * its entirely possible that the messenger will exit before |
|
501 | + */ |
|
502 | + public function execute($save = true, $sending_messenger = null, $by_priority = false) |
|
503 | + { |
|
504 | + $messages_sent = 0; |
|
505 | + $this->_did_hook = array(); |
|
506 | + $this->_message_repository->rewind(); |
|
507 | + |
|
508 | + while ($this->_message_repository->valid()) { |
|
509 | + $error_messages = array(); |
|
510 | + /** @type EE_Message $message */ |
|
511 | + $message = $this->_message_repository->current(); |
|
512 | + // only process things that are queued for sending |
|
513 | + if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
514 | + $this->_message_repository->next(); |
|
515 | + continue; |
|
516 | + } |
|
517 | + // if $by_priority is set and does not match then continue; |
|
518 | + if ($by_priority && $by_priority != $message->priority()) { |
|
519 | + $this->_message_repository->next(); |
|
520 | + continue; |
|
521 | + } |
|
522 | + // error checking |
|
523 | + if (! $message->valid_messenger()) { |
|
524 | + $error_messages[] = sprintf( |
|
525 | + __('The %s messenger is not active at time of sending.', 'event_espresso'), |
|
526 | + $message->messenger() |
|
527 | + ); |
|
528 | + } |
|
529 | + if (! $message->valid_message_type()) { |
|
530 | + $error_messages[] = sprintf( |
|
531 | + __('The %s message type is not active at the time of sending.', 'event_espresso'), |
|
532 | + $message->message_type() |
|
533 | + ); |
|
534 | + } |
|
535 | + // if there was supposed to be a sending messenger for this message, but it was invalid/inactive, |
|
536 | + // then it will instead be an EE_Error object, so let's check for that |
|
537 | + if ($sending_messenger instanceof EE_Error) { |
|
538 | + $error_messages[] = $sending_messenger->getMessage(); |
|
539 | + } |
|
540 | + // if there are no errors, then let's process the message |
|
541 | + if (empty($error_messages)) { |
|
542 | + if ($save) { |
|
543 | + $message->set_messenger_is_executing(); |
|
544 | + } |
|
545 | + if ($this->_process_message($message, $sending_messenger)) { |
|
546 | + $messages_sent++; |
|
547 | + } |
|
548 | + } |
|
549 | + $this->_set_error_message($message, $error_messages); |
|
550 | + // add modified time |
|
551 | + $message->set_modified(time()); |
|
552 | + // we save each message after its processed to make sure its status persists in case PHP times-out or runs |
|
553 | + // out of memory. @see https://events.codebasehq.com/projects/event-espresso/tickets/10281 |
|
554 | + if ($save) { |
|
555 | + $message->save(); |
|
556 | + } |
|
557 | + |
|
558 | + $this->_message_repository->next(); |
|
559 | + } |
|
560 | + if ($save) { |
|
561 | + $this->save(true); |
|
562 | + } |
|
563 | + return $messages_sent; |
|
564 | + } |
|
565 | + |
|
566 | + |
|
567 | + /** |
|
568 | + * _process_message |
|
569 | + * |
|
570 | + * @param EE_Message $message |
|
571 | + * @param mixed $sending_messenger (optional) |
|
572 | + * @return bool |
|
573 | + */ |
|
574 | + protected function _process_message(EE_Message $message, $sending_messenger = null) |
|
575 | + { |
|
576 | + // these *should* have been validated in the execute() method above |
|
577 | + $messenger = $message->messenger_object(); |
|
578 | + $message_type = $message->message_type_object(); |
|
579 | + // do actions for sending messenger if it differs from generating messenger and swap values. |
|
580 | + if ( |
|
581 | + $sending_messenger instanceof EE_messenger |
|
582 | + && $messenger instanceof EE_messenger |
|
583 | + && $sending_messenger->name != $messenger->name |
|
584 | + ) { |
|
585 | + $messenger->do_secondary_messenger_hooks($sending_messenger->name); |
|
586 | + $messenger = $sending_messenger; |
|
587 | + } |
|
588 | + // send using messenger, but double check objects |
|
589 | + if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
|
590 | + // set hook for message type (but only if not using another messenger to send). |
|
591 | + if (! isset($this->_did_hook[ $message_type->name ])) { |
|
592 | + $message_type->do_messenger_hooks($messenger); |
|
593 | + $this->_did_hook[ $message_type->name ] = 1; |
|
594 | + } |
|
595 | + // if preview then use preview method |
|
596 | + return $this->_message_repository->is_preview() |
|
597 | + ? $this->_do_preview($message, $messenger, $message_type, $this->_message_repository->is_test_send()) |
|
598 | + : $this->_do_send($message, $messenger, $message_type); |
|
599 | + } |
|
600 | + return false; |
|
601 | + } |
|
602 | + |
|
603 | + |
|
604 | + /** |
|
605 | + * The intention of this method is to count how many EE_Message objects |
|
606 | + * are in the queue with a given status. |
|
607 | + * Example usage: |
|
608 | + * After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed |
|
609 | + * sends by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
|
610 | + * |
|
611 | + * @param array|string $status Stati to check for in queue |
|
612 | + * @return int Count of EE_Message's matching the given status. |
|
613 | + */ |
|
614 | + public function count_STS_in_queue($status) |
|
615 | + { |
|
616 | + $count = 0; |
|
617 | + $status = is_array($status) ? $status : array($status); |
|
618 | + $this->_message_repository->rewind(); |
|
619 | + foreach ($this->_message_repository as $message) { |
|
620 | + if (in_array($message->STS_ID(), $status)) { |
|
621 | + $count++; |
|
622 | + } |
|
623 | + } |
|
624 | + return $count; |
|
625 | + } |
|
626 | + |
|
627 | + |
|
628 | + /** |
|
629 | + * Executes the get_preview method on the provided messenger. |
|
630 | + * |
|
631 | + * @param EE_Message $message |
|
632 | + * @param EE_messenger $messenger |
|
633 | + * @param EE_message_type $message_type |
|
634 | + * @param $test_send |
|
635 | + * @return bool true means all went well, false means, not so much. |
|
636 | + */ |
|
637 | + protected function _do_preview( |
|
638 | + EE_Message $message, |
|
639 | + EE_messenger $messenger, |
|
640 | + EE_message_type $message_type, |
|
641 | + $test_send |
|
642 | + ) { |
|
643 | + if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
|
644 | + if (! $test_send) { |
|
645 | + $message->set_content($preview); |
|
646 | + } |
|
647 | + $message->set_STS_ID(EEM_Message::status_sent); |
|
648 | + return true; |
|
649 | + } else { |
|
650 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
651 | + return false; |
|
652 | + } |
|
653 | + } |
|
654 | + |
|
655 | + |
|
656 | + /** |
|
657 | + * Executes the send method on the provided messenger |
|
658 | + * EE_Messengers are expected to: |
|
659 | + * - return true if the send was successful. |
|
660 | + * - return false if the send was unsuccessful but can be tried again. |
|
661 | + * - throw an Exception if the send was unsuccessful and cannot be tried again. |
|
662 | + * |
|
663 | + * @param EE_Message $message |
|
664 | + * @param EE_messenger $messenger |
|
665 | + * @param EE_message_type $message_type |
|
666 | + * @return bool true means all went well, false means, not so much. |
|
667 | + */ |
|
668 | + protected function _do_send(EE_Message $message, EE_messenger $messenger, EE_message_type $message_type) |
|
669 | + { |
|
670 | + try { |
|
671 | + if ($messenger->send_message($message, $message_type)) { |
|
672 | + $message->set_STS_ID(EEM_Message::status_sent); |
|
673 | + return true; |
|
674 | + } else { |
|
675 | + $message->set_STS_ID(EEM_Message::status_retry); |
|
676 | + return false; |
|
677 | + } |
|
678 | + } catch (SendMessageException $e) { |
|
679 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
680 | + $message->set_error_message($e->getMessage()); |
|
681 | + return false; |
|
682 | + } |
|
683 | + } |
|
684 | + |
|
685 | + |
|
686 | + /** |
|
687 | + * This sets any necessary error messages on the message object and its status to failed. |
|
688 | + * |
|
689 | + * @param EE_Message $message |
|
690 | + * @param array $error_messages the response from the messenger. |
|
691 | + */ |
|
692 | + protected function _set_error_message(EE_Message $message, $error_messages) |
|
693 | + { |
|
694 | + $error_messages = (array) $error_messages; |
|
695 | + if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_failed_sending())) { |
|
696 | + $notices = EE_Error::has_notices(); |
|
697 | + $error_messages[] = __( |
|
698 | + 'Messenger and Message Type were valid and active, but the messenger send method failed.', |
|
699 | + 'event_espresso' |
|
700 | + ); |
|
701 | + if ($notices === 1) { |
|
702 | + $notices = EE_Error::get_vanilla_notices(); |
|
703 | + $notices['errors'] = isset($notices['errors']) ? $notices['errors'] : array(); |
|
704 | + $error_messages[] = implode("\n", $notices['errors']); |
|
705 | + } |
|
706 | + } |
|
707 | + if (count($error_messages) > 0) { |
|
708 | + $msg = __('Message was not executed successfully.', 'event_espresso'); |
|
709 | + $msg = $msg . "\n" . implode("\n", $error_messages); |
|
710 | + $message->set_error_message($msg); |
|
711 | + } |
|
712 | + } |
|
713 | 713 | } |
@@ -16,27 +16,27 @@ |
||
16 | 16 | class InvalidAliasException extends DomainException |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * InvalidClassException constructor. |
|
21 | - * |
|
22 | - * @param string $fqcn |
|
23 | - * @param string $alias |
|
24 | - * @param string $message |
|
25 | - * @param int $code |
|
26 | - * @param Exception $previous |
|
27 | - */ |
|
28 | - public function __construct($fqcn, $alias, $message = '', $code = 0, Exception $previous = null) |
|
29 | - { |
|
30 | - if (empty($message)) { |
|
31 | - $message = sprintf( |
|
32 | - __( |
|
33 | - '"%1$s" can not be used as an alias because the "%2$s" class does not extend or implement it.', |
|
34 | - 'event_espresso' |
|
35 | - ), |
|
36 | - $alias, |
|
37 | - $fqcn |
|
38 | - ); |
|
39 | - } |
|
40 | - parent::__construct($message, $code, $previous); |
|
41 | - } |
|
19 | + /** |
|
20 | + * InvalidClassException constructor. |
|
21 | + * |
|
22 | + * @param string $fqcn |
|
23 | + * @param string $alias |
|
24 | + * @param string $message |
|
25 | + * @param int $code |
|
26 | + * @param Exception $previous |
|
27 | + */ |
|
28 | + public function __construct($fqcn, $alias, $message = '', $code = 0, Exception $previous = null) |
|
29 | + { |
|
30 | + if (empty($message)) { |
|
31 | + $message = sprintf( |
|
32 | + __( |
|
33 | + '"%1$s" can not be used as an alias because the "%2$s" class does not extend or implement it.', |
|
34 | + 'event_espresso' |
|
35 | + ), |
|
36 | + $alias, |
|
37 | + $fqcn |
|
38 | + ); |
|
39 | + } |
|
40 | + parent::__construct($message, $code, $previous); |
|
41 | + } |
|
42 | 42 | } |
@@ -34,7 +34,7 @@ |
||
34 | 34 | */ |
35 | 35 | public function __construct($alias, $fqcn) |
36 | 36 | { |
37 | - if (! is_subclass_of($fqcn, $alias)) { |
|
37 | + if ( ! is_subclass_of($fqcn, $alias)) { |
|
38 | 38 | throw new InvalidAliasException($fqcn, $alias); |
39 | 39 | } |
40 | 40 | $this->alias = $alias; |
@@ -17,46 +17,46 @@ |
||
17 | 17 | class ClassAlias |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var string $alias an interface or base class representing what object |
|
22 | - * can be utilized by another object and/or function |
|
23 | - */ |
|
24 | - private $alias; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var string $fqcn the actual class that should be substituted for the alias above |
|
28 | - */ |
|
29 | - private $fqcn; |
|
30 | - |
|
31 | - /** |
|
32 | - * ClassAlias constructor. |
|
33 | - * |
|
34 | - * @param string $alias Interface specified by implementing class |
|
35 | - * @param string $fqcn Concrete class that satisfies interface |
|
36 | - * @throws InvalidAliasException |
|
37 | - */ |
|
38 | - public function __construct($alias, $fqcn) |
|
39 | - { |
|
40 | - if (! is_subclass_of($fqcn, $alias)) { |
|
41 | - throw new InvalidAliasException($fqcn, $alias); |
|
42 | - } |
|
43 | - $this->alias = $alias; |
|
44 | - $this->fqcn = $fqcn; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - public function alias() |
|
51 | - { |
|
52 | - return $this->alias; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - public function fqcn() |
|
59 | - { |
|
60 | - return $this->fqcn; |
|
61 | - } |
|
20 | + /** |
|
21 | + * @var string $alias an interface or base class representing what object |
|
22 | + * can be utilized by another object and/or function |
|
23 | + */ |
|
24 | + private $alias; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var string $fqcn the actual class that should be substituted for the alias above |
|
28 | + */ |
|
29 | + private $fqcn; |
|
30 | + |
|
31 | + /** |
|
32 | + * ClassAlias constructor. |
|
33 | + * |
|
34 | + * @param string $alias Interface specified by implementing class |
|
35 | + * @param string $fqcn Concrete class that satisfies interface |
|
36 | + * @throws InvalidAliasException |
|
37 | + */ |
|
38 | + public function __construct($alias, $fqcn) |
|
39 | + { |
|
40 | + if (! is_subclass_of($fqcn, $alias)) { |
|
41 | + throw new InvalidAliasException($fqcn, $alias); |
|
42 | + } |
|
43 | + $this->alias = $alias; |
|
44 | + $this->fqcn = $fqcn; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + public function alias() |
|
51 | + { |
|
52 | + return $this->alias; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + public function fqcn() |
|
59 | + { |
|
60 | + return $this->fqcn; |
|
61 | + } |
|
62 | 62 | } |
@@ -18,41 +18,41 @@ |
||
18 | 18 | */ |
19 | 19 | abstract class FactoryWithDependencyResolver implements FactoryInterface |
20 | 20 | { |
21 | - /** |
|
22 | - * @var DependencyResolverInterface $dependency_resolver |
|
23 | - */ |
|
24 | - private $dependency_resolver; |
|
21 | + /** |
|
22 | + * @var DependencyResolverInterface $dependency_resolver |
|
23 | + */ |
|
24 | + private $dependency_resolver; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @var LoaderInterface $loader |
|
28 | - */ |
|
29 | - private $loader; |
|
26 | + /** |
|
27 | + * @var LoaderInterface $loader |
|
28 | + */ |
|
29 | + private $loader; |
|
30 | 30 | |
31 | - /** |
|
32 | - * FactoryWithDependencyResolver constructor. |
|
33 | - * |
|
34 | - * @param DependencyResolverInterface $dependency_resolver |
|
35 | - * @param LoaderInterface $loader |
|
36 | - */ |
|
37 | - public function __construct(DependencyResolverInterface $dependency_resolver, LoaderInterface $loader) |
|
38 | - { |
|
39 | - $this->dependency_resolver = $dependency_resolver; |
|
40 | - $this->loader = $loader; |
|
41 | - } |
|
31 | + /** |
|
32 | + * FactoryWithDependencyResolver constructor. |
|
33 | + * |
|
34 | + * @param DependencyResolverInterface $dependency_resolver |
|
35 | + * @param LoaderInterface $loader |
|
36 | + */ |
|
37 | + public function __construct(DependencyResolverInterface $dependency_resolver, LoaderInterface $loader) |
|
38 | + { |
|
39 | + $this->dependency_resolver = $dependency_resolver; |
|
40 | + $this->loader = $loader; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @return DependencyResolverInterface |
|
45 | - */ |
|
46 | - public function dependencyResolver() |
|
47 | - { |
|
48 | - return $this->dependency_resolver; |
|
49 | - } |
|
43 | + /** |
|
44 | + * @return DependencyResolverInterface |
|
45 | + */ |
|
46 | + public function dependencyResolver() |
|
47 | + { |
|
48 | + return $this->dependency_resolver; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @return LoaderInterface |
|
53 | - */ |
|
54 | - public function loader() |
|
55 | - { |
|
56 | - return $this->loader; |
|
57 | - } |
|
51 | + /** |
|
52 | + * @return LoaderInterface |
|
53 | + */ |
|
54 | + public function loader() |
|
55 | + { |
|
56 | + return $this->loader; |
|
57 | + } |
|
58 | 58 | } |
@@ -11,10 +11,10 @@ |
||
11 | 11 | interface FactoryInterface |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * @param mixed $arguments Either a Fully Qualified Class Name |
|
16 | - * or array of data required for construction |
|
17 | - * @return mixed |
|
18 | - */ |
|
19 | - public static function create($arguments); |
|
14 | + /** |
|
15 | + * @param mixed $arguments Either a Fully Qualified Class Name |
|
16 | + * or array of data required for construction |
|
17 | + * @return mixed |
|
18 | + */ |
|
19 | + public static function create($arguments); |
|
20 | 20 | } |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | foreach ($this->blocks as $block) { |
146 | 146 | $block->initialize(); |
147 | 147 | $this->trackAssetManagersForBlocks($block); |
148 | - if (! $this->block_asset_manager_collection->has($block->assetManager())) { |
|
148 | + if ( ! $this->block_asset_manager_collection->has($block->assetManager())) { |
|
149 | 149 | $this->block_asset_manager_collection->add($block->assetManager()); |
150 | 150 | $block->assetManager()->setAssetHandles(); |
151 | 151 | } |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | $supported_routes = $block->supportedRoutes(); |
168 | 168 | foreach ($supported_routes as $supported_route) { |
169 | 169 | if ($this->route_manager->routeMatchesCurrentRequest($supported_route)) { |
170 | - $this->block_asset_managers[ $block->blockType() ] = $block->assetManager()->assetNamespace(); |
|
170 | + $this->block_asset_managers[$block->blockType()] = $block->assetManager()->assetNamespace(); |
|
171 | 171 | } |
172 | 172 | } |
173 | 173 | } |
@@ -183,10 +183,10 @@ discard block |
||
183 | 183 | */ |
184 | 184 | public function matchesRoute(BlockInterface $block) |
185 | 185 | { |
186 | - if (isset($this->block_asset_managers[ $block->blockType() ])) { |
|
186 | + if (isset($this->block_asset_managers[$block->blockType()])) { |
|
187 | 187 | return true; |
188 | 188 | } |
189 | - unset($this->block_asset_managers[ $block->blockType() ]); |
|
189 | + unset($this->block_asset_managers[$block->blockType()]); |
|
190 | 190 | return false; |
191 | 191 | } |
192 | 192 | |
@@ -202,12 +202,12 @@ discard block |
||
202 | 202 | try { |
203 | 203 | // cycle thru block loader folders |
204 | 204 | foreach ($this->blocks as $block) { |
205 | - if (! $this->matchesRoute($block)) { |
|
205 | + if ( ! $this->matchesRoute($block)) { |
|
206 | 206 | continue; |
207 | 207 | } |
208 | 208 | // perform any setup required for the block |
209 | 209 | $block_type = $block->registerBlock(); |
210 | - if (! $block_type instanceof WP_Block_Type) { |
|
210 | + if ( ! $block_type instanceof WP_Block_Type) { |
|
211 | 211 | throw new InvalidEntityException($block_type, 'WP_Block_Type'); |
212 | 212 | } |
213 | 213 | do_action( |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | $assets = array_flip($this->block_asset_managers); |
227 | 227 | foreach ($this->block_asset_manager_collection as $asset_manager) { |
228 | 228 | // if there are no longer any blocks that require these assets, |
229 | - if (! isset($assets[ $asset_manager->assetNamespace() ])) { |
|
229 | + if ( ! isset($assets[$asset_manager->assetNamespace()])) { |
|
230 | 230 | // then unset asset enqueueing and bail |
231 | 231 | remove_action('wp_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
232 | 232 | remove_action('admin_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
@@ -32,223 +32,223 @@ |
||
32 | 32 | class BlockRegistrationManager extends BlockManager |
33 | 33 | { |
34 | 34 | |
35 | - /** |
|
36 | - * @var BlockAssetManagerCollection $block_asset_manager_collection |
|
37 | - */ |
|
38 | - protected $block_asset_manager_collection; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var RouteMatchSpecificationManager $route_manager |
|
42 | - */ |
|
43 | - protected $route_manager; |
|
44 | - |
|
45 | - /** |
|
46 | - * array for tracking asset managers required by blocks for the current route |
|
47 | - * |
|
48 | - * @var array $block_asset_managers |
|
49 | - */ |
|
50 | - protected $block_asset_managers = array(); |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * BlockRegistrationManager constructor. |
|
55 | - * |
|
56 | - * @param BlockAssetManagerCollection $block_asset_manager_collection |
|
57 | - * @param BlockCollection $blocks |
|
58 | - * @param RouteMatchSpecificationManager $route_manager |
|
59 | - * @param RequestInterface $request |
|
60 | - */ |
|
61 | - public function __construct( |
|
62 | - BlockAssetManagerCollection $block_asset_manager_collection, |
|
63 | - BlockCollection $blocks, |
|
64 | - RouteMatchSpecificationManager $route_manager, |
|
65 | - RequestInterface $request |
|
66 | - ) { |
|
67 | - $this->block_asset_manager_collection = $block_asset_manager_collection; |
|
68 | - $this->route_manager = $route_manager; |
|
69 | - parent::__construct($blocks, $request); |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - /** |
|
74 | - * Returns the name of a hook point to be used to call initialize() |
|
75 | - * |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - public function initHook() |
|
79 | - { |
|
80 | - return 'AHEE__EE_System__initialize'; |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Perform any early setup required for block editors to functions |
|
86 | - * |
|
87 | - * @return void |
|
88 | - * @throws Exception |
|
89 | - */ |
|
90 | - public function initialize() |
|
91 | - { |
|
92 | - $this->initializeBlocks(); |
|
93 | - add_action('AHEE__EE_System__initialize_last', array($this, 'registerBlocks')); |
|
94 | - add_action('wp_loaded', array($this, 'unloadAssets')); |
|
95 | - add_filter('block_categories', array($this, 'addEspressoBlockCategories')); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * @param array $categories |
|
101 | - * @since 4.9.71.p |
|
102 | - * @return array |
|
103 | - */ |
|
104 | - public function addEspressoBlockCategories(array $categories) |
|
105 | - { |
|
106 | - return array_merge( |
|
107 | - $categories, |
|
108 | - array( |
|
109 | - array( |
|
110 | - 'slug' => 'event-espresso', |
|
111 | - 'title' => esc_html__('Event Espresso', 'event_espresso'), |
|
112 | - ), |
|
113 | - ) |
|
114 | - ); |
|
115 | - } |
|
116 | - |
|
117 | - |
|
118 | - /** |
|
119 | - * @return CollectionInterface|BlockInterface[] |
|
120 | - * @throws CollectionLoaderException |
|
121 | - * @throws CollectionDetailsException |
|
122 | - */ |
|
123 | - protected function populateBlockCollection() |
|
124 | - { |
|
125 | - $loader = new CollectionLoader( |
|
126 | - new CollectionDetails( |
|
127 | - // collection name |
|
128 | - 'editor_blocks', |
|
129 | - // collection interface |
|
130 | - 'EventEspresso\core\domain\entities\editor\BlockInterface', |
|
131 | - // FQCNs for classes to add (all classes within each namespace will be loaded) |
|
132 | - apply_filters( |
|
133 | - 'FHEE__EventEspresso_core_services_editor_BlockManager__populateBlockCollection__collection_FQCNs', |
|
134 | - array('EventEspresso\core\domain\entities\editor\blocks') |
|
135 | - ), |
|
136 | - // file paths to classes to add |
|
137 | - array(), |
|
138 | - // file mask to use if parsing folder for files to add |
|
139 | - '', |
|
140 | - // what to use as identifier for collection entities |
|
141 | - // using CLASS NAME prevents duplicates (works like a singleton) |
|
142 | - CollectionDetails::ID_CLASS_NAME |
|
143 | - ), |
|
144 | - $this->blocks |
|
145 | - ); |
|
146 | - return $loader->getCollection(); |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * populates the BlockCollection and calls initialize() on all installed blocks |
|
152 | - * |
|
153 | - * @return void |
|
154 | - * @throws Exception |
|
155 | - */ |
|
156 | - public function initializeBlocks() |
|
157 | - { |
|
158 | - try { |
|
159 | - $this->populateBlockCollection(); |
|
160 | - // cycle thru block loaders and initialize each loader |
|
161 | - foreach ($this->blocks as $block) { |
|
162 | - $block->initialize(); |
|
163 | - $this->trackAssetManagersForBlocks($block); |
|
164 | - if (! $this->block_asset_manager_collection->has($block->assetManager())) { |
|
165 | - $this->block_asset_manager_collection->add($block->assetManager()); |
|
166 | - $block->assetManager()->setAssetHandles(); |
|
167 | - } |
|
168 | - } |
|
169 | - } catch (Exception $exception) { |
|
170 | - new ExceptionStackTraceDisplay($exception); |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - /** |
|
176 | - * track blocks with routes that match the current request |
|
177 | - * |
|
178 | - * @param BlockInterface $block |
|
179 | - * @throws InvalidClassException |
|
180 | - */ |
|
181 | - private function trackAssetManagersForBlocks(BlockInterface $block) |
|
182 | - { |
|
183 | - $supported_routes = $block->supportedRoutes(); |
|
184 | - foreach ($supported_routes as $supported_route) { |
|
185 | - if ($this->route_manager->routeMatchesCurrentRequest($supported_route)) { |
|
186 | - $this->block_asset_managers[ $block->blockType() ] = $block->assetManager()->assetNamespace(); |
|
187 | - } |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - |
|
192 | - /** |
|
193 | - * returns true if the block should be registered for the current request |
|
194 | - * else removes block from block_routes array and returns false |
|
195 | - * |
|
196 | - * @param BlockInterface $block |
|
197 | - * @return boolean |
|
198 | - * @throws InvalidClassException |
|
199 | - */ |
|
200 | - public function matchesRoute(BlockInterface $block) |
|
201 | - { |
|
202 | - if (isset($this->block_asset_managers[ $block->blockType() ])) { |
|
203 | - return true; |
|
204 | - } |
|
205 | - unset($this->block_asset_managers[ $block->blockType() ]); |
|
206 | - return false; |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - /** |
|
211 | - * calls registerBlock() and load assets for all installed blocks |
|
212 | - * |
|
213 | - * @return void |
|
214 | - * @throws Exception |
|
215 | - */ |
|
216 | - public function registerBlocks() |
|
217 | - { |
|
218 | - try { |
|
219 | - // cycle thru block loader folders |
|
220 | - foreach ($this->blocks as $block) { |
|
221 | - if (! $this->matchesRoute($block)) { |
|
222 | - continue; |
|
223 | - } |
|
224 | - // perform any setup required for the block |
|
225 | - $block_type = $block->registerBlock(); |
|
226 | - if (! $block_type instanceof WP_Block_Type) { |
|
227 | - throw new InvalidEntityException($block_type, 'WP_Block_Type'); |
|
228 | - } |
|
229 | - do_action( |
|
230 | - 'FHEE__EventEspresso_core_services_editor_BlockManager__registerBlocks__block_type_registered', |
|
231 | - $block, |
|
232 | - $block_type |
|
233 | - ); |
|
234 | - } |
|
235 | - } catch (Exception $exception) { |
|
236 | - new ExceptionStackTraceDisplay($exception); |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - public function unloadAssets() |
|
241 | - { |
|
242 | - $assets = array_flip($this->block_asset_managers); |
|
243 | - foreach ($this->block_asset_manager_collection as $asset_manager) { |
|
244 | - // if there are no longer any blocks that require these assets, |
|
245 | - if (! isset($assets[ $asset_manager->assetNamespace() ])) { |
|
246 | - // then unset asset enqueueing and bail |
|
247 | - remove_action('wp_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
|
248 | - remove_action('admin_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
|
249 | - remove_action('wp_enqueue_scripts', array($asset_manager, 'addAssets'), 2); |
|
250 | - remove_action('admin_enqueue_scripts', array($asset_manager, 'addAssets'), 2); |
|
251 | - } |
|
252 | - } |
|
253 | - } |
|
35 | + /** |
|
36 | + * @var BlockAssetManagerCollection $block_asset_manager_collection |
|
37 | + */ |
|
38 | + protected $block_asset_manager_collection; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var RouteMatchSpecificationManager $route_manager |
|
42 | + */ |
|
43 | + protected $route_manager; |
|
44 | + |
|
45 | + /** |
|
46 | + * array for tracking asset managers required by blocks for the current route |
|
47 | + * |
|
48 | + * @var array $block_asset_managers |
|
49 | + */ |
|
50 | + protected $block_asset_managers = array(); |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * BlockRegistrationManager constructor. |
|
55 | + * |
|
56 | + * @param BlockAssetManagerCollection $block_asset_manager_collection |
|
57 | + * @param BlockCollection $blocks |
|
58 | + * @param RouteMatchSpecificationManager $route_manager |
|
59 | + * @param RequestInterface $request |
|
60 | + */ |
|
61 | + public function __construct( |
|
62 | + BlockAssetManagerCollection $block_asset_manager_collection, |
|
63 | + BlockCollection $blocks, |
|
64 | + RouteMatchSpecificationManager $route_manager, |
|
65 | + RequestInterface $request |
|
66 | + ) { |
|
67 | + $this->block_asset_manager_collection = $block_asset_manager_collection; |
|
68 | + $this->route_manager = $route_manager; |
|
69 | + parent::__construct($blocks, $request); |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + /** |
|
74 | + * Returns the name of a hook point to be used to call initialize() |
|
75 | + * |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + public function initHook() |
|
79 | + { |
|
80 | + return 'AHEE__EE_System__initialize'; |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Perform any early setup required for block editors to functions |
|
86 | + * |
|
87 | + * @return void |
|
88 | + * @throws Exception |
|
89 | + */ |
|
90 | + public function initialize() |
|
91 | + { |
|
92 | + $this->initializeBlocks(); |
|
93 | + add_action('AHEE__EE_System__initialize_last', array($this, 'registerBlocks')); |
|
94 | + add_action('wp_loaded', array($this, 'unloadAssets')); |
|
95 | + add_filter('block_categories', array($this, 'addEspressoBlockCategories')); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * @param array $categories |
|
101 | + * @since 4.9.71.p |
|
102 | + * @return array |
|
103 | + */ |
|
104 | + public function addEspressoBlockCategories(array $categories) |
|
105 | + { |
|
106 | + return array_merge( |
|
107 | + $categories, |
|
108 | + array( |
|
109 | + array( |
|
110 | + 'slug' => 'event-espresso', |
|
111 | + 'title' => esc_html__('Event Espresso', 'event_espresso'), |
|
112 | + ), |
|
113 | + ) |
|
114 | + ); |
|
115 | + } |
|
116 | + |
|
117 | + |
|
118 | + /** |
|
119 | + * @return CollectionInterface|BlockInterface[] |
|
120 | + * @throws CollectionLoaderException |
|
121 | + * @throws CollectionDetailsException |
|
122 | + */ |
|
123 | + protected function populateBlockCollection() |
|
124 | + { |
|
125 | + $loader = new CollectionLoader( |
|
126 | + new CollectionDetails( |
|
127 | + // collection name |
|
128 | + 'editor_blocks', |
|
129 | + // collection interface |
|
130 | + 'EventEspresso\core\domain\entities\editor\BlockInterface', |
|
131 | + // FQCNs for classes to add (all classes within each namespace will be loaded) |
|
132 | + apply_filters( |
|
133 | + 'FHEE__EventEspresso_core_services_editor_BlockManager__populateBlockCollection__collection_FQCNs', |
|
134 | + array('EventEspresso\core\domain\entities\editor\blocks') |
|
135 | + ), |
|
136 | + // file paths to classes to add |
|
137 | + array(), |
|
138 | + // file mask to use if parsing folder for files to add |
|
139 | + '', |
|
140 | + // what to use as identifier for collection entities |
|
141 | + // using CLASS NAME prevents duplicates (works like a singleton) |
|
142 | + CollectionDetails::ID_CLASS_NAME |
|
143 | + ), |
|
144 | + $this->blocks |
|
145 | + ); |
|
146 | + return $loader->getCollection(); |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * populates the BlockCollection and calls initialize() on all installed blocks |
|
152 | + * |
|
153 | + * @return void |
|
154 | + * @throws Exception |
|
155 | + */ |
|
156 | + public function initializeBlocks() |
|
157 | + { |
|
158 | + try { |
|
159 | + $this->populateBlockCollection(); |
|
160 | + // cycle thru block loaders and initialize each loader |
|
161 | + foreach ($this->blocks as $block) { |
|
162 | + $block->initialize(); |
|
163 | + $this->trackAssetManagersForBlocks($block); |
|
164 | + if (! $this->block_asset_manager_collection->has($block->assetManager())) { |
|
165 | + $this->block_asset_manager_collection->add($block->assetManager()); |
|
166 | + $block->assetManager()->setAssetHandles(); |
|
167 | + } |
|
168 | + } |
|
169 | + } catch (Exception $exception) { |
|
170 | + new ExceptionStackTraceDisplay($exception); |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + /** |
|
176 | + * track blocks with routes that match the current request |
|
177 | + * |
|
178 | + * @param BlockInterface $block |
|
179 | + * @throws InvalidClassException |
|
180 | + */ |
|
181 | + private function trackAssetManagersForBlocks(BlockInterface $block) |
|
182 | + { |
|
183 | + $supported_routes = $block->supportedRoutes(); |
|
184 | + foreach ($supported_routes as $supported_route) { |
|
185 | + if ($this->route_manager->routeMatchesCurrentRequest($supported_route)) { |
|
186 | + $this->block_asset_managers[ $block->blockType() ] = $block->assetManager()->assetNamespace(); |
|
187 | + } |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + |
|
192 | + /** |
|
193 | + * returns true if the block should be registered for the current request |
|
194 | + * else removes block from block_routes array and returns false |
|
195 | + * |
|
196 | + * @param BlockInterface $block |
|
197 | + * @return boolean |
|
198 | + * @throws InvalidClassException |
|
199 | + */ |
|
200 | + public function matchesRoute(BlockInterface $block) |
|
201 | + { |
|
202 | + if (isset($this->block_asset_managers[ $block->blockType() ])) { |
|
203 | + return true; |
|
204 | + } |
|
205 | + unset($this->block_asset_managers[ $block->blockType() ]); |
|
206 | + return false; |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + /** |
|
211 | + * calls registerBlock() and load assets for all installed blocks |
|
212 | + * |
|
213 | + * @return void |
|
214 | + * @throws Exception |
|
215 | + */ |
|
216 | + public function registerBlocks() |
|
217 | + { |
|
218 | + try { |
|
219 | + // cycle thru block loader folders |
|
220 | + foreach ($this->blocks as $block) { |
|
221 | + if (! $this->matchesRoute($block)) { |
|
222 | + continue; |
|
223 | + } |
|
224 | + // perform any setup required for the block |
|
225 | + $block_type = $block->registerBlock(); |
|
226 | + if (! $block_type instanceof WP_Block_Type) { |
|
227 | + throw new InvalidEntityException($block_type, 'WP_Block_Type'); |
|
228 | + } |
|
229 | + do_action( |
|
230 | + 'FHEE__EventEspresso_core_services_editor_BlockManager__registerBlocks__block_type_registered', |
|
231 | + $block, |
|
232 | + $block_type |
|
233 | + ); |
|
234 | + } |
|
235 | + } catch (Exception $exception) { |
|
236 | + new ExceptionStackTraceDisplay($exception); |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + public function unloadAssets() |
|
241 | + { |
|
242 | + $assets = array_flip($this->block_asset_managers); |
|
243 | + foreach ($this->block_asset_manager_collection as $asset_manager) { |
|
244 | + // if there are no longer any blocks that require these assets, |
|
245 | + if (! isset($assets[ $asset_manager->assetNamespace() ])) { |
|
246 | + // then unset asset enqueueing and bail |
|
247 | + remove_action('wp_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
|
248 | + remove_action('admin_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
|
249 | + remove_action('wp_enqueue_scripts', array($asset_manager, 'addAssets'), 2); |
|
250 | + remove_action('admin_enqueue_scripts', array($asset_manager, 'addAssets'), 2); |
|
251 | + } |
|
252 | + } |
|
253 | + } |
|
254 | 254 | } |
@@ -16,131 +16,131 @@ |
||
16 | 16 | */ |
17 | 17 | class Config |
18 | 18 | { |
19 | - /** |
|
20 | - * @var EE_Network_Config |
|
21 | - */ |
|
22 | - private $network_config; |
|
23 | - |
|
24 | - |
|
25 | - /** |
|
26 | - * @var EE_Config |
|
27 | - */ |
|
28 | - private $ee_config; |
|
29 | - |
|
30 | - |
|
31 | - public function __construct(EE_Network_Config $network_config, EE_Config $ee_config) |
|
32 | - { |
|
33 | - $this->network_config = $network_config; |
|
34 | - $this->ee_config = $ee_config; |
|
35 | - } |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * Get the site license key for the site. |
|
40 | - */ |
|
41 | - public function siteLicenseKey() |
|
42 | - { |
|
43 | - return $this->network_config->core->site_license_key; |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - public function i18nDomain() |
|
48 | - { |
|
49 | - return 'event_espresso'; |
|
50 | - } |
|
51 | - |
|
52 | - |
|
53 | - public function checkPeriod() |
|
54 | - { |
|
55 | - return 24; |
|
56 | - } |
|
57 | - |
|
58 | - |
|
59 | - public function optionKey() |
|
60 | - { |
|
61 | - return 'ee_site_license_key'; |
|
62 | - } |
|
63 | - |
|
64 | - |
|
65 | - public function optionsPageSlug() |
|
66 | - { |
|
67 | - return 'espresso_general_settings'; |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - public function hostServerUrl() |
|
72 | - { |
|
73 | - return defined('PUE_UPDATES_ENDPOINT') |
|
74 | - ? PUE_UPDATES_ENDPOINT |
|
75 | - : 'https://eventespresso.com'; |
|
76 | - } |
|
77 | - |
|
78 | - |
|
79 | - public function pluginSlug() |
|
80 | - { |
|
81 | - // Note: PUE uses a simple preg_match to determine what type is currently installed based on version number. |
|
82 | - // So it's important that you use a key for the version type that is unique and not found in another key. |
|
83 | - // For example: |
|
84 | - // $plugin_slug['premium']['p'] = 'some-premium-slug'; |
|
85 | - // $plugin_slug['prerelease']['pr'] = 'some-pre-release-slug'; |
|
86 | - // The above would not work because "p" is found in both keys for the version type. ( i.e 1.0.p vs 1.0.pr ) |
|
87 | - // so doing something like: |
|
88 | - // $plugin_slug['premium']['p'] = 'some-premium-slug'; |
|
89 | - // $plugin_slug['prerelease']['b'] = 'some-pre-release-slug'; |
|
90 | - // ..WOULD work! |
|
91 | - return array( |
|
92 | - 'free' => array('decaf' => 'event-espresso-core-decaf'), |
|
93 | - 'premium' => array('p' => 'event-espresso-core-reg'), |
|
94 | - 'prerelease' => array('beta' => 'event-espresso-core-pr'), |
|
95 | - ); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * Return whether the site is opted in for UXIP or not. |
|
101 | - * |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public function isOptedInForUxip() |
|
105 | - { |
|
106 | - return filter_var($this->ee_config->core->ee_ueip_optin, FILTER_VALIDATE_BOOLEAN); |
|
107 | - } |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * Return whether the site has been notified about UXIP or not. |
|
112 | - * |
|
113 | - * @return bool |
|
114 | - */ |
|
115 | - public function hasNotifiedForUxip() |
|
116 | - { |
|
117 | - return filter_var($this->ee_config->core->ee_ueip_has_notified, FILTER_VALIDATE_BOOLEAN); |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * Set the site opted in for UXIP. |
|
123 | - */ |
|
124 | - public function setHasOptedInForUxip() |
|
125 | - { |
|
126 | - $this->ee_config->core->ee_ueip_optin = true; |
|
127 | - $this->ee_config->update_espresso_config(false, false); |
|
128 | - } |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * Set the site opted out for UXIP |
|
133 | - */ |
|
134 | - public function setHasOptedOutForUxip() |
|
135 | - { |
|
136 | - $this->ee_config->core->ee_ueip_optin = false; |
|
137 | - $this->ee_config->update_espresso_config(false, false); |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - public function setHasNotifiedAboutUxip() |
|
142 | - { |
|
143 | - $this->ee_config->core->ee_ueip_has_notified = true; |
|
144 | - $this->ee_config->update_espresso_config(false, false); |
|
145 | - } |
|
19 | + /** |
|
20 | + * @var EE_Network_Config |
|
21 | + */ |
|
22 | + private $network_config; |
|
23 | + |
|
24 | + |
|
25 | + /** |
|
26 | + * @var EE_Config |
|
27 | + */ |
|
28 | + private $ee_config; |
|
29 | + |
|
30 | + |
|
31 | + public function __construct(EE_Network_Config $network_config, EE_Config $ee_config) |
|
32 | + { |
|
33 | + $this->network_config = $network_config; |
|
34 | + $this->ee_config = $ee_config; |
|
35 | + } |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * Get the site license key for the site. |
|
40 | + */ |
|
41 | + public function siteLicenseKey() |
|
42 | + { |
|
43 | + return $this->network_config->core->site_license_key; |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + public function i18nDomain() |
|
48 | + { |
|
49 | + return 'event_espresso'; |
|
50 | + } |
|
51 | + |
|
52 | + |
|
53 | + public function checkPeriod() |
|
54 | + { |
|
55 | + return 24; |
|
56 | + } |
|
57 | + |
|
58 | + |
|
59 | + public function optionKey() |
|
60 | + { |
|
61 | + return 'ee_site_license_key'; |
|
62 | + } |
|
63 | + |
|
64 | + |
|
65 | + public function optionsPageSlug() |
|
66 | + { |
|
67 | + return 'espresso_general_settings'; |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + public function hostServerUrl() |
|
72 | + { |
|
73 | + return defined('PUE_UPDATES_ENDPOINT') |
|
74 | + ? PUE_UPDATES_ENDPOINT |
|
75 | + : 'https://eventespresso.com'; |
|
76 | + } |
|
77 | + |
|
78 | + |
|
79 | + public function pluginSlug() |
|
80 | + { |
|
81 | + // Note: PUE uses a simple preg_match to determine what type is currently installed based on version number. |
|
82 | + // So it's important that you use a key for the version type that is unique and not found in another key. |
|
83 | + // For example: |
|
84 | + // $plugin_slug['premium']['p'] = 'some-premium-slug'; |
|
85 | + // $plugin_slug['prerelease']['pr'] = 'some-pre-release-slug'; |
|
86 | + // The above would not work because "p" is found in both keys for the version type. ( i.e 1.0.p vs 1.0.pr ) |
|
87 | + // so doing something like: |
|
88 | + // $plugin_slug['premium']['p'] = 'some-premium-slug'; |
|
89 | + // $plugin_slug['prerelease']['b'] = 'some-pre-release-slug'; |
|
90 | + // ..WOULD work! |
|
91 | + return array( |
|
92 | + 'free' => array('decaf' => 'event-espresso-core-decaf'), |
|
93 | + 'premium' => array('p' => 'event-espresso-core-reg'), |
|
94 | + 'prerelease' => array('beta' => 'event-espresso-core-pr'), |
|
95 | + ); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * Return whether the site is opted in for UXIP or not. |
|
101 | + * |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public function isOptedInForUxip() |
|
105 | + { |
|
106 | + return filter_var($this->ee_config->core->ee_ueip_optin, FILTER_VALIDATE_BOOLEAN); |
|
107 | + } |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * Return whether the site has been notified about UXIP or not. |
|
112 | + * |
|
113 | + * @return bool |
|
114 | + */ |
|
115 | + public function hasNotifiedForUxip() |
|
116 | + { |
|
117 | + return filter_var($this->ee_config->core->ee_ueip_has_notified, FILTER_VALIDATE_BOOLEAN); |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * Set the site opted in for UXIP. |
|
123 | + */ |
|
124 | + public function setHasOptedInForUxip() |
|
125 | + { |
|
126 | + $this->ee_config->core->ee_ueip_optin = true; |
|
127 | + $this->ee_config->update_espresso_config(false, false); |
|
128 | + } |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * Set the site opted out for UXIP |
|
133 | + */ |
|
134 | + public function setHasOptedOutForUxip() |
|
135 | + { |
|
136 | + $this->ee_config->core->ee_ueip_optin = false; |
|
137 | + $this->ee_config->update_espresso_config(false, false); |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + public function setHasNotifiedAboutUxip() |
|
142 | + { |
|
143 | + $this->ee_config->core->ee_ueip_has_notified = true; |
|
144 | + $this->ee_config->update_espresso_config(false, false); |
|
145 | + } |
|
146 | 146 | } |
@@ -15,145 +15,145 @@ |
||
15 | 15 | class CustomTaxonomyDefinitions |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var array $taxonomies |
|
20 | - */ |
|
21 | - private $taxonomies; |
|
18 | + /** |
|
19 | + * @var array $taxonomies |
|
20 | + */ |
|
21 | + private $taxonomies; |
|
22 | 22 | |
23 | 23 | |
24 | - /** |
|
25 | - * EspressoCustomPostTypeDefinitions constructor. |
|
26 | - */ |
|
27 | - public function __construct() |
|
28 | - { |
|
29 | - $this->setTaxonomies(); |
|
30 | - add_filter('pre_term_description', array($this, 'filterCustomTermDescription'), 1, 2); |
|
31 | - } |
|
24 | + /** |
|
25 | + * EspressoCustomPostTypeDefinitions constructor. |
|
26 | + */ |
|
27 | + public function __construct() |
|
28 | + { |
|
29 | + $this->setTaxonomies(); |
|
30 | + add_filter('pre_term_description', array($this, 'filterCustomTermDescription'), 1, 2); |
|
31 | + } |
|
32 | 32 | |
33 | 33 | |
34 | - private function setTaxonomies() |
|
35 | - { |
|
36 | - $this->taxonomies = array( |
|
37 | - 'espresso_event_categories' => array( |
|
38 | - 'singular_name' => esc_html__('Event Category', 'event_espresso'), |
|
39 | - 'plural_name' => esc_html__('Event Categories', 'event_espresso'), |
|
40 | - 'args' => array( |
|
41 | - 'public' => true, |
|
42 | - 'show_in_nav_menus' => true, |
|
43 | - 'show_in_rest' => true, |
|
44 | - 'capabilities' => array( |
|
45 | - 'manage_terms' => 'ee_manage_event_categories', |
|
46 | - 'edit_terms' => 'ee_edit_event_category', |
|
47 | - 'delete_terms' => 'ee_delete_event_category', |
|
48 | - 'assign_terms' => 'ee_assign_event_category', |
|
49 | - ), |
|
50 | - 'rewrite' => array( |
|
51 | - 'slug' => EEH_URL::slugify( |
|
52 | - __('event-category', 'event_espresso'), |
|
53 | - 'event-category' |
|
54 | - ) |
|
55 | - ), |
|
56 | - ), |
|
57 | - ), |
|
58 | - 'espresso_venue_categories' => array( |
|
59 | - 'singular_name' => esc_html__('Venue Category', 'event_espresso'), |
|
60 | - 'plural_name' => esc_html__('Venue Categories', 'event_espresso'), |
|
61 | - 'args' => array( |
|
62 | - 'public' => true, |
|
63 | - 'show_in_nav_menus' => false, // by default this doesn't show for decaf |
|
64 | - 'show_in_rest' => true, |
|
65 | - 'capabilities' => array( |
|
66 | - 'manage_terms' => 'ee_manage_venue_categories', |
|
67 | - 'edit_terms' => 'ee_edit_venue_category', |
|
68 | - 'delete_terms' => 'ee_delete_venue_category', |
|
69 | - 'assign_terms' => 'ee_assign_venue_category', |
|
70 | - ), |
|
71 | - 'rewrite' => array( |
|
72 | - 'slug' => EEH_URL::slugify( |
|
73 | - __('venue-category', 'event_espresso'), |
|
74 | - 'venue-category' |
|
75 | - ) |
|
76 | - ), |
|
77 | - ), |
|
78 | - ), |
|
79 | - 'espresso_event_type' => array( |
|
80 | - 'singular_name' => esc_html__('Event Type', 'event_espresso'), |
|
81 | - 'plural_name' => esc_html__('Event Types', 'event_espresso'), |
|
82 | - 'args' => array( |
|
83 | - 'public' => true, |
|
84 | - 'show_ui' => false, |
|
85 | - 'show_in_rest' => true, |
|
86 | - 'capabilities' => array( |
|
87 | - 'manage_terms' => 'ee_read_event_type', |
|
88 | - 'edit_terms' => 'ee_edit_event_type', |
|
89 | - 'delete_terms' => 'ee_delete_event_type', |
|
90 | - 'assign_terms' => 'ee_assign_event_type', |
|
91 | - ), |
|
92 | - 'rewrite' => array( |
|
93 | - 'slug' => EEH_URL::slugify( |
|
94 | - __('event-type', 'event_espresso'), |
|
95 | - 'event-type' |
|
96 | - ) |
|
97 | - ), |
|
98 | - 'hierarchical' => true, |
|
99 | - ), |
|
100 | - ), |
|
101 | - ); |
|
102 | - } |
|
34 | + private function setTaxonomies() |
|
35 | + { |
|
36 | + $this->taxonomies = array( |
|
37 | + 'espresso_event_categories' => array( |
|
38 | + 'singular_name' => esc_html__('Event Category', 'event_espresso'), |
|
39 | + 'plural_name' => esc_html__('Event Categories', 'event_espresso'), |
|
40 | + 'args' => array( |
|
41 | + 'public' => true, |
|
42 | + 'show_in_nav_menus' => true, |
|
43 | + 'show_in_rest' => true, |
|
44 | + 'capabilities' => array( |
|
45 | + 'manage_terms' => 'ee_manage_event_categories', |
|
46 | + 'edit_terms' => 'ee_edit_event_category', |
|
47 | + 'delete_terms' => 'ee_delete_event_category', |
|
48 | + 'assign_terms' => 'ee_assign_event_category', |
|
49 | + ), |
|
50 | + 'rewrite' => array( |
|
51 | + 'slug' => EEH_URL::slugify( |
|
52 | + __('event-category', 'event_espresso'), |
|
53 | + 'event-category' |
|
54 | + ) |
|
55 | + ), |
|
56 | + ), |
|
57 | + ), |
|
58 | + 'espresso_venue_categories' => array( |
|
59 | + 'singular_name' => esc_html__('Venue Category', 'event_espresso'), |
|
60 | + 'plural_name' => esc_html__('Venue Categories', 'event_espresso'), |
|
61 | + 'args' => array( |
|
62 | + 'public' => true, |
|
63 | + 'show_in_nav_menus' => false, // by default this doesn't show for decaf |
|
64 | + 'show_in_rest' => true, |
|
65 | + 'capabilities' => array( |
|
66 | + 'manage_terms' => 'ee_manage_venue_categories', |
|
67 | + 'edit_terms' => 'ee_edit_venue_category', |
|
68 | + 'delete_terms' => 'ee_delete_venue_category', |
|
69 | + 'assign_terms' => 'ee_assign_venue_category', |
|
70 | + ), |
|
71 | + 'rewrite' => array( |
|
72 | + 'slug' => EEH_URL::slugify( |
|
73 | + __('venue-category', 'event_espresso'), |
|
74 | + 'venue-category' |
|
75 | + ) |
|
76 | + ), |
|
77 | + ), |
|
78 | + ), |
|
79 | + 'espresso_event_type' => array( |
|
80 | + 'singular_name' => esc_html__('Event Type', 'event_espresso'), |
|
81 | + 'plural_name' => esc_html__('Event Types', 'event_espresso'), |
|
82 | + 'args' => array( |
|
83 | + 'public' => true, |
|
84 | + 'show_ui' => false, |
|
85 | + 'show_in_rest' => true, |
|
86 | + 'capabilities' => array( |
|
87 | + 'manage_terms' => 'ee_read_event_type', |
|
88 | + 'edit_terms' => 'ee_edit_event_type', |
|
89 | + 'delete_terms' => 'ee_delete_event_type', |
|
90 | + 'assign_terms' => 'ee_assign_event_type', |
|
91 | + ), |
|
92 | + 'rewrite' => array( |
|
93 | + 'slug' => EEH_URL::slugify( |
|
94 | + __('event-type', 'event_espresso'), |
|
95 | + 'event-type' |
|
96 | + ) |
|
97 | + ), |
|
98 | + 'hierarchical' => true, |
|
99 | + ), |
|
100 | + ), |
|
101 | + ); |
|
102 | + } |
|
103 | 103 | |
104 | 104 | |
105 | - /** |
|
106 | - * @return array |
|
107 | - */ |
|
108 | - public function getCustomTaxonomyDefinitions() |
|
109 | - { |
|
110 | - return (array) apply_filters( |
|
111 | - 'FHEE__EventEspresso_core_domain_entities_custom_post_types_TaxonomyDefinitions__getTaxonomies', |
|
112 | - // legacy filter applied for now, |
|
113 | - // later on we'll run a has_filter($tag) check and throw a doing_it_wrong() notice |
|
114 | - apply_filters( |
|
115 | - 'FHEE__EE_Register_CPTs__get_taxonomies__taxonomies', |
|
116 | - $this->taxonomies |
|
117 | - ) |
|
118 | - ); |
|
119 | - } |
|
105 | + /** |
|
106 | + * @return array |
|
107 | + */ |
|
108 | + public function getCustomTaxonomyDefinitions() |
|
109 | + { |
|
110 | + return (array) apply_filters( |
|
111 | + 'FHEE__EventEspresso_core_domain_entities_custom_post_types_TaxonomyDefinitions__getTaxonomies', |
|
112 | + // legacy filter applied for now, |
|
113 | + // later on we'll run a has_filter($tag) check and throw a doing_it_wrong() notice |
|
114 | + apply_filters( |
|
115 | + 'FHEE__EE_Register_CPTs__get_taxonomies__taxonomies', |
|
116 | + $this->taxonomies |
|
117 | + ) |
|
118 | + ); |
|
119 | + } |
|
120 | 120 | |
121 | 121 | |
122 | - /** |
|
123 | - * @return array |
|
124 | - */ |
|
125 | - public function getCustomTaxonomySlugs() |
|
126 | - { |
|
127 | - return array_keys($this->getCustomTaxonomyDefinitions()); |
|
128 | - } |
|
122 | + /** |
|
123 | + * @return array |
|
124 | + */ |
|
125 | + public function getCustomTaxonomySlugs() |
|
126 | + { |
|
127 | + return array_keys($this->getCustomTaxonomyDefinitions()); |
|
128 | + } |
|
129 | 129 | |
130 | 130 | |
131 | - /** |
|
132 | - * By default, WordPress strips all html from term taxonomy description content. |
|
133 | - * The purpose of this method is to remove that restriction |
|
134 | - * and ensure that we still run ee term taxonomy descriptions |
|
135 | - * through some full html sanitization equivalent to the post content field. |
|
136 | - * So first we remove default filter for term description |
|
137 | - * but we have to do this earlier before wp sets their own filter |
|
138 | - * because they just set a global filter on all term descriptions |
|
139 | - * before the custom term description filter. |
|
140 | - * Really sux. |
|
141 | - * |
|
142 | - * @param string $description The description content. |
|
143 | - * @param string $taxonomy The taxonomy name for the taxonomy being filtered. |
|
144 | - * @return string |
|
145 | - */ |
|
146 | - public function filterCustomTermDescription($description, $taxonomy) |
|
147 | - { |
|
148 | - // get a list of EE taxonomies |
|
149 | - $custom_taxonomies = $this->getCustomTaxonomySlugs(); |
|
150 | - // only do our own thing if the taxonomy listed is an ee taxonomy. |
|
151 | - if (in_array($taxonomy, $custom_taxonomies, true)) { |
|
152 | - // remove default wp filter |
|
153 | - remove_filter('pre_term_description', 'wp_filter_kses'); |
|
154 | - // sanitize THIS content. |
|
155 | - $description = wp_kses($description, wp_kses_allowed_html('post')); |
|
156 | - } |
|
157 | - return $description; |
|
158 | - } |
|
131 | + /** |
|
132 | + * By default, WordPress strips all html from term taxonomy description content. |
|
133 | + * The purpose of this method is to remove that restriction |
|
134 | + * and ensure that we still run ee term taxonomy descriptions |
|
135 | + * through some full html sanitization equivalent to the post content field. |
|
136 | + * So first we remove default filter for term description |
|
137 | + * but we have to do this earlier before wp sets their own filter |
|
138 | + * because they just set a global filter on all term descriptions |
|
139 | + * before the custom term description filter. |
|
140 | + * Really sux. |
|
141 | + * |
|
142 | + * @param string $description The description content. |
|
143 | + * @param string $taxonomy The taxonomy name for the taxonomy being filtered. |
|
144 | + * @return string |
|
145 | + */ |
|
146 | + public function filterCustomTermDescription($description, $taxonomy) |
|
147 | + { |
|
148 | + // get a list of EE taxonomies |
|
149 | + $custom_taxonomies = $this->getCustomTaxonomySlugs(); |
|
150 | + // only do our own thing if the taxonomy listed is an ee taxonomy. |
|
151 | + if (in_array($taxonomy, $custom_taxonomies, true)) { |
|
152 | + // remove default wp filter |
|
153 | + remove_filter('pre_term_description', 'wp_filter_kses'); |
|
154 | + // sanitize THIS content. |
|
155 | + $description = wp_kses($description, wp_kses_allowed_html('post')); |
|
156 | + } |
|
157 | + return $description; |
|
158 | + } |
|
159 | 159 | } |
@@ -7,92 +7,92 @@ |
||
7 | 7 | class EE_Money_Field extends EE_Float_Field |
8 | 8 | { |
9 | 9 | |
10 | - /** |
|
11 | - * @param string $table_column |
|
12 | - * @param string $nicename |
|
13 | - * @param bool $nullable |
|
14 | - * @param null $default_value |
|
15 | - */ |
|
16 | - public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
17 | - { |
|
18 | - parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
19 | - $this->setSchemaType('object'); |
|
20 | - } |
|
10 | + /** |
|
11 | + * @param string $table_column |
|
12 | + * @param string $nicename |
|
13 | + * @param bool $nullable |
|
14 | + * @param null $default_value |
|
15 | + */ |
|
16 | + public function __construct($table_column, $nicename, $nullable, $default_value = null) |
|
17 | + { |
|
18 | + parent::__construct($table_column, $nicename, $nullable, $default_value); |
|
19 | + $this->setSchemaType('object'); |
|
20 | + } |
|
21 | 21 | |
22 | 22 | |
23 | - /** |
|
24 | - * Schemas: |
|
25 | - * 'localized_float': "3,023.00" |
|
26 | - * 'no_currency_code': "$3,023.00" |
|
27 | - * null: "$3,023.00<span>USD</span>" |
|
28 | - * |
|
29 | - * @param string $value_on_field_to_be_outputted |
|
30 | - * @param string $schema |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
34 | - { |
|
35 | - $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
23 | + /** |
|
24 | + * Schemas: |
|
25 | + * 'localized_float': "3,023.00" |
|
26 | + * 'no_currency_code': "$3,023.00" |
|
27 | + * null: "$3,023.00<span>USD</span>" |
|
28 | + * |
|
29 | + * @param string $value_on_field_to_be_outputted |
|
30 | + * @param string $schema |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
|
34 | + { |
|
35 | + $pretty_float = parent::prepare_for_pretty_echoing($value_on_field_to_be_outputted); |
|
36 | 36 | |
37 | - if ($schema == 'localized_float') { |
|
38 | - return $pretty_float; |
|
39 | - } |
|
40 | - if ($schema == 'no_currency_code') { |
|
37 | + if ($schema == 'localized_float') { |
|
38 | + return $pretty_float; |
|
39 | + } |
|
40 | + if ($schema == 'no_currency_code') { |
|
41 | 41 | // echo "schema no currency!"; |
42 | - $display_code = false; |
|
43 | - } else { |
|
44 | - $display_code = true; |
|
45 | - } |
|
46 | - // we don't use the $pretty_float because format_currency will take care of it. |
|
47 | - return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
48 | - } |
|
42 | + $display_code = false; |
|
43 | + } else { |
|
44 | + $display_code = true; |
|
45 | + } |
|
46 | + // we don't use the $pretty_float because format_currency will take care of it. |
|
47 | + return EEH_Template::format_currency($value_on_field_to_be_outputted, false, $display_code); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
52 | - * Rounds the float to the correct number of decimal places for this country's currency. |
|
53 | - * Also, interprets periods and commas according to the country's currency settings. |
|
54 | - * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
55 | - * |
|
56 | - * @param string $value_inputted_for_field_on_model_object |
|
57 | - * @return float |
|
58 | - */ |
|
59 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
60 | - { |
|
61 | - // remove any currencies etc. |
|
50 | + /** |
|
51 | + * If provided with a string, strips out money-related formatting to turn it into a proper float. |
|
52 | + * Rounds the float to the correct number of decimal places for this country's currency. |
|
53 | + * Also, interprets periods and commas according to the country's currency settings. |
|
54 | + * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
|
55 | + * |
|
56 | + * @param string $value_inputted_for_field_on_model_object |
|
57 | + * @return float |
|
58 | + */ |
|
59 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
60 | + { |
|
61 | + // remove any currencies etc. |
|
62 | 62 | // if(is_string($value_inputted_for_field_on_model_object)){ |
63 | 63 | // $value_inputted_for_field_on_model_object = preg_replace("/[^0-9,.]/", "", $value_inputted_for_field_on_model_object); |
64 | 64 | // } |
65 | - // now it's a float-style string or number |
|
66 | - $float_val = parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
67 | - // round to the correctly number of decimal places for this currency |
|
68 | - $rounded_value = round($float_val, EE_Registry::instance()->CFG->currency->dec_plc); |
|
69 | - return $rounded_value; |
|
70 | - } |
|
65 | + // now it's a float-style string or number |
|
66 | + $float_val = parent::prepare_for_set($value_inputted_for_field_on_model_object); |
|
67 | + // round to the correctly number of decimal places for this currency |
|
68 | + $rounded_value = round($float_val, EE_Registry::instance()->CFG->currency->dec_plc); |
|
69 | + return $rounded_value; |
|
70 | + } |
|
71 | 71 | |
72 | - public function prepare_for_get($value_of_field_on_model_object) |
|
73 | - { |
|
74 | - $c = EE_Registry::instance()->CFG->currency; |
|
75 | - return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc); |
|
76 | - } |
|
72 | + public function prepare_for_get($value_of_field_on_model_object) |
|
73 | + { |
|
74 | + $c = EE_Registry::instance()->CFG->currency; |
|
75 | + return round(parent::prepare_for_get($value_of_field_on_model_object), $c->dec_plc); |
|
76 | + } |
|
77 | 77 | |
78 | - public function getSchemaProperties() |
|
79 | - { |
|
80 | - return array( |
|
81 | - 'raw' => array( |
|
82 | - 'description' => sprintf( |
|
83 | - __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
84 | - $this->get_nicename() |
|
85 | - ), |
|
86 | - 'type' => 'number', |
|
87 | - ), |
|
88 | - 'pretty' => array( |
|
89 | - 'description' => sprintf( |
|
90 | - __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
91 | - $this->get_nicename() |
|
92 | - ), |
|
93 | - 'type' => 'string', |
|
94 | - 'format' => 'money' |
|
95 | - ) |
|
96 | - ); |
|
97 | - } |
|
78 | + public function getSchemaProperties() |
|
79 | + { |
|
80 | + return array( |
|
81 | + 'raw' => array( |
|
82 | + 'description' => sprintf( |
|
83 | + __('%s - the raw value as it exists in the database as a simple float.', 'event_espresso'), |
|
84 | + $this->get_nicename() |
|
85 | + ), |
|
86 | + 'type' => 'number', |
|
87 | + ), |
|
88 | + 'pretty' => array( |
|
89 | + 'description' => sprintf( |
|
90 | + __('%s - formatted for display in the set currency and decimal places.', 'event_espresso'), |
|
91 | + $this->get_nicename() |
|
92 | + ), |
|
93 | + 'type' => 'string', |
|
94 | + 'format' => 'money' |
|
95 | + ) |
|
96 | + ); |
|
97 | + } |
|
98 | 98 | } |