@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | use \EventEspresso\core\exceptions\SendMessageException; |
| 3 | 3 | |
| 4 | 4 | if (! defined('EVENT_ESPRESSO_VERSION')) { |
| 5 | - exit('No direct script access allowed'); |
|
| 5 | + exit('No direct script access allowed'); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | /** |
@@ -18,681 +18,681 @@ discard block |
||
| 18 | 18 | { |
| 19 | 19 | |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @type string reference for sending action |
|
| 23 | - */ |
|
| 24 | - const action_sending = 'sending'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @type string reference for generation action |
|
| 28 | - */ |
|
| 29 | - const action_generating = 'generation'; |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @type EE_Message_Repository $_message_repository |
|
| 34 | - */ |
|
| 35 | - protected $_message_repository; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Sets the limit of how many messages are generated per process. |
|
| 39 | - * |
|
| 40 | - * @type int |
|
| 41 | - */ |
|
| 42 | - protected $_batch_count; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Sets the limit of how many messages can be sent per hour. |
|
| 46 | - * |
|
| 47 | - * @type int |
|
| 48 | - */ |
|
| 49 | - protected $_rate_limit; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * This is an array of cached queue items being stored in this object. |
|
| 53 | - * The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
|
| 54 | - * is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
|
| 55 | - * |
|
| 56 | - * @type EE_Message[] |
|
| 57 | - */ |
|
| 58 | - protected $_cached_queue_items; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Tracks the number of unsaved queue items. |
|
| 62 | - * |
|
| 63 | - * @type int |
|
| 64 | - */ |
|
| 65 | - protected $_unsaved_count = 0; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
|
| 69 | - * hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
|
| 70 | - * |
|
| 71 | - * @type array |
|
| 72 | - */ |
|
| 73 | - protected $_did_hook = array(); |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Constructor. |
|
| 78 | - * Setup all the initial properties and load a EE_Message_Repository. |
|
| 79 | - * |
|
| 80 | - * @param \EE_Message_Repository $message_repository |
|
| 81 | - */ |
|
| 82 | - public function __construct(EE_Message_Repository $message_repository) |
|
| 83 | - { |
|
| 84 | - $this->_batch_count = apply_filters('FHEE__EE_Messages_Queue___batch_count', 50); |
|
| 85 | - $this->_rate_limit = $this->get_rate_limit(); |
|
| 86 | - $this->_message_repository = $message_repository; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Add a EE_Message object to the queue |
|
| 92 | - * |
|
| 93 | - * @param EE_Message $message |
|
| 94 | - * @param array $data This will be an array of data to attach to the object in the repository. If the |
|
| 95 | - * object is persisted, this data will be saved on an extra_meta object related to |
|
| 96 | - * EE_Message. |
|
| 97 | - * @param bool $preview Whether this EE_Message represents a preview or not. |
|
| 98 | - * @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
|
| 99 | - * use the messenger send method but typically is based on preview data. |
|
| 100 | - * @return bool Whether the message was successfully added to the repository or not. |
|
| 101 | - */ |
|
| 102 | - public function add(EE_Message $message, $data = array(), $preview = false, $test_send = false) |
|
| 103 | - { |
|
| 104 | - $data['preview'] = $preview; |
|
| 105 | - $data['test_send'] = $test_send; |
|
| 106 | - return $this->_message_repository->add($message, $data); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
|
| 112 | - * |
|
| 113 | - * @param EE_Message $message The message to detach from the queue |
|
| 114 | - * @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
|
| 115 | - * @return bool |
|
| 116 | - */ |
|
| 117 | - public function remove(EE_Message $message, $persist = false) |
|
| 118 | - { |
|
| 119 | - if ($persist && $this->_message_repository->current() !== $message) { |
|
| 120 | - //get pointer on right message |
|
| 121 | - if ($this->_message_repository->has($message)) { |
|
| 122 | - $this->_message_repository->rewind(); |
|
| 123 | - while ($this->_message_repository->valid()) { |
|
| 124 | - if ($this->_message_repository->current() === $message) { |
|
| 125 | - break; |
|
| 126 | - } |
|
| 127 | - $this->_message_repository->next(); |
|
| 128 | - } |
|
| 129 | - } else { |
|
| 130 | - return false; |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - return $persist ? $this->_message_repository->delete() : $this->_message_repository->remove($message); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Persists all queued EE_Message objects to the db. |
|
| 139 | - * |
|
| 140 | - * @param bool $do_hooks_only @see EE_Message_Repository::saveAll |
|
| 141 | - * @return array @see EE_Messages_Repository::saveAll() for return values. |
|
| 142 | - */ |
|
| 143 | - public function save($do_hooks_only = false) |
|
| 144 | - { |
|
| 145 | - return $this->_message_repository->saveAll($do_hooks_only); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @return EE_Message_Repository |
|
| 151 | - */ |
|
| 152 | - public function get_message_repository() |
|
| 153 | - { |
|
| 154 | - return $this->_message_repository; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * This does the following things: |
|
| 160 | - * 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return |
|
| 161 | - * false). |
|
| 162 | - * 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
|
| 163 | - * 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
|
| 164 | - * Note: Callers should make sure they release the lock otherwise batch generation will be prevented from |
|
| 165 | - * continuing. The lock is on a transient that is set to expire after one hour as a fallback in case locks are not |
|
| 166 | - * removed. |
|
| 167 | - * |
|
| 168 | - * @return bool true if successfully retrieved batch, false no batch ready. |
|
| 169 | - */ |
|
| 170 | - public function get_batch_to_generate() |
|
| 171 | - { |
|
| 172 | - if ($this->is_locked(EE_Messages_Queue::action_generating)) { |
|
| 173 | - return false; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - //lock batch generation to prevent race conditions. |
|
| 177 | - $this->lock_queue(EE_Messages_Queue::action_generating); |
|
| 178 | - |
|
| 179 | - $query_args = array( |
|
| 180 | - // key 0 = where conditions |
|
| 181 | - 0 => array('STS_ID' => EEM_Message::status_incomplete), |
|
| 182 | - 'order_by' => $this->_get_priority_orderby(), |
|
| 183 | - 'limit' => $this->_batch_count, |
|
| 184 | - ); |
|
| 185 | - $messages = EEM_Message::instance()->get_all($query_args); |
|
| 186 | - |
|
| 187 | - if ( ! $messages) { |
|
| 188 | - return false; //nothing to generate |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - foreach ($messages as $message) { |
|
| 192 | - if ($message instanceof EE_Message) { |
|
| 193 | - $data = $message->all_extra_meta_array(); |
|
| 194 | - $this->add($message, $data); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - return true; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * This does the following things: |
|
| 203 | - * 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return |
|
| 204 | - * false). |
|
| 205 | - * 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then |
|
| 206 | - * return false. |
|
| 207 | - * 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
|
| 208 | - * 3. On success or unsuccessful send, sets status appropriately. |
|
| 209 | - * 4. Saves messages via the queue |
|
| 210 | - * 5. Releases lock. |
|
| 211 | - * |
|
| 212 | - * @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not |
|
| 213 | - * necessarily mean that all messages were successfully sent. It just means that this method |
|
| 214 | - * successfully completed. On true, client may want to call $this->count_STS_in_queue( |
|
| 215 | - * EEM_Message::status_failed ) to see if any failed EE_Message objects. Each failed message object |
|
| 216 | - * will also have a saved error message on it to assist with notifying user. |
|
| 217 | - */ |
|
| 218 | - public function get_to_send_batch_and_send() |
|
| 219 | - { |
|
| 220 | - if ($this->is_locked(EE_Messages_Queue::action_sending) || $this->_rate_limit < 1) { |
|
| 221 | - return false; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $this->lock_queue(EE_Messages_Queue::action_sending); |
|
| 225 | - |
|
| 226 | - $batch = $this->_batch_count < $this->_rate_limit ? $this->_batch_count : $this->_rate_limit; |
|
| 227 | - |
|
| 228 | - $query_args = array( |
|
| 229 | - // key 0 = where conditions |
|
| 230 | - 0 => array('STS_ID' => array('IN', EEM_Message::instance()->stati_indicating_to_send())), |
|
| 231 | - 'order_by' => $this->_get_priority_orderby(), |
|
| 232 | - 'limit' => $batch, |
|
| 233 | - ); |
|
| 234 | - |
|
| 235 | - $messages_to_send = EEM_Message::instance()->get_all($query_args); |
|
| 236 | - |
|
| 237 | - |
|
| 238 | - //any to send? |
|
| 239 | - if ( ! $messages_to_send) { |
|
| 240 | - $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
| 241 | - return false; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - //add to queue. |
|
| 245 | - foreach ($messages_to_send as $message) { |
|
| 246 | - if ($message instanceof EE_Message) { |
|
| 247 | - $this->add($message); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - //send messages (this also updates the rate limit) |
|
| 252 | - $this->execute(); |
|
| 253 | - |
|
| 254 | - //release lock |
|
| 255 | - $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
| 256 | - return true; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Locks the queue so that no other queues can call the "batch" methods. |
|
| 262 | - * |
|
| 263 | - * @param string $type The type of queue being locked. |
|
| 264 | - */ |
|
| 265 | - public function lock_queue($type = EE_Messages_Queue::action_generating) |
|
| 266 | - { |
|
| 267 | - set_transient($this->_get_lock_key($type), 1, $this->_get_lock_expiry($type)); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Unlocks the queue so that batch methods can be used. |
|
| 273 | - * |
|
| 274 | - * @param string $type The type of queue being unlocked. |
|
| 275 | - */ |
|
| 276 | - public function unlock_queue($type = EE_Messages_Queue::action_generating) |
|
| 277 | - { |
|
| 278 | - delete_transient($this->_get_lock_key($type)); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Retrieve the key used for the lock transient. |
|
| 284 | - * |
|
| 285 | - * @param string $type The type of lock. |
|
| 286 | - * @return string |
|
| 287 | - */ |
|
| 288 | - protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
|
| 289 | - { |
|
| 290 | - return '_ee_lock_' . $type; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Retrieve the expiry time for the lock transient. |
|
| 296 | - * |
|
| 297 | - * @param string $type The type of lock |
|
| 298 | - * @return int time to expiry in seconds. |
|
| 299 | - */ |
|
| 300 | - protected function _get_lock_expiry($type = EE_Messages_Queue::action_generating) |
|
| 301 | - { |
|
| 302 | - return (int)apply_filters('FHEE__EE_Messages_Queue__lock_expiry', HOUR_IN_SECONDS, $type); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Returns the key used for rate limit transient. |
|
| 308 | - * |
|
| 309 | - * @return string |
|
| 310 | - */ |
|
| 311 | - protected function _get_rate_limit_key() |
|
| 312 | - { |
|
| 313 | - return '_ee_rate_limit'; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Returns the rate limit expiry time. |
|
| 319 | - * |
|
| 320 | - * @return int |
|
| 321 | - */ |
|
| 322 | - protected function _get_rate_limit_expiry() |
|
| 323 | - { |
|
| 324 | - return (int)apply_filters('FHEE__EE_Messages_Queue__rate_limit_expiry', HOUR_IN_SECONDS); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Returns the default rate limit for sending messages. |
|
| 330 | - * |
|
| 331 | - * @return int |
|
| 332 | - */ |
|
| 333 | - protected function _default_rate_limit() |
|
| 334 | - { |
|
| 335 | - return (int)apply_filters('FHEE__EE_Messages_Queue___rate_limit', 200); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Return the orderby array for priority. |
|
| 341 | - * |
|
| 342 | - * @return array |
|
| 343 | - */ |
|
| 344 | - protected function _get_priority_orderby() |
|
| 345 | - { |
|
| 346 | - return array( |
|
| 347 | - 'MSG_priority' => 'ASC', |
|
| 348 | - 'MSG_modified' => 'DESC', |
|
| 349 | - ); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * Returns whether batch methods are "locked" or not. |
|
| 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 | - /** |
|
| 362 | - * This filters the default is_locked behaviour. |
|
| 363 | - */ |
|
| 364 | - $is_locked = filter_var( |
|
| 365 | - apply_filters( |
|
| 366 | - 'FHEE__EE_Messages_Queue__is_locked', |
|
| 367 | - get_transient($this->_get_lock_key($type)), |
|
| 368 | - $this |
|
| 369 | - ), |
|
| 370 | - FILTER_VALIDATE_BOOLEAN |
|
| 371 | - ); |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * @see usage of this filter in EE_Messages_Queue::initiate_request_by_priority() method. |
|
| 375 | - * Also implemented here because messages processed on the same request should not have any locks applied. |
|
| 376 | - */ |
|
| 377 | - if ( |
|
| 378 | - apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
| 379 | - || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
| 380 | - ) { |
|
| 381 | - $is_locked = false; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - |
|
| 385 | - return $is_locked; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Retrieves the rate limit that may be cached as a transient. |
|
| 391 | - * If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
|
| 392 | - * |
|
| 393 | - * @return int |
|
| 394 | - */ |
|
| 395 | - public function get_rate_limit() |
|
| 396 | - { |
|
| 397 | - if ( ! $rate_limit = get_transient($this->_get_rate_limit_key())) { |
|
| 398 | - $rate_limit = $this->_default_rate_limit(); |
|
| 399 | - set_transient($this->_get_rate_limit_key(), $rate_limit, $this->_get_rate_limit_key()); |
|
| 400 | - } |
|
| 401 | - return $rate_limit; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * This updates existing rate limit with the new limit which is the old minus the batch. |
|
| 407 | - * |
|
| 408 | - * @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
|
| 409 | - */ |
|
| 410 | - public function set_rate_limit($batch_completed) |
|
| 411 | - { |
|
| 412 | - //first get the most up to date rate limit (in case its expired and reset) |
|
| 413 | - $rate_limit = $this->get_rate_limit(); |
|
| 414 | - $new_limit = $rate_limit - $batch_completed; |
|
| 415 | - //updating the transient option directly to avoid resetting the expiry. |
|
| 416 | - update_option('_transient_' . $this->_get_rate_limit_key(), $new_limit); |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
|
| 422 | - * If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
|
| 423 | - * Note: Keep in mind that there is the possibility that the request will not execute if there is already another |
|
| 424 | - * request running on a queue for the given task. |
|
| 425 | - * |
|
| 426 | - * @param string $task This indicates what type of request is going to be initiated. |
|
| 427 | - * @param int $priority This indicates the priority that triggers initiating the request. |
|
| 428 | - */ |
|
| 429 | - public function initiate_request_by_priority($task = 'generate', $priority = EEM_Message::priority_high) |
|
| 430 | - { |
|
| 431 | - //determine what status is matched with the priority as part of the trigger conditions. |
|
| 432 | - $status = $task == 'generate' |
|
| 433 | - ? EEM_Message::status_incomplete |
|
| 434 | - : EEM_Message::instance()->stati_indicating_to_send(); |
|
| 435 | - // always make sure we save because either this will get executed immediately on a separate request |
|
| 436 | - // or remains in the queue for the regularly scheduled queue batch. |
|
| 437 | - $this->save(); |
|
| 438 | - /** |
|
| 439 | - * This filter/option allows users to override processing of messages on separate requests and instead have everything |
|
| 440 | - * happen on the same request. If this is utilized remember: |
|
| 441 | - * - message priorities don't matter |
|
| 442 | - * - existing unprocessed messages in the queue will not get processed unless manually triggered. |
|
| 443 | - * - things will be perceived to take longer to happen for end users (i.e. registrations) because of the additional |
|
| 444 | - * processing happening on the same request. |
|
| 445 | - * - any race condition protection (locks) are removed because they don't apply when things are processed on |
|
| 446 | - * the same request. |
|
| 447 | - */ |
|
| 448 | - if ( |
|
| 449 | - apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
| 450 | - || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
| 451 | - ) { |
|
| 452 | - $messages_processor = EE_Registry::instance()->load_lib('Messages_Processor'); |
|
| 453 | - if ($messages_processor instanceof EE_Messages_Processor) { |
|
| 454 | - return $messages_processor->process_immediately_from_queue($this); |
|
| 455 | - } |
|
| 456 | - //if we get here then that means the messages processor couldn't be loaded so messages will just remain |
|
| 457 | - //queued for manual triggering by end user. |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - if ($this->_message_repository->count_by_priority_and_status($priority, $status)) { |
|
| 461 | - EE_Messages_Scheduler::initiate_scheduled_non_blocking_request($task); |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
|
| 468 | - * |
|
| 469 | - * @param bool $save Used to indicate whether to save the message queue after sending |
|
| 470 | - * (default will save). |
|
| 471 | - * @param mixed $sending_messenger (optional) When the sending messenger is different than |
|
| 472 | - * what is on the EE_Message object in the queue. |
|
| 473 | - * For instance, showing the browser view of an email message, |
|
| 474 | - * or giving a pdf generated view of an html document. |
|
| 475 | - * This should be an instance of EE_messenger but if you call this |
|
| 476 | - * method |
|
| 477 | - * intending it to be a sending messenger but a valid one could not be |
|
| 478 | - * retrieved then send in an instance of EE_Error that contains the |
|
| 479 | - * related error message. |
|
| 480 | - * @param bool|int $by_priority When set, this indicates that only messages |
|
| 481 | - * matching the given priority should be executed. |
|
| 482 | - * @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
|
| 483 | - * Also, if the messenger is an request type messenger (or a preview), |
|
| 484 | - * its entirely possible that the messenger will exit before |
|
| 485 | - */ |
|
| 486 | - public function execute($save = true, $sending_messenger = null, $by_priority = false) |
|
| 487 | - { |
|
| 488 | - $messages_sent = 0; |
|
| 489 | - $this->_did_hook = array(); |
|
| 490 | - $this->_message_repository->rewind(); |
|
| 491 | - |
|
| 492 | - while ($this->_message_repository->valid()) { |
|
| 493 | - $error_messages = array(); |
|
| 494 | - /** @type EE_Message $message */ |
|
| 495 | - $message = $this->_message_repository->current(); |
|
| 496 | - //only process things that are queued for sending |
|
| 497 | - if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
| 498 | - $this->_message_repository->next(); |
|
| 499 | - continue; |
|
| 500 | - } |
|
| 501 | - //if $by_priority is set and does not match then continue; |
|
| 502 | - if ($by_priority && $by_priority != $message->priority()) { |
|
| 503 | - $this->_message_repository->next(); |
|
| 504 | - continue; |
|
| 505 | - } |
|
| 506 | - //error checking |
|
| 507 | - if (! $message->valid_messenger()) { |
|
| 508 | - $error_messages[] = sprintf( |
|
| 509 | - __('The %s messenger is not active at time of sending.', 'event_espresso'), |
|
| 510 | - $message->messenger() |
|
| 511 | - ); |
|
| 512 | - } |
|
| 513 | - if (! $message->valid_message_type()) { |
|
| 514 | - $error_messages[] = sprintf( |
|
| 515 | - __('The %s message type is not active at the time of sending.', 'event_espresso'), |
|
| 516 | - $message->message_type() |
|
| 517 | - ); |
|
| 518 | - } |
|
| 519 | - // if there was supposed to be a sending messenger for this message, but it was invalid/inactive, |
|
| 520 | - // then it will instead be an EE_Error object, so let's check for that |
|
| 521 | - if ($sending_messenger instanceof EE_Error) { |
|
| 522 | - $error_messages[] = $sending_messenger->getMessage(); |
|
| 523 | - } |
|
| 524 | - // if there are no errors, then let's process the message |
|
| 525 | - if (empty($error_messages)) { |
|
| 526 | - if ($save) { |
|
| 527 | - $message->set_messenger_is_executing(); |
|
| 528 | - } |
|
| 529 | - if ($this->_process_message($message, $sending_messenger)) { |
|
| 530 | - $messages_sent++; |
|
| 531 | - } |
|
| 532 | - } |
|
| 533 | - $this->_set_error_message($message, $error_messages); |
|
| 534 | - //add modified time |
|
| 535 | - $message->set_modified(time()); |
|
| 536 | - //we save each message after its processed to make sure its status persists in case PHP times-out or runs |
|
| 537 | - //out of memory. @see https://events.codebasehq.com/projects/event-espresso/tickets/10281 |
|
| 538 | - if ($save) { |
|
| 539 | - $message->save(); |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - $this->_message_repository->next(); |
|
| 543 | - } |
|
| 544 | - if ($save) { |
|
| 545 | - $this->save(true); |
|
| 546 | - } |
|
| 547 | - return $messages_sent; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * _process_message |
|
| 553 | - * |
|
| 554 | - * @param EE_Message $message |
|
| 555 | - * @param mixed $sending_messenger (optional) |
|
| 556 | - * @return bool |
|
| 557 | - */ |
|
| 558 | - protected function _process_message(EE_Message $message, $sending_messenger = null) |
|
| 559 | - { |
|
| 560 | - // these *should* have been validated in the execute() method above |
|
| 561 | - $messenger = $message->messenger_object(); |
|
| 562 | - $message_type = $message->message_type_object(); |
|
| 563 | - //do actions for sending messenger if it differs from generating messenger and swap values. |
|
| 564 | - if ( |
|
| 565 | - $sending_messenger instanceof EE_messenger |
|
| 566 | - && $messenger instanceof EE_messenger |
|
| 567 | - && $sending_messenger->name != $messenger->name |
|
| 568 | - ) { |
|
| 569 | - $messenger->do_secondary_messenger_hooks($sending_messenger->name); |
|
| 570 | - $messenger = $sending_messenger; |
|
| 571 | - } |
|
| 572 | - // send using messenger, but double check objects |
|
| 573 | - if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
|
| 574 | - //set hook for message type (but only if not using another messenger to send). |
|
| 575 | - if ( ! isset($this->_did_hook[$message_type->name])) { |
|
| 576 | - $message_type->do_messenger_hooks($messenger); |
|
| 577 | - $this->_did_hook[$message_type->name] = 1; |
|
| 578 | - } |
|
| 579 | - //if preview then use preview method |
|
| 580 | - return $this->_message_repository->is_preview() |
|
| 581 | - ? $this->_do_preview($message, $messenger, $message_type, $this->_message_repository->is_test_send()) |
|
| 582 | - : $this->_do_send($message, $messenger, $message_type); |
|
| 583 | - } |
|
| 584 | - return false; |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - |
|
| 588 | - /** |
|
| 589 | - * The intention of this method is to count how many EE_Message objects |
|
| 590 | - * are in the queue with a given status. |
|
| 591 | - * Example usage: |
|
| 592 | - * After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed |
|
| 593 | - * sends by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
|
| 594 | - * |
|
| 595 | - * @param array|string $status Stati to check for in queue |
|
| 596 | - * @return int Count of EE_Message's matching the given status. |
|
| 597 | - */ |
|
| 598 | - public function count_STS_in_queue($status) |
|
| 599 | - { |
|
| 600 | - $count = 0; |
|
| 601 | - $status = is_array($status) ? $status : array($status); |
|
| 602 | - $this->_message_repository->rewind(); |
|
| 603 | - foreach ($this->_message_repository as $message) { |
|
| 604 | - if (in_array($message->STS_ID(), $status)) { |
|
| 605 | - $count++; |
|
| 606 | - } |
|
| 607 | - } |
|
| 608 | - return $count; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - |
|
| 612 | - /** |
|
| 613 | - * Executes the get_preview method on the provided messenger. |
|
| 614 | - * |
|
| 615 | - * @param EE_Message $message |
|
| 616 | - * @param EE_messenger $messenger |
|
| 617 | - * @param EE_message_type $message_type |
|
| 618 | - * @param $test_send |
|
| 619 | - * @return bool true means all went well, false means, not so much. |
|
| 620 | - */ |
|
| 621 | - protected function _do_preview( |
|
| 622 | - EE_Message $message, |
|
| 623 | - EE_messenger $messenger, |
|
| 624 | - EE_message_type $message_type, |
|
| 625 | - $test_send |
|
| 626 | - ) { |
|
| 627 | - if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
|
| 628 | - if ( ! $test_send) { |
|
| 629 | - $message->set_content($preview); |
|
| 630 | - } |
|
| 631 | - $message->set_STS_ID(EEM_Message::status_sent); |
|
| 632 | - return true; |
|
| 633 | - } else { |
|
| 634 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
| 635 | - return false; |
|
| 636 | - } |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - |
|
| 640 | - /** |
|
| 641 | - * Executes the send method on the provided messenger |
|
| 642 | - * EE_Messengers are expected to: |
|
| 643 | - * - return true if the send was successful. |
|
| 644 | - * - return false if the send was unsuccessful but can be tried again. |
|
| 645 | - * - throw an Exception if the send was unsuccessful and cannot be tried again. |
|
| 646 | - * |
|
| 647 | - * @param EE_Message $message |
|
| 648 | - * @param EE_messenger $messenger |
|
| 649 | - * @param EE_message_type $message_type |
|
| 650 | - * @return bool true means all went well, false means, not so much. |
|
| 651 | - */ |
|
| 652 | - protected function _do_send(EE_Message $message, EE_messenger $messenger, EE_message_type $message_type) |
|
| 653 | - { |
|
| 654 | - try { |
|
| 655 | - if ($messenger->send_message($message, $message_type)) { |
|
| 656 | - $message->set_STS_ID(EEM_Message::status_sent); |
|
| 657 | - return true; |
|
| 658 | - } else { |
|
| 659 | - $message->set_STS_ID(EEM_Message::status_retry); |
|
| 660 | - return false; |
|
| 661 | - } |
|
| 662 | - } catch (SendMessageException $e) { |
|
| 663 | - $message->set_STS_ID(EEM_Message::status_failed); |
|
| 664 | - $message->set_error_message($e->getMessage()); |
|
| 665 | - return false; |
|
| 666 | - } |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * This sets any necessary error messages on the message object and its status to failed. |
|
| 672 | - * |
|
| 673 | - * @param EE_Message $message |
|
| 674 | - * @param array $error_messages the response from the messenger. |
|
| 675 | - */ |
|
| 676 | - protected function _set_error_message(EE_Message $message, $error_messages) |
|
| 677 | - { |
|
| 678 | - $error_messages = (array)$error_messages; |
|
| 679 | - if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_failed_sending())) { |
|
| 680 | - $notices = EE_Error::has_notices(); |
|
| 681 | - $error_messages[] = __( |
|
| 682 | - 'Messenger and Message Type were valid and active, but the messenger send method failed.', |
|
| 683 | - 'event_espresso' |
|
| 684 | - ); |
|
| 685 | - if ($notices === 1) { |
|
| 686 | - $notices = EE_Error::get_vanilla_notices(); |
|
| 687 | - $notices['errors'] = isset($notices['errors']) ? $notices['errors'] : array(); |
|
| 688 | - $error_messages[] = implode("\n", $notices['errors']); |
|
| 689 | - } |
|
| 690 | - } |
|
| 691 | - if (count($error_messages) > 0) { |
|
| 692 | - $msg = __('Message was not executed successfully.', 'event_espresso'); |
|
| 693 | - $msg = $msg . "\n" . implode("\n", $error_messages); |
|
| 694 | - $message->set_error_message($msg); |
|
| 695 | - } |
|
| 696 | - } |
|
| 21 | + /** |
|
| 22 | + * @type string reference for sending action |
|
| 23 | + */ |
|
| 24 | + const action_sending = 'sending'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @type string reference for generation action |
|
| 28 | + */ |
|
| 29 | + const action_generating = 'generation'; |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @type EE_Message_Repository $_message_repository |
|
| 34 | + */ |
|
| 35 | + protected $_message_repository; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Sets the limit of how many messages are generated per process. |
|
| 39 | + * |
|
| 40 | + * @type int |
|
| 41 | + */ |
|
| 42 | + protected $_batch_count; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Sets the limit of how many messages can be sent per hour. |
|
| 46 | + * |
|
| 47 | + * @type int |
|
| 48 | + */ |
|
| 49 | + protected $_rate_limit; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * This is an array of cached queue items being stored in this object. |
|
| 53 | + * The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
|
| 54 | + * is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
|
| 55 | + * |
|
| 56 | + * @type EE_Message[] |
|
| 57 | + */ |
|
| 58 | + protected $_cached_queue_items; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Tracks the number of unsaved queue items. |
|
| 62 | + * |
|
| 63 | + * @type int |
|
| 64 | + */ |
|
| 65 | + protected $_unsaved_count = 0; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
|
| 69 | + * hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
|
| 70 | + * |
|
| 71 | + * @type array |
|
| 72 | + */ |
|
| 73 | + protected $_did_hook = array(); |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Constructor. |
|
| 78 | + * Setup all the initial properties and load a EE_Message_Repository. |
|
| 79 | + * |
|
| 80 | + * @param \EE_Message_Repository $message_repository |
|
| 81 | + */ |
|
| 82 | + public function __construct(EE_Message_Repository $message_repository) |
|
| 83 | + { |
|
| 84 | + $this->_batch_count = apply_filters('FHEE__EE_Messages_Queue___batch_count', 50); |
|
| 85 | + $this->_rate_limit = $this->get_rate_limit(); |
|
| 86 | + $this->_message_repository = $message_repository; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Add a EE_Message object to the queue |
|
| 92 | + * |
|
| 93 | + * @param EE_Message $message |
|
| 94 | + * @param array $data This will be an array of data to attach to the object in the repository. If the |
|
| 95 | + * object is persisted, this data will be saved on an extra_meta object related to |
|
| 96 | + * EE_Message. |
|
| 97 | + * @param bool $preview Whether this EE_Message represents a preview or not. |
|
| 98 | + * @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
|
| 99 | + * use the messenger send method but typically is based on preview data. |
|
| 100 | + * @return bool Whether the message was successfully added to the repository or not. |
|
| 101 | + */ |
|
| 102 | + public function add(EE_Message $message, $data = array(), $preview = false, $test_send = false) |
|
| 103 | + { |
|
| 104 | + $data['preview'] = $preview; |
|
| 105 | + $data['test_send'] = $test_send; |
|
| 106 | + return $this->_message_repository->add($message, $data); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
|
| 112 | + * |
|
| 113 | + * @param EE_Message $message The message to detach from the queue |
|
| 114 | + * @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
|
| 115 | + * @return bool |
|
| 116 | + */ |
|
| 117 | + public function remove(EE_Message $message, $persist = false) |
|
| 118 | + { |
|
| 119 | + if ($persist && $this->_message_repository->current() !== $message) { |
|
| 120 | + //get pointer on right message |
|
| 121 | + if ($this->_message_repository->has($message)) { |
|
| 122 | + $this->_message_repository->rewind(); |
|
| 123 | + while ($this->_message_repository->valid()) { |
|
| 124 | + if ($this->_message_repository->current() === $message) { |
|
| 125 | + break; |
|
| 126 | + } |
|
| 127 | + $this->_message_repository->next(); |
|
| 128 | + } |
|
| 129 | + } else { |
|
| 130 | + return false; |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + return $persist ? $this->_message_repository->delete() : $this->_message_repository->remove($message); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Persists all queued EE_Message objects to the db. |
|
| 139 | + * |
|
| 140 | + * @param bool $do_hooks_only @see EE_Message_Repository::saveAll |
|
| 141 | + * @return array @see EE_Messages_Repository::saveAll() for return values. |
|
| 142 | + */ |
|
| 143 | + public function save($do_hooks_only = false) |
|
| 144 | + { |
|
| 145 | + return $this->_message_repository->saveAll($do_hooks_only); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @return EE_Message_Repository |
|
| 151 | + */ |
|
| 152 | + public function get_message_repository() |
|
| 153 | + { |
|
| 154 | + return $this->_message_repository; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * This does the following things: |
|
| 160 | + * 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return |
|
| 161 | + * false). |
|
| 162 | + * 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
|
| 163 | + * 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
|
| 164 | + * Note: Callers should make sure they release the lock otherwise batch generation will be prevented from |
|
| 165 | + * continuing. The lock is on a transient that is set to expire after one hour as a fallback in case locks are not |
|
| 166 | + * removed. |
|
| 167 | + * |
|
| 168 | + * @return bool true if successfully retrieved batch, false no batch ready. |
|
| 169 | + */ |
|
| 170 | + public function get_batch_to_generate() |
|
| 171 | + { |
|
| 172 | + if ($this->is_locked(EE_Messages_Queue::action_generating)) { |
|
| 173 | + return false; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + //lock batch generation to prevent race conditions. |
|
| 177 | + $this->lock_queue(EE_Messages_Queue::action_generating); |
|
| 178 | + |
|
| 179 | + $query_args = array( |
|
| 180 | + // key 0 = where conditions |
|
| 181 | + 0 => array('STS_ID' => EEM_Message::status_incomplete), |
|
| 182 | + 'order_by' => $this->_get_priority_orderby(), |
|
| 183 | + 'limit' => $this->_batch_count, |
|
| 184 | + ); |
|
| 185 | + $messages = EEM_Message::instance()->get_all($query_args); |
|
| 186 | + |
|
| 187 | + if ( ! $messages) { |
|
| 188 | + return false; //nothing to generate |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + foreach ($messages as $message) { |
|
| 192 | + if ($message instanceof EE_Message) { |
|
| 193 | + $data = $message->all_extra_meta_array(); |
|
| 194 | + $this->add($message, $data); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + return true; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * This does the following things: |
|
| 203 | + * 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return |
|
| 204 | + * false). |
|
| 205 | + * 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then |
|
| 206 | + * return false. |
|
| 207 | + * 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
|
| 208 | + * 3. On success or unsuccessful send, sets status appropriately. |
|
| 209 | + * 4. Saves messages via the queue |
|
| 210 | + * 5. Releases lock. |
|
| 211 | + * |
|
| 212 | + * @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not |
|
| 213 | + * necessarily mean that all messages were successfully sent. It just means that this method |
|
| 214 | + * successfully completed. On true, client may want to call $this->count_STS_in_queue( |
|
| 215 | + * EEM_Message::status_failed ) to see if any failed EE_Message objects. Each failed message object |
|
| 216 | + * will also have a saved error message on it to assist with notifying user. |
|
| 217 | + */ |
|
| 218 | + public function get_to_send_batch_and_send() |
|
| 219 | + { |
|
| 220 | + if ($this->is_locked(EE_Messages_Queue::action_sending) || $this->_rate_limit < 1) { |
|
| 221 | + return false; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $this->lock_queue(EE_Messages_Queue::action_sending); |
|
| 225 | + |
|
| 226 | + $batch = $this->_batch_count < $this->_rate_limit ? $this->_batch_count : $this->_rate_limit; |
|
| 227 | + |
|
| 228 | + $query_args = array( |
|
| 229 | + // key 0 = where conditions |
|
| 230 | + 0 => array('STS_ID' => array('IN', EEM_Message::instance()->stati_indicating_to_send())), |
|
| 231 | + 'order_by' => $this->_get_priority_orderby(), |
|
| 232 | + 'limit' => $batch, |
|
| 233 | + ); |
|
| 234 | + |
|
| 235 | + $messages_to_send = EEM_Message::instance()->get_all($query_args); |
|
| 236 | + |
|
| 237 | + |
|
| 238 | + //any to send? |
|
| 239 | + if ( ! $messages_to_send) { |
|
| 240 | + $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
| 241 | + return false; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + //add to queue. |
|
| 245 | + foreach ($messages_to_send as $message) { |
|
| 246 | + if ($message instanceof EE_Message) { |
|
| 247 | + $this->add($message); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + //send messages (this also updates the rate limit) |
|
| 252 | + $this->execute(); |
|
| 253 | + |
|
| 254 | + //release lock |
|
| 255 | + $this->unlock_queue(EE_Messages_Queue::action_sending); |
|
| 256 | + return true; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Locks the queue so that no other queues can call the "batch" methods. |
|
| 262 | + * |
|
| 263 | + * @param string $type The type of queue being locked. |
|
| 264 | + */ |
|
| 265 | + public function lock_queue($type = EE_Messages_Queue::action_generating) |
|
| 266 | + { |
|
| 267 | + set_transient($this->_get_lock_key($type), 1, $this->_get_lock_expiry($type)); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Unlocks the queue so that batch methods can be used. |
|
| 273 | + * |
|
| 274 | + * @param string $type The type of queue being unlocked. |
|
| 275 | + */ |
|
| 276 | + public function unlock_queue($type = EE_Messages_Queue::action_generating) |
|
| 277 | + { |
|
| 278 | + delete_transient($this->_get_lock_key($type)); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Retrieve the key used for the lock transient. |
|
| 284 | + * |
|
| 285 | + * @param string $type The type of lock. |
|
| 286 | + * @return string |
|
| 287 | + */ |
|
| 288 | + protected function _get_lock_key($type = EE_Messages_Queue::action_generating) |
|
| 289 | + { |
|
| 290 | + return '_ee_lock_' . $type; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Retrieve the expiry time for the lock transient. |
|
| 296 | + * |
|
| 297 | + * @param string $type The type of lock |
|
| 298 | + * @return int time to expiry in seconds. |
|
| 299 | + */ |
|
| 300 | + protected function _get_lock_expiry($type = EE_Messages_Queue::action_generating) |
|
| 301 | + { |
|
| 302 | + return (int)apply_filters('FHEE__EE_Messages_Queue__lock_expiry', HOUR_IN_SECONDS, $type); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Returns the key used for rate limit transient. |
|
| 308 | + * |
|
| 309 | + * @return string |
|
| 310 | + */ |
|
| 311 | + protected function _get_rate_limit_key() |
|
| 312 | + { |
|
| 313 | + return '_ee_rate_limit'; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Returns the rate limit expiry time. |
|
| 319 | + * |
|
| 320 | + * @return int |
|
| 321 | + */ |
|
| 322 | + protected function _get_rate_limit_expiry() |
|
| 323 | + { |
|
| 324 | + return (int)apply_filters('FHEE__EE_Messages_Queue__rate_limit_expiry', HOUR_IN_SECONDS); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Returns the default rate limit for sending messages. |
|
| 330 | + * |
|
| 331 | + * @return int |
|
| 332 | + */ |
|
| 333 | + protected function _default_rate_limit() |
|
| 334 | + { |
|
| 335 | + return (int)apply_filters('FHEE__EE_Messages_Queue___rate_limit', 200); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Return the orderby array for priority. |
|
| 341 | + * |
|
| 342 | + * @return array |
|
| 343 | + */ |
|
| 344 | + protected function _get_priority_orderby() |
|
| 345 | + { |
|
| 346 | + return array( |
|
| 347 | + 'MSG_priority' => 'ASC', |
|
| 348 | + 'MSG_modified' => 'DESC', |
|
| 349 | + ); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * Returns whether batch methods are "locked" or not. |
|
| 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 | + /** |
|
| 362 | + * This filters the default is_locked behaviour. |
|
| 363 | + */ |
|
| 364 | + $is_locked = filter_var( |
|
| 365 | + apply_filters( |
|
| 366 | + 'FHEE__EE_Messages_Queue__is_locked', |
|
| 367 | + get_transient($this->_get_lock_key($type)), |
|
| 368 | + $this |
|
| 369 | + ), |
|
| 370 | + FILTER_VALIDATE_BOOLEAN |
|
| 371 | + ); |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * @see usage of this filter in EE_Messages_Queue::initiate_request_by_priority() method. |
|
| 375 | + * Also implemented here because messages processed on the same request should not have any locks applied. |
|
| 376 | + */ |
|
| 377 | + if ( |
|
| 378 | + apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
| 379 | + || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
| 380 | + ) { |
|
| 381 | + $is_locked = false; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + |
|
| 385 | + return $is_locked; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Retrieves the rate limit that may be cached as a transient. |
|
| 391 | + * If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
|
| 392 | + * |
|
| 393 | + * @return int |
|
| 394 | + */ |
|
| 395 | + public function get_rate_limit() |
|
| 396 | + { |
|
| 397 | + if ( ! $rate_limit = get_transient($this->_get_rate_limit_key())) { |
|
| 398 | + $rate_limit = $this->_default_rate_limit(); |
|
| 399 | + set_transient($this->_get_rate_limit_key(), $rate_limit, $this->_get_rate_limit_key()); |
|
| 400 | + } |
|
| 401 | + return $rate_limit; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * This updates existing rate limit with the new limit which is the old minus the batch. |
|
| 407 | + * |
|
| 408 | + * @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
|
| 409 | + */ |
|
| 410 | + public function set_rate_limit($batch_completed) |
|
| 411 | + { |
|
| 412 | + //first get the most up to date rate limit (in case its expired and reset) |
|
| 413 | + $rate_limit = $this->get_rate_limit(); |
|
| 414 | + $new_limit = $rate_limit - $batch_completed; |
|
| 415 | + //updating the transient option directly to avoid resetting the expiry. |
|
| 416 | + update_option('_transient_' . $this->_get_rate_limit_key(), $new_limit); |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
|
| 422 | + * If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
|
| 423 | + * Note: Keep in mind that there is the possibility that the request will not execute if there is already another |
|
| 424 | + * request running on a queue for the given task. |
|
| 425 | + * |
|
| 426 | + * @param string $task This indicates what type of request is going to be initiated. |
|
| 427 | + * @param int $priority This indicates the priority that triggers initiating the request. |
|
| 428 | + */ |
|
| 429 | + public function initiate_request_by_priority($task = 'generate', $priority = EEM_Message::priority_high) |
|
| 430 | + { |
|
| 431 | + //determine what status is matched with the priority as part of the trigger conditions. |
|
| 432 | + $status = $task == 'generate' |
|
| 433 | + ? EEM_Message::status_incomplete |
|
| 434 | + : EEM_Message::instance()->stati_indicating_to_send(); |
|
| 435 | + // always make sure we save because either this will get executed immediately on a separate request |
|
| 436 | + // or remains in the queue for the regularly scheduled queue batch. |
|
| 437 | + $this->save(); |
|
| 438 | + /** |
|
| 439 | + * This filter/option allows users to override processing of messages on separate requests and instead have everything |
|
| 440 | + * happen on the same request. If this is utilized remember: |
|
| 441 | + * - message priorities don't matter |
|
| 442 | + * - existing unprocessed messages in the queue will not get processed unless manually triggered. |
|
| 443 | + * - things will be perceived to take longer to happen for end users (i.e. registrations) because of the additional |
|
| 444 | + * processing happening on the same request. |
|
| 445 | + * - any race condition protection (locks) are removed because they don't apply when things are processed on |
|
| 446 | + * the same request. |
|
| 447 | + */ |
|
| 448 | + if ( |
|
| 449 | + apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false) |
|
| 450 | + || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
| 451 | + ) { |
|
| 452 | + $messages_processor = EE_Registry::instance()->load_lib('Messages_Processor'); |
|
| 453 | + if ($messages_processor instanceof EE_Messages_Processor) { |
|
| 454 | + return $messages_processor->process_immediately_from_queue($this); |
|
| 455 | + } |
|
| 456 | + //if we get here then that means the messages processor couldn't be loaded so messages will just remain |
|
| 457 | + //queued for manual triggering by end user. |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + if ($this->_message_repository->count_by_priority_and_status($priority, $status)) { |
|
| 461 | + EE_Messages_Scheduler::initiate_scheduled_non_blocking_request($task); |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
|
| 468 | + * |
|
| 469 | + * @param bool $save Used to indicate whether to save the message queue after sending |
|
| 470 | + * (default will save). |
|
| 471 | + * @param mixed $sending_messenger (optional) When the sending messenger is different than |
|
| 472 | + * what is on the EE_Message object in the queue. |
|
| 473 | + * For instance, showing the browser view of an email message, |
|
| 474 | + * or giving a pdf generated view of an html document. |
|
| 475 | + * This should be an instance of EE_messenger but if you call this |
|
| 476 | + * method |
|
| 477 | + * intending it to be a sending messenger but a valid one could not be |
|
| 478 | + * retrieved then send in an instance of EE_Error that contains the |
|
| 479 | + * related error message. |
|
| 480 | + * @param bool|int $by_priority When set, this indicates that only messages |
|
| 481 | + * matching the given priority should be executed. |
|
| 482 | + * @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
|
| 483 | + * Also, if the messenger is an request type messenger (or a preview), |
|
| 484 | + * its entirely possible that the messenger will exit before |
|
| 485 | + */ |
|
| 486 | + public function execute($save = true, $sending_messenger = null, $by_priority = false) |
|
| 487 | + { |
|
| 488 | + $messages_sent = 0; |
|
| 489 | + $this->_did_hook = array(); |
|
| 490 | + $this->_message_repository->rewind(); |
|
| 491 | + |
|
| 492 | + while ($this->_message_repository->valid()) { |
|
| 493 | + $error_messages = array(); |
|
| 494 | + /** @type EE_Message $message */ |
|
| 495 | + $message = $this->_message_repository->current(); |
|
| 496 | + //only process things that are queued for sending |
|
| 497 | + if (! in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
| 498 | + $this->_message_repository->next(); |
|
| 499 | + continue; |
|
| 500 | + } |
|
| 501 | + //if $by_priority is set and does not match then continue; |
|
| 502 | + if ($by_priority && $by_priority != $message->priority()) { |
|
| 503 | + $this->_message_repository->next(); |
|
| 504 | + continue; |
|
| 505 | + } |
|
| 506 | + //error checking |
|
| 507 | + if (! $message->valid_messenger()) { |
|
| 508 | + $error_messages[] = sprintf( |
|
| 509 | + __('The %s messenger is not active at time of sending.', 'event_espresso'), |
|
| 510 | + $message->messenger() |
|
| 511 | + ); |
|
| 512 | + } |
|
| 513 | + if (! $message->valid_message_type()) { |
|
| 514 | + $error_messages[] = sprintf( |
|
| 515 | + __('The %s message type is not active at the time of sending.', 'event_espresso'), |
|
| 516 | + $message->message_type() |
|
| 517 | + ); |
|
| 518 | + } |
|
| 519 | + // if there was supposed to be a sending messenger for this message, but it was invalid/inactive, |
|
| 520 | + // then it will instead be an EE_Error object, so let's check for that |
|
| 521 | + if ($sending_messenger instanceof EE_Error) { |
|
| 522 | + $error_messages[] = $sending_messenger->getMessage(); |
|
| 523 | + } |
|
| 524 | + // if there are no errors, then let's process the message |
|
| 525 | + if (empty($error_messages)) { |
|
| 526 | + if ($save) { |
|
| 527 | + $message->set_messenger_is_executing(); |
|
| 528 | + } |
|
| 529 | + if ($this->_process_message($message, $sending_messenger)) { |
|
| 530 | + $messages_sent++; |
|
| 531 | + } |
|
| 532 | + } |
|
| 533 | + $this->_set_error_message($message, $error_messages); |
|
| 534 | + //add modified time |
|
| 535 | + $message->set_modified(time()); |
|
| 536 | + //we save each message after its processed to make sure its status persists in case PHP times-out or runs |
|
| 537 | + //out of memory. @see https://events.codebasehq.com/projects/event-espresso/tickets/10281 |
|
| 538 | + if ($save) { |
|
| 539 | + $message->save(); |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + $this->_message_repository->next(); |
|
| 543 | + } |
|
| 544 | + if ($save) { |
|
| 545 | + $this->save(true); |
|
| 546 | + } |
|
| 547 | + return $messages_sent; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * _process_message |
|
| 553 | + * |
|
| 554 | + * @param EE_Message $message |
|
| 555 | + * @param mixed $sending_messenger (optional) |
|
| 556 | + * @return bool |
|
| 557 | + */ |
|
| 558 | + protected function _process_message(EE_Message $message, $sending_messenger = null) |
|
| 559 | + { |
|
| 560 | + // these *should* have been validated in the execute() method above |
|
| 561 | + $messenger = $message->messenger_object(); |
|
| 562 | + $message_type = $message->message_type_object(); |
|
| 563 | + //do actions for sending messenger if it differs from generating messenger and swap values. |
|
| 564 | + if ( |
|
| 565 | + $sending_messenger instanceof EE_messenger |
|
| 566 | + && $messenger instanceof EE_messenger |
|
| 567 | + && $sending_messenger->name != $messenger->name |
|
| 568 | + ) { |
|
| 569 | + $messenger->do_secondary_messenger_hooks($sending_messenger->name); |
|
| 570 | + $messenger = $sending_messenger; |
|
| 571 | + } |
|
| 572 | + // send using messenger, but double check objects |
|
| 573 | + if ($messenger instanceof EE_messenger && $message_type instanceof EE_message_type) { |
|
| 574 | + //set hook for message type (but only if not using another messenger to send). |
|
| 575 | + if ( ! isset($this->_did_hook[$message_type->name])) { |
|
| 576 | + $message_type->do_messenger_hooks($messenger); |
|
| 577 | + $this->_did_hook[$message_type->name] = 1; |
|
| 578 | + } |
|
| 579 | + //if preview then use preview method |
|
| 580 | + return $this->_message_repository->is_preview() |
|
| 581 | + ? $this->_do_preview($message, $messenger, $message_type, $this->_message_repository->is_test_send()) |
|
| 582 | + : $this->_do_send($message, $messenger, $message_type); |
|
| 583 | + } |
|
| 584 | + return false; |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + |
|
| 588 | + /** |
|
| 589 | + * The intention of this method is to count how many EE_Message objects |
|
| 590 | + * are in the queue with a given status. |
|
| 591 | + * Example usage: |
|
| 592 | + * After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed |
|
| 593 | + * sends by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
|
| 594 | + * |
|
| 595 | + * @param array|string $status Stati to check for in queue |
|
| 596 | + * @return int Count of EE_Message's matching the given status. |
|
| 597 | + */ |
|
| 598 | + public function count_STS_in_queue($status) |
|
| 599 | + { |
|
| 600 | + $count = 0; |
|
| 601 | + $status = is_array($status) ? $status : array($status); |
|
| 602 | + $this->_message_repository->rewind(); |
|
| 603 | + foreach ($this->_message_repository as $message) { |
|
| 604 | + if (in_array($message->STS_ID(), $status)) { |
|
| 605 | + $count++; |
|
| 606 | + } |
|
| 607 | + } |
|
| 608 | + return $count; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + |
|
| 612 | + /** |
|
| 613 | + * Executes the get_preview method on the provided messenger. |
|
| 614 | + * |
|
| 615 | + * @param EE_Message $message |
|
| 616 | + * @param EE_messenger $messenger |
|
| 617 | + * @param EE_message_type $message_type |
|
| 618 | + * @param $test_send |
|
| 619 | + * @return bool true means all went well, false means, not so much. |
|
| 620 | + */ |
|
| 621 | + protected function _do_preview( |
|
| 622 | + EE_Message $message, |
|
| 623 | + EE_messenger $messenger, |
|
| 624 | + EE_message_type $message_type, |
|
| 625 | + $test_send |
|
| 626 | + ) { |
|
| 627 | + if ($preview = $messenger->get_preview($message, $message_type, $test_send)) { |
|
| 628 | + if ( ! $test_send) { |
|
| 629 | + $message->set_content($preview); |
|
| 630 | + } |
|
| 631 | + $message->set_STS_ID(EEM_Message::status_sent); |
|
| 632 | + return true; |
|
| 633 | + } else { |
|
| 634 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
| 635 | + return false; |
|
| 636 | + } |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + |
|
| 640 | + /** |
|
| 641 | + * Executes the send method on the provided messenger |
|
| 642 | + * EE_Messengers are expected to: |
|
| 643 | + * - return true if the send was successful. |
|
| 644 | + * - return false if the send was unsuccessful but can be tried again. |
|
| 645 | + * - throw an Exception if the send was unsuccessful and cannot be tried again. |
|
| 646 | + * |
|
| 647 | + * @param EE_Message $message |
|
| 648 | + * @param EE_messenger $messenger |
|
| 649 | + * @param EE_message_type $message_type |
|
| 650 | + * @return bool true means all went well, false means, not so much. |
|
| 651 | + */ |
|
| 652 | + protected function _do_send(EE_Message $message, EE_messenger $messenger, EE_message_type $message_type) |
|
| 653 | + { |
|
| 654 | + try { |
|
| 655 | + if ($messenger->send_message($message, $message_type)) { |
|
| 656 | + $message->set_STS_ID(EEM_Message::status_sent); |
|
| 657 | + return true; |
|
| 658 | + } else { |
|
| 659 | + $message->set_STS_ID(EEM_Message::status_retry); |
|
| 660 | + return false; |
|
| 661 | + } |
|
| 662 | + } catch (SendMessageException $e) { |
|
| 663 | + $message->set_STS_ID(EEM_Message::status_failed); |
|
| 664 | + $message->set_error_message($e->getMessage()); |
|
| 665 | + return false; |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * This sets any necessary error messages on the message object and its status to failed. |
|
| 672 | + * |
|
| 673 | + * @param EE_Message $message |
|
| 674 | + * @param array $error_messages the response from the messenger. |
|
| 675 | + */ |
|
| 676 | + protected function _set_error_message(EE_Message $message, $error_messages) |
|
| 677 | + { |
|
| 678 | + $error_messages = (array)$error_messages; |
|
| 679 | + if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_failed_sending())) { |
|
| 680 | + $notices = EE_Error::has_notices(); |
|
| 681 | + $error_messages[] = __( |
|
| 682 | + 'Messenger and Message Type were valid and active, but the messenger send method failed.', |
|
| 683 | + 'event_espresso' |
|
| 684 | + ); |
|
| 685 | + if ($notices === 1) { |
|
| 686 | + $notices = EE_Error::get_vanilla_notices(); |
|
| 687 | + $notices['errors'] = isset($notices['errors']) ? $notices['errors'] : array(); |
|
| 688 | + $error_messages[] = implode("\n", $notices['errors']); |
|
| 689 | + } |
|
| 690 | + } |
|
| 691 | + if (count($error_messages) > 0) { |
|
| 692 | + $msg = __('Message was not executed successfully.', 'event_espresso'); |
|
| 693 | + $msg = $msg . "\n" . implode("\n", $error_messages); |
|
| 694 | + $message->set_error_message($msg); |
|
| 695 | + } |
|
| 696 | + } |
|
| 697 | 697 | |
| 698 | 698 | } //end EE_Messages_Queue class |
@@ -560,10 +560,10 @@ |
||
| 560 | 560 | |
| 561 | 561 | foreach ( $regs_to_send as $status_group ) { |
| 562 | 562 | foreach ( $status_group as $status_id => $registrations ) { |
| 563 | - $message_type = EEH_MSG_Template::convert_reg_status_to_message_type($status_id); |
|
| 564 | - if (! $message_type) { |
|
| 565 | - continue; |
|
| 566 | - } |
|
| 563 | + $message_type = EEH_MSG_Template::convert_reg_status_to_message_type($status_id); |
|
| 564 | + if (! $message_type) { |
|
| 565 | + continue; |
|
| 566 | + } |
|
| 567 | 567 | $messages_to_generate = array_merge( |
| 568 | 568 | $messages_to_generate, |
| 569 | 569 | $this->setup_mtgs_for_all_active_messengers( |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | * |
| 36 | 36 | * @param EE_Message_Resource_Manager $message_resource_manager |
| 37 | 37 | */ |
| 38 | - public function __construct( EE_Message_Resource_Manager $message_resource_manager ) { |
|
| 38 | + public function __construct(EE_Message_Resource_Manager $message_resource_manager) { |
|
| 39 | 39 | $this->_message_resource_manager = $message_resource_manager; |
| 40 | 40 | $this->_init_queue_and_generator(); |
| 41 | 41 | } |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * - $_generator = holds the messages generator |
| 51 | 51 | */ |
| 52 | 52 | protected function _init_queue_and_generator() { |
| 53 | - $this->_generator = EE_Registry::factory( 'EE_Messages_Generator' ); |
|
| 53 | + $this->_generator = EE_Registry::factory('EE_Messages_Generator'); |
|
| 54 | 54 | $this->_queue = $this->_generator->generation_queue(); |
| 55 | 55 | } |
| 56 | 56 | |
@@ -75,31 +75,31 @@ discard block |
||
| 75 | 75 | * @param EE_Messages_Queue $queue_to_process |
| 76 | 76 | * @return bool true for success false for error. |
| 77 | 77 | */ |
| 78 | - public function process_immediately_from_queue( EE_Messages_Queue $queue_to_process ) { |
|
| 78 | + public function process_immediately_from_queue(EE_Messages_Queue $queue_to_process) { |
|
| 79 | 79 | $success = false; |
| 80 | 80 | $messages_to_send = array(); |
| 81 | 81 | $messages_to_generate = array(); |
| 82 | 82 | //loop through and setup the various messages from the queue so we know what is being processed |
| 83 | 83 | $queue_to_process->get_message_repository()->rewind(); |
| 84 | - foreach ( $queue_to_process->get_message_repository() as $message ) { |
|
| 85 | - if ( $message->STS_ID() === EEM_Message::status_incomplete ) { |
|
| 84 | + foreach ($queue_to_process->get_message_repository() as $message) { |
|
| 85 | + if ($message->STS_ID() === EEM_Message::status_incomplete) { |
|
| 86 | 86 | $messages_to_generate[] = $message; |
| 87 | 87 | continue; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | - if ( in_array( $message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send() ) ) { |
|
| 90 | + if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) { |
|
| 91 | 91 | $messages_to_send[] = $message; |
| 92 | 92 | continue; |
| 93 | 93 | } |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | //do generation/sends |
| 97 | - if ( $messages_to_generate ) { |
|
| 98 | - $success = $this->batch_generate_from_queue( $messages_to_generate, true ); |
|
| 97 | + if ($messages_to_generate) { |
|
| 98 | + $success = $this->batch_generate_from_queue($messages_to_generate, true); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - if ( $messages_to_send ) { |
|
| 102 | - $sent = $this->batch_send_from_queue( $messages_to_send, true ); |
|
| 101 | + if ($messages_to_send) { |
|
| 102 | + $sent = $this->batch_send_from_queue($messages_to_send, true); |
|
| 103 | 103 | //if there was messages to generate and it failed, then we override any success value for the sending process |
| 104 | 104 | //otherwise we just use the return from batch send. The intent is that there is a simple response for success/fail. |
| 105 | 105 | //Either everything was successful or we consider it a fail. To be clear, this is a limitation of doing |
@@ -119,13 +119,13 @@ discard block |
||
| 119 | 119 | * @return bool|EE_Messages_Queue return false if nothing generated. This returns a new EE_Message_Queue with |
| 120 | 120 | * generated messages. |
| 121 | 121 | */ |
| 122 | - public function batch_generate_from_queue( $messages = array(), $clear_queue = false ) { |
|
| 123 | - if ( $this->_build_queue_for_generation( $messages, $clear_queue ) ) { |
|
| 122 | + public function batch_generate_from_queue($messages = array(), $clear_queue = false) { |
|
| 123 | + if ($this->_build_queue_for_generation($messages, $clear_queue)) { |
|
| 124 | 124 | $new_queue = $this->_generator->generate(); |
| 125 | - if ( $new_queue instanceof EE_Messages_Queue ) { |
|
| 125 | + if ($new_queue instanceof EE_Messages_Queue) { |
|
| 126 | 126 | //unlock queue |
| 127 | 127 | $this->_queue->unlock_queue(); |
| 128 | - $new_queue->initiate_request_by_priority( 'send' ); |
|
| 128 | + $new_queue->initiate_request_by_priority('send'); |
|
| 129 | 129 | return $new_queue; |
| 130 | 130 | } |
| 131 | 131 | } |
@@ -146,24 +146,24 @@ discard block |
||
| 146 | 146 | * |
| 147 | 147 | * @return bool true means queue prepped, false means there was a lock so no generation please. |
| 148 | 148 | */ |
| 149 | - protected function _build_queue_for_generation( $messages = array(), $clear_queue = false ) { |
|
| 149 | + protected function _build_queue_for_generation($messages = array(), $clear_queue = false) { |
|
| 150 | 150 | |
| 151 | - if ( $clear_queue ) { |
|
| 151 | + if ($clear_queue) { |
|
| 152 | 152 | $this->_init_queue_and_generator(); |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - if ( $messages ) { |
|
| 155 | + if ($messages) { |
|
| 156 | 156 | //if generation is locked then get out now because that means processing is already happening. |
| 157 | - if ( $this->_queue->is_locked() ) { |
|
| 157 | + if ($this->_queue->is_locked()) { |
|
| 158 | 158 | return false; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | $this->_queue->lock_queue(); |
| 162 | - $messages = is_array( $messages ) ? $messages : array( $messages ); |
|
| 163 | - foreach ( $messages as $message ) { |
|
| 164 | - if ( $message instanceof EE_Message ) { |
|
| 162 | + $messages = is_array($messages) ? $messages : array($messages); |
|
| 163 | + foreach ($messages as $message) { |
|
| 164 | + if ($message instanceof EE_Message) { |
|
| 165 | 165 | $data = $message->all_extra_meta_array(); |
| 166 | - $this->_queue->add( $message, $data ); |
|
| 166 | + $this->_queue->add($message, $data); |
|
| 167 | 167 | } |
| 168 | 168 | } |
| 169 | 169 | return true; |
@@ -181,22 +181,22 @@ discard block |
||
| 181 | 181 | * |
| 182 | 182 | * @return bool true means queue prepped, false means there was a lock so no queue prepped. |
| 183 | 183 | */ |
| 184 | - protected function _build_queue_for_sending( $messages, $clear_queue = false ) { |
|
| 184 | + protected function _build_queue_for_sending($messages, $clear_queue = false) { |
|
| 185 | 185 | //if sending is locked then get out now because that means processing is already happening. |
| 186 | - if ( $this->_queue->is_locked( EE_Messages_Queue::action_sending ) ) { |
|
| 186 | + if ($this->_queue->is_locked(EE_Messages_Queue::action_sending)) { |
|
| 187 | 187 | return false; |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - $this->_queue->lock_queue( EE_Messages_Queue::action_sending ); |
|
| 190 | + $this->_queue->lock_queue(EE_Messages_Queue::action_sending); |
|
| 191 | 191 | |
| 192 | - if ( $clear_queue ) { |
|
| 192 | + if ($clear_queue) { |
|
| 193 | 193 | $this->_init_queue_and_generator(); |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | - $messages = is_array( $messages ) ? $messages : array( $messages ); |
|
| 196 | + $messages = is_array($messages) ? $messages : array($messages); |
|
| 197 | 197 | |
| 198 | - foreach ( $messages as $message ) { |
|
| 199 | - $this->_queue->add( $message ); |
|
| 198 | + foreach ($messages as $message) { |
|
| 199 | + $this->_queue->add($message); |
|
| 200 | 200 | } |
| 201 | 201 | return true; |
| 202 | 202 | } |
@@ -212,11 +212,11 @@ discard block |
||
| 212 | 212 | * |
| 213 | 213 | * @return EE_Messages_Queue |
| 214 | 214 | */ |
| 215 | - public function batch_send_from_queue( $messages = array(), $clear_queue = false ) { |
|
| 215 | + public function batch_send_from_queue($messages = array(), $clear_queue = false) { |
|
| 216 | 216 | |
| 217 | - if ( $messages && $this->_build_queue_for_sending( $messages, $clear_queue ) ) { |
|
| 217 | + if ($messages && $this->_build_queue_for_sending($messages, $clear_queue)) { |
|
| 218 | 218 | $this->_queue->execute(); |
| 219 | - $this->_queue->unlock_queue( EE_Messages_Queue::action_sending ); |
|
| 219 | + $this->_queue->unlock_queue(EE_Messages_Queue::action_sending); |
|
| 220 | 220 | } else { |
| 221 | 221 | //get messages to send and execute. |
| 222 | 222 | $this->_queue->get_to_send_batch_and_send(); |
@@ -239,10 +239,10 @@ discard block |
||
| 239 | 239 | * @param EE_Message_To_Generate[] $messages_to_generate |
| 240 | 240 | * @return EE_Messages_Queue |
| 241 | 241 | */ |
| 242 | - public function generate_and_return( $messages_to_generate ) { |
|
| 242 | + public function generate_and_return($messages_to_generate) { |
|
| 243 | 243 | $this->_init_queue_and_generator(); |
| 244 | - $this->_queue_for_generation_loop( $messages_to_generate ); |
|
| 245 | - return $this->_generator->generate( false ); |
|
| 244 | + $this->_queue_for_generation_loop($messages_to_generate); |
|
| 245 | + return $this->_generator->generate(false); |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | |
@@ -253,8 +253,8 @@ discard block |
||
| 253 | 253 | * @param bool $persist Indicate whether to instruct the generator to persist the generated queue (true) or not (false). |
| 254 | 254 | * @return EE_Messages_Queue |
| 255 | 255 | */ |
| 256 | - public function generate_queue( $persist = true ) { |
|
| 257 | - return $this->_generator->generate( $persist ); |
|
| 256 | + public function generate_queue($persist = true) { |
|
| 257 | + return $this->_generator->generate($persist); |
|
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | |
@@ -267,9 +267,9 @@ discard block |
||
| 267 | 267 | * @param bool $test_send Whether this item is for a test send or not. |
| 268 | 268 | * @return EE_Messages_Queue |
| 269 | 269 | */ |
| 270 | - public function queue_for_generation( EE_Message_To_Generate $message_to_generate, $test_send = false ) { |
|
| 271 | - if ( $message_to_generate->valid() ) { |
|
| 272 | - $this->_generator->create_and_add_message_to_queue( $message_to_generate, $test_send ); |
|
| 270 | + public function queue_for_generation(EE_Message_To_Generate $message_to_generate, $test_send = false) { |
|
| 271 | + if ($message_to_generate->valid()) { |
|
| 272 | + $this->_generator->create_and_add_message_to_queue($message_to_generate, $test_send); |
|
| 273 | 273 | } |
| 274 | 274 | } |
| 275 | 275 | |
@@ -285,9 +285,9 @@ discard block |
||
| 285 | 285 | * |
| 286 | 286 | * @param EE_Message_To_Generate[] $messages_to_generate |
| 287 | 287 | */ |
| 288 | - public function batch_queue_for_generation_and_persist( $messages_to_generate ) { |
|
| 288 | + public function batch_queue_for_generation_and_persist($messages_to_generate) { |
|
| 289 | 289 | $this->_init_queue_and_generator(); |
| 290 | - $this->_queue_for_generation_loop( $messages_to_generate ); |
|
| 290 | + $this->_queue_for_generation_loop($messages_to_generate); |
|
| 291 | 291 | $this->_queue->save(); |
| 292 | 292 | } |
| 293 | 293 | |
@@ -303,9 +303,9 @@ discard block |
||
| 303 | 303 | * |
| 304 | 304 | * @param EE_Message_To_Generate[] $messages_to_generate |
| 305 | 305 | */ |
| 306 | - public function batch_queue_for_generation_no_persist( $messages_to_generate ) { |
|
| 306 | + public function batch_queue_for_generation_no_persist($messages_to_generate) { |
|
| 307 | 307 | $this->_init_queue_and_generator(); |
| 308 | - $this->_queue_for_generation_loop( $messages_to_generate ); |
|
| 308 | + $this->_queue_for_generation_loop($messages_to_generate); |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | |
@@ -317,15 +317,15 @@ discard block |
||
| 317 | 317 | * |
| 318 | 318 | * @param EE_Message_To_Generate[] $messages_to_generate |
| 319 | 319 | */ |
| 320 | - protected function _queue_for_generation_loop( $messages_to_generate ) { |
|
| 320 | + protected function _queue_for_generation_loop($messages_to_generate) { |
|
| 321 | 321 | //make sure is in an array. |
| 322 | - if ( ! is_array( $messages_to_generate ) ) { |
|
| 323 | - $messages_to_generate = array( $messages_to_generate ); |
|
| 322 | + if ( ! is_array($messages_to_generate)) { |
|
| 323 | + $messages_to_generate = array($messages_to_generate); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | - foreach ( $messages_to_generate as $message_to_generate ) { |
|
| 327 | - if ( $message_to_generate instanceof EE_Message_To_Generate && $message_to_generate->valid() ) { |
|
| 328 | - $this->queue_for_generation( $message_to_generate ); |
|
| 326 | + foreach ($messages_to_generate as $message_to_generate) { |
|
| 327 | + if ($message_to_generate instanceof EE_Message_To_Generate && $message_to_generate->valid()) { |
|
| 328 | + $this->queue_for_generation($message_to_generate); |
|
| 329 | 329 | } |
| 330 | 330 | } |
| 331 | 331 | } |
@@ -340,10 +340,10 @@ discard block |
||
| 340 | 340 | * @param EE_Message_To_Generate[] |
| 341 | 341 | * @return EE_Messages_Queue |
| 342 | 342 | */ |
| 343 | - public function generate_and_queue_for_sending( $messages_to_generate ) { |
|
| 343 | + public function generate_and_queue_for_sending($messages_to_generate) { |
|
| 344 | 344 | $this->_init_queue_and_generator(); |
| 345 | - $this->_queue_for_generation_loop( $messages_to_generate ); |
|
| 346 | - return $this->_generator->generate( true ); |
|
| 345 | + $this->_queue_for_generation_loop($messages_to_generate); |
|
| 346 | + return $this->_generator->generate(true); |
|
| 347 | 347 | } |
| 348 | 348 | |
| 349 | 349 | |
@@ -357,10 +357,10 @@ discard block |
||
| 357 | 357 | * @param bool $test_send Whether this is a test send or not. |
| 358 | 358 | * @return EE_Messages_Queue | bool false if unable to generate otherwise the generated queue. |
| 359 | 359 | */ |
| 360 | - public function generate_for_preview( EE_Message_To_Generate $message_to_generate, $test_send = false ) { |
|
| 361 | - if ( ! $message_to_generate->valid() ) { |
|
| 360 | + public function generate_for_preview(EE_Message_To_Generate $message_to_generate, $test_send = false) { |
|
| 361 | + if ( ! $message_to_generate->valid()) { |
|
| 362 | 362 | EE_Error::add_error( |
| 363 | - __( 'Unable to generate preview because of invalid data', 'event_espresso' ), |
|
| 363 | + __('Unable to generate preview because of invalid data', 'event_espresso'), |
|
| 364 | 364 | __FILE__, |
| 365 | 365 | __FUNCTION__, |
| 366 | 366 | __LINE__ |
@@ -368,14 +368,14 @@ discard block |
||
| 368 | 368 | return false; |
| 369 | 369 | } |
| 370 | 370 | //just make sure preview is set on the $message_to_generate (in case client forgot) |
| 371 | - $message_to_generate->set_preview( true ); |
|
| 371 | + $message_to_generate->set_preview(true); |
|
| 372 | 372 | $this->_init_queue_and_generator(); |
| 373 | - $this->queue_for_generation( $message_to_generate, $test_send ); |
|
| 374 | - $generated_queue = $this->_generator->generate( false ); |
|
| 375 | - if ( $generated_queue->execute( false ) ) { |
|
| 373 | + $this->queue_for_generation($message_to_generate, $test_send); |
|
| 374 | + $generated_queue = $this->_generator->generate(false); |
|
| 375 | + if ($generated_queue->execute(false)) { |
|
| 376 | 376 | //the first queue item should be the preview |
| 377 | 377 | $generated_queue->get_message_repository()->rewind(); |
| 378 | - if ( ! $generated_queue->get_message_repository()->valid() ) { |
|
| 378 | + if ( ! $generated_queue->get_message_repository()->valid()) { |
|
| 379 | 379 | return $generated_queue; |
| 380 | 380 | } |
| 381 | 381 | return $generated_queue; |
@@ -392,15 +392,15 @@ discard block |
||
| 392 | 392 | * @param EE_Message_To_Generate $message_to_generate |
| 393 | 393 | * @return bool true or false for success. |
| 394 | 394 | */ |
| 395 | - public function queue_for_sending( EE_Message_To_Generate $message_to_generate ) { |
|
| 396 | - if ( ! $message_to_generate->valid() ) { |
|
| 395 | + public function queue_for_sending(EE_Message_To_Generate $message_to_generate) { |
|
| 396 | + if ( ! $message_to_generate->valid()) { |
|
| 397 | 397 | return false; |
| 398 | 398 | } |
| 399 | 399 | $this->_init_queue_and_generator(); |
| 400 | 400 | $message = $message_to_generate->get_EE_Message(); |
| 401 | - $this->_queue->add( $message ); |
|
| 402 | - if ( $message->send_now() ) { |
|
| 403 | - $this->_queue->execute( false ); |
|
| 401 | + $this->_queue->add($message); |
|
| 402 | + if ($message->send_now()) { |
|
| 403 | + $this->_queue->execute(false); |
|
| 404 | 404 | } else { |
| 405 | 405 | $this->_queue->save(); |
| 406 | 406 | } |
@@ -413,12 +413,12 @@ discard block |
||
| 413 | 413 | * @param EE_Message_To_Generate $message_to_generate |
| 414 | 414 | * @return EE_Messages_Queue | null |
| 415 | 415 | */ |
| 416 | - public function generate_and_send_now( EE_Message_To_Generate $message_to_generate ) { |
|
| 417 | - if ( ! $message_to_generate->valid() ) { |
|
| 416 | + public function generate_and_send_now(EE_Message_To_Generate $message_to_generate) { |
|
| 417 | + if ( ! $message_to_generate->valid()) { |
|
| 418 | 418 | return null; |
| 419 | 419 | } |
| 420 | 420 | // is there supposed to be a sending messenger for this message? |
| 421 | - if ( $message_to_generate instanceof EEI_Has_Sending_Messenger ) { |
|
| 421 | + if ($message_to_generate instanceof EEI_Has_Sending_Messenger) { |
|
| 422 | 422 | // make sure it's valid, but if it's not, |
| 423 | 423 | // then set the value of $sending_messenger to an EE_Error object |
| 424 | 424 | // so that downstream code can easily see that things went wrong. |
@@ -434,14 +434,14 @@ discard block |
||
| 434 | 434 | $sending_messenger = null; |
| 435 | 435 | } |
| 436 | 436 | |
| 437 | - if ( $message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_idle ) { |
|
| 437 | + if ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_idle) { |
|
| 438 | 438 | $this->_init_queue_and_generator(); |
| 439 | - $this->_queue->add( $message_to_generate->get_EE_Message() ); |
|
| 440 | - $this->_queue->execute( false, $sending_messenger ); |
|
| 439 | + $this->_queue->add($message_to_generate->get_EE_Message()); |
|
| 440 | + $this->_queue->execute(false, $sending_messenger); |
|
| 441 | 441 | return $this->_queue; |
| 442 | - } elseif ( $message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_incomplete ) { |
|
| 443 | - $generated_queue = $this->generate_and_return( array( $message_to_generate ) ); |
|
| 444 | - $generated_queue->execute( false, $sending_messenger ); |
|
| 442 | + } elseif ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_incomplete) { |
|
| 443 | + $generated_queue = $this->generate_and_return(array($message_to_generate)); |
|
| 444 | + $generated_queue->execute(false, $sending_messenger); |
|
| 445 | 445 | return $generated_queue; |
| 446 | 446 | } |
| 447 | 447 | return null; |
@@ -458,13 +458,13 @@ discard block |
||
| 458 | 458 | * @param mixed $data The data being used for generation. |
| 459 | 459 | * @param bool $persist Whether to persist the queued messages to the db or not. |
| 460 | 460 | */ |
| 461 | - public function generate_for_all_active_messengers( $message_type, $data, $persist = true ) { |
|
| 462 | - $messages_to_generate = $this->setup_mtgs_for_all_active_messengers( $message_type, $data ); |
|
| 463 | - if ( $persist ) { |
|
| 464 | - $this->batch_queue_for_generation_and_persist( $messages_to_generate ); |
|
| 461 | + public function generate_for_all_active_messengers($message_type, $data, $persist = true) { |
|
| 462 | + $messages_to_generate = $this->setup_mtgs_for_all_active_messengers($message_type, $data); |
|
| 463 | + if ($persist) { |
|
| 464 | + $this->batch_queue_for_generation_and_persist($messages_to_generate); |
|
| 465 | 465 | $this->_queue->initiate_request_by_priority(); |
| 466 | 466 | } else { |
| 467 | - $this->batch_queue_for_generation_no_persist( $messages_to_generate ); |
|
| 467 | + $this->batch_queue_for_generation_no_persist($messages_to_generate); |
|
| 468 | 468 | } |
| 469 | 469 | } |
| 470 | 470 | |
@@ -479,11 +479,11 @@ discard block |
||
| 479 | 479 | * |
| 480 | 480 | * @return EE_Message_To_Generate[] |
| 481 | 481 | */ |
| 482 | - public function setup_mtgs_for_all_active_messengers( $message_type, $data ) { |
|
| 482 | + public function setup_mtgs_for_all_active_messengers($message_type, $data) { |
|
| 483 | 483 | $messages_to_generate = array(); |
| 484 | - foreach ( $this->_message_resource_manager->active_messengers() as $messenger_slug => $messenger_object ) { |
|
| 485 | - $message_to_generate = new EE_Message_To_Generate( $messenger_slug, $message_type, $data ); |
|
| 486 | - if ( $message_to_generate->valid() ) { |
|
| 484 | + foreach ($this->_message_resource_manager->active_messengers() as $messenger_slug => $messenger_object) { |
|
| 485 | + $message_to_generate = new EE_Message_To_Generate($messenger_slug, $message_type, $data); |
|
| 486 | + if ($message_to_generate->valid()) { |
|
| 487 | 487 | $messages_to_generate[] = $message_to_generate; |
| 488 | 488 | } |
| 489 | 489 | } |
@@ -498,29 +498,29 @@ discard block |
||
| 498 | 498 | * and send. |
| 499 | 499 | * @param array $message_ids |
| 500 | 500 | */ |
| 501 | - public function setup_messages_from_ids_and_send( $message_ids ) { |
|
| 501 | + public function setup_messages_from_ids_and_send($message_ids) { |
|
| 502 | 502 | $this->_init_queue_and_generator(); |
| 503 | - $messages = EEM_Message::instance()->get_all( array( |
|
| 503 | + $messages = EEM_Message::instance()->get_all(array( |
|
| 504 | 504 | array( |
| 505 | - 'MSG_ID' => array( 'IN', $message_ids ), |
|
| 505 | + 'MSG_ID' => array('IN', $message_ids), |
|
| 506 | 506 | 'STS_ID' => array( |
| 507 | 507 | 'IN', |
| 508 | 508 | array_merge( |
| 509 | 509 | EEM_Message::instance()->stati_indicating_sent(), |
| 510 | - array( EEM_Message::status_retry ) |
|
| 510 | + array(EEM_Message::status_retry) |
|
| 511 | 511 | ), |
| 512 | 512 | ), |
| 513 | 513 | ), |
| 514 | 514 | )); |
| 515 | 515 | //set the Messages to resend. |
| 516 | - foreach ( $messages as $message ) { |
|
| 517 | - if ( $message instanceof EE_Message ) { |
|
| 518 | - $message->set_STS_ID( EEM_Message::status_resend ); |
|
| 519 | - $this->_queue->add( $message ); |
|
| 516 | + foreach ($messages as $message) { |
|
| 517 | + if ($message instanceof EE_Message) { |
|
| 518 | + $message->set_STS_ID(EEM_Message::status_resend); |
|
| 519 | + $this->_queue->add($message); |
|
| 520 | 520 | } |
| 521 | 521 | } |
| 522 | 522 | |
| 523 | - $this->_queue->initiate_request_by_priority( 'send' ); |
|
| 523 | + $this->_queue->initiate_request_by_priority('send'); |
|
| 524 | 524 | } |
| 525 | 525 | |
| 526 | 526 | |
@@ -534,23 +534,23 @@ discard block |
||
| 534 | 534 | * |
| 535 | 535 | * @return EE_Message_To_Generate[] |
| 536 | 536 | */ |
| 537 | - public function setup_messages_to_generate_from_registration_ids_in_request( $registration_ids_key = '_REG_ID' ) { |
|
| 538 | - EE_Registry::instance()->load_core( 'Request_Handler' ); |
|
| 539 | - EE_Registry::instance()->load_helper( 'MSG_Template' ); |
|
| 537 | + public function setup_messages_to_generate_from_registration_ids_in_request($registration_ids_key = '_REG_ID') { |
|
| 538 | + EE_Registry::instance()->load_core('Request_Handler'); |
|
| 539 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 540 | 540 | $regs_to_send = array(); |
| 541 | - $regIDs = EE_Registry::instance()->REQ->get( $registration_ids_key ); |
|
| 542 | - if ( empty( $regIDs ) ) { |
|
| 543 | - EE_Error::add_error( __('Something went wrong because we\'re missing the registration ID', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__ ); |
|
| 541 | + $regIDs = EE_Registry::instance()->REQ->get($registration_ids_key); |
|
| 542 | + if (empty($regIDs)) { |
|
| 543 | + EE_Error::add_error(__('Something went wrong because we\'re missing the registration ID', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 544 | 544 | return false; |
| 545 | 545 | } |
| 546 | 546 | |
| 547 | 547 | //make sure is an array |
| 548 | - $regIDs = is_array( $regIDs ) ? $regIDs : array( $regIDs ); |
|
| 548 | + $regIDs = is_array($regIDs) ? $regIDs : array($regIDs); |
|
| 549 | 549 | |
| 550 | - foreach( $regIDs as $regID ) { |
|
| 551 | - $reg = EEM_Registration::instance()->get_one_by_ID( $regID ); |
|
| 552 | - if ( ! $reg instanceof EE_Registration ) { |
|
| 553 | - EE_Error::add_error( sprintf( __('Unable to retrieve a registration object for the given reg id (%s)', 'event_espresso'), $regID ) ); |
|
| 550 | + foreach ($regIDs as $regID) { |
|
| 551 | + $reg = EEM_Registration::instance()->get_one_by_ID($regID); |
|
| 552 | + if ( ! $reg instanceof EE_Registration) { |
|
| 553 | + EE_Error::add_error(sprintf(__('Unable to retrieve a registration object for the given reg id (%s)', 'event_espresso'), $regID)); |
|
| 554 | 554 | return false; |
| 555 | 555 | } |
| 556 | 556 | $regs_to_send[$reg->transaction_ID()][$reg->status_ID()][] = $reg; |
@@ -558,17 +558,17 @@ discard block |
||
| 558 | 558 | |
| 559 | 559 | $messages_to_generate = array(); |
| 560 | 560 | |
| 561 | - foreach ( $regs_to_send as $status_group ) { |
|
| 562 | - foreach ( $status_group as $status_id => $registrations ) { |
|
| 561 | + foreach ($regs_to_send as $status_group) { |
|
| 562 | + foreach ($status_group as $status_id => $registrations) { |
|
| 563 | 563 | $message_type = EEH_MSG_Template::convert_reg_status_to_message_type($status_id); |
| 564 | - if (! $message_type) { |
|
| 564 | + if ( ! $message_type) { |
|
| 565 | 565 | continue; |
| 566 | 566 | } |
| 567 | 567 | $messages_to_generate = array_merge( |
| 568 | 568 | $messages_to_generate, |
| 569 | 569 | $this->setup_mtgs_for_all_active_messengers( |
| 570 | 570 | $message_type, |
| 571 | - array( $registrations, $status_id ) |
|
| 571 | + array($registrations, $status_id) |
|
| 572 | 572 | ) |
| 573 | 573 | ); |
| 574 | 574 | } |
@@ -18,64 +18,64 @@ |
||
| 18 | 18 | class Context |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var string $slug |
|
| 23 | - */ |
|
| 24 | - private $slug; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var string $description |
|
| 28 | - */ |
|
| 29 | - private $description; |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Context constructor. |
|
| 34 | - * |
|
| 35 | - * @param string $slug |
|
| 36 | - * @param string $description |
|
| 37 | - */ |
|
| 38 | - public function __construct($slug, $description) |
|
| 39 | - { |
|
| 40 | - $this->setSlug($slug); |
|
| 41 | - $this->setDescription($description); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @return string |
|
| 47 | - */ |
|
| 48 | - public function slug() |
|
| 49 | - { |
|
| 50 | - return $this->slug; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param string $slug |
|
| 56 | - */ |
|
| 57 | - private function setSlug($slug) |
|
| 58 | - { |
|
| 59 | - $this->slug = sanitize_key($slug); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @return string |
|
| 65 | - */ |
|
| 66 | - public function description() |
|
| 67 | - { |
|
| 68 | - return $this->description; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param string $description |
|
| 74 | - */ |
|
| 75 | - private function setDescription($description) |
|
| 76 | - { |
|
| 77 | - $this->description = sanitize_text_field($description); |
|
| 78 | - } |
|
| 21 | + /** |
|
| 22 | + * @var string $slug |
|
| 23 | + */ |
|
| 24 | + private $slug; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var string $description |
|
| 28 | + */ |
|
| 29 | + private $description; |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Context constructor. |
|
| 34 | + * |
|
| 35 | + * @param string $slug |
|
| 36 | + * @param string $description |
|
| 37 | + */ |
|
| 38 | + public function __construct($slug, $description) |
|
| 39 | + { |
|
| 40 | + $this->setSlug($slug); |
|
| 41 | + $this->setDescription($description); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @return string |
|
| 47 | + */ |
|
| 48 | + public function slug() |
|
| 49 | + { |
|
| 50 | + return $this->slug; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param string $slug |
|
| 56 | + */ |
|
| 57 | + private function setSlug($slug) |
|
| 58 | + { |
|
| 59 | + $this->slug = sanitize_key($slug); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @return string |
|
| 65 | + */ |
|
| 66 | + public function description() |
|
| 67 | + { |
|
| 68 | + return $this->description; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param string $description |
|
| 74 | + */ |
|
| 75 | + private function setDescription($description) |
|
| 76 | + { |
|
| 77 | + $this->description = sanitize_text_field($description); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | 80 | } |
| 81 | 81 | // Location: Context.php |
@@ -15,13 +15,13 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Domain extends DomainBase |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Slug used for the context where a registration status is changed from a manual trigger in the Registration Admin |
|
| 20 | - * Page ui. |
|
| 21 | - */ |
|
| 22 | - const CONTEXT_REGISTRATION_STATUS_CHANGE_REGISTRATION_ADMIN |
|
| 23 | - = 'manual_registration_status_change_from_registration_admin'; |
|
| 18 | + /** |
|
| 19 | + * Slug used for the context where a registration status is changed from a manual trigger in the Registration Admin |
|
| 20 | + * Page ui. |
|
| 21 | + */ |
|
| 22 | + const CONTEXT_REGISTRATION_STATUS_CHANGE_REGISTRATION_ADMIN |
|
| 23 | + = 'manual_registration_status_change_from_registration_admin'; |
|
| 24 | 24 | |
| 25 | - const CONTEXT_REGISTRATION_STATUS_CHANGE_REGISTRATION_ADMIN_NOTIFY |
|
| 26 | - = 'manual_registration_status_change_from_registration_admin_and_notify'; |
|
| 25 | + const CONTEXT_REGISTRATION_STATUS_CHANGE_REGISTRATION_ADMIN_NOTIFY |
|
| 26 | + = 'manual_registration_status_change_from_registration_admin_and_notify'; |
|
| 27 | 27 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 2 | - exit('No direct script access allowed'); |
|
| 2 | + exit('No direct script access allowed'); |
|
| 3 | 3 | } |
| 4 | 4 | |
| 5 | 5 | /** |
@@ -27,1980 +27,1980 @@ discard block |
||
| 27 | 27 | class Transactions_Admin_Page extends EE_Admin_Page |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var EE_Transaction |
|
| 32 | - */ |
|
| 33 | - private $_transaction; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var EE_Session |
|
| 37 | - */ |
|
| 38 | - private $_session; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var array $_txn_status |
|
| 42 | - */ |
|
| 43 | - private static $_txn_status; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var array $_pay_status |
|
| 47 | - */ |
|
| 48 | - private static $_pay_status; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var array $_existing_reg_payment_REG_IDs |
|
| 52 | - */ |
|
| 53 | - protected $_existing_reg_payment_REG_IDs = null; |
|
| 54 | - |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @Constructor |
|
| 58 | - * @access public |
|
| 59 | - * |
|
| 60 | - * @param bool $routing |
|
| 61 | - * |
|
| 62 | - * @return Transactions_Admin_Page |
|
| 63 | - */ |
|
| 64 | - public function __construct($routing = true) |
|
| 65 | - { |
|
| 66 | - parent::__construct($routing); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * _init_page_props |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - protected function _init_page_props() |
|
| 75 | - { |
|
| 76 | - $this->page_slug = TXN_PG_SLUG; |
|
| 77 | - $this->page_label = esc_html__('Transactions', 'event_espresso'); |
|
| 78 | - $this->_admin_base_url = TXN_ADMIN_URL; |
|
| 79 | - $this->_admin_base_path = TXN_ADMIN; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * _ajax_hooks |
|
| 85 | - * @return void |
|
| 86 | - */ |
|
| 87 | - protected function _ajax_hooks() |
|
| 88 | - { |
|
| 89 | - add_action('wp_ajax_espresso_apply_payment', array($this, 'apply_payments_or_refunds')); |
|
| 90 | - add_action('wp_ajax_espresso_apply_refund', array($this, 'apply_payments_or_refunds')); |
|
| 91 | - add_action('wp_ajax_espresso_delete_payment', array($this, 'delete_payment')); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * _define_page_props |
|
| 97 | - * @return void |
|
| 98 | - */ |
|
| 99 | - protected function _define_page_props() |
|
| 100 | - { |
|
| 101 | - $this->_admin_page_title = $this->page_label; |
|
| 102 | - $this->_labels = array( |
|
| 103 | - 'buttons' => array( |
|
| 104 | - 'add' => esc_html__('Add New Transaction', 'event_espresso'), |
|
| 105 | - 'edit' => esc_html__('Edit Transaction', 'event_espresso'), |
|
| 106 | - 'delete' => esc_html__('Delete Transaction', 'event_espresso'), |
|
| 107 | - ) |
|
| 108 | - ); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * grab url requests and route them |
|
| 114 | - * @access private |
|
| 115 | - * @return void |
|
| 116 | - */ |
|
| 117 | - public function _set_page_routes() |
|
| 118 | - { |
|
| 119 | - |
|
| 120 | - $this->_set_transaction_status_array(); |
|
| 121 | - |
|
| 122 | - $txn_id = ! empty($this->_req_data['TXN_ID']) && ! is_array($this->_req_data['TXN_ID']) ? $this->_req_data['TXN_ID'] : 0; |
|
| 123 | - |
|
| 124 | - $this->_page_routes = array( |
|
| 125 | - |
|
| 126 | - 'default' => array( |
|
| 127 | - 'func' => '_transactions_overview_list_table', |
|
| 128 | - 'capability' => 'ee_read_transactions' |
|
| 129 | - ), |
|
| 130 | - |
|
| 131 | - 'view_transaction' => array( |
|
| 132 | - 'func' => '_transaction_details', |
|
| 133 | - 'capability' => 'ee_read_transaction', |
|
| 134 | - 'obj_id' => $txn_id |
|
| 135 | - ), |
|
| 136 | - |
|
| 137 | - 'send_payment_reminder' => array( |
|
| 138 | - 'func' => '_send_payment_reminder', |
|
| 139 | - 'noheader' => true, |
|
| 140 | - 'capability' => 'ee_send_message' |
|
| 141 | - ), |
|
| 142 | - |
|
| 143 | - 'espresso_apply_payment' => array( |
|
| 144 | - 'func' => 'apply_payments_or_refunds', |
|
| 145 | - 'noheader' => true, |
|
| 146 | - 'capability' => 'ee_edit_payments' |
|
| 147 | - ), |
|
| 148 | - |
|
| 149 | - 'espresso_apply_refund' => array( |
|
| 150 | - 'func' => 'apply_payments_or_refunds', |
|
| 151 | - 'noheader' => true, |
|
| 152 | - 'capability' => 'ee_edit_payments' |
|
| 153 | - ), |
|
| 154 | - |
|
| 155 | - 'espresso_delete_payment' => array( |
|
| 156 | - 'func' => 'delete_payment', |
|
| 157 | - 'noheader' => true, |
|
| 158 | - 'capability' => 'ee_delete_payments' |
|
| 159 | - ), |
|
| 160 | - |
|
| 161 | - ); |
|
| 162 | - |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - protected function _set_page_config() |
|
| 167 | - { |
|
| 168 | - $this->_page_config = array( |
|
| 169 | - 'default' => array( |
|
| 170 | - 'nav' => array( |
|
| 171 | - 'label' => esc_html__('Overview', 'event_espresso'), |
|
| 172 | - 'order' => 10 |
|
| 173 | - ), |
|
| 174 | - 'list_table' => 'EE_Admin_Transactions_List_Table', |
|
| 175 | - 'help_tabs' => array( |
|
| 176 | - 'transactions_overview_help_tab' => array( |
|
| 177 | - 'title' => esc_html__('Transactions Overview', 'event_espresso'), |
|
| 178 | - 'filename' => 'transactions_overview' |
|
| 179 | - ), |
|
| 180 | - 'transactions_overview_table_column_headings_help_tab' => array( |
|
| 181 | - 'title' => esc_html__('Transactions Table Column Headings', 'event_espresso'), |
|
| 182 | - 'filename' => 'transactions_overview_table_column_headings' |
|
| 183 | - ), |
|
| 184 | - 'transactions_overview_views_filters_help_tab' => array( |
|
| 185 | - 'title' => esc_html__('Transaction Views & Filters & Search', 'event_espresso'), |
|
| 186 | - 'filename' => 'transactions_overview_views_filters_search' |
|
| 187 | - ), |
|
| 188 | - ), |
|
| 189 | - 'help_tour' => array('Transactions_Overview_Help_Tour'), |
|
| 190 | - /** |
|
| 191 | - * commented out because currently we are not displaying tips for transaction list table status but this |
|
| 192 | - * may change in a later iteration so want to keep the code for then. |
|
| 193 | - */ |
|
| 194 | - //'qtips' => array( 'Transactions_List_Table_Tips' ), |
|
| 195 | - 'require_nonce' => false |
|
| 196 | - ), |
|
| 197 | - 'view_transaction' => array( |
|
| 198 | - 'nav' => array( |
|
| 199 | - 'label' => esc_html__('View Transaction', 'event_espresso'), |
|
| 200 | - 'order' => 5, |
|
| 201 | - 'url' => isset($this->_req_data['TXN_ID']) ? add_query_arg(array('TXN_ID' => $this->_req_data['TXN_ID']), |
|
| 202 | - $this->_current_page_view_url) : $this->_admin_base_url, |
|
| 203 | - 'persistent' => false |
|
| 204 | - ), |
|
| 205 | - 'help_tabs' => array( |
|
| 206 | - 'transactions_view_transaction_help_tab' => array( |
|
| 207 | - 'title' => esc_html__('View Transaction', 'event_espresso'), |
|
| 208 | - 'filename' => 'transactions_view_transaction' |
|
| 209 | - ), |
|
| 210 | - 'transactions_view_transaction_transaction_details_table_help_tab' => array( |
|
| 211 | - 'title' => esc_html__('Transaction Details Table', 'event_espresso'), |
|
| 212 | - 'filename' => 'transactions_view_transaction_transaction_details_table' |
|
| 213 | - ), |
|
| 214 | - 'transactions_view_transaction_attendees_registered_help_tab' => array( |
|
| 215 | - 'title' => esc_html__('Attendees Registered', 'event_espresso'), |
|
| 216 | - 'filename' => 'transactions_view_transaction_attendees_registered' |
|
| 217 | - ), |
|
| 218 | - 'transactions_view_transaction_views_primary_registrant_billing_information_help_tab' => array( |
|
| 219 | - 'title' => esc_html__('Primary Registrant & Billing Information', 'event_espresso'), |
|
| 220 | - 'filename' => 'transactions_view_transaction_primary_registrant_billing_information' |
|
| 221 | - ), |
|
| 222 | - ), |
|
| 223 | - 'qtips' => array('Transaction_Details_Tips'), |
|
| 224 | - 'help_tour' => array('Transaction_Details_Help_Tour'), |
|
| 225 | - 'metaboxes' => array('_transaction_details_metaboxes'), |
|
| 226 | - |
|
| 227 | - 'require_nonce' => false |
|
| 228 | - ) |
|
| 229 | - ); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * The below methods aren't used by this class currently |
|
| 235 | - */ |
|
| 236 | - protected function _add_screen_options() |
|
| 237 | - { |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - protected function _add_feature_pointers() |
|
| 241 | - { |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - public function admin_init() |
|
| 245 | - { |
|
| 246 | - // IF a registration was JUST added via the admin... |
|
| 247 | - if ( |
|
| 248 | - isset( |
|
| 249 | - $this->_req_data['redirect_from'], |
|
| 250 | - $this->_req_data['EVT_ID'], |
|
| 251 | - $this->_req_data['event_name'] |
|
| 252 | - ) |
|
| 253 | - ) { |
|
| 254 | - // then set a cookie so that we can block any attempts to use |
|
| 255 | - // the back button as a way to enter another registration. |
|
| 256 | - setcookie('ee_registration_added', $this->_req_data['EVT_ID'], time() + WEEK_IN_SECONDS, '/'); |
|
| 257 | - // and update the global |
|
| 258 | - $_COOKIE['ee_registration_added'] = $this->_req_data['EVT_ID']; |
|
| 259 | - } |
|
| 260 | - EE_Registry::$i18n_js_strings['invalid_server_response'] = esc_html__('An error occurred! Your request may have been processed, but a valid response from the server was not received. Please refresh the page and try again.', |
|
| 261 | - 'event_espresso'); |
|
| 262 | - EE_Registry::$i18n_js_strings['error_occurred'] = esc_html__('An error occurred! Please refresh the page and try again.', |
|
| 263 | - 'event_espresso'); |
|
| 264 | - EE_Registry::$i18n_js_strings['txn_status_array'] = self::$_txn_status; |
|
| 265 | - EE_Registry::$i18n_js_strings['pay_status_array'] = self::$_pay_status; |
|
| 266 | - EE_Registry::$i18n_js_strings['payments_total'] = esc_html__('Payments Total', 'event_espresso'); |
|
| 267 | - EE_Registry::$i18n_js_strings['transaction_overpaid'] = esc_html__('This transaction has been overpaid ! Payments Total', |
|
| 268 | - 'event_espresso'); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - public function admin_notices() |
|
| 272 | - { |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - public function admin_footer_scripts() |
|
| 276 | - { |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * _set_transaction_status_array |
|
| 282 | - * sets list of transaction statuses |
|
| 283 | - * |
|
| 284 | - * @access private |
|
| 285 | - * @return void |
|
| 286 | - */ |
|
| 287 | - private function _set_transaction_status_array() |
|
| 288 | - { |
|
| 289 | - self::$_txn_status = EEM_Transaction::instance()->status_array(true); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * get_transaction_status_array |
|
| 295 | - * return the transaction status array for wp_list_table |
|
| 296 | - * |
|
| 297 | - * @access public |
|
| 298 | - * @return array |
|
| 299 | - */ |
|
| 300 | - public function get_transaction_status_array() |
|
| 301 | - { |
|
| 302 | - return self::$_txn_status; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * get list of payment statuses |
|
| 308 | - * |
|
| 309 | - * @access private |
|
| 310 | - * @return void |
|
| 311 | - */ |
|
| 312 | - private function _get_payment_status_array() |
|
| 313 | - { |
|
| 314 | - self::$_pay_status = EEM_Payment::instance()->status_array(true); |
|
| 315 | - $this->_template_args['payment_status'] = self::$_pay_status; |
|
| 316 | - |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * _add_screen_options_default |
|
| 322 | - * |
|
| 323 | - * @access protected |
|
| 324 | - * @return void |
|
| 325 | - */ |
|
| 326 | - protected function _add_screen_options_default() |
|
| 327 | - { |
|
| 328 | - $this->_per_page_screen_option(); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * load_scripts_styles |
|
| 334 | - * |
|
| 335 | - * @access public |
|
| 336 | - * @return void |
|
| 337 | - */ |
|
| 338 | - public function load_scripts_styles() |
|
| 339 | - { |
|
| 340 | - //enqueue style |
|
| 341 | - wp_register_style('espresso_txn', TXN_ASSETS_URL . 'espresso_transactions_admin.css', array(), |
|
| 342 | - EVENT_ESPRESSO_VERSION); |
|
| 343 | - wp_enqueue_style('espresso_txn'); |
|
| 344 | - //scripts |
|
| 345 | - wp_register_script('espresso_txn', TXN_ASSETS_URL . 'espresso_transactions_admin.js', array( |
|
| 346 | - 'ee_admin_js', |
|
| 347 | - 'ee-datepicker', |
|
| 348 | - 'jquery-ui-datepicker', |
|
| 349 | - 'jquery-ui-draggable', |
|
| 350 | - 'ee-dialog', |
|
| 351 | - 'ee-accounting', |
|
| 352 | - 'ee-serialize-full-array' |
|
| 353 | - ), EVENT_ESPRESSO_VERSION, true); |
|
| 354 | - wp_enqueue_script('espresso_txn'); |
|
| 355 | - |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * load_scripts_styles_view_transaction |
|
| 361 | - * |
|
| 362 | - * @access public |
|
| 363 | - * @return void |
|
| 364 | - */ |
|
| 365 | - public function load_scripts_styles_view_transaction() |
|
| 366 | - { |
|
| 367 | - //styles |
|
| 368 | - wp_enqueue_style('espresso-ui-theme'); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * load_scripts_styles_default |
|
| 374 | - * |
|
| 375 | - * @access public |
|
| 376 | - * @return void |
|
| 377 | - */ |
|
| 378 | - public function load_scripts_styles_default() |
|
| 379 | - { |
|
| 380 | - //styles |
|
| 381 | - wp_enqueue_style('espresso-ui-theme'); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * _set_list_table_views_default |
|
| 387 | - * |
|
| 388 | - * @access protected |
|
| 389 | - * @return void |
|
| 390 | - */ |
|
| 391 | - protected function _set_list_table_views_default() |
|
| 392 | - { |
|
| 393 | - $this->_views = array( |
|
| 394 | - 'all' => array( |
|
| 395 | - 'slug' => 'all', |
|
| 396 | - 'label' => esc_html__('View All Transactions', 'event_espresso'), |
|
| 397 | - 'count' => 0 |
|
| 398 | - ), |
|
| 399 | - 'abandoned' => array( |
|
| 400 | - 'slug' => 'abandoned', |
|
| 401 | - 'label' => esc_html__('Abandoned Transactions', 'event_espresso'), |
|
| 402 | - 'count' => 0 |
|
| 403 | - ), |
|
| 404 | - 'failed' => array( |
|
| 405 | - 'slug' => 'failed', |
|
| 406 | - 'label' => esc_html__('Failed Transactions', 'event_espresso'), |
|
| 407 | - 'count' => 0 |
|
| 408 | - ) |
|
| 409 | - ); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * _set_transaction_object |
|
| 415 | - * This sets the _transaction property for the transaction details screen |
|
| 416 | - * |
|
| 417 | - * @access private |
|
| 418 | - * @return void |
|
| 419 | - */ |
|
| 420 | - private function _set_transaction_object() |
|
| 421 | - { |
|
| 422 | - if (is_object($this->_transaction)) { |
|
| 423 | - return; |
|
| 424 | - } //get out we've already set the object |
|
| 425 | - |
|
| 426 | - $TXN = EEM_Transaction::instance(); |
|
| 427 | - |
|
| 428 | - $TXN_ID = ( ! empty($this->_req_data['TXN_ID'])) ? absint($this->_req_data['TXN_ID']) : false; |
|
| 429 | - |
|
| 430 | - //get transaction object |
|
| 431 | - $this->_transaction = $TXN->get_one_by_ID($TXN_ID); |
|
| 432 | - $this->_session = ! empty($this->_transaction) ? $this->_transaction->get('TXN_session_data') : null; |
|
| 433 | - $this->_transaction->verify_abandoned_transaction_status(); |
|
| 434 | - |
|
| 435 | - if (empty($this->_transaction)) { |
|
| 436 | - $error_msg = esc_html__('An error occurred and the details for Transaction ID #', |
|
| 437 | - 'event_espresso') . $TXN_ID . esc_html__(' could not be retrieved.', 'event_espresso'); |
|
| 438 | - EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * _transaction_legend_items |
|
| 445 | - * |
|
| 446 | - * @access protected |
|
| 447 | - * @return array |
|
| 448 | - */ |
|
| 449 | - protected function _transaction_legend_items() |
|
| 450 | - { |
|
| 451 | - EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 452 | - $items = array(); |
|
| 453 | - |
|
| 454 | - if (EE_Registry::instance()->CAP->current_user_can('ee_read_global_messages', 'view_filtered_messages')) { |
|
| 455 | - $related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for'); |
|
| 456 | - if (isset($related_for_icon['css_class']) && isset($related_for_icon['label'])) { |
|
| 457 | - $items['view_related_messages'] = array( |
|
| 458 | - 'class' => $related_for_icon['css_class'], |
|
| 459 | - 'desc' => $related_for_icon['label'], |
|
| 460 | - ); |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - $items = apply_filters( |
|
| 465 | - 'FHEE__Transactions_Admin_Page___transaction_legend_items__items', |
|
| 466 | - array_merge($items, |
|
| 467 | - array( |
|
| 468 | - 'view_details' => array( |
|
| 469 | - 'class' => 'dashicons dashicons-cart', |
|
| 470 | - 'desc' => esc_html__('View Transaction Details', 'event_espresso') |
|
| 471 | - ), |
|
| 472 | - 'view_invoice' => array( |
|
| 473 | - 'class' => 'dashicons dashicons-media-spreadsheet', |
|
| 474 | - 'desc' => esc_html__('View Transaction Invoice', 'event_espresso') |
|
| 475 | - ), |
|
| 476 | - 'view_receipt' => array( |
|
| 477 | - 'class' => 'dashicons dashicons-media-default', |
|
| 478 | - 'desc' => esc_html__('View Transaction Receipt', 'event_espresso') |
|
| 479 | - ), |
|
| 480 | - 'view_registration' => array( |
|
| 481 | - 'class' => 'dashicons dashicons-clipboard', |
|
| 482 | - 'desc' => esc_html__('View Registration Details', 'event_espresso') |
|
| 483 | - ), |
|
| 484 | - 'payment_overview_link' => array( |
|
| 485 | - 'class' => 'dashicons dashicons-money', |
|
| 486 | - 'desc' => esc_html__('Make Payment on Frontend', 'event_espresso') |
|
| 487 | - ) |
|
| 488 | - ) |
|
| 489 | - ) |
|
| 490 | - ); |
|
| 491 | - |
|
| 492 | - if (EE_Registry::instance()->CAP->current_user_can('ee_send_message', |
|
| 493 | - 'espresso_transactions_send_payment_reminder') |
|
| 494 | - ) { |
|
| 495 | - if (EEH_MSG_Template::is_mt_active('payment_reminder')) { |
|
| 496 | - $items['send_payment_reminder'] = array( |
|
| 497 | - 'class' => 'dashicons dashicons-email-alt', |
|
| 498 | - 'desc' => esc_html__('Send Payment Reminder', 'event_espresso') |
|
| 499 | - ); |
|
| 500 | - } else { |
|
| 501 | - $items['blank*'] = array( |
|
| 502 | - 'class' => '', |
|
| 503 | - 'desc' => '' |
|
| 504 | - ); |
|
| 505 | - } |
|
| 506 | - } else { |
|
| 507 | - $items['blank*'] = array( |
|
| 508 | - 'class' => '', |
|
| 509 | - 'desc' => '' |
|
| 510 | - ); |
|
| 511 | - } |
|
| 512 | - $more_items = apply_filters( |
|
| 513 | - 'FHEE__Transactions_Admin_Page___transaction_legend_items__more_items', |
|
| 514 | - array( |
|
| 515 | - 'overpaid' => array( |
|
| 516 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::overpaid_status_code, |
|
| 517 | - 'desc' => EEH_Template::pretty_status(EEM_Transaction::overpaid_status_code, false, 'sentence') |
|
| 518 | - ), |
|
| 519 | - 'complete' => array( |
|
| 520 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::complete_status_code, |
|
| 521 | - 'desc' => EEH_Template::pretty_status(EEM_Transaction::complete_status_code, false, 'sentence') |
|
| 522 | - ), |
|
| 523 | - 'incomplete' => array( |
|
| 524 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::incomplete_status_code, |
|
| 525 | - 'desc' => EEH_Template::pretty_status(EEM_Transaction::incomplete_status_code, false, 'sentence') |
|
| 526 | - ), |
|
| 527 | - 'abandoned' => array( |
|
| 528 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::abandoned_status_code, |
|
| 529 | - 'desc' => EEH_Template::pretty_status(EEM_Transaction::abandoned_status_code, false, 'sentence') |
|
| 530 | - ), |
|
| 531 | - 'failed' => array( |
|
| 532 | - 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::failed_status_code, |
|
| 533 | - 'desc' => EEH_Template::pretty_status(EEM_Transaction::failed_status_code, false, 'sentence') |
|
| 534 | - ) |
|
| 535 | - ) |
|
| 536 | - ); |
|
| 537 | - |
|
| 538 | - return array_merge($items, $more_items); |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * _transactions_overview_list_table |
|
| 544 | - * |
|
| 545 | - * @access protected |
|
| 546 | - * @return void |
|
| 547 | - */ |
|
| 548 | - protected function _transactions_overview_list_table() |
|
| 549 | - { |
|
| 550 | - $this->_admin_page_title = esc_html__('Transactions', 'event_espresso'); |
|
| 551 | - $event = isset($this->_req_data['EVT_ID']) ? EEM_Event::instance()->get_one_by_ID($this->_req_data['EVT_ID']) : null; |
|
| 552 | - $this->_template_args['admin_page_header'] = $event instanceof EE_Event ? sprintf(esc_html__('%sViewing Transactions for the Event: %s%s', |
|
| 553 | - 'event_espresso'), '<h3>', |
|
| 554 | - '<a href="' . EE_Admin_Page::add_query_args_and_nonce(array('action' => 'edit', 'post' => $event->ID()), |
|
| 555 | - EVENTS_ADMIN_URL) . '" title="' . esc_attr__('Click to Edit event', |
|
| 556 | - 'event_espresso') . '">' . $event->get('EVT_name') . '</a>', '</h3>') : ''; |
|
| 557 | - $this->_template_args['after_list_table'] = $this->_display_legend($this->_transaction_legend_items()); |
|
| 558 | - $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * _transaction_details |
|
| 564 | - * generates HTML for the View Transaction Details Admin page |
|
| 565 | - * |
|
| 566 | - * @access protected |
|
| 567 | - * @return void |
|
| 568 | - */ |
|
| 569 | - protected function _transaction_details() |
|
| 570 | - { |
|
| 571 | - do_action('AHEE__Transactions_Admin_Page__transaction_details__start', $this->_transaction); |
|
| 572 | - |
|
| 573 | - $this->_set_transaction_status_array(); |
|
| 574 | - |
|
| 575 | - $this->_template_args = array(); |
|
| 576 | - $this->_template_args['transactions_page'] = $this->_wp_page_slug; |
|
| 577 | - |
|
| 578 | - $this->_set_transaction_object(); |
|
| 579 | - |
|
| 580 | - $primary_registration = $this->_transaction->primary_registration(); |
|
| 581 | - $attendee = $primary_registration instanceof EE_Registration ? $primary_registration->attendee() : null; |
|
| 582 | - |
|
| 583 | - $this->_template_args['txn_nmbr']['value'] = $this->_transaction->ID(); |
|
| 584 | - $this->_template_args['txn_nmbr']['label'] = esc_html__('Transaction Number', 'event_espresso'); |
|
| 585 | - |
|
| 586 | - $this->_template_args['txn_datetime']['value'] = $this->_transaction->get_i18n_datetime('TXN_timestamp'); |
|
| 587 | - $this->_template_args['txn_datetime']['label'] = esc_html__('Date', 'event_espresso'); |
|
| 588 | - |
|
| 589 | - $this->_template_args['txn_status']['value'] = self::$_txn_status[$this->_transaction->get('STS_ID')]; |
|
| 590 | - $this->_template_args['txn_status']['label'] = esc_html__('Transaction Status', 'event_espresso'); |
|
| 591 | - $this->_template_args['txn_status']['class'] = 'status-' . $this->_transaction->get('STS_ID'); |
|
| 592 | - |
|
| 593 | - $this->_template_args['grand_total'] = $this->_transaction->get('TXN_total'); |
|
| 594 | - $this->_template_args['total_paid'] = $this->_transaction->get('TXN_paid'); |
|
| 595 | - |
|
| 596 | - if ( |
|
| 597 | - $attendee instanceof EE_Attendee |
|
| 598 | - && EE_Registry::instance()->CAP->current_user_can( |
|
| 599 | - 'ee_send_message', |
|
| 600 | - 'espresso_transactions_send_payment_reminder' |
|
| 601 | - ) |
|
| 602 | - ) { |
|
| 603 | - $this->_template_args['send_payment_reminder_button'] = |
|
| 604 | - EEH_MSG_Template::is_mt_active('payment_reminder') |
|
| 605 | - && $this->_transaction->get('STS_ID') != EEM_Transaction::complete_status_code |
|
| 606 | - && $this->_transaction->get('STS_ID') != EEM_Transaction::overpaid_status_code |
|
| 607 | - ? EEH_Template::get_button_or_link( |
|
| 608 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 609 | - array( |
|
| 610 | - 'action' => 'send_payment_reminder', |
|
| 611 | - 'TXN_ID' => $this->_transaction->ID(), |
|
| 612 | - 'redirect_to' => 'view_transaction' |
|
| 613 | - ), |
|
| 614 | - TXN_ADMIN_URL |
|
| 615 | - ), |
|
| 616 | - __(' Send Payment Reminder', 'event_espresso'), |
|
| 617 | - 'button secondary-button right', |
|
| 618 | - 'dashicons dashicons-email-alt' |
|
| 619 | - ) |
|
| 620 | - : ''; |
|
| 621 | - } else { |
|
| 622 | - $this->_template_args['send_payment_reminder_button'] = ''; |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - $amount_due = $this->_transaction->get('TXN_total') - $this->_transaction->get('TXN_paid'); |
|
| 626 | - $this->_template_args['amount_due'] = EEH_Template::format_currency($amount_due, true); |
|
| 627 | - if (EE_Registry::instance()->CFG->currency->sign_b4) { |
|
| 628 | - $this->_template_args['amount_due'] = EE_Registry::instance()->CFG->currency->sign . $this->_template_args['amount_due']; |
|
| 629 | - } else { |
|
| 630 | - $this->_template_args['amount_due'] = $this->_template_args['amount_due'] . EE_Registry::instance()->CFG->currency->sign; |
|
| 631 | - } |
|
| 632 | - $this->_template_args['amount_due_class'] = ''; |
|
| 633 | - |
|
| 634 | - if ($this->_transaction->get('TXN_paid') == $this->_transaction->get('TXN_total')) { |
|
| 635 | - // paid in full |
|
| 636 | - $this->_template_args['amount_due'] = false; |
|
| 637 | - } elseif ($this->_transaction->get('TXN_paid') > $this->_transaction->get('TXN_total')) { |
|
| 638 | - // overpaid |
|
| 639 | - $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn'; |
|
| 640 | - } elseif (($this->_transaction->get('TXN_total') > 0) && ($this->_transaction->get('TXN_paid') > 0)) { |
|
| 641 | - // monies owing |
|
| 642 | - $this->_template_args['amount_due_class'] = 'txn-overview-part-payment-spn'; |
|
| 643 | - } elseif (($this->_transaction->get('TXN_total') > 0) && ($this->_transaction->get('TXN_paid') == 0)) { |
|
| 644 | - // no payments made yet |
|
| 645 | - $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn'; |
|
| 646 | - } elseif ($this->_transaction->get('TXN_total') == 0) { |
|
| 647 | - // free event |
|
| 648 | - $this->_template_args['amount_due'] = false; |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - $payment_method = $this->_transaction->payment_method(); |
|
| 652 | - |
|
| 653 | - $this->_template_args['method_of_payment_name'] = $payment_method instanceof EE_Payment_Method |
|
| 654 | - ? $payment_method->admin_name() |
|
| 655 | - : esc_html__('Unknown', 'event_espresso'); |
|
| 656 | - |
|
| 657 | - $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
|
| 658 | - // link back to overview |
|
| 659 | - $this->_template_args['txn_overview_url'] = ! empty ($_SERVER['HTTP_REFERER']) |
|
| 660 | - ? $_SERVER['HTTP_REFERER'] |
|
| 661 | - : TXN_ADMIN_URL; |
|
| 662 | - |
|
| 663 | - |
|
| 664 | - // next link |
|
| 665 | - $next_txn = $this->_transaction->next( |
|
| 666 | - null, |
|
| 667 | - array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))), |
|
| 668 | - 'TXN_ID' |
|
| 669 | - ); |
|
| 670 | - $this->_template_args['next_transaction'] = $next_txn |
|
| 671 | - ? $this->_next_link( |
|
| 672 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 673 | - array('action' => 'view_transaction', 'TXN_ID' => $next_txn['TXN_ID']), |
|
| 674 | - TXN_ADMIN_URL |
|
| 675 | - ), |
|
| 676 | - 'dashicons dashicons-arrow-right ee-icon-size-22' |
|
| 677 | - ) |
|
| 678 | - : ''; |
|
| 679 | - // previous link |
|
| 680 | - $previous_txn = $this->_transaction->previous( |
|
| 681 | - null, |
|
| 682 | - array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))), |
|
| 683 | - 'TXN_ID' |
|
| 684 | - ); |
|
| 685 | - $this->_template_args['previous_transaction'] = $previous_txn |
|
| 686 | - ? $this->_previous_link( |
|
| 687 | - EE_Admin_Page::add_query_args_and_nonce( |
|
| 688 | - array('action' => 'view_transaction', 'TXN_ID' => $previous_txn['TXN_ID']), |
|
| 689 | - TXN_ADMIN_URL |
|
| 690 | - ), |
|
| 691 | - 'dashicons dashicons-arrow-left ee-icon-size-22' |
|
| 692 | - ) |
|
| 693 | - : ''; |
|
| 694 | - |
|
| 695 | - // were we just redirected here after adding a new registration ??? |
|
| 696 | - if ( |
|
| 697 | - isset( |
|
| 698 | - $this->_req_data['redirect_from'], |
|
| 699 | - $this->_req_data['EVT_ID'], |
|
| 700 | - $this->_req_data['event_name'] |
|
| 701 | - ) |
|
| 702 | - ) { |
|
| 703 | - if ( |
|
| 704 | - EE_Registry::instance()->CAP->current_user_can( |
|
| 705 | - 'ee_edit_registrations', |
|
| 706 | - 'espresso_registrations_new_registration', |
|
| 707 | - $this->_req_data['EVT_ID'] |
|
| 708 | - ) |
|
| 709 | - ) { |
|
| 710 | - $this->_admin_page_title .= '<a id="add-new-registration" class="add-new-h2 button-primary" href="'; |
|
| 711 | - $this->_admin_page_title .= EE_Admin_Page::add_query_args_and_nonce( |
|
| 712 | - array( |
|
| 713 | - 'page' => 'espresso_registrations', |
|
| 714 | - 'action' => 'new_registration', |
|
| 715 | - 'return' => 'default', |
|
| 716 | - 'TXN_ID' => $this->_transaction->ID(), |
|
| 717 | - 'event_id' => $this->_req_data['EVT_ID'], |
|
| 718 | - ), |
|
| 719 | - REG_ADMIN_URL |
|
| 720 | - ); |
|
| 721 | - $this->_admin_page_title .= '">'; |
|
| 722 | - |
|
| 723 | - $this->_admin_page_title .= sprintf( |
|
| 724 | - esc_html__('Add Another New Registration to Event: "%1$s" ?', 'event_espresso'), |
|
| 725 | - htmlentities(urldecode($this->_req_data['event_name']), ENT_QUOTES, 'UTF-8') |
|
| 726 | - ); |
|
| 727 | - $this->_admin_page_title .= '</a>'; |
|
| 728 | - } |
|
| 729 | - EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 730 | - } |
|
| 731 | - // grab messages at the last second |
|
| 732 | - $this->_template_args['notices'] = EE_Error::get_notices(); |
|
| 733 | - // path to template |
|
| 734 | - $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_header.template.php'; |
|
| 735 | - $this->_template_args['admin_page_header'] = EEH_Template::display_template($template_path, |
|
| 736 | - $this->_template_args, true); |
|
| 737 | - |
|
| 738 | - // the details template wrapper |
|
| 739 | - $this->display_admin_page_with_sidebar(); |
|
| 740 | - |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - |
|
| 744 | - /** |
|
| 745 | - * _transaction_details_metaboxes |
|
| 746 | - * |
|
| 747 | - * @access protected |
|
| 748 | - * @return void |
|
| 749 | - */ |
|
| 750 | - protected function _transaction_details_metaboxes() |
|
| 751 | - { |
|
| 752 | - |
|
| 753 | - $this->_set_transaction_object(); |
|
| 754 | - |
|
| 755 | - add_meta_box('edit-txn-details-mbox', esc_html__('Transaction Details', 'event_espresso'), |
|
| 756 | - array($this, 'txn_details_meta_box'), $this->_wp_page_slug, 'normal', 'high'); |
|
| 757 | - add_meta_box( |
|
| 758 | - 'edit-txn-attendees-mbox', |
|
| 759 | - esc_html__('Attendees Registered in this Transaction', 'event_espresso'), |
|
| 760 | - array($this, 'txn_attendees_meta_box'), |
|
| 761 | - $this->_wp_page_slug, |
|
| 762 | - 'normal', |
|
| 763 | - 'high', |
|
| 764 | - array('TXN_ID' => $this->_transaction->ID()) |
|
| 765 | - ); |
|
| 766 | - add_meta_box('edit-txn-registrant-mbox', esc_html__('Primary Contact', 'event_espresso'), |
|
| 767 | - array($this, 'txn_registrant_side_meta_box'), $this->_wp_page_slug, 'side', 'high'); |
|
| 768 | - add_meta_box('edit-txn-billing-info-mbox', esc_html__('Billing Information', 'event_espresso'), |
|
| 769 | - array($this, 'txn_billing_info_side_meta_box'), $this->_wp_page_slug, 'side', 'high'); |
|
| 770 | - |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - |
|
| 774 | - /** |
|
| 775 | - * txn_details_meta_box |
|
| 776 | - * generates HTML for the Transaction main meta box |
|
| 777 | - * |
|
| 778 | - * @access public |
|
| 779 | - * @return void |
|
| 780 | - */ |
|
| 781 | - public function txn_details_meta_box() |
|
| 782 | - { |
|
| 783 | - |
|
| 784 | - $this->_set_transaction_object(); |
|
| 785 | - $this->_template_args['TXN_ID'] = $this->_transaction->ID(); |
|
| 786 | - $this->_template_args['attendee'] = $this->_transaction->primary_registration() instanceof EE_Registration ? $this->_transaction->primary_registration()->attendee() : null; |
|
| 787 | - |
|
| 788 | - //get line table |
|
| 789 | - EEH_Autoloader::register_line_item_display_autoloaders(); |
|
| 790 | - $Line_Item_Display = new EE_Line_Item_Display('admin_table', |
|
| 791 | - 'EE_Admin_Table_Line_Item_Display_Strategy'); |
|
| 792 | - $this->_template_args['line_item_table'] = $Line_Item_Display->display_line_item($this->_transaction->total_line_item()); |
|
| 793 | - $this->_template_args['REG_code'] = $this->_transaction->get_first_related('Registration')->get('REG_code'); |
|
| 794 | - |
|
| 795 | - // process taxes |
|
| 796 | - $taxes = $this->_transaction->get_many_related('Line_Item', |
|
| 797 | - array(array('LIN_type' => EEM_Line_Item::type_tax))); |
|
| 798 | - $this->_template_args['taxes'] = ! empty($taxes) ? $taxes : false; |
|
| 799 | - |
|
| 800 | - $this->_template_args['grand_total'] = EEH_Template::format_currency($this->_transaction->get('TXN_total'), |
|
| 801 | - false, false); |
|
| 802 | - $this->_template_args['grand_raw_total'] = $this->_transaction->get('TXN_total'); |
|
| 803 | - $this->_template_args['TXN_status'] = $this->_transaction->get('STS_ID'); |
|
| 30 | + /** |
|
| 31 | + * @var EE_Transaction |
|
| 32 | + */ |
|
| 33 | + private $_transaction; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var EE_Session |
|
| 37 | + */ |
|
| 38 | + private $_session; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var array $_txn_status |
|
| 42 | + */ |
|
| 43 | + private static $_txn_status; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var array $_pay_status |
|
| 47 | + */ |
|
| 48 | + private static $_pay_status; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var array $_existing_reg_payment_REG_IDs |
|
| 52 | + */ |
|
| 53 | + protected $_existing_reg_payment_REG_IDs = null; |
|
| 54 | + |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @Constructor |
|
| 58 | + * @access public |
|
| 59 | + * |
|
| 60 | + * @param bool $routing |
|
| 61 | + * |
|
| 62 | + * @return Transactions_Admin_Page |
|
| 63 | + */ |
|
| 64 | + public function __construct($routing = true) |
|
| 65 | + { |
|
| 66 | + parent::__construct($routing); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * _init_page_props |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + protected function _init_page_props() |
|
| 75 | + { |
|
| 76 | + $this->page_slug = TXN_PG_SLUG; |
|
| 77 | + $this->page_label = esc_html__('Transactions', 'event_espresso'); |
|
| 78 | + $this->_admin_base_url = TXN_ADMIN_URL; |
|
| 79 | + $this->_admin_base_path = TXN_ADMIN; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * _ajax_hooks |
|
| 85 | + * @return void |
|
| 86 | + */ |
|
| 87 | + protected function _ajax_hooks() |
|
| 88 | + { |
|
| 89 | + add_action('wp_ajax_espresso_apply_payment', array($this, 'apply_payments_or_refunds')); |
|
| 90 | + add_action('wp_ajax_espresso_apply_refund', array($this, 'apply_payments_or_refunds')); |
|
| 91 | + add_action('wp_ajax_espresso_delete_payment', array($this, 'delete_payment')); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * _define_page_props |
|
| 97 | + * @return void |
|
| 98 | + */ |
|
| 99 | + protected function _define_page_props() |
|
| 100 | + { |
|
| 101 | + $this->_admin_page_title = $this->page_label; |
|
| 102 | + $this->_labels = array( |
|
| 103 | + 'buttons' => array( |
|
| 104 | + 'add' => esc_html__('Add New Transaction', 'event_espresso'), |
|
| 105 | + 'edit' => esc_html__('Edit Transaction', 'event_espresso'), |
|
| 106 | + 'delete' => esc_html__('Delete Transaction', 'event_espresso'), |
|
| 107 | + ) |
|
| 108 | + ); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * grab url requests and route them |
|
| 114 | + * @access private |
|
| 115 | + * @return void |
|
| 116 | + */ |
|
| 117 | + public function _set_page_routes() |
|
| 118 | + { |
|
| 119 | + |
|
| 120 | + $this->_set_transaction_status_array(); |
|
| 121 | + |
|
| 122 | + $txn_id = ! empty($this->_req_data['TXN_ID']) && ! is_array($this->_req_data['TXN_ID']) ? $this->_req_data['TXN_ID'] : 0; |
|
| 123 | + |
|
| 124 | + $this->_page_routes = array( |
|
| 125 | + |
|
| 126 | + 'default' => array( |
|
| 127 | + 'func' => '_transactions_overview_list_table', |
|
| 128 | + 'capability' => 'ee_read_transactions' |
|
| 129 | + ), |
|
| 130 | + |
|
| 131 | + 'view_transaction' => array( |
|
| 132 | + 'func' => '_transaction_details', |
|
| 133 | + 'capability' => 'ee_read_transaction', |
|
| 134 | + 'obj_id' => $txn_id |
|
| 135 | + ), |
|
| 136 | + |
|
| 137 | + 'send_payment_reminder' => array( |
|
| 138 | + 'func' => '_send_payment_reminder', |
|
| 139 | + 'noheader' => true, |
|
| 140 | + 'capability' => 'ee_send_message' |
|
| 141 | + ), |
|
| 142 | + |
|
| 143 | + 'espresso_apply_payment' => array( |
|
| 144 | + 'func' => 'apply_payments_or_refunds', |
|
| 145 | + 'noheader' => true, |
|
| 146 | + 'capability' => 'ee_edit_payments' |
|
| 147 | + ), |
|
| 148 | + |
|
| 149 | + 'espresso_apply_refund' => array( |
|
| 150 | + 'func' => 'apply_payments_or_refunds', |
|
| 151 | + 'noheader' => true, |
|
| 152 | + 'capability' => 'ee_edit_payments' |
|
| 153 | + ), |
|
| 154 | + |
|
| 155 | + 'espresso_delete_payment' => array( |
|
| 156 | + 'func' => 'delete_payment', |
|
| 157 | + 'noheader' => true, |
|
| 158 | + 'capability' => 'ee_delete_payments' |
|
| 159 | + ), |
|
| 160 | + |
|
| 161 | + ); |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + protected function _set_page_config() |
|
| 167 | + { |
|
| 168 | + $this->_page_config = array( |
|
| 169 | + 'default' => array( |
|
| 170 | + 'nav' => array( |
|
| 171 | + 'label' => esc_html__('Overview', 'event_espresso'), |
|
| 172 | + 'order' => 10 |
|
| 173 | + ), |
|
| 174 | + 'list_table' => 'EE_Admin_Transactions_List_Table', |
|
| 175 | + 'help_tabs' => array( |
|
| 176 | + 'transactions_overview_help_tab' => array( |
|
| 177 | + 'title' => esc_html__('Transactions Overview', 'event_espresso'), |
|
| 178 | + 'filename' => 'transactions_overview' |
|
| 179 | + ), |
|
| 180 | + 'transactions_overview_table_column_headings_help_tab' => array( |
|
| 181 | + 'title' => esc_html__('Transactions Table Column Headings', 'event_espresso'), |
|
| 182 | + 'filename' => 'transactions_overview_table_column_headings' |
|
| 183 | + ), |
|
| 184 | + 'transactions_overview_views_filters_help_tab' => array( |
|
| 185 | + 'title' => esc_html__('Transaction Views & Filters & Search', 'event_espresso'), |
|
| 186 | + 'filename' => 'transactions_overview_views_filters_search' |
|
| 187 | + ), |
|
| 188 | + ), |
|
| 189 | + 'help_tour' => array('Transactions_Overview_Help_Tour'), |
|
| 190 | + /** |
|
| 191 | + * commented out because currently we are not displaying tips for transaction list table status but this |
|
| 192 | + * may change in a later iteration so want to keep the code for then. |
|
| 193 | + */ |
|
| 194 | + //'qtips' => array( 'Transactions_List_Table_Tips' ), |
|
| 195 | + 'require_nonce' => false |
|
| 196 | + ), |
|
| 197 | + 'view_transaction' => array( |
|
| 198 | + 'nav' => array( |
|
| 199 | + 'label' => esc_html__('View Transaction', 'event_espresso'), |
|
| 200 | + 'order' => 5, |
|
| 201 | + 'url' => isset($this->_req_data['TXN_ID']) ? add_query_arg(array('TXN_ID' => $this->_req_data['TXN_ID']), |
|
| 202 | + $this->_current_page_view_url) : $this->_admin_base_url, |
|
| 203 | + 'persistent' => false |
|
| 204 | + ), |
|
| 205 | + 'help_tabs' => array( |
|
| 206 | + 'transactions_view_transaction_help_tab' => array( |
|
| 207 | + 'title' => esc_html__('View Transaction', 'event_espresso'), |
|
| 208 | + 'filename' => 'transactions_view_transaction' |
|
| 209 | + ), |
|
| 210 | + 'transactions_view_transaction_transaction_details_table_help_tab' => array( |
|
| 211 | + 'title' => esc_html__('Transaction Details Table', 'event_espresso'), |
|
| 212 | + 'filename' => 'transactions_view_transaction_transaction_details_table' |
|
| 213 | + ), |
|
| 214 | + 'transactions_view_transaction_attendees_registered_help_tab' => array( |
|
| 215 | + 'title' => esc_html__('Attendees Registered', 'event_espresso'), |
|
| 216 | + 'filename' => 'transactions_view_transaction_attendees_registered' |
|
| 217 | + ), |
|
| 218 | + 'transactions_view_transaction_views_primary_registrant_billing_information_help_tab' => array( |
|
| 219 | + 'title' => esc_html__('Primary Registrant & Billing Information', 'event_espresso'), |
|
| 220 | + 'filename' => 'transactions_view_transaction_primary_registrant_billing_information' |
|
| 221 | + ), |
|
| 222 | + ), |
|
| 223 | + 'qtips' => array('Transaction_Details_Tips'), |
|
| 224 | + 'help_tour' => array('Transaction_Details_Help_Tour'), |
|
| 225 | + 'metaboxes' => array('_transaction_details_metaboxes'), |
|
| 226 | + |
|
| 227 | + 'require_nonce' => false |
|
| 228 | + ) |
|
| 229 | + ); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * The below methods aren't used by this class currently |
|
| 235 | + */ |
|
| 236 | + protected function _add_screen_options() |
|
| 237 | + { |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + protected function _add_feature_pointers() |
|
| 241 | + { |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + public function admin_init() |
|
| 245 | + { |
|
| 246 | + // IF a registration was JUST added via the admin... |
|
| 247 | + if ( |
|
| 248 | + isset( |
|
| 249 | + $this->_req_data['redirect_from'], |
|
| 250 | + $this->_req_data['EVT_ID'], |
|
| 251 | + $this->_req_data['event_name'] |
|
| 252 | + ) |
|
| 253 | + ) { |
|
| 254 | + // then set a cookie so that we can block any attempts to use |
|
| 255 | + // the back button as a way to enter another registration. |
|
| 256 | + setcookie('ee_registration_added', $this->_req_data['EVT_ID'], time() + WEEK_IN_SECONDS, '/'); |
|
| 257 | + // and update the global |
|
| 258 | + $_COOKIE['ee_registration_added'] = $this->_req_data['EVT_ID']; |
|
| 259 | + } |
|
| 260 | + EE_Registry::$i18n_js_strings['invalid_server_response'] = esc_html__('An error occurred! Your request may have been processed, but a valid response from the server was not received. Please refresh the page and try again.', |
|
| 261 | + 'event_espresso'); |
|
| 262 | + EE_Registry::$i18n_js_strings['error_occurred'] = esc_html__('An error occurred! Please refresh the page and try again.', |
|
| 263 | + 'event_espresso'); |
|
| 264 | + EE_Registry::$i18n_js_strings['txn_status_array'] = self::$_txn_status; |
|
| 265 | + EE_Registry::$i18n_js_strings['pay_status_array'] = self::$_pay_status; |
|
| 266 | + EE_Registry::$i18n_js_strings['payments_total'] = esc_html__('Payments Total', 'event_espresso'); |
|
| 267 | + EE_Registry::$i18n_js_strings['transaction_overpaid'] = esc_html__('This transaction has been overpaid ! Payments Total', |
|
| 268 | + 'event_espresso'); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + public function admin_notices() |
|
| 272 | + { |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + public function admin_footer_scripts() |
|
| 276 | + { |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * _set_transaction_status_array |
|
| 282 | + * sets list of transaction statuses |
|
| 283 | + * |
|
| 284 | + * @access private |
|
| 285 | + * @return void |
|
| 286 | + */ |
|
| 287 | + private function _set_transaction_status_array() |
|
| 288 | + { |
|
| 289 | + self::$_txn_status = EEM_Transaction::instance()->status_array(true); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * get_transaction_status_array |
|
| 295 | + * return the transaction status array for wp_list_table |
|
| 296 | + * |
|
| 297 | + * @access public |
|
| 298 | + * @return array |
|
| 299 | + */ |
|
| 300 | + public function get_transaction_status_array() |
|
| 301 | + { |
|
| 302 | + return self::$_txn_status; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * get list of payment statuses |
|
| 308 | + * |
|
| 309 | + * @access private |
|
| 310 | + * @return void |
|
| 311 | + */ |
|
| 312 | + private function _get_payment_status_array() |
|
| 313 | + { |
|
| 314 | + self::$_pay_status = EEM_Payment::instance()->status_array(true); |
|
| 315 | + $this->_template_args['payment_status'] = self::$_pay_status; |
|
| 316 | + |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * _add_screen_options_default |
|
| 322 | + * |
|
| 323 | + * @access protected |
|
| 324 | + * @return void |
|
| 325 | + */ |
|
| 326 | + protected function _add_screen_options_default() |
|
| 327 | + { |
|
| 328 | + $this->_per_page_screen_option(); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * load_scripts_styles |
|
| 334 | + * |
|
| 335 | + * @access public |
|
| 336 | + * @return void |
|
| 337 | + */ |
|
| 338 | + public function load_scripts_styles() |
|
| 339 | + { |
|
| 340 | + //enqueue style |
|
| 341 | + wp_register_style('espresso_txn', TXN_ASSETS_URL . 'espresso_transactions_admin.css', array(), |
|
| 342 | + EVENT_ESPRESSO_VERSION); |
|
| 343 | + wp_enqueue_style('espresso_txn'); |
|
| 344 | + //scripts |
|
| 345 | + wp_register_script('espresso_txn', TXN_ASSETS_URL . 'espresso_transactions_admin.js', array( |
|
| 346 | + 'ee_admin_js', |
|
| 347 | + 'ee-datepicker', |
|
| 348 | + 'jquery-ui-datepicker', |
|
| 349 | + 'jquery-ui-draggable', |
|
| 350 | + 'ee-dialog', |
|
| 351 | + 'ee-accounting', |
|
| 352 | + 'ee-serialize-full-array' |
|
| 353 | + ), EVENT_ESPRESSO_VERSION, true); |
|
| 354 | + wp_enqueue_script('espresso_txn'); |
|
| 355 | + |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * load_scripts_styles_view_transaction |
|
| 361 | + * |
|
| 362 | + * @access public |
|
| 363 | + * @return void |
|
| 364 | + */ |
|
| 365 | + public function load_scripts_styles_view_transaction() |
|
| 366 | + { |
|
| 367 | + //styles |
|
| 368 | + wp_enqueue_style('espresso-ui-theme'); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * load_scripts_styles_default |
|
| 374 | + * |
|
| 375 | + * @access public |
|
| 376 | + * @return void |
|
| 377 | + */ |
|
| 378 | + public function load_scripts_styles_default() |
|
| 379 | + { |
|
| 380 | + //styles |
|
| 381 | + wp_enqueue_style('espresso-ui-theme'); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * _set_list_table_views_default |
|
| 387 | + * |
|
| 388 | + * @access protected |
|
| 389 | + * @return void |
|
| 390 | + */ |
|
| 391 | + protected function _set_list_table_views_default() |
|
| 392 | + { |
|
| 393 | + $this->_views = array( |
|
| 394 | + 'all' => array( |
|
| 395 | + 'slug' => 'all', |
|
| 396 | + 'label' => esc_html__('View All Transactions', 'event_espresso'), |
|
| 397 | + 'count' => 0 |
|
| 398 | + ), |
|
| 399 | + 'abandoned' => array( |
|
| 400 | + 'slug' => 'abandoned', |
|
| 401 | + 'label' => esc_html__('Abandoned Transactions', 'event_espresso'), |
|
| 402 | + 'count' => 0 |
|
| 403 | + ), |
|
| 404 | + 'failed' => array( |
|
| 405 | + 'slug' => 'failed', |
|
| 406 | + 'label' => esc_html__('Failed Transactions', 'event_espresso'), |
|
| 407 | + 'count' => 0 |
|
| 408 | + ) |
|
| 409 | + ); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * _set_transaction_object |
|
| 415 | + * This sets the _transaction property for the transaction details screen |
|
| 416 | + * |
|
| 417 | + * @access private |
|
| 418 | + * @return void |
|
| 419 | + */ |
|
| 420 | + private function _set_transaction_object() |
|
| 421 | + { |
|
| 422 | + if (is_object($this->_transaction)) { |
|
| 423 | + return; |
|
| 424 | + } //get out we've already set the object |
|
| 425 | + |
|
| 426 | + $TXN = EEM_Transaction::instance(); |
|
| 427 | + |
|
| 428 | + $TXN_ID = ( ! empty($this->_req_data['TXN_ID'])) ? absint($this->_req_data['TXN_ID']) : false; |
|
| 429 | + |
|
| 430 | + //get transaction object |
|
| 431 | + $this->_transaction = $TXN->get_one_by_ID($TXN_ID); |
|
| 432 | + $this->_session = ! empty($this->_transaction) ? $this->_transaction->get('TXN_session_data') : null; |
|
| 433 | + $this->_transaction->verify_abandoned_transaction_status(); |
|
| 434 | + |
|
| 435 | + if (empty($this->_transaction)) { |
|
| 436 | + $error_msg = esc_html__('An error occurred and the details for Transaction ID #', |
|
| 437 | + 'event_espresso') . $TXN_ID . esc_html__(' could not be retrieved.', 'event_espresso'); |
|
| 438 | + EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * _transaction_legend_items |
|
| 445 | + * |
|
| 446 | + * @access protected |
|
| 447 | + * @return array |
|
| 448 | + */ |
|
| 449 | + protected function _transaction_legend_items() |
|
| 450 | + { |
|
| 451 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 452 | + $items = array(); |
|
| 453 | + |
|
| 454 | + if (EE_Registry::instance()->CAP->current_user_can('ee_read_global_messages', 'view_filtered_messages')) { |
|
| 455 | + $related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for'); |
|
| 456 | + if (isset($related_for_icon['css_class']) && isset($related_for_icon['label'])) { |
|
| 457 | + $items['view_related_messages'] = array( |
|
| 458 | + 'class' => $related_for_icon['css_class'], |
|
| 459 | + 'desc' => $related_for_icon['label'], |
|
| 460 | + ); |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + $items = apply_filters( |
|
| 465 | + 'FHEE__Transactions_Admin_Page___transaction_legend_items__items', |
|
| 466 | + array_merge($items, |
|
| 467 | + array( |
|
| 468 | + 'view_details' => array( |
|
| 469 | + 'class' => 'dashicons dashicons-cart', |
|
| 470 | + 'desc' => esc_html__('View Transaction Details', 'event_espresso') |
|
| 471 | + ), |
|
| 472 | + 'view_invoice' => array( |
|
| 473 | + 'class' => 'dashicons dashicons-media-spreadsheet', |
|
| 474 | + 'desc' => esc_html__('View Transaction Invoice', 'event_espresso') |
|
| 475 | + ), |
|
| 476 | + 'view_receipt' => array( |
|
| 477 | + 'class' => 'dashicons dashicons-media-default', |
|
| 478 | + 'desc' => esc_html__('View Transaction Receipt', 'event_espresso') |
|
| 479 | + ), |
|
| 480 | + 'view_registration' => array( |
|
| 481 | + 'class' => 'dashicons dashicons-clipboard', |
|
| 482 | + 'desc' => esc_html__('View Registration Details', 'event_espresso') |
|
| 483 | + ), |
|
| 484 | + 'payment_overview_link' => array( |
|
| 485 | + 'class' => 'dashicons dashicons-money', |
|
| 486 | + 'desc' => esc_html__('Make Payment on Frontend', 'event_espresso') |
|
| 487 | + ) |
|
| 488 | + ) |
|
| 489 | + ) |
|
| 490 | + ); |
|
| 491 | + |
|
| 492 | + if (EE_Registry::instance()->CAP->current_user_can('ee_send_message', |
|
| 493 | + 'espresso_transactions_send_payment_reminder') |
|
| 494 | + ) { |
|
| 495 | + if (EEH_MSG_Template::is_mt_active('payment_reminder')) { |
|
| 496 | + $items['send_payment_reminder'] = array( |
|
| 497 | + 'class' => 'dashicons dashicons-email-alt', |
|
| 498 | + 'desc' => esc_html__('Send Payment Reminder', 'event_espresso') |
|
| 499 | + ); |
|
| 500 | + } else { |
|
| 501 | + $items['blank*'] = array( |
|
| 502 | + 'class' => '', |
|
| 503 | + 'desc' => '' |
|
| 504 | + ); |
|
| 505 | + } |
|
| 506 | + } else { |
|
| 507 | + $items['blank*'] = array( |
|
| 508 | + 'class' => '', |
|
| 509 | + 'desc' => '' |
|
| 510 | + ); |
|
| 511 | + } |
|
| 512 | + $more_items = apply_filters( |
|
| 513 | + 'FHEE__Transactions_Admin_Page___transaction_legend_items__more_items', |
|
| 514 | + array( |
|
| 515 | + 'overpaid' => array( |
|
| 516 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::overpaid_status_code, |
|
| 517 | + 'desc' => EEH_Template::pretty_status(EEM_Transaction::overpaid_status_code, false, 'sentence') |
|
| 518 | + ), |
|
| 519 | + 'complete' => array( |
|
| 520 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::complete_status_code, |
|
| 521 | + 'desc' => EEH_Template::pretty_status(EEM_Transaction::complete_status_code, false, 'sentence') |
|
| 522 | + ), |
|
| 523 | + 'incomplete' => array( |
|
| 524 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::incomplete_status_code, |
|
| 525 | + 'desc' => EEH_Template::pretty_status(EEM_Transaction::incomplete_status_code, false, 'sentence') |
|
| 526 | + ), |
|
| 527 | + 'abandoned' => array( |
|
| 528 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::abandoned_status_code, |
|
| 529 | + 'desc' => EEH_Template::pretty_status(EEM_Transaction::abandoned_status_code, false, 'sentence') |
|
| 530 | + ), |
|
| 531 | + 'failed' => array( |
|
| 532 | + 'class' => 'ee-status-legend ee-status-legend-' . EEM_Transaction::failed_status_code, |
|
| 533 | + 'desc' => EEH_Template::pretty_status(EEM_Transaction::failed_status_code, false, 'sentence') |
|
| 534 | + ) |
|
| 535 | + ) |
|
| 536 | + ); |
|
| 537 | + |
|
| 538 | + return array_merge($items, $more_items); |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * _transactions_overview_list_table |
|
| 544 | + * |
|
| 545 | + * @access protected |
|
| 546 | + * @return void |
|
| 547 | + */ |
|
| 548 | + protected function _transactions_overview_list_table() |
|
| 549 | + { |
|
| 550 | + $this->_admin_page_title = esc_html__('Transactions', 'event_espresso'); |
|
| 551 | + $event = isset($this->_req_data['EVT_ID']) ? EEM_Event::instance()->get_one_by_ID($this->_req_data['EVT_ID']) : null; |
|
| 552 | + $this->_template_args['admin_page_header'] = $event instanceof EE_Event ? sprintf(esc_html__('%sViewing Transactions for the Event: %s%s', |
|
| 553 | + 'event_espresso'), '<h3>', |
|
| 554 | + '<a href="' . EE_Admin_Page::add_query_args_and_nonce(array('action' => 'edit', 'post' => $event->ID()), |
|
| 555 | + EVENTS_ADMIN_URL) . '" title="' . esc_attr__('Click to Edit event', |
|
| 556 | + 'event_espresso') . '">' . $event->get('EVT_name') . '</a>', '</h3>') : ''; |
|
| 557 | + $this->_template_args['after_list_table'] = $this->_display_legend($this->_transaction_legend_items()); |
|
| 558 | + $this->display_admin_list_table_page_with_no_sidebar(); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * _transaction_details |
|
| 564 | + * generates HTML for the View Transaction Details Admin page |
|
| 565 | + * |
|
| 566 | + * @access protected |
|
| 567 | + * @return void |
|
| 568 | + */ |
|
| 569 | + protected function _transaction_details() |
|
| 570 | + { |
|
| 571 | + do_action('AHEE__Transactions_Admin_Page__transaction_details__start', $this->_transaction); |
|
| 572 | + |
|
| 573 | + $this->_set_transaction_status_array(); |
|
| 574 | + |
|
| 575 | + $this->_template_args = array(); |
|
| 576 | + $this->_template_args['transactions_page'] = $this->_wp_page_slug; |
|
| 577 | + |
|
| 578 | + $this->_set_transaction_object(); |
|
| 579 | + |
|
| 580 | + $primary_registration = $this->_transaction->primary_registration(); |
|
| 581 | + $attendee = $primary_registration instanceof EE_Registration ? $primary_registration->attendee() : null; |
|
| 582 | + |
|
| 583 | + $this->_template_args['txn_nmbr']['value'] = $this->_transaction->ID(); |
|
| 584 | + $this->_template_args['txn_nmbr']['label'] = esc_html__('Transaction Number', 'event_espresso'); |
|
| 585 | + |
|
| 586 | + $this->_template_args['txn_datetime']['value'] = $this->_transaction->get_i18n_datetime('TXN_timestamp'); |
|
| 587 | + $this->_template_args['txn_datetime']['label'] = esc_html__('Date', 'event_espresso'); |
|
| 588 | + |
|
| 589 | + $this->_template_args['txn_status']['value'] = self::$_txn_status[$this->_transaction->get('STS_ID')]; |
|
| 590 | + $this->_template_args['txn_status']['label'] = esc_html__('Transaction Status', 'event_espresso'); |
|
| 591 | + $this->_template_args['txn_status']['class'] = 'status-' . $this->_transaction->get('STS_ID'); |
|
| 592 | + |
|
| 593 | + $this->_template_args['grand_total'] = $this->_transaction->get('TXN_total'); |
|
| 594 | + $this->_template_args['total_paid'] = $this->_transaction->get('TXN_paid'); |
|
| 595 | + |
|
| 596 | + if ( |
|
| 597 | + $attendee instanceof EE_Attendee |
|
| 598 | + && EE_Registry::instance()->CAP->current_user_can( |
|
| 599 | + 'ee_send_message', |
|
| 600 | + 'espresso_transactions_send_payment_reminder' |
|
| 601 | + ) |
|
| 602 | + ) { |
|
| 603 | + $this->_template_args['send_payment_reminder_button'] = |
|
| 604 | + EEH_MSG_Template::is_mt_active('payment_reminder') |
|
| 605 | + && $this->_transaction->get('STS_ID') != EEM_Transaction::complete_status_code |
|
| 606 | + && $this->_transaction->get('STS_ID') != EEM_Transaction::overpaid_status_code |
|
| 607 | + ? EEH_Template::get_button_or_link( |
|
| 608 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 609 | + array( |
|
| 610 | + 'action' => 'send_payment_reminder', |
|
| 611 | + 'TXN_ID' => $this->_transaction->ID(), |
|
| 612 | + 'redirect_to' => 'view_transaction' |
|
| 613 | + ), |
|
| 614 | + TXN_ADMIN_URL |
|
| 615 | + ), |
|
| 616 | + __(' Send Payment Reminder', 'event_espresso'), |
|
| 617 | + 'button secondary-button right', |
|
| 618 | + 'dashicons dashicons-email-alt' |
|
| 619 | + ) |
|
| 620 | + : ''; |
|
| 621 | + } else { |
|
| 622 | + $this->_template_args['send_payment_reminder_button'] = ''; |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + $amount_due = $this->_transaction->get('TXN_total') - $this->_transaction->get('TXN_paid'); |
|
| 626 | + $this->_template_args['amount_due'] = EEH_Template::format_currency($amount_due, true); |
|
| 627 | + if (EE_Registry::instance()->CFG->currency->sign_b4) { |
|
| 628 | + $this->_template_args['amount_due'] = EE_Registry::instance()->CFG->currency->sign . $this->_template_args['amount_due']; |
|
| 629 | + } else { |
|
| 630 | + $this->_template_args['amount_due'] = $this->_template_args['amount_due'] . EE_Registry::instance()->CFG->currency->sign; |
|
| 631 | + } |
|
| 632 | + $this->_template_args['amount_due_class'] = ''; |
|
| 633 | + |
|
| 634 | + if ($this->_transaction->get('TXN_paid') == $this->_transaction->get('TXN_total')) { |
|
| 635 | + // paid in full |
|
| 636 | + $this->_template_args['amount_due'] = false; |
|
| 637 | + } elseif ($this->_transaction->get('TXN_paid') > $this->_transaction->get('TXN_total')) { |
|
| 638 | + // overpaid |
|
| 639 | + $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn'; |
|
| 640 | + } elseif (($this->_transaction->get('TXN_total') > 0) && ($this->_transaction->get('TXN_paid') > 0)) { |
|
| 641 | + // monies owing |
|
| 642 | + $this->_template_args['amount_due_class'] = 'txn-overview-part-payment-spn'; |
|
| 643 | + } elseif (($this->_transaction->get('TXN_total') > 0) && ($this->_transaction->get('TXN_paid') == 0)) { |
|
| 644 | + // no payments made yet |
|
| 645 | + $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn'; |
|
| 646 | + } elseif ($this->_transaction->get('TXN_total') == 0) { |
|
| 647 | + // free event |
|
| 648 | + $this->_template_args['amount_due'] = false; |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + $payment_method = $this->_transaction->payment_method(); |
|
| 652 | + |
|
| 653 | + $this->_template_args['method_of_payment_name'] = $payment_method instanceof EE_Payment_Method |
|
| 654 | + ? $payment_method->admin_name() |
|
| 655 | + : esc_html__('Unknown', 'event_espresso'); |
|
| 656 | + |
|
| 657 | + $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
|
| 658 | + // link back to overview |
|
| 659 | + $this->_template_args['txn_overview_url'] = ! empty ($_SERVER['HTTP_REFERER']) |
|
| 660 | + ? $_SERVER['HTTP_REFERER'] |
|
| 661 | + : TXN_ADMIN_URL; |
|
| 662 | + |
|
| 663 | + |
|
| 664 | + // next link |
|
| 665 | + $next_txn = $this->_transaction->next( |
|
| 666 | + null, |
|
| 667 | + array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))), |
|
| 668 | + 'TXN_ID' |
|
| 669 | + ); |
|
| 670 | + $this->_template_args['next_transaction'] = $next_txn |
|
| 671 | + ? $this->_next_link( |
|
| 672 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 673 | + array('action' => 'view_transaction', 'TXN_ID' => $next_txn['TXN_ID']), |
|
| 674 | + TXN_ADMIN_URL |
|
| 675 | + ), |
|
| 676 | + 'dashicons dashicons-arrow-right ee-icon-size-22' |
|
| 677 | + ) |
|
| 678 | + : ''; |
|
| 679 | + // previous link |
|
| 680 | + $previous_txn = $this->_transaction->previous( |
|
| 681 | + null, |
|
| 682 | + array(array('STS_ID' => array('!=', EEM_Transaction::failed_status_code))), |
|
| 683 | + 'TXN_ID' |
|
| 684 | + ); |
|
| 685 | + $this->_template_args['previous_transaction'] = $previous_txn |
|
| 686 | + ? $this->_previous_link( |
|
| 687 | + EE_Admin_Page::add_query_args_and_nonce( |
|
| 688 | + array('action' => 'view_transaction', 'TXN_ID' => $previous_txn['TXN_ID']), |
|
| 689 | + TXN_ADMIN_URL |
|
| 690 | + ), |
|
| 691 | + 'dashicons dashicons-arrow-left ee-icon-size-22' |
|
| 692 | + ) |
|
| 693 | + : ''; |
|
| 694 | + |
|
| 695 | + // were we just redirected here after adding a new registration ??? |
|
| 696 | + if ( |
|
| 697 | + isset( |
|
| 698 | + $this->_req_data['redirect_from'], |
|
| 699 | + $this->_req_data['EVT_ID'], |
|
| 700 | + $this->_req_data['event_name'] |
|
| 701 | + ) |
|
| 702 | + ) { |
|
| 703 | + if ( |
|
| 704 | + EE_Registry::instance()->CAP->current_user_can( |
|
| 705 | + 'ee_edit_registrations', |
|
| 706 | + 'espresso_registrations_new_registration', |
|
| 707 | + $this->_req_data['EVT_ID'] |
|
| 708 | + ) |
|
| 709 | + ) { |
|
| 710 | + $this->_admin_page_title .= '<a id="add-new-registration" class="add-new-h2 button-primary" href="'; |
|
| 711 | + $this->_admin_page_title .= EE_Admin_Page::add_query_args_and_nonce( |
|
| 712 | + array( |
|
| 713 | + 'page' => 'espresso_registrations', |
|
| 714 | + 'action' => 'new_registration', |
|
| 715 | + 'return' => 'default', |
|
| 716 | + 'TXN_ID' => $this->_transaction->ID(), |
|
| 717 | + 'event_id' => $this->_req_data['EVT_ID'], |
|
| 718 | + ), |
|
| 719 | + REG_ADMIN_URL |
|
| 720 | + ); |
|
| 721 | + $this->_admin_page_title .= '">'; |
|
| 722 | + |
|
| 723 | + $this->_admin_page_title .= sprintf( |
|
| 724 | + esc_html__('Add Another New Registration to Event: "%1$s" ?', 'event_espresso'), |
|
| 725 | + htmlentities(urldecode($this->_req_data['event_name']), ENT_QUOTES, 'UTF-8') |
|
| 726 | + ); |
|
| 727 | + $this->_admin_page_title .= '</a>'; |
|
| 728 | + } |
|
| 729 | + EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
| 730 | + } |
|
| 731 | + // grab messages at the last second |
|
| 732 | + $this->_template_args['notices'] = EE_Error::get_notices(); |
|
| 733 | + // path to template |
|
| 734 | + $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_header.template.php'; |
|
| 735 | + $this->_template_args['admin_page_header'] = EEH_Template::display_template($template_path, |
|
| 736 | + $this->_template_args, true); |
|
| 737 | + |
|
| 738 | + // the details template wrapper |
|
| 739 | + $this->display_admin_page_with_sidebar(); |
|
| 740 | + |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + |
|
| 744 | + /** |
|
| 745 | + * _transaction_details_metaboxes |
|
| 746 | + * |
|
| 747 | + * @access protected |
|
| 748 | + * @return void |
|
| 749 | + */ |
|
| 750 | + protected function _transaction_details_metaboxes() |
|
| 751 | + { |
|
| 752 | + |
|
| 753 | + $this->_set_transaction_object(); |
|
| 754 | + |
|
| 755 | + add_meta_box('edit-txn-details-mbox', esc_html__('Transaction Details', 'event_espresso'), |
|
| 756 | + array($this, 'txn_details_meta_box'), $this->_wp_page_slug, 'normal', 'high'); |
|
| 757 | + add_meta_box( |
|
| 758 | + 'edit-txn-attendees-mbox', |
|
| 759 | + esc_html__('Attendees Registered in this Transaction', 'event_espresso'), |
|
| 760 | + array($this, 'txn_attendees_meta_box'), |
|
| 761 | + $this->_wp_page_slug, |
|
| 762 | + 'normal', |
|
| 763 | + 'high', |
|
| 764 | + array('TXN_ID' => $this->_transaction->ID()) |
|
| 765 | + ); |
|
| 766 | + add_meta_box('edit-txn-registrant-mbox', esc_html__('Primary Contact', 'event_espresso'), |
|
| 767 | + array($this, 'txn_registrant_side_meta_box'), $this->_wp_page_slug, 'side', 'high'); |
|
| 768 | + add_meta_box('edit-txn-billing-info-mbox', esc_html__('Billing Information', 'event_espresso'), |
|
| 769 | + array($this, 'txn_billing_info_side_meta_box'), $this->_wp_page_slug, 'side', 'high'); |
|
| 770 | + |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + |
|
| 774 | + /** |
|
| 775 | + * txn_details_meta_box |
|
| 776 | + * generates HTML for the Transaction main meta box |
|
| 777 | + * |
|
| 778 | + * @access public |
|
| 779 | + * @return void |
|
| 780 | + */ |
|
| 781 | + public function txn_details_meta_box() |
|
| 782 | + { |
|
| 783 | + |
|
| 784 | + $this->_set_transaction_object(); |
|
| 785 | + $this->_template_args['TXN_ID'] = $this->_transaction->ID(); |
|
| 786 | + $this->_template_args['attendee'] = $this->_transaction->primary_registration() instanceof EE_Registration ? $this->_transaction->primary_registration()->attendee() : null; |
|
| 787 | + |
|
| 788 | + //get line table |
|
| 789 | + EEH_Autoloader::register_line_item_display_autoloaders(); |
|
| 790 | + $Line_Item_Display = new EE_Line_Item_Display('admin_table', |
|
| 791 | + 'EE_Admin_Table_Line_Item_Display_Strategy'); |
|
| 792 | + $this->_template_args['line_item_table'] = $Line_Item_Display->display_line_item($this->_transaction->total_line_item()); |
|
| 793 | + $this->_template_args['REG_code'] = $this->_transaction->get_first_related('Registration')->get('REG_code'); |
|
| 794 | + |
|
| 795 | + // process taxes |
|
| 796 | + $taxes = $this->_transaction->get_many_related('Line_Item', |
|
| 797 | + array(array('LIN_type' => EEM_Line_Item::type_tax))); |
|
| 798 | + $this->_template_args['taxes'] = ! empty($taxes) ? $taxes : false; |
|
| 799 | + |
|
| 800 | + $this->_template_args['grand_total'] = EEH_Template::format_currency($this->_transaction->get('TXN_total'), |
|
| 801 | + false, false); |
|
| 802 | + $this->_template_args['grand_raw_total'] = $this->_transaction->get('TXN_total'); |
|
| 803 | + $this->_template_args['TXN_status'] = $this->_transaction->get('STS_ID'); |
|
| 804 | 804 | |
| 805 | 805 | // $txn_status_class = 'status-' . $this->_transaction->get('STS_ID'); |
| 806 | 806 | |
| 807 | - // process payment details |
|
| 808 | - $payments = $this->_transaction->get_many_related('Payment'); |
|
| 809 | - if ( ! empty($payments)) { |
|
| 810 | - $this->_template_args['payments'] = $payments; |
|
| 811 | - $this->_template_args['existing_reg_payments'] = $this->_get_registration_payment_IDs($payments); |
|
| 812 | - } else { |
|
| 813 | - $this->_template_args['payments'] = false; |
|
| 814 | - $this->_template_args['existing_reg_payments'] = array(); |
|
| 815 | - } |
|
| 816 | - |
|
| 817 | - $this->_template_args['edit_payment_url'] = add_query_arg(array('action' => 'edit_payment'), TXN_ADMIN_URL); |
|
| 818 | - $this->_template_args['delete_payment_url'] = add_query_arg(array('action' => 'espresso_delete_payment'), |
|
| 819 | - TXN_ADMIN_URL); |
|
| 820 | - |
|
| 821 | - if (isset($txn_details['invoice_number'])) { |
|
| 822 | - $this->_template_args['txn_details']['invoice_number']['value'] = $this->_template_args['REG_code']; |
|
| 823 | - $this->_template_args['txn_details']['invoice_number']['label'] = esc_html__('Invoice Number', |
|
| 824 | - 'event_espresso'); |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - $this->_template_args['txn_details']['registration_session']['value'] = $this->_transaction->get_first_related('Registration')->get('REG_session'); |
|
| 828 | - $this->_template_args['txn_details']['registration_session']['label'] = esc_html__('Registration Session', |
|
| 829 | - 'event_espresso'); |
|
| 830 | - |
|
| 831 | - $this->_template_args['txn_details']['ip_address']['value'] = isset($this->_session['ip_address']) ? $this->_session['ip_address'] : ''; |
|
| 832 | - $this->_template_args['txn_details']['ip_address']['label'] = esc_html__('Transaction placed from IP', |
|
| 833 | - 'event_espresso'); |
|
| 834 | - |
|
| 835 | - $this->_template_args['txn_details']['user_agent']['value'] = isset($this->_session['user_agent']) ? $this->_session['user_agent'] : ''; |
|
| 836 | - $this->_template_args['txn_details']['user_agent']['label'] = esc_html__('Registrant User Agent', |
|
| 837 | - 'event_espresso'); |
|
| 838 | - |
|
| 839 | - $reg_steps = '<ul>'; |
|
| 840 | - foreach ($this->_transaction->reg_steps() as $reg_step => $reg_step_status) { |
|
| 841 | - if ($reg_step_status === true) { |
|
| 842 | - $reg_steps .= '<li style="color:#70cc50">' . sprintf(esc_html__('%1$s : Completed', 'event_espresso'), |
|
| 843 | - ucwords(str_replace('_', ' ', $reg_step))) . '</li>'; |
|
| 844 | - } else if (is_numeric($reg_step_status) && $reg_step_status !== false) { |
|
| 845 | - $reg_steps .= '<li style="color:#2EA2CC">' . sprintf( |
|
| 846 | - esc_html__('%1$s : Initiated %2$s', 'event_espresso'), |
|
| 847 | - ucwords(str_replace('_', ' ', $reg_step)), |
|
| 848 | - date(get_option('date_format') . ' ' . get_option('time_format'), |
|
| 849 | - ($reg_step_status + (get_option('gmt_offset') * HOUR_IN_SECONDS))) |
|
| 850 | - ) . '</li>'; |
|
| 851 | - } else { |
|
| 852 | - $reg_steps .= '<li style="color:#E76700">' . sprintf(esc_html__('%1$s : Never Initiated', |
|
| 853 | - 'event_espresso'), ucwords(str_replace('_', ' ', $reg_step))) . '</li>'; |
|
| 854 | - } |
|
| 855 | - } |
|
| 856 | - $reg_steps .= '</ul>'; |
|
| 857 | - $this->_template_args['txn_details']['reg_steps']['value'] = $reg_steps; |
|
| 858 | - $this->_template_args['txn_details']['reg_steps']['label'] = esc_html__('Registration Step Progress', |
|
| 859 | - 'event_espresso'); |
|
| 860 | - |
|
| 861 | - |
|
| 862 | - $this->_get_registrations_to_apply_payment_to(); |
|
| 863 | - $this->_get_payment_methods($payments); |
|
| 864 | - $this->_get_payment_status_array(); |
|
| 865 | - $this->_get_reg_status_selection(); //sets up the template args for the reg status array for the transaction. |
|
| 866 | - |
|
| 867 | - $this->_template_args['transaction_form_url'] = add_query_arg(array( |
|
| 868 | - 'action' => 'edit_transaction', |
|
| 869 | - 'process' => 'transaction' |
|
| 870 | - ), TXN_ADMIN_URL); |
|
| 871 | - $this->_template_args['apply_payment_form_url'] = add_query_arg(array( |
|
| 872 | - 'page' => 'espresso_transactions', |
|
| 873 | - 'action' => 'espresso_apply_payment' |
|
| 874 | - ), WP_AJAX_URL); |
|
| 875 | - $this->_template_args['delete_payment_form_url'] = add_query_arg(array( |
|
| 876 | - 'page' => 'espresso_transactions', |
|
| 877 | - 'action' => 'espresso_delete_payment' |
|
| 878 | - ), WP_AJAX_URL); |
|
| 879 | - |
|
| 880 | - // 'espresso_delete_payment_nonce' |
|
| 881 | - |
|
| 882 | - $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_txn_details.template.php'; |
|
| 883 | - echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 884 | - |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - |
|
| 888 | - /** |
|
| 889 | - * _get_registration_payment_IDs |
|
| 890 | - * |
|
| 891 | - * generates an array of Payment IDs and their corresponding Registration IDs |
|
| 892 | - * |
|
| 893 | - * @access protected |
|
| 894 | - * |
|
| 895 | - * @param EE_Payment[] $payments |
|
| 896 | - * |
|
| 897 | - * @return array |
|
| 898 | - */ |
|
| 899 | - protected function _get_registration_payment_IDs($payments = array()) |
|
| 900 | - { |
|
| 901 | - $existing_reg_payments = array(); |
|
| 902 | - // get all reg payments for these payments |
|
| 903 | - $reg_payments = EEM_Registration_Payment::instance()->get_all(array( |
|
| 904 | - array( |
|
| 905 | - 'PAY_ID' => array( |
|
| 906 | - 'IN', |
|
| 907 | - array_keys($payments) |
|
| 908 | - ) |
|
| 909 | - ) |
|
| 910 | - )); |
|
| 911 | - if ( ! empty($reg_payments)) { |
|
| 912 | - foreach ($payments as $payment) { |
|
| 913 | - if ( ! $payment instanceof EE_Payment) { |
|
| 914 | - continue; |
|
| 915 | - } else if ( ! isset($existing_reg_payments[$payment->ID()])) { |
|
| 916 | - $existing_reg_payments[$payment->ID()] = array(); |
|
| 917 | - } |
|
| 918 | - foreach ($reg_payments as $reg_payment) { |
|
| 919 | - if ($reg_payment instanceof EE_Registration_Payment && $reg_payment->payment_ID() === $payment->ID()) { |
|
| 920 | - $existing_reg_payments[$payment->ID()][] = $reg_payment->registration_ID(); |
|
| 921 | - } |
|
| 922 | - } |
|
| 923 | - } |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - return $existing_reg_payments; |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - |
|
| 930 | - /** |
|
| 931 | - * _get_registrations_to_apply_payment_to |
|
| 932 | - * generates HTML for displaying a series of checkboxes in the admin payment modal window |
|
| 933 | - * which allows the admin to only apply the payment to the specific registrations |
|
| 934 | - * |
|
| 935 | - * @access protected |
|
| 936 | - * @return void |
|
| 937 | - * @throws \EE_Error |
|
| 938 | - */ |
|
| 939 | - protected function _get_registrations_to_apply_payment_to() |
|
| 940 | - { |
|
| 941 | - // we want any registration with an active status (ie: not deleted or cancelled) |
|
| 942 | - $query_params = array( |
|
| 943 | - array( |
|
| 944 | - 'STS_ID' => array( |
|
| 945 | - 'IN', |
|
| 946 | - array( |
|
| 947 | - EEM_Registration::status_id_approved, |
|
| 948 | - EEM_Registration::status_id_pending_payment, |
|
| 949 | - EEM_Registration::status_id_not_approved, |
|
| 950 | - ) |
|
| 951 | - ) |
|
| 952 | - ) |
|
| 953 | - ); |
|
| 954 | - $registrations_to_apply_payment_to = EEH_HTML::br() . EEH_HTML::div( |
|
| 955 | - '', 'txn-admin-apply-payment-to-registrations-dv', '', 'clear: both; margin: 1.5em 0 0; display: none;' |
|
| 956 | - ); |
|
| 957 | - $registrations_to_apply_payment_to .= EEH_HTML::br() . EEH_HTML::div('', '', 'admin-primary-mbox-tbl-wrap'); |
|
| 958 | - $registrations_to_apply_payment_to .= EEH_HTML::table('', '', 'admin-primary-mbox-tbl'); |
|
| 959 | - $registrations_to_apply_payment_to .= EEH_HTML::thead( |
|
| 960 | - EEH_HTML::tr( |
|
| 961 | - EEH_HTML::th(esc_html__('ID', 'event_espresso')) . |
|
| 962 | - EEH_HTML::th(esc_html__('Registrant', 'event_espresso')) . |
|
| 963 | - EEH_HTML::th(esc_html__('Ticket', 'event_espresso')) . |
|
| 964 | - EEH_HTML::th(esc_html__('Event', 'event_espresso')) . |
|
| 965 | - EEH_HTML::th(esc_html__('Paid', 'event_espresso'), '', 'txn-admin-payment-paid-td jst-cntr') . |
|
| 966 | - EEH_HTML::th(esc_html__('Owing', 'event_espresso'), '', 'txn-admin-payment-owing-td jst-cntr') . |
|
| 967 | - EEH_HTML::th(esc_html__('Apply', 'event_espresso'), '', 'jst-cntr') |
|
| 968 | - ) |
|
| 969 | - ); |
|
| 970 | - $registrations_to_apply_payment_to .= EEH_HTML::tbody(); |
|
| 971 | - // get registrations for TXN |
|
| 972 | - $registrations = $this->_transaction->registrations($query_params); |
|
| 973 | - foreach ($registrations as $registration) { |
|
| 974 | - if ($registration instanceof EE_Registration) { |
|
| 975 | - $attendee_name = $registration->attendee() instanceof EE_Attendee |
|
| 976 | - ? $registration->attendee()->full_name() |
|
| 977 | - : esc_html__('Unknown Attendee', 'event_espresso'); |
|
| 978 | - $owing = $registration->final_price() - $registration->paid(); |
|
| 979 | - $taxable = $registration->ticket()->taxable() |
|
| 980 | - ? ' <span class="smaller-text lt-grey-text"> ' . esc_html__('+ tax', 'event_espresso') . '</span>' |
|
| 981 | - : ''; |
|
| 982 | - $checked = empty($existing_reg_payments) || in_array($registration->ID(), $existing_reg_payments) |
|
| 983 | - ? ' checked="checked"' |
|
| 984 | - : ''; |
|
| 985 | - $disabled = $registration->final_price() > 0 ? '' : ' disabled'; |
|
| 986 | - $registrations_to_apply_payment_to .= EEH_HTML::tr( |
|
| 987 | - EEH_HTML::td($registration->ID()) . |
|
| 988 | - EEH_HTML::td($attendee_name) . |
|
| 989 | - EEH_HTML::td( |
|
| 990 | - $registration->ticket()->name() . ' : ' . $registration->ticket()->pretty_price() . $taxable |
|
| 991 | - ) . |
|
| 992 | - EEH_HTML::td($registration->event_name()) . |
|
| 993 | - EEH_HTML::td($registration->pretty_paid(), '', 'txn-admin-payment-paid-td jst-cntr') . |
|
| 994 | - EEH_HTML::td(EEH_Template::format_currency($owing), '', 'txn-admin-payment-owing-td jst-cntr') . |
|
| 995 | - EEH_HTML::td( |
|
| 996 | - '<input type="checkbox" value="' . $registration->ID() |
|
| 997 | - . '" name="txn_admin_payment[registrations]"' |
|
| 998 | - . $checked . $disabled . '>', |
|
| 999 | - '', 'jst-cntr' |
|
| 1000 | - ), |
|
| 1001 | - 'apply-payment-registration-row-' . $registration->ID() |
|
| 1002 | - ); |
|
| 1003 | - } |
|
| 1004 | - } |
|
| 1005 | - $registrations_to_apply_payment_to .= EEH_HTML::tbodyx(); |
|
| 1006 | - $registrations_to_apply_payment_to .= EEH_HTML::tablex(); |
|
| 1007 | - $registrations_to_apply_payment_to .= EEH_HTML::divx(); |
|
| 1008 | - $registrations_to_apply_payment_to .= EEH_HTML::p( |
|
| 1009 | - esc_html__( |
|
| 1010 | - 'The payment will only be applied to the registrations that have a check mark in their corresponding check box. Checkboxes for free registrations have been disabled.', |
|
| 1011 | - 'event_espresso' |
|
| 1012 | - ), |
|
| 1013 | - '', 'clear description' |
|
| 1014 | - ); |
|
| 1015 | - $registrations_to_apply_payment_to .= EEH_HTML::divx(); |
|
| 1016 | - $this->_template_args['registrations_to_apply_payment_to'] = $registrations_to_apply_payment_to; |
|
| 1017 | - } |
|
| 1018 | - |
|
| 1019 | - |
|
| 1020 | - /** |
|
| 1021 | - * _get_reg_status_selection |
|
| 1022 | - * |
|
| 1023 | - * @todo this will need to be adjusted either once MER comes along OR we move default reg status to tickets |
|
| 1024 | - * instead of events. |
|
| 1025 | - * @access protected |
|
| 1026 | - * @return void |
|
| 1027 | - */ |
|
| 1028 | - protected function _get_reg_status_selection() |
|
| 1029 | - { |
|
| 1030 | - //first get all possible statuses |
|
| 1031 | - $statuses = EEM_Registration::reg_status_array(array(), true); |
|
| 1032 | - //let's add a "don't change" option. |
|
| 1033 | - $status_array['NAN'] = esc_html__('Leave the Same', 'event_espresso'); |
|
| 1034 | - $status_array = array_merge($status_array, $statuses); |
|
| 1035 | - $this->_template_args['status_change_select'] = EEH_Form_Fields::select_input('txn_reg_status_change[reg_status]', |
|
| 1036 | - $status_array, 'NAN', 'id="txn-admin-payment-reg-status-inp"', 'txn-reg-status-change-reg-status'); |
|
| 1037 | - $this->_template_args['delete_status_change_select'] = EEH_Form_Fields::select_input('delete_txn_reg_status_change[reg_status]', |
|
| 1038 | - $status_array, 'NAN', 'delete-txn-admin-payment-reg-status-inp', 'delete-txn-reg-status-change-reg-status'); |
|
| 1039 | - |
|
| 1040 | - } |
|
| 1041 | - |
|
| 1042 | - |
|
| 1043 | - /** |
|
| 1044 | - * _get_payment_methods |
|
| 1045 | - * Gets all the payment methods available generally, or the ones that are already |
|
| 1046 | - * selected on these payments (in case their payment methods are no longer active). |
|
| 1047 | - * Has the side-effect of updating the template args' payment_methods item |
|
| 1048 | - * @access private |
|
| 1049 | - * |
|
| 1050 | - * @param EE_Payment[] to show on this page |
|
| 1051 | - * |
|
| 1052 | - * @return void |
|
| 1053 | - */ |
|
| 1054 | - private function _get_payment_methods($payments = array()) |
|
| 1055 | - { |
|
| 1056 | - $payment_methods_of_payments = array(); |
|
| 1057 | - foreach ($payments as $payment) { |
|
| 1058 | - if ($payment instanceof EE_Payment) { |
|
| 1059 | - $payment_methods_of_payments[] = $payment->get('PMD_ID'); |
|
| 1060 | - } |
|
| 1061 | - } |
|
| 1062 | - if ($payment_methods_of_payments) { |
|
| 1063 | - $query_args = array( |
|
| 1064 | - array( |
|
| 1065 | - 'OR*payment_method_for_payment' => array( |
|
| 1066 | - 'PMD_ID' => array('IN', $payment_methods_of_payments), |
|
| 1067 | - 'PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%') |
|
| 1068 | - ) |
|
| 1069 | - ) |
|
| 1070 | - ); |
|
| 1071 | - } else { |
|
| 1072 | - $query_args = array(array('PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'))); |
|
| 1073 | - } |
|
| 1074 | - $this->_template_args['payment_methods'] = EEM_Payment_Method::instance()->get_all($query_args); |
|
| 1075 | - } |
|
| 1076 | - |
|
| 1077 | - |
|
| 1078 | - /** |
|
| 1079 | - * txn_attendees_meta_box |
|
| 1080 | - * generates HTML for the Attendees Transaction main meta box |
|
| 1081 | - * |
|
| 1082 | - * @access public |
|
| 1083 | - * |
|
| 1084 | - * @param WP_Post $post |
|
| 1085 | - * @param array $metabox |
|
| 1086 | - * |
|
| 1087 | - * @return void |
|
| 1088 | - */ |
|
| 1089 | - public function txn_attendees_meta_box($post, $metabox = array('args' => array())) |
|
| 1090 | - { |
|
| 1091 | - |
|
| 1092 | - extract($metabox['args']); |
|
| 1093 | - $this->_template_args['post'] = $post; |
|
| 1094 | - $this->_template_args['event_attendees'] = array(); |
|
| 1095 | - // process items in cart |
|
| 1096 | - $line_items = $this->_transaction->get_many_related('Line_Item', array(array('LIN_type' => 'line-item'))); |
|
| 1097 | - if ( ! empty($line_items)) { |
|
| 1098 | - foreach ($line_items as $item) { |
|
| 1099 | - if ($item instanceof EE_Line_Item) { |
|
| 1100 | - switch ($item->OBJ_type()) { |
|
| 1101 | - |
|
| 1102 | - case 'Event' : |
|
| 1103 | - break; |
|
| 1104 | - |
|
| 1105 | - case 'Ticket' : |
|
| 1106 | - $ticket = $item->ticket(); |
|
| 1107 | - //right now we're only handling tickets here. Cause its expected that only tickets will have attendees right? |
|
| 1108 | - if ( ! $ticket instanceof EE_Ticket) { |
|
| 1109 | - continue; |
|
| 1110 | - } |
|
| 1111 | - try { |
|
| 1112 | - $event_name = $ticket->get_event_name(); |
|
| 1113 | - } catch (Exception $e) { |
|
| 1114 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1115 | - $event_name = esc_html__('Unknown Event', 'event_espresso'); |
|
| 1116 | - } |
|
| 1117 | - $event_name .= ' - ' . $item->get('LIN_name'); |
|
| 1118 | - $ticket_price = EEH_Template::format_currency($item->get('LIN_unit_price')); |
|
| 1119 | - // now get all of the registrations for this transaction that use this ticket |
|
| 1120 | - $registrations = $ticket->get_many_related('Registration', |
|
| 1121 | - array(array('TXN_ID' => $this->_transaction->ID()))); |
|
| 1122 | - foreach ($registrations as $registration) { |
|
| 1123 | - if ( ! $registration instanceof EE_Registration) { |
|
| 1124 | - continue; |
|
| 1125 | - } |
|
| 1126 | - $this->_template_args['event_attendees'][$registration->ID()]['STS_ID'] = $registration->status_ID(); |
|
| 1127 | - $this->_template_args['event_attendees'][$registration->ID()]['att_num'] = $registration->count(); |
|
| 1128 | - $this->_template_args['event_attendees'][$registration->ID()]['event_ticket_name'] = $event_name; |
|
| 1129 | - $this->_template_args['event_attendees'][$registration->ID()]['ticket_price'] = $ticket_price; |
|
| 1130 | - // attendee info |
|
| 1131 | - $attendee = $registration->get_first_related('Attendee'); |
|
| 1132 | - if ($attendee instanceof EE_Attendee) { |
|
| 1133 | - $this->_template_args['event_attendees'][$registration->ID()]['att_id'] = $attendee->ID(); |
|
| 1134 | - $this->_template_args['event_attendees'][$registration->ID()]['attendee'] = $attendee->full_name(); |
|
| 1135 | - $this->_template_args['event_attendees'][$registration->ID()]['email'] = '<a href="mailto:' . $attendee->email() . '?subject=' . $event_name . esc_html__(' Event', |
|
| 1136 | - 'event_espresso') . '">' . $attendee->email() . '</a>'; |
|
| 1137 | - $this->_template_args['event_attendees'][$registration->ID()]['address'] = EEH_Address::format($attendee, |
|
| 1138 | - 'inline', false, false); |
|
| 1139 | - } else { |
|
| 1140 | - $this->_template_args['event_attendees'][$registration->ID()]['att_id'] = ''; |
|
| 1141 | - $this->_template_args['event_attendees'][$registration->ID()]['attendee'] = ''; |
|
| 1142 | - $this->_template_args['event_attendees'][$registration->ID()]['email'] = ''; |
|
| 1143 | - $this->_template_args['event_attendees'][$registration->ID()]['address'] = ''; |
|
| 1144 | - } |
|
| 1145 | - } |
|
| 1146 | - break; |
|
| 1147 | - |
|
| 1148 | - } |
|
| 1149 | - } |
|
| 1150 | - } |
|
| 1151 | - |
|
| 1152 | - $this->_template_args['transaction_form_url'] = add_query_arg(array( |
|
| 1153 | - 'action' => 'edit_transaction', |
|
| 1154 | - 'process' => 'attendees' |
|
| 1155 | - ), TXN_ADMIN_URL); |
|
| 1156 | - echo EEH_Template::display_template(TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_attendees.template.php', |
|
| 1157 | - $this->_template_args, true); |
|
| 1158 | - |
|
| 1159 | - } else { |
|
| 1160 | - echo sprintf( |
|
| 1161 | - esc_html__('%1$sFor some reason, there are no attendees registered for this transaction. Likely the registration was abandoned in process.%2$s', |
|
| 1162 | - 'event_espresso'), |
|
| 1163 | - '<p class="important-notice">', |
|
| 1164 | - '</p>' |
|
| 1165 | - ); |
|
| 1166 | - } |
|
| 1167 | - } |
|
| 1168 | - |
|
| 1169 | - |
|
| 1170 | - /** |
|
| 1171 | - * txn_registrant_side_meta_box |
|
| 1172 | - * generates HTML for the Edit Transaction side meta box |
|
| 1173 | - * |
|
| 1174 | - * @access public |
|
| 1175 | - * @throws \EE_Error |
|
| 1176 | - * @return void |
|
| 1177 | - */ |
|
| 1178 | - public function txn_registrant_side_meta_box() |
|
| 1179 | - { |
|
| 1180 | - $primary_att = $this->_transaction->primary_registration() instanceof EE_Registration ? $this->_transaction->primary_registration()->get_first_related('Attendee') : null; |
|
| 1181 | - if ( ! $primary_att instanceof EE_Attendee) { |
|
| 1182 | - $this->_template_args['no_attendee_message'] = esc_html__('There is no attached contact for this transaction. The transaction either failed due to an error or was abandoned.', |
|
| 1183 | - 'event_espresso'); |
|
| 1184 | - $primary_att = EEM_Attendee::instance()->create_default_object(); |
|
| 1185 | - } |
|
| 1186 | - $this->_template_args['ATT_ID'] = $primary_att->ID(); |
|
| 1187 | - $this->_template_args['prime_reg_fname'] = $primary_att->fname(); |
|
| 1188 | - $this->_template_args['prime_reg_lname'] = $primary_att->lname(); |
|
| 1189 | - $this->_template_args['prime_reg_email'] = $primary_att->email(); |
|
| 1190 | - $this->_template_args['prime_reg_phone'] = $primary_att->phone(); |
|
| 1191 | - $this->_template_args['edit_attendee_url'] = EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 1192 | - 'action' => 'edit_attendee', |
|
| 1193 | - 'post' => $primary_att->ID() |
|
| 1194 | - ), REG_ADMIN_URL); |
|
| 1195 | - // get formatted address for registrant |
|
| 1196 | - $this->_template_args['formatted_address'] = EEH_Address::format($primary_att); |
|
| 1197 | - echo EEH_Template::display_template(TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_registrant.template.php', |
|
| 1198 | - $this->_template_args, true); |
|
| 1199 | - } |
|
| 1200 | - |
|
| 1201 | - |
|
| 1202 | - /** |
|
| 1203 | - * txn_billing_info_side_meta_box |
|
| 1204 | - * generates HTML for the Edit Transaction side meta box |
|
| 1205 | - * |
|
| 1206 | - * @access public |
|
| 1207 | - * @return void |
|
| 1208 | - */ |
|
| 1209 | - public function txn_billing_info_side_meta_box() |
|
| 1210 | - { |
|
| 1211 | - |
|
| 1212 | - $this->_template_args['billing_form'] = $this->_transaction->billing_info(); |
|
| 1213 | - $this->_template_args['billing_form_url'] = add_query_arg( |
|
| 1214 | - array('action' => 'edit_transaction', 'process' => 'billing'), |
|
| 1215 | - TXN_ADMIN_URL |
|
| 1216 | - ); |
|
| 1217 | - |
|
| 1218 | - $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_billing_info.template.php'; |
|
| 1219 | - echo EEH_Template::display_template($template_path, $this->_template_args, true);/**/ |
|
| 1220 | - } |
|
| 1221 | - |
|
| 1222 | - |
|
| 1223 | - /** |
|
| 1224 | - * apply_payments_or_refunds |
|
| 1225 | - * registers a payment or refund made towards a transaction |
|
| 1226 | - * |
|
| 1227 | - * @access public |
|
| 1228 | - * @return void |
|
| 1229 | - */ |
|
| 1230 | - public function apply_payments_or_refunds() |
|
| 1231 | - { |
|
| 1232 | - $json_response_data = array('return_data' => false); |
|
| 1233 | - $valid_data = $this->_validate_payment_request_data(); |
|
| 1234 | - if ( ! empty($valid_data)) { |
|
| 1235 | - $PAY_ID = $valid_data['PAY_ID']; |
|
| 1236 | - //save the new payment |
|
| 1237 | - $payment = $this->_create_payment_from_request_data($valid_data); |
|
| 1238 | - // get the TXN for this payment |
|
| 1239 | - $transaction = $payment->transaction(); |
|
| 1240 | - // verify transaction |
|
| 1241 | - if ($transaction instanceof EE_Transaction) { |
|
| 1242 | - // calculate_total_payments_and_update_status |
|
| 1243 | - $this->_process_transaction_payments($transaction); |
|
| 1244 | - $REG_IDs = $this->_get_REG_IDs_to_apply_payment_to($payment); |
|
| 1245 | - $this->_remove_existing_registration_payments($payment, $PAY_ID); |
|
| 1246 | - // apply payment to registrations (if applicable) |
|
| 1247 | - if ( ! empty($REG_IDs)) { |
|
| 1248 | - $this->_update_registration_payments($transaction, $payment, $REG_IDs); |
|
| 1249 | - $this->_maybe_send_notifications(); |
|
| 1250 | - // now process status changes for the same registrations |
|
| 1251 | - $this->_process_registration_status_change($transaction, $REG_IDs); |
|
| 1252 | - } |
|
| 1253 | - $this->_maybe_send_notifications($payment); |
|
| 1254 | - //prepare to render page |
|
| 1255 | - $json_response_data['return_data'] = $this->_build_payment_json_response($payment, $REG_IDs); |
|
| 1256 | - do_action('AHEE__Transactions_Admin_Page__apply_payments_or_refund__after_recording', $transaction, |
|
| 1257 | - $payment); |
|
| 1258 | - } else { |
|
| 1259 | - EE_Error::add_error( |
|
| 1260 | - esc_html__('A valid Transaction for this payment could not be retrieved.', 'event_espresso'), |
|
| 1261 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 1262 | - ); |
|
| 1263 | - } |
|
| 1264 | - } else { |
|
| 1265 | - EE_Error::add_error(esc_html__('The payment form data could not be processed. Please try again.', |
|
| 1266 | - 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 1267 | - } |
|
| 1268 | - |
|
| 1269 | - $notices = EE_Error::get_notices(false, false, false); |
|
| 1270 | - $this->_template_args = array( |
|
| 1271 | - 'data' => $json_response_data, |
|
| 1272 | - 'error' => $notices['errors'], |
|
| 1273 | - 'success' => $notices['success'] |
|
| 1274 | - ); |
|
| 1275 | - $this->_return_json(); |
|
| 1276 | - } |
|
| 1277 | - |
|
| 1278 | - |
|
| 1279 | - /** |
|
| 1280 | - * _validate_payment_request_data |
|
| 1281 | - * |
|
| 1282 | - * @return array |
|
| 1283 | - */ |
|
| 1284 | - protected function _validate_payment_request_data() |
|
| 1285 | - { |
|
| 1286 | - if ( ! isset($this->_req_data['txn_admin_payment'])) { |
|
| 1287 | - return false; |
|
| 1288 | - } |
|
| 1289 | - $payment_form = $this->_generate_payment_form_section(); |
|
| 1290 | - try { |
|
| 1291 | - if ($payment_form->was_submitted()) { |
|
| 1292 | - $payment_form->receive_form_submission(); |
|
| 1293 | - if ( ! $payment_form->is_valid()) { |
|
| 1294 | - $submission_error_messages = array(); |
|
| 1295 | - foreach ($payment_form->get_validation_errors_accumulated() as $validation_error) { |
|
| 1296 | - if ($validation_error instanceof EE_Validation_Error) { |
|
| 1297 | - $submission_error_messages[] = sprintf( |
|
| 1298 | - _x('%s : %s', 'Form Section Name : Form Validation Error', 'event_espresso'), |
|
| 1299 | - $validation_error->get_form_section()->html_label_text(), |
|
| 1300 | - $validation_error->getMessage() |
|
| 1301 | - ); |
|
| 1302 | - } |
|
| 1303 | - } |
|
| 1304 | - EE_Error::add_error(join('<br />', $submission_error_messages), __FILE__, __FUNCTION__, __LINE__); |
|
| 1305 | - |
|
| 1306 | - return array(); |
|
| 1307 | - } |
|
| 1308 | - } |
|
| 1309 | - } catch (EE_Error $e) { |
|
| 1310 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1311 | - |
|
| 1312 | - return array(); |
|
| 1313 | - } |
|
| 1314 | - |
|
| 1315 | - return $payment_form->valid_data(); |
|
| 1316 | - } |
|
| 1317 | - |
|
| 1318 | - |
|
| 1319 | - /** |
|
| 1320 | - * _generate_payment_form_section |
|
| 1321 | - * |
|
| 1322 | - * @return EE_Form_Section_Proper |
|
| 1323 | - */ |
|
| 1324 | - protected function _generate_payment_form_section() |
|
| 1325 | - { |
|
| 1326 | - return new EE_Form_Section_Proper( |
|
| 1327 | - array( |
|
| 1328 | - 'name' => 'txn_admin_payment', |
|
| 1329 | - 'subsections' => array( |
|
| 1330 | - 'PAY_ID' => new EE_Text_Input( |
|
| 1331 | - array( |
|
| 1332 | - 'default' => 0, |
|
| 1333 | - 'required' => false, |
|
| 1334 | - 'html_label_text' => esc_html__('Payment ID', 'event_espresso'), |
|
| 1335 | - 'validation_strategies' => array(new EE_Int_Normalization()) |
|
| 1336 | - ) |
|
| 1337 | - ), |
|
| 1338 | - 'TXN_ID' => new EE_Text_Input( |
|
| 1339 | - array( |
|
| 1340 | - 'default' => 0, |
|
| 1341 | - 'required' => true, |
|
| 1342 | - 'html_label_text' => esc_html__('Transaction ID', 'event_espresso'), |
|
| 1343 | - 'validation_strategies' => array(new EE_Int_Normalization()) |
|
| 1344 | - ) |
|
| 1345 | - ), |
|
| 1346 | - 'type' => new EE_Text_Input( |
|
| 1347 | - array( |
|
| 1348 | - 'default' => 1, |
|
| 1349 | - 'required' => true, |
|
| 1350 | - 'html_label_text' => esc_html__('Payment or Refund', 'event_espresso'), |
|
| 1351 | - 'validation_strategies' => array(new EE_Int_Normalization()) |
|
| 1352 | - ) |
|
| 1353 | - ), |
|
| 1354 | - 'amount' => new EE_Text_Input( |
|
| 1355 | - array( |
|
| 1356 | - 'default' => 0, |
|
| 1357 | - 'required' => true, |
|
| 1358 | - 'html_label_text' => esc_html__('Payment amount', 'event_espresso'), |
|
| 1359 | - 'validation_strategies' => array(new EE_Float_Normalization()) |
|
| 1360 | - ) |
|
| 1361 | - ), |
|
| 1362 | - 'status' => new EE_Text_Input( |
|
| 1363 | - array( |
|
| 1364 | - 'default' => EEM_Payment::status_id_approved, |
|
| 1365 | - 'required' => true, |
|
| 1366 | - 'html_label_text' => esc_html__('Payment status', 'event_espresso'), |
|
| 1367 | - ) |
|
| 1368 | - ), |
|
| 1369 | - 'PMD_ID' => new EE_Text_Input( |
|
| 1370 | - array( |
|
| 1371 | - 'default' => 2, |
|
| 1372 | - 'required' => true, |
|
| 1373 | - 'html_label_text' => esc_html__('Payment Method', 'event_espresso'), |
|
| 1374 | - 'validation_strategies' => array(new EE_Int_Normalization()) |
|
| 1375 | - ) |
|
| 1376 | - ), |
|
| 1377 | - 'date' => new EE_Text_Input( |
|
| 1378 | - array( |
|
| 1379 | - 'default' => time(), |
|
| 1380 | - 'required' => true, |
|
| 1381 | - 'html_label_text' => esc_html__('Payment date', 'event_espresso'), |
|
| 1382 | - ) |
|
| 1383 | - ), |
|
| 1384 | - 'txn_id_chq_nmbr' => new EE_Text_Input( |
|
| 1385 | - array( |
|
| 1386 | - 'default' => '', |
|
| 1387 | - 'required' => false, |
|
| 1388 | - 'html_label_text' => esc_html__('Transaction or Cheque Number', 'event_espresso'), |
|
| 1389 | - 'validation_strategies' => array( |
|
| 1390 | - new EE_Max_Length_Validation_Strategy(esc_html__('Input too long', 'event_espresso'), |
|
| 1391 | - 100), |
|
| 1392 | - ) |
|
| 1393 | - ) |
|
| 1394 | - ), |
|
| 1395 | - 'po_number' => new EE_Text_Input( |
|
| 1396 | - array( |
|
| 1397 | - 'default' => '', |
|
| 1398 | - 'required' => false, |
|
| 1399 | - 'html_label_text' => esc_html__('Purchase Order Number', 'event_espresso'), |
|
| 1400 | - 'validation_strategies' => array( |
|
| 1401 | - new EE_Max_Length_Validation_Strategy(esc_html__('Input too long', 'event_espresso'), |
|
| 1402 | - 100), |
|
| 1403 | - ) |
|
| 1404 | - ) |
|
| 1405 | - ), |
|
| 1406 | - 'accounting' => new EE_Text_Input( |
|
| 1407 | - array( |
|
| 1408 | - 'default' => '', |
|
| 1409 | - 'required' => false, |
|
| 1410 | - 'html_label_text' => esc_html__('Extra Field for Accounting', 'event_espresso'), |
|
| 1411 | - 'validation_strategies' => array( |
|
| 1412 | - new EE_Max_Length_Validation_Strategy(esc_html__('Input too long', 'event_espresso'), |
|
| 1413 | - 100), |
|
| 1414 | - ) |
|
| 1415 | - ) |
|
| 1416 | - ), |
|
| 1417 | - ) |
|
| 1418 | - ) |
|
| 1419 | - ); |
|
| 1420 | - } |
|
| 1421 | - |
|
| 1422 | - |
|
| 1423 | - /** |
|
| 1424 | - * _create_payment_from_request_data |
|
| 1425 | - * |
|
| 1426 | - * @param array $valid_data |
|
| 1427 | - * |
|
| 1428 | - * @return EE_Payment |
|
| 1429 | - */ |
|
| 1430 | - protected function _create_payment_from_request_data($valid_data) |
|
| 1431 | - { |
|
| 1432 | - $PAY_ID = $valid_data['PAY_ID']; |
|
| 1433 | - // get payment amount |
|
| 1434 | - $amount = $valid_data['amount'] ? abs($valid_data['amount']) : 0; |
|
| 1435 | - // payments have a type value of 1 and refunds have a type value of -1 |
|
| 1436 | - // so multiplying amount by type will give a positive value for payments, and negative values for refunds |
|
| 1437 | - $amount = $valid_data['type'] < 0 ? $amount * -1 : $amount; |
|
| 1438 | - // for some reason the date string coming in has extra spaces between the date and time. This fixes that. |
|
| 1439 | - $date = $valid_data['date'] ? preg_replace('/\s+/', ' ', $valid_data['date']) : date('Y-m-d g:i a', |
|
| 1440 | - current_time('timestamp')); |
|
| 1441 | - $payment = EE_Payment::new_instance( |
|
| 1442 | - array( |
|
| 1443 | - 'TXN_ID' => $valid_data['TXN_ID'], |
|
| 1444 | - 'STS_ID' => $valid_data['status'], |
|
| 1445 | - 'PAY_timestamp' => $date, |
|
| 1446 | - 'PAY_source' => EEM_Payment_Method::scope_admin, |
|
| 1447 | - 'PMD_ID' => $valid_data['PMD_ID'], |
|
| 1448 | - 'PAY_amount' => $amount, |
|
| 1449 | - 'PAY_txn_id_chq_nmbr' => $valid_data['txn_id_chq_nmbr'], |
|
| 1450 | - 'PAY_po_number' => $valid_data['po_number'], |
|
| 1451 | - 'PAY_extra_accntng' => $valid_data['accounting'], |
|
| 1452 | - 'PAY_details' => $valid_data, |
|
| 1453 | - 'PAY_ID' => $PAY_ID |
|
| 1454 | - ), |
|
| 1455 | - '', |
|
| 1456 | - array('Y-m-d', 'g:i a') |
|
| 1457 | - ); |
|
| 1458 | - |
|
| 1459 | - if ( ! $payment->save()) { |
|
| 1460 | - EE_Error::add_error( |
|
| 1461 | - sprintf( |
|
| 1462 | - esc_html__('Payment %1$d has not been successfully saved to the database.', 'event_espresso'), |
|
| 1463 | - $payment->ID() |
|
| 1464 | - ), |
|
| 1465 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 1466 | - ); |
|
| 1467 | - } |
|
| 1468 | - |
|
| 1469 | - return $payment; |
|
| 1470 | - } |
|
| 1471 | - |
|
| 1472 | - |
|
| 1473 | - /** |
|
| 1474 | - * _process_transaction_payments |
|
| 1475 | - * |
|
| 1476 | - * @param \EE_Transaction $transaction |
|
| 1477 | - * |
|
| 1478 | - * @return array |
|
| 1479 | - */ |
|
| 1480 | - protected function _process_transaction_payments(EE_Transaction $transaction) |
|
| 1481 | - { |
|
| 1482 | - /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 1483 | - $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 1484 | - //update the transaction with this payment |
|
| 1485 | - if ($transaction_payments->calculate_total_payments_and_update_status($transaction)) { |
|
| 1486 | - EE_Error::add_success(esc_html__('The payment has been processed successfully.', 'event_espresso'), |
|
| 1487 | - __FILE__, __FUNCTION__, __LINE__); |
|
| 1488 | - } else { |
|
| 1489 | - EE_Error::add_error( |
|
| 1490 | - esc_html__('The payment was processed successfully but the amount paid for the transaction was not updated.', |
|
| 1491 | - 'event_espresso') |
|
| 1492 | - , __FILE__, __FUNCTION__, __LINE__ |
|
| 1493 | - ); |
|
| 1494 | - } |
|
| 1495 | - } |
|
| 1496 | - |
|
| 1497 | - |
|
| 1498 | - /** |
|
| 1499 | - * _get_REG_IDs_to_apply_payment_to |
|
| 1500 | - * |
|
| 1501 | - * returns a list of registration IDs that the payment will apply to |
|
| 1502 | - * |
|
| 1503 | - * @param \EE_Payment $payment |
|
| 1504 | - * |
|
| 1505 | - * @return array |
|
| 1506 | - */ |
|
| 1507 | - protected function _get_REG_IDs_to_apply_payment_to(EE_Payment $payment) |
|
| 1508 | - { |
|
| 1509 | - $REG_IDs = array(); |
|
| 1510 | - // grab array of IDs for specific registrations to apply changes to |
|
| 1511 | - if (isset($this->_req_data['txn_admin_payment']['registrations'])) { |
|
| 1512 | - $REG_IDs = (array)$this->_req_data['txn_admin_payment']['registrations']; |
|
| 1513 | - } |
|
| 1514 | - //nothing specified ? then get all reg IDs |
|
| 1515 | - if (empty($REG_IDs)) { |
|
| 1516 | - $registrations = $payment->transaction()->registrations(); |
|
| 1517 | - $REG_IDs = ! empty($registrations) ? array_keys($registrations) : $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 1518 | - } |
|
| 1519 | - |
|
| 1520 | - // ensure that REG_IDs are integers and NOT strings |
|
| 1521 | - return array_map('intval', $REG_IDs); |
|
| 1522 | - } |
|
| 1523 | - |
|
| 1524 | - |
|
| 1525 | - /** |
|
| 1526 | - * @return array |
|
| 1527 | - */ |
|
| 1528 | - public function existing_reg_payment_REG_IDs() |
|
| 1529 | - { |
|
| 1530 | - return $this->_existing_reg_payment_REG_IDs; |
|
| 1531 | - } |
|
| 1532 | - |
|
| 1533 | - |
|
| 1534 | - /** |
|
| 1535 | - * @param array $existing_reg_payment_REG_IDs |
|
| 1536 | - */ |
|
| 1537 | - public function set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs = null) |
|
| 1538 | - { |
|
| 1539 | - $this->_existing_reg_payment_REG_IDs = $existing_reg_payment_REG_IDs; |
|
| 1540 | - } |
|
| 1541 | - |
|
| 1542 | - |
|
| 1543 | - /** |
|
| 1544 | - * _get_existing_reg_payment_REG_IDs |
|
| 1545 | - * |
|
| 1546 | - * returns a list of registration IDs that the payment is currently related to |
|
| 1547 | - * as recorded in the database |
|
| 1548 | - * |
|
| 1549 | - * @param \EE_Payment $payment |
|
| 1550 | - * |
|
| 1551 | - * @return array |
|
| 1552 | - */ |
|
| 1553 | - protected function _get_existing_reg_payment_REG_IDs(EE_Payment $payment) |
|
| 1554 | - { |
|
| 1555 | - if ($this->existing_reg_payment_REG_IDs() === null) { |
|
| 1556 | - // let's get any existing reg payment records for this payment |
|
| 1557 | - $existing_reg_payment_REG_IDs = $payment->get_many_related('Registration'); |
|
| 1558 | - // but we only want the REG IDs, so grab the array keys |
|
| 1559 | - $existing_reg_payment_REG_IDs = ! empty($existing_reg_payment_REG_IDs) ? array_keys($existing_reg_payment_REG_IDs) : array(); |
|
| 1560 | - $this->set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs); |
|
| 1561 | - } |
|
| 1562 | - |
|
| 1563 | - return $this->existing_reg_payment_REG_IDs(); |
|
| 1564 | - } |
|
| 1565 | - |
|
| 1566 | - |
|
| 1567 | - /** |
|
| 1568 | - * _remove_existing_registration_payments |
|
| 1569 | - * |
|
| 1570 | - * this calculates the difference between existing relations |
|
| 1571 | - * to the supplied payment and the new list registration IDs, |
|
| 1572 | - * removes any related registrations that no longer apply, |
|
| 1573 | - * and then updates the registration paid fields |
|
| 1574 | - * |
|
| 1575 | - * @param \EE_Payment $payment |
|
| 1576 | - * @param int $PAY_ID |
|
| 1577 | - * |
|
| 1578 | - * @return bool; |
|
| 1579 | - */ |
|
| 1580 | - protected function _remove_existing_registration_payments(EE_Payment $payment, $PAY_ID = 0) |
|
| 1581 | - { |
|
| 1582 | - // newly created payments will have nothing recorded for $PAY_ID |
|
| 1583 | - if ($PAY_ID == 0) { |
|
| 1584 | - return false; |
|
| 1585 | - } |
|
| 1586 | - $existing_reg_payment_REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 1587 | - if (empty($existing_reg_payment_REG_IDs)) { |
|
| 1588 | - return false; |
|
| 1589 | - } |
|
| 1590 | - /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 1591 | - $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 1592 | - |
|
| 1593 | - return $transaction_payments->delete_registration_payments_and_update_registrations( |
|
| 1594 | - $payment, |
|
| 1595 | - array( |
|
| 1596 | - array( |
|
| 1597 | - 'PAY_ID' => $payment->ID(), |
|
| 1598 | - 'REG_ID' => array('IN', $existing_reg_payment_REG_IDs), |
|
| 1599 | - ) |
|
| 1600 | - ) |
|
| 1601 | - ); |
|
| 1602 | - } |
|
| 1603 | - |
|
| 1604 | - |
|
| 1605 | - /** |
|
| 1606 | - * _update_registration_payments |
|
| 1607 | - * |
|
| 1608 | - * this applies the payments to the selected registrations |
|
| 1609 | - * but only if they have not already been paid for |
|
| 1610 | - * |
|
| 1611 | - * @param EE_Transaction $transaction |
|
| 1612 | - * @param \EE_Payment $payment |
|
| 1613 | - * @param array $REG_IDs |
|
| 1614 | - * |
|
| 1615 | - * @return bool |
|
| 1616 | - */ |
|
| 1617 | - protected function _update_registration_payments( |
|
| 1618 | - EE_Transaction $transaction, |
|
| 1619 | - EE_Payment $payment, |
|
| 1620 | - $REG_IDs = array() |
|
| 1621 | - ) { |
|
| 1622 | - // we can pass our own custom set of registrations to EE_Payment_Processor::process_registration_payments() |
|
| 1623 | - // so let's do that using our set of REG_IDs from the form |
|
| 1624 | - $registration_query_where_params = array( |
|
| 1625 | - 'REG_ID' => array('IN', $REG_IDs) |
|
| 1626 | - ); |
|
| 1627 | - // but add in some conditions regarding payment, |
|
| 1628 | - // so that we don't apply payments to registrations that are free or have already been paid for |
|
| 1629 | - // but ONLY if the payment is NOT a refund ( ie: the payment amount is not negative ) |
|
| 1630 | - if ( ! $payment->is_a_refund()) { |
|
| 1631 | - $registration_query_where_params['REG_final_price'] = array('!=', 0); |
|
| 1632 | - $registration_query_where_params['REG_final_price*'] = array('!=', 'REG_paid', true); |
|
| 1633 | - } |
|
| 1634 | - //EEH_Debug_Tools::printr( $registration_query_where_params, '$registration_query_where_params', __FILE__, __LINE__ ); |
|
| 1635 | - $registrations = $transaction->registrations(array($registration_query_where_params)); |
|
| 1636 | - if ( ! empty($registrations)) { |
|
| 1637 | - /** @type EE_Payment_Processor $payment_processor */ |
|
| 1638 | - $payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
|
| 1639 | - $payment_processor->process_registration_payments($transaction, $payment, $registrations); |
|
| 1640 | - } |
|
| 1641 | - } |
|
| 1642 | - |
|
| 1643 | - |
|
| 1644 | - /** |
|
| 1645 | - * _process_registration_status_change |
|
| 1646 | - * |
|
| 1647 | - * This processes requested registration status changes for all the registrations |
|
| 1648 | - * on a given transaction and (optionally) sends out notifications for the changes. |
|
| 1649 | - * |
|
| 1650 | - * @param EE_Transaction $transaction |
|
| 1651 | - * @param array $REG_IDs |
|
| 1652 | - * |
|
| 1653 | - * @return bool |
|
| 1654 | - */ |
|
| 1655 | - protected function _process_registration_status_change(EE_Transaction $transaction, $REG_IDs = array()) |
|
| 1656 | - { |
|
| 1657 | - // first if there is no change in status then we get out. |
|
| 1658 | - if ( |
|
| 1659 | - ! isset($this->_req_data['txn_reg_status_change'], $this->_req_data['txn_reg_status_change']['reg_status']) |
|
| 1660 | - || $this->_req_data['txn_reg_status_change']['reg_status'] == 'NAN' |
|
| 1661 | - ) { |
|
| 1662 | - //no error message, no change requested, just nothing to do man. |
|
| 1663 | - return false; |
|
| 1664 | - } |
|
| 1665 | - /** @type EE_Transaction_Processor $transaction_processor */ |
|
| 1666 | - $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
|
| 1667 | - |
|
| 1668 | - // made it here dude? Oh WOW. K, let's take care of changing the statuses |
|
| 1669 | - return $transaction_processor->manually_update_registration_statuses( |
|
| 1670 | - $transaction, |
|
| 1671 | - sanitize_text_field($this->_req_data['txn_reg_status_change']['reg_status']), |
|
| 1672 | - array(array('REG_ID' => array('IN', $REG_IDs))) |
|
| 1673 | - ); |
|
| 1674 | - } |
|
| 1675 | - |
|
| 1676 | - |
|
| 1677 | - /** |
|
| 1678 | - * _build_payment_json_response |
|
| 1679 | - * |
|
| 1680 | - * @access public |
|
| 1681 | - * |
|
| 1682 | - * @param \EE_Payment $payment |
|
| 1683 | - * @param array $REG_IDs |
|
| 1684 | - * @param bool | null $delete_txn_reg_status_change |
|
| 1685 | - * |
|
| 1686 | - * @return array |
|
| 1687 | - */ |
|
| 1688 | - protected function _build_payment_json_response( |
|
| 1689 | - EE_Payment $payment, |
|
| 1690 | - $REG_IDs = array(), |
|
| 1691 | - $delete_txn_reg_status_change = null |
|
| 1692 | - ) { |
|
| 1693 | - // was the payment deleted ? |
|
| 1694 | - if (is_bool($delete_txn_reg_status_change)) { |
|
| 1695 | - return array( |
|
| 1696 | - 'PAY_ID' => $payment->ID(), |
|
| 1697 | - 'amount' => $payment->amount(), |
|
| 1698 | - 'total_paid' => $payment->transaction()->paid(), |
|
| 1699 | - 'txn_status' => $payment->transaction()->status_ID(), |
|
| 1700 | - 'pay_status' => $payment->STS_ID(), |
|
| 1701 | - 'registrations' => $this->_registration_payment_data_array($REG_IDs), |
|
| 1702 | - 'delete_txn_reg_status_change' => $delete_txn_reg_status_change, |
|
| 1703 | - ); |
|
| 1704 | - } else { |
|
| 1705 | - $this->_get_payment_status_array(); |
|
| 1706 | - |
|
| 1707 | - return array( |
|
| 1708 | - 'amount' => $payment->amount(), |
|
| 1709 | - 'total_paid' => $payment->transaction()->paid(), |
|
| 1710 | - 'txn_status' => $payment->transaction()->status_ID(), |
|
| 1711 | - 'pay_status' => $payment->STS_ID(), |
|
| 1712 | - 'PAY_ID' => $payment->ID(), |
|
| 1713 | - 'STS_ID' => $payment->STS_ID(), |
|
| 1714 | - 'status' => self::$_pay_status[$payment->STS_ID()], |
|
| 1715 | - 'date' => $payment->timestamp('Y-m-d', 'h:i a'), |
|
| 1716 | - 'method' => strtoupper($payment->source()), |
|
| 1717 | - 'PM_ID' => $payment->payment_method() ? $payment->payment_method()->ID() : 1, |
|
| 1718 | - 'gateway' => $payment->payment_method() ? $payment->payment_method()->admin_name() : esc_html__("Unknown", |
|
| 1719 | - 'event_espresso'), |
|
| 1720 | - 'gateway_response' => $payment->gateway_response(), |
|
| 1721 | - 'txn_id_chq_nmbr' => $payment->txn_id_chq_nmbr(), |
|
| 1722 | - 'po_number' => $payment->po_number(), |
|
| 1723 | - 'extra_accntng' => $payment->extra_accntng(), |
|
| 1724 | - 'registrations' => $this->_registration_payment_data_array($REG_IDs), |
|
| 1725 | - ); |
|
| 1726 | - } |
|
| 1727 | - } |
|
| 1728 | - |
|
| 1729 | - |
|
| 1730 | - /** |
|
| 1731 | - * delete_payment |
|
| 1732 | - * delete a payment or refund made towards a transaction |
|
| 1733 | - * |
|
| 1734 | - * @access public |
|
| 1735 | - * @return void |
|
| 1736 | - */ |
|
| 1737 | - public function delete_payment() |
|
| 1738 | - { |
|
| 1739 | - $json_response_data = array('return_data' => false); |
|
| 1740 | - $PAY_ID = isset($this->_req_data['delete_txn_admin_payment'], $this->_req_data['delete_txn_admin_payment']['PAY_ID']) ? absint($this->_req_data['delete_txn_admin_payment']['PAY_ID']) : 0; |
|
| 1741 | - if ($PAY_ID) { |
|
| 1742 | - $delete_txn_reg_status_change = isset($this->_req_data['delete_txn_reg_status_change']) ? $this->_req_data['delete_txn_reg_status_change'] : false; |
|
| 1743 | - $payment = EEM_Payment::instance()->get_one_by_ID($PAY_ID); |
|
| 1744 | - if ($payment instanceof EE_Payment) { |
|
| 1745 | - $REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 1746 | - /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 1747 | - $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 1748 | - if ($transaction_payments->delete_payment_and_update_transaction($payment)) { |
|
| 1749 | - $json_response_data['return_data'] = $this->_build_payment_json_response($payment, $REG_IDs, |
|
| 1750 | - $delete_txn_reg_status_change); |
|
| 1751 | - if ($delete_txn_reg_status_change) { |
|
| 1752 | - $this->_req_data['txn_reg_status_change'] = $delete_txn_reg_status_change; |
|
| 1753 | - //MAKE sure we also add the delete_txn_req_status_change to the |
|
| 1754 | - //$_REQUEST global because that's how messages will be looking for it. |
|
| 1755 | - $_REQUEST['txn_reg_status_change'] = $delete_txn_reg_status_change; |
|
| 1756 | - $this->_maybe_send_notifications(); |
|
| 1757 | - $this->_process_registration_status_change($payment->transaction(), $REG_IDs); |
|
| 1758 | - } |
|
| 1759 | - } |
|
| 1760 | - } else { |
|
| 1761 | - EE_Error::add_error( |
|
| 1762 | - esc_html__('Valid Payment data could not be retrieved from the database.', 'event_espresso'), |
|
| 1763 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 1764 | - ); |
|
| 1765 | - } |
|
| 1766 | - } else { |
|
| 1767 | - EE_Error::add_error( |
|
| 1768 | - esc_html__('A valid Payment ID was not received, therefore payment form data could not be loaded.', |
|
| 1769 | - 'event_espresso'), |
|
| 1770 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 1771 | - ); |
|
| 1772 | - } |
|
| 1773 | - $notices = EE_Error::get_notices(false, false, false); |
|
| 1774 | - $this->_template_args = array( |
|
| 1775 | - 'data' => $json_response_data, |
|
| 1776 | - 'success' => $notices['success'], |
|
| 1777 | - 'error' => $notices['errors'], |
|
| 1778 | - 'attention' => $notices['attention'] |
|
| 1779 | - ); |
|
| 1780 | - $this->_return_json(); |
|
| 1781 | - } |
|
| 1782 | - |
|
| 1783 | - |
|
| 1784 | - /** |
|
| 1785 | - * _registration_payment_data_array |
|
| 1786 | - * adds info for 'owing' and 'paid' for each registration to the json response |
|
| 1787 | - * |
|
| 1788 | - * @access protected |
|
| 1789 | - * |
|
| 1790 | - * @param array $REG_IDs |
|
| 1791 | - * |
|
| 1792 | - * @return array |
|
| 1793 | - */ |
|
| 1794 | - protected function _registration_payment_data_array($REG_IDs) |
|
| 1795 | - { |
|
| 1796 | - $registration_payment_data = array(); |
|
| 1797 | - //if non empty reg_ids lets get an array of registrations and update the values for the apply_payment/refund rows. |
|
| 1798 | - if ( ! empty($REG_IDs)) { |
|
| 1799 | - $registrations = EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $REG_IDs)))); |
|
| 1800 | - foreach ($registrations as $registration) { |
|
| 1801 | - if ($registration instanceof EE_Registration) { |
|
| 1802 | - $registration_payment_data[$registration->ID()] = array( |
|
| 1803 | - 'paid' => $registration->pretty_paid(), |
|
| 1804 | - 'owing' => EEH_Template::format_currency($registration->final_price() - $registration->paid()), |
|
| 1805 | - ); |
|
| 1806 | - } |
|
| 1807 | - } |
|
| 1808 | - } |
|
| 1809 | - |
|
| 1810 | - return $registration_payment_data; |
|
| 1811 | - } |
|
| 1812 | - |
|
| 1813 | - |
|
| 1814 | - /** |
|
| 1815 | - * _maybe_send_notifications |
|
| 1816 | - * |
|
| 1817 | - * determines whether or not the admin has indicated that notifications should be sent. |
|
| 1818 | - * If so, will toggle a filter switch for delivering registration notices. |
|
| 1819 | - * If passed an EE_Payment object, then it will trigger payment notifications instead. |
|
| 1820 | - * |
|
| 1821 | - * @access protected |
|
| 1822 | - * |
|
| 1823 | - * @param \EE_Payment | null $payment |
|
| 1824 | - */ |
|
| 1825 | - protected function _maybe_send_notifications($payment = null) |
|
| 1826 | - { |
|
| 1827 | - switch ($payment instanceof EE_Payment) { |
|
| 1828 | - // payment notifications |
|
| 1829 | - case true : |
|
| 1830 | - if ( |
|
| 1831 | - isset( |
|
| 1832 | - $this->_req_data['txn_payments'], |
|
| 1833 | - $this->_req_data['txn_payments']['send_notifications'] |
|
| 1834 | - ) && |
|
| 1835 | - filter_var($this->_req_data['txn_payments']['send_notifications'], FILTER_VALIDATE_BOOLEAN) |
|
| 1836 | - ) { |
|
| 1837 | - $this->_process_payment_notification($payment); |
|
| 1838 | - } |
|
| 1839 | - break; |
|
| 1840 | - // registration notifications |
|
| 1841 | - case false : |
|
| 1842 | - if ( |
|
| 1843 | - isset( |
|
| 1844 | - $this->_req_data['txn_reg_status_change'], |
|
| 1845 | - $this->_req_data['txn_reg_status_change']['send_notifications'] |
|
| 1846 | - ) && |
|
| 1847 | - filter_var($this->_req_data['txn_reg_status_change']['send_notifications'], FILTER_VALIDATE_BOOLEAN) |
|
| 1848 | - ) { |
|
| 1849 | - add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true'); |
|
| 1850 | - } |
|
| 1851 | - break; |
|
| 1852 | - } |
|
| 1853 | - } |
|
| 1854 | - |
|
| 1855 | - |
|
| 1856 | - /** |
|
| 1857 | - * _send_payment_reminder |
|
| 1858 | - * generates HTML for the View Transaction Details Admin page |
|
| 1859 | - * |
|
| 1860 | - * @access protected |
|
| 1861 | - * @return void |
|
| 1862 | - */ |
|
| 1863 | - protected function _send_payment_reminder() |
|
| 1864 | - { |
|
| 1865 | - $TXN_ID = ( ! empty($this->_req_data['TXN_ID'])) ? absint($this->_req_data['TXN_ID']) : false; |
|
| 1866 | - $transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
|
| 1867 | - $query_args = isset($this->_req_data['redirect_to']) ? array( |
|
| 1868 | - 'action' => $this->_req_data['redirect_to'], |
|
| 1869 | - 'TXN_ID' => $this->_req_data['TXN_ID'] |
|
| 1870 | - ) : array(); |
|
| 1871 | - do_action('AHEE__Transactions_Admin_Page___send_payment_reminder__process_admin_payment_reminder', |
|
| 1872 | - $transaction); |
|
| 1873 | - $this->_redirect_after_action(false, esc_html__('payment reminder', 'event_espresso'), |
|
| 1874 | - esc_html__('sent', 'event_espresso'), $query_args, true); |
|
| 1875 | - } |
|
| 1876 | - |
|
| 1877 | - |
|
| 1878 | - /** |
|
| 1879 | - * get_transactions |
|
| 1880 | - * get transactions for given parameters (used by list table) |
|
| 1881 | - * |
|
| 1882 | - * @param int $perpage how many transactions displayed per page |
|
| 1883 | - * @param boolean $count return the count or objects |
|
| 1884 | - * @param string $view |
|
| 1885 | - * |
|
| 1886 | - * @return mixed int = count || array of transaction objects |
|
| 1887 | - */ |
|
| 1888 | - public function get_transactions($perpage, $count = false, $view = '') |
|
| 1889 | - { |
|
| 1890 | - |
|
| 1891 | - $TXN = EEM_Transaction::instance(); |
|
| 1892 | - |
|
| 1893 | - $start_date = isset($this->_req_data['txn-filter-start-date']) ? wp_strip_all_tags($this->_req_data['txn-filter-start-date']) : date('m/d/Y', |
|
| 1894 | - strtotime('-10 year')); |
|
| 1895 | - $end_date = isset($this->_req_data['txn-filter-end-date']) ? wp_strip_all_tags($this->_req_data['txn-filter-end-date']) : date('m/d/Y'); |
|
| 1896 | - |
|
| 1897 | - //make sure our timestamps start and end right at the boundaries for each day |
|
| 1898 | - $start_date = date('Y-m-d', strtotime($start_date)) . ' 00:00:00'; |
|
| 1899 | - $end_date = date('Y-m-d', strtotime($end_date)) . ' 23:59:59'; |
|
| 1900 | - |
|
| 1901 | - |
|
| 1902 | - //convert to timestamps |
|
| 1903 | - $start_date = strtotime($start_date); |
|
| 1904 | - $end_date = strtotime($end_date); |
|
| 1905 | - |
|
| 1906 | - //makes sure start date is the lowest value and vice versa |
|
| 1907 | - $start_date = min($start_date, $end_date); |
|
| 1908 | - $end_date = max($start_date, $end_date); |
|
| 1909 | - |
|
| 1910 | - //convert to correct format for query |
|
| 1911 | - $start_date = EEM_Transaction::instance()->convert_datetime_for_query('TXN_timestamp', |
|
| 1912 | - date('Y-m-d H:i:s', $start_date), 'Y-m-d H:i:s'); |
|
| 1913 | - $end_date = EEM_Transaction::instance()->convert_datetime_for_query('TXN_timestamp', |
|
| 1914 | - date('Y-m-d H:i:s', $end_date), 'Y-m-d H:i:s'); |
|
| 1915 | - |
|
| 1916 | - |
|
| 1917 | - //set orderby |
|
| 1918 | - $this->_req_data['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : ''; |
|
| 1919 | - |
|
| 1920 | - switch ($this->_req_data['orderby']) { |
|
| 1921 | - case 'TXN_ID': |
|
| 1922 | - $orderby = 'TXN_ID'; |
|
| 1923 | - break; |
|
| 1924 | - case 'ATT_fname': |
|
| 1925 | - $orderby = 'Registration.Attendee.ATT_fname'; |
|
| 1926 | - break; |
|
| 1927 | - case 'event_name': |
|
| 1928 | - $orderby = 'Registration.Event.EVT_name'; |
|
| 1929 | - break; |
|
| 1930 | - default: //'TXN_timestamp' |
|
| 1931 | - $orderby = 'TXN_timestamp'; |
|
| 1932 | - } |
|
| 1933 | - |
|
| 1934 | - $sort = (isset($this->_req_data['order']) && ! empty($this->_req_data['order'])) ? $this->_req_data['order'] : 'DESC'; |
|
| 1935 | - $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged']) ? $this->_req_data['paged'] : 1; |
|
| 1936 | - $per_page = isset($perpage) && ! empty($perpage) ? $perpage : 10; |
|
| 1937 | - $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage']) ? $this->_req_data['perpage'] : $per_page; |
|
| 1938 | - |
|
| 1939 | - $offset = ($current_page - 1) * $per_page; |
|
| 1940 | - $limit = array($offset, $per_page); |
|
| 1941 | - |
|
| 1942 | - $_where = array( |
|
| 1943 | - 'TXN_timestamp' => array('BETWEEN', array($start_date, $end_date)), |
|
| 1944 | - 'Registration.REG_count' => 1 |
|
| 1945 | - ); |
|
| 1946 | - |
|
| 1947 | - if (isset($this->_req_data['EVT_ID'])) { |
|
| 1948 | - $_where['Registration.EVT_ID'] = $this->_req_data['EVT_ID']; |
|
| 1949 | - } |
|
| 1950 | - |
|
| 1951 | - if (isset($this->_req_data['s'])) { |
|
| 1952 | - $search_string = '%' . $this->_req_data['s'] . '%'; |
|
| 1953 | - $_where['OR'] = array( |
|
| 1954 | - 'Registration.Event.EVT_name' => array('LIKE', $search_string), |
|
| 1955 | - 'Registration.Event.EVT_desc' => array('LIKE', $search_string), |
|
| 1956 | - 'Registration.Event.EVT_short_desc' => array('LIKE', $search_string), |
|
| 1957 | - 'Registration.Attendee.ATT_full_name' => array('LIKE', $search_string), |
|
| 1958 | - 'Registration.Attendee.ATT_fname' => array('LIKE', $search_string), |
|
| 1959 | - 'Registration.Attendee.ATT_lname' => array('LIKE', $search_string), |
|
| 1960 | - 'Registration.Attendee.ATT_short_bio' => array('LIKE', $search_string), |
|
| 1961 | - 'Registration.Attendee.ATT_email' => array('LIKE', $search_string), |
|
| 1962 | - 'Registration.Attendee.ATT_address' => array('LIKE', $search_string), |
|
| 1963 | - 'Registration.Attendee.ATT_address2' => array('LIKE', $search_string), |
|
| 1964 | - 'Registration.Attendee.ATT_city' => array('LIKE', $search_string), |
|
| 1965 | - 'Registration.REG_final_price' => array('LIKE', $search_string), |
|
| 1966 | - 'Registration.REG_code' => array('LIKE', $search_string), |
|
| 1967 | - 'Registration.REG_count' => array('LIKE', $search_string), |
|
| 1968 | - 'Registration.REG_group_size' => array('LIKE', $search_string), |
|
| 1969 | - 'Registration.Ticket.TKT_name' => array('LIKE', $search_string), |
|
| 1970 | - 'Registration.Ticket.TKT_description' => array('LIKE', $search_string), |
|
| 1971 | - 'Payment.PAY_source' => array('LIKE', $search_string), |
|
| 1972 | - 'Payment.Payment_Method.PMD_name' => array('LIKE', $search_string), |
|
| 1973 | - 'TXN_session_data' => array('LIKE', $search_string), |
|
| 1974 | - 'Payment.PAY_txn_id_chq_nmbr' => array('LIKE', $search_string) |
|
| 1975 | - ); |
|
| 1976 | - } |
|
| 1977 | - |
|
| 1978 | - //failed transactions |
|
| 1979 | - $failed = ( ! empty($this->_req_data['status']) && $this->_req_data['status'] == 'failed' && ! $count) || ($count && $view == 'failed') ? true : false; |
|
| 1980 | - $abandoned = ( ! empty($this->_req_data['status']) && $this->_req_data['status'] == 'abandoned' && ! $count) || ($count && $view == 'abandoned') ? true : false; |
|
| 1981 | - |
|
| 1982 | - if ($failed) { |
|
| 1983 | - $_where['STS_ID'] = EEM_Transaction::failed_status_code; |
|
| 1984 | - } else if ($abandoned) { |
|
| 1985 | - $_where['STS_ID'] = EEM_Transaction::abandoned_status_code; |
|
| 1986 | - } else { |
|
| 1987 | - $_where['STS_ID'] = array('!=', EEM_Transaction::failed_status_code); |
|
| 1988 | - $_where['STS_ID*'] = array('!=', EEM_Transaction::abandoned_status_code); |
|
| 1989 | - } |
|
| 1990 | - |
|
| 1991 | - $query_params = array( |
|
| 1992 | - $_where, |
|
| 1993 | - 'order_by' => array($orderby => $sort), |
|
| 1994 | - 'limit' => $limit, |
|
| 1995 | - 'default_where_conditions' => EEM_Base::default_where_conditions_this_only |
|
| 1996 | - ); |
|
| 1997 | - |
|
| 1998 | - $transactions = $count ? $TXN->count(array($_where), 'TXN_ID', true) : $TXN->get_all($query_params); |
|
| 1999 | - |
|
| 2000 | - |
|
| 2001 | - return $transactions; |
|
| 2002 | - |
|
| 2003 | - } |
|
| 807 | + // process payment details |
|
| 808 | + $payments = $this->_transaction->get_many_related('Payment'); |
|
| 809 | + if ( ! empty($payments)) { |
|
| 810 | + $this->_template_args['payments'] = $payments; |
|
| 811 | + $this->_template_args['existing_reg_payments'] = $this->_get_registration_payment_IDs($payments); |
|
| 812 | + } else { |
|
| 813 | + $this->_template_args['payments'] = false; |
|
| 814 | + $this->_template_args['existing_reg_payments'] = array(); |
|
| 815 | + } |
|
| 816 | + |
|
| 817 | + $this->_template_args['edit_payment_url'] = add_query_arg(array('action' => 'edit_payment'), TXN_ADMIN_URL); |
|
| 818 | + $this->_template_args['delete_payment_url'] = add_query_arg(array('action' => 'espresso_delete_payment'), |
|
| 819 | + TXN_ADMIN_URL); |
|
| 820 | + |
|
| 821 | + if (isset($txn_details['invoice_number'])) { |
|
| 822 | + $this->_template_args['txn_details']['invoice_number']['value'] = $this->_template_args['REG_code']; |
|
| 823 | + $this->_template_args['txn_details']['invoice_number']['label'] = esc_html__('Invoice Number', |
|
| 824 | + 'event_espresso'); |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + $this->_template_args['txn_details']['registration_session']['value'] = $this->_transaction->get_first_related('Registration')->get('REG_session'); |
|
| 828 | + $this->_template_args['txn_details']['registration_session']['label'] = esc_html__('Registration Session', |
|
| 829 | + 'event_espresso'); |
|
| 830 | + |
|
| 831 | + $this->_template_args['txn_details']['ip_address']['value'] = isset($this->_session['ip_address']) ? $this->_session['ip_address'] : ''; |
|
| 832 | + $this->_template_args['txn_details']['ip_address']['label'] = esc_html__('Transaction placed from IP', |
|
| 833 | + 'event_espresso'); |
|
| 834 | + |
|
| 835 | + $this->_template_args['txn_details']['user_agent']['value'] = isset($this->_session['user_agent']) ? $this->_session['user_agent'] : ''; |
|
| 836 | + $this->_template_args['txn_details']['user_agent']['label'] = esc_html__('Registrant User Agent', |
|
| 837 | + 'event_espresso'); |
|
| 838 | + |
|
| 839 | + $reg_steps = '<ul>'; |
|
| 840 | + foreach ($this->_transaction->reg_steps() as $reg_step => $reg_step_status) { |
|
| 841 | + if ($reg_step_status === true) { |
|
| 842 | + $reg_steps .= '<li style="color:#70cc50">' . sprintf(esc_html__('%1$s : Completed', 'event_espresso'), |
|
| 843 | + ucwords(str_replace('_', ' ', $reg_step))) . '</li>'; |
|
| 844 | + } else if (is_numeric($reg_step_status) && $reg_step_status !== false) { |
|
| 845 | + $reg_steps .= '<li style="color:#2EA2CC">' . sprintf( |
|
| 846 | + esc_html__('%1$s : Initiated %2$s', 'event_espresso'), |
|
| 847 | + ucwords(str_replace('_', ' ', $reg_step)), |
|
| 848 | + date(get_option('date_format') . ' ' . get_option('time_format'), |
|
| 849 | + ($reg_step_status + (get_option('gmt_offset') * HOUR_IN_SECONDS))) |
|
| 850 | + ) . '</li>'; |
|
| 851 | + } else { |
|
| 852 | + $reg_steps .= '<li style="color:#E76700">' . sprintf(esc_html__('%1$s : Never Initiated', |
|
| 853 | + 'event_espresso'), ucwords(str_replace('_', ' ', $reg_step))) . '</li>'; |
|
| 854 | + } |
|
| 855 | + } |
|
| 856 | + $reg_steps .= '</ul>'; |
|
| 857 | + $this->_template_args['txn_details']['reg_steps']['value'] = $reg_steps; |
|
| 858 | + $this->_template_args['txn_details']['reg_steps']['label'] = esc_html__('Registration Step Progress', |
|
| 859 | + 'event_espresso'); |
|
| 860 | + |
|
| 861 | + |
|
| 862 | + $this->_get_registrations_to_apply_payment_to(); |
|
| 863 | + $this->_get_payment_methods($payments); |
|
| 864 | + $this->_get_payment_status_array(); |
|
| 865 | + $this->_get_reg_status_selection(); //sets up the template args for the reg status array for the transaction. |
|
| 866 | + |
|
| 867 | + $this->_template_args['transaction_form_url'] = add_query_arg(array( |
|
| 868 | + 'action' => 'edit_transaction', |
|
| 869 | + 'process' => 'transaction' |
|
| 870 | + ), TXN_ADMIN_URL); |
|
| 871 | + $this->_template_args['apply_payment_form_url'] = add_query_arg(array( |
|
| 872 | + 'page' => 'espresso_transactions', |
|
| 873 | + 'action' => 'espresso_apply_payment' |
|
| 874 | + ), WP_AJAX_URL); |
|
| 875 | + $this->_template_args['delete_payment_form_url'] = add_query_arg(array( |
|
| 876 | + 'page' => 'espresso_transactions', |
|
| 877 | + 'action' => 'espresso_delete_payment' |
|
| 878 | + ), WP_AJAX_URL); |
|
| 879 | + |
|
| 880 | + // 'espresso_delete_payment_nonce' |
|
| 881 | + |
|
| 882 | + $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_txn_details.template.php'; |
|
| 883 | + echo EEH_Template::display_template($template_path, $this->_template_args, true); |
|
| 884 | + |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + |
|
| 888 | + /** |
|
| 889 | + * _get_registration_payment_IDs |
|
| 890 | + * |
|
| 891 | + * generates an array of Payment IDs and their corresponding Registration IDs |
|
| 892 | + * |
|
| 893 | + * @access protected |
|
| 894 | + * |
|
| 895 | + * @param EE_Payment[] $payments |
|
| 896 | + * |
|
| 897 | + * @return array |
|
| 898 | + */ |
|
| 899 | + protected function _get_registration_payment_IDs($payments = array()) |
|
| 900 | + { |
|
| 901 | + $existing_reg_payments = array(); |
|
| 902 | + // get all reg payments for these payments |
|
| 903 | + $reg_payments = EEM_Registration_Payment::instance()->get_all(array( |
|
| 904 | + array( |
|
| 905 | + 'PAY_ID' => array( |
|
| 906 | + 'IN', |
|
| 907 | + array_keys($payments) |
|
| 908 | + ) |
|
| 909 | + ) |
|
| 910 | + )); |
|
| 911 | + if ( ! empty($reg_payments)) { |
|
| 912 | + foreach ($payments as $payment) { |
|
| 913 | + if ( ! $payment instanceof EE_Payment) { |
|
| 914 | + continue; |
|
| 915 | + } else if ( ! isset($existing_reg_payments[$payment->ID()])) { |
|
| 916 | + $existing_reg_payments[$payment->ID()] = array(); |
|
| 917 | + } |
|
| 918 | + foreach ($reg_payments as $reg_payment) { |
|
| 919 | + if ($reg_payment instanceof EE_Registration_Payment && $reg_payment->payment_ID() === $payment->ID()) { |
|
| 920 | + $existing_reg_payments[$payment->ID()][] = $reg_payment->registration_ID(); |
|
| 921 | + } |
|
| 922 | + } |
|
| 923 | + } |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + return $existing_reg_payments; |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + |
|
| 930 | + /** |
|
| 931 | + * _get_registrations_to_apply_payment_to |
|
| 932 | + * generates HTML for displaying a series of checkboxes in the admin payment modal window |
|
| 933 | + * which allows the admin to only apply the payment to the specific registrations |
|
| 934 | + * |
|
| 935 | + * @access protected |
|
| 936 | + * @return void |
|
| 937 | + * @throws \EE_Error |
|
| 938 | + */ |
|
| 939 | + protected function _get_registrations_to_apply_payment_to() |
|
| 940 | + { |
|
| 941 | + // we want any registration with an active status (ie: not deleted or cancelled) |
|
| 942 | + $query_params = array( |
|
| 943 | + array( |
|
| 944 | + 'STS_ID' => array( |
|
| 945 | + 'IN', |
|
| 946 | + array( |
|
| 947 | + EEM_Registration::status_id_approved, |
|
| 948 | + EEM_Registration::status_id_pending_payment, |
|
| 949 | + EEM_Registration::status_id_not_approved, |
|
| 950 | + ) |
|
| 951 | + ) |
|
| 952 | + ) |
|
| 953 | + ); |
|
| 954 | + $registrations_to_apply_payment_to = EEH_HTML::br() . EEH_HTML::div( |
|
| 955 | + '', 'txn-admin-apply-payment-to-registrations-dv', '', 'clear: both; margin: 1.5em 0 0; display: none;' |
|
| 956 | + ); |
|
| 957 | + $registrations_to_apply_payment_to .= EEH_HTML::br() . EEH_HTML::div('', '', 'admin-primary-mbox-tbl-wrap'); |
|
| 958 | + $registrations_to_apply_payment_to .= EEH_HTML::table('', '', 'admin-primary-mbox-tbl'); |
|
| 959 | + $registrations_to_apply_payment_to .= EEH_HTML::thead( |
|
| 960 | + EEH_HTML::tr( |
|
| 961 | + EEH_HTML::th(esc_html__('ID', 'event_espresso')) . |
|
| 962 | + EEH_HTML::th(esc_html__('Registrant', 'event_espresso')) . |
|
| 963 | + EEH_HTML::th(esc_html__('Ticket', 'event_espresso')) . |
|
| 964 | + EEH_HTML::th(esc_html__('Event', 'event_espresso')) . |
|
| 965 | + EEH_HTML::th(esc_html__('Paid', 'event_espresso'), '', 'txn-admin-payment-paid-td jst-cntr') . |
|
| 966 | + EEH_HTML::th(esc_html__('Owing', 'event_espresso'), '', 'txn-admin-payment-owing-td jst-cntr') . |
|
| 967 | + EEH_HTML::th(esc_html__('Apply', 'event_espresso'), '', 'jst-cntr') |
|
| 968 | + ) |
|
| 969 | + ); |
|
| 970 | + $registrations_to_apply_payment_to .= EEH_HTML::tbody(); |
|
| 971 | + // get registrations for TXN |
|
| 972 | + $registrations = $this->_transaction->registrations($query_params); |
|
| 973 | + foreach ($registrations as $registration) { |
|
| 974 | + if ($registration instanceof EE_Registration) { |
|
| 975 | + $attendee_name = $registration->attendee() instanceof EE_Attendee |
|
| 976 | + ? $registration->attendee()->full_name() |
|
| 977 | + : esc_html__('Unknown Attendee', 'event_espresso'); |
|
| 978 | + $owing = $registration->final_price() - $registration->paid(); |
|
| 979 | + $taxable = $registration->ticket()->taxable() |
|
| 980 | + ? ' <span class="smaller-text lt-grey-text"> ' . esc_html__('+ tax', 'event_espresso') . '</span>' |
|
| 981 | + : ''; |
|
| 982 | + $checked = empty($existing_reg_payments) || in_array($registration->ID(), $existing_reg_payments) |
|
| 983 | + ? ' checked="checked"' |
|
| 984 | + : ''; |
|
| 985 | + $disabled = $registration->final_price() > 0 ? '' : ' disabled'; |
|
| 986 | + $registrations_to_apply_payment_to .= EEH_HTML::tr( |
|
| 987 | + EEH_HTML::td($registration->ID()) . |
|
| 988 | + EEH_HTML::td($attendee_name) . |
|
| 989 | + EEH_HTML::td( |
|
| 990 | + $registration->ticket()->name() . ' : ' . $registration->ticket()->pretty_price() . $taxable |
|
| 991 | + ) . |
|
| 992 | + EEH_HTML::td($registration->event_name()) . |
|
| 993 | + EEH_HTML::td($registration->pretty_paid(), '', 'txn-admin-payment-paid-td jst-cntr') . |
|
| 994 | + EEH_HTML::td(EEH_Template::format_currency($owing), '', 'txn-admin-payment-owing-td jst-cntr') . |
|
| 995 | + EEH_HTML::td( |
|
| 996 | + '<input type="checkbox" value="' . $registration->ID() |
|
| 997 | + . '" name="txn_admin_payment[registrations]"' |
|
| 998 | + . $checked . $disabled . '>', |
|
| 999 | + '', 'jst-cntr' |
|
| 1000 | + ), |
|
| 1001 | + 'apply-payment-registration-row-' . $registration->ID() |
|
| 1002 | + ); |
|
| 1003 | + } |
|
| 1004 | + } |
|
| 1005 | + $registrations_to_apply_payment_to .= EEH_HTML::tbodyx(); |
|
| 1006 | + $registrations_to_apply_payment_to .= EEH_HTML::tablex(); |
|
| 1007 | + $registrations_to_apply_payment_to .= EEH_HTML::divx(); |
|
| 1008 | + $registrations_to_apply_payment_to .= EEH_HTML::p( |
|
| 1009 | + esc_html__( |
|
| 1010 | + 'The payment will only be applied to the registrations that have a check mark in their corresponding check box. Checkboxes for free registrations have been disabled.', |
|
| 1011 | + 'event_espresso' |
|
| 1012 | + ), |
|
| 1013 | + '', 'clear description' |
|
| 1014 | + ); |
|
| 1015 | + $registrations_to_apply_payment_to .= EEH_HTML::divx(); |
|
| 1016 | + $this->_template_args['registrations_to_apply_payment_to'] = $registrations_to_apply_payment_to; |
|
| 1017 | + } |
|
| 1018 | + |
|
| 1019 | + |
|
| 1020 | + /** |
|
| 1021 | + * _get_reg_status_selection |
|
| 1022 | + * |
|
| 1023 | + * @todo this will need to be adjusted either once MER comes along OR we move default reg status to tickets |
|
| 1024 | + * instead of events. |
|
| 1025 | + * @access protected |
|
| 1026 | + * @return void |
|
| 1027 | + */ |
|
| 1028 | + protected function _get_reg_status_selection() |
|
| 1029 | + { |
|
| 1030 | + //first get all possible statuses |
|
| 1031 | + $statuses = EEM_Registration::reg_status_array(array(), true); |
|
| 1032 | + //let's add a "don't change" option. |
|
| 1033 | + $status_array['NAN'] = esc_html__('Leave the Same', 'event_espresso'); |
|
| 1034 | + $status_array = array_merge($status_array, $statuses); |
|
| 1035 | + $this->_template_args['status_change_select'] = EEH_Form_Fields::select_input('txn_reg_status_change[reg_status]', |
|
| 1036 | + $status_array, 'NAN', 'id="txn-admin-payment-reg-status-inp"', 'txn-reg-status-change-reg-status'); |
|
| 1037 | + $this->_template_args['delete_status_change_select'] = EEH_Form_Fields::select_input('delete_txn_reg_status_change[reg_status]', |
|
| 1038 | + $status_array, 'NAN', 'delete-txn-admin-payment-reg-status-inp', 'delete-txn-reg-status-change-reg-status'); |
|
| 1039 | + |
|
| 1040 | + } |
|
| 1041 | + |
|
| 1042 | + |
|
| 1043 | + /** |
|
| 1044 | + * _get_payment_methods |
|
| 1045 | + * Gets all the payment methods available generally, or the ones that are already |
|
| 1046 | + * selected on these payments (in case their payment methods are no longer active). |
|
| 1047 | + * Has the side-effect of updating the template args' payment_methods item |
|
| 1048 | + * @access private |
|
| 1049 | + * |
|
| 1050 | + * @param EE_Payment[] to show on this page |
|
| 1051 | + * |
|
| 1052 | + * @return void |
|
| 1053 | + */ |
|
| 1054 | + private function _get_payment_methods($payments = array()) |
|
| 1055 | + { |
|
| 1056 | + $payment_methods_of_payments = array(); |
|
| 1057 | + foreach ($payments as $payment) { |
|
| 1058 | + if ($payment instanceof EE_Payment) { |
|
| 1059 | + $payment_methods_of_payments[] = $payment->get('PMD_ID'); |
|
| 1060 | + } |
|
| 1061 | + } |
|
| 1062 | + if ($payment_methods_of_payments) { |
|
| 1063 | + $query_args = array( |
|
| 1064 | + array( |
|
| 1065 | + 'OR*payment_method_for_payment' => array( |
|
| 1066 | + 'PMD_ID' => array('IN', $payment_methods_of_payments), |
|
| 1067 | + 'PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%') |
|
| 1068 | + ) |
|
| 1069 | + ) |
|
| 1070 | + ); |
|
| 1071 | + } else { |
|
| 1072 | + $query_args = array(array('PMD_scope' => array('LIKE', '%' . EEM_Payment_Method::scope_admin . '%'))); |
|
| 1073 | + } |
|
| 1074 | + $this->_template_args['payment_methods'] = EEM_Payment_Method::instance()->get_all($query_args); |
|
| 1075 | + } |
|
| 1076 | + |
|
| 1077 | + |
|
| 1078 | + /** |
|
| 1079 | + * txn_attendees_meta_box |
|
| 1080 | + * generates HTML for the Attendees Transaction main meta box |
|
| 1081 | + * |
|
| 1082 | + * @access public |
|
| 1083 | + * |
|
| 1084 | + * @param WP_Post $post |
|
| 1085 | + * @param array $metabox |
|
| 1086 | + * |
|
| 1087 | + * @return void |
|
| 1088 | + */ |
|
| 1089 | + public function txn_attendees_meta_box($post, $metabox = array('args' => array())) |
|
| 1090 | + { |
|
| 1091 | + |
|
| 1092 | + extract($metabox['args']); |
|
| 1093 | + $this->_template_args['post'] = $post; |
|
| 1094 | + $this->_template_args['event_attendees'] = array(); |
|
| 1095 | + // process items in cart |
|
| 1096 | + $line_items = $this->_transaction->get_many_related('Line_Item', array(array('LIN_type' => 'line-item'))); |
|
| 1097 | + if ( ! empty($line_items)) { |
|
| 1098 | + foreach ($line_items as $item) { |
|
| 1099 | + if ($item instanceof EE_Line_Item) { |
|
| 1100 | + switch ($item->OBJ_type()) { |
|
| 1101 | + |
|
| 1102 | + case 'Event' : |
|
| 1103 | + break; |
|
| 1104 | + |
|
| 1105 | + case 'Ticket' : |
|
| 1106 | + $ticket = $item->ticket(); |
|
| 1107 | + //right now we're only handling tickets here. Cause its expected that only tickets will have attendees right? |
|
| 1108 | + if ( ! $ticket instanceof EE_Ticket) { |
|
| 1109 | + continue; |
|
| 1110 | + } |
|
| 1111 | + try { |
|
| 1112 | + $event_name = $ticket->get_event_name(); |
|
| 1113 | + } catch (Exception $e) { |
|
| 1114 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1115 | + $event_name = esc_html__('Unknown Event', 'event_espresso'); |
|
| 1116 | + } |
|
| 1117 | + $event_name .= ' - ' . $item->get('LIN_name'); |
|
| 1118 | + $ticket_price = EEH_Template::format_currency($item->get('LIN_unit_price')); |
|
| 1119 | + // now get all of the registrations for this transaction that use this ticket |
|
| 1120 | + $registrations = $ticket->get_many_related('Registration', |
|
| 1121 | + array(array('TXN_ID' => $this->_transaction->ID()))); |
|
| 1122 | + foreach ($registrations as $registration) { |
|
| 1123 | + if ( ! $registration instanceof EE_Registration) { |
|
| 1124 | + continue; |
|
| 1125 | + } |
|
| 1126 | + $this->_template_args['event_attendees'][$registration->ID()]['STS_ID'] = $registration->status_ID(); |
|
| 1127 | + $this->_template_args['event_attendees'][$registration->ID()]['att_num'] = $registration->count(); |
|
| 1128 | + $this->_template_args['event_attendees'][$registration->ID()]['event_ticket_name'] = $event_name; |
|
| 1129 | + $this->_template_args['event_attendees'][$registration->ID()]['ticket_price'] = $ticket_price; |
|
| 1130 | + // attendee info |
|
| 1131 | + $attendee = $registration->get_first_related('Attendee'); |
|
| 1132 | + if ($attendee instanceof EE_Attendee) { |
|
| 1133 | + $this->_template_args['event_attendees'][$registration->ID()]['att_id'] = $attendee->ID(); |
|
| 1134 | + $this->_template_args['event_attendees'][$registration->ID()]['attendee'] = $attendee->full_name(); |
|
| 1135 | + $this->_template_args['event_attendees'][$registration->ID()]['email'] = '<a href="mailto:' . $attendee->email() . '?subject=' . $event_name . esc_html__(' Event', |
|
| 1136 | + 'event_espresso') . '">' . $attendee->email() . '</a>'; |
|
| 1137 | + $this->_template_args['event_attendees'][$registration->ID()]['address'] = EEH_Address::format($attendee, |
|
| 1138 | + 'inline', false, false); |
|
| 1139 | + } else { |
|
| 1140 | + $this->_template_args['event_attendees'][$registration->ID()]['att_id'] = ''; |
|
| 1141 | + $this->_template_args['event_attendees'][$registration->ID()]['attendee'] = ''; |
|
| 1142 | + $this->_template_args['event_attendees'][$registration->ID()]['email'] = ''; |
|
| 1143 | + $this->_template_args['event_attendees'][$registration->ID()]['address'] = ''; |
|
| 1144 | + } |
|
| 1145 | + } |
|
| 1146 | + break; |
|
| 1147 | + |
|
| 1148 | + } |
|
| 1149 | + } |
|
| 1150 | + } |
|
| 1151 | + |
|
| 1152 | + $this->_template_args['transaction_form_url'] = add_query_arg(array( |
|
| 1153 | + 'action' => 'edit_transaction', |
|
| 1154 | + 'process' => 'attendees' |
|
| 1155 | + ), TXN_ADMIN_URL); |
|
| 1156 | + echo EEH_Template::display_template(TXN_TEMPLATE_PATH . 'txn_admin_details_main_meta_box_attendees.template.php', |
|
| 1157 | + $this->_template_args, true); |
|
| 1158 | + |
|
| 1159 | + } else { |
|
| 1160 | + echo sprintf( |
|
| 1161 | + esc_html__('%1$sFor some reason, there are no attendees registered for this transaction. Likely the registration was abandoned in process.%2$s', |
|
| 1162 | + 'event_espresso'), |
|
| 1163 | + '<p class="important-notice">', |
|
| 1164 | + '</p>' |
|
| 1165 | + ); |
|
| 1166 | + } |
|
| 1167 | + } |
|
| 1168 | + |
|
| 1169 | + |
|
| 1170 | + /** |
|
| 1171 | + * txn_registrant_side_meta_box |
|
| 1172 | + * generates HTML for the Edit Transaction side meta box |
|
| 1173 | + * |
|
| 1174 | + * @access public |
|
| 1175 | + * @throws \EE_Error |
|
| 1176 | + * @return void |
|
| 1177 | + */ |
|
| 1178 | + public function txn_registrant_side_meta_box() |
|
| 1179 | + { |
|
| 1180 | + $primary_att = $this->_transaction->primary_registration() instanceof EE_Registration ? $this->_transaction->primary_registration()->get_first_related('Attendee') : null; |
|
| 1181 | + if ( ! $primary_att instanceof EE_Attendee) { |
|
| 1182 | + $this->_template_args['no_attendee_message'] = esc_html__('There is no attached contact for this transaction. The transaction either failed due to an error or was abandoned.', |
|
| 1183 | + 'event_espresso'); |
|
| 1184 | + $primary_att = EEM_Attendee::instance()->create_default_object(); |
|
| 1185 | + } |
|
| 1186 | + $this->_template_args['ATT_ID'] = $primary_att->ID(); |
|
| 1187 | + $this->_template_args['prime_reg_fname'] = $primary_att->fname(); |
|
| 1188 | + $this->_template_args['prime_reg_lname'] = $primary_att->lname(); |
|
| 1189 | + $this->_template_args['prime_reg_email'] = $primary_att->email(); |
|
| 1190 | + $this->_template_args['prime_reg_phone'] = $primary_att->phone(); |
|
| 1191 | + $this->_template_args['edit_attendee_url'] = EE_Admin_Page::add_query_args_and_nonce(array( |
|
| 1192 | + 'action' => 'edit_attendee', |
|
| 1193 | + 'post' => $primary_att->ID() |
|
| 1194 | + ), REG_ADMIN_URL); |
|
| 1195 | + // get formatted address for registrant |
|
| 1196 | + $this->_template_args['formatted_address'] = EEH_Address::format($primary_att); |
|
| 1197 | + echo EEH_Template::display_template(TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_registrant.template.php', |
|
| 1198 | + $this->_template_args, true); |
|
| 1199 | + } |
|
| 1200 | + |
|
| 1201 | + |
|
| 1202 | + /** |
|
| 1203 | + * txn_billing_info_side_meta_box |
|
| 1204 | + * generates HTML for the Edit Transaction side meta box |
|
| 1205 | + * |
|
| 1206 | + * @access public |
|
| 1207 | + * @return void |
|
| 1208 | + */ |
|
| 1209 | + public function txn_billing_info_side_meta_box() |
|
| 1210 | + { |
|
| 1211 | + |
|
| 1212 | + $this->_template_args['billing_form'] = $this->_transaction->billing_info(); |
|
| 1213 | + $this->_template_args['billing_form_url'] = add_query_arg( |
|
| 1214 | + array('action' => 'edit_transaction', 'process' => 'billing'), |
|
| 1215 | + TXN_ADMIN_URL |
|
| 1216 | + ); |
|
| 1217 | + |
|
| 1218 | + $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_side_meta_box_billing_info.template.php'; |
|
| 1219 | + echo EEH_Template::display_template($template_path, $this->_template_args, true);/**/ |
|
| 1220 | + } |
|
| 1221 | + |
|
| 1222 | + |
|
| 1223 | + /** |
|
| 1224 | + * apply_payments_or_refunds |
|
| 1225 | + * registers a payment or refund made towards a transaction |
|
| 1226 | + * |
|
| 1227 | + * @access public |
|
| 1228 | + * @return void |
|
| 1229 | + */ |
|
| 1230 | + public function apply_payments_or_refunds() |
|
| 1231 | + { |
|
| 1232 | + $json_response_data = array('return_data' => false); |
|
| 1233 | + $valid_data = $this->_validate_payment_request_data(); |
|
| 1234 | + if ( ! empty($valid_data)) { |
|
| 1235 | + $PAY_ID = $valid_data['PAY_ID']; |
|
| 1236 | + //save the new payment |
|
| 1237 | + $payment = $this->_create_payment_from_request_data($valid_data); |
|
| 1238 | + // get the TXN for this payment |
|
| 1239 | + $transaction = $payment->transaction(); |
|
| 1240 | + // verify transaction |
|
| 1241 | + if ($transaction instanceof EE_Transaction) { |
|
| 1242 | + // calculate_total_payments_and_update_status |
|
| 1243 | + $this->_process_transaction_payments($transaction); |
|
| 1244 | + $REG_IDs = $this->_get_REG_IDs_to_apply_payment_to($payment); |
|
| 1245 | + $this->_remove_existing_registration_payments($payment, $PAY_ID); |
|
| 1246 | + // apply payment to registrations (if applicable) |
|
| 1247 | + if ( ! empty($REG_IDs)) { |
|
| 1248 | + $this->_update_registration_payments($transaction, $payment, $REG_IDs); |
|
| 1249 | + $this->_maybe_send_notifications(); |
|
| 1250 | + // now process status changes for the same registrations |
|
| 1251 | + $this->_process_registration_status_change($transaction, $REG_IDs); |
|
| 1252 | + } |
|
| 1253 | + $this->_maybe_send_notifications($payment); |
|
| 1254 | + //prepare to render page |
|
| 1255 | + $json_response_data['return_data'] = $this->_build_payment_json_response($payment, $REG_IDs); |
|
| 1256 | + do_action('AHEE__Transactions_Admin_Page__apply_payments_or_refund__after_recording', $transaction, |
|
| 1257 | + $payment); |
|
| 1258 | + } else { |
|
| 1259 | + EE_Error::add_error( |
|
| 1260 | + esc_html__('A valid Transaction for this payment could not be retrieved.', 'event_espresso'), |
|
| 1261 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 1262 | + ); |
|
| 1263 | + } |
|
| 1264 | + } else { |
|
| 1265 | + EE_Error::add_error(esc_html__('The payment form data could not be processed. Please try again.', |
|
| 1266 | + 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 1267 | + } |
|
| 1268 | + |
|
| 1269 | + $notices = EE_Error::get_notices(false, false, false); |
|
| 1270 | + $this->_template_args = array( |
|
| 1271 | + 'data' => $json_response_data, |
|
| 1272 | + 'error' => $notices['errors'], |
|
| 1273 | + 'success' => $notices['success'] |
|
| 1274 | + ); |
|
| 1275 | + $this->_return_json(); |
|
| 1276 | + } |
|
| 1277 | + |
|
| 1278 | + |
|
| 1279 | + /** |
|
| 1280 | + * _validate_payment_request_data |
|
| 1281 | + * |
|
| 1282 | + * @return array |
|
| 1283 | + */ |
|
| 1284 | + protected function _validate_payment_request_data() |
|
| 1285 | + { |
|
| 1286 | + if ( ! isset($this->_req_data['txn_admin_payment'])) { |
|
| 1287 | + return false; |
|
| 1288 | + } |
|
| 1289 | + $payment_form = $this->_generate_payment_form_section(); |
|
| 1290 | + try { |
|
| 1291 | + if ($payment_form->was_submitted()) { |
|
| 1292 | + $payment_form->receive_form_submission(); |
|
| 1293 | + if ( ! $payment_form->is_valid()) { |
|
| 1294 | + $submission_error_messages = array(); |
|
| 1295 | + foreach ($payment_form->get_validation_errors_accumulated() as $validation_error) { |
|
| 1296 | + if ($validation_error instanceof EE_Validation_Error) { |
|
| 1297 | + $submission_error_messages[] = sprintf( |
|
| 1298 | + _x('%s : %s', 'Form Section Name : Form Validation Error', 'event_espresso'), |
|
| 1299 | + $validation_error->get_form_section()->html_label_text(), |
|
| 1300 | + $validation_error->getMessage() |
|
| 1301 | + ); |
|
| 1302 | + } |
|
| 1303 | + } |
|
| 1304 | + EE_Error::add_error(join('<br />', $submission_error_messages), __FILE__, __FUNCTION__, __LINE__); |
|
| 1305 | + |
|
| 1306 | + return array(); |
|
| 1307 | + } |
|
| 1308 | + } |
|
| 1309 | + } catch (EE_Error $e) { |
|
| 1310 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 1311 | + |
|
| 1312 | + return array(); |
|
| 1313 | + } |
|
| 1314 | + |
|
| 1315 | + return $payment_form->valid_data(); |
|
| 1316 | + } |
|
| 1317 | + |
|
| 1318 | + |
|
| 1319 | + /** |
|
| 1320 | + * _generate_payment_form_section |
|
| 1321 | + * |
|
| 1322 | + * @return EE_Form_Section_Proper |
|
| 1323 | + */ |
|
| 1324 | + protected function _generate_payment_form_section() |
|
| 1325 | + { |
|
| 1326 | + return new EE_Form_Section_Proper( |
|
| 1327 | + array( |
|
| 1328 | + 'name' => 'txn_admin_payment', |
|
| 1329 | + 'subsections' => array( |
|
| 1330 | + 'PAY_ID' => new EE_Text_Input( |
|
| 1331 | + array( |
|
| 1332 | + 'default' => 0, |
|
| 1333 | + 'required' => false, |
|
| 1334 | + 'html_label_text' => esc_html__('Payment ID', 'event_espresso'), |
|
| 1335 | + 'validation_strategies' => array(new EE_Int_Normalization()) |
|
| 1336 | + ) |
|
| 1337 | + ), |
|
| 1338 | + 'TXN_ID' => new EE_Text_Input( |
|
| 1339 | + array( |
|
| 1340 | + 'default' => 0, |
|
| 1341 | + 'required' => true, |
|
| 1342 | + 'html_label_text' => esc_html__('Transaction ID', 'event_espresso'), |
|
| 1343 | + 'validation_strategies' => array(new EE_Int_Normalization()) |
|
| 1344 | + ) |
|
| 1345 | + ), |
|
| 1346 | + 'type' => new EE_Text_Input( |
|
| 1347 | + array( |
|
| 1348 | + 'default' => 1, |
|
| 1349 | + 'required' => true, |
|
| 1350 | + 'html_label_text' => esc_html__('Payment or Refund', 'event_espresso'), |
|
| 1351 | + 'validation_strategies' => array(new EE_Int_Normalization()) |
|
| 1352 | + ) |
|
| 1353 | + ), |
|
| 1354 | + 'amount' => new EE_Text_Input( |
|
| 1355 | + array( |
|
| 1356 | + 'default' => 0, |
|
| 1357 | + 'required' => true, |
|
| 1358 | + 'html_label_text' => esc_html__('Payment amount', 'event_espresso'), |
|
| 1359 | + 'validation_strategies' => array(new EE_Float_Normalization()) |
|
| 1360 | + ) |
|
| 1361 | + ), |
|
| 1362 | + 'status' => new EE_Text_Input( |
|
| 1363 | + array( |
|
| 1364 | + 'default' => EEM_Payment::status_id_approved, |
|
| 1365 | + 'required' => true, |
|
| 1366 | + 'html_label_text' => esc_html__('Payment status', 'event_espresso'), |
|
| 1367 | + ) |
|
| 1368 | + ), |
|
| 1369 | + 'PMD_ID' => new EE_Text_Input( |
|
| 1370 | + array( |
|
| 1371 | + 'default' => 2, |
|
| 1372 | + 'required' => true, |
|
| 1373 | + 'html_label_text' => esc_html__('Payment Method', 'event_espresso'), |
|
| 1374 | + 'validation_strategies' => array(new EE_Int_Normalization()) |
|
| 1375 | + ) |
|
| 1376 | + ), |
|
| 1377 | + 'date' => new EE_Text_Input( |
|
| 1378 | + array( |
|
| 1379 | + 'default' => time(), |
|
| 1380 | + 'required' => true, |
|
| 1381 | + 'html_label_text' => esc_html__('Payment date', 'event_espresso'), |
|
| 1382 | + ) |
|
| 1383 | + ), |
|
| 1384 | + 'txn_id_chq_nmbr' => new EE_Text_Input( |
|
| 1385 | + array( |
|
| 1386 | + 'default' => '', |
|
| 1387 | + 'required' => false, |
|
| 1388 | + 'html_label_text' => esc_html__('Transaction or Cheque Number', 'event_espresso'), |
|
| 1389 | + 'validation_strategies' => array( |
|
| 1390 | + new EE_Max_Length_Validation_Strategy(esc_html__('Input too long', 'event_espresso'), |
|
| 1391 | + 100), |
|
| 1392 | + ) |
|
| 1393 | + ) |
|
| 1394 | + ), |
|
| 1395 | + 'po_number' => new EE_Text_Input( |
|
| 1396 | + array( |
|
| 1397 | + 'default' => '', |
|
| 1398 | + 'required' => false, |
|
| 1399 | + 'html_label_text' => esc_html__('Purchase Order Number', 'event_espresso'), |
|
| 1400 | + 'validation_strategies' => array( |
|
| 1401 | + new EE_Max_Length_Validation_Strategy(esc_html__('Input too long', 'event_espresso'), |
|
| 1402 | + 100), |
|
| 1403 | + ) |
|
| 1404 | + ) |
|
| 1405 | + ), |
|
| 1406 | + 'accounting' => new EE_Text_Input( |
|
| 1407 | + array( |
|
| 1408 | + 'default' => '', |
|
| 1409 | + 'required' => false, |
|
| 1410 | + 'html_label_text' => esc_html__('Extra Field for Accounting', 'event_espresso'), |
|
| 1411 | + 'validation_strategies' => array( |
|
| 1412 | + new EE_Max_Length_Validation_Strategy(esc_html__('Input too long', 'event_espresso'), |
|
| 1413 | + 100), |
|
| 1414 | + ) |
|
| 1415 | + ) |
|
| 1416 | + ), |
|
| 1417 | + ) |
|
| 1418 | + ) |
|
| 1419 | + ); |
|
| 1420 | + } |
|
| 1421 | + |
|
| 1422 | + |
|
| 1423 | + /** |
|
| 1424 | + * _create_payment_from_request_data |
|
| 1425 | + * |
|
| 1426 | + * @param array $valid_data |
|
| 1427 | + * |
|
| 1428 | + * @return EE_Payment |
|
| 1429 | + */ |
|
| 1430 | + protected function _create_payment_from_request_data($valid_data) |
|
| 1431 | + { |
|
| 1432 | + $PAY_ID = $valid_data['PAY_ID']; |
|
| 1433 | + // get payment amount |
|
| 1434 | + $amount = $valid_data['amount'] ? abs($valid_data['amount']) : 0; |
|
| 1435 | + // payments have a type value of 1 and refunds have a type value of -1 |
|
| 1436 | + // so multiplying amount by type will give a positive value for payments, and negative values for refunds |
|
| 1437 | + $amount = $valid_data['type'] < 0 ? $amount * -1 : $amount; |
|
| 1438 | + // for some reason the date string coming in has extra spaces between the date and time. This fixes that. |
|
| 1439 | + $date = $valid_data['date'] ? preg_replace('/\s+/', ' ', $valid_data['date']) : date('Y-m-d g:i a', |
|
| 1440 | + current_time('timestamp')); |
|
| 1441 | + $payment = EE_Payment::new_instance( |
|
| 1442 | + array( |
|
| 1443 | + 'TXN_ID' => $valid_data['TXN_ID'], |
|
| 1444 | + 'STS_ID' => $valid_data['status'], |
|
| 1445 | + 'PAY_timestamp' => $date, |
|
| 1446 | + 'PAY_source' => EEM_Payment_Method::scope_admin, |
|
| 1447 | + 'PMD_ID' => $valid_data['PMD_ID'], |
|
| 1448 | + 'PAY_amount' => $amount, |
|
| 1449 | + 'PAY_txn_id_chq_nmbr' => $valid_data['txn_id_chq_nmbr'], |
|
| 1450 | + 'PAY_po_number' => $valid_data['po_number'], |
|
| 1451 | + 'PAY_extra_accntng' => $valid_data['accounting'], |
|
| 1452 | + 'PAY_details' => $valid_data, |
|
| 1453 | + 'PAY_ID' => $PAY_ID |
|
| 1454 | + ), |
|
| 1455 | + '', |
|
| 1456 | + array('Y-m-d', 'g:i a') |
|
| 1457 | + ); |
|
| 1458 | + |
|
| 1459 | + if ( ! $payment->save()) { |
|
| 1460 | + EE_Error::add_error( |
|
| 1461 | + sprintf( |
|
| 1462 | + esc_html__('Payment %1$d has not been successfully saved to the database.', 'event_espresso'), |
|
| 1463 | + $payment->ID() |
|
| 1464 | + ), |
|
| 1465 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 1466 | + ); |
|
| 1467 | + } |
|
| 1468 | + |
|
| 1469 | + return $payment; |
|
| 1470 | + } |
|
| 1471 | + |
|
| 1472 | + |
|
| 1473 | + /** |
|
| 1474 | + * _process_transaction_payments |
|
| 1475 | + * |
|
| 1476 | + * @param \EE_Transaction $transaction |
|
| 1477 | + * |
|
| 1478 | + * @return array |
|
| 1479 | + */ |
|
| 1480 | + protected function _process_transaction_payments(EE_Transaction $transaction) |
|
| 1481 | + { |
|
| 1482 | + /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 1483 | + $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 1484 | + //update the transaction with this payment |
|
| 1485 | + if ($transaction_payments->calculate_total_payments_and_update_status($transaction)) { |
|
| 1486 | + EE_Error::add_success(esc_html__('The payment has been processed successfully.', 'event_espresso'), |
|
| 1487 | + __FILE__, __FUNCTION__, __LINE__); |
|
| 1488 | + } else { |
|
| 1489 | + EE_Error::add_error( |
|
| 1490 | + esc_html__('The payment was processed successfully but the amount paid for the transaction was not updated.', |
|
| 1491 | + 'event_espresso') |
|
| 1492 | + , __FILE__, __FUNCTION__, __LINE__ |
|
| 1493 | + ); |
|
| 1494 | + } |
|
| 1495 | + } |
|
| 1496 | + |
|
| 1497 | + |
|
| 1498 | + /** |
|
| 1499 | + * _get_REG_IDs_to_apply_payment_to |
|
| 1500 | + * |
|
| 1501 | + * returns a list of registration IDs that the payment will apply to |
|
| 1502 | + * |
|
| 1503 | + * @param \EE_Payment $payment |
|
| 1504 | + * |
|
| 1505 | + * @return array |
|
| 1506 | + */ |
|
| 1507 | + protected function _get_REG_IDs_to_apply_payment_to(EE_Payment $payment) |
|
| 1508 | + { |
|
| 1509 | + $REG_IDs = array(); |
|
| 1510 | + // grab array of IDs for specific registrations to apply changes to |
|
| 1511 | + if (isset($this->_req_data['txn_admin_payment']['registrations'])) { |
|
| 1512 | + $REG_IDs = (array)$this->_req_data['txn_admin_payment']['registrations']; |
|
| 1513 | + } |
|
| 1514 | + //nothing specified ? then get all reg IDs |
|
| 1515 | + if (empty($REG_IDs)) { |
|
| 1516 | + $registrations = $payment->transaction()->registrations(); |
|
| 1517 | + $REG_IDs = ! empty($registrations) ? array_keys($registrations) : $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 1518 | + } |
|
| 1519 | + |
|
| 1520 | + // ensure that REG_IDs are integers and NOT strings |
|
| 1521 | + return array_map('intval', $REG_IDs); |
|
| 1522 | + } |
|
| 1523 | + |
|
| 1524 | + |
|
| 1525 | + /** |
|
| 1526 | + * @return array |
|
| 1527 | + */ |
|
| 1528 | + public function existing_reg_payment_REG_IDs() |
|
| 1529 | + { |
|
| 1530 | + return $this->_existing_reg_payment_REG_IDs; |
|
| 1531 | + } |
|
| 1532 | + |
|
| 1533 | + |
|
| 1534 | + /** |
|
| 1535 | + * @param array $existing_reg_payment_REG_IDs |
|
| 1536 | + */ |
|
| 1537 | + public function set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs = null) |
|
| 1538 | + { |
|
| 1539 | + $this->_existing_reg_payment_REG_IDs = $existing_reg_payment_REG_IDs; |
|
| 1540 | + } |
|
| 1541 | + |
|
| 1542 | + |
|
| 1543 | + /** |
|
| 1544 | + * _get_existing_reg_payment_REG_IDs |
|
| 1545 | + * |
|
| 1546 | + * returns a list of registration IDs that the payment is currently related to |
|
| 1547 | + * as recorded in the database |
|
| 1548 | + * |
|
| 1549 | + * @param \EE_Payment $payment |
|
| 1550 | + * |
|
| 1551 | + * @return array |
|
| 1552 | + */ |
|
| 1553 | + protected function _get_existing_reg_payment_REG_IDs(EE_Payment $payment) |
|
| 1554 | + { |
|
| 1555 | + if ($this->existing_reg_payment_REG_IDs() === null) { |
|
| 1556 | + // let's get any existing reg payment records for this payment |
|
| 1557 | + $existing_reg_payment_REG_IDs = $payment->get_many_related('Registration'); |
|
| 1558 | + // but we only want the REG IDs, so grab the array keys |
|
| 1559 | + $existing_reg_payment_REG_IDs = ! empty($existing_reg_payment_REG_IDs) ? array_keys($existing_reg_payment_REG_IDs) : array(); |
|
| 1560 | + $this->set_existing_reg_payment_REG_IDs($existing_reg_payment_REG_IDs); |
|
| 1561 | + } |
|
| 1562 | + |
|
| 1563 | + return $this->existing_reg_payment_REG_IDs(); |
|
| 1564 | + } |
|
| 1565 | + |
|
| 1566 | + |
|
| 1567 | + /** |
|
| 1568 | + * _remove_existing_registration_payments |
|
| 1569 | + * |
|
| 1570 | + * this calculates the difference between existing relations |
|
| 1571 | + * to the supplied payment and the new list registration IDs, |
|
| 1572 | + * removes any related registrations that no longer apply, |
|
| 1573 | + * and then updates the registration paid fields |
|
| 1574 | + * |
|
| 1575 | + * @param \EE_Payment $payment |
|
| 1576 | + * @param int $PAY_ID |
|
| 1577 | + * |
|
| 1578 | + * @return bool; |
|
| 1579 | + */ |
|
| 1580 | + protected function _remove_existing_registration_payments(EE_Payment $payment, $PAY_ID = 0) |
|
| 1581 | + { |
|
| 1582 | + // newly created payments will have nothing recorded for $PAY_ID |
|
| 1583 | + if ($PAY_ID == 0) { |
|
| 1584 | + return false; |
|
| 1585 | + } |
|
| 1586 | + $existing_reg_payment_REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 1587 | + if (empty($existing_reg_payment_REG_IDs)) { |
|
| 1588 | + return false; |
|
| 1589 | + } |
|
| 1590 | + /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 1591 | + $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 1592 | + |
|
| 1593 | + return $transaction_payments->delete_registration_payments_and_update_registrations( |
|
| 1594 | + $payment, |
|
| 1595 | + array( |
|
| 1596 | + array( |
|
| 1597 | + 'PAY_ID' => $payment->ID(), |
|
| 1598 | + 'REG_ID' => array('IN', $existing_reg_payment_REG_IDs), |
|
| 1599 | + ) |
|
| 1600 | + ) |
|
| 1601 | + ); |
|
| 1602 | + } |
|
| 1603 | + |
|
| 1604 | + |
|
| 1605 | + /** |
|
| 1606 | + * _update_registration_payments |
|
| 1607 | + * |
|
| 1608 | + * this applies the payments to the selected registrations |
|
| 1609 | + * but only if they have not already been paid for |
|
| 1610 | + * |
|
| 1611 | + * @param EE_Transaction $transaction |
|
| 1612 | + * @param \EE_Payment $payment |
|
| 1613 | + * @param array $REG_IDs |
|
| 1614 | + * |
|
| 1615 | + * @return bool |
|
| 1616 | + */ |
|
| 1617 | + protected function _update_registration_payments( |
|
| 1618 | + EE_Transaction $transaction, |
|
| 1619 | + EE_Payment $payment, |
|
| 1620 | + $REG_IDs = array() |
|
| 1621 | + ) { |
|
| 1622 | + // we can pass our own custom set of registrations to EE_Payment_Processor::process_registration_payments() |
|
| 1623 | + // so let's do that using our set of REG_IDs from the form |
|
| 1624 | + $registration_query_where_params = array( |
|
| 1625 | + 'REG_ID' => array('IN', $REG_IDs) |
|
| 1626 | + ); |
|
| 1627 | + // but add in some conditions regarding payment, |
|
| 1628 | + // so that we don't apply payments to registrations that are free or have already been paid for |
|
| 1629 | + // but ONLY if the payment is NOT a refund ( ie: the payment amount is not negative ) |
|
| 1630 | + if ( ! $payment->is_a_refund()) { |
|
| 1631 | + $registration_query_where_params['REG_final_price'] = array('!=', 0); |
|
| 1632 | + $registration_query_where_params['REG_final_price*'] = array('!=', 'REG_paid', true); |
|
| 1633 | + } |
|
| 1634 | + //EEH_Debug_Tools::printr( $registration_query_where_params, '$registration_query_where_params', __FILE__, __LINE__ ); |
|
| 1635 | + $registrations = $transaction->registrations(array($registration_query_where_params)); |
|
| 1636 | + if ( ! empty($registrations)) { |
|
| 1637 | + /** @type EE_Payment_Processor $payment_processor */ |
|
| 1638 | + $payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
|
| 1639 | + $payment_processor->process_registration_payments($transaction, $payment, $registrations); |
|
| 1640 | + } |
|
| 1641 | + } |
|
| 1642 | + |
|
| 1643 | + |
|
| 1644 | + /** |
|
| 1645 | + * _process_registration_status_change |
|
| 1646 | + * |
|
| 1647 | + * This processes requested registration status changes for all the registrations |
|
| 1648 | + * on a given transaction and (optionally) sends out notifications for the changes. |
|
| 1649 | + * |
|
| 1650 | + * @param EE_Transaction $transaction |
|
| 1651 | + * @param array $REG_IDs |
|
| 1652 | + * |
|
| 1653 | + * @return bool |
|
| 1654 | + */ |
|
| 1655 | + protected function _process_registration_status_change(EE_Transaction $transaction, $REG_IDs = array()) |
|
| 1656 | + { |
|
| 1657 | + // first if there is no change in status then we get out. |
|
| 1658 | + if ( |
|
| 1659 | + ! isset($this->_req_data['txn_reg_status_change'], $this->_req_data['txn_reg_status_change']['reg_status']) |
|
| 1660 | + || $this->_req_data['txn_reg_status_change']['reg_status'] == 'NAN' |
|
| 1661 | + ) { |
|
| 1662 | + //no error message, no change requested, just nothing to do man. |
|
| 1663 | + return false; |
|
| 1664 | + } |
|
| 1665 | + /** @type EE_Transaction_Processor $transaction_processor */ |
|
| 1666 | + $transaction_processor = EE_Registry::instance()->load_class('Transaction_Processor'); |
|
| 1667 | + |
|
| 1668 | + // made it here dude? Oh WOW. K, let's take care of changing the statuses |
|
| 1669 | + return $transaction_processor->manually_update_registration_statuses( |
|
| 1670 | + $transaction, |
|
| 1671 | + sanitize_text_field($this->_req_data['txn_reg_status_change']['reg_status']), |
|
| 1672 | + array(array('REG_ID' => array('IN', $REG_IDs))) |
|
| 1673 | + ); |
|
| 1674 | + } |
|
| 1675 | + |
|
| 1676 | + |
|
| 1677 | + /** |
|
| 1678 | + * _build_payment_json_response |
|
| 1679 | + * |
|
| 1680 | + * @access public |
|
| 1681 | + * |
|
| 1682 | + * @param \EE_Payment $payment |
|
| 1683 | + * @param array $REG_IDs |
|
| 1684 | + * @param bool | null $delete_txn_reg_status_change |
|
| 1685 | + * |
|
| 1686 | + * @return array |
|
| 1687 | + */ |
|
| 1688 | + protected function _build_payment_json_response( |
|
| 1689 | + EE_Payment $payment, |
|
| 1690 | + $REG_IDs = array(), |
|
| 1691 | + $delete_txn_reg_status_change = null |
|
| 1692 | + ) { |
|
| 1693 | + // was the payment deleted ? |
|
| 1694 | + if (is_bool($delete_txn_reg_status_change)) { |
|
| 1695 | + return array( |
|
| 1696 | + 'PAY_ID' => $payment->ID(), |
|
| 1697 | + 'amount' => $payment->amount(), |
|
| 1698 | + 'total_paid' => $payment->transaction()->paid(), |
|
| 1699 | + 'txn_status' => $payment->transaction()->status_ID(), |
|
| 1700 | + 'pay_status' => $payment->STS_ID(), |
|
| 1701 | + 'registrations' => $this->_registration_payment_data_array($REG_IDs), |
|
| 1702 | + 'delete_txn_reg_status_change' => $delete_txn_reg_status_change, |
|
| 1703 | + ); |
|
| 1704 | + } else { |
|
| 1705 | + $this->_get_payment_status_array(); |
|
| 1706 | + |
|
| 1707 | + return array( |
|
| 1708 | + 'amount' => $payment->amount(), |
|
| 1709 | + 'total_paid' => $payment->transaction()->paid(), |
|
| 1710 | + 'txn_status' => $payment->transaction()->status_ID(), |
|
| 1711 | + 'pay_status' => $payment->STS_ID(), |
|
| 1712 | + 'PAY_ID' => $payment->ID(), |
|
| 1713 | + 'STS_ID' => $payment->STS_ID(), |
|
| 1714 | + 'status' => self::$_pay_status[$payment->STS_ID()], |
|
| 1715 | + 'date' => $payment->timestamp('Y-m-d', 'h:i a'), |
|
| 1716 | + 'method' => strtoupper($payment->source()), |
|
| 1717 | + 'PM_ID' => $payment->payment_method() ? $payment->payment_method()->ID() : 1, |
|
| 1718 | + 'gateway' => $payment->payment_method() ? $payment->payment_method()->admin_name() : esc_html__("Unknown", |
|
| 1719 | + 'event_espresso'), |
|
| 1720 | + 'gateway_response' => $payment->gateway_response(), |
|
| 1721 | + 'txn_id_chq_nmbr' => $payment->txn_id_chq_nmbr(), |
|
| 1722 | + 'po_number' => $payment->po_number(), |
|
| 1723 | + 'extra_accntng' => $payment->extra_accntng(), |
|
| 1724 | + 'registrations' => $this->_registration_payment_data_array($REG_IDs), |
|
| 1725 | + ); |
|
| 1726 | + } |
|
| 1727 | + } |
|
| 1728 | + |
|
| 1729 | + |
|
| 1730 | + /** |
|
| 1731 | + * delete_payment |
|
| 1732 | + * delete a payment or refund made towards a transaction |
|
| 1733 | + * |
|
| 1734 | + * @access public |
|
| 1735 | + * @return void |
|
| 1736 | + */ |
|
| 1737 | + public function delete_payment() |
|
| 1738 | + { |
|
| 1739 | + $json_response_data = array('return_data' => false); |
|
| 1740 | + $PAY_ID = isset($this->_req_data['delete_txn_admin_payment'], $this->_req_data['delete_txn_admin_payment']['PAY_ID']) ? absint($this->_req_data['delete_txn_admin_payment']['PAY_ID']) : 0; |
|
| 1741 | + if ($PAY_ID) { |
|
| 1742 | + $delete_txn_reg_status_change = isset($this->_req_data['delete_txn_reg_status_change']) ? $this->_req_data['delete_txn_reg_status_change'] : false; |
|
| 1743 | + $payment = EEM_Payment::instance()->get_one_by_ID($PAY_ID); |
|
| 1744 | + if ($payment instanceof EE_Payment) { |
|
| 1745 | + $REG_IDs = $this->_get_existing_reg_payment_REG_IDs($payment); |
|
| 1746 | + /** @type EE_Transaction_Payments $transaction_payments */ |
|
| 1747 | + $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments'); |
|
| 1748 | + if ($transaction_payments->delete_payment_and_update_transaction($payment)) { |
|
| 1749 | + $json_response_data['return_data'] = $this->_build_payment_json_response($payment, $REG_IDs, |
|
| 1750 | + $delete_txn_reg_status_change); |
|
| 1751 | + if ($delete_txn_reg_status_change) { |
|
| 1752 | + $this->_req_data['txn_reg_status_change'] = $delete_txn_reg_status_change; |
|
| 1753 | + //MAKE sure we also add the delete_txn_req_status_change to the |
|
| 1754 | + //$_REQUEST global because that's how messages will be looking for it. |
|
| 1755 | + $_REQUEST['txn_reg_status_change'] = $delete_txn_reg_status_change; |
|
| 1756 | + $this->_maybe_send_notifications(); |
|
| 1757 | + $this->_process_registration_status_change($payment->transaction(), $REG_IDs); |
|
| 1758 | + } |
|
| 1759 | + } |
|
| 1760 | + } else { |
|
| 1761 | + EE_Error::add_error( |
|
| 1762 | + esc_html__('Valid Payment data could not be retrieved from the database.', 'event_espresso'), |
|
| 1763 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 1764 | + ); |
|
| 1765 | + } |
|
| 1766 | + } else { |
|
| 1767 | + EE_Error::add_error( |
|
| 1768 | + esc_html__('A valid Payment ID was not received, therefore payment form data could not be loaded.', |
|
| 1769 | + 'event_espresso'), |
|
| 1770 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 1771 | + ); |
|
| 1772 | + } |
|
| 1773 | + $notices = EE_Error::get_notices(false, false, false); |
|
| 1774 | + $this->_template_args = array( |
|
| 1775 | + 'data' => $json_response_data, |
|
| 1776 | + 'success' => $notices['success'], |
|
| 1777 | + 'error' => $notices['errors'], |
|
| 1778 | + 'attention' => $notices['attention'] |
|
| 1779 | + ); |
|
| 1780 | + $this->_return_json(); |
|
| 1781 | + } |
|
| 1782 | + |
|
| 1783 | + |
|
| 1784 | + /** |
|
| 1785 | + * _registration_payment_data_array |
|
| 1786 | + * adds info for 'owing' and 'paid' for each registration to the json response |
|
| 1787 | + * |
|
| 1788 | + * @access protected |
|
| 1789 | + * |
|
| 1790 | + * @param array $REG_IDs |
|
| 1791 | + * |
|
| 1792 | + * @return array |
|
| 1793 | + */ |
|
| 1794 | + protected function _registration_payment_data_array($REG_IDs) |
|
| 1795 | + { |
|
| 1796 | + $registration_payment_data = array(); |
|
| 1797 | + //if non empty reg_ids lets get an array of registrations and update the values for the apply_payment/refund rows. |
|
| 1798 | + if ( ! empty($REG_IDs)) { |
|
| 1799 | + $registrations = EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $REG_IDs)))); |
|
| 1800 | + foreach ($registrations as $registration) { |
|
| 1801 | + if ($registration instanceof EE_Registration) { |
|
| 1802 | + $registration_payment_data[$registration->ID()] = array( |
|
| 1803 | + 'paid' => $registration->pretty_paid(), |
|
| 1804 | + 'owing' => EEH_Template::format_currency($registration->final_price() - $registration->paid()), |
|
| 1805 | + ); |
|
| 1806 | + } |
|
| 1807 | + } |
|
| 1808 | + } |
|
| 1809 | + |
|
| 1810 | + return $registration_payment_data; |
|
| 1811 | + } |
|
| 1812 | + |
|
| 1813 | + |
|
| 1814 | + /** |
|
| 1815 | + * _maybe_send_notifications |
|
| 1816 | + * |
|
| 1817 | + * determines whether or not the admin has indicated that notifications should be sent. |
|
| 1818 | + * If so, will toggle a filter switch for delivering registration notices. |
|
| 1819 | + * If passed an EE_Payment object, then it will trigger payment notifications instead. |
|
| 1820 | + * |
|
| 1821 | + * @access protected |
|
| 1822 | + * |
|
| 1823 | + * @param \EE_Payment | null $payment |
|
| 1824 | + */ |
|
| 1825 | + protected function _maybe_send_notifications($payment = null) |
|
| 1826 | + { |
|
| 1827 | + switch ($payment instanceof EE_Payment) { |
|
| 1828 | + // payment notifications |
|
| 1829 | + case true : |
|
| 1830 | + if ( |
|
| 1831 | + isset( |
|
| 1832 | + $this->_req_data['txn_payments'], |
|
| 1833 | + $this->_req_data['txn_payments']['send_notifications'] |
|
| 1834 | + ) && |
|
| 1835 | + filter_var($this->_req_data['txn_payments']['send_notifications'], FILTER_VALIDATE_BOOLEAN) |
|
| 1836 | + ) { |
|
| 1837 | + $this->_process_payment_notification($payment); |
|
| 1838 | + } |
|
| 1839 | + break; |
|
| 1840 | + // registration notifications |
|
| 1841 | + case false : |
|
| 1842 | + if ( |
|
| 1843 | + isset( |
|
| 1844 | + $this->_req_data['txn_reg_status_change'], |
|
| 1845 | + $this->_req_data['txn_reg_status_change']['send_notifications'] |
|
| 1846 | + ) && |
|
| 1847 | + filter_var($this->_req_data['txn_reg_status_change']['send_notifications'], FILTER_VALIDATE_BOOLEAN) |
|
| 1848 | + ) { |
|
| 1849 | + add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true'); |
|
| 1850 | + } |
|
| 1851 | + break; |
|
| 1852 | + } |
|
| 1853 | + } |
|
| 1854 | + |
|
| 1855 | + |
|
| 1856 | + /** |
|
| 1857 | + * _send_payment_reminder |
|
| 1858 | + * generates HTML for the View Transaction Details Admin page |
|
| 1859 | + * |
|
| 1860 | + * @access protected |
|
| 1861 | + * @return void |
|
| 1862 | + */ |
|
| 1863 | + protected function _send_payment_reminder() |
|
| 1864 | + { |
|
| 1865 | + $TXN_ID = ( ! empty($this->_req_data['TXN_ID'])) ? absint($this->_req_data['TXN_ID']) : false; |
|
| 1866 | + $transaction = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
|
| 1867 | + $query_args = isset($this->_req_data['redirect_to']) ? array( |
|
| 1868 | + 'action' => $this->_req_data['redirect_to'], |
|
| 1869 | + 'TXN_ID' => $this->_req_data['TXN_ID'] |
|
| 1870 | + ) : array(); |
|
| 1871 | + do_action('AHEE__Transactions_Admin_Page___send_payment_reminder__process_admin_payment_reminder', |
|
| 1872 | + $transaction); |
|
| 1873 | + $this->_redirect_after_action(false, esc_html__('payment reminder', 'event_espresso'), |
|
| 1874 | + esc_html__('sent', 'event_espresso'), $query_args, true); |
|
| 1875 | + } |
|
| 1876 | + |
|
| 1877 | + |
|
| 1878 | + /** |
|
| 1879 | + * get_transactions |
|
| 1880 | + * get transactions for given parameters (used by list table) |
|
| 1881 | + * |
|
| 1882 | + * @param int $perpage how many transactions displayed per page |
|
| 1883 | + * @param boolean $count return the count or objects |
|
| 1884 | + * @param string $view |
|
| 1885 | + * |
|
| 1886 | + * @return mixed int = count || array of transaction objects |
|
| 1887 | + */ |
|
| 1888 | + public function get_transactions($perpage, $count = false, $view = '') |
|
| 1889 | + { |
|
| 1890 | + |
|
| 1891 | + $TXN = EEM_Transaction::instance(); |
|
| 1892 | + |
|
| 1893 | + $start_date = isset($this->_req_data['txn-filter-start-date']) ? wp_strip_all_tags($this->_req_data['txn-filter-start-date']) : date('m/d/Y', |
|
| 1894 | + strtotime('-10 year')); |
|
| 1895 | + $end_date = isset($this->_req_data['txn-filter-end-date']) ? wp_strip_all_tags($this->_req_data['txn-filter-end-date']) : date('m/d/Y'); |
|
| 1896 | + |
|
| 1897 | + //make sure our timestamps start and end right at the boundaries for each day |
|
| 1898 | + $start_date = date('Y-m-d', strtotime($start_date)) . ' 00:00:00'; |
|
| 1899 | + $end_date = date('Y-m-d', strtotime($end_date)) . ' 23:59:59'; |
|
| 1900 | + |
|
| 1901 | + |
|
| 1902 | + //convert to timestamps |
|
| 1903 | + $start_date = strtotime($start_date); |
|
| 1904 | + $end_date = strtotime($end_date); |
|
| 1905 | + |
|
| 1906 | + //makes sure start date is the lowest value and vice versa |
|
| 1907 | + $start_date = min($start_date, $end_date); |
|
| 1908 | + $end_date = max($start_date, $end_date); |
|
| 1909 | + |
|
| 1910 | + //convert to correct format for query |
|
| 1911 | + $start_date = EEM_Transaction::instance()->convert_datetime_for_query('TXN_timestamp', |
|
| 1912 | + date('Y-m-d H:i:s', $start_date), 'Y-m-d H:i:s'); |
|
| 1913 | + $end_date = EEM_Transaction::instance()->convert_datetime_for_query('TXN_timestamp', |
|
| 1914 | + date('Y-m-d H:i:s', $end_date), 'Y-m-d H:i:s'); |
|
| 1915 | + |
|
| 1916 | + |
|
| 1917 | + //set orderby |
|
| 1918 | + $this->_req_data['orderby'] = ! empty($this->_req_data['orderby']) ? $this->_req_data['orderby'] : ''; |
|
| 1919 | + |
|
| 1920 | + switch ($this->_req_data['orderby']) { |
|
| 1921 | + case 'TXN_ID': |
|
| 1922 | + $orderby = 'TXN_ID'; |
|
| 1923 | + break; |
|
| 1924 | + case 'ATT_fname': |
|
| 1925 | + $orderby = 'Registration.Attendee.ATT_fname'; |
|
| 1926 | + break; |
|
| 1927 | + case 'event_name': |
|
| 1928 | + $orderby = 'Registration.Event.EVT_name'; |
|
| 1929 | + break; |
|
| 1930 | + default: //'TXN_timestamp' |
|
| 1931 | + $orderby = 'TXN_timestamp'; |
|
| 1932 | + } |
|
| 1933 | + |
|
| 1934 | + $sort = (isset($this->_req_data['order']) && ! empty($this->_req_data['order'])) ? $this->_req_data['order'] : 'DESC'; |
|
| 1935 | + $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged']) ? $this->_req_data['paged'] : 1; |
|
| 1936 | + $per_page = isset($perpage) && ! empty($perpage) ? $perpage : 10; |
|
| 1937 | + $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage']) ? $this->_req_data['perpage'] : $per_page; |
|
| 1938 | + |
|
| 1939 | + $offset = ($current_page - 1) * $per_page; |
|
| 1940 | + $limit = array($offset, $per_page); |
|
| 1941 | + |
|
| 1942 | + $_where = array( |
|
| 1943 | + 'TXN_timestamp' => array('BETWEEN', array($start_date, $end_date)), |
|
| 1944 | + 'Registration.REG_count' => 1 |
|
| 1945 | + ); |
|
| 1946 | + |
|
| 1947 | + if (isset($this->_req_data['EVT_ID'])) { |
|
| 1948 | + $_where['Registration.EVT_ID'] = $this->_req_data['EVT_ID']; |
|
| 1949 | + } |
|
| 1950 | + |
|
| 1951 | + if (isset($this->_req_data['s'])) { |
|
| 1952 | + $search_string = '%' . $this->_req_data['s'] . '%'; |
|
| 1953 | + $_where['OR'] = array( |
|
| 1954 | + 'Registration.Event.EVT_name' => array('LIKE', $search_string), |
|
| 1955 | + 'Registration.Event.EVT_desc' => array('LIKE', $search_string), |
|
| 1956 | + 'Registration.Event.EVT_short_desc' => array('LIKE', $search_string), |
|
| 1957 | + 'Registration.Attendee.ATT_full_name' => array('LIKE', $search_string), |
|
| 1958 | + 'Registration.Attendee.ATT_fname' => array('LIKE', $search_string), |
|
| 1959 | + 'Registration.Attendee.ATT_lname' => array('LIKE', $search_string), |
|
| 1960 | + 'Registration.Attendee.ATT_short_bio' => array('LIKE', $search_string), |
|
| 1961 | + 'Registration.Attendee.ATT_email' => array('LIKE', $search_string), |
|
| 1962 | + 'Registration.Attendee.ATT_address' => array('LIKE', $search_string), |
|
| 1963 | + 'Registration.Attendee.ATT_address2' => array('LIKE', $search_string), |
|
| 1964 | + 'Registration.Attendee.ATT_city' => array('LIKE', $search_string), |
|
| 1965 | + 'Registration.REG_final_price' => array('LIKE', $search_string), |
|
| 1966 | + 'Registration.REG_code' => array('LIKE', $search_string), |
|
| 1967 | + 'Registration.REG_count' => array('LIKE', $search_string), |
|
| 1968 | + 'Registration.REG_group_size' => array('LIKE', $search_string), |
|
| 1969 | + 'Registration.Ticket.TKT_name' => array('LIKE', $search_string), |
|
| 1970 | + 'Registration.Ticket.TKT_description' => array('LIKE', $search_string), |
|
| 1971 | + 'Payment.PAY_source' => array('LIKE', $search_string), |
|
| 1972 | + 'Payment.Payment_Method.PMD_name' => array('LIKE', $search_string), |
|
| 1973 | + 'TXN_session_data' => array('LIKE', $search_string), |
|
| 1974 | + 'Payment.PAY_txn_id_chq_nmbr' => array('LIKE', $search_string) |
|
| 1975 | + ); |
|
| 1976 | + } |
|
| 1977 | + |
|
| 1978 | + //failed transactions |
|
| 1979 | + $failed = ( ! empty($this->_req_data['status']) && $this->_req_data['status'] == 'failed' && ! $count) || ($count && $view == 'failed') ? true : false; |
|
| 1980 | + $abandoned = ( ! empty($this->_req_data['status']) && $this->_req_data['status'] == 'abandoned' && ! $count) || ($count && $view == 'abandoned') ? true : false; |
|
| 1981 | + |
|
| 1982 | + if ($failed) { |
|
| 1983 | + $_where['STS_ID'] = EEM_Transaction::failed_status_code; |
|
| 1984 | + } else if ($abandoned) { |
|
| 1985 | + $_where['STS_ID'] = EEM_Transaction::abandoned_status_code; |
|
| 1986 | + } else { |
|
| 1987 | + $_where['STS_ID'] = array('!=', EEM_Transaction::failed_status_code); |
|
| 1988 | + $_where['STS_ID*'] = array('!=', EEM_Transaction::abandoned_status_code); |
|
| 1989 | + } |
|
| 1990 | + |
|
| 1991 | + $query_params = array( |
|
| 1992 | + $_where, |
|
| 1993 | + 'order_by' => array($orderby => $sort), |
|
| 1994 | + 'limit' => $limit, |
|
| 1995 | + 'default_where_conditions' => EEM_Base::default_where_conditions_this_only |
|
| 1996 | + ); |
|
| 1997 | + |
|
| 1998 | + $transactions = $count ? $TXN->count(array($_where), 'TXN_ID', true) : $TXN->get_all($query_params); |
|
| 1999 | + |
|
| 2000 | + |
|
| 2001 | + return $transactions; |
|
| 2002 | + |
|
| 2003 | + } |
|
| 2004 | 2004 | |
| 2005 | 2005 | |
| 2006 | 2006 | } |
@@ -13,1095 +13,1095 @@ |
||
| 13 | 13 | class EED_Messages extends EED_Module |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * This holds the EE_messages controller |
|
| 18 | - * |
|
| 19 | - * @deprecated 4.9.0 |
|
| 20 | - * @var EE_messages $_EEMSG |
|
| 21 | - */ |
|
| 22 | - protected static $_EEMSG; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @type EE_Message_Resource_Manager $_message_resource_manager |
|
| 26 | - */ |
|
| 27 | - protected static $_message_resource_manager; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * This holds the EE_Messages_Processor business class. |
|
| 31 | - * |
|
| 32 | - * @type EE_Messages_Processor |
|
| 33 | - */ |
|
| 34 | - protected static $_MSG_PROCESSOR; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * holds all the paths for various messages components. |
|
| 38 | - * Utilized by autoloader registry |
|
| 39 | - * |
|
| 40 | - * @var array |
|
| 41 | - */ |
|
| 42 | - protected static $_MSG_PATHS; |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * This will hold an array of messages template packs that are registered in the messages system. |
|
| 47 | - * Format is: |
|
| 48 | - * array( |
|
| 49 | - * 'template_pack_dbref' => EE_Messages_Template_Pack (instance) |
|
| 50 | - * ) |
|
| 51 | - * |
|
| 52 | - * @var EE_Messages_Template_Pack[] |
|
| 53 | - */ |
|
| 54 | - protected static $_TMP_PACKS = array(); |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @return EED_Messages |
|
| 59 | - */ |
|
| 60 | - public static function instance() |
|
| 61 | - { |
|
| 62 | - return parent::get_instance(__CLASS__); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
| 68 | - * |
|
| 69 | - * @since 4.5.0 |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - public static function set_hooks() |
|
| 73 | - { |
|
| 74 | - //actions |
|
| 75 | - add_action('AHEE__EE_Payment_Processor__update_txn_based_on_payment', array('EED_Messages', 'payment'), 10, 2); |
|
| 76 | - add_action('AHEE__EE_Registration_Processor__trigger_registration_update_notifications', |
|
| 77 | - array('EED_Messages', 'maybe_registration'), 10, 2); |
|
| 78 | - //filters |
|
| 79 | - add_filter('FHEE__EE_Registration__receipt_url__receipt_url', |
|
| 80 | - array('EED_Messages', 'registration_message_trigger_url'), 10, 4); |
|
| 81 | - add_filter('FHEE__EE_Registration__invoice_url__invoice_url', |
|
| 82 | - array('EED_Messages', 'registration_message_trigger_url'), 10, 4); |
|
| 83 | - //register routes |
|
| 84 | - self::_register_routes(); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
| 89 | - * |
|
| 90 | - * @access public |
|
| 91 | - * @return void |
|
| 92 | - */ |
|
| 93 | - public static function set_hooks_admin() |
|
| 94 | - { |
|
| 95 | - //actions |
|
| 96 | - add_action('AHEE__EE_Payment_Processor__update_txn_based_on_payment', array('EED_Messages', 'payment'), 10, 2); |
|
| 97 | - add_action('AHEE__Transactions_Admin_Page___send_payment_reminder__process_admin_payment_reminder', |
|
| 98 | - array('EED_Messages', 'payment_reminder'), 10); |
|
| 99 | - add_action('AHEE__EE_Registration_Processor__trigger_registration_update_notifications', |
|
| 100 | - array('EED_Messages', 'maybe_registration'), 10, 3); |
|
| 101 | - add_action('AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send__with_registrations', |
|
| 102 | - array('EED_Messages', 'send_newsletter_message'), 10, 2); |
|
| 103 | - add_action('AHEE__EES_Espresso_Cancelled__process_shortcode__transaction', |
|
| 104 | - array('EED_Messages', 'cancelled_registration'), 10); |
|
| 105 | - add_action('AHEE__EE_Admin_Page___process_admin_payment_notification', |
|
| 106 | - array('EED_Messages', 'process_admin_payment'), 10, 1); |
|
| 107 | - //filters |
|
| 108 | - add_filter('FHEE__EE_Admin_Page___process_resend_registration__success', |
|
| 109 | - array('EED_Messages', 'process_resend'), 10, 2); |
|
| 110 | - add_filter('FHEE__EE_Registration__receipt_url__receipt_url', |
|
| 111 | - array('EED_Messages', 'registration_message_trigger_url'), 10, 4); |
|
| 112 | - add_filter('FHEE__EE_Registration__invoice_url__invoice_url', |
|
| 113 | - array('EED_Messages', 'registration_message_trigger_url'), 10, 4); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * All the message triggers done by route go in here. |
|
| 119 | - * |
|
| 120 | - * @since 4.5.0 |
|
| 121 | - * @return void |
|
| 122 | - */ |
|
| 123 | - protected static function _register_routes() |
|
| 124 | - { |
|
| 125 | - EE_Config::register_route('msg_url_trigger', 'Messages', 'run'); |
|
| 126 | - EE_Config::register_route('msg_cron_trigger', 'Messages', 'execute_batch_request'); |
|
| 127 | - EE_Config::register_route('msg_browser_trigger', 'Messages', 'browser_trigger'); |
|
| 128 | - EE_Config::register_route('msg_browser_error_trigger', 'Messages', 'browser_error_trigger'); |
|
| 129 | - do_action('AHEE__EED_Messages___register_routes'); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * This is called when a browser display trigger is executed. |
|
| 135 | - * The browser display trigger is typically used when a already generated message is displayed directly in the |
|
| 136 | - * browser. |
|
| 137 | - * |
|
| 138 | - * @since 4.9.0 |
|
| 139 | - * @param WP $WP |
|
| 140 | - */ |
|
| 141 | - public function browser_trigger($WP) |
|
| 142 | - { |
|
| 143 | - //ensure controller is loaded |
|
| 144 | - self::_load_controller(); |
|
| 145 | - $token = EE_Registry::instance()->REQ->get('token'); |
|
| 146 | - try { |
|
| 147 | - $mtg = new EE_Message_Generated_From_Token($token, 'html', self::$_message_resource_manager); |
|
| 148 | - self::$_MSG_PROCESSOR->generate_and_send_now($mtg); |
|
| 149 | - } catch (EE_Error $e) { |
|
| 150 | - $error_msg = __('Please note that a system message failed to send due to a technical issue.', |
|
| 151 | - 'event_espresso'); |
|
| 152 | - // add specific message for developers if WP_DEBUG in on |
|
| 153 | - $error_msg .= '||' . $e->getMessage(); |
|
| 154 | - EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * This is called when a browser error trigger is executed. |
|
| 161 | - * When triggered this will grab the EE_Message matching the token in the request and use that to get the error |
|
| 162 | - * message and display it. |
|
| 163 | - * |
|
| 164 | - * @since 4.9.0 |
|
| 165 | - * @param $WP |
|
| 166 | - */ |
|
| 167 | - public function browser_error_trigger($WP) |
|
| 168 | - { |
|
| 169 | - $token = EE_Registry::instance()->REQ->get('token'); |
|
| 170 | - if ($token) { |
|
| 171 | - $message = EEM_Message::instance()->get_one_by_token($token); |
|
| 172 | - if ($message instanceof EE_Message) { |
|
| 173 | - header('HTTP/1.1 200 OK'); |
|
| 174 | - $error_msg = nl2br($message->error_message()); |
|
| 175 | - ?> |
|
| 16 | + /** |
|
| 17 | + * This holds the EE_messages controller |
|
| 18 | + * |
|
| 19 | + * @deprecated 4.9.0 |
|
| 20 | + * @var EE_messages $_EEMSG |
|
| 21 | + */ |
|
| 22 | + protected static $_EEMSG; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @type EE_Message_Resource_Manager $_message_resource_manager |
|
| 26 | + */ |
|
| 27 | + protected static $_message_resource_manager; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * This holds the EE_Messages_Processor business class. |
|
| 31 | + * |
|
| 32 | + * @type EE_Messages_Processor |
|
| 33 | + */ |
|
| 34 | + protected static $_MSG_PROCESSOR; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * holds all the paths for various messages components. |
|
| 38 | + * Utilized by autoloader registry |
|
| 39 | + * |
|
| 40 | + * @var array |
|
| 41 | + */ |
|
| 42 | + protected static $_MSG_PATHS; |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * This will hold an array of messages template packs that are registered in the messages system. |
|
| 47 | + * Format is: |
|
| 48 | + * array( |
|
| 49 | + * 'template_pack_dbref' => EE_Messages_Template_Pack (instance) |
|
| 50 | + * ) |
|
| 51 | + * |
|
| 52 | + * @var EE_Messages_Template_Pack[] |
|
| 53 | + */ |
|
| 54 | + protected static $_TMP_PACKS = array(); |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @return EED_Messages |
|
| 59 | + */ |
|
| 60 | + public static function instance() |
|
| 61 | + { |
|
| 62 | + return parent::get_instance(__CLASS__); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
| 68 | + * |
|
| 69 | + * @since 4.5.0 |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + public static function set_hooks() |
|
| 73 | + { |
|
| 74 | + //actions |
|
| 75 | + add_action('AHEE__EE_Payment_Processor__update_txn_based_on_payment', array('EED_Messages', 'payment'), 10, 2); |
|
| 76 | + add_action('AHEE__EE_Registration_Processor__trigger_registration_update_notifications', |
|
| 77 | + array('EED_Messages', 'maybe_registration'), 10, 2); |
|
| 78 | + //filters |
|
| 79 | + add_filter('FHEE__EE_Registration__receipt_url__receipt_url', |
|
| 80 | + array('EED_Messages', 'registration_message_trigger_url'), 10, 4); |
|
| 81 | + add_filter('FHEE__EE_Registration__invoice_url__invoice_url', |
|
| 82 | + array('EED_Messages', 'registration_message_trigger_url'), 10, 4); |
|
| 83 | + //register routes |
|
| 84 | + self::_register_routes(); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
|
| 89 | + * |
|
| 90 | + * @access public |
|
| 91 | + * @return void |
|
| 92 | + */ |
|
| 93 | + public static function set_hooks_admin() |
|
| 94 | + { |
|
| 95 | + //actions |
|
| 96 | + add_action('AHEE__EE_Payment_Processor__update_txn_based_on_payment', array('EED_Messages', 'payment'), 10, 2); |
|
| 97 | + add_action('AHEE__Transactions_Admin_Page___send_payment_reminder__process_admin_payment_reminder', |
|
| 98 | + array('EED_Messages', 'payment_reminder'), 10); |
|
| 99 | + add_action('AHEE__EE_Registration_Processor__trigger_registration_update_notifications', |
|
| 100 | + array('EED_Messages', 'maybe_registration'), 10, 3); |
|
| 101 | + add_action('AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send__with_registrations', |
|
| 102 | + array('EED_Messages', 'send_newsletter_message'), 10, 2); |
|
| 103 | + add_action('AHEE__EES_Espresso_Cancelled__process_shortcode__transaction', |
|
| 104 | + array('EED_Messages', 'cancelled_registration'), 10); |
|
| 105 | + add_action('AHEE__EE_Admin_Page___process_admin_payment_notification', |
|
| 106 | + array('EED_Messages', 'process_admin_payment'), 10, 1); |
|
| 107 | + //filters |
|
| 108 | + add_filter('FHEE__EE_Admin_Page___process_resend_registration__success', |
|
| 109 | + array('EED_Messages', 'process_resend'), 10, 2); |
|
| 110 | + add_filter('FHEE__EE_Registration__receipt_url__receipt_url', |
|
| 111 | + array('EED_Messages', 'registration_message_trigger_url'), 10, 4); |
|
| 112 | + add_filter('FHEE__EE_Registration__invoice_url__invoice_url', |
|
| 113 | + array('EED_Messages', 'registration_message_trigger_url'), 10, 4); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * All the message triggers done by route go in here. |
|
| 119 | + * |
|
| 120 | + * @since 4.5.0 |
|
| 121 | + * @return void |
|
| 122 | + */ |
|
| 123 | + protected static function _register_routes() |
|
| 124 | + { |
|
| 125 | + EE_Config::register_route('msg_url_trigger', 'Messages', 'run'); |
|
| 126 | + EE_Config::register_route('msg_cron_trigger', 'Messages', 'execute_batch_request'); |
|
| 127 | + EE_Config::register_route('msg_browser_trigger', 'Messages', 'browser_trigger'); |
|
| 128 | + EE_Config::register_route('msg_browser_error_trigger', 'Messages', 'browser_error_trigger'); |
|
| 129 | + do_action('AHEE__EED_Messages___register_routes'); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * This is called when a browser display trigger is executed. |
|
| 135 | + * The browser display trigger is typically used when a already generated message is displayed directly in the |
|
| 136 | + * browser. |
|
| 137 | + * |
|
| 138 | + * @since 4.9.0 |
|
| 139 | + * @param WP $WP |
|
| 140 | + */ |
|
| 141 | + public function browser_trigger($WP) |
|
| 142 | + { |
|
| 143 | + //ensure controller is loaded |
|
| 144 | + self::_load_controller(); |
|
| 145 | + $token = EE_Registry::instance()->REQ->get('token'); |
|
| 146 | + try { |
|
| 147 | + $mtg = new EE_Message_Generated_From_Token($token, 'html', self::$_message_resource_manager); |
|
| 148 | + self::$_MSG_PROCESSOR->generate_and_send_now($mtg); |
|
| 149 | + } catch (EE_Error $e) { |
|
| 150 | + $error_msg = __('Please note that a system message failed to send due to a technical issue.', |
|
| 151 | + 'event_espresso'); |
|
| 152 | + // add specific message for developers if WP_DEBUG in on |
|
| 153 | + $error_msg .= '||' . $e->getMessage(); |
|
| 154 | + EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * This is called when a browser error trigger is executed. |
|
| 161 | + * When triggered this will grab the EE_Message matching the token in the request and use that to get the error |
|
| 162 | + * message and display it. |
|
| 163 | + * |
|
| 164 | + * @since 4.9.0 |
|
| 165 | + * @param $WP |
|
| 166 | + */ |
|
| 167 | + public function browser_error_trigger($WP) |
|
| 168 | + { |
|
| 169 | + $token = EE_Registry::instance()->REQ->get('token'); |
|
| 170 | + if ($token) { |
|
| 171 | + $message = EEM_Message::instance()->get_one_by_token($token); |
|
| 172 | + if ($message instanceof EE_Message) { |
|
| 173 | + header('HTTP/1.1 200 OK'); |
|
| 174 | + $error_msg = nl2br($message->error_message()); |
|
| 175 | + ?> |
|
| 176 | 176 | <!DOCTYPE html> |
| 177 | 177 | <html> |
| 178 | 178 | <head></head> |
| 179 | 179 | <body> |
| 180 | 180 | <?php echo empty($error_msg) |
| 181 | - ? esc_html__('Unfortunately, we were unable to capture the error message for this message.', |
|
| 182 | - 'event_espresso') |
|
| 183 | - : wp_kses( |
|
| 184 | - $error_msg, |
|
| 185 | - array( |
|
| 186 | - 'a' => array( |
|
| 187 | - 'href' => array(), |
|
| 188 | - 'title' => array(), |
|
| 189 | - ), |
|
| 190 | - 'span' => array(), |
|
| 191 | - 'div' => array(), |
|
| 192 | - 'p' => array(), |
|
| 193 | - 'strong' => array(), |
|
| 194 | - 'em' => array(), |
|
| 195 | - 'br' => array(), |
|
| 196 | - ) |
|
| 197 | - ); ?> |
|
| 181 | + ? esc_html__('Unfortunately, we were unable to capture the error message for this message.', |
|
| 182 | + 'event_espresso') |
|
| 183 | + : wp_kses( |
|
| 184 | + $error_msg, |
|
| 185 | + array( |
|
| 186 | + 'a' => array( |
|
| 187 | + 'href' => array(), |
|
| 188 | + 'title' => array(), |
|
| 189 | + ), |
|
| 190 | + 'span' => array(), |
|
| 191 | + 'div' => array(), |
|
| 192 | + 'p' => array(), |
|
| 193 | + 'strong' => array(), |
|
| 194 | + 'em' => array(), |
|
| 195 | + 'br' => array(), |
|
| 196 | + ) |
|
| 197 | + ); ?> |
|
| 198 | 198 | </body> |
| 199 | 199 | </html> |
| 200 | 200 | <?php |
| 201 | - exit; |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - return; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * This runs when the msg_url_trigger route has initiated. |
|
| 210 | - * |
|
| 211 | - * @since 4.5.0 |
|
| 212 | - * @param WP $WP |
|
| 213 | - * @throws EE_Error |
|
| 214 | - * @return void |
|
| 215 | - */ |
|
| 216 | - public function run($WP) |
|
| 217 | - { |
|
| 218 | - //ensure controller is loaded |
|
| 219 | - self::_load_controller(); |
|
| 220 | - // attempt to process message |
|
| 221 | - try { |
|
| 222 | - /** @type EE_Message_To_Generate_From_Request $message_to_generate */ |
|
| 223 | - $message_to_generate = EE_Registry::instance()->load_lib('Message_To_Generate_From_Request'); |
|
| 224 | - self::$_MSG_PROCESSOR->generate_and_send_now($message_to_generate); |
|
| 225 | - } catch (EE_Error $e) { |
|
| 226 | - $error_msg = __('Please note that a system message failed to send due to a technical issue.', |
|
| 227 | - 'event_espresso'); |
|
| 228 | - // add specific message for developers if WP_DEBUG in on |
|
| 229 | - $error_msg .= '||' . $e->getMessage(); |
|
| 230 | - EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * This is triggered by the 'msg_cron_trigger' route. |
|
| 237 | - * |
|
| 238 | - * @param WP $WP |
|
| 239 | - */ |
|
| 240 | - public function execute_batch_request($WP) |
|
| 241 | - { |
|
| 242 | - $this->run_cron(); |
|
| 243 | - header('HTTP/1.1 200 OK'); |
|
| 244 | - exit(); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * This gets executed on wp_cron jobs or when a batch request is initiated on its own separate non regular wp |
|
| 250 | - * request. |
|
| 251 | - */ |
|
| 252 | - public function run_cron() |
|
| 253 | - { |
|
| 254 | - self::_load_controller(); |
|
| 255 | - //get required vars |
|
| 256 | - $cron_type = EE_Registry::instance()->REQ->get('type'); |
|
| 257 | - $transient_key = EE_Registry::instance()->REQ->get('key'); |
|
| 258 | - |
|
| 259 | - //now let's verify transient, if not valid exit immediately |
|
| 260 | - if (! get_transient($transient_key)) { |
|
| 261 | - /** |
|
| 262 | - * trigger error so this gets in the error logs. This is important because it happens on a non-user request. |
|
| 263 | - */ |
|
| 264 | - trigger_error(esc_attr__('Invalid Request (Transient does not exist)', 'event_espresso')); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - //if made it here, lets' delete the transient to keep the db clean |
|
| 268 | - delete_transient($transient_key); |
|
| 269 | - |
|
| 270 | - if (apply_filters('FHEE__EED_Messages__run_cron__use_wp_cron', true)) { |
|
| 271 | - |
|
| 272 | - $method = 'batch_' . $cron_type . '_from_queue'; |
|
| 273 | - if (method_exists(self::$_MSG_PROCESSOR, $method)) { |
|
| 274 | - self::$_MSG_PROCESSOR->$method(); |
|
| 275 | - } else { |
|
| 276 | - //no matching task |
|
| 277 | - /** |
|
| 278 | - * trigger error so this gets in the error logs. This is important because it happens on a non user request. |
|
| 279 | - */ |
|
| 280 | - trigger_error(esc_attr(sprintf(__('There is no task corresponding to this route %s', 'event_espresso'), |
|
| 281 | - $cron_type))); |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - do_action('FHEE__EED_Messages__run_cron__end'); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * This is used to retrieve the template pack for the given name. |
|
| 291 | - * Retrieved packs are cached on the static $_TMP_PACKS array. If there is no class matching the given name then |
|
| 292 | - * the default template pack is returned. |
|
| 293 | - * |
|
| 294 | - * @deprecated 4.9.0 @see EEH_MSG_Template::get_template_pack() |
|
| 295 | - * @param string $template_pack_name This should correspond to the dbref of the template pack (which is also used |
|
| 296 | - * in generating the Pack class name). |
|
| 297 | - * @return EE_Messages_Template_Pack |
|
| 298 | - */ |
|
| 299 | - public static function get_template_pack($template_pack_name) |
|
| 300 | - { |
|
| 301 | - EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 302 | - return EEH_MSG_Template::get_template_pack($template_pack_name); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Retrieves an array of all template packs. |
|
| 308 | - * Array is in the format array( 'dbref' => EE_Messages_Template_Pack ) |
|
| 309 | - * |
|
| 310 | - * @deprecated 4.9.0 @see EEH_MSG_Template_Pack::get_template_pack_collection |
|
| 311 | - * @return EE_Messages_Template_Pack[] |
|
| 312 | - */ |
|
| 313 | - public static function get_template_packs() |
|
| 314 | - { |
|
| 315 | - EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 316 | - |
|
| 317 | - //for backward compat, let's make sure this returns in the same format as originally. |
|
| 318 | - $template_pack_collection = EEH_MSG_Template::get_template_pack_collection(); |
|
| 319 | - $template_pack_collection->rewind(); |
|
| 320 | - $template_packs = array(); |
|
| 321 | - while ($template_pack_collection->valid()) { |
|
| 322 | - $template_packs[$template_pack_collection->current()->dbref] = $template_pack_collection->current(); |
|
| 323 | - $template_pack_collection->next(); |
|
| 324 | - } |
|
| 325 | - return $template_packs; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * This simply makes sure the autoloaders are registered for the EE_messages system. |
|
| 331 | - * |
|
| 332 | - * @since 4.5.0 |
|
| 333 | - * @return void |
|
| 334 | - */ |
|
| 335 | - public static function set_autoloaders() |
|
| 336 | - { |
|
| 337 | - if (empty(self::$_MSG_PATHS)) { |
|
| 338 | - self::_set_messages_paths(); |
|
| 339 | - foreach (self::$_MSG_PATHS as $path) { |
|
| 340 | - EEH_Autoloader::register_autoloaders_for_each_file_in_folder($path); |
|
| 341 | - } |
|
| 342 | - // add aliases |
|
| 343 | - EEH_Autoloader::add_alias('EE_messages', 'EE_messages'); |
|
| 344 | - EEH_Autoloader::add_alias('EE_messenger', 'EE_messenger'); |
|
| 345 | - } |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Take care of adding all the paths for the messages components to the $_MSG_PATHS property |
|
| 351 | - * for use by the Messages Autoloaders |
|
| 352 | - * |
|
| 353 | - * @since 4.5.0 |
|
| 354 | - * @return void. |
|
| 355 | - */ |
|
| 356 | - protected static function _set_messages_paths() |
|
| 357 | - { |
|
| 358 | - $dir_ref = array( |
|
| 359 | - 'messages/message_type', |
|
| 360 | - 'messages/messenger', |
|
| 361 | - 'messages/defaults', |
|
| 362 | - 'messages/defaults/email', |
|
| 363 | - 'messages/data_class', |
|
| 364 | - 'messages/validators', |
|
| 365 | - 'messages/validators/email', |
|
| 366 | - 'messages/validators/html', |
|
| 367 | - 'shortcodes', |
|
| 368 | - ); |
|
| 369 | - $paths = array(); |
|
| 370 | - foreach ($dir_ref as $index => $dir) { |
|
| 371 | - $paths[$index] = EE_LIBRARIES . $dir; |
|
| 372 | - } |
|
| 373 | - self::$_MSG_PATHS = apply_filters('FHEE__EED_Messages___set_messages_paths___MSG_PATHS', $paths); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Takes care of loading dependencies |
|
| 379 | - * |
|
| 380 | - * @since 4.5.0 |
|
| 381 | - * @return void |
|
| 382 | - */ |
|
| 383 | - protected static function _load_controller() |
|
| 384 | - { |
|
| 385 | - if (! self::$_MSG_PROCESSOR instanceof EE_Messages_Processor) { |
|
| 386 | - EE_Registry::instance()->load_core('Request_Handler'); |
|
| 387 | - self::set_autoloaders(); |
|
| 388 | - self::$_EEMSG = EE_Registry::instance()->load_lib('messages'); |
|
| 389 | - self::$_MSG_PROCESSOR = EE_Registry::instance()->load_lib('Messages_Processor'); |
|
| 390 | - self::$_message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * @param EE_Transaction $transaction |
|
| 397 | - */ |
|
| 398 | - public static function payment_reminder(EE_Transaction $transaction) |
|
| 399 | - { |
|
| 400 | - self::_load_controller(); |
|
| 401 | - $data = array($transaction, null); |
|
| 402 | - self::$_MSG_PROCESSOR->generate_for_all_active_messengers('payment_reminder', $data); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Any messages triggers for after successful gateway payments should go in here. |
|
| 408 | - * |
|
| 409 | - * @param EE_Transaction object |
|
| 410 | - * @param EE_Payment object |
|
| 411 | - * @return void |
|
| 412 | - */ |
|
| 413 | - public static function payment(EE_Transaction $transaction, EE_Payment $payment) |
|
| 414 | - { |
|
| 415 | - self::_load_controller(); |
|
| 416 | - $data = array($transaction, $payment); |
|
| 417 | - EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 418 | - $message_type = EEH_MSG_Template::convert_payment_status_to_message_type($payment->STS_ID()); |
|
| 419 | - //if payment amount is less than 0 then switch to payment_refund message type. |
|
| 420 | - $message_type = $payment->amount() < 0 ? 'payment_refund' : $message_type; |
|
| 421 | - self::$_MSG_PROCESSOR->generate_for_all_active_messengers($message_type, $data); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * @param EE_Transaction $transaction |
|
| 427 | - */ |
|
| 428 | - public static function cancelled_registration(EE_Transaction $transaction) |
|
| 429 | - { |
|
| 430 | - self::_load_controller(); |
|
| 431 | - $data = array($transaction, null); |
|
| 432 | - self::$_MSG_PROCESSOR->generate_for_all_active_messengers('cancelled_registration', $data); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Trigger for Registration messages |
|
| 438 | - * Note that what registration message type is sent depends on what the reg status is for the registrations on the |
|
| 439 | - * incoming transaction. |
|
| 440 | - * |
|
| 441 | - * @param EE_Registration $registration |
|
| 442 | - * @param array $extra_details |
|
| 443 | - * @return void |
|
| 444 | - */ |
|
| 445 | - public static function maybe_registration(EE_Registration $registration, $extra_details = array()) |
|
| 446 | - { |
|
| 447 | - |
|
| 448 | - if (! self::_verify_registration_notification_send($registration, $extra_details)) { |
|
| 449 | - //no messages please |
|
| 450 | - return; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - |
|
| 454 | - //get all registrations so we make sure we send messages for the right status. |
|
| 455 | - $all_registrations = $registration->transaction()->registrations(); |
|
| 456 | - |
|
| 457 | - //cached array of statuses so we only trigger messages once per status. |
|
| 458 | - $statuses_sent = array(); |
|
| 459 | - self::_load_controller(); |
|
| 460 | - $mtgs = array(); |
|
| 461 | - |
|
| 462 | - //loop through registrations and trigger messages once per status. |
|
| 463 | - foreach ($all_registrations as $reg) { |
|
| 464 | - |
|
| 465 | - //already triggered? |
|
| 466 | - if (in_array($reg->status_ID(), $statuses_sent)) { |
|
| 467 | - continue; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - $message_type = EEH_MSG_Template::convert_reg_status_to_message_type($reg->status_ID()); |
|
| 471 | - $mtgs = array_merge( |
|
| 472 | - $mtgs, |
|
| 473 | - self::$_MSG_PROCESSOR->setup_mtgs_for_all_active_messengers( |
|
| 474 | - $message_type, |
|
| 475 | - array($registration->transaction(), null, $reg->status_ID()) |
|
| 476 | - ) |
|
| 477 | - ); |
|
| 478 | - $statuses_sent[] = $reg->status_ID(); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - if (count($statuses_sent) > 1) { |
|
| 482 | - $mtgs = array_merge( |
|
| 483 | - $mtgs, |
|
| 484 | - self::$_MSG_PROCESSOR->setup_mtgs_for_all_active_messengers( |
|
| 485 | - 'registration_summary', |
|
| 486 | - array($registration->transaction(), null) |
|
| 487 | - ) |
|
| 488 | - ); |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - //batch queue and initiate request |
|
| 492 | - self::$_MSG_PROCESSOR->batch_queue_for_generation_and_persist($mtgs); |
|
| 493 | - self::$_MSG_PROCESSOR->get_queue()->initiate_request_by_priority(); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * This is a helper method used to very whether a registration notification should be sent or |
|
| 499 | - * not. Prevents duplicate notifications going out for registration context notifications. |
|
| 500 | - * |
|
| 501 | - * @param EE_Registration $registration [description] |
|
| 502 | - * @param array $extra_details [description] |
|
| 503 | - * @return bool true = send away, false = nope halt the presses. |
|
| 504 | - */ |
|
| 505 | - protected static function _verify_registration_notification_send( |
|
| 506 | - EE_Registration $registration, |
|
| 507 | - $extra_details = array() |
|
| 508 | - ) { |
|
| 509 | - //self::log( |
|
| 510 | - // __CLASS__, __FUNCTION__, __LINE__, |
|
| 511 | - // $registration->transaction(), |
|
| 512 | - // array( '$extra_details' => $extra_details ) |
|
| 513 | - //); |
|
| 514 | - // currently only using this to send messages for the primary registrant |
|
| 515 | - if (! $registration->is_primary_registrant()) { |
|
| 516 | - return false; |
|
| 517 | - } |
|
| 518 | - // first we check if we're in admin and not doing front ajax |
|
| 519 | - if (is_admin() && ! EE_FRONT_AJAX) { |
|
| 520 | - //make sure appropriate admin params are set for sending messages |
|
| 521 | - if (empty($_REQUEST['txn_reg_status_change']['send_notifications']) || ! absint($_REQUEST['txn_reg_status_change']['send_notifications'])) { |
|
| 522 | - //no messages sent please. |
|
| 523 | - return false; |
|
| 524 | - } |
|
| 525 | - } else { |
|
| 526 | - // frontend request (either regular or via AJAX) |
|
| 527 | - // TXN is NOT finalized ? |
|
| 528 | - if (! isset($extra_details['finalized']) || $extra_details['finalized'] === false) { |
|
| 529 | - return false; |
|
| 530 | - } |
|
| 531 | - // return visit but nothing changed ??? |
|
| 532 | - if ( |
|
| 533 | - isset($extra_details['revisit'], $extra_details['status_updates']) && |
|
| 534 | - $extra_details['revisit'] && ! $extra_details['status_updates'] |
|
| 535 | - ) { |
|
| 536 | - return false; |
|
| 537 | - } |
|
| 538 | - // NOT sending messages && reg status is something other than "Not-Approved" |
|
| 539 | - if ( |
|
| 540 | - ! apply_filters('FHEE__EED_Messages___maybe_registration__deliver_notifications', false) && |
|
| 541 | - $registration->status_ID() !== EEM_Registration::status_id_not_approved |
|
| 542 | - ) { |
|
| 543 | - return false; |
|
| 544 | - } |
|
| 545 | - } |
|
| 546 | - // release the kraken |
|
| 547 | - return true; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * Simply returns an array indexed by Registration Status ID and the related message_type name associated with that |
|
| 553 | - * status id. |
|
| 554 | - * |
|
| 555 | - * @deprecated 4.9.0 Use EEH_MSG_Template::reg_status_to_message_type_array() |
|
| 556 | - * or EEH_MSG_Template::convert_reg_status_to_message_type |
|
| 557 | - * @param string $reg_status |
|
| 558 | - * @return array |
|
| 559 | - */ |
|
| 560 | - protected static function _get_reg_status_array($reg_status = '') |
|
| 561 | - { |
|
| 562 | - EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 563 | - return EEH_MSG_Template::convert_reg_status_to_message_type($reg_status) |
|
| 564 | - ? EEH_MSG_Template::convert_reg_status_to_message_type($reg_status) |
|
| 565 | - : EEH_MSG_Template::reg_status_to_message_type_array(); |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Simply returns the payment message type for the given payment status. |
|
| 571 | - * |
|
| 572 | - * @deprecated 4.9.0 Use EEH_MSG_Template::payment_status_to_message_type_array |
|
| 573 | - * or EEH_MSG_Template::convert_payment_status_to_message_type |
|
| 574 | - * @param string $payment_status The payment status being matched. |
|
| 575 | - * @return string|bool The payment message type slug matching the status or false if no match. |
|
| 576 | - */ |
|
| 577 | - protected static function _get_payment_message_type($payment_status) |
|
| 578 | - { |
|
| 579 | - EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 580 | - return EEH_MSG_Template::convert_payment_status_to_message_type($payment_status) |
|
| 581 | - ? EEH_MSG_Template::convert_payment_status_to_message_type($payment_status) |
|
| 582 | - : false; |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * Message triggers for a resending already sent message(s) (via EE_Message list table) |
|
| 588 | - * |
|
| 589 | - * @access public |
|
| 590 | - * @param array $req_data This is the $_POST & $_GET data sent from EE_Admin Pages |
|
| 591 | - * @return bool success/fail |
|
| 592 | - */ |
|
| 593 | - public static function process_resend($req_data) |
|
| 594 | - { |
|
| 595 | - self::_load_controller(); |
|
| 596 | - |
|
| 597 | - //if $msgID in this request then skip to the new resend_message |
|
| 598 | - if (EE_Registry::instance()->REQ->get('MSG_ID')) { |
|
| 599 | - return self::resend_message(); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - //make sure any incoming request data is set on the REQ so that it gets picked up later. |
|
| 603 | - $req_data = (array)$req_data; |
|
| 604 | - foreach ($req_data as $request_key => $request_value) { |
|
| 605 | - EE_Registry::instance()->REQ->set($request_key, $request_value); |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - if (! $messages_to_send = self::$_MSG_PROCESSOR->setup_messages_to_generate_from_registration_ids_in_request()) { |
|
| 609 | - return false; |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - try { |
|
| 613 | - self::$_MSG_PROCESSOR->batch_queue_for_generation_and_persist($messages_to_send); |
|
| 614 | - self::$_MSG_PROCESSOR->get_queue()->initiate_request_by_priority(); |
|
| 615 | - } catch (EE_Error $e) { |
|
| 616 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 617 | - return false; |
|
| 618 | - } |
|
| 619 | - EE_Error::add_success( |
|
| 620 | - __('Messages have been successfully queued for generation and sending.', 'event_espresso') |
|
| 621 | - ); |
|
| 622 | - return true; //everything got queued. |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * Message triggers for a resending already sent message(s) (via EE_Message list table) |
|
| 628 | - * |
|
| 629 | - * @return bool |
|
| 630 | - */ |
|
| 631 | - public static function resend_message() |
|
| 632 | - { |
|
| 633 | - self::_load_controller(); |
|
| 634 | - |
|
| 635 | - $msgID = EE_Registry::instance()->REQ->get('MSG_ID'); |
|
| 636 | - if (! $msgID) { |
|
| 637 | - EE_Error::add_error(__('Something went wrong because there is no "MSG_ID" value in the request', |
|
| 638 | - 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 639 | - return false; |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - self::$_MSG_PROCESSOR->setup_messages_from_ids_and_send((array)$msgID); |
|
| 643 | - |
|
| 644 | - //setup success message. |
|
| 645 | - $count_ready_for_resend = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue(EEM_Message::status_resend); |
|
| 646 | - EE_Error::add_success(sprintf( |
|
| 647 | - _n( |
|
| 648 | - 'There was %d message queued for resending.', |
|
| 649 | - 'There were %d messages queued for resending.', |
|
| 650 | - $count_ready_for_resend, |
|
| 651 | - 'event_espresso' |
|
| 652 | - ), |
|
| 653 | - $count_ready_for_resend |
|
| 654 | - )); |
|
| 655 | - return true; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * Message triggers for manual payment applied by admin |
|
| 661 | - * |
|
| 662 | - * @param EE_Payment $payment EE_payment object |
|
| 663 | - * @return bool success/fail |
|
| 664 | - */ |
|
| 665 | - public static function process_admin_payment(EE_Payment $payment) |
|
| 666 | - { |
|
| 667 | - EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 668 | - //we need to get the transaction object |
|
| 669 | - $transaction = $payment->transaction(); |
|
| 670 | - if ($transaction instanceof EE_Transaction) { |
|
| 671 | - $data = array($transaction, $payment); |
|
| 672 | - $message_type = EEH_MSG_Template::convert_payment_status_to_message_type($payment->STS_ID()); |
|
| 673 | - |
|
| 674 | - //if payment amount is less than 0 then switch to payment_refund message type. |
|
| 675 | - $message_type = $payment->amount() < 0 ? 'payment_refund' : $message_type; |
|
| 676 | - |
|
| 677 | - //if payment_refund is selected, but the status is NOT accepted. Then change message type to false so NO message notification goes out. |
|
| 678 | - $message_type = $message_type == 'payment_refund' && $payment->STS_ID() != EEM_Payment::status_id_approved ? false : $message_type; |
|
| 679 | - |
|
| 680 | - self::_load_controller(); |
|
| 681 | - |
|
| 682 | - self::$_MSG_PROCESSOR->generate_for_all_active_messengers($message_type, $data); |
|
| 683 | - |
|
| 684 | - //get count of queued for generation |
|
| 685 | - $count_to_generate = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue(array( |
|
| 686 | - EEM_Message::status_incomplete, |
|
| 687 | - EEM_Message::status_idle, |
|
| 688 | - )); |
|
| 689 | - |
|
| 690 | - if ($count_to_generate > 0 && self::$_MSG_PROCESSOR->get_queue()->get_message_repository()->count() !== 0) { |
|
| 691 | - add_filter('FHEE__EE_Admin_Page___process_admin_payment_notification__success', '__return_true'); |
|
| 692 | - return true; |
|
| 693 | - } else { |
|
| 694 | - $count_failed = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue(EEM_Message::instance()->stati_indicating_failed_sending()); |
|
| 695 | - /** |
|
| 696 | - * Verify that there are actually errors. If not then we return a success message because the queue might have been emptied due to successful |
|
| 697 | - * IMMEDIATE generation. |
|
| 698 | - */ |
|
| 699 | - if ($count_failed > 0) { |
|
| 700 | - EE_Error::add_error(sprintf( |
|
| 701 | - _n( |
|
| 702 | - 'The payment notification generation failed.', |
|
| 703 | - '%d payment notifications failed being sent.', |
|
| 704 | - $count_failed, |
|
| 705 | - 'event_espresso' |
|
| 706 | - ), |
|
| 707 | - $count_failed |
|
| 708 | - ), __FILE__, __FUNCTION__, __LINE__); |
|
| 709 | - |
|
| 710 | - return false; |
|
| 711 | - } else { |
|
| 712 | - add_filter('FHEE__EE_Admin_Page___process_admin_payment_notification__success', '__return_true'); |
|
| 713 | - return true; |
|
| 714 | - } |
|
| 715 | - } |
|
| 716 | - } else { |
|
| 717 | - EE_Error::add_error( |
|
| 718 | - 'Unable to generate the payment notification because the given value for the transaction is invalid.', |
|
| 719 | - 'event_espresso' |
|
| 720 | - ); |
|
| 721 | - return false; |
|
| 722 | - } |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - |
|
| 726 | - /** |
|
| 727 | - * Callback for AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send_with_registrations trigger |
|
| 728 | - * |
|
| 729 | - * @since 4.3.0 |
|
| 730 | - * @param EE_Registration[] $registrations an array of EE_Registration objects |
|
| 731 | - * @param int $grp_id a specific message template group id. |
|
| 732 | - * @return void |
|
| 733 | - */ |
|
| 734 | - public static function send_newsletter_message($registrations, $grp_id) |
|
| 735 | - { |
|
| 736 | - //make sure mtp is id and set it in the EE_Request Handler later messages setup. |
|
| 737 | - EE_Registry::instance()->REQ->set('GRP_ID', (int)$grp_id); |
|
| 738 | - self::_load_controller(); |
|
| 739 | - self::$_MSG_PROCESSOR->generate_for_all_active_messengers('newsletter', $registrations); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - |
|
| 743 | - /** |
|
| 744 | - * Callback for FHEE__EE_Registration__invoice_url__invoice_url or FHEE__EE_Registration__receipt_url__receipt_url |
|
| 745 | - * |
|
| 746 | - * @since 4.3.0 |
|
| 747 | - * @param string $registration_message_trigger_url |
|
| 748 | - * @param EE_Registration $registration |
|
| 749 | - * @param string $messenger |
|
| 750 | - * @param string $message_type |
|
| 751 | - * @return string |
|
| 752 | - */ |
|
| 753 | - public static function registration_message_trigger_url( |
|
| 754 | - $registration_message_trigger_url, |
|
| 755 | - EE_Registration $registration, |
|
| 756 | - $messenger = 'html', |
|
| 757 | - $message_type = 'invoice' |
|
| 758 | - ) { |
|
| 759 | - // whitelist $messenger |
|
| 760 | - switch ($messenger) { |
|
| 761 | - case 'pdf' : |
|
| 762 | - $sending_messenger = 'pdf'; |
|
| 763 | - $generating_messenger = 'html'; |
|
| 764 | - break; |
|
| 765 | - case 'html' : |
|
| 766 | - default : |
|
| 767 | - $sending_messenger = 'html'; |
|
| 768 | - $generating_messenger = 'html'; |
|
| 769 | - break; |
|
| 770 | - } |
|
| 771 | - // whitelist $message_type |
|
| 772 | - switch ($message_type) { |
|
| 773 | - case 'receipt' : |
|
| 774 | - $message_type = 'receipt'; |
|
| 775 | - break; |
|
| 776 | - case 'invoice' : |
|
| 777 | - default : |
|
| 778 | - $message_type = 'invoice'; |
|
| 779 | - break; |
|
| 780 | - } |
|
| 781 | - // verify that both the messenger AND the message type are active |
|
| 782 | - if (EEH_MSG_Template::is_messenger_active($sending_messenger) && EEH_MSG_Template::is_mt_active($message_type)) { |
|
| 783 | - //need to get the correct message template group for this (i.e. is there a custom invoice for the event this registration is registered for?) |
|
| 784 | - $template_query_params = array( |
|
| 785 | - 'MTP_is_active' => true, |
|
| 786 | - 'MTP_messenger' => $generating_messenger, |
|
| 787 | - 'MTP_message_type' => $message_type, |
|
| 788 | - 'Event.EVT_ID' => $registration->event_ID(), |
|
| 789 | - ); |
|
| 790 | - //get the message template group. |
|
| 791 | - $msg_template_group = EEM_Message_Template_Group::instance()->get_one(array($template_query_params)); |
|
| 792 | - //if we don't have an EE_Message_Template_Group then return |
|
| 793 | - if (! $msg_template_group instanceof EE_Message_Template_Group) { |
|
| 794 | - // remove EVT_ID from query params so that global templates get picked up |
|
| 795 | - unset($template_query_params['Event.EVT_ID']); |
|
| 796 | - //get global template as the fallback |
|
| 797 | - $msg_template_group = EEM_Message_Template_Group::instance()->get_one(array($template_query_params)); |
|
| 798 | - } |
|
| 799 | - //if we don't have an EE_Message_Template_Group then return |
|
| 800 | - if (! $msg_template_group instanceof EE_Message_Template_Group) { |
|
| 801 | - return ''; |
|
| 802 | - } |
|
| 803 | - // generate the URL |
|
| 804 | - $registration_message_trigger_url = EEH_MSG_Template::generate_url_trigger( |
|
| 805 | - $sending_messenger, |
|
| 806 | - $generating_messenger, |
|
| 807 | - 'purchaser', |
|
| 808 | - $message_type, |
|
| 809 | - $registration, |
|
| 810 | - $msg_template_group->ID(), |
|
| 811 | - $registration->transaction_ID() |
|
| 812 | - ); |
|
| 813 | - |
|
| 814 | - } |
|
| 815 | - return $registration_message_trigger_url; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - |
|
| 819 | - /** |
|
| 820 | - * Use to generate and return a message preview! |
|
| 821 | - * |
|
| 822 | - * @param string $type This should correspond with a valid message type |
|
| 823 | - * @param string $context This should correspond with a valid context for the message type |
|
| 824 | - * @param string $messenger This should correspond with a valid messenger. |
|
| 825 | - * @param bool $send true we will do a test send using the messenger delivery, false we just do a regular |
|
| 826 | - * preview |
|
| 827 | - * @return bool|string The body of the message or if send is requested, sends. |
|
| 828 | - * @throws EE_Error |
|
| 829 | - */ |
|
| 830 | - public static function preview_message($type, $context, $messenger, $send = false) |
|
| 831 | - { |
|
| 832 | - self::_load_controller(); |
|
| 833 | - $mtg = new EE_Message_To_Generate( |
|
| 834 | - $messenger, |
|
| 835 | - $type, |
|
| 836 | - array(), |
|
| 837 | - $context, |
|
| 838 | - true |
|
| 839 | - ); |
|
| 840 | - $generated_preview_queue = self::$_MSG_PROCESSOR->generate_for_preview($mtg, $send); |
|
| 841 | - if ($generated_preview_queue instanceof EE_Messages_Queue) { |
|
| 842 | - //loop through all content for the preview and remove any persisted records. |
|
| 843 | - $content = ''; |
|
| 844 | - foreach ($generated_preview_queue->get_message_repository() as $message) { |
|
| 845 | - $content = $message->content(); |
|
| 846 | - if ($message->ID() > 0 && $message->STS_ID() !== EEM_Message::status_failed) { |
|
| 847 | - $message->delete(); |
|
| 848 | - } |
|
| 849 | - } |
|
| 850 | - return $content; |
|
| 851 | - } else { |
|
| 852 | - return $generated_preview_queue; |
|
| 853 | - } |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - |
|
| 857 | - /** |
|
| 858 | - * This is a method that allows for sending a message using a messenger matching the string given and the provided |
|
| 859 | - * EE_Message_Queue object. The EE_Message_Queue object is used to create a single aggregate EE_Message via the |
|
| 860 | - * content found in the EE_Message objects in the queue. |
|
| 861 | - * |
|
| 862 | - * @since 4.9.0 |
|
| 863 | - * @param string $messenger a string matching a valid active messenger in the system |
|
| 864 | - * @param string $message_type Although it seems contrary to the name of the method, a message |
|
| 865 | - * type name is still required to send along the message type to the |
|
| 866 | - * messenger because this is used for determining what specific |
|
| 867 | - * variations might be loaded for the generated message. |
|
| 868 | - * @param EE_Messages_Queue $queue |
|
| 869 | - * @param string $custom_subject Can be used to set what the custom subject string will be on the |
|
| 870 | - * aggregate EE_Message object. |
|
| 871 | - * @return bool success or fail. |
|
| 872 | - */ |
|
| 873 | - public static function send_message_with_messenger_only( |
|
| 874 | - $messenger, |
|
| 875 | - $message_type, |
|
| 876 | - EE_Messages_Queue $queue, |
|
| 877 | - $custom_subject = '' |
|
| 878 | - ) { |
|
| 879 | - self::_load_controller(); |
|
| 880 | - /** @type EE_Message_To_Generate_From_Queue $message_to_generate */ |
|
| 881 | - $message_to_generate = EE_Registry::instance()->load_lib( |
|
| 882 | - 'Message_To_Generate_From_Queue', |
|
| 883 | - array( |
|
| 884 | - $messenger, |
|
| 885 | - $message_type, |
|
| 886 | - $queue, |
|
| 887 | - $custom_subject, |
|
| 888 | - ) |
|
| 889 | - ); |
|
| 890 | - return self::$_MSG_PROCESSOR->queue_for_sending($message_to_generate); |
|
| 891 | - } |
|
| 892 | - |
|
| 893 | - |
|
| 894 | - /** |
|
| 895 | - * Generates Messages immediately for EE_Message IDs (but only for the correct status for generation) |
|
| 896 | - * |
|
| 897 | - * @since 4.9.0 |
|
| 898 | - * @param array $message_ids An array of message ids |
|
| 899 | - * @return bool | EE_Messages_Queue false if nothing was generated, EE_Messages_Queue containing generated |
|
| 900 | - * messages. |
|
| 901 | - */ |
|
| 902 | - public static function generate_now($message_ids) |
|
| 903 | - { |
|
| 904 | - self::_load_controller(); |
|
| 905 | - $messages = EEM_Message::instance()->get_all( |
|
| 906 | - array( |
|
| 907 | - 0 => array( |
|
| 908 | - 'MSG_ID' => array('IN', $message_ids), |
|
| 909 | - 'STS_ID' => EEM_Message::status_incomplete, |
|
| 910 | - ), |
|
| 911 | - ) |
|
| 912 | - ); |
|
| 913 | - $generated_queue = false; |
|
| 914 | - if ($messages) { |
|
| 915 | - $generated_queue = self::$_MSG_PROCESSOR->batch_generate_from_queue($messages); |
|
| 916 | - } |
|
| 917 | - |
|
| 918 | - if (! $generated_queue instanceof EE_Messages_Queue) { |
|
| 919 | - EE_Error::add_error( |
|
| 920 | - __('The messages were not generated. This could mean there is already a batch being generated on a separate request, or because the selected messages are not ready for generation. Please wait a minute or two and try again.', |
|
| 921 | - 'event_espresso'), |
|
| 922 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 923 | - ); |
|
| 924 | - } |
|
| 925 | - return $generated_queue; |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * Sends messages immediately for the incoming message_ids that have the status of EEM_Message::status_resend or, |
|
| 931 | - * EEM_Message::status_idle |
|
| 932 | - * |
|
| 933 | - * @since 4.9.0 |
|
| 934 | - * @param $message_ids |
|
| 935 | - * @return bool | EE_Messages_Queue false if no messages sent. |
|
| 936 | - */ |
|
| 937 | - public static function send_now($message_ids) |
|
| 938 | - { |
|
| 939 | - self::_load_controller(); |
|
| 940 | - $messages = EEM_Message::instance()->get_all( |
|
| 941 | - array( |
|
| 942 | - 0 => array( |
|
| 943 | - 'MSG_ID' => array('IN', $message_ids), |
|
| 944 | - 'STS_ID' => array( |
|
| 945 | - 'IN', |
|
| 946 | - array(EEM_Message::status_idle, EEM_Message::status_resend, EEM_Message::status_retry), |
|
| 947 | - ), |
|
| 948 | - ), |
|
| 949 | - ) |
|
| 950 | - ); |
|
| 951 | - $sent_queue = false; |
|
| 952 | - if ($messages) { |
|
| 953 | - $sent_queue = self::$_MSG_PROCESSOR->batch_send_from_queue($messages); |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - if (! $sent_queue instanceof EE_Messages_Queue) { |
|
| 957 | - EE_Error::add_error( |
|
| 958 | - __('The messages were not sent. This could mean there is already a batch being sent on a separate request, or because the selected messages are not sendable. Please wait a minute or two and try again.', |
|
| 959 | - 'event_espresso'), |
|
| 960 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 961 | - ); |
|
| 962 | - } else { |
|
| 963 | - //can count how many sent by using the messages in the queue |
|
| 964 | - $sent_count = $sent_queue->count_STS_in_queue(EEM_Message::instance()->stati_indicating_sent()); |
|
| 965 | - if ($sent_count > 0) { |
|
| 966 | - EE_Error::add_success( |
|
| 967 | - sprintf( |
|
| 968 | - _n( |
|
| 969 | - 'There was %d message successfully sent.', |
|
| 970 | - 'There were %d messages successfully sent.', |
|
| 971 | - $sent_count, |
|
| 972 | - 'event_espresso' |
|
| 973 | - ), |
|
| 974 | - $sent_count |
|
| 975 | - ) |
|
| 976 | - ); |
|
| 977 | - } else { |
|
| 978 | - EE_Error::overwrite_errors(); |
|
| 979 | - EE_Error::add_error( |
|
| 980 | - __('No message was sent because of problems with sending. Either all the messages you selected were not a sendable message, they were ALREADY sent on a different scheduled task, or there was an error. |
|
| 201 | + exit; |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + return; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * This runs when the msg_url_trigger route has initiated. |
|
| 210 | + * |
|
| 211 | + * @since 4.5.0 |
|
| 212 | + * @param WP $WP |
|
| 213 | + * @throws EE_Error |
|
| 214 | + * @return void |
|
| 215 | + */ |
|
| 216 | + public function run($WP) |
|
| 217 | + { |
|
| 218 | + //ensure controller is loaded |
|
| 219 | + self::_load_controller(); |
|
| 220 | + // attempt to process message |
|
| 221 | + try { |
|
| 222 | + /** @type EE_Message_To_Generate_From_Request $message_to_generate */ |
|
| 223 | + $message_to_generate = EE_Registry::instance()->load_lib('Message_To_Generate_From_Request'); |
|
| 224 | + self::$_MSG_PROCESSOR->generate_and_send_now($message_to_generate); |
|
| 225 | + } catch (EE_Error $e) { |
|
| 226 | + $error_msg = __('Please note that a system message failed to send due to a technical issue.', |
|
| 227 | + 'event_espresso'); |
|
| 228 | + // add specific message for developers if WP_DEBUG in on |
|
| 229 | + $error_msg .= '||' . $e->getMessage(); |
|
| 230 | + EE_Error::add_error($error_msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * This is triggered by the 'msg_cron_trigger' route. |
|
| 237 | + * |
|
| 238 | + * @param WP $WP |
|
| 239 | + */ |
|
| 240 | + public function execute_batch_request($WP) |
|
| 241 | + { |
|
| 242 | + $this->run_cron(); |
|
| 243 | + header('HTTP/1.1 200 OK'); |
|
| 244 | + exit(); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * This gets executed on wp_cron jobs or when a batch request is initiated on its own separate non regular wp |
|
| 250 | + * request. |
|
| 251 | + */ |
|
| 252 | + public function run_cron() |
|
| 253 | + { |
|
| 254 | + self::_load_controller(); |
|
| 255 | + //get required vars |
|
| 256 | + $cron_type = EE_Registry::instance()->REQ->get('type'); |
|
| 257 | + $transient_key = EE_Registry::instance()->REQ->get('key'); |
|
| 258 | + |
|
| 259 | + //now let's verify transient, if not valid exit immediately |
|
| 260 | + if (! get_transient($transient_key)) { |
|
| 261 | + /** |
|
| 262 | + * trigger error so this gets in the error logs. This is important because it happens on a non-user request. |
|
| 263 | + */ |
|
| 264 | + trigger_error(esc_attr__('Invalid Request (Transient does not exist)', 'event_espresso')); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + //if made it here, lets' delete the transient to keep the db clean |
|
| 268 | + delete_transient($transient_key); |
|
| 269 | + |
|
| 270 | + if (apply_filters('FHEE__EED_Messages__run_cron__use_wp_cron', true)) { |
|
| 271 | + |
|
| 272 | + $method = 'batch_' . $cron_type . '_from_queue'; |
|
| 273 | + if (method_exists(self::$_MSG_PROCESSOR, $method)) { |
|
| 274 | + self::$_MSG_PROCESSOR->$method(); |
|
| 275 | + } else { |
|
| 276 | + //no matching task |
|
| 277 | + /** |
|
| 278 | + * trigger error so this gets in the error logs. This is important because it happens on a non user request. |
|
| 279 | + */ |
|
| 280 | + trigger_error(esc_attr(sprintf(__('There is no task corresponding to this route %s', 'event_espresso'), |
|
| 281 | + $cron_type))); |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + do_action('FHEE__EED_Messages__run_cron__end'); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * This is used to retrieve the template pack for the given name. |
|
| 291 | + * Retrieved packs are cached on the static $_TMP_PACKS array. If there is no class matching the given name then |
|
| 292 | + * the default template pack is returned. |
|
| 293 | + * |
|
| 294 | + * @deprecated 4.9.0 @see EEH_MSG_Template::get_template_pack() |
|
| 295 | + * @param string $template_pack_name This should correspond to the dbref of the template pack (which is also used |
|
| 296 | + * in generating the Pack class name). |
|
| 297 | + * @return EE_Messages_Template_Pack |
|
| 298 | + */ |
|
| 299 | + public static function get_template_pack($template_pack_name) |
|
| 300 | + { |
|
| 301 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 302 | + return EEH_MSG_Template::get_template_pack($template_pack_name); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Retrieves an array of all template packs. |
|
| 308 | + * Array is in the format array( 'dbref' => EE_Messages_Template_Pack ) |
|
| 309 | + * |
|
| 310 | + * @deprecated 4.9.0 @see EEH_MSG_Template_Pack::get_template_pack_collection |
|
| 311 | + * @return EE_Messages_Template_Pack[] |
|
| 312 | + */ |
|
| 313 | + public static function get_template_packs() |
|
| 314 | + { |
|
| 315 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 316 | + |
|
| 317 | + //for backward compat, let's make sure this returns in the same format as originally. |
|
| 318 | + $template_pack_collection = EEH_MSG_Template::get_template_pack_collection(); |
|
| 319 | + $template_pack_collection->rewind(); |
|
| 320 | + $template_packs = array(); |
|
| 321 | + while ($template_pack_collection->valid()) { |
|
| 322 | + $template_packs[$template_pack_collection->current()->dbref] = $template_pack_collection->current(); |
|
| 323 | + $template_pack_collection->next(); |
|
| 324 | + } |
|
| 325 | + return $template_packs; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * This simply makes sure the autoloaders are registered for the EE_messages system. |
|
| 331 | + * |
|
| 332 | + * @since 4.5.0 |
|
| 333 | + * @return void |
|
| 334 | + */ |
|
| 335 | + public static function set_autoloaders() |
|
| 336 | + { |
|
| 337 | + if (empty(self::$_MSG_PATHS)) { |
|
| 338 | + self::_set_messages_paths(); |
|
| 339 | + foreach (self::$_MSG_PATHS as $path) { |
|
| 340 | + EEH_Autoloader::register_autoloaders_for_each_file_in_folder($path); |
|
| 341 | + } |
|
| 342 | + // add aliases |
|
| 343 | + EEH_Autoloader::add_alias('EE_messages', 'EE_messages'); |
|
| 344 | + EEH_Autoloader::add_alias('EE_messenger', 'EE_messenger'); |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Take care of adding all the paths for the messages components to the $_MSG_PATHS property |
|
| 351 | + * for use by the Messages Autoloaders |
|
| 352 | + * |
|
| 353 | + * @since 4.5.0 |
|
| 354 | + * @return void. |
|
| 355 | + */ |
|
| 356 | + protected static function _set_messages_paths() |
|
| 357 | + { |
|
| 358 | + $dir_ref = array( |
|
| 359 | + 'messages/message_type', |
|
| 360 | + 'messages/messenger', |
|
| 361 | + 'messages/defaults', |
|
| 362 | + 'messages/defaults/email', |
|
| 363 | + 'messages/data_class', |
|
| 364 | + 'messages/validators', |
|
| 365 | + 'messages/validators/email', |
|
| 366 | + 'messages/validators/html', |
|
| 367 | + 'shortcodes', |
|
| 368 | + ); |
|
| 369 | + $paths = array(); |
|
| 370 | + foreach ($dir_ref as $index => $dir) { |
|
| 371 | + $paths[$index] = EE_LIBRARIES . $dir; |
|
| 372 | + } |
|
| 373 | + self::$_MSG_PATHS = apply_filters('FHEE__EED_Messages___set_messages_paths___MSG_PATHS', $paths); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Takes care of loading dependencies |
|
| 379 | + * |
|
| 380 | + * @since 4.5.0 |
|
| 381 | + * @return void |
|
| 382 | + */ |
|
| 383 | + protected static function _load_controller() |
|
| 384 | + { |
|
| 385 | + if (! self::$_MSG_PROCESSOR instanceof EE_Messages_Processor) { |
|
| 386 | + EE_Registry::instance()->load_core('Request_Handler'); |
|
| 387 | + self::set_autoloaders(); |
|
| 388 | + self::$_EEMSG = EE_Registry::instance()->load_lib('messages'); |
|
| 389 | + self::$_MSG_PROCESSOR = EE_Registry::instance()->load_lib('Messages_Processor'); |
|
| 390 | + self::$_message_resource_manager = EE_Registry::instance()->load_lib('Message_Resource_Manager'); |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * @param EE_Transaction $transaction |
|
| 397 | + */ |
|
| 398 | + public static function payment_reminder(EE_Transaction $transaction) |
|
| 399 | + { |
|
| 400 | + self::_load_controller(); |
|
| 401 | + $data = array($transaction, null); |
|
| 402 | + self::$_MSG_PROCESSOR->generate_for_all_active_messengers('payment_reminder', $data); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Any messages triggers for after successful gateway payments should go in here. |
|
| 408 | + * |
|
| 409 | + * @param EE_Transaction object |
|
| 410 | + * @param EE_Payment object |
|
| 411 | + * @return void |
|
| 412 | + */ |
|
| 413 | + public static function payment(EE_Transaction $transaction, EE_Payment $payment) |
|
| 414 | + { |
|
| 415 | + self::_load_controller(); |
|
| 416 | + $data = array($transaction, $payment); |
|
| 417 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 418 | + $message_type = EEH_MSG_Template::convert_payment_status_to_message_type($payment->STS_ID()); |
|
| 419 | + //if payment amount is less than 0 then switch to payment_refund message type. |
|
| 420 | + $message_type = $payment->amount() < 0 ? 'payment_refund' : $message_type; |
|
| 421 | + self::$_MSG_PROCESSOR->generate_for_all_active_messengers($message_type, $data); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * @param EE_Transaction $transaction |
|
| 427 | + */ |
|
| 428 | + public static function cancelled_registration(EE_Transaction $transaction) |
|
| 429 | + { |
|
| 430 | + self::_load_controller(); |
|
| 431 | + $data = array($transaction, null); |
|
| 432 | + self::$_MSG_PROCESSOR->generate_for_all_active_messengers('cancelled_registration', $data); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Trigger for Registration messages |
|
| 438 | + * Note that what registration message type is sent depends on what the reg status is for the registrations on the |
|
| 439 | + * incoming transaction. |
|
| 440 | + * |
|
| 441 | + * @param EE_Registration $registration |
|
| 442 | + * @param array $extra_details |
|
| 443 | + * @return void |
|
| 444 | + */ |
|
| 445 | + public static function maybe_registration(EE_Registration $registration, $extra_details = array()) |
|
| 446 | + { |
|
| 447 | + |
|
| 448 | + if (! self::_verify_registration_notification_send($registration, $extra_details)) { |
|
| 449 | + //no messages please |
|
| 450 | + return; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + |
|
| 454 | + //get all registrations so we make sure we send messages for the right status. |
|
| 455 | + $all_registrations = $registration->transaction()->registrations(); |
|
| 456 | + |
|
| 457 | + //cached array of statuses so we only trigger messages once per status. |
|
| 458 | + $statuses_sent = array(); |
|
| 459 | + self::_load_controller(); |
|
| 460 | + $mtgs = array(); |
|
| 461 | + |
|
| 462 | + //loop through registrations and trigger messages once per status. |
|
| 463 | + foreach ($all_registrations as $reg) { |
|
| 464 | + |
|
| 465 | + //already triggered? |
|
| 466 | + if (in_array($reg->status_ID(), $statuses_sent)) { |
|
| 467 | + continue; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + $message_type = EEH_MSG_Template::convert_reg_status_to_message_type($reg->status_ID()); |
|
| 471 | + $mtgs = array_merge( |
|
| 472 | + $mtgs, |
|
| 473 | + self::$_MSG_PROCESSOR->setup_mtgs_for_all_active_messengers( |
|
| 474 | + $message_type, |
|
| 475 | + array($registration->transaction(), null, $reg->status_ID()) |
|
| 476 | + ) |
|
| 477 | + ); |
|
| 478 | + $statuses_sent[] = $reg->status_ID(); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + if (count($statuses_sent) > 1) { |
|
| 482 | + $mtgs = array_merge( |
|
| 483 | + $mtgs, |
|
| 484 | + self::$_MSG_PROCESSOR->setup_mtgs_for_all_active_messengers( |
|
| 485 | + 'registration_summary', |
|
| 486 | + array($registration->transaction(), null) |
|
| 487 | + ) |
|
| 488 | + ); |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + //batch queue and initiate request |
|
| 492 | + self::$_MSG_PROCESSOR->batch_queue_for_generation_and_persist($mtgs); |
|
| 493 | + self::$_MSG_PROCESSOR->get_queue()->initiate_request_by_priority(); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * This is a helper method used to very whether a registration notification should be sent or |
|
| 499 | + * not. Prevents duplicate notifications going out for registration context notifications. |
|
| 500 | + * |
|
| 501 | + * @param EE_Registration $registration [description] |
|
| 502 | + * @param array $extra_details [description] |
|
| 503 | + * @return bool true = send away, false = nope halt the presses. |
|
| 504 | + */ |
|
| 505 | + protected static function _verify_registration_notification_send( |
|
| 506 | + EE_Registration $registration, |
|
| 507 | + $extra_details = array() |
|
| 508 | + ) { |
|
| 509 | + //self::log( |
|
| 510 | + // __CLASS__, __FUNCTION__, __LINE__, |
|
| 511 | + // $registration->transaction(), |
|
| 512 | + // array( '$extra_details' => $extra_details ) |
|
| 513 | + //); |
|
| 514 | + // currently only using this to send messages for the primary registrant |
|
| 515 | + if (! $registration->is_primary_registrant()) { |
|
| 516 | + return false; |
|
| 517 | + } |
|
| 518 | + // first we check if we're in admin and not doing front ajax |
|
| 519 | + if (is_admin() && ! EE_FRONT_AJAX) { |
|
| 520 | + //make sure appropriate admin params are set for sending messages |
|
| 521 | + if (empty($_REQUEST['txn_reg_status_change']['send_notifications']) || ! absint($_REQUEST['txn_reg_status_change']['send_notifications'])) { |
|
| 522 | + //no messages sent please. |
|
| 523 | + return false; |
|
| 524 | + } |
|
| 525 | + } else { |
|
| 526 | + // frontend request (either regular or via AJAX) |
|
| 527 | + // TXN is NOT finalized ? |
|
| 528 | + if (! isset($extra_details['finalized']) || $extra_details['finalized'] === false) { |
|
| 529 | + return false; |
|
| 530 | + } |
|
| 531 | + // return visit but nothing changed ??? |
|
| 532 | + if ( |
|
| 533 | + isset($extra_details['revisit'], $extra_details['status_updates']) && |
|
| 534 | + $extra_details['revisit'] && ! $extra_details['status_updates'] |
|
| 535 | + ) { |
|
| 536 | + return false; |
|
| 537 | + } |
|
| 538 | + // NOT sending messages && reg status is something other than "Not-Approved" |
|
| 539 | + if ( |
|
| 540 | + ! apply_filters('FHEE__EED_Messages___maybe_registration__deliver_notifications', false) && |
|
| 541 | + $registration->status_ID() !== EEM_Registration::status_id_not_approved |
|
| 542 | + ) { |
|
| 543 | + return false; |
|
| 544 | + } |
|
| 545 | + } |
|
| 546 | + // release the kraken |
|
| 547 | + return true; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * Simply returns an array indexed by Registration Status ID and the related message_type name associated with that |
|
| 553 | + * status id. |
|
| 554 | + * |
|
| 555 | + * @deprecated 4.9.0 Use EEH_MSG_Template::reg_status_to_message_type_array() |
|
| 556 | + * or EEH_MSG_Template::convert_reg_status_to_message_type |
|
| 557 | + * @param string $reg_status |
|
| 558 | + * @return array |
|
| 559 | + */ |
|
| 560 | + protected static function _get_reg_status_array($reg_status = '') |
|
| 561 | + { |
|
| 562 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 563 | + return EEH_MSG_Template::convert_reg_status_to_message_type($reg_status) |
|
| 564 | + ? EEH_MSG_Template::convert_reg_status_to_message_type($reg_status) |
|
| 565 | + : EEH_MSG_Template::reg_status_to_message_type_array(); |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Simply returns the payment message type for the given payment status. |
|
| 571 | + * |
|
| 572 | + * @deprecated 4.9.0 Use EEH_MSG_Template::payment_status_to_message_type_array |
|
| 573 | + * or EEH_MSG_Template::convert_payment_status_to_message_type |
|
| 574 | + * @param string $payment_status The payment status being matched. |
|
| 575 | + * @return string|bool The payment message type slug matching the status or false if no match. |
|
| 576 | + */ |
|
| 577 | + protected static function _get_payment_message_type($payment_status) |
|
| 578 | + { |
|
| 579 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 580 | + return EEH_MSG_Template::convert_payment_status_to_message_type($payment_status) |
|
| 581 | + ? EEH_MSG_Template::convert_payment_status_to_message_type($payment_status) |
|
| 582 | + : false; |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Message triggers for a resending already sent message(s) (via EE_Message list table) |
|
| 588 | + * |
|
| 589 | + * @access public |
|
| 590 | + * @param array $req_data This is the $_POST & $_GET data sent from EE_Admin Pages |
|
| 591 | + * @return bool success/fail |
|
| 592 | + */ |
|
| 593 | + public static function process_resend($req_data) |
|
| 594 | + { |
|
| 595 | + self::_load_controller(); |
|
| 596 | + |
|
| 597 | + //if $msgID in this request then skip to the new resend_message |
|
| 598 | + if (EE_Registry::instance()->REQ->get('MSG_ID')) { |
|
| 599 | + return self::resend_message(); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + //make sure any incoming request data is set on the REQ so that it gets picked up later. |
|
| 603 | + $req_data = (array)$req_data; |
|
| 604 | + foreach ($req_data as $request_key => $request_value) { |
|
| 605 | + EE_Registry::instance()->REQ->set($request_key, $request_value); |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + if (! $messages_to_send = self::$_MSG_PROCESSOR->setup_messages_to_generate_from_registration_ids_in_request()) { |
|
| 609 | + return false; |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + try { |
|
| 613 | + self::$_MSG_PROCESSOR->batch_queue_for_generation_and_persist($messages_to_send); |
|
| 614 | + self::$_MSG_PROCESSOR->get_queue()->initiate_request_by_priority(); |
|
| 615 | + } catch (EE_Error $e) { |
|
| 616 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 617 | + return false; |
|
| 618 | + } |
|
| 619 | + EE_Error::add_success( |
|
| 620 | + __('Messages have been successfully queued for generation and sending.', 'event_espresso') |
|
| 621 | + ); |
|
| 622 | + return true; //everything got queued. |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * Message triggers for a resending already sent message(s) (via EE_Message list table) |
|
| 628 | + * |
|
| 629 | + * @return bool |
|
| 630 | + */ |
|
| 631 | + public static function resend_message() |
|
| 632 | + { |
|
| 633 | + self::_load_controller(); |
|
| 634 | + |
|
| 635 | + $msgID = EE_Registry::instance()->REQ->get('MSG_ID'); |
|
| 636 | + if (! $msgID) { |
|
| 637 | + EE_Error::add_error(__('Something went wrong because there is no "MSG_ID" value in the request', |
|
| 638 | + 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 639 | + return false; |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + self::$_MSG_PROCESSOR->setup_messages_from_ids_and_send((array)$msgID); |
|
| 643 | + |
|
| 644 | + //setup success message. |
|
| 645 | + $count_ready_for_resend = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue(EEM_Message::status_resend); |
|
| 646 | + EE_Error::add_success(sprintf( |
|
| 647 | + _n( |
|
| 648 | + 'There was %d message queued for resending.', |
|
| 649 | + 'There were %d messages queued for resending.', |
|
| 650 | + $count_ready_for_resend, |
|
| 651 | + 'event_espresso' |
|
| 652 | + ), |
|
| 653 | + $count_ready_for_resend |
|
| 654 | + )); |
|
| 655 | + return true; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Message triggers for manual payment applied by admin |
|
| 661 | + * |
|
| 662 | + * @param EE_Payment $payment EE_payment object |
|
| 663 | + * @return bool success/fail |
|
| 664 | + */ |
|
| 665 | + public static function process_admin_payment(EE_Payment $payment) |
|
| 666 | + { |
|
| 667 | + EE_Registry::instance()->load_helper('MSG_Template'); |
|
| 668 | + //we need to get the transaction object |
|
| 669 | + $transaction = $payment->transaction(); |
|
| 670 | + if ($transaction instanceof EE_Transaction) { |
|
| 671 | + $data = array($transaction, $payment); |
|
| 672 | + $message_type = EEH_MSG_Template::convert_payment_status_to_message_type($payment->STS_ID()); |
|
| 673 | + |
|
| 674 | + //if payment amount is less than 0 then switch to payment_refund message type. |
|
| 675 | + $message_type = $payment->amount() < 0 ? 'payment_refund' : $message_type; |
|
| 676 | + |
|
| 677 | + //if payment_refund is selected, but the status is NOT accepted. Then change message type to false so NO message notification goes out. |
|
| 678 | + $message_type = $message_type == 'payment_refund' && $payment->STS_ID() != EEM_Payment::status_id_approved ? false : $message_type; |
|
| 679 | + |
|
| 680 | + self::_load_controller(); |
|
| 681 | + |
|
| 682 | + self::$_MSG_PROCESSOR->generate_for_all_active_messengers($message_type, $data); |
|
| 683 | + |
|
| 684 | + //get count of queued for generation |
|
| 685 | + $count_to_generate = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue(array( |
|
| 686 | + EEM_Message::status_incomplete, |
|
| 687 | + EEM_Message::status_idle, |
|
| 688 | + )); |
|
| 689 | + |
|
| 690 | + if ($count_to_generate > 0 && self::$_MSG_PROCESSOR->get_queue()->get_message_repository()->count() !== 0) { |
|
| 691 | + add_filter('FHEE__EE_Admin_Page___process_admin_payment_notification__success', '__return_true'); |
|
| 692 | + return true; |
|
| 693 | + } else { |
|
| 694 | + $count_failed = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue(EEM_Message::instance()->stati_indicating_failed_sending()); |
|
| 695 | + /** |
|
| 696 | + * Verify that there are actually errors. If not then we return a success message because the queue might have been emptied due to successful |
|
| 697 | + * IMMEDIATE generation. |
|
| 698 | + */ |
|
| 699 | + if ($count_failed > 0) { |
|
| 700 | + EE_Error::add_error(sprintf( |
|
| 701 | + _n( |
|
| 702 | + 'The payment notification generation failed.', |
|
| 703 | + '%d payment notifications failed being sent.', |
|
| 704 | + $count_failed, |
|
| 705 | + 'event_espresso' |
|
| 706 | + ), |
|
| 707 | + $count_failed |
|
| 708 | + ), __FILE__, __FUNCTION__, __LINE__); |
|
| 709 | + |
|
| 710 | + return false; |
|
| 711 | + } else { |
|
| 712 | + add_filter('FHEE__EE_Admin_Page___process_admin_payment_notification__success', '__return_true'); |
|
| 713 | + return true; |
|
| 714 | + } |
|
| 715 | + } |
|
| 716 | + } else { |
|
| 717 | + EE_Error::add_error( |
|
| 718 | + 'Unable to generate the payment notification because the given value for the transaction is invalid.', |
|
| 719 | + 'event_espresso' |
|
| 720 | + ); |
|
| 721 | + return false; |
|
| 722 | + } |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + |
|
| 726 | + /** |
|
| 727 | + * Callback for AHEE__Extend_Registrations_Admin_Page___newsletter_selected_send_with_registrations trigger |
|
| 728 | + * |
|
| 729 | + * @since 4.3.0 |
|
| 730 | + * @param EE_Registration[] $registrations an array of EE_Registration objects |
|
| 731 | + * @param int $grp_id a specific message template group id. |
|
| 732 | + * @return void |
|
| 733 | + */ |
|
| 734 | + public static function send_newsletter_message($registrations, $grp_id) |
|
| 735 | + { |
|
| 736 | + //make sure mtp is id and set it in the EE_Request Handler later messages setup. |
|
| 737 | + EE_Registry::instance()->REQ->set('GRP_ID', (int)$grp_id); |
|
| 738 | + self::_load_controller(); |
|
| 739 | + self::$_MSG_PROCESSOR->generate_for_all_active_messengers('newsletter', $registrations); |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + |
|
| 743 | + /** |
|
| 744 | + * Callback for FHEE__EE_Registration__invoice_url__invoice_url or FHEE__EE_Registration__receipt_url__receipt_url |
|
| 745 | + * |
|
| 746 | + * @since 4.3.0 |
|
| 747 | + * @param string $registration_message_trigger_url |
|
| 748 | + * @param EE_Registration $registration |
|
| 749 | + * @param string $messenger |
|
| 750 | + * @param string $message_type |
|
| 751 | + * @return string |
|
| 752 | + */ |
|
| 753 | + public static function registration_message_trigger_url( |
|
| 754 | + $registration_message_trigger_url, |
|
| 755 | + EE_Registration $registration, |
|
| 756 | + $messenger = 'html', |
|
| 757 | + $message_type = 'invoice' |
|
| 758 | + ) { |
|
| 759 | + // whitelist $messenger |
|
| 760 | + switch ($messenger) { |
|
| 761 | + case 'pdf' : |
|
| 762 | + $sending_messenger = 'pdf'; |
|
| 763 | + $generating_messenger = 'html'; |
|
| 764 | + break; |
|
| 765 | + case 'html' : |
|
| 766 | + default : |
|
| 767 | + $sending_messenger = 'html'; |
|
| 768 | + $generating_messenger = 'html'; |
|
| 769 | + break; |
|
| 770 | + } |
|
| 771 | + // whitelist $message_type |
|
| 772 | + switch ($message_type) { |
|
| 773 | + case 'receipt' : |
|
| 774 | + $message_type = 'receipt'; |
|
| 775 | + break; |
|
| 776 | + case 'invoice' : |
|
| 777 | + default : |
|
| 778 | + $message_type = 'invoice'; |
|
| 779 | + break; |
|
| 780 | + } |
|
| 781 | + // verify that both the messenger AND the message type are active |
|
| 782 | + if (EEH_MSG_Template::is_messenger_active($sending_messenger) && EEH_MSG_Template::is_mt_active($message_type)) { |
|
| 783 | + //need to get the correct message template group for this (i.e. is there a custom invoice for the event this registration is registered for?) |
|
| 784 | + $template_query_params = array( |
|
| 785 | + 'MTP_is_active' => true, |
|
| 786 | + 'MTP_messenger' => $generating_messenger, |
|
| 787 | + 'MTP_message_type' => $message_type, |
|
| 788 | + 'Event.EVT_ID' => $registration->event_ID(), |
|
| 789 | + ); |
|
| 790 | + //get the message template group. |
|
| 791 | + $msg_template_group = EEM_Message_Template_Group::instance()->get_one(array($template_query_params)); |
|
| 792 | + //if we don't have an EE_Message_Template_Group then return |
|
| 793 | + if (! $msg_template_group instanceof EE_Message_Template_Group) { |
|
| 794 | + // remove EVT_ID from query params so that global templates get picked up |
|
| 795 | + unset($template_query_params['Event.EVT_ID']); |
|
| 796 | + //get global template as the fallback |
|
| 797 | + $msg_template_group = EEM_Message_Template_Group::instance()->get_one(array($template_query_params)); |
|
| 798 | + } |
|
| 799 | + //if we don't have an EE_Message_Template_Group then return |
|
| 800 | + if (! $msg_template_group instanceof EE_Message_Template_Group) { |
|
| 801 | + return ''; |
|
| 802 | + } |
|
| 803 | + // generate the URL |
|
| 804 | + $registration_message_trigger_url = EEH_MSG_Template::generate_url_trigger( |
|
| 805 | + $sending_messenger, |
|
| 806 | + $generating_messenger, |
|
| 807 | + 'purchaser', |
|
| 808 | + $message_type, |
|
| 809 | + $registration, |
|
| 810 | + $msg_template_group->ID(), |
|
| 811 | + $registration->transaction_ID() |
|
| 812 | + ); |
|
| 813 | + |
|
| 814 | + } |
|
| 815 | + return $registration_message_trigger_url; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + |
|
| 819 | + /** |
|
| 820 | + * Use to generate and return a message preview! |
|
| 821 | + * |
|
| 822 | + * @param string $type This should correspond with a valid message type |
|
| 823 | + * @param string $context This should correspond with a valid context for the message type |
|
| 824 | + * @param string $messenger This should correspond with a valid messenger. |
|
| 825 | + * @param bool $send true we will do a test send using the messenger delivery, false we just do a regular |
|
| 826 | + * preview |
|
| 827 | + * @return bool|string The body of the message or if send is requested, sends. |
|
| 828 | + * @throws EE_Error |
|
| 829 | + */ |
|
| 830 | + public static function preview_message($type, $context, $messenger, $send = false) |
|
| 831 | + { |
|
| 832 | + self::_load_controller(); |
|
| 833 | + $mtg = new EE_Message_To_Generate( |
|
| 834 | + $messenger, |
|
| 835 | + $type, |
|
| 836 | + array(), |
|
| 837 | + $context, |
|
| 838 | + true |
|
| 839 | + ); |
|
| 840 | + $generated_preview_queue = self::$_MSG_PROCESSOR->generate_for_preview($mtg, $send); |
|
| 841 | + if ($generated_preview_queue instanceof EE_Messages_Queue) { |
|
| 842 | + //loop through all content for the preview and remove any persisted records. |
|
| 843 | + $content = ''; |
|
| 844 | + foreach ($generated_preview_queue->get_message_repository() as $message) { |
|
| 845 | + $content = $message->content(); |
|
| 846 | + if ($message->ID() > 0 && $message->STS_ID() !== EEM_Message::status_failed) { |
|
| 847 | + $message->delete(); |
|
| 848 | + } |
|
| 849 | + } |
|
| 850 | + return $content; |
|
| 851 | + } else { |
|
| 852 | + return $generated_preview_queue; |
|
| 853 | + } |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + |
|
| 857 | + /** |
|
| 858 | + * This is a method that allows for sending a message using a messenger matching the string given and the provided |
|
| 859 | + * EE_Message_Queue object. The EE_Message_Queue object is used to create a single aggregate EE_Message via the |
|
| 860 | + * content found in the EE_Message objects in the queue. |
|
| 861 | + * |
|
| 862 | + * @since 4.9.0 |
|
| 863 | + * @param string $messenger a string matching a valid active messenger in the system |
|
| 864 | + * @param string $message_type Although it seems contrary to the name of the method, a message |
|
| 865 | + * type name is still required to send along the message type to the |
|
| 866 | + * messenger because this is used for determining what specific |
|
| 867 | + * variations might be loaded for the generated message. |
|
| 868 | + * @param EE_Messages_Queue $queue |
|
| 869 | + * @param string $custom_subject Can be used to set what the custom subject string will be on the |
|
| 870 | + * aggregate EE_Message object. |
|
| 871 | + * @return bool success or fail. |
|
| 872 | + */ |
|
| 873 | + public static function send_message_with_messenger_only( |
|
| 874 | + $messenger, |
|
| 875 | + $message_type, |
|
| 876 | + EE_Messages_Queue $queue, |
|
| 877 | + $custom_subject = '' |
|
| 878 | + ) { |
|
| 879 | + self::_load_controller(); |
|
| 880 | + /** @type EE_Message_To_Generate_From_Queue $message_to_generate */ |
|
| 881 | + $message_to_generate = EE_Registry::instance()->load_lib( |
|
| 882 | + 'Message_To_Generate_From_Queue', |
|
| 883 | + array( |
|
| 884 | + $messenger, |
|
| 885 | + $message_type, |
|
| 886 | + $queue, |
|
| 887 | + $custom_subject, |
|
| 888 | + ) |
|
| 889 | + ); |
|
| 890 | + return self::$_MSG_PROCESSOR->queue_for_sending($message_to_generate); |
|
| 891 | + } |
|
| 892 | + |
|
| 893 | + |
|
| 894 | + /** |
|
| 895 | + * Generates Messages immediately for EE_Message IDs (but only for the correct status for generation) |
|
| 896 | + * |
|
| 897 | + * @since 4.9.0 |
|
| 898 | + * @param array $message_ids An array of message ids |
|
| 899 | + * @return bool | EE_Messages_Queue false if nothing was generated, EE_Messages_Queue containing generated |
|
| 900 | + * messages. |
|
| 901 | + */ |
|
| 902 | + public static function generate_now($message_ids) |
|
| 903 | + { |
|
| 904 | + self::_load_controller(); |
|
| 905 | + $messages = EEM_Message::instance()->get_all( |
|
| 906 | + array( |
|
| 907 | + 0 => array( |
|
| 908 | + 'MSG_ID' => array('IN', $message_ids), |
|
| 909 | + 'STS_ID' => EEM_Message::status_incomplete, |
|
| 910 | + ), |
|
| 911 | + ) |
|
| 912 | + ); |
|
| 913 | + $generated_queue = false; |
|
| 914 | + if ($messages) { |
|
| 915 | + $generated_queue = self::$_MSG_PROCESSOR->batch_generate_from_queue($messages); |
|
| 916 | + } |
|
| 917 | + |
|
| 918 | + if (! $generated_queue instanceof EE_Messages_Queue) { |
|
| 919 | + EE_Error::add_error( |
|
| 920 | + __('The messages were not generated. This could mean there is already a batch being generated on a separate request, or because the selected messages are not ready for generation. Please wait a minute or two and try again.', |
|
| 921 | + 'event_espresso'), |
|
| 922 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 923 | + ); |
|
| 924 | + } |
|
| 925 | + return $generated_queue; |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * Sends messages immediately for the incoming message_ids that have the status of EEM_Message::status_resend or, |
|
| 931 | + * EEM_Message::status_idle |
|
| 932 | + * |
|
| 933 | + * @since 4.9.0 |
|
| 934 | + * @param $message_ids |
|
| 935 | + * @return bool | EE_Messages_Queue false if no messages sent. |
|
| 936 | + */ |
|
| 937 | + public static function send_now($message_ids) |
|
| 938 | + { |
|
| 939 | + self::_load_controller(); |
|
| 940 | + $messages = EEM_Message::instance()->get_all( |
|
| 941 | + array( |
|
| 942 | + 0 => array( |
|
| 943 | + 'MSG_ID' => array('IN', $message_ids), |
|
| 944 | + 'STS_ID' => array( |
|
| 945 | + 'IN', |
|
| 946 | + array(EEM_Message::status_idle, EEM_Message::status_resend, EEM_Message::status_retry), |
|
| 947 | + ), |
|
| 948 | + ), |
|
| 949 | + ) |
|
| 950 | + ); |
|
| 951 | + $sent_queue = false; |
|
| 952 | + if ($messages) { |
|
| 953 | + $sent_queue = self::$_MSG_PROCESSOR->batch_send_from_queue($messages); |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + if (! $sent_queue instanceof EE_Messages_Queue) { |
|
| 957 | + EE_Error::add_error( |
|
| 958 | + __('The messages were not sent. This could mean there is already a batch being sent on a separate request, or because the selected messages are not sendable. Please wait a minute or two and try again.', |
|
| 959 | + 'event_espresso'), |
|
| 960 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 961 | + ); |
|
| 962 | + } else { |
|
| 963 | + //can count how many sent by using the messages in the queue |
|
| 964 | + $sent_count = $sent_queue->count_STS_in_queue(EEM_Message::instance()->stati_indicating_sent()); |
|
| 965 | + if ($sent_count > 0) { |
|
| 966 | + EE_Error::add_success( |
|
| 967 | + sprintf( |
|
| 968 | + _n( |
|
| 969 | + 'There was %d message successfully sent.', |
|
| 970 | + 'There were %d messages successfully sent.', |
|
| 971 | + $sent_count, |
|
| 972 | + 'event_espresso' |
|
| 973 | + ), |
|
| 974 | + $sent_count |
|
| 975 | + ) |
|
| 976 | + ); |
|
| 977 | + } else { |
|
| 978 | + EE_Error::overwrite_errors(); |
|
| 979 | + EE_Error::add_error( |
|
| 980 | + __('No message was sent because of problems with sending. Either all the messages you selected were not a sendable message, they were ALREADY sent on a different scheduled task, or there was an error. |
|
| 981 | 981 | If there was an error, you can look at the messages in the message activity list table for any error messages.', |
| 982 | - 'event_espresso'), |
|
| 983 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 984 | - ); |
|
| 985 | - } |
|
| 986 | - } |
|
| 987 | - return $sent_queue; |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - |
|
| 991 | - /** |
|
| 992 | - * This will queue the incoming message ids for resending. |
|
| 993 | - * Note, only message_ids corresponding to messages with the status of EEM_Message::sent will be queued. |
|
| 994 | - * |
|
| 995 | - * @since 4.9.0 |
|
| 996 | - * @param array $message_ids An array of EE_Message IDs |
|
| 997 | - * @return bool true means messages were successfully queued for resending, false means none were queued for |
|
| 998 | - * resending. |
|
| 999 | - */ |
|
| 1000 | - public static function queue_for_resending($message_ids) |
|
| 1001 | - { |
|
| 1002 | - self::_load_controller(); |
|
| 1003 | - self::$_MSG_PROCESSOR->setup_messages_from_ids_and_send($message_ids); |
|
| 1004 | - |
|
| 1005 | - //get queue and count |
|
| 1006 | - $queue_count = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue(EEM_Message::status_resend); |
|
| 1007 | - |
|
| 1008 | - if ( |
|
| 1009 | - $queue_count > 0 |
|
| 1010 | - ) { |
|
| 1011 | - EE_Error::add_success( |
|
| 1012 | - sprintf( |
|
| 1013 | - _n( |
|
| 1014 | - '%d message successfully queued for resending.', |
|
| 1015 | - '%d messages successfully queued for resending.', |
|
| 1016 | - $queue_count, |
|
| 1017 | - 'event_espresso' |
|
| 1018 | - ), |
|
| 1019 | - $queue_count |
|
| 1020 | - ) |
|
| 1021 | - ); |
|
| 1022 | - /** |
|
| 1023 | - * @see filter usage in EE_Messages_Queue::initiate_request_by_priority |
|
| 1024 | - */ |
|
| 1025 | - } elseif ( |
|
| 1026 | - apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', true) |
|
| 1027 | - || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
| 1028 | - ) { |
|
| 1029 | - $queue_count = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue(EEM_Message::status_sent); |
|
| 1030 | - if ($queue_count > 0) { |
|
| 1031 | - EE_Error::add_success( |
|
| 1032 | - sprintf( |
|
| 1033 | - _n( |
|
| 1034 | - '%d message successfully sent.', |
|
| 1035 | - '%d messages successfully sent.', |
|
| 1036 | - $queue_count, |
|
| 1037 | - 'event_espresso' |
|
| 1038 | - ), |
|
| 1039 | - $queue_count |
|
| 1040 | - ) |
|
| 1041 | - ); |
|
| 1042 | - } else { |
|
| 1043 | - EE_Error::add_error( |
|
| 1044 | - __('No messages were queued for resending. This usually only happens when all the messages flagged for resending are not a status that can be resent.', |
|
| 1045 | - 'event_espresso'), |
|
| 1046 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 1047 | - ); |
|
| 1048 | - } |
|
| 1049 | - } else { |
|
| 1050 | - EE_Error::add_error( |
|
| 1051 | - __('No messages were queued for resending. This usually only happens when all the messages flagged for resending are not a status that can be resent.', |
|
| 1052 | - 'event_espresso'), |
|
| 1053 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 1054 | - ); |
|
| 1055 | - } |
|
| 1056 | - return (bool)$queue_count; |
|
| 1057 | - } |
|
| 1058 | - |
|
| 1059 | - |
|
| 1060 | - /** |
|
| 1061 | - * debug |
|
| 1062 | - * |
|
| 1063 | - * @param string $class |
|
| 1064 | - * @param string $func |
|
| 1065 | - * @param string $line |
|
| 1066 | - * @param \EE_Transaction $transaction |
|
| 1067 | - * @param array $info |
|
| 1068 | - * @param bool $display_request |
|
| 1069 | - */ |
|
| 1070 | - protected static function log( |
|
| 1071 | - $class = '', |
|
| 1072 | - $func = '', |
|
| 1073 | - $line = '', |
|
| 1074 | - EE_Transaction $transaction, |
|
| 1075 | - $info = array(), |
|
| 1076 | - $display_request = false |
|
| 1077 | - ) { |
|
| 1078 | - if (WP_DEBUG && false) { |
|
| 1079 | - if ($transaction instanceof EE_Transaction) { |
|
| 1080 | - // don't serialize objects |
|
| 1081 | - $info = EEH_Debug_Tools::strip_objects($info); |
|
| 1082 | - $info['TXN_status'] = $transaction->status_ID(); |
|
| 1083 | - $info['TXN_reg_steps'] = $transaction->reg_steps(); |
|
| 1084 | - if ($transaction->ID()) { |
|
| 1085 | - $index = 'EE_Transaction: ' . $transaction->ID(); |
|
| 1086 | - EEH_Debug_Tools::log($class, $func, $line, $info, $display_request, $index); |
|
| 1087 | - } |
|
| 1088 | - } |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - } |
|
| 1092 | - |
|
| 1093 | - |
|
| 1094 | - /** |
|
| 1095 | - * Resets all the static properties in this class when called. |
|
| 1096 | - */ |
|
| 1097 | - public static function reset() |
|
| 1098 | - { |
|
| 1099 | - self::$_EEMSG = null; |
|
| 1100 | - self::$_message_resource_manager = null; |
|
| 1101 | - self::$_MSG_PROCESSOR = null; |
|
| 1102 | - self::$_MSG_PATHS = null; |
|
| 1103 | - self::$_TMP_PACKS = array(); |
|
| 1104 | - } |
|
| 982 | + 'event_espresso'), |
|
| 983 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 984 | + ); |
|
| 985 | + } |
|
| 986 | + } |
|
| 987 | + return $sent_queue; |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + |
|
| 991 | + /** |
|
| 992 | + * This will queue the incoming message ids for resending. |
|
| 993 | + * Note, only message_ids corresponding to messages with the status of EEM_Message::sent will be queued. |
|
| 994 | + * |
|
| 995 | + * @since 4.9.0 |
|
| 996 | + * @param array $message_ids An array of EE_Message IDs |
|
| 997 | + * @return bool true means messages were successfully queued for resending, false means none were queued for |
|
| 998 | + * resending. |
|
| 999 | + */ |
|
| 1000 | + public static function queue_for_resending($message_ids) |
|
| 1001 | + { |
|
| 1002 | + self::_load_controller(); |
|
| 1003 | + self::$_MSG_PROCESSOR->setup_messages_from_ids_and_send($message_ids); |
|
| 1004 | + |
|
| 1005 | + //get queue and count |
|
| 1006 | + $queue_count = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue(EEM_Message::status_resend); |
|
| 1007 | + |
|
| 1008 | + if ( |
|
| 1009 | + $queue_count > 0 |
|
| 1010 | + ) { |
|
| 1011 | + EE_Error::add_success( |
|
| 1012 | + sprintf( |
|
| 1013 | + _n( |
|
| 1014 | + '%d message successfully queued for resending.', |
|
| 1015 | + '%d messages successfully queued for resending.', |
|
| 1016 | + $queue_count, |
|
| 1017 | + 'event_espresso' |
|
| 1018 | + ), |
|
| 1019 | + $queue_count |
|
| 1020 | + ) |
|
| 1021 | + ); |
|
| 1022 | + /** |
|
| 1023 | + * @see filter usage in EE_Messages_Queue::initiate_request_by_priority |
|
| 1024 | + */ |
|
| 1025 | + } elseif ( |
|
| 1026 | + apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', true) |
|
| 1027 | + || EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request |
|
| 1028 | + ) { |
|
| 1029 | + $queue_count = self::$_MSG_PROCESSOR->get_queue()->count_STS_in_queue(EEM_Message::status_sent); |
|
| 1030 | + if ($queue_count > 0) { |
|
| 1031 | + EE_Error::add_success( |
|
| 1032 | + sprintf( |
|
| 1033 | + _n( |
|
| 1034 | + '%d message successfully sent.', |
|
| 1035 | + '%d messages successfully sent.', |
|
| 1036 | + $queue_count, |
|
| 1037 | + 'event_espresso' |
|
| 1038 | + ), |
|
| 1039 | + $queue_count |
|
| 1040 | + ) |
|
| 1041 | + ); |
|
| 1042 | + } else { |
|
| 1043 | + EE_Error::add_error( |
|
| 1044 | + __('No messages were queued for resending. This usually only happens when all the messages flagged for resending are not a status that can be resent.', |
|
| 1045 | + 'event_espresso'), |
|
| 1046 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 1047 | + ); |
|
| 1048 | + } |
|
| 1049 | + } else { |
|
| 1050 | + EE_Error::add_error( |
|
| 1051 | + __('No messages were queued for resending. This usually only happens when all the messages flagged for resending are not a status that can be resent.', |
|
| 1052 | + 'event_espresso'), |
|
| 1053 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 1054 | + ); |
|
| 1055 | + } |
|
| 1056 | + return (bool)$queue_count; |
|
| 1057 | + } |
|
| 1058 | + |
|
| 1059 | + |
|
| 1060 | + /** |
|
| 1061 | + * debug |
|
| 1062 | + * |
|
| 1063 | + * @param string $class |
|
| 1064 | + * @param string $func |
|
| 1065 | + * @param string $line |
|
| 1066 | + * @param \EE_Transaction $transaction |
|
| 1067 | + * @param array $info |
|
| 1068 | + * @param bool $display_request |
|
| 1069 | + */ |
|
| 1070 | + protected static function log( |
|
| 1071 | + $class = '', |
|
| 1072 | + $func = '', |
|
| 1073 | + $line = '', |
|
| 1074 | + EE_Transaction $transaction, |
|
| 1075 | + $info = array(), |
|
| 1076 | + $display_request = false |
|
| 1077 | + ) { |
|
| 1078 | + if (WP_DEBUG && false) { |
|
| 1079 | + if ($transaction instanceof EE_Transaction) { |
|
| 1080 | + // don't serialize objects |
|
| 1081 | + $info = EEH_Debug_Tools::strip_objects($info); |
|
| 1082 | + $info['TXN_status'] = $transaction->status_ID(); |
|
| 1083 | + $info['TXN_reg_steps'] = $transaction->reg_steps(); |
|
| 1084 | + if ($transaction->ID()) { |
|
| 1085 | + $index = 'EE_Transaction: ' . $transaction->ID(); |
|
| 1086 | + EEH_Debug_Tools::log($class, $func, $line, $info, $display_request, $index); |
|
| 1087 | + } |
|
| 1088 | + } |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + } |
|
| 1092 | + |
|
| 1093 | + |
|
| 1094 | + /** |
|
| 1095 | + * Resets all the static properties in this class when called. |
|
| 1096 | + */ |
|
| 1097 | + public static function reset() |
|
| 1098 | + { |
|
| 1099 | + self::$_EEMSG = null; |
|
| 1100 | + self::$_message_resource_manager = null; |
|
| 1101 | + self::$_MSG_PROCESSOR = null; |
|
| 1102 | + self::$_MSG_PATHS = null; |
|
| 1103 | + self::$_TMP_PACKS = array(); |
|
| 1104 | + } |
|
| 1105 | 1105 | |
| 1106 | 1106 | } |
| 1107 | 1107 | // End of file EED_Messages.module.php |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 2 | - exit('No direct script access allowed'); |
|
| 2 | + exit('No direct script access allowed'); |
|
| 3 | 3 | } |
| 4 | 4 | do_action('AHEE_log', __FILE__, ' FILE LOADED', ''); |
| 5 | 5 | |
@@ -25,2718 +25,2718 @@ discard block |
||
| 25 | 25 | abstract class EE_Base_Class |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * This is an array of the original properties and values provided during construction |
|
| 30 | - * of this model object. (keys are model field names, values are their values). |
|
| 31 | - * This list is important to remember so that when we are merging data from the db, we know |
|
| 32 | - * which values to override and which to not override. |
|
| 33 | - * |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - protected $_props_n_values_provided_in_constructor; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Timezone |
|
| 40 | - * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 41 | - * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 42 | - * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 43 | - * access to it. |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - protected $_timezone; |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * date format |
|
| 53 | - * pattern or format for displaying dates |
|
| 54 | - * |
|
| 55 | - * @var string $_dt_frmt |
|
| 56 | - */ |
|
| 57 | - protected $_dt_frmt; |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * time format |
|
| 63 | - * pattern or format for displaying time |
|
| 64 | - * |
|
| 65 | - * @var string $_tm_frmt |
|
| 66 | - */ |
|
| 67 | - protected $_tm_frmt; |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 73 | - * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 74 | - * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 75 | - * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 76 | - * |
|
| 77 | - * @var array |
|
| 78 | - */ |
|
| 79 | - protected $_cached_properties = array(); |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 83 | - * single |
|
| 84 | - * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 85 | - * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 86 | - * all others have an array) |
|
| 87 | - * |
|
| 88 | - * @var array |
|
| 89 | - */ |
|
| 90 | - protected $_model_relations = array(); |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 94 | - * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 95 | - * |
|
| 96 | - * @var array |
|
| 97 | - */ |
|
| 98 | - protected $_fields = array(); |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 102 | - * For example, we might create model objects intended to only be used for the duration |
|
| 103 | - * of this request and to be thrown away, and if they were accidentally saved |
|
| 104 | - * it would be a bug. |
|
| 105 | - */ |
|
| 106 | - protected $_allow_persist = true; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 110 | - */ |
|
| 111 | - protected $_has_changes = false; |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @var EEM_Base |
|
| 115 | - */ |
|
| 116 | - protected $_model; |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 122 | - * play nice |
|
| 123 | - * |
|
| 124 | - * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 125 | - * layer of the model's _fields array, (eg, EVT_ID, |
|
| 126 | - * TXN_amount, QST_name, etc) and values are their values |
|
| 127 | - * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 128 | - * corresponding db model or not. |
|
| 129 | - * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 130 | - * be in when instantiating a EE_Base_Class object. |
|
| 131 | - * @param array $date_formats An array of date formats to set on construct where first |
|
| 132 | - * value is the date_format and second value is the time |
|
| 133 | - * format. |
|
| 134 | - * @throws EE_Error |
|
| 135 | - */ |
|
| 136 | - protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 137 | - { |
|
| 138 | - $className = get_class($this); |
|
| 139 | - do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 140 | - $model = $this->get_model(); |
|
| 141 | - $model_fields = $model->field_settings(false); |
|
| 142 | - // ensure $fieldValues is an array |
|
| 143 | - $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 144 | - // EEH_Debug_Tools::printr( $fieldValues, '$fieldValues <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 145 | - // verify client code has not passed any invalid field names |
|
| 146 | - foreach ($fieldValues as $field_name => $field_value) { |
|
| 147 | - if ( ! isset($model_fields[$field_name])) { |
|
| 148 | - throw new EE_Error(sprintf(__("Invalid field (%s) passed to constructor of %s. Allowed fields are :%s", |
|
| 149 | - "event_espresso"), $field_name, get_class($this), implode(", ", array_keys($model_fields)))); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - // EEH_Debug_Tools::printr( $model_fields, '$model_fields <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 153 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 154 | - if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 155 | - list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 156 | - } else { |
|
| 157 | - //set default formats for date and time |
|
| 158 | - $this->_dt_frmt = (string)get_option('date_format', 'Y-m-d'); |
|
| 159 | - $this->_tm_frmt = (string)get_option('time_format', 'g:i a'); |
|
| 160 | - } |
|
| 161 | - //if db model is instantiating |
|
| 162 | - if ($bydb) { |
|
| 163 | - //client code has indicated these field values are from the database |
|
| 164 | - foreach ($model_fields as $fieldName => $field) { |
|
| 165 | - $this->set_from_db($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null); |
|
| 166 | - } |
|
| 167 | - } else { |
|
| 168 | - //we're constructing a brand |
|
| 169 | - //new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 170 | - foreach ($model_fields as $fieldName => $field) { |
|
| 171 | - $this->set($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, true); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - //remember what values were passed to this constructor |
|
| 175 | - $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 176 | - //remember in entity mapper |
|
| 177 | - if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 178 | - $model->add_to_entity_map($this); |
|
| 179 | - } |
|
| 180 | - //setup all the relations |
|
| 181 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 182 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 183 | - $this->_model_relations[$relation_name] = null; |
|
| 184 | - } else { |
|
| 185 | - $this->_model_relations[$relation_name] = array(); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - /** |
|
| 189 | - * Action done at the end of each model object construction |
|
| 190 | - * |
|
| 191 | - * @param EE_Base_Class $this the model object just created |
|
| 192 | - */ |
|
| 193 | - do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 200 | - * |
|
| 201 | - * @return boolean |
|
| 202 | - */ |
|
| 203 | - public function allow_persist() |
|
| 204 | - { |
|
| 205 | - return $this->_allow_persist; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 212 | - * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 213 | - * you got new information that somehow made you change your mind. |
|
| 214 | - * |
|
| 215 | - * @param boolean $allow_persist |
|
| 216 | - * @return boolean |
|
| 217 | - */ |
|
| 218 | - public function set_allow_persist($allow_persist) |
|
| 219 | - { |
|
| 220 | - return $this->_allow_persist = $allow_persist; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Gets the field's original value when this object was constructed during this request. |
|
| 227 | - * This can be helpful when determining if a model object has changed or not |
|
| 228 | - * |
|
| 229 | - * @param string $field_name |
|
| 230 | - * @return mixed|null |
|
| 231 | - * @throws \EE_Error |
|
| 232 | - */ |
|
| 233 | - public function get_original($field_name) |
|
| 234 | - { |
|
| 235 | - if (isset($this->_props_n_values_provided_in_constructor[$field_name]) |
|
| 236 | - && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 237 | - ) { |
|
| 238 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
|
| 239 | - } else { |
|
| 240 | - return null; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param EE_Base_Class $obj |
|
| 248 | - * @return string |
|
| 249 | - */ |
|
| 250 | - public function get_class($obj) |
|
| 251 | - { |
|
| 252 | - return get_class($obj); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Overrides parent because parent expects old models. |
|
| 259 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 260 | - * |
|
| 261 | - * @param string $field_name |
|
| 262 | - * @param mixed $field_value |
|
| 263 | - * @param bool $use_default |
|
| 264 | - * @throws \EE_Error |
|
| 265 | - */ |
|
| 266 | - public function set($field_name, $field_value, $use_default = false) |
|
| 267 | - { |
|
| 268 | - // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 269 | - // then don't do anything |
|
| 270 | - if ( |
|
| 271 | - ! $use_default |
|
| 272 | - && $this->_fields[$field_name] === $field_value |
|
| 273 | - && $this->ID() |
|
| 274 | - ) { |
|
| 275 | - return; |
|
| 276 | - } |
|
| 277 | - $model = $this->get_model(); |
|
| 278 | - $this->_has_changes = true; |
|
| 279 | - $field_obj = $model->field_settings_for($field_name); |
|
| 280 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 281 | - // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 282 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 283 | - $field_obj->set_timezone($this->_timezone); |
|
| 284 | - $field_obj->set_date_format($this->_dt_frmt); |
|
| 285 | - $field_obj->set_time_format($this->_tm_frmt); |
|
| 286 | - } |
|
| 287 | - $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 288 | - //should the value be null? |
|
| 289 | - if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 290 | - $this->_fields[$field_name] = $field_obj->get_default_value(); |
|
| 291 | - /** |
|
| 292 | - * To save having to refactor all the models, if a default value is used for a |
|
| 293 | - * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 294 | - * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 295 | - * object. |
|
| 296 | - * |
|
| 297 | - * @since 4.6.10+ |
|
| 298 | - */ |
|
| 299 | - if ( |
|
| 300 | - $field_obj instanceof EE_Datetime_Field |
|
| 301 | - && $this->_fields[$field_name] !== null |
|
| 302 | - && ! $this->_fields[$field_name] instanceof DateTime |
|
| 303 | - ) { |
|
| 304 | - empty($this->_fields[$field_name]) |
|
| 305 | - ? $this->set($field_name, time()) |
|
| 306 | - : $this->set($field_name, $this->_fields[$field_name]); |
|
| 307 | - } |
|
| 308 | - } else { |
|
| 309 | - $this->_fields[$field_name] = $holder_of_value; |
|
| 310 | - } |
|
| 311 | - //if we're not in the constructor... |
|
| 312 | - //now check if what we set was a primary key |
|
| 313 | - if ( |
|
| 314 | - //note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 315 | - $this->_props_n_values_provided_in_constructor |
|
| 316 | - && $field_value |
|
| 317 | - && $field_name === $model->primary_key_name() |
|
| 318 | - ) { |
|
| 319 | - //if so, we want all this object's fields to be filled either with |
|
| 320 | - //what we've explicitly set on this model |
|
| 321 | - //or what we have in the db |
|
| 322 | - // echo "setting primary key!"; |
|
| 323 | - $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 324 | - $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 325 | - foreach ($fields_on_model as $field_obj) { |
|
| 326 | - if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 327 | - && $field_obj->get_name() !== $field_name |
|
| 328 | - ) { |
|
| 329 | - $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - //oh this model object has an ID? well make sure its in the entity mapper |
|
| 333 | - $model->add_to_entity_map($this); |
|
| 334 | - } |
|
| 335 | - //let's unset any cache for this field_name from the $_cached_properties property. |
|
| 336 | - $this->_clear_cached_property($field_name); |
|
| 337 | - } else { |
|
| 338 | - throw new EE_Error(sprintf(__("A valid EE_Model_Field_Base could not be found for the given field name: %s", |
|
| 339 | - "event_espresso"), $field_name)); |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * This sets the field value on the db column if it exists for the given $column_name or |
|
| 347 | - * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 348 | - * |
|
| 349 | - * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 350 | - * @param string $field_name Must be the exact column name. |
|
| 351 | - * @param mixed $field_value The value to set. |
|
| 352 | - * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 353 | - * @throws \EE_Error |
|
| 354 | - */ |
|
| 355 | - public function set_field_or_extra_meta($field_name, $field_value) |
|
| 356 | - { |
|
| 357 | - if ($this->get_model()->has_field($field_name)) { |
|
| 358 | - $this->set($field_name, $field_value); |
|
| 359 | - return true; |
|
| 360 | - } else { |
|
| 361 | - //ensure this object is saved first so that extra meta can be properly related. |
|
| 362 | - $this->save(); |
|
| 363 | - return $this->update_extra_meta($field_name, $field_value); |
|
| 364 | - } |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * This retrieves the value of the db column set on this class or if that's not present |
|
| 371 | - * it will attempt to retrieve from extra_meta if found. |
|
| 372 | - * Example Usage: |
|
| 373 | - * Via EE_Message child class: |
|
| 374 | - * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 375 | - * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 376 | - * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 377 | - * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 378 | - * value for those extra fields dynamically via the EE_message object. |
|
| 379 | - * |
|
| 380 | - * @param string $field_name expecting the fully qualified field name. |
|
| 381 | - * @return mixed|null value for the field if found. null if not found. |
|
| 382 | - * @throws \EE_Error |
|
| 383 | - */ |
|
| 384 | - public function get_field_or_extra_meta($field_name) |
|
| 385 | - { |
|
| 386 | - if ($this->get_model()->has_field($field_name)) { |
|
| 387 | - $column_value = $this->get($field_name); |
|
| 388 | - } else { |
|
| 389 | - //This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 390 | - $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 391 | - } |
|
| 392 | - return $column_value; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 399 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 400 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 401 | - * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 402 | - * |
|
| 403 | - * @access public |
|
| 404 | - * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 405 | - * @return void |
|
| 406 | - * @throws \EE_Error |
|
| 407 | - */ |
|
| 408 | - public function set_timezone($timezone = '') |
|
| 409 | - { |
|
| 410 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 411 | - //make sure we clear all cached properties because they won't be relevant now |
|
| 412 | - $this->_clear_cached_properties(); |
|
| 413 | - //make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 414 | - $model_fields = $this->get_model()->field_settings(false); |
|
| 415 | - foreach ($model_fields as $field_name => $field_obj) { |
|
| 416 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 417 | - $field_obj->set_timezone($this->_timezone); |
|
| 418 | - if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) { |
|
| 419 | - $this->_fields[$field_name]->setTimezone(new DateTimeZone($this->_timezone)); |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * This just returns whatever is set for the current timezone. |
|
| 429 | - * |
|
| 430 | - * @access public |
|
| 431 | - * @return string timezone string |
|
| 432 | - */ |
|
| 433 | - public function get_timezone() |
|
| 434 | - { |
|
| 435 | - return $this->_timezone; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 442 | - * internally instead of wp set date format options |
|
| 443 | - * |
|
| 444 | - * @since 4.6 |
|
| 445 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 446 | - */ |
|
| 447 | - public function set_date_format($format) |
|
| 448 | - { |
|
| 449 | - $this->_dt_frmt = $format; |
|
| 450 | - //clear cached_properties because they won't be relevant now. |
|
| 451 | - $this->_clear_cached_properties(); |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 458 | - * class internally instead of wp set time format options. |
|
| 459 | - * |
|
| 460 | - * @since 4.6 |
|
| 461 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 462 | - */ |
|
| 463 | - public function set_time_format($format) |
|
| 464 | - { |
|
| 465 | - $this->_tm_frmt = $format; |
|
| 466 | - //clear cached_properties because they won't be relevant now. |
|
| 467 | - $this->_clear_cached_properties(); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * This returns the current internal set format for the date and time formats. |
|
| 474 | - * |
|
| 475 | - * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 476 | - * where the first value is the date format and the second value is the time format. |
|
| 477 | - * @return mixed string|array |
|
| 478 | - */ |
|
| 479 | - public function get_format($full = true) |
|
| 480 | - { |
|
| 481 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * cache |
|
| 488 | - * stores the passed model object on the current model object. |
|
| 489 | - * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 490 | - * |
|
| 491 | - * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 492 | - * 'Registration' associated with this model object |
|
| 493 | - * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 494 | - * that could be a payment or a registration) |
|
| 495 | - * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 496 | - * items which will be stored in an array on this object |
|
| 497 | - * @throws EE_Error |
|
| 498 | - * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 499 | - * related thing, no array) |
|
| 500 | - */ |
|
| 501 | - public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 502 | - { |
|
| 503 | - // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 504 | - if ( ! $object_to_cache instanceof EE_Base_Class) { |
|
| 505 | - return false; |
|
| 506 | - } |
|
| 507 | - // also get "how" the object is related, or throw an error |
|
| 508 | - if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 509 | - throw new EE_Error(sprintf(__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 510 | - $relationName, get_class($this))); |
|
| 511 | - } |
|
| 512 | - // how many things are related ? |
|
| 513 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 514 | - // if it's a "belongs to" relationship, then there's only one related model object eg, if this is a registration, there's only 1 attendee for it |
|
| 515 | - // so for these model objects just set it to be cached |
|
| 516 | - $this->_model_relations[$relationName] = $object_to_cache; |
|
| 517 | - $return = true; |
|
| 518 | - } else { |
|
| 519 | - // otherwise, this is the "many" side of a one to many relationship, so we'll add the object to the array of related objects for that type. |
|
| 520 | - // eg: if this is an event, there are many registrations for that event, so we cache the registrations in an array |
|
| 521 | - if ( ! is_array($this->_model_relations[$relationName])) { |
|
| 522 | - // if for some reason, the cached item is a model object, then stick that in the array, otherwise start with an empty array |
|
| 523 | - $this->_model_relations[$relationName] = $this->_model_relations[$relationName] instanceof EE_Base_Class |
|
| 524 | - ? array($this->_model_relations[$relationName]) : array(); |
|
| 525 | - } |
|
| 526 | - // first check for a cache_id which is normally empty |
|
| 527 | - if ( ! empty($cache_id)) { |
|
| 528 | - // if the cache_id exists, then it means we are purposely trying to cache this with a known key that can then be used to retrieve the object later on |
|
| 529 | - $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
|
| 530 | - $return = $cache_id; |
|
| 531 | - } elseif ($object_to_cache->ID()) { |
|
| 532 | - // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 533 | - $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
|
| 534 | - $return = $object_to_cache->ID(); |
|
| 535 | - } else { |
|
| 536 | - // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 537 | - $this->_model_relations[$relationName][] = $object_to_cache; |
|
| 538 | - // move the internal pointer to the end of the array |
|
| 539 | - end($this->_model_relations[$relationName]); |
|
| 540 | - // and grab the key so that we can return it |
|
| 541 | - $return = key($this->_model_relations[$relationName]); |
|
| 542 | - } |
|
| 543 | - } |
|
| 544 | - return $return; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - |
|
| 548 | - |
|
| 549 | - /** |
|
| 550 | - * For adding an item to the cached_properties property. |
|
| 551 | - * |
|
| 552 | - * @access protected |
|
| 553 | - * @param string $fieldname the property item the corresponding value is for. |
|
| 554 | - * @param mixed $value The value we are caching. |
|
| 555 | - * @param string|null $cache_type |
|
| 556 | - * @return void |
|
| 557 | - * @throws \EE_Error |
|
| 558 | - */ |
|
| 559 | - protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 560 | - { |
|
| 561 | - //first make sure this property exists |
|
| 562 | - $this->get_model()->field_settings_for($fieldname); |
|
| 563 | - $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 564 | - $this->_cached_properties[$fieldname][$cache_type] = $value; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 571 | - * This also SETS the cache if we return the actual property! |
|
| 572 | - * |
|
| 573 | - * @param string $fieldname the name of the property we're trying to retrieve |
|
| 574 | - * @param bool $pretty |
|
| 575 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 576 | - * (in cases where the same property may be used for different outputs |
|
| 577 | - * - i.e. datetime, money etc.) |
|
| 578 | - * It can also accept certain pre-defined "schema" strings |
|
| 579 | - * to define how to output the property. |
|
| 580 | - * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 581 | - * @return mixed whatever the value for the property is we're retrieving |
|
| 582 | - * @throws \EE_Error |
|
| 583 | - */ |
|
| 584 | - protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 585 | - { |
|
| 586 | - //verify the field exists |
|
| 587 | - $model = $this->get_model(); |
|
| 588 | - $model->field_settings_for($fieldname); |
|
| 589 | - $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 590 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 591 | - if (isset($this->_cached_properties[$fieldname][$cache_type])) { |
|
| 592 | - return $this->_cached_properties[$fieldname][$cache_type]; |
|
| 593 | - } |
|
| 594 | - $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
| 595 | - $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 596 | - return $value; |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * If the cache didn't fetch the needed item, this fetches it. |
|
| 603 | - * @param string $fieldname |
|
| 604 | - * @param bool $pretty |
|
| 605 | - * @param string $extra_cache_ref |
|
| 606 | - * @return mixed |
|
| 607 | - */ |
|
| 608 | - protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 609 | - { |
|
| 610 | - $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 611 | - // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 612 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 613 | - $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 614 | - } |
|
| 615 | - if ( ! isset($this->_fields[$fieldname])) { |
|
| 616 | - $this->_fields[$fieldname] = null; |
|
| 617 | - } |
|
| 618 | - $value = $pretty |
|
| 619 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref) |
|
| 620 | - : $field_obj->prepare_for_get($this->_fields[$fieldname]); |
|
| 621 | - return $value; |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 628 | - * |
|
| 629 | - * @param \EE_Datetime_Field $datetime_field |
|
| 630 | - * @param bool $pretty |
|
| 631 | - * @param null $date_or_time |
|
| 632 | - * @return void |
|
| 633 | - * @throws \EE_Error |
|
| 634 | - */ |
|
| 635 | - protected function _prepare_datetime_field( |
|
| 636 | - EE_Datetime_Field $datetime_field, |
|
| 637 | - $pretty = false, |
|
| 638 | - $date_or_time = null |
|
| 639 | - ) { |
|
| 640 | - $datetime_field->set_timezone($this->_timezone); |
|
| 641 | - $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 642 | - $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 643 | - //set the output returned |
|
| 644 | - switch ($date_or_time) { |
|
| 645 | - case 'D' : |
|
| 646 | - $datetime_field->set_date_time_output('date'); |
|
| 647 | - break; |
|
| 648 | - case 'T' : |
|
| 649 | - $datetime_field->set_date_time_output('time'); |
|
| 650 | - break; |
|
| 651 | - default : |
|
| 652 | - $datetime_field->set_date_time_output(); |
|
| 653 | - } |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * This just takes care of clearing out the cached_properties |
|
| 660 | - * |
|
| 661 | - * @return void |
|
| 662 | - */ |
|
| 663 | - protected function _clear_cached_properties() |
|
| 664 | - { |
|
| 665 | - $this->_cached_properties = array(); |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * This just clears out ONE property if it exists in the cache |
|
| 672 | - * |
|
| 673 | - * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 674 | - * @return void |
|
| 675 | - */ |
|
| 676 | - protected function _clear_cached_property($property_name) |
|
| 677 | - { |
|
| 678 | - if (isset($this->_cached_properties[$property_name])) { |
|
| 679 | - unset($this->_cached_properties[$property_name]); |
|
| 680 | - } |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - |
|
| 684 | - |
|
| 685 | - /** |
|
| 686 | - * Ensures that this related thing is a model object. |
|
| 687 | - * |
|
| 688 | - * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 689 | - * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 690 | - * @return EE_Base_Class |
|
| 691 | - * @throws \EE_Error |
|
| 692 | - */ |
|
| 693 | - protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 694 | - { |
|
| 695 | - $other_model_instance = self::_get_model_instance_with_name( |
|
| 696 | - self::_get_model_classname($model_name), |
|
| 697 | - $this->_timezone |
|
| 698 | - ); |
|
| 699 | - return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - |
|
| 703 | - |
|
| 704 | - /** |
|
| 705 | - * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 706 | - * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 707 | - * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 708 | - * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 709 | - * |
|
| 710 | - * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 711 | - * Eg 'Registration' |
|
| 712 | - * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 713 | - * if you intend to use $clear_all = TRUE, or the relation only |
|
| 714 | - * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 715 | - * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 716 | - * this is HasMany or HABTM. |
|
| 717 | - * @throws EE_Error |
|
| 718 | - * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 719 | - * relation from all |
|
| 720 | - */ |
|
| 721 | - public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 722 | - { |
|
| 723 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 724 | - $index_in_cache = ''; |
|
| 725 | - if ( ! $relationship_to_model) { |
|
| 726 | - throw new EE_Error( |
|
| 727 | - sprintf( |
|
| 728 | - __("There is no relationship to %s on a %s. Cannot clear that cache", 'event_espresso'), |
|
| 729 | - $relationName, |
|
| 730 | - get_class($this) |
|
| 731 | - ) |
|
| 732 | - ); |
|
| 733 | - } |
|
| 734 | - if ($clear_all) { |
|
| 735 | - $obj_removed = true; |
|
| 736 | - $this->_model_relations[$relationName] = null; |
|
| 737 | - } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 738 | - $obj_removed = $this->_model_relations[$relationName]; |
|
| 739 | - $this->_model_relations[$relationName] = null; |
|
| 740 | - } else { |
|
| 741 | - if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 742 | - && $object_to_remove_or_index_into_array->ID() |
|
| 743 | - ) { |
|
| 744 | - $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 745 | - if (is_array($this->_model_relations[$relationName]) |
|
| 746 | - && ! isset($this->_model_relations[$relationName][$index_in_cache]) |
|
| 747 | - ) { |
|
| 748 | - $index_found_at = null; |
|
| 749 | - //find this object in the array even though it has a different key |
|
| 750 | - foreach ($this->_model_relations[$relationName] as $index => $obj) { |
|
| 751 | - if ( |
|
| 752 | - $obj instanceof EE_Base_Class |
|
| 753 | - && ( |
|
| 754 | - $obj == $object_to_remove_or_index_into_array |
|
| 755 | - || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 756 | - ) |
|
| 757 | - ) { |
|
| 758 | - $index_found_at = $index; |
|
| 759 | - break; |
|
| 760 | - } |
|
| 761 | - } |
|
| 762 | - if ($index_found_at) { |
|
| 763 | - $index_in_cache = $index_found_at; |
|
| 764 | - } else { |
|
| 765 | - //it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 766 | - //if it wasn't in it to begin with. So we're done |
|
| 767 | - return $object_to_remove_or_index_into_array; |
|
| 768 | - } |
|
| 769 | - } |
|
| 770 | - } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 771 | - //so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 772 | - foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 773 | - if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 774 | - $index_in_cache = $index; |
|
| 775 | - } |
|
| 776 | - } |
|
| 777 | - } else { |
|
| 778 | - $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 779 | - } |
|
| 780 | - //supposedly we've found it. But it could just be that the client code |
|
| 781 | - //provided a bad index/object |
|
| 782 | - if ( |
|
| 783 | - isset( |
|
| 784 | - $this->_model_relations[$relationName], |
|
| 785 | - $this->_model_relations[$relationName][$index_in_cache] |
|
| 786 | - ) |
|
| 787 | - ) { |
|
| 788 | - $obj_removed = $this->_model_relations[$relationName][$index_in_cache]; |
|
| 789 | - unset($this->_model_relations[$relationName][$index_in_cache]); |
|
| 790 | - } else { |
|
| 791 | - //that thing was never cached anyways. |
|
| 792 | - $obj_removed = null; |
|
| 793 | - } |
|
| 794 | - } |
|
| 795 | - return $obj_removed; |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - |
|
| 799 | - |
|
| 800 | - /** |
|
| 801 | - * update_cache_after_object_save |
|
| 802 | - * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 803 | - * obtained after being saved to the db |
|
| 804 | - * |
|
| 805 | - * @param string $relationName - the type of object that is cached |
|
| 806 | - * @param \EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 807 | - * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 808 | - * @return boolean TRUE on success, FALSE on fail |
|
| 809 | - * @throws \EE_Error |
|
| 810 | - */ |
|
| 811 | - public function update_cache_after_object_save( |
|
| 812 | - $relationName, |
|
| 813 | - EE_Base_Class $newly_saved_object, |
|
| 814 | - $current_cache_id = '' |
|
| 815 | - ) { |
|
| 816 | - // verify that incoming object is of the correct type |
|
| 817 | - $obj_class = 'EE_' . $relationName; |
|
| 818 | - if ($newly_saved_object instanceof $obj_class) { |
|
| 819 | - /* @type EE_Base_Class $newly_saved_object */ |
|
| 820 | - // now get the type of relation |
|
| 821 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 822 | - // if this is a 1:1 relationship |
|
| 823 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 824 | - // then just replace the cached object with the newly saved object |
|
| 825 | - $this->_model_relations[$relationName] = $newly_saved_object; |
|
| 826 | - return true; |
|
| 827 | - // or if it's some kind of sordid feral polyamorous relationship... |
|
| 828 | - } elseif (is_array($this->_model_relations[$relationName]) |
|
| 829 | - && isset($this->_model_relations[$relationName][$current_cache_id]) |
|
| 830 | - ) { |
|
| 831 | - // then remove the current cached item |
|
| 832 | - unset($this->_model_relations[$relationName][$current_cache_id]); |
|
| 833 | - // and cache the newly saved object using it's new ID |
|
| 834 | - $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object; |
|
| 835 | - return true; |
|
| 836 | - } |
|
| 837 | - } |
|
| 838 | - return false; |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - |
|
| 842 | - |
|
| 843 | - /** |
|
| 844 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 845 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 846 | - * |
|
| 847 | - * @param string $relationName |
|
| 848 | - * @return EE_Base_Class |
|
| 849 | - */ |
|
| 850 | - public function get_one_from_cache($relationName) |
|
| 851 | - { |
|
| 852 | - $cached_array_or_object = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] |
|
| 853 | - : null; |
|
| 854 | - if (is_array($cached_array_or_object)) { |
|
| 855 | - return array_shift($cached_array_or_object); |
|
| 856 | - } else { |
|
| 857 | - return $cached_array_or_object; |
|
| 858 | - } |
|
| 859 | - } |
|
| 860 | - |
|
| 861 | - |
|
| 862 | - |
|
| 863 | - /** |
|
| 864 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 865 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 866 | - * |
|
| 867 | - * @param string $relationName |
|
| 868 | - * @throws \EE_Error |
|
| 869 | - * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 870 | - */ |
|
| 871 | - public function get_all_from_cache($relationName) |
|
| 872 | - { |
|
| 873 | - $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array(); |
|
| 874 | - // if the result is not an array, but exists, make it an array |
|
| 875 | - $objects = is_array($objects) ? $objects : array($objects); |
|
| 876 | - //bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 877 | - //basically, if this model object was stored in the session, and these cached model objects |
|
| 878 | - //already have IDs, let's make sure they're in their model's entity mapper |
|
| 879 | - //otherwise we will have duplicates next time we call |
|
| 880 | - // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 881 | - $model = EE_Registry::instance()->load_model($relationName); |
|
| 882 | - foreach ($objects as $model_object) { |
|
| 883 | - if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 884 | - //ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 885 | - if ($model_object->ID()) { |
|
| 886 | - $model->add_to_entity_map($model_object); |
|
| 887 | - } |
|
| 888 | - } else { |
|
| 889 | - throw new EE_Error( |
|
| 890 | - sprintf( |
|
| 891 | - __( |
|
| 892 | - 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 893 | - 'event_espresso' |
|
| 894 | - ), |
|
| 895 | - $relationName, |
|
| 896 | - gettype($model_object) |
|
| 897 | - ) |
|
| 898 | - ); |
|
| 899 | - } |
|
| 900 | - } |
|
| 901 | - return $objects; |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - |
|
| 905 | - |
|
| 906 | - /** |
|
| 907 | - * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 908 | - * matching the given query conditions. |
|
| 909 | - * |
|
| 910 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 911 | - * @param int $limit How many objects to return. |
|
| 912 | - * @param array $query_params Any additional conditions on the query. |
|
| 913 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 914 | - * you can indicate just the columns you want returned |
|
| 915 | - * @return array|EE_Base_Class[] |
|
| 916 | - * @throws \EE_Error |
|
| 917 | - */ |
|
| 918 | - public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 919 | - { |
|
| 920 | - $model = $this->get_model(); |
|
| 921 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 922 | - ? $model->get_primary_key_field()->get_name() |
|
| 923 | - : $field_to_order_by; |
|
| 924 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 925 | - if (empty($field) || empty($current_value)) { |
|
| 926 | - return array(); |
|
| 927 | - } |
|
| 928 | - return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 929 | - } |
|
| 930 | - |
|
| 931 | - |
|
| 932 | - |
|
| 933 | - /** |
|
| 934 | - * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 935 | - * matching the given query conditions. |
|
| 936 | - * |
|
| 937 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 938 | - * @param int $limit How many objects to return. |
|
| 939 | - * @param array $query_params Any additional conditions on the query. |
|
| 940 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 941 | - * you can indicate just the columns you want returned |
|
| 942 | - * @return array|EE_Base_Class[] |
|
| 943 | - * @throws \EE_Error |
|
| 944 | - */ |
|
| 945 | - public function previous_x( |
|
| 946 | - $field_to_order_by = null, |
|
| 947 | - $limit = 1, |
|
| 948 | - $query_params = array(), |
|
| 949 | - $columns_to_select = null |
|
| 950 | - ) { |
|
| 951 | - $model = $this->get_model(); |
|
| 952 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 953 | - ? $model->get_primary_key_field()->get_name() |
|
| 954 | - : $field_to_order_by; |
|
| 955 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 956 | - if (empty($field) || empty($current_value)) { |
|
| 957 | - return array(); |
|
| 958 | - } |
|
| 959 | - return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 960 | - } |
|
| 961 | - |
|
| 962 | - |
|
| 963 | - |
|
| 964 | - /** |
|
| 965 | - * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 966 | - * matching the given query conditions. |
|
| 967 | - * |
|
| 968 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 969 | - * @param array $query_params Any additional conditions on the query. |
|
| 970 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 971 | - * you can indicate just the columns you want returned |
|
| 972 | - * @return array|EE_Base_Class |
|
| 973 | - * @throws \EE_Error |
|
| 974 | - */ |
|
| 975 | - public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 976 | - { |
|
| 977 | - $model = $this->get_model(); |
|
| 978 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 979 | - ? $model->get_primary_key_field()->get_name() |
|
| 980 | - : $field_to_order_by; |
|
| 981 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 982 | - if (empty($field) || empty($current_value)) { |
|
| 983 | - return array(); |
|
| 984 | - } |
|
| 985 | - return $model->next($current_value, $field, $query_params, $columns_to_select); |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - |
|
| 989 | - |
|
| 990 | - /** |
|
| 991 | - * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 992 | - * matching the given query conditions. |
|
| 993 | - * |
|
| 994 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 995 | - * @param array $query_params Any additional conditions on the query. |
|
| 996 | - * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 997 | - * you can indicate just the column you want returned |
|
| 998 | - * @return array|EE_Base_Class |
|
| 999 | - * @throws \EE_Error |
|
| 1000 | - */ |
|
| 1001 | - public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1002 | - { |
|
| 1003 | - $model = $this->get_model(); |
|
| 1004 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1005 | - ? $model->get_primary_key_field()->get_name() |
|
| 1006 | - : $field_to_order_by; |
|
| 1007 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1008 | - if (empty($field) || empty($current_value)) { |
|
| 1009 | - return array(); |
|
| 1010 | - } |
|
| 1011 | - return $model->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 1012 | - } |
|
| 1013 | - |
|
| 1014 | - |
|
| 1015 | - |
|
| 1016 | - /** |
|
| 1017 | - * Overrides parent because parent expects old models. |
|
| 1018 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 1019 | - * |
|
| 1020 | - * @param string $field_name |
|
| 1021 | - * @param mixed $field_value_from_db |
|
| 1022 | - * @throws \EE_Error |
|
| 1023 | - */ |
|
| 1024 | - public function set_from_db($field_name, $field_value_from_db) |
|
| 1025 | - { |
|
| 1026 | - $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1027 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1028 | - //you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1029 | - //eg, a CPT model object could have an entry in the posts table, but no |
|
| 1030 | - //entry in the meta table. Meaning that all its columns in the meta table |
|
| 1031 | - //are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1032 | - if ($field_value_from_db === null) { |
|
| 1033 | - if ($field_obj->is_nullable()) { |
|
| 1034 | - //if the field allows nulls, then let it be null |
|
| 1035 | - $field_value = null; |
|
| 1036 | - } else { |
|
| 1037 | - $field_value = $field_obj->get_default_value(); |
|
| 1038 | - } |
|
| 1039 | - } else { |
|
| 1040 | - $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1041 | - } |
|
| 1042 | - $this->_fields[$field_name] = $field_value; |
|
| 1043 | - $this->_clear_cached_property($field_name); |
|
| 1044 | - } |
|
| 1045 | - } |
|
| 1046 | - |
|
| 1047 | - |
|
| 1048 | - |
|
| 1049 | - /** |
|
| 1050 | - * verifies that the specified field is of the correct type |
|
| 1051 | - * |
|
| 1052 | - * @param string $field_name |
|
| 1053 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1054 | - * (in cases where the same property may be used for different outputs |
|
| 1055 | - * - i.e. datetime, money etc.) |
|
| 1056 | - * @return mixed |
|
| 1057 | - * @throws \EE_Error |
|
| 1058 | - */ |
|
| 1059 | - public function get($field_name, $extra_cache_ref = null) |
|
| 1060 | - { |
|
| 1061 | - return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1062 | - } |
|
| 1063 | - |
|
| 1064 | - |
|
| 1065 | - |
|
| 1066 | - /** |
|
| 1067 | - * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1068 | - * |
|
| 1069 | - * @param string $field_name A valid fieldname |
|
| 1070 | - * @return mixed Whatever the raw value stored on the property is. |
|
| 1071 | - * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1072 | - */ |
|
| 1073 | - public function get_raw($field_name) |
|
| 1074 | - { |
|
| 1075 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1076 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime |
|
| 1077 | - ? $this->_fields[$field_name]->format('U') |
|
| 1078 | - : $this->_fields[$field_name]; |
|
| 1079 | - } |
|
| 1080 | - |
|
| 1081 | - |
|
| 1082 | - |
|
| 1083 | - /** |
|
| 1084 | - * This is used to return the internal DateTime object used for a field that is a |
|
| 1085 | - * EE_Datetime_Field. |
|
| 1086 | - * |
|
| 1087 | - * @param string $field_name The field name retrieving the DateTime object. |
|
| 1088 | - * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1089 | - * @throws \EE_Error |
|
| 1090 | - * an error is set and false returned. If the field IS an |
|
| 1091 | - * EE_Datetime_Field and but the field value is null, then |
|
| 1092 | - * just null is returned (because that indicates that likely |
|
| 1093 | - * this field is nullable). |
|
| 1094 | - */ |
|
| 1095 | - public function get_DateTime_object($field_name) |
|
| 1096 | - { |
|
| 1097 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1098 | - if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
| 1099 | - EE_Error::add_error( |
|
| 1100 | - sprintf( |
|
| 1101 | - __( |
|
| 1102 | - 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1103 | - 'event_espresso' |
|
| 1104 | - ), |
|
| 1105 | - $field_name |
|
| 1106 | - ), |
|
| 1107 | - __FILE__, |
|
| 1108 | - __FUNCTION__, |
|
| 1109 | - __LINE__ |
|
| 1110 | - ); |
|
| 1111 | - return false; |
|
| 1112 | - } |
|
| 1113 | - return $this->_fields[$field_name]; |
|
| 1114 | - } |
|
| 1115 | - |
|
| 1116 | - |
|
| 1117 | - |
|
| 1118 | - /** |
|
| 1119 | - * To be used in template to immediately echo out the value, and format it for output. |
|
| 1120 | - * Eg, should call stripslashes and whatnot before echoing |
|
| 1121 | - * |
|
| 1122 | - * @param string $field_name the name of the field as it appears in the DB |
|
| 1123 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1124 | - * (in cases where the same property may be used for different outputs |
|
| 1125 | - * - i.e. datetime, money etc.) |
|
| 1126 | - * @return void |
|
| 1127 | - * @throws \EE_Error |
|
| 1128 | - */ |
|
| 1129 | - public function e($field_name, $extra_cache_ref = null) |
|
| 1130 | - { |
|
| 1131 | - echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - |
|
| 1135 | - |
|
| 1136 | - /** |
|
| 1137 | - * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1138 | - * can be easily used as the value of form input. |
|
| 1139 | - * |
|
| 1140 | - * @param string $field_name |
|
| 1141 | - * @return void |
|
| 1142 | - * @throws \EE_Error |
|
| 1143 | - */ |
|
| 1144 | - public function f($field_name) |
|
| 1145 | - { |
|
| 1146 | - $this->e($field_name, 'form_input'); |
|
| 1147 | - } |
|
| 1148 | - |
|
| 1149 | - |
|
| 1150 | - |
|
| 1151 | - /** |
|
| 1152 | - * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1153 | - * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1154 | - * to see what options are available. |
|
| 1155 | - * @param string $field_name |
|
| 1156 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1157 | - * (in cases where the same property may be used for different outputs |
|
| 1158 | - * - i.e. datetime, money etc.) |
|
| 1159 | - * @return mixed |
|
| 1160 | - * @throws \EE_Error |
|
| 1161 | - */ |
|
| 1162 | - public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1163 | - { |
|
| 1164 | - return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1165 | - } |
|
| 1166 | - |
|
| 1167 | - |
|
| 1168 | - |
|
| 1169 | - /** |
|
| 1170 | - * This simply returns the datetime for the given field name |
|
| 1171 | - * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1172 | - * (and the equivalent e_date, e_time, e_datetime). |
|
| 1173 | - * |
|
| 1174 | - * @access protected |
|
| 1175 | - * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1176 | - * @param string $dt_frmt valid datetime format used for date |
|
| 1177 | - * (if '' then we just use the default on the field, |
|
| 1178 | - * if NULL we use the last-used format) |
|
| 1179 | - * @param string $tm_frmt Same as above except this is for time format |
|
| 1180 | - * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1181 | - * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1182 | - * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1183 | - * if field is not a valid dtt field, or void if echoing |
|
| 1184 | - * @throws \EE_Error |
|
| 1185 | - */ |
|
| 1186 | - protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1187 | - { |
|
| 1188 | - // clear cached property |
|
| 1189 | - $this->_clear_cached_property($field_name); |
|
| 1190 | - //reset format properties because they are used in get() |
|
| 1191 | - $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1192 | - $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1193 | - if ($echo) { |
|
| 1194 | - $this->e($field_name, $date_or_time); |
|
| 1195 | - return ''; |
|
| 1196 | - } |
|
| 1197 | - return $this->get($field_name, $date_or_time); |
|
| 1198 | - } |
|
| 1199 | - |
|
| 1200 | - |
|
| 1201 | - |
|
| 1202 | - /** |
|
| 1203 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1204 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1205 | - * other echoes the pretty value for dtt) |
|
| 1206 | - * |
|
| 1207 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1208 | - * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1209 | - * @return string datetime value formatted |
|
| 1210 | - * @throws \EE_Error |
|
| 1211 | - */ |
|
| 1212 | - public function get_date($field_name, $format = '') |
|
| 1213 | - { |
|
| 1214 | - return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - |
|
| 1218 | - |
|
| 1219 | - /** |
|
| 1220 | - * @param $field_name |
|
| 1221 | - * @param string $format |
|
| 1222 | - * @throws \EE_Error |
|
| 1223 | - */ |
|
| 1224 | - public function e_date($field_name, $format = '') |
|
| 1225 | - { |
|
| 1226 | - $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1227 | - } |
|
| 1228 | - |
|
| 1229 | - |
|
| 1230 | - |
|
| 1231 | - /** |
|
| 1232 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1233 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1234 | - * other echoes the pretty value for dtt) |
|
| 1235 | - * |
|
| 1236 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1237 | - * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1238 | - * @return string datetime value formatted |
|
| 1239 | - * @throws \EE_Error |
|
| 1240 | - */ |
|
| 1241 | - public function get_time($field_name, $format = '') |
|
| 1242 | - { |
|
| 1243 | - return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1244 | - } |
|
| 1245 | - |
|
| 1246 | - |
|
| 1247 | - |
|
| 1248 | - /** |
|
| 1249 | - * @param $field_name |
|
| 1250 | - * @param string $format |
|
| 1251 | - * @throws \EE_Error |
|
| 1252 | - */ |
|
| 1253 | - public function e_time($field_name, $format = '') |
|
| 1254 | - { |
|
| 1255 | - $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1256 | - } |
|
| 1257 | - |
|
| 1258 | - |
|
| 1259 | - |
|
| 1260 | - /** |
|
| 1261 | - * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1262 | - * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1263 | - * other echoes the pretty value for dtt) |
|
| 1264 | - * |
|
| 1265 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1266 | - * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1267 | - * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1268 | - * @return string datetime value formatted |
|
| 1269 | - * @throws \EE_Error |
|
| 1270 | - */ |
|
| 1271 | - public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1272 | - { |
|
| 1273 | - return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1274 | - } |
|
| 1275 | - |
|
| 1276 | - |
|
| 1277 | - |
|
| 1278 | - /** |
|
| 1279 | - * @param string $field_name |
|
| 1280 | - * @param string $dt_frmt |
|
| 1281 | - * @param string $tm_frmt |
|
| 1282 | - * @throws \EE_Error |
|
| 1283 | - */ |
|
| 1284 | - public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1285 | - { |
|
| 1286 | - $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - |
|
| 1290 | - |
|
| 1291 | - /** |
|
| 1292 | - * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1293 | - * |
|
| 1294 | - * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1295 | - * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1296 | - * on the object will be used. |
|
| 1297 | - * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1298 | - * @throws \EE_Error |
|
| 1299 | - * field name. |
|
| 1300 | - */ |
|
| 1301 | - public function get_i18n_datetime($field_name, $format = '') |
|
| 1302 | - { |
|
| 1303 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1304 | - return date_i18n( |
|
| 1305 | - $format, |
|
| 1306 | - EEH_DTT_Helper::get_timestamp_with_offset($this->get_raw($field_name), $this->_timezone) |
|
| 1307 | - ); |
|
| 1308 | - } |
|
| 1309 | - |
|
| 1310 | - |
|
| 1311 | - |
|
| 1312 | - /** |
|
| 1313 | - * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1314 | - * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1315 | - * thrown. |
|
| 1316 | - * |
|
| 1317 | - * @param string $field_name The field name being checked |
|
| 1318 | - * @throws EE_Error |
|
| 1319 | - * @return EE_Datetime_Field |
|
| 1320 | - */ |
|
| 1321 | - protected function _get_dtt_field_settings($field_name) |
|
| 1322 | - { |
|
| 1323 | - $field = $this->get_model()->field_settings_for($field_name); |
|
| 1324 | - //check if field is dtt |
|
| 1325 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1326 | - return $field; |
|
| 1327 | - } else { |
|
| 1328 | - throw new EE_Error(sprintf(__('The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
| 1329 | - 'event_espresso'), $field_name, self::_get_model_classname(get_class($this)))); |
|
| 1330 | - } |
|
| 1331 | - } |
|
| 1332 | - |
|
| 1333 | - |
|
| 1334 | - |
|
| 1335 | - |
|
| 1336 | - /** |
|
| 1337 | - * NOTE ABOUT BELOW: |
|
| 1338 | - * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1339 | - * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1340 | - * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1341 | - * method and make sure you send the entire datetime value for setting. |
|
| 1342 | - */ |
|
| 1343 | - /** |
|
| 1344 | - * sets the time on a datetime property |
|
| 1345 | - * |
|
| 1346 | - * @access protected |
|
| 1347 | - * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1348 | - * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1349 | - * @throws \EE_Error |
|
| 1350 | - */ |
|
| 1351 | - protected function _set_time_for($time, $fieldname) |
|
| 1352 | - { |
|
| 1353 | - $this->_set_date_time('T', $time, $fieldname); |
|
| 1354 | - } |
|
| 1355 | - |
|
| 1356 | - |
|
| 1357 | - |
|
| 1358 | - /** |
|
| 1359 | - * sets the date on a datetime property |
|
| 1360 | - * |
|
| 1361 | - * @access protected |
|
| 1362 | - * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1363 | - * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1364 | - * @throws \EE_Error |
|
| 1365 | - */ |
|
| 1366 | - protected function _set_date_for($date, $fieldname) |
|
| 1367 | - { |
|
| 1368 | - $this->_set_date_time('D', $date, $fieldname); |
|
| 1369 | - } |
|
| 1370 | - |
|
| 1371 | - |
|
| 1372 | - |
|
| 1373 | - /** |
|
| 1374 | - * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1375 | - * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1376 | - * |
|
| 1377 | - * @access protected |
|
| 1378 | - * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1379 | - * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1380 | - * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1381 | - * EE_Datetime_Field property) |
|
| 1382 | - * @throws \EE_Error |
|
| 1383 | - */ |
|
| 1384 | - protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1385 | - { |
|
| 1386 | - $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1387 | - $field->set_timezone($this->_timezone); |
|
| 1388 | - $field->set_date_format($this->_dt_frmt); |
|
| 1389 | - $field->set_time_format($this->_tm_frmt); |
|
| 1390 | - switch ($what) { |
|
| 1391 | - case 'T' : |
|
| 1392 | - $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time( |
|
| 1393 | - $datetime_value, |
|
| 1394 | - $this->_fields[$fieldname] |
|
| 1395 | - ); |
|
| 1396 | - break; |
|
| 1397 | - case 'D' : |
|
| 1398 | - $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date( |
|
| 1399 | - $datetime_value, |
|
| 1400 | - $this->_fields[$fieldname] |
|
| 1401 | - ); |
|
| 1402 | - break; |
|
| 1403 | - case 'B' : |
|
| 1404 | - $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value); |
|
| 1405 | - break; |
|
| 1406 | - } |
|
| 1407 | - $this->_clear_cached_property($fieldname); |
|
| 1408 | - } |
|
| 1409 | - |
|
| 1410 | - |
|
| 1411 | - |
|
| 1412 | - /** |
|
| 1413 | - * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1414 | - * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1415 | - * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1416 | - * that could lead to some unexpected results! |
|
| 1417 | - * |
|
| 1418 | - * @access public |
|
| 1419 | - * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1420 | - * value being returned. |
|
| 1421 | - * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1422 | - * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1423 | - * @param string $prepend You can include something to prepend on the timestamp |
|
| 1424 | - * @param string $append You can include something to append on the timestamp |
|
| 1425 | - * @throws EE_Error |
|
| 1426 | - * @return string timestamp |
|
| 1427 | - */ |
|
| 1428 | - public function display_in_my_timezone( |
|
| 1429 | - $field_name, |
|
| 1430 | - $callback = 'get_datetime', |
|
| 1431 | - $args = null, |
|
| 1432 | - $prepend = '', |
|
| 1433 | - $append = '' |
|
| 1434 | - ) { |
|
| 1435 | - $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1436 | - if ($timezone === $this->_timezone) { |
|
| 1437 | - return ''; |
|
| 1438 | - } |
|
| 1439 | - $original_timezone = $this->_timezone; |
|
| 1440 | - $this->set_timezone($timezone); |
|
| 1441 | - $fn = (array)$field_name; |
|
| 1442 | - $args = array_merge($fn, (array)$args); |
|
| 1443 | - if ( ! method_exists($this, $callback)) { |
|
| 1444 | - throw new EE_Error( |
|
| 1445 | - sprintf( |
|
| 1446 | - __( |
|
| 1447 | - 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1448 | - 'event_espresso' |
|
| 1449 | - ), |
|
| 1450 | - $callback |
|
| 1451 | - ) |
|
| 1452 | - ); |
|
| 1453 | - } |
|
| 1454 | - $args = (array)$args; |
|
| 1455 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1456 | - $this->set_timezone($original_timezone); |
|
| 1457 | - return $return; |
|
| 1458 | - } |
|
| 1459 | - |
|
| 1460 | - |
|
| 1461 | - |
|
| 1462 | - /** |
|
| 1463 | - * Deletes this model object. |
|
| 1464 | - * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1465 | - * override |
|
| 1466 | - * `EE_Base_Class::_delete` NOT this class. |
|
| 1467 | - * |
|
| 1468 | - * @return boolean | int |
|
| 1469 | - * @throws \EE_Error |
|
| 1470 | - */ |
|
| 1471 | - public function delete() |
|
| 1472 | - { |
|
| 1473 | - /** |
|
| 1474 | - * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1475 | - * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1476 | - * should be aware that `_delete` may not always result in a permanent delete. For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1477 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1478 | - * |
|
| 1479 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1480 | - */ |
|
| 1481 | - do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1482 | - $result = $this->_delete(); |
|
| 1483 | - /** |
|
| 1484 | - * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1485 | - * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1486 | - * should be aware that `_delete` may not always result in a permanent delete. For example `EE_Soft_Base_Class::_delete` |
|
| 1487 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1488 | - * |
|
| 1489 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1490 | - * @param boolean $result |
|
| 1491 | - */ |
|
| 1492 | - do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1493 | - return $result; |
|
| 1494 | - } |
|
| 1495 | - |
|
| 1496 | - |
|
| 1497 | - |
|
| 1498 | - /** |
|
| 1499 | - * Calls the specific delete method for the instantiated class. |
|
| 1500 | - * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1501 | - * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1502 | - * `EE_Base_Class::delete` |
|
| 1503 | - * |
|
| 1504 | - * @return bool|int |
|
| 1505 | - * @throws \EE_Error |
|
| 1506 | - */ |
|
| 1507 | - protected function _delete() |
|
| 1508 | - { |
|
| 1509 | - return $this->delete_permanently(); |
|
| 1510 | - } |
|
| 1511 | - |
|
| 1512 | - |
|
| 1513 | - |
|
| 1514 | - /** |
|
| 1515 | - * Deletes this model object permanently from db (but keep in mind related models my block the delete and return an |
|
| 1516 | - * error) |
|
| 1517 | - * |
|
| 1518 | - * @return bool | int |
|
| 1519 | - * @throws \EE_Error |
|
| 1520 | - */ |
|
| 1521 | - public function delete_permanently() |
|
| 1522 | - { |
|
| 1523 | - /** |
|
| 1524 | - * Called just before HARD deleting a model object |
|
| 1525 | - * |
|
| 1526 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1527 | - */ |
|
| 1528 | - do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1529 | - $model = $this->get_model(); |
|
| 1530 | - $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1531 | - $this->refresh_cache_of_related_objects(); |
|
| 1532 | - /** |
|
| 1533 | - * Called just after HARD deleting a model object |
|
| 1534 | - * |
|
| 1535 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1536 | - * @param boolean $result |
|
| 1537 | - */ |
|
| 1538 | - do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1539 | - return $result; |
|
| 1540 | - } |
|
| 1541 | - |
|
| 1542 | - |
|
| 1543 | - |
|
| 1544 | - /** |
|
| 1545 | - * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1546 | - * related model objects |
|
| 1547 | - * |
|
| 1548 | - * @throws \EE_Error |
|
| 1549 | - */ |
|
| 1550 | - public function refresh_cache_of_related_objects() |
|
| 1551 | - { |
|
| 1552 | - $model = $this->get_model(); |
|
| 1553 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1554 | - if ( ! empty($this->_model_relations[$relation_name])) { |
|
| 1555 | - $related_objects = $this->_model_relations[$relation_name]; |
|
| 1556 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1557 | - //this relation only stores a single model object, not an array |
|
| 1558 | - //but let's make it consistent |
|
| 1559 | - $related_objects = array($related_objects); |
|
| 1560 | - } |
|
| 1561 | - foreach ($related_objects as $related_object) { |
|
| 1562 | - //only refresh their cache if they're in memory |
|
| 1563 | - if ($related_object instanceof EE_Base_Class) { |
|
| 1564 | - $related_object->clear_cache($model->get_this_model_name(), $this); |
|
| 1565 | - } |
|
| 1566 | - } |
|
| 1567 | - } |
|
| 1568 | - } |
|
| 1569 | - } |
|
| 1570 | - |
|
| 1571 | - |
|
| 1572 | - |
|
| 1573 | - /** |
|
| 1574 | - * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1575 | - * object just before saving. |
|
| 1576 | - * |
|
| 1577 | - * @access public |
|
| 1578 | - * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1579 | - * if provided during the save() method (often client code will change the fields' |
|
| 1580 | - * values before calling save) |
|
| 1581 | - * @throws \EE_Error |
|
| 1582 | - * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1583 | - * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1584 | - */ |
|
| 1585 | - public function save($set_cols_n_values = array()) |
|
| 1586 | - { |
|
| 1587 | - $model = $this->get_model(); |
|
| 1588 | - /** |
|
| 1589 | - * Filters the fields we're about to save on the model object |
|
| 1590 | - * |
|
| 1591 | - * @param array $set_cols_n_values |
|
| 1592 | - * @param EE_Base_Class $model_object |
|
| 1593 | - */ |
|
| 1594 | - $set_cols_n_values = (array)apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values, |
|
| 1595 | - $this); |
|
| 1596 | - //set attributes as provided in $set_cols_n_values |
|
| 1597 | - foreach ($set_cols_n_values as $column => $value) { |
|
| 1598 | - $this->set($column, $value); |
|
| 1599 | - } |
|
| 1600 | - // no changes ? then don't do anything |
|
| 1601 | - if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1602 | - return 0; |
|
| 1603 | - } |
|
| 1604 | - /** |
|
| 1605 | - * Saving a model object. |
|
| 1606 | - * Before we perform a save, this action is fired. |
|
| 1607 | - * |
|
| 1608 | - * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1609 | - */ |
|
| 1610 | - do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1611 | - if ( ! $this->allow_persist()) { |
|
| 1612 | - return 0; |
|
| 1613 | - } |
|
| 1614 | - //now get current attribute values |
|
| 1615 | - $save_cols_n_values = $this->_fields; |
|
| 1616 | - //if the object already has an ID, update it. Otherwise, insert it |
|
| 1617 | - //also: change the assumption about values passed to the model NOT being prepare dby the model object. They have been |
|
| 1618 | - $old_assumption_concerning_value_preparation = $model |
|
| 1619 | - ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1620 | - $model->assume_values_already_prepared_by_model_object(true); |
|
| 1621 | - //does this model have an autoincrement PK? |
|
| 1622 | - if ($model->has_primary_key_field()) { |
|
| 1623 | - if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1624 | - //ok check if it's set, if so: update; if not, insert |
|
| 1625 | - if ( ! empty($save_cols_n_values[$model->primary_key_name()])) { |
|
| 1626 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1627 | - } else { |
|
| 1628 | - unset($save_cols_n_values[$model->primary_key_name()]); |
|
| 1629 | - $results = $model->insert($save_cols_n_values); |
|
| 1630 | - if ($results) { |
|
| 1631 | - //if successful, set the primary key |
|
| 1632 | - //but don't use the normal SET method, because it will check if |
|
| 1633 | - //an item with the same ID exists in the mapper & db, then |
|
| 1634 | - //will find it in the db (because we just added it) and THAT object |
|
| 1635 | - //will get added to the mapper before we can add this one! |
|
| 1636 | - //but if we just avoid using the SET method, all that headache can be avoided |
|
| 1637 | - $pk_field_name = $model->primary_key_name(); |
|
| 1638 | - $this->_fields[$pk_field_name] = $results; |
|
| 1639 | - $this->_clear_cached_property($pk_field_name); |
|
| 1640 | - $model->add_to_entity_map($this); |
|
| 1641 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1642 | - } |
|
| 1643 | - } |
|
| 1644 | - } else {//PK is NOT auto-increment |
|
| 1645 | - //so check if one like it already exists in the db |
|
| 1646 | - if ($model->exists_by_ID($this->ID())) { |
|
| 1647 | - if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1648 | - throw new EE_Error( |
|
| 1649 | - sprintf( |
|
| 1650 | - __('Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
| 1651 | - 'event_espresso'), |
|
| 1652 | - get_class($this), |
|
| 1653 | - get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1654 | - get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1655 | - '<br />' |
|
| 1656 | - ) |
|
| 1657 | - ); |
|
| 1658 | - } |
|
| 1659 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1660 | - } else { |
|
| 1661 | - $results = $model->insert($save_cols_n_values); |
|
| 1662 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1663 | - } |
|
| 1664 | - } |
|
| 1665 | - } else {//there is NO primary key |
|
| 1666 | - $already_in_db = false; |
|
| 1667 | - foreach ($model->unique_indexes() as $index) { |
|
| 1668 | - $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1669 | - if ($model->exists(array($uniqueness_where_params))) { |
|
| 1670 | - $already_in_db = true; |
|
| 1671 | - } |
|
| 1672 | - } |
|
| 1673 | - if ($already_in_db) { |
|
| 1674 | - $combined_pk_fields_n_values = array_intersect_key($save_cols_n_values, |
|
| 1675 | - $model->get_combined_primary_key_fields()); |
|
| 1676 | - $results = $model->update($save_cols_n_values, $combined_pk_fields_n_values); |
|
| 1677 | - } else { |
|
| 1678 | - $results = $model->insert($save_cols_n_values); |
|
| 1679 | - } |
|
| 1680 | - } |
|
| 1681 | - //restore the old assumption about values being prepared by the model object |
|
| 1682 | - $model |
|
| 1683 | - ->assume_values_already_prepared_by_model_object($old_assumption_concerning_value_preparation); |
|
| 1684 | - /** |
|
| 1685 | - * After saving the model object this action is called |
|
| 1686 | - * |
|
| 1687 | - * @param EE_Base_Class $model_object which was just saved |
|
| 1688 | - * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1689 | - * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1690 | - */ |
|
| 1691 | - do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1692 | - $this->_has_changes = false; |
|
| 1693 | - return $results; |
|
| 1694 | - } |
|
| 1695 | - |
|
| 1696 | - |
|
| 1697 | - |
|
| 1698 | - /** |
|
| 1699 | - * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1700 | - * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1701 | - * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1702 | - * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1703 | - * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
|
| 1704 | - * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1705 | - * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1706 | - * |
|
| 1707 | - * @return void |
|
| 1708 | - * @throws \EE_Error |
|
| 1709 | - */ |
|
| 1710 | - protected function _update_cached_related_model_objs_fks() |
|
| 1711 | - { |
|
| 1712 | - $model = $this->get_model(); |
|
| 1713 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1714 | - if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1715 | - foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1716 | - $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1717 | - $model->get_this_model_name() |
|
| 1718 | - ); |
|
| 1719 | - $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1720 | - if ($related_model_obj_in_cache->ID()) { |
|
| 1721 | - $related_model_obj_in_cache->save(); |
|
| 1722 | - } |
|
| 1723 | - } |
|
| 1724 | - } |
|
| 1725 | - } |
|
| 1726 | - } |
|
| 1727 | - |
|
| 1728 | - |
|
| 1729 | - |
|
| 1730 | - /** |
|
| 1731 | - * Saves this model object and its NEW cached relations to the database. |
|
| 1732 | - * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1733 | - * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1734 | - * because otherwise, there's a potential for infinite looping of saving |
|
| 1735 | - * Saves the cached related model objects, and ensures the relation between them |
|
| 1736 | - * and this object and properly setup |
|
| 1737 | - * |
|
| 1738 | - * @return int ID of new model object on save; 0 on failure+ |
|
| 1739 | - * @throws \EE_Error |
|
| 1740 | - */ |
|
| 1741 | - public function save_new_cached_related_model_objs() |
|
| 1742 | - { |
|
| 1743 | - //make sure this has been saved |
|
| 1744 | - if ( ! $this->ID()) { |
|
| 1745 | - $id = $this->save(); |
|
| 1746 | - } else { |
|
| 1747 | - $id = $this->ID(); |
|
| 1748 | - } |
|
| 1749 | - //now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1750 | - foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1751 | - if ($this->_model_relations[$relationName]) { |
|
| 1752 | - //is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1753 | - //or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1754 | - if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1755 | - //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1756 | - //but ONLY if it DOES NOT exist in the DB |
|
| 1757 | - /* @var $related_model_obj EE_Base_Class */ |
|
| 1758 | - $related_model_obj = $this->_model_relations[$relationName]; |
|
| 1759 | - // if( ! $related_model_obj->ID()){ |
|
| 1760 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1761 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1762 | - // } |
|
| 1763 | - } else { |
|
| 1764 | - foreach ($this->_model_relations[$relationName] as $related_model_obj) { |
|
| 1765 | - //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1766 | - //but ONLY if it DOES NOT exist in the DB |
|
| 1767 | - // if( ! $related_model_obj->ID()){ |
|
| 1768 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1769 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1770 | - // } |
|
| 1771 | - } |
|
| 1772 | - } |
|
| 1773 | - } |
|
| 1774 | - } |
|
| 1775 | - return $id; |
|
| 1776 | - } |
|
| 1777 | - |
|
| 1778 | - |
|
| 1779 | - |
|
| 1780 | - /** |
|
| 1781 | - * for getting a model while instantiated. |
|
| 1782 | - * |
|
| 1783 | - * @return \EEM_Base | \EEM_CPT_Base |
|
| 1784 | - */ |
|
| 1785 | - public function get_model() |
|
| 1786 | - { |
|
| 1787 | - if( ! $this->_model){ |
|
| 1788 | - $modelName = self::_get_model_classname(get_class($this)); |
|
| 1789 | - $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 1790 | - } else { |
|
| 1791 | - $this->_model->set_timezone($this->_timezone); |
|
| 1792 | - } |
|
| 1793 | - |
|
| 1794 | - return $this->_model; |
|
| 1795 | - } |
|
| 1796 | - |
|
| 1797 | - |
|
| 1798 | - |
|
| 1799 | - /** |
|
| 1800 | - * @param $props_n_values |
|
| 1801 | - * @param $classname |
|
| 1802 | - * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 1803 | - * @throws \EE_Error |
|
| 1804 | - */ |
|
| 1805 | - protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 1806 | - { |
|
| 1807 | - //TODO: will not work for Term_Relationships because they have no PK! |
|
| 1808 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1809 | - if (array_key_exists($primary_id_ref, $props_n_values) && ! empty($props_n_values[$primary_id_ref])) { |
|
| 1810 | - $id = $props_n_values[$primary_id_ref]; |
|
| 1811 | - return self::_get_model($classname)->get_from_entity_map($id); |
|
| 1812 | - } |
|
| 1813 | - return false; |
|
| 1814 | - } |
|
| 1815 | - |
|
| 1816 | - |
|
| 1817 | - |
|
| 1818 | - /** |
|
| 1819 | - * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 1820 | - * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 1821 | - * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
|
| 1822 | - * we return false. |
|
| 1823 | - * |
|
| 1824 | - * @param array $props_n_values incoming array of properties and their values |
|
| 1825 | - * @param string $classname the classname of the child class |
|
| 1826 | - * @param null $timezone |
|
| 1827 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 1828 | - * date_format and the second value is the time format |
|
| 1829 | - * @return mixed (EE_Base_Class|bool) |
|
| 1830 | - * @throws \EE_Error |
|
| 1831 | - */ |
|
| 1832 | - protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 1833 | - { |
|
| 1834 | - $existing = null; |
|
| 1835 | - $model = self::_get_model($classname, $timezone); |
|
| 1836 | - if ($model->has_primary_key_field()) { |
|
| 1837 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1838 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 1839 | - && ! empty($props_n_values[$primary_id_ref]) |
|
| 1840 | - ) { |
|
| 1841 | - $existing = $model->get_one_by_ID( |
|
| 1842 | - $props_n_values[$primary_id_ref] |
|
| 1843 | - ); |
|
| 1844 | - } |
|
| 1845 | - } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 1846 | - //no primary key on this model, but there's still a matching item in the DB |
|
| 1847 | - $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 1848 | - self::_get_model($classname, $timezone)->get_index_primary_key_string($props_n_values) |
|
| 1849 | - ); |
|
| 1850 | - } |
|
| 1851 | - if ($existing) { |
|
| 1852 | - //set date formats if present before setting values |
|
| 1853 | - if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 1854 | - $existing->set_date_format($date_formats[0]); |
|
| 1855 | - $existing->set_time_format($date_formats[1]); |
|
| 1856 | - } else { |
|
| 1857 | - //set default formats for date and time |
|
| 1858 | - $existing->set_date_format(get_option('date_format')); |
|
| 1859 | - $existing->set_time_format(get_option('time_format')); |
|
| 1860 | - } |
|
| 1861 | - foreach ($props_n_values as $property => $field_value) { |
|
| 1862 | - $existing->set($property, $field_value); |
|
| 1863 | - } |
|
| 1864 | - return $existing; |
|
| 1865 | - } else { |
|
| 1866 | - return false; |
|
| 1867 | - } |
|
| 1868 | - } |
|
| 1869 | - |
|
| 1870 | - |
|
| 1871 | - |
|
| 1872 | - /** |
|
| 1873 | - * Gets the EEM_*_Model for this class |
|
| 1874 | - * |
|
| 1875 | - * @access public now, as this is more convenient |
|
| 1876 | - * @param $classname |
|
| 1877 | - * @param null $timezone |
|
| 1878 | - * @throws EE_Error |
|
| 1879 | - * @return EEM_Base |
|
| 1880 | - */ |
|
| 1881 | - protected static function _get_model($classname, $timezone = null) |
|
| 1882 | - { |
|
| 1883 | - //find model for this class |
|
| 1884 | - if ( ! $classname) { |
|
| 1885 | - throw new EE_Error( |
|
| 1886 | - sprintf( |
|
| 1887 | - __( |
|
| 1888 | - "What were you thinking calling _get_model(%s)?? You need to specify the class name", |
|
| 1889 | - "event_espresso" |
|
| 1890 | - ), |
|
| 1891 | - $classname |
|
| 1892 | - ) |
|
| 1893 | - ); |
|
| 1894 | - } |
|
| 1895 | - $modelName = self::_get_model_classname($classname); |
|
| 1896 | - return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 1897 | - } |
|
| 1898 | - |
|
| 1899 | - |
|
| 1900 | - |
|
| 1901 | - /** |
|
| 1902 | - * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 1903 | - * |
|
| 1904 | - * @param string $model_classname |
|
| 1905 | - * @param null $timezone |
|
| 1906 | - * @return EEM_Base |
|
| 1907 | - */ |
|
| 1908 | - protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 1909 | - { |
|
| 1910 | - $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 1911 | - $model = EE_Registry::instance()->load_model($model_classname); |
|
| 1912 | - $model->set_timezone($timezone); |
|
| 1913 | - return $model; |
|
| 1914 | - } |
|
| 1915 | - |
|
| 1916 | - |
|
| 1917 | - |
|
| 1918 | - /** |
|
| 1919 | - * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 1920 | - * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 1921 | - * |
|
| 1922 | - * @param null $model_name |
|
| 1923 | - * @return string like EEM_Attendee |
|
| 1924 | - */ |
|
| 1925 | - private static function _get_model_classname($model_name = null) |
|
| 1926 | - { |
|
| 1927 | - if (strpos($model_name, "EE_") === 0) { |
|
| 1928 | - $model_classname = str_replace("EE_", "EEM_", $model_name); |
|
| 1929 | - } else { |
|
| 1930 | - $model_classname = "EEM_" . $model_name; |
|
| 1931 | - } |
|
| 1932 | - return $model_classname; |
|
| 1933 | - } |
|
| 1934 | - |
|
| 1935 | - |
|
| 1936 | - |
|
| 1937 | - /** |
|
| 1938 | - * returns the name of the primary key attribute |
|
| 1939 | - * |
|
| 1940 | - * @param null $classname |
|
| 1941 | - * @throws EE_Error |
|
| 1942 | - * @return string |
|
| 1943 | - */ |
|
| 1944 | - protected static function _get_primary_key_name($classname = null) |
|
| 1945 | - { |
|
| 1946 | - if ( ! $classname) { |
|
| 1947 | - throw new EE_Error( |
|
| 1948 | - sprintf( |
|
| 1949 | - __("What were you thinking calling _get_primary_key_name(%s)", "event_espresso"), |
|
| 1950 | - $classname |
|
| 1951 | - ) |
|
| 1952 | - ); |
|
| 1953 | - } |
|
| 1954 | - return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 1955 | - } |
|
| 1956 | - |
|
| 1957 | - |
|
| 1958 | - |
|
| 1959 | - /** |
|
| 1960 | - * Gets the value of the primary key. |
|
| 1961 | - * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 1962 | - * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 1963 | - * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 1964 | - * |
|
| 1965 | - * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 1966 | - * @throws \EE_Error |
|
| 1967 | - */ |
|
| 1968 | - public function ID() |
|
| 1969 | - { |
|
| 1970 | - $model = $this->get_model(); |
|
| 1971 | - //now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 1972 | - if ($model->has_primary_key_field()) { |
|
| 1973 | - return $this->_fields[$model->primary_key_name()]; |
|
| 1974 | - } else { |
|
| 1975 | - return $model->get_index_primary_key_string($this->_fields); |
|
| 1976 | - } |
|
| 1977 | - } |
|
| 1978 | - |
|
| 1979 | - |
|
| 1980 | - |
|
| 1981 | - /** |
|
| 1982 | - * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 1983 | - * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 1984 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 1985 | - * |
|
| 1986 | - * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 1987 | - * @param string $relationName eg 'Events','Question',etc. |
|
| 1988 | - * an attendee to a group, you also want to specify which role they |
|
| 1989 | - * will have in that group. So you would use this parameter to |
|
| 1990 | - * specify array('role-column-name'=>'role-id') |
|
| 1991 | - * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 1992 | - * allow you to further constrict the relation to being added. |
|
| 1993 | - * However, keep in mind that the columns (keys) given must match a |
|
| 1994 | - * column on the JOIN table and currently only the HABTM models |
|
| 1995 | - * accept these additional conditions. Also remember that if an |
|
| 1996 | - * exact match isn't found for these extra cols/val pairs, then a |
|
| 1997 | - * NEW row is created in the join table. |
|
| 1998 | - * @param null $cache_id |
|
| 1999 | - * @throws EE_Error |
|
| 2000 | - * @return EE_Base_Class the object the relation was added to |
|
| 2001 | - */ |
|
| 2002 | - public function _add_relation_to( |
|
| 2003 | - $otherObjectModelObjectOrID, |
|
| 2004 | - $relationName, |
|
| 2005 | - $extra_join_model_fields_n_values = array(), |
|
| 2006 | - $cache_id = null |
|
| 2007 | - ) { |
|
| 2008 | - $model = $this->get_model(); |
|
| 2009 | - //if this thing exists in the DB, save the relation to the DB |
|
| 2010 | - if ($this->ID()) { |
|
| 2011 | - $otherObject = $model |
|
| 2012 | - ->add_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 2013 | - $extra_join_model_fields_n_values); |
|
| 2014 | - //clear cache so future get_many_related and get_first_related() return new results. |
|
| 2015 | - $this->clear_cache($relationName, $otherObject, true); |
|
| 2016 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2017 | - $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2018 | - } |
|
| 2019 | - } else { |
|
| 2020 | - //this thing doesn't exist in the DB, so just cache it |
|
| 2021 | - if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2022 | - throw new EE_Error(sprintf( |
|
| 2023 | - __('Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
| 2024 | - 'event_espresso'), |
|
| 2025 | - $otherObjectModelObjectOrID, |
|
| 2026 | - get_class($this) |
|
| 2027 | - )); |
|
| 2028 | - } else { |
|
| 2029 | - $otherObject = $otherObjectModelObjectOrID; |
|
| 2030 | - } |
|
| 2031 | - $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2032 | - } |
|
| 2033 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2034 | - //fix the reciprocal relation too |
|
| 2035 | - if ($otherObject->ID()) { |
|
| 2036 | - //its saved so assumed relations exist in the DB, so we can just |
|
| 2037 | - //clear the cache so future queries use the updated info in the DB |
|
| 2038 | - $otherObject->clear_cache($model->get_this_model_name(), null, true); |
|
| 2039 | - } else { |
|
| 2040 | - //it's not saved, so it caches relations like this |
|
| 2041 | - $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2042 | - } |
|
| 2043 | - } |
|
| 2044 | - return $otherObject; |
|
| 2045 | - } |
|
| 2046 | - |
|
| 2047 | - |
|
| 2048 | - |
|
| 2049 | - /** |
|
| 2050 | - * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2051 | - * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2052 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2053 | - * from the cache |
|
| 2054 | - * |
|
| 2055 | - * @param mixed $otherObjectModelObjectOrID |
|
| 2056 | - * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2057 | - * to the DB yet |
|
| 2058 | - * @param string $relationName |
|
| 2059 | - * @param array $where_query |
|
| 2060 | - * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2061 | - * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2062 | - * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2063 | - * remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is |
|
| 2064 | - * created in the join table. |
|
| 2065 | - * @return EE_Base_Class the relation was removed from |
|
| 2066 | - * @throws \EE_Error |
|
| 2067 | - */ |
|
| 2068 | - public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2069 | - { |
|
| 2070 | - if ($this->ID()) { |
|
| 2071 | - //if this exists in the DB, save the relation change to the DB too |
|
| 2072 | - $otherObject = $this->get_model() |
|
| 2073 | - ->remove_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 2074 | - $where_query); |
|
| 2075 | - $this->clear_cache($relationName, $otherObject); |
|
| 2076 | - } else { |
|
| 2077 | - //this doesn't exist in the DB, just remove it from the cache |
|
| 2078 | - $otherObject = $this->clear_cache($relationName, $otherObjectModelObjectOrID); |
|
| 2079 | - } |
|
| 2080 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2081 | - $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2082 | - } |
|
| 2083 | - return $otherObject; |
|
| 2084 | - } |
|
| 2085 | - |
|
| 2086 | - |
|
| 2087 | - |
|
| 2088 | - /** |
|
| 2089 | - * Removes ALL the related things for the $relationName. |
|
| 2090 | - * |
|
| 2091 | - * @param string $relationName |
|
| 2092 | - * @param array $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 2093 | - * @return EE_Base_Class |
|
| 2094 | - * @throws \EE_Error |
|
| 2095 | - */ |
|
| 2096 | - public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2097 | - { |
|
| 2098 | - if ($this->ID()) { |
|
| 2099 | - //if this exists in the DB, save the relation change to the DB too |
|
| 2100 | - $otherObjects = $this->get_model()->remove_relations($this, $relationName, $where_query_params); |
|
| 2101 | - $this->clear_cache($relationName, null, true); |
|
| 2102 | - } else { |
|
| 2103 | - //this doesn't exist in the DB, just remove it from the cache |
|
| 2104 | - $otherObjects = $this->clear_cache($relationName, null, true); |
|
| 2105 | - } |
|
| 2106 | - if (is_array($otherObjects)) { |
|
| 2107 | - foreach ($otherObjects as $otherObject) { |
|
| 2108 | - $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2109 | - } |
|
| 2110 | - } |
|
| 2111 | - return $otherObjects; |
|
| 2112 | - } |
|
| 2113 | - |
|
| 2114 | - |
|
| 2115 | - |
|
| 2116 | - /** |
|
| 2117 | - * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2118 | - * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2119 | - * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2120 | - * because we want to get even deleted items etc. |
|
| 2121 | - * |
|
| 2122 | - * @param string $relationName key in the model's _model_relations array |
|
| 2123 | - * @param array $query_params like EEM_Base::get_all |
|
| 2124 | - * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary keys |
|
| 2125 | - * @throws \EE_Error |
|
| 2126 | - * or might not be saved yet. Consider using EEM_Base::get_IDs() on these results if |
|
| 2127 | - * you want IDs |
|
| 2128 | - */ |
|
| 2129 | - public function get_many_related($relationName, $query_params = array()) |
|
| 2130 | - { |
|
| 2131 | - if ($this->ID()) { |
|
| 2132 | - //this exists in the DB, so get the related things from either the cache or the DB |
|
| 2133 | - //if there are query parameters, forget about caching the related model objects. |
|
| 2134 | - if ($query_params) { |
|
| 2135 | - $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2136 | - } else { |
|
| 2137 | - //did we already cache the result of this query? |
|
| 2138 | - $cached_results = $this->get_all_from_cache($relationName); |
|
| 2139 | - if ( ! $cached_results) { |
|
| 2140 | - $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2141 | - //if no query parameters were passed, then we got all the related model objects |
|
| 2142 | - //for that relation. We can cache them then. |
|
| 2143 | - foreach ($related_model_objects as $related_model_object) { |
|
| 2144 | - $this->cache($relationName, $related_model_object); |
|
| 2145 | - } |
|
| 2146 | - } else { |
|
| 2147 | - $related_model_objects = $cached_results; |
|
| 2148 | - } |
|
| 2149 | - } |
|
| 2150 | - } else { |
|
| 2151 | - //this doesn't exist in the DB, so just get the related things from the cache |
|
| 2152 | - $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2153 | - } |
|
| 2154 | - return $related_model_objects; |
|
| 2155 | - } |
|
| 2156 | - |
|
| 2157 | - |
|
| 2158 | - |
|
| 2159 | - /** |
|
| 2160 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2161 | - * unless otherwise specified in the $query_params |
|
| 2162 | - * |
|
| 2163 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2164 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2165 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2166 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2167 | - * that by the setting $distinct to TRUE; |
|
| 2168 | - * @return int |
|
| 2169 | - */ |
|
| 2170 | - public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2171 | - { |
|
| 2172 | - return $this->get_model()->count_related($this, $relation_name, $query_params, $field_to_count, $distinct); |
|
| 2173 | - } |
|
| 2174 | - |
|
| 2175 | - |
|
| 2176 | - |
|
| 2177 | - /** |
|
| 2178 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2179 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2180 | - * |
|
| 2181 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2182 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2183 | - * @param string $field_to_sum name of field to count by. |
|
| 2184 | - * By default, uses primary key (which doesn't make much sense, so you should probably |
|
| 2185 | - * change it) |
|
| 2186 | - * @return int |
|
| 2187 | - */ |
|
| 2188 | - public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2189 | - { |
|
| 2190 | - return $this->get_model()->sum_related($this, $relation_name, $query_params, $field_to_sum); |
|
| 2191 | - } |
|
| 2192 | - |
|
| 2193 | - |
|
| 2194 | - |
|
| 2195 | - /** |
|
| 2196 | - * Gets the first (ie, one) related model object of the specified type. |
|
| 2197 | - * |
|
| 2198 | - * @param string $relationName key in the model's _model_relations array |
|
| 2199 | - * @param array $query_params like EEM_Base::get_all |
|
| 2200 | - * @return EE_Base_Class (not an array, a single object) |
|
| 2201 | - * @throws \EE_Error |
|
| 2202 | - */ |
|
| 2203 | - public function get_first_related($relationName, $query_params = array()) |
|
| 2204 | - { |
|
| 2205 | - $model = $this->get_model(); |
|
| 2206 | - if ($this->ID()) {//this exists in the DB, get from the cache OR the DB |
|
| 2207 | - //if they've provided some query parameters, don't bother trying to cache the result |
|
| 2208 | - //also make sure we're not caching the result of get_first_related |
|
| 2209 | - //on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2210 | - if ($query_params |
|
| 2211 | - || ! $model->related_settings_for($relationName) |
|
| 2212 | - instanceof |
|
| 2213 | - EE_Belongs_To_Relation |
|
| 2214 | - ) { |
|
| 2215 | - $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2216 | - } else { |
|
| 2217 | - //first, check if we've already cached the result of this query |
|
| 2218 | - $cached_result = $this->get_one_from_cache($relationName); |
|
| 2219 | - if ( ! $cached_result) { |
|
| 2220 | - $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2221 | - $this->cache($relationName, $related_model_object); |
|
| 2222 | - } else { |
|
| 2223 | - $related_model_object = $cached_result; |
|
| 2224 | - } |
|
| 2225 | - } |
|
| 2226 | - } else { |
|
| 2227 | - $related_model_object = null; |
|
| 2228 | - //this doesn't exist in the Db, but maybe the relation is of type belongs to, and so the related thing might |
|
| 2229 | - if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2230 | - $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2231 | - } |
|
| 2232 | - //this doesn't exist in the DB and apparently the thing it belongs to doesn't either, just get what's cached on this object |
|
| 2233 | - if ( ! $related_model_object) { |
|
| 2234 | - $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2235 | - } |
|
| 2236 | - } |
|
| 2237 | - return $related_model_object; |
|
| 2238 | - } |
|
| 2239 | - |
|
| 2240 | - |
|
| 2241 | - |
|
| 2242 | - /** |
|
| 2243 | - * Does a delete on all related objects of type $relationName and removes |
|
| 2244 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2245 | - * of blocking related model objects) does nothing. If the related model objects are |
|
| 2246 | - * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2247 | - * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2248 | - * |
|
| 2249 | - * @param string $relationName |
|
| 2250 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2251 | - * @return int how many deleted |
|
| 2252 | - * @throws \EE_Error |
|
| 2253 | - */ |
|
| 2254 | - public function delete_related($relationName, $query_params = array()) |
|
| 2255 | - { |
|
| 2256 | - if ($this->ID()) { |
|
| 2257 | - $count = $this->get_model()->delete_related($this, $relationName, $query_params); |
|
| 2258 | - } else { |
|
| 2259 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2260 | - $this->clear_cache($relationName, null, true); |
|
| 2261 | - } |
|
| 2262 | - return $count; |
|
| 2263 | - } |
|
| 2264 | - |
|
| 2265 | - |
|
| 2266 | - |
|
| 2267 | - /** |
|
| 2268 | - * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2269 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2270 | - * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2271 | - * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2272 | - * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2273 | - * |
|
| 2274 | - * @param string $relationName |
|
| 2275 | - * @param array $query_params like EEM_Base::get_all's |
|
| 2276 | - * @return int how many deleted (including those soft deleted) |
|
| 2277 | - * @throws \EE_Error |
|
| 2278 | - */ |
|
| 2279 | - public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2280 | - { |
|
| 2281 | - if ($this->ID()) { |
|
| 2282 | - $count = $this->get_model()->delete_related_permanently($this, $relationName, $query_params); |
|
| 2283 | - } else { |
|
| 2284 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2285 | - } |
|
| 2286 | - $this->clear_cache($relationName, null, true); |
|
| 2287 | - return $count; |
|
| 2288 | - } |
|
| 2289 | - |
|
| 2290 | - |
|
| 2291 | - |
|
| 2292 | - /** |
|
| 2293 | - * is_set |
|
| 2294 | - * Just a simple utility function children can use for checking if property exists |
|
| 2295 | - * |
|
| 2296 | - * @access public |
|
| 2297 | - * @param string $field_name property to check |
|
| 2298 | - * @return bool TRUE if existing,FALSE if not. |
|
| 2299 | - */ |
|
| 2300 | - public function is_set($field_name) |
|
| 2301 | - { |
|
| 2302 | - return isset($this->_fields[$field_name]); |
|
| 2303 | - } |
|
| 2304 | - |
|
| 2305 | - |
|
| 2306 | - |
|
| 2307 | - /** |
|
| 2308 | - * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2309 | - * EE_Error exception if they don't |
|
| 2310 | - * |
|
| 2311 | - * @param mixed (string|array) $properties properties to check |
|
| 2312 | - * @throws EE_Error |
|
| 2313 | - * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2314 | - */ |
|
| 2315 | - protected function _property_exists($properties) |
|
| 2316 | - { |
|
| 2317 | - foreach ((array)$properties as $property_name) { |
|
| 2318 | - //first make sure this property exists |
|
| 2319 | - if ( ! $this->_fields[$property_name]) { |
|
| 2320 | - throw new EE_Error( |
|
| 2321 | - sprintf( |
|
| 2322 | - __( |
|
| 2323 | - 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2324 | - 'event_espresso' |
|
| 2325 | - ), |
|
| 2326 | - $property_name |
|
| 2327 | - ) |
|
| 2328 | - ); |
|
| 2329 | - } |
|
| 2330 | - } |
|
| 2331 | - return true; |
|
| 2332 | - } |
|
| 2333 | - |
|
| 2334 | - |
|
| 2335 | - |
|
| 2336 | - /** |
|
| 2337 | - * This simply returns an array of model fields for this object |
|
| 2338 | - * |
|
| 2339 | - * @return array |
|
| 2340 | - * @throws \EE_Error |
|
| 2341 | - */ |
|
| 2342 | - public function model_field_array() |
|
| 2343 | - { |
|
| 2344 | - $fields = $this->get_model()->field_settings(false); |
|
| 2345 | - $properties = array(); |
|
| 2346 | - //remove prepended underscore |
|
| 2347 | - foreach ($fields as $field_name => $settings) { |
|
| 2348 | - $properties[$field_name] = $this->get($field_name); |
|
| 2349 | - } |
|
| 2350 | - return $properties; |
|
| 2351 | - } |
|
| 2352 | - |
|
| 2353 | - |
|
| 2354 | - |
|
| 2355 | - /** |
|
| 2356 | - * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2357 | - * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2358 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 2359 | - * requiring a plugin to extend the EE_Base_Class (which works fine is there's only 1 plugin, but when will that |
|
| 2360 | - * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 2361 | - * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 2362 | - * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 2363 | - * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 2364 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 2365 | - * my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2366 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2367 | - * return $previousReturnValue.$returnString; |
|
| 2368 | - * } |
|
| 2369 | - * require('EE_Answer.class.php'); |
|
| 2370 | - * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2371 | - * echo $answer->my_callback('monkeys',100); |
|
| 2372 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2373 | - * |
|
| 2374 | - * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2375 | - * @param array $args array of original arguments passed to the function |
|
| 2376 | - * @throws EE_Error |
|
| 2377 | - * @return mixed whatever the plugin which calls add_filter decides |
|
| 2378 | - */ |
|
| 2379 | - public function __call($methodName, $args) |
|
| 2380 | - { |
|
| 2381 | - $className = get_class($this); |
|
| 2382 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2383 | - if ( ! has_filter($tagName)) { |
|
| 2384 | - throw new EE_Error( |
|
| 2385 | - sprintf( |
|
| 2386 | - __( |
|
| 2387 | - "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
| 2388 | - "event_espresso" |
|
| 2389 | - ), |
|
| 2390 | - $methodName, |
|
| 2391 | - $className, |
|
| 2392 | - $tagName |
|
| 2393 | - ) |
|
| 2394 | - ); |
|
| 2395 | - } |
|
| 2396 | - return apply_filters($tagName, null, $this, $args); |
|
| 2397 | - } |
|
| 2398 | - |
|
| 2399 | - |
|
| 2400 | - |
|
| 2401 | - /** |
|
| 2402 | - * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2403 | - * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2404 | - * |
|
| 2405 | - * @param string $meta_key |
|
| 2406 | - * @param mixed $meta_value |
|
| 2407 | - * @param mixed $previous_value |
|
| 2408 | - * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2409 | - * @throws \EE_Error |
|
| 2410 | - * NOTE: if the values haven't changed, returns 0 |
|
| 2411 | - */ |
|
| 2412 | - public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2413 | - { |
|
| 2414 | - $query_params = array( |
|
| 2415 | - array( |
|
| 2416 | - 'EXM_key' => $meta_key, |
|
| 2417 | - 'OBJ_ID' => $this->ID(), |
|
| 2418 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2419 | - ), |
|
| 2420 | - ); |
|
| 2421 | - if ($previous_value !== null) { |
|
| 2422 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2423 | - } |
|
| 2424 | - $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2425 | - if ( ! $existing_rows_like_that) { |
|
| 2426 | - return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2427 | - } |
|
| 2428 | - foreach ($existing_rows_like_that as $existing_row) { |
|
| 2429 | - $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2430 | - } |
|
| 2431 | - return count($existing_rows_like_that); |
|
| 2432 | - } |
|
| 2433 | - |
|
| 2434 | - |
|
| 2435 | - |
|
| 2436 | - /** |
|
| 2437 | - * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2438 | - * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2439 | - * extra meta row was entered, false if not |
|
| 2440 | - * |
|
| 2441 | - * @param string $meta_key |
|
| 2442 | - * @param mixed $meta_value |
|
| 2443 | - * @param boolean $unique |
|
| 2444 | - * @return boolean |
|
| 2445 | - * @throws \EE_Error |
|
| 2446 | - */ |
|
| 2447 | - public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2448 | - { |
|
| 2449 | - if ($unique) { |
|
| 2450 | - $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2451 | - array( |
|
| 2452 | - array( |
|
| 2453 | - 'EXM_key' => $meta_key, |
|
| 2454 | - 'OBJ_ID' => $this->ID(), |
|
| 2455 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2456 | - ), |
|
| 2457 | - ) |
|
| 2458 | - ); |
|
| 2459 | - if ($existing_extra_meta) { |
|
| 2460 | - return false; |
|
| 2461 | - } |
|
| 2462 | - } |
|
| 2463 | - $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2464 | - array( |
|
| 2465 | - 'EXM_key' => $meta_key, |
|
| 2466 | - 'EXM_value' => $meta_value, |
|
| 2467 | - 'OBJ_ID' => $this->ID(), |
|
| 2468 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2469 | - ) |
|
| 2470 | - ); |
|
| 2471 | - $new_extra_meta->save(); |
|
| 2472 | - return true; |
|
| 2473 | - } |
|
| 2474 | - |
|
| 2475 | - |
|
| 2476 | - |
|
| 2477 | - /** |
|
| 2478 | - * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2479 | - * is specified, only deletes extra meta records with that value. |
|
| 2480 | - * |
|
| 2481 | - * @param string $meta_key |
|
| 2482 | - * @param mixed $meta_value |
|
| 2483 | - * @return int number of extra meta rows deleted |
|
| 2484 | - * @throws \EE_Error |
|
| 2485 | - */ |
|
| 2486 | - public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2487 | - { |
|
| 2488 | - $query_params = array( |
|
| 2489 | - array( |
|
| 2490 | - 'EXM_key' => $meta_key, |
|
| 2491 | - 'OBJ_ID' => $this->ID(), |
|
| 2492 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2493 | - ), |
|
| 2494 | - ); |
|
| 2495 | - if ($meta_value !== null) { |
|
| 2496 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2497 | - } |
|
| 2498 | - return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2499 | - } |
|
| 2500 | - |
|
| 2501 | - |
|
| 2502 | - |
|
| 2503 | - /** |
|
| 2504 | - * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2505 | - * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2506 | - * You can specify $default is case you haven't found the extra meta |
|
| 2507 | - * |
|
| 2508 | - * @param string $meta_key |
|
| 2509 | - * @param boolean $single |
|
| 2510 | - * @param mixed $default if we don't find anything, what should we return? |
|
| 2511 | - * @return mixed single value if $single; array if ! $single |
|
| 2512 | - * @throws \EE_Error |
|
| 2513 | - */ |
|
| 2514 | - public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2515 | - { |
|
| 2516 | - if ($single) { |
|
| 2517 | - $result = $this->get_first_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2518 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2519 | - return $result->value(); |
|
| 2520 | - } |
|
| 2521 | - } else { |
|
| 2522 | - $results = $this->get_many_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2523 | - if ($results) { |
|
| 2524 | - $values = array(); |
|
| 2525 | - foreach ($results as $result) { |
|
| 2526 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2527 | - $values[$result->ID()] = $result->value(); |
|
| 2528 | - } |
|
| 2529 | - } |
|
| 2530 | - return $values; |
|
| 2531 | - } |
|
| 2532 | - } |
|
| 2533 | - //if nothing discovered yet return default. |
|
| 2534 | - return apply_filters( |
|
| 2535 | - 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
| 2536 | - $default, |
|
| 2537 | - $meta_key, |
|
| 2538 | - $single, |
|
| 2539 | - $this |
|
| 2540 | - ); |
|
| 2541 | - } |
|
| 2542 | - |
|
| 2543 | - |
|
| 2544 | - |
|
| 2545 | - /** |
|
| 2546 | - * Returns a simple array of all the extra meta associated with this model object. |
|
| 2547 | - * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2548 | - * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2549 | - * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2550 | - * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2551 | - * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2552 | - * finally the extra meta's value as each sub-value. (eg |
|
| 2553 | - * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2554 | - * |
|
| 2555 | - * @param boolean $one_of_each_key |
|
| 2556 | - * @return array |
|
| 2557 | - * @throws \EE_Error |
|
| 2558 | - */ |
|
| 2559 | - public function all_extra_meta_array($one_of_each_key = true) |
|
| 2560 | - { |
|
| 2561 | - $return_array = array(); |
|
| 2562 | - if ($one_of_each_key) { |
|
| 2563 | - $extra_meta_objs = $this->get_many_related('Extra_Meta', array('group_by' => 'EXM_key')); |
|
| 2564 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2565 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2566 | - $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value(); |
|
| 2567 | - } |
|
| 2568 | - } |
|
| 2569 | - } else { |
|
| 2570 | - $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2571 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2572 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2573 | - if ( ! isset($return_array[$extra_meta_obj->key()])) { |
|
| 2574 | - $return_array[$extra_meta_obj->key()] = array(); |
|
| 2575 | - } |
|
| 2576 | - $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value(); |
|
| 2577 | - } |
|
| 2578 | - } |
|
| 2579 | - } |
|
| 2580 | - return $return_array; |
|
| 2581 | - } |
|
| 2582 | - |
|
| 2583 | - |
|
| 2584 | - |
|
| 2585 | - /** |
|
| 2586 | - * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2587 | - * |
|
| 2588 | - * @return string |
|
| 2589 | - * @throws \EE_Error |
|
| 2590 | - */ |
|
| 2591 | - public function name() |
|
| 2592 | - { |
|
| 2593 | - //find a field that's not a text field |
|
| 2594 | - $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2595 | - if ($field_we_can_use) { |
|
| 2596 | - return $this->get($field_we_can_use->get_name()); |
|
| 2597 | - } else { |
|
| 2598 | - $first_few_properties = $this->model_field_array(); |
|
| 2599 | - $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2600 | - $name_parts = array(); |
|
| 2601 | - foreach ($first_few_properties as $name => $value) { |
|
| 2602 | - $name_parts[] = "$name:$value"; |
|
| 2603 | - } |
|
| 2604 | - return implode(",", $name_parts); |
|
| 2605 | - } |
|
| 2606 | - } |
|
| 2607 | - |
|
| 2608 | - |
|
| 2609 | - |
|
| 2610 | - /** |
|
| 2611 | - * in_entity_map |
|
| 2612 | - * Checks if this model object has been proven to already be in the entity map |
|
| 2613 | - * |
|
| 2614 | - * @return boolean |
|
| 2615 | - * @throws \EE_Error |
|
| 2616 | - */ |
|
| 2617 | - public function in_entity_map() |
|
| 2618 | - { |
|
| 2619 | - if ($this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this) { |
|
| 2620 | - //well, if we looked, did we find it in the entity map? |
|
| 2621 | - return true; |
|
| 2622 | - } else { |
|
| 2623 | - return false; |
|
| 2624 | - } |
|
| 2625 | - } |
|
| 2626 | - |
|
| 2627 | - |
|
| 2628 | - |
|
| 2629 | - /** |
|
| 2630 | - * refresh_from_db |
|
| 2631 | - * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 2632 | - * |
|
| 2633 | - * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 2634 | - * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 2635 | - */ |
|
| 2636 | - public function refresh_from_db() |
|
| 2637 | - { |
|
| 2638 | - if ($this->ID() && $this->in_entity_map()) { |
|
| 2639 | - $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 2640 | - } else { |
|
| 2641 | - //if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 2642 | - //if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 2643 | - //that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 2644 | - //absolutely nothing in it for this ID |
|
| 2645 | - if (WP_DEBUG) { |
|
| 2646 | - throw new EE_Error( |
|
| 2647 | - sprintf( |
|
| 2648 | - __('Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
| 2649 | - 'event_espresso'), |
|
| 2650 | - $this->ID(), |
|
| 2651 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 2652 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 2653 | - ) |
|
| 2654 | - ); |
|
| 2655 | - } |
|
| 2656 | - } |
|
| 2657 | - } |
|
| 2658 | - |
|
| 2659 | - |
|
| 2660 | - |
|
| 2661 | - /** |
|
| 2662 | - * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 2663 | - * (probably a bad assumption they have made, oh well) |
|
| 2664 | - * |
|
| 2665 | - * @return string |
|
| 2666 | - */ |
|
| 2667 | - public function __toString() |
|
| 2668 | - { |
|
| 2669 | - try { |
|
| 2670 | - return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 2671 | - } catch (Exception $e) { |
|
| 2672 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2673 | - return ''; |
|
| 2674 | - } |
|
| 2675 | - } |
|
| 2676 | - |
|
| 2677 | - |
|
| 2678 | - |
|
| 2679 | - /** |
|
| 2680 | - * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 2681 | - * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 2682 | - * This means if we have made changes to those related model objects, and want to unserialize |
|
| 2683 | - * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 2684 | - * Instead, those related model objects should be directly serialized and stored. |
|
| 2685 | - * Eg, the following won't work: |
|
| 2686 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2687 | - * $att = $reg->attendee(); |
|
| 2688 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2689 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 2690 | - * //END REQUEST |
|
| 2691 | - * //START NEXT REQUEST |
|
| 2692 | - * $reg = get_option( 'my_option' ); |
|
| 2693 | - * $reg->attendee()->save(); |
|
| 2694 | - * And would need to be replace with: |
|
| 2695 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2696 | - * $att = $reg->attendee(); |
|
| 2697 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2698 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 2699 | - * //END REQUEST |
|
| 2700 | - * //START NEXT REQUEST |
|
| 2701 | - * $att = get_option( 'my_option' ); |
|
| 2702 | - * $att->save(); |
|
| 2703 | - * |
|
| 2704 | - * @return array |
|
| 2705 | - * @throws \EE_Error |
|
| 2706 | - */ |
|
| 2707 | - public function __sleep() |
|
| 2708 | - { |
|
| 2709 | - $model = $this->get_model(); |
|
| 2710 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 2711 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 2712 | - $classname = 'EE_' . $model->get_this_model_name(); |
|
| 2713 | - if ( |
|
| 2714 | - $this->get_one_from_cache($relation_name) instanceof $classname |
|
| 2715 | - && $this->get_one_from_cache($relation_name)->ID() |
|
| 2716 | - ) { |
|
| 2717 | - $this->clear_cache($relation_name, $this->get_one_from_cache($relation_name)->ID()); |
|
| 2718 | - } |
|
| 2719 | - } |
|
| 2720 | - } |
|
| 2721 | - $this->_props_n_values_provided_in_constructor = array(); |
|
| 2722 | - $properties_to_serialize = get_object_vars($this); |
|
| 2723 | - //don't serialize the model. It's big and that risks recursion |
|
| 2724 | - unset($properties_to_serialize['_model']); |
|
| 2725 | - return array_keys($properties_to_serialize); |
|
| 2726 | - } |
|
| 2727 | - |
|
| 2728 | - |
|
| 2729 | - |
|
| 2730 | - /** |
|
| 2731 | - * restore _props_n_values_provided_in_constructor |
|
| 2732 | - * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 2733 | - * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 2734 | - * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 2735 | - */ |
|
| 2736 | - public function __wakeup() |
|
| 2737 | - { |
|
| 2738 | - $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 2739 | - } |
|
| 28 | + /** |
|
| 29 | + * This is an array of the original properties and values provided during construction |
|
| 30 | + * of this model object. (keys are model field names, values are their values). |
|
| 31 | + * This list is important to remember so that when we are merging data from the db, we know |
|
| 32 | + * which values to override and which to not override. |
|
| 33 | + * |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + protected $_props_n_values_provided_in_constructor; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Timezone |
|
| 40 | + * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 41 | + * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 42 | + * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 43 | + * access to it. |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + protected $_timezone; |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * date format |
|
| 53 | + * pattern or format for displaying dates |
|
| 54 | + * |
|
| 55 | + * @var string $_dt_frmt |
|
| 56 | + */ |
|
| 57 | + protected $_dt_frmt; |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * time format |
|
| 63 | + * pattern or format for displaying time |
|
| 64 | + * |
|
| 65 | + * @var string $_tm_frmt |
|
| 66 | + */ |
|
| 67 | + protected $_tm_frmt; |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 73 | + * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 74 | + * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 75 | + * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 76 | + * |
|
| 77 | + * @var array |
|
| 78 | + */ |
|
| 79 | + protected $_cached_properties = array(); |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 83 | + * single |
|
| 84 | + * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 85 | + * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 86 | + * all others have an array) |
|
| 87 | + * |
|
| 88 | + * @var array |
|
| 89 | + */ |
|
| 90 | + protected $_model_relations = array(); |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 94 | + * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 95 | + * |
|
| 96 | + * @var array |
|
| 97 | + */ |
|
| 98 | + protected $_fields = array(); |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 102 | + * For example, we might create model objects intended to only be used for the duration |
|
| 103 | + * of this request and to be thrown away, and if they were accidentally saved |
|
| 104 | + * it would be a bug. |
|
| 105 | + */ |
|
| 106 | + protected $_allow_persist = true; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 110 | + */ |
|
| 111 | + protected $_has_changes = false; |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @var EEM_Base |
|
| 115 | + */ |
|
| 116 | + protected $_model; |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 122 | + * play nice |
|
| 123 | + * |
|
| 124 | + * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 125 | + * layer of the model's _fields array, (eg, EVT_ID, |
|
| 126 | + * TXN_amount, QST_name, etc) and values are their values |
|
| 127 | + * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 128 | + * corresponding db model or not. |
|
| 129 | + * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 130 | + * be in when instantiating a EE_Base_Class object. |
|
| 131 | + * @param array $date_formats An array of date formats to set on construct where first |
|
| 132 | + * value is the date_format and second value is the time |
|
| 133 | + * format. |
|
| 134 | + * @throws EE_Error |
|
| 135 | + */ |
|
| 136 | + protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 137 | + { |
|
| 138 | + $className = get_class($this); |
|
| 139 | + do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 140 | + $model = $this->get_model(); |
|
| 141 | + $model_fields = $model->field_settings(false); |
|
| 142 | + // ensure $fieldValues is an array |
|
| 143 | + $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 144 | + // EEH_Debug_Tools::printr( $fieldValues, '$fieldValues <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 145 | + // verify client code has not passed any invalid field names |
|
| 146 | + foreach ($fieldValues as $field_name => $field_value) { |
|
| 147 | + if ( ! isset($model_fields[$field_name])) { |
|
| 148 | + throw new EE_Error(sprintf(__("Invalid field (%s) passed to constructor of %s. Allowed fields are :%s", |
|
| 149 | + "event_espresso"), $field_name, get_class($this), implode(", ", array_keys($model_fields)))); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + // EEH_Debug_Tools::printr( $model_fields, '$model_fields <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
|
| 153 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 154 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 155 | + list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 156 | + } else { |
|
| 157 | + //set default formats for date and time |
|
| 158 | + $this->_dt_frmt = (string)get_option('date_format', 'Y-m-d'); |
|
| 159 | + $this->_tm_frmt = (string)get_option('time_format', 'g:i a'); |
|
| 160 | + } |
|
| 161 | + //if db model is instantiating |
|
| 162 | + if ($bydb) { |
|
| 163 | + //client code has indicated these field values are from the database |
|
| 164 | + foreach ($model_fields as $fieldName => $field) { |
|
| 165 | + $this->set_from_db($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null); |
|
| 166 | + } |
|
| 167 | + } else { |
|
| 168 | + //we're constructing a brand |
|
| 169 | + //new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 170 | + foreach ($model_fields as $fieldName => $field) { |
|
| 171 | + $this->set($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, true); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + //remember what values were passed to this constructor |
|
| 175 | + $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 176 | + //remember in entity mapper |
|
| 177 | + if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 178 | + $model->add_to_entity_map($this); |
|
| 179 | + } |
|
| 180 | + //setup all the relations |
|
| 181 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 182 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 183 | + $this->_model_relations[$relation_name] = null; |
|
| 184 | + } else { |
|
| 185 | + $this->_model_relations[$relation_name] = array(); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + /** |
|
| 189 | + * Action done at the end of each model object construction |
|
| 190 | + * |
|
| 191 | + * @param EE_Base_Class $this the model object just created |
|
| 192 | + */ |
|
| 193 | + do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 200 | + * |
|
| 201 | + * @return boolean |
|
| 202 | + */ |
|
| 203 | + public function allow_persist() |
|
| 204 | + { |
|
| 205 | + return $this->_allow_persist; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 212 | + * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 213 | + * you got new information that somehow made you change your mind. |
|
| 214 | + * |
|
| 215 | + * @param boolean $allow_persist |
|
| 216 | + * @return boolean |
|
| 217 | + */ |
|
| 218 | + public function set_allow_persist($allow_persist) |
|
| 219 | + { |
|
| 220 | + return $this->_allow_persist = $allow_persist; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Gets the field's original value when this object was constructed during this request. |
|
| 227 | + * This can be helpful when determining if a model object has changed or not |
|
| 228 | + * |
|
| 229 | + * @param string $field_name |
|
| 230 | + * @return mixed|null |
|
| 231 | + * @throws \EE_Error |
|
| 232 | + */ |
|
| 233 | + public function get_original($field_name) |
|
| 234 | + { |
|
| 235 | + if (isset($this->_props_n_values_provided_in_constructor[$field_name]) |
|
| 236 | + && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 237 | + ) { |
|
| 238 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
|
| 239 | + } else { |
|
| 240 | + return null; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param EE_Base_Class $obj |
|
| 248 | + * @return string |
|
| 249 | + */ |
|
| 250 | + public function get_class($obj) |
|
| 251 | + { |
|
| 252 | + return get_class($obj); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Overrides parent because parent expects old models. |
|
| 259 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 260 | + * |
|
| 261 | + * @param string $field_name |
|
| 262 | + * @param mixed $field_value |
|
| 263 | + * @param bool $use_default |
|
| 264 | + * @throws \EE_Error |
|
| 265 | + */ |
|
| 266 | + public function set($field_name, $field_value, $use_default = false) |
|
| 267 | + { |
|
| 268 | + // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 269 | + // then don't do anything |
|
| 270 | + if ( |
|
| 271 | + ! $use_default |
|
| 272 | + && $this->_fields[$field_name] === $field_value |
|
| 273 | + && $this->ID() |
|
| 274 | + ) { |
|
| 275 | + return; |
|
| 276 | + } |
|
| 277 | + $model = $this->get_model(); |
|
| 278 | + $this->_has_changes = true; |
|
| 279 | + $field_obj = $model->field_settings_for($field_name); |
|
| 280 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 281 | + // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 282 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 283 | + $field_obj->set_timezone($this->_timezone); |
|
| 284 | + $field_obj->set_date_format($this->_dt_frmt); |
|
| 285 | + $field_obj->set_time_format($this->_tm_frmt); |
|
| 286 | + } |
|
| 287 | + $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 288 | + //should the value be null? |
|
| 289 | + if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 290 | + $this->_fields[$field_name] = $field_obj->get_default_value(); |
|
| 291 | + /** |
|
| 292 | + * To save having to refactor all the models, if a default value is used for a |
|
| 293 | + * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 294 | + * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 295 | + * object. |
|
| 296 | + * |
|
| 297 | + * @since 4.6.10+ |
|
| 298 | + */ |
|
| 299 | + if ( |
|
| 300 | + $field_obj instanceof EE_Datetime_Field |
|
| 301 | + && $this->_fields[$field_name] !== null |
|
| 302 | + && ! $this->_fields[$field_name] instanceof DateTime |
|
| 303 | + ) { |
|
| 304 | + empty($this->_fields[$field_name]) |
|
| 305 | + ? $this->set($field_name, time()) |
|
| 306 | + : $this->set($field_name, $this->_fields[$field_name]); |
|
| 307 | + } |
|
| 308 | + } else { |
|
| 309 | + $this->_fields[$field_name] = $holder_of_value; |
|
| 310 | + } |
|
| 311 | + //if we're not in the constructor... |
|
| 312 | + //now check if what we set was a primary key |
|
| 313 | + if ( |
|
| 314 | + //note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 315 | + $this->_props_n_values_provided_in_constructor |
|
| 316 | + && $field_value |
|
| 317 | + && $field_name === $model->primary_key_name() |
|
| 318 | + ) { |
|
| 319 | + //if so, we want all this object's fields to be filled either with |
|
| 320 | + //what we've explicitly set on this model |
|
| 321 | + //or what we have in the db |
|
| 322 | + // echo "setting primary key!"; |
|
| 323 | + $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 324 | + $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 325 | + foreach ($fields_on_model as $field_obj) { |
|
| 326 | + if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 327 | + && $field_obj->get_name() !== $field_name |
|
| 328 | + ) { |
|
| 329 | + $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + //oh this model object has an ID? well make sure its in the entity mapper |
|
| 333 | + $model->add_to_entity_map($this); |
|
| 334 | + } |
|
| 335 | + //let's unset any cache for this field_name from the $_cached_properties property. |
|
| 336 | + $this->_clear_cached_property($field_name); |
|
| 337 | + } else { |
|
| 338 | + throw new EE_Error(sprintf(__("A valid EE_Model_Field_Base could not be found for the given field name: %s", |
|
| 339 | + "event_espresso"), $field_name)); |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * This sets the field value on the db column if it exists for the given $column_name or |
|
| 347 | + * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 348 | + * |
|
| 349 | + * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 350 | + * @param string $field_name Must be the exact column name. |
|
| 351 | + * @param mixed $field_value The value to set. |
|
| 352 | + * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 353 | + * @throws \EE_Error |
|
| 354 | + */ |
|
| 355 | + public function set_field_or_extra_meta($field_name, $field_value) |
|
| 356 | + { |
|
| 357 | + if ($this->get_model()->has_field($field_name)) { |
|
| 358 | + $this->set($field_name, $field_value); |
|
| 359 | + return true; |
|
| 360 | + } else { |
|
| 361 | + //ensure this object is saved first so that extra meta can be properly related. |
|
| 362 | + $this->save(); |
|
| 363 | + return $this->update_extra_meta($field_name, $field_value); |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * This retrieves the value of the db column set on this class or if that's not present |
|
| 371 | + * it will attempt to retrieve from extra_meta if found. |
|
| 372 | + * Example Usage: |
|
| 373 | + * Via EE_Message child class: |
|
| 374 | + * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 375 | + * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 376 | + * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 377 | + * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 378 | + * value for those extra fields dynamically via the EE_message object. |
|
| 379 | + * |
|
| 380 | + * @param string $field_name expecting the fully qualified field name. |
|
| 381 | + * @return mixed|null value for the field if found. null if not found. |
|
| 382 | + * @throws \EE_Error |
|
| 383 | + */ |
|
| 384 | + public function get_field_or_extra_meta($field_name) |
|
| 385 | + { |
|
| 386 | + if ($this->get_model()->has_field($field_name)) { |
|
| 387 | + $column_value = $this->get($field_name); |
|
| 388 | + } else { |
|
| 389 | + //This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 390 | + $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 391 | + } |
|
| 392 | + return $column_value; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 399 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 400 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 401 | + * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 402 | + * |
|
| 403 | + * @access public |
|
| 404 | + * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 405 | + * @return void |
|
| 406 | + * @throws \EE_Error |
|
| 407 | + */ |
|
| 408 | + public function set_timezone($timezone = '') |
|
| 409 | + { |
|
| 410 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 411 | + //make sure we clear all cached properties because they won't be relevant now |
|
| 412 | + $this->_clear_cached_properties(); |
|
| 413 | + //make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 414 | + $model_fields = $this->get_model()->field_settings(false); |
|
| 415 | + foreach ($model_fields as $field_name => $field_obj) { |
|
| 416 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 417 | + $field_obj->set_timezone($this->_timezone); |
|
| 418 | + if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) { |
|
| 419 | + $this->_fields[$field_name]->setTimezone(new DateTimeZone($this->_timezone)); |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * This just returns whatever is set for the current timezone. |
|
| 429 | + * |
|
| 430 | + * @access public |
|
| 431 | + * @return string timezone string |
|
| 432 | + */ |
|
| 433 | + public function get_timezone() |
|
| 434 | + { |
|
| 435 | + return $this->_timezone; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 442 | + * internally instead of wp set date format options |
|
| 443 | + * |
|
| 444 | + * @since 4.6 |
|
| 445 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 446 | + */ |
|
| 447 | + public function set_date_format($format) |
|
| 448 | + { |
|
| 449 | + $this->_dt_frmt = $format; |
|
| 450 | + //clear cached_properties because they won't be relevant now. |
|
| 451 | + $this->_clear_cached_properties(); |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 458 | + * class internally instead of wp set time format options. |
|
| 459 | + * |
|
| 460 | + * @since 4.6 |
|
| 461 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 462 | + */ |
|
| 463 | + public function set_time_format($format) |
|
| 464 | + { |
|
| 465 | + $this->_tm_frmt = $format; |
|
| 466 | + //clear cached_properties because they won't be relevant now. |
|
| 467 | + $this->_clear_cached_properties(); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * This returns the current internal set format for the date and time formats. |
|
| 474 | + * |
|
| 475 | + * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 476 | + * where the first value is the date format and the second value is the time format. |
|
| 477 | + * @return mixed string|array |
|
| 478 | + */ |
|
| 479 | + public function get_format($full = true) |
|
| 480 | + { |
|
| 481 | + return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * cache |
|
| 488 | + * stores the passed model object on the current model object. |
|
| 489 | + * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 490 | + * |
|
| 491 | + * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 492 | + * 'Registration' associated with this model object |
|
| 493 | + * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 494 | + * that could be a payment or a registration) |
|
| 495 | + * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 496 | + * items which will be stored in an array on this object |
|
| 497 | + * @throws EE_Error |
|
| 498 | + * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 499 | + * related thing, no array) |
|
| 500 | + */ |
|
| 501 | + public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 502 | + { |
|
| 503 | + // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 504 | + if ( ! $object_to_cache instanceof EE_Base_Class) { |
|
| 505 | + return false; |
|
| 506 | + } |
|
| 507 | + // also get "how" the object is related, or throw an error |
|
| 508 | + if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 509 | + throw new EE_Error(sprintf(__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 510 | + $relationName, get_class($this))); |
|
| 511 | + } |
|
| 512 | + // how many things are related ? |
|
| 513 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 514 | + // if it's a "belongs to" relationship, then there's only one related model object eg, if this is a registration, there's only 1 attendee for it |
|
| 515 | + // so for these model objects just set it to be cached |
|
| 516 | + $this->_model_relations[$relationName] = $object_to_cache; |
|
| 517 | + $return = true; |
|
| 518 | + } else { |
|
| 519 | + // otherwise, this is the "many" side of a one to many relationship, so we'll add the object to the array of related objects for that type. |
|
| 520 | + // eg: if this is an event, there are many registrations for that event, so we cache the registrations in an array |
|
| 521 | + if ( ! is_array($this->_model_relations[$relationName])) { |
|
| 522 | + // if for some reason, the cached item is a model object, then stick that in the array, otherwise start with an empty array |
|
| 523 | + $this->_model_relations[$relationName] = $this->_model_relations[$relationName] instanceof EE_Base_Class |
|
| 524 | + ? array($this->_model_relations[$relationName]) : array(); |
|
| 525 | + } |
|
| 526 | + // first check for a cache_id which is normally empty |
|
| 527 | + if ( ! empty($cache_id)) { |
|
| 528 | + // if the cache_id exists, then it means we are purposely trying to cache this with a known key that can then be used to retrieve the object later on |
|
| 529 | + $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
|
| 530 | + $return = $cache_id; |
|
| 531 | + } elseif ($object_to_cache->ID()) { |
|
| 532 | + // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 533 | + $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
|
| 534 | + $return = $object_to_cache->ID(); |
|
| 535 | + } else { |
|
| 536 | + // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 537 | + $this->_model_relations[$relationName][] = $object_to_cache; |
|
| 538 | + // move the internal pointer to the end of the array |
|
| 539 | + end($this->_model_relations[$relationName]); |
|
| 540 | + // and grab the key so that we can return it |
|
| 541 | + $return = key($this->_model_relations[$relationName]); |
|
| 542 | + } |
|
| 543 | + } |
|
| 544 | + return $return; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + |
|
| 548 | + |
|
| 549 | + /** |
|
| 550 | + * For adding an item to the cached_properties property. |
|
| 551 | + * |
|
| 552 | + * @access protected |
|
| 553 | + * @param string $fieldname the property item the corresponding value is for. |
|
| 554 | + * @param mixed $value The value we are caching. |
|
| 555 | + * @param string|null $cache_type |
|
| 556 | + * @return void |
|
| 557 | + * @throws \EE_Error |
|
| 558 | + */ |
|
| 559 | + protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 560 | + { |
|
| 561 | + //first make sure this property exists |
|
| 562 | + $this->get_model()->field_settings_for($fieldname); |
|
| 563 | + $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 564 | + $this->_cached_properties[$fieldname][$cache_type] = $value; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 571 | + * This also SETS the cache if we return the actual property! |
|
| 572 | + * |
|
| 573 | + * @param string $fieldname the name of the property we're trying to retrieve |
|
| 574 | + * @param bool $pretty |
|
| 575 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 576 | + * (in cases where the same property may be used for different outputs |
|
| 577 | + * - i.e. datetime, money etc.) |
|
| 578 | + * It can also accept certain pre-defined "schema" strings |
|
| 579 | + * to define how to output the property. |
|
| 580 | + * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 581 | + * @return mixed whatever the value for the property is we're retrieving |
|
| 582 | + * @throws \EE_Error |
|
| 583 | + */ |
|
| 584 | + protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 585 | + { |
|
| 586 | + //verify the field exists |
|
| 587 | + $model = $this->get_model(); |
|
| 588 | + $model->field_settings_for($fieldname); |
|
| 589 | + $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 590 | + $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 591 | + if (isset($this->_cached_properties[$fieldname][$cache_type])) { |
|
| 592 | + return $this->_cached_properties[$fieldname][$cache_type]; |
|
| 593 | + } |
|
| 594 | + $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
| 595 | + $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 596 | + return $value; |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * If the cache didn't fetch the needed item, this fetches it. |
|
| 603 | + * @param string $fieldname |
|
| 604 | + * @param bool $pretty |
|
| 605 | + * @param string $extra_cache_ref |
|
| 606 | + * @return mixed |
|
| 607 | + */ |
|
| 608 | + protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 609 | + { |
|
| 610 | + $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 611 | + // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 612 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 613 | + $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 614 | + } |
|
| 615 | + if ( ! isset($this->_fields[$fieldname])) { |
|
| 616 | + $this->_fields[$fieldname] = null; |
|
| 617 | + } |
|
| 618 | + $value = $pretty |
|
| 619 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref) |
|
| 620 | + : $field_obj->prepare_for_get($this->_fields[$fieldname]); |
|
| 621 | + return $value; |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 628 | + * |
|
| 629 | + * @param \EE_Datetime_Field $datetime_field |
|
| 630 | + * @param bool $pretty |
|
| 631 | + * @param null $date_or_time |
|
| 632 | + * @return void |
|
| 633 | + * @throws \EE_Error |
|
| 634 | + */ |
|
| 635 | + protected function _prepare_datetime_field( |
|
| 636 | + EE_Datetime_Field $datetime_field, |
|
| 637 | + $pretty = false, |
|
| 638 | + $date_or_time = null |
|
| 639 | + ) { |
|
| 640 | + $datetime_field->set_timezone($this->_timezone); |
|
| 641 | + $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 642 | + $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 643 | + //set the output returned |
|
| 644 | + switch ($date_or_time) { |
|
| 645 | + case 'D' : |
|
| 646 | + $datetime_field->set_date_time_output('date'); |
|
| 647 | + break; |
|
| 648 | + case 'T' : |
|
| 649 | + $datetime_field->set_date_time_output('time'); |
|
| 650 | + break; |
|
| 651 | + default : |
|
| 652 | + $datetime_field->set_date_time_output(); |
|
| 653 | + } |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * This just takes care of clearing out the cached_properties |
|
| 660 | + * |
|
| 661 | + * @return void |
|
| 662 | + */ |
|
| 663 | + protected function _clear_cached_properties() |
|
| 664 | + { |
|
| 665 | + $this->_cached_properties = array(); |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * This just clears out ONE property if it exists in the cache |
|
| 672 | + * |
|
| 673 | + * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 674 | + * @return void |
|
| 675 | + */ |
|
| 676 | + protected function _clear_cached_property($property_name) |
|
| 677 | + { |
|
| 678 | + if (isset($this->_cached_properties[$property_name])) { |
|
| 679 | + unset($this->_cached_properties[$property_name]); |
|
| 680 | + } |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + |
|
| 684 | + |
|
| 685 | + /** |
|
| 686 | + * Ensures that this related thing is a model object. |
|
| 687 | + * |
|
| 688 | + * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 689 | + * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 690 | + * @return EE_Base_Class |
|
| 691 | + * @throws \EE_Error |
|
| 692 | + */ |
|
| 693 | + protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 694 | + { |
|
| 695 | + $other_model_instance = self::_get_model_instance_with_name( |
|
| 696 | + self::_get_model_classname($model_name), |
|
| 697 | + $this->_timezone |
|
| 698 | + ); |
|
| 699 | + return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + |
|
| 703 | + |
|
| 704 | + /** |
|
| 705 | + * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 706 | + * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 707 | + * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 708 | + * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 709 | + * |
|
| 710 | + * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 711 | + * Eg 'Registration' |
|
| 712 | + * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 713 | + * if you intend to use $clear_all = TRUE, or the relation only |
|
| 714 | + * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 715 | + * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 716 | + * this is HasMany or HABTM. |
|
| 717 | + * @throws EE_Error |
|
| 718 | + * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 719 | + * relation from all |
|
| 720 | + */ |
|
| 721 | + public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 722 | + { |
|
| 723 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 724 | + $index_in_cache = ''; |
|
| 725 | + if ( ! $relationship_to_model) { |
|
| 726 | + throw new EE_Error( |
|
| 727 | + sprintf( |
|
| 728 | + __("There is no relationship to %s on a %s. Cannot clear that cache", 'event_espresso'), |
|
| 729 | + $relationName, |
|
| 730 | + get_class($this) |
|
| 731 | + ) |
|
| 732 | + ); |
|
| 733 | + } |
|
| 734 | + if ($clear_all) { |
|
| 735 | + $obj_removed = true; |
|
| 736 | + $this->_model_relations[$relationName] = null; |
|
| 737 | + } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 738 | + $obj_removed = $this->_model_relations[$relationName]; |
|
| 739 | + $this->_model_relations[$relationName] = null; |
|
| 740 | + } else { |
|
| 741 | + if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 742 | + && $object_to_remove_or_index_into_array->ID() |
|
| 743 | + ) { |
|
| 744 | + $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 745 | + if (is_array($this->_model_relations[$relationName]) |
|
| 746 | + && ! isset($this->_model_relations[$relationName][$index_in_cache]) |
|
| 747 | + ) { |
|
| 748 | + $index_found_at = null; |
|
| 749 | + //find this object in the array even though it has a different key |
|
| 750 | + foreach ($this->_model_relations[$relationName] as $index => $obj) { |
|
| 751 | + if ( |
|
| 752 | + $obj instanceof EE_Base_Class |
|
| 753 | + && ( |
|
| 754 | + $obj == $object_to_remove_or_index_into_array |
|
| 755 | + || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 756 | + ) |
|
| 757 | + ) { |
|
| 758 | + $index_found_at = $index; |
|
| 759 | + break; |
|
| 760 | + } |
|
| 761 | + } |
|
| 762 | + if ($index_found_at) { |
|
| 763 | + $index_in_cache = $index_found_at; |
|
| 764 | + } else { |
|
| 765 | + //it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 766 | + //if it wasn't in it to begin with. So we're done |
|
| 767 | + return $object_to_remove_or_index_into_array; |
|
| 768 | + } |
|
| 769 | + } |
|
| 770 | + } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 771 | + //so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 772 | + foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 773 | + if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 774 | + $index_in_cache = $index; |
|
| 775 | + } |
|
| 776 | + } |
|
| 777 | + } else { |
|
| 778 | + $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 779 | + } |
|
| 780 | + //supposedly we've found it. But it could just be that the client code |
|
| 781 | + //provided a bad index/object |
|
| 782 | + if ( |
|
| 783 | + isset( |
|
| 784 | + $this->_model_relations[$relationName], |
|
| 785 | + $this->_model_relations[$relationName][$index_in_cache] |
|
| 786 | + ) |
|
| 787 | + ) { |
|
| 788 | + $obj_removed = $this->_model_relations[$relationName][$index_in_cache]; |
|
| 789 | + unset($this->_model_relations[$relationName][$index_in_cache]); |
|
| 790 | + } else { |
|
| 791 | + //that thing was never cached anyways. |
|
| 792 | + $obj_removed = null; |
|
| 793 | + } |
|
| 794 | + } |
|
| 795 | + return $obj_removed; |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + |
|
| 799 | + |
|
| 800 | + /** |
|
| 801 | + * update_cache_after_object_save |
|
| 802 | + * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 803 | + * obtained after being saved to the db |
|
| 804 | + * |
|
| 805 | + * @param string $relationName - the type of object that is cached |
|
| 806 | + * @param \EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 807 | + * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 808 | + * @return boolean TRUE on success, FALSE on fail |
|
| 809 | + * @throws \EE_Error |
|
| 810 | + */ |
|
| 811 | + public function update_cache_after_object_save( |
|
| 812 | + $relationName, |
|
| 813 | + EE_Base_Class $newly_saved_object, |
|
| 814 | + $current_cache_id = '' |
|
| 815 | + ) { |
|
| 816 | + // verify that incoming object is of the correct type |
|
| 817 | + $obj_class = 'EE_' . $relationName; |
|
| 818 | + if ($newly_saved_object instanceof $obj_class) { |
|
| 819 | + /* @type EE_Base_Class $newly_saved_object */ |
|
| 820 | + // now get the type of relation |
|
| 821 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 822 | + // if this is a 1:1 relationship |
|
| 823 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 824 | + // then just replace the cached object with the newly saved object |
|
| 825 | + $this->_model_relations[$relationName] = $newly_saved_object; |
|
| 826 | + return true; |
|
| 827 | + // or if it's some kind of sordid feral polyamorous relationship... |
|
| 828 | + } elseif (is_array($this->_model_relations[$relationName]) |
|
| 829 | + && isset($this->_model_relations[$relationName][$current_cache_id]) |
|
| 830 | + ) { |
|
| 831 | + // then remove the current cached item |
|
| 832 | + unset($this->_model_relations[$relationName][$current_cache_id]); |
|
| 833 | + // and cache the newly saved object using it's new ID |
|
| 834 | + $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object; |
|
| 835 | + return true; |
|
| 836 | + } |
|
| 837 | + } |
|
| 838 | + return false; |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + |
|
| 842 | + |
|
| 843 | + /** |
|
| 844 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 845 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 846 | + * |
|
| 847 | + * @param string $relationName |
|
| 848 | + * @return EE_Base_Class |
|
| 849 | + */ |
|
| 850 | + public function get_one_from_cache($relationName) |
|
| 851 | + { |
|
| 852 | + $cached_array_or_object = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] |
|
| 853 | + : null; |
|
| 854 | + if (is_array($cached_array_or_object)) { |
|
| 855 | + return array_shift($cached_array_or_object); |
|
| 856 | + } else { |
|
| 857 | + return $cached_array_or_object; |
|
| 858 | + } |
|
| 859 | + } |
|
| 860 | + |
|
| 861 | + |
|
| 862 | + |
|
| 863 | + /** |
|
| 864 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 865 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 866 | + * |
|
| 867 | + * @param string $relationName |
|
| 868 | + * @throws \EE_Error |
|
| 869 | + * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 870 | + */ |
|
| 871 | + public function get_all_from_cache($relationName) |
|
| 872 | + { |
|
| 873 | + $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array(); |
|
| 874 | + // if the result is not an array, but exists, make it an array |
|
| 875 | + $objects = is_array($objects) ? $objects : array($objects); |
|
| 876 | + //bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 877 | + //basically, if this model object was stored in the session, and these cached model objects |
|
| 878 | + //already have IDs, let's make sure they're in their model's entity mapper |
|
| 879 | + //otherwise we will have duplicates next time we call |
|
| 880 | + // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 881 | + $model = EE_Registry::instance()->load_model($relationName); |
|
| 882 | + foreach ($objects as $model_object) { |
|
| 883 | + if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 884 | + //ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 885 | + if ($model_object->ID()) { |
|
| 886 | + $model->add_to_entity_map($model_object); |
|
| 887 | + } |
|
| 888 | + } else { |
|
| 889 | + throw new EE_Error( |
|
| 890 | + sprintf( |
|
| 891 | + __( |
|
| 892 | + 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 893 | + 'event_espresso' |
|
| 894 | + ), |
|
| 895 | + $relationName, |
|
| 896 | + gettype($model_object) |
|
| 897 | + ) |
|
| 898 | + ); |
|
| 899 | + } |
|
| 900 | + } |
|
| 901 | + return $objects; |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + |
|
| 905 | + |
|
| 906 | + /** |
|
| 907 | + * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 908 | + * matching the given query conditions. |
|
| 909 | + * |
|
| 910 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 911 | + * @param int $limit How many objects to return. |
|
| 912 | + * @param array $query_params Any additional conditions on the query. |
|
| 913 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 914 | + * you can indicate just the columns you want returned |
|
| 915 | + * @return array|EE_Base_Class[] |
|
| 916 | + * @throws \EE_Error |
|
| 917 | + */ |
|
| 918 | + public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 919 | + { |
|
| 920 | + $model = $this->get_model(); |
|
| 921 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 922 | + ? $model->get_primary_key_field()->get_name() |
|
| 923 | + : $field_to_order_by; |
|
| 924 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 925 | + if (empty($field) || empty($current_value)) { |
|
| 926 | + return array(); |
|
| 927 | + } |
|
| 928 | + return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 929 | + } |
|
| 930 | + |
|
| 931 | + |
|
| 932 | + |
|
| 933 | + /** |
|
| 934 | + * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 935 | + * matching the given query conditions. |
|
| 936 | + * |
|
| 937 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 938 | + * @param int $limit How many objects to return. |
|
| 939 | + * @param array $query_params Any additional conditions on the query. |
|
| 940 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 941 | + * you can indicate just the columns you want returned |
|
| 942 | + * @return array|EE_Base_Class[] |
|
| 943 | + * @throws \EE_Error |
|
| 944 | + */ |
|
| 945 | + public function previous_x( |
|
| 946 | + $field_to_order_by = null, |
|
| 947 | + $limit = 1, |
|
| 948 | + $query_params = array(), |
|
| 949 | + $columns_to_select = null |
|
| 950 | + ) { |
|
| 951 | + $model = $this->get_model(); |
|
| 952 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 953 | + ? $model->get_primary_key_field()->get_name() |
|
| 954 | + : $field_to_order_by; |
|
| 955 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 956 | + if (empty($field) || empty($current_value)) { |
|
| 957 | + return array(); |
|
| 958 | + } |
|
| 959 | + return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 960 | + } |
|
| 961 | + |
|
| 962 | + |
|
| 963 | + |
|
| 964 | + /** |
|
| 965 | + * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 966 | + * matching the given query conditions. |
|
| 967 | + * |
|
| 968 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 969 | + * @param array $query_params Any additional conditions on the query. |
|
| 970 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 971 | + * you can indicate just the columns you want returned |
|
| 972 | + * @return array|EE_Base_Class |
|
| 973 | + * @throws \EE_Error |
|
| 974 | + */ |
|
| 975 | + public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 976 | + { |
|
| 977 | + $model = $this->get_model(); |
|
| 978 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 979 | + ? $model->get_primary_key_field()->get_name() |
|
| 980 | + : $field_to_order_by; |
|
| 981 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 982 | + if (empty($field) || empty($current_value)) { |
|
| 983 | + return array(); |
|
| 984 | + } |
|
| 985 | + return $model->next($current_value, $field, $query_params, $columns_to_select); |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + |
|
| 989 | + |
|
| 990 | + /** |
|
| 991 | + * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 992 | + * matching the given query conditions. |
|
| 993 | + * |
|
| 994 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 995 | + * @param array $query_params Any additional conditions on the query. |
|
| 996 | + * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 997 | + * you can indicate just the column you want returned |
|
| 998 | + * @return array|EE_Base_Class |
|
| 999 | + * @throws \EE_Error |
|
| 1000 | + */ |
|
| 1001 | + public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1002 | + { |
|
| 1003 | + $model = $this->get_model(); |
|
| 1004 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1005 | + ? $model->get_primary_key_field()->get_name() |
|
| 1006 | + : $field_to_order_by; |
|
| 1007 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1008 | + if (empty($field) || empty($current_value)) { |
|
| 1009 | + return array(); |
|
| 1010 | + } |
|
| 1011 | + return $model->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 1012 | + } |
|
| 1013 | + |
|
| 1014 | + |
|
| 1015 | + |
|
| 1016 | + /** |
|
| 1017 | + * Overrides parent because parent expects old models. |
|
| 1018 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 1019 | + * |
|
| 1020 | + * @param string $field_name |
|
| 1021 | + * @param mixed $field_value_from_db |
|
| 1022 | + * @throws \EE_Error |
|
| 1023 | + */ |
|
| 1024 | + public function set_from_db($field_name, $field_value_from_db) |
|
| 1025 | + { |
|
| 1026 | + $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1027 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1028 | + //you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1029 | + //eg, a CPT model object could have an entry in the posts table, but no |
|
| 1030 | + //entry in the meta table. Meaning that all its columns in the meta table |
|
| 1031 | + //are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1032 | + if ($field_value_from_db === null) { |
|
| 1033 | + if ($field_obj->is_nullable()) { |
|
| 1034 | + //if the field allows nulls, then let it be null |
|
| 1035 | + $field_value = null; |
|
| 1036 | + } else { |
|
| 1037 | + $field_value = $field_obj->get_default_value(); |
|
| 1038 | + } |
|
| 1039 | + } else { |
|
| 1040 | + $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1041 | + } |
|
| 1042 | + $this->_fields[$field_name] = $field_value; |
|
| 1043 | + $this->_clear_cached_property($field_name); |
|
| 1044 | + } |
|
| 1045 | + } |
|
| 1046 | + |
|
| 1047 | + |
|
| 1048 | + |
|
| 1049 | + /** |
|
| 1050 | + * verifies that the specified field is of the correct type |
|
| 1051 | + * |
|
| 1052 | + * @param string $field_name |
|
| 1053 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1054 | + * (in cases where the same property may be used for different outputs |
|
| 1055 | + * - i.e. datetime, money etc.) |
|
| 1056 | + * @return mixed |
|
| 1057 | + * @throws \EE_Error |
|
| 1058 | + */ |
|
| 1059 | + public function get($field_name, $extra_cache_ref = null) |
|
| 1060 | + { |
|
| 1061 | + return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1062 | + } |
|
| 1063 | + |
|
| 1064 | + |
|
| 1065 | + |
|
| 1066 | + /** |
|
| 1067 | + * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1068 | + * |
|
| 1069 | + * @param string $field_name A valid fieldname |
|
| 1070 | + * @return mixed Whatever the raw value stored on the property is. |
|
| 1071 | + * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1072 | + */ |
|
| 1073 | + public function get_raw($field_name) |
|
| 1074 | + { |
|
| 1075 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1076 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime |
|
| 1077 | + ? $this->_fields[$field_name]->format('U') |
|
| 1078 | + : $this->_fields[$field_name]; |
|
| 1079 | + } |
|
| 1080 | + |
|
| 1081 | + |
|
| 1082 | + |
|
| 1083 | + /** |
|
| 1084 | + * This is used to return the internal DateTime object used for a field that is a |
|
| 1085 | + * EE_Datetime_Field. |
|
| 1086 | + * |
|
| 1087 | + * @param string $field_name The field name retrieving the DateTime object. |
|
| 1088 | + * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1089 | + * @throws \EE_Error |
|
| 1090 | + * an error is set and false returned. If the field IS an |
|
| 1091 | + * EE_Datetime_Field and but the field value is null, then |
|
| 1092 | + * just null is returned (because that indicates that likely |
|
| 1093 | + * this field is nullable). |
|
| 1094 | + */ |
|
| 1095 | + public function get_DateTime_object($field_name) |
|
| 1096 | + { |
|
| 1097 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1098 | + if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
| 1099 | + EE_Error::add_error( |
|
| 1100 | + sprintf( |
|
| 1101 | + __( |
|
| 1102 | + 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1103 | + 'event_espresso' |
|
| 1104 | + ), |
|
| 1105 | + $field_name |
|
| 1106 | + ), |
|
| 1107 | + __FILE__, |
|
| 1108 | + __FUNCTION__, |
|
| 1109 | + __LINE__ |
|
| 1110 | + ); |
|
| 1111 | + return false; |
|
| 1112 | + } |
|
| 1113 | + return $this->_fields[$field_name]; |
|
| 1114 | + } |
|
| 1115 | + |
|
| 1116 | + |
|
| 1117 | + |
|
| 1118 | + /** |
|
| 1119 | + * To be used in template to immediately echo out the value, and format it for output. |
|
| 1120 | + * Eg, should call stripslashes and whatnot before echoing |
|
| 1121 | + * |
|
| 1122 | + * @param string $field_name the name of the field as it appears in the DB |
|
| 1123 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1124 | + * (in cases where the same property may be used for different outputs |
|
| 1125 | + * - i.e. datetime, money etc.) |
|
| 1126 | + * @return void |
|
| 1127 | + * @throws \EE_Error |
|
| 1128 | + */ |
|
| 1129 | + public function e($field_name, $extra_cache_ref = null) |
|
| 1130 | + { |
|
| 1131 | + echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + |
|
| 1135 | + |
|
| 1136 | + /** |
|
| 1137 | + * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1138 | + * can be easily used as the value of form input. |
|
| 1139 | + * |
|
| 1140 | + * @param string $field_name |
|
| 1141 | + * @return void |
|
| 1142 | + * @throws \EE_Error |
|
| 1143 | + */ |
|
| 1144 | + public function f($field_name) |
|
| 1145 | + { |
|
| 1146 | + $this->e($field_name, 'form_input'); |
|
| 1147 | + } |
|
| 1148 | + |
|
| 1149 | + |
|
| 1150 | + |
|
| 1151 | + /** |
|
| 1152 | + * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1153 | + * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1154 | + * to see what options are available. |
|
| 1155 | + * @param string $field_name |
|
| 1156 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1157 | + * (in cases where the same property may be used for different outputs |
|
| 1158 | + * - i.e. datetime, money etc.) |
|
| 1159 | + * @return mixed |
|
| 1160 | + * @throws \EE_Error |
|
| 1161 | + */ |
|
| 1162 | + public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1163 | + { |
|
| 1164 | + return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1165 | + } |
|
| 1166 | + |
|
| 1167 | + |
|
| 1168 | + |
|
| 1169 | + /** |
|
| 1170 | + * This simply returns the datetime for the given field name |
|
| 1171 | + * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1172 | + * (and the equivalent e_date, e_time, e_datetime). |
|
| 1173 | + * |
|
| 1174 | + * @access protected |
|
| 1175 | + * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1176 | + * @param string $dt_frmt valid datetime format used for date |
|
| 1177 | + * (if '' then we just use the default on the field, |
|
| 1178 | + * if NULL we use the last-used format) |
|
| 1179 | + * @param string $tm_frmt Same as above except this is for time format |
|
| 1180 | + * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1181 | + * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1182 | + * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1183 | + * if field is not a valid dtt field, or void if echoing |
|
| 1184 | + * @throws \EE_Error |
|
| 1185 | + */ |
|
| 1186 | + protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1187 | + { |
|
| 1188 | + // clear cached property |
|
| 1189 | + $this->_clear_cached_property($field_name); |
|
| 1190 | + //reset format properties because they are used in get() |
|
| 1191 | + $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1192 | + $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1193 | + if ($echo) { |
|
| 1194 | + $this->e($field_name, $date_or_time); |
|
| 1195 | + return ''; |
|
| 1196 | + } |
|
| 1197 | + return $this->get($field_name, $date_or_time); |
|
| 1198 | + } |
|
| 1199 | + |
|
| 1200 | + |
|
| 1201 | + |
|
| 1202 | + /** |
|
| 1203 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1204 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1205 | + * other echoes the pretty value for dtt) |
|
| 1206 | + * |
|
| 1207 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1208 | + * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1209 | + * @return string datetime value formatted |
|
| 1210 | + * @throws \EE_Error |
|
| 1211 | + */ |
|
| 1212 | + public function get_date($field_name, $format = '') |
|
| 1213 | + { |
|
| 1214 | + return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + |
|
| 1218 | + |
|
| 1219 | + /** |
|
| 1220 | + * @param $field_name |
|
| 1221 | + * @param string $format |
|
| 1222 | + * @throws \EE_Error |
|
| 1223 | + */ |
|
| 1224 | + public function e_date($field_name, $format = '') |
|
| 1225 | + { |
|
| 1226 | + $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1227 | + } |
|
| 1228 | + |
|
| 1229 | + |
|
| 1230 | + |
|
| 1231 | + /** |
|
| 1232 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1233 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1234 | + * other echoes the pretty value for dtt) |
|
| 1235 | + * |
|
| 1236 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1237 | + * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1238 | + * @return string datetime value formatted |
|
| 1239 | + * @throws \EE_Error |
|
| 1240 | + */ |
|
| 1241 | + public function get_time($field_name, $format = '') |
|
| 1242 | + { |
|
| 1243 | + return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1244 | + } |
|
| 1245 | + |
|
| 1246 | + |
|
| 1247 | + |
|
| 1248 | + /** |
|
| 1249 | + * @param $field_name |
|
| 1250 | + * @param string $format |
|
| 1251 | + * @throws \EE_Error |
|
| 1252 | + */ |
|
| 1253 | + public function e_time($field_name, $format = '') |
|
| 1254 | + { |
|
| 1255 | + $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1256 | + } |
|
| 1257 | + |
|
| 1258 | + |
|
| 1259 | + |
|
| 1260 | + /** |
|
| 1261 | + * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1262 | + * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1263 | + * other echoes the pretty value for dtt) |
|
| 1264 | + * |
|
| 1265 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1266 | + * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1267 | + * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1268 | + * @return string datetime value formatted |
|
| 1269 | + * @throws \EE_Error |
|
| 1270 | + */ |
|
| 1271 | + public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1272 | + { |
|
| 1273 | + return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1274 | + } |
|
| 1275 | + |
|
| 1276 | + |
|
| 1277 | + |
|
| 1278 | + /** |
|
| 1279 | + * @param string $field_name |
|
| 1280 | + * @param string $dt_frmt |
|
| 1281 | + * @param string $tm_frmt |
|
| 1282 | + * @throws \EE_Error |
|
| 1283 | + */ |
|
| 1284 | + public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1285 | + { |
|
| 1286 | + $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + |
|
| 1290 | + |
|
| 1291 | + /** |
|
| 1292 | + * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1293 | + * |
|
| 1294 | + * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1295 | + * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1296 | + * on the object will be used. |
|
| 1297 | + * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1298 | + * @throws \EE_Error |
|
| 1299 | + * field name. |
|
| 1300 | + */ |
|
| 1301 | + public function get_i18n_datetime($field_name, $format = '') |
|
| 1302 | + { |
|
| 1303 | + $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1304 | + return date_i18n( |
|
| 1305 | + $format, |
|
| 1306 | + EEH_DTT_Helper::get_timestamp_with_offset($this->get_raw($field_name), $this->_timezone) |
|
| 1307 | + ); |
|
| 1308 | + } |
|
| 1309 | + |
|
| 1310 | + |
|
| 1311 | + |
|
| 1312 | + /** |
|
| 1313 | + * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1314 | + * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1315 | + * thrown. |
|
| 1316 | + * |
|
| 1317 | + * @param string $field_name The field name being checked |
|
| 1318 | + * @throws EE_Error |
|
| 1319 | + * @return EE_Datetime_Field |
|
| 1320 | + */ |
|
| 1321 | + protected function _get_dtt_field_settings($field_name) |
|
| 1322 | + { |
|
| 1323 | + $field = $this->get_model()->field_settings_for($field_name); |
|
| 1324 | + //check if field is dtt |
|
| 1325 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1326 | + return $field; |
|
| 1327 | + } else { |
|
| 1328 | + throw new EE_Error(sprintf(__('The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field. Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor', |
|
| 1329 | + 'event_espresso'), $field_name, self::_get_model_classname(get_class($this)))); |
|
| 1330 | + } |
|
| 1331 | + } |
|
| 1332 | + |
|
| 1333 | + |
|
| 1334 | + |
|
| 1335 | + |
|
| 1336 | + /** |
|
| 1337 | + * NOTE ABOUT BELOW: |
|
| 1338 | + * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1339 | + * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1340 | + * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1341 | + * method and make sure you send the entire datetime value for setting. |
|
| 1342 | + */ |
|
| 1343 | + /** |
|
| 1344 | + * sets the time on a datetime property |
|
| 1345 | + * |
|
| 1346 | + * @access protected |
|
| 1347 | + * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1348 | + * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1349 | + * @throws \EE_Error |
|
| 1350 | + */ |
|
| 1351 | + protected function _set_time_for($time, $fieldname) |
|
| 1352 | + { |
|
| 1353 | + $this->_set_date_time('T', $time, $fieldname); |
|
| 1354 | + } |
|
| 1355 | + |
|
| 1356 | + |
|
| 1357 | + |
|
| 1358 | + /** |
|
| 1359 | + * sets the date on a datetime property |
|
| 1360 | + * |
|
| 1361 | + * @access protected |
|
| 1362 | + * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1363 | + * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1364 | + * @throws \EE_Error |
|
| 1365 | + */ |
|
| 1366 | + protected function _set_date_for($date, $fieldname) |
|
| 1367 | + { |
|
| 1368 | + $this->_set_date_time('D', $date, $fieldname); |
|
| 1369 | + } |
|
| 1370 | + |
|
| 1371 | + |
|
| 1372 | + |
|
| 1373 | + /** |
|
| 1374 | + * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1375 | + * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1376 | + * |
|
| 1377 | + * @access protected |
|
| 1378 | + * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1379 | + * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1380 | + * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1381 | + * EE_Datetime_Field property) |
|
| 1382 | + * @throws \EE_Error |
|
| 1383 | + */ |
|
| 1384 | + protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1385 | + { |
|
| 1386 | + $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1387 | + $field->set_timezone($this->_timezone); |
|
| 1388 | + $field->set_date_format($this->_dt_frmt); |
|
| 1389 | + $field->set_time_format($this->_tm_frmt); |
|
| 1390 | + switch ($what) { |
|
| 1391 | + case 'T' : |
|
| 1392 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time( |
|
| 1393 | + $datetime_value, |
|
| 1394 | + $this->_fields[$fieldname] |
|
| 1395 | + ); |
|
| 1396 | + break; |
|
| 1397 | + case 'D' : |
|
| 1398 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date( |
|
| 1399 | + $datetime_value, |
|
| 1400 | + $this->_fields[$fieldname] |
|
| 1401 | + ); |
|
| 1402 | + break; |
|
| 1403 | + case 'B' : |
|
| 1404 | + $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value); |
|
| 1405 | + break; |
|
| 1406 | + } |
|
| 1407 | + $this->_clear_cached_property($fieldname); |
|
| 1408 | + } |
|
| 1409 | + |
|
| 1410 | + |
|
| 1411 | + |
|
| 1412 | + /** |
|
| 1413 | + * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1414 | + * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1415 | + * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1416 | + * that could lead to some unexpected results! |
|
| 1417 | + * |
|
| 1418 | + * @access public |
|
| 1419 | + * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1420 | + * value being returned. |
|
| 1421 | + * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1422 | + * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1423 | + * @param string $prepend You can include something to prepend on the timestamp |
|
| 1424 | + * @param string $append You can include something to append on the timestamp |
|
| 1425 | + * @throws EE_Error |
|
| 1426 | + * @return string timestamp |
|
| 1427 | + */ |
|
| 1428 | + public function display_in_my_timezone( |
|
| 1429 | + $field_name, |
|
| 1430 | + $callback = 'get_datetime', |
|
| 1431 | + $args = null, |
|
| 1432 | + $prepend = '', |
|
| 1433 | + $append = '' |
|
| 1434 | + ) { |
|
| 1435 | + $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1436 | + if ($timezone === $this->_timezone) { |
|
| 1437 | + return ''; |
|
| 1438 | + } |
|
| 1439 | + $original_timezone = $this->_timezone; |
|
| 1440 | + $this->set_timezone($timezone); |
|
| 1441 | + $fn = (array)$field_name; |
|
| 1442 | + $args = array_merge($fn, (array)$args); |
|
| 1443 | + if ( ! method_exists($this, $callback)) { |
|
| 1444 | + throw new EE_Error( |
|
| 1445 | + sprintf( |
|
| 1446 | + __( |
|
| 1447 | + 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1448 | + 'event_espresso' |
|
| 1449 | + ), |
|
| 1450 | + $callback |
|
| 1451 | + ) |
|
| 1452 | + ); |
|
| 1453 | + } |
|
| 1454 | + $args = (array)$args; |
|
| 1455 | + $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1456 | + $this->set_timezone($original_timezone); |
|
| 1457 | + return $return; |
|
| 1458 | + } |
|
| 1459 | + |
|
| 1460 | + |
|
| 1461 | + |
|
| 1462 | + /** |
|
| 1463 | + * Deletes this model object. |
|
| 1464 | + * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1465 | + * override |
|
| 1466 | + * `EE_Base_Class::_delete` NOT this class. |
|
| 1467 | + * |
|
| 1468 | + * @return boolean | int |
|
| 1469 | + * @throws \EE_Error |
|
| 1470 | + */ |
|
| 1471 | + public function delete() |
|
| 1472 | + { |
|
| 1473 | + /** |
|
| 1474 | + * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1475 | + * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1476 | + * should be aware that `_delete` may not always result in a permanent delete. For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1477 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1478 | + * |
|
| 1479 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1480 | + */ |
|
| 1481 | + do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1482 | + $result = $this->_delete(); |
|
| 1483 | + /** |
|
| 1484 | + * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1485 | + * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1486 | + * should be aware that `_delete` may not always result in a permanent delete. For example `EE_Soft_Base_Class::_delete` |
|
| 1487 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1488 | + * |
|
| 1489 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1490 | + * @param boolean $result |
|
| 1491 | + */ |
|
| 1492 | + do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1493 | + return $result; |
|
| 1494 | + } |
|
| 1495 | + |
|
| 1496 | + |
|
| 1497 | + |
|
| 1498 | + /** |
|
| 1499 | + * Calls the specific delete method for the instantiated class. |
|
| 1500 | + * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1501 | + * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1502 | + * `EE_Base_Class::delete` |
|
| 1503 | + * |
|
| 1504 | + * @return bool|int |
|
| 1505 | + * @throws \EE_Error |
|
| 1506 | + */ |
|
| 1507 | + protected function _delete() |
|
| 1508 | + { |
|
| 1509 | + return $this->delete_permanently(); |
|
| 1510 | + } |
|
| 1511 | + |
|
| 1512 | + |
|
| 1513 | + |
|
| 1514 | + /** |
|
| 1515 | + * Deletes this model object permanently from db (but keep in mind related models my block the delete and return an |
|
| 1516 | + * error) |
|
| 1517 | + * |
|
| 1518 | + * @return bool | int |
|
| 1519 | + * @throws \EE_Error |
|
| 1520 | + */ |
|
| 1521 | + public function delete_permanently() |
|
| 1522 | + { |
|
| 1523 | + /** |
|
| 1524 | + * Called just before HARD deleting a model object |
|
| 1525 | + * |
|
| 1526 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1527 | + */ |
|
| 1528 | + do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1529 | + $model = $this->get_model(); |
|
| 1530 | + $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1531 | + $this->refresh_cache_of_related_objects(); |
|
| 1532 | + /** |
|
| 1533 | + * Called just after HARD deleting a model object |
|
| 1534 | + * |
|
| 1535 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1536 | + * @param boolean $result |
|
| 1537 | + */ |
|
| 1538 | + do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1539 | + return $result; |
|
| 1540 | + } |
|
| 1541 | + |
|
| 1542 | + |
|
| 1543 | + |
|
| 1544 | + /** |
|
| 1545 | + * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1546 | + * related model objects |
|
| 1547 | + * |
|
| 1548 | + * @throws \EE_Error |
|
| 1549 | + */ |
|
| 1550 | + public function refresh_cache_of_related_objects() |
|
| 1551 | + { |
|
| 1552 | + $model = $this->get_model(); |
|
| 1553 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1554 | + if ( ! empty($this->_model_relations[$relation_name])) { |
|
| 1555 | + $related_objects = $this->_model_relations[$relation_name]; |
|
| 1556 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1557 | + //this relation only stores a single model object, not an array |
|
| 1558 | + //but let's make it consistent |
|
| 1559 | + $related_objects = array($related_objects); |
|
| 1560 | + } |
|
| 1561 | + foreach ($related_objects as $related_object) { |
|
| 1562 | + //only refresh their cache if they're in memory |
|
| 1563 | + if ($related_object instanceof EE_Base_Class) { |
|
| 1564 | + $related_object->clear_cache($model->get_this_model_name(), $this); |
|
| 1565 | + } |
|
| 1566 | + } |
|
| 1567 | + } |
|
| 1568 | + } |
|
| 1569 | + } |
|
| 1570 | + |
|
| 1571 | + |
|
| 1572 | + |
|
| 1573 | + /** |
|
| 1574 | + * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1575 | + * object just before saving. |
|
| 1576 | + * |
|
| 1577 | + * @access public |
|
| 1578 | + * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1579 | + * if provided during the save() method (often client code will change the fields' |
|
| 1580 | + * values before calling save) |
|
| 1581 | + * @throws \EE_Error |
|
| 1582 | + * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1583 | + * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1584 | + */ |
|
| 1585 | + public function save($set_cols_n_values = array()) |
|
| 1586 | + { |
|
| 1587 | + $model = $this->get_model(); |
|
| 1588 | + /** |
|
| 1589 | + * Filters the fields we're about to save on the model object |
|
| 1590 | + * |
|
| 1591 | + * @param array $set_cols_n_values |
|
| 1592 | + * @param EE_Base_Class $model_object |
|
| 1593 | + */ |
|
| 1594 | + $set_cols_n_values = (array)apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values, |
|
| 1595 | + $this); |
|
| 1596 | + //set attributes as provided in $set_cols_n_values |
|
| 1597 | + foreach ($set_cols_n_values as $column => $value) { |
|
| 1598 | + $this->set($column, $value); |
|
| 1599 | + } |
|
| 1600 | + // no changes ? then don't do anything |
|
| 1601 | + if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1602 | + return 0; |
|
| 1603 | + } |
|
| 1604 | + /** |
|
| 1605 | + * Saving a model object. |
|
| 1606 | + * Before we perform a save, this action is fired. |
|
| 1607 | + * |
|
| 1608 | + * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1609 | + */ |
|
| 1610 | + do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1611 | + if ( ! $this->allow_persist()) { |
|
| 1612 | + return 0; |
|
| 1613 | + } |
|
| 1614 | + //now get current attribute values |
|
| 1615 | + $save_cols_n_values = $this->_fields; |
|
| 1616 | + //if the object already has an ID, update it. Otherwise, insert it |
|
| 1617 | + //also: change the assumption about values passed to the model NOT being prepare dby the model object. They have been |
|
| 1618 | + $old_assumption_concerning_value_preparation = $model |
|
| 1619 | + ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1620 | + $model->assume_values_already_prepared_by_model_object(true); |
|
| 1621 | + //does this model have an autoincrement PK? |
|
| 1622 | + if ($model->has_primary_key_field()) { |
|
| 1623 | + if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1624 | + //ok check if it's set, if so: update; if not, insert |
|
| 1625 | + if ( ! empty($save_cols_n_values[$model->primary_key_name()])) { |
|
| 1626 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1627 | + } else { |
|
| 1628 | + unset($save_cols_n_values[$model->primary_key_name()]); |
|
| 1629 | + $results = $model->insert($save_cols_n_values); |
|
| 1630 | + if ($results) { |
|
| 1631 | + //if successful, set the primary key |
|
| 1632 | + //but don't use the normal SET method, because it will check if |
|
| 1633 | + //an item with the same ID exists in the mapper & db, then |
|
| 1634 | + //will find it in the db (because we just added it) and THAT object |
|
| 1635 | + //will get added to the mapper before we can add this one! |
|
| 1636 | + //but if we just avoid using the SET method, all that headache can be avoided |
|
| 1637 | + $pk_field_name = $model->primary_key_name(); |
|
| 1638 | + $this->_fields[$pk_field_name] = $results; |
|
| 1639 | + $this->_clear_cached_property($pk_field_name); |
|
| 1640 | + $model->add_to_entity_map($this); |
|
| 1641 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1642 | + } |
|
| 1643 | + } |
|
| 1644 | + } else {//PK is NOT auto-increment |
|
| 1645 | + //so check if one like it already exists in the db |
|
| 1646 | + if ($model->exists_by_ID($this->ID())) { |
|
| 1647 | + if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1648 | + throw new EE_Error( |
|
| 1649 | + sprintf( |
|
| 1650 | + __('Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s', |
|
| 1651 | + 'event_espresso'), |
|
| 1652 | + get_class($this), |
|
| 1653 | + get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1654 | + get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1655 | + '<br />' |
|
| 1656 | + ) |
|
| 1657 | + ); |
|
| 1658 | + } |
|
| 1659 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1660 | + } else { |
|
| 1661 | + $results = $model->insert($save_cols_n_values); |
|
| 1662 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1663 | + } |
|
| 1664 | + } |
|
| 1665 | + } else {//there is NO primary key |
|
| 1666 | + $already_in_db = false; |
|
| 1667 | + foreach ($model->unique_indexes() as $index) { |
|
| 1668 | + $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1669 | + if ($model->exists(array($uniqueness_where_params))) { |
|
| 1670 | + $already_in_db = true; |
|
| 1671 | + } |
|
| 1672 | + } |
|
| 1673 | + if ($already_in_db) { |
|
| 1674 | + $combined_pk_fields_n_values = array_intersect_key($save_cols_n_values, |
|
| 1675 | + $model->get_combined_primary_key_fields()); |
|
| 1676 | + $results = $model->update($save_cols_n_values, $combined_pk_fields_n_values); |
|
| 1677 | + } else { |
|
| 1678 | + $results = $model->insert($save_cols_n_values); |
|
| 1679 | + } |
|
| 1680 | + } |
|
| 1681 | + //restore the old assumption about values being prepared by the model object |
|
| 1682 | + $model |
|
| 1683 | + ->assume_values_already_prepared_by_model_object($old_assumption_concerning_value_preparation); |
|
| 1684 | + /** |
|
| 1685 | + * After saving the model object this action is called |
|
| 1686 | + * |
|
| 1687 | + * @param EE_Base_Class $model_object which was just saved |
|
| 1688 | + * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1689 | + * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1690 | + */ |
|
| 1691 | + do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1692 | + $this->_has_changes = false; |
|
| 1693 | + return $results; |
|
| 1694 | + } |
|
| 1695 | + |
|
| 1696 | + |
|
| 1697 | + |
|
| 1698 | + /** |
|
| 1699 | + * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1700 | + * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1701 | + * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1702 | + * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1703 | + * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
|
| 1704 | + * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1705 | + * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1706 | + * |
|
| 1707 | + * @return void |
|
| 1708 | + * @throws \EE_Error |
|
| 1709 | + */ |
|
| 1710 | + protected function _update_cached_related_model_objs_fks() |
|
| 1711 | + { |
|
| 1712 | + $model = $this->get_model(); |
|
| 1713 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1714 | + if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1715 | + foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1716 | + $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1717 | + $model->get_this_model_name() |
|
| 1718 | + ); |
|
| 1719 | + $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1720 | + if ($related_model_obj_in_cache->ID()) { |
|
| 1721 | + $related_model_obj_in_cache->save(); |
|
| 1722 | + } |
|
| 1723 | + } |
|
| 1724 | + } |
|
| 1725 | + } |
|
| 1726 | + } |
|
| 1727 | + |
|
| 1728 | + |
|
| 1729 | + |
|
| 1730 | + /** |
|
| 1731 | + * Saves this model object and its NEW cached relations to the database. |
|
| 1732 | + * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1733 | + * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1734 | + * because otherwise, there's a potential for infinite looping of saving |
|
| 1735 | + * Saves the cached related model objects, and ensures the relation between them |
|
| 1736 | + * and this object and properly setup |
|
| 1737 | + * |
|
| 1738 | + * @return int ID of new model object on save; 0 on failure+ |
|
| 1739 | + * @throws \EE_Error |
|
| 1740 | + */ |
|
| 1741 | + public function save_new_cached_related_model_objs() |
|
| 1742 | + { |
|
| 1743 | + //make sure this has been saved |
|
| 1744 | + if ( ! $this->ID()) { |
|
| 1745 | + $id = $this->save(); |
|
| 1746 | + } else { |
|
| 1747 | + $id = $this->ID(); |
|
| 1748 | + } |
|
| 1749 | + //now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1750 | + foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1751 | + if ($this->_model_relations[$relationName]) { |
|
| 1752 | + //is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1753 | + //or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1754 | + if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1755 | + //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1756 | + //but ONLY if it DOES NOT exist in the DB |
|
| 1757 | + /* @var $related_model_obj EE_Base_Class */ |
|
| 1758 | + $related_model_obj = $this->_model_relations[$relationName]; |
|
| 1759 | + // if( ! $related_model_obj->ID()){ |
|
| 1760 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1761 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1762 | + // } |
|
| 1763 | + } else { |
|
| 1764 | + foreach ($this->_model_relations[$relationName] as $related_model_obj) { |
|
| 1765 | + //add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1766 | + //but ONLY if it DOES NOT exist in the DB |
|
| 1767 | + // if( ! $related_model_obj->ID()){ |
|
| 1768 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1769 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1770 | + // } |
|
| 1771 | + } |
|
| 1772 | + } |
|
| 1773 | + } |
|
| 1774 | + } |
|
| 1775 | + return $id; |
|
| 1776 | + } |
|
| 1777 | + |
|
| 1778 | + |
|
| 1779 | + |
|
| 1780 | + /** |
|
| 1781 | + * for getting a model while instantiated. |
|
| 1782 | + * |
|
| 1783 | + * @return \EEM_Base | \EEM_CPT_Base |
|
| 1784 | + */ |
|
| 1785 | + public function get_model() |
|
| 1786 | + { |
|
| 1787 | + if( ! $this->_model){ |
|
| 1788 | + $modelName = self::_get_model_classname(get_class($this)); |
|
| 1789 | + $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 1790 | + } else { |
|
| 1791 | + $this->_model->set_timezone($this->_timezone); |
|
| 1792 | + } |
|
| 1793 | + |
|
| 1794 | + return $this->_model; |
|
| 1795 | + } |
|
| 1796 | + |
|
| 1797 | + |
|
| 1798 | + |
|
| 1799 | + /** |
|
| 1800 | + * @param $props_n_values |
|
| 1801 | + * @param $classname |
|
| 1802 | + * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 1803 | + * @throws \EE_Error |
|
| 1804 | + */ |
|
| 1805 | + protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 1806 | + { |
|
| 1807 | + //TODO: will not work for Term_Relationships because they have no PK! |
|
| 1808 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1809 | + if (array_key_exists($primary_id_ref, $props_n_values) && ! empty($props_n_values[$primary_id_ref])) { |
|
| 1810 | + $id = $props_n_values[$primary_id_ref]; |
|
| 1811 | + return self::_get_model($classname)->get_from_entity_map($id); |
|
| 1812 | + } |
|
| 1813 | + return false; |
|
| 1814 | + } |
|
| 1815 | + |
|
| 1816 | + |
|
| 1817 | + |
|
| 1818 | + /** |
|
| 1819 | + * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 1820 | + * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 1821 | + * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
|
| 1822 | + * we return false. |
|
| 1823 | + * |
|
| 1824 | + * @param array $props_n_values incoming array of properties and their values |
|
| 1825 | + * @param string $classname the classname of the child class |
|
| 1826 | + * @param null $timezone |
|
| 1827 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 1828 | + * date_format and the second value is the time format |
|
| 1829 | + * @return mixed (EE_Base_Class|bool) |
|
| 1830 | + * @throws \EE_Error |
|
| 1831 | + */ |
|
| 1832 | + protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 1833 | + { |
|
| 1834 | + $existing = null; |
|
| 1835 | + $model = self::_get_model($classname, $timezone); |
|
| 1836 | + if ($model->has_primary_key_field()) { |
|
| 1837 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 1838 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 1839 | + && ! empty($props_n_values[$primary_id_ref]) |
|
| 1840 | + ) { |
|
| 1841 | + $existing = $model->get_one_by_ID( |
|
| 1842 | + $props_n_values[$primary_id_ref] |
|
| 1843 | + ); |
|
| 1844 | + } |
|
| 1845 | + } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 1846 | + //no primary key on this model, but there's still a matching item in the DB |
|
| 1847 | + $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 1848 | + self::_get_model($classname, $timezone)->get_index_primary_key_string($props_n_values) |
|
| 1849 | + ); |
|
| 1850 | + } |
|
| 1851 | + if ($existing) { |
|
| 1852 | + //set date formats if present before setting values |
|
| 1853 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 1854 | + $existing->set_date_format($date_formats[0]); |
|
| 1855 | + $existing->set_time_format($date_formats[1]); |
|
| 1856 | + } else { |
|
| 1857 | + //set default formats for date and time |
|
| 1858 | + $existing->set_date_format(get_option('date_format')); |
|
| 1859 | + $existing->set_time_format(get_option('time_format')); |
|
| 1860 | + } |
|
| 1861 | + foreach ($props_n_values as $property => $field_value) { |
|
| 1862 | + $existing->set($property, $field_value); |
|
| 1863 | + } |
|
| 1864 | + return $existing; |
|
| 1865 | + } else { |
|
| 1866 | + return false; |
|
| 1867 | + } |
|
| 1868 | + } |
|
| 1869 | + |
|
| 1870 | + |
|
| 1871 | + |
|
| 1872 | + /** |
|
| 1873 | + * Gets the EEM_*_Model for this class |
|
| 1874 | + * |
|
| 1875 | + * @access public now, as this is more convenient |
|
| 1876 | + * @param $classname |
|
| 1877 | + * @param null $timezone |
|
| 1878 | + * @throws EE_Error |
|
| 1879 | + * @return EEM_Base |
|
| 1880 | + */ |
|
| 1881 | + protected static function _get_model($classname, $timezone = null) |
|
| 1882 | + { |
|
| 1883 | + //find model for this class |
|
| 1884 | + if ( ! $classname) { |
|
| 1885 | + throw new EE_Error( |
|
| 1886 | + sprintf( |
|
| 1887 | + __( |
|
| 1888 | + "What were you thinking calling _get_model(%s)?? You need to specify the class name", |
|
| 1889 | + "event_espresso" |
|
| 1890 | + ), |
|
| 1891 | + $classname |
|
| 1892 | + ) |
|
| 1893 | + ); |
|
| 1894 | + } |
|
| 1895 | + $modelName = self::_get_model_classname($classname); |
|
| 1896 | + return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 1897 | + } |
|
| 1898 | + |
|
| 1899 | + |
|
| 1900 | + |
|
| 1901 | + /** |
|
| 1902 | + * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 1903 | + * |
|
| 1904 | + * @param string $model_classname |
|
| 1905 | + * @param null $timezone |
|
| 1906 | + * @return EEM_Base |
|
| 1907 | + */ |
|
| 1908 | + protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 1909 | + { |
|
| 1910 | + $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 1911 | + $model = EE_Registry::instance()->load_model($model_classname); |
|
| 1912 | + $model->set_timezone($timezone); |
|
| 1913 | + return $model; |
|
| 1914 | + } |
|
| 1915 | + |
|
| 1916 | + |
|
| 1917 | + |
|
| 1918 | + /** |
|
| 1919 | + * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 1920 | + * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 1921 | + * |
|
| 1922 | + * @param null $model_name |
|
| 1923 | + * @return string like EEM_Attendee |
|
| 1924 | + */ |
|
| 1925 | + private static function _get_model_classname($model_name = null) |
|
| 1926 | + { |
|
| 1927 | + if (strpos($model_name, "EE_") === 0) { |
|
| 1928 | + $model_classname = str_replace("EE_", "EEM_", $model_name); |
|
| 1929 | + } else { |
|
| 1930 | + $model_classname = "EEM_" . $model_name; |
|
| 1931 | + } |
|
| 1932 | + return $model_classname; |
|
| 1933 | + } |
|
| 1934 | + |
|
| 1935 | + |
|
| 1936 | + |
|
| 1937 | + /** |
|
| 1938 | + * returns the name of the primary key attribute |
|
| 1939 | + * |
|
| 1940 | + * @param null $classname |
|
| 1941 | + * @throws EE_Error |
|
| 1942 | + * @return string |
|
| 1943 | + */ |
|
| 1944 | + protected static function _get_primary_key_name($classname = null) |
|
| 1945 | + { |
|
| 1946 | + if ( ! $classname) { |
|
| 1947 | + throw new EE_Error( |
|
| 1948 | + sprintf( |
|
| 1949 | + __("What were you thinking calling _get_primary_key_name(%s)", "event_espresso"), |
|
| 1950 | + $classname |
|
| 1951 | + ) |
|
| 1952 | + ); |
|
| 1953 | + } |
|
| 1954 | + return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 1955 | + } |
|
| 1956 | + |
|
| 1957 | + |
|
| 1958 | + |
|
| 1959 | + /** |
|
| 1960 | + * Gets the value of the primary key. |
|
| 1961 | + * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 1962 | + * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 1963 | + * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 1964 | + * |
|
| 1965 | + * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 1966 | + * @throws \EE_Error |
|
| 1967 | + */ |
|
| 1968 | + public function ID() |
|
| 1969 | + { |
|
| 1970 | + $model = $this->get_model(); |
|
| 1971 | + //now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 1972 | + if ($model->has_primary_key_field()) { |
|
| 1973 | + return $this->_fields[$model->primary_key_name()]; |
|
| 1974 | + } else { |
|
| 1975 | + return $model->get_index_primary_key_string($this->_fields); |
|
| 1976 | + } |
|
| 1977 | + } |
|
| 1978 | + |
|
| 1979 | + |
|
| 1980 | + |
|
| 1981 | + /** |
|
| 1982 | + * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 1983 | + * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 1984 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 1985 | + * |
|
| 1986 | + * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 1987 | + * @param string $relationName eg 'Events','Question',etc. |
|
| 1988 | + * an attendee to a group, you also want to specify which role they |
|
| 1989 | + * will have in that group. So you would use this parameter to |
|
| 1990 | + * specify array('role-column-name'=>'role-id') |
|
| 1991 | + * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 1992 | + * allow you to further constrict the relation to being added. |
|
| 1993 | + * However, keep in mind that the columns (keys) given must match a |
|
| 1994 | + * column on the JOIN table and currently only the HABTM models |
|
| 1995 | + * accept these additional conditions. Also remember that if an |
|
| 1996 | + * exact match isn't found for these extra cols/val pairs, then a |
|
| 1997 | + * NEW row is created in the join table. |
|
| 1998 | + * @param null $cache_id |
|
| 1999 | + * @throws EE_Error |
|
| 2000 | + * @return EE_Base_Class the object the relation was added to |
|
| 2001 | + */ |
|
| 2002 | + public function _add_relation_to( |
|
| 2003 | + $otherObjectModelObjectOrID, |
|
| 2004 | + $relationName, |
|
| 2005 | + $extra_join_model_fields_n_values = array(), |
|
| 2006 | + $cache_id = null |
|
| 2007 | + ) { |
|
| 2008 | + $model = $this->get_model(); |
|
| 2009 | + //if this thing exists in the DB, save the relation to the DB |
|
| 2010 | + if ($this->ID()) { |
|
| 2011 | + $otherObject = $model |
|
| 2012 | + ->add_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 2013 | + $extra_join_model_fields_n_values); |
|
| 2014 | + //clear cache so future get_many_related and get_first_related() return new results. |
|
| 2015 | + $this->clear_cache($relationName, $otherObject, true); |
|
| 2016 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2017 | + $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2018 | + } |
|
| 2019 | + } else { |
|
| 2020 | + //this thing doesn't exist in the DB, so just cache it |
|
| 2021 | + if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2022 | + throw new EE_Error(sprintf( |
|
| 2023 | + __('Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s', |
|
| 2024 | + 'event_espresso'), |
|
| 2025 | + $otherObjectModelObjectOrID, |
|
| 2026 | + get_class($this) |
|
| 2027 | + )); |
|
| 2028 | + } else { |
|
| 2029 | + $otherObject = $otherObjectModelObjectOrID; |
|
| 2030 | + } |
|
| 2031 | + $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2032 | + } |
|
| 2033 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2034 | + //fix the reciprocal relation too |
|
| 2035 | + if ($otherObject->ID()) { |
|
| 2036 | + //its saved so assumed relations exist in the DB, so we can just |
|
| 2037 | + //clear the cache so future queries use the updated info in the DB |
|
| 2038 | + $otherObject->clear_cache($model->get_this_model_name(), null, true); |
|
| 2039 | + } else { |
|
| 2040 | + //it's not saved, so it caches relations like this |
|
| 2041 | + $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2042 | + } |
|
| 2043 | + } |
|
| 2044 | + return $otherObject; |
|
| 2045 | + } |
|
| 2046 | + |
|
| 2047 | + |
|
| 2048 | + |
|
| 2049 | + /** |
|
| 2050 | + * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2051 | + * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2052 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2053 | + * from the cache |
|
| 2054 | + * |
|
| 2055 | + * @param mixed $otherObjectModelObjectOrID |
|
| 2056 | + * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2057 | + * to the DB yet |
|
| 2058 | + * @param string $relationName |
|
| 2059 | + * @param array $where_query |
|
| 2060 | + * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2061 | + * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2062 | + * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2063 | + * remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is |
|
| 2064 | + * created in the join table. |
|
| 2065 | + * @return EE_Base_Class the relation was removed from |
|
| 2066 | + * @throws \EE_Error |
|
| 2067 | + */ |
|
| 2068 | + public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2069 | + { |
|
| 2070 | + if ($this->ID()) { |
|
| 2071 | + //if this exists in the DB, save the relation change to the DB too |
|
| 2072 | + $otherObject = $this->get_model() |
|
| 2073 | + ->remove_relationship_to($this, $otherObjectModelObjectOrID, $relationName, |
|
| 2074 | + $where_query); |
|
| 2075 | + $this->clear_cache($relationName, $otherObject); |
|
| 2076 | + } else { |
|
| 2077 | + //this doesn't exist in the DB, just remove it from the cache |
|
| 2078 | + $otherObject = $this->clear_cache($relationName, $otherObjectModelObjectOrID); |
|
| 2079 | + } |
|
| 2080 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2081 | + $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2082 | + } |
|
| 2083 | + return $otherObject; |
|
| 2084 | + } |
|
| 2085 | + |
|
| 2086 | + |
|
| 2087 | + |
|
| 2088 | + /** |
|
| 2089 | + * Removes ALL the related things for the $relationName. |
|
| 2090 | + * |
|
| 2091 | + * @param string $relationName |
|
| 2092 | + * @param array $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions) |
|
| 2093 | + * @return EE_Base_Class |
|
| 2094 | + * @throws \EE_Error |
|
| 2095 | + */ |
|
| 2096 | + public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2097 | + { |
|
| 2098 | + if ($this->ID()) { |
|
| 2099 | + //if this exists in the DB, save the relation change to the DB too |
|
| 2100 | + $otherObjects = $this->get_model()->remove_relations($this, $relationName, $where_query_params); |
|
| 2101 | + $this->clear_cache($relationName, null, true); |
|
| 2102 | + } else { |
|
| 2103 | + //this doesn't exist in the DB, just remove it from the cache |
|
| 2104 | + $otherObjects = $this->clear_cache($relationName, null, true); |
|
| 2105 | + } |
|
| 2106 | + if (is_array($otherObjects)) { |
|
| 2107 | + foreach ($otherObjects as $otherObject) { |
|
| 2108 | + $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this); |
|
| 2109 | + } |
|
| 2110 | + } |
|
| 2111 | + return $otherObjects; |
|
| 2112 | + } |
|
| 2113 | + |
|
| 2114 | + |
|
| 2115 | + |
|
| 2116 | + /** |
|
| 2117 | + * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2118 | + * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2119 | + * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2120 | + * because we want to get even deleted items etc. |
|
| 2121 | + * |
|
| 2122 | + * @param string $relationName key in the model's _model_relations array |
|
| 2123 | + * @param array $query_params like EEM_Base::get_all |
|
| 2124 | + * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary keys |
|
| 2125 | + * @throws \EE_Error |
|
| 2126 | + * or might not be saved yet. Consider using EEM_Base::get_IDs() on these results if |
|
| 2127 | + * you want IDs |
|
| 2128 | + */ |
|
| 2129 | + public function get_many_related($relationName, $query_params = array()) |
|
| 2130 | + { |
|
| 2131 | + if ($this->ID()) { |
|
| 2132 | + //this exists in the DB, so get the related things from either the cache or the DB |
|
| 2133 | + //if there are query parameters, forget about caching the related model objects. |
|
| 2134 | + if ($query_params) { |
|
| 2135 | + $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2136 | + } else { |
|
| 2137 | + //did we already cache the result of this query? |
|
| 2138 | + $cached_results = $this->get_all_from_cache($relationName); |
|
| 2139 | + if ( ! $cached_results) { |
|
| 2140 | + $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params); |
|
| 2141 | + //if no query parameters were passed, then we got all the related model objects |
|
| 2142 | + //for that relation. We can cache them then. |
|
| 2143 | + foreach ($related_model_objects as $related_model_object) { |
|
| 2144 | + $this->cache($relationName, $related_model_object); |
|
| 2145 | + } |
|
| 2146 | + } else { |
|
| 2147 | + $related_model_objects = $cached_results; |
|
| 2148 | + } |
|
| 2149 | + } |
|
| 2150 | + } else { |
|
| 2151 | + //this doesn't exist in the DB, so just get the related things from the cache |
|
| 2152 | + $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2153 | + } |
|
| 2154 | + return $related_model_objects; |
|
| 2155 | + } |
|
| 2156 | + |
|
| 2157 | + |
|
| 2158 | + |
|
| 2159 | + /** |
|
| 2160 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2161 | + * unless otherwise specified in the $query_params |
|
| 2162 | + * |
|
| 2163 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2164 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2165 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2166 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2167 | + * that by the setting $distinct to TRUE; |
|
| 2168 | + * @return int |
|
| 2169 | + */ |
|
| 2170 | + public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2171 | + { |
|
| 2172 | + return $this->get_model()->count_related($this, $relation_name, $query_params, $field_to_count, $distinct); |
|
| 2173 | + } |
|
| 2174 | + |
|
| 2175 | + |
|
| 2176 | + |
|
| 2177 | + /** |
|
| 2178 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2179 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2180 | + * |
|
| 2181 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2182 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2183 | + * @param string $field_to_sum name of field to count by. |
|
| 2184 | + * By default, uses primary key (which doesn't make much sense, so you should probably |
|
| 2185 | + * change it) |
|
| 2186 | + * @return int |
|
| 2187 | + */ |
|
| 2188 | + public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2189 | + { |
|
| 2190 | + return $this->get_model()->sum_related($this, $relation_name, $query_params, $field_to_sum); |
|
| 2191 | + } |
|
| 2192 | + |
|
| 2193 | + |
|
| 2194 | + |
|
| 2195 | + /** |
|
| 2196 | + * Gets the first (ie, one) related model object of the specified type. |
|
| 2197 | + * |
|
| 2198 | + * @param string $relationName key in the model's _model_relations array |
|
| 2199 | + * @param array $query_params like EEM_Base::get_all |
|
| 2200 | + * @return EE_Base_Class (not an array, a single object) |
|
| 2201 | + * @throws \EE_Error |
|
| 2202 | + */ |
|
| 2203 | + public function get_first_related($relationName, $query_params = array()) |
|
| 2204 | + { |
|
| 2205 | + $model = $this->get_model(); |
|
| 2206 | + if ($this->ID()) {//this exists in the DB, get from the cache OR the DB |
|
| 2207 | + //if they've provided some query parameters, don't bother trying to cache the result |
|
| 2208 | + //also make sure we're not caching the result of get_first_related |
|
| 2209 | + //on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2210 | + if ($query_params |
|
| 2211 | + || ! $model->related_settings_for($relationName) |
|
| 2212 | + instanceof |
|
| 2213 | + EE_Belongs_To_Relation |
|
| 2214 | + ) { |
|
| 2215 | + $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2216 | + } else { |
|
| 2217 | + //first, check if we've already cached the result of this query |
|
| 2218 | + $cached_result = $this->get_one_from_cache($relationName); |
|
| 2219 | + if ( ! $cached_result) { |
|
| 2220 | + $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2221 | + $this->cache($relationName, $related_model_object); |
|
| 2222 | + } else { |
|
| 2223 | + $related_model_object = $cached_result; |
|
| 2224 | + } |
|
| 2225 | + } |
|
| 2226 | + } else { |
|
| 2227 | + $related_model_object = null; |
|
| 2228 | + //this doesn't exist in the Db, but maybe the relation is of type belongs to, and so the related thing might |
|
| 2229 | + if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2230 | + $related_model_object = $model->get_first_related($this, $relationName, $query_params); |
|
| 2231 | + } |
|
| 2232 | + //this doesn't exist in the DB and apparently the thing it belongs to doesn't either, just get what's cached on this object |
|
| 2233 | + if ( ! $related_model_object) { |
|
| 2234 | + $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2235 | + } |
|
| 2236 | + } |
|
| 2237 | + return $related_model_object; |
|
| 2238 | + } |
|
| 2239 | + |
|
| 2240 | + |
|
| 2241 | + |
|
| 2242 | + /** |
|
| 2243 | + * Does a delete on all related objects of type $relationName and removes |
|
| 2244 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2245 | + * of blocking related model objects) does nothing. If the related model objects are |
|
| 2246 | + * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2247 | + * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2248 | + * |
|
| 2249 | + * @param string $relationName |
|
| 2250 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2251 | + * @return int how many deleted |
|
| 2252 | + * @throws \EE_Error |
|
| 2253 | + */ |
|
| 2254 | + public function delete_related($relationName, $query_params = array()) |
|
| 2255 | + { |
|
| 2256 | + if ($this->ID()) { |
|
| 2257 | + $count = $this->get_model()->delete_related($this, $relationName, $query_params); |
|
| 2258 | + } else { |
|
| 2259 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2260 | + $this->clear_cache($relationName, null, true); |
|
| 2261 | + } |
|
| 2262 | + return $count; |
|
| 2263 | + } |
|
| 2264 | + |
|
| 2265 | + |
|
| 2266 | + |
|
| 2267 | + /** |
|
| 2268 | + * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2269 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2270 | + * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2271 | + * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2272 | + * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2273 | + * |
|
| 2274 | + * @param string $relationName |
|
| 2275 | + * @param array $query_params like EEM_Base::get_all's |
|
| 2276 | + * @return int how many deleted (including those soft deleted) |
|
| 2277 | + * @throws \EE_Error |
|
| 2278 | + */ |
|
| 2279 | + public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2280 | + { |
|
| 2281 | + if ($this->ID()) { |
|
| 2282 | + $count = $this->get_model()->delete_related_permanently($this, $relationName, $query_params); |
|
| 2283 | + } else { |
|
| 2284 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2285 | + } |
|
| 2286 | + $this->clear_cache($relationName, null, true); |
|
| 2287 | + return $count; |
|
| 2288 | + } |
|
| 2289 | + |
|
| 2290 | + |
|
| 2291 | + |
|
| 2292 | + /** |
|
| 2293 | + * is_set |
|
| 2294 | + * Just a simple utility function children can use for checking if property exists |
|
| 2295 | + * |
|
| 2296 | + * @access public |
|
| 2297 | + * @param string $field_name property to check |
|
| 2298 | + * @return bool TRUE if existing,FALSE if not. |
|
| 2299 | + */ |
|
| 2300 | + public function is_set($field_name) |
|
| 2301 | + { |
|
| 2302 | + return isset($this->_fields[$field_name]); |
|
| 2303 | + } |
|
| 2304 | + |
|
| 2305 | + |
|
| 2306 | + |
|
| 2307 | + /** |
|
| 2308 | + * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2309 | + * EE_Error exception if they don't |
|
| 2310 | + * |
|
| 2311 | + * @param mixed (string|array) $properties properties to check |
|
| 2312 | + * @throws EE_Error |
|
| 2313 | + * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2314 | + */ |
|
| 2315 | + protected function _property_exists($properties) |
|
| 2316 | + { |
|
| 2317 | + foreach ((array)$properties as $property_name) { |
|
| 2318 | + //first make sure this property exists |
|
| 2319 | + if ( ! $this->_fields[$property_name]) { |
|
| 2320 | + throw new EE_Error( |
|
| 2321 | + sprintf( |
|
| 2322 | + __( |
|
| 2323 | + 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2324 | + 'event_espresso' |
|
| 2325 | + ), |
|
| 2326 | + $property_name |
|
| 2327 | + ) |
|
| 2328 | + ); |
|
| 2329 | + } |
|
| 2330 | + } |
|
| 2331 | + return true; |
|
| 2332 | + } |
|
| 2333 | + |
|
| 2334 | + |
|
| 2335 | + |
|
| 2336 | + /** |
|
| 2337 | + * This simply returns an array of model fields for this object |
|
| 2338 | + * |
|
| 2339 | + * @return array |
|
| 2340 | + * @throws \EE_Error |
|
| 2341 | + */ |
|
| 2342 | + public function model_field_array() |
|
| 2343 | + { |
|
| 2344 | + $fields = $this->get_model()->field_settings(false); |
|
| 2345 | + $properties = array(); |
|
| 2346 | + //remove prepended underscore |
|
| 2347 | + foreach ($fields as $field_name => $settings) { |
|
| 2348 | + $properties[$field_name] = $this->get($field_name); |
|
| 2349 | + } |
|
| 2350 | + return $properties; |
|
| 2351 | + } |
|
| 2352 | + |
|
| 2353 | + |
|
| 2354 | + |
|
| 2355 | + /** |
|
| 2356 | + * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2357 | + * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2358 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
|
| 2359 | + * requiring a plugin to extend the EE_Base_Class (which works fine is there's only 1 plugin, but when will that |
|
| 2360 | + * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
|
| 2361 | + * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
|
| 2362 | + * was called, and an array of the original arguments passed to the function. Whatever their callback function |
|
| 2363 | + * returns will be returned by this function. Example: in functions.php (or in a plugin): |
|
| 2364 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
|
| 2365 | + * my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2366 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2367 | + * return $previousReturnValue.$returnString; |
|
| 2368 | + * } |
|
| 2369 | + * require('EE_Answer.class.php'); |
|
| 2370 | + * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2371 | + * echo $answer->my_callback('monkeys',100); |
|
| 2372 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2373 | + * |
|
| 2374 | + * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2375 | + * @param array $args array of original arguments passed to the function |
|
| 2376 | + * @throws EE_Error |
|
| 2377 | + * @return mixed whatever the plugin which calls add_filter decides |
|
| 2378 | + */ |
|
| 2379 | + public function __call($methodName, $args) |
|
| 2380 | + { |
|
| 2381 | + $className = get_class($this); |
|
| 2382 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2383 | + if ( ! has_filter($tagName)) { |
|
| 2384 | + throw new EE_Error( |
|
| 2385 | + sprintf( |
|
| 2386 | + __( |
|
| 2387 | + "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}", |
|
| 2388 | + "event_espresso" |
|
| 2389 | + ), |
|
| 2390 | + $methodName, |
|
| 2391 | + $className, |
|
| 2392 | + $tagName |
|
| 2393 | + ) |
|
| 2394 | + ); |
|
| 2395 | + } |
|
| 2396 | + return apply_filters($tagName, null, $this, $args); |
|
| 2397 | + } |
|
| 2398 | + |
|
| 2399 | + |
|
| 2400 | + |
|
| 2401 | + /** |
|
| 2402 | + * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2403 | + * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2404 | + * |
|
| 2405 | + * @param string $meta_key |
|
| 2406 | + * @param mixed $meta_value |
|
| 2407 | + * @param mixed $previous_value |
|
| 2408 | + * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2409 | + * @throws \EE_Error |
|
| 2410 | + * NOTE: if the values haven't changed, returns 0 |
|
| 2411 | + */ |
|
| 2412 | + public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2413 | + { |
|
| 2414 | + $query_params = array( |
|
| 2415 | + array( |
|
| 2416 | + 'EXM_key' => $meta_key, |
|
| 2417 | + 'OBJ_ID' => $this->ID(), |
|
| 2418 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2419 | + ), |
|
| 2420 | + ); |
|
| 2421 | + if ($previous_value !== null) { |
|
| 2422 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2423 | + } |
|
| 2424 | + $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2425 | + if ( ! $existing_rows_like_that) { |
|
| 2426 | + return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2427 | + } |
|
| 2428 | + foreach ($existing_rows_like_that as $existing_row) { |
|
| 2429 | + $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2430 | + } |
|
| 2431 | + return count($existing_rows_like_that); |
|
| 2432 | + } |
|
| 2433 | + |
|
| 2434 | + |
|
| 2435 | + |
|
| 2436 | + /** |
|
| 2437 | + * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2438 | + * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2439 | + * extra meta row was entered, false if not |
|
| 2440 | + * |
|
| 2441 | + * @param string $meta_key |
|
| 2442 | + * @param mixed $meta_value |
|
| 2443 | + * @param boolean $unique |
|
| 2444 | + * @return boolean |
|
| 2445 | + * @throws \EE_Error |
|
| 2446 | + */ |
|
| 2447 | + public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2448 | + { |
|
| 2449 | + if ($unique) { |
|
| 2450 | + $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2451 | + array( |
|
| 2452 | + array( |
|
| 2453 | + 'EXM_key' => $meta_key, |
|
| 2454 | + 'OBJ_ID' => $this->ID(), |
|
| 2455 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2456 | + ), |
|
| 2457 | + ) |
|
| 2458 | + ); |
|
| 2459 | + if ($existing_extra_meta) { |
|
| 2460 | + return false; |
|
| 2461 | + } |
|
| 2462 | + } |
|
| 2463 | + $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2464 | + array( |
|
| 2465 | + 'EXM_key' => $meta_key, |
|
| 2466 | + 'EXM_value' => $meta_value, |
|
| 2467 | + 'OBJ_ID' => $this->ID(), |
|
| 2468 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2469 | + ) |
|
| 2470 | + ); |
|
| 2471 | + $new_extra_meta->save(); |
|
| 2472 | + return true; |
|
| 2473 | + } |
|
| 2474 | + |
|
| 2475 | + |
|
| 2476 | + |
|
| 2477 | + /** |
|
| 2478 | + * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2479 | + * is specified, only deletes extra meta records with that value. |
|
| 2480 | + * |
|
| 2481 | + * @param string $meta_key |
|
| 2482 | + * @param mixed $meta_value |
|
| 2483 | + * @return int number of extra meta rows deleted |
|
| 2484 | + * @throws \EE_Error |
|
| 2485 | + */ |
|
| 2486 | + public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2487 | + { |
|
| 2488 | + $query_params = array( |
|
| 2489 | + array( |
|
| 2490 | + 'EXM_key' => $meta_key, |
|
| 2491 | + 'OBJ_ID' => $this->ID(), |
|
| 2492 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2493 | + ), |
|
| 2494 | + ); |
|
| 2495 | + if ($meta_value !== null) { |
|
| 2496 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2497 | + } |
|
| 2498 | + return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2499 | + } |
|
| 2500 | + |
|
| 2501 | + |
|
| 2502 | + |
|
| 2503 | + /** |
|
| 2504 | + * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2505 | + * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2506 | + * You can specify $default is case you haven't found the extra meta |
|
| 2507 | + * |
|
| 2508 | + * @param string $meta_key |
|
| 2509 | + * @param boolean $single |
|
| 2510 | + * @param mixed $default if we don't find anything, what should we return? |
|
| 2511 | + * @return mixed single value if $single; array if ! $single |
|
| 2512 | + * @throws \EE_Error |
|
| 2513 | + */ |
|
| 2514 | + public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2515 | + { |
|
| 2516 | + if ($single) { |
|
| 2517 | + $result = $this->get_first_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2518 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2519 | + return $result->value(); |
|
| 2520 | + } |
|
| 2521 | + } else { |
|
| 2522 | + $results = $this->get_many_related('Extra_Meta', array(array('EXM_key' => $meta_key))); |
|
| 2523 | + if ($results) { |
|
| 2524 | + $values = array(); |
|
| 2525 | + foreach ($results as $result) { |
|
| 2526 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2527 | + $values[$result->ID()] = $result->value(); |
|
| 2528 | + } |
|
| 2529 | + } |
|
| 2530 | + return $values; |
|
| 2531 | + } |
|
| 2532 | + } |
|
| 2533 | + //if nothing discovered yet return default. |
|
| 2534 | + return apply_filters( |
|
| 2535 | + 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
| 2536 | + $default, |
|
| 2537 | + $meta_key, |
|
| 2538 | + $single, |
|
| 2539 | + $this |
|
| 2540 | + ); |
|
| 2541 | + } |
|
| 2542 | + |
|
| 2543 | + |
|
| 2544 | + |
|
| 2545 | + /** |
|
| 2546 | + * Returns a simple array of all the extra meta associated with this model object. |
|
| 2547 | + * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2548 | + * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2549 | + * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2550 | + * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2551 | + * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2552 | + * finally the extra meta's value as each sub-value. (eg |
|
| 2553 | + * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2554 | + * |
|
| 2555 | + * @param boolean $one_of_each_key |
|
| 2556 | + * @return array |
|
| 2557 | + * @throws \EE_Error |
|
| 2558 | + */ |
|
| 2559 | + public function all_extra_meta_array($one_of_each_key = true) |
|
| 2560 | + { |
|
| 2561 | + $return_array = array(); |
|
| 2562 | + if ($one_of_each_key) { |
|
| 2563 | + $extra_meta_objs = $this->get_many_related('Extra_Meta', array('group_by' => 'EXM_key')); |
|
| 2564 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2565 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2566 | + $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value(); |
|
| 2567 | + } |
|
| 2568 | + } |
|
| 2569 | + } else { |
|
| 2570 | + $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2571 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2572 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2573 | + if ( ! isset($return_array[$extra_meta_obj->key()])) { |
|
| 2574 | + $return_array[$extra_meta_obj->key()] = array(); |
|
| 2575 | + } |
|
| 2576 | + $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value(); |
|
| 2577 | + } |
|
| 2578 | + } |
|
| 2579 | + } |
|
| 2580 | + return $return_array; |
|
| 2581 | + } |
|
| 2582 | + |
|
| 2583 | + |
|
| 2584 | + |
|
| 2585 | + /** |
|
| 2586 | + * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2587 | + * |
|
| 2588 | + * @return string |
|
| 2589 | + * @throws \EE_Error |
|
| 2590 | + */ |
|
| 2591 | + public function name() |
|
| 2592 | + { |
|
| 2593 | + //find a field that's not a text field |
|
| 2594 | + $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2595 | + if ($field_we_can_use) { |
|
| 2596 | + return $this->get($field_we_can_use->get_name()); |
|
| 2597 | + } else { |
|
| 2598 | + $first_few_properties = $this->model_field_array(); |
|
| 2599 | + $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2600 | + $name_parts = array(); |
|
| 2601 | + foreach ($first_few_properties as $name => $value) { |
|
| 2602 | + $name_parts[] = "$name:$value"; |
|
| 2603 | + } |
|
| 2604 | + return implode(",", $name_parts); |
|
| 2605 | + } |
|
| 2606 | + } |
|
| 2607 | + |
|
| 2608 | + |
|
| 2609 | + |
|
| 2610 | + /** |
|
| 2611 | + * in_entity_map |
|
| 2612 | + * Checks if this model object has been proven to already be in the entity map |
|
| 2613 | + * |
|
| 2614 | + * @return boolean |
|
| 2615 | + * @throws \EE_Error |
|
| 2616 | + */ |
|
| 2617 | + public function in_entity_map() |
|
| 2618 | + { |
|
| 2619 | + if ($this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this) { |
|
| 2620 | + //well, if we looked, did we find it in the entity map? |
|
| 2621 | + return true; |
|
| 2622 | + } else { |
|
| 2623 | + return false; |
|
| 2624 | + } |
|
| 2625 | + } |
|
| 2626 | + |
|
| 2627 | + |
|
| 2628 | + |
|
| 2629 | + /** |
|
| 2630 | + * refresh_from_db |
|
| 2631 | + * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 2632 | + * |
|
| 2633 | + * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 2634 | + * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 2635 | + */ |
|
| 2636 | + public function refresh_from_db() |
|
| 2637 | + { |
|
| 2638 | + if ($this->ID() && $this->in_entity_map()) { |
|
| 2639 | + $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 2640 | + } else { |
|
| 2641 | + //if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 2642 | + //if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 2643 | + //that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 2644 | + //absolutely nothing in it for this ID |
|
| 2645 | + if (WP_DEBUG) { |
|
| 2646 | + throw new EE_Error( |
|
| 2647 | + sprintf( |
|
| 2648 | + __('Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.', |
|
| 2649 | + 'event_espresso'), |
|
| 2650 | + $this->ID(), |
|
| 2651 | + get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 2652 | + get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 2653 | + ) |
|
| 2654 | + ); |
|
| 2655 | + } |
|
| 2656 | + } |
|
| 2657 | + } |
|
| 2658 | + |
|
| 2659 | + |
|
| 2660 | + |
|
| 2661 | + /** |
|
| 2662 | + * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 2663 | + * (probably a bad assumption they have made, oh well) |
|
| 2664 | + * |
|
| 2665 | + * @return string |
|
| 2666 | + */ |
|
| 2667 | + public function __toString() |
|
| 2668 | + { |
|
| 2669 | + try { |
|
| 2670 | + return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 2671 | + } catch (Exception $e) { |
|
| 2672 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 2673 | + return ''; |
|
| 2674 | + } |
|
| 2675 | + } |
|
| 2676 | + |
|
| 2677 | + |
|
| 2678 | + |
|
| 2679 | + /** |
|
| 2680 | + * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 2681 | + * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 2682 | + * This means if we have made changes to those related model objects, and want to unserialize |
|
| 2683 | + * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 2684 | + * Instead, those related model objects should be directly serialized and stored. |
|
| 2685 | + * Eg, the following won't work: |
|
| 2686 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2687 | + * $att = $reg->attendee(); |
|
| 2688 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2689 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 2690 | + * //END REQUEST |
|
| 2691 | + * //START NEXT REQUEST |
|
| 2692 | + * $reg = get_option( 'my_option' ); |
|
| 2693 | + * $reg->attendee()->save(); |
|
| 2694 | + * And would need to be replace with: |
|
| 2695 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 2696 | + * $att = $reg->attendee(); |
|
| 2697 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 2698 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 2699 | + * //END REQUEST |
|
| 2700 | + * //START NEXT REQUEST |
|
| 2701 | + * $att = get_option( 'my_option' ); |
|
| 2702 | + * $att->save(); |
|
| 2703 | + * |
|
| 2704 | + * @return array |
|
| 2705 | + * @throws \EE_Error |
|
| 2706 | + */ |
|
| 2707 | + public function __sleep() |
|
| 2708 | + { |
|
| 2709 | + $model = $this->get_model(); |
|
| 2710 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 2711 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 2712 | + $classname = 'EE_' . $model->get_this_model_name(); |
|
| 2713 | + if ( |
|
| 2714 | + $this->get_one_from_cache($relation_name) instanceof $classname |
|
| 2715 | + && $this->get_one_from_cache($relation_name)->ID() |
|
| 2716 | + ) { |
|
| 2717 | + $this->clear_cache($relation_name, $this->get_one_from_cache($relation_name)->ID()); |
|
| 2718 | + } |
|
| 2719 | + } |
|
| 2720 | + } |
|
| 2721 | + $this->_props_n_values_provided_in_constructor = array(); |
|
| 2722 | + $properties_to_serialize = get_object_vars($this); |
|
| 2723 | + //don't serialize the model. It's big and that risks recursion |
|
| 2724 | + unset($properties_to_serialize['_model']); |
|
| 2725 | + return array_keys($properties_to_serialize); |
|
| 2726 | + } |
|
| 2727 | + |
|
| 2728 | + |
|
| 2729 | + |
|
| 2730 | + /** |
|
| 2731 | + * restore _props_n_values_provided_in_constructor |
|
| 2732 | + * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 2733 | + * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 2734 | + * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 2735 | + */ |
|
| 2736 | + public function __wakeup() |
|
| 2737 | + { |
|
| 2738 | + $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 2739 | + } |
|
| 2740 | 2740 | |
| 2741 | 2741 | |
| 2742 | 2742 | |
@@ -15,257 +15,257 @@ |
||
| 15 | 15 | abstract class EE_Form_Section_Layout_Base |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Form form section to lay out |
|
| 20 | - * |
|
| 21 | - * @var EE_Form_Section_Proper |
|
| 22 | - */ |
|
| 23 | - protected $_form_section; |
|
| 24 | - |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * __construct |
|
| 29 | - */ |
|
| 30 | - public function __construct() |
|
| 31 | - { |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The form section on which this strategy is to perform |
|
| 38 | - * |
|
| 39 | - * @param EE_Form_Section_Proper $form |
|
| 40 | - */ |
|
| 41 | - public function _construct_finalize(EE_Form_Section_Proper $form) |
|
| 42 | - { |
|
| 43 | - $this->_form_section = $form; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @return EE_Form_Section_Proper |
|
| 50 | - */ |
|
| 51 | - public function form_section() |
|
| 52 | - { |
|
| 53 | - return $this->_form_section; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Also has teh side effect of enqueuing any needed JS and CSS for |
|
| 60 | - * this form. |
|
| 61 | - * Creates all the HTML necessary for displaying this form, its inputs, and |
|
| 62 | - * proper subsections. |
|
| 63 | - * Returns the HTML |
|
| 64 | - * |
|
| 65 | - * @return string HTML for displaying |
|
| 66 | - * @throws EE_Error |
|
| 67 | - */ |
|
| 68 | - public function layout_form() |
|
| 69 | - { |
|
| 70 | - $html = ''; |
|
| 71 | - // layout_form_begin |
|
| 72 | - $html .= apply_filters( |
|
| 73 | - 'FHEE__EE_Form_Section_Layout_Base__layout_form__start__for_' . $this->_form_section->name(), |
|
| 74 | - $this->layout_form_begin(), |
|
| 75 | - $this->_form_section |
|
| 76 | - ); |
|
| 77 | - // layout_form_loop |
|
| 78 | - $html .= apply_filters( |
|
| 79 | - 'FHEE__EE_Form_Section_Layout_Base__layout_form__loop__for_' . $this->_form_section->name(), |
|
| 80 | - $this->layout_form_loop(), |
|
| 81 | - $this->_form_section |
|
| 82 | - ); |
|
| 83 | - // layout_form_end |
|
| 84 | - $html .= apply_filters( |
|
| 85 | - 'FHEE__EE_Form_Section_Layout_Base__layout_form__end__for_' . $this->_form_section->name(), |
|
| 86 | - $this->layout_form_end(), |
|
| 87 | - $this->_form_section |
|
| 88 | - ); |
|
| 89 | - $html = $this->add_form_section_hooks_and_filters($html); |
|
| 90 | - return $html; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @return string |
|
| 97 | - * @throws EE_Error |
|
| 98 | - */ |
|
| 99 | - public function layout_form_loop() |
|
| 100 | - { |
|
| 101 | - $html = ''; |
|
| 102 | - foreach ($this->_form_section->subsections() as $name => $subsection) { |
|
| 103 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
| 104 | - $html .= apply_filters( |
|
| 105 | - 'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_input_' |
|
| 106 | - . $name . '__in_' . $this->_form_section->name(), |
|
| 107 | - $this->layout_input($subsection), |
|
| 108 | - $this->_form_section, |
|
| 109 | - $subsection |
|
| 110 | - ); |
|
| 111 | - } elseif ($subsection instanceof EE_Form_Section_Base) { |
|
| 112 | - $html .= apply_filters( |
|
| 113 | - 'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_non_input_' |
|
| 114 | - . $name . '__in_' . $this->_form_section->name(), |
|
| 115 | - $this->layout_subsection($subsection), |
|
| 116 | - $this->_form_section, |
|
| 117 | - $subsection |
|
| 118 | - ); |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - return $html; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Should be used to start teh form section (Eg a table tag, or a div tag, etc.) |
|
| 128 | - * |
|
| 129 | - * @return string |
|
| 130 | - */ |
|
| 131 | - abstract public function layout_form_begin(); |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Should be used to end the form section (eg a /table tag, or a /div tag, etc) |
|
| 137 | - * |
|
| 138 | - * @return string |
|
| 139 | - */ |
|
| 140 | - abstract public function layout_form_end(); |
|
| 141 | - |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Should be used internally by layout_form() to layout each input (eg, if this layout |
|
| 146 | - * is putting each input in a row of its own, this should probably be called by a |
|
| 147 | - * foreach loop in layout_form() (WITHOUT adding any content directly within layout_form()'s foreach loop. |
|
| 148 | - * Eg, this method should add the tr and td tags). This method is exposed in case you want to completely |
|
| 149 | - * customize the form's layout, but would like to make use of it for laying out |
|
| 150 | - * 'easy-to-layout' inputs |
|
| 151 | - * |
|
| 152 | - * @param EE_Form_Input_Base $input |
|
| 153 | - * @return string html |
|
| 154 | - */ |
|
| 155 | - abstract public function layout_input($input); |
|
| 156 | - |
|
| 157 | - |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Similar to layout_input(), should be used internally by layout_form() within a |
|
| 161 | - * loop to layout each proper subsection. Unlike layout_input(), however, it is assumed |
|
| 162 | - * that the proper subsection will layout its container, label, etc on its own. |
|
| 163 | - * |
|
| 164 | - * @param EE_Form_Section_Base $subsection |
|
| 165 | - * @return string html |
|
| 166 | - */ |
|
| 167 | - abstract public function layout_subsection($subsection); |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Gets the HTML for the label tag and its contents for the input |
|
| 173 | - * |
|
| 174 | - * @param EE_Form_Input_Base $input |
|
| 175 | - * @return string |
|
| 176 | - */ |
|
| 177 | - public function display_label($input) |
|
| 178 | - { |
|
| 179 | - $class = $input->required() |
|
| 180 | - ? 'ee-required-label ' . $input->html_label_class() |
|
| 181 | - : $input->html_label_class(); |
|
| 182 | - $label_text = $input->required() |
|
| 183 | - ? $input->html_label_text() . '<span class="ee-asterisk">*</span>' |
|
| 184 | - : $input->html_label_text(); |
|
| 185 | - return '<label id="' |
|
| 186 | - . $input->html_label_id() |
|
| 187 | - . '" class="' |
|
| 188 | - . $class |
|
| 189 | - . '" style="' |
|
| 190 | - . $input->html_label_style() |
|
| 191 | - . '" for="' . $input->html_name() |
|
| 192 | - . '">' |
|
| 193 | - . $label_text |
|
| 194 | - . '</label>'; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * returns the HTML for the server-side validation errors for the specified input |
|
| 201 | - * Note that if JS is enabled, it should remove these and instead |
|
| 202 | - * populate the form's errors in the jquery validate fashion |
|
| 203 | - * using the localized data provided to the JS |
|
| 204 | - * |
|
| 205 | - * @param EE_Form_Input_Base $input |
|
| 206 | - * @return string |
|
| 207 | - */ |
|
| 208 | - public function display_errors($input) |
|
| 209 | - { |
|
| 210 | - if ($input->get_validation_errors()) { |
|
| 211 | - return "<label id='" |
|
| 212 | - . $input->html_id() |
|
| 213 | - . "-error' class='error' for='{$input->html_name()}'>" |
|
| 214 | - . $input->get_validation_error_string() |
|
| 215 | - . '</label>'; |
|
| 216 | - } |
|
| 217 | - return ''; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Displays the help span for the specified input |
|
| 224 | - * |
|
| 225 | - * @param EE_Form_Input_Base $input |
|
| 226 | - * @return string |
|
| 227 | - */ |
|
| 228 | - public function display_help_text($input) |
|
| 229 | - { |
|
| 230 | - if ($input->html_help_text() !== '') { |
|
| 231 | - $tag = is_admin() ? 'p' : 'span'; |
|
| 232 | - return '<' |
|
| 233 | - . $tag |
|
| 234 | - . ' id="' |
|
| 235 | - . $input->html_id() |
|
| 236 | - . '-help" class="' |
|
| 237 | - . $input->html_help_class() |
|
| 238 | - . '" style="' |
|
| 239 | - . $input->html_help_style() |
|
| 240 | - . '">' |
|
| 241 | - . $input->html_help_text() |
|
| 242 | - . '</' |
|
| 243 | - . $tag |
|
| 244 | - . '>'; |
|
| 245 | - } |
|
| 246 | - return ''; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Does an action and hook onto the end of teh form |
|
| 253 | - * |
|
| 254 | - * @param string $html |
|
| 255 | - * @return string |
|
| 256 | - */ |
|
| 257 | - public function add_form_section_hooks_and_filters($html) |
|
| 258 | - { |
|
| 259 | - // replace dashes and spaces with underscores |
|
| 260 | - $hook_name = str_replace(array('-', ' '), '_', $this->_form_section->html_id()); |
|
| 261 | - do_action('AHEE__Form_Section_Layout__' . $hook_name, $this->_form_section); |
|
| 262 | - $html = (string) apply_filters( |
|
| 263 | - 'AFEE__Form_Section_Layout__' . $hook_name . '__html', |
|
| 264 | - $html, |
|
| 265 | - $this->_form_section |
|
| 266 | - ); |
|
| 267 | - $html .= EEH_HTML::nl() . '<!-- AHEE__Form_Section_Layout__' . $hook_name . '__html -->'; |
|
| 268 | - $html .= EEH_HTML::nl() . '<!-- AFEE__Form_Section_Layout__' . $hook_name . ' -->'; |
|
| 269 | - return $html; |
|
| 270 | - } |
|
| 18 | + /** |
|
| 19 | + * Form form section to lay out |
|
| 20 | + * |
|
| 21 | + * @var EE_Form_Section_Proper |
|
| 22 | + */ |
|
| 23 | + protected $_form_section; |
|
| 24 | + |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * __construct |
|
| 29 | + */ |
|
| 30 | + public function __construct() |
|
| 31 | + { |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The form section on which this strategy is to perform |
|
| 38 | + * |
|
| 39 | + * @param EE_Form_Section_Proper $form |
|
| 40 | + */ |
|
| 41 | + public function _construct_finalize(EE_Form_Section_Proper $form) |
|
| 42 | + { |
|
| 43 | + $this->_form_section = $form; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @return EE_Form_Section_Proper |
|
| 50 | + */ |
|
| 51 | + public function form_section() |
|
| 52 | + { |
|
| 53 | + return $this->_form_section; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Also has teh side effect of enqueuing any needed JS and CSS for |
|
| 60 | + * this form. |
|
| 61 | + * Creates all the HTML necessary for displaying this form, its inputs, and |
|
| 62 | + * proper subsections. |
|
| 63 | + * Returns the HTML |
|
| 64 | + * |
|
| 65 | + * @return string HTML for displaying |
|
| 66 | + * @throws EE_Error |
|
| 67 | + */ |
|
| 68 | + public function layout_form() |
|
| 69 | + { |
|
| 70 | + $html = ''; |
|
| 71 | + // layout_form_begin |
|
| 72 | + $html .= apply_filters( |
|
| 73 | + 'FHEE__EE_Form_Section_Layout_Base__layout_form__start__for_' . $this->_form_section->name(), |
|
| 74 | + $this->layout_form_begin(), |
|
| 75 | + $this->_form_section |
|
| 76 | + ); |
|
| 77 | + // layout_form_loop |
|
| 78 | + $html .= apply_filters( |
|
| 79 | + 'FHEE__EE_Form_Section_Layout_Base__layout_form__loop__for_' . $this->_form_section->name(), |
|
| 80 | + $this->layout_form_loop(), |
|
| 81 | + $this->_form_section |
|
| 82 | + ); |
|
| 83 | + // layout_form_end |
|
| 84 | + $html .= apply_filters( |
|
| 85 | + 'FHEE__EE_Form_Section_Layout_Base__layout_form__end__for_' . $this->_form_section->name(), |
|
| 86 | + $this->layout_form_end(), |
|
| 87 | + $this->_form_section |
|
| 88 | + ); |
|
| 89 | + $html = $this->add_form_section_hooks_and_filters($html); |
|
| 90 | + return $html; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @return string |
|
| 97 | + * @throws EE_Error |
|
| 98 | + */ |
|
| 99 | + public function layout_form_loop() |
|
| 100 | + { |
|
| 101 | + $html = ''; |
|
| 102 | + foreach ($this->_form_section->subsections() as $name => $subsection) { |
|
| 103 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
| 104 | + $html .= apply_filters( |
|
| 105 | + 'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_input_' |
|
| 106 | + . $name . '__in_' . $this->_form_section->name(), |
|
| 107 | + $this->layout_input($subsection), |
|
| 108 | + $this->_form_section, |
|
| 109 | + $subsection |
|
| 110 | + ); |
|
| 111 | + } elseif ($subsection instanceof EE_Form_Section_Base) { |
|
| 112 | + $html .= apply_filters( |
|
| 113 | + 'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_non_input_' |
|
| 114 | + . $name . '__in_' . $this->_form_section->name(), |
|
| 115 | + $this->layout_subsection($subsection), |
|
| 116 | + $this->_form_section, |
|
| 117 | + $subsection |
|
| 118 | + ); |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + return $html; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Should be used to start teh form section (Eg a table tag, or a div tag, etc.) |
|
| 128 | + * |
|
| 129 | + * @return string |
|
| 130 | + */ |
|
| 131 | + abstract public function layout_form_begin(); |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Should be used to end the form section (eg a /table tag, or a /div tag, etc) |
|
| 137 | + * |
|
| 138 | + * @return string |
|
| 139 | + */ |
|
| 140 | + abstract public function layout_form_end(); |
|
| 141 | + |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Should be used internally by layout_form() to layout each input (eg, if this layout |
|
| 146 | + * is putting each input in a row of its own, this should probably be called by a |
|
| 147 | + * foreach loop in layout_form() (WITHOUT adding any content directly within layout_form()'s foreach loop. |
|
| 148 | + * Eg, this method should add the tr and td tags). This method is exposed in case you want to completely |
|
| 149 | + * customize the form's layout, but would like to make use of it for laying out |
|
| 150 | + * 'easy-to-layout' inputs |
|
| 151 | + * |
|
| 152 | + * @param EE_Form_Input_Base $input |
|
| 153 | + * @return string html |
|
| 154 | + */ |
|
| 155 | + abstract public function layout_input($input); |
|
| 156 | + |
|
| 157 | + |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Similar to layout_input(), should be used internally by layout_form() within a |
|
| 161 | + * loop to layout each proper subsection. Unlike layout_input(), however, it is assumed |
|
| 162 | + * that the proper subsection will layout its container, label, etc on its own. |
|
| 163 | + * |
|
| 164 | + * @param EE_Form_Section_Base $subsection |
|
| 165 | + * @return string html |
|
| 166 | + */ |
|
| 167 | + abstract public function layout_subsection($subsection); |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Gets the HTML for the label tag and its contents for the input |
|
| 173 | + * |
|
| 174 | + * @param EE_Form_Input_Base $input |
|
| 175 | + * @return string |
|
| 176 | + */ |
|
| 177 | + public function display_label($input) |
|
| 178 | + { |
|
| 179 | + $class = $input->required() |
|
| 180 | + ? 'ee-required-label ' . $input->html_label_class() |
|
| 181 | + : $input->html_label_class(); |
|
| 182 | + $label_text = $input->required() |
|
| 183 | + ? $input->html_label_text() . '<span class="ee-asterisk">*</span>' |
|
| 184 | + : $input->html_label_text(); |
|
| 185 | + return '<label id="' |
|
| 186 | + . $input->html_label_id() |
|
| 187 | + . '" class="' |
|
| 188 | + . $class |
|
| 189 | + . '" style="' |
|
| 190 | + . $input->html_label_style() |
|
| 191 | + . '" for="' . $input->html_name() |
|
| 192 | + . '">' |
|
| 193 | + . $label_text |
|
| 194 | + . '</label>'; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * returns the HTML for the server-side validation errors for the specified input |
|
| 201 | + * Note that if JS is enabled, it should remove these and instead |
|
| 202 | + * populate the form's errors in the jquery validate fashion |
|
| 203 | + * using the localized data provided to the JS |
|
| 204 | + * |
|
| 205 | + * @param EE_Form_Input_Base $input |
|
| 206 | + * @return string |
|
| 207 | + */ |
|
| 208 | + public function display_errors($input) |
|
| 209 | + { |
|
| 210 | + if ($input->get_validation_errors()) { |
|
| 211 | + return "<label id='" |
|
| 212 | + . $input->html_id() |
|
| 213 | + . "-error' class='error' for='{$input->html_name()}'>" |
|
| 214 | + . $input->get_validation_error_string() |
|
| 215 | + . '</label>'; |
|
| 216 | + } |
|
| 217 | + return ''; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Displays the help span for the specified input |
|
| 224 | + * |
|
| 225 | + * @param EE_Form_Input_Base $input |
|
| 226 | + * @return string |
|
| 227 | + */ |
|
| 228 | + public function display_help_text($input) |
|
| 229 | + { |
|
| 230 | + if ($input->html_help_text() !== '') { |
|
| 231 | + $tag = is_admin() ? 'p' : 'span'; |
|
| 232 | + return '<' |
|
| 233 | + . $tag |
|
| 234 | + . ' id="' |
|
| 235 | + . $input->html_id() |
|
| 236 | + . '-help" class="' |
|
| 237 | + . $input->html_help_class() |
|
| 238 | + . '" style="' |
|
| 239 | + . $input->html_help_style() |
|
| 240 | + . '">' |
|
| 241 | + . $input->html_help_text() |
|
| 242 | + . '</' |
|
| 243 | + . $tag |
|
| 244 | + . '>'; |
|
| 245 | + } |
|
| 246 | + return ''; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Does an action and hook onto the end of teh form |
|
| 253 | + * |
|
| 254 | + * @param string $html |
|
| 255 | + * @return string |
|
| 256 | + */ |
|
| 257 | + public function add_form_section_hooks_and_filters($html) |
|
| 258 | + { |
|
| 259 | + // replace dashes and spaces with underscores |
|
| 260 | + $hook_name = str_replace(array('-', ' '), '_', $this->_form_section->html_id()); |
|
| 261 | + do_action('AHEE__Form_Section_Layout__' . $hook_name, $this->_form_section); |
|
| 262 | + $html = (string) apply_filters( |
|
| 263 | + 'AFEE__Form_Section_Layout__' . $hook_name . '__html', |
|
| 264 | + $html, |
|
| 265 | + $this->_form_section |
|
| 266 | + ); |
|
| 267 | + $html .= EEH_HTML::nl() . '<!-- AHEE__Form_Section_Layout__' . $hook_name . '__html -->'; |
|
| 268 | + $html .= EEH_HTML::nl() . '<!-- AFEE__Form_Section_Layout__' . $hook_name . ' -->'; |
|
| 269 | + return $html; |
|
| 270 | + } |
|
| 271 | 271 | } |
@@ -70,19 +70,19 @@ discard block |
||
| 70 | 70 | $html = ''; |
| 71 | 71 | // layout_form_begin |
| 72 | 72 | $html .= apply_filters( |
| 73 | - 'FHEE__EE_Form_Section_Layout_Base__layout_form__start__for_' . $this->_form_section->name(), |
|
| 73 | + 'FHEE__EE_Form_Section_Layout_Base__layout_form__start__for_'.$this->_form_section->name(), |
|
| 74 | 74 | $this->layout_form_begin(), |
| 75 | 75 | $this->_form_section |
| 76 | 76 | ); |
| 77 | 77 | // layout_form_loop |
| 78 | 78 | $html .= apply_filters( |
| 79 | - 'FHEE__EE_Form_Section_Layout_Base__layout_form__loop__for_' . $this->_form_section->name(), |
|
| 79 | + 'FHEE__EE_Form_Section_Layout_Base__layout_form__loop__for_'.$this->_form_section->name(), |
|
| 80 | 80 | $this->layout_form_loop(), |
| 81 | 81 | $this->_form_section |
| 82 | 82 | ); |
| 83 | 83 | // layout_form_end |
| 84 | 84 | $html .= apply_filters( |
| 85 | - 'FHEE__EE_Form_Section_Layout_Base__layout_form__end__for_' . $this->_form_section->name(), |
|
| 85 | + 'FHEE__EE_Form_Section_Layout_Base__layout_form__end__for_'.$this->_form_section->name(), |
|
| 86 | 86 | $this->layout_form_end(), |
| 87 | 87 | $this->_form_section |
| 88 | 88 | ); |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | if ($subsection instanceof EE_Form_Input_Base) { |
| 104 | 104 | $html .= apply_filters( |
| 105 | 105 | 'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_input_' |
| 106 | - . $name . '__in_' . $this->_form_section->name(), |
|
| 106 | + . $name.'__in_'.$this->_form_section->name(), |
|
| 107 | 107 | $this->layout_input($subsection), |
| 108 | 108 | $this->_form_section, |
| 109 | 109 | $subsection |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | } elseif ($subsection instanceof EE_Form_Section_Base) { |
| 112 | 112 | $html .= apply_filters( |
| 113 | 113 | 'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_non_input_' |
| 114 | - . $name . '__in_' . $this->_form_section->name(), |
|
| 114 | + . $name.'__in_'.$this->_form_section->name(), |
|
| 115 | 115 | $this->layout_subsection($subsection), |
| 116 | 116 | $this->_form_section, |
| 117 | 117 | $subsection |
@@ -177,10 +177,10 @@ discard block |
||
| 177 | 177 | public function display_label($input) |
| 178 | 178 | { |
| 179 | 179 | $class = $input->required() |
| 180 | - ? 'ee-required-label ' . $input->html_label_class() |
|
| 180 | + ? 'ee-required-label '.$input->html_label_class() |
|
| 181 | 181 | : $input->html_label_class(); |
| 182 | 182 | $label_text = $input->required() |
| 183 | - ? $input->html_label_text() . '<span class="ee-asterisk">*</span>' |
|
| 183 | + ? $input->html_label_text().'<span class="ee-asterisk">*</span>' |
|
| 184 | 184 | : $input->html_label_text(); |
| 185 | 185 | return '<label id="' |
| 186 | 186 | . $input->html_label_id() |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | . $class |
| 189 | 189 | . '" style="' |
| 190 | 190 | . $input->html_label_style() |
| 191 | - . '" for="' . $input->html_name() |
|
| 191 | + . '" for="'.$input->html_name() |
|
| 192 | 192 | . '">' |
| 193 | 193 | . $label_text |
| 194 | 194 | . '</label>'; |
@@ -258,14 +258,14 @@ discard block |
||
| 258 | 258 | { |
| 259 | 259 | // replace dashes and spaces with underscores |
| 260 | 260 | $hook_name = str_replace(array('-', ' '), '_', $this->_form_section->html_id()); |
| 261 | - do_action('AHEE__Form_Section_Layout__' . $hook_name, $this->_form_section); |
|
| 261 | + do_action('AHEE__Form_Section_Layout__'.$hook_name, $this->_form_section); |
|
| 262 | 262 | $html = (string) apply_filters( |
| 263 | - 'AFEE__Form_Section_Layout__' . $hook_name . '__html', |
|
| 263 | + 'AFEE__Form_Section_Layout__'.$hook_name.'__html', |
|
| 264 | 264 | $html, |
| 265 | 265 | $this->_form_section |
| 266 | 266 | ); |
| 267 | - $html .= EEH_HTML::nl() . '<!-- AHEE__Form_Section_Layout__' . $hook_name . '__html -->'; |
|
| 268 | - $html .= EEH_HTML::nl() . '<!-- AFEE__Form_Section_Layout__' . $hook_name . ' -->'; |
|
| 267 | + $html .= EEH_HTML::nl().'<!-- AHEE__Form_Section_Layout__'.$hook_name.'__html -->'; |
|
| 268 | + $html .= EEH_HTML::nl().'<!-- AFEE__Form_Section_Layout__'.$hook_name.' -->'; |
|
| 269 | 269 | return $html; |
| 270 | 270 | } |
| 271 | 271 | } |
@@ -24,49 +24,49 @@ |
||
| 24 | 24 | class VsprintfFilter extends FormHtmlFilter |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var string $format |
|
| 29 | - */ |
|
| 30 | - protected $format = ''; |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var array $args |
|
| 35 | - */ |
|
| 36 | - protected $args = array(); |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * VsprintfFilter constructor. |
|
| 42 | - * |
|
| 43 | - * @param string $format |
|
| 44 | - * @param array $args |
|
| 45 | - */ |
|
| 46 | - public function __construct($format, array $args) |
|
| 47 | - { |
|
| 48 | - $this->format = $format; |
|
| 49 | - $this->args = $args; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param $html |
|
| 56 | - * @param EE_Form_Section_Validatable $form_section |
|
| 57 | - * @return string |
|
| 58 | - */ |
|
| 59 | - public function filterHtml($html, EE_Form_Section_Validatable $form_section) |
|
| 60 | - { |
|
| 61 | - $this->args[] = $html; |
|
| 62 | - if ($form_section instanceof EE_Form_Section_Proper) { |
|
| 63 | - $subsections = $form_section->subsections(); |
|
| 64 | - foreach ((array)$subsections as $subsection) { |
|
| 65 | - $this->args[] = $subsection->get_html(); |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - return vsprintf($this->format, $this->args); |
|
| 69 | - } |
|
| 27 | + /** |
|
| 28 | + * @var string $format |
|
| 29 | + */ |
|
| 30 | + protected $format = ''; |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var array $args |
|
| 35 | + */ |
|
| 36 | + protected $args = array(); |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * VsprintfFilter constructor. |
|
| 42 | + * |
|
| 43 | + * @param string $format |
|
| 44 | + * @param array $args |
|
| 45 | + */ |
|
| 46 | + public function __construct($format, array $args) |
|
| 47 | + { |
|
| 48 | + $this->format = $format; |
|
| 49 | + $this->args = $args; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param $html |
|
| 56 | + * @param EE_Form_Section_Validatable $form_section |
|
| 57 | + * @return string |
|
| 58 | + */ |
|
| 59 | + public function filterHtml($html, EE_Form_Section_Validatable $form_section) |
|
| 60 | + { |
|
| 61 | + $this->args[] = $html; |
|
| 62 | + if ($form_section instanceof EE_Form_Section_Proper) { |
|
| 63 | + $subsections = $form_section->subsections(); |
|
| 64 | + foreach ((array)$subsections as $subsection) { |
|
| 65 | + $this->args[] = $subsection->get_html(); |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + return vsprintf($this->format, $this->args); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | |
| 72 | 72 | |