@@ -12,2893 +12,2893 @@ |
||
| 12 | 12 | class EE_SPCO_Reg_Step_Payment_Options extends EE_SPCO_Reg_Step |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @access protected |
|
| 17 | - * @var EE_Line_Item_Display $Line_Item_Display |
|
| 18 | - */ |
|
| 19 | - protected $line_item_display; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @access protected |
|
| 23 | - * @var boolean $handle_IPN_in_this_request |
|
| 24 | - */ |
|
| 25 | - protected $handle_IPN_in_this_request = false; |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * set_hooks - for hooking into EE Core, other modules, etc |
|
| 30 | - * |
|
| 31 | - * @access public |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - public static function set_hooks() |
|
| 35 | - { |
|
| 36 | - add_filter( |
|
| 37 | - 'FHEE__SPCO__EE_Line_Item_Filter_Collection', |
|
| 38 | - array('EE_SPCO_Reg_Step_Payment_Options', 'add_spco_line_item_filters') |
|
| 39 | - ); |
|
| 40 | - add_action( |
|
| 41 | - 'wp_ajax_switch_spco_billing_form', |
|
| 42 | - array('EE_SPCO_Reg_Step_Payment_Options', 'switch_spco_billing_form') |
|
| 43 | - ); |
|
| 44 | - add_action( |
|
| 45 | - 'wp_ajax_nopriv_switch_spco_billing_form', |
|
| 46 | - array('EE_SPCO_Reg_Step_Payment_Options', 'switch_spco_billing_form') |
|
| 47 | - ); |
|
| 48 | - add_action('wp_ajax_save_payer_details', array('EE_SPCO_Reg_Step_Payment_Options', 'save_payer_details')); |
|
| 49 | - add_action( |
|
| 50 | - 'wp_ajax_nopriv_save_payer_details', |
|
| 51 | - array('EE_SPCO_Reg_Step_Payment_Options', 'save_payer_details') |
|
| 52 | - ); |
|
| 53 | - add_action( |
|
| 54 | - 'wp_ajax_get_transaction_details_for_gateways', |
|
| 55 | - array('EE_SPCO_Reg_Step_Payment_Options', 'get_transaction_details') |
|
| 56 | - ); |
|
| 57 | - add_action( |
|
| 58 | - 'wp_ajax_nopriv_get_transaction_details_for_gateways', |
|
| 59 | - array('EE_SPCO_Reg_Step_Payment_Options', 'get_transaction_details') |
|
| 60 | - ); |
|
| 61 | - add_filter( |
|
| 62 | - 'FHEE__EED_Recaptcha___bypass_recaptcha__bypass_request_params_array', |
|
| 63 | - array('EE_SPCO_Reg_Step_Payment_Options', 'bypass_recaptcha_for_load_payment_method'), |
|
| 64 | - 10, |
|
| 65 | - 1 |
|
| 66 | - ); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * ajax switch_spco_billing_form |
|
| 72 | - * |
|
| 73 | - * @throws \EE_Error |
|
| 74 | - */ |
|
| 75 | - public static function switch_spco_billing_form() |
|
| 76 | - { |
|
| 77 | - EED_Single_Page_Checkout::process_ajax_request('switch_payment_method'); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * ajax save_payer_details |
|
| 83 | - * |
|
| 84 | - * @throws \EE_Error |
|
| 85 | - */ |
|
| 86 | - public static function save_payer_details() |
|
| 87 | - { |
|
| 88 | - EED_Single_Page_Checkout::process_ajax_request('save_payer_details_via_ajax'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * ajax get_transaction_details |
|
| 94 | - * |
|
| 95 | - * @throws \EE_Error |
|
| 96 | - */ |
|
| 97 | - public static function get_transaction_details() |
|
| 98 | - { |
|
| 99 | - EED_Single_Page_Checkout::process_ajax_request('get_transaction_details_for_gateways'); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * bypass_recaptcha_for_load_payment_method |
|
| 105 | - * |
|
| 106 | - * @access public |
|
| 107 | - * @return array |
|
| 108 | - * @throws InvalidArgumentException |
|
| 109 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 110 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 111 | - */ |
|
| 112 | - public static function bypass_recaptcha_for_load_payment_method() |
|
| 113 | - { |
|
| 114 | - return array( |
|
| 115 | - 'EESID' => EE_Registry::instance()->SSN->id(), |
|
| 116 | - 'step' => 'payment_options', |
|
| 117 | - 'action' => 'spco_billing_form', |
|
| 118 | - ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * class constructor |
|
| 124 | - * |
|
| 125 | - * @access public |
|
| 126 | - * @param EE_Checkout $checkout |
|
| 127 | - */ |
|
| 128 | - public function __construct(EE_Checkout $checkout) |
|
| 129 | - { |
|
| 130 | - $this->_slug = 'payment_options'; |
|
| 131 | - $this->_name = esc_html__('Payment Options', 'event_espresso'); |
|
| 132 | - $this->_template = SPCO_REG_STEPS_PATH . $this->_slug . DS . 'payment_options_main.template.php'; |
|
| 133 | - $this->checkout = $checkout; |
|
| 134 | - $this->_reset_success_message(); |
|
| 135 | - $this->set_instructions( |
|
| 136 | - esc_html__( |
|
| 137 | - 'Please select a method of payment and provide any necessary billing information before proceeding.', |
|
| 138 | - 'event_espresso' |
|
| 139 | - ) |
|
| 140 | - ); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @return null |
|
| 146 | - */ |
|
| 147 | - public function line_item_display() |
|
| 148 | - { |
|
| 149 | - return $this->line_item_display; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @param null $line_item_display |
|
| 155 | - */ |
|
| 156 | - public function set_line_item_display($line_item_display) |
|
| 157 | - { |
|
| 158 | - $this->line_item_display = $line_item_display; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @return boolean |
|
| 164 | - */ |
|
| 165 | - public function handle_IPN_in_this_request() |
|
| 166 | - { |
|
| 167 | - return $this->handle_IPN_in_this_request; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * @param boolean $handle_IPN_in_this_request |
|
| 173 | - */ |
|
| 174 | - public function set_handle_IPN_in_this_request($handle_IPN_in_this_request) |
|
| 175 | - { |
|
| 176 | - $this->handle_IPN_in_this_request = filter_var($handle_IPN_in_this_request, FILTER_VALIDATE_BOOLEAN); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * translate_js_strings |
|
| 182 | - * |
|
| 183 | - * @return void |
|
| 184 | - */ |
|
| 185 | - public function translate_js_strings() |
|
| 186 | - { |
|
| 187 | - EE_Registry::$i18n_js_strings['no_payment_method'] = esc_html__( |
|
| 188 | - 'Please select a method of payment in order to continue.', |
|
| 189 | - 'event_espresso' |
|
| 190 | - ); |
|
| 191 | - EE_Registry::$i18n_js_strings['invalid_payment_method'] = esc_html__( |
|
| 192 | - 'A valid method of payment could not be determined. Please refresh the page and try again.', |
|
| 193 | - 'event_espresso' |
|
| 194 | - ); |
|
| 195 | - EE_Registry::$i18n_js_strings['forwarding_to_offsite'] = esc_html__( |
|
| 196 | - 'Forwarding to Secure Payment Provider.', |
|
| 197 | - 'event_espresso' |
|
| 198 | - ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * enqueue_styles_and_scripts |
|
| 204 | - * |
|
| 205 | - * @return void |
|
| 206 | - * @throws EE_Error |
|
| 207 | - * @throws InvalidArgumentException |
|
| 208 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 209 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 210 | - */ |
|
| 211 | - public function enqueue_styles_and_scripts() |
|
| 212 | - { |
|
| 213 | - $transaction = $this->checkout->transaction; |
|
| 214 | - // if the transaction isn't set or nothing is owed on it, don't enqueue any JS |
|
| 215 | - if (! $transaction instanceof EE_Transaction || EEH_Money::compare_floats($transaction->remaining(), 0)) { |
|
| 216 | - return; |
|
| 217 | - } |
|
| 218 | - foreach (EEM_Payment_Method::instance()->get_all_for_transaction( |
|
| 219 | - $transaction, |
|
| 220 | - EEM_Payment_Method::scope_cart |
|
| 221 | - ) as $payment_method) { |
|
| 222 | - $type_obj = $payment_method->type_obj(); |
|
| 223 | - if ($type_obj instanceof EE_PMT_Base) { |
|
| 224 | - $billing_form = $type_obj->generate_new_billing_form($transaction); |
|
| 225 | - if ($billing_form instanceof EE_Form_Section_Proper) { |
|
| 226 | - $billing_form->enqueue_js(); |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * initialize_reg_step |
|
| 235 | - * |
|
| 236 | - * @return bool |
|
| 237 | - * @throws EE_Error |
|
| 238 | - * @throws InvalidArgumentException |
|
| 239 | - * @throws ReflectionException |
|
| 240 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 241 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 242 | - */ |
|
| 243 | - public function initialize_reg_step() |
|
| 244 | - { |
|
| 245 | - // TODO: if /when we implement donations, then this will need overriding |
|
| 246 | - if (// don't need payment options for: |
|
| 247 | - // registrations made via the admin |
|
| 248 | - // completed transactions |
|
| 249 | - // overpaid transactions |
|
| 250 | - // $ 0.00 transactions(no payment required) |
|
| 251 | - ! $this->checkout->payment_required() |
|
| 252 | - // but do NOT remove if current action being called belongs to this reg step |
|
| 253 | - && ! is_callable(array($this, $this->checkout->action)) |
|
| 254 | - && ! $this->completed() |
|
| 255 | - ) { |
|
| 256 | - // and if so, then we no longer need the Payment Options step |
|
| 257 | - if ($this->is_current_step()) { |
|
| 258 | - $this->checkout->generate_reg_form = false; |
|
| 259 | - } |
|
| 260 | - $this->checkout->remove_reg_step($this->_slug); |
|
| 261 | - // DEBUG LOG |
|
| 262 | - // $this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ ); |
|
| 263 | - return false; |
|
| 264 | - } |
|
| 265 | - // load EEM_Payment_Method |
|
| 266 | - EE_Registry::instance()->load_model('Payment_Method'); |
|
| 267 | - // get all active payment methods |
|
| 268 | - $this->checkout->available_payment_methods = EEM_Payment_Method::instance()->get_all_for_transaction( |
|
| 269 | - $this->checkout->transaction, |
|
| 270 | - EEM_Payment_Method::scope_cart |
|
| 271 | - ); |
|
| 272 | - return true; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * @return EE_Form_Section_Proper |
|
| 278 | - * @throws EE_Error |
|
| 279 | - * @throws InvalidArgumentException |
|
| 280 | - * @throws ReflectionException |
|
| 281 | - * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 282 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 283 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 284 | - * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 285 | - */ |
|
| 286 | - public function generate_reg_form() |
|
| 287 | - { |
|
| 288 | - // reset in case someone changes their mind |
|
| 289 | - $this->_reset_selected_method_of_payment(); |
|
| 290 | - // set some defaults |
|
| 291 | - $this->checkout->selected_method_of_payment = 'payments_closed'; |
|
| 292 | - $registrations_requiring_payment = array(); |
|
| 293 | - $registrations_for_free_events = array(); |
|
| 294 | - $registrations_requiring_pre_approval = array(); |
|
| 295 | - $sold_out_events = array(); |
|
| 296 | - $insufficient_spaces_available = array(); |
|
| 297 | - $no_payment_required = true; |
|
| 298 | - // loop thru registrations to gather info |
|
| 299 | - $registrations = $this->checkout->transaction->registrations($this->checkout->reg_cache_where_params); |
|
| 300 | - $ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space( |
|
| 301 | - $registrations, |
|
| 302 | - $this->checkout->revisit |
|
| 303 | - ); |
|
| 304 | - foreach ($registrations as $REG_ID => $registration) { |
|
| 305 | - /** @var $registration EE_Registration */ |
|
| 306 | - // has this registration lost it's space ? |
|
| 307 | - if (isset($ejected_registrations[ $REG_ID ])) { |
|
| 308 | - if ($registration->event()->is_sold_out() || $registration->event()->is_sold_out(true)) { |
|
| 309 | - $sold_out_events[ $registration->event()->ID() ] = $registration->event(); |
|
| 310 | - } else { |
|
| 311 | - $insufficient_spaces_available[ $registration->event()->ID() ] = $registration->event(); |
|
| 312 | - } |
|
| 313 | - continue; |
|
| 314 | - } |
|
| 315 | - // event requires admin approval |
|
| 316 | - if ($registration->status_ID() === EEM_Registration::status_id_not_approved) { |
|
| 317 | - // add event to list of events with pre-approval reg status |
|
| 318 | - $registrations_requiring_pre_approval[ $REG_ID ] = $registration; |
|
| 319 | - do_action( |
|
| 320 | - 'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__event_requires_pre_approval', |
|
| 321 | - $registration->event(), |
|
| 322 | - $this |
|
| 323 | - ); |
|
| 324 | - continue; |
|
| 325 | - } |
|
| 326 | - if ($this->checkout->revisit |
|
| 327 | - && $registration->status_ID() !== EEM_Registration::status_id_approved |
|
| 328 | - && ( |
|
| 329 | - $registration->event()->is_sold_out() |
|
| 330 | - || $registration->event()->is_sold_out(true) |
|
| 331 | - ) |
|
| 332 | - ) { |
|
| 333 | - // add event to list of events that are sold out |
|
| 334 | - $sold_out_events[ $registration->event()->ID() ] = $registration->event(); |
|
| 335 | - do_action( |
|
| 336 | - 'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__sold_out_event', |
|
| 337 | - $registration->event(), |
|
| 338 | - $this |
|
| 339 | - ); |
|
| 340 | - continue; |
|
| 341 | - } |
|
| 342 | - // are they allowed to pay now and is there monies owing? |
|
| 343 | - if ($registration->owes_monies_and_can_pay()) { |
|
| 344 | - $registrations_requiring_payment[ $REG_ID ] = $registration; |
|
| 345 | - do_action( |
|
| 346 | - 'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__event_requires_payment', |
|
| 347 | - $registration->event(), |
|
| 348 | - $this |
|
| 349 | - ); |
|
| 350 | - } elseif (! $this->checkout->revisit |
|
| 351 | - && $registration->status_ID() !== EEM_Registration::status_id_not_approved |
|
| 352 | - && $registration->ticket()->is_free() |
|
| 353 | - ) { |
|
| 354 | - $registrations_for_free_events[ $registration->ticket()->ID() ] = $registration; |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - $subsections = array(); |
|
| 358 | - // now decide which template to load |
|
| 359 | - if (! empty($sold_out_events)) { |
|
| 360 | - $subsections['sold_out_events'] = $this->_sold_out_events($sold_out_events); |
|
| 361 | - } |
|
| 362 | - if (! empty($insufficient_spaces_available)) { |
|
| 363 | - $subsections['insufficient_space'] = $this->_insufficient_spaces_available( |
|
| 364 | - $insufficient_spaces_available |
|
| 365 | - ); |
|
| 366 | - } |
|
| 367 | - if (! empty($registrations_requiring_pre_approval)) { |
|
| 368 | - $subsections['registrations_requiring_pre_approval'] = $this->_registrations_requiring_pre_approval( |
|
| 369 | - $registrations_requiring_pre_approval |
|
| 370 | - ); |
|
| 371 | - } |
|
| 372 | - if (! empty($registrations_for_free_events)) { |
|
| 373 | - $subsections['no_payment_required'] = $this->_no_payment_required($registrations_for_free_events); |
|
| 374 | - } |
|
| 375 | - if (! empty($registrations_requiring_payment)) { |
|
| 376 | - if ($this->checkout->amount_owing > 0) { |
|
| 377 | - // autoload Line_Item_Display classes |
|
| 378 | - EEH_Autoloader::register_line_item_filter_autoloaders(); |
|
| 379 | - $line_item_filter_processor = new EE_Line_Item_Filter_Processor( |
|
| 380 | - apply_filters( |
|
| 381 | - 'FHEE__SPCO__EE_Line_Item_Filter_Collection', |
|
| 382 | - new EE_Line_Item_Filter_Collection() |
|
| 383 | - ), |
|
| 384 | - $this->checkout->cart->get_grand_total() |
|
| 385 | - ); |
|
| 386 | - /** @var EE_Line_Item $filtered_line_item_tree */ |
|
| 387 | - $filtered_line_item_tree = $line_item_filter_processor->process(); |
|
| 388 | - EEH_Autoloader::register_line_item_display_autoloaders(); |
|
| 389 | - $this->set_line_item_display(new EE_Line_Item_Display('spco')); |
|
| 390 | - $subsections['payment_options'] = $this->_display_payment_options( |
|
| 391 | - $this->line_item_display->display_line_item( |
|
| 392 | - $filtered_line_item_tree, |
|
| 393 | - array('registrations' => $registrations) |
|
| 394 | - ) |
|
| 395 | - ); |
|
| 396 | - $this->checkout->amount_owing = $filtered_line_item_tree->total(); |
|
| 397 | - $this->_apply_registration_payments_to_amount_owing($registrations); |
|
| 398 | - } |
|
| 399 | - $no_payment_required = false; |
|
| 400 | - } else { |
|
| 401 | - $this->_hide_reg_step_submit_button_if_revisit(); |
|
| 402 | - } |
|
| 403 | - $this->_save_selected_method_of_payment(); |
|
| 404 | - |
|
| 405 | - $subsections['default_hidden_inputs'] = $this->reg_step_hidden_inputs(); |
|
| 406 | - $subsections['extra_hidden_inputs'] = $this->_extra_hidden_inputs($no_payment_required); |
|
| 407 | - |
|
| 408 | - return new EE_Form_Section_Proper( |
|
| 409 | - array( |
|
| 410 | - 'name' => $this->reg_form_name(), |
|
| 411 | - 'html_id' => $this->reg_form_name(), |
|
| 412 | - 'subsections' => $subsections, |
|
| 413 | - 'layout_strategy' => new EE_No_Layout(), |
|
| 414 | - ) |
|
| 415 | - ); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * add line item filters required for this reg step |
|
| 421 | - * these filters are applied via this line in EE_SPCO_Reg_Step_Payment_Options::set_hooks(): |
|
| 422 | - * add_filter( 'FHEE__SPCO__EE_Line_Item_Filter_Collection', array( 'EE_SPCO_Reg_Step_Payment_Options', |
|
| 423 | - * 'add_spco_line_item_filters' ) ); so any code that wants to use the same set of filters during the |
|
| 424 | - * payment options reg step, can apply these filters via the following: apply_filters( |
|
| 425 | - * 'FHEE__SPCO__EE_Line_Item_Filter_Collection', new EE_Line_Item_Filter_Collection() ) or to an existing |
|
| 426 | - * filter collection by passing that instead of instantiating a new collection |
|
| 427 | - * |
|
| 428 | - * @param \EE_Line_Item_Filter_Collection $line_item_filter_collection |
|
| 429 | - * @return EE_Line_Item_Filter_Collection |
|
| 430 | - * @throws EE_Error |
|
| 431 | - * @throws InvalidArgumentException |
|
| 432 | - * @throws ReflectionException |
|
| 433 | - * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 434 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 435 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 436 | - * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 437 | - */ |
|
| 438 | - public static function add_spco_line_item_filters(EE_Line_Item_Filter_Collection $line_item_filter_collection) |
|
| 439 | - { |
|
| 440 | - if (! EE_Registry::instance()->SSN instanceof EE_Session) { |
|
| 441 | - return $line_item_filter_collection; |
|
| 442 | - } |
|
| 443 | - if (! EE_Registry::instance()->SSN->checkout() instanceof EE_Checkout) { |
|
| 444 | - return $line_item_filter_collection; |
|
| 445 | - } |
|
| 446 | - if (! EE_Registry::instance()->SSN->checkout()->transaction instanceof EE_Transaction) { |
|
| 447 | - return $line_item_filter_collection; |
|
| 448 | - } |
|
| 449 | - $line_item_filter_collection->add( |
|
| 450 | - new EE_Billable_Line_Item_Filter( |
|
| 451 | - EE_SPCO_Reg_Step_Payment_Options::remove_ejected_registrations( |
|
| 452 | - EE_Registry::instance()->SSN->checkout()->transaction->registrations( |
|
| 453 | - EE_Registry::instance()->SSN->checkout()->reg_cache_where_params |
|
| 454 | - ) |
|
| 455 | - ) |
|
| 456 | - ) |
|
| 457 | - ); |
|
| 458 | - $line_item_filter_collection->add(new EE_Non_Zero_Line_Item_Filter()); |
|
| 459 | - return $line_item_filter_collection; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * remove_ejected_registrations |
|
| 465 | - * if a registrant has lost their potential space at an event due to lack of payment, |
|
| 466 | - * then this method removes them from the list of registrations being paid for during this request |
|
| 467 | - * |
|
| 468 | - * @param \EE_Registration[] $registrations |
|
| 469 | - * @return EE_Registration[] |
|
| 470 | - * @throws EE_Error |
|
| 471 | - * @throws InvalidArgumentException |
|
| 472 | - * @throws ReflectionException |
|
| 473 | - * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 474 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 475 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 476 | - * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 477 | - */ |
|
| 478 | - public static function remove_ejected_registrations(array $registrations) |
|
| 479 | - { |
|
| 480 | - $ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space( |
|
| 481 | - $registrations, |
|
| 482 | - EE_Registry::instance()->SSN->checkout()->revisit |
|
| 483 | - ); |
|
| 484 | - foreach ($registrations as $REG_ID => $registration) { |
|
| 485 | - // has this registration lost it's space ? |
|
| 486 | - if (isset($ejected_registrations[ $REG_ID ])) { |
|
| 487 | - unset($registrations[ $REG_ID ]); |
|
| 488 | - continue; |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - return $registrations; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * find_registrations_that_lost_their_space |
|
| 497 | - * If a registrant chooses an offline payment method like Invoice, |
|
| 498 | - * then no space is reserved for them at the event until they fully pay fo that site |
|
| 499 | - * (unless the event's default reg status is set to APPROVED) |
|
| 500 | - * if a registrant then later returns to pay, but the number of spaces available has been reduced due to sales, |
|
| 501 | - * then this method will determine which registrations have lost the ability to complete the reg process. |
|
| 502 | - * |
|
| 503 | - * @param \EE_Registration[] $registrations |
|
| 504 | - * @param bool $revisit |
|
| 505 | - * @return array |
|
| 506 | - * @throws EE_Error |
|
| 507 | - * @throws InvalidArgumentException |
|
| 508 | - * @throws ReflectionException |
|
| 509 | - * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 510 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 511 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 512 | - * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 513 | - */ |
|
| 514 | - public static function find_registrations_that_lost_their_space(array $registrations, $revisit = false) |
|
| 515 | - { |
|
| 516 | - // registrations per event |
|
| 517 | - $event_reg_count = array(); |
|
| 518 | - // spaces left per event |
|
| 519 | - $event_spaces_remaining = array(); |
|
| 520 | - // tickets left sorted by ID |
|
| 521 | - $tickets_remaining = array(); |
|
| 522 | - // registrations that have lost their space |
|
| 523 | - $ejected_registrations = array(); |
|
| 524 | - foreach ($registrations as $REG_ID => $registration) { |
|
| 525 | - if ($registration->status_ID() === EEM_Registration::status_id_approved |
|
| 526 | - || apply_filters( |
|
| 527 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options__find_registrations_that_lost_their_space__allow_reg_payment', |
|
| 528 | - false, |
|
| 529 | - $registration, |
|
| 530 | - $revisit |
|
| 531 | - ) |
|
| 532 | - ) { |
|
| 533 | - continue; |
|
| 534 | - } |
|
| 535 | - $EVT_ID = $registration->event_ID(); |
|
| 536 | - $ticket = $registration->ticket(); |
|
| 537 | - if (! isset($tickets_remaining[ $ticket->ID() ])) { |
|
| 538 | - $tickets_remaining[ $ticket->ID() ] = $ticket->remaining(); |
|
| 539 | - } |
|
| 540 | - if ($tickets_remaining[ $ticket->ID() ] > 0) { |
|
| 541 | - if (! isset($event_reg_count[ $EVT_ID ])) { |
|
| 542 | - $event_reg_count[ $EVT_ID ] = 0; |
|
| 543 | - } |
|
| 544 | - $event_reg_count[ $EVT_ID ]++; |
|
| 545 | - if (! isset($event_spaces_remaining[ $EVT_ID ])) { |
|
| 546 | - $event_spaces_remaining[ $EVT_ID ] = $registration->event()->spaces_remaining_for_sale(); |
|
| 547 | - } |
|
| 548 | - } |
|
| 549 | - if ($revisit |
|
| 550 | - && ($tickets_remaining[ $ticket->ID() ] === 0 |
|
| 551 | - || $event_reg_count[ $EVT_ID ] > $event_spaces_remaining[ $EVT_ID ] |
|
| 552 | - ) |
|
| 553 | - ) { |
|
| 554 | - $ejected_registrations[ $REG_ID ] = $registration->event(); |
|
| 555 | - if ($registration->status_ID() !== EEM_Registration::status_id_wait_list) { |
|
| 556 | - /** @type EE_Registration_Processor $registration_processor */ |
|
| 557 | - $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
| 558 | - // at this point, we should have enough details about the registrant to consider the registration |
|
| 559 | - // NOT incomplete |
|
| 560 | - $registration_processor->manually_update_registration_status( |
|
| 561 | - $registration, |
|
| 562 | - EEM_Registration::status_id_wait_list |
|
| 563 | - ); |
|
| 564 | - } |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - return $ejected_registrations; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * _hide_reg_step_submit_button |
|
| 573 | - * removes the html for the reg step submit button |
|
| 574 | - * by replacing it with an empty string via filter callback |
|
| 575 | - * |
|
| 576 | - * @return void |
|
| 577 | - */ |
|
| 578 | - protected function _adjust_registration_status_if_event_old_sold() |
|
| 579 | - { |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * _hide_reg_step_submit_button |
|
| 585 | - * removes the html for the reg step submit button |
|
| 586 | - * by replacing it with an empty string via filter callback |
|
| 587 | - * |
|
| 588 | - * @return void |
|
| 589 | - */ |
|
| 590 | - protected function _hide_reg_step_submit_button_if_revisit() |
|
| 591 | - { |
|
| 592 | - if ($this->checkout->revisit) { |
|
| 593 | - add_filter('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', '__return_empty_string'); |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * sold_out_events |
|
| 600 | - * displays notices regarding events that have sold out since hte registrant first signed up |
|
| 601 | - * |
|
| 602 | - * @param \EE_Event[] $sold_out_events_array |
|
| 603 | - * @return \EE_Form_Section_Proper |
|
| 604 | - * @throws \EE_Error |
|
| 605 | - */ |
|
| 606 | - private function _sold_out_events($sold_out_events_array = array()) |
|
| 607 | - { |
|
| 608 | - // set some defaults |
|
| 609 | - $this->checkout->selected_method_of_payment = 'events_sold_out'; |
|
| 610 | - $sold_out_events = ''; |
|
| 611 | - foreach ($sold_out_events_array as $sold_out_event) { |
|
| 612 | - $sold_out_events .= EEH_HTML::li( |
|
| 613 | - EEH_HTML::span( |
|
| 614 | - ' ' . $sold_out_event->name(), |
|
| 615 | - '', |
|
| 616 | - 'dashicons dashicons-marker ee-icon-size-16 pink-text' |
|
| 617 | - ) |
|
| 618 | - ); |
|
| 619 | - } |
|
| 620 | - return new EE_Form_Section_Proper( |
|
| 621 | - array( |
|
| 622 | - 'layout_strategy' => new EE_Template_Layout( |
|
| 623 | - array( |
|
| 624 | - 'layout_template_file' => SPCO_REG_STEPS_PATH |
|
| 625 | - . $this->_slug |
|
| 626 | - . DS |
|
| 627 | - . 'sold_out_events.template.php', |
|
| 628 | - 'template_args' => apply_filters( |
|
| 629 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__template_args', |
|
| 630 | - array( |
|
| 631 | - 'sold_out_events' => $sold_out_events, |
|
| 632 | - 'sold_out_events_msg' => apply_filters( |
|
| 633 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__sold_out_events_msg', |
|
| 634 | - sprintf( |
|
| 635 | - esc_html__( |
|
| 636 | - 'It appears that the event you were about to make a payment for has sold out since you first registered. If you have already made a partial payment towards this event, please contact the event administrator for a refund.%3$s%3$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%2$s', |
|
| 637 | - 'event_espresso' |
|
| 638 | - ), |
|
| 639 | - '<strong>', |
|
| 640 | - '</strong>', |
|
| 641 | - '<br />' |
|
| 642 | - ) |
|
| 643 | - ), |
|
| 644 | - ) |
|
| 645 | - ), |
|
| 646 | - ) |
|
| 647 | - ), |
|
| 648 | - ) |
|
| 649 | - ); |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * _insufficient_spaces_available |
|
| 655 | - * displays notices regarding events that do not have enough remaining spaces |
|
| 656 | - * to satisfy the current number of registrations looking to pay |
|
| 657 | - * |
|
| 658 | - * @param \EE_Event[] $insufficient_spaces_events_array |
|
| 659 | - * @return \EE_Form_Section_Proper |
|
| 660 | - * @throws \EE_Error |
|
| 661 | - */ |
|
| 662 | - private function _insufficient_spaces_available($insufficient_spaces_events_array = array()) |
|
| 663 | - { |
|
| 664 | - // set some defaults |
|
| 665 | - $this->checkout->selected_method_of_payment = 'invoice'; |
|
| 666 | - $insufficient_space_events = ''; |
|
| 667 | - foreach ($insufficient_spaces_events_array as $event) { |
|
| 668 | - if ($event instanceof EE_Event) { |
|
| 669 | - $insufficient_space_events .= EEH_HTML::li( |
|
| 670 | - EEH_HTML::span(' ' . $event->name(), '', 'dashicons dashicons-marker ee-icon-size-16 pink-text') |
|
| 671 | - ); |
|
| 672 | - } |
|
| 673 | - } |
|
| 674 | - return new EE_Form_Section_Proper( |
|
| 675 | - array( |
|
| 676 | - 'subsections' => array( |
|
| 677 | - 'default_hidden_inputs' => $this->reg_step_hidden_inputs(), |
|
| 678 | - 'extra_hidden_inputs' => $this->_extra_hidden_inputs(), |
|
| 679 | - ), |
|
| 680 | - 'layout_strategy' => new EE_Template_Layout( |
|
| 681 | - array( |
|
| 682 | - 'layout_template_file' => SPCO_REG_STEPS_PATH |
|
| 683 | - . $this->_slug |
|
| 684 | - . DS |
|
| 685 | - . 'sold_out_events.template.php', |
|
| 686 | - 'template_args' => apply_filters( |
|
| 687 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___insufficient_spaces_available__template_args', |
|
| 688 | - array( |
|
| 689 | - 'sold_out_events' => $insufficient_space_events, |
|
| 690 | - 'sold_out_events_msg' => apply_filters( |
|
| 691 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___insufficient_spaces_available__insufficient_space_msg', |
|
| 692 | - esc_html__( |
|
| 693 | - 'It appears that the event you were about to make a payment for has sold additional tickets since you first registered, and there are no longer enough spaces left to accommodate your selections. You may continue to pay and secure the available space(s) remaining, or simply cancel if you no longer wish to purchase. If you have already made a partial payment towards this event, please contact the event administrator for a refund.', |
|
| 694 | - 'event_espresso' |
|
| 695 | - ) |
|
| 696 | - ), |
|
| 697 | - ) |
|
| 698 | - ), |
|
| 699 | - ) |
|
| 700 | - ), |
|
| 701 | - ) |
|
| 702 | - ); |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * registrations_requiring_pre_approval |
|
| 708 | - * |
|
| 709 | - * @param array $registrations_requiring_pre_approval |
|
| 710 | - * @return EE_Form_Section_Proper |
|
| 711 | - * @throws EE_Error |
|
| 712 | - * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 713 | - */ |
|
| 714 | - private function _registrations_requiring_pre_approval($registrations_requiring_pre_approval = array()) |
|
| 715 | - { |
|
| 716 | - $events_requiring_pre_approval = ''; |
|
| 717 | - foreach ($registrations_requiring_pre_approval as $registration) { |
|
| 718 | - if ($registration instanceof EE_Registration && $registration->event() instanceof EE_Event) { |
|
| 719 | - $events_requiring_pre_approval[ $registration->event()->ID() ] = EEH_HTML::li( |
|
| 720 | - EEH_HTML::span( |
|
| 721 | - '', |
|
| 722 | - '', |
|
| 723 | - 'dashicons dashicons-marker ee-icon-size-16 orange-text' |
|
| 724 | - ) |
|
| 725 | - . EEH_HTML::span($registration->event()->name(), '', 'orange-text') |
|
| 726 | - ); |
|
| 727 | - } |
|
| 728 | - } |
|
| 729 | - return new EE_Form_Section_Proper( |
|
| 730 | - array( |
|
| 731 | - 'layout_strategy' => new EE_Template_Layout( |
|
| 732 | - array( |
|
| 733 | - 'layout_template_file' => SPCO_REG_STEPS_PATH |
|
| 734 | - . $this->_slug |
|
| 735 | - . DS |
|
| 736 | - . 'events_requiring_pre_approval.template.php', // layout_template |
|
| 737 | - 'template_args' => apply_filters( |
|
| 738 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__template_args', |
|
| 739 | - array( |
|
| 740 | - 'events_requiring_pre_approval' => implode('', $events_requiring_pre_approval), |
|
| 741 | - 'events_requiring_pre_approval_msg' => apply_filters( |
|
| 742 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___events_requiring_pre_approval__events_requiring_pre_approval_msg', |
|
| 743 | - esc_html__( |
|
| 744 | - 'The following events do not require payment at this time and will not be billed during this transaction. Billing will only occur after the attendee has been approved by the event organizer. You will be notified when your registration has been processed. If this is a free event, then no billing will occur.', |
|
| 745 | - 'event_espresso' |
|
| 746 | - ) |
|
| 747 | - ), |
|
| 748 | - ) |
|
| 749 | - ), |
|
| 750 | - ) |
|
| 751 | - ), |
|
| 752 | - ) |
|
| 753 | - ); |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - |
|
| 757 | - /** |
|
| 758 | - * _no_payment_required |
|
| 759 | - * |
|
| 760 | - * @param \EE_Event[] $registrations_for_free_events |
|
| 761 | - * @return \EE_Form_Section_Proper |
|
| 762 | - * @throws \EE_Error |
|
| 763 | - */ |
|
| 764 | - private function _no_payment_required($registrations_for_free_events = array()) |
|
| 765 | - { |
|
| 766 | - // set some defaults |
|
| 767 | - $this->checkout->selected_method_of_payment = 'no_payment_required'; |
|
| 768 | - // generate no_payment_required form |
|
| 769 | - return new EE_Form_Section_Proper( |
|
| 770 | - array( |
|
| 771 | - 'layout_strategy' => new EE_Template_Layout( |
|
| 772 | - array( |
|
| 773 | - 'layout_template_file' => SPCO_REG_STEPS_PATH |
|
| 774 | - . $this->_slug |
|
| 775 | - . DS |
|
| 776 | - . 'no_payment_required.template.php', // layout_template |
|
| 777 | - 'template_args' => apply_filters( |
|
| 778 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___no_payment_required__template_args', |
|
| 779 | - array( |
|
| 780 | - 'revisit' => $this->checkout->revisit, |
|
| 781 | - 'registrations' => array(), |
|
| 782 | - 'ticket_count' => array(), |
|
| 783 | - 'registrations_for_free_events' => $registrations_for_free_events, |
|
| 784 | - 'no_payment_required_msg' => EEH_HTML::p( |
|
| 785 | - esc_html__('This is a free event, so no billing will occur.', 'event_espresso') |
|
| 786 | - ), |
|
| 787 | - ) |
|
| 788 | - ), |
|
| 789 | - ) |
|
| 790 | - ), |
|
| 791 | - ) |
|
| 792 | - ); |
|
| 793 | - } |
|
| 794 | - |
|
| 795 | - |
|
| 796 | - /** |
|
| 797 | - * _display_payment_options |
|
| 798 | - * |
|
| 799 | - * @param string $transaction_details |
|
| 800 | - * @return EE_Form_Section_Proper |
|
| 801 | - * @throws EE_Error |
|
| 802 | - * @throws InvalidArgumentException |
|
| 803 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 804 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 805 | - */ |
|
| 806 | - private function _display_payment_options($transaction_details = '') |
|
| 807 | - { |
|
| 808 | - // has method_of_payment been set by no-js user? |
|
| 809 | - $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(); |
|
| 810 | - // build payment options form |
|
| 811 | - return apply_filters( |
|
| 812 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__payment_options_form', |
|
| 813 | - new EE_Form_Section_Proper( |
|
| 814 | - array( |
|
| 815 | - 'subsections' => array( |
|
| 816 | - 'before_payment_options' => apply_filters( |
|
| 817 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__before_payment_options', |
|
| 818 | - new EE_Form_Section_Proper( |
|
| 819 | - array('layout_strategy' => new EE_Div_Per_Section_Layout()) |
|
| 820 | - ) |
|
| 821 | - ), |
|
| 822 | - 'payment_options' => $this->_setup_payment_options(), |
|
| 823 | - 'after_payment_options' => apply_filters( |
|
| 824 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__after_payment_options', |
|
| 825 | - new EE_Form_Section_Proper( |
|
| 826 | - array('layout_strategy' => new EE_Div_Per_Section_Layout()) |
|
| 827 | - ) |
|
| 828 | - ), |
|
| 829 | - ), |
|
| 830 | - 'layout_strategy' => new EE_Template_Layout( |
|
| 831 | - array( |
|
| 832 | - 'layout_template_file' => $this->_template, |
|
| 833 | - 'template_args' => apply_filters( |
|
| 834 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__template_args', |
|
| 835 | - array( |
|
| 836 | - 'reg_count' => $this->line_item_display->total_items(), |
|
| 837 | - 'transaction_details' => $transaction_details, |
|
| 838 | - 'available_payment_methods' => array(), |
|
| 839 | - ) |
|
| 840 | - ), |
|
| 841 | - ) |
|
| 842 | - ), |
|
| 843 | - ) |
|
| 844 | - ) |
|
| 845 | - ); |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - |
|
| 849 | - /** |
|
| 850 | - * _extra_hidden_inputs |
|
| 851 | - * |
|
| 852 | - * @param bool $no_payment_required |
|
| 853 | - * @return \EE_Form_Section_Proper |
|
| 854 | - * @throws \EE_Error |
|
| 855 | - */ |
|
| 856 | - private function _extra_hidden_inputs($no_payment_required = true) |
|
| 857 | - { |
|
| 858 | - return new EE_Form_Section_Proper( |
|
| 859 | - array( |
|
| 860 | - 'html_id' => 'ee-' . $this->slug() . '-extra-hidden-inputs', |
|
| 861 | - 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 862 | - 'subsections' => array( |
|
| 863 | - 'spco_no_payment_required' => new EE_Hidden_Input( |
|
| 864 | - array( |
|
| 865 | - 'normalization_strategy' => new EE_Boolean_Normalization(), |
|
| 866 | - 'html_name' => 'spco_no_payment_required', |
|
| 867 | - 'html_id' => 'spco-no-payment-required-payment_options', |
|
| 868 | - 'default' => $no_payment_required, |
|
| 869 | - ) |
|
| 870 | - ), |
|
| 871 | - 'spco_transaction_id' => new EE_Fixed_Hidden_Input( |
|
| 872 | - array( |
|
| 873 | - 'normalization_strategy' => new EE_Int_Normalization(), |
|
| 874 | - 'html_name' => 'spco_transaction_id', |
|
| 875 | - 'html_id' => 'spco-transaction-id', |
|
| 876 | - 'default' => $this->checkout->transaction->ID(), |
|
| 877 | - ) |
|
| 878 | - ), |
|
| 879 | - ), |
|
| 880 | - ) |
|
| 881 | - ); |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - |
|
| 885 | - /** |
|
| 886 | - * _apply_registration_payments_to_amount_owing |
|
| 887 | - * |
|
| 888 | - * @access protected |
|
| 889 | - * @param array $registrations |
|
| 890 | - * @throws EE_Error |
|
| 891 | - */ |
|
| 892 | - protected function _apply_registration_payments_to_amount_owing(array $registrations) |
|
| 893 | - { |
|
| 894 | - $payments = array(); |
|
| 895 | - foreach ($registrations as $registration) { |
|
| 896 | - if ($registration instanceof EE_Registration && $registration->owes_monies_and_can_pay()) { |
|
| 897 | - $payments += $registration->registration_payments(); |
|
| 898 | - } |
|
| 899 | - } |
|
| 900 | - if (! empty($payments)) { |
|
| 901 | - foreach ($payments as $payment) { |
|
| 902 | - if ($payment instanceof EE_Registration_Payment) { |
|
| 903 | - $this->checkout->amount_owing -= $payment->amount(); |
|
| 904 | - } |
|
| 905 | - } |
|
| 906 | - } |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - |
|
| 910 | - /** |
|
| 911 | - * _reset_selected_method_of_payment |
|
| 912 | - * |
|
| 913 | - * @access private |
|
| 914 | - * @param bool $force_reset |
|
| 915 | - * @return void |
|
| 916 | - * @throws InvalidArgumentException |
|
| 917 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 918 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 919 | - */ |
|
| 920 | - private function _reset_selected_method_of_payment($force_reset = false) |
|
| 921 | - { |
|
| 922 | - $reset_payment_method = $force_reset |
|
| 923 | - ? true |
|
| 924 | - : sanitize_text_field(EE_Registry::instance()->REQ->get('reset_payment_method', false)); |
|
| 925 | - if ($reset_payment_method) { |
|
| 926 | - $this->checkout->selected_method_of_payment = null; |
|
| 927 | - $this->checkout->payment_method = null; |
|
| 928 | - $this->checkout->billing_form = null; |
|
| 929 | - $this->_save_selected_method_of_payment(); |
|
| 930 | - } |
|
| 931 | - } |
|
| 932 | - |
|
| 933 | - |
|
| 934 | - /** |
|
| 935 | - * _save_selected_method_of_payment |
|
| 936 | - * stores the selected_method_of_payment in the session |
|
| 937 | - * so that it's available for all subsequent requests including AJAX |
|
| 938 | - * |
|
| 939 | - * @access private |
|
| 940 | - * @param string $selected_method_of_payment |
|
| 941 | - * @return void |
|
| 942 | - * @throws InvalidArgumentException |
|
| 943 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 944 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 945 | - */ |
|
| 946 | - private function _save_selected_method_of_payment($selected_method_of_payment = '') |
|
| 947 | - { |
|
| 948 | - $selected_method_of_payment = ! empty($selected_method_of_payment) |
|
| 949 | - ? $selected_method_of_payment |
|
| 950 | - : $this->checkout->selected_method_of_payment; |
|
| 951 | - EE_Registry::instance()->SSN->set_session_data( |
|
| 952 | - array('selected_method_of_payment' => $selected_method_of_payment) |
|
| 953 | - ); |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - |
|
| 957 | - /** |
|
| 958 | - * _setup_payment_options |
|
| 959 | - * |
|
| 960 | - * @return EE_Form_Section_Proper |
|
| 961 | - * @throws EE_Error |
|
| 962 | - * @throws InvalidArgumentException |
|
| 963 | - * @throws ReflectionException |
|
| 964 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 965 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 966 | - */ |
|
| 967 | - public function _setup_payment_options() |
|
| 968 | - { |
|
| 969 | - // load payment method classes |
|
| 970 | - $this->checkout->available_payment_methods = $this->_get_available_payment_methods(); |
|
| 971 | - if (empty($this->checkout->available_payment_methods)) { |
|
| 972 | - EE_Error::add_error( |
|
| 973 | - apply_filters( |
|
| 974 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___setup_payment_options__error_message_no_payment_methods', |
|
| 975 | - sprintf( |
|
| 976 | - esc_html__( |
|
| 977 | - 'Sorry, you cannot complete your purchase because a payment method is not active.%1$s Please contact %2$s for assistance and provide a description of the problem.', |
|
| 978 | - 'event_espresso' |
|
| 979 | - ), |
|
| 980 | - '<br>', |
|
| 981 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 982 | - ) |
|
| 983 | - ), |
|
| 984 | - __FILE__, |
|
| 985 | - __FUNCTION__, |
|
| 986 | - __LINE__ |
|
| 987 | - ); |
|
| 988 | - } |
|
| 989 | - // switch up header depending on number of available payment methods |
|
| 990 | - $payment_method_header = count($this->checkout->available_payment_methods) > 1 |
|
| 991 | - ? apply_filters( |
|
| 992 | - 'FHEE__registration_page_payment_options__method_of_payment_hdr', |
|
| 993 | - esc_html__('Please Select Your Method of Payment', 'event_espresso') |
|
| 994 | - ) |
|
| 995 | - : apply_filters( |
|
| 996 | - 'FHEE__registration_page_payment_options__method_of_payment_hdr', |
|
| 997 | - esc_html__('Method of Payment', 'event_espresso') |
|
| 998 | - ); |
|
| 999 | - $available_payment_methods = array( |
|
| 1000 | - // display the "Payment Method" header |
|
| 1001 | - 'payment_method_header' => new EE_Form_Section_HTML( |
|
| 1002 | - apply_filters( |
|
| 1003 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___setup_payment_options__payment_method_header', |
|
| 1004 | - EEH_HTML::h4($payment_method_header, 'method-of-payment-hdr'), |
|
| 1005 | - $payment_method_header |
|
| 1006 | - ) |
|
| 1007 | - ), |
|
| 1008 | - ); |
|
| 1009 | - // the list of actual payment methods ( invoice, paypal, etc ) in a ( slug => HTML ) format |
|
| 1010 | - $available_payment_method_options = array(); |
|
| 1011 | - $default_payment_method_option = array(); |
|
| 1012 | - // additional instructions to be displayed and hidden below payment methods (adding a clearing div to start) |
|
| 1013 | - $payment_methods_billing_info = array( |
|
| 1014 | - new EE_Form_Section_HTML( |
|
| 1015 | - EEH_HTML::div('<br />', '', '', 'clear:both;') |
|
| 1016 | - ), |
|
| 1017 | - ); |
|
| 1018 | - // loop through payment methods |
|
| 1019 | - foreach ($this->checkout->available_payment_methods as $payment_method) { |
|
| 1020 | - if ($payment_method instanceof EE_Payment_Method) { |
|
| 1021 | - $payment_method_button = EEH_HTML::img( |
|
| 1022 | - $payment_method->button_url(), |
|
| 1023 | - $payment_method->name(), |
|
| 1024 | - 'spco-payment-method-' . $payment_method->slug() . '-btn-img', |
|
| 1025 | - 'spco-payment-method-btn-img' |
|
| 1026 | - ); |
|
| 1027 | - // check if any payment methods are set as default |
|
| 1028 | - // if payment method is already selected OR nothing is selected and this payment method should be |
|
| 1029 | - // open_by_default |
|
| 1030 | - if (($this->checkout->selected_method_of_payment === $payment_method->slug()) |
|
| 1031 | - || (! $this->checkout->selected_method_of_payment && $payment_method->open_by_default()) |
|
| 1032 | - ) { |
|
| 1033 | - $this->checkout->selected_method_of_payment = $payment_method->slug(); |
|
| 1034 | - $this->_save_selected_method_of_payment(); |
|
| 1035 | - $default_payment_method_option[ $payment_method->slug() ] = $payment_method_button; |
|
| 1036 | - } else { |
|
| 1037 | - $available_payment_method_options[ $payment_method->slug() ] = $payment_method_button; |
|
| 1038 | - } |
|
| 1039 | - $payment_methods_billing_info[ $payment_method->slug( |
|
| 1040 | - ) . '-info' ] = $this->_payment_method_billing_info( |
|
| 1041 | - $payment_method |
|
| 1042 | - ); |
|
| 1043 | - } |
|
| 1044 | - } |
|
| 1045 | - // prepend available_payment_method_options with default_payment_method_option so that it appears first in list |
|
| 1046 | - // of PMs |
|
| 1047 | - $available_payment_method_options = $default_payment_method_option + $available_payment_method_options; |
|
| 1048 | - // now generate the actual form inputs |
|
| 1049 | - $available_payment_methods['available_payment_methods'] = $this->_available_payment_method_inputs( |
|
| 1050 | - $available_payment_method_options |
|
| 1051 | - ); |
|
| 1052 | - $available_payment_methods += $payment_methods_billing_info; |
|
| 1053 | - // build the available payment methods form |
|
| 1054 | - return new EE_Form_Section_Proper( |
|
| 1055 | - array( |
|
| 1056 | - 'html_id' => 'spco-available-methods-of-payment-dv', |
|
| 1057 | - 'subsections' => $available_payment_methods, |
|
| 1058 | - 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 1059 | - ) |
|
| 1060 | - ); |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - |
|
| 1064 | - /** |
|
| 1065 | - * _get_available_payment_methods |
|
| 1066 | - * |
|
| 1067 | - * @return EE_Payment_Method[] |
|
| 1068 | - * @throws EE_Error |
|
| 1069 | - * @throws InvalidArgumentException |
|
| 1070 | - * @throws ReflectionException |
|
| 1071 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1072 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1073 | - */ |
|
| 1074 | - protected function _get_available_payment_methods() |
|
| 1075 | - { |
|
| 1076 | - if (! empty($this->checkout->available_payment_methods)) { |
|
| 1077 | - return $this->checkout->available_payment_methods; |
|
| 1078 | - } |
|
| 1079 | - $available_payment_methods = array(); |
|
| 1080 | - // load EEM_Payment_Method |
|
| 1081 | - EE_Registry::instance()->load_model('Payment_Method'); |
|
| 1082 | - /** @type EEM_Payment_Method $EEM_Payment_Method */ |
|
| 1083 | - $EEM_Payment_Method = EE_Registry::instance()->LIB->EEM_Payment_Method; |
|
| 1084 | - // get all active payment methods |
|
| 1085 | - $payment_methods = $EEM_Payment_Method->get_all_for_transaction( |
|
| 1086 | - $this->checkout->transaction, |
|
| 1087 | - EEM_Payment_Method::scope_cart |
|
| 1088 | - ); |
|
| 1089 | - foreach ($payment_methods as $payment_method) { |
|
| 1090 | - if ($payment_method instanceof EE_Payment_Method) { |
|
| 1091 | - $available_payment_methods[ $payment_method->slug() ] = $payment_method; |
|
| 1092 | - } |
|
| 1093 | - } |
|
| 1094 | - return $available_payment_methods; |
|
| 1095 | - } |
|
| 1096 | - |
|
| 1097 | - |
|
| 1098 | - /** |
|
| 1099 | - * _available_payment_method_inputs |
|
| 1100 | - * |
|
| 1101 | - * @access private |
|
| 1102 | - * @param array $available_payment_method_options |
|
| 1103 | - * @return \EE_Form_Section_Proper |
|
| 1104 | - */ |
|
| 1105 | - private function _available_payment_method_inputs($available_payment_method_options = array()) |
|
| 1106 | - { |
|
| 1107 | - // generate inputs |
|
| 1108 | - return new EE_Form_Section_Proper( |
|
| 1109 | - array( |
|
| 1110 | - 'html_id' => 'ee-available-payment-method-inputs', |
|
| 1111 | - 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 1112 | - 'subsections' => array( |
|
| 1113 | - '' => new EE_Radio_Button_Input( |
|
| 1114 | - $available_payment_method_options, |
|
| 1115 | - array( |
|
| 1116 | - 'html_name' => 'selected_method_of_payment', |
|
| 1117 | - 'html_class' => 'spco-payment-method', |
|
| 1118 | - 'default' => $this->checkout->selected_method_of_payment, |
|
| 1119 | - 'label_size' => 11, |
|
| 1120 | - 'enforce_label_size' => true, |
|
| 1121 | - ) |
|
| 1122 | - ), |
|
| 1123 | - ), |
|
| 1124 | - ) |
|
| 1125 | - ); |
|
| 1126 | - } |
|
| 1127 | - |
|
| 1128 | - |
|
| 1129 | - /** |
|
| 1130 | - * _payment_method_billing_info |
|
| 1131 | - * |
|
| 1132 | - * @access private |
|
| 1133 | - * @param EE_Payment_Method $payment_method |
|
| 1134 | - * @return EE_Form_Section_Proper |
|
| 1135 | - * @throws EE_Error |
|
| 1136 | - * @throws InvalidArgumentException |
|
| 1137 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1138 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1139 | - */ |
|
| 1140 | - private function _payment_method_billing_info(EE_Payment_Method $payment_method) |
|
| 1141 | - { |
|
| 1142 | - $currently_selected = $this->checkout->selected_method_of_payment === $payment_method->slug() |
|
| 1143 | - ? true |
|
| 1144 | - : false; |
|
| 1145 | - // generate the billing form for payment method |
|
| 1146 | - $billing_form = $currently_selected |
|
| 1147 | - ? $this->_get_billing_form_for_payment_method($payment_method) |
|
| 1148 | - : new EE_Form_Section_HTML(); |
|
| 1149 | - $this->checkout->billing_form = $currently_selected |
|
| 1150 | - ? $billing_form |
|
| 1151 | - : $this->checkout->billing_form; |
|
| 1152 | - // it's all in the details |
|
| 1153 | - $info_html = EEH_HTML::h3( |
|
| 1154 | - esc_html__('Important information regarding your payment', 'event_espresso'), |
|
| 1155 | - '', |
|
| 1156 | - 'spco-payment-method-hdr' |
|
| 1157 | - ); |
|
| 1158 | - // add some info regarding the step, either from what's saved in the admin, |
|
| 1159 | - // or a default string depending on whether the PM has a billing form or not |
|
| 1160 | - if ($payment_method->description()) { |
|
| 1161 | - $payment_method_info = $payment_method->description(); |
|
| 1162 | - } elseif ($billing_form instanceof EE_Billing_Info_Form) { |
|
| 1163 | - $payment_method_info = sprintf( |
|
| 1164 | - esc_html__( |
|
| 1165 | - 'Please provide the following billing information, then click the "%1$s" button below in order to proceed.', |
|
| 1166 | - 'event_espresso' |
|
| 1167 | - ), |
|
| 1168 | - $this->submit_button_text() |
|
| 1169 | - ); |
|
| 1170 | - } else { |
|
| 1171 | - $payment_method_info = sprintf( |
|
| 1172 | - esc_html__('Please click the "%1$s" button below in order to proceed.', 'event_espresso'), |
|
| 1173 | - $this->submit_button_text() |
|
| 1174 | - ); |
|
| 1175 | - } |
|
| 1176 | - $info_html .= EEH_HTML::div( |
|
| 1177 | - apply_filters( |
|
| 1178 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___payment_method_billing_info__payment_method_info', |
|
| 1179 | - $payment_method_info |
|
| 1180 | - ), |
|
| 1181 | - '', |
|
| 1182 | - 'spco-payment-method-desc ee-attention' |
|
| 1183 | - ); |
|
| 1184 | - return new EE_Form_Section_Proper( |
|
| 1185 | - array( |
|
| 1186 | - 'html_id' => 'spco-payment-method-info-' . $payment_method->slug(), |
|
| 1187 | - 'html_class' => 'spco-payment-method-info-dv', |
|
| 1188 | - // only display the selected or default PM |
|
| 1189 | - 'html_style' => $currently_selected ? '' : 'display:none;', |
|
| 1190 | - 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 1191 | - 'subsections' => array( |
|
| 1192 | - 'info' => new EE_Form_Section_HTML($info_html), |
|
| 1193 | - 'billing_form' => $currently_selected ? $billing_form : new EE_Form_Section_HTML(), |
|
| 1194 | - ), |
|
| 1195 | - ) |
|
| 1196 | - ); |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - |
|
| 1200 | - /** |
|
| 1201 | - * get_billing_form_html_for_payment_method |
|
| 1202 | - * |
|
| 1203 | - * @access public |
|
| 1204 | - * @return string |
|
| 1205 | - * @throws EE_Error |
|
| 1206 | - * @throws InvalidArgumentException |
|
| 1207 | - * @throws ReflectionException |
|
| 1208 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1209 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1210 | - */ |
|
| 1211 | - public function get_billing_form_html_for_payment_method() |
|
| 1212 | - { |
|
| 1213 | - // how have they chosen to pay? |
|
| 1214 | - $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true); |
|
| 1215 | - $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment(); |
|
| 1216 | - if (! $this->checkout->payment_method instanceof EE_Payment_Method) { |
|
| 1217 | - return false; |
|
| 1218 | - } |
|
| 1219 | - if (apply_filters( |
|
| 1220 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success', |
|
| 1221 | - false |
|
| 1222 | - )) { |
|
| 1223 | - EE_Error::add_success( |
|
| 1224 | - apply_filters( |
|
| 1225 | - 'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method', |
|
| 1226 | - sprintf( |
|
| 1227 | - esc_html__( |
|
| 1228 | - 'You have selected "%s" as your method of payment. Please note the important payment information below.', |
|
| 1229 | - 'event_espresso' |
|
| 1230 | - ), |
|
| 1231 | - $this->checkout->payment_method->name() |
|
| 1232 | - ) |
|
| 1233 | - ) |
|
| 1234 | - ); |
|
| 1235 | - } |
|
| 1236 | - // now generate billing form for selected method of payment |
|
| 1237 | - $payment_method_billing_form = $this->_get_billing_form_for_payment_method($this->checkout->payment_method); |
|
| 1238 | - // fill form with attendee info if applicable |
|
| 1239 | - if ($payment_method_billing_form instanceof EE_Billing_Attendee_Info_Form |
|
| 1240 | - && $this->checkout->transaction_has_primary_registrant() |
|
| 1241 | - ) { |
|
| 1242 | - $payment_method_billing_form->populate_from_attendee( |
|
| 1243 | - $this->checkout->transaction->primary_registration()->attendee() |
|
| 1244 | - ); |
|
| 1245 | - } |
|
| 1246 | - // and debug content |
|
| 1247 | - if ($payment_method_billing_form instanceof EE_Billing_Info_Form |
|
| 1248 | - && $this->checkout->payment_method->type_obj() instanceof EE_PMT_Base |
|
| 1249 | - ) { |
|
| 1250 | - $payment_method_billing_form = |
|
| 1251 | - $this->checkout->payment_method->type_obj()->apply_billing_form_debug_settings( |
|
| 1252 | - $payment_method_billing_form |
|
| 1253 | - ); |
|
| 1254 | - } |
|
| 1255 | - $billing_info = $payment_method_billing_form instanceof EE_Form_Section_Proper |
|
| 1256 | - ? $payment_method_billing_form->get_html() |
|
| 1257 | - : ''; |
|
| 1258 | - $this->checkout->json_response->set_return_data(array('payment_method_info' => $billing_info)); |
|
| 1259 | - // localize validation rules for main form |
|
| 1260 | - $this->checkout->current_step->reg_form->localize_validation_rules(); |
|
| 1261 | - $this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization()); |
|
| 1262 | - return true; |
|
| 1263 | - } |
|
| 1264 | - |
|
| 1265 | - |
|
| 1266 | - /** |
|
| 1267 | - * _get_billing_form_for_payment_method |
|
| 1268 | - * |
|
| 1269 | - * @access private |
|
| 1270 | - * @param EE_Payment_Method $payment_method |
|
| 1271 | - * @return EE_Billing_Info_Form|EE_Form_Section_HTML |
|
| 1272 | - * @throws EE_Error |
|
| 1273 | - * @throws InvalidArgumentException |
|
| 1274 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1275 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1276 | - */ |
|
| 1277 | - private function _get_billing_form_for_payment_method(EE_Payment_Method $payment_method) |
|
| 1278 | - { |
|
| 1279 | - $billing_form = $payment_method->type_obj()->billing_form( |
|
| 1280 | - $this->checkout->transaction, |
|
| 1281 | - array('amount_owing' => $this->checkout->amount_owing) |
|
| 1282 | - ); |
|
| 1283 | - if ($billing_form instanceof EE_Billing_Info_Form) { |
|
| 1284 | - if (apply_filters( |
|
| 1285 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success', |
|
| 1286 | - false |
|
| 1287 | - ) |
|
| 1288 | - && EE_Registry::instance()->REQ->is_set('payment_method') |
|
| 1289 | - ) { |
|
| 1290 | - EE_Error::add_success( |
|
| 1291 | - apply_filters( |
|
| 1292 | - 'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method', |
|
| 1293 | - sprintf( |
|
| 1294 | - esc_html__( |
|
| 1295 | - 'You have selected "%s" as your method of payment. Please note the important payment information below.', |
|
| 1296 | - 'event_espresso' |
|
| 1297 | - ), |
|
| 1298 | - $payment_method->name() |
|
| 1299 | - ) |
|
| 1300 | - ) |
|
| 1301 | - ); |
|
| 1302 | - } |
|
| 1303 | - return apply_filters( |
|
| 1304 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___get_billing_form_for_payment_method__billing_form', |
|
| 1305 | - $billing_form, |
|
| 1306 | - $payment_method |
|
| 1307 | - ); |
|
| 1308 | - } |
|
| 1309 | - // no actual billing form, so return empty HTML form section |
|
| 1310 | - return new EE_Form_Section_HTML(); |
|
| 1311 | - } |
|
| 1312 | - |
|
| 1313 | - |
|
| 1314 | - /** |
|
| 1315 | - * _get_selected_method_of_payment |
|
| 1316 | - * |
|
| 1317 | - * @access private |
|
| 1318 | - * @param boolean $required whether to throw an error if the "selected_method_of_payment" |
|
| 1319 | - * is not found in the incoming request |
|
| 1320 | - * @param string $request_param |
|
| 1321 | - * @return NULL|string |
|
| 1322 | - * @throws EE_Error |
|
| 1323 | - * @throws InvalidArgumentException |
|
| 1324 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1325 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1326 | - */ |
|
| 1327 | - private function _get_selected_method_of_payment( |
|
| 1328 | - $required = false, |
|
| 1329 | - $request_param = 'selected_method_of_payment' |
|
| 1330 | - ) { |
|
| 1331 | - // is selected_method_of_payment set in the request ? |
|
| 1332 | - $selected_method_of_payment = EE_Registry::instance()->REQ->get($request_param, false); |
|
| 1333 | - if ($selected_method_of_payment) { |
|
| 1334 | - // sanitize it |
|
| 1335 | - $selected_method_of_payment = is_array($selected_method_of_payment) |
|
| 1336 | - ? array_shift($selected_method_of_payment) |
|
| 1337 | - : $selected_method_of_payment; |
|
| 1338 | - $selected_method_of_payment = sanitize_text_field($selected_method_of_payment); |
|
| 1339 | - // store it in the session so that it's available for all subsequent requests including AJAX |
|
| 1340 | - $this->_save_selected_method_of_payment($selected_method_of_payment); |
|
| 1341 | - } else { |
|
| 1342 | - // or is is set in the session ? |
|
| 1343 | - $selected_method_of_payment = EE_Registry::instance()->SSN->get_session_data( |
|
| 1344 | - 'selected_method_of_payment' |
|
| 1345 | - ); |
|
| 1346 | - } |
|
| 1347 | - // do ya really really gotta have it? |
|
| 1348 | - if (empty($selected_method_of_payment) && $required) { |
|
| 1349 | - EE_Error::add_error( |
|
| 1350 | - sprintf( |
|
| 1351 | - esc_html__( |
|
| 1352 | - 'The selected method of payment could not be determined.%sPlease ensure that you have selected one before proceeding.%sIf you continue to experience difficulties, then refresh your browser and try again, or contact %s for assistance.', |
|
| 1353 | - 'event_espresso' |
|
| 1354 | - ), |
|
| 1355 | - '<br/>', |
|
| 1356 | - '<br/>', |
|
| 1357 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 1358 | - ), |
|
| 1359 | - __FILE__, |
|
| 1360 | - __FUNCTION__, |
|
| 1361 | - __LINE__ |
|
| 1362 | - ); |
|
| 1363 | - return null; |
|
| 1364 | - } |
|
| 1365 | - return $selected_method_of_payment; |
|
| 1366 | - } |
|
| 1367 | - |
|
| 1368 | - |
|
| 1369 | - |
|
| 1370 | - |
|
| 1371 | - |
|
| 1372 | - |
|
| 1373 | - /********************************************************************************************************/ |
|
| 1374 | - /*********************************** SWITCH PAYMENT METHOD ************************************/ |
|
| 1375 | - /********************************************************************************************************/ |
|
| 1376 | - /** |
|
| 1377 | - * switch_payment_method |
|
| 1378 | - * |
|
| 1379 | - * @access public |
|
| 1380 | - * @return string |
|
| 1381 | - * @throws EE_Error |
|
| 1382 | - * @throws InvalidArgumentException |
|
| 1383 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1384 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1385 | - */ |
|
| 1386 | - public function switch_payment_method() |
|
| 1387 | - { |
|
| 1388 | - if (! $this->_verify_payment_method_is_set()) { |
|
| 1389 | - return false; |
|
| 1390 | - } |
|
| 1391 | - if (apply_filters( |
|
| 1392 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success', |
|
| 1393 | - false |
|
| 1394 | - )) { |
|
| 1395 | - EE_Error::add_success( |
|
| 1396 | - apply_filters( |
|
| 1397 | - 'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method', |
|
| 1398 | - sprintf( |
|
| 1399 | - esc_html__( |
|
| 1400 | - 'You have selected "%s" as your method of payment. Please note the important payment information below.', |
|
| 1401 | - 'event_espresso' |
|
| 1402 | - ), |
|
| 1403 | - $this->checkout->payment_method->name() |
|
| 1404 | - ) |
|
| 1405 | - ) |
|
| 1406 | - ); |
|
| 1407 | - } |
|
| 1408 | - // generate billing form for selected method of payment if it hasn't been done already |
|
| 1409 | - if ($this->checkout->payment_method->type_obj()->has_billing_form()) { |
|
| 1410 | - $this->checkout->billing_form = $this->_get_billing_form_for_payment_method( |
|
| 1411 | - $this->checkout->payment_method |
|
| 1412 | - ); |
|
| 1413 | - } |
|
| 1414 | - // fill form with attendee info if applicable |
|
| 1415 | - if (apply_filters( |
|
| 1416 | - 'FHEE__populate_billing_form_fields_from_attendee', |
|
| 1417 | - ( |
|
| 1418 | - $this->checkout->billing_form instanceof EE_Billing_Attendee_Info_Form |
|
| 1419 | - && $this->checkout->transaction_has_primary_registrant() |
|
| 1420 | - ), |
|
| 1421 | - $this->checkout->billing_form, |
|
| 1422 | - $this->checkout->transaction |
|
| 1423 | - ) |
|
| 1424 | - ) { |
|
| 1425 | - $this->checkout->billing_form->populate_from_attendee( |
|
| 1426 | - $this->checkout->transaction->primary_registration()->attendee() |
|
| 1427 | - ); |
|
| 1428 | - } |
|
| 1429 | - // and debug content |
|
| 1430 | - if ($this->checkout->billing_form instanceof EE_Billing_Info_Form |
|
| 1431 | - && $this->checkout->payment_method->type_obj() instanceof EE_PMT_Base |
|
| 1432 | - ) { |
|
| 1433 | - $this->checkout->billing_form = |
|
| 1434 | - $this->checkout->payment_method->type_obj()->apply_billing_form_debug_settings( |
|
| 1435 | - $this->checkout->billing_form |
|
| 1436 | - ); |
|
| 1437 | - } |
|
| 1438 | - // get html and validation rules for form |
|
| 1439 | - if ($this->checkout->billing_form instanceof EE_Form_Section_Proper) { |
|
| 1440 | - $this->checkout->json_response->set_return_data( |
|
| 1441 | - array('payment_method_info' => $this->checkout->billing_form->get_html()) |
|
| 1442 | - ); |
|
| 1443 | - // localize validation rules for main form |
|
| 1444 | - $this->checkout->billing_form->localize_validation_rules(true); |
|
| 1445 | - $this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization()); |
|
| 1446 | - } else { |
|
| 1447 | - $this->checkout->json_response->set_return_data(array('payment_method_info' => '')); |
|
| 1448 | - } |
|
| 1449 | - // prevents advancement to next step |
|
| 1450 | - $this->checkout->continue_reg = false; |
|
| 1451 | - return true; |
|
| 1452 | - } |
|
| 1453 | - |
|
| 1454 | - |
|
| 1455 | - /** |
|
| 1456 | - * _verify_payment_method_is_set |
|
| 1457 | - * |
|
| 1458 | - * @return bool |
|
| 1459 | - * @throws EE_Error |
|
| 1460 | - * @throws InvalidArgumentException |
|
| 1461 | - * @throws ReflectionException |
|
| 1462 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1463 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1464 | - */ |
|
| 1465 | - protected function _verify_payment_method_is_set() |
|
| 1466 | - { |
|
| 1467 | - // generate billing form for selected method of payment if it hasn't been done already |
|
| 1468 | - if (empty($this->checkout->selected_method_of_payment)) { |
|
| 1469 | - // how have they chosen to pay? |
|
| 1470 | - $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true); |
|
| 1471 | - } else { |
|
| 1472 | - // choose your own adventure based on method_of_payment |
|
| 1473 | - switch ($this->checkout->selected_method_of_payment) { |
|
| 1474 | - case 'events_sold_out': |
|
| 1475 | - EE_Error::add_attention( |
|
| 1476 | - apply_filters( |
|
| 1477 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__sold_out_events_msg', |
|
| 1478 | - esc_html__( |
|
| 1479 | - 'It appears that the event you were about to make a payment for has sold out since this form first loaded. Please contact the event administrator if you believe this is an error.', |
|
| 1480 | - 'event_espresso' |
|
| 1481 | - ) |
|
| 1482 | - ), |
|
| 1483 | - __FILE__, |
|
| 1484 | - __FUNCTION__, |
|
| 1485 | - __LINE__ |
|
| 1486 | - ); |
|
| 1487 | - return false; |
|
| 1488 | - break; |
|
| 1489 | - case 'payments_closed': |
|
| 1490 | - EE_Error::add_attention( |
|
| 1491 | - apply_filters( |
|
| 1492 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__payments_closed_msg', |
|
| 1493 | - esc_html__( |
|
| 1494 | - 'It appears that the event you were about to make a payment for is not accepting payments at this time. Please contact the event administrator if you believe this is an error.', |
|
| 1495 | - 'event_espresso' |
|
| 1496 | - ) |
|
| 1497 | - ), |
|
| 1498 | - __FILE__, |
|
| 1499 | - __FUNCTION__, |
|
| 1500 | - __LINE__ |
|
| 1501 | - ); |
|
| 1502 | - return false; |
|
| 1503 | - break; |
|
| 1504 | - case 'no_payment_required': |
|
| 1505 | - EE_Error::add_attention( |
|
| 1506 | - apply_filters( |
|
| 1507 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__no_payment_required_msg', |
|
| 1508 | - esc_html__( |
|
| 1509 | - 'It appears that the event you were about to make a payment for does not require payment. Please contact the event administrator if you believe this is an error.', |
|
| 1510 | - 'event_espresso' |
|
| 1511 | - ) |
|
| 1512 | - ), |
|
| 1513 | - __FILE__, |
|
| 1514 | - __FUNCTION__, |
|
| 1515 | - __LINE__ |
|
| 1516 | - ); |
|
| 1517 | - return false; |
|
| 1518 | - break; |
|
| 1519 | - default: |
|
| 1520 | - } |
|
| 1521 | - } |
|
| 1522 | - // verify payment method |
|
| 1523 | - if (! $this->checkout->payment_method instanceof EE_Payment_Method) { |
|
| 1524 | - // get payment method for selected method of payment |
|
| 1525 | - $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment(); |
|
| 1526 | - } |
|
| 1527 | - return $this->checkout->payment_method instanceof EE_Payment_Method ? true : false; |
|
| 1528 | - } |
|
| 1529 | - |
|
| 1530 | - |
|
| 1531 | - |
|
| 1532 | - /********************************************************************************************************/ |
|
| 1533 | - /*************************************** SAVE PAYER DETAILS ****************************************/ |
|
| 1534 | - /********************************************************************************************************/ |
|
| 1535 | - /** |
|
| 1536 | - * save_payer_details_via_ajax |
|
| 1537 | - * |
|
| 1538 | - * @return void |
|
| 1539 | - * @throws EE_Error |
|
| 1540 | - * @throws InvalidArgumentException |
|
| 1541 | - * @throws ReflectionException |
|
| 1542 | - * @throws RuntimeException |
|
| 1543 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1544 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1545 | - */ |
|
| 1546 | - public function save_payer_details_via_ajax() |
|
| 1547 | - { |
|
| 1548 | - if (! $this->_verify_payment_method_is_set()) { |
|
| 1549 | - return; |
|
| 1550 | - } |
|
| 1551 | - // generate billing form for selected method of payment if it hasn't been done already |
|
| 1552 | - if ($this->checkout->payment_method->type_obj()->has_billing_form()) { |
|
| 1553 | - $this->checkout->billing_form = $this->_get_billing_form_for_payment_method( |
|
| 1554 | - $this->checkout->payment_method |
|
| 1555 | - ); |
|
| 1556 | - } |
|
| 1557 | - // generate primary attendee from payer info if applicable |
|
| 1558 | - if (! $this->checkout->transaction_has_primary_registrant()) { |
|
| 1559 | - $attendee = $this->_create_attendee_from_request_data(); |
|
| 1560 | - if ($attendee instanceof EE_Attendee) { |
|
| 1561 | - foreach ($this->checkout->transaction->registrations() as $registration) { |
|
| 1562 | - if ($registration->is_primary_registrant()) { |
|
| 1563 | - $this->checkout->primary_attendee_obj = $attendee; |
|
| 1564 | - $registration->_add_relation_to($attendee, 'Attendee'); |
|
| 1565 | - $registration->set_attendee_id($attendee->ID()); |
|
| 1566 | - $registration->update_cache_after_object_save('Attendee', $attendee); |
|
| 1567 | - } |
|
| 1568 | - } |
|
| 1569 | - } |
|
| 1570 | - } |
|
| 1571 | - } |
|
| 1572 | - |
|
| 1573 | - |
|
| 1574 | - /** |
|
| 1575 | - * create_attendee_from_request_data |
|
| 1576 | - * uses info from alternate GET or POST data (such as AJAX) to create a new attendee |
|
| 1577 | - * |
|
| 1578 | - * @return EE_Attendee |
|
| 1579 | - * @throws EE_Error |
|
| 1580 | - * @throws InvalidArgumentException |
|
| 1581 | - * @throws ReflectionException |
|
| 1582 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1583 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1584 | - */ |
|
| 1585 | - protected function _create_attendee_from_request_data() |
|
| 1586 | - { |
|
| 1587 | - // get State ID |
|
| 1588 | - $STA_ID = ! empty($_REQUEST['state']) ? sanitize_text_field($_REQUEST['state']) : ''; |
|
| 1589 | - if (! empty($STA_ID)) { |
|
| 1590 | - // can we get state object from name ? |
|
| 1591 | - EE_Registry::instance()->load_model('State'); |
|
| 1592 | - $state = EEM_State::instance()->get_col(array(array('STA_name' => $STA_ID), 'limit' => 1), 'STA_ID'); |
|
| 1593 | - $STA_ID = is_array($state) && ! empty($state) ? reset($state) : $STA_ID; |
|
| 1594 | - } |
|
| 1595 | - // get Country ISO |
|
| 1596 | - $CNT_ISO = ! empty($_REQUEST['country']) ? sanitize_text_field($_REQUEST['country']) : ''; |
|
| 1597 | - if (! empty($CNT_ISO)) { |
|
| 1598 | - // can we get country object from name ? |
|
| 1599 | - EE_Registry::instance()->load_model('Country'); |
|
| 1600 | - $country = EEM_Country::instance()->get_col( |
|
| 1601 | - array(array('CNT_name' => $CNT_ISO), 'limit' => 1), |
|
| 1602 | - 'CNT_ISO' |
|
| 1603 | - ); |
|
| 1604 | - $CNT_ISO = is_array($country) && ! empty($country) ? reset($country) : $CNT_ISO; |
|
| 1605 | - } |
|
| 1606 | - // grab attendee data |
|
| 1607 | - $attendee_data = array( |
|
| 1608 | - 'ATT_fname' => ! empty($_REQUEST['first_name']) ? sanitize_text_field($_REQUEST['first_name']) : '', |
|
| 1609 | - 'ATT_lname' => ! empty($_REQUEST['last_name']) ? sanitize_text_field($_REQUEST['last_name']) : '', |
|
| 1610 | - 'ATT_email' => ! empty($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '', |
|
| 1611 | - 'ATT_address' => ! empty($_REQUEST['address']) ? sanitize_text_field($_REQUEST['address']) : '', |
|
| 1612 | - 'ATT_address2' => ! empty($_REQUEST['address2']) ? sanitize_text_field($_REQUEST['address2']) : '', |
|
| 1613 | - 'ATT_city' => ! empty($_REQUEST['city']) ? sanitize_text_field($_REQUEST['city']) : '', |
|
| 1614 | - 'STA_ID' => $STA_ID, |
|
| 1615 | - 'CNT_ISO' => $CNT_ISO, |
|
| 1616 | - 'ATT_zip' => ! empty($_REQUEST['zip']) ? sanitize_text_field($_REQUEST['zip']) : '', |
|
| 1617 | - 'ATT_phone' => ! empty($_REQUEST['phone']) ? sanitize_text_field($_REQUEST['phone']) : '', |
|
| 1618 | - ); |
|
| 1619 | - // validate the email address since it is the most important piece of info |
|
| 1620 | - if (empty($attendee_data['ATT_email']) || $attendee_data['ATT_email'] !== $_REQUEST['email']) { |
|
| 1621 | - EE_Error::add_error( |
|
| 1622 | - esc_html__('An invalid email address was submitted.', 'event_espresso'), |
|
| 1623 | - __FILE__, |
|
| 1624 | - __FUNCTION__, |
|
| 1625 | - __LINE__ |
|
| 1626 | - ); |
|
| 1627 | - } |
|
| 1628 | - // does this attendee already exist in the db ? we're searching using a combination of first name, last name, |
|
| 1629 | - // AND email address |
|
| 1630 | - if (! empty($attendee_data['ATT_fname']) |
|
| 1631 | - && ! empty($attendee_data['ATT_lname']) |
|
| 1632 | - && ! empty($attendee_data['ATT_email']) |
|
| 1633 | - ) { |
|
| 1634 | - $existing_attendee = EE_Registry::instance()->LIB->EEM_Attendee->find_existing_attendee( |
|
| 1635 | - array( |
|
| 1636 | - 'ATT_fname' => $attendee_data['ATT_fname'], |
|
| 1637 | - 'ATT_lname' => $attendee_data['ATT_lname'], |
|
| 1638 | - 'ATT_email' => $attendee_data['ATT_email'], |
|
| 1639 | - ) |
|
| 1640 | - ); |
|
| 1641 | - if ($existing_attendee instanceof EE_Attendee) { |
|
| 1642 | - return $existing_attendee; |
|
| 1643 | - } |
|
| 1644 | - } |
|
| 1645 | - // no existing attendee? kk let's create a new one |
|
| 1646 | - // kinda lame, but we need a first and last name to create an attendee, so use the email address if those |
|
| 1647 | - // don't exist |
|
| 1648 | - $attendee_data['ATT_fname'] = ! empty($attendee_data['ATT_fname']) |
|
| 1649 | - ? $attendee_data['ATT_fname'] |
|
| 1650 | - : $attendee_data['ATT_email']; |
|
| 1651 | - $attendee_data['ATT_lname'] = ! empty($attendee_data['ATT_lname']) |
|
| 1652 | - ? $attendee_data['ATT_lname'] |
|
| 1653 | - : $attendee_data['ATT_email']; |
|
| 1654 | - return EE_Attendee::new_instance($attendee_data); |
|
| 1655 | - } |
|
| 1656 | - |
|
| 1657 | - |
|
| 1658 | - |
|
| 1659 | - /********************************************************************************************************/ |
|
| 1660 | - /**************************************** PROCESS REG STEP *****************************************/ |
|
| 1661 | - /********************************************************************************************************/ |
|
| 1662 | - /** |
|
| 1663 | - * process_reg_step |
|
| 1664 | - * |
|
| 1665 | - * @return bool |
|
| 1666 | - * @throws EE_Error |
|
| 1667 | - * @throws InvalidArgumentException |
|
| 1668 | - * @throws ReflectionException |
|
| 1669 | - * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 1670 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1671 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1672 | - * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 1673 | - */ |
|
| 1674 | - public function process_reg_step() |
|
| 1675 | - { |
|
| 1676 | - // how have they chosen to pay? |
|
| 1677 | - $this->checkout->selected_method_of_payment = $this->checkout->transaction->is_free() |
|
| 1678 | - ? 'no_payment_required' |
|
| 1679 | - : $this->_get_selected_method_of_payment(true); |
|
| 1680 | - // choose your own adventure based on method_of_payment |
|
| 1681 | - switch ($this->checkout->selected_method_of_payment) { |
|
| 1682 | - case 'events_sold_out': |
|
| 1683 | - $this->checkout->redirect = true; |
|
| 1684 | - $this->checkout->redirect_url = $this->checkout->cancel_page_url; |
|
| 1685 | - $this->checkout->json_response->set_redirect_url($this->checkout->redirect_url); |
|
| 1686 | - // mark this reg step as completed |
|
| 1687 | - $this->set_completed(); |
|
| 1688 | - return false; |
|
| 1689 | - break; |
|
| 1690 | - |
|
| 1691 | - case 'payments_closed': |
|
| 1692 | - if (apply_filters( |
|
| 1693 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options__process_reg_step__payments_closed__display_success', |
|
| 1694 | - false |
|
| 1695 | - )) { |
|
| 1696 | - EE_Error::add_success( |
|
| 1697 | - esc_html__('no payment required at this time.', 'event_espresso'), |
|
| 1698 | - __FILE__, |
|
| 1699 | - __FUNCTION__, |
|
| 1700 | - __LINE__ |
|
| 1701 | - ); |
|
| 1702 | - } |
|
| 1703 | - // mark this reg step as completed |
|
| 1704 | - $this->set_completed(); |
|
| 1705 | - return true; |
|
| 1706 | - break; |
|
| 1707 | - |
|
| 1708 | - case 'no_payment_required': |
|
| 1709 | - if (apply_filters( |
|
| 1710 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options__process_reg_step__no_payment_required__display_success', |
|
| 1711 | - false |
|
| 1712 | - )) { |
|
| 1713 | - EE_Error::add_success( |
|
| 1714 | - esc_html__('no payment required.', 'event_espresso'), |
|
| 1715 | - __FILE__, |
|
| 1716 | - __FUNCTION__, |
|
| 1717 | - __LINE__ |
|
| 1718 | - ); |
|
| 1719 | - } |
|
| 1720 | - // mark this reg step as completed |
|
| 1721 | - $this->set_completed(); |
|
| 1722 | - return true; |
|
| 1723 | - break; |
|
| 1724 | - |
|
| 1725 | - default: |
|
| 1726 | - $registrations = EE_Registry::instance()->SSN->checkout()->transaction->registrations( |
|
| 1727 | - EE_Registry::instance()->SSN->checkout()->reg_cache_where_params |
|
| 1728 | - ); |
|
| 1729 | - $ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space( |
|
| 1730 | - $registrations, |
|
| 1731 | - EE_Registry::instance()->SSN->checkout()->revisit |
|
| 1732 | - ); |
|
| 1733 | - // calculate difference between the two arrays |
|
| 1734 | - $registrations = array_diff($registrations, $ejected_registrations); |
|
| 1735 | - if (empty($registrations)) { |
|
| 1736 | - $this->_redirect_because_event_sold_out(); |
|
| 1737 | - return false; |
|
| 1738 | - } |
|
| 1739 | - $payment_successful = $this->_process_payment(); |
|
| 1740 | - if ($payment_successful) { |
|
| 1741 | - $this->checkout->continue_reg = true; |
|
| 1742 | - $this->_maybe_set_completed($this->checkout->payment_method); |
|
| 1743 | - } else { |
|
| 1744 | - $this->checkout->continue_reg = false; |
|
| 1745 | - } |
|
| 1746 | - return $payment_successful; |
|
| 1747 | - } |
|
| 1748 | - } |
|
| 1749 | - |
|
| 1750 | - |
|
| 1751 | - /** |
|
| 1752 | - * _redirect_because_event_sold_out |
|
| 1753 | - * |
|
| 1754 | - * @access protected |
|
| 1755 | - * @return void |
|
| 1756 | - */ |
|
| 1757 | - protected function _redirect_because_event_sold_out() |
|
| 1758 | - { |
|
| 1759 | - $this->checkout->continue_reg = false; |
|
| 1760 | - // set redirect URL |
|
| 1761 | - $this->checkout->redirect_url = add_query_arg( |
|
| 1762 | - array('e_reg_url_link' => $this->checkout->reg_url_link), |
|
| 1763 | - $this->checkout->current_step->reg_step_url() |
|
| 1764 | - ); |
|
| 1765 | - $this->checkout->json_response->set_redirect_url($this->checkout->redirect_url); |
|
| 1766 | - } |
|
| 1767 | - |
|
| 1768 | - |
|
| 1769 | - /** |
|
| 1770 | - * _maybe_set_completed |
|
| 1771 | - * |
|
| 1772 | - * @access protected |
|
| 1773 | - * @param \EE_Payment_Method $payment_method |
|
| 1774 | - * @return void |
|
| 1775 | - * @throws \EE_Error |
|
| 1776 | - */ |
|
| 1777 | - protected function _maybe_set_completed(EE_Payment_Method $payment_method) |
|
| 1778 | - { |
|
| 1779 | - switch ($payment_method->type_obj()->payment_occurs()) { |
|
| 1780 | - case EE_PMT_Base::offsite: |
|
| 1781 | - break; |
|
| 1782 | - case EE_PMT_Base::onsite: |
|
| 1783 | - case EE_PMT_Base::offline: |
|
| 1784 | - // mark this reg step as completed |
|
| 1785 | - $this->set_completed(); |
|
| 1786 | - break; |
|
| 1787 | - } |
|
| 1788 | - } |
|
| 1789 | - |
|
| 1790 | - |
|
| 1791 | - /** |
|
| 1792 | - * update_reg_step |
|
| 1793 | - * this is the final step after a user revisits the site to retry a payment |
|
| 1794 | - * |
|
| 1795 | - * @return bool |
|
| 1796 | - * @throws EE_Error |
|
| 1797 | - * @throws InvalidArgumentException |
|
| 1798 | - * @throws ReflectionException |
|
| 1799 | - * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 1800 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1801 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1802 | - * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 1803 | - */ |
|
| 1804 | - public function update_reg_step() |
|
| 1805 | - { |
|
| 1806 | - $success = true; |
|
| 1807 | - // if payment required |
|
| 1808 | - if ($this->checkout->transaction->total() > 0) { |
|
| 1809 | - do_action( |
|
| 1810 | - 'AHEE__EE_Single_Page_Checkout__process_finalize_registration__before_gateway', |
|
| 1811 | - $this->checkout->transaction |
|
| 1812 | - ); |
|
| 1813 | - // attempt payment via payment method |
|
| 1814 | - $success = $this->process_reg_step(); |
|
| 1815 | - } |
|
| 1816 | - if ($success && ! $this->checkout->redirect) { |
|
| 1817 | - $this->checkout->cart->get_grand_total()->save_this_and_descendants_to_txn( |
|
| 1818 | - $this->checkout->transaction->ID() |
|
| 1819 | - ); |
|
| 1820 | - // set return URL |
|
| 1821 | - $this->checkout->redirect_url = add_query_arg( |
|
| 1822 | - array('e_reg_url_link' => $this->checkout->reg_url_link), |
|
| 1823 | - $this->checkout->thank_you_page_url |
|
| 1824 | - ); |
|
| 1825 | - } |
|
| 1826 | - return $success; |
|
| 1827 | - } |
|
| 1828 | - |
|
| 1829 | - |
|
| 1830 | - /** |
|
| 1831 | - * _process_payment |
|
| 1832 | - * |
|
| 1833 | - * @access private |
|
| 1834 | - * @return bool |
|
| 1835 | - * @throws EE_Error |
|
| 1836 | - * @throws InvalidArgumentException |
|
| 1837 | - * @throws ReflectionException |
|
| 1838 | - * @throws RuntimeException |
|
| 1839 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1840 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1841 | - */ |
|
| 1842 | - private function _process_payment() |
|
| 1843 | - { |
|
| 1844 | - // basically confirm that the event hasn't sold out since they hit the page |
|
| 1845 | - if (! $this->_last_second_ticket_verifications()) { |
|
| 1846 | - return false; |
|
| 1847 | - } |
|
| 1848 | - // ya gotta make a choice man |
|
| 1849 | - if (empty($this->checkout->selected_method_of_payment)) { |
|
| 1850 | - $this->checkout->json_response->set_plz_select_method_of_payment( |
|
| 1851 | - esc_html__('Please select a method of payment before proceeding.', 'event_espresso') |
|
| 1852 | - ); |
|
| 1853 | - return false; |
|
| 1854 | - } |
|
| 1855 | - // get EE_Payment_Method object |
|
| 1856 | - if (! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) { |
|
| 1857 | - return false; |
|
| 1858 | - } |
|
| 1859 | - // setup billing form |
|
| 1860 | - if ($this->checkout->payment_method->is_on_site()) { |
|
| 1861 | - $this->checkout->billing_form = $this->_get_billing_form_for_payment_method( |
|
| 1862 | - $this->checkout->payment_method |
|
| 1863 | - ); |
|
| 1864 | - // bad billing form ? |
|
| 1865 | - if (! $this->_billing_form_is_valid()) { |
|
| 1866 | - return false; |
|
| 1867 | - } |
|
| 1868 | - } |
|
| 1869 | - // ensure primary registrant has been fully processed |
|
| 1870 | - if (! $this->_setup_primary_registrant_prior_to_payment()) { |
|
| 1871 | - return false; |
|
| 1872 | - } |
|
| 1873 | - // if session is close to expiring (under 10 minutes by default) |
|
| 1874 | - if ((time() - EE_Registry::instance()->SSN->expiration()) < EE_Registry::instance()->SSN->extension()) { |
|
| 1875 | - // add some time to session expiration so that payment can be completed |
|
| 1876 | - EE_Registry::instance()->SSN->extend_expiration(); |
|
| 1877 | - } |
|
| 1878 | - /** @type EE_Transaction_Processor $transaction_processor */ |
|
| 1879 | - // $transaction_processor = EE_Registry::instance()->load_class( 'Transaction_Processor' ); |
|
| 1880 | - // in case a registrant leaves to an Off-Site Gateway and never returns, we want to approve any registrations |
|
| 1881 | - // for events with a default reg status of Approved |
|
| 1882 | - // $transaction_processor->toggle_registration_statuses_for_default_approved_events( |
|
| 1883 | - // $this->checkout->transaction, $this->checkout->reg_cache_where_params |
|
| 1884 | - // ); |
|
| 1885 | - // attempt payment |
|
| 1886 | - $payment = $this->_attempt_payment($this->checkout->payment_method); |
|
| 1887 | - // process results |
|
| 1888 | - $payment = $this->_validate_payment($payment); |
|
| 1889 | - $payment = $this->_post_payment_processing($payment); |
|
| 1890 | - // verify payment |
|
| 1891 | - if ($payment instanceof EE_Payment) { |
|
| 1892 | - // store that for later |
|
| 1893 | - $this->checkout->payment = $payment; |
|
| 1894 | - // we can also consider the TXN to not have been failed, so temporarily upgrade it's status to abandoned |
|
| 1895 | - $this->checkout->transaction->toggle_failed_transaction_status(); |
|
| 1896 | - $payment_status = $payment->status(); |
|
| 1897 | - if ($payment_status === EEM_Payment::status_id_approved |
|
| 1898 | - || $payment_status === EEM_Payment::status_id_pending |
|
| 1899 | - ) { |
|
| 1900 | - return true; |
|
| 1901 | - } else { |
|
| 1902 | - return false; |
|
| 1903 | - } |
|
| 1904 | - } elseif ($payment === true) { |
|
| 1905 | - // please note that offline payment methods will NOT make a payment, |
|
| 1906 | - // but instead just mark themselves as the PMD_ID on the transaction, and return true |
|
| 1907 | - $this->checkout->payment = $payment; |
|
| 1908 | - return true; |
|
| 1909 | - } |
|
| 1910 | - // where's my money? |
|
| 1911 | - return false; |
|
| 1912 | - } |
|
| 1913 | - |
|
| 1914 | - |
|
| 1915 | - /** |
|
| 1916 | - * _last_second_ticket_verifications |
|
| 1917 | - * |
|
| 1918 | - * @access public |
|
| 1919 | - * @return bool |
|
| 1920 | - * @throws EE_Error |
|
| 1921 | - */ |
|
| 1922 | - protected function _last_second_ticket_verifications() |
|
| 1923 | - { |
|
| 1924 | - // don't bother re-validating if not a return visit |
|
| 1925 | - if (! $this->checkout->revisit) { |
|
| 1926 | - return true; |
|
| 1927 | - } |
|
| 1928 | - $registrations = $this->checkout->transaction->registrations(); |
|
| 1929 | - if (empty($registrations)) { |
|
| 1930 | - return false; |
|
| 1931 | - } |
|
| 1932 | - foreach ($registrations as $registration) { |
|
| 1933 | - if ($registration instanceof EE_Registration && ! $registration->is_approved()) { |
|
| 1934 | - $event = $registration->event_obj(); |
|
| 1935 | - if ($event instanceof EE_Event && $event->is_sold_out(true)) { |
|
| 1936 | - EE_Error::add_error( |
|
| 1937 | - apply_filters( |
|
| 1938 | - 'FHEE__EE_SPCO_Reg_Step_Payment_Options___last_second_ticket_verifications__sold_out_events_msg', |
|
| 1939 | - sprintf( |
|
| 1940 | - esc_html__( |
|
| 1941 | - 'It appears that the %1$s event that you were about to make a payment for has sold out since you first registered and/or arrived at this page. Please refresh the page and try again. If you have already made a partial payment towards this event, please contact the event administrator for a refund.', |
|
| 1942 | - 'event_espresso' |
|
| 1943 | - ), |
|
| 1944 | - $event->name() |
|
| 1945 | - ) |
|
| 1946 | - ), |
|
| 1947 | - __FILE__, |
|
| 1948 | - __FUNCTION__, |
|
| 1949 | - __LINE__ |
|
| 1950 | - ); |
|
| 1951 | - return false; |
|
| 1952 | - } |
|
| 1953 | - } |
|
| 1954 | - } |
|
| 1955 | - return true; |
|
| 1956 | - } |
|
| 1957 | - |
|
| 1958 | - |
|
| 1959 | - /** |
|
| 1960 | - * redirect_form |
|
| 1961 | - * |
|
| 1962 | - * @access public |
|
| 1963 | - * @return bool |
|
| 1964 | - * @throws EE_Error |
|
| 1965 | - * @throws InvalidArgumentException |
|
| 1966 | - * @throws ReflectionException |
|
| 1967 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1968 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1969 | - */ |
|
| 1970 | - public function redirect_form() |
|
| 1971 | - { |
|
| 1972 | - $payment_method_billing_info = $this->_payment_method_billing_info( |
|
| 1973 | - $this->_get_payment_method_for_selected_method_of_payment() |
|
| 1974 | - ); |
|
| 1975 | - $html = $payment_method_billing_info->get_html(); |
|
| 1976 | - $html .= $this->checkout->redirect_form; |
|
| 1977 | - EE_Registry::instance()->REQ->add_output($html); |
|
| 1978 | - return true; |
|
| 1979 | - } |
|
| 1980 | - |
|
| 1981 | - |
|
| 1982 | - /** |
|
| 1983 | - * _billing_form_is_valid |
|
| 1984 | - * |
|
| 1985 | - * @access private |
|
| 1986 | - * @return bool |
|
| 1987 | - * @throws \EE_Error |
|
| 1988 | - */ |
|
| 1989 | - private function _billing_form_is_valid() |
|
| 1990 | - { |
|
| 1991 | - if (! $this->checkout->payment_method->type_obj()->has_billing_form()) { |
|
| 1992 | - return true; |
|
| 1993 | - } |
|
| 1994 | - if ($this->checkout->billing_form instanceof EE_Billing_Info_Form) { |
|
| 1995 | - if ($this->checkout->billing_form->was_submitted()) { |
|
| 1996 | - $this->checkout->billing_form->receive_form_submission(); |
|
| 1997 | - if ($this->checkout->billing_form->is_valid()) { |
|
| 1998 | - return true; |
|
| 1999 | - } |
|
| 2000 | - $validation_errors = $this->checkout->billing_form->get_validation_errors_accumulated(); |
|
| 2001 | - $error_strings = array(); |
|
| 2002 | - foreach ($validation_errors as $validation_error) { |
|
| 2003 | - if ($validation_error instanceof EE_Validation_Error) { |
|
| 2004 | - $form_section = $validation_error->get_form_section(); |
|
| 2005 | - if ($form_section instanceof EE_Form_Input_Base) { |
|
| 2006 | - $label = $form_section->html_label_text(); |
|
| 2007 | - } elseif ($form_section instanceof EE_Form_Section_Base) { |
|
| 2008 | - $label = $form_section->name(); |
|
| 2009 | - } else { |
|
| 2010 | - $label = esc_html__('Validation Error', 'event_espresso'); |
|
| 2011 | - } |
|
| 2012 | - $error_strings[] = sprintf('%1$s: %2$s', $label, $validation_error->getMessage()); |
|
| 2013 | - } |
|
| 2014 | - } |
|
| 2015 | - EE_Error::add_error( |
|
| 2016 | - sprintf( |
|
| 2017 | - esc_html__( |
|
| 2018 | - 'One or more billing form inputs are invalid and require correction before proceeding. %1$s %2$s', |
|
| 2019 | - 'event_espresso' |
|
| 2020 | - ), |
|
| 2021 | - '<br/>', |
|
| 2022 | - implode('<br/>', $error_strings) |
|
| 2023 | - ), |
|
| 2024 | - __FILE__, |
|
| 2025 | - __FUNCTION__, |
|
| 2026 | - __LINE__ |
|
| 2027 | - ); |
|
| 2028 | - } else { |
|
| 2029 | - EE_Error::add_error( |
|
| 2030 | - esc_html__( |
|
| 2031 | - 'The billing form was not submitted or something prevented it\'s submission.', |
|
| 2032 | - 'event_espresso' |
|
| 2033 | - ), |
|
| 2034 | - __FILE__, |
|
| 2035 | - __FUNCTION__, |
|
| 2036 | - __LINE__ |
|
| 2037 | - ); |
|
| 2038 | - } |
|
| 2039 | - } else { |
|
| 2040 | - EE_Error::add_error( |
|
| 2041 | - esc_html__( |
|
| 2042 | - 'The submitted billing form is invalid possibly due to a technical reason.', |
|
| 2043 | - 'event_espresso' |
|
| 2044 | - ), |
|
| 2045 | - __FILE__, |
|
| 2046 | - __FUNCTION__, |
|
| 2047 | - __LINE__ |
|
| 2048 | - ); |
|
| 2049 | - } |
|
| 2050 | - return false; |
|
| 2051 | - } |
|
| 2052 | - |
|
| 2053 | - |
|
| 2054 | - /** |
|
| 2055 | - * _setup_primary_registrant_prior_to_payment |
|
| 2056 | - * ensures that the primary registrant has a valid attendee object created with the critical details populated |
|
| 2057 | - * (first & last name & email) and that both the transaction object and primary registration object have been saved |
|
| 2058 | - * plz note that any other registrations will NOT be saved at this point (because they may not have any details |
|
| 2059 | - * yet) |
|
| 2060 | - * |
|
| 2061 | - * @access private |
|
| 2062 | - * @return bool |
|
| 2063 | - * @throws EE_Error |
|
| 2064 | - * @throws InvalidArgumentException |
|
| 2065 | - * @throws ReflectionException |
|
| 2066 | - * @throws RuntimeException |
|
| 2067 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2068 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2069 | - */ |
|
| 2070 | - private function _setup_primary_registrant_prior_to_payment() |
|
| 2071 | - { |
|
| 2072 | - // check if transaction has a primary registrant and that it has a related Attendee object |
|
| 2073 | - // if not, then we need to at least gather some primary registrant data before attempting payment |
|
| 2074 | - if ($this->checkout->billing_form instanceof EE_Billing_Attendee_Info_Form |
|
| 2075 | - && ! $this->checkout->transaction_has_primary_registrant() |
|
| 2076 | - && ! $this->_capture_primary_registration_data_from_billing_form() |
|
| 2077 | - ) { |
|
| 2078 | - return false; |
|
| 2079 | - } |
|
| 2080 | - // because saving an object clears it's cache, we need to do the chevy shuffle |
|
| 2081 | - // grab the primary_registration object |
|
| 2082 | - $primary_registration = $this->checkout->transaction->primary_registration(); |
|
| 2083 | - // at this point we'll consider a TXN to not have been failed |
|
| 2084 | - $this->checkout->transaction->toggle_failed_transaction_status(); |
|
| 2085 | - // save the TXN ( which clears cached copy of primary_registration) |
|
| 2086 | - $this->checkout->transaction->save(); |
|
| 2087 | - // grab TXN ID and save it to the primary_registration |
|
| 2088 | - $primary_registration->set_transaction_id($this->checkout->transaction->ID()); |
|
| 2089 | - // save what we have so far |
|
| 2090 | - $primary_registration->save(); |
|
| 2091 | - return true; |
|
| 2092 | - } |
|
| 2093 | - |
|
| 2094 | - |
|
| 2095 | - /** |
|
| 2096 | - * _capture_primary_registration_data_from_billing_form |
|
| 2097 | - * |
|
| 2098 | - * @access private |
|
| 2099 | - * @return bool |
|
| 2100 | - * @throws EE_Error |
|
| 2101 | - * @throws InvalidArgumentException |
|
| 2102 | - * @throws ReflectionException |
|
| 2103 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2104 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2105 | - */ |
|
| 2106 | - private function _capture_primary_registration_data_from_billing_form() |
|
| 2107 | - { |
|
| 2108 | - // convert billing form data into an attendee |
|
| 2109 | - $this->checkout->primary_attendee_obj = $this->checkout->billing_form->create_attendee_from_billing_form_data(); |
|
| 2110 | - if (! $this->checkout->primary_attendee_obj instanceof EE_Attendee) { |
|
| 2111 | - EE_Error::add_error( |
|
| 2112 | - sprintf( |
|
| 2113 | - esc_html__( |
|
| 2114 | - 'The billing form details could not be used for attendee details due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2115 | - 'event_espresso' |
|
| 2116 | - ), |
|
| 2117 | - '<br/>', |
|
| 2118 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2119 | - ), |
|
| 2120 | - __FILE__, |
|
| 2121 | - __FUNCTION__, |
|
| 2122 | - __LINE__ |
|
| 2123 | - ); |
|
| 2124 | - return false; |
|
| 2125 | - } |
|
| 2126 | - $primary_registration = $this->checkout->transaction->primary_registration(); |
|
| 2127 | - if (! $primary_registration instanceof EE_Registration) { |
|
| 2128 | - EE_Error::add_error( |
|
| 2129 | - sprintf( |
|
| 2130 | - esc_html__( |
|
| 2131 | - 'The primary registrant for this transaction could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2132 | - 'event_espresso' |
|
| 2133 | - ), |
|
| 2134 | - '<br/>', |
|
| 2135 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2136 | - ), |
|
| 2137 | - __FILE__, |
|
| 2138 | - __FUNCTION__, |
|
| 2139 | - __LINE__ |
|
| 2140 | - ); |
|
| 2141 | - return false; |
|
| 2142 | - } |
|
| 2143 | - if (! $primary_registration->_add_relation_to($this->checkout->primary_attendee_obj, 'Attendee') |
|
| 2144 | - instanceof |
|
| 2145 | - EE_Attendee |
|
| 2146 | - ) { |
|
| 2147 | - EE_Error::add_error( |
|
| 2148 | - sprintf( |
|
| 2149 | - esc_html__( |
|
| 2150 | - 'The primary registrant could not be associated with this transaction due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2151 | - 'event_espresso' |
|
| 2152 | - ), |
|
| 2153 | - '<br/>', |
|
| 2154 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2155 | - ), |
|
| 2156 | - __FILE__, |
|
| 2157 | - __FUNCTION__, |
|
| 2158 | - __LINE__ |
|
| 2159 | - ); |
|
| 2160 | - return false; |
|
| 2161 | - } |
|
| 2162 | - /** @type EE_Registration_Processor $registration_processor */ |
|
| 2163 | - $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
| 2164 | - // at this point, we should have enough details about the registrant to consider the registration NOT incomplete |
|
| 2165 | - $registration_processor->toggle_incomplete_registration_status_to_default($primary_registration); |
|
| 2166 | - return true; |
|
| 2167 | - } |
|
| 2168 | - |
|
| 2169 | - |
|
| 2170 | - /** |
|
| 2171 | - * _get_payment_method_for_selected_method_of_payment |
|
| 2172 | - * retrieves a valid payment method |
|
| 2173 | - * |
|
| 2174 | - * @access public |
|
| 2175 | - * @return EE_Payment_Method |
|
| 2176 | - * @throws EE_Error |
|
| 2177 | - * @throws InvalidArgumentException |
|
| 2178 | - * @throws ReflectionException |
|
| 2179 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2180 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2181 | - */ |
|
| 2182 | - private function _get_payment_method_for_selected_method_of_payment() |
|
| 2183 | - { |
|
| 2184 | - if ($this->checkout->selected_method_of_payment === 'events_sold_out') { |
|
| 2185 | - $this->_redirect_because_event_sold_out(); |
|
| 2186 | - return null; |
|
| 2187 | - } |
|
| 2188 | - // get EE_Payment_Method object |
|
| 2189 | - if (isset($this->checkout->available_payment_methods[ $this->checkout->selected_method_of_payment ])) { |
|
| 2190 | - $payment_method = $this->checkout->available_payment_methods[ $this->checkout->selected_method_of_payment ]; |
|
| 2191 | - } else { |
|
| 2192 | - // load EEM_Payment_Method |
|
| 2193 | - EE_Registry::instance()->load_model('Payment_Method'); |
|
| 2194 | - /** @type EEM_Payment_Method $EEM_Payment_Method */ |
|
| 2195 | - $EEM_Payment_Method = EE_Registry::instance()->LIB->EEM_Payment_Method; |
|
| 2196 | - $payment_method = $EEM_Payment_Method->get_one_by_slug($this->checkout->selected_method_of_payment); |
|
| 2197 | - } |
|
| 2198 | - // verify $payment_method |
|
| 2199 | - if (! $payment_method instanceof EE_Payment_Method) { |
|
| 2200 | - // not a payment |
|
| 2201 | - EE_Error::add_error( |
|
| 2202 | - sprintf( |
|
| 2203 | - esc_html__( |
|
| 2204 | - 'The selected method of payment could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2205 | - 'event_espresso' |
|
| 2206 | - ), |
|
| 2207 | - '<br/>', |
|
| 2208 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2209 | - ), |
|
| 2210 | - __FILE__, |
|
| 2211 | - __FUNCTION__, |
|
| 2212 | - __LINE__ |
|
| 2213 | - ); |
|
| 2214 | - return null; |
|
| 2215 | - } |
|
| 2216 | - // and verify it has a valid Payment_Method Type object |
|
| 2217 | - if (! $payment_method->type_obj() instanceof EE_PMT_Base) { |
|
| 2218 | - // not a payment |
|
| 2219 | - EE_Error::add_error( |
|
| 2220 | - sprintf( |
|
| 2221 | - esc_html__( |
|
| 2222 | - 'A valid payment method could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2223 | - 'event_espresso' |
|
| 2224 | - ), |
|
| 2225 | - '<br/>', |
|
| 2226 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2227 | - ), |
|
| 2228 | - __FILE__, |
|
| 2229 | - __FUNCTION__, |
|
| 2230 | - __LINE__ |
|
| 2231 | - ); |
|
| 2232 | - return null; |
|
| 2233 | - } |
|
| 2234 | - return $payment_method; |
|
| 2235 | - } |
|
| 2236 | - |
|
| 2237 | - |
|
| 2238 | - /** |
|
| 2239 | - * _attempt_payment |
|
| 2240 | - * |
|
| 2241 | - * @access private |
|
| 2242 | - * @type EE_Payment_Method $payment_method |
|
| 2243 | - * @return mixed EE_Payment | boolean |
|
| 2244 | - * @throws EE_Error |
|
| 2245 | - * @throws InvalidArgumentException |
|
| 2246 | - * @throws ReflectionException |
|
| 2247 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2248 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2249 | - */ |
|
| 2250 | - private function _attempt_payment(EE_Payment_Method $payment_method) |
|
| 2251 | - { |
|
| 2252 | - $payment = null; |
|
| 2253 | - $this->checkout->transaction->save(); |
|
| 2254 | - $payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
|
| 2255 | - if (! $payment_processor instanceof EE_Payment_Processor) { |
|
| 2256 | - return false; |
|
| 2257 | - } |
|
| 2258 | - try { |
|
| 2259 | - $payment_processor->set_revisit($this->checkout->revisit); |
|
| 2260 | - // generate payment object |
|
| 2261 | - $payment = $payment_processor->process_payment( |
|
| 2262 | - $payment_method, |
|
| 2263 | - $this->checkout->transaction, |
|
| 2264 | - $this->checkout->amount_owing, |
|
| 2265 | - $this->checkout->billing_form, |
|
| 2266 | - $this->_get_return_url($payment_method), |
|
| 2267 | - 'CART', |
|
| 2268 | - $this->checkout->admin_request, |
|
| 2269 | - true, |
|
| 2270 | - $this->reg_step_url() |
|
| 2271 | - ); |
|
| 2272 | - } catch (Exception $e) { |
|
| 2273 | - $this->_handle_payment_processor_exception($e); |
|
| 2274 | - } |
|
| 2275 | - return $payment; |
|
| 2276 | - } |
|
| 2277 | - |
|
| 2278 | - |
|
| 2279 | - /** |
|
| 2280 | - * _handle_payment_processor_exception |
|
| 2281 | - * |
|
| 2282 | - * @access protected |
|
| 2283 | - * @param \Exception $e |
|
| 2284 | - * @return void |
|
| 2285 | - * @throws EE_Error |
|
| 2286 | - * @throws InvalidArgumentException |
|
| 2287 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2288 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2289 | - */ |
|
| 2290 | - protected function _handle_payment_processor_exception(Exception $e) |
|
| 2291 | - { |
|
| 2292 | - EE_Error::add_error( |
|
| 2293 | - sprintf( |
|
| 2294 | - esc_html__( |
|
| 2295 | - 'The payment could not br processed due to a technical issue.%1$sPlease try again or contact %2$s for assistance.||The following Exception was thrown in %4$s on line %5$s:%1$s%3$s', |
|
| 2296 | - 'event_espresso' |
|
| 2297 | - ), |
|
| 2298 | - '<br/>', |
|
| 2299 | - EE_Registry::instance()->CFG->organization->get_pretty('email'), |
|
| 2300 | - $e->getMessage(), |
|
| 2301 | - $e->getFile(), |
|
| 2302 | - $e->getLine() |
|
| 2303 | - ), |
|
| 2304 | - __FILE__, |
|
| 2305 | - __FUNCTION__, |
|
| 2306 | - __LINE__ |
|
| 2307 | - ); |
|
| 2308 | - } |
|
| 2309 | - |
|
| 2310 | - |
|
| 2311 | - /** |
|
| 2312 | - * _get_return_url |
|
| 2313 | - * |
|
| 2314 | - * @access protected |
|
| 2315 | - * @param \EE_Payment_Method $payment_method |
|
| 2316 | - * @return string |
|
| 2317 | - * @throws \EE_Error |
|
| 2318 | - */ |
|
| 2319 | - protected function _get_return_url(EE_Payment_Method $payment_method) |
|
| 2320 | - { |
|
| 2321 | - $return_url = ''; |
|
| 2322 | - switch ($payment_method->type_obj()->payment_occurs()) { |
|
| 2323 | - case EE_PMT_Base::offsite: |
|
| 2324 | - $return_url = add_query_arg( |
|
| 2325 | - array( |
|
| 2326 | - 'action' => 'process_gateway_response', |
|
| 2327 | - 'selected_method_of_payment' => $this->checkout->selected_method_of_payment, |
|
| 2328 | - 'spco_txn' => $this->checkout->transaction->ID(), |
|
| 2329 | - ), |
|
| 2330 | - $this->reg_step_url() |
|
| 2331 | - ); |
|
| 2332 | - break; |
|
| 2333 | - case EE_PMT_Base::onsite: |
|
| 2334 | - case EE_PMT_Base::offline: |
|
| 2335 | - $return_url = $this->checkout->next_step->reg_step_url(); |
|
| 2336 | - break; |
|
| 2337 | - } |
|
| 2338 | - return $return_url; |
|
| 2339 | - } |
|
| 2340 | - |
|
| 2341 | - |
|
| 2342 | - /** |
|
| 2343 | - * _validate_payment |
|
| 2344 | - * |
|
| 2345 | - * @access private |
|
| 2346 | - * @param EE_Payment $payment |
|
| 2347 | - * @return EE_Payment|FALSE |
|
| 2348 | - * @throws EE_Error |
|
| 2349 | - * @throws InvalidArgumentException |
|
| 2350 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2351 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2352 | - */ |
|
| 2353 | - private function _validate_payment($payment = null) |
|
| 2354 | - { |
|
| 2355 | - if ($this->checkout->payment_method->is_off_line()) { |
|
| 2356 | - return true; |
|
| 2357 | - } |
|
| 2358 | - // verify payment object |
|
| 2359 | - if (! $payment instanceof EE_Payment) { |
|
| 2360 | - // not a payment |
|
| 2361 | - EE_Error::add_error( |
|
| 2362 | - sprintf( |
|
| 2363 | - esc_html__( |
|
| 2364 | - 'A valid payment was not generated due to a technical issue.%1$sPlease try again or contact %2$s for assistance.', |
|
| 2365 | - 'event_espresso' |
|
| 2366 | - ), |
|
| 2367 | - '<br/>', |
|
| 2368 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2369 | - ), |
|
| 2370 | - __FILE__, |
|
| 2371 | - __FUNCTION__, |
|
| 2372 | - __LINE__ |
|
| 2373 | - ); |
|
| 2374 | - return false; |
|
| 2375 | - } |
|
| 2376 | - return $payment; |
|
| 2377 | - } |
|
| 2378 | - |
|
| 2379 | - |
|
| 2380 | - /** |
|
| 2381 | - * _post_payment_processing |
|
| 2382 | - * |
|
| 2383 | - * @access private |
|
| 2384 | - * @param EE_Payment|bool $payment |
|
| 2385 | - * @return bool |
|
| 2386 | - * @throws EE_Error |
|
| 2387 | - * @throws InvalidArgumentException |
|
| 2388 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2389 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2390 | - */ |
|
| 2391 | - private function _post_payment_processing($payment = null) |
|
| 2392 | - { |
|
| 2393 | - // Off-Line payment? |
|
| 2394 | - if ($payment === true) { |
|
| 2395 | - // $this->_setup_redirect_for_next_step(); |
|
| 2396 | - return true; |
|
| 2397 | - // On-Site payment? |
|
| 2398 | - } elseif ($this->checkout->payment_method->is_on_site()) { |
|
| 2399 | - if (! $this->_process_payment_status($payment, EE_PMT_Base::onsite)) { |
|
| 2400 | - // $this->_setup_redirect_for_next_step(); |
|
| 2401 | - $this->checkout->continue_reg = false; |
|
| 2402 | - } |
|
| 2403 | - // Off-Site payment? |
|
| 2404 | - } elseif ($this->checkout->payment_method->is_off_site()) { |
|
| 2405 | - // if a payment object was made and it specifies a redirect url, then we'll setup that redirect info |
|
| 2406 | - if ($payment instanceof EE_Payment && $payment->redirect_url()) { |
|
| 2407 | - do_action('AHEE_log', __CLASS__, __FUNCTION__, $payment->redirect_url(), '$payment->redirect_url()'); |
|
| 2408 | - $this->checkout->redirect = true; |
|
| 2409 | - $this->checkout->redirect_form = $payment->redirect_form(); |
|
| 2410 | - $this->checkout->redirect_url = $this->reg_step_url('redirect_form'); |
|
| 2411 | - // set JSON response |
|
| 2412 | - $this->checkout->json_response->set_redirect_form($this->checkout->redirect_form); |
|
| 2413 | - // and lastly, let's bump the payment status to pending |
|
| 2414 | - $payment->set_status(EEM_Payment::status_id_pending); |
|
| 2415 | - $payment->save(); |
|
| 2416 | - } else { |
|
| 2417 | - // not a payment |
|
| 2418 | - $this->checkout->continue_reg = false; |
|
| 2419 | - EE_Error::add_error( |
|
| 2420 | - sprintf( |
|
| 2421 | - esc_html__( |
|
| 2422 | - 'It appears the Off Site Payment Method was not configured properly.%sPlease try again or contact %s for assistance.', |
|
| 2423 | - 'event_espresso' |
|
| 2424 | - ), |
|
| 2425 | - '<br/>', |
|
| 2426 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2427 | - ), |
|
| 2428 | - __FILE__, |
|
| 2429 | - __FUNCTION__, |
|
| 2430 | - __LINE__ |
|
| 2431 | - ); |
|
| 2432 | - } |
|
| 2433 | - } else { |
|
| 2434 | - // ummm ya... not Off-Line, not On-Site, not off-Site ???? |
|
| 2435 | - $this->checkout->continue_reg = false; |
|
| 2436 | - return false; |
|
| 2437 | - } |
|
| 2438 | - return $payment; |
|
| 2439 | - } |
|
| 2440 | - |
|
| 2441 | - |
|
| 2442 | - /** |
|
| 2443 | - * _process_payment_status |
|
| 2444 | - * |
|
| 2445 | - * @access private |
|
| 2446 | - * @type EE_Payment $payment |
|
| 2447 | - * @param string $payment_occurs |
|
| 2448 | - * @return bool |
|
| 2449 | - * @throws EE_Error |
|
| 2450 | - * @throws InvalidArgumentException |
|
| 2451 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2452 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2453 | - */ |
|
| 2454 | - private function _process_payment_status($payment, $payment_occurs = EE_PMT_Base::offline) |
|
| 2455 | - { |
|
| 2456 | - // off-line payment? carry on |
|
| 2457 | - if ($payment_occurs === EE_PMT_Base::offline) { |
|
| 2458 | - return true; |
|
| 2459 | - } |
|
| 2460 | - // verify payment validity |
|
| 2461 | - if ($payment instanceof EE_Payment) { |
|
| 2462 | - do_action('AHEE_log', __CLASS__, __FUNCTION__, $payment->status(), '$payment->status()'); |
|
| 2463 | - $msg = $payment->gateway_response(); |
|
| 2464 | - // check results |
|
| 2465 | - switch ($payment->status()) { |
|
| 2466 | - // good payment |
|
| 2467 | - case EEM_Payment::status_id_approved: |
|
| 2468 | - EE_Error::add_success( |
|
| 2469 | - esc_html__('Your payment was processed successfully.', 'event_espresso'), |
|
| 2470 | - __FILE__, |
|
| 2471 | - __FUNCTION__, |
|
| 2472 | - __LINE__ |
|
| 2473 | - ); |
|
| 2474 | - return true; |
|
| 2475 | - break; |
|
| 2476 | - // slow payment |
|
| 2477 | - case EEM_Payment::status_id_pending: |
|
| 2478 | - if (empty($msg)) { |
|
| 2479 | - $msg = esc_html__( |
|
| 2480 | - 'Your payment appears to have been processed successfully, but the Instant Payment Notification has not yet been received. It should arrive shortly.', |
|
| 2481 | - 'event_espresso' |
|
| 2482 | - ); |
|
| 2483 | - } |
|
| 2484 | - EE_Error::add_success($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 2485 | - return true; |
|
| 2486 | - break; |
|
| 2487 | - // don't wanna payment |
|
| 2488 | - case EEM_Payment::status_id_cancelled: |
|
| 2489 | - if (empty($msg)) { |
|
| 2490 | - $msg = _n( |
|
| 2491 | - 'Payment cancelled. Please try again.', |
|
| 2492 | - 'Payment cancelled. Please try again or select another method of payment.', |
|
| 2493 | - count($this->checkout->available_payment_methods), |
|
| 2494 | - 'event_espresso' |
|
| 2495 | - ); |
|
| 2496 | - } |
|
| 2497 | - EE_Error::add_attention($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 2498 | - return false; |
|
| 2499 | - break; |
|
| 2500 | - // not enough payment |
|
| 2501 | - case EEM_Payment::status_id_declined: |
|
| 2502 | - if (empty($msg)) { |
|
| 2503 | - $msg = _n( |
|
| 2504 | - 'We\'re sorry but your payment was declined. Please try again.', |
|
| 2505 | - 'We\'re sorry but your payment was declined. Please try again or select another method of payment.', |
|
| 2506 | - count($this->checkout->available_payment_methods), |
|
| 2507 | - 'event_espresso' |
|
| 2508 | - ); |
|
| 2509 | - } |
|
| 2510 | - EE_Error::add_attention($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 2511 | - return false; |
|
| 2512 | - break; |
|
| 2513 | - // bad payment |
|
| 2514 | - case EEM_Payment::status_id_failed: |
|
| 2515 | - if (! empty($msg)) { |
|
| 2516 | - EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 2517 | - return false; |
|
| 2518 | - } |
|
| 2519 | - // default to error below |
|
| 2520 | - break; |
|
| 2521 | - } |
|
| 2522 | - } |
|
| 2523 | - // off-site payment gateway responses are too unreliable, so let's just assume that |
|
| 2524 | - // the payment processing is just running slower than the registrant's request |
|
| 2525 | - if ($payment_occurs === EE_PMT_Base::offsite) { |
|
| 2526 | - return true; |
|
| 2527 | - } |
|
| 2528 | - EE_Error::add_error( |
|
| 2529 | - sprintf( |
|
| 2530 | - esc_html__( |
|
| 2531 | - 'Your payment could not be processed successfully due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2532 | - 'event_espresso' |
|
| 2533 | - ), |
|
| 2534 | - '<br/>', |
|
| 2535 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2536 | - ), |
|
| 2537 | - __FILE__, |
|
| 2538 | - __FUNCTION__, |
|
| 2539 | - __LINE__ |
|
| 2540 | - ); |
|
| 2541 | - return false; |
|
| 2542 | - } |
|
| 2543 | - |
|
| 2544 | - |
|
| 2545 | - |
|
| 2546 | - |
|
| 2547 | - |
|
| 2548 | - |
|
| 2549 | - /********************************************************************************************************/ |
|
| 2550 | - /********************************** PROCESS GATEWAY RESPONSE **********************************/ |
|
| 2551 | - /********************************************************************************************************/ |
|
| 2552 | - /** |
|
| 2553 | - * process_gateway_response |
|
| 2554 | - * this is the return point for Off-Site Payment Methods |
|
| 2555 | - * It will attempt to "handle the IPN" if it appears that this has not already occurred, |
|
| 2556 | - * otherwise, it will load up the last payment made for the TXN. |
|
| 2557 | - * If the payment retrieved looks good, it will then either: |
|
| 2558 | - * complete the current step and allow advancement to the next reg step |
|
| 2559 | - * or present the payment options again |
|
| 2560 | - * |
|
| 2561 | - * @access private |
|
| 2562 | - * @return EE_Payment|FALSE |
|
| 2563 | - * @throws EE_Error |
|
| 2564 | - * @throws InvalidArgumentException |
|
| 2565 | - * @throws ReflectionException |
|
| 2566 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2567 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2568 | - * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
|
| 2569 | - */ |
|
| 2570 | - public function process_gateway_response() |
|
| 2571 | - { |
|
| 2572 | - $payment = null; |
|
| 2573 | - // how have they chosen to pay? |
|
| 2574 | - $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true); |
|
| 2575 | - // get EE_Payment_Method object |
|
| 2576 | - if (! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) { |
|
| 2577 | - $this->checkout->continue_reg = false; |
|
| 2578 | - return false; |
|
| 2579 | - } |
|
| 2580 | - if (! $this->checkout->payment_method->is_off_site()) { |
|
| 2581 | - return false; |
|
| 2582 | - } |
|
| 2583 | - $this->_validate_offsite_return(); |
|
| 2584 | - // DEBUG LOG |
|
| 2585 | - // $this->checkout->log( |
|
| 2586 | - // __CLASS__, |
|
| 2587 | - // __FUNCTION__, |
|
| 2588 | - // __LINE__, |
|
| 2589 | - // array( |
|
| 2590 | - // 'selected_method_of_payment' => $this->checkout->selected_method_of_payment, |
|
| 2591 | - // 'payment_method' => $this->checkout->payment_method, |
|
| 2592 | - // ), |
|
| 2593 | - // true |
|
| 2594 | - // ); |
|
| 2595 | - // verify TXN |
|
| 2596 | - if ($this->checkout->transaction instanceof EE_Transaction) { |
|
| 2597 | - $gateway = $this->checkout->payment_method->type_obj()->get_gateway(); |
|
| 2598 | - if (! $gateway instanceof EE_Offsite_Gateway) { |
|
| 2599 | - $this->checkout->continue_reg = false; |
|
| 2600 | - return false; |
|
| 2601 | - } |
|
| 2602 | - $payment = $this->_process_off_site_payment($gateway); |
|
| 2603 | - $payment = $this->_process_cancelled_payments($payment); |
|
| 2604 | - $payment = $this->_validate_payment($payment); |
|
| 2605 | - // if payment was not declined by the payment gateway or cancelled by the registrant |
|
| 2606 | - if ($this->_process_payment_status($payment, EE_PMT_Base::offsite)) { |
|
| 2607 | - // $this->_setup_redirect_for_next_step(); |
|
| 2608 | - // store that for later |
|
| 2609 | - $this->checkout->payment = $payment; |
|
| 2610 | - // mark this reg step as completed, as long as gateway doesn't use a separate IPN request, |
|
| 2611 | - // because we will complete this step during the IPN processing then |
|
| 2612 | - if ($gateway instanceof EE_Offsite_Gateway && ! $this->handle_IPN_in_this_request()) { |
|
| 2613 | - $this->set_completed(); |
|
| 2614 | - } |
|
| 2615 | - return true; |
|
| 2616 | - } |
|
| 2617 | - } |
|
| 2618 | - // DEBUG LOG |
|
| 2619 | - // $this->checkout->log( |
|
| 2620 | - // __CLASS__, |
|
| 2621 | - // __FUNCTION__, |
|
| 2622 | - // __LINE__, |
|
| 2623 | - // array('payment' => $payment) |
|
| 2624 | - // ); |
|
| 2625 | - $this->checkout->continue_reg = false; |
|
| 2626 | - return false; |
|
| 2627 | - } |
|
| 2628 | - |
|
| 2629 | - |
|
| 2630 | - /** |
|
| 2631 | - * _validate_return |
|
| 2632 | - * |
|
| 2633 | - * @access private |
|
| 2634 | - * @return void |
|
| 2635 | - * @throws EE_Error |
|
| 2636 | - * @throws InvalidArgumentException |
|
| 2637 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2638 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2639 | - * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
|
| 2640 | - */ |
|
| 2641 | - private function _validate_offsite_return() |
|
| 2642 | - { |
|
| 2643 | - $TXN_ID = (int) EE_Registry::instance()->REQ->get('spco_txn', 0); |
|
| 2644 | - if ($TXN_ID !== $this->checkout->transaction->ID()) { |
|
| 2645 | - // Houston... we might have a problem |
|
| 2646 | - $invalid_TXN = false; |
|
| 2647 | - // first gather some info |
|
| 2648 | - $valid_TXN = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
|
| 2649 | - $primary_registrant = $valid_TXN instanceof EE_Transaction |
|
| 2650 | - ? $valid_TXN->primary_registration() |
|
| 2651 | - : null; |
|
| 2652 | - // let's start by retrieving the cart for this TXN |
|
| 2653 | - $cart = $this->checkout->get_cart_for_transaction($this->checkout->transaction); |
|
| 2654 | - if ($cart instanceof EE_Cart) { |
|
| 2655 | - // verify that the current cart has tickets |
|
| 2656 | - $tickets = $cart->get_tickets(); |
|
| 2657 | - if (empty($tickets)) { |
|
| 2658 | - $invalid_TXN = true; |
|
| 2659 | - } |
|
| 2660 | - } else { |
|
| 2661 | - $invalid_TXN = true; |
|
| 2662 | - } |
|
| 2663 | - $valid_TXN_SID = $primary_registrant instanceof EE_Registration |
|
| 2664 | - ? $primary_registrant->session_ID() |
|
| 2665 | - : null; |
|
| 2666 | - // validate current Session ID and compare against valid TXN session ID |
|
| 2667 | - if ($invalid_TXN // if this is already true, then skip other checks |
|
| 2668 | - || EE_Session::instance()->id() === null |
|
| 2669 | - || ( |
|
| 2670 | - // WARNING !!! |
|
| 2671 | - // this could be PayPal sending back duplicate requests (ya they do that) |
|
| 2672 | - // or it **could** mean someone is simply registering AGAIN after having just done so |
|
| 2673 | - // so now we need to determine if this current TXN looks valid or not |
|
| 2674 | - // and whether this reg step has even been started ? |
|
| 2675 | - EE_Session::instance()->id() === $valid_TXN_SID |
|
| 2676 | - // really? you're half way through this reg step, but you never started it ? |
|
| 2677 | - && $this->checkout->transaction->reg_step_completed($this->slug()) === false |
|
| 2678 | - ) |
|
| 2679 | - ) { |
|
| 2680 | - $invalid_TXN = true; |
|
| 2681 | - } |
|
| 2682 | - if ($invalid_TXN) { |
|
| 2683 | - // is the valid TXN completed ? |
|
| 2684 | - if ($valid_TXN instanceof EE_Transaction) { |
|
| 2685 | - // has this step even been started ? |
|
| 2686 | - $reg_step_completed = $valid_TXN->reg_step_completed($this->slug()); |
|
| 2687 | - if ($reg_step_completed !== false && $reg_step_completed !== true) { |
|
| 2688 | - // so it **looks** like this is a double request from PayPal |
|
| 2689 | - // so let's try to pick up where we left off |
|
| 2690 | - $this->checkout->transaction = $valid_TXN; |
|
| 2691 | - $this->checkout->refresh_all_entities(true); |
|
| 2692 | - return; |
|
| 2693 | - } |
|
| 2694 | - } |
|
| 2695 | - // you appear to be lost? |
|
| 2696 | - $this->_redirect_wayward_request($primary_registrant); |
|
| 2697 | - } |
|
| 2698 | - } |
|
| 2699 | - } |
|
| 2700 | - |
|
| 2701 | - |
|
| 2702 | - /** |
|
| 2703 | - * _redirect_wayward_request |
|
| 2704 | - * |
|
| 2705 | - * @access private |
|
| 2706 | - * @param \EE_Registration|null $primary_registrant |
|
| 2707 | - * @return bool |
|
| 2708 | - * @throws EE_Error |
|
| 2709 | - * @throws InvalidArgumentException |
|
| 2710 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2711 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2712 | - */ |
|
| 2713 | - private function _redirect_wayward_request(EE_Registration $primary_registrant) |
|
| 2714 | - { |
|
| 2715 | - if (! $primary_registrant instanceof EE_Registration) { |
|
| 2716 | - // try redirecting based on the current TXN |
|
| 2717 | - $primary_registrant = $this->checkout->transaction instanceof EE_Transaction |
|
| 2718 | - ? $this->checkout->transaction->primary_registration() |
|
| 2719 | - : null; |
|
| 2720 | - } |
|
| 2721 | - if (! $primary_registrant instanceof EE_Registration) { |
|
| 2722 | - EE_Error::add_error( |
|
| 2723 | - sprintf( |
|
| 2724 | - esc_html__( |
|
| 2725 | - 'Invalid information was received from the Off-Site Payment Processor and your Transaction details could not be retrieved from the database.%1$sPlease try again or contact %2$s for assistance.', |
|
| 2726 | - 'event_espresso' |
|
| 2727 | - ), |
|
| 2728 | - '<br/>', |
|
| 2729 | - EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2730 | - ), |
|
| 2731 | - __FILE__, |
|
| 2732 | - __FUNCTION__, |
|
| 2733 | - __LINE__ |
|
| 2734 | - ); |
|
| 2735 | - return false; |
|
| 2736 | - } |
|
| 2737 | - // make sure transaction is not locked |
|
| 2738 | - $this->checkout->transaction->unlock(); |
|
| 2739 | - wp_safe_redirect( |
|
| 2740 | - add_query_arg( |
|
| 2741 | - array( |
|
| 2742 | - 'e_reg_url_link' => $primary_registrant->reg_url_link(), |
|
| 2743 | - ), |
|
| 2744 | - $this->checkout->thank_you_page_url |
|
| 2745 | - ) |
|
| 2746 | - ); |
|
| 2747 | - exit(); |
|
| 2748 | - } |
|
| 2749 | - |
|
| 2750 | - |
|
| 2751 | - /** |
|
| 2752 | - * _process_off_site_payment |
|
| 2753 | - * |
|
| 2754 | - * @access private |
|
| 2755 | - * @param \EE_Offsite_Gateway $gateway |
|
| 2756 | - * @return EE_Payment |
|
| 2757 | - * @throws EE_Error |
|
| 2758 | - * @throws InvalidArgumentException |
|
| 2759 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2760 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2761 | - */ |
|
| 2762 | - private function _process_off_site_payment(EE_Offsite_Gateway $gateway) |
|
| 2763 | - { |
|
| 2764 | - try { |
|
| 2765 | - $request_data = \EE_Registry::instance()->REQ->params(); |
|
| 2766 | - // if gateway uses_separate_IPN_request, then we don't have to process the IPN manually |
|
| 2767 | - $this->set_handle_IPN_in_this_request( |
|
| 2768 | - $gateway->handle_IPN_in_this_request($request_data, false) |
|
| 2769 | - ); |
|
| 2770 | - if ($this->handle_IPN_in_this_request()) { |
|
| 2771 | - // get payment details and process results |
|
| 2772 | - /** @type EE_Payment_Processor $payment_processor */ |
|
| 2773 | - $payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
|
| 2774 | - $payment = $payment_processor->process_ipn( |
|
| 2775 | - $request_data, |
|
| 2776 | - $this->checkout->transaction, |
|
| 2777 | - $this->checkout->payment_method, |
|
| 2778 | - true, |
|
| 2779 | - false |
|
| 2780 | - ); |
|
| 2781 | - // $payment_source = 'process_ipn'; |
|
| 2782 | - } else { |
|
| 2783 | - $payment = $this->checkout->transaction->last_payment(); |
|
| 2784 | - // $payment_source = 'last_payment'; |
|
| 2785 | - } |
|
| 2786 | - } catch (Exception $e) { |
|
| 2787 | - // let's just eat the exception and try to move on using any previously set payment info |
|
| 2788 | - $payment = $this->checkout->transaction->last_payment(); |
|
| 2789 | - // $payment_source = 'last_payment after Exception'; |
|
| 2790 | - // but if we STILL don't have a payment object |
|
| 2791 | - if (! $payment instanceof EE_Payment) { |
|
| 2792 | - // then we'll object ! ( not object like a thing... but object like what a lawyer says ! ) |
|
| 2793 | - $this->_handle_payment_processor_exception($e); |
|
| 2794 | - } |
|
| 2795 | - } |
|
| 2796 | - // DEBUG LOG |
|
| 2797 | - // $this->checkout->log( |
|
| 2798 | - // __CLASS__, |
|
| 2799 | - // __FUNCTION__, |
|
| 2800 | - // __LINE__, |
|
| 2801 | - // array( |
|
| 2802 | - // 'process_ipn_payment' => $payment, |
|
| 2803 | - // 'payment_source' => $payment_source, |
|
| 2804 | - // ) |
|
| 2805 | - // ); |
|
| 2806 | - return $payment; |
|
| 2807 | - } |
|
| 2808 | - |
|
| 2809 | - |
|
| 2810 | - /** |
|
| 2811 | - * _process_cancelled_payments |
|
| 2812 | - * just makes sure that the payment status gets updated correctly |
|
| 2813 | - * so tha tan error isn't generated during payment validation |
|
| 2814 | - * |
|
| 2815 | - * @access private |
|
| 2816 | - * @param EE_Payment $payment |
|
| 2817 | - * @return EE_Payment | FALSE |
|
| 2818 | - * @throws \EE_Error |
|
| 2819 | - */ |
|
| 2820 | - private function _process_cancelled_payments($payment = null) |
|
| 2821 | - { |
|
| 2822 | - if ($payment instanceof EE_Payment |
|
| 2823 | - && isset($_REQUEST['ee_cancel_payment']) |
|
| 2824 | - && $payment->status() === EEM_Payment::status_id_failed |
|
| 2825 | - ) { |
|
| 2826 | - $payment->set_status(EEM_Payment::status_id_cancelled); |
|
| 2827 | - } |
|
| 2828 | - return $payment; |
|
| 2829 | - } |
|
| 2830 | - |
|
| 2831 | - |
|
| 2832 | - /** |
|
| 2833 | - * get_transaction_details_for_gateways |
|
| 2834 | - * |
|
| 2835 | - * @access public |
|
| 2836 | - * @return int |
|
| 2837 | - * @throws EE_Error |
|
| 2838 | - * @throws InvalidArgumentException |
|
| 2839 | - * @throws ReflectionException |
|
| 2840 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2841 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2842 | - */ |
|
| 2843 | - public function get_transaction_details_for_gateways() |
|
| 2844 | - { |
|
| 2845 | - $txn_details = array(); |
|
| 2846 | - // ya gotta make a choice man |
|
| 2847 | - if (empty($this->checkout->selected_method_of_payment)) { |
|
| 2848 | - $txn_details = array( |
|
| 2849 | - 'error' => esc_html__('Please select a method of payment before proceeding.', 'event_espresso'), |
|
| 2850 | - ); |
|
| 2851 | - } |
|
| 2852 | - // get EE_Payment_Method object |
|
| 2853 | - if (empty($txn_details) |
|
| 2854 | - && |
|
| 2855 | - ! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment() |
|
| 2856 | - ) { |
|
| 2857 | - $txn_details = array( |
|
| 2858 | - 'selected_method_of_payment' => $this->checkout->selected_method_of_payment, |
|
| 2859 | - 'error' => esc_html__( |
|
| 2860 | - 'A valid Payment Method could not be determined.', |
|
| 2861 | - 'event_espresso' |
|
| 2862 | - ), |
|
| 2863 | - ); |
|
| 2864 | - } |
|
| 2865 | - if (empty($txn_details) && $this->checkout->transaction instanceof EE_Transaction) { |
|
| 2866 | - $return_url = $this->_get_return_url($this->checkout->payment_method); |
|
| 2867 | - $txn_details = array( |
|
| 2868 | - 'TXN_ID' => $this->checkout->transaction->ID(), |
|
| 2869 | - 'TXN_timestamp' => $this->checkout->transaction->datetime(), |
|
| 2870 | - 'TXN_total' => $this->checkout->transaction->total(), |
|
| 2871 | - 'TXN_paid' => $this->checkout->transaction->paid(), |
|
| 2872 | - 'TXN_reg_steps' => $this->checkout->transaction->reg_steps(), |
|
| 2873 | - 'STS_ID' => $this->checkout->transaction->status_ID(), |
|
| 2874 | - 'PMD_ID' => $this->checkout->transaction->payment_method_ID(), |
|
| 2875 | - 'payment_amount' => $this->checkout->amount_owing, |
|
| 2876 | - 'return_url' => $return_url, |
|
| 2877 | - 'cancel_url' => add_query_arg(array('ee_cancel_payment' => true), $return_url), |
|
| 2878 | - 'notify_url' => EE_Config::instance()->core->txn_page_url( |
|
| 2879 | - array( |
|
| 2880 | - 'e_reg_url_link' => $this->checkout->transaction->primary_registration()->reg_url_link(), |
|
| 2881 | - 'ee_payment_method' => $this->checkout->payment_method->slug(), |
|
| 2882 | - ) |
|
| 2883 | - ), |
|
| 2884 | - ); |
|
| 2885 | - } |
|
| 2886 | - echo wp_json_encode($txn_details); |
|
| 2887 | - exit(); |
|
| 2888 | - } |
|
| 2889 | - |
|
| 2890 | - |
|
| 2891 | - /** |
|
| 2892 | - * __sleep |
|
| 2893 | - * to conserve db space, let's remove the reg_form and the EE_Checkout object from EE_SPCO_Reg_Step objects upon |
|
| 2894 | - * serialization EE_Checkout will handle the reimplementation of itself upon waking, but we won't bother with the |
|
| 2895 | - * reg form, because if needed, it will be regenerated anyways |
|
| 2896 | - * |
|
| 2897 | - * @return array |
|
| 2898 | - */ |
|
| 2899 | - public function __sleep() |
|
| 2900 | - { |
|
| 2901 | - // remove the reg form and the checkout |
|
| 2902 | - return array_diff(array_keys(get_object_vars($this)), array('reg_form', 'checkout', 'line_item_display')); |
|
| 2903 | - } |
|
| 15 | + /** |
|
| 16 | + * @access protected |
|
| 17 | + * @var EE_Line_Item_Display $Line_Item_Display |
|
| 18 | + */ |
|
| 19 | + protected $line_item_display; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @access protected |
|
| 23 | + * @var boolean $handle_IPN_in_this_request |
|
| 24 | + */ |
|
| 25 | + protected $handle_IPN_in_this_request = false; |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * set_hooks - for hooking into EE Core, other modules, etc |
|
| 30 | + * |
|
| 31 | + * @access public |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + public static function set_hooks() |
|
| 35 | + { |
|
| 36 | + add_filter( |
|
| 37 | + 'FHEE__SPCO__EE_Line_Item_Filter_Collection', |
|
| 38 | + array('EE_SPCO_Reg_Step_Payment_Options', 'add_spco_line_item_filters') |
|
| 39 | + ); |
|
| 40 | + add_action( |
|
| 41 | + 'wp_ajax_switch_spco_billing_form', |
|
| 42 | + array('EE_SPCO_Reg_Step_Payment_Options', 'switch_spco_billing_form') |
|
| 43 | + ); |
|
| 44 | + add_action( |
|
| 45 | + 'wp_ajax_nopriv_switch_spco_billing_form', |
|
| 46 | + array('EE_SPCO_Reg_Step_Payment_Options', 'switch_spco_billing_form') |
|
| 47 | + ); |
|
| 48 | + add_action('wp_ajax_save_payer_details', array('EE_SPCO_Reg_Step_Payment_Options', 'save_payer_details')); |
|
| 49 | + add_action( |
|
| 50 | + 'wp_ajax_nopriv_save_payer_details', |
|
| 51 | + array('EE_SPCO_Reg_Step_Payment_Options', 'save_payer_details') |
|
| 52 | + ); |
|
| 53 | + add_action( |
|
| 54 | + 'wp_ajax_get_transaction_details_for_gateways', |
|
| 55 | + array('EE_SPCO_Reg_Step_Payment_Options', 'get_transaction_details') |
|
| 56 | + ); |
|
| 57 | + add_action( |
|
| 58 | + 'wp_ajax_nopriv_get_transaction_details_for_gateways', |
|
| 59 | + array('EE_SPCO_Reg_Step_Payment_Options', 'get_transaction_details') |
|
| 60 | + ); |
|
| 61 | + add_filter( |
|
| 62 | + 'FHEE__EED_Recaptcha___bypass_recaptcha__bypass_request_params_array', |
|
| 63 | + array('EE_SPCO_Reg_Step_Payment_Options', 'bypass_recaptcha_for_load_payment_method'), |
|
| 64 | + 10, |
|
| 65 | + 1 |
|
| 66 | + ); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * ajax switch_spco_billing_form |
|
| 72 | + * |
|
| 73 | + * @throws \EE_Error |
|
| 74 | + */ |
|
| 75 | + public static function switch_spco_billing_form() |
|
| 76 | + { |
|
| 77 | + EED_Single_Page_Checkout::process_ajax_request('switch_payment_method'); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * ajax save_payer_details |
|
| 83 | + * |
|
| 84 | + * @throws \EE_Error |
|
| 85 | + */ |
|
| 86 | + public static function save_payer_details() |
|
| 87 | + { |
|
| 88 | + EED_Single_Page_Checkout::process_ajax_request('save_payer_details_via_ajax'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * ajax get_transaction_details |
|
| 94 | + * |
|
| 95 | + * @throws \EE_Error |
|
| 96 | + */ |
|
| 97 | + public static function get_transaction_details() |
|
| 98 | + { |
|
| 99 | + EED_Single_Page_Checkout::process_ajax_request('get_transaction_details_for_gateways'); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * bypass_recaptcha_for_load_payment_method |
|
| 105 | + * |
|
| 106 | + * @access public |
|
| 107 | + * @return array |
|
| 108 | + * @throws InvalidArgumentException |
|
| 109 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 110 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 111 | + */ |
|
| 112 | + public static function bypass_recaptcha_for_load_payment_method() |
|
| 113 | + { |
|
| 114 | + return array( |
|
| 115 | + 'EESID' => EE_Registry::instance()->SSN->id(), |
|
| 116 | + 'step' => 'payment_options', |
|
| 117 | + 'action' => 'spco_billing_form', |
|
| 118 | + ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * class constructor |
|
| 124 | + * |
|
| 125 | + * @access public |
|
| 126 | + * @param EE_Checkout $checkout |
|
| 127 | + */ |
|
| 128 | + public function __construct(EE_Checkout $checkout) |
|
| 129 | + { |
|
| 130 | + $this->_slug = 'payment_options'; |
|
| 131 | + $this->_name = esc_html__('Payment Options', 'event_espresso'); |
|
| 132 | + $this->_template = SPCO_REG_STEPS_PATH . $this->_slug . DS . 'payment_options_main.template.php'; |
|
| 133 | + $this->checkout = $checkout; |
|
| 134 | + $this->_reset_success_message(); |
|
| 135 | + $this->set_instructions( |
|
| 136 | + esc_html__( |
|
| 137 | + 'Please select a method of payment and provide any necessary billing information before proceeding.', |
|
| 138 | + 'event_espresso' |
|
| 139 | + ) |
|
| 140 | + ); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @return null |
|
| 146 | + */ |
|
| 147 | + public function line_item_display() |
|
| 148 | + { |
|
| 149 | + return $this->line_item_display; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @param null $line_item_display |
|
| 155 | + */ |
|
| 156 | + public function set_line_item_display($line_item_display) |
|
| 157 | + { |
|
| 158 | + $this->line_item_display = $line_item_display; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @return boolean |
|
| 164 | + */ |
|
| 165 | + public function handle_IPN_in_this_request() |
|
| 166 | + { |
|
| 167 | + return $this->handle_IPN_in_this_request; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * @param boolean $handle_IPN_in_this_request |
|
| 173 | + */ |
|
| 174 | + public function set_handle_IPN_in_this_request($handle_IPN_in_this_request) |
|
| 175 | + { |
|
| 176 | + $this->handle_IPN_in_this_request = filter_var($handle_IPN_in_this_request, FILTER_VALIDATE_BOOLEAN); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * translate_js_strings |
|
| 182 | + * |
|
| 183 | + * @return void |
|
| 184 | + */ |
|
| 185 | + public function translate_js_strings() |
|
| 186 | + { |
|
| 187 | + EE_Registry::$i18n_js_strings['no_payment_method'] = esc_html__( |
|
| 188 | + 'Please select a method of payment in order to continue.', |
|
| 189 | + 'event_espresso' |
|
| 190 | + ); |
|
| 191 | + EE_Registry::$i18n_js_strings['invalid_payment_method'] = esc_html__( |
|
| 192 | + 'A valid method of payment could not be determined. Please refresh the page and try again.', |
|
| 193 | + 'event_espresso' |
|
| 194 | + ); |
|
| 195 | + EE_Registry::$i18n_js_strings['forwarding_to_offsite'] = esc_html__( |
|
| 196 | + 'Forwarding to Secure Payment Provider.', |
|
| 197 | + 'event_espresso' |
|
| 198 | + ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * enqueue_styles_and_scripts |
|
| 204 | + * |
|
| 205 | + * @return void |
|
| 206 | + * @throws EE_Error |
|
| 207 | + * @throws InvalidArgumentException |
|
| 208 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 209 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 210 | + */ |
|
| 211 | + public function enqueue_styles_and_scripts() |
|
| 212 | + { |
|
| 213 | + $transaction = $this->checkout->transaction; |
|
| 214 | + // if the transaction isn't set or nothing is owed on it, don't enqueue any JS |
|
| 215 | + if (! $transaction instanceof EE_Transaction || EEH_Money::compare_floats($transaction->remaining(), 0)) { |
|
| 216 | + return; |
|
| 217 | + } |
|
| 218 | + foreach (EEM_Payment_Method::instance()->get_all_for_transaction( |
|
| 219 | + $transaction, |
|
| 220 | + EEM_Payment_Method::scope_cart |
|
| 221 | + ) as $payment_method) { |
|
| 222 | + $type_obj = $payment_method->type_obj(); |
|
| 223 | + if ($type_obj instanceof EE_PMT_Base) { |
|
| 224 | + $billing_form = $type_obj->generate_new_billing_form($transaction); |
|
| 225 | + if ($billing_form instanceof EE_Form_Section_Proper) { |
|
| 226 | + $billing_form->enqueue_js(); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * initialize_reg_step |
|
| 235 | + * |
|
| 236 | + * @return bool |
|
| 237 | + * @throws EE_Error |
|
| 238 | + * @throws InvalidArgumentException |
|
| 239 | + * @throws ReflectionException |
|
| 240 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 241 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 242 | + */ |
|
| 243 | + public function initialize_reg_step() |
|
| 244 | + { |
|
| 245 | + // TODO: if /when we implement donations, then this will need overriding |
|
| 246 | + if (// don't need payment options for: |
|
| 247 | + // registrations made via the admin |
|
| 248 | + // completed transactions |
|
| 249 | + // overpaid transactions |
|
| 250 | + // $ 0.00 transactions(no payment required) |
|
| 251 | + ! $this->checkout->payment_required() |
|
| 252 | + // but do NOT remove if current action being called belongs to this reg step |
|
| 253 | + && ! is_callable(array($this, $this->checkout->action)) |
|
| 254 | + && ! $this->completed() |
|
| 255 | + ) { |
|
| 256 | + // and if so, then we no longer need the Payment Options step |
|
| 257 | + if ($this->is_current_step()) { |
|
| 258 | + $this->checkout->generate_reg_form = false; |
|
| 259 | + } |
|
| 260 | + $this->checkout->remove_reg_step($this->_slug); |
|
| 261 | + // DEBUG LOG |
|
| 262 | + // $this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ ); |
|
| 263 | + return false; |
|
| 264 | + } |
|
| 265 | + // load EEM_Payment_Method |
|
| 266 | + EE_Registry::instance()->load_model('Payment_Method'); |
|
| 267 | + // get all active payment methods |
|
| 268 | + $this->checkout->available_payment_methods = EEM_Payment_Method::instance()->get_all_for_transaction( |
|
| 269 | + $this->checkout->transaction, |
|
| 270 | + EEM_Payment_Method::scope_cart |
|
| 271 | + ); |
|
| 272 | + return true; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * @return EE_Form_Section_Proper |
|
| 278 | + * @throws EE_Error |
|
| 279 | + * @throws InvalidArgumentException |
|
| 280 | + * @throws ReflectionException |
|
| 281 | + * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 282 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 283 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 284 | + * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 285 | + */ |
|
| 286 | + public function generate_reg_form() |
|
| 287 | + { |
|
| 288 | + // reset in case someone changes their mind |
|
| 289 | + $this->_reset_selected_method_of_payment(); |
|
| 290 | + // set some defaults |
|
| 291 | + $this->checkout->selected_method_of_payment = 'payments_closed'; |
|
| 292 | + $registrations_requiring_payment = array(); |
|
| 293 | + $registrations_for_free_events = array(); |
|
| 294 | + $registrations_requiring_pre_approval = array(); |
|
| 295 | + $sold_out_events = array(); |
|
| 296 | + $insufficient_spaces_available = array(); |
|
| 297 | + $no_payment_required = true; |
|
| 298 | + // loop thru registrations to gather info |
|
| 299 | + $registrations = $this->checkout->transaction->registrations($this->checkout->reg_cache_where_params); |
|
| 300 | + $ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space( |
|
| 301 | + $registrations, |
|
| 302 | + $this->checkout->revisit |
|
| 303 | + ); |
|
| 304 | + foreach ($registrations as $REG_ID => $registration) { |
|
| 305 | + /** @var $registration EE_Registration */ |
|
| 306 | + // has this registration lost it's space ? |
|
| 307 | + if (isset($ejected_registrations[ $REG_ID ])) { |
|
| 308 | + if ($registration->event()->is_sold_out() || $registration->event()->is_sold_out(true)) { |
|
| 309 | + $sold_out_events[ $registration->event()->ID() ] = $registration->event(); |
|
| 310 | + } else { |
|
| 311 | + $insufficient_spaces_available[ $registration->event()->ID() ] = $registration->event(); |
|
| 312 | + } |
|
| 313 | + continue; |
|
| 314 | + } |
|
| 315 | + // event requires admin approval |
|
| 316 | + if ($registration->status_ID() === EEM_Registration::status_id_not_approved) { |
|
| 317 | + // add event to list of events with pre-approval reg status |
|
| 318 | + $registrations_requiring_pre_approval[ $REG_ID ] = $registration; |
|
| 319 | + do_action( |
|
| 320 | + 'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__event_requires_pre_approval', |
|
| 321 | + $registration->event(), |
|
| 322 | + $this |
|
| 323 | + ); |
|
| 324 | + continue; |
|
| 325 | + } |
|
| 326 | + if ($this->checkout->revisit |
|
| 327 | + && $registration->status_ID() !== EEM_Registration::status_id_approved |
|
| 328 | + && ( |
|
| 329 | + $registration->event()->is_sold_out() |
|
| 330 | + || $registration->event()->is_sold_out(true) |
|
| 331 | + ) |
|
| 332 | + ) { |
|
| 333 | + // add event to list of events that are sold out |
|
| 334 | + $sold_out_events[ $registration->event()->ID() ] = $registration->event(); |
|
| 335 | + do_action( |
|
| 336 | + 'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__sold_out_event', |
|
| 337 | + $registration->event(), |
|
| 338 | + $this |
|
| 339 | + ); |
|
| 340 | + continue; |
|
| 341 | + } |
|
| 342 | + // are they allowed to pay now and is there monies owing? |
|
| 343 | + if ($registration->owes_monies_and_can_pay()) { |
|
| 344 | + $registrations_requiring_payment[ $REG_ID ] = $registration; |
|
| 345 | + do_action( |
|
| 346 | + 'AHEE__EE_SPCO_Reg_Step_Payment_Options__generate_reg_form__event_requires_payment', |
|
| 347 | + $registration->event(), |
|
| 348 | + $this |
|
| 349 | + ); |
|
| 350 | + } elseif (! $this->checkout->revisit |
|
| 351 | + && $registration->status_ID() !== EEM_Registration::status_id_not_approved |
|
| 352 | + && $registration->ticket()->is_free() |
|
| 353 | + ) { |
|
| 354 | + $registrations_for_free_events[ $registration->ticket()->ID() ] = $registration; |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + $subsections = array(); |
|
| 358 | + // now decide which template to load |
|
| 359 | + if (! empty($sold_out_events)) { |
|
| 360 | + $subsections['sold_out_events'] = $this->_sold_out_events($sold_out_events); |
|
| 361 | + } |
|
| 362 | + if (! empty($insufficient_spaces_available)) { |
|
| 363 | + $subsections['insufficient_space'] = $this->_insufficient_spaces_available( |
|
| 364 | + $insufficient_spaces_available |
|
| 365 | + ); |
|
| 366 | + } |
|
| 367 | + if (! empty($registrations_requiring_pre_approval)) { |
|
| 368 | + $subsections['registrations_requiring_pre_approval'] = $this->_registrations_requiring_pre_approval( |
|
| 369 | + $registrations_requiring_pre_approval |
|
| 370 | + ); |
|
| 371 | + } |
|
| 372 | + if (! empty($registrations_for_free_events)) { |
|
| 373 | + $subsections['no_payment_required'] = $this->_no_payment_required($registrations_for_free_events); |
|
| 374 | + } |
|
| 375 | + if (! empty($registrations_requiring_payment)) { |
|
| 376 | + if ($this->checkout->amount_owing > 0) { |
|
| 377 | + // autoload Line_Item_Display classes |
|
| 378 | + EEH_Autoloader::register_line_item_filter_autoloaders(); |
|
| 379 | + $line_item_filter_processor = new EE_Line_Item_Filter_Processor( |
|
| 380 | + apply_filters( |
|
| 381 | + 'FHEE__SPCO__EE_Line_Item_Filter_Collection', |
|
| 382 | + new EE_Line_Item_Filter_Collection() |
|
| 383 | + ), |
|
| 384 | + $this->checkout->cart->get_grand_total() |
|
| 385 | + ); |
|
| 386 | + /** @var EE_Line_Item $filtered_line_item_tree */ |
|
| 387 | + $filtered_line_item_tree = $line_item_filter_processor->process(); |
|
| 388 | + EEH_Autoloader::register_line_item_display_autoloaders(); |
|
| 389 | + $this->set_line_item_display(new EE_Line_Item_Display('spco')); |
|
| 390 | + $subsections['payment_options'] = $this->_display_payment_options( |
|
| 391 | + $this->line_item_display->display_line_item( |
|
| 392 | + $filtered_line_item_tree, |
|
| 393 | + array('registrations' => $registrations) |
|
| 394 | + ) |
|
| 395 | + ); |
|
| 396 | + $this->checkout->amount_owing = $filtered_line_item_tree->total(); |
|
| 397 | + $this->_apply_registration_payments_to_amount_owing($registrations); |
|
| 398 | + } |
|
| 399 | + $no_payment_required = false; |
|
| 400 | + } else { |
|
| 401 | + $this->_hide_reg_step_submit_button_if_revisit(); |
|
| 402 | + } |
|
| 403 | + $this->_save_selected_method_of_payment(); |
|
| 404 | + |
|
| 405 | + $subsections['default_hidden_inputs'] = $this->reg_step_hidden_inputs(); |
|
| 406 | + $subsections['extra_hidden_inputs'] = $this->_extra_hidden_inputs($no_payment_required); |
|
| 407 | + |
|
| 408 | + return new EE_Form_Section_Proper( |
|
| 409 | + array( |
|
| 410 | + 'name' => $this->reg_form_name(), |
|
| 411 | + 'html_id' => $this->reg_form_name(), |
|
| 412 | + 'subsections' => $subsections, |
|
| 413 | + 'layout_strategy' => new EE_No_Layout(), |
|
| 414 | + ) |
|
| 415 | + ); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * add line item filters required for this reg step |
|
| 421 | + * these filters are applied via this line in EE_SPCO_Reg_Step_Payment_Options::set_hooks(): |
|
| 422 | + * add_filter( 'FHEE__SPCO__EE_Line_Item_Filter_Collection', array( 'EE_SPCO_Reg_Step_Payment_Options', |
|
| 423 | + * 'add_spco_line_item_filters' ) ); so any code that wants to use the same set of filters during the |
|
| 424 | + * payment options reg step, can apply these filters via the following: apply_filters( |
|
| 425 | + * 'FHEE__SPCO__EE_Line_Item_Filter_Collection', new EE_Line_Item_Filter_Collection() ) or to an existing |
|
| 426 | + * filter collection by passing that instead of instantiating a new collection |
|
| 427 | + * |
|
| 428 | + * @param \EE_Line_Item_Filter_Collection $line_item_filter_collection |
|
| 429 | + * @return EE_Line_Item_Filter_Collection |
|
| 430 | + * @throws EE_Error |
|
| 431 | + * @throws InvalidArgumentException |
|
| 432 | + * @throws ReflectionException |
|
| 433 | + * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 434 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 435 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 436 | + * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 437 | + */ |
|
| 438 | + public static function add_spco_line_item_filters(EE_Line_Item_Filter_Collection $line_item_filter_collection) |
|
| 439 | + { |
|
| 440 | + if (! EE_Registry::instance()->SSN instanceof EE_Session) { |
|
| 441 | + return $line_item_filter_collection; |
|
| 442 | + } |
|
| 443 | + if (! EE_Registry::instance()->SSN->checkout() instanceof EE_Checkout) { |
|
| 444 | + return $line_item_filter_collection; |
|
| 445 | + } |
|
| 446 | + if (! EE_Registry::instance()->SSN->checkout()->transaction instanceof EE_Transaction) { |
|
| 447 | + return $line_item_filter_collection; |
|
| 448 | + } |
|
| 449 | + $line_item_filter_collection->add( |
|
| 450 | + new EE_Billable_Line_Item_Filter( |
|
| 451 | + EE_SPCO_Reg_Step_Payment_Options::remove_ejected_registrations( |
|
| 452 | + EE_Registry::instance()->SSN->checkout()->transaction->registrations( |
|
| 453 | + EE_Registry::instance()->SSN->checkout()->reg_cache_where_params |
|
| 454 | + ) |
|
| 455 | + ) |
|
| 456 | + ) |
|
| 457 | + ); |
|
| 458 | + $line_item_filter_collection->add(new EE_Non_Zero_Line_Item_Filter()); |
|
| 459 | + return $line_item_filter_collection; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * remove_ejected_registrations |
|
| 465 | + * if a registrant has lost their potential space at an event due to lack of payment, |
|
| 466 | + * then this method removes them from the list of registrations being paid for during this request |
|
| 467 | + * |
|
| 468 | + * @param \EE_Registration[] $registrations |
|
| 469 | + * @return EE_Registration[] |
|
| 470 | + * @throws EE_Error |
|
| 471 | + * @throws InvalidArgumentException |
|
| 472 | + * @throws ReflectionException |
|
| 473 | + * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 474 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 475 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 476 | + * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 477 | + */ |
|
| 478 | + public static function remove_ejected_registrations(array $registrations) |
|
| 479 | + { |
|
| 480 | + $ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space( |
|
| 481 | + $registrations, |
|
| 482 | + EE_Registry::instance()->SSN->checkout()->revisit |
|
| 483 | + ); |
|
| 484 | + foreach ($registrations as $REG_ID => $registration) { |
|
| 485 | + // has this registration lost it's space ? |
|
| 486 | + if (isset($ejected_registrations[ $REG_ID ])) { |
|
| 487 | + unset($registrations[ $REG_ID ]); |
|
| 488 | + continue; |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + return $registrations; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * find_registrations_that_lost_their_space |
|
| 497 | + * If a registrant chooses an offline payment method like Invoice, |
|
| 498 | + * then no space is reserved for them at the event until they fully pay fo that site |
|
| 499 | + * (unless the event's default reg status is set to APPROVED) |
|
| 500 | + * if a registrant then later returns to pay, but the number of spaces available has been reduced due to sales, |
|
| 501 | + * then this method will determine which registrations have lost the ability to complete the reg process. |
|
| 502 | + * |
|
| 503 | + * @param \EE_Registration[] $registrations |
|
| 504 | + * @param bool $revisit |
|
| 505 | + * @return array |
|
| 506 | + * @throws EE_Error |
|
| 507 | + * @throws InvalidArgumentException |
|
| 508 | + * @throws ReflectionException |
|
| 509 | + * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 510 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 511 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 512 | + * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 513 | + */ |
|
| 514 | + public static function find_registrations_that_lost_their_space(array $registrations, $revisit = false) |
|
| 515 | + { |
|
| 516 | + // registrations per event |
|
| 517 | + $event_reg_count = array(); |
|
| 518 | + // spaces left per event |
|
| 519 | + $event_spaces_remaining = array(); |
|
| 520 | + // tickets left sorted by ID |
|
| 521 | + $tickets_remaining = array(); |
|
| 522 | + // registrations that have lost their space |
|
| 523 | + $ejected_registrations = array(); |
|
| 524 | + foreach ($registrations as $REG_ID => $registration) { |
|
| 525 | + if ($registration->status_ID() === EEM_Registration::status_id_approved |
|
| 526 | + || apply_filters( |
|
| 527 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options__find_registrations_that_lost_their_space__allow_reg_payment', |
|
| 528 | + false, |
|
| 529 | + $registration, |
|
| 530 | + $revisit |
|
| 531 | + ) |
|
| 532 | + ) { |
|
| 533 | + continue; |
|
| 534 | + } |
|
| 535 | + $EVT_ID = $registration->event_ID(); |
|
| 536 | + $ticket = $registration->ticket(); |
|
| 537 | + if (! isset($tickets_remaining[ $ticket->ID() ])) { |
|
| 538 | + $tickets_remaining[ $ticket->ID() ] = $ticket->remaining(); |
|
| 539 | + } |
|
| 540 | + if ($tickets_remaining[ $ticket->ID() ] > 0) { |
|
| 541 | + if (! isset($event_reg_count[ $EVT_ID ])) { |
|
| 542 | + $event_reg_count[ $EVT_ID ] = 0; |
|
| 543 | + } |
|
| 544 | + $event_reg_count[ $EVT_ID ]++; |
|
| 545 | + if (! isset($event_spaces_remaining[ $EVT_ID ])) { |
|
| 546 | + $event_spaces_remaining[ $EVT_ID ] = $registration->event()->spaces_remaining_for_sale(); |
|
| 547 | + } |
|
| 548 | + } |
|
| 549 | + if ($revisit |
|
| 550 | + && ($tickets_remaining[ $ticket->ID() ] === 0 |
|
| 551 | + || $event_reg_count[ $EVT_ID ] > $event_spaces_remaining[ $EVT_ID ] |
|
| 552 | + ) |
|
| 553 | + ) { |
|
| 554 | + $ejected_registrations[ $REG_ID ] = $registration->event(); |
|
| 555 | + if ($registration->status_ID() !== EEM_Registration::status_id_wait_list) { |
|
| 556 | + /** @type EE_Registration_Processor $registration_processor */ |
|
| 557 | + $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
| 558 | + // at this point, we should have enough details about the registrant to consider the registration |
|
| 559 | + // NOT incomplete |
|
| 560 | + $registration_processor->manually_update_registration_status( |
|
| 561 | + $registration, |
|
| 562 | + EEM_Registration::status_id_wait_list |
|
| 563 | + ); |
|
| 564 | + } |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + return $ejected_registrations; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * _hide_reg_step_submit_button |
|
| 573 | + * removes the html for the reg step submit button |
|
| 574 | + * by replacing it with an empty string via filter callback |
|
| 575 | + * |
|
| 576 | + * @return void |
|
| 577 | + */ |
|
| 578 | + protected function _adjust_registration_status_if_event_old_sold() |
|
| 579 | + { |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * _hide_reg_step_submit_button |
|
| 585 | + * removes the html for the reg step submit button |
|
| 586 | + * by replacing it with an empty string via filter callback |
|
| 587 | + * |
|
| 588 | + * @return void |
|
| 589 | + */ |
|
| 590 | + protected function _hide_reg_step_submit_button_if_revisit() |
|
| 591 | + { |
|
| 592 | + if ($this->checkout->revisit) { |
|
| 593 | + add_filter('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', '__return_empty_string'); |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * sold_out_events |
|
| 600 | + * displays notices regarding events that have sold out since hte registrant first signed up |
|
| 601 | + * |
|
| 602 | + * @param \EE_Event[] $sold_out_events_array |
|
| 603 | + * @return \EE_Form_Section_Proper |
|
| 604 | + * @throws \EE_Error |
|
| 605 | + */ |
|
| 606 | + private function _sold_out_events($sold_out_events_array = array()) |
|
| 607 | + { |
|
| 608 | + // set some defaults |
|
| 609 | + $this->checkout->selected_method_of_payment = 'events_sold_out'; |
|
| 610 | + $sold_out_events = ''; |
|
| 611 | + foreach ($sold_out_events_array as $sold_out_event) { |
|
| 612 | + $sold_out_events .= EEH_HTML::li( |
|
| 613 | + EEH_HTML::span( |
|
| 614 | + ' ' . $sold_out_event->name(), |
|
| 615 | + '', |
|
| 616 | + 'dashicons dashicons-marker ee-icon-size-16 pink-text' |
|
| 617 | + ) |
|
| 618 | + ); |
|
| 619 | + } |
|
| 620 | + return new EE_Form_Section_Proper( |
|
| 621 | + array( |
|
| 622 | + 'layout_strategy' => new EE_Template_Layout( |
|
| 623 | + array( |
|
| 624 | + 'layout_template_file' => SPCO_REG_STEPS_PATH |
|
| 625 | + . $this->_slug |
|
| 626 | + . DS |
|
| 627 | + . 'sold_out_events.template.php', |
|
| 628 | + 'template_args' => apply_filters( |
|
| 629 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__template_args', |
|
| 630 | + array( |
|
| 631 | + 'sold_out_events' => $sold_out_events, |
|
| 632 | + 'sold_out_events_msg' => apply_filters( |
|
| 633 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__sold_out_events_msg', |
|
| 634 | + sprintf( |
|
| 635 | + esc_html__( |
|
| 636 | + 'It appears that the event you were about to make a payment for has sold out since you first registered. If you have already made a partial payment towards this event, please contact the event administrator for a refund.%3$s%3$s%1$sPlease note that availability can change at any time due to cancellations, so please check back again later if registration for this event(s) is important to you.%2$s', |
|
| 637 | + 'event_espresso' |
|
| 638 | + ), |
|
| 639 | + '<strong>', |
|
| 640 | + '</strong>', |
|
| 641 | + '<br />' |
|
| 642 | + ) |
|
| 643 | + ), |
|
| 644 | + ) |
|
| 645 | + ), |
|
| 646 | + ) |
|
| 647 | + ), |
|
| 648 | + ) |
|
| 649 | + ); |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * _insufficient_spaces_available |
|
| 655 | + * displays notices regarding events that do not have enough remaining spaces |
|
| 656 | + * to satisfy the current number of registrations looking to pay |
|
| 657 | + * |
|
| 658 | + * @param \EE_Event[] $insufficient_spaces_events_array |
|
| 659 | + * @return \EE_Form_Section_Proper |
|
| 660 | + * @throws \EE_Error |
|
| 661 | + */ |
|
| 662 | + private function _insufficient_spaces_available($insufficient_spaces_events_array = array()) |
|
| 663 | + { |
|
| 664 | + // set some defaults |
|
| 665 | + $this->checkout->selected_method_of_payment = 'invoice'; |
|
| 666 | + $insufficient_space_events = ''; |
|
| 667 | + foreach ($insufficient_spaces_events_array as $event) { |
|
| 668 | + if ($event instanceof EE_Event) { |
|
| 669 | + $insufficient_space_events .= EEH_HTML::li( |
|
| 670 | + EEH_HTML::span(' ' . $event->name(), '', 'dashicons dashicons-marker ee-icon-size-16 pink-text') |
|
| 671 | + ); |
|
| 672 | + } |
|
| 673 | + } |
|
| 674 | + return new EE_Form_Section_Proper( |
|
| 675 | + array( |
|
| 676 | + 'subsections' => array( |
|
| 677 | + 'default_hidden_inputs' => $this->reg_step_hidden_inputs(), |
|
| 678 | + 'extra_hidden_inputs' => $this->_extra_hidden_inputs(), |
|
| 679 | + ), |
|
| 680 | + 'layout_strategy' => new EE_Template_Layout( |
|
| 681 | + array( |
|
| 682 | + 'layout_template_file' => SPCO_REG_STEPS_PATH |
|
| 683 | + . $this->_slug |
|
| 684 | + . DS |
|
| 685 | + . 'sold_out_events.template.php', |
|
| 686 | + 'template_args' => apply_filters( |
|
| 687 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___insufficient_spaces_available__template_args', |
|
| 688 | + array( |
|
| 689 | + 'sold_out_events' => $insufficient_space_events, |
|
| 690 | + 'sold_out_events_msg' => apply_filters( |
|
| 691 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___insufficient_spaces_available__insufficient_space_msg', |
|
| 692 | + esc_html__( |
|
| 693 | + 'It appears that the event you were about to make a payment for has sold additional tickets since you first registered, and there are no longer enough spaces left to accommodate your selections. You may continue to pay and secure the available space(s) remaining, or simply cancel if you no longer wish to purchase. If you have already made a partial payment towards this event, please contact the event administrator for a refund.', |
|
| 694 | + 'event_espresso' |
|
| 695 | + ) |
|
| 696 | + ), |
|
| 697 | + ) |
|
| 698 | + ), |
|
| 699 | + ) |
|
| 700 | + ), |
|
| 701 | + ) |
|
| 702 | + ); |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * registrations_requiring_pre_approval |
|
| 708 | + * |
|
| 709 | + * @param array $registrations_requiring_pre_approval |
|
| 710 | + * @return EE_Form_Section_Proper |
|
| 711 | + * @throws EE_Error |
|
| 712 | + * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 713 | + */ |
|
| 714 | + private function _registrations_requiring_pre_approval($registrations_requiring_pre_approval = array()) |
|
| 715 | + { |
|
| 716 | + $events_requiring_pre_approval = ''; |
|
| 717 | + foreach ($registrations_requiring_pre_approval as $registration) { |
|
| 718 | + if ($registration instanceof EE_Registration && $registration->event() instanceof EE_Event) { |
|
| 719 | + $events_requiring_pre_approval[ $registration->event()->ID() ] = EEH_HTML::li( |
|
| 720 | + EEH_HTML::span( |
|
| 721 | + '', |
|
| 722 | + '', |
|
| 723 | + 'dashicons dashicons-marker ee-icon-size-16 orange-text' |
|
| 724 | + ) |
|
| 725 | + . EEH_HTML::span($registration->event()->name(), '', 'orange-text') |
|
| 726 | + ); |
|
| 727 | + } |
|
| 728 | + } |
|
| 729 | + return new EE_Form_Section_Proper( |
|
| 730 | + array( |
|
| 731 | + 'layout_strategy' => new EE_Template_Layout( |
|
| 732 | + array( |
|
| 733 | + 'layout_template_file' => SPCO_REG_STEPS_PATH |
|
| 734 | + . $this->_slug |
|
| 735 | + . DS |
|
| 736 | + . 'events_requiring_pre_approval.template.php', // layout_template |
|
| 737 | + 'template_args' => apply_filters( |
|
| 738 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___sold_out_events__template_args', |
|
| 739 | + array( |
|
| 740 | + 'events_requiring_pre_approval' => implode('', $events_requiring_pre_approval), |
|
| 741 | + 'events_requiring_pre_approval_msg' => apply_filters( |
|
| 742 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___events_requiring_pre_approval__events_requiring_pre_approval_msg', |
|
| 743 | + esc_html__( |
|
| 744 | + 'The following events do not require payment at this time and will not be billed during this transaction. Billing will only occur after the attendee has been approved by the event organizer. You will be notified when your registration has been processed. If this is a free event, then no billing will occur.', |
|
| 745 | + 'event_espresso' |
|
| 746 | + ) |
|
| 747 | + ), |
|
| 748 | + ) |
|
| 749 | + ), |
|
| 750 | + ) |
|
| 751 | + ), |
|
| 752 | + ) |
|
| 753 | + ); |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + |
|
| 757 | + /** |
|
| 758 | + * _no_payment_required |
|
| 759 | + * |
|
| 760 | + * @param \EE_Event[] $registrations_for_free_events |
|
| 761 | + * @return \EE_Form_Section_Proper |
|
| 762 | + * @throws \EE_Error |
|
| 763 | + */ |
|
| 764 | + private function _no_payment_required($registrations_for_free_events = array()) |
|
| 765 | + { |
|
| 766 | + // set some defaults |
|
| 767 | + $this->checkout->selected_method_of_payment = 'no_payment_required'; |
|
| 768 | + // generate no_payment_required form |
|
| 769 | + return new EE_Form_Section_Proper( |
|
| 770 | + array( |
|
| 771 | + 'layout_strategy' => new EE_Template_Layout( |
|
| 772 | + array( |
|
| 773 | + 'layout_template_file' => SPCO_REG_STEPS_PATH |
|
| 774 | + . $this->_slug |
|
| 775 | + . DS |
|
| 776 | + . 'no_payment_required.template.php', // layout_template |
|
| 777 | + 'template_args' => apply_filters( |
|
| 778 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___no_payment_required__template_args', |
|
| 779 | + array( |
|
| 780 | + 'revisit' => $this->checkout->revisit, |
|
| 781 | + 'registrations' => array(), |
|
| 782 | + 'ticket_count' => array(), |
|
| 783 | + 'registrations_for_free_events' => $registrations_for_free_events, |
|
| 784 | + 'no_payment_required_msg' => EEH_HTML::p( |
|
| 785 | + esc_html__('This is a free event, so no billing will occur.', 'event_espresso') |
|
| 786 | + ), |
|
| 787 | + ) |
|
| 788 | + ), |
|
| 789 | + ) |
|
| 790 | + ), |
|
| 791 | + ) |
|
| 792 | + ); |
|
| 793 | + } |
|
| 794 | + |
|
| 795 | + |
|
| 796 | + /** |
|
| 797 | + * _display_payment_options |
|
| 798 | + * |
|
| 799 | + * @param string $transaction_details |
|
| 800 | + * @return EE_Form_Section_Proper |
|
| 801 | + * @throws EE_Error |
|
| 802 | + * @throws InvalidArgumentException |
|
| 803 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 804 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 805 | + */ |
|
| 806 | + private function _display_payment_options($transaction_details = '') |
|
| 807 | + { |
|
| 808 | + // has method_of_payment been set by no-js user? |
|
| 809 | + $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(); |
|
| 810 | + // build payment options form |
|
| 811 | + return apply_filters( |
|
| 812 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__payment_options_form', |
|
| 813 | + new EE_Form_Section_Proper( |
|
| 814 | + array( |
|
| 815 | + 'subsections' => array( |
|
| 816 | + 'before_payment_options' => apply_filters( |
|
| 817 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__before_payment_options', |
|
| 818 | + new EE_Form_Section_Proper( |
|
| 819 | + array('layout_strategy' => new EE_Div_Per_Section_Layout()) |
|
| 820 | + ) |
|
| 821 | + ), |
|
| 822 | + 'payment_options' => $this->_setup_payment_options(), |
|
| 823 | + 'after_payment_options' => apply_filters( |
|
| 824 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__after_payment_options', |
|
| 825 | + new EE_Form_Section_Proper( |
|
| 826 | + array('layout_strategy' => new EE_Div_Per_Section_Layout()) |
|
| 827 | + ) |
|
| 828 | + ), |
|
| 829 | + ), |
|
| 830 | + 'layout_strategy' => new EE_Template_Layout( |
|
| 831 | + array( |
|
| 832 | + 'layout_template_file' => $this->_template, |
|
| 833 | + 'template_args' => apply_filters( |
|
| 834 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___display_payment_options__template_args', |
|
| 835 | + array( |
|
| 836 | + 'reg_count' => $this->line_item_display->total_items(), |
|
| 837 | + 'transaction_details' => $transaction_details, |
|
| 838 | + 'available_payment_methods' => array(), |
|
| 839 | + ) |
|
| 840 | + ), |
|
| 841 | + ) |
|
| 842 | + ), |
|
| 843 | + ) |
|
| 844 | + ) |
|
| 845 | + ); |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + |
|
| 849 | + /** |
|
| 850 | + * _extra_hidden_inputs |
|
| 851 | + * |
|
| 852 | + * @param bool $no_payment_required |
|
| 853 | + * @return \EE_Form_Section_Proper |
|
| 854 | + * @throws \EE_Error |
|
| 855 | + */ |
|
| 856 | + private function _extra_hidden_inputs($no_payment_required = true) |
|
| 857 | + { |
|
| 858 | + return new EE_Form_Section_Proper( |
|
| 859 | + array( |
|
| 860 | + 'html_id' => 'ee-' . $this->slug() . '-extra-hidden-inputs', |
|
| 861 | + 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 862 | + 'subsections' => array( |
|
| 863 | + 'spco_no_payment_required' => new EE_Hidden_Input( |
|
| 864 | + array( |
|
| 865 | + 'normalization_strategy' => new EE_Boolean_Normalization(), |
|
| 866 | + 'html_name' => 'spco_no_payment_required', |
|
| 867 | + 'html_id' => 'spco-no-payment-required-payment_options', |
|
| 868 | + 'default' => $no_payment_required, |
|
| 869 | + ) |
|
| 870 | + ), |
|
| 871 | + 'spco_transaction_id' => new EE_Fixed_Hidden_Input( |
|
| 872 | + array( |
|
| 873 | + 'normalization_strategy' => new EE_Int_Normalization(), |
|
| 874 | + 'html_name' => 'spco_transaction_id', |
|
| 875 | + 'html_id' => 'spco-transaction-id', |
|
| 876 | + 'default' => $this->checkout->transaction->ID(), |
|
| 877 | + ) |
|
| 878 | + ), |
|
| 879 | + ), |
|
| 880 | + ) |
|
| 881 | + ); |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + |
|
| 885 | + /** |
|
| 886 | + * _apply_registration_payments_to_amount_owing |
|
| 887 | + * |
|
| 888 | + * @access protected |
|
| 889 | + * @param array $registrations |
|
| 890 | + * @throws EE_Error |
|
| 891 | + */ |
|
| 892 | + protected function _apply_registration_payments_to_amount_owing(array $registrations) |
|
| 893 | + { |
|
| 894 | + $payments = array(); |
|
| 895 | + foreach ($registrations as $registration) { |
|
| 896 | + if ($registration instanceof EE_Registration && $registration->owes_monies_and_can_pay()) { |
|
| 897 | + $payments += $registration->registration_payments(); |
|
| 898 | + } |
|
| 899 | + } |
|
| 900 | + if (! empty($payments)) { |
|
| 901 | + foreach ($payments as $payment) { |
|
| 902 | + if ($payment instanceof EE_Registration_Payment) { |
|
| 903 | + $this->checkout->amount_owing -= $payment->amount(); |
|
| 904 | + } |
|
| 905 | + } |
|
| 906 | + } |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + |
|
| 910 | + /** |
|
| 911 | + * _reset_selected_method_of_payment |
|
| 912 | + * |
|
| 913 | + * @access private |
|
| 914 | + * @param bool $force_reset |
|
| 915 | + * @return void |
|
| 916 | + * @throws InvalidArgumentException |
|
| 917 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 918 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 919 | + */ |
|
| 920 | + private function _reset_selected_method_of_payment($force_reset = false) |
|
| 921 | + { |
|
| 922 | + $reset_payment_method = $force_reset |
|
| 923 | + ? true |
|
| 924 | + : sanitize_text_field(EE_Registry::instance()->REQ->get('reset_payment_method', false)); |
|
| 925 | + if ($reset_payment_method) { |
|
| 926 | + $this->checkout->selected_method_of_payment = null; |
|
| 927 | + $this->checkout->payment_method = null; |
|
| 928 | + $this->checkout->billing_form = null; |
|
| 929 | + $this->_save_selected_method_of_payment(); |
|
| 930 | + } |
|
| 931 | + } |
|
| 932 | + |
|
| 933 | + |
|
| 934 | + /** |
|
| 935 | + * _save_selected_method_of_payment |
|
| 936 | + * stores the selected_method_of_payment in the session |
|
| 937 | + * so that it's available for all subsequent requests including AJAX |
|
| 938 | + * |
|
| 939 | + * @access private |
|
| 940 | + * @param string $selected_method_of_payment |
|
| 941 | + * @return void |
|
| 942 | + * @throws InvalidArgumentException |
|
| 943 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 944 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 945 | + */ |
|
| 946 | + private function _save_selected_method_of_payment($selected_method_of_payment = '') |
|
| 947 | + { |
|
| 948 | + $selected_method_of_payment = ! empty($selected_method_of_payment) |
|
| 949 | + ? $selected_method_of_payment |
|
| 950 | + : $this->checkout->selected_method_of_payment; |
|
| 951 | + EE_Registry::instance()->SSN->set_session_data( |
|
| 952 | + array('selected_method_of_payment' => $selected_method_of_payment) |
|
| 953 | + ); |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + |
|
| 957 | + /** |
|
| 958 | + * _setup_payment_options |
|
| 959 | + * |
|
| 960 | + * @return EE_Form_Section_Proper |
|
| 961 | + * @throws EE_Error |
|
| 962 | + * @throws InvalidArgumentException |
|
| 963 | + * @throws ReflectionException |
|
| 964 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 965 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 966 | + */ |
|
| 967 | + public function _setup_payment_options() |
|
| 968 | + { |
|
| 969 | + // load payment method classes |
|
| 970 | + $this->checkout->available_payment_methods = $this->_get_available_payment_methods(); |
|
| 971 | + if (empty($this->checkout->available_payment_methods)) { |
|
| 972 | + EE_Error::add_error( |
|
| 973 | + apply_filters( |
|
| 974 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___setup_payment_options__error_message_no_payment_methods', |
|
| 975 | + sprintf( |
|
| 976 | + esc_html__( |
|
| 977 | + 'Sorry, you cannot complete your purchase because a payment method is not active.%1$s Please contact %2$s for assistance and provide a description of the problem.', |
|
| 978 | + 'event_espresso' |
|
| 979 | + ), |
|
| 980 | + '<br>', |
|
| 981 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 982 | + ) |
|
| 983 | + ), |
|
| 984 | + __FILE__, |
|
| 985 | + __FUNCTION__, |
|
| 986 | + __LINE__ |
|
| 987 | + ); |
|
| 988 | + } |
|
| 989 | + // switch up header depending on number of available payment methods |
|
| 990 | + $payment_method_header = count($this->checkout->available_payment_methods) > 1 |
|
| 991 | + ? apply_filters( |
|
| 992 | + 'FHEE__registration_page_payment_options__method_of_payment_hdr', |
|
| 993 | + esc_html__('Please Select Your Method of Payment', 'event_espresso') |
|
| 994 | + ) |
|
| 995 | + : apply_filters( |
|
| 996 | + 'FHEE__registration_page_payment_options__method_of_payment_hdr', |
|
| 997 | + esc_html__('Method of Payment', 'event_espresso') |
|
| 998 | + ); |
|
| 999 | + $available_payment_methods = array( |
|
| 1000 | + // display the "Payment Method" header |
|
| 1001 | + 'payment_method_header' => new EE_Form_Section_HTML( |
|
| 1002 | + apply_filters( |
|
| 1003 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___setup_payment_options__payment_method_header', |
|
| 1004 | + EEH_HTML::h4($payment_method_header, 'method-of-payment-hdr'), |
|
| 1005 | + $payment_method_header |
|
| 1006 | + ) |
|
| 1007 | + ), |
|
| 1008 | + ); |
|
| 1009 | + // the list of actual payment methods ( invoice, paypal, etc ) in a ( slug => HTML ) format |
|
| 1010 | + $available_payment_method_options = array(); |
|
| 1011 | + $default_payment_method_option = array(); |
|
| 1012 | + // additional instructions to be displayed and hidden below payment methods (adding a clearing div to start) |
|
| 1013 | + $payment_methods_billing_info = array( |
|
| 1014 | + new EE_Form_Section_HTML( |
|
| 1015 | + EEH_HTML::div('<br />', '', '', 'clear:both;') |
|
| 1016 | + ), |
|
| 1017 | + ); |
|
| 1018 | + // loop through payment methods |
|
| 1019 | + foreach ($this->checkout->available_payment_methods as $payment_method) { |
|
| 1020 | + if ($payment_method instanceof EE_Payment_Method) { |
|
| 1021 | + $payment_method_button = EEH_HTML::img( |
|
| 1022 | + $payment_method->button_url(), |
|
| 1023 | + $payment_method->name(), |
|
| 1024 | + 'spco-payment-method-' . $payment_method->slug() . '-btn-img', |
|
| 1025 | + 'spco-payment-method-btn-img' |
|
| 1026 | + ); |
|
| 1027 | + // check if any payment methods are set as default |
|
| 1028 | + // if payment method is already selected OR nothing is selected and this payment method should be |
|
| 1029 | + // open_by_default |
|
| 1030 | + if (($this->checkout->selected_method_of_payment === $payment_method->slug()) |
|
| 1031 | + || (! $this->checkout->selected_method_of_payment && $payment_method->open_by_default()) |
|
| 1032 | + ) { |
|
| 1033 | + $this->checkout->selected_method_of_payment = $payment_method->slug(); |
|
| 1034 | + $this->_save_selected_method_of_payment(); |
|
| 1035 | + $default_payment_method_option[ $payment_method->slug() ] = $payment_method_button; |
|
| 1036 | + } else { |
|
| 1037 | + $available_payment_method_options[ $payment_method->slug() ] = $payment_method_button; |
|
| 1038 | + } |
|
| 1039 | + $payment_methods_billing_info[ $payment_method->slug( |
|
| 1040 | + ) . '-info' ] = $this->_payment_method_billing_info( |
|
| 1041 | + $payment_method |
|
| 1042 | + ); |
|
| 1043 | + } |
|
| 1044 | + } |
|
| 1045 | + // prepend available_payment_method_options with default_payment_method_option so that it appears first in list |
|
| 1046 | + // of PMs |
|
| 1047 | + $available_payment_method_options = $default_payment_method_option + $available_payment_method_options; |
|
| 1048 | + // now generate the actual form inputs |
|
| 1049 | + $available_payment_methods['available_payment_methods'] = $this->_available_payment_method_inputs( |
|
| 1050 | + $available_payment_method_options |
|
| 1051 | + ); |
|
| 1052 | + $available_payment_methods += $payment_methods_billing_info; |
|
| 1053 | + // build the available payment methods form |
|
| 1054 | + return new EE_Form_Section_Proper( |
|
| 1055 | + array( |
|
| 1056 | + 'html_id' => 'spco-available-methods-of-payment-dv', |
|
| 1057 | + 'subsections' => $available_payment_methods, |
|
| 1058 | + 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 1059 | + ) |
|
| 1060 | + ); |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + |
|
| 1064 | + /** |
|
| 1065 | + * _get_available_payment_methods |
|
| 1066 | + * |
|
| 1067 | + * @return EE_Payment_Method[] |
|
| 1068 | + * @throws EE_Error |
|
| 1069 | + * @throws InvalidArgumentException |
|
| 1070 | + * @throws ReflectionException |
|
| 1071 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1072 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1073 | + */ |
|
| 1074 | + protected function _get_available_payment_methods() |
|
| 1075 | + { |
|
| 1076 | + if (! empty($this->checkout->available_payment_methods)) { |
|
| 1077 | + return $this->checkout->available_payment_methods; |
|
| 1078 | + } |
|
| 1079 | + $available_payment_methods = array(); |
|
| 1080 | + // load EEM_Payment_Method |
|
| 1081 | + EE_Registry::instance()->load_model('Payment_Method'); |
|
| 1082 | + /** @type EEM_Payment_Method $EEM_Payment_Method */ |
|
| 1083 | + $EEM_Payment_Method = EE_Registry::instance()->LIB->EEM_Payment_Method; |
|
| 1084 | + // get all active payment methods |
|
| 1085 | + $payment_methods = $EEM_Payment_Method->get_all_for_transaction( |
|
| 1086 | + $this->checkout->transaction, |
|
| 1087 | + EEM_Payment_Method::scope_cart |
|
| 1088 | + ); |
|
| 1089 | + foreach ($payment_methods as $payment_method) { |
|
| 1090 | + if ($payment_method instanceof EE_Payment_Method) { |
|
| 1091 | + $available_payment_methods[ $payment_method->slug() ] = $payment_method; |
|
| 1092 | + } |
|
| 1093 | + } |
|
| 1094 | + return $available_payment_methods; |
|
| 1095 | + } |
|
| 1096 | + |
|
| 1097 | + |
|
| 1098 | + /** |
|
| 1099 | + * _available_payment_method_inputs |
|
| 1100 | + * |
|
| 1101 | + * @access private |
|
| 1102 | + * @param array $available_payment_method_options |
|
| 1103 | + * @return \EE_Form_Section_Proper |
|
| 1104 | + */ |
|
| 1105 | + private function _available_payment_method_inputs($available_payment_method_options = array()) |
|
| 1106 | + { |
|
| 1107 | + // generate inputs |
|
| 1108 | + return new EE_Form_Section_Proper( |
|
| 1109 | + array( |
|
| 1110 | + 'html_id' => 'ee-available-payment-method-inputs', |
|
| 1111 | + 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 1112 | + 'subsections' => array( |
|
| 1113 | + '' => new EE_Radio_Button_Input( |
|
| 1114 | + $available_payment_method_options, |
|
| 1115 | + array( |
|
| 1116 | + 'html_name' => 'selected_method_of_payment', |
|
| 1117 | + 'html_class' => 'spco-payment-method', |
|
| 1118 | + 'default' => $this->checkout->selected_method_of_payment, |
|
| 1119 | + 'label_size' => 11, |
|
| 1120 | + 'enforce_label_size' => true, |
|
| 1121 | + ) |
|
| 1122 | + ), |
|
| 1123 | + ), |
|
| 1124 | + ) |
|
| 1125 | + ); |
|
| 1126 | + } |
|
| 1127 | + |
|
| 1128 | + |
|
| 1129 | + /** |
|
| 1130 | + * _payment_method_billing_info |
|
| 1131 | + * |
|
| 1132 | + * @access private |
|
| 1133 | + * @param EE_Payment_Method $payment_method |
|
| 1134 | + * @return EE_Form_Section_Proper |
|
| 1135 | + * @throws EE_Error |
|
| 1136 | + * @throws InvalidArgumentException |
|
| 1137 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1138 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1139 | + */ |
|
| 1140 | + private function _payment_method_billing_info(EE_Payment_Method $payment_method) |
|
| 1141 | + { |
|
| 1142 | + $currently_selected = $this->checkout->selected_method_of_payment === $payment_method->slug() |
|
| 1143 | + ? true |
|
| 1144 | + : false; |
|
| 1145 | + // generate the billing form for payment method |
|
| 1146 | + $billing_form = $currently_selected |
|
| 1147 | + ? $this->_get_billing_form_for_payment_method($payment_method) |
|
| 1148 | + : new EE_Form_Section_HTML(); |
|
| 1149 | + $this->checkout->billing_form = $currently_selected |
|
| 1150 | + ? $billing_form |
|
| 1151 | + : $this->checkout->billing_form; |
|
| 1152 | + // it's all in the details |
|
| 1153 | + $info_html = EEH_HTML::h3( |
|
| 1154 | + esc_html__('Important information regarding your payment', 'event_espresso'), |
|
| 1155 | + '', |
|
| 1156 | + 'spco-payment-method-hdr' |
|
| 1157 | + ); |
|
| 1158 | + // add some info regarding the step, either from what's saved in the admin, |
|
| 1159 | + // or a default string depending on whether the PM has a billing form or not |
|
| 1160 | + if ($payment_method->description()) { |
|
| 1161 | + $payment_method_info = $payment_method->description(); |
|
| 1162 | + } elseif ($billing_form instanceof EE_Billing_Info_Form) { |
|
| 1163 | + $payment_method_info = sprintf( |
|
| 1164 | + esc_html__( |
|
| 1165 | + 'Please provide the following billing information, then click the "%1$s" button below in order to proceed.', |
|
| 1166 | + 'event_espresso' |
|
| 1167 | + ), |
|
| 1168 | + $this->submit_button_text() |
|
| 1169 | + ); |
|
| 1170 | + } else { |
|
| 1171 | + $payment_method_info = sprintf( |
|
| 1172 | + esc_html__('Please click the "%1$s" button below in order to proceed.', 'event_espresso'), |
|
| 1173 | + $this->submit_button_text() |
|
| 1174 | + ); |
|
| 1175 | + } |
|
| 1176 | + $info_html .= EEH_HTML::div( |
|
| 1177 | + apply_filters( |
|
| 1178 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___payment_method_billing_info__payment_method_info', |
|
| 1179 | + $payment_method_info |
|
| 1180 | + ), |
|
| 1181 | + '', |
|
| 1182 | + 'spco-payment-method-desc ee-attention' |
|
| 1183 | + ); |
|
| 1184 | + return new EE_Form_Section_Proper( |
|
| 1185 | + array( |
|
| 1186 | + 'html_id' => 'spco-payment-method-info-' . $payment_method->slug(), |
|
| 1187 | + 'html_class' => 'spco-payment-method-info-dv', |
|
| 1188 | + // only display the selected or default PM |
|
| 1189 | + 'html_style' => $currently_selected ? '' : 'display:none;', |
|
| 1190 | + 'layout_strategy' => new EE_Div_Per_Section_Layout(), |
|
| 1191 | + 'subsections' => array( |
|
| 1192 | + 'info' => new EE_Form_Section_HTML($info_html), |
|
| 1193 | + 'billing_form' => $currently_selected ? $billing_form : new EE_Form_Section_HTML(), |
|
| 1194 | + ), |
|
| 1195 | + ) |
|
| 1196 | + ); |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + |
|
| 1200 | + /** |
|
| 1201 | + * get_billing_form_html_for_payment_method |
|
| 1202 | + * |
|
| 1203 | + * @access public |
|
| 1204 | + * @return string |
|
| 1205 | + * @throws EE_Error |
|
| 1206 | + * @throws InvalidArgumentException |
|
| 1207 | + * @throws ReflectionException |
|
| 1208 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1209 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1210 | + */ |
|
| 1211 | + public function get_billing_form_html_for_payment_method() |
|
| 1212 | + { |
|
| 1213 | + // how have they chosen to pay? |
|
| 1214 | + $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true); |
|
| 1215 | + $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment(); |
|
| 1216 | + if (! $this->checkout->payment_method instanceof EE_Payment_Method) { |
|
| 1217 | + return false; |
|
| 1218 | + } |
|
| 1219 | + if (apply_filters( |
|
| 1220 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success', |
|
| 1221 | + false |
|
| 1222 | + )) { |
|
| 1223 | + EE_Error::add_success( |
|
| 1224 | + apply_filters( |
|
| 1225 | + 'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method', |
|
| 1226 | + sprintf( |
|
| 1227 | + esc_html__( |
|
| 1228 | + 'You have selected "%s" as your method of payment. Please note the important payment information below.', |
|
| 1229 | + 'event_espresso' |
|
| 1230 | + ), |
|
| 1231 | + $this->checkout->payment_method->name() |
|
| 1232 | + ) |
|
| 1233 | + ) |
|
| 1234 | + ); |
|
| 1235 | + } |
|
| 1236 | + // now generate billing form for selected method of payment |
|
| 1237 | + $payment_method_billing_form = $this->_get_billing_form_for_payment_method($this->checkout->payment_method); |
|
| 1238 | + // fill form with attendee info if applicable |
|
| 1239 | + if ($payment_method_billing_form instanceof EE_Billing_Attendee_Info_Form |
|
| 1240 | + && $this->checkout->transaction_has_primary_registrant() |
|
| 1241 | + ) { |
|
| 1242 | + $payment_method_billing_form->populate_from_attendee( |
|
| 1243 | + $this->checkout->transaction->primary_registration()->attendee() |
|
| 1244 | + ); |
|
| 1245 | + } |
|
| 1246 | + // and debug content |
|
| 1247 | + if ($payment_method_billing_form instanceof EE_Billing_Info_Form |
|
| 1248 | + && $this->checkout->payment_method->type_obj() instanceof EE_PMT_Base |
|
| 1249 | + ) { |
|
| 1250 | + $payment_method_billing_form = |
|
| 1251 | + $this->checkout->payment_method->type_obj()->apply_billing_form_debug_settings( |
|
| 1252 | + $payment_method_billing_form |
|
| 1253 | + ); |
|
| 1254 | + } |
|
| 1255 | + $billing_info = $payment_method_billing_form instanceof EE_Form_Section_Proper |
|
| 1256 | + ? $payment_method_billing_form->get_html() |
|
| 1257 | + : ''; |
|
| 1258 | + $this->checkout->json_response->set_return_data(array('payment_method_info' => $billing_info)); |
|
| 1259 | + // localize validation rules for main form |
|
| 1260 | + $this->checkout->current_step->reg_form->localize_validation_rules(); |
|
| 1261 | + $this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization()); |
|
| 1262 | + return true; |
|
| 1263 | + } |
|
| 1264 | + |
|
| 1265 | + |
|
| 1266 | + /** |
|
| 1267 | + * _get_billing_form_for_payment_method |
|
| 1268 | + * |
|
| 1269 | + * @access private |
|
| 1270 | + * @param EE_Payment_Method $payment_method |
|
| 1271 | + * @return EE_Billing_Info_Form|EE_Form_Section_HTML |
|
| 1272 | + * @throws EE_Error |
|
| 1273 | + * @throws InvalidArgumentException |
|
| 1274 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1275 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1276 | + */ |
|
| 1277 | + private function _get_billing_form_for_payment_method(EE_Payment_Method $payment_method) |
|
| 1278 | + { |
|
| 1279 | + $billing_form = $payment_method->type_obj()->billing_form( |
|
| 1280 | + $this->checkout->transaction, |
|
| 1281 | + array('amount_owing' => $this->checkout->amount_owing) |
|
| 1282 | + ); |
|
| 1283 | + if ($billing_form instanceof EE_Billing_Info_Form) { |
|
| 1284 | + if (apply_filters( |
|
| 1285 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success', |
|
| 1286 | + false |
|
| 1287 | + ) |
|
| 1288 | + && EE_Registry::instance()->REQ->is_set('payment_method') |
|
| 1289 | + ) { |
|
| 1290 | + EE_Error::add_success( |
|
| 1291 | + apply_filters( |
|
| 1292 | + 'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method', |
|
| 1293 | + sprintf( |
|
| 1294 | + esc_html__( |
|
| 1295 | + 'You have selected "%s" as your method of payment. Please note the important payment information below.', |
|
| 1296 | + 'event_espresso' |
|
| 1297 | + ), |
|
| 1298 | + $payment_method->name() |
|
| 1299 | + ) |
|
| 1300 | + ) |
|
| 1301 | + ); |
|
| 1302 | + } |
|
| 1303 | + return apply_filters( |
|
| 1304 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___get_billing_form_for_payment_method__billing_form', |
|
| 1305 | + $billing_form, |
|
| 1306 | + $payment_method |
|
| 1307 | + ); |
|
| 1308 | + } |
|
| 1309 | + // no actual billing form, so return empty HTML form section |
|
| 1310 | + return new EE_Form_Section_HTML(); |
|
| 1311 | + } |
|
| 1312 | + |
|
| 1313 | + |
|
| 1314 | + /** |
|
| 1315 | + * _get_selected_method_of_payment |
|
| 1316 | + * |
|
| 1317 | + * @access private |
|
| 1318 | + * @param boolean $required whether to throw an error if the "selected_method_of_payment" |
|
| 1319 | + * is not found in the incoming request |
|
| 1320 | + * @param string $request_param |
|
| 1321 | + * @return NULL|string |
|
| 1322 | + * @throws EE_Error |
|
| 1323 | + * @throws InvalidArgumentException |
|
| 1324 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1325 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1326 | + */ |
|
| 1327 | + private function _get_selected_method_of_payment( |
|
| 1328 | + $required = false, |
|
| 1329 | + $request_param = 'selected_method_of_payment' |
|
| 1330 | + ) { |
|
| 1331 | + // is selected_method_of_payment set in the request ? |
|
| 1332 | + $selected_method_of_payment = EE_Registry::instance()->REQ->get($request_param, false); |
|
| 1333 | + if ($selected_method_of_payment) { |
|
| 1334 | + // sanitize it |
|
| 1335 | + $selected_method_of_payment = is_array($selected_method_of_payment) |
|
| 1336 | + ? array_shift($selected_method_of_payment) |
|
| 1337 | + : $selected_method_of_payment; |
|
| 1338 | + $selected_method_of_payment = sanitize_text_field($selected_method_of_payment); |
|
| 1339 | + // store it in the session so that it's available for all subsequent requests including AJAX |
|
| 1340 | + $this->_save_selected_method_of_payment($selected_method_of_payment); |
|
| 1341 | + } else { |
|
| 1342 | + // or is is set in the session ? |
|
| 1343 | + $selected_method_of_payment = EE_Registry::instance()->SSN->get_session_data( |
|
| 1344 | + 'selected_method_of_payment' |
|
| 1345 | + ); |
|
| 1346 | + } |
|
| 1347 | + // do ya really really gotta have it? |
|
| 1348 | + if (empty($selected_method_of_payment) && $required) { |
|
| 1349 | + EE_Error::add_error( |
|
| 1350 | + sprintf( |
|
| 1351 | + esc_html__( |
|
| 1352 | + 'The selected method of payment could not be determined.%sPlease ensure that you have selected one before proceeding.%sIf you continue to experience difficulties, then refresh your browser and try again, or contact %s for assistance.', |
|
| 1353 | + 'event_espresso' |
|
| 1354 | + ), |
|
| 1355 | + '<br/>', |
|
| 1356 | + '<br/>', |
|
| 1357 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 1358 | + ), |
|
| 1359 | + __FILE__, |
|
| 1360 | + __FUNCTION__, |
|
| 1361 | + __LINE__ |
|
| 1362 | + ); |
|
| 1363 | + return null; |
|
| 1364 | + } |
|
| 1365 | + return $selected_method_of_payment; |
|
| 1366 | + } |
|
| 1367 | + |
|
| 1368 | + |
|
| 1369 | + |
|
| 1370 | + |
|
| 1371 | + |
|
| 1372 | + |
|
| 1373 | + /********************************************************************************************************/ |
|
| 1374 | + /*********************************** SWITCH PAYMENT METHOD ************************************/ |
|
| 1375 | + /********************************************************************************************************/ |
|
| 1376 | + /** |
|
| 1377 | + * switch_payment_method |
|
| 1378 | + * |
|
| 1379 | + * @access public |
|
| 1380 | + * @return string |
|
| 1381 | + * @throws EE_Error |
|
| 1382 | + * @throws InvalidArgumentException |
|
| 1383 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1384 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1385 | + */ |
|
| 1386 | + public function switch_payment_method() |
|
| 1387 | + { |
|
| 1388 | + if (! $this->_verify_payment_method_is_set()) { |
|
| 1389 | + return false; |
|
| 1390 | + } |
|
| 1391 | + if (apply_filters( |
|
| 1392 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options__registration_checkout__selected_payment_method__display_success', |
|
| 1393 | + false |
|
| 1394 | + )) { |
|
| 1395 | + EE_Error::add_success( |
|
| 1396 | + apply_filters( |
|
| 1397 | + 'FHEE__Single_Page_Checkout__registration_checkout__selected_payment_method', |
|
| 1398 | + sprintf( |
|
| 1399 | + esc_html__( |
|
| 1400 | + 'You have selected "%s" as your method of payment. Please note the important payment information below.', |
|
| 1401 | + 'event_espresso' |
|
| 1402 | + ), |
|
| 1403 | + $this->checkout->payment_method->name() |
|
| 1404 | + ) |
|
| 1405 | + ) |
|
| 1406 | + ); |
|
| 1407 | + } |
|
| 1408 | + // generate billing form for selected method of payment if it hasn't been done already |
|
| 1409 | + if ($this->checkout->payment_method->type_obj()->has_billing_form()) { |
|
| 1410 | + $this->checkout->billing_form = $this->_get_billing_form_for_payment_method( |
|
| 1411 | + $this->checkout->payment_method |
|
| 1412 | + ); |
|
| 1413 | + } |
|
| 1414 | + // fill form with attendee info if applicable |
|
| 1415 | + if (apply_filters( |
|
| 1416 | + 'FHEE__populate_billing_form_fields_from_attendee', |
|
| 1417 | + ( |
|
| 1418 | + $this->checkout->billing_form instanceof EE_Billing_Attendee_Info_Form |
|
| 1419 | + && $this->checkout->transaction_has_primary_registrant() |
|
| 1420 | + ), |
|
| 1421 | + $this->checkout->billing_form, |
|
| 1422 | + $this->checkout->transaction |
|
| 1423 | + ) |
|
| 1424 | + ) { |
|
| 1425 | + $this->checkout->billing_form->populate_from_attendee( |
|
| 1426 | + $this->checkout->transaction->primary_registration()->attendee() |
|
| 1427 | + ); |
|
| 1428 | + } |
|
| 1429 | + // and debug content |
|
| 1430 | + if ($this->checkout->billing_form instanceof EE_Billing_Info_Form |
|
| 1431 | + && $this->checkout->payment_method->type_obj() instanceof EE_PMT_Base |
|
| 1432 | + ) { |
|
| 1433 | + $this->checkout->billing_form = |
|
| 1434 | + $this->checkout->payment_method->type_obj()->apply_billing_form_debug_settings( |
|
| 1435 | + $this->checkout->billing_form |
|
| 1436 | + ); |
|
| 1437 | + } |
|
| 1438 | + // get html and validation rules for form |
|
| 1439 | + if ($this->checkout->billing_form instanceof EE_Form_Section_Proper) { |
|
| 1440 | + $this->checkout->json_response->set_return_data( |
|
| 1441 | + array('payment_method_info' => $this->checkout->billing_form->get_html()) |
|
| 1442 | + ); |
|
| 1443 | + // localize validation rules for main form |
|
| 1444 | + $this->checkout->billing_form->localize_validation_rules(true); |
|
| 1445 | + $this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization()); |
|
| 1446 | + } else { |
|
| 1447 | + $this->checkout->json_response->set_return_data(array('payment_method_info' => '')); |
|
| 1448 | + } |
|
| 1449 | + // prevents advancement to next step |
|
| 1450 | + $this->checkout->continue_reg = false; |
|
| 1451 | + return true; |
|
| 1452 | + } |
|
| 1453 | + |
|
| 1454 | + |
|
| 1455 | + /** |
|
| 1456 | + * _verify_payment_method_is_set |
|
| 1457 | + * |
|
| 1458 | + * @return bool |
|
| 1459 | + * @throws EE_Error |
|
| 1460 | + * @throws InvalidArgumentException |
|
| 1461 | + * @throws ReflectionException |
|
| 1462 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1463 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1464 | + */ |
|
| 1465 | + protected function _verify_payment_method_is_set() |
|
| 1466 | + { |
|
| 1467 | + // generate billing form for selected method of payment if it hasn't been done already |
|
| 1468 | + if (empty($this->checkout->selected_method_of_payment)) { |
|
| 1469 | + // how have they chosen to pay? |
|
| 1470 | + $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true); |
|
| 1471 | + } else { |
|
| 1472 | + // choose your own adventure based on method_of_payment |
|
| 1473 | + switch ($this->checkout->selected_method_of_payment) { |
|
| 1474 | + case 'events_sold_out': |
|
| 1475 | + EE_Error::add_attention( |
|
| 1476 | + apply_filters( |
|
| 1477 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__sold_out_events_msg', |
|
| 1478 | + esc_html__( |
|
| 1479 | + 'It appears that the event you were about to make a payment for has sold out since this form first loaded. Please contact the event administrator if you believe this is an error.', |
|
| 1480 | + 'event_espresso' |
|
| 1481 | + ) |
|
| 1482 | + ), |
|
| 1483 | + __FILE__, |
|
| 1484 | + __FUNCTION__, |
|
| 1485 | + __LINE__ |
|
| 1486 | + ); |
|
| 1487 | + return false; |
|
| 1488 | + break; |
|
| 1489 | + case 'payments_closed': |
|
| 1490 | + EE_Error::add_attention( |
|
| 1491 | + apply_filters( |
|
| 1492 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__payments_closed_msg', |
|
| 1493 | + esc_html__( |
|
| 1494 | + 'It appears that the event you were about to make a payment for is not accepting payments at this time. Please contact the event administrator if you believe this is an error.', |
|
| 1495 | + 'event_espresso' |
|
| 1496 | + ) |
|
| 1497 | + ), |
|
| 1498 | + __FILE__, |
|
| 1499 | + __FUNCTION__, |
|
| 1500 | + __LINE__ |
|
| 1501 | + ); |
|
| 1502 | + return false; |
|
| 1503 | + break; |
|
| 1504 | + case 'no_payment_required': |
|
| 1505 | + EE_Error::add_attention( |
|
| 1506 | + apply_filters( |
|
| 1507 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___verify_payment_method_is_set__no_payment_required_msg', |
|
| 1508 | + esc_html__( |
|
| 1509 | + 'It appears that the event you were about to make a payment for does not require payment. Please contact the event administrator if you believe this is an error.', |
|
| 1510 | + 'event_espresso' |
|
| 1511 | + ) |
|
| 1512 | + ), |
|
| 1513 | + __FILE__, |
|
| 1514 | + __FUNCTION__, |
|
| 1515 | + __LINE__ |
|
| 1516 | + ); |
|
| 1517 | + return false; |
|
| 1518 | + break; |
|
| 1519 | + default: |
|
| 1520 | + } |
|
| 1521 | + } |
|
| 1522 | + // verify payment method |
|
| 1523 | + if (! $this->checkout->payment_method instanceof EE_Payment_Method) { |
|
| 1524 | + // get payment method for selected method of payment |
|
| 1525 | + $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment(); |
|
| 1526 | + } |
|
| 1527 | + return $this->checkout->payment_method instanceof EE_Payment_Method ? true : false; |
|
| 1528 | + } |
|
| 1529 | + |
|
| 1530 | + |
|
| 1531 | + |
|
| 1532 | + /********************************************************************************************************/ |
|
| 1533 | + /*************************************** SAVE PAYER DETAILS ****************************************/ |
|
| 1534 | + /********************************************************************************************************/ |
|
| 1535 | + /** |
|
| 1536 | + * save_payer_details_via_ajax |
|
| 1537 | + * |
|
| 1538 | + * @return void |
|
| 1539 | + * @throws EE_Error |
|
| 1540 | + * @throws InvalidArgumentException |
|
| 1541 | + * @throws ReflectionException |
|
| 1542 | + * @throws RuntimeException |
|
| 1543 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1544 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1545 | + */ |
|
| 1546 | + public function save_payer_details_via_ajax() |
|
| 1547 | + { |
|
| 1548 | + if (! $this->_verify_payment_method_is_set()) { |
|
| 1549 | + return; |
|
| 1550 | + } |
|
| 1551 | + // generate billing form for selected method of payment if it hasn't been done already |
|
| 1552 | + if ($this->checkout->payment_method->type_obj()->has_billing_form()) { |
|
| 1553 | + $this->checkout->billing_form = $this->_get_billing_form_for_payment_method( |
|
| 1554 | + $this->checkout->payment_method |
|
| 1555 | + ); |
|
| 1556 | + } |
|
| 1557 | + // generate primary attendee from payer info if applicable |
|
| 1558 | + if (! $this->checkout->transaction_has_primary_registrant()) { |
|
| 1559 | + $attendee = $this->_create_attendee_from_request_data(); |
|
| 1560 | + if ($attendee instanceof EE_Attendee) { |
|
| 1561 | + foreach ($this->checkout->transaction->registrations() as $registration) { |
|
| 1562 | + if ($registration->is_primary_registrant()) { |
|
| 1563 | + $this->checkout->primary_attendee_obj = $attendee; |
|
| 1564 | + $registration->_add_relation_to($attendee, 'Attendee'); |
|
| 1565 | + $registration->set_attendee_id($attendee->ID()); |
|
| 1566 | + $registration->update_cache_after_object_save('Attendee', $attendee); |
|
| 1567 | + } |
|
| 1568 | + } |
|
| 1569 | + } |
|
| 1570 | + } |
|
| 1571 | + } |
|
| 1572 | + |
|
| 1573 | + |
|
| 1574 | + /** |
|
| 1575 | + * create_attendee_from_request_data |
|
| 1576 | + * uses info from alternate GET or POST data (such as AJAX) to create a new attendee |
|
| 1577 | + * |
|
| 1578 | + * @return EE_Attendee |
|
| 1579 | + * @throws EE_Error |
|
| 1580 | + * @throws InvalidArgumentException |
|
| 1581 | + * @throws ReflectionException |
|
| 1582 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1583 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1584 | + */ |
|
| 1585 | + protected function _create_attendee_from_request_data() |
|
| 1586 | + { |
|
| 1587 | + // get State ID |
|
| 1588 | + $STA_ID = ! empty($_REQUEST['state']) ? sanitize_text_field($_REQUEST['state']) : ''; |
|
| 1589 | + if (! empty($STA_ID)) { |
|
| 1590 | + // can we get state object from name ? |
|
| 1591 | + EE_Registry::instance()->load_model('State'); |
|
| 1592 | + $state = EEM_State::instance()->get_col(array(array('STA_name' => $STA_ID), 'limit' => 1), 'STA_ID'); |
|
| 1593 | + $STA_ID = is_array($state) && ! empty($state) ? reset($state) : $STA_ID; |
|
| 1594 | + } |
|
| 1595 | + // get Country ISO |
|
| 1596 | + $CNT_ISO = ! empty($_REQUEST['country']) ? sanitize_text_field($_REQUEST['country']) : ''; |
|
| 1597 | + if (! empty($CNT_ISO)) { |
|
| 1598 | + // can we get country object from name ? |
|
| 1599 | + EE_Registry::instance()->load_model('Country'); |
|
| 1600 | + $country = EEM_Country::instance()->get_col( |
|
| 1601 | + array(array('CNT_name' => $CNT_ISO), 'limit' => 1), |
|
| 1602 | + 'CNT_ISO' |
|
| 1603 | + ); |
|
| 1604 | + $CNT_ISO = is_array($country) && ! empty($country) ? reset($country) : $CNT_ISO; |
|
| 1605 | + } |
|
| 1606 | + // grab attendee data |
|
| 1607 | + $attendee_data = array( |
|
| 1608 | + 'ATT_fname' => ! empty($_REQUEST['first_name']) ? sanitize_text_field($_REQUEST['first_name']) : '', |
|
| 1609 | + 'ATT_lname' => ! empty($_REQUEST['last_name']) ? sanitize_text_field($_REQUEST['last_name']) : '', |
|
| 1610 | + 'ATT_email' => ! empty($_REQUEST['email']) ? sanitize_email($_REQUEST['email']) : '', |
|
| 1611 | + 'ATT_address' => ! empty($_REQUEST['address']) ? sanitize_text_field($_REQUEST['address']) : '', |
|
| 1612 | + 'ATT_address2' => ! empty($_REQUEST['address2']) ? sanitize_text_field($_REQUEST['address2']) : '', |
|
| 1613 | + 'ATT_city' => ! empty($_REQUEST['city']) ? sanitize_text_field($_REQUEST['city']) : '', |
|
| 1614 | + 'STA_ID' => $STA_ID, |
|
| 1615 | + 'CNT_ISO' => $CNT_ISO, |
|
| 1616 | + 'ATT_zip' => ! empty($_REQUEST['zip']) ? sanitize_text_field($_REQUEST['zip']) : '', |
|
| 1617 | + 'ATT_phone' => ! empty($_REQUEST['phone']) ? sanitize_text_field($_REQUEST['phone']) : '', |
|
| 1618 | + ); |
|
| 1619 | + // validate the email address since it is the most important piece of info |
|
| 1620 | + if (empty($attendee_data['ATT_email']) || $attendee_data['ATT_email'] !== $_REQUEST['email']) { |
|
| 1621 | + EE_Error::add_error( |
|
| 1622 | + esc_html__('An invalid email address was submitted.', 'event_espresso'), |
|
| 1623 | + __FILE__, |
|
| 1624 | + __FUNCTION__, |
|
| 1625 | + __LINE__ |
|
| 1626 | + ); |
|
| 1627 | + } |
|
| 1628 | + // does this attendee already exist in the db ? we're searching using a combination of first name, last name, |
|
| 1629 | + // AND email address |
|
| 1630 | + if (! empty($attendee_data['ATT_fname']) |
|
| 1631 | + && ! empty($attendee_data['ATT_lname']) |
|
| 1632 | + && ! empty($attendee_data['ATT_email']) |
|
| 1633 | + ) { |
|
| 1634 | + $existing_attendee = EE_Registry::instance()->LIB->EEM_Attendee->find_existing_attendee( |
|
| 1635 | + array( |
|
| 1636 | + 'ATT_fname' => $attendee_data['ATT_fname'], |
|
| 1637 | + 'ATT_lname' => $attendee_data['ATT_lname'], |
|
| 1638 | + 'ATT_email' => $attendee_data['ATT_email'], |
|
| 1639 | + ) |
|
| 1640 | + ); |
|
| 1641 | + if ($existing_attendee instanceof EE_Attendee) { |
|
| 1642 | + return $existing_attendee; |
|
| 1643 | + } |
|
| 1644 | + } |
|
| 1645 | + // no existing attendee? kk let's create a new one |
|
| 1646 | + // kinda lame, but we need a first and last name to create an attendee, so use the email address if those |
|
| 1647 | + // don't exist |
|
| 1648 | + $attendee_data['ATT_fname'] = ! empty($attendee_data['ATT_fname']) |
|
| 1649 | + ? $attendee_data['ATT_fname'] |
|
| 1650 | + : $attendee_data['ATT_email']; |
|
| 1651 | + $attendee_data['ATT_lname'] = ! empty($attendee_data['ATT_lname']) |
|
| 1652 | + ? $attendee_data['ATT_lname'] |
|
| 1653 | + : $attendee_data['ATT_email']; |
|
| 1654 | + return EE_Attendee::new_instance($attendee_data); |
|
| 1655 | + } |
|
| 1656 | + |
|
| 1657 | + |
|
| 1658 | + |
|
| 1659 | + /********************************************************************************************************/ |
|
| 1660 | + /**************************************** PROCESS REG STEP *****************************************/ |
|
| 1661 | + /********************************************************************************************************/ |
|
| 1662 | + /** |
|
| 1663 | + * process_reg_step |
|
| 1664 | + * |
|
| 1665 | + * @return bool |
|
| 1666 | + * @throws EE_Error |
|
| 1667 | + * @throws InvalidArgumentException |
|
| 1668 | + * @throws ReflectionException |
|
| 1669 | + * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 1670 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1671 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1672 | + * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 1673 | + */ |
|
| 1674 | + public function process_reg_step() |
|
| 1675 | + { |
|
| 1676 | + // how have they chosen to pay? |
|
| 1677 | + $this->checkout->selected_method_of_payment = $this->checkout->transaction->is_free() |
|
| 1678 | + ? 'no_payment_required' |
|
| 1679 | + : $this->_get_selected_method_of_payment(true); |
|
| 1680 | + // choose your own adventure based on method_of_payment |
|
| 1681 | + switch ($this->checkout->selected_method_of_payment) { |
|
| 1682 | + case 'events_sold_out': |
|
| 1683 | + $this->checkout->redirect = true; |
|
| 1684 | + $this->checkout->redirect_url = $this->checkout->cancel_page_url; |
|
| 1685 | + $this->checkout->json_response->set_redirect_url($this->checkout->redirect_url); |
|
| 1686 | + // mark this reg step as completed |
|
| 1687 | + $this->set_completed(); |
|
| 1688 | + return false; |
|
| 1689 | + break; |
|
| 1690 | + |
|
| 1691 | + case 'payments_closed': |
|
| 1692 | + if (apply_filters( |
|
| 1693 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options__process_reg_step__payments_closed__display_success', |
|
| 1694 | + false |
|
| 1695 | + )) { |
|
| 1696 | + EE_Error::add_success( |
|
| 1697 | + esc_html__('no payment required at this time.', 'event_espresso'), |
|
| 1698 | + __FILE__, |
|
| 1699 | + __FUNCTION__, |
|
| 1700 | + __LINE__ |
|
| 1701 | + ); |
|
| 1702 | + } |
|
| 1703 | + // mark this reg step as completed |
|
| 1704 | + $this->set_completed(); |
|
| 1705 | + return true; |
|
| 1706 | + break; |
|
| 1707 | + |
|
| 1708 | + case 'no_payment_required': |
|
| 1709 | + if (apply_filters( |
|
| 1710 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options__process_reg_step__no_payment_required__display_success', |
|
| 1711 | + false |
|
| 1712 | + )) { |
|
| 1713 | + EE_Error::add_success( |
|
| 1714 | + esc_html__('no payment required.', 'event_espresso'), |
|
| 1715 | + __FILE__, |
|
| 1716 | + __FUNCTION__, |
|
| 1717 | + __LINE__ |
|
| 1718 | + ); |
|
| 1719 | + } |
|
| 1720 | + // mark this reg step as completed |
|
| 1721 | + $this->set_completed(); |
|
| 1722 | + return true; |
|
| 1723 | + break; |
|
| 1724 | + |
|
| 1725 | + default: |
|
| 1726 | + $registrations = EE_Registry::instance()->SSN->checkout()->transaction->registrations( |
|
| 1727 | + EE_Registry::instance()->SSN->checkout()->reg_cache_where_params |
|
| 1728 | + ); |
|
| 1729 | + $ejected_registrations = EE_SPCO_Reg_Step_Payment_Options::find_registrations_that_lost_their_space( |
|
| 1730 | + $registrations, |
|
| 1731 | + EE_Registry::instance()->SSN->checkout()->revisit |
|
| 1732 | + ); |
|
| 1733 | + // calculate difference between the two arrays |
|
| 1734 | + $registrations = array_diff($registrations, $ejected_registrations); |
|
| 1735 | + if (empty($registrations)) { |
|
| 1736 | + $this->_redirect_because_event_sold_out(); |
|
| 1737 | + return false; |
|
| 1738 | + } |
|
| 1739 | + $payment_successful = $this->_process_payment(); |
|
| 1740 | + if ($payment_successful) { |
|
| 1741 | + $this->checkout->continue_reg = true; |
|
| 1742 | + $this->_maybe_set_completed($this->checkout->payment_method); |
|
| 1743 | + } else { |
|
| 1744 | + $this->checkout->continue_reg = false; |
|
| 1745 | + } |
|
| 1746 | + return $payment_successful; |
|
| 1747 | + } |
|
| 1748 | + } |
|
| 1749 | + |
|
| 1750 | + |
|
| 1751 | + /** |
|
| 1752 | + * _redirect_because_event_sold_out |
|
| 1753 | + * |
|
| 1754 | + * @access protected |
|
| 1755 | + * @return void |
|
| 1756 | + */ |
|
| 1757 | + protected function _redirect_because_event_sold_out() |
|
| 1758 | + { |
|
| 1759 | + $this->checkout->continue_reg = false; |
|
| 1760 | + // set redirect URL |
|
| 1761 | + $this->checkout->redirect_url = add_query_arg( |
|
| 1762 | + array('e_reg_url_link' => $this->checkout->reg_url_link), |
|
| 1763 | + $this->checkout->current_step->reg_step_url() |
|
| 1764 | + ); |
|
| 1765 | + $this->checkout->json_response->set_redirect_url($this->checkout->redirect_url); |
|
| 1766 | + } |
|
| 1767 | + |
|
| 1768 | + |
|
| 1769 | + /** |
|
| 1770 | + * _maybe_set_completed |
|
| 1771 | + * |
|
| 1772 | + * @access protected |
|
| 1773 | + * @param \EE_Payment_Method $payment_method |
|
| 1774 | + * @return void |
|
| 1775 | + * @throws \EE_Error |
|
| 1776 | + */ |
|
| 1777 | + protected function _maybe_set_completed(EE_Payment_Method $payment_method) |
|
| 1778 | + { |
|
| 1779 | + switch ($payment_method->type_obj()->payment_occurs()) { |
|
| 1780 | + case EE_PMT_Base::offsite: |
|
| 1781 | + break; |
|
| 1782 | + case EE_PMT_Base::onsite: |
|
| 1783 | + case EE_PMT_Base::offline: |
|
| 1784 | + // mark this reg step as completed |
|
| 1785 | + $this->set_completed(); |
|
| 1786 | + break; |
|
| 1787 | + } |
|
| 1788 | + } |
|
| 1789 | + |
|
| 1790 | + |
|
| 1791 | + /** |
|
| 1792 | + * update_reg_step |
|
| 1793 | + * this is the final step after a user revisits the site to retry a payment |
|
| 1794 | + * |
|
| 1795 | + * @return bool |
|
| 1796 | + * @throws EE_Error |
|
| 1797 | + * @throws InvalidArgumentException |
|
| 1798 | + * @throws ReflectionException |
|
| 1799 | + * @throws \EventEspresso\core\exceptions\EntityNotFoundException |
|
| 1800 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1801 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1802 | + * @throws \EventEspresso\core\exceptions\InvalidStatusException |
|
| 1803 | + */ |
|
| 1804 | + public function update_reg_step() |
|
| 1805 | + { |
|
| 1806 | + $success = true; |
|
| 1807 | + // if payment required |
|
| 1808 | + if ($this->checkout->transaction->total() > 0) { |
|
| 1809 | + do_action( |
|
| 1810 | + 'AHEE__EE_Single_Page_Checkout__process_finalize_registration__before_gateway', |
|
| 1811 | + $this->checkout->transaction |
|
| 1812 | + ); |
|
| 1813 | + // attempt payment via payment method |
|
| 1814 | + $success = $this->process_reg_step(); |
|
| 1815 | + } |
|
| 1816 | + if ($success && ! $this->checkout->redirect) { |
|
| 1817 | + $this->checkout->cart->get_grand_total()->save_this_and_descendants_to_txn( |
|
| 1818 | + $this->checkout->transaction->ID() |
|
| 1819 | + ); |
|
| 1820 | + // set return URL |
|
| 1821 | + $this->checkout->redirect_url = add_query_arg( |
|
| 1822 | + array('e_reg_url_link' => $this->checkout->reg_url_link), |
|
| 1823 | + $this->checkout->thank_you_page_url |
|
| 1824 | + ); |
|
| 1825 | + } |
|
| 1826 | + return $success; |
|
| 1827 | + } |
|
| 1828 | + |
|
| 1829 | + |
|
| 1830 | + /** |
|
| 1831 | + * _process_payment |
|
| 1832 | + * |
|
| 1833 | + * @access private |
|
| 1834 | + * @return bool |
|
| 1835 | + * @throws EE_Error |
|
| 1836 | + * @throws InvalidArgumentException |
|
| 1837 | + * @throws ReflectionException |
|
| 1838 | + * @throws RuntimeException |
|
| 1839 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1840 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1841 | + */ |
|
| 1842 | + private function _process_payment() |
|
| 1843 | + { |
|
| 1844 | + // basically confirm that the event hasn't sold out since they hit the page |
|
| 1845 | + if (! $this->_last_second_ticket_verifications()) { |
|
| 1846 | + return false; |
|
| 1847 | + } |
|
| 1848 | + // ya gotta make a choice man |
|
| 1849 | + if (empty($this->checkout->selected_method_of_payment)) { |
|
| 1850 | + $this->checkout->json_response->set_plz_select_method_of_payment( |
|
| 1851 | + esc_html__('Please select a method of payment before proceeding.', 'event_espresso') |
|
| 1852 | + ); |
|
| 1853 | + return false; |
|
| 1854 | + } |
|
| 1855 | + // get EE_Payment_Method object |
|
| 1856 | + if (! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) { |
|
| 1857 | + return false; |
|
| 1858 | + } |
|
| 1859 | + // setup billing form |
|
| 1860 | + if ($this->checkout->payment_method->is_on_site()) { |
|
| 1861 | + $this->checkout->billing_form = $this->_get_billing_form_for_payment_method( |
|
| 1862 | + $this->checkout->payment_method |
|
| 1863 | + ); |
|
| 1864 | + // bad billing form ? |
|
| 1865 | + if (! $this->_billing_form_is_valid()) { |
|
| 1866 | + return false; |
|
| 1867 | + } |
|
| 1868 | + } |
|
| 1869 | + // ensure primary registrant has been fully processed |
|
| 1870 | + if (! $this->_setup_primary_registrant_prior_to_payment()) { |
|
| 1871 | + return false; |
|
| 1872 | + } |
|
| 1873 | + // if session is close to expiring (under 10 minutes by default) |
|
| 1874 | + if ((time() - EE_Registry::instance()->SSN->expiration()) < EE_Registry::instance()->SSN->extension()) { |
|
| 1875 | + // add some time to session expiration so that payment can be completed |
|
| 1876 | + EE_Registry::instance()->SSN->extend_expiration(); |
|
| 1877 | + } |
|
| 1878 | + /** @type EE_Transaction_Processor $transaction_processor */ |
|
| 1879 | + // $transaction_processor = EE_Registry::instance()->load_class( 'Transaction_Processor' ); |
|
| 1880 | + // in case a registrant leaves to an Off-Site Gateway and never returns, we want to approve any registrations |
|
| 1881 | + // for events with a default reg status of Approved |
|
| 1882 | + // $transaction_processor->toggle_registration_statuses_for_default_approved_events( |
|
| 1883 | + // $this->checkout->transaction, $this->checkout->reg_cache_where_params |
|
| 1884 | + // ); |
|
| 1885 | + // attempt payment |
|
| 1886 | + $payment = $this->_attempt_payment($this->checkout->payment_method); |
|
| 1887 | + // process results |
|
| 1888 | + $payment = $this->_validate_payment($payment); |
|
| 1889 | + $payment = $this->_post_payment_processing($payment); |
|
| 1890 | + // verify payment |
|
| 1891 | + if ($payment instanceof EE_Payment) { |
|
| 1892 | + // store that for later |
|
| 1893 | + $this->checkout->payment = $payment; |
|
| 1894 | + // we can also consider the TXN to not have been failed, so temporarily upgrade it's status to abandoned |
|
| 1895 | + $this->checkout->transaction->toggle_failed_transaction_status(); |
|
| 1896 | + $payment_status = $payment->status(); |
|
| 1897 | + if ($payment_status === EEM_Payment::status_id_approved |
|
| 1898 | + || $payment_status === EEM_Payment::status_id_pending |
|
| 1899 | + ) { |
|
| 1900 | + return true; |
|
| 1901 | + } else { |
|
| 1902 | + return false; |
|
| 1903 | + } |
|
| 1904 | + } elseif ($payment === true) { |
|
| 1905 | + // please note that offline payment methods will NOT make a payment, |
|
| 1906 | + // but instead just mark themselves as the PMD_ID on the transaction, and return true |
|
| 1907 | + $this->checkout->payment = $payment; |
|
| 1908 | + return true; |
|
| 1909 | + } |
|
| 1910 | + // where's my money? |
|
| 1911 | + return false; |
|
| 1912 | + } |
|
| 1913 | + |
|
| 1914 | + |
|
| 1915 | + /** |
|
| 1916 | + * _last_second_ticket_verifications |
|
| 1917 | + * |
|
| 1918 | + * @access public |
|
| 1919 | + * @return bool |
|
| 1920 | + * @throws EE_Error |
|
| 1921 | + */ |
|
| 1922 | + protected function _last_second_ticket_verifications() |
|
| 1923 | + { |
|
| 1924 | + // don't bother re-validating if not a return visit |
|
| 1925 | + if (! $this->checkout->revisit) { |
|
| 1926 | + return true; |
|
| 1927 | + } |
|
| 1928 | + $registrations = $this->checkout->transaction->registrations(); |
|
| 1929 | + if (empty($registrations)) { |
|
| 1930 | + return false; |
|
| 1931 | + } |
|
| 1932 | + foreach ($registrations as $registration) { |
|
| 1933 | + if ($registration instanceof EE_Registration && ! $registration->is_approved()) { |
|
| 1934 | + $event = $registration->event_obj(); |
|
| 1935 | + if ($event instanceof EE_Event && $event->is_sold_out(true)) { |
|
| 1936 | + EE_Error::add_error( |
|
| 1937 | + apply_filters( |
|
| 1938 | + 'FHEE__EE_SPCO_Reg_Step_Payment_Options___last_second_ticket_verifications__sold_out_events_msg', |
|
| 1939 | + sprintf( |
|
| 1940 | + esc_html__( |
|
| 1941 | + 'It appears that the %1$s event that you were about to make a payment for has sold out since you first registered and/or arrived at this page. Please refresh the page and try again. If you have already made a partial payment towards this event, please contact the event administrator for a refund.', |
|
| 1942 | + 'event_espresso' |
|
| 1943 | + ), |
|
| 1944 | + $event->name() |
|
| 1945 | + ) |
|
| 1946 | + ), |
|
| 1947 | + __FILE__, |
|
| 1948 | + __FUNCTION__, |
|
| 1949 | + __LINE__ |
|
| 1950 | + ); |
|
| 1951 | + return false; |
|
| 1952 | + } |
|
| 1953 | + } |
|
| 1954 | + } |
|
| 1955 | + return true; |
|
| 1956 | + } |
|
| 1957 | + |
|
| 1958 | + |
|
| 1959 | + /** |
|
| 1960 | + * redirect_form |
|
| 1961 | + * |
|
| 1962 | + * @access public |
|
| 1963 | + * @return bool |
|
| 1964 | + * @throws EE_Error |
|
| 1965 | + * @throws InvalidArgumentException |
|
| 1966 | + * @throws ReflectionException |
|
| 1967 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 1968 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 1969 | + */ |
|
| 1970 | + public function redirect_form() |
|
| 1971 | + { |
|
| 1972 | + $payment_method_billing_info = $this->_payment_method_billing_info( |
|
| 1973 | + $this->_get_payment_method_for_selected_method_of_payment() |
|
| 1974 | + ); |
|
| 1975 | + $html = $payment_method_billing_info->get_html(); |
|
| 1976 | + $html .= $this->checkout->redirect_form; |
|
| 1977 | + EE_Registry::instance()->REQ->add_output($html); |
|
| 1978 | + return true; |
|
| 1979 | + } |
|
| 1980 | + |
|
| 1981 | + |
|
| 1982 | + /** |
|
| 1983 | + * _billing_form_is_valid |
|
| 1984 | + * |
|
| 1985 | + * @access private |
|
| 1986 | + * @return bool |
|
| 1987 | + * @throws \EE_Error |
|
| 1988 | + */ |
|
| 1989 | + private function _billing_form_is_valid() |
|
| 1990 | + { |
|
| 1991 | + if (! $this->checkout->payment_method->type_obj()->has_billing_form()) { |
|
| 1992 | + return true; |
|
| 1993 | + } |
|
| 1994 | + if ($this->checkout->billing_form instanceof EE_Billing_Info_Form) { |
|
| 1995 | + if ($this->checkout->billing_form->was_submitted()) { |
|
| 1996 | + $this->checkout->billing_form->receive_form_submission(); |
|
| 1997 | + if ($this->checkout->billing_form->is_valid()) { |
|
| 1998 | + return true; |
|
| 1999 | + } |
|
| 2000 | + $validation_errors = $this->checkout->billing_form->get_validation_errors_accumulated(); |
|
| 2001 | + $error_strings = array(); |
|
| 2002 | + foreach ($validation_errors as $validation_error) { |
|
| 2003 | + if ($validation_error instanceof EE_Validation_Error) { |
|
| 2004 | + $form_section = $validation_error->get_form_section(); |
|
| 2005 | + if ($form_section instanceof EE_Form_Input_Base) { |
|
| 2006 | + $label = $form_section->html_label_text(); |
|
| 2007 | + } elseif ($form_section instanceof EE_Form_Section_Base) { |
|
| 2008 | + $label = $form_section->name(); |
|
| 2009 | + } else { |
|
| 2010 | + $label = esc_html__('Validation Error', 'event_espresso'); |
|
| 2011 | + } |
|
| 2012 | + $error_strings[] = sprintf('%1$s: %2$s', $label, $validation_error->getMessage()); |
|
| 2013 | + } |
|
| 2014 | + } |
|
| 2015 | + EE_Error::add_error( |
|
| 2016 | + sprintf( |
|
| 2017 | + esc_html__( |
|
| 2018 | + 'One or more billing form inputs are invalid and require correction before proceeding. %1$s %2$s', |
|
| 2019 | + 'event_espresso' |
|
| 2020 | + ), |
|
| 2021 | + '<br/>', |
|
| 2022 | + implode('<br/>', $error_strings) |
|
| 2023 | + ), |
|
| 2024 | + __FILE__, |
|
| 2025 | + __FUNCTION__, |
|
| 2026 | + __LINE__ |
|
| 2027 | + ); |
|
| 2028 | + } else { |
|
| 2029 | + EE_Error::add_error( |
|
| 2030 | + esc_html__( |
|
| 2031 | + 'The billing form was not submitted or something prevented it\'s submission.', |
|
| 2032 | + 'event_espresso' |
|
| 2033 | + ), |
|
| 2034 | + __FILE__, |
|
| 2035 | + __FUNCTION__, |
|
| 2036 | + __LINE__ |
|
| 2037 | + ); |
|
| 2038 | + } |
|
| 2039 | + } else { |
|
| 2040 | + EE_Error::add_error( |
|
| 2041 | + esc_html__( |
|
| 2042 | + 'The submitted billing form is invalid possibly due to a technical reason.', |
|
| 2043 | + 'event_espresso' |
|
| 2044 | + ), |
|
| 2045 | + __FILE__, |
|
| 2046 | + __FUNCTION__, |
|
| 2047 | + __LINE__ |
|
| 2048 | + ); |
|
| 2049 | + } |
|
| 2050 | + return false; |
|
| 2051 | + } |
|
| 2052 | + |
|
| 2053 | + |
|
| 2054 | + /** |
|
| 2055 | + * _setup_primary_registrant_prior_to_payment |
|
| 2056 | + * ensures that the primary registrant has a valid attendee object created with the critical details populated |
|
| 2057 | + * (first & last name & email) and that both the transaction object and primary registration object have been saved |
|
| 2058 | + * plz note that any other registrations will NOT be saved at this point (because they may not have any details |
|
| 2059 | + * yet) |
|
| 2060 | + * |
|
| 2061 | + * @access private |
|
| 2062 | + * @return bool |
|
| 2063 | + * @throws EE_Error |
|
| 2064 | + * @throws InvalidArgumentException |
|
| 2065 | + * @throws ReflectionException |
|
| 2066 | + * @throws RuntimeException |
|
| 2067 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2068 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2069 | + */ |
|
| 2070 | + private function _setup_primary_registrant_prior_to_payment() |
|
| 2071 | + { |
|
| 2072 | + // check if transaction has a primary registrant and that it has a related Attendee object |
|
| 2073 | + // if not, then we need to at least gather some primary registrant data before attempting payment |
|
| 2074 | + if ($this->checkout->billing_form instanceof EE_Billing_Attendee_Info_Form |
|
| 2075 | + && ! $this->checkout->transaction_has_primary_registrant() |
|
| 2076 | + && ! $this->_capture_primary_registration_data_from_billing_form() |
|
| 2077 | + ) { |
|
| 2078 | + return false; |
|
| 2079 | + } |
|
| 2080 | + // because saving an object clears it's cache, we need to do the chevy shuffle |
|
| 2081 | + // grab the primary_registration object |
|
| 2082 | + $primary_registration = $this->checkout->transaction->primary_registration(); |
|
| 2083 | + // at this point we'll consider a TXN to not have been failed |
|
| 2084 | + $this->checkout->transaction->toggle_failed_transaction_status(); |
|
| 2085 | + // save the TXN ( which clears cached copy of primary_registration) |
|
| 2086 | + $this->checkout->transaction->save(); |
|
| 2087 | + // grab TXN ID and save it to the primary_registration |
|
| 2088 | + $primary_registration->set_transaction_id($this->checkout->transaction->ID()); |
|
| 2089 | + // save what we have so far |
|
| 2090 | + $primary_registration->save(); |
|
| 2091 | + return true; |
|
| 2092 | + } |
|
| 2093 | + |
|
| 2094 | + |
|
| 2095 | + /** |
|
| 2096 | + * _capture_primary_registration_data_from_billing_form |
|
| 2097 | + * |
|
| 2098 | + * @access private |
|
| 2099 | + * @return bool |
|
| 2100 | + * @throws EE_Error |
|
| 2101 | + * @throws InvalidArgumentException |
|
| 2102 | + * @throws ReflectionException |
|
| 2103 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2104 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2105 | + */ |
|
| 2106 | + private function _capture_primary_registration_data_from_billing_form() |
|
| 2107 | + { |
|
| 2108 | + // convert billing form data into an attendee |
|
| 2109 | + $this->checkout->primary_attendee_obj = $this->checkout->billing_form->create_attendee_from_billing_form_data(); |
|
| 2110 | + if (! $this->checkout->primary_attendee_obj instanceof EE_Attendee) { |
|
| 2111 | + EE_Error::add_error( |
|
| 2112 | + sprintf( |
|
| 2113 | + esc_html__( |
|
| 2114 | + 'The billing form details could not be used for attendee details due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2115 | + 'event_espresso' |
|
| 2116 | + ), |
|
| 2117 | + '<br/>', |
|
| 2118 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2119 | + ), |
|
| 2120 | + __FILE__, |
|
| 2121 | + __FUNCTION__, |
|
| 2122 | + __LINE__ |
|
| 2123 | + ); |
|
| 2124 | + return false; |
|
| 2125 | + } |
|
| 2126 | + $primary_registration = $this->checkout->transaction->primary_registration(); |
|
| 2127 | + if (! $primary_registration instanceof EE_Registration) { |
|
| 2128 | + EE_Error::add_error( |
|
| 2129 | + sprintf( |
|
| 2130 | + esc_html__( |
|
| 2131 | + 'The primary registrant for this transaction could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2132 | + 'event_espresso' |
|
| 2133 | + ), |
|
| 2134 | + '<br/>', |
|
| 2135 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2136 | + ), |
|
| 2137 | + __FILE__, |
|
| 2138 | + __FUNCTION__, |
|
| 2139 | + __LINE__ |
|
| 2140 | + ); |
|
| 2141 | + return false; |
|
| 2142 | + } |
|
| 2143 | + if (! $primary_registration->_add_relation_to($this->checkout->primary_attendee_obj, 'Attendee') |
|
| 2144 | + instanceof |
|
| 2145 | + EE_Attendee |
|
| 2146 | + ) { |
|
| 2147 | + EE_Error::add_error( |
|
| 2148 | + sprintf( |
|
| 2149 | + esc_html__( |
|
| 2150 | + 'The primary registrant could not be associated with this transaction due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2151 | + 'event_espresso' |
|
| 2152 | + ), |
|
| 2153 | + '<br/>', |
|
| 2154 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2155 | + ), |
|
| 2156 | + __FILE__, |
|
| 2157 | + __FUNCTION__, |
|
| 2158 | + __LINE__ |
|
| 2159 | + ); |
|
| 2160 | + return false; |
|
| 2161 | + } |
|
| 2162 | + /** @type EE_Registration_Processor $registration_processor */ |
|
| 2163 | + $registration_processor = EE_Registry::instance()->load_class('Registration_Processor'); |
|
| 2164 | + // at this point, we should have enough details about the registrant to consider the registration NOT incomplete |
|
| 2165 | + $registration_processor->toggle_incomplete_registration_status_to_default($primary_registration); |
|
| 2166 | + return true; |
|
| 2167 | + } |
|
| 2168 | + |
|
| 2169 | + |
|
| 2170 | + /** |
|
| 2171 | + * _get_payment_method_for_selected_method_of_payment |
|
| 2172 | + * retrieves a valid payment method |
|
| 2173 | + * |
|
| 2174 | + * @access public |
|
| 2175 | + * @return EE_Payment_Method |
|
| 2176 | + * @throws EE_Error |
|
| 2177 | + * @throws InvalidArgumentException |
|
| 2178 | + * @throws ReflectionException |
|
| 2179 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2180 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2181 | + */ |
|
| 2182 | + private function _get_payment_method_for_selected_method_of_payment() |
|
| 2183 | + { |
|
| 2184 | + if ($this->checkout->selected_method_of_payment === 'events_sold_out') { |
|
| 2185 | + $this->_redirect_because_event_sold_out(); |
|
| 2186 | + return null; |
|
| 2187 | + } |
|
| 2188 | + // get EE_Payment_Method object |
|
| 2189 | + if (isset($this->checkout->available_payment_methods[ $this->checkout->selected_method_of_payment ])) { |
|
| 2190 | + $payment_method = $this->checkout->available_payment_methods[ $this->checkout->selected_method_of_payment ]; |
|
| 2191 | + } else { |
|
| 2192 | + // load EEM_Payment_Method |
|
| 2193 | + EE_Registry::instance()->load_model('Payment_Method'); |
|
| 2194 | + /** @type EEM_Payment_Method $EEM_Payment_Method */ |
|
| 2195 | + $EEM_Payment_Method = EE_Registry::instance()->LIB->EEM_Payment_Method; |
|
| 2196 | + $payment_method = $EEM_Payment_Method->get_one_by_slug($this->checkout->selected_method_of_payment); |
|
| 2197 | + } |
|
| 2198 | + // verify $payment_method |
|
| 2199 | + if (! $payment_method instanceof EE_Payment_Method) { |
|
| 2200 | + // not a payment |
|
| 2201 | + EE_Error::add_error( |
|
| 2202 | + sprintf( |
|
| 2203 | + esc_html__( |
|
| 2204 | + 'The selected method of payment could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2205 | + 'event_espresso' |
|
| 2206 | + ), |
|
| 2207 | + '<br/>', |
|
| 2208 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2209 | + ), |
|
| 2210 | + __FILE__, |
|
| 2211 | + __FUNCTION__, |
|
| 2212 | + __LINE__ |
|
| 2213 | + ); |
|
| 2214 | + return null; |
|
| 2215 | + } |
|
| 2216 | + // and verify it has a valid Payment_Method Type object |
|
| 2217 | + if (! $payment_method->type_obj() instanceof EE_PMT_Base) { |
|
| 2218 | + // not a payment |
|
| 2219 | + EE_Error::add_error( |
|
| 2220 | + sprintf( |
|
| 2221 | + esc_html__( |
|
| 2222 | + 'A valid payment method could not be determined due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2223 | + 'event_espresso' |
|
| 2224 | + ), |
|
| 2225 | + '<br/>', |
|
| 2226 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2227 | + ), |
|
| 2228 | + __FILE__, |
|
| 2229 | + __FUNCTION__, |
|
| 2230 | + __LINE__ |
|
| 2231 | + ); |
|
| 2232 | + return null; |
|
| 2233 | + } |
|
| 2234 | + return $payment_method; |
|
| 2235 | + } |
|
| 2236 | + |
|
| 2237 | + |
|
| 2238 | + /** |
|
| 2239 | + * _attempt_payment |
|
| 2240 | + * |
|
| 2241 | + * @access private |
|
| 2242 | + * @type EE_Payment_Method $payment_method |
|
| 2243 | + * @return mixed EE_Payment | boolean |
|
| 2244 | + * @throws EE_Error |
|
| 2245 | + * @throws InvalidArgumentException |
|
| 2246 | + * @throws ReflectionException |
|
| 2247 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2248 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2249 | + */ |
|
| 2250 | + private function _attempt_payment(EE_Payment_Method $payment_method) |
|
| 2251 | + { |
|
| 2252 | + $payment = null; |
|
| 2253 | + $this->checkout->transaction->save(); |
|
| 2254 | + $payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
|
| 2255 | + if (! $payment_processor instanceof EE_Payment_Processor) { |
|
| 2256 | + return false; |
|
| 2257 | + } |
|
| 2258 | + try { |
|
| 2259 | + $payment_processor->set_revisit($this->checkout->revisit); |
|
| 2260 | + // generate payment object |
|
| 2261 | + $payment = $payment_processor->process_payment( |
|
| 2262 | + $payment_method, |
|
| 2263 | + $this->checkout->transaction, |
|
| 2264 | + $this->checkout->amount_owing, |
|
| 2265 | + $this->checkout->billing_form, |
|
| 2266 | + $this->_get_return_url($payment_method), |
|
| 2267 | + 'CART', |
|
| 2268 | + $this->checkout->admin_request, |
|
| 2269 | + true, |
|
| 2270 | + $this->reg_step_url() |
|
| 2271 | + ); |
|
| 2272 | + } catch (Exception $e) { |
|
| 2273 | + $this->_handle_payment_processor_exception($e); |
|
| 2274 | + } |
|
| 2275 | + return $payment; |
|
| 2276 | + } |
|
| 2277 | + |
|
| 2278 | + |
|
| 2279 | + /** |
|
| 2280 | + * _handle_payment_processor_exception |
|
| 2281 | + * |
|
| 2282 | + * @access protected |
|
| 2283 | + * @param \Exception $e |
|
| 2284 | + * @return void |
|
| 2285 | + * @throws EE_Error |
|
| 2286 | + * @throws InvalidArgumentException |
|
| 2287 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2288 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2289 | + */ |
|
| 2290 | + protected function _handle_payment_processor_exception(Exception $e) |
|
| 2291 | + { |
|
| 2292 | + EE_Error::add_error( |
|
| 2293 | + sprintf( |
|
| 2294 | + esc_html__( |
|
| 2295 | + 'The payment could not br processed due to a technical issue.%1$sPlease try again or contact %2$s for assistance.||The following Exception was thrown in %4$s on line %5$s:%1$s%3$s', |
|
| 2296 | + 'event_espresso' |
|
| 2297 | + ), |
|
| 2298 | + '<br/>', |
|
| 2299 | + EE_Registry::instance()->CFG->organization->get_pretty('email'), |
|
| 2300 | + $e->getMessage(), |
|
| 2301 | + $e->getFile(), |
|
| 2302 | + $e->getLine() |
|
| 2303 | + ), |
|
| 2304 | + __FILE__, |
|
| 2305 | + __FUNCTION__, |
|
| 2306 | + __LINE__ |
|
| 2307 | + ); |
|
| 2308 | + } |
|
| 2309 | + |
|
| 2310 | + |
|
| 2311 | + /** |
|
| 2312 | + * _get_return_url |
|
| 2313 | + * |
|
| 2314 | + * @access protected |
|
| 2315 | + * @param \EE_Payment_Method $payment_method |
|
| 2316 | + * @return string |
|
| 2317 | + * @throws \EE_Error |
|
| 2318 | + */ |
|
| 2319 | + protected function _get_return_url(EE_Payment_Method $payment_method) |
|
| 2320 | + { |
|
| 2321 | + $return_url = ''; |
|
| 2322 | + switch ($payment_method->type_obj()->payment_occurs()) { |
|
| 2323 | + case EE_PMT_Base::offsite: |
|
| 2324 | + $return_url = add_query_arg( |
|
| 2325 | + array( |
|
| 2326 | + 'action' => 'process_gateway_response', |
|
| 2327 | + 'selected_method_of_payment' => $this->checkout->selected_method_of_payment, |
|
| 2328 | + 'spco_txn' => $this->checkout->transaction->ID(), |
|
| 2329 | + ), |
|
| 2330 | + $this->reg_step_url() |
|
| 2331 | + ); |
|
| 2332 | + break; |
|
| 2333 | + case EE_PMT_Base::onsite: |
|
| 2334 | + case EE_PMT_Base::offline: |
|
| 2335 | + $return_url = $this->checkout->next_step->reg_step_url(); |
|
| 2336 | + break; |
|
| 2337 | + } |
|
| 2338 | + return $return_url; |
|
| 2339 | + } |
|
| 2340 | + |
|
| 2341 | + |
|
| 2342 | + /** |
|
| 2343 | + * _validate_payment |
|
| 2344 | + * |
|
| 2345 | + * @access private |
|
| 2346 | + * @param EE_Payment $payment |
|
| 2347 | + * @return EE_Payment|FALSE |
|
| 2348 | + * @throws EE_Error |
|
| 2349 | + * @throws InvalidArgumentException |
|
| 2350 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2351 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2352 | + */ |
|
| 2353 | + private function _validate_payment($payment = null) |
|
| 2354 | + { |
|
| 2355 | + if ($this->checkout->payment_method->is_off_line()) { |
|
| 2356 | + return true; |
|
| 2357 | + } |
|
| 2358 | + // verify payment object |
|
| 2359 | + if (! $payment instanceof EE_Payment) { |
|
| 2360 | + // not a payment |
|
| 2361 | + EE_Error::add_error( |
|
| 2362 | + sprintf( |
|
| 2363 | + esc_html__( |
|
| 2364 | + 'A valid payment was not generated due to a technical issue.%1$sPlease try again or contact %2$s for assistance.', |
|
| 2365 | + 'event_espresso' |
|
| 2366 | + ), |
|
| 2367 | + '<br/>', |
|
| 2368 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2369 | + ), |
|
| 2370 | + __FILE__, |
|
| 2371 | + __FUNCTION__, |
|
| 2372 | + __LINE__ |
|
| 2373 | + ); |
|
| 2374 | + return false; |
|
| 2375 | + } |
|
| 2376 | + return $payment; |
|
| 2377 | + } |
|
| 2378 | + |
|
| 2379 | + |
|
| 2380 | + /** |
|
| 2381 | + * _post_payment_processing |
|
| 2382 | + * |
|
| 2383 | + * @access private |
|
| 2384 | + * @param EE_Payment|bool $payment |
|
| 2385 | + * @return bool |
|
| 2386 | + * @throws EE_Error |
|
| 2387 | + * @throws InvalidArgumentException |
|
| 2388 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2389 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2390 | + */ |
|
| 2391 | + private function _post_payment_processing($payment = null) |
|
| 2392 | + { |
|
| 2393 | + // Off-Line payment? |
|
| 2394 | + if ($payment === true) { |
|
| 2395 | + // $this->_setup_redirect_for_next_step(); |
|
| 2396 | + return true; |
|
| 2397 | + // On-Site payment? |
|
| 2398 | + } elseif ($this->checkout->payment_method->is_on_site()) { |
|
| 2399 | + if (! $this->_process_payment_status($payment, EE_PMT_Base::onsite)) { |
|
| 2400 | + // $this->_setup_redirect_for_next_step(); |
|
| 2401 | + $this->checkout->continue_reg = false; |
|
| 2402 | + } |
|
| 2403 | + // Off-Site payment? |
|
| 2404 | + } elseif ($this->checkout->payment_method->is_off_site()) { |
|
| 2405 | + // if a payment object was made and it specifies a redirect url, then we'll setup that redirect info |
|
| 2406 | + if ($payment instanceof EE_Payment && $payment->redirect_url()) { |
|
| 2407 | + do_action('AHEE_log', __CLASS__, __FUNCTION__, $payment->redirect_url(), '$payment->redirect_url()'); |
|
| 2408 | + $this->checkout->redirect = true; |
|
| 2409 | + $this->checkout->redirect_form = $payment->redirect_form(); |
|
| 2410 | + $this->checkout->redirect_url = $this->reg_step_url('redirect_form'); |
|
| 2411 | + // set JSON response |
|
| 2412 | + $this->checkout->json_response->set_redirect_form($this->checkout->redirect_form); |
|
| 2413 | + // and lastly, let's bump the payment status to pending |
|
| 2414 | + $payment->set_status(EEM_Payment::status_id_pending); |
|
| 2415 | + $payment->save(); |
|
| 2416 | + } else { |
|
| 2417 | + // not a payment |
|
| 2418 | + $this->checkout->continue_reg = false; |
|
| 2419 | + EE_Error::add_error( |
|
| 2420 | + sprintf( |
|
| 2421 | + esc_html__( |
|
| 2422 | + 'It appears the Off Site Payment Method was not configured properly.%sPlease try again or contact %s for assistance.', |
|
| 2423 | + 'event_espresso' |
|
| 2424 | + ), |
|
| 2425 | + '<br/>', |
|
| 2426 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2427 | + ), |
|
| 2428 | + __FILE__, |
|
| 2429 | + __FUNCTION__, |
|
| 2430 | + __LINE__ |
|
| 2431 | + ); |
|
| 2432 | + } |
|
| 2433 | + } else { |
|
| 2434 | + // ummm ya... not Off-Line, not On-Site, not off-Site ???? |
|
| 2435 | + $this->checkout->continue_reg = false; |
|
| 2436 | + return false; |
|
| 2437 | + } |
|
| 2438 | + return $payment; |
|
| 2439 | + } |
|
| 2440 | + |
|
| 2441 | + |
|
| 2442 | + /** |
|
| 2443 | + * _process_payment_status |
|
| 2444 | + * |
|
| 2445 | + * @access private |
|
| 2446 | + * @type EE_Payment $payment |
|
| 2447 | + * @param string $payment_occurs |
|
| 2448 | + * @return bool |
|
| 2449 | + * @throws EE_Error |
|
| 2450 | + * @throws InvalidArgumentException |
|
| 2451 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2452 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2453 | + */ |
|
| 2454 | + private function _process_payment_status($payment, $payment_occurs = EE_PMT_Base::offline) |
|
| 2455 | + { |
|
| 2456 | + // off-line payment? carry on |
|
| 2457 | + if ($payment_occurs === EE_PMT_Base::offline) { |
|
| 2458 | + return true; |
|
| 2459 | + } |
|
| 2460 | + // verify payment validity |
|
| 2461 | + if ($payment instanceof EE_Payment) { |
|
| 2462 | + do_action('AHEE_log', __CLASS__, __FUNCTION__, $payment->status(), '$payment->status()'); |
|
| 2463 | + $msg = $payment->gateway_response(); |
|
| 2464 | + // check results |
|
| 2465 | + switch ($payment->status()) { |
|
| 2466 | + // good payment |
|
| 2467 | + case EEM_Payment::status_id_approved: |
|
| 2468 | + EE_Error::add_success( |
|
| 2469 | + esc_html__('Your payment was processed successfully.', 'event_espresso'), |
|
| 2470 | + __FILE__, |
|
| 2471 | + __FUNCTION__, |
|
| 2472 | + __LINE__ |
|
| 2473 | + ); |
|
| 2474 | + return true; |
|
| 2475 | + break; |
|
| 2476 | + // slow payment |
|
| 2477 | + case EEM_Payment::status_id_pending: |
|
| 2478 | + if (empty($msg)) { |
|
| 2479 | + $msg = esc_html__( |
|
| 2480 | + 'Your payment appears to have been processed successfully, but the Instant Payment Notification has not yet been received. It should arrive shortly.', |
|
| 2481 | + 'event_espresso' |
|
| 2482 | + ); |
|
| 2483 | + } |
|
| 2484 | + EE_Error::add_success($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 2485 | + return true; |
|
| 2486 | + break; |
|
| 2487 | + // don't wanna payment |
|
| 2488 | + case EEM_Payment::status_id_cancelled: |
|
| 2489 | + if (empty($msg)) { |
|
| 2490 | + $msg = _n( |
|
| 2491 | + 'Payment cancelled. Please try again.', |
|
| 2492 | + 'Payment cancelled. Please try again or select another method of payment.', |
|
| 2493 | + count($this->checkout->available_payment_methods), |
|
| 2494 | + 'event_espresso' |
|
| 2495 | + ); |
|
| 2496 | + } |
|
| 2497 | + EE_Error::add_attention($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 2498 | + return false; |
|
| 2499 | + break; |
|
| 2500 | + // not enough payment |
|
| 2501 | + case EEM_Payment::status_id_declined: |
|
| 2502 | + if (empty($msg)) { |
|
| 2503 | + $msg = _n( |
|
| 2504 | + 'We\'re sorry but your payment was declined. Please try again.', |
|
| 2505 | + 'We\'re sorry but your payment was declined. Please try again or select another method of payment.', |
|
| 2506 | + count($this->checkout->available_payment_methods), |
|
| 2507 | + 'event_espresso' |
|
| 2508 | + ); |
|
| 2509 | + } |
|
| 2510 | + EE_Error::add_attention($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 2511 | + return false; |
|
| 2512 | + break; |
|
| 2513 | + // bad payment |
|
| 2514 | + case EEM_Payment::status_id_failed: |
|
| 2515 | + if (! empty($msg)) { |
|
| 2516 | + EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
| 2517 | + return false; |
|
| 2518 | + } |
|
| 2519 | + // default to error below |
|
| 2520 | + break; |
|
| 2521 | + } |
|
| 2522 | + } |
|
| 2523 | + // off-site payment gateway responses are too unreliable, so let's just assume that |
|
| 2524 | + // the payment processing is just running slower than the registrant's request |
|
| 2525 | + if ($payment_occurs === EE_PMT_Base::offsite) { |
|
| 2526 | + return true; |
|
| 2527 | + } |
|
| 2528 | + EE_Error::add_error( |
|
| 2529 | + sprintf( |
|
| 2530 | + esc_html__( |
|
| 2531 | + 'Your payment could not be processed successfully due to a technical issue.%sPlease try again or contact %s for assistance.', |
|
| 2532 | + 'event_espresso' |
|
| 2533 | + ), |
|
| 2534 | + '<br/>', |
|
| 2535 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2536 | + ), |
|
| 2537 | + __FILE__, |
|
| 2538 | + __FUNCTION__, |
|
| 2539 | + __LINE__ |
|
| 2540 | + ); |
|
| 2541 | + return false; |
|
| 2542 | + } |
|
| 2543 | + |
|
| 2544 | + |
|
| 2545 | + |
|
| 2546 | + |
|
| 2547 | + |
|
| 2548 | + |
|
| 2549 | + /********************************************************************************************************/ |
|
| 2550 | + /********************************** PROCESS GATEWAY RESPONSE **********************************/ |
|
| 2551 | + /********************************************************************************************************/ |
|
| 2552 | + /** |
|
| 2553 | + * process_gateway_response |
|
| 2554 | + * this is the return point for Off-Site Payment Methods |
|
| 2555 | + * It will attempt to "handle the IPN" if it appears that this has not already occurred, |
|
| 2556 | + * otherwise, it will load up the last payment made for the TXN. |
|
| 2557 | + * If the payment retrieved looks good, it will then either: |
|
| 2558 | + * complete the current step and allow advancement to the next reg step |
|
| 2559 | + * or present the payment options again |
|
| 2560 | + * |
|
| 2561 | + * @access private |
|
| 2562 | + * @return EE_Payment|FALSE |
|
| 2563 | + * @throws EE_Error |
|
| 2564 | + * @throws InvalidArgumentException |
|
| 2565 | + * @throws ReflectionException |
|
| 2566 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2567 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2568 | + * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
|
| 2569 | + */ |
|
| 2570 | + public function process_gateway_response() |
|
| 2571 | + { |
|
| 2572 | + $payment = null; |
|
| 2573 | + // how have they chosen to pay? |
|
| 2574 | + $this->checkout->selected_method_of_payment = $this->_get_selected_method_of_payment(true); |
|
| 2575 | + // get EE_Payment_Method object |
|
| 2576 | + if (! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment()) { |
|
| 2577 | + $this->checkout->continue_reg = false; |
|
| 2578 | + return false; |
|
| 2579 | + } |
|
| 2580 | + if (! $this->checkout->payment_method->is_off_site()) { |
|
| 2581 | + return false; |
|
| 2582 | + } |
|
| 2583 | + $this->_validate_offsite_return(); |
|
| 2584 | + // DEBUG LOG |
|
| 2585 | + // $this->checkout->log( |
|
| 2586 | + // __CLASS__, |
|
| 2587 | + // __FUNCTION__, |
|
| 2588 | + // __LINE__, |
|
| 2589 | + // array( |
|
| 2590 | + // 'selected_method_of_payment' => $this->checkout->selected_method_of_payment, |
|
| 2591 | + // 'payment_method' => $this->checkout->payment_method, |
|
| 2592 | + // ), |
|
| 2593 | + // true |
|
| 2594 | + // ); |
|
| 2595 | + // verify TXN |
|
| 2596 | + if ($this->checkout->transaction instanceof EE_Transaction) { |
|
| 2597 | + $gateway = $this->checkout->payment_method->type_obj()->get_gateway(); |
|
| 2598 | + if (! $gateway instanceof EE_Offsite_Gateway) { |
|
| 2599 | + $this->checkout->continue_reg = false; |
|
| 2600 | + return false; |
|
| 2601 | + } |
|
| 2602 | + $payment = $this->_process_off_site_payment($gateway); |
|
| 2603 | + $payment = $this->_process_cancelled_payments($payment); |
|
| 2604 | + $payment = $this->_validate_payment($payment); |
|
| 2605 | + // if payment was not declined by the payment gateway or cancelled by the registrant |
|
| 2606 | + if ($this->_process_payment_status($payment, EE_PMT_Base::offsite)) { |
|
| 2607 | + // $this->_setup_redirect_for_next_step(); |
|
| 2608 | + // store that for later |
|
| 2609 | + $this->checkout->payment = $payment; |
|
| 2610 | + // mark this reg step as completed, as long as gateway doesn't use a separate IPN request, |
|
| 2611 | + // because we will complete this step during the IPN processing then |
|
| 2612 | + if ($gateway instanceof EE_Offsite_Gateway && ! $this->handle_IPN_in_this_request()) { |
|
| 2613 | + $this->set_completed(); |
|
| 2614 | + } |
|
| 2615 | + return true; |
|
| 2616 | + } |
|
| 2617 | + } |
|
| 2618 | + // DEBUG LOG |
|
| 2619 | + // $this->checkout->log( |
|
| 2620 | + // __CLASS__, |
|
| 2621 | + // __FUNCTION__, |
|
| 2622 | + // __LINE__, |
|
| 2623 | + // array('payment' => $payment) |
|
| 2624 | + // ); |
|
| 2625 | + $this->checkout->continue_reg = false; |
|
| 2626 | + return false; |
|
| 2627 | + } |
|
| 2628 | + |
|
| 2629 | + |
|
| 2630 | + /** |
|
| 2631 | + * _validate_return |
|
| 2632 | + * |
|
| 2633 | + * @access private |
|
| 2634 | + * @return void |
|
| 2635 | + * @throws EE_Error |
|
| 2636 | + * @throws InvalidArgumentException |
|
| 2637 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2638 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2639 | + * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
|
| 2640 | + */ |
|
| 2641 | + private function _validate_offsite_return() |
|
| 2642 | + { |
|
| 2643 | + $TXN_ID = (int) EE_Registry::instance()->REQ->get('spco_txn', 0); |
|
| 2644 | + if ($TXN_ID !== $this->checkout->transaction->ID()) { |
|
| 2645 | + // Houston... we might have a problem |
|
| 2646 | + $invalid_TXN = false; |
|
| 2647 | + // first gather some info |
|
| 2648 | + $valid_TXN = EEM_Transaction::instance()->get_one_by_ID($TXN_ID); |
|
| 2649 | + $primary_registrant = $valid_TXN instanceof EE_Transaction |
|
| 2650 | + ? $valid_TXN->primary_registration() |
|
| 2651 | + : null; |
|
| 2652 | + // let's start by retrieving the cart for this TXN |
|
| 2653 | + $cart = $this->checkout->get_cart_for_transaction($this->checkout->transaction); |
|
| 2654 | + if ($cart instanceof EE_Cart) { |
|
| 2655 | + // verify that the current cart has tickets |
|
| 2656 | + $tickets = $cart->get_tickets(); |
|
| 2657 | + if (empty($tickets)) { |
|
| 2658 | + $invalid_TXN = true; |
|
| 2659 | + } |
|
| 2660 | + } else { |
|
| 2661 | + $invalid_TXN = true; |
|
| 2662 | + } |
|
| 2663 | + $valid_TXN_SID = $primary_registrant instanceof EE_Registration |
|
| 2664 | + ? $primary_registrant->session_ID() |
|
| 2665 | + : null; |
|
| 2666 | + // validate current Session ID and compare against valid TXN session ID |
|
| 2667 | + if ($invalid_TXN // if this is already true, then skip other checks |
|
| 2668 | + || EE_Session::instance()->id() === null |
|
| 2669 | + || ( |
|
| 2670 | + // WARNING !!! |
|
| 2671 | + // this could be PayPal sending back duplicate requests (ya they do that) |
|
| 2672 | + // or it **could** mean someone is simply registering AGAIN after having just done so |
|
| 2673 | + // so now we need to determine if this current TXN looks valid or not |
|
| 2674 | + // and whether this reg step has even been started ? |
|
| 2675 | + EE_Session::instance()->id() === $valid_TXN_SID |
|
| 2676 | + // really? you're half way through this reg step, but you never started it ? |
|
| 2677 | + && $this->checkout->transaction->reg_step_completed($this->slug()) === false |
|
| 2678 | + ) |
|
| 2679 | + ) { |
|
| 2680 | + $invalid_TXN = true; |
|
| 2681 | + } |
|
| 2682 | + if ($invalid_TXN) { |
|
| 2683 | + // is the valid TXN completed ? |
|
| 2684 | + if ($valid_TXN instanceof EE_Transaction) { |
|
| 2685 | + // has this step even been started ? |
|
| 2686 | + $reg_step_completed = $valid_TXN->reg_step_completed($this->slug()); |
|
| 2687 | + if ($reg_step_completed !== false && $reg_step_completed !== true) { |
|
| 2688 | + // so it **looks** like this is a double request from PayPal |
|
| 2689 | + // so let's try to pick up where we left off |
|
| 2690 | + $this->checkout->transaction = $valid_TXN; |
|
| 2691 | + $this->checkout->refresh_all_entities(true); |
|
| 2692 | + return; |
|
| 2693 | + } |
|
| 2694 | + } |
|
| 2695 | + // you appear to be lost? |
|
| 2696 | + $this->_redirect_wayward_request($primary_registrant); |
|
| 2697 | + } |
|
| 2698 | + } |
|
| 2699 | + } |
|
| 2700 | + |
|
| 2701 | + |
|
| 2702 | + /** |
|
| 2703 | + * _redirect_wayward_request |
|
| 2704 | + * |
|
| 2705 | + * @access private |
|
| 2706 | + * @param \EE_Registration|null $primary_registrant |
|
| 2707 | + * @return bool |
|
| 2708 | + * @throws EE_Error |
|
| 2709 | + * @throws InvalidArgumentException |
|
| 2710 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2711 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2712 | + */ |
|
| 2713 | + private function _redirect_wayward_request(EE_Registration $primary_registrant) |
|
| 2714 | + { |
|
| 2715 | + if (! $primary_registrant instanceof EE_Registration) { |
|
| 2716 | + // try redirecting based on the current TXN |
|
| 2717 | + $primary_registrant = $this->checkout->transaction instanceof EE_Transaction |
|
| 2718 | + ? $this->checkout->transaction->primary_registration() |
|
| 2719 | + : null; |
|
| 2720 | + } |
|
| 2721 | + if (! $primary_registrant instanceof EE_Registration) { |
|
| 2722 | + EE_Error::add_error( |
|
| 2723 | + sprintf( |
|
| 2724 | + esc_html__( |
|
| 2725 | + 'Invalid information was received from the Off-Site Payment Processor and your Transaction details could not be retrieved from the database.%1$sPlease try again or contact %2$s for assistance.', |
|
| 2726 | + 'event_espresso' |
|
| 2727 | + ), |
|
| 2728 | + '<br/>', |
|
| 2729 | + EE_Registry::instance()->CFG->organization->get_pretty('email') |
|
| 2730 | + ), |
|
| 2731 | + __FILE__, |
|
| 2732 | + __FUNCTION__, |
|
| 2733 | + __LINE__ |
|
| 2734 | + ); |
|
| 2735 | + return false; |
|
| 2736 | + } |
|
| 2737 | + // make sure transaction is not locked |
|
| 2738 | + $this->checkout->transaction->unlock(); |
|
| 2739 | + wp_safe_redirect( |
|
| 2740 | + add_query_arg( |
|
| 2741 | + array( |
|
| 2742 | + 'e_reg_url_link' => $primary_registrant->reg_url_link(), |
|
| 2743 | + ), |
|
| 2744 | + $this->checkout->thank_you_page_url |
|
| 2745 | + ) |
|
| 2746 | + ); |
|
| 2747 | + exit(); |
|
| 2748 | + } |
|
| 2749 | + |
|
| 2750 | + |
|
| 2751 | + /** |
|
| 2752 | + * _process_off_site_payment |
|
| 2753 | + * |
|
| 2754 | + * @access private |
|
| 2755 | + * @param \EE_Offsite_Gateway $gateway |
|
| 2756 | + * @return EE_Payment |
|
| 2757 | + * @throws EE_Error |
|
| 2758 | + * @throws InvalidArgumentException |
|
| 2759 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2760 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2761 | + */ |
|
| 2762 | + private function _process_off_site_payment(EE_Offsite_Gateway $gateway) |
|
| 2763 | + { |
|
| 2764 | + try { |
|
| 2765 | + $request_data = \EE_Registry::instance()->REQ->params(); |
|
| 2766 | + // if gateway uses_separate_IPN_request, then we don't have to process the IPN manually |
|
| 2767 | + $this->set_handle_IPN_in_this_request( |
|
| 2768 | + $gateway->handle_IPN_in_this_request($request_data, false) |
|
| 2769 | + ); |
|
| 2770 | + if ($this->handle_IPN_in_this_request()) { |
|
| 2771 | + // get payment details and process results |
|
| 2772 | + /** @type EE_Payment_Processor $payment_processor */ |
|
| 2773 | + $payment_processor = EE_Registry::instance()->load_core('Payment_Processor'); |
|
| 2774 | + $payment = $payment_processor->process_ipn( |
|
| 2775 | + $request_data, |
|
| 2776 | + $this->checkout->transaction, |
|
| 2777 | + $this->checkout->payment_method, |
|
| 2778 | + true, |
|
| 2779 | + false |
|
| 2780 | + ); |
|
| 2781 | + // $payment_source = 'process_ipn'; |
|
| 2782 | + } else { |
|
| 2783 | + $payment = $this->checkout->transaction->last_payment(); |
|
| 2784 | + // $payment_source = 'last_payment'; |
|
| 2785 | + } |
|
| 2786 | + } catch (Exception $e) { |
|
| 2787 | + // let's just eat the exception and try to move on using any previously set payment info |
|
| 2788 | + $payment = $this->checkout->transaction->last_payment(); |
|
| 2789 | + // $payment_source = 'last_payment after Exception'; |
|
| 2790 | + // but if we STILL don't have a payment object |
|
| 2791 | + if (! $payment instanceof EE_Payment) { |
|
| 2792 | + // then we'll object ! ( not object like a thing... but object like what a lawyer says ! ) |
|
| 2793 | + $this->_handle_payment_processor_exception($e); |
|
| 2794 | + } |
|
| 2795 | + } |
|
| 2796 | + // DEBUG LOG |
|
| 2797 | + // $this->checkout->log( |
|
| 2798 | + // __CLASS__, |
|
| 2799 | + // __FUNCTION__, |
|
| 2800 | + // __LINE__, |
|
| 2801 | + // array( |
|
| 2802 | + // 'process_ipn_payment' => $payment, |
|
| 2803 | + // 'payment_source' => $payment_source, |
|
| 2804 | + // ) |
|
| 2805 | + // ); |
|
| 2806 | + return $payment; |
|
| 2807 | + } |
|
| 2808 | + |
|
| 2809 | + |
|
| 2810 | + /** |
|
| 2811 | + * _process_cancelled_payments |
|
| 2812 | + * just makes sure that the payment status gets updated correctly |
|
| 2813 | + * so tha tan error isn't generated during payment validation |
|
| 2814 | + * |
|
| 2815 | + * @access private |
|
| 2816 | + * @param EE_Payment $payment |
|
| 2817 | + * @return EE_Payment | FALSE |
|
| 2818 | + * @throws \EE_Error |
|
| 2819 | + */ |
|
| 2820 | + private function _process_cancelled_payments($payment = null) |
|
| 2821 | + { |
|
| 2822 | + if ($payment instanceof EE_Payment |
|
| 2823 | + && isset($_REQUEST['ee_cancel_payment']) |
|
| 2824 | + && $payment->status() === EEM_Payment::status_id_failed |
|
| 2825 | + ) { |
|
| 2826 | + $payment->set_status(EEM_Payment::status_id_cancelled); |
|
| 2827 | + } |
|
| 2828 | + return $payment; |
|
| 2829 | + } |
|
| 2830 | + |
|
| 2831 | + |
|
| 2832 | + /** |
|
| 2833 | + * get_transaction_details_for_gateways |
|
| 2834 | + * |
|
| 2835 | + * @access public |
|
| 2836 | + * @return int |
|
| 2837 | + * @throws EE_Error |
|
| 2838 | + * @throws InvalidArgumentException |
|
| 2839 | + * @throws ReflectionException |
|
| 2840 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 2841 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 2842 | + */ |
|
| 2843 | + public function get_transaction_details_for_gateways() |
|
| 2844 | + { |
|
| 2845 | + $txn_details = array(); |
|
| 2846 | + // ya gotta make a choice man |
|
| 2847 | + if (empty($this->checkout->selected_method_of_payment)) { |
|
| 2848 | + $txn_details = array( |
|
| 2849 | + 'error' => esc_html__('Please select a method of payment before proceeding.', 'event_espresso'), |
|
| 2850 | + ); |
|
| 2851 | + } |
|
| 2852 | + // get EE_Payment_Method object |
|
| 2853 | + if (empty($txn_details) |
|
| 2854 | + && |
|
| 2855 | + ! $this->checkout->payment_method = $this->_get_payment_method_for_selected_method_of_payment() |
|
| 2856 | + ) { |
|
| 2857 | + $txn_details = array( |
|
| 2858 | + 'selected_method_of_payment' => $this->checkout->selected_method_of_payment, |
|
| 2859 | + 'error' => esc_html__( |
|
| 2860 | + 'A valid Payment Method could not be determined.', |
|
| 2861 | + 'event_espresso' |
|
| 2862 | + ), |
|
| 2863 | + ); |
|
| 2864 | + } |
|
| 2865 | + if (empty($txn_details) && $this->checkout->transaction instanceof EE_Transaction) { |
|
| 2866 | + $return_url = $this->_get_return_url($this->checkout->payment_method); |
|
| 2867 | + $txn_details = array( |
|
| 2868 | + 'TXN_ID' => $this->checkout->transaction->ID(), |
|
| 2869 | + 'TXN_timestamp' => $this->checkout->transaction->datetime(), |
|
| 2870 | + 'TXN_total' => $this->checkout->transaction->total(), |
|
| 2871 | + 'TXN_paid' => $this->checkout->transaction->paid(), |
|
| 2872 | + 'TXN_reg_steps' => $this->checkout->transaction->reg_steps(), |
|
| 2873 | + 'STS_ID' => $this->checkout->transaction->status_ID(), |
|
| 2874 | + 'PMD_ID' => $this->checkout->transaction->payment_method_ID(), |
|
| 2875 | + 'payment_amount' => $this->checkout->amount_owing, |
|
| 2876 | + 'return_url' => $return_url, |
|
| 2877 | + 'cancel_url' => add_query_arg(array('ee_cancel_payment' => true), $return_url), |
|
| 2878 | + 'notify_url' => EE_Config::instance()->core->txn_page_url( |
|
| 2879 | + array( |
|
| 2880 | + 'e_reg_url_link' => $this->checkout->transaction->primary_registration()->reg_url_link(), |
|
| 2881 | + 'ee_payment_method' => $this->checkout->payment_method->slug(), |
|
| 2882 | + ) |
|
| 2883 | + ), |
|
| 2884 | + ); |
|
| 2885 | + } |
|
| 2886 | + echo wp_json_encode($txn_details); |
|
| 2887 | + exit(); |
|
| 2888 | + } |
|
| 2889 | + |
|
| 2890 | + |
|
| 2891 | + /** |
|
| 2892 | + * __sleep |
|
| 2893 | + * to conserve db space, let's remove the reg_form and the EE_Checkout object from EE_SPCO_Reg_Step objects upon |
|
| 2894 | + * serialization EE_Checkout will handle the reimplementation of itself upon waking, but we won't bother with the |
|
| 2895 | + * reg form, because if needed, it will be regenerated anyways |
|
| 2896 | + * |
|
| 2897 | + * @return array |
|
| 2898 | + */ |
|
| 2899 | + public function __sleep() |
|
| 2900 | + { |
|
| 2901 | + // remove the reg form and the checkout |
|
| 2902 | + return array_diff(array_keys(get_object_vars($this)), array('reg_form', 'checkout', 'line_item_display')); |
|
| 2903 | + } |
|
| 2904 | 2904 | } |
@@ -13,3348 +13,3348 @@ |
||
| 13 | 13 | abstract class EE_Base_Class |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * This is an array of the original properties and values provided during construction |
|
| 18 | - * of this model object. (keys are model field names, values are their values). |
|
| 19 | - * This list is important to remember so that when we are merging data from the db, we know |
|
| 20 | - * which values to override and which to not override. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $_props_n_values_provided_in_constructor; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Timezone |
|
| 28 | - * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 29 | - * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 30 | - * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 31 | - * access to it. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - protected $_timezone; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * date format |
|
| 39 | - * pattern or format for displaying dates |
|
| 40 | - * |
|
| 41 | - * @var string $_dt_frmt |
|
| 42 | - */ |
|
| 43 | - protected $_dt_frmt; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * time format |
|
| 47 | - * pattern or format for displaying time |
|
| 48 | - * |
|
| 49 | - * @var string $_tm_frmt |
|
| 50 | - */ |
|
| 51 | - protected $_tm_frmt; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 55 | - * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 56 | - * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 57 | - * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 58 | - * |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - protected $_cached_properties = array(); |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 65 | - * single |
|
| 66 | - * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 67 | - * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 68 | - * all others have an array) |
|
| 69 | - * |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected $_model_relations = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 76 | - * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 77 | - * |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected $_fields = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 84 | - * For example, we might create model objects intended to only be used for the duration |
|
| 85 | - * of this request and to be thrown away, and if they were accidentally saved |
|
| 86 | - * it would be a bug. |
|
| 87 | - */ |
|
| 88 | - protected $_allow_persist = true; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 92 | - */ |
|
| 93 | - protected $_has_changes = false; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @var EEM_Base |
|
| 97 | - */ |
|
| 98 | - protected $_model; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
|
| 102 | - * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
|
| 103 | - * the db. They also do not automatically update if there are any changes to the data that produced their results. |
|
| 104 | - * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
|
| 105 | - * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
|
| 106 | - * array as: |
|
| 107 | - * array( |
|
| 108 | - * 'Registration_Count' => 24 |
|
| 109 | - * ); |
|
| 110 | - * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
|
| 111 | - * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
|
| 112 | - * info) |
|
| 113 | - * |
|
| 114 | - * @var array |
|
| 115 | - */ |
|
| 116 | - protected $custom_selection_results = array(); |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 121 | - * play nice |
|
| 122 | - * |
|
| 123 | - * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 124 | - * layer of the model's _fields array, (eg, EVT_ID, |
|
| 125 | - * TXN_amount, QST_name, etc) and values are their values |
|
| 126 | - * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 127 | - * corresponding db model or not. |
|
| 128 | - * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 129 | - * be in when instantiating a EE_Base_Class object. |
|
| 130 | - * @param array $date_formats An array of date formats to set on construct where first |
|
| 131 | - * value is the date_format and second value is the time |
|
| 132 | - * format. |
|
| 133 | - * @throws InvalidArgumentException |
|
| 134 | - * @throws InvalidInterfaceException |
|
| 135 | - * @throws InvalidDataTypeException |
|
| 136 | - * @throws EE_Error |
|
| 137 | - * @throws ReflectionException |
|
| 138 | - */ |
|
| 139 | - protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 140 | - { |
|
| 141 | - $className = get_class($this); |
|
| 142 | - do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 143 | - $model = $this->get_model(); |
|
| 144 | - $model_fields = $model->field_settings(false); |
|
| 145 | - // ensure $fieldValues is an array |
|
| 146 | - $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 147 | - // verify client code has not passed any invalid field names |
|
| 148 | - foreach ($fieldValues as $field_name => $field_value) { |
|
| 149 | - if (! isset($model_fields[ $field_name ])) { |
|
| 150 | - throw new EE_Error( |
|
| 151 | - sprintf( |
|
| 152 | - esc_html__( |
|
| 153 | - 'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s', |
|
| 154 | - 'event_espresso' |
|
| 155 | - ), |
|
| 156 | - $field_name, |
|
| 157 | - get_class($this), |
|
| 158 | - implode(', ', array_keys($model_fields)) |
|
| 159 | - ) |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 164 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 165 | - list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 166 | - } else { |
|
| 167 | - // set default formats for date and time |
|
| 168 | - $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
| 169 | - $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
| 170 | - } |
|
| 171 | - // if db model is instantiating |
|
| 172 | - if ($bydb) { |
|
| 173 | - // client code has indicated these field values are from the database |
|
| 174 | - foreach ($model_fields as $fieldName => $field) { |
|
| 175 | - $this->set_from_db( |
|
| 176 | - $fieldName, |
|
| 177 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
| 178 | - ); |
|
| 179 | - } |
|
| 180 | - } else { |
|
| 181 | - // we're constructing a brand |
|
| 182 | - // new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 183 | - foreach ($model_fields as $fieldName => $field) { |
|
| 184 | - $this->set( |
|
| 185 | - $fieldName, |
|
| 186 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
| 187 | - true |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - // remember what values were passed to this constructor |
|
| 192 | - $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 193 | - // remember in entity mapper |
|
| 194 | - if (! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 195 | - $model->add_to_entity_map($this); |
|
| 196 | - } |
|
| 197 | - // setup all the relations |
|
| 198 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 199 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 200 | - $this->_model_relations[ $relation_name ] = null; |
|
| 201 | - } else { |
|
| 202 | - $this->_model_relations[ $relation_name ] = array(); |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - /** |
|
| 206 | - * Action done at the end of each model object construction |
|
| 207 | - * |
|
| 208 | - * @param EE_Base_Class $this the model object just created |
|
| 209 | - */ |
|
| 210 | - do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 216 | - * |
|
| 217 | - * @return boolean |
|
| 218 | - */ |
|
| 219 | - public function allow_persist() |
|
| 220 | - { |
|
| 221 | - return $this->_allow_persist; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 227 | - * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 228 | - * you got new information that somehow made you change your mind. |
|
| 229 | - * |
|
| 230 | - * @param boolean $allow_persist |
|
| 231 | - * @return boolean |
|
| 232 | - */ |
|
| 233 | - public function set_allow_persist($allow_persist) |
|
| 234 | - { |
|
| 235 | - return $this->_allow_persist = $allow_persist; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Gets the field's original value when this object was constructed during this request. |
|
| 241 | - * This can be helpful when determining if a model object has changed or not |
|
| 242 | - * |
|
| 243 | - * @param string $field_name |
|
| 244 | - * @return mixed|null |
|
| 245 | - * @throws ReflectionException |
|
| 246 | - * @throws InvalidArgumentException |
|
| 247 | - * @throws InvalidInterfaceException |
|
| 248 | - * @throws InvalidDataTypeException |
|
| 249 | - * @throws EE_Error |
|
| 250 | - */ |
|
| 251 | - public function get_original($field_name) |
|
| 252 | - { |
|
| 253 | - if (isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
| 254 | - && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 255 | - ) { |
|
| 256 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
| 257 | - } |
|
| 258 | - return null; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * @param EE_Base_Class $obj |
|
| 264 | - * @return string |
|
| 265 | - */ |
|
| 266 | - public function get_class($obj) |
|
| 267 | - { |
|
| 268 | - return get_class($obj); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Overrides parent because parent expects old models. |
|
| 274 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 275 | - * |
|
| 276 | - * @param string $field_name |
|
| 277 | - * @param mixed $field_value |
|
| 278 | - * @param bool $use_default |
|
| 279 | - * @throws InvalidArgumentException |
|
| 280 | - * @throws InvalidInterfaceException |
|
| 281 | - * @throws InvalidDataTypeException |
|
| 282 | - * @throws EE_Error |
|
| 283 | - * @throws ReflectionException |
|
| 284 | - * @throws ReflectionException |
|
| 285 | - * @throws ReflectionException |
|
| 286 | - */ |
|
| 287 | - public function set($field_name, $field_value, $use_default = false) |
|
| 288 | - { |
|
| 289 | - // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 290 | - // then don't do anything |
|
| 291 | - if (! $use_default |
|
| 292 | - && $this->_fields[ $field_name ] === $field_value |
|
| 293 | - && $this->ID() |
|
| 294 | - ) { |
|
| 295 | - return; |
|
| 296 | - } |
|
| 297 | - $model = $this->get_model(); |
|
| 298 | - $this->_has_changes = true; |
|
| 299 | - $field_obj = $model->field_settings_for($field_name); |
|
| 300 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 301 | - // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 302 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 303 | - $field_obj->set_timezone($this->_timezone); |
|
| 304 | - $field_obj->set_date_format($this->_dt_frmt); |
|
| 305 | - $field_obj->set_time_format($this->_tm_frmt); |
|
| 306 | - } |
|
| 307 | - $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 308 | - // should the value be null? |
|
| 309 | - if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 310 | - $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
| 311 | - /** |
|
| 312 | - * To save having to refactor all the models, if a default value is used for a |
|
| 313 | - * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 314 | - * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 315 | - * object. |
|
| 316 | - * |
|
| 317 | - * @since 4.6.10+ |
|
| 318 | - */ |
|
| 319 | - if ($field_obj instanceof EE_Datetime_Field |
|
| 320 | - && $this->_fields[ $field_name ] !== null |
|
| 321 | - && ! $this->_fields[ $field_name ] instanceof DateTime |
|
| 322 | - ) { |
|
| 323 | - empty($this->_fields[ $field_name ]) |
|
| 324 | - ? $this->set($field_name, time()) |
|
| 325 | - : $this->set($field_name, $this->_fields[ $field_name ]); |
|
| 326 | - } |
|
| 327 | - } else { |
|
| 328 | - $this->_fields[ $field_name ] = $holder_of_value; |
|
| 329 | - } |
|
| 330 | - // if we're not in the constructor... |
|
| 331 | - // now check if what we set was a primary key |
|
| 332 | - if (// note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 333 | - $this->_props_n_values_provided_in_constructor |
|
| 334 | - && $field_value |
|
| 335 | - && $field_name === $model->primary_key_name() |
|
| 336 | - ) { |
|
| 337 | - // if so, we want all this object's fields to be filled either with |
|
| 338 | - // what we've explicitly set on this model |
|
| 339 | - // or what we have in the db |
|
| 340 | - // echo "setting primary key!"; |
|
| 341 | - $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 342 | - $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 343 | - foreach ($fields_on_model as $field_obj) { |
|
| 344 | - if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 345 | - && $field_obj->get_name() !== $field_name |
|
| 346 | - ) { |
|
| 347 | - $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - // oh this model object has an ID? well make sure its in the entity mapper |
|
| 351 | - $model->add_to_entity_map($this); |
|
| 352 | - } |
|
| 353 | - // let's unset any cache for this field_name from the $_cached_properties property. |
|
| 354 | - $this->_clear_cached_property($field_name); |
|
| 355 | - } else { |
|
| 356 | - throw new EE_Error( |
|
| 357 | - sprintf( |
|
| 358 | - esc_html__( |
|
| 359 | - 'A valid EE_Model_Field_Base could not be found for the given field name: %s', |
|
| 360 | - 'event_espresso' |
|
| 361 | - ), |
|
| 362 | - $field_name |
|
| 363 | - ) |
|
| 364 | - ); |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Set custom select values for model. |
|
| 371 | - * |
|
| 372 | - * @param array $custom_select_values |
|
| 373 | - */ |
|
| 374 | - public function setCustomSelectsValues(array $custom_select_values) |
|
| 375 | - { |
|
| 376 | - $this->custom_selection_results = $custom_select_values; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Returns the custom select value for the provided alias if its set. |
|
| 382 | - * If not set, returns null. |
|
| 383 | - * |
|
| 384 | - * @param string $alias |
|
| 385 | - * @return string|int|float|null |
|
| 386 | - */ |
|
| 387 | - public function getCustomSelect($alias) |
|
| 388 | - { |
|
| 389 | - return isset($this->custom_selection_results[ $alias ]) |
|
| 390 | - ? $this->custom_selection_results[ $alias ] |
|
| 391 | - : null; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * This sets the field value on the db column if it exists for the given $column_name or |
|
| 397 | - * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 398 | - * |
|
| 399 | - * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 400 | - * @param string $field_name Must be the exact column name. |
|
| 401 | - * @param mixed $field_value The value to set. |
|
| 402 | - * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 403 | - * @throws InvalidArgumentException |
|
| 404 | - * @throws InvalidInterfaceException |
|
| 405 | - * @throws InvalidDataTypeException |
|
| 406 | - * @throws EE_Error |
|
| 407 | - * @throws ReflectionException |
|
| 408 | - */ |
|
| 409 | - public function set_field_or_extra_meta($field_name, $field_value) |
|
| 410 | - { |
|
| 411 | - if ($this->get_model()->has_field($field_name)) { |
|
| 412 | - $this->set($field_name, $field_value); |
|
| 413 | - return true; |
|
| 414 | - } |
|
| 415 | - // ensure this object is saved first so that extra meta can be properly related. |
|
| 416 | - $this->save(); |
|
| 417 | - return $this->update_extra_meta($field_name, $field_value); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * This retrieves the value of the db column set on this class or if that's not present |
|
| 423 | - * it will attempt to retrieve from extra_meta if found. |
|
| 424 | - * Example Usage: |
|
| 425 | - * Via EE_Message child class: |
|
| 426 | - * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 427 | - * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 428 | - * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 429 | - * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 430 | - * value for those extra fields dynamically via the EE_message object. |
|
| 431 | - * |
|
| 432 | - * @param string $field_name expecting the fully qualified field name. |
|
| 433 | - * @return mixed|null value for the field if found. null if not found. |
|
| 434 | - * @throws ReflectionException |
|
| 435 | - * @throws InvalidArgumentException |
|
| 436 | - * @throws InvalidInterfaceException |
|
| 437 | - * @throws InvalidDataTypeException |
|
| 438 | - * @throws EE_Error |
|
| 439 | - */ |
|
| 440 | - public function get_field_or_extra_meta($field_name) |
|
| 441 | - { |
|
| 442 | - if ($this->get_model()->has_field($field_name)) { |
|
| 443 | - $column_value = $this->get($field_name); |
|
| 444 | - } else { |
|
| 445 | - // This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 446 | - $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 447 | - } |
|
| 448 | - return $column_value; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 454 | - * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 455 | - * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 456 | - * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 457 | - * |
|
| 458 | - * @access public |
|
| 459 | - * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 460 | - * @return void |
|
| 461 | - * @throws InvalidArgumentException |
|
| 462 | - * @throws InvalidInterfaceException |
|
| 463 | - * @throws InvalidDataTypeException |
|
| 464 | - * @throws EE_Error |
|
| 465 | - * @throws ReflectionException |
|
| 466 | - */ |
|
| 467 | - public function set_timezone($timezone = '') |
|
| 468 | - { |
|
| 469 | - $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 470 | - // make sure we clear all cached properties because they won't be relevant now |
|
| 471 | - $this->_clear_cached_properties(); |
|
| 472 | - // make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 473 | - $model_fields = $this->get_model()->field_settings(false); |
|
| 474 | - foreach ($model_fields as $field_name => $field_obj) { |
|
| 475 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 476 | - $field_obj->set_timezone($this->_timezone); |
|
| 477 | - if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
| 478 | - EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * This just returns whatever is set for the current timezone. |
|
| 487 | - * |
|
| 488 | - * @access public |
|
| 489 | - * @return string timezone string |
|
| 490 | - */ |
|
| 491 | - public function get_timezone() |
|
| 492 | - { |
|
| 493 | - return $this->_timezone; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 499 | - * internally instead of wp set date format options |
|
| 500 | - * |
|
| 501 | - * @since 4.6 |
|
| 502 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 503 | - */ |
|
| 504 | - public function set_date_format($format) |
|
| 505 | - { |
|
| 506 | - $this->_dt_frmt = $format; |
|
| 507 | - // clear cached_properties because they won't be relevant now. |
|
| 508 | - $this->_clear_cached_properties(); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 514 | - * class internally instead of wp set time format options. |
|
| 515 | - * |
|
| 516 | - * @since 4.6 |
|
| 517 | - * @param string $format should be a format recognizable by PHP date() functions. |
|
| 518 | - */ |
|
| 519 | - public function set_time_format($format) |
|
| 520 | - { |
|
| 521 | - $this->_tm_frmt = $format; |
|
| 522 | - // clear cached_properties because they won't be relevant now. |
|
| 523 | - $this->_clear_cached_properties(); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * This returns the current internal set format for the date and time formats. |
|
| 529 | - * |
|
| 530 | - * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 531 | - * where the first value is the date format and the second value is the time format. |
|
| 532 | - * @return mixed string|array |
|
| 533 | - */ |
|
| 534 | - public function get_format($full = true) |
|
| 535 | - { |
|
| 536 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * cache |
|
| 542 | - * stores the passed model object on the current model object. |
|
| 543 | - * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 544 | - * |
|
| 545 | - * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 546 | - * 'Registration' associated with this model object |
|
| 547 | - * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 548 | - * that could be a payment or a registration) |
|
| 549 | - * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 550 | - * items which will be stored in an array on this object |
|
| 551 | - * @throws ReflectionException |
|
| 552 | - * @throws InvalidArgumentException |
|
| 553 | - * @throws InvalidInterfaceException |
|
| 554 | - * @throws InvalidDataTypeException |
|
| 555 | - * @throws EE_Error |
|
| 556 | - * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 557 | - * related thing, no array) |
|
| 558 | - */ |
|
| 559 | - public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 560 | - { |
|
| 561 | - // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 562 | - if (! $object_to_cache instanceof EE_Base_Class) { |
|
| 563 | - return false; |
|
| 564 | - } |
|
| 565 | - // also get "how" the object is related, or throw an error |
|
| 566 | - if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 567 | - throw new EE_Error( |
|
| 568 | - sprintf( |
|
| 569 | - esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 570 | - $relationName, |
|
| 571 | - get_class($this) |
|
| 572 | - ) |
|
| 573 | - ); |
|
| 574 | - } |
|
| 575 | - // how many things are related ? |
|
| 576 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 577 | - // if it's a "belongs to" relationship, then there's only one related model object |
|
| 578 | - // eg, if this is a registration, there's only 1 attendee for it |
|
| 579 | - // so for these model objects just set it to be cached |
|
| 580 | - $this->_model_relations[ $relationName ] = $object_to_cache; |
|
| 581 | - $return = true; |
|
| 582 | - } else { |
|
| 583 | - // otherwise, this is the "many" side of a one to many relationship, |
|
| 584 | - // so we'll add the object to the array of related objects for that type. |
|
| 585 | - // eg: if this is an event, there are many registrations for that event, |
|
| 586 | - // so we cache the registrations in an array |
|
| 587 | - if (! is_array($this->_model_relations[ $relationName ])) { |
|
| 588 | - // if for some reason, the cached item is a model object, |
|
| 589 | - // then stick that in the array, otherwise start with an empty array |
|
| 590 | - $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
| 591 | - instanceof |
|
| 592 | - EE_Base_Class |
|
| 593 | - ? array($this->_model_relations[ $relationName ]) : array(); |
|
| 594 | - } |
|
| 595 | - // first check for a cache_id which is normally empty |
|
| 596 | - if (! empty($cache_id)) { |
|
| 597 | - // if the cache_id exists, then it means we are purposely trying to cache this |
|
| 598 | - // with a known key that can then be used to retrieve the object later on |
|
| 599 | - $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
| 600 | - $return = $cache_id; |
|
| 601 | - } elseif ($object_to_cache->ID()) { |
|
| 602 | - // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 603 | - $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
| 604 | - $return = $object_to_cache->ID(); |
|
| 605 | - } else { |
|
| 606 | - // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 607 | - $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
| 608 | - // move the internal pointer to the end of the array |
|
| 609 | - end($this->_model_relations[ $relationName ]); |
|
| 610 | - // and grab the key so that we can return it |
|
| 611 | - $return = key($this->_model_relations[ $relationName ]); |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - return $return; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - |
|
| 618 | - /** |
|
| 619 | - * For adding an item to the cached_properties property. |
|
| 620 | - * |
|
| 621 | - * @access protected |
|
| 622 | - * @param string $fieldname the property item the corresponding value is for. |
|
| 623 | - * @param mixed $value The value we are caching. |
|
| 624 | - * @param string|null $cache_type |
|
| 625 | - * @return void |
|
| 626 | - * @throws ReflectionException |
|
| 627 | - * @throws InvalidArgumentException |
|
| 628 | - * @throws InvalidInterfaceException |
|
| 629 | - * @throws InvalidDataTypeException |
|
| 630 | - * @throws EE_Error |
|
| 631 | - */ |
|
| 632 | - protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 633 | - { |
|
| 634 | - // first make sure this property exists |
|
| 635 | - $this->get_model()->field_settings_for($fieldname); |
|
| 636 | - $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 637 | - $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - |
|
| 641 | - /** |
|
| 642 | - * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 643 | - * This also SETS the cache if we return the actual property! |
|
| 644 | - * |
|
| 645 | - * @param string $fieldname the name of the property we're trying to retrieve |
|
| 646 | - * @param bool $pretty |
|
| 647 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 648 | - * (in cases where the same property may be used for different outputs |
|
| 649 | - * - i.e. datetime, money etc.) |
|
| 650 | - * It can also accept certain pre-defined "schema" strings |
|
| 651 | - * to define how to output the property. |
|
| 652 | - * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 653 | - * @return mixed whatever the value for the property is we're retrieving |
|
| 654 | - * @throws ReflectionException |
|
| 655 | - * @throws InvalidArgumentException |
|
| 656 | - * @throws InvalidInterfaceException |
|
| 657 | - * @throws InvalidDataTypeException |
|
| 658 | - * @throws EE_Error |
|
| 659 | - */ |
|
| 660 | - protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 661 | - { |
|
| 662 | - // verify the field exists |
|
| 663 | - $model = $this->get_model(); |
|
| 664 | - $model->field_settings_for($fieldname); |
|
| 665 | - $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 666 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 667 | - if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
| 668 | - return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
| 669 | - } |
|
| 670 | - $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
| 671 | - $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 672 | - return $value; |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * If the cache didn't fetch the needed item, this fetches it. |
|
| 678 | - * |
|
| 679 | - * @param string $fieldname |
|
| 680 | - * @param bool $pretty |
|
| 681 | - * @param string $extra_cache_ref |
|
| 682 | - * @return mixed |
|
| 683 | - * @throws InvalidArgumentException |
|
| 684 | - * @throws InvalidInterfaceException |
|
| 685 | - * @throws InvalidDataTypeException |
|
| 686 | - * @throws EE_Error |
|
| 687 | - * @throws ReflectionException |
|
| 688 | - */ |
|
| 689 | - protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 690 | - { |
|
| 691 | - $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 692 | - // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 693 | - if ($field_obj instanceof EE_Datetime_Field) { |
|
| 694 | - $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 695 | - } |
|
| 696 | - if (! isset($this->_fields[ $fieldname ])) { |
|
| 697 | - $this->_fields[ $fieldname ] = null; |
|
| 698 | - } |
|
| 699 | - $value = $pretty |
|
| 700 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
| 701 | - : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
| 702 | - return $value; |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 708 | - * |
|
| 709 | - * @param \EE_Datetime_Field $datetime_field |
|
| 710 | - * @param bool $pretty |
|
| 711 | - * @param null $date_or_time |
|
| 712 | - * @return void |
|
| 713 | - * @throws InvalidArgumentException |
|
| 714 | - * @throws InvalidInterfaceException |
|
| 715 | - * @throws InvalidDataTypeException |
|
| 716 | - * @throws EE_Error |
|
| 717 | - */ |
|
| 718 | - protected function _prepare_datetime_field( |
|
| 719 | - EE_Datetime_Field $datetime_field, |
|
| 720 | - $pretty = false, |
|
| 721 | - $date_or_time = null |
|
| 722 | - ) { |
|
| 723 | - $datetime_field->set_timezone($this->_timezone); |
|
| 724 | - $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 725 | - $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 726 | - // set the output returned |
|
| 727 | - switch ($date_or_time) { |
|
| 728 | - case 'D': |
|
| 729 | - $datetime_field->set_date_time_output('date'); |
|
| 730 | - break; |
|
| 731 | - case 'T': |
|
| 732 | - $datetime_field->set_date_time_output('time'); |
|
| 733 | - break; |
|
| 734 | - default: |
|
| 735 | - $datetime_field->set_date_time_output(); |
|
| 736 | - } |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * This just takes care of clearing out the cached_properties |
|
| 742 | - * |
|
| 743 | - * @return void |
|
| 744 | - */ |
|
| 745 | - protected function _clear_cached_properties() |
|
| 746 | - { |
|
| 747 | - $this->_cached_properties = array(); |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - |
|
| 751 | - /** |
|
| 752 | - * This just clears out ONE property if it exists in the cache |
|
| 753 | - * |
|
| 754 | - * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 755 | - * @return void |
|
| 756 | - */ |
|
| 757 | - protected function _clear_cached_property($property_name) |
|
| 758 | - { |
|
| 759 | - if (isset($this->_cached_properties[ $property_name ])) { |
|
| 760 | - unset($this->_cached_properties[ $property_name ]); |
|
| 761 | - } |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * Ensures that this related thing is a model object. |
|
| 767 | - * |
|
| 768 | - * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 769 | - * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 770 | - * @return EE_Base_Class |
|
| 771 | - * @throws ReflectionException |
|
| 772 | - * @throws InvalidArgumentException |
|
| 773 | - * @throws InvalidInterfaceException |
|
| 774 | - * @throws InvalidDataTypeException |
|
| 775 | - * @throws EE_Error |
|
| 776 | - */ |
|
| 777 | - protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 778 | - { |
|
| 779 | - $other_model_instance = self::_get_model_instance_with_name( |
|
| 780 | - self::_get_model_classname($model_name), |
|
| 781 | - $this->_timezone |
|
| 782 | - ); |
|
| 783 | - return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - |
|
| 787 | - /** |
|
| 788 | - * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 789 | - * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 790 | - * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 791 | - * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 792 | - * |
|
| 793 | - * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 794 | - * Eg 'Registration' |
|
| 795 | - * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 796 | - * if you intend to use $clear_all = TRUE, or the relation only |
|
| 797 | - * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 798 | - * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 799 | - * this is HasMany or HABTM. |
|
| 800 | - * @throws ReflectionException |
|
| 801 | - * @throws InvalidArgumentException |
|
| 802 | - * @throws InvalidInterfaceException |
|
| 803 | - * @throws InvalidDataTypeException |
|
| 804 | - * @throws EE_Error |
|
| 805 | - * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 806 | - * relation from all |
|
| 807 | - */ |
|
| 808 | - public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 809 | - { |
|
| 810 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 811 | - $index_in_cache = ''; |
|
| 812 | - if (! $relationship_to_model) { |
|
| 813 | - throw new EE_Error( |
|
| 814 | - sprintf( |
|
| 815 | - esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
|
| 816 | - $relationName, |
|
| 817 | - get_class($this) |
|
| 818 | - ) |
|
| 819 | - ); |
|
| 820 | - } |
|
| 821 | - if ($clear_all) { |
|
| 822 | - $obj_removed = true; |
|
| 823 | - $this->_model_relations[ $relationName ] = null; |
|
| 824 | - } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 825 | - $obj_removed = $this->_model_relations[ $relationName ]; |
|
| 826 | - $this->_model_relations[ $relationName ] = null; |
|
| 827 | - } else { |
|
| 828 | - if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 829 | - && $object_to_remove_or_index_into_array->ID() |
|
| 830 | - ) { |
|
| 831 | - $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 832 | - if (is_array($this->_model_relations[ $relationName ]) |
|
| 833 | - && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
| 834 | - ) { |
|
| 835 | - $index_found_at = null; |
|
| 836 | - // find this object in the array even though it has a different key |
|
| 837 | - foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
| 838 | - /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 839 | - if ($obj instanceof EE_Base_Class |
|
| 840 | - && ( |
|
| 841 | - $obj == $object_to_remove_or_index_into_array |
|
| 842 | - || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 843 | - ) |
|
| 844 | - ) { |
|
| 845 | - $index_found_at = $index; |
|
| 846 | - break; |
|
| 847 | - } |
|
| 848 | - } |
|
| 849 | - if ($index_found_at) { |
|
| 850 | - $index_in_cache = $index_found_at; |
|
| 851 | - } else { |
|
| 852 | - // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 853 | - // if it wasn't in it to begin with. So we're done |
|
| 854 | - return $object_to_remove_or_index_into_array; |
|
| 855 | - } |
|
| 856 | - } |
|
| 857 | - } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 858 | - // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 859 | - foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 860 | - /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 861 | - if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 862 | - $index_in_cache = $index; |
|
| 863 | - } |
|
| 864 | - } |
|
| 865 | - } else { |
|
| 866 | - $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 867 | - } |
|
| 868 | - // supposedly we've found it. But it could just be that the client code |
|
| 869 | - // provided a bad index/object |
|
| 870 | - if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
| 871 | - $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
| 872 | - unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
| 873 | - } else { |
|
| 874 | - // that thing was never cached anyways. |
|
| 875 | - $obj_removed = null; |
|
| 876 | - } |
|
| 877 | - } |
|
| 878 | - return $obj_removed; |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * update_cache_after_object_save |
|
| 884 | - * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 885 | - * obtained after being saved to the db |
|
| 886 | - * |
|
| 887 | - * @param string $relationName - the type of object that is cached |
|
| 888 | - * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 889 | - * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 890 | - * @return boolean TRUE on success, FALSE on fail |
|
| 891 | - * @throws ReflectionException |
|
| 892 | - * @throws InvalidArgumentException |
|
| 893 | - * @throws InvalidInterfaceException |
|
| 894 | - * @throws InvalidDataTypeException |
|
| 895 | - * @throws EE_Error |
|
| 896 | - */ |
|
| 897 | - public function update_cache_after_object_save( |
|
| 898 | - $relationName, |
|
| 899 | - EE_Base_Class $newly_saved_object, |
|
| 900 | - $current_cache_id = '' |
|
| 901 | - ) { |
|
| 902 | - // verify that incoming object is of the correct type |
|
| 903 | - $obj_class = 'EE_' . $relationName; |
|
| 904 | - if ($newly_saved_object instanceof $obj_class) { |
|
| 905 | - /* @type EE_Base_Class $newly_saved_object */ |
|
| 906 | - // now get the type of relation |
|
| 907 | - $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 908 | - // if this is a 1:1 relationship |
|
| 909 | - if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 910 | - // then just replace the cached object with the newly saved object |
|
| 911 | - $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
| 912 | - return true; |
|
| 913 | - // or if it's some kind of sordid feral polyamorous relationship... |
|
| 914 | - } |
|
| 915 | - if (is_array($this->_model_relations[ $relationName ]) |
|
| 916 | - && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
| 917 | - ) { |
|
| 918 | - // then remove the current cached item |
|
| 919 | - unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
| 920 | - // and cache the newly saved object using it's new ID |
|
| 921 | - $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
| 922 | - return true; |
|
| 923 | - } |
|
| 924 | - } |
|
| 925 | - return false; |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 931 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 932 | - * |
|
| 933 | - * @param string $relationName |
|
| 934 | - * @return EE_Base_Class |
|
| 935 | - */ |
|
| 936 | - public function get_one_from_cache($relationName) |
|
| 937 | - { |
|
| 938 | - $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
| 939 | - ? $this->_model_relations[ $relationName ] |
|
| 940 | - : null; |
|
| 941 | - if (is_array($cached_array_or_object)) { |
|
| 942 | - return array_shift($cached_array_or_object); |
|
| 943 | - } |
|
| 944 | - return $cached_array_or_object; |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - |
|
| 948 | - /** |
|
| 949 | - * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 950 | - * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 951 | - * |
|
| 952 | - * @param string $relationName |
|
| 953 | - * @throws ReflectionException |
|
| 954 | - * @throws InvalidArgumentException |
|
| 955 | - * @throws InvalidInterfaceException |
|
| 956 | - * @throws InvalidDataTypeException |
|
| 957 | - * @throws EE_Error |
|
| 958 | - * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 959 | - */ |
|
| 960 | - public function get_all_from_cache($relationName) |
|
| 961 | - { |
|
| 962 | - $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
| 963 | - // if the result is not an array, but exists, make it an array |
|
| 964 | - $objects = is_array($objects) ? $objects : array($objects); |
|
| 965 | - // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 966 | - // basically, if this model object was stored in the session, and these cached model objects |
|
| 967 | - // already have IDs, let's make sure they're in their model's entity mapper |
|
| 968 | - // otherwise we will have duplicates next time we call |
|
| 969 | - // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 970 | - $model = EE_Registry::instance()->load_model($relationName); |
|
| 971 | - foreach ($objects as $model_object) { |
|
| 972 | - if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 973 | - // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 974 | - if ($model_object->ID()) { |
|
| 975 | - $model->add_to_entity_map($model_object); |
|
| 976 | - } |
|
| 977 | - } else { |
|
| 978 | - throw new EE_Error( |
|
| 979 | - sprintf( |
|
| 980 | - esc_html__( |
|
| 981 | - 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 982 | - 'event_espresso' |
|
| 983 | - ), |
|
| 984 | - $relationName, |
|
| 985 | - gettype($model_object) |
|
| 986 | - ) |
|
| 987 | - ); |
|
| 988 | - } |
|
| 989 | - } |
|
| 990 | - return $objects; |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - |
|
| 994 | - /** |
|
| 995 | - * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 996 | - * matching the given query conditions. |
|
| 997 | - * |
|
| 998 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 999 | - * @param int $limit How many objects to return. |
|
| 1000 | - * @param array $query_params Any additional conditions on the query. |
|
| 1001 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1002 | - * you can indicate just the columns you want returned |
|
| 1003 | - * @return array|EE_Base_Class[] |
|
| 1004 | - * @throws ReflectionException |
|
| 1005 | - * @throws InvalidArgumentException |
|
| 1006 | - * @throws InvalidInterfaceException |
|
| 1007 | - * @throws InvalidDataTypeException |
|
| 1008 | - * @throws EE_Error |
|
| 1009 | - */ |
|
| 1010 | - public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 1011 | - { |
|
| 1012 | - $model = $this->get_model(); |
|
| 1013 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1014 | - ? $model->get_primary_key_field()->get_name() |
|
| 1015 | - : $field_to_order_by; |
|
| 1016 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1017 | - if (empty($field) || empty($current_value)) { |
|
| 1018 | - return array(); |
|
| 1019 | - } |
|
| 1020 | - return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - |
|
| 1024 | - /** |
|
| 1025 | - * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 1026 | - * matching the given query conditions. |
|
| 1027 | - * |
|
| 1028 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1029 | - * @param int $limit How many objects to return. |
|
| 1030 | - * @param array $query_params Any additional conditions on the query. |
|
| 1031 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1032 | - * you can indicate just the columns you want returned |
|
| 1033 | - * @return array|EE_Base_Class[] |
|
| 1034 | - * @throws ReflectionException |
|
| 1035 | - * @throws InvalidArgumentException |
|
| 1036 | - * @throws InvalidInterfaceException |
|
| 1037 | - * @throws InvalidDataTypeException |
|
| 1038 | - * @throws EE_Error |
|
| 1039 | - */ |
|
| 1040 | - public function previous_x( |
|
| 1041 | - $field_to_order_by = null, |
|
| 1042 | - $limit = 1, |
|
| 1043 | - $query_params = array(), |
|
| 1044 | - $columns_to_select = null |
|
| 1045 | - ) { |
|
| 1046 | - $model = $this->get_model(); |
|
| 1047 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1048 | - ? $model->get_primary_key_field()->get_name() |
|
| 1049 | - : $field_to_order_by; |
|
| 1050 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1051 | - if (empty($field) || empty($current_value)) { |
|
| 1052 | - return array(); |
|
| 1053 | - } |
|
| 1054 | - return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - |
|
| 1058 | - /** |
|
| 1059 | - * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 1060 | - * matching the given query conditions. |
|
| 1061 | - * |
|
| 1062 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1063 | - * @param array $query_params Any additional conditions on the query. |
|
| 1064 | - * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1065 | - * you can indicate just the columns you want returned |
|
| 1066 | - * @return array|EE_Base_Class |
|
| 1067 | - * @throws ReflectionException |
|
| 1068 | - * @throws InvalidArgumentException |
|
| 1069 | - * @throws InvalidInterfaceException |
|
| 1070 | - * @throws InvalidDataTypeException |
|
| 1071 | - * @throws EE_Error |
|
| 1072 | - */ |
|
| 1073 | - public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1074 | - { |
|
| 1075 | - $model = $this->get_model(); |
|
| 1076 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1077 | - ? $model->get_primary_key_field()->get_name() |
|
| 1078 | - : $field_to_order_by; |
|
| 1079 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1080 | - if (empty($field) || empty($current_value)) { |
|
| 1081 | - return array(); |
|
| 1082 | - } |
|
| 1083 | - return $model->next($current_value, $field, $query_params, $columns_to_select); |
|
| 1084 | - } |
|
| 1085 | - |
|
| 1086 | - |
|
| 1087 | - /** |
|
| 1088 | - * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 1089 | - * matching the given query conditions. |
|
| 1090 | - * |
|
| 1091 | - * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1092 | - * @param array $query_params Any additional conditions on the query. |
|
| 1093 | - * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 1094 | - * you can indicate just the column you want returned |
|
| 1095 | - * @return array|EE_Base_Class |
|
| 1096 | - * @throws ReflectionException |
|
| 1097 | - * @throws InvalidArgumentException |
|
| 1098 | - * @throws InvalidInterfaceException |
|
| 1099 | - * @throws InvalidDataTypeException |
|
| 1100 | - * @throws EE_Error |
|
| 1101 | - */ |
|
| 1102 | - public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1103 | - { |
|
| 1104 | - $model = $this->get_model(); |
|
| 1105 | - $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1106 | - ? $model->get_primary_key_field()->get_name() |
|
| 1107 | - : $field_to_order_by; |
|
| 1108 | - $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1109 | - if (empty($field) || empty($current_value)) { |
|
| 1110 | - return array(); |
|
| 1111 | - } |
|
| 1112 | - return $model->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - |
|
| 1116 | - /** |
|
| 1117 | - * Overrides parent because parent expects old models. |
|
| 1118 | - * This also doesn't do any validation, and won't work for serialized arrays |
|
| 1119 | - * |
|
| 1120 | - * @param string $field_name |
|
| 1121 | - * @param mixed $field_value_from_db |
|
| 1122 | - * @throws ReflectionException |
|
| 1123 | - * @throws InvalidArgumentException |
|
| 1124 | - * @throws InvalidInterfaceException |
|
| 1125 | - * @throws InvalidDataTypeException |
|
| 1126 | - * @throws EE_Error |
|
| 1127 | - */ |
|
| 1128 | - public function set_from_db($field_name, $field_value_from_db) |
|
| 1129 | - { |
|
| 1130 | - $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1131 | - if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1132 | - // you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1133 | - // eg, a CPT model object could have an entry in the posts table, but no |
|
| 1134 | - // entry in the meta table. Meaning that all its columns in the meta table |
|
| 1135 | - // are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1136 | - if ($field_value_from_db === null) { |
|
| 1137 | - if ($field_obj->is_nullable()) { |
|
| 1138 | - // if the field allows nulls, then let it be null |
|
| 1139 | - $field_value = null; |
|
| 1140 | - } else { |
|
| 1141 | - $field_value = $field_obj->get_default_value(); |
|
| 1142 | - } |
|
| 1143 | - } else { |
|
| 1144 | - $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1145 | - } |
|
| 1146 | - $this->_fields[ $field_name ] = $field_value; |
|
| 1147 | - $this->_clear_cached_property($field_name); |
|
| 1148 | - } |
|
| 1149 | - } |
|
| 1150 | - |
|
| 1151 | - |
|
| 1152 | - /** |
|
| 1153 | - * verifies that the specified field is of the correct type |
|
| 1154 | - * |
|
| 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 ReflectionException |
|
| 1161 | - * @throws InvalidArgumentException |
|
| 1162 | - * @throws InvalidInterfaceException |
|
| 1163 | - * @throws InvalidDataTypeException |
|
| 1164 | - * @throws EE_Error |
|
| 1165 | - */ |
|
| 1166 | - public function get($field_name, $extra_cache_ref = null) |
|
| 1167 | - { |
|
| 1168 | - return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1169 | - } |
|
| 1170 | - |
|
| 1171 | - |
|
| 1172 | - /** |
|
| 1173 | - * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1174 | - * |
|
| 1175 | - * @param string $field_name A valid fieldname |
|
| 1176 | - * @return mixed Whatever the raw value stored on the property is. |
|
| 1177 | - * @throws ReflectionException |
|
| 1178 | - * @throws InvalidArgumentException |
|
| 1179 | - * @throws InvalidInterfaceException |
|
| 1180 | - * @throws InvalidDataTypeException |
|
| 1181 | - * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1182 | - */ |
|
| 1183 | - public function get_raw($field_name) |
|
| 1184 | - { |
|
| 1185 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1186 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1187 | - ? $this->_fields[ $field_name ]->format('U') |
|
| 1188 | - : $this->_fields[ $field_name ]; |
|
| 1189 | - } |
|
| 1190 | - |
|
| 1191 | - |
|
| 1192 | - /** |
|
| 1193 | - * This is used to return the internal DateTime object used for a field that is a |
|
| 1194 | - * EE_Datetime_Field. |
|
| 1195 | - * |
|
| 1196 | - * @param string $field_name The field name retrieving the DateTime object. |
|
| 1197 | - * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1198 | - * @throws EE_Error an error is set and false returned. If the field IS an |
|
| 1199 | - * EE_Datetime_Field and but the field value is null, then |
|
| 1200 | - * just null is returned (because that indicates that likely |
|
| 1201 | - * this field is nullable). |
|
| 1202 | - * @throws InvalidArgumentException |
|
| 1203 | - * @throws InvalidDataTypeException |
|
| 1204 | - * @throws InvalidInterfaceException |
|
| 1205 | - * @throws ReflectionException |
|
| 1206 | - */ |
|
| 1207 | - public function get_DateTime_object($field_name) |
|
| 1208 | - { |
|
| 1209 | - $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1210 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1211 | - EE_Error::add_error( |
|
| 1212 | - sprintf( |
|
| 1213 | - esc_html__( |
|
| 1214 | - 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1215 | - 'event_espresso' |
|
| 1216 | - ), |
|
| 1217 | - $field_name |
|
| 1218 | - ), |
|
| 1219 | - __FILE__, |
|
| 1220 | - __FUNCTION__, |
|
| 1221 | - __LINE__ |
|
| 1222 | - ); |
|
| 1223 | - return false; |
|
| 1224 | - } |
|
| 1225 | - return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1226 | - ? clone $this->_fields[ $field_name ] |
|
| 1227 | - : null; |
|
| 1228 | - } |
|
| 1229 | - |
|
| 1230 | - |
|
| 1231 | - /** |
|
| 1232 | - * To be used in template to immediately echo out the value, and format it for output. |
|
| 1233 | - * Eg, should call stripslashes and whatnot before echoing |
|
| 1234 | - * |
|
| 1235 | - * @param string $field_name the name of the field as it appears in the DB |
|
| 1236 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1237 | - * (in cases where the same property may be used for different outputs |
|
| 1238 | - * - i.e. datetime, money etc.) |
|
| 1239 | - * @return void |
|
| 1240 | - * @throws ReflectionException |
|
| 1241 | - * @throws InvalidArgumentException |
|
| 1242 | - * @throws InvalidInterfaceException |
|
| 1243 | - * @throws InvalidDataTypeException |
|
| 1244 | - * @throws EE_Error |
|
| 1245 | - */ |
|
| 1246 | - public function e($field_name, $extra_cache_ref = null) |
|
| 1247 | - { |
|
| 1248 | - echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1249 | - } |
|
| 1250 | - |
|
| 1251 | - |
|
| 1252 | - /** |
|
| 1253 | - * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1254 | - * can be easily used as the value of form input. |
|
| 1255 | - * |
|
| 1256 | - * @param string $field_name |
|
| 1257 | - * @return void |
|
| 1258 | - * @throws ReflectionException |
|
| 1259 | - * @throws InvalidArgumentException |
|
| 1260 | - * @throws InvalidInterfaceException |
|
| 1261 | - * @throws InvalidDataTypeException |
|
| 1262 | - * @throws EE_Error |
|
| 1263 | - */ |
|
| 1264 | - public function f($field_name) |
|
| 1265 | - { |
|
| 1266 | - $this->e($field_name, 'form_input'); |
|
| 1267 | - } |
|
| 1268 | - |
|
| 1269 | - |
|
| 1270 | - /** |
|
| 1271 | - * Same as `f()` but just returns the value instead of echoing it |
|
| 1272 | - * |
|
| 1273 | - * @param string $field_name |
|
| 1274 | - * @return string |
|
| 1275 | - * @throws ReflectionException |
|
| 1276 | - * @throws InvalidArgumentException |
|
| 1277 | - * @throws InvalidInterfaceException |
|
| 1278 | - * @throws InvalidDataTypeException |
|
| 1279 | - * @throws EE_Error |
|
| 1280 | - */ |
|
| 1281 | - public function get_f($field_name) |
|
| 1282 | - { |
|
| 1283 | - return (string) $this->get_pretty($field_name, 'form_input'); |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - |
|
| 1287 | - /** |
|
| 1288 | - * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1289 | - * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1290 | - * to see what options are available. |
|
| 1291 | - * |
|
| 1292 | - * @param string $field_name |
|
| 1293 | - * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1294 | - * (in cases where the same property may be used for different outputs |
|
| 1295 | - * - i.e. datetime, money etc.) |
|
| 1296 | - * @return mixed |
|
| 1297 | - * @throws ReflectionException |
|
| 1298 | - * @throws InvalidArgumentException |
|
| 1299 | - * @throws InvalidInterfaceException |
|
| 1300 | - * @throws InvalidDataTypeException |
|
| 1301 | - * @throws EE_Error |
|
| 1302 | - */ |
|
| 1303 | - public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1304 | - { |
|
| 1305 | - return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1306 | - } |
|
| 1307 | - |
|
| 1308 | - |
|
| 1309 | - /** |
|
| 1310 | - * This simply returns the datetime for the given field name |
|
| 1311 | - * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1312 | - * (and the equivalent e_date, e_time, e_datetime). |
|
| 1313 | - * |
|
| 1314 | - * @access protected |
|
| 1315 | - * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1316 | - * @param string $dt_frmt valid datetime format used for date |
|
| 1317 | - * (if '' then we just use the default on the field, |
|
| 1318 | - * if NULL we use the last-used format) |
|
| 1319 | - * @param string $tm_frmt Same as above except this is for time format |
|
| 1320 | - * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1321 | - * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1322 | - * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1323 | - * if field is not a valid dtt field, or void if echoing |
|
| 1324 | - * @throws ReflectionException |
|
| 1325 | - * @throws InvalidArgumentException |
|
| 1326 | - * @throws InvalidInterfaceException |
|
| 1327 | - * @throws InvalidDataTypeException |
|
| 1328 | - * @throws EE_Error |
|
| 1329 | - */ |
|
| 1330 | - protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1331 | - { |
|
| 1332 | - // clear cached property |
|
| 1333 | - $this->_clear_cached_property($field_name); |
|
| 1334 | - // reset format properties because they are used in get() |
|
| 1335 | - $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1336 | - $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1337 | - if ($echo) { |
|
| 1338 | - $this->e($field_name, $date_or_time); |
|
| 1339 | - return ''; |
|
| 1340 | - } |
|
| 1341 | - return $this->get($field_name, $date_or_time); |
|
| 1342 | - } |
|
| 1343 | - |
|
| 1344 | - |
|
| 1345 | - /** |
|
| 1346 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1347 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1348 | - * other echoes the pretty value for dtt) |
|
| 1349 | - * |
|
| 1350 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1351 | - * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1352 | - * @return string datetime value formatted |
|
| 1353 | - * @throws ReflectionException |
|
| 1354 | - * @throws InvalidArgumentException |
|
| 1355 | - * @throws InvalidInterfaceException |
|
| 1356 | - * @throws InvalidDataTypeException |
|
| 1357 | - * @throws EE_Error |
|
| 1358 | - */ |
|
| 1359 | - public function get_date($field_name, $format = '') |
|
| 1360 | - { |
|
| 1361 | - return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1362 | - } |
|
| 1363 | - |
|
| 1364 | - |
|
| 1365 | - /** |
|
| 1366 | - * @param $field_name |
|
| 1367 | - * @param string $format |
|
| 1368 | - * @throws ReflectionException |
|
| 1369 | - * @throws InvalidArgumentException |
|
| 1370 | - * @throws InvalidInterfaceException |
|
| 1371 | - * @throws InvalidDataTypeException |
|
| 1372 | - * @throws EE_Error |
|
| 1373 | - */ |
|
| 1374 | - public function e_date($field_name, $format = '') |
|
| 1375 | - { |
|
| 1376 | - $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1377 | - } |
|
| 1378 | - |
|
| 1379 | - |
|
| 1380 | - /** |
|
| 1381 | - * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1382 | - * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1383 | - * other echoes the pretty value for dtt) |
|
| 1384 | - * |
|
| 1385 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1386 | - * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1387 | - * @return string datetime value formatted |
|
| 1388 | - * @throws ReflectionException |
|
| 1389 | - * @throws InvalidArgumentException |
|
| 1390 | - * @throws InvalidInterfaceException |
|
| 1391 | - * @throws InvalidDataTypeException |
|
| 1392 | - * @throws EE_Error |
|
| 1393 | - */ |
|
| 1394 | - public function get_time($field_name, $format = '') |
|
| 1395 | - { |
|
| 1396 | - return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1397 | - } |
|
| 1398 | - |
|
| 1399 | - |
|
| 1400 | - /** |
|
| 1401 | - * @param $field_name |
|
| 1402 | - * @param string $format |
|
| 1403 | - * @throws ReflectionException |
|
| 1404 | - * @throws InvalidArgumentException |
|
| 1405 | - * @throws InvalidInterfaceException |
|
| 1406 | - * @throws InvalidDataTypeException |
|
| 1407 | - * @throws EE_Error |
|
| 1408 | - */ |
|
| 1409 | - public function e_time($field_name, $format = '') |
|
| 1410 | - { |
|
| 1411 | - $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1412 | - } |
|
| 1413 | - |
|
| 1414 | - |
|
| 1415 | - /** |
|
| 1416 | - * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1417 | - * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1418 | - * other echoes the pretty value for dtt) |
|
| 1419 | - * |
|
| 1420 | - * @param string $field_name name of model object datetime field holding the value |
|
| 1421 | - * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1422 | - * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1423 | - * @return string datetime value formatted |
|
| 1424 | - * @throws ReflectionException |
|
| 1425 | - * @throws InvalidArgumentException |
|
| 1426 | - * @throws InvalidInterfaceException |
|
| 1427 | - * @throws InvalidDataTypeException |
|
| 1428 | - * @throws EE_Error |
|
| 1429 | - */ |
|
| 1430 | - public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1431 | - { |
|
| 1432 | - return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1433 | - } |
|
| 1434 | - |
|
| 1435 | - |
|
| 1436 | - /** |
|
| 1437 | - * @param string $field_name |
|
| 1438 | - * @param string $dt_frmt |
|
| 1439 | - * @param string $tm_frmt |
|
| 1440 | - * @throws ReflectionException |
|
| 1441 | - * @throws InvalidArgumentException |
|
| 1442 | - * @throws InvalidInterfaceException |
|
| 1443 | - * @throws InvalidDataTypeException |
|
| 1444 | - * @throws EE_Error |
|
| 1445 | - */ |
|
| 1446 | - public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1447 | - { |
|
| 1448 | - $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1449 | - } |
|
| 1450 | - |
|
| 1451 | - |
|
| 1452 | - /** |
|
| 1453 | - * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1454 | - * |
|
| 1455 | - * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1456 | - * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1457 | - * on the object will be used. |
|
| 1458 | - * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1459 | - * @throws ReflectionException |
|
| 1460 | - * @throws InvalidArgumentException |
|
| 1461 | - * @throws InvalidInterfaceException |
|
| 1462 | - * @throws InvalidDataTypeException |
|
| 1463 | - * @throws EE_Error |
|
| 1464 | - * field name. |
|
| 1465 | - */ |
|
| 1466 | - public function get_i18n_datetime($field_name, $format = '') |
|
| 1467 | - { |
|
| 1468 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1469 | - return date_i18n( |
|
| 1470 | - $format, |
|
| 1471 | - EEH_DTT_Helper::get_timestamp_with_offset( |
|
| 1472 | - $this->get_raw($field_name), |
|
| 1473 | - $this->_timezone |
|
| 1474 | - ) |
|
| 1475 | - ); |
|
| 1476 | - } |
|
| 1477 | - |
|
| 1478 | - |
|
| 1479 | - /** |
|
| 1480 | - * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1481 | - * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1482 | - * thrown. |
|
| 1483 | - * |
|
| 1484 | - * @param string $field_name The field name being checked |
|
| 1485 | - * @throws ReflectionException |
|
| 1486 | - * @throws InvalidArgumentException |
|
| 1487 | - * @throws InvalidInterfaceException |
|
| 1488 | - * @throws InvalidDataTypeException |
|
| 1489 | - * @throws EE_Error |
|
| 1490 | - * @return EE_Datetime_Field |
|
| 1491 | - */ |
|
| 1492 | - protected function _get_dtt_field_settings($field_name) |
|
| 1493 | - { |
|
| 1494 | - $field = $this->get_model()->field_settings_for($field_name); |
|
| 1495 | - // check if field is dtt |
|
| 1496 | - if ($field instanceof EE_Datetime_Field) { |
|
| 1497 | - return $field; |
|
| 1498 | - } |
|
| 1499 | - throw new EE_Error( |
|
| 1500 | - sprintf( |
|
| 1501 | - esc_html__( |
|
| 1502 | - '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', |
|
| 1503 | - 'event_espresso' |
|
| 1504 | - ), |
|
| 1505 | - $field_name, |
|
| 1506 | - self::_get_model_classname(get_class($this)) |
|
| 1507 | - ) |
|
| 1508 | - ); |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - |
|
| 1512 | - |
|
| 1513 | - |
|
| 1514 | - /** |
|
| 1515 | - * NOTE ABOUT BELOW: |
|
| 1516 | - * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1517 | - * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1518 | - * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1519 | - * method and make sure you send the entire datetime value for setting. |
|
| 1520 | - */ |
|
| 1521 | - /** |
|
| 1522 | - * sets the time on a datetime property |
|
| 1523 | - * |
|
| 1524 | - * @access protected |
|
| 1525 | - * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1526 | - * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1527 | - * @throws ReflectionException |
|
| 1528 | - * @throws InvalidArgumentException |
|
| 1529 | - * @throws InvalidInterfaceException |
|
| 1530 | - * @throws InvalidDataTypeException |
|
| 1531 | - * @throws EE_Error |
|
| 1532 | - */ |
|
| 1533 | - protected function _set_time_for($time, $fieldname) |
|
| 1534 | - { |
|
| 1535 | - $this->_set_date_time('T', $time, $fieldname); |
|
| 1536 | - } |
|
| 1537 | - |
|
| 1538 | - |
|
| 1539 | - /** |
|
| 1540 | - * sets the date on a datetime property |
|
| 1541 | - * |
|
| 1542 | - * @access protected |
|
| 1543 | - * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1544 | - * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1545 | - * @throws ReflectionException |
|
| 1546 | - * @throws InvalidArgumentException |
|
| 1547 | - * @throws InvalidInterfaceException |
|
| 1548 | - * @throws InvalidDataTypeException |
|
| 1549 | - * @throws EE_Error |
|
| 1550 | - */ |
|
| 1551 | - protected function _set_date_for($date, $fieldname) |
|
| 1552 | - { |
|
| 1553 | - $this->_set_date_time('D', $date, $fieldname); |
|
| 1554 | - } |
|
| 1555 | - |
|
| 1556 | - |
|
| 1557 | - /** |
|
| 1558 | - * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1559 | - * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1560 | - * |
|
| 1561 | - * @access protected |
|
| 1562 | - * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1563 | - * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1564 | - * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1565 | - * EE_Datetime_Field property) |
|
| 1566 | - * @throws ReflectionException |
|
| 1567 | - * @throws InvalidArgumentException |
|
| 1568 | - * @throws InvalidInterfaceException |
|
| 1569 | - * @throws InvalidDataTypeException |
|
| 1570 | - * @throws EE_Error |
|
| 1571 | - */ |
|
| 1572 | - protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1573 | - { |
|
| 1574 | - $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1575 | - $field->set_timezone($this->_timezone); |
|
| 1576 | - $field->set_date_format($this->_dt_frmt); |
|
| 1577 | - $field->set_time_format($this->_tm_frmt); |
|
| 1578 | - switch ($what) { |
|
| 1579 | - case 'T': |
|
| 1580 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
| 1581 | - $datetime_value, |
|
| 1582 | - $this->_fields[ $fieldname ] |
|
| 1583 | - ); |
|
| 1584 | - $this->_has_changes = true; |
|
| 1585 | - break; |
|
| 1586 | - case 'D': |
|
| 1587 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
| 1588 | - $datetime_value, |
|
| 1589 | - $this->_fields[ $fieldname ] |
|
| 1590 | - ); |
|
| 1591 | - $this->_has_changes = true; |
|
| 1592 | - break; |
|
| 1593 | - case 'B': |
|
| 1594 | - $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
| 1595 | - $this->_has_changes = true; |
|
| 1596 | - break; |
|
| 1597 | - } |
|
| 1598 | - $this->_clear_cached_property($fieldname); |
|
| 1599 | - } |
|
| 1600 | - |
|
| 1601 | - |
|
| 1602 | - /** |
|
| 1603 | - * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1604 | - * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1605 | - * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1606 | - * that could lead to some unexpected results! |
|
| 1607 | - * |
|
| 1608 | - * @access public |
|
| 1609 | - * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1610 | - * value being returned. |
|
| 1611 | - * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1612 | - * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1613 | - * @param string $prepend You can include something to prepend on the timestamp |
|
| 1614 | - * @param string $append You can include something to append on the timestamp |
|
| 1615 | - * @throws ReflectionException |
|
| 1616 | - * @throws InvalidArgumentException |
|
| 1617 | - * @throws InvalidInterfaceException |
|
| 1618 | - * @throws InvalidDataTypeException |
|
| 1619 | - * @throws EE_Error |
|
| 1620 | - * @return string timestamp |
|
| 1621 | - */ |
|
| 1622 | - public function display_in_my_timezone( |
|
| 1623 | - $field_name, |
|
| 1624 | - $callback = 'get_datetime', |
|
| 1625 | - $args = null, |
|
| 1626 | - $prepend = '', |
|
| 1627 | - $append = '' |
|
| 1628 | - ) { |
|
| 1629 | - $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1630 | - if ($timezone === $this->_timezone) { |
|
| 1631 | - return ''; |
|
| 1632 | - } |
|
| 1633 | - $original_timezone = $this->_timezone; |
|
| 1634 | - $this->set_timezone($timezone); |
|
| 1635 | - $fn = (array) $field_name; |
|
| 1636 | - $args = array_merge($fn, (array) $args); |
|
| 1637 | - if (! method_exists($this, $callback)) { |
|
| 1638 | - throw new EE_Error( |
|
| 1639 | - sprintf( |
|
| 1640 | - esc_html__( |
|
| 1641 | - 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1642 | - 'event_espresso' |
|
| 1643 | - ), |
|
| 1644 | - $callback |
|
| 1645 | - ) |
|
| 1646 | - ); |
|
| 1647 | - } |
|
| 1648 | - $args = (array) $args; |
|
| 1649 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1650 | - $this->set_timezone($original_timezone); |
|
| 1651 | - return $return; |
|
| 1652 | - } |
|
| 1653 | - |
|
| 1654 | - |
|
| 1655 | - /** |
|
| 1656 | - * Deletes this model object. |
|
| 1657 | - * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1658 | - * override |
|
| 1659 | - * `EE_Base_Class::_delete` NOT this class. |
|
| 1660 | - * |
|
| 1661 | - * @return boolean | int |
|
| 1662 | - * @throws ReflectionException |
|
| 1663 | - * @throws InvalidArgumentException |
|
| 1664 | - * @throws InvalidInterfaceException |
|
| 1665 | - * @throws InvalidDataTypeException |
|
| 1666 | - * @throws EE_Error |
|
| 1667 | - */ |
|
| 1668 | - public function delete() |
|
| 1669 | - { |
|
| 1670 | - /** |
|
| 1671 | - * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1672 | - * Note: |
|
| 1673 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1674 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1675 | - * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1676 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1677 | - * |
|
| 1678 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1679 | - */ |
|
| 1680 | - do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1681 | - $result = $this->_delete(); |
|
| 1682 | - /** |
|
| 1683 | - * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1684 | - * Note: |
|
| 1685 | - * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1686 | - * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1687 | - * For example `EE_Soft_Base_Class::_delete` |
|
| 1688 | - * soft deletes (trash) the object and does not permanently delete it. |
|
| 1689 | - * |
|
| 1690 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1691 | - * @param boolean $result |
|
| 1692 | - */ |
|
| 1693 | - do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1694 | - return $result; |
|
| 1695 | - } |
|
| 1696 | - |
|
| 1697 | - |
|
| 1698 | - /** |
|
| 1699 | - * Calls the specific delete method for the instantiated class. |
|
| 1700 | - * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1701 | - * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1702 | - * `EE_Base_Class::delete` |
|
| 1703 | - * |
|
| 1704 | - * @return bool|int |
|
| 1705 | - * @throws ReflectionException |
|
| 1706 | - * @throws InvalidArgumentException |
|
| 1707 | - * @throws InvalidInterfaceException |
|
| 1708 | - * @throws InvalidDataTypeException |
|
| 1709 | - * @throws EE_Error |
|
| 1710 | - */ |
|
| 1711 | - protected function _delete() |
|
| 1712 | - { |
|
| 1713 | - return $this->delete_permanently(); |
|
| 1714 | - } |
|
| 1715 | - |
|
| 1716 | - |
|
| 1717 | - /** |
|
| 1718 | - * Deletes this model object permanently from db |
|
| 1719 | - * (but keep in mind related models may block the delete and return an error) |
|
| 1720 | - * |
|
| 1721 | - * @return bool | int |
|
| 1722 | - * @throws ReflectionException |
|
| 1723 | - * @throws InvalidArgumentException |
|
| 1724 | - * @throws InvalidInterfaceException |
|
| 1725 | - * @throws InvalidDataTypeException |
|
| 1726 | - * @throws EE_Error |
|
| 1727 | - */ |
|
| 1728 | - public function delete_permanently() |
|
| 1729 | - { |
|
| 1730 | - /** |
|
| 1731 | - * Called just before HARD deleting a model object |
|
| 1732 | - * |
|
| 1733 | - * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1734 | - */ |
|
| 1735 | - do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1736 | - $model = $this->get_model(); |
|
| 1737 | - $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1738 | - $this->refresh_cache_of_related_objects(); |
|
| 1739 | - /** |
|
| 1740 | - * Called just after HARD deleting a model object |
|
| 1741 | - * |
|
| 1742 | - * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1743 | - * @param boolean $result |
|
| 1744 | - */ |
|
| 1745 | - do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1746 | - return $result; |
|
| 1747 | - } |
|
| 1748 | - |
|
| 1749 | - |
|
| 1750 | - /** |
|
| 1751 | - * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1752 | - * related model objects |
|
| 1753 | - * |
|
| 1754 | - * @throws ReflectionException |
|
| 1755 | - * @throws InvalidArgumentException |
|
| 1756 | - * @throws InvalidInterfaceException |
|
| 1757 | - * @throws InvalidDataTypeException |
|
| 1758 | - * @throws EE_Error |
|
| 1759 | - */ |
|
| 1760 | - public function refresh_cache_of_related_objects() |
|
| 1761 | - { |
|
| 1762 | - $model = $this->get_model(); |
|
| 1763 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1764 | - if (! empty($this->_model_relations[ $relation_name ])) { |
|
| 1765 | - $related_objects = $this->_model_relations[ $relation_name ]; |
|
| 1766 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1767 | - // this relation only stores a single model object, not an array |
|
| 1768 | - // but let's make it consistent |
|
| 1769 | - $related_objects = array($related_objects); |
|
| 1770 | - } |
|
| 1771 | - foreach ($related_objects as $related_object) { |
|
| 1772 | - // only refresh their cache if they're in memory |
|
| 1773 | - if ($related_object instanceof EE_Base_Class) { |
|
| 1774 | - $related_object->clear_cache( |
|
| 1775 | - $model->get_this_model_name(), |
|
| 1776 | - $this |
|
| 1777 | - ); |
|
| 1778 | - } |
|
| 1779 | - } |
|
| 1780 | - } |
|
| 1781 | - } |
|
| 1782 | - } |
|
| 1783 | - |
|
| 1784 | - |
|
| 1785 | - /** |
|
| 1786 | - * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1787 | - * object just before saving. |
|
| 1788 | - * |
|
| 1789 | - * @access public |
|
| 1790 | - * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1791 | - * if provided during the save() method (often client code will change the fields' |
|
| 1792 | - * values before calling save) |
|
| 1793 | - * @throws InvalidArgumentException |
|
| 1794 | - * @throws InvalidInterfaceException |
|
| 1795 | - * @throws InvalidDataTypeException |
|
| 1796 | - * @throws EE_Error |
|
| 1797 | - * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1798 | - * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1799 | - * @throws ReflectionException |
|
| 1800 | - * @throws ReflectionException |
|
| 1801 | - * @throws ReflectionException |
|
| 1802 | - */ |
|
| 1803 | - public function save($set_cols_n_values = array()) |
|
| 1804 | - { |
|
| 1805 | - $model = $this->get_model(); |
|
| 1806 | - /** |
|
| 1807 | - * Filters the fields we're about to save on the model object |
|
| 1808 | - * |
|
| 1809 | - * @param array $set_cols_n_values |
|
| 1810 | - * @param EE_Base_Class $model_object |
|
| 1811 | - */ |
|
| 1812 | - $set_cols_n_values = (array) apply_filters( |
|
| 1813 | - 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
| 1814 | - $set_cols_n_values, |
|
| 1815 | - $this |
|
| 1816 | - ); |
|
| 1817 | - // set attributes as provided in $set_cols_n_values |
|
| 1818 | - foreach ($set_cols_n_values as $column => $value) { |
|
| 1819 | - $this->set($column, $value); |
|
| 1820 | - } |
|
| 1821 | - // no changes ? then don't do anything |
|
| 1822 | - if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1823 | - return 0; |
|
| 1824 | - } |
|
| 1825 | - /** |
|
| 1826 | - * Saving a model object. |
|
| 1827 | - * Before we perform a save, this action is fired. |
|
| 1828 | - * |
|
| 1829 | - * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1830 | - */ |
|
| 1831 | - do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1832 | - if (! $this->allow_persist()) { |
|
| 1833 | - return 0; |
|
| 1834 | - } |
|
| 1835 | - // now get current attribute values |
|
| 1836 | - $save_cols_n_values = $this->_fields; |
|
| 1837 | - // if the object already has an ID, update it. Otherwise, insert it |
|
| 1838 | - // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
| 1839 | - // They have been |
|
| 1840 | - $old_assumption_concerning_value_preparation = $model |
|
| 1841 | - ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1842 | - $model->assume_values_already_prepared_by_model_object(true); |
|
| 1843 | - // does this model have an autoincrement PK? |
|
| 1844 | - if ($model->has_primary_key_field()) { |
|
| 1845 | - if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1846 | - // ok check if it's set, if so: update; if not, insert |
|
| 1847 | - if (! empty($save_cols_n_values[ $model->primary_key_name() ])) { |
|
| 1848 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1849 | - } else { |
|
| 1850 | - unset($save_cols_n_values[ $model->primary_key_name() ]); |
|
| 1851 | - $results = $model->insert($save_cols_n_values); |
|
| 1852 | - if ($results) { |
|
| 1853 | - // if successful, set the primary key |
|
| 1854 | - // but don't use the normal SET method, because it will check if |
|
| 1855 | - // an item with the same ID exists in the mapper & db, then |
|
| 1856 | - // will find it in the db (because we just added it) and THAT object |
|
| 1857 | - // will get added to the mapper before we can add this one! |
|
| 1858 | - // but if we just avoid using the SET method, all that headache can be avoided |
|
| 1859 | - $pk_field_name = $model->primary_key_name(); |
|
| 1860 | - $this->_fields[ $pk_field_name ] = $results; |
|
| 1861 | - $this->_clear_cached_property($pk_field_name); |
|
| 1862 | - $model->add_to_entity_map($this); |
|
| 1863 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1864 | - } |
|
| 1865 | - } |
|
| 1866 | - } else {// PK is NOT auto-increment |
|
| 1867 | - // so check if one like it already exists in the db |
|
| 1868 | - if ($model->exists_by_ID($this->ID())) { |
|
| 1869 | - if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1870 | - throw new EE_Error( |
|
| 1871 | - sprintf( |
|
| 1872 | - esc_html__( |
|
| 1873 | - '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', |
|
| 1874 | - 'event_espresso' |
|
| 1875 | - ), |
|
| 1876 | - get_class($this), |
|
| 1877 | - get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1878 | - get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1879 | - '<br />' |
|
| 1880 | - ) |
|
| 1881 | - ); |
|
| 1882 | - } |
|
| 1883 | - $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1884 | - } else { |
|
| 1885 | - $results = $model->insert($save_cols_n_values); |
|
| 1886 | - $this->_update_cached_related_model_objs_fks(); |
|
| 1887 | - } |
|
| 1888 | - } |
|
| 1889 | - } else {// there is NO primary key |
|
| 1890 | - $already_in_db = false; |
|
| 1891 | - foreach ($model->unique_indexes() as $index) { |
|
| 1892 | - $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1893 | - if ($model->exists(array($uniqueness_where_params))) { |
|
| 1894 | - $already_in_db = true; |
|
| 1895 | - } |
|
| 1896 | - } |
|
| 1897 | - if ($already_in_db) { |
|
| 1898 | - $combined_pk_fields_n_values = array_intersect_key( |
|
| 1899 | - $save_cols_n_values, |
|
| 1900 | - $model->get_combined_primary_key_fields() |
|
| 1901 | - ); |
|
| 1902 | - $results = $model->update( |
|
| 1903 | - $save_cols_n_values, |
|
| 1904 | - $combined_pk_fields_n_values |
|
| 1905 | - ); |
|
| 1906 | - } else { |
|
| 1907 | - $results = $model->insert($save_cols_n_values); |
|
| 1908 | - } |
|
| 1909 | - } |
|
| 1910 | - // restore the old assumption about values being prepared by the model object |
|
| 1911 | - $model->assume_values_already_prepared_by_model_object( |
|
| 1912 | - $old_assumption_concerning_value_preparation |
|
| 1913 | - ); |
|
| 1914 | - /** |
|
| 1915 | - * After saving the model object this action is called |
|
| 1916 | - * |
|
| 1917 | - * @param EE_Base_Class $model_object which was just saved |
|
| 1918 | - * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1919 | - * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1920 | - */ |
|
| 1921 | - do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1922 | - $this->_has_changes = false; |
|
| 1923 | - return $results; |
|
| 1924 | - } |
|
| 1925 | - |
|
| 1926 | - |
|
| 1927 | - /** |
|
| 1928 | - * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1929 | - * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1930 | - * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1931 | - * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1932 | - * 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 |
|
| 1933 | - * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1934 | - * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1935 | - * |
|
| 1936 | - * @return void |
|
| 1937 | - * @throws ReflectionException |
|
| 1938 | - * @throws InvalidArgumentException |
|
| 1939 | - * @throws InvalidInterfaceException |
|
| 1940 | - * @throws InvalidDataTypeException |
|
| 1941 | - * @throws EE_Error |
|
| 1942 | - */ |
|
| 1943 | - protected function _update_cached_related_model_objs_fks() |
|
| 1944 | - { |
|
| 1945 | - $model = $this->get_model(); |
|
| 1946 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1947 | - if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1948 | - foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1949 | - $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1950 | - $model->get_this_model_name() |
|
| 1951 | - ); |
|
| 1952 | - $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1953 | - if ($related_model_obj_in_cache->ID()) { |
|
| 1954 | - $related_model_obj_in_cache->save(); |
|
| 1955 | - } |
|
| 1956 | - } |
|
| 1957 | - } |
|
| 1958 | - } |
|
| 1959 | - } |
|
| 1960 | - |
|
| 1961 | - |
|
| 1962 | - /** |
|
| 1963 | - * Saves this model object and its NEW cached relations to the database. |
|
| 1964 | - * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1965 | - * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1966 | - * because otherwise, there's a potential for infinite looping of saving |
|
| 1967 | - * Saves the cached related model objects, and ensures the relation between them |
|
| 1968 | - * and this object and properly setup |
|
| 1969 | - * |
|
| 1970 | - * @return int ID of new model object on save; 0 on failure+ |
|
| 1971 | - * @throws ReflectionException |
|
| 1972 | - * @throws InvalidArgumentException |
|
| 1973 | - * @throws InvalidInterfaceException |
|
| 1974 | - * @throws InvalidDataTypeException |
|
| 1975 | - * @throws EE_Error |
|
| 1976 | - */ |
|
| 1977 | - public function save_new_cached_related_model_objs() |
|
| 1978 | - { |
|
| 1979 | - // make sure this has been saved |
|
| 1980 | - if (! $this->ID()) { |
|
| 1981 | - $id = $this->save(); |
|
| 1982 | - } else { |
|
| 1983 | - $id = $this->ID(); |
|
| 1984 | - } |
|
| 1985 | - // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1986 | - foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1987 | - if ($this->_model_relations[ $relationName ]) { |
|
| 1988 | - // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1989 | - // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1990 | - /* @var $related_model_obj EE_Base_Class */ |
|
| 1991 | - if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1992 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1993 | - // but ONLY if it DOES NOT exist in the DB |
|
| 1994 | - $related_model_obj = $this->_model_relations[ $relationName ]; |
|
| 1995 | - // if( ! $related_model_obj->ID()){ |
|
| 1996 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1997 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1998 | - // } |
|
| 1999 | - } else { |
|
| 2000 | - foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
| 2001 | - // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 2002 | - // but ONLY if it DOES NOT exist in the DB |
|
| 2003 | - // if( ! $related_model_obj->ID()){ |
|
| 2004 | - $this->_add_relation_to($related_model_obj, $relationName); |
|
| 2005 | - $related_model_obj->save_new_cached_related_model_objs(); |
|
| 2006 | - // } |
|
| 2007 | - } |
|
| 2008 | - } |
|
| 2009 | - } |
|
| 2010 | - } |
|
| 2011 | - return $id; |
|
| 2012 | - } |
|
| 2013 | - |
|
| 2014 | - |
|
| 2015 | - /** |
|
| 2016 | - * for getting a model while instantiated. |
|
| 2017 | - * |
|
| 2018 | - * @return EEM_Base | EEM_CPT_Base |
|
| 2019 | - * @throws ReflectionException |
|
| 2020 | - * @throws InvalidArgumentException |
|
| 2021 | - * @throws InvalidInterfaceException |
|
| 2022 | - * @throws InvalidDataTypeException |
|
| 2023 | - * @throws EE_Error |
|
| 2024 | - */ |
|
| 2025 | - public function get_model() |
|
| 2026 | - { |
|
| 2027 | - if (! $this->_model) { |
|
| 2028 | - $modelName = self::_get_model_classname(get_class($this)); |
|
| 2029 | - $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 2030 | - } else { |
|
| 2031 | - $this->_model->set_timezone($this->_timezone); |
|
| 2032 | - } |
|
| 2033 | - return $this->_model; |
|
| 2034 | - } |
|
| 2035 | - |
|
| 2036 | - |
|
| 2037 | - /** |
|
| 2038 | - * @param $props_n_values |
|
| 2039 | - * @param $classname |
|
| 2040 | - * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 2041 | - * @throws ReflectionException |
|
| 2042 | - * @throws InvalidArgumentException |
|
| 2043 | - * @throws InvalidInterfaceException |
|
| 2044 | - * @throws InvalidDataTypeException |
|
| 2045 | - * @throws EE_Error |
|
| 2046 | - */ |
|
| 2047 | - protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 2048 | - { |
|
| 2049 | - // TODO: will not work for Term_Relationships because they have no PK! |
|
| 2050 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2051 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2052 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2053 | - ) { |
|
| 2054 | - $id = $props_n_values[ $primary_id_ref ]; |
|
| 2055 | - return self::_get_model($classname)->get_from_entity_map($id); |
|
| 2056 | - } |
|
| 2057 | - return false; |
|
| 2058 | - } |
|
| 2059 | - |
|
| 2060 | - |
|
| 2061 | - /** |
|
| 2062 | - * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 2063 | - * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 2064 | - * 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 |
|
| 2065 | - * we return false. |
|
| 2066 | - * |
|
| 2067 | - * @param array $props_n_values incoming array of properties and their values |
|
| 2068 | - * @param string $classname the classname of the child class |
|
| 2069 | - * @param null $timezone |
|
| 2070 | - * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 2071 | - * date_format and the second value is the time format |
|
| 2072 | - * @return mixed (EE_Base_Class|bool) |
|
| 2073 | - * @throws InvalidArgumentException |
|
| 2074 | - * @throws InvalidInterfaceException |
|
| 2075 | - * @throws InvalidDataTypeException |
|
| 2076 | - * @throws EE_Error |
|
| 2077 | - * @throws ReflectionException |
|
| 2078 | - * @throws ReflectionException |
|
| 2079 | - * @throws ReflectionException |
|
| 2080 | - */ |
|
| 2081 | - protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 2082 | - { |
|
| 2083 | - $existing = null; |
|
| 2084 | - $model = self::_get_model($classname, $timezone); |
|
| 2085 | - if ($model->has_primary_key_field()) { |
|
| 2086 | - $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2087 | - if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2088 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2089 | - ) { |
|
| 2090 | - $existing = $model->get_one_by_ID( |
|
| 2091 | - $props_n_values[ $primary_id_ref ] |
|
| 2092 | - ); |
|
| 2093 | - } |
|
| 2094 | - } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 2095 | - // no primary key on this model, but there's still a matching item in the DB |
|
| 2096 | - $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 2097 | - self::_get_model($classname, $timezone) |
|
| 2098 | - ->get_index_primary_key_string($props_n_values) |
|
| 2099 | - ); |
|
| 2100 | - } |
|
| 2101 | - if ($existing) { |
|
| 2102 | - // set date formats if present before setting values |
|
| 2103 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 2104 | - $existing->set_date_format($date_formats[0]); |
|
| 2105 | - $existing->set_time_format($date_formats[1]); |
|
| 2106 | - } else { |
|
| 2107 | - // set default formats for date and time |
|
| 2108 | - $existing->set_date_format(get_option('date_format')); |
|
| 2109 | - $existing->set_time_format(get_option('time_format')); |
|
| 2110 | - } |
|
| 2111 | - foreach ($props_n_values as $property => $field_value) { |
|
| 2112 | - $existing->set($property, $field_value); |
|
| 2113 | - } |
|
| 2114 | - return $existing; |
|
| 2115 | - } |
|
| 2116 | - return false; |
|
| 2117 | - } |
|
| 2118 | - |
|
| 2119 | - |
|
| 2120 | - /** |
|
| 2121 | - * Gets the EEM_*_Model for this class |
|
| 2122 | - * |
|
| 2123 | - * @access public now, as this is more convenient |
|
| 2124 | - * @param $classname |
|
| 2125 | - * @param null $timezone |
|
| 2126 | - * @throws ReflectionException |
|
| 2127 | - * @throws InvalidArgumentException |
|
| 2128 | - * @throws InvalidInterfaceException |
|
| 2129 | - * @throws InvalidDataTypeException |
|
| 2130 | - * @throws EE_Error |
|
| 2131 | - * @return EEM_Base |
|
| 2132 | - */ |
|
| 2133 | - protected static function _get_model($classname, $timezone = null) |
|
| 2134 | - { |
|
| 2135 | - // find model for this class |
|
| 2136 | - if (! $classname) { |
|
| 2137 | - throw new EE_Error( |
|
| 2138 | - sprintf( |
|
| 2139 | - esc_html__( |
|
| 2140 | - 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
| 2141 | - 'event_espresso' |
|
| 2142 | - ), |
|
| 2143 | - $classname |
|
| 2144 | - ) |
|
| 2145 | - ); |
|
| 2146 | - } |
|
| 2147 | - $modelName = self::_get_model_classname($classname); |
|
| 2148 | - return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 2149 | - } |
|
| 2150 | - |
|
| 2151 | - |
|
| 2152 | - /** |
|
| 2153 | - * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 2154 | - * |
|
| 2155 | - * @param string $model_classname |
|
| 2156 | - * @param null $timezone |
|
| 2157 | - * @return EEM_Base |
|
| 2158 | - * @throws ReflectionException |
|
| 2159 | - * @throws InvalidArgumentException |
|
| 2160 | - * @throws InvalidInterfaceException |
|
| 2161 | - * @throws InvalidDataTypeException |
|
| 2162 | - * @throws EE_Error |
|
| 2163 | - */ |
|
| 2164 | - protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 2165 | - { |
|
| 2166 | - $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 2167 | - $model = EE_Registry::instance()->load_model($model_classname); |
|
| 2168 | - $model->set_timezone($timezone); |
|
| 2169 | - return $model; |
|
| 2170 | - } |
|
| 2171 | - |
|
| 2172 | - |
|
| 2173 | - /** |
|
| 2174 | - * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 2175 | - * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 2176 | - * |
|
| 2177 | - * @param null $model_name |
|
| 2178 | - * @return string like EEM_Attendee |
|
| 2179 | - */ |
|
| 2180 | - private static function _get_model_classname($model_name = null) |
|
| 2181 | - { |
|
| 2182 | - if (strpos($model_name, 'EE_') === 0) { |
|
| 2183 | - $model_classname = str_replace('EE_', 'EEM_', $model_name); |
|
| 2184 | - } else { |
|
| 2185 | - $model_classname = 'EEM_' . $model_name; |
|
| 2186 | - } |
|
| 2187 | - return $model_classname; |
|
| 2188 | - } |
|
| 2189 | - |
|
| 2190 | - |
|
| 2191 | - /** |
|
| 2192 | - * returns the name of the primary key attribute |
|
| 2193 | - * |
|
| 2194 | - * @param null $classname |
|
| 2195 | - * @throws ReflectionException |
|
| 2196 | - * @throws InvalidArgumentException |
|
| 2197 | - * @throws InvalidInterfaceException |
|
| 2198 | - * @throws InvalidDataTypeException |
|
| 2199 | - * @throws EE_Error |
|
| 2200 | - * @return string |
|
| 2201 | - */ |
|
| 2202 | - protected static function _get_primary_key_name($classname = null) |
|
| 2203 | - { |
|
| 2204 | - if (! $classname) { |
|
| 2205 | - throw new EE_Error( |
|
| 2206 | - sprintf( |
|
| 2207 | - esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
| 2208 | - $classname |
|
| 2209 | - ) |
|
| 2210 | - ); |
|
| 2211 | - } |
|
| 2212 | - return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 2213 | - } |
|
| 2214 | - |
|
| 2215 | - |
|
| 2216 | - /** |
|
| 2217 | - * Gets the value of the primary key. |
|
| 2218 | - * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 2219 | - * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 2220 | - * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 2221 | - * |
|
| 2222 | - * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 2223 | - * @throws ReflectionException |
|
| 2224 | - * @throws InvalidArgumentException |
|
| 2225 | - * @throws InvalidInterfaceException |
|
| 2226 | - * @throws InvalidDataTypeException |
|
| 2227 | - * @throws EE_Error |
|
| 2228 | - */ |
|
| 2229 | - public function ID() |
|
| 2230 | - { |
|
| 2231 | - $model = $this->get_model(); |
|
| 2232 | - // now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 2233 | - if ($model->has_primary_key_field()) { |
|
| 2234 | - return $this->_fields[ $model->primary_key_name() ]; |
|
| 2235 | - } |
|
| 2236 | - return $model->get_index_primary_key_string($this->_fields); |
|
| 2237 | - } |
|
| 2238 | - |
|
| 2239 | - |
|
| 2240 | - /** |
|
| 2241 | - * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 2242 | - * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 2243 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 2244 | - * |
|
| 2245 | - * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 2246 | - * @param string $relationName eg 'Events','Question',etc. |
|
| 2247 | - * an attendee to a group, you also want to specify which role they |
|
| 2248 | - * will have in that group. So you would use this parameter to |
|
| 2249 | - * specify array('role-column-name'=>'role-id') |
|
| 2250 | - * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 2251 | - * allow you to further constrict the relation to being added. |
|
| 2252 | - * However, keep in mind that the columns (keys) given must match a |
|
| 2253 | - * column on the JOIN table and currently only the HABTM models |
|
| 2254 | - * accept these additional conditions. Also remember that if an |
|
| 2255 | - * exact match isn't found for these extra cols/val pairs, then a |
|
| 2256 | - * NEW row is created in the join table. |
|
| 2257 | - * @param null $cache_id |
|
| 2258 | - * @throws ReflectionException |
|
| 2259 | - * @throws InvalidArgumentException |
|
| 2260 | - * @throws InvalidInterfaceException |
|
| 2261 | - * @throws InvalidDataTypeException |
|
| 2262 | - * @throws EE_Error |
|
| 2263 | - * @return EE_Base_Class the object the relation was added to |
|
| 2264 | - */ |
|
| 2265 | - public function _add_relation_to( |
|
| 2266 | - $otherObjectModelObjectOrID, |
|
| 2267 | - $relationName, |
|
| 2268 | - $extra_join_model_fields_n_values = array(), |
|
| 2269 | - $cache_id = null |
|
| 2270 | - ) { |
|
| 2271 | - $model = $this->get_model(); |
|
| 2272 | - // if this thing exists in the DB, save the relation to the DB |
|
| 2273 | - if ($this->ID()) { |
|
| 2274 | - $otherObject = $model->add_relationship_to( |
|
| 2275 | - $this, |
|
| 2276 | - $otherObjectModelObjectOrID, |
|
| 2277 | - $relationName, |
|
| 2278 | - $extra_join_model_fields_n_values |
|
| 2279 | - ); |
|
| 2280 | - // clear cache so future get_many_related and get_first_related() return new results. |
|
| 2281 | - $this->clear_cache($relationName, $otherObject, true); |
|
| 2282 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2283 | - $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2284 | - } |
|
| 2285 | - } else { |
|
| 2286 | - // this thing doesn't exist in the DB, so just cache it |
|
| 2287 | - if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2288 | - throw new EE_Error( |
|
| 2289 | - sprintf( |
|
| 2290 | - esc_html__( |
|
| 2291 | - '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', |
|
| 2292 | - 'event_espresso' |
|
| 2293 | - ), |
|
| 2294 | - $otherObjectModelObjectOrID, |
|
| 2295 | - get_class($this) |
|
| 2296 | - ) |
|
| 2297 | - ); |
|
| 2298 | - } |
|
| 2299 | - $otherObject = $otherObjectModelObjectOrID; |
|
| 2300 | - $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2301 | - } |
|
| 2302 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2303 | - // fix the reciprocal relation too |
|
| 2304 | - if ($otherObject->ID()) { |
|
| 2305 | - // its saved so assumed relations exist in the DB, so we can just |
|
| 2306 | - // clear the cache so future queries use the updated info in the DB |
|
| 2307 | - $otherObject->clear_cache( |
|
| 2308 | - $model->get_this_model_name(), |
|
| 2309 | - null, |
|
| 2310 | - true |
|
| 2311 | - ); |
|
| 2312 | - } else { |
|
| 2313 | - // it's not saved, so it caches relations like this |
|
| 2314 | - $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2315 | - } |
|
| 2316 | - } |
|
| 2317 | - return $otherObject; |
|
| 2318 | - } |
|
| 2319 | - |
|
| 2320 | - |
|
| 2321 | - /** |
|
| 2322 | - * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2323 | - * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2324 | - * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2325 | - * from the cache |
|
| 2326 | - * |
|
| 2327 | - * @param mixed $otherObjectModelObjectOrID |
|
| 2328 | - * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2329 | - * to the DB yet |
|
| 2330 | - * @param string $relationName |
|
| 2331 | - * @param array $where_query |
|
| 2332 | - * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2333 | - * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2334 | - * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2335 | - * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
| 2336 | - * deleted. |
|
| 2337 | - * @return EE_Base_Class the relation was removed from |
|
| 2338 | - * @throws ReflectionException |
|
| 2339 | - * @throws InvalidArgumentException |
|
| 2340 | - * @throws InvalidInterfaceException |
|
| 2341 | - * @throws InvalidDataTypeException |
|
| 2342 | - * @throws EE_Error |
|
| 2343 | - */ |
|
| 2344 | - public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2345 | - { |
|
| 2346 | - if ($this->ID()) { |
|
| 2347 | - // if this exists in the DB, save the relation change to the DB too |
|
| 2348 | - $otherObject = $this->get_model()->remove_relationship_to( |
|
| 2349 | - $this, |
|
| 2350 | - $otherObjectModelObjectOrID, |
|
| 2351 | - $relationName, |
|
| 2352 | - $where_query |
|
| 2353 | - ); |
|
| 2354 | - $this->clear_cache( |
|
| 2355 | - $relationName, |
|
| 2356 | - $otherObject |
|
| 2357 | - ); |
|
| 2358 | - } else { |
|
| 2359 | - // this doesn't exist in the DB, just remove it from the cache |
|
| 2360 | - $otherObject = $this->clear_cache( |
|
| 2361 | - $relationName, |
|
| 2362 | - $otherObjectModelObjectOrID |
|
| 2363 | - ); |
|
| 2364 | - } |
|
| 2365 | - if ($otherObject instanceof EE_Base_Class) { |
|
| 2366 | - $otherObject->clear_cache( |
|
| 2367 | - $this->get_model()->get_this_model_name(), |
|
| 2368 | - $this |
|
| 2369 | - ); |
|
| 2370 | - } |
|
| 2371 | - return $otherObject; |
|
| 2372 | - } |
|
| 2373 | - |
|
| 2374 | - |
|
| 2375 | - /** |
|
| 2376 | - * Removes ALL the related things for the $relationName. |
|
| 2377 | - * |
|
| 2378 | - * @param string $relationName |
|
| 2379 | - * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2380 | - * @return EE_Base_Class |
|
| 2381 | - * @throws ReflectionException |
|
| 2382 | - * @throws InvalidArgumentException |
|
| 2383 | - * @throws InvalidInterfaceException |
|
| 2384 | - * @throws InvalidDataTypeException |
|
| 2385 | - * @throws EE_Error |
|
| 2386 | - */ |
|
| 2387 | - public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2388 | - { |
|
| 2389 | - if ($this->ID()) { |
|
| 2390 | - // if this exists in the DB, save the relation change to the DB too |
|
| 2391 | - $otherObjects = $this->get_model()->remove_relations( |
|
| 2392 | - $this, |
|
| 2393 | - $relationName, |
|
| 2394 | - $where_query_params |
|
| 2395 | - ); |
|
| 2396 | - $this->clear_cache( |
|
| 2397 | - $relationName, |
|
| 2398 | - null, |
|
| 2399 | - true |
|
| 2400 | - ); |
|
| 2401 | - } else { |
|
| 2402 | - // this doesn't exist in the DB, just remove it from the cache |
|
| 2403 | - $otherObjects = $this->clear_cache( |
|
| 2404 | - $relationName, |
|
| 2405 | - null, |
|
| 2406 | - true |
|
| 2407 | - ); |
|
| 2408 | - } |
|
| 2409 | - if (is_array($otherObjects)) { |
|
| 2410 | - foreach ($otherObjects as $otherObject) { |
|
| 2411 | - $otherObject->clear_cache( |
|
| 2412 | - $this->get_model()->get_this_model_name(), |
|
| 2413 | - $this |
|
| 2414 | - ); |
|
| 2415 | - } |
|
| 2416 | - } |
|
| 2417 | - return $otherObjects; |
|
| 2418 | - } |
|
| 2419 | - |
|
| 2420 | - |
|
| 2421 | - /** |
|
| 2422 | - * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2423 | - * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2424 | - * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2425 | - * because we want to get even deleted items etc. |
|
| 2426 | - * |
|
| 2427 | - * @param string $relationName key in the model's _model_relations array |
|
| 2428 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2429 | - * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
| 2430 | - * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
| 2431 | - * results if you want IDs |
|
| 2432 | - * @throws ReflectionException |
|
| 2433 | - * @throws InvalidArgumentException |
|
| 2434 | - * @throws InvalidInterfaceException |
|
| 2435 | - * @throws InvalidDataTypeException |
|
| 2436 | - * @throws EE_Error |
|
| 2437 | - */ |
|
| 2438 | - public function get_many_related($relationName, $query_params = array()) |
|
| 2439 | - { |
|
| 2440 | - if ($this->ID()) { |
|
| 2441 | - // this exists in the DB, so get the related things from either the cache or the DB |
|
| 2442 | - // if there are query parameters, forget about caching the related model objects. |
|
| 2443 | - if ($query_params) { |
|
| 2444 | - $related_model_objects = $this->get_model()->get_all_related( |
|
| 2445 | - $this, |
|
| 2446 | - $relationName, |
|
| 2447 | - $query_params |
|
| 2448 | - ); |
|
| 2449 | - } else { |
|
| 2450 | - // did we already cache the result of this query? |
|
| 2451 | - $cached_results = $this->get_all_from_cache($relationName); |
|
| 2452 | - if (! $cached_results) { |
|
| 2453 | - $related_model_objects = $this->get_model()->get_all_related( |
|
| 2454 | - $this, |
|
| 2455 | - $relationName, |
|
| 2456 | - $query_params |
|
| 2457 | - ); |
|
| 2458 | - // if no query parameters were passed, then we got all the related model objects |
|
| 2459 | - // for that relation. We can cache them then. |
|
| 2460 | - foreach ($related_model_objects as $related_model_object) { |
|
| 2461 | - $this->cache($relationName, $related_model_object); |
|
| 2462 | - } |
|
| 2463 | - } else { |
|
| 2464 | - $related_model_objects = $cached_results; |
|
| 2465 | - } |
|
| 2466 | - } |
|
| 2467 | - } else { |
|
| 2468 | - // this doesn't exist in the DB, so just get the related things from the cache |
|
| 2469 | - $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2470 | - } |
|
| 2471 | - return $related_model_objects; |
|
| 2472 | - } |
|
| 2473 | - |
|
| 2474 | - |
|
| 2475 | - /** |
|
| 2476 | - * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2477 | - * unless otherwise specified in the $query_params |
|
| 2478 | - * |
|
| 2479 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2480 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2481 | - * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2482 | - * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2483 | - * that by the setting $distinct to TRUE; |
|
| 2484 | - * @return int |
|
| 2485 | - * @throws ReflectionException |
|
| 2486 | - * @throws InvalidArgumentException |
|
| 2487 | - * @throws InvalidInterfaceException |
|
| 2488 | - * @throws InvalidDataTypeException |
|
| 2489 | - * @throws EE_Error |
|
| 2490 | - */ |
|
| 2491 | - public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2492 | - { |
|
| 2493 | - return $this->get_model()->count_related( |
|
| 2494 | - $this, |
|
| 2495 | - $relation_name, |
|
| 2496 | - $query_params, |
|
| 2497 | - $field_to_count, |
|
| 2498 | - $distinct |
|
| 2499 | - ); |
|
| 2500 | - } |
|
| 2501 | - |
|
| 2502 | - |
|
| 2503 | - /** |
|
| 2504 | - * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2505 | - * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2506 | - * |
|
| 2507 | - * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2508 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2509 | - * @param string $field_to_sum name of field to count by. |
|
| 2510 | - * By default, uses primary key |
|
| 2511 | - * (which doesn't make much sense, so you should probably change it) |
|
| 2512 | - * @return int |
|
| 2513 | - * @throws ReflectionException |
|
| 2514 | - * @throws InvalidArgumentException |
|
| 2515 | - * @throws InvalidInterfaceException |
|
| 2516 | - * @throws InvalidDataTypeException |
|
| 2517 | - * @throws EE_Error |
|
| 2518 | - */ |
|
| 2519 | - public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2520 | - { |
|
| 2521 | - return $this->get_model()->sum_related( |
|
| 2522 | - $this, |
|
| 2523 | - $relation_name, |
|
| 2524 | - $query_params, |
|
| 2525 | - $field_to_sum |
|
| 2526 | - ); |
|
| 2527 | - } |
|
| 2528 | - |
|
| 2529 | - |
|
| 2530 | - /** |
|
| 2531 | - * Gets the first (ie, one) related model object of the specified type. |
|
| 2532 | - * |
|
| 2533 | - * @param string $relationName key in the model's _model_relations array |
|
| 2534 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2535 | - * @return EE_Base_Class (not an array, a single object) |
|
| 2536 | - * @throws ReflectionException |
|
| 2537 | - * @throws InvalidArgumentException |
|
| 2538 | - * @throws InvalidInterfaceException |
|
| 2539 | - * @throws InvalidDataTypeException |
|
| 2540 | - * @throws EE_Error |
|
| 2541 | - */ |
|
| 2542 | - public function get_first_related($relationName, $query_params = array()) |
|
| 2543 | - { |
|
| 2544 | - $model = $this->get_model(); |
|
| 2545 | - if ($this->ID()) {// this exists in the DB, get from the cache OR the DB |
|
| 2546 | - // if they've provided some query parameters, don't bother trying to cache the result |
|
| 2547 | - // also make sure we're not caching the result of get_first_related |
|
| 2548 | - // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2549 | - if ($query_params |
|
| 2550 | - || ! $model->related_settings_for($relationName) |
|
| 2551 | - instanceof |
|
| 2552 | - EE_Belongs_To_Relation |
|
| 2553 | - ) { |
|
| 2554 | - $related_model_object = $model->get_first_related( |
|
| 2555 | - $this, |
|
| 2556 | - $relationName, |
|
| 2557 | - $query_params |
|
| 2558 | - ); |
|
| 2559 | - } else { |
|
| 2560 | - // first, check if we've already cached the result of this query |
|
| 2561 | - $cached_result = $this->get_one_from_cache($relationName); |
|
| 2562 | - if (! $cached_result) { |
|
| 2563 | - $related_model_object = $model->get_first_related( |
|
| 2564 | - $this, |
|
| 2565 | - $relationName, |
|
| 2566 | - $query_params |
|
| 2567 | - ); |
|
| 2568 | - $this->cache($relationName, $related_model_object); |
|
| 2569 | - } else { |
|
| 2570 | - $related_model_object = $cached_result; |
|
| 2571 | - } |
|
| 2572 | - } |
|
| 2573 | - } else { |
|
| 2574 | - $related_model_object = null; |
|
| 2575 | - // this doesn't exist in the Db, |
|
| 2576 | - // but maybe the relation is of type belongs to, and so the related thing might |
|
| 2577 | - if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2578 | - $related_model_object = $model->get_first_related( |
|
| 2579 | - $this, |
|
| 2580 | - $relationName, |
|
| 2581 | - $query_params |
|
| 2582 | - ); |
|
| 2583 | - } |
|
| 2584 | - // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
| 2585 | - // just get what's cached on this object |
|
| 2586 | - if (! $related_model_object) { |
|
| 2587 | - $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2588 | - } |
|
| 2589 | - } |
|
| 2590 | - return $related_model_object; |
|
| 2591 | - } |
|
| 2592 | - |
|
| 2593 | - |
|
| 2594 | - /** |
|
| 2595 | - * Does a delete on all related objects of type $relationName and removes |
|
| 2596 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2597 | - * of blocking related model objects) does nothing. If the related model objects are |
|
| 2598 | - * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2599 | - * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2600 | - * |
|
| 2601 | - * @param string $relationName |
|
| 2602 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2603 | - * @return int how many deleted |
|
| 2604 | - * @throws ReflectionException |
|
| 2605 | - * @throws InvalidArgumentException |
|
| 2606 | - * @throws InvalidInterfaceException |
|
| 2607 | - * @throws InvalidDataTypeException |
|
| 2608 | - * @throws EE_Error |
|
| 2609 | - */ |
|
| 2610 | - public function delete_related($relationName, $query_params = array()) |
|
| 2611 | - { |
|
| 2612 | - if ($this->ID()) { |
|
| 2613 | - $count = $this->get_model()->delete_related( |
|
| 2614 | - $this, |
|
| 2615 | - $relationName, |
|
| 2616 | - $query_params |
|
| 2617 | - ); |
|
| 2618 | - } else { |
|
| 2619 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2620 | - $this->clear_cache($relationName, null, true); |
|
| 2621 | - } |
|
| 2622 | - return $count; |
|
| 2623 | - } |
|
| 2624 | - |
|
| 2625 | - |
|
| 2626 | - /** |
|
| 2627 | - * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2628 | - * the current model object's relation to them. If they can't be deleted (because |
|
| 2629 | - * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2630 | - * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2631 | - * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2632 | - * |
|
| 2633 | - * @param string $relationName |
|
| 2634 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2635 | - * @return int how many deleted (including those soft deleted) |
|
| 2636 | - * @throws ReflectionException |
|
| 2637 | - * @throws InvalidArgumentException |
|
| 2638 | - * @throws InvalidInterfaceException |
|
| 2639 | - * @throws InvalidDataTypeException |
|
| 2640 | - * @throws EE_Error |
|
| 2641 | - */ |
|
| 2642 | - public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2643 | - { |
|
| 2644 | - if ($this->ID()) { |
|
| 2645 | - $count = $this->get_model()->delete_related_permanently( |
|
| 2646 | - $this, |
|
| 2647 | - $relationName, |
|
| 2648 | - $query_params |
|
| 2649 | - ); |
|
| 2650 | - } else { |
|
| 2651 | - $count = count($this->get_all_from_cache($relationName)); |
|
| 2652 | - } |
|
| 2653 | - $this->clear_cache($relationName, null, true); |
|
| 2654 | - return $count; |
|
| 2655 | - } |
|
| 2656 | - |
|
| 2657 | - |
|
| 2658 | - /** |
|
| 2659 | - * is_set |
|
| 2660 | - * Just a simple utility function children can use for checking if property exists |
|
| 2661 | - * |
|
| 2662 | - * @access public |
|
| 2663 | - * @param string $field_name property to check |
|
| 2664 | - * @return bool TRUE if existing,FALSE if not. |
|
| 2665 | - */ |
|
| 2666 | - public function is_set($field_name) |
|
| 2667 | - { |
|
| 2668 | - return isset($this->_fields[ $field_name ]); |
|
| 2669 | - } |
|
| 2670 | - |
|
| 2671 | - |
|
| 2672 | - /** |
|
| 2673 | - * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2674 | - * EE_Error exception if they don't |
|
| 2675 | - * |
|
| 2676 | - * @param mixed (string|array) $properties properties to check |
|
| 2677 | - * @throws EE_Error |
|
| 2678 | - * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2679 | - */ |
|
| 2680 | - protected function _property_exists($properties) |
|
| 2681 | - { |
|
| 2682 | - foreach ((array) $properties as $property_name) { |
|
| 2683 | - // first make sure this property exists |
|
| 2684 | - if (! $this->_fields[ $property_name ]) { |
|
| 2685 | - throw new EE_Error( |
|
| 2686 | - sprintf( |
|
| 2687 | - esc_html__( |
|
| 2688 | - 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2689 | - 'event_espresso' |
|
| 2690 | - ), |
|
| 2691 | - $property_name |
|
| 2692 | - ) |
|
| 2693 | - ); |
|
| 2694 | - } |
|
| 2695 | - } |
|
| 2696 | - return true; |
|
| 2697 | - } |
|
| 2698 | - |
|
| 2699 | - |
|
| 2700 | - /** |
|
| 2701 | - * This simply returns an array of model fields for this object |
|
| 2702 | - * |
|
| 2703 | - * @return array |
|
| 2704 | - * @throws ReflectionException |
|
| 2705 | - * @throws InvalidArgumentException |
|
| 2706 | - * @throws InvalidInterfaceException |
|
| 2707 | - * @throws InvalidDataTypeException |
|
| 2708 | - * @throws EE_Error |
|
| 2709 | - */ |
|
| 2710 | - public function model_field_array() |
|
| 2711 | - { |
|
| 2712 | - $fields = $this->get_model()->field_settings(false); |
|
| 2713 | - $properties = array(); |
|
| 2714 | - // remove prepended underscore |
|
| 2715 | - foreach ($fields as $field_name => $settings) { |
|
| 2716 | - $properties[ $field_name ] = $this->get($field_name); |
|
| 2717 | - } |
|
| 2718 | - return $properties; |
|
| 2719 | - } |
|
| 2720 | - |
|
| 2721 | - |
|
| 2722 | - /** |
|
| 2723 | - * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2724 | - * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2725 | - * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
| 2726 | - * Instead of requiring a plugin to extend the EE_Base_Class |
|
| 2727 | - * (which works fine is there's only 1 plugin, but when will that happen?) |
|
| 2728 | - * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
| 2729 | - * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
| 2730 | - * and accepts 2 arguments: the object on which the function was called, |
|
| 2731 | - * and an array of the original arguments passed to the function. |
|
| 2732 | - * Whatever their callback function returns will be returned by this function. |
|
| 2733 | - * Example: in functions.php (or in a plugin): |
|
| 2734 | - * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
| 2735 | - * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2736 | - * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2737 | - * return $previousReturnValue.$returnString; |
|
| 2738 | - * } |
|
| 2739 | - * require('EE_Answer.class.php'); |
|
| 2740 | - * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2741 | - * echo $answer->my_callback('monkeys',100); |
|
| 2742 | - * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2743 | - * |
|
| 2744 | - * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2745 | - * @param array $args array of original arguments passed to the function |
|
| 2746 | - * @throws EE_Error |
|
| 2747 | - * @return mixed whatever the plugin which calls add_filter decides |
|
| 2748 | - */ |
|
| 2749 | - public function __call($methodName, $args) |
|
| 2750 | - { |
|
| 2751 | - $className = get_class($this); |
|
| 2752 | - $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2753 | - if (! has_filter($tagName)) { |
|
| 2754 | - throw new EE_Error( |
|
| 2755 | - sprintf( |
|
| 2756 | - esc_html__( |
|
| 2757 | - "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;}", |
|
| 2758 | - 'event_espresso' |
|
| 2759 | - ), |
|
| 2760 | - $methodName, |
|
| 2761 | - $className, |
|
| 2762 | - $tagName |
|
| 2763 | - ) |
|
| 2764 | - ); |
|
| 2765 | - } |
|
| 2766 | - return apply_filters($tagName, null, $this, $args); |
|
| 2767 | - } |
|
| 2768 | - |
|
| 2769 | - |
|
| 2770 | - /** |
|
| 2771 | - * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2772 | - * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2773 | - * |
|
| 2774 | - * @param string $meta_key |
|
| 2775 | - * @param mixed $meta_value |
|
| 2776 | - * @param mixed $previous_value |
|
| 2777 | - * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2778 | - * NOTE: if the values haven't changed, returns 0 |
|
| 2779 | - * @throws InvalidArgumentException |
|
| 2780 | - * @throws InvalidInterfaceException |
|
| 2781 | - * @throws InvalidDataTypeException |
|
| 2782 | - * @throws EE_Error |
|
| 2783 | - * @throws ReflectionException |
|
| 2784 | - */ |
|
| 2785 | - public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2786 | - { |
|
| 2787 | - $query_params = array( |
|
| 2788 | - array( |
|
| 2789 | - 'EXM_key' => $meta_key, |
|
| 2790 | - 'OBJ_ID' => $this->ID(), |
|
| 2791 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2792 | - ), |
|
| 2793 | - ); |
|
| 2794 | - if ($previous_value !== null) { |
|
| 2795 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2796 | - } |
|
| 2797 | - $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2798 | - if (! $existing_rows_like_that) { |
|
| 2799 | - return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2800 | - } |
|
| 2801 | - foreach ($existing_rows_like_that as $existing_row) { |
|
| 2802 | - $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2803 | - } |
|
| 2804 | - return count($existing_rows_like_that); |
|
| 2805 | - } |
|
| 2806 | - |
|
| 2807 | - |
|
| 2808 | - /** |
|
| 2809 | - * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2810 | - * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2811 | - * extra meta row was entered, false if not |
|
| 2812 | - * |
|
| 2813 | - * @param string $meta_key |
|
| 2814 | - * @param mixed $meta_value |
|
| 2815 | - * @param boolean $unique |
|
| 2816 | - * @return boolean |
|
| 2817 | - * @throws InvalidArgumentException |
|
| 2818 | - * @throws InvalidInterfaceException |
|
| 2819 | - * @throws InvalidDataTypeException |
|
| 2820 | - * @throws EE_Error |
|
| 2821 | - * @throws ReflectionException |
|
| 2822 | - * @throws ReflectionException |
|
| 2823 | - */ |
|
| 2824 | - public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2825 | - { |
|
| 2826 | - if ($unique) { |
|
| 2827 | - $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2828 | - array( |
|
| 2829 | - array( |
|
| 2830 | - 'EXM_key' => $meta_key, |
|
| 2831 | - 'OBJ_ID' => $this->ID(), |
|
| 2832 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2833 | - ), |
|
| 2834 | - ) |
|
| 2835 | - ); |
|
| 2836 | - if ($existing_extra_meta) { |
|
| 2837 | - return false; |
|
| 2838 | - } |
|
| 2839 | - } |
|
| 2840 | - $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2841 | - array( |
|
| 2842 | - 'EXM_key' => $meta_key, |
|
| 2843 | - 'EXM_value' => $meta_value, |
|
| 2844 | - 'OBJ_ID' => $this->ID(), |
|
| 2845 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2846 | - ) |
|
| 2847 | - ); |
|
| 2848 | - $new_extra_meta->save(); |
|
| 2849 | - return true; |
|
| 2850 | - } |
|
| 2851 | - |
|
| 2852 | - |
|
| 2853 | - /** |
|
| 2854 | - * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2855 | - * is specified, only deletes extra meta records with that value. |
|
| 2856 | - * |
|
| 2857 | - * @param string $meta_key |
|
| 2858 | - * @param mixed $meta_value |
|
| 2859 | - * @return int number of extra meta rows deleted |
|
| 2860 | - * @throws InvalidArgumentException |
|
| 2861 | - * @throws InvalidInterfaceException |
|
| 2862 | - * @throws InvalidDataTypeException |
|
| 2863 | - * @throws EE_Error |
|
| 2864 | - * @throws ReflectionException |
|
| 2865 | - */ |
|
| 2866 | - public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2867 | - { |
|
| 2868 | - $query_params = array( |
|
| 2869 | - array( |
|
| 2870 | - 'EXM_key' => $meta_key, |
|
| 2871 | - 'OBJ_ID' => $this->ID(), |
|
| 2872 | - 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2873 | - ), |
|
| 2874 | - ); |
|
| 2875 | - if ($meta_value !== null) { |
|
| 2876 | - $query_params[0]['EXM_value'] = $meta_value; |
|
| 2877 | - } |
|
| 2878 | - return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2879 | - } |
|
| 2880 | - |
|
| 2881 | - |
|
| 2882 | - /** |
|
| 2883 | - * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2884 | - * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2885 | - * You can specify $default is case you haven't found the extra meta |
|
| 2886 | - * |
|
| 2887 | - * @param string $meta_key |
|
| 2888 | - * @param boolean $single |
|
| 2889 | - * @param mixed $default if we don't find anything, what should we return? |
|
| 2890 | - * @return mixed single value if $single; array if ! $single |
|
| 2891 | - * @throws ReflectionException |
|
| 2892 | - * @throws InvalidArgumentException |
|
| 2893 | - * @throws InvalidInterfaceException |
|
| 2894 | - * @throws InvalidDataTypeException |
|
| 2895 | - * @throws EE_Error |
|
| 2896 | - */ |
|
| 2897 | - public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2898 | - { |
|
| 2899 | - if ($single) { |
|
| 2900 | - $result = $this->get_first_related( |
|
| 2901 | - 'Extra_Meta', |
|
| 2902 | - array(array('EXM_key' => $meta_key)) |
|
| 2903 | - ); |
|
| 2904 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2905 | - return $result->value(); |
|
| 2906 | - } |
|
| 2907 | - } else { |
|
| 2908 | - $results = $this->get_many_related( |
|
| 2909 | - 'Extra_Meta', |
|
| 2910 | - array(array('EXM_key' => $meta_key)) |
|
| 2911 | - ); |
|
| 2912 | - if ($results) { |
|
| 2913 | - $values = array(); |
|
| 2914 | - foreach ($results as $result) { |
|
| 2915 | - if ($result instanceof EE_Extra_Meta) { |
|
| 2916 | - $values[ $result->ID() ] = $result->value(); |
|
| 2917 | - } |
|
| 2918 | - } |
|
| 2919 | - return $values; |
|
| 2920 | - } |
|
| 2921 | - } |
|
| 2922 | - // if nothing discovered yet return default. |
|
| 2923 | - return apply_filters( |
|
| 2924 | - 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
| 2925 | - $default, |
|
| 2926 | - $meta_key, |
|
| 2927 | - $single, |
|
| 2928 | - $this |
|
| 2929 | - ); |
|
| 2930 | - } |
|
| 2931 | - |
|
| 2932 | - |
|
| 2933 | - /** |
|
| 2934 | - * Returns a simple array of all the extra meta associated with this model object. |
|
| 2935 | - * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2936 | - * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2937 | - * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2938 | - * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2939 | - * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2940 | - * finally the extra meta's value as each sub-value. (eg |
|
| 2941 | - * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2942 | - * |
|
| 2943 | - * @param boolean $one_of_each_key |
|
| 2944 | - * @return array |
|
| 2945 | - * @throws ReflectionException |
|
| 2946 | - * @throws InvalidArgumentException |
|
| 2947 | - * @throws InvalidInterfaceException |
|
| 2948 | - * @throws InvalidDataTypeException |
|
| 2949 | - * @throws EE_Error |
|
| 2950 | - */ |
|
| 2951 | - public function all_extra_meta_array($one_of_each_key = true) |
|
| 2952 | - { |
|
| 2953 | - $return_array = array(); |
|
| 2954 | - if ($one_of_each_key) { |
|
| 2955 | - $extra_meta_objs = $this->get_many_related( |
|
| 2956 | - 'Extra_Meta', |
|
| 2957 | - array('group_by' => 'EXM_key') |
|
| 2958 | - ); |
|
| 2959 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2960 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2961 | - $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
| 2962 | - } |
|
| 2963 | - } |
|
| 2964 | - } else { |
|
| 2965 | - $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2966 | - foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2967 | - if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2968 | - if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
| 2969 | - $return_array[ $extra_meta_obj->key() ] = array(); |
|
| 2970 | - } |
|
| 2971 | - $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
| 2972 | - } |
|
| 2973 | - } |
|
| 2974 | - } |
|
| 2975 | - return $return_array; |
|
| 2976 | - } |
|
| 2977 | - |
|
| 2978 | - |
|
| 2979 | - /** |
|
| 2980 | - * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2981 | - * |
|
| 2982 | - * @return string |
|
| 2983 | - * @throws ReflectionException |
|
| 2984 | - * @throws InvalidArgumentException |
|
| 2985 | - * @throws InvalidInterfaceException |
|
| 2986 | - * @throws InvalidDataTypeException |
|
| 2987 | - * @throws EE_Error |
|
| 2988 | - */ |
|
| 2989 | - public function name() |
|
| 2990 | - { |
|
| 2991 | - // find a field that's not a text field |
|
| 2992 | - $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2993 | - if ($field_we_can_use) { |
|
| 2994 | - return $this->get($field_we_can_use->get_name()); |
|
| 2995 | - } |
|
| 2996 | - $first_few_properties = $this->model_field_array(); |
|
| 2997 | - $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2998 | - $name_parts = array(); |
|
| 2999 | - foreach ($first_few_properties as $name => $value) { |
|
| 3000 | - $name_parts[] = "$name:$value"; |
|
| 3001 | - } |
|
| 3002 | - return implode(',', $name_parts); |
|
| 3003 | - } |
|
| 3004 | - |
|
| 3005 | - |
|
| 3006 | - /** |
|
| 3007 | - * in_entity_map |
|
| 3008 | - * Checks if this model object has been proven to already be in the entity map |
|
| 3009 | - * |
|
| 3010 | - * @return boolean |
|
| 3011 | - * @throws ReflectionException |
|
| 3012 | - * @throws InvalidArgumentException |
|
| 3013 | - * @throws InvalidInterfaceException |
|
| 3014 | - * @throws InvalidDataTypeException |
|
| 3015 | - * @throws EE_Error |
|
| 3016 | - */ |
|
| 3017 | - public function in_entity_map() |
|
| 3018 | - { |
|
| 3019 | - // well, if we looked, did we find it in the entity map? |
|
| 3020 | - return $this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this; |
|
| 3021 | - } |
|
| 3022 | - |
|
| 3023 | - |
|
| 3024 | - /** |
|
| 3025 | - * refresh_from_db |
|
| 3026 | - * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 3027 | - * |
|
| 3028 | - * @throws ReflectionException |
|
| 3029 | - * @throws InvalidArgumentException |
|
| 3030 | - * @throws InvalidInterfaceException |
|
| 3031 | - * @throws InvalidDataTypeException |
|
| 3032 | - * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 3033 | - * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 3034 | - */ |
|
| 3035 | - public function refresh_from_db() |
|
| 3036 | - { |
|
| 3037 | - if ($this->ID() && $this->in_entity_map()) { |
|
| 3038 | - $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 3039 | - } else { |
|
| 3040 | - // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 3041 | - // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 3042 | - // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 3043 | - // absolutely nothing in it for this ID |
|
| 3044 | - if (WP_DEBUG) { |
|
| 3045 | - throw new EE_Error( |
|
| 3046 | - sprintf( |
|
| 3047 | - esc_html__( |
|
| 3048 | - '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.', |
|
| 3049 | - 'event_espresso' |
|
| 3050 | - ), |
|
| 3051 | - $this->ID(), |
|
| 3052 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 3053 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 3054 | - ) |
|
| 3055 | - ); |
|
| 3056 | - } |
|
| 3057 | - } |
|
| 3058 | - } |
|
| 3059 | - |
|
| 3060 | - |
|
| 3061 | - /** |
|
| 3062 | - * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
|
| 3063 | - * |
|
| 3064 | - * @since 4.9.80.p |
|
| 3065 | - * @param EE_Model_Field_Base[] $fields |
|
| 3066 | - * @param string $new_value_sql |
|
| 3067 | - * example: 'column_name=123', |
|
| 3068 | - * or 'column_name=column_name+1', |
|
| 3069 | - * or 'column_name= CASE |
|
| 3070 | - * WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
|
| 3071 | - * THEN `column_name` + 5 |
|
| 3072 | - * ELSE `column_name` |
|
| 3073 | - * END' |
|
| 3074 | - * Also updates $field on this model object with the latest value from the database. |
|
| 3075 | - * @return bool |
|
| 3076 | - * @throws EE_Error |
|
| 3077 | - * @throws InvalidArgumentException |
|
| 3078 | - * @throws InvalidDataTypeException |
|
| 3079 | - * @throws InvalidInterfaceException |
|
| 3080 | - * @throws ReflectionException |
|
| 3081 | - */ |
|
| 3082 | - protected function updateFieldsInDB($fields, $new_value_sql) |
|
| 3083 | - { |
|
| 3084 | - // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
|
| 3085 | - // if it wasn't even there to start off. |
|
| 3086 | - if (! $this->ID()) { |
|
| 3087 | - $this->save(); |
|
| 3088 | - } |
|
| 3089 | - global $wpdb; |
|
| 3090 | - if (empty($fields)) { |
|
| 3091 | - throw new InvalidArgumentException( |
|
| 3092 | - esc_html__( |
|
| 3093 | - 'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.', |
|
| 3094 | - 'event_espresso' |
|
| 3095 | - ) |
|
| 3096 | - ); |
|
| 3097 | - } |
|
| 3098 | - $first_field = reset($fields); |
|
| 3099 | - $table_alias = $first_field->get_table_alias(); |
|
| 3100 | - foreach ($fields as $field) { |
|
| 3101 | - if ($table_alias !== $field->get_table_alias()) { |
|
| 3102 | - throw new InvalidArgumentException( |
|
| 3103 | - sprintf( |
|
| 3104 | - esc_html__( |
|
| 3105 | - // @codingStandardsIgnoreStart |
|
| 3106 | - 'EE_Base_Class::updateFieldsInDB was passed fields for different tables ("%1$s" and "%2$s"), which is not supported. Instead, please call the method multiple times.', |
|
| 3107 | - // @codingStandardsIgnoreEnd |
|
| 3108 | - 'event_espresso' |
|
| 3109 | - ), |
|
| 3110 | - $table_alias, |
|
| 3111 | - $field->get_table_alias() |
|
| 3112 | - ) |
|
| 3113 | - ); |
|
| 3114 | - } |
|
| 3115 | - } |
|
| 3116 | - // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it. |
|
| 3117 | - $table_obj = $this->get_model()->get_table_obj_by_alias($table_alias); |
|
| 3118 | - $table_pk_value = $this->ID(); |
|
| 3119 | - $table_name = $table_obj->get_table_name(); |
|
| 3120 | - if ($table_obj instanceof EE_Secondary_Table) { |
|
| 3121 | - $table_pk_field_name = $table_obj->get_fk_on_table(); |
|
| 3122 | - } else { |
|
| 3123 | - $table_pk_field_name = $table_obj->get_pk_column(); |
|
| 3124 | - } |
|
| 3125 | - |
|
| 3126 | - $query = |
|
| 3127 | - "UPDATE `{$table_name}` |
|
| 16 | + /** |
|
| 17 | + * This is an array of the original properties and values provided during construction |
|
| 18 | + * of this model object. (keys are model field names, values are their values). |
|
| 19 | + * This list is important to remember so that when we are merging data from the db, we know |
|
| 20 | + * which values to override and which to not override. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $_props_n_values_provided_in_constructor; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Timezone |
|
| 28 | + * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
|
| 29 | + * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
|
| 30 | + * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
|
| 31 | + * access to it. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + protected $_timezone; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * date format |
|
| 39 | + * pattern or format for displaying dates |
|
| 40 | + * |
|
| 41 | + * @var string $_dt_frmt |
|
| 42 | + */ |
|
| 43 | + protected $_dt_frmt; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * time format |
|
| 47 | + * pattern or format for displaying time |
|
| 48 | + * |
|
| 49 | + * @var string $_tm_frmt |
|
| 50 | + */ |
|
| 51 | + protected $_tm_frmt; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * This property is for holding a cached array of object properties indexed by property name as the key. |
|
| 55 | + * The purpose of this is for setting a cache on properties that may have calculated values after a |
|
| 56 | + * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
|
| 57 | + * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
|
| 58 | + * |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + protected $_cached_properties = array(); |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * An array containing keys of the related model, and values are either an array of related mode objects or a |
|
| 65 | + * single |
|
| 66 | + * related model object. see the model's _model_relations. The keys should match those specified. And if the |
|
| 67 | + * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
|
| 68 | + * all others have an array) |
|
| 69 | + * |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected $_model_relations = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
|
| 76 | + * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
|
| 77 | + * |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected $_fields = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @var boolean indicating whether or not this model object is intended to ever be saved |
|
| 84 | + * For example, we might create model objects intended to only be used for the duration |
|
| 85 | + * of this request and to be thrown away, and if they were accidentally saved |
|
| 86 | + * it would be a bug. |
|
| 87 | + */ |
|
| 88 | + protected $_allow_persist = true; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @var boolean indicating whether or not this model object's properties have changed since construction |
|
| 92 | + */ |
|
| 93 | + protected $_has_changes = false; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @var EEM_Base |
|
| 97 | + */ |
|
| 98 | + protected $_model; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
|
| 102 | + * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
|
| 103 | + * the db. They also do not automatically update if there are any changes to the data that produced their results. |
|
| 104 | + * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
|
| 105 | + * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
|
| 106 | + * array as: |
|
| 107 | + * array( |
|
| 108 | + * 'Registration_Count' => 24 |
|
| 109 | + * ); |
|
| 110 | + * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
|
| 111 | + * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
|
| 112 | + * info) |
|
| 113 | + * |
|
| 114 | + * @var array |
|
| 115 | + */ |
|
| 116 | + protected $custom_selection_results = array(); |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
|
| 121 | + * play nice |
|
| 122 | + * |
|
| 123 | + * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
|
| 124 | + * layer of the model's _fields array, (eg, EVT_ID, |
|
| 125 | + * TXN_amount, QST_name, etc) and values are their values |
|
| 126 | + * @param boolean $bydb a flag for setting if the class is instantiated by the |
|
| 127 | + * corresponding db model or not. |
|
| 128 | + * @param string $timezone indicate what timezone you want any datetime fields to |
|
| 129 | + * be in when instantiating a EE_Base_Class object. |
|
| 130 | + * @param array $date_formats An array of date formats to set on construct where first |
|
| 131 | + * value is the date_format and second value is the time |
|
| 132 | + * format. |
|
| 133 | + * @throws InvalidArgumentException |
|
| 134 | + * @throws InvalidInterfaceException |
|
| 135 | + * @throws InvalidDataTypeException |
|
| 136 | + * @throws EE_Error |
|
| 137 | + * @throws ReflectionException |
|
| 138 | + */ |
|
| 139 | + protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
|
| 140 | + { |
|
| 141 | + $className = get_class($this); |
|
| 142 | + do_action("AHEE__{$className}__construct", $this, $fieldValues); |
|
| 143 | + $model = $this->get_model(); |
|
| 144 | + $model_fields = $model->field_settings(false); |
|
| 145 | + // ensure $fieldValues is an array |
|
| 146 | + $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
|
| 147 | + // verify client code has not passed any invalid field names |
|
| 148 | + foreach ($fieldValues as $field_name => $field_value) { |
|
| 149 | + if (! isset($model_fields[ $field_name ])) { |
|
| 150 | + throw new EE_Error( |
|
| 151 | + sprintf( |
|
| 152 | + esc_html__( |
|
| 153 | + 'Invalid field (%s) passed to constructor of %s. Allowed fields are :%s', |
|
| 154 | + 'event_espresso' |
|
| 155 | + ), |
|
| 156 | + $field_name, |
|
| 157 | + get_class($this), |
|
| 158 | + implode(', ', array_keys($model_fields)) |
|
| 159 | + ) |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 164 | + if (! empty($date_formats) && is_array($date_formats)) { |
|
| 165 | + list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
|
| 166 | + } else { |
|
| 167 | + // set default formats for date and time |
|
| 168 | + $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d'); |
|
| 169 | + $this->_tm_frmt = (string) get_option('time_format', 'g:i a'); |
|
| 170 | + } |
|
| 171 | + // if db model is instantiating |
|
| 172 | + if ($bydb) { |
|
| 173 | + // client code has indicated these field values are from the database |
|
| 174 | + foreach ($model_fields as $fieldName => $field) { |
|
| 175 | + $this->set_from_db( |
|
| 176 | + $fieldName, |
|
| 177 | + isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
| 178 | + ); |
|
| 179 | + } |
|
| 180 | + } else { |
|
| 181 | + // we're constructing a brand |
|
| 182 | + // new instance of the model object. Generally, this means we'll need to do more field validation |
|
| 183 | + foreach ($model_fields as $fieldName => $field) { |
|
| 184 | + $this->set( |
|
| 185 | + $fieldName, |
|
| 186 | + isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
| 187 | + true |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + // remember what values were passed to this constructor |
|
| 192 | + $this->_props_n_values_provided_in_constructor = $fieldValues; |
|
| 193 | + // remember in entity mapper |
|
| 194 | + if (! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 195 | + $model->add_to_entity_map($this); |
|
| 196 | + } |
|
| 197 | + // setup all the relations |
|
| 198 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 199 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 200 | + $this->_model_relations[ $relation_name ] = null; |
|
| 201 | + } else { |
|
| 202 | + $this->_model_relations[ $relation_name ] = array(); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + /** |
|
| 206 | + * Action done at the end of each model object construction |
|
| 207 | + * |
|
| 208 | + * @param EE_Base_Class $this the model object just created |
|
| 209 | + */ |
|
| 210 | + do_action('AHEE__EE_Base_Class__construct__finished', $this); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Gets whether or not this model object is allowed to persist/be saved to the database. |
|
| 216 | + * |
|
| 217 | + * @return boolean |
|
| 218 | + */ |
|
| 219 | + public function allow_persist() |
|
| 220 | + { |
|
| 221 | + return $this->_allow_persist; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Sets whether or not this model object should be allowed to be saved to the DB. |
|
| 227 | + * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
|
| 228 | + * you got new information that somehow made you change your mind. |
|
| 229 | + * |
|
| 230 | + * @param boolean $allow_persist |
|
| 231 | + * @return boolean |
|
| 232 | + */ |
|
| 233 | + public function set_allow_persist($allow_persist) |
|
| 234 | + { |
|
| 235 | + return $this->_allow_persist = $allow_persist; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Gets the field's original value when this object was constructed during this request. |
|
| 241 | + * This can be helpful when determining if a model object has changed or not |
|
| 242 | + * |
|
| 243 | + * @param string $field_name |
|
| 244 | + * @return mixed|null |
|
| 245 | + * @throws ReflectionException |
|
| 246 | + * @throws InvalidArgumentException |
|
| 247 | + * @throws InvalidInterfaceException |
|
| 248 | + * @throws InvalidDataTypeException |
|
| 249 | + * @throws EE_Error |
|
| 250 | + */ |
|
| 251 | + public function get_original($field_name) |
|
| 252 | + { |
|
| 253 | + if (isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
| 254 | + && $field_settings = $this->get_model()->field_settings_for($field_name) |
|
| 255 | + ) { |
|
| 256 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
| 257 | + } |
|
| 258 | + return null; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * @param EE_Base_Class $obj |
|
| 264 | + * @return string |
|
| 265 | + */ |
|
| 266 | + public function get_class($obj) |
|
| 267 | + { |
|
| 268 | + return get_class($obj); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Overrides parent because parent expects old models. |
|
| 274 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 275 | + * |
|
| 276 | + * @param string $field_name |
|
| 277 | + * @param mixed $field_value |
|
| 278 | + * @param bool $use_default |
|
| 279 | + * @throws InvalidArgumentException |
|
| 280 | + * @throws InvalidInterfaceException |
|
| 281 | + * @throws InvalidDataTypeException |
|
| 282 | + * @throws EE_Error |
|
| 283 | + * @throws ReflectionException |
|
| 284 | + * @throws ReflectionException |
|
| 285 | + * @throws ReflectionException |
|
| 286 | + */ |
|
| 287 | + public function set($field_name, $field_value, $use_default = false) |
|
| 288 | + { |
|
| 289 | + // if not using default and nothing has changed, and object has already been setup (has ID), |
|
| 290 | + // then don't do anything |
|
| 291 | + if (! $use_default |
|
| 292 | + && $this->_fields[ $field_name ] === $field_value |
|
| 293 | + && $this->ID() |
|
| 294 | + ) { |
|
| 295 | + return; |
|
| 296 | + } |
|
| 297 | + $model = $this->get_model(); |
|
| 298 | + $this->_has_changes = true; |
|
| 299 | + $field_obj = $model->field_settings_for($field_name); |
|
| 300 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 301 | + // if ( method_exists( $field_obj, 'set_timezone' )) { |
|
| 302 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 303 | + $field_obj->set_timezone($this->_timezone); |
|
| 304 | + $field_obj->set_date_format($this->_dt_frmt); |
|
| 305 | + $field_obj->set_time_format($this->_tm_frmt); |
|
| 306 | + } |
|
| 307 | + $holder_of_value = $field_obj->prepare_for_set($field_value); |
|
| 308 | + // should the value be null? |
|
| 309 | + if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
|
| 310 | + $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
| 311 | + /** |
|
| 312 | + * To save having to refactor all the models, if a default value is used for a |
|
| 313 | + * EE_Datetime_Field, and that value is not null nor is it a DateTime |
|
| 314 | + * object. Then let's do a set again to ensure that it becomes a DateTime |
|
| 315 | + * object. |
|
| 316 | + * |
|
| 317 | + * @since 4.6.10+ |
|
| 318 | + */ |
|
| 319 | + if ($field_obj instanceof EE_Datetime_Field |
|
| 320 | + && $this->_fields[ $field_name ] !== null |
|
| 321 | + && ! $this->_fields[ $field_name ] instanceof DateTime |
|
| 322 | + ) { |
|
| 323 | + empty($this->_fields[ $field_name ]) |
|
| 324 | + ? $this->set($field_name, time()) |
|
| 325 | + : $this->set($field_name, $this->_fields[ $field_name ]); |
|
| 326 | + } |
|
| 327 | + } else { |
|
| 328 | + $this->_fields[ $field_name ] = $holder_of_value; |
|
| 329 | + } |
|
| 330 | + // if we're not in the constructor... |
|
| 331 | + // now check if what we set was a primary key |
|
| 332 | + if (// note: props_n_values_provided_in_constructor is only set at the END of the constructor |
|
| 333 | + $this->_props_n_values_provided_in_constructor |
|
| 334 | + && $field_value |
|
| 335 | + && $field_name === $model->primary_key_name() |
|
| 336 | + ) { |
|
| 337 | + // if so, we want all this object's fields to be filled either with |
|
| 338 | + // what we've explicitly set on this model |
|
| 339 | + // or what we have in the db |
|
| 340 | + // echo "setting primary key!"; |
|
| 341 | + $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
|
| 342 | + $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
|
| 343 | + foreach ($fields_on_model as $field_obj) { |
|
| 344 | + if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 345 | + && $field_obj->get_name() !== $field_name |
|
| 346 | + ) { |
|
| 347 | + $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + // oh this model object has an ID? well make sure its in the entity mapper |
|
| 351 | + $model->add_to_entity_map($this); |
|
| 352 | + } |
|
| 353 | + // let's unset any cache for this field_name from the $_cached_properties property. |
|
| 354 | + $this->_clear_cached_property($field_name); |
|
| 355 | + } else { |
|
| 356 | + throw new EE_Error( |
|
| 357 | + sprintf( |
|
| 358 | + esc_html__( |
|
| 359 | + 'A valid EE_Model_Field_Base could not be found for the given field name: %s', |
|
| 360 | + 'event_espresso' |
|
| 361 | + ), |
|
| 362 | + $field_name |
|
| 363 | + ) |
|
| 364 | + ); |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Set custom select values for model. |
|
| 371 | + * |
|
| 372 | + * @param array $custom_select_values |
|
| 373 | + */ |
|
| 374 | + public function setCustomSelectsValues(array $custom_select_values) |
|
| 375 | + { |
|
| 376 | + $this->custom_selection_results = $custom_select_values; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Returns the custom select value for the provided alias if its set. |
|
| 382 | + * If not set, returns null. |
|
| 383 | + * |
|
| 384 | + * @param string $alias |
|
| 385 | + * @return string|int|float|null |
|
| 386 | + */ |
|
| 387 | + public function getCustomSelect($alias) |
|
| 388 | + { |
|
| 389 | + return isset($this->custom_selection_results[ $alias ]) |
|
| 390 | + ? $this->custom_selection_results[ $alias ] |
|
| 391 | + : null; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * This sets the field value on the db column if it exists for the given $column_name or |
|
| 397 | + * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
|
| 398 | + * |
|
| 399 | + * @see EE_message::get_column_value for related documentation on the necessity of this method. |
|
| 400 | + * @param string $field_name Must be the exact column name. |
|
| 401 | + * @param mixed $field_value The value to set. |
|
| 402 | + * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
|
| 403 | + * @throws InvalidArgumentException |
|
| 404 | + * @throws InvalidInterfaceException |
|
| 405 | + * @throws InvalidDataTypeException |
|
| 406 | + * @throws EE_Error |
|
| 407 | + * @throws ReflectionException |
|
| 408 | + */ |
|
| 409 | + public function set_field_or_extra_meta($field_name, $field_value) |
|
| 410 | + { |
|
| 411 | + if ($this->get_model()->has_field($field_name)) { |
|
| 412 | + $this->set($field_name, $field_value); |
|
| 413 | + return true; |
|
| 414 | + } |
|
| 415 | + // ensure this object is saved first so that extra meta can be properly related. |
|
| 416 | + $this->save(); |
|
| 417 | + return $this->update_extra_meta($field_name, $field_value); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * This retrieves the value of the db column set on this class or if that's not present |
|
| 423 | + * it will attempt to retrieve from extra_meta if found. |
|
| 424 | + * Example Usage: |
|
| 425 | + * Via EE_Message child class: |
|
| 426 | + * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
|
| 427 | + * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
|
| 428 | + * also have additional main fields specific to the messenger. The system accommodates those extra |
|
| 429 | + * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
|
| 430 | + * value for those extra fields dynamically via the EE_message object. |
|
| 431 | + * |
|
| 432 | + * @param string $field_name expecting the fully qualified field name. |
|
| 433 | + * @return mixed|null value for the field if found. null if not found. |
|
| 434 | + * @throws ReflectionException |
|
| 435 | + * @throws InvalidArgumentException |
|
| 436 | + * @throws InvalidInterfaceException |
|
| 437 | + * @throws InvalidDataTypeException |
|
| 438 | + * @throws EE_Error |
|
| 439 | + */ |
|
| 440 | + public function get_field_or_extra_meta($field_name) |
|
| 441 | + { |
|
| 442 | + if ($this->get_model()->has_field($field_name)) { |
|
| 443 | + $column_value = $this->get($field_name); |
|
| 444 | + } else { |
|
| 445 | + // This isn't a column in the main table, let's see if it is in the extra meta. |
|
| 446 | + $column_value = $this->get_extra_meta($field_name, true, null); |
|
| 447 | + } |
|
| 448 | + return $column_value; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
|
| 454 | + * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
|
| 455 | + * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
|
| 456 | + * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
|
| 457 | + * |
|
| 458 | + * @access public |
|
| 459 | + * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
|
| 460 | + * @return void |
|
| 461 | + * @throws InvalidArgumentException |
|
| 462 | + * @throws InvalidInterfaceException |
|
| 463 | + * @throws InvalidDataTypeException |
|
| 464 | + * @throws EE_Error |
|
| 465 | + * @throws ReflectionException |
|
| 466 | + */ |
|
| 467 | + public function set_timezone($timezone = '') |
|
| 468 | + { |
|
| 469 | + $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
|
| 470 | + // make sure we clear all cached properties because they won't be relevant now |
|
| 471 | + $this->_clear_cached_properties(); |
|
| 472 | + // make sure we update field settings and the date for all EE_Datetime_Fields |
|
| 473 | + $model_fields = $this->get_model()->field_settings(false); |
|
| 474 | + foreach ($model_fields as $field_name => $field_obj) { |
|
| 475 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 476 | + $field_obj->set_timezone($this->_timezone); |
|
| 477 | + if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
| 478 | + EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * This just returns whatever is set for the current timezone. |
|
| 487 | + * |
|
| 488 | + * @access public |
|
| 489 | + * @return string timezone string |
|
| 490 | + */ |
|
| 491 | + public function get_timezone() |
|
| 492 | + { |
|
| 493 | + return $this->_timezone; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * This sets the internal date format to what is sent in to be used as the new default for the class |
|
| 499 | + * internally instead of wp set date format options |
|
| 500 | + * |
|
| 501 | + * @since 4.6 |
|
| 502 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 503 | + */ |
|
| 504 | + public function set_date_format($format) |
|
| 505 | + { |
|
| 506 | + $this->_dt_frmt = $format; |
|
| 507 | + // clear cached_properties because they won't be relevant now. |
|
| 508 | + $this->_clear_cached_properties(); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * This sets the internal time format string to what is sent in to be used as the new default for the |
|
| 514 | + * class internally instead of wp set time format options. |
|
| 515 | + * |
|
| 516 | + * @since 4.6 |
|
| 517 | + * @param string $format should be a format recognizable by PHP date() functions. |
|
| 518 | + */ |
|
| 519 | + public function set_time_format($format) |
|
| 520 | + { |
|
| 521 | + $this->_tm_frmt = $format; |
|
| 522 | + // clear cached_properties because they won't be relevant now. |
|
| 523 | + $this->_clear_cached_properties(); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * This returns the current internal set format for the date and time formats. |
|
| 529 | + * |
|
| 530 | + * @param bool $full if true (default), then return the full format. Otherwise will return an array |
|
| 531 | + * where the first value is the date format and the second value is the time format. |
|
| 532 | + * @return mixed string|array |
|
| 533 | + */ |
|
| 534 | + public function get_format($full = true) |
|
| 535 | + { |
|
| 536 | + return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * cache |
|
| 542 | + * stores the passed model object on the current model object. |
|
| 543 | + * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
|
| 544 | + * |
|
| 545 | + * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
|
| 546 | + * 'Registration' associated with this model object |
|
| 547 | + * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
|
| 548 | + * that could be a payment or a registration) |
|
| 549 | + * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
|
| 550 | + * items which will be stored in an array on this object |
|
| 551 | + * @throws ReflectionException |
|
| 552 | + * @throws InvalidArgumentException |
|
| 553 | + * @throws InvalidInterfaceException |
|
| 554 | + * @throws InvalidDataTypeException |
|
| 555 | + * @throws EE_Error |
|
| 556 | + * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
|
| 557 | + * related thing, no array) |
|
| 558 | + */ |
|
| 559 | + public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
|
| 560 | + { |
|
| 561 | + // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
|
| 562 | + if (! $object_to_cache instanceof EE_Base_Class) { |
|
| 563 | + return false; |
|
| 564 | + } |
|
| 565 | + // also get "how" the object is related, or throw an error |
|
| 566 | + if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 567 | + throw new EE_Error( |
|
| 568 | + sprintf( |
|
| 569 | + esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
|
| 570 | + $relationName, |
|
| 571 | + get_class($this) |
|
| 572 | + ) |
|
| 573 | + ); |
|
| 574 | + } |
|
| 575 | + // how many things are related ? |
|
| 576 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 577 | + // if it's a "belongs to" relationship, then there's only one related model object |
|
| 578 | + // eg, if this is a registration, there's only 1 attendee for it |
|
| 579 | + // so for these model objects just set it to be cached |
|
| 580 | + $this->_model_relations[ $relationName ] = $object_to_cache; |
|
| 581 | + $return = true; |
|
| 582 | + } else { |
|
| 583 | + // otherwise, this is the "many" side of a one to many relationship, |
|
| 584 | + // so we'll add the object to the array of related objects for that type. |
|
| 585 | + // eg: if this is an event, there are many registrations for that event, |
|
| 586 | + // so we cache the registrations in an array |
|
| 587 | + if (! is_array($this->_model_relations[ $relationName ])) { |
|
| 588 | + // if for some reason, the cached item is a model object, |
|
| 589 | + // then stick that in the array, otherwise start with an empty array |
|
| 590 | + $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
| 591 | + instanceof |
|
| 592 | + EE_Base_Class |
|
| 593 | + ? array($this->_model_relations[ $relationName ]) : array(); |
|
| 594 | + } |
|
| 595 | + // first check for a cache_id which is normally empty |
|
| 596 | + if (! empty($cache_id)) { |
|
| 597 | + // if the cache_id exists, then it means we are purposely trying to cache this |
|
| 598 | + // with a known key that can then be used to retrieve the object later on |
|
| 599 | + $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
| 600 | + $return = $cache_id; |
|
| 601 | + } elseif ($object_to_cache->ID()) { |
|
| 602 | + // OR the cached object originally came from the db, so let's just use it's PK for an ID |
|
| 603 | + $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
| 604 | + $return = $object_to_cache->ID(); |
|
| 605 | + } else { |
|
| 606 | + // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
|
| 607 | + $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
| 608 | + // move the internal pointer to the end of the array |
|
| 609 | + end($this->_model_relations[ $relationName ]); |
|
| 610 | + // and grab the key so that we can return it |
|
| 611 | + $return = key($this->_model_relations[ $relationName ]); |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + return $return; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + |
|
| 618 | + /** |
|
| 619 | + * For adding an item to the cached_properties property. |
|
| 620 | + * |
|
| 621 | + * @access protected |
|
| 622 | + * @param string $fieldname the property item the corresponding value is for. |
|
| 623 | + * @param mixed $value The value we are caching. |
|
| 624 | + * @param string|null $cache_type |
|
| 625 | + * @return void |
|
| 626 | + * @throws ReflectionException |
|
| 627 | + * @throws InvalidArgumentException |
|
| 628 | + * @throws InvalidInterfaceException |
|
| 629 | + * @throws InvalidDataTypeException |
|
| 630 | + * @throws EE_Error |
|
| 631 | + */ |
|
| 632 | + protected function _set_cached_property($fieldname, $value, $cache_type = null) |
|
| 633 | + { |
|
| 634 | + // first make sure this property exists |
|
| 635 | + $this->get_model()->field_settings_for($fieldname); |
|
| 636 | + $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
|
| 637 | + $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + |
|
| 641 | + /** |
|
| 642 | + * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
|
| 643 | + * This also SETS the cache if we return the actual property! |
|
| 644 | + * |
|
| 645 | + * @param string $fieldname the name of the property we're trying to retrieve |
|
| 646 | + * @param bool $pretty |
|
| 647 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 648 | + * (in cases where the same property may be used for different outputs |
|
| 649 | + * - i.e. datetime, money etc.) |
|
| 650 | + * It can also accept certain pre-defined "schema" strings |
|
| 651 | + * to define how to output the property. |
|
| 652 | + * see the field's prepare_for_pretty_echoing for what strings can be used |
|
| 653 | + * @return mixed whatever the value for the property is we're retrieving |
|
| 654 | + * @throws ReflectionException |
|
| 655 | + * @throws InvalidArgumentException |
|
| 656 | + * @throws InvalidInterfaceException |
|
| 657 | + * @throws InvalidDataTypeException |
|
| 658 | + * @throws EE_Error |
|
| 659 | + */ |
|
| 660 | + protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 661 | + { |
|
| 662 | + // verify the field exists |
|
| 663 | + $model = $this->get_model(); |
|
| 664 | + $model->field_settings_for($fieldname); |
|
| 665 | + $cache_type = $pretty ? 'pretty' : 'standard'; |
|
| 666 | + $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 667 | + if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
| 668 | + return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
| 669 | + } |
|
| 670 | + $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
|
| 671 | + $this->_set_cached_property($fieldname, $value, $cache_type); |
|
| 672 | + return $value; |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * If the cache didn't fetch the needed item, this fetches it. |
|
| 678 | + * |
|
| 679 | + * @param string $fieldname |
|
| 680 | + * @param bool $pretty |
|
| 681 | + * @param string $extra_cache_ref |
|
| 682 | + * @return mixed |
|
| 683 | + * @throws InvalidArgumentException |
|
| 684 | + * @throws InvalidInterfaceException |
|
| 685 | + * @throws InvalidDataTypeException |
|
| 686 | + * @throws EE_Error |
|
| 687 | + * @throws ReflectionException |
|
| 688 | + */ |
|
| 689 | + protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
|
| 690 | + { |
|
| 691 | + $field_obj = $this->get_model()->field_settings_for($fieldname); |
|
| 692 | + // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct |
|
| 693 | + if ($field_obj instanceof EE_Datetime_Field) { |
|
| 694 | + $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
|
| 695 | + } |
|
| 696 | + if (! isset($this->_fields[ $fieldname ])) { |
|
| 697 | + $this->_fields[ $fieldname ] = null; |
|
| 698 | + } |
|
| 699 | + $value = $pretty |
|
| 700 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
| 701 | + : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
| 702 | + return $value; |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * set timezone, formats, and output for EE_Datetime_Field objects |
|
| 708 | + * |
|
| 709 | + * @param \EE_Datetime_Field $datetime_field |
|
| 710 | + * @param bool $pretty |
|
| 711 | + * @param null $date_or_time |
|
| 712 | + * @return void |
|
| 713 | + * @throws InvalidArgumentException |
|
| 714 | + * @throws InvalidInterfaceException |
|
| 715 | + * @throws InvalidDataTypeException |
|
| 716 | + * @throws EE_Error |
|
| 717 | + */ |
|
| 718 | + protected function _prepare_datetime_field( |
|
| 719 | + EE_Datetime_Field $datetime_field, |
|
| 720 | + $pretty = false, |
|
| 721 | + $date_or_time = null |
|
| 722 | + ) { |
|
| 723 | + $datetime_field->set_timezone($this->_timezone); |
|
| 724 | + $datetime_field->set_date_format($this->_dt_frmt, $pretty); |
|
| 725 | + $datetime_field->set_time_format($this->_tm_frmt, $pretty); |
|
| 726 | + // set the output returned |
|
| 727 | + switch ($date_or_time) { |
|
| 728 | + case 'D': |
|
| 729 | + $datetime_field->set_date_time_output('date'); |
|
| 730 | + break; |
|
| 731 | + case 'T': |
|
| 732 | + $datetime_field->set_date_time_output('time'); |
|
| 733 | + break; |
|
| 734 | + default: |
|
| 735 | + $datetime_field->set_date_time_output(); |
|
| 736 | + } |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * This just takes care of clearing out the cached_properties |
|
| 742 | + * |
|
| 743 | + * @return void |
|
| 744 | + */ |
|
| 745 | + protected function _clear_cached_properties() |
|
| 746 | + { |
|
| 747 | + $this->_cached_properties = array(); |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + |
|
| 751 | + /** |
|
| 752 | + * This just clears out ONE property if it exists in the cache |
|
| 753 | + * |
|
| 754 | + * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
|
| 755 | + * @return void |
|
| 756 | + */ |
|
| 757 | + protected function _clear_cached_property($property_name) |
|
| 758 | + { |
|
| 759 | + if (isset($this->_cached_properties[ $property_name ])) { |
|
| 760 | + unset($this->_cached_properties[ $property_name ]); |
|
| 761 | + } |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * Ensures that this related thing is a model object. |
|
| 767 | + * |
|
| 768 | + * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
|
| 769 | + * @param string $model_name name of the related thing, eg 'Attendee', |
|
| 770 | + * @return EE_Base_Class |
|
| 771 | + * @throws ReflectionException |
|
| 772 | + * @throws InvalidArgumentException |
|
| 773 | + * @throws InvalidInterfaceException |
|
| 774 | + * @throws InvalidDataTypeException |
|
| 775 | + * @throws EE_Error |
|
| 776 | + */ |
|
| 777 | + protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
|
| 778 | + { |
|
| 779 | + $other_model_instance = self::_get_model_instance_with_name( |
|
| 780 | + self::_get_model_classname($model_name), |
|
| 781 | + $this->_timezone |
|
| 782 | + ); |
|
| 783 | + return $other_model_instance->ensure_is_obj($object_or_id); |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + |
|
| 787 | + /** |
|
| 788 | + * Forgets the cached model of the given relation Name. So the next time we request it, |
|
| 789 | + * we will fetch it again from the database. (Handy if you know it's changed somehow). |
|
| 790 | + * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
|
| 791 | + * then only remove that one object from our cached array. Otherwise, clear the entire list |
|
| 792 | + * |
|
| 793 | + * @param string $relationName one of the keys in the _model_relations array on the model. |
|
| 794 | + * Eg 'Registration' |
|
| 795 | + * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
|
| 796 | + * if you intend to use $clear_all = TRUE, or the relation only |
|
| 797 | + * has 1 object anyways (ie, it's a BelongsToRelation) |
|
| 798 | + * @param bool $clear_all This flags clearing the entire cache relation property if |
|
| 799 | + * this is HasMany or HABTM. |
|
| 800 | + * @throws ReflectionException |
|
| 801 | + * @throws InvalidArgumentException |
|
| 802 | + * @throws InvalidInterfaceException |
|
| 803 | + * @throws InvalidDataTypeException |
|
| 804 | + * @throws EE_Error |
|
| 805 | + * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
|
| 806 | + * relation from all |
|
| 807 | + */ |
|
| 808 | + public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
|
| 809 | + { |
|
| 810 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 811 | + $index_in_cache = ''; |
|
| 812 | + if (! $relationship_to_model) { |
|
| 813 | + throw new EE_Error( |
|
| 814 | + sprintf( |
|
| 815 | + esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
|
| 816 | + $relationName, |
|
| 817 | + get_class($this) |
|
| 818 | + ) |
|
| 819 | + ); |
|
| 820 | + } |
|
| 821 | + if ($clear_all) { |
|
| 822 | + $obj_removed = true; |
|
| 823 | + $this->_model_relations[ $relationName ] = null; |
|
| 824 | + } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 825 | + $obj_removed = $this->_model_relations[ $relationName ]; |
|
| 826 | + $this->_model_relations[ $relationName ] = null; |
|
| 827 | + } else { |
|
| 828 | + if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
|
| 829 | + && $object_to_remove_or_index_into_array->ID() |
|
| 830 | + ) { |
|
| 831 | + $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
|
| 832 | + if (is_array($this->_model_relations[ $relationName ]) |
|
| 833 | + && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
| 834 | + ) { |
|
| 835 | + $index_found_at = null; |
|
| 836 | + // find this object in the array even though it has a different key |
|
| 837 | + foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
| 838 | + /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 839 | + if ($obj instanceof EE_Base_Class |
|
| 840 | + && ( |
|
| 841 | + $obj == $object_to_remove_or_index_into_array |
|
| 842 | + || $obj->ID() === $object_to_remove_or_index_into_array->ID() |
|
| 843 | + ) |
|
| 844 | + ) { |
|
| 845 | + $index_found_at = $index; |
|
| 846 | + break; |
|
| 847 | + } |
|
| 848 | + } |
|
| 849 | + if ($index_found_at) { |
|
| 850 | + $index_in_cache = $index_found_at; |
|
| 851 | + } else { |
|
| 852 | + // it wasn't found. huh. well obviously it doesn't need to be removed from teh cache |
|
| 853 | + // if it wasn't in it to begin with. So we're done |
|
| 854 | + return $object_to_remove_or_index_into_array; |
|
| 855 | + } |
|
| 856 | + } |
|
| 857 | + } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) { |
|
| 858 | + // so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it! |
|
| 859 | + foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) { |
|
| 860 | + /** @noinspection TypeUnsafeComparisonInspection */ |
|
| 861 | + if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) { |
|
| 862 | + $index_in_cache = $index; |
|
| 863 | + } |
|
| 864 | + } |
|
| 865 | + } else { |
|
| 866 | + $index_in_cache = $object_to_remove_or_index_into_array; |
|
| 867 | + } |
|
| 868 | + // supposedly we've found it. But it could just be that the client code |
|
| 869 | + // provided a bad index/object |
|
| 870 | + if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
| 871 | + $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
| 872 | + unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
| 873 | + } else { |
|
| 874 | + // that thing was never cached anyways. |
|
| 875 | + $obj_removed = null; |
|
| 876 | + } |
|
| 877 | + } |
|
| 878 | + return $obj_removed; |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * update_cache_after_object_save |
|
| 884 | + * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
|
| 885 | + * obtained after being saved to the db |
|
| 886 | + * |
|
| 887 | + * @param string $relationName - the type of object that is cached |
|
| 888 | + * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
|
| 889 | + * @param string $current_cache_id - the ID that was used when originally caching the object |
|
| 890 | + * @return boolean TRUE on success, FALSE on fail |
|
| 891 | + * @throws ReflectionException |
|
| 892 | + * @throws InvalidArgumentException |
|
| 893 | + * @throws InvalidInterfaceException |
|
| 894 | + * @throws InvalidDataTypeException |
|
| 895 | + * @throws EE_Error |
|
| 896 | + */ |
|
| 897 | + public function update_cache_after_object_save( |
|
| 898 | + $relationName, |
|
| 899 | + EE_Base_Class $newly_saved_object, |
|
| 900 | + $current_cache_id = '' |
|
| 901 | + ) { |
|
| 902 | + // verify that incoming object is of the correct type |
|
| 903 | + $obj_class = 'EE_' . $relationName; |
|
| 904 | + if ($newly_saved_object instanceof $obj_class) { |
|
| 905 | + /* @type EE_Base_Class $newly_saved_object */ |
|
| 906 | + // now get the type of relation |
|
| 907 | + $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
|
| 908 | + // if this is a 1:1 relationship |
|
| 909 | + if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
|
| 910 | + // then just replace the cached object with the newly saved object |
|
| 911 | + $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
| 912 | + return true; |
|
| 913 | + // or if it's some kind of sordid feral polyamorous relationship... |
|
| 914 | + } |
|
| 915 | + if (is_array($this->_model_relations[ $relationName ]) |
|
| 916 | + && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
| 917 | + ) { |
|
| 918 | + // then remove the current cached item |
|
| 919 | + unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
| 920 | + // and cache the newly saved object using it's new ID |
|
| 921 | + $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
| 922 | + return true; |
|
| 923 | + } |
|
| 924 | + } |
|
| 925 | + return false; |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 931 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 932 | + * |
|
| 933 | + * @param string $relationName |
|
| 934 | + * @return EE_Base_Class |
|
| 935 | + */ |
|
| 936 | + public function get_one_from_cache($relationName) |
|
| 937 | + { |
|
| 938 | + $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
| 939 | + ? $this->_model_relations[ $relationName ] |
|
| 940 | + : null; |
|
| 941 | + if (is_array($cached_array_or_object)) { |
|
| 942 | + return array_shift($cached_array_or_object); |
|
| 943 | + } |
|
| 944 | + return $cached_array_or_object; |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + |
|
| 948 | + /** |
|
| 949 | + * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
|
| 950 | + * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
|
| 951 | + * |
|
| 952 | + * @param string $relationName |
|
| 953 | + * @throws ReflectionException |
|
| 954 | + * @throws InvalidArgumentException |
|
| 955 | + * @throws InvalidInterfaceException |
|
| 956 | + * @throws InvalidDataTypeException |
|
| 957 | + * @throws EE_Error |
|
| 958 | + * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
|
| 959 | + */ |
|
| 960 | + public function get_all_from_cache($relationName) |
|
| 961 | + { |
|
| 962 | + $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
| 963 | + // if the result is not an array, but exists, make it an array |
|
| 964 | + $objects = is_array($objects) ? $objects : array($objects); |
|
| 965 | + // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
|
| 966 | + // basically, if this model object was stored in the session, and these cached model objects |
|
| 967 | + // already have IDs, let's make sure they're in their model's entity mapper |
|
| 968 | + // otherwise we will have duplicates next time we call |
|
| 969 | + // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() ); |
|
| 970 | + $model = EE_Registry::instance()->load_model($relationName); |
|
| 971 | + foreach ($objects as $model_object) { |
|
| 972 | + if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) { |
|
| 973 | + // ensure its in the map if it has an ID; otherwise it will be added to the map when its saved |
|
| 974 | + if ($model_object->ID()) { |
|
| 975 | + $model->add_to_entity_map($model_object); |
|
| 976 | + } |
|
| 977 | + } else { |
|
| 978 | + throw new EE_Error( |
|
| 979 | + sprintf( |
|
| 980 | + esc_html__( |
|
| 981 | + 'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object', |
|
| 982 | + 'event_espresso' |
|
| 983 | + ), |
|
| 984 | + $relationName, |
|
| 985 | + gettype($model_object) |
|
| 986 | + ) |
|
| 987 | + ); |
|
| 988 | + } |
|
| 989 | + } |
|
| 990 | + return $objects; |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + |
|
| 994 | + /** |
|
| 995 | + * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 996 | + * matching the given query conditions. |
|
| 997 | + * |
|
| 998 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 999 | + * @param int $limit How many objects to return. |
|
| 1000 | + * @param array $query_params Any additional conditions on the query. |
|
| 1001 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1002 | + * you can indicate just the columns you want returned |
|
| 1003 | + * @return array|EE_Base_Class[] |
|
| 1004 | + * @throws ReflectionException |
|
| 1005 | + * @throws InvalidArgumentException |
|
| 1006 | + * @throws InvalidInterfaceException |
|
| 1007 | + * @throws InvalidDataTypeException |
|
| 1008 | + * @throws EE_Error |
|
| 1009 | + */ |
|
| 1010 | + public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 1011 | + { |
|
| 1012 | + $model = $this->get_model(); |
|
| 1013 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1014 | + ? $model->get_primary_key_field()->get_name() |
|
| 1015 | + : $field_to_order_by; |
|
| 1016 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1017 | + if (empty($field) || empty($current_value)) { |
|
| 1018 | + return array(); |
|
| 1019 | + } |
|
| 1020 | + return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + |
|
| 1024 | + /** |
|
| 1025 | + * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
|
| 1026 | + * matching the given query conditions. |
|
| 1027 | + * |
|
| 1028 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1029 | + * @param int $limit How many objects to return. |
|
| 1030 | + * @param array $query_params Any additional conditions on the query. |
|
| 1031 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1032 | + * you can indicate just the columns you want returned |
|
| 1033 | + * @return array|EE_Base_Class[] |
|
| 1034 | + * @throws ReflectionException |
|
| 1035 | + * @throws InvalidArgumentException |
|
| 1036 | + * @throws InvalidInterfaceException |
|
| 1037 | + * @throws InvalidDataTypeException |
|
| 1038 | + * @throws EE_Error |
|
| 1039 | + */ |
|
| 1040 | + public function previous_x( |
|
| 1041 | + $field_to_order_by = null, |
|
| 1042 | + $limit = 1, |
|
| 1043 | + $query_params = array(), |
|
| 1044 | + $columns_to_select = null |
|
| 1045 | + ) { |
|
| 1046 | + $model = $this->get_model(); |
|
| 1047 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1048 | + ? $model->get_primary_key_field()->get_name() |
|
| 1049 | + : $field_to_order_by; |
|
| 1050 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1051 | + if (empty($field) || empty($current_value)) { |
|
| 1052 | + return array(); |
|
| 1053 | + } |
|
| 1054 | + return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select); |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + |
|
| 1058 | + /** |
|
| 1059 | + * Returns the next EE_Base_Class object in sequence from this object as found in the database |
|
| 1060 | + * matching the given query conditions. |
|
| 1061 | + * |
|
| 1062 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1063 | + * @param array $query_params Any additional conditions on the query. |
|
| 1064 | + * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
|
| 1065 | + * you can indicate just the columns you want returned |
|
| 1066 | + * @return array|EE_Base_Class |
|
| 1067 | + * @throws ReflectionException |
|
| 1068 | + * @throws InvalidArgumentException |
|
| 1069 | + * @throws InvalidInterfaceException |
|
| 1070 | + * @throws InvalidDataTypeException |
|
| 1071 | + * @throws EE_Error |
|
| 1072 | + */ |
|
| 1073 | + public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1074 | + { |
|
| 1075 | + $model = $this->get_model(); |
|
| 1076 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1077 | + ? $model->get_primary_key_field()->get_name() |
|
| 1078 | + : $field_to_order_by; |
|
| 1079 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1080 | + if (empty($field) || empty($current_value)) { |
|
| 1081 | + return array(); |
|
| 1082 | + } |
|
| 1083 | + return $model->next($current_value, $field, $query_params, $columns_to_select); |
|
| 1084 | + } |
|
| 1085 | + |
|
| 1086 | + |
|
| 1087 | + /** |
|
| 1088 | + * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
|
| 1089 | + * matching the given query conditions. |
|
| 1090 | + * |
|
| 1091 | + * @param null $field_to_order_by What field is being used as the reference point. |
|
| 1092 | + * @param array $query_params Any additional conditions on the query. |
|
| 1093 | + * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
|
| 1094 | + * you can indicate just the column you want returned |
|
| 1095 | + * @return array|EE_Base_Class |
|
| 1096 | + * @throws ReflectionException |
|
| 1097 | + * @throws InvalidArgumentException |
|
| 1098 | + * @throws InvalidInterfaceException |
|
| 1099 | + * @throws InvalidDataTypeException |
|
| 1100 | + * @throws EE_Error |
|
| 1101 | + */ |
|
| 1102 | + public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1103 | + { |
|
| 1104 | + $model = $this->get_model(); |
|
| 1105 | + $field = empty($field_to_order_by) && $model->has_primary_key_field() |
|
| 1106 | + ? $model->get_primary_key_field()->get_name() |
|
| 1107 | + : $field_to_order_by; |
|
| 1108 | + $current_value = ! empty($field) ? $this->get($field) : null; |
|
| 1109 | + if (empty($field) || empty($current_value)) { |
|
| 1110 | + return array(); |
|
| 1111 | + } |
|
| 1112 | + return $model->previous($current_value, $field, $query_params, $columns_to_select); |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + |
|
| 1116 | + /** |
|
| 1117 | + * Overrides parent because parent expects old models. |
|
| 1118 | + * This also doesn't do any validation, and won't work for serialized arrays |
|
| 1119 | + * |
|
| 1120 | + * @param string $field_name |
|
| 1121 | + * @param mixed $field_value_from_db |
|
| 1122 | + * @throws ReflectionException |
|
| 1123 | + * @throws InvalidArgumentException |
|
| 1124 | + * @throws InvalidInterfaceException |
|
| 1125 | + * @throws InvalidDataTypeException |
|
| 1126 | + * @throws EE_Error |
|
| 1127 | + */ |
|
| 1128 | + public function set_from_db($field_name, $field_value_from_db) |
|
| 1129 | + { |
|
| 1130 | + $field_obj = $this->get_model()->field_settings_for($field_name); |
|
| 1131 | + if ($field_obj instanceof EE_Model_Field_Base) { |
|
| 1132 | + // you would think the DB has no NULLs for non-null label fields right? wrong! |
|
| 1133 | + // eg, a CPT model object could have an entry in the posts table, but no |
|
| 1134 | + // entry in the meta table. Meaning that all its columns in the meta table |
|
| 1135 | + // are null! yikes! so when we find one like that, use defaults for its meta columns |
|
| 1136 | + if ($field_value_from_db === null) { |
|
| 1137 | + if ($field_obj->is_nullable()) { |
|
| 1138 | + // if the field allows nulls, then let it be null |
|
| 1139 | + $field_value = null; |
|
| 1140 | + } else { |
|
| 1141 | + $field_value = $field_obj->get_default_value(); |
|
| 1142 | + } |
|
| 1143 | + } else { |
|
| 1144 | + $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
|
| 1145 | + } |
|
| 1146 | + $this->_fields[ $field_name ] = $field_value; |
|
| 1147 | + $this->_clear_cached_property($field_name); |
|
| 1148 | + } |
|
| 1149 | + } |
|
| 1150 | + |
|
| 1151 | + |
|
| 1152 | + /** |
|
| 1153 | + * verifies that the specified field is of the correct type |
|
| 1154 | + * |
|
| 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 ReflectionException |
|
| 1161 | + * @throws InvalidArgumentException |
|
| 1162 | + * @throws InvalidInterfaceException |
|
| 1163 | + * @throws InvalidDataTypeException |
|
| 1164 | + * @throws EE_Error |
|
| 1165 | + */ |
|
| 1166 | + public function get($field_name, $extra_cache_ref = null) |
|
| 1167 | + { |
|
| 1168 | + return $this->_get_cached_property($field_name, false, $extra_cache_ref); |
|
| 1169 | + } |
|
| 1170 | + |
|
| 1171 | + |
|
| 1172 | + /** |
|
| 1173 | + * This method simply returns the RAW unprocessed value for the given property in this class |
|
| 1174 | + * |
|
| 1175 | + * @param string $field_name A valid fieldname |
|
| 1176 | + * @return mixed Whatever the raw value stored on the property is. |
|
| 1177 | + * @throws ReflectionException |
|
| 1178 | + * @throws InvalidArgumentException |
|
| 1179 | + * @throws InvalidInterfaceException |
|
| 1180 | + * @throws InvalidDataTypeException |
|
| 1181 | + * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
|
| 1182 | + */ |
|
| 1183 | + public function get_raw($field_name) |
|
| 1184 | + { |
|
| 1185 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1186 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1187 | + ? $this->_fields[ $field_name ]->format('U') |
|
| 1188 | + : $this->_fields[ $field_name ]; |
|
| 1189 | + } |
|
| 1190 | + |
|
| 1191 | + |
|
| 1192 | + /** |
|
| 1193 | + * This is used to return the internal DateTime object used for a field that is a |
|
| 1194 | + * EE_Datetime_Field. |
|
| 1195 | + * |
|
| 1196 | + * @param string $field_name The field name retrieving the DateTime object. |
|
| 1197 | + * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
|
| 1198 | + * @throws EE_Error an error is set and false returned. If the field IS an |
|
| 1199 | + * EE_Datetime_Field and but the field value is null, then |
|
| 1200 | + * just null is returned (because that indicates that likely |
|
| 1201 | + * this field is nullable). |
|
| 1202 | + * @throws InvalidArgumentException |
|
| 1203 | + * @throws InvalidDataTypeException |
|
| 1204 | + * @throws InvalidInterfaceException |
|
| 1205 | + * @throws ReflectionException |
|
| 1206 | + */ |
|
| 1207 | + public function get_DateTime_object($field_name) |
|
| 1208 | + { |
|
| 1209 | + $field_settings = $this->get_model()->field_settings_for($field_name); |
|
| 1210 | + if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1211 | + EE_Error::add_error( |
|
| 1212 | + sprintf( |
|
| 1213 | + esc_html__( |
|
| 1214 | + 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
|
| 1215 | + 'event_espresso' |
|
| 1216 | + ), |
|
| 1217 | + $field_name |
|
| 1218 | + ), |
|
| 1219 | + __FILE__, |
|
| 1220 | + __FUNCTION__, |
|
| 1221 | + __LINE__ |
|
| 1222 | + ); |
|
| 1223 | + return false; |
|
| 1224 | + } |
|
| 1225 | + return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1226 | + ? clone $this->_fields[ $field_name ] |
|
| 1227 | + : null; |
|
| 1228 | + } |
|
| 1229 | + |
|
| 1230 | + |
|
| 1231 | + /** |
|
| 1232 | + * To be used in template to immediately echo out the value, and format it for output. |
|
| 1233 | + * Eg, should call stripslashes and whatnot before echoing |
|
| 1234 | + * |
|
| 1235 | + * @param string $field_name the name of the field as it appears in the DB |
|
| 1236 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1237 | + * (in cases where the same property may be used for different outputs |
|
| 1238 | + * - i.e. datetime, money etc.) |
|
| 1239 | + * @return void |
|
| 1240 | + * @throws ReflectionException |
|
| 1241 | + * @throws InvalidArgumentException |
|
| 1242 | + * @throws InvalidInterfaceException |
|
| 1243 | + * @throws InvalidDataTypeException |
|
| 1244 | + * @throws EE_Error |
|
| 1245 | + */ |
|
| 1246 | + public function e($field_name, $extra_cache_ref = null) |
|
| 1247 | + { |
|
| 1248 | + echo $this->get_pretty($field_name, $extra_cache_ref); |
|
| 1249 | + } |
|
| 1250 | + |
|
| 1251 | + |
|
| 1252 | + /** |
|
| 1253 | + * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
|
| 1254 | + * can be easily used as the value of form input. |
|
| 1255 | + * |
|
| 1256 | + * @param string $field_name |
|
| 1257 | + * @return void |
|
| 1258 | + * @throws ReflectionException |
|
| 1259 | + * @throws InvalidArgumentException |
|
| 1260 | + * @throws InvalidInterfaceException |
|
| 1261 | + * @throws InvalidDataTypeException |
|
| 1262 | + * @throws EE_Error |
|
| 1263 | + */ |
|
| 1264 | + public function f($field_name) |
|
| 1265 | + { |
|
| 1266 | + $this->e($field_name, 'form_input'); |
|
| 1267 | + } |
|
| 1268 | + |
|
| 1269 | + |
|
| 1270 | + /** |
|
| 1271 | + * Same as `f()` but just returns the value instead of echoing it |
|
| 1272 | + * |
|
| 1273 | + * @param string $field_name |
|
| 1274 | + * @return string |
|
| 1275 | + * @throws ReflectionException |
|
| 1276 | + * @throws InvalidArgumentException |
|
| 1277 | + * @throws InvalidInterfaceException |
|
| 1278 | + * @throws InvalidDataTypeException |
|
| 1279 | + * @throws EE_Error |
|
| 1280 | + */ |
|
| 1281 | + public function get_f($field_name) |
|
| 1282 | + { |
|
| 1283 | + return (string) $this->get_pretty($field_name, 'form_input'); |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + |
|
| 1287 | + /** |
|
| 1288 | + * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
|
| 1289 | + * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
|
| 1290 | + * to see what options are available. |
|
| 1291 | + * |
|
| 1292 | + * @param string $field_name |
|
| 1293 | + * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
|
| 1294 | + * (in cases where the same property may be used for different outputs |
|
| 1295 | + * - i.e. datetime, money etc.) |
|
| 1296 | + * @return mixed |
|
| 1297 | + * @throws ReflectionException |
|
| 1298 | + * @throws InvalidArgumentException |
|
| 1299 | + * @throws InvalidInterfaceException |
|
| 1300 | + * @throws InvalidDataTypeException |
|
| 1301 | + * @throws EE_Error |
|
| 1302 | + */ |
|
| 1303 | + public function get_pretty($field_name, $extra_cache_ref = null) |
|
| 1304 | + { |
|
| 1305 | + return $this->_get_cached_property($field_name, true, $extra_cache_ref); |
|
| 1306 | + } |
|
| 1307 | + |
|
| 1308 | + |
|
| 1309 | + /** |
|
| 1310 | + * This simply returns the datetime for the given field name |
|
| 1311 | + * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
|
| 1312 | + * (and the equivalent e_date, e_time, e_datetime). |
|
| 1313 | + * |
|
| 1314 | + * @access protected |
|
| 1315 | + * @param string $field_name Field on the instantiated EE_Base_Class child object |
|
| 1316 | + * @param string $dt_frmt valid datetime format used for date |
|
| 1317 | + * (if '' then we just use the default on the field, |
|
| 1318 | + * if NULL we use the last-used format) |
|
| 1319 | + * @param string $tm_frmt Same as above except this is for time format |
|
| 1320 | + * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
|
| 1321 | + * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
|
| 1322 | + * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
|
| 1323 | + * if field is not a valid dtt field, or void if echoing |
|
| 1324 | + * @throws ReflectionException |
|
| 1325 | + * @throws InvalidArgumentException |
|
| 1326 | + * @throws InvalidInterfaceException |
|
| 1327 | + * @throws InvalidDataTypeException |
|
| 1328 | + * @throws EE_Error |
|
| 1329 | + */ |
|
| 1330 | + protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
|
| 1331 | + { |
|
| 1332 | + // clear cached property |
|
| 1333 | + $this->_clear_cached_property($field_name); |
|
| 1334 | + // reset format properties because they are used in get() |
|
| 1335 | + $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt; |
|
| 1336 | + $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt; |
|
| 1337 | + if ($echo) { |
|
| 1338 | + $this->e($field_name, $date_or_time); |
|
| 1339 | + return ''; |
|
| 1340 | + } |
|
| 1341 | + return $this->get($field_name, $date_or_time); |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + |
|
| 1345 | + /** |
|
| 1346 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
|
| 1347 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1348 | + * other echoes the pretty value for dtt) |
|
| 1349 | + * |
|
| 1350 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1351 | + * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1352 | + * @return string datetime value formatted |
|
| 1353 | + * @throws ReflectionException |
|
| 1354 | + * @throws InvalidArgumentException |
|
| 1355 | + * @throws InvalidInterfaceException |
|
| 1356 | + * @throws InvalidDataTypeException |
|
| 1357 | + * @throws EE_Error |
|
| 1358 | + */ |
|
| 1359 | + public function get_date($field_name, $format = '') |
|
| 1360 | + { |
|
| 1361 | + return $this->_get_datetime($field_name, $format, null, 'D'); |
|
| 1362 | + } |
|
| 1363 | + |
|
| 1364 | + |
|
| 1365 | + /** |
|
| 1366 | + * @param $field_name |
|
| 1367 | + * @param string $format |
|
| 1368 | + * @throws ReflectionException |
|
| 1369 | + * @throws InvalidArgumentException |
|
| 1370 | + * @throws InvalidInterfaceException |
|
| 1371 | + * @throws InvalidDataTypeException |
|
| 1372 | + * @throws EE_Error |
|
| 1373 | + */ |
|
| 1374 | + public function e_date($field_name, $format = '') |
|
| 1375 | + { |
|
| 1376 | + $this->_get_datetime($field_name, $format, null, 'D', true); |
|
| 1377 | + } |
|
| 1378 | + |
|
| 1379 | + |
|
| 1380 | + /** |
|
| 1381 | + * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
|
| 1382 | + * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1383 | + * other echoes the pretty value for dtt) |
|
| 1384 | + * |
|
| 1385 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1386 | + * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
|
| 1387 | + * @return string datetime value formatted |
|
| 1388 | + * @throws ReflectionException |
|
| 1389 | + * @throws InvalidArgumentException |
|
| 1390 | + * @throws InvalidInterfaceException |
|
| 1391 | + * @throws InvalidDataTypeException |
|
| 1392 | + * @throws EE_Error |
|
| 1393 | + */ |
|
| 1394 | + public function get_time($field_name, $format = '') |
|
| 1395 | + { |
|
| 1396 | + return $this->_get_datetime($field_name, null, $format, 'T'); |
|
| 1397 | + } |
|
| 1398 | + |
|
| 1399 | + |
|
| 1400 | + /** |
|
| 1401 | + * @param $field_name |
|
| 1402 | + * @param string $format |
|
| 1403 | + * @throws ReflectionException |
|
| 1404 | + * @throws InvalidArgumentException |
|
| 1405 | + * @throws InvalidInterfaceException |
|
| 1406 | + * @throws InvalidDataTypeException |
|
| 1407 | + * @throws EE_Error |
|
| 1408 | + */ |
|
| 1409 | + public function e_time($field_name, $format = '') |
|
| 1410 | + { |
|
| 1411 | + $this->_get_datetime($field_name, null, $format, 'T', true); |
|
| 1412 | + } |
|
| 1413 | + |
|
| 1414 | + |
|
| 1415 | + /** |
|
| 1416 | + * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
|
| 1417 | + * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
|
| 1418 | + * other echoes the pretty value for dtt) |
|
| 1419 | + * |
|
| 1420 | + * @param string $field_name name of model object datetime field holding the value |
|
| 1421 | + * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
|
| 1422 | + * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
|
| 1423 | + * @return string datetime value formatted |
|
| 1424 | + * @throws ReflectionException |
|
| 1425 | + * @throws InvalidArgumentException |
|
| 1426 | + * @throws InvalidInterfaceException |
|
| 1427 | + * @throws InvalidDataTypeException |
|
| 1428 | + * @throws EE_Error |
|
| 1429 | + */ |
|
| 1430 | + public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1431 | + { |
|
| 1432 | + return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt); |
|
| 1433 | + } |
|
| 1434 | + |
|
| 1435 | + |
|
| 1436 | + /** |
|
| 1437 | + * @param string $field_name |
|
| 1438 | + * @param string $dt_frmt |
|
| 1439 | + * @param string $tm_frmt |
|
| 1440 | + * @throws ReflectionException |
|
| 1441 | + * @throws InvalidArgumentException |
|
| 1442 | + * @throws InvalidInterfaceException |
|
| 1443 | + * @throws InvalidDataTypeException |
|
| 1444 | + * @throws EE_Error |
|
| 1445 | + */ |
|
| 1446 | + public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
|
| 1447 | + { |
|
| 1448 | + $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true); |
|
| 1449 | + } |
|
| 1450 | + |
|
| 1451 | + |
|
| 1452 | + /** |
|
| 1453 | + * Get the i8ln value for a date using the WordPress @see date_i18n function. |
|
| 1454 | + * |
|
| 1455 | + * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
|
| 1456 | + * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
|
| 1457 | + * on the object will be used. |
|
| 1458 | + * @return string Date and time string in set locale or false if no field exists for the given |
|
| 1459 | + * @throws ReflectionException |
|
| 1460 | + * @throws InvalidArgumentException |
|
| 1461 | + * @throws InvalidInterfaceException |
|
| 1462 | + * @throws InvalidDataTypeException |
|
| 1463 | + * @throws EE_Error |
|
| 1464 | + * field name. |
|
| 1465 | + */ |
|
| 1466 | + public function get_i18n_datetime($field_name, $format = '') |
|
| 1467 | + { |
|
| 1468 | + $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1469 | + return date_i18n( |
|
| 1470 | + $format, |
|
| 1471 | + EEH_DTT_Helper::get_timestamp_with_offset( |
|
| 1472 | + $this->get_raw($field_name), |
|
| 1473 | + $this->_timezone |
|
| 1474 | + ) |
|
| 1475 | + ); |
|
| 1476 | + } |
|
| 1477 | + |
|
| 1478 | + |
|
| 1479 | + /** |
|
| 1480 | + * This method validates whether the given field name is a valid field on the model object as well as it is of a |
|
| 1481 | + * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
|
| 1482 | + * thrown. |
|
| 1483 | + * |
|
| 1484 | + * @param string $field_name The field name being checked |
|
| 1485 | + * @throws ReflectionException |
|
| 1486 | + * @throws InvalidArgumentException |
|
| 1487 | + * @throws InvalidInterfaceException |
|
| 1488 | + * @throws InvalidDataTypeException |
|
| 1489 | + * @throws EE_Error |
|
| 1490 | + * @return EE_Datetime_Field |
|
| 1491 | + */ |
|
| 1492 | + protected function _get_dtt_field_settings($field_name) |
|
| 1493 | + { |
|
| 1494 | + $field = $this->get_model()->field_settings_for($field_name); |
|
| 1495 | + // check if field is dtt |
|
| 1496 | + if ($field instanceof EE_Datetime_Field) { |
|
| 1497 | + return $field; |
|
| 1498 | + } |
|
| 1499 | + throw new EE_Error( |
|
| 1500 | + sprintf( |
|
| 1501 | + esc_html__( |
|
| 1502 | + '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', |
|
| 1503 | + 'event_espresso' |
|
| 1504 | + ), |
|
| 1505 | + $field_name, |
|
| 1506 | + self::_get_model_classname(get_class($this)) |
|
| 1507 | + ) |
|
| 1508 | + ); |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + |
|
| 1512 | + |
|
| 1513 | + |
|
| 1514 | + /** |
|
| 1515 | + * NOTE ABOUT BELOW: |
|
| 1516 | + * These convenience date and time setters are for setting date and time independently. In other words you might |
|
| 1517 | + * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
|
| 1518 | + * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
|
| 1519 | + * method and make sure you send the entire datetime value for setting. |
|
| 1520 | + */ |
|
| 1521 | + /** |
|
| 1522 | + * sets the time on a datetime property |
|
| 1523 | + * |
|
| 1524 | + * @access protected |
|
| 1525 | + * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
|
| 1526 | + * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
|
| 1527 | + * @throws ReflectionException |
|
| 1528 | + * @throws InvalidArgumentException |
|
| 1529 | + * @throws InvalidInterfaceException |
|
| 1530 | + * @throws InvalidDataTypeException |
|
| 1531 | + * @throws EE_Error |
|
| 1532 | + */ |
|
| 1533 | + protected function _set_time_for($time, $fieldname) |
|
| 1534 | + { |
|
| 1535 | + $this->_set_date_time('T', $time, $fieldname); |
|
| 1536 | + } |
|
| 1537 | + |
|
| 1538 | + |
|
| 1539 | + /** |
|
| 1540 | + * sets the date on a datetime property |
|
| 1541 | + * |
|
| 1542 | + * @access protected |
|
| 1543 | + * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
|
| 1544 | + * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
|
| 1545 | + * @throws ReflectionException |
|
| 1546 | + * @throws InvalidArgumentException |
|
| 1547 | + * @throws InvalidInterfaceException |
|
| 1548 | + * @throws InvalidDataTypeException |
|
| 1549 | + * @throws EE_Error |
|
| 1550 | + */ |
|
| 1551 | + protected function _set_date_for($date, $fieldname) |
|
| 1552 | + { |
|
| 1553 | + $this->_set_date_time('D', $date, $fieldname); |
|
| 1554 | + } |
|
| 1555 | + |
|
| 1556 | + |
|
| 1557 | + /** |
|
| 1558 | + * This takes care of setting a date or time independently on a given model object property. This method also |
|
| 1559 | + * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
|
| 1560 | + * |
|
| 1561 | + * @access protected |
|
| 1562 | + * @param string $what "T" for time, 'B' for both, 'D' for Date. |
|
| 1563 | + * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
|
| 1564 | + * @param string $fieldname the name of the field the date OR time is being set on (must match a |
|
| 1565 | + * EE_Datetime_Field property) |
|
| 1566 | + * @throws ReflectionException |
|
| 1567 | + * @throws InvalidArgumentException |
|
| 1568 | + * @throws InvalidInterfaceException |
|
| 1569 | + * @throws InvalidDataTypeException |
|
| 1570 | + * @throws EE_Error |
|
| 1571 | + */ |
|
| 1572 | + protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
|
| 1573 | + { |
|
| 1574 | + $field = $this->_get_dtt_field_settings($fieldname); |
|
| 1575 | + $field->set_timezone($this->_timezone); |
|
| 1576 | + $field->set_date_format($this->_dt_frmt); |
|
| 1577 | + $field->set_time_format($this->_tm_frmt); |
|
| 1578 | + switch ($what) { |
|
| 1579 | + case 'T': |
|
| 1580 | + $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
| 1581 | + $datetime_value, |
|
| 1582 | + $this->_fields[ $fieldname ] |
|
| 1583 | + ); |
|
| 1584 | + $this->_has_changes = true; |
|
| 1585 | + break; |
|
| 1586 | + case 'D': |
|
| 1587 | + $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
| 1588 | + $datetime_value, |
|
| 1589 | + $this->_fields[ $fieldname ] |
|
| 1590 | + ); |
|
| 1591 | + $this->_has_changes = true; |
|
| 1592 | + break; |
|
| 1593 | + case 'B': |
|
| 1594 | + $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
| 1595 | + $this->_has_changes = true; |
|
| 1596 | + break; |
|
| 1597 | + } |
|
| 1598 | + $this->_clear_cached_property($fieldname); |
|
| 1599 | + } |
|
| 1600 | + |
|
| 1601 | + |
|
| 1602 | + /** |
|
| 1603 | + * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
|
| 1604 | + * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
|
| 1605 | + * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
|
| 1606 | + * that could lead to some unexpected results! |
|
| 1607 | + * |
|
| 1608 | + * @access public |
|
| 1609 | + * @param string $field_name This is the name of the field on the object that contains the date/time |
|
| 1610 | + * value being returned. |
|
| 1611 | + * @param string $callback must match a valid method in this class (defaults to get_datetime) |
|
| 1612 | + * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
|
| 1613 | + * @param string $prepend You can include something to prepend on the timestamp |
|
| 1614 | + * @param string $append You can include something to append on the timestamp |
|
| 1615 | + * @throws ReflectionException |
|
| 1616 | + * @throws InvalidArgumentException |
|
| 1617 | + * @throws InvalidInterfaceException |
|
| 1618 | + * @throws InvalidDataTypeException |
|
| 1619 | + * @throws EE_Error |
|
| 1620 | + * @return string timestamp |
|
| 1621 | + */ |
|
| 1622 | + public function display_in_my_timezone( |
|
| 1623 | + $field_name, |
|
| 1624 | + $callback = 'get_datetime', |
|
| 1625 | + $args = null, |
|
| 1626 | + $prepend = '', |
|
| 1627 | + $append = '' |
|
| 1628 | + ) { |
|
| 1629 | + $timezone = EEH_DTT_Helper::get_timezone(); |
|
| 1630 | + if ($timezone === $this->_timezone) { |
|
| 1631 | + return ''; |
|
| 1632 | + } |
|
| 1633 | + $original_timezone = $this->_timezone; |
|
| 1634 | + $this->set_timezone($timezone); |
|
| 1635 | + $fn = (array) $field_name; |
|
| 1636 | + $args = array_merge($fn, (array) $args); |
|
| 1637 | + if (! method_exists($this, $callback)) { |
|
| 1638 | + throw new EE_Error( |
|
| 1639 | + sprintf( |
|
| 1640 | + esc_html__( |
|
| 1641 | + 'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist. Please check your spelling', |
|
| 1642 | + 'event_espresso' |
|
| 1643 | + ), |
|
| 1644 | + $callback |
|
| 1645 | + ) |
|
| 1646 | + ); |
|
| 1647 | + } |
|
| 1648 | + $args = (array) $args; |
|
| 1649 | + $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1650 | + $this->set_timezone($original_timezone); |
|
| 1651 | + return $return; |
|
| 1652 | + } |
|
| 1653 | + |
|
| 1654 | + |
|
| 1655 | + /** |
|
| 1656 | + * Deletes this model object. |
|
| 1657 | + * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
|
| 1658 | + * override |
|
| 1659 | + * `EE_Base_Class::_delete` NOT this class. |
|
| 1660 | + * |
|
| 1661 | + * @return boolean | int |
|
| 1662 | + * @throws ReflectionException |
|
| 1663 | + * @throws InvalidArgumentException |
|
| 1664 | + * @throws InvalidInterfaceException |
|
| 1665 | + * @throws InvalidDataTypeException |
|
| 1666 | + * @throws EE_Error |
|
| 1667 | + */ |
|
| 1668 | + public function delete() |
|
| 1669 | + { |
|
| 1670 | + /** |
|
| 1671 | + * Called just before the `EE_Base_Class::_delete` method call. |
|
| 1672 | + * Note: |
|
| 1673 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1674 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1675 | + * For example, `EE_Soft_Delete_Base_Class::_delete` |
|
| 1676 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1677 | + * |
|
| 1678 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1679 | + */ |
|
| 1680 | + do_action('AHEE__EE_Base_Class__delete__before', $this); |
|
| 1681 | + $result = $this->_delete(); |
|
| 1682 | + /** |
|
| 1683 | + * Called just after the `EE_Base_Class::_delete` method call. |
|
| 1684 | + * Note: |
|
| 1685 | + * `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions |
|
| 1686 | + * should be aware that `_delete` may not always result in a permanent delete. |
|
| 1687 | + * For example `EE_Soft_Base_Class::_delete` |
|
| 1688 | + * soft deletes (trash) the object and does not permanently delete it. |
|
| 1689 | + * |
|
| 1690 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1691 | + * @param boolean $result |
|
| 1692 | + */ |
|
| 1693 | + do_action('AHEE__EE_Base_Class__delete__end', $this, $result); |
|
| 1694 | + return $result; |
|
| 1695 | + } |
|
| 1696 | + |
|
| 1697 | + |
|
| 1698 | + /** |
|
| 1699 | + * Calls the specific delete method for the instantiated class. |
|
| 1700 | + * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
|
| 1701 | + * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
|
| 1702 | + * `EE_Base_Class::delete` |
|
| 1703 | + * |
|
| 1704 | + * @return bool|int |
|
| 1705 | + * @throws ReflectionException |
|
| 1706 | + * @throws InvalidArgumentException |
|
| 1707 | + * @throws InvalidInterfaceException |
|
| 1708 | + * @throws InvalidDataTypeException |
|
| 1709 | + * @throws EE_Error |
|
| 1710 | + */ |
|
| 1711 | + protected function _delete() |
|
| 1712 | + { |
|
| 1713 | + return $this->delete_permanently(); |
|
| 1714 | + } |
|
| 1715 | + |
|
| 1716 | + |
|
| 1717 | + /** |
|
| 1718 | + * Deletes this model object permanently from db |
|
| 1719 | + * (but keep in mind related models may block the delete and return an error) |
|
| 1720 | + * |
|
| 1721 | + * @return bool | int |
|
| 1722 | + * @throws ReflectionException |
|
| 1723 | + * @throws InvalidArgumentException |
|
| 1724 | + * @throws InvalidInterfaceException |
|
| 1725 | + * @throws InvalidDataTypeException |
|
| 1726 | + * @throws EE_Error |
|
| 1727 | + */ |
|
| 1728 | + public function delete_permanently() |
|
| 1729 | + { |
|
| 1730 | + /** |
|
| 1731 | + * Called just before HARD deleting a model object |
|
| 1732 | + * |
|
| 1733 | + * @param EE_Base_Class $model_object about to be 'deleted' |
|
| 1734 | + */ |
|
| 1735 | + do_action('AHEE__EE_Base_Class__delete_permanently__before', $this); |
|
| 1736 | + $model = $this->get_model(); |
|
| 1737 | + $result = $model->delete_permanently_by_ID($this->ID()); |
|
| 1738 | + $this->refresh_cache_of_related_objects(); |
|
| 1739 | + /** |
|
| 1740 | + * Called just after HARD deleting a model object |
|
| 1741 | + * |
|
| 1742 | + * @param EE_Base_Class $model_object that was just 'deleted' |
|
| 1743 | + * @param boolean $result |
|
| 1744 | + */ |
|
| 1745 | + do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result); |
|
| 1746 | + return $result; |
|
| 1747 | + } |
|
| 1748 | + |
|
| 1749 | + |
|
| 1750 | + /** |
|
| 1751 | + * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
|
| 1752 | + * related model objects |
|
| 1753 | + * |
|
| 1754 | + * @throws ReflectionException |
|
| 1755 | + * @throws InvalidArgumentException |
|
| 1756 | + * @throws InvalidInterfaceException |
|
| 1757 | + * @throws InvalidDataTypeException |
|
| 1758 | + * @throws EE_Error |
|
| 1759 | + */ |
|
| 1760 | + public function refresh_cache_of_related_objects() |
|
| 1761 | + { |
|
| 1762 | + $model = $this->get_model(); |
|
| 1763 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1764 | + if (! empty($this->_model_relations[ $relation_name ])) { |
|
| 1765 | + $related_objects = $this->_model_relations[ $relation_name ]; |
|
| 1766 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 1767 | + // this relation only stores a single model object, not an array |
|
| 1768 | + // but let's make it consistent |
|
| 1769 | + $related_objects = array($related_objects); |
|
| 1770 | + } |
|
| 1771 | + foreach ($related_objects as $related_object) { |
|
| 1772 | + // only refresh their cache if they're in memory |
|
| 1773 | + if ($related_object instanceof EE_Base_Class) { |
|
| 1774 | + $related_object->clear_cache( |
|
| 1775 | + $model->get_this_model_name(), |
|
| 1776 | + $this |
|
| 1777 | + ); |
|
| 1778 | + } |
|
| 1779 | + } |
|
| 1780 | + } |
|
| 1781 | + } |
|
| 1782 | + } |
|
| 1783 | + |
|
| 1784 | + |
|
| 1785 | + /** |
|
| 1786 | + * Saves this object to the database. An array may be supplied to set some values on this |
|
| 1787 | + * object just before saving. |
|
| 1788 | + * |
|
| 1789 | + * @access public |
|
| 1790 | + * @param array $set_cols_n_values keys are field names, values are their new values, |
|
| 1791 | + * if provided during the save() method (often client code will change the fields' |
|
| 1792 | + * values before calling save) |
|
| 1793 | + * @throws InvalidArgumentException |
|
| 1794 | + * @throws InvalidInterfaceException |
|
| 1795 | + * @throws InvalidDataTypeException |
|
| 1796 | + * @throws EE_Error |
|
| 1797 | + * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
|
| 1798 | + * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
|
| 1799 | + * @throws ReflectionException |
|
| 1800 | + * @throws ReflectionException |
|
| 1801 | + * @throws ReflectionException |
|
| 1802 | + */ |
|
| 1803 | + public function save($set_cols_n_values = array()) |
|
| 1804 | + { |
|
| 1805 | + $model = $this->get_model(); |
|
| 1806 | + /** |
|
| 1807 | + * Filters the fields we're about to save on the model object |
|
| 1808 | + * |
|
| 1809 | + * @param array $set_cols_n_values |
|
| 1810 | + * @param EE_Base_Class $model_object |
|
| 1811 | + */ |
|
| 1812 | + $set_cols_n_values = (array) apply_filters( |
|
| 1813 | + 'FHEE__EE_Base_Class__save__set_cols_n_values', |
|
| 1814 | + $set_cols_n_values, |
|
| 1815 | + $this |
|
| 1816 | + ); |
|
| 1817 | + // set attributes as provided in $set_cols_n_values |
|
| 1818 | + foreach ($set_cols_n_values as $column => $value) { |
|
| 1819 | + $this->set($column, $value); |
|
| 1820 | + } |
|
| 1821 | + // no changes ? then don't do anything |
|
| 1822 | + if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1823 | + return 0; |
|
| 1824 | + } |
|
| 1825 | + /** |
|
| 1826 | + * Saving a model object. |
|
| 1827 | + * Before we perform a save, this action is fired. |
|
| 1828 | + * |
|
| 1829 | + * @param EE_Base_Class $model_object the model object about to be saved. |
|
| 1830 | + */ |
|
| 1831 | + do_action('AHEE__EE_Base_Class__save__begin', $this); |
|
| 1832 | + if (! $this->allow_persist()) { |
|
| 1833 | + return 0; |
|
| 1834 | + } |
|
| 1835 | + // now get current attribute values |
|
| 1836 | + $save_cols_n_values = $this->_fields; |
|
| 1837 | + // if the object already has an ID, update it. Otherwise, insert it |
|
| 1838 | + // also: change the assumption about values passed to the model NOT being prepare dby the model object. |
|
| 1839 | + // They have been |
|
| 1840 | + $old_assumption_concerning_value_preparation = $model |
|
| 1841 | + ->get_assumption_concerning_values_already_prepared_by_model_object(); |
|
| 1842 | + $model->assume_values_already_prepared_by_model_object(true); |
|
| 1843 | + // does this model have an autoincrement PK? |
|
| 1844 | + if ($model->has_primary_key_field()) { |
|
| 1845 | + if ($model->get_primary_key_field()->is_auto_increment()) { |
|
| 1846 | + // ok check if it's set, if so: update; if not, insert |
|
| 1847 | + if (! empty($save_cols_n_values[ $model->primary_key_name() ])) { |
|
| 1848 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1849 | + } else { |
|
| 1850 | + unset($save_cols_n_values[ $model->primary_key_name() ]); |
|
| 1851 | + $results = $model->insert($save_cols_n_values); |
|
| 1852 | + if ($results) { |
|
| 1853 | + // if successful, set the primary key |
|
| 1854 | + // but don't use the normal SET method, because it will check if |
|
| 1855 | + // an item with the same ID exists in the mapper & db, then |
|
| 1856 | + // will find it in the db (because we just added it) and THAT object |
|
| 1857 | + // will get added to the mapper before we can add this one! |
|
| 1858 | + // but if we just avoid using the SET method, all that headache can be avoided |
|
| 1859 | + $pk_field_name = $model->primary_key_name(); |
|
| 1860 | + $this->_fields[ $pk_field_name ] = $results; |
|
| 1861 | + $this->_clear_cached_property($pk_field_name); |
|
| 1862 | + $model->add_to_entity_map($this); |
|
| 1863 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1864 | + } |
|
| 1865 | + } |
|
| 1866 | + } else {// PK is NOT auto-increment |
|
| 1867 | + // so check if one like it already exists in the db |
|
| 1868 | + if ($model->exists_by_ID($this->ID())) { |
|
| 1869 | + if (WP_DEBUG && ! $this->in_entity_map()) { |
|
| 1870 | + throw new EE_Error( |
|
| 1871 | + sprintf( |
|
| 1872 | + esc_html__( |
|
| 1873 | + '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', |
|
| 1874 | + 'event_espresso' |
|
| 1875 | + ), |
|
| 1876 | + get_class($this), |
|
| 1877 | + get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1878 | + get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1879 | + '<br />' |
|
| 1880 | + ) |
|
| 1881 | + ); |
|
| 1882 | + } |
|
| 1883 | + $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
|
| 1884 | + } else { |
|
| 1885 | + $results = $model->insert($save_cols_n_values); |
|
| 1886 | + $this->_update_cached_related_model_objs_fks(); |
|
| 1887 | + } |
|
| 1888 | + } |
|
| 1889 | + } else {// there is NO primary key |
|
| 1890 | + $already_in_db = false; |
|
| 1891 | + foreach ($model->unique_indexes() as $index) { |
|
| 1892 | + $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields()); |
|
| 1893 | + if ($model->exists(array($uniqueness_where_params))) { |
|
| 1894 | + $already_in_db = true; |
|
| 1895 | + } |
|
| 1896 | + } |
|
| 1897 | + if ($already_in_db) { |
|
| 1898 | + $combined_pk_fields_n_values = array_intersect_key( |
|
| 1899 | + $save_cols_n_values, |
|
| 1900 | + $model->get_combined_primary_key_fields() |
|
| 1901 | + ); |
|
| 1902 | + $results = $model->update( |
|
| 1903 | + $save_cols_n_values, |
|
| 1904 | + $combined_pk_fields_n_values |
|
| 1905 | + ); |
|
| 1906 | + } else { |
|
| 1907 | + $results = $model->insert($save_cols_n_values); |
|
| 1908 | + } |
|
| 1909 | + } |
|
| 1910 | + // restore the old assumption about values being prepared by the model object |
|
| 1911 | + $model->assume_values_already_prepared_by_model_object( |
|
| 1912 | + $old_assumption_concerning_value_preparation |
|
| 1913 | + ); |
|
| 1914 | + /** |
|
| 1915 | + * After saving the model object this action is called |
|
| 1916 | + * |
|
| 1917 | + * @param EE_Base_Class $model_object which was just saved |
|
| 1918 | + * @param boolean|int $results if it were updated, TRUE or FALSE; if it were newly inserted |
|
| 1919 | + * the new ID (or 0 if an error occurred and it wasn't updated) |
|
| 1920 | + */ |
|
| 1921 | + do_action('AHEE__EE_Base_Class__save__end', $this, $results); |
|
| 1922 | + $this->_has_changes = false; |
|
| 1923 | + return $results; |
|
| 1924 | + } |
|
| 1925 | + |
|
| 1926 | + |
|
| 1927 | + /** |
|
| 1928 | + * Updates the foreign key on related models objects pointing to this to have this model object's ID |
|
| 1929 | + * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
|
| 1930 | + * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
|
| 1931 | + * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
|
| 1932 | + * 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 |
|
| 1933 | + * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
|
| 1934 | + * or not they exist in the DB (if they do, their DB records will be automatically updated) |
|
| 1935 | + * |
|
| 1936 | + * @return void |
|
| 1937 | + * @throws ReflectionException |
|
| 1938 | + * @throws InvalidArgumentException |
|
| 1939 | + * @throws InvalidInterfaceException |
|
| 1940 | + * @throws InvalidDataTypeException |
|
| 1941 | + * @throws EE_Error |
|
| 1942 | + */ |
|
| 1943 | + protected function _update_cached_related_model_objs_fks() |
|
| 1944 | + { |
|
| 1945 | + $model = $this->get_model(); |
|
| 1946 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 1947 | + if ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 1948 | + foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) { |
|
| 1949 | + $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to( |
|
| 1950 | + $model->get_this_model_name() |
|
| 1951 | + ); |
|
| 1952 | + $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID()); |
|
| 1953 | + if ($related_model_obj_in_cache->ID()) { |
|
| 1954 | + $related_model_obj_in_cache->save(); |
|
| 1955 | + } |
|
| 1956 | + } |
|
| 1957 | + } |
|
| 1958 | + } |
|
| 1959 | + } |
|
| 1960 | + |
|
| 1961 | + |
|
| 1962 | + /** |
|
| 1963 | + * Saves this model object and its NEW cached relations to the database. |
|
| 1964 | + * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
|
| 1965 | + * In order for that to work, we would need to mark model objects as dirty/clean... |
|
| 1966 | + * because otherwise, there's a potential for infinite looping of saving |
|
| 1967 | + * Saves the cached related model objects, and ensures the relation between them |
|
| 1968 | + * and this object and properly setup |
|
| 1969 | + * |
|
| 1970 | + * @return int ID of new model object on save; 0 on failure+ |
|
| 1971 | + * @throws ReflectionException |
|
| 1972 | + * @throws InvalidArgumentException |
|
| 1973 | + * @throws InvalidInterfaceException |
|
| 1974 | + * @throws InvalidDataTypeException |
|
| 1975 | + * @throws EE_Error |
|
| 1976 | + */ |
|
| 1977 | + public function save_new_cached_related_model_objs() |
|
| 1978 | + { |
|
| 1979 | + // make sure this has been saved |
|
| 1980 | + if (! $this->ID()) { |
|
| 1981 | + $id = $this->save(); |
|
| 1982 | + } else { |
|
| 1983 | + $id = $this->ID(); |
|
| 1984 | + } |
|
| 1985 | + // now save all the NEW cached model objects (ie they don't exist in the DB) |
|
| 1986 | + foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
|
| 1987 | + if ($this->_model_relations[ $relationName ]) { |
|
| 1988 | + // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
|
| 1989 | + // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
|
| 1990 | + /* @var $related_model_obj EE_Base_Class */ |
|
| 1991 | + if ($relationObj instanceof EE_Belongs_To_Relation) { |
|
| 1992 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 1993 | + // but ONLY if it DOES NOT exist in the DB |
|
| 1994 | + $related_model_obj = $this->_model_relations[ $relationName ]; |
|
| 1995 | + // if( ! $related_model_obj->ID()){ |
|
| 1996 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 1997 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 1998 | + // } |
|
| 1999 | + } else { |
|
| 2000 | + foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
| 2001 | + // add a relation to that relation type (which saves the appropriate thing in the process) |
|
| 2002 | + // but ONLY if it DOES NOT exist in the DB |
|
| 2003 | + // if( ! $related_model_obj->ID()){ |
|
| 2004 | + $this->_add_relation_to($related_model_obj, $relationName); |
|
| 2005 | + $related_model_obj->save_new_cached_related_model_objs(); |
|
| 2006 | + // } |
|
| 2007 | + } |
|
| 2008 | + } |
|
| 2009 | + } |
|
| 2010 | + } |
|
| 2011 | + return $id; |
|
| 2012 | + } |
|
| 2013 | + |
|
| 2014 | + |
|
| 2015 | + /** |
|
| 2016 | + * for getting a model while instantiated. |
|
| 2017 | + * |
|
| 2018 | + * @return EEM_Base | EEM_CPT_Base |
|
| 2019 | + * @throws ReflectionException |
|
| 2020 | + * @throws InvalidArgumentException |
|
| 2021 | + * @throws InvalidInterfaceException |
|
| 2022 | + * @throws InvalidDataTypeException |
|
| 2023 | + * @throws EE_Error |
|
| 2024 | + */ |
|
| 2025 | + public function get_model() |
|
| 2026 | + { |
|
| 2027 | + if (! $this->_model) { |
|
| 2028 | + $modelName = self::_get_model_classname(get_class($this)); |
|
| 2029 | + $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
|
| 2030 | + } else { |
|
| 2031 | + $this->_model->set_timezone($this->_timezone); |
|
| 2032 | + } |
|
| 2033 | + return $this->_model; |
|
| 2034 | + } |
|
| 2035 | + |
|
| 2036 | + |
|
| 2037 | + /** |
|
| 2038 | + * @param $props_n_values |
|
| 2039 | + * @param $classname |
|
| 2040 | + * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
|
| 2041 | + * @throws ReflectionException |
|
| 2042 | + * @throws InvalidArgumentException |
|
| 2043 | + * @throws InvalidInterfaceException |
|
| 2044 | + * @throws InvalidDataTypeException |
|
| 2045 | + * @throws EE_Error |
|
| 2046 | + */ |
|
| 2047 | + protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
|
| 2048 | + { |
|
| 2049 | + // TODO: will not work for Term_Relationships because they have no PK! |
|
| 2050 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2051 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2052 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2053 | + ) { |
|
| 2054 | + $id = $props_n_values[ $primary_id_ref ]; |
|
| 2055 | + return self::_get_model($classname)->get_from_entity_map($id); |
|
| 2056 | + } |
|
| 2057 | + return false; |
|
| 2058 | + } |
|
| 2059 | + |
|
| 2060 | + |
|
| 2061 | + /** |
|
| 2062 | + * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
|
| 2063 | + * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
|
| 2064 | + * 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 |
|
| 2065 | + * we return false. |
|
| 2066 | + * |
|
| 2067 | + * @param array $props_n_values incoming array of properties and their values |
|
| 2068 | + * @param string $classname the classname of the child class |
|
| 2069 | + * @param null $timezone |
|
| 2070 | + * @param array $date_formats incoming date_formats in an array where the first value is the |
|
| 2071 | + * date_format and the second value is the time format |
|
| 2072 | + * @return mixed (EE_Base_Class|bool) |
|
| 2073 | + * @throws InvalidArgumentException |
|
| 2074 | + * @throws InvalidInterfaceException |
|
| 2075 | + * @throws InvalidDataTypeException |
|
| 2076 | + * @throws EE_Error |
|
| 2077 | + * @throws ReflectionException |
|
| 2078 | + * @throws ReflectionException |
|
| 2079 | + * @throws ReflectionException |
|
| 2080 | + */ |
|
| 2081 | + protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
|
| 2082 | + { |
|
| 2083 | + $existing = null; |
|
| 2084 | + $model = self::_get_model($classname, $timezone); |
|
| 2085 | + if ($model->has_primary_key_field()) { |
|
| 2086 | + $primary_id_ref = self::_get_primary_key_name($classname); |
|
| 2087 | + if (array_key_exists($primary_id_ref, $props_n_values) |
|
| 2088 | + && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2089 | + ) { |
|
| 2090 | + $existing = $model->get_one_by_ID( |
|
| 2091 | + $props_n_values[ $primary_id_ref ] |
|
| 2092 | + ); |
|
| 2093 | + } |
|
| 2094 | + } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
|
| 2095 | + // no primary key on this model, but there's still a matching item in the DB |
|
| 2096 | + $existing = self::_get_model($classname, $timezone)->get_one_by_ID( |
|
| 2097 | + self::_get_model($classname, $timezone) |
|
| 2098 | + ->get_index_primary_key_string($props_n_values) |
|
| 2099 | + ); |
|
| 2100 | + } |
|
| 2101 | + if ($existing) { |
|
| 2102 | + // set date formats if present before setting values |
|
| 2103 | + if (! empty($date_formats) && is_array($date_formats)) { |
|
| 2104 | + $existing->set_date_format($date_formats[0]); |
|
| 2105 | + $existing->set_time_format($date_formats[1]); |
|
| 2106 | + } else { |
|
| 2107 | + // set default formats for date and time |
|
| 2108 | + $existing->set_date_format(get_option('date_format')); |
|
| 2109 | + $existing->set_time_format(get_option('time_format')); |
|
| 2110 | + } |
|
| 2111 | + foreach ($props_n_values as $property => $field_value) { |
|
| 2112 | + $existing->set($property, $field_value); |
|
| 2113 | + } |
|
| 2114 | + return $existing; |
|
| 2115 | + } |
|
| 2116 | + return false; |
|
| 2117 | + } |
|
| 2118 | + |
|
| 2119 | + |
|
| 2120 | + /** |
|
| 2121 | + * Gets the EEM_*_Model for this class |
|
| 2122 | + * |
|
| 2123 | + * @access public now, as this is more convenient |
|
| 2124 | + * @param $classname |
|
| 2125 | + * @param null $timezone |
|
| 2126 | + * @throws ReflectionException |
|
| 2127 | + * @throws InvalidArgumentException |
|
| 2128 | + * @throws InvalidInterfaceException |
|
| 2129 | + * @throws InvalidDataTypeException |
|
| 2130 | + * @throws EE_Error |
|
| 2131 | + * @return EEM_Base |
|
| 2132 | + */ |
|
| 2133 | + protected static function _get_model($classname, $timezone = null) |
|
| 2134 | + { |
|
| 2135 | + // find model for this class |
|
| 2136 | + if (! $classname) { |
|
| 2137 | + throw new EE_Error( |
|
| 2138 | + sprintf( |
|
| 2139 | + esc_html__( |
|
| 2140 | + 'What were you thinking calling _get_model(%s)?? You need to specify the class name', |
|
| 2141 | + 'event_espresso' |
|
| 2142 | + ), |
|
| 2143 | + $classname |
|
| 2144 | + ) |
|
| 2145 | + ); |
|
| 2146 | + } |
|
| 2147 | + $modelName = self::_get_model_classname($classname); |
|
| 2148 | + return self::_get_model_instance_with_name($modelName, $timezone); |
|
| 2149 | + } |
|
| 2150 | + |
|
| 2151 | + |
|
| 2152 | + /** |
|
| 2153 | + * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
|
| 2154 | + * |
|
| 2155 | + * @param string $model_classname |
|
| 2156 | + * @param null $timezone |
|
| 2157 | + * @return EEM_Base |
|
| 2158 | + * @throws ReflectionException |
|
| 2159 | + * @throws InvalidArgumentException |
|
| 2160 | + * @throws InvalidInterfaceException |
|
| 2161 | + * @throws InvalidDataTypeException |
|
| 2162 | + * @throws EE_Error |
|
| 2163 | + */ |
|
| 2164 | + protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
|
| 2165 | + { |
|
| 2166 | + $model_classname = str_replace('EEM_', '', $model_classname); |
|
| 2167 | + $model = EE_Registry::instance()->load_model($model_classname); |
|
| 2168 | + $model->set_timezone($timezone); |
|
| 2169 | + return $model; |
|
| 2170 | + } |
|
| 2171 | + |
|
| 2172 | + |
|
| 2173 | + /** |
|
| 2174 | + * If a model name is provided (eg Registration), gets the model classname for that model. |
|
| 2175 | + * Also works if a model class's classname is provided (eg EE_Registration). |
|
| 2176 | + * |
|
| 2177 | + * @param null $model_name |
|
| 2178 | + * @return string like EEM_Attendee |
|
| 2179 | + */ |
|
| 2180 | + private static function _get_model_classname($model_name = null) |
|
| 2181 | + { |
|
| 2182 | + if (strpos($model_name, 'EE_') === 0) { |
|
| 2183 | + $model_classname = str_replace('EE_', 'EEM_', $model_name); |
|
| 2184 | + } else { |
|
| 2185 | + $model_classname = 'EEM_' . $model_name; |
|
| 2186 | + } |
|
| 2187 | + return $model_classname; |
|
| 2188 | + } |
|
| 2189 | + |
|
| 2190 | + |
|
| 2191 | + /** |
|
| 2192 | + * returns the name of the primary key attribute |
|
| 2193 | + * |
|
| 2194 | + * @param null $classname |
|
| 2195 | + * @throws ReflectionException |
|
| 2196 | + * @throws InvalidArgumentException |
|
| 2197 | + * @throws InvalidInterfaceException |
|
| 2198 | + * @throws InvalidDataTypeException |
|
| 2199 | + * @throws EE_Error |
|
| 2200 | + * @return string |
|
| 2201 | + */ |
|
| 2202 | + protected static function _get_primary_key_name($classname = null) |
|
| 2203 | + { |
|
| 2204 | + if (! $classname) { |
|
| 2205 | + throw new EE_Error( |
|
| 2206 | + sprintf( |
|
| 2207 | + esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
|
| 2208 | + $classname |
|
| 2209 | + ) |
|
| 2210 | + ); |
|
| 2211 | + } |
|
| 2212 | + return self::_get_model($classname)->get_primary_key_field()->get_name(); |
|
| 2213 | + } |
|
| 2214 | + |
|
| 2215 | + |
|
| 2216 | + /** |
|
| 2217 | + * Gets the value of the primary key. |
|
| 2218 | + * If the object hasn't yet been saved, it should be whatever the model field's default was |
|
| 2219 | + * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
|
| 2220 | + * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
|
| 2221 | + * |
|
| 2222 | + * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
|
| 2223 | + * @throws ReflectionException |
|
| 2224 | + * @throws InvalidArgumentException |
|
| 2225 | + * @throws InvalidInterfaceException |
|
| 2226 | + * @throws InvalidDataTypeException |
|
| 2227 | + * @throws EE_Error |
|
| 2228 | + */ |
|
| 2229 | + public function ID() |
|
| 2230 | + { |
|
| 2231 | + $model = $this->get_model(); |
|
| 2232 | + // now that we know the name of the variable, use a variable variable to get its value and return its |
|
| 2233 | + if ($model->has_primary_key_field()) { |
|
| 2234 | + return $this->_fields[ $model->primary_key_name() ]; |
|
| 2235 | + } |
|
| 2236 | + return $model->get_index_primary_key_string($this->_fields); |
|
| 2237 | + } |
|
| 2238 | + |
|
| 2239 | + |
|
| 2240 | + /** |
|
| 2241 | + * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
|
| 2242 | + * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
|
| 2243 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
|
| 2244 | + * |
|
| 2245 | + * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
|
| 2246 | + * @param string $relationName eg 'Events','Question',etc. |
|
| 2247 | + * an attendee to a group, you also want to specify which role they |
|
| 2248 | + * will have in that group. So you would use this parameter to |
|
| 2249 | + * specify array('role-column-name'=>'role-id') |
|
| 2250 | + * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
|
| 2251 | + * allow you to further constrict the relation to being added. |
|
| 2252 | + * However, keep in mind that the columns (keys) given must match a |
|
| 2253 | + * column on the JOIN table and currently only the HABTM models |
|
| 2254 | + * accept these additional conditions. Also remember that if an |
|
| 2255 | + * exact match isn't found for these extra cols/val pairs, then a |
|
| 2256 | + * NEW row is created in the join table. |
|
| 2257 | + * @param null $cache_id |
|
| 2258 | + * @throws ReflectionException |
|
| 2259 | + * @throws InvalidArgumentException |
|
| 2260 | + * @throws InvalidInterfaceException |
|
| 2261 | + * @throws InvalidDataTypeException |
|
| 2262 | + * @throws EE_Error |
|
| 2263 | + * @return EE_Base_Class the object the relation was added to |
|
| 2264 | + */ |
|
| 2265 | + public function _add_relation_to( |
|
| 2266 | + $otherObjectModelObjectOrID, |
|
| 2267 | + $relationName, |
|
| 2268 | + $extra_join_model_fields_n_values = array(), |
|
| 2269 | + $cache_id = null |
|
| 2270 | + ) { |
|
| 2271 | + $model = $this->get_model(); |
|
| 2272 | + // if this thing exists in the DB, save the relation to the DB |
|
| 2273 | + if ($this->ID()) { |
|
| 2274 | + $otherObject = $model->add_relationship_to( |
|
| 2275 | + $this, |
|
| 2276 | + $otherObjectModelObjectOrID, |
|
| 2277 | + $relationName, |
|
| 2278 | + $extra_join_model_fields_n_values |
|
| 2279 | + ); |
|
| 2280 | + // clear cache so future get_many_related and get_first_related() return new results. |
|
| 2281 | + $this->clear_cache($relationName, $otherObject, true); |
|
| 2282 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2283 | + $otherObject->clear_cache($model->get_this_model_name(), $this); |
|
| 2284 | + } |
|
| 2285 | + } else { |
|
| 2286 | + // this thing doesn't exist in the DB, so just cache it |
|
| 2287 | + if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2288 | + throw new EE_Error( |
|
| 2289 | + sprintf( |
|
| 2290 | + esc_html__( |
|
| 2291 | + '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', |
|
| 2292 | + 'event_espresso' |
|
| 2293 | + ), |
|
| 2294 | + $otherObjectModelObjectOrID, |
|
| 2295 | + get_class($this) |
|
| 2296 | + ) |
|
| 2297 | + ); |
|
| 2298 | + } |
|
| 2299 | + $otherObject = $otherObjectModelObjectOrID; |
|
| 2300 | + $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id); |
|
| 2301 | + } |
|
| 2302 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2303 | + // fix the reciprocal relation too |
|
| 2304 | + if ($otherObject->ID()) { |
|
| 2305 | + // its saved so assumed relations exist in the DB, so we can just |
|
| 2306 | + // clear the cache so future queries use the updated info in the DB |
|
| 2307 | + $otherObject->clear_cache( |
|
| 2308 | + $model->get_this_model_name(), |
|
| 2309 | + null, |
|
| 2310 | + true |
|
| 2311 | + ); |
|
| 2312 | + } else { |
|
| 2313 | + // it's not saved, so it caches relations like this |
|
| 2314 | + $otherObject->cache($model->get_this_model_name(), $this); |
|
| 2315 | + } |
|
| 2316 | + } |
|
| 2317 | + return $otherObject; |
|
| 2318 | + } |
|
| 2319 | + |
|
| 2320 | + |
|
| 2321 | + /** |
|
| 2322 | + * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
|
| 2323 | + * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
|
| 2324 | + * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
|
| 2325 | + * from the cache |
|
| 2326 | + * |
|
| 2327 | + * @param mixed $otherObjectModelObjectOrID |
|
| 2328 | + * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
|
| 2329 | + * to the DB yet |
|
| 2330 | + * @param string $relationName |
|
| 2331 | + * @param array $where_query |
|
| 2332 | + * You can optionally include an array of key=>value pairs that allow you to further constrict the |
|
| 2333 | + * relation to being added. However, keep in mind that the columns (keys) given must match a column |
|
| 2334 | + * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
|
| 2335 | + * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
|
| 2336 | + * deleted. |
|
| 2337 | + * @return EE_Base_Class the relation was removed from |
|
| 2338 | + * @throws ReflectionException |
|
| 2339 | + * @throws InvalidArgumentException |
|
| 2340 | + * @throws InvalidInterfaceException |
|
| 2341 | + * @throws InvalidDataTypeException |
|
| 2342 | + * @throws EE_Error |
|
| 2343 | + */ |
|
| 2344 | + public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
|
| 2345 | + { |
|
| 2346 | + if ($this->ID()) { |
|
| 2347 | + // if this exists in the DB, save the relation change to the DB too |
|
| 2348 | + $otherObject = $this->get_model()->remove_relationship_to( |
|
| 2349 | + $this, |
|
| 2350 | + $otherObjectModelObjectOrID, |
|
| 2351 | + $relationName, |
|
| 2352 | + $where_query |
|
| 2353 | + ); |
|
| 2354 | + $this->clear_cache( |
|
| 2355 | + $relationName, |
|
| 2356 | + $otherObject |
|
| 2357 | + ); |
|
| 2358 | + } else { |
|
| 2359 | + // this doesn't exist in the DB, just remove it from the cache |
|
| 2360 | + $otherObject = $this->clear_cache( |
|
| 2361 | + $relationName, |
|
| 2362 | + $otherObjectModelObjectOrID |
|
| 2363 | + ); |
|
| 2364 | + } |
|
| 2365 | + if ($otherObject instanceof EE_Base_Class) { |
|
| 2366 | + $otherObject->clear_cache( |
|
| 2367 | + $this->get_model()->get_this_model_name(), |
|
| 2368 | + $this |
|
| 2369 | + ); |
|
| 2370 | + } |
|
| 2371 | + return $otherObject; |
|
| 2372 | + } |
|
| 2373 | + |
|
| 2374 | + |
|
| 2375 | + /** |
|
| 2376 | + * Removes ALL the related things for the $relationName. |
|
| 2377 | + * |
|
| 2378 | + * @param string $relationName |
|
| 2379 | + * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2380 | + * @return EE_Base_Class |
|
| 2381 | + * @throws ReflectionException |
|
| 2382 | + * @throws InvalidArgumentException |
|
| 2383 | + * @throws InvalidInterfaceException |
|
| 2384 | + * @throws InvalidDataTypeException |
|
| 2385 | + * @throws EE_Error |
|
| 2386 | + */ |
|
| 2387 | + public function _remove_relations($relationName, $where_query_params = array()) |
|
| 2388 | + { |
|
| 2389 | + if ($this->ID()) { |
|
| 2390 | + // if this exists in the DB, save the relation change to the DB too |
|
| 2391 | + $otherObjects = $this->get_model()->remove_relations( |
|
| 2392 | + $this, |
|
| 2393 | + $relationName, |
|
| 2394 | + $where_query_params |
|
| 2395 | + ); |
|
| 2396 | + $this->clear_cache( |
|
| 2397 | + $relationName, |
|
| 2398 | + null, |
|
| 2399 | + true |
|
| 2400 | + ); |
|
| 2401 | + } else { |
|
| 2402 | + // this doesn't exist in the DB, just remove it from the cache |
|
| 2403 | + $otherObjects = $this->clear_cache( |
|
| 2404 | + $relationName, |
|
| 2405 | + null, |
|
| 2406 | + true |
|
| 2407 | + ); |
|
| 2408 | + } |
|
| 2409 | + if (is_array($otherObjects)) { |
|
| 2410 | + foreach ($otherObjects as $otherObject) { |
|
| 2411 | + $otherObject->clear_cache( |
|
| 2412 | + $this->get_model()->get_this_model_name(), |
|
| 2413 | + $this |
|
| 2414 | + ); |
|
| 2415 | + } |
|
| 2416 | + } |
|
| 2417 | + return $otherObjects; |
|
| 2418 | + } |
|
| 2419 | + |
|
| 2420 | + |
|
| 2421 | + /** |
|
| 2422 | + * Gets all the related model objects of the specified type. Eg, if the current class if |
|
| 2423 | + * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
|
| 2424 | + * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
|
| 2425 | + * because we want to get even deleted items etc. |
|
| 2426 | + * |
|
| 2427 | + * @param string $relationName key in the model's _model_relations array |
|
| 2428 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
|
| 2429 | + * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
|
| 2430 | + * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
|
| 2431 | + * results if you want IDs |
|
| 2432 | + * @throws ReflectionException |
|
| 2433 | + * @throws InvalidArgumentException |
|
| 2434 | + * @throws InvalidInterfaceException |
|
| 2435 | + * @throws InvalidDataTypeException |
|
| 2436 | + * @throws EE_Error |
|
| 2437 | + */ |
|
| 2438 | + public function get_many_related($relationName, $query_params = array()) |
|
| 2439 | + { |
|
| 2440 | + if ($this->ID()) { |
|
| 2441 | + // this exists in the DB, so get the related things from either the cache or the DB |
|
| 2442 | + // if there are query parameters, forget about caching the related model objects. |
|
| 2443 | + if ($query_params) { |
|
| 2444 | + $related_model_objects = $this->get_model()->get_all_related( |
|
| 2445 | + $this, |
|
| 2446 | + $relationName, |
|
| 2447 | + $query_params |
|
| 2448 | + ); |
|
| 2449 | + } else { |
|
| 2450 | + // did we already cache the result of this query? |
|
| 2451 | + $cached_results = $this->get_all_from_cache($relationName); |
|
| 2452 | + if (! $cached_results) { |
|
| 2453 | + $related_model_objects = $this->get_model()->get_all_related( |
|
| 2454 | + $this, |
|
| 2455 | + $relationName, |
|
| 2456 | + $query_params |
|
| 2457 | + ); |
|
| 2458 | + // if no query parameters were passed, then we got all the related model objects |
|
| 2459 | + // for that relation. We can cache them then. |
|
| 2460 | + foreach ($related_model_objects as $related_model_object) { |
|
| 2461 | + $this->cache($relationName, $related_model_object); |
|
| 2462 | + } |
|
| 2463 | + } else { |
|
| 2464 | + $related_model_objects = $cached_results; |
|
| 2465 | + } |
|
| 2466 | + } |
|
| 2467 | + } else { |
|
| 2468 | + // this doesn't exist in the DB, so just get the related things from the cache |
|
| 2469 | + $related_model_objects = $this->get_all_from_cache($relationName); |
|
| 2470 | + } |
|
| 2471 | + return $related_model_objects; |
|
| 2472 | + } |
|
| 2473 | + |
|
| 2474 | + |
|
| 2475 | + /** |
|
| 2476 | + * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
|
| 2477 | + * unless otherwise specified in the $query_params |
|
| 2478 | + * |
|
| 2479 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2480 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2481 | + * @param string $field_to_count name of field to count by. By default, uses primary key |
|
| 2482 | + * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
|
| 2483 | + * that by the setting $distinct to TRUE; |
|
| 2484 | + * @return int |
|
| 2485 | + * @throws ReflectionException |
|
| 2486 | + * @throws InvalidArgumentException |
|
| 2487 | + * @throws InvalidInterfaceException |
|
| 2488 | + * @throws InvalidDataTypeException |
|
| 2489 | + * @throws EE_Error |
|
| 2490 | + */ |
|
| 2491 | + public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
|
| 2492 | + { |
|
| 2493 | + return $this->get_model()->count_related( |
|
| 2494 | + $this, |
|
| 2495 | + $relation_name, |
|
| 2496 | + $query_params, |
|
| 2497 | + $field_to_count, |
|
| 2498 | + $distinct |
|
| 2499 | + ); |
|
| 2500 | + } |
|
| 2501 | + |
|
| 2502 | + |
|
| 2503 | + /** |
|
| 2504 | + * Instead of getting the related model objects, simply sums up the values of the specified field. |
|
| 2505 | + * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
|
| 2506 | + * |
|
| 2507 | + * @param string $relation_name model_name like 'Event', or 'Registration' |
|
| 2508 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2509 | + * @param string $field_to_sum name of field to count by. |
|
| 2510 | + * By default, uses primary key |
|
| 2511 | + * (which doesn't make much sense, so you should probably change it) |
|
| 2512 | + * @return int |
|
| 2513 | + * @throws ReflectionException |
|
| 2514 | + * @throws InvalidArgumentException |
|
| 2515 | + * @throws InvalidInterfaceException |
|
| 2516 | + * @throws InvalidDataTypeException |
|
| 2517 | + * @throws EE_Error |
|
| 2518 | + */ |
|
| 2519 | + public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
|
| 2520 | + { |
|
| 2521 | + return $this->get_model()->sum_related( |
|
| 2522 | + $this, |
|
| 2523 | + $relation_name, |
|
| 2524 | + $query_params, |
|
| 2525 | + $field_to_sum |
|
| 2526 | + ); |
|
| 2527 | + } |
|
| 2528 | + |
|
| 2529 | + |
|
| 2530 | + /** |
|
| 2531 | + * Gets the first (ie, one) related model object of the specified type. |
|
| 2532 | + * |
|
| 2533 | + * @param string $relationName key in the model's _model_relations array |
|
| 2534 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2535 | + * @return EE_Base_Class (not an array, a single object) |
|
| 2536 | + * @throws ReflectionException |
|
| 2537 | + * @throws InvalidArgumentException |
|
| 2538 | + * @throws InvalidInterfaceException |
|
| 2539 | + * @throws InvalidDataTypeException |
|
| 2540 | + * @throws EE_Error |
|
| 2541 | + */ |
|
| 2542 | + public function get_first_related($relationName, $query_params = array()) |
|
| 2543 | + { |
|
| 2544 | + $model = $this->get_model(); |
|
| 2545 | + if ($this->ID()) {// this exists in the DB, get from the cache OR the DB |
|
| 2546 | + // if they've provided some query parameters, don't bother trying to cache the result |
|
| 2547 | + // also make sure we're not caching the result of get_first_related |
|
| 2548 | + // on a relation which should have an array of objects (because the cache might have an array of objects) |
|
| 2549 | + if ($query_params |
|
| 2550 | + || ! $model->related_settings_for($relationName) |
|
| 2551 | + instanceof |
|
| 2552 | + EE_Belongs_To_Relation |
|
| 2553 | + ) { |
|
| 2554 | + $related_model_object = $model->get_first_related( |
|
| 2555 | + $this, |
|
| 2556 | + $relationName, |
|
| 2557 | + $query_params |
|
| 2558 | + ); |
|
| 2559 | + } else { |
|
| 2560 | + // first, check if we've already cached the result of this query |
|
| 2561 | + $cached_result = $this->get_one_from_cache($relationName); |
|
| 2562 | + if (! $cached_result) { |
|
| 2563 | + $related_model_object = $model->get_first_related( |
|
| 2564 | + $this, |
|
| 2565 | + $relationName, |
|
| 2566 | + $query_params |
|
| 2567 | + ); |
|
| 2568 | + $this->cache($relationName, $related_model_object); |
|
| 2569 | + } else { |
|
| 2570 | + $related_model_object = $cached_result; |
|
| 2571 | + } |
|
| 2572 | + } |
|
| 2573 | + } else { |
|
| 2574 | + $related_model_object = null; |
|
| 2575 | + // this doesn't exist in the Db, |
|
| 2576 | + // but maybe the relation is of type belongs to, and so the related thing might |
|
| 2577 | + if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) { |
|
| 2578 | + $related_model_object = $model->get_first_related( |
|
| 2579 | + $this, |
|
| 2580 | + $relationName, |
|
| 2581 | + $query_params |
|
| 2582 | + ); |
|
| 2583 | + } |
|
| 2584 | + // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
|
| 2585 | + // just get what's cached on this object |
|
| 2586 | + if (! $related_model_object) { |
|
| 2587 | + $related_model_object = $this->get_one_from_cache($relationName); |
|
| 2588 | + } |
|
| 2589 | + } |
|
| 2590 | + return $related_model_object; |
|
| 2591 | + } |
|
| 2592 | + |
|
| 2593 | + |
|
| 2594 | + /** |
|
| 2595 | + * Does a delete on all related objects of type $relationName and removes |
|
| 2596 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2597 | + * of blocking related model objects) does nothing. If the related model objects are |
|
| 2598 | + * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
|
| 2599 | + * If this model object doesn't exist yet in the DB, just removes its related things |
|
| 2600 | + * |
|
| 2601 | + * @param string $relationName |
|
| 2602 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2603 | + * @return int how many deleted |
|
| 2604 | + * @throws ReflectionException |
|
| 2605 | + * @throws InvalidArgumentException |
|
| 2606 | + * @throws InvalidInterfaceException |
|
| 2607 | + * @throws InvalidDataTypeException |
|
| 2608 | + * @throws EE_Error |
|
| 2609 | + */ |
|
| 2610 | + public function delete_related($relationName, $query_params = array()) |
|
| 2611 | + { |
|
| 2612 | + if ($this->ID()) { |
|
| 2613 | + $count = $this->get_model()->delete_related( |
|
| 2614 | + $this, |
|
| 2615 | + $relationName, |
|
| 2616 | + $query_params |
|
| 2617 | + ); |
|
| 2618 | + } else { |
|
| 2619 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2620 | + $this->clear_cache($relationName, null, true); |
|
| 2621 | + } |
|
| 2622 | + return $count; |
|
| 2623 | + } |
|
| 2624 | + |
|
| 2625 | + |
|
| 2626 | + /** |
|
| 2627 | + * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
|
| 2628 | + * the current model object's relation to them. If they can't be deleted (because |
|
| 2629 | + * of blocking related model objects) just does a soft delete on it instead, if possible. |
|
| 2630 | + * If the related thing isn't a soft-deletable model object, this function is identical |
|
| 2631 | + * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
|
| 2632 | + * |
|
| 2633 | + * @param string $relationName |
|
| 2634 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
| 2635 | + * @return int how many deleted (including those soft deleted) |
|
| 2636 | + * @throws ReflectionException |
|
| 2637 | + * @throws InvalidArgumentException |
|
| 2638 | + * @throws InvalidInterfaceException |
|
| 2639 | + * @throws InvalidDataTypeException |
|
| 2640 | + * @throws EE_Error |
|
| 2641 | + */ |
|
| 2642 | + public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2643 | + { |
|
| 2644 | + if ($this->ID()) { |
|
| 2645 | + $count = $this->get_model()->delete_related_permanently( |
|
| 2646 | + $this, |
|
| 2647 | + $relationName, |
|
| 2648 | + $query_params |
|
| 2649 | + ); |
|
| 2650 | + } else { |
|
| 2651 | + $count = count($this->get_all_from_cache($relationName)); |
|
| 2652 | + } |
|
| 2653 | + $this->clear_cache($relationName, null, true); |
|
| 2654 | + return $count; |
|
| 2655 | + } |
|
| 2656 | + |
|
| 2657 | + |
|
| 2658 | + /** |
|
| 2659 | + * is_set |
|
| 2660 | + * Just a simple utility function children can use for checking if property exists |
|
| 2661 | + * |
|
| 2662 | + * @access public |
|
| 2663 | + * @param string $field_name property to check |
|
| 2664 | + * @return bool TRUE if existing,FALSE if not. |
|
| 2665 | + */ |
|
| 2666 | + public function is_set($field_name) |
|
| 2667 | + { |
|
| 2668 | + return isset($this->_fields[ $field_name ]); |
|
| 2669 | + } |
|
| 2670 | + |
|
| 2671 | + |
|
| 2672 | + /** |
|
| 2673 | + * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
|
| 2674 | + * EE_Error exception if they don't |
|
| 2675 | + * |
|
| 2676 | + * @param mixed (string|array) $properties properties to check |
|
| 2677 | + * @throws EE_Error |
|
| 2678 | + * @return bool TRUE if existing, throw EE_Error if not. |
|
| 2679 | + */ |
|
| 2680 | + protected function _property_exists($properties) |
|
| 2681 | + { |
|
| 2682 | + foreach ((array) $properties as $property_name) { |
|
| 2683 | + // first make sure this property exists |
|
| 2684 | + if (! $this->_fields[ $property_name ]) { |
|
| 2685 | + throw new EE_Error( |
|
| 2686 | + sprintf( |
|
| 2687 | + esc_html__( |
|
| 2688 | + 'Trying to retrieve a non-existent property (%s). Double check the spelling please', |
|
| 2689 | + 'event_espresso' |
|
| 2690 | + ), |
|
| 2691 | + $property_name |
|
| 2692 | + ) |
|
| 2693 | + ); |
|
| 2694 | + } |
|
| 2695 | + } |
|
| 2696 | + return true; |
|
| 2697 | + } |
|
| 2698 | + |
|
| 2699 | + |
|
| 2700 | + /** |
|
| 2701 | + * This simply returns an array of model fields for this object |
|
| 2702 | + * |
|
| 2703 | + * @return array |
|
| 2704 | + * @throws ReflectionException |
|
| 2705 | + * @throws InvalidArgumentException |
|
| 2706 | + * @throws InvalidInterfaceException |
|
| 2707 | + * @throws InvalidDataTypeException |
|
| 2708 | + * @throws EE_Error |
|
| 2709 | + */ |
|
| 2710 | + public function model_field_array() |
|
| 2711 | + { |
|
| 2712 | + $fields = $this->get_model()->field_settings(false); |
|
| 2713 | + $properties = array(); |
|
| 2714 | + // remove prepended underscore |
|
| 2715 | + foreach ($fields as $field_name => $settings) { |
|
| 2716 | + $properties[ $field_name ] = $this->get($field_name); |
|
| 2717 | + } |
|
| 2718 | + return $properties; |
|
| 2719 | + } |
|
| 2720 | + |
|
| 2721 | + |
|
| 2722 | + /** |
|
| 2723 | + * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
|
| 2724 | + * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
|
| 2725 | + * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
|
| 2726 | + * Instead of requiring a plugin to extend the EE_Base_Class |
|
| 2727 | + * (which works fine is there's only 1 plugin, but when will that happen?) |
|
| 2728 | + * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
|
| 2729 | + * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
|
| 2730 | + * and accepts 2 arguments: the object on which the function was called, |
|
| 2731 | + * and an array of the original arguments passed to the function. |
|
| 2732 | + * Whatever their callback function returns will be returned by this function. |
|
| 2733 | + * Example: in functions.php (or in a plugin): |
|
| 2734 | + * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
|
| 2735 | + * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
|
| 2736 | + * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
|
| 2737 | + * return $previousReturnValue.$returnString; |
|
| 2738 | + * } |
|
| 2739 | + * require('EE_Answer.class.php'); |
|
| 2740 | + * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
|
| 2741 | + * echo $answer->my_callback('monkeys',100); |
|
| 2742 | + * //will output "you called my_callback! and passed args:monkeys,100" |
|
| 2743 | + * |
|
| 2744 | + * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
|
| 2745 | + * @param array $args array of original arguments passed to the function |
|
| 2746 | + * @throws EE_Error |
|
| 2747 | + * @return mixed whatever the plugin which calls add_filter decides |
|
| 2748 | + */ |
|
| 2749 | + public function __call($methodName, $args) |
|
| 2750 | + { |
|
| 2751 | + $className = get_class($this); |
|
| 2752 | + $tagName = "FHEE__{$className}__{$methodName}"; |
|
| 2753 | + if (! has_filter($tagName)) { |
|
| 2754 | + throw new EE_Error( |
|
| 2755 | + sprintf( |
|
| 2756 | + esc_html__( |
|
| 2757 | + "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;}", |
|
| 2758 | + 'event_espresso' |
|
| 2759 | + ), |
|
| 2760 | + $methodName, |
|
| 2761 | + $className, |
|
| 2762 | + $tagName |
|
| 2763 | + ) |
|
| 2764 | + ); |
|
| 2765 | + } |
|
| 2766 | + return apply_filters($tagName, null, $this, $args); |
|
| 2767 | + } |
|
| 2768 | + |
|
| 2769 | + |
|
| 2770 | + /** |
|
| 2771 | + * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
|
| 2772 | + * A $previous_value can be specified in case there are many meta rows with the same key |
|
| 2773 | + * |
|
| 2774 | + * @param string $meta_key |
|
| 2775 | + * @param mixed $meta_value |
|
| 2776 | + * @param mixed $previous_value |
|
| 2777 | + * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
|
| 2778 | + * NOTE: if the values haven't changed, returns 0 |
|
| 2779 | + * @throws InvalidArgumentException |
|
| 2780 | + * @throws InvalidInterfaceException |
|
| 2781 | + * @throws InvalidDataTypeException |
|
| 2782 | + * @throws EE_Error |
|
| 2783 | + * @throws ReflectionException |
|
| 2784 | + */ |
|
| 2785 | + public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
|
| 2786 | + { |
|
| 2787 | + $query_params = array( |
|
| 2788 | + array( |
|
| 2789 | + 'EXM_key' => $meta_key, |
|
| 2790 | + 'OBJ_ID' => $this->ID(), |
|
| 2791 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2792 | + ), |
|
| 2793 | + ); |
|
| 2794 | + if ($previous_value !== null) { |
|
| 2795 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2796 | + } |
|
| 2797 | + $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
|
| 2798 | + if (! $existing_rows_like_that) { |
|
| 2799 | + return $this->add_extra_meta($meta_key, $meta_value); |
|
| 2800 | + } |
|
| 2801 | + foreach ($existing_rows_like_that as $existing_row) { |
|
| 2802 | + $existing_row->save(array('EXM_value' => $meta_value)); |
|
| 2803 | + } |
|
| 2804 | + return count($existing_rows_like_that); |
|
| 2805 | + } |
|
| 2806 | + |
|
| 2807 | + |
|
| 2808 | + /** |
|
| 2809 | + * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
|
| 2810 | + * no other extra meta for this model object have the same key. Returns TRUE if the |
|
| 2811 | + * extra meta row was entered, false if not |
|
| 2812 | + * |
|
| 2813 | + * @param string $meta_key |
|
| 2814 | + * @param mixed $meta_value |
|
| 2815 | + * @param boolean $unique |
|
| 2816 | + * @return boolean |
|
| 2817 | + * @throws InvalidArgumentException |
|
| 2818 | + * @throws InvalidInterfaceException |
|
| 2819 | + * @throws InvalidDataTypeException |
|
| 2820 | + * @throws EE_Error |
|
| 2821 | + * @throws ReflectionException |
|
| 2822 | + * @throws ReflectionException |
|
| 2823 | + */ |
|
| 2824 | + public function add_extra_meta($meta_key, $meta_value, $unique = false) |
|
| 2825 | + { |
|
| 2826 | + if ($unique) { |
|
| 2827 | + $existing_extra_meta = EEM_Extra_Meta::instance()->get_one( |
|
| 2828 | + array( |
|
| 2829 | + array( |
|
| 2830 | + 'EXM_key' => $meta_key, |
|
| 2831 | + 'OBJ_ID' => $this->ID(), |
|
| 2832 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2833 | + ), |
|
| 2834 | + ) |
|
| 2835 | + ); |
|
| 2836 | + if ($existing_extra_meta) { |
|
| 2837 | + return false; |
|
| 2838 | + } |
|
| 2839 | + } |
|
| 2840 | + $new_extra_meta = EE_Extra_Meta::new_instance( |
|
| 2841 | + array( |
|
| 2842 | + 'EXM_key' => $meta_key, |
|
| 2843 | + 'EXM_value' => $meta_value, |
|
| 2844 | + 'OBJ_ID' => $this->ID(), |
|
| 2845 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2846 | + ) |
|
| 2847 | + ); |
|
| 2848 | + $new_extra_meta->save(); |
|
| 2849 | + return true; |
|
| 2850 | + } |
|
| 2851 | + |
|
| 2852 | + |
|
| 2853 | + /** |
|
| 2854 | + * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
|
| 2855 | + * is specified, only deletes extra meta records with that value. |
|
| 2856 | + * |
|
| 2857 | + * @param string $meta_key |
|
| 2858 | + * @param mixed $meta_value |
|
| 2859 | + * @return int number of extra meta rows deleted |
|
| 2860 | + * @throws InvalidArgumentException |
|
| 2861 | + * @throws InvalidInterfaceException |
|
| 2862 | + * @throws InvalidDataTypeException |
|
| 2863 | + * @throws EE_Error |
|
| 2864 | + * @throws ReflectionException |
|
| 2865 | + */ |
|
| 2866 | + public function delete_extra_meta($meta_key, $meta_value = null) |
|
| 2867 | + { |
|
| 2868 | + $query_params = array( |
|
| 2869 | + array( |
|
| 2870 | + 'EXM_key' => $meta_key, |
|
| 2871 | + 'OBJ_ID' => $this->ID(), |
|
| 2872 | + 'EXM_type' => $this->get_model()->get_this_model_name(), |
|
| 2873 | + ), |
|
| 2874 | + ); |
|
| 2875 | + if ($meta_value !== null) { |
|
| 2876 | + $query_params[0]['EXM_value'] = $meta_value; |
|
| 2877 | + } |
|
| 2878 | + return EEM_Extra_Meta::instance()->delete($query_params); |
|
| 2879 | + } |
|
| 2880 | + |
|
| 2881 | + |
|
| 2882 | + /** |
|
| 2883 | + * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
|
| 2884 | + * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
|
| 2885 | + * You can specify $default is case you haven't found the extra meta |
|
| 2886 | + * |
|
| 2887 | + * @param string $meta_key |
|
| 2888 | + * @param boolean $single |
|
| 2889 | + * @param mixed $default if we don't find anything, what should we return? |
|
| 2890 | + * @return mixed single value if $single; array if ! $single |
|
| 2891 | + * @throws ReflectionException |
|
| 2892 | + * @throws InvalidArgumentException |
|
| 2893 | + * @throws InvalidInterfaceException |
|
| 2894 | + * @throws InvalidDataTypeException |
|
| 2895 | + * @throws EE_Error |
|
| 2896 | + */ |
|
| 2897 | + public function get_extra_meta($meta_key, $single = false, $default = null) |
|
| 2898 | + { |
|
| 2899 | + if ($single) { |
|
| 2900 | + $result = $this->get_first_related( |
|
| 2901 | + 'Extra_Meta', |
|
| 2902 | + array(array('EXM_key' => $meta_key)) |
|
| 2903 | + ); |
|
| 2904 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2905 | + return $result->value(); |
|
| 2906 | + } |
|
| 2907 | + } else { |
|
| 2908 | + $results = $this->get_many_related( |
|
| 2909 | + 'Extra_Meta', |
|
| 2910 | + array(array('EXM_key' => $meta_key)) |
|
| 2911 | + ); |
|
| 2912 | + if ($results) { |
|
| 2913 | + $values = array(); |
|
| 2914 | + foreach ($results as $result) { |
|
| 2915 | + if ($result instanceof EE_Extra_Meta) { |
|
| 2916 | + $values[ $result->ID() ] = $result->value(); |
|
| 2917 | + } |
|
| 2918 | + } |
|
| 2919 | + return $values; |
|
| 2920 | + } |
|
| 2921 | + } |
|
| 2922 | + // if nothing discovered yet return default. |
|
| 2923 | + return apply_filters( |
|
| 2924 | + 'FHEE__EE_Base_Class__get_extra_meta__default_value', |
|
| 2925 | + $default, |
|
| 2926 | + $meta_key, |
|
| 2927 | + $single, |
|
| 2928 | + $this |
|
| 2929 | + ); |
|
| 2930 | + } |
|
| 2931 | + |
|
| 2932 | + |
|
| 2933 | + /** |
|
| 2934 | + * Returns a simple array of all the extra meta associated with this model object. |
|
| 2935 | + * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
|
| 2936 | + * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
|
| 2937 | + * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
|
| 2938 | + * If $one_of_each_key is false, it will return an array with the top-level keys being |
|
| 2939 | + * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
|
| 2940 | + * finally the extra meta's value as each sub-value. (eg |
|
| 2941 | + * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
|
| 2942 | + * |
|
| 2943 | + * @param boolean $one_of_each_key |
|
| 2944 | + * @return array |
|
| 2945 | + * @throws ReflectionException |
|
| 2946 | + * @throws InvalidArgumentException |
|
| 2947 | + * @throws InvalidInterfaceException |
|
| 2948 | + * @throws InvalidDataTypeException |
|
| 2949 | + * @throws EE_Error |
|
| 2950 | + */ |
|
| 2951 | + public function all_extra_meta_array($one_of_each_key = true) |
|
| 2952 | + { |
|
| 2953 | + $return_array = array(); |
|
| 2954 | + if ($one_of_each_key) { |
|
| 2955 | + $extra_meta_objs = $this->get_many_related( |
|
| 2956 | + 'Extra_Meta', |
|
| 2957 | + array('group_by' => 'EXM_key') |
|
| 2958 | + ); |
|
| 2959 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2960 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2961 | + $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
| 2962 | + } |
|
| 2963 | + } |
|
| 2964 | + } else { |
|
| 2965 | + $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
|
| 2966 | + foreach ($extra_meta_objs as $extra_meta_obj) { |
|
| 2967 | + if ($extra_meta_obj instanceof EE_Extra_Meta) { |
|
| 2968 | + if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
| 2969 | + $return_array[ $extra_meta_obj->key() ] = array(); |
|
| 2970 | + } |
|
| 2971 | + $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
| 2972 | + } |
|
| 2973 | + } |
|
| 2974 | + } |
|
| 2975 | + return $return_array; |
|
| 2976 | + } |
|
| 2977 | + |
|
| 2978 | + |
|
| 2979 | + /** |
|
| 2980 | + * Gets a pretty nice displayable nice for this model object. Often overridden |
|
| 2981 | + * |
|
| 2982 | + * @return string |
|
| 2983 | + * @throws ReflectionException |
|
| 2984 | + * @throws InvalidArgumentException |
|
| 2985 | + * @throws InvalidInterfaceException |
|
| 2986 | + * @throws InvalidDataTypeException |
|
| 2987 | + * @throws EE_Error |
|
| 2988 | + */ |
|
| 2989 | + public function name() |
|
| 2990 | + { |
|
| 2991 | + // find a field that's not a text field |
|
| 2992 | + $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base'); |
|
| 2993 | + if ($field_we_can_use) { |
|
| 2994 | + return $this->get($field_we_can_use->get_name()); |
|
| 2995 | + } |
|
| 2996 | + $first_few_properties = $this->model_field_array(); |
|
| 2997 | + $first_few_properties = array_slice($first_few_properties, 0, 3); |
|
| 2998 | + $name_parts = array(); |
|
| 2999 | + foreach ($first_few_properties as $name => $value) { |
|
| 3000 | + $name_parts[] = "$name:$value"; |
|
| 3001 | + } |
|
| 3002 | + return implode(',', $name_parts); |
|
| 3003 | + } |
|
| 3004 | + |
|
| 3005 | + |
|
| 3006 | + /** |
|
| 3007 | + * in_entity_map |
|
| 3008 | + * Checks if this model object has been proven to already be in the entity map |
|
| 3009 | + * |
|
| 3010 | + * @return boolean |
|
| 3011 | + * @throws ReflectionException |
|
| 3012 | + * @throws InvalidArgumentException |
|
| 3013 | + * @throws InvalidInterfaceException |
|
| 3014 | + * @throws InvalidDataTypeException |
|
| 3015 | + * @throws EE_Error |
|
| 3016 | + */ |
|
| 3017 | + public function in_entity_map() |
|
| 3018 | + { |
|
| 3019 | + // well, if we looked, did we find it in the entity map? |
|
| 3020 | + return $this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this; |
|
| 3021 | + } |
|
| 3022 | + |
|
| 3023 | + |
|
| 3024 | + /** |
|
| 3025 | + * refresh_from_db |
|
| 3026 | + * Makes sure the fields and values on this model object are in-sync with what's in the database. |
|
| 3027 | + * |
|
| 3028 | + * @throws ReflectionException |
|
| 3029 | + * @throws InvalidArgumentException |
|
| 3030 | + * @throws InvalidInterfaceException |
|
| 3031 | + * @throws InvalidDataTypeException |
|
| 3032 | + * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
|
| 3033 | + * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
|
| 3034 | + */ |
|
| 3035 | + public function refresh_from_db() |
|
| 3036 | + { |
|
| 3037 | + if ($this->ID() && $this->in_entity_map()) { |
|
| 3038 | + $this->get_model()->refresh_entity_map_from_db($this->ID()); |
|
| 3039 | + } else { |
|
| 3040 | + // if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database) |
|
| 3041 | + // if it has an ID but it's not in the map, and you're asking me to refresh it |
|
| 3042 | + // that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's |
|
| 3043 | + // absolutely nothing in it for this ID |
|
| 3044 | + if (WP_DEBUG) { |
|
| 3045 | + throw new EE_Error( |
|
| 3046 | + sprintf( |
|
| 3047 | + esc_html__( |
|
| 3048 | + '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.', |
|
| 3049 | + 'event_espresso' |
|
| 3050 | + ), |
|
| 3051 | + $this->ID(), |
|
| 3052 | + get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 3053 | + get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 3054 | + ) |
|
| 3055 | + ); |
|
| 3056 | + } |
|
| 3057 | + } |
|
| 3058 | + } |
|
| 3059 | + |
|
| 3060 | + |
|
| 3061 | + /** |
|
| 3062 | + * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
|
| 3063 | + * |
|
| 3064 | + * @since 4.9.80.p |
|
| 3065 | + * @param EE_Model_Field_Base[] $fields |
|
| 3066 | + * @param string $new_value_sql |
|
| 3067 | + * example: 'column_name=123', |
|
| 3068 | + * or 'column_name=column_name+1', |
|
| 3069 | + * or 'column_name= CASE |
|
| 3070 | + * WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
|
| 3071 | + * THEN `column_name` + 5 |
|
| 3072 | + * ELSE `column_name` |
|
| 3073 | + * END' |
|
| 3074 | + * Also updates $field on this model object with the latest value from the database. |
|
| 3075 | + * @return bool |
|
| 3076 | + * @throws EE_Error |
|
| 3077 | + * @throws InvalidArgumentException |
|
| 3078 | + * @throws InvalidDataTypeException |
|
| 3079 | + * @throws InvalidInterfaceException |
|
| 3080 | + * @throws ReflectionException |
|
| 3081 | + */ |
|
| 3082 | + protected function updateFieldsInDB($fields, $new_value_sql) |
|
| 3083 | + { |
|
| 3084 | + // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
|
| 3085 | + // if it wasn't even there to start off. |
|
| 3086 | + if (! $this->ID()) { |
|
| 3087 | + $this->save(); |
|
| 3088 | + } |
|
| 3089 | + global $wpdb; |
|
| 3090 | + if (empty($fields)) { |
|
| 3091 | + throw new InvalidArgumentException( |
|
| 3092 | + esc_html__( |
|
| 3093 | + 'EE_Base_Class::updateFieldsInDB was passed an empty array of fields.', |
|
| 3094 | + 'event_espresso' |
|
| 3095 | + ) |
|
| 3096 | + ); |
|
| 3097 | + } |
|
| 3098 | + $first_field = reset($fields); |
|
| 3099 | + $table_alias = $first_field->get_table_alias(); |
|
| 3100 | + foreach ($fields as $field) { |
|
| 3101 | + if ($table_alias !== $field->get_table_alias()) { |
|
| 3102 | + throw new InvalidArgumentException( |
|
| 3103 | + sprintf( |
|
| 3104 | + esc_html__( |
|
| 3105 | + // @codingStandardsIgnoreStart |
|
| 3106 | + 'EE_Base_Class::updateFieldsInDB was passed fields for different tables ("%1$s" and "%2$s"), which is not supported. Instead, please call the method multiple times.', |
|
| 3107 | + // @codingStandardsIgnoreEnd |
|
| 3108 | + 'event_espresso' |
|
| 3109 | + ), |
|
| 3110 | + $table_alias, |
|
| 3111 | + $field->get_table_alias() |
|
| 3112 | + ) |
|
| 3113 | + ); |
|
| 3114 | + } |
|
| 3115 | + } |
|
| 3116 | + // Ok the fields are now known to all be for the same table. Proceed with creating the SQL to update it. |
|
| 3117 | + $table_obj = $this->get_model()->get_table_obj_by_alias($table_alias); |
|
| 3118 | + $table_pk_value = $this->ID(); |
|
| 3119 | + $table_name = $table_obj->get_table_name(); |
|
| 3120 | + if ($table_obj instanceof EE_Secondary_Table) { |
|
| 3121 | + $table_pk_field_name = $table_obj->get_fk_on_table(); |
|
| 3122 | + } else { |
|
| 3123 | + $table_pk_field_name = $table_obj->get_pk_column(); |
|
| 3124 | + } |
|
| 3125 | + |
|
| 3126 | + $query = |
|
| 3127 | + "UPDATE `{$table_name}` |
|
| 3128 | 3128 | SET " |
| 3129 | - . $new_value_sql |
|
| 3130 | - . $wpdb->prepare( |
|
| 3131 | - " |
|
| 3129 | + . $new_value_sql |
|
| 3130 | + . $wpdb->prepare( |
|
| 3131 | + " |
|
| 3132 | 3132 | WHERE `{$table_pk_field_name}` = %d;", |
| 3133 | - $table_pk_value |
|
| 3134 | - ); |
|
| 3135 | - $result = $wpdb->query($query); |
|
| 3136 | - foreach ($fields as $field) { |
|
| 3137 | - // If it was successful, we'd like to know the new value. |
|
| 3138 | - // If it failed, we'd also like to know the new value. |
|
| 3139 | - $new_value = $this->get_model()->get_var( |
|
| 3140 | - $this->get_model()->alter_query_params_to_restrict_by_ID( |
|
| 3141 | - $this->get_model()->get_index_primary_key_string( |
|
| 3142 | - $this->model_field_array() |
|
| 3143 | - ), |
|
| 3144 | - array( |
|
| 3145 | - 'default_where_conditions' => 'minimum', |
|
| 3146 | - ) |
|
| 3147 | - ), |
|
| 3148 | - $field->get_name() |
|
| 3149 | - ); |
|
| 3150 | - $this->set_from_db( |
|
| 3151 | - $field->get_name(), |
|
| 3152 | - $new_value |
|
| 3153 | - ); |
|
| 3154 | - } |
|
| 3155 | - return (bool) $result; |
|
| 3156 | - } |
|
| 3157 | - |
|
| 3158 | - |
|
| 3159 | - /** |
|
| 3160 | - * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
|
| 3161 | - * Does not allow negative values, however. |
|
| 3162 | - * |
|
| 3163 | - * @since 4.9.80.p |
|
| 3164 | - * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
|
| 3165 | - * (positive or negative). One important gotcha: all these values must be |
|
| 3166 | - * on the same table (eg don't pass in one field for the posts table and |
|
| 3167 | - * another for the event meta table.) |
|
| 3168 | - * @return bool |
|
| 3169 | - * @throws EE_Error |
|
| 3170 | - * @throws InvalidArgumentException |
|
| 3171 | - * @throws InvalidDataTypeException |
|
| 3172 | - * @throws InvalidInterfaceException |
|
| 3173 | - * @throws ReflectionException |
|
| 3174 | - */ |
|
| 3175 | - public function adjustNumericFieldsInDb(array $fields_n_quantities) |
|
| 3176 | - { |
|
| 3177 | - global $wpdb; |
|
| 3178 | - if (empty($fields_n_quantities)) { |
|
| 3179 | - // No fields to update? Well sure, we updated them to that value just fine. |
|
| 3180 | - return true; |
|
| 3181 | - } |
|
| 3182 | - $fields = []; |
|
| 3183 | - $set_sql_statements = []; |
|
| 3184 | - foreach ($fields_n_quantities as $field_name => $quantity) { |
|
| 3185 | - $field = $this->get_model()->field_settings_for($field_name, true); |
|
| 3186 | - $fields[] = $field; |
|
| 3187 | - $column_name = $field->get_table_column(); |
|
| 3188 | - |
|
| 3189 | - $abs_qty = absint($quantity); |
|
| 3190 | - if ($quantity > 0) { |
|
| 3191 | - // don't let the value be negative as often these fields are unsigned |
|
| 3192 | - $set_sql_statements[] = $wpdb->prepare( |
|
| 3193 | - "`{$column_name}` = `{$column_name}` + %d", |
|
| 3194 | - $abs_qty |
|
| 3195 | - ); |
|
| 3196 | - } else { |
|
| 3197 | - $set_sql_statements[] = $wpdb->prepare( |
|
| 3198 | - "`{$column_name}` = CASE |
|
| 3133 | + $table_pk_value |
|
| 3134 | + ); |
|
| 3135 | + $result = $wpdb->query($query); |
|
| 3136 | + foreach ($fields as $field) { |
|
| 3137 | + // If it was successful, we'd like to know the new value. |
|
| 3138 | + // If it failed, we'd also like to know the new value. |
|
| 3139 | + $new_value = $this->get_model()->get_var( |
|
| 3140 | + $this->get_model()->alter_query_params_to_restrict_by_ID( |
|
| 3141 | + $this->get_model()->get_index_primary_key_string( |
|
| 3142 | + $this->model_field_array() |
|
| 3143 | + ), |
|
| 3144 | + array( |
|
| 3145 | + 'default_where_conditions' => 'minimum', |
|
| 3146 | + ) |
|
| 3147 | + ), |
|
| 3148 | + $field->get_name() |
|
| 3149 | + ); |
|
| 3150 | + $this->set_from_db( |
|
| 3151 | + $field->get_name(), |
|
| 3152 | + $new_value |
|
| 3153 | + ); |
|
| 3154 | + } |
|
| 3155 | + return (bool) $result; |
|
| 3156 | + } |
|
| 3157 | + |
|
| 3158 | + |
|
| 3159 | + /** |
|
| 3160 | + * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
|
| 3161 | + * Does not allow negative values, however. |
|
| 3162 | + * |
|
| 3163 | + * @since 4.9.80.p |
|
| 3164 | + * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
|
| 3165 | + * (positive or negative). One important gotcha: all these values must be |
|
| 3166 | + * on the same table (eg don't pass in one field for the posts table and |
|
| 3167 | + * another for the event meta table.) |
|
| 3168 | + * @return bool |
|
| 3169 | + * @throws EE_Error |
|
| 3170 | + * @throws InvalidArgumentException |
|
| 3171 | + * @throws InvalidDataTypeException |
|
| 3172 | + * @throws InvalidInterfaceException |
|
| 3173 | + * @throws ReflectionException |
|
| 3174 | + */ |
|
| 3175 | + public function adjustNumericFieldsInDb(array $fields_n_quantities) |
|
| 3176 | + { |
|
| 3177 | + global $wpdb; |
|
| 3178 | + if (empty($fields_n_quantities)) { |
|
| 3179 | + // No fields to update? Well sure, we updated them to that value just fine. |
|
| 3180 | + return true; |
|
| 3181 | + } |
|
| 3182 | + $fields = []; |
|
| 3183 | + $set_sql_statements = []; |
|
| 3184 | + foreach ($fields_n_quantities as $field_name => $quantity) { |
|
| 3185 | + $field = $this->get_model()->field_settings_for($field_name, true); |
|
| 3186 | + $fields[] = $field; |
|
| 3187 | + $column_name = $field->get_table_column(); |
|
| 3188 | + |
|
| 3189 | + $abs_qty = absint($quantity); |
|
| 3190 | + if ($quantity > 0) { |
|
| 3191 | + // don't let the value be negative as often these fields are unsigned |
|
| 3192 | + $set_sql_statements[] = $wpdb->prepare( |
|
| 3193 | + "`{$column_name}` = `{$column_name}` + %d", |
|
| 3194 | + $abs_qty |
|
| 3195 | + ); |
|
| 3196 | + } else { |
|
| 3197 | + $set_sql_statements[] = $wpdb->prepare( |
|
| 3198 | + "`{$column_name}` = CASE |
|
| 3199 | 3199 | WHEN (`{$column_name}` >= %d) |
| 3200 | 3200 | THEN `{$column_name}` - %d |
| 3201 | 3201 | ELSE 0 |
| 3202 | 3202 | END", |
| 3203 | - $abs_qty, |
|
| 3204 | - $abs_qty |
|
| 3205 | - ); |
|
| 3206 | - } |
|
| 3207 | - } |
|
| 3208 | - return $this->updateFieldsInDB( |
|
| 3209 | - $fields, |
|
| 3210 | - implode(', ', $set_sql_statements) |
|
| 3211 | - ); |
|
| 3212 | - } |
|
| 3213 | - |
|
| 3214 | - |
|
| 3215 | - /** |
|
| 3216 | - * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
|
| 3217 | - * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
|
| 3218 | - * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
|
| 3219 | - * Returns true if the value was successfully bumped, and updates the value on this model object. |
|
| 3220 | - * Otherwise returns false. |
|
| 3221 | - * |
|
| 3222 | - * @since 4.9.80.p |
|
| 3223 | - * @param string $field_name_to_bump |
|
| 3224 | - * @param string $field_name_affecting_total |
|
| 3225 | - * @param string $limit_field_name |
|
| 3226 | - * @param int $quantity |
|
| 3227 | - * @return bool |
|
| 3228 | - * @throws EE_Error |
|
| 3229 | - * @throws InvalidArgumentException |
|
| 3230 | - * @throws InvalidDataTypeException |
|
| 3231 | - * @throws InvalidInterfaceException |
|
| 3232 | - * @throws ReflectionException |
|
| 3233 | - */ |
|
| 3234 | - public function incrementFieldConditionallyInDb($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity) |
|
| 3235 | - { |
|
| 3236 | - global $wpdb; |
|
| 3237 | - $field = $this->get_model()->field_settings_for($field_name_to_bump, true); |
|
| 3238 | - $column_name = $field->get_table_column(); |
|
| 3239 | - |
|
| 3240 | - $field_affecting_total = $this->get_model()->field_settings_for($field_name_affecting_total, true); |
|
| 3241 | - $column_affecting_total = $field_affecting_total->get_table_column(); |
|
| 3242 | - |
|
| 3243 | - $limiting_field = $this->get_model()->field_settings_for($limit_field_name, true); |
|
| 3244 | - $limiting_column = $limiting_field->get_table_column(); |
|
| 3245 | - return $this->updateFieldsInDB( |
|
| 3246 | - [$field], |
|
| 3247 | - $wpdb->prepare( |
|
| 3248 | - "`{$column_name}` = |
|
| 3203 | + $abs_qty, |
|
| 3204 | + $abs_qty |
|
| 3205 | + ); |
|
| 3206 | + } |
|
| 3207 | + } |
|
| 3208 | + return $this->updateFieldsInDB( |
|
| 3209 | + $fields, |
|
| 3210 | + implode(', ', $set_sql_statements) |
|
| 3211 | + ); |
|
| 3212 | + } |
|
| 3213 | + |
|
| 3214 | + |
|
| 3215 | + /** |
|
| 3216 | + * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
|
| 3217 | + * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
|
| 3218 | + * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
|
| 3219 | + * Returns true if the value was successfully bumped, and updates the value on this model object. |
|
| 3220 | + * Otherwise returns false. |
|
| 3221 | + * |
|
| 3222 | + * @since 4.9.80.p |
|
| 3223 | + * @param string $field_name_to_bump |
|
| 3224 | + * @param string $field_name_affecting_total |
|
| 3225 | + * @param string $limit_field_name |
|
| 3226 | + * @param int $quantity |
|
| 3227 | + * @return bool |
|
| 3228 | + * @throws EE_Error |
|
| 3229 | + * @throws InvalidArgumentException |
|
| 3230 | + * @throws InvalidDataTypeException |
|
| 3231 | + * @throws InvalidInterfaceException |
|
| 3232 | + * @throws ReflectionException |
|
| 3233 | + */ |
|
| 3234 | + public function incrementFieldConditionallyInDb($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity) |
|
| 3235 | + { |
|
| 3236 | + global $wpdb; |
|
| 3237 | + $field = $this->get_model()->field_settings_for($field_name_to_bump, true); |
|
| 3238 | + $column_name = $field->get_table_column(); |
|
| 3239 | + |
|
| 3240 | + $field_affecting_total = $this->get_model()->field_settings_for($field_name_affecting_total, true); |
|
| 3241 | + $column_affecting_total = $field_affecting_total->get_table_column(); |
|
| 3242 | + |
|
| 3243 | + $limiting_field = $this->get_model()->field_settings_for($limit_field_name, true); |
|
| 3244 | + $limiting_column = $limiting_field->get_table_column(); |
|
| 3245 | + return $this->updateFieldsInDB( |
|
| 3246 | + [$field], |
|
| 3247 | + $wpdb->prepare( |
|
| 3248 | + "`{$column_name}` = |
|
| 3249 | 3249 | CASE |
| 3250 | 3250 | WHEN ((`{$column_name}` + `{$column_affecting_total}` + %d) <= `{$limiting_column}`) OR `{$limiting_column}` = %d |
| 3251 | 3251 | THEN `{$column_name}` + %d |
| 3252 | 3252 | ELSE `{$column_name}` |
| 3253 | 3253 | END", |
| 3254 | - $quantity, |
|
| 3255 | - EE_INF_IN_DB, |
|
| 3256 | - $quantity |
|
| 3257 | - ) |
|
| 3258 | - ); |
|
| 3259 | - } |
|
| 3260 | - |
|
| 3261 | - |
|
| 3262 | - /** |
|
| 3263 | - * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 3264 | - * (probably a bad assumption they have made, oh well) |
|
| 3265 | - * |
|
| 3266 | - * @return string |
|
| 3267 | - */ |
|
| 3268 | - public function __toString() |
|
| 3269 | - { |
|
| 3270 | - try { |
|
| 3271 | - return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 3272 | - } catch (Exception $e) { |
|
| 3273 | - EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 3274 | - return ''; |
|
| 3275 | - } |
|
| 3276 | - } |
|
| 3277 | - |
|
| 3278 | - |
|
| 3279 | - /** |
|
| 3280 | - * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 3281 | - * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 3282 | - * This means if we have made changes to those related model objects, and want to unserialize |
|
| 3283 | - * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 3284 | - * Instead, those related model objects should be directly serialized and stored. |
|
| 3285 | - * Eg, the following won't work: |
|
| 3286 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3287 | - * $att = $reg->attendee(); |
|
| 3288 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3289 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 3290 | - * //END REQUEST |
|
| 3291 | - * //START NEXT REQUEST |
|
| 3292 | - * $reg = get_option( 'my_option' ); |
|
| 3293 | - * $reg->attendee()->save(); |
|
| 3294 | - * And would need to be replace with: |
|
| 3295 | - * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3296 | - * $att = $reg->attendee(); |
|
| 3297 | - * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3298 | - * update_option( 'my_option', serialize( $reg ) ); |
|
| 3299 | - * //END REQUEST |
|
| 3300 | - * //START NEXT REQUEST |
|
| 3301 | - * $att = get_option( 'my_option' ); |
|
| 3302 | - * $att->save(); |
|
| 3303 | - * |
|
| 3304 | - * @return array |
|
| 3305 | - * @throws ReflectionException |
|
| 3306 | - * @throws InvalidArgumentException |
|
| 3307 | - * @throws InvalidInterfaceException |
|
| 3308 | - * @throws InvalidDataTypeException |
|
| 3309 | - * @throws EE_Error |
|
| 3310 | - */ |
|
| 3311 | - public function __sleep() |
|
| 3312 | - { |
|
| 3313 | - $model = $this->get_model(); |
|
| 3314 | - foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 3315 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 3316 | - $classname = 'EE_' . $model->get_this_model_name(); |
|
| 3317 | - if ($this->get_one_from_cache($relation_name) instanceof $classname |
|
| 3318 | - && $this->get_one_from_cache($relation_name)->ID() |
|
| 3319 | - ) { |
|
| 3320 | - $this->clear_cache( |
|
| 3321 | - $relation_name, |
|
| 3322 | - $this->get_one_from_cache($relation_name)->ID() |
|
| 3323 | - ); |
|
| 3324 | - } |
|
| 3325 | - } |
|
| 3326 | - } |
|
| 3327 | - $this->_props_n_values_provided_in_constructor = array(); |
|
| 3328 | - $properties_to_serialize = get_object_vars($this); |
|
| 3329 | - // don't serialize the model. It's big and that risks recursion |
|
| 3330 | - unset($properties_to_serialize['_model']); |
|
| 3331 | - return array_keys($properties_to_serialize); |
|
| 3332 | - } |
|
| 3333 | - |
|
| 3334 | - |
|
| 3335 | - /** |
|
| 3336 | - * restore _props_n_values_provided_in_constructor |
|
| 3337 | - * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 3338 | - * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 3339 | - * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 3340 | - */ |
|
| 3341 | - public function __wakeup() |
|
| 3342 | - { |
|
| 3343 | - $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 3344 | - } |
|
| 3345 | - |
|
| 3346 | - |
|
| 3347 | - /** |
|
| 3348 | - * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
| 3349 | - * distinct with the clone host instance are also cloned. |
|
| 3350 | - */ |
|
| 3351 | - public function __clone() |
|
| 3352 | - { |
|
| 3353 | - // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
| 3354 | - foreach ($this->_fields as $field => $value) { |
|
| 3355 | - if ($value instanceof DateTime) { |
|
| 3356 | - $this->_fields[ $field ] = clone $value; |
|
| 3357 | - } |
|
| 3358 | - } |
|
| 3359 | - } |
|
| 3254 | + $quantity, |
|
| 3255 | + EE_INF_IN_DB, |
|
| 3256 | + $quantity |
|
| 3257 | + ) |
|
| 3258 | + ); |
|
| 3259 | + } |
|
| 3260 | + |
|
| 3261 | + |
|
| 3262 | + /** |
|
| 3263 | + * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
|
| 3264 | + * (probably a bad assumption they have made, oh well) |
|
| 3265 | + * |
|
| 3266 | + * @return string |
|
| 3267 | + */ |
|
| 3268 | + public function __toString() |
|
| 3269 | + { |
|
| 3270 | + try { |
|
| 3271 | + return sprintf('%s (%s)', $this->name(), $this->ID()); |
|
| 3272 | + } catch (Exception $e) { |
|
| 3273 | + EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__); |
|
| 3274 | + return ''; |
|
| 3275 | + } |
|
| 3276 | + } |
|
| 3277 | + |
|
| 3278 | + |
|
| 3279 | + /** |
|
| 3280 | + * Clear related model objects if they're already in the DB, because otherwise when we |
|
| 3281 | + * UN-serialize this model object we'll need to be careful to add them to the entity map. |
|
| 3282 | + * This means if we have made changes to those related model objects, and want to unserialize |
|
| 3283 | + * the this model object on a subsequent request, changes to those related model objects will be lost. |
|
| 3284 | + * Instead, those related model objects should be directly serialized and stored. |
|
| 3285 | + * Eg, the following won't work: |
|
| 3286 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3287 | + * $att = $reg->attendee(); |
|
| 3288 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3289 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 3290 | + * //END REQUEST |
|
| 3291 | + * //START NEXT REQUEST |
|
| 3292 | + * $reg = get_option( 'my_option' ); |
|
| 3293 | + * $reg->attendee()->save(); |
|
| 3294 | + * And would need to be replace with: |
|
| 3295 | + * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
|
| 3296 | + * $att = $reg->attendee(); |
|
| 3297 | + * $att->set( 'ATT_fname', 'Dirk' ); |
|
| 3298 | + * update_option( 'my_option', serialize( $reg ) ); |
|
| 3299 | + * //END REQUEST |
|
| 3300 | + * //START NEXT REQUEST |
|
| 3301 | + * $att = get_option( 'my_option' ); |
|
| 3302 | + * $att->save(); |
|
| 3303 | + * |
|
| 3304 | + * @return array |
|
| 3305 | + * @throws ReflectionException |
|
| 3306 | + * @throws InvalidArgumentException |
|
| 3307 | + * @throws InvalidInterfaceException |
|
| 3308 | + * @throws InvalidDataTypeException |
|
| 3309 | + * @throws EE_Error |
|
| 3310 | + */ |
|
| 3311 | + public function __sleep() |
|
| 3312 | + { |
|
| 3313 | + $model = $this->get_model(); |
|
| 3314 | + foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
|
| 3315 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 3316 | + $classname = 'EE_' . $model->get_this_model_name(); |
|
| 3317 | + if ($this->get_one_from_cache($relation_name) instanceof $classname |
|
| 3318 | + && $this->get_one_from_cache($relation_name)->ID() |
|
| 3319 | + ) { |
|
| 3320 | + $this->clear_cache( |
|
| 3321 | + $relation_name, |
|
| 3322 | + $this->get_one_from_cache($relation_name)->ID() |
|
| 3323 | + ); |
|
| 3324 | + } |
|
| 3325 | + } |
|
| 3326 | + } |
|
| 3327 | + $this->_props_n_values_provided_in_constructor = array(); |
|
| 3328 | + $properties_to_serialize = get_object_vars($this); |
|
| 3329 | + // don't serialize the model. It's big and that risks recursion |
|
| 3330 | + unset($properties_to_serialize['_model']); |
|
| 3331 | + return array_keys($properties_to_serialize); |
|
| 3332 | + } |
|
| 3333 | + |
|
| 3334 | + |
|
| 3335 | + /** |
|
| 3336 | + * restore _props_n_values_provided_in_constructor |
|
| 3337 | + * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
|
| 3338 | + * and therefore should NOT be used to determine if state change has occurred since initial construction. |
|
| 3339 | + * At best, you would only be able to detect if state change has occurred during THIS request. |
|
| 3340 | + */ |
|
| 3341 | + public function __wakeup() |
|
| 3342 | + { |
|
| 3343 | + $this->_props_n_values_provided_in_constructor = $this->_fields; |
|
| 3344 | + } |
|
| 3345 | + |
|
| 3346 | + |
|
| 3347 | + /** |
|
| 3348 | + * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
|
| 3349 | + * distinct with the clone host instance are also cloned. |
|
| 3350 | + */ |
|
| 3351 | + public function __clone() |
|
| 3352 | + { |
|
| 3353 | + // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
|
| 3354 | + foreach ($this->_fields as $field => $value) { |
|
| 3355 | + if ($value instanceof DateTime) { |
|
| 3356 | + $this->_fields[ $field ] = clone $value; |
|
| 3357 | + } |
|
| 3358 | + } |
|
| 3359 | + } |
|
| 3360 | 3360 | } |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
| 147 | 147 | // verify client code has not passed any invalid field names |
| 148 | 148 | foreach ($fieldValues as $field_name => $field_value) { |
| 149 | - if (! isset($model_fields[ $field_name ])) { |
|
| 149 | + if ( ! isset($model_fields[$field_name])) { |
|
| 150 | 150 | throw new EE_Error( |
| 151 | 151 | sprintf( |
| 152 | 152 | esc_html__( |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | 163 | $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
| 164 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 164 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 165 | 165 | list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
| 166 | 166 | } else { |
| 167 | 167 | // set default formats for date and time |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | foreach ($model_fields as $fieldName => $field) { |
| 175 | 175 | $this->set_from_db( |
| 176 | 176 | $fieldName, |
| 177 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null |
|
| 177 | + isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null |
|
| 178 | 178 | ); |
| 179 | 179 | } |
| 180 | 180 | } else { |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | foreach ($model_fields as $fieldName => $field) { |
| 184 | 184 | $this->set( |
| 185 | 185 | $fieldName, |
| 186 | - isset($fieldValues[ $fieldName ]) ? $fieldValues[ $fieldName ] : null, |
|
| 186 | + isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, |
|
| 187 | 187 | true |
| 188 | 188 | ); |
| 189 | 189 | } |
@@ -191,15 +191,15 @@ discard block |
||
| 191 | 191 | // remember what values were passed to this constructor |
| 192 | 192 | $this->_props_n_values_provided_in_constructor = $fieldValues; |
| 193 | 193 | // remember in entity mapper |
| 194 | - if (! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 194 | + if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) { |
|
| 195 | 195 | $model->add_to_entity_map($this); |
| 196 | 196 | } |
| 197 | 197 | // setup all the relations |
| 198 | 198 | foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
| 199 | 199 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
| 200 | - $this->_model_relations[ $relation_name ] = null; |
|
| 200 | + $this->_model_relations[$relation_name] = null; |
|
| 201 | 201 | } else { |
| 202 | - $this->_model_relations[ $relation_name ] = array(); |
|
| 202 | + $this->_model_relations[$relation_name] = array(); |
|
| 203 | 203 | } |
| 204 | 204 | } |
| 205 | 205 | /** |
@@ -250,10 +250,10 @@ discard block |
||
| 250 | 250 | */ |
| 251 | 251 | public function get_original($field_name) |
| 252 | 252 | { |
| 253 | - if (isset($this->_props_n_values_provided_in_constructor[ $field_name ]) |
|
| 253 | + if (isset($this->_props_n_values_provided_in_constructor[$field_name]) |
|
| 254 | 254 | && $field_settings = $this->get_model()->field_settings_for($field_name) |
| 255 | 255 | ) { |
| 256 | - return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[ $field_name ]); |
|
| 256 | + return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
|
| 257 | 257 | } |
| 258 | 258 | return null; |
| 259 | 259 | } |
@@ -288,8 +288,8 @@ discard block |
||
| 288 | 288 | { |
| 289 | 289 | // if not using default and nothing has changed, and object has already been setup (has ID), |
| 290 | 290 | // then don't do anything |
| 291 | - if (! $use_default |
|
| 292 | - && $this->_fields[ $field_name ] === $field_value |
|
| 291 | + if ( ! $use_default |
|
| 292 | + && $this->_fields[$field_name] === $field_value |
|
| 293 | 293 | && $this->ID() |
| 294 | 294 | ) { |
| 295 | 295 | return; |
@@ -307,7 +307,7 @@ discard block |
||
| 307 | 307 | $holder_of_value = $field_obj->prepare_for_set($field_value); |
| 308 | 308 | // should the value be null? |
| 309 | 309 | if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
| 310 | - $this->_fields[ $field_name ] = $field_obj->get_default_value(); |
|
| 310 | + $this->_fields[$field_name] = $field_obj->get_default_value(); |
|
| 311 | 311 | /** |
| 312 | 312 | * To save having to refactor all the models, if a default value is used for a |
| 313 | 313 | * EE_Datetime_Field, and that value is not null nor is it a DateTime |
@@ -317,15 +317,15 @@ discard block |
||
| 317 | 317 | * @since 4.6.10+ |
| 318 | 318 | */ |
| 319 | 319 | if ($field_obj instanceof EE_Datetime_Field |
| 320 | - && $this->_fields[ $field_name ] !== null |
|
| 321 | - && ! $this->_fields[ $field_name ] instanceof DateTime |
|
| 320 | + && $this->_fields[$field_name] !== null |
|
| 321 | + && ! $this->_fields[$field_name] instanceof DateTime |
|
| 322 | 322 | ) { |
| 323 | - empty($this->_fields[ $field_name ]) |
|
| 323 | + empty($this->_fields[$field_name]) |
|
| 324 | 324 | ? $this->set($field_name, time()) |
| 325 | - : $this->set($field_name, $this->_fields[ $field_name ]); |
|
| 325 | + : $this->set($field_name, $this->_fields[$field_name]); |
|
| 326 | 326 | } |
| 327 | 327 | } else { |
| 328 | - $this->_fields[ $field_name ] = $holder_of_value; |
|
| 328 | + $this->_fields[$field_name] = $holder_of_value; |
|
| 329 | 329 | } |
| 330 | 330 | // if we're not in the constructor... |
| 331 | 331 | // now check if what we set was a primary key |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
| 342 | 342 | $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
| 343 | 343 | foreach ($fields_on_model as $field_obj) { |
| 344 | - if (! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 344 | + if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
|
| 345 | 345 | && $field_obj->get_name() !== $field_name |
| 346 | 346 | ) { |
| 347 | 347 | $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
@@ -386,8 +386,8 @@ discard block |
||
| 386 | 386 | */ |
| 387 | 387 | public function getCustomSelect($alias) |
| 388 | 388 | { |
| 389 | - return isset($this->custom_selection_results[ $alias ]) |
|
| 390 | - ? $this->custom_selection_results[ $alias ] |
|
| 389 | + return isset($this->custom_selection_results[$alias]) |
|
| 390 | + ? $this->custom_selection_results[$alias] |
|
| 391 | 391 | : null; |
| 392 | 392 | } |
| 393 | 393 | |
@@ -474,8 +474,8 @@ discard block |
||
| 474 | 474 | foreach ($model_fields as $field_name => $field_obj) { |
| 475 | 475 | if ($field_obj instanceof EE_Datetime_Field) { |
| 476 | 476 | $field_obj->set_timezone($this->_timezone); |
| 477 | - if (isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime) { |
|
| 478 | - EEH_DTT_Helper::setTimezone($this->_fields[ $field_name ], new DateTimeZone($this->_timezone)); |
|
| 477 | + if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) { |
|
| 478 | + EEH_DTT_Helper::setTimezone($this->_fields[$field_name], new DateTimeZone($this->_timezone)); |
|
| 479 | 479 | } |
| 480 | 480 | } |
| 481 | 481 | } |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | */ |
| 534 | 534 | public function get_format($full = true) |
| 535 | 535 | { |
| 536 | - return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 536 | + return $full ? $this->_dt_frmt.' '.$this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
|
| 537 | 537 | } |
| 538 | 538 | |
| 539 | 539 | |
@@ -559,11 +559,11 @@ discard block |
||
| 559 | 559 | public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
| 560 | 560 | { |
| 561 | 561 | // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
| 562 | - if (! $object_to_cache instanceof EE_Base_Class) { |
|
| 562 | + if ( ! $object_to_cache instanceof EE_Base_Class) { |
|
| 563 | 563 | return false; |
| 564 | 564 | } |
| 565 | 565 | // also get "how" the object is related, or throw an error |
| 566 | - if (! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 566 | + if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
|
| 567 | 567 | throw new EE_Error( |
| 568 | 568 | sprintf( |
| 569 | 569 | esc_html__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
@@ -577,38 +577,38 @@ discard block |
||
| 577 | 577 | // if it's a "belongs to" relationship, then there's only one related model object |
| 578 | 578 | // eg, if this is a registration, there's only 1 attendee for it |
| 579 | 579 | // so for these model objects just set it to be cached |
| 580 | - $this->_model_relations[ $relationName ] = $object_to_cache; |
|
| 580 | + $this->_model_relations[$relationName] = $object_to_cache; |
|
| 581 | 581 | $return = true; |
| 582 | 582 | } else { |
| 583 | 583 | // otherwise, this is the "many" side of a one to many relationship, |
| 584 | 584 | // so we'll add the object to the array of related objects for that type. |
| 585 | 585 | // eg: if this is an event, there are many registrations for that event, |
| 586 | 586 | // so we cache the registrations in an array |
| 587 | - if (! is_array($this->_model_relations[ $relationName ])) { |
|
| 587 | + if ( ! is_array($this->_model_relations[$relationName])) { |
|
| 588 | 588 | // if for some reason, the cached item is a model object, |
| 589 | 589 | // then stick that in the array, otherwise start with an empty array |
| 590 | - $this->_model_relations[ $relationName ] = $this->_model_relations[ $relationName ] |
|
| 590 | + $this->_model_relations[$relationName] = $this->_model_relations[$relationName] |
|
| 591 | 591 | instanceof |
| 592 | 592 | EE_Base_Class |
| 593 | - ? array($this->_model_relations[ $relationName ]) : array(); |
|
| 593 | + ? array($this->_model_relations[$relationName]) : array(); |
|
| 594 | 594 | } |
| 595 | 595 | // first check for a cache_id which is normally empty |
| 596 | - if (! empty($cache_id)) { |
|
| 596 | + if ( ! empty($cache_id)) { |
|
| 597 | 597 | // if the cache_id exists, then it means we are purposely trying to cache this |
| 598 | 598 | // with a known key that can then be used to retrieve the object later on |
| 599 | - $this->_model_relations[ $relationName ][ $cache_id ] = $object_to_cache; |
|
| 599 | + $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
|
| 600 | 600 | $return = $cache_id; |
| 601 | 601 | } elseif ($object_to_cache->ID()) { |
| 602 | 602 | // OR the cached object originally came from the db, so let's just use it's PK for an ID |
| 603 | - $this->_model_relations[ $relationName ][ $object_to_cache->ID() ] = $object_to_cache; |
|
| 603 | + $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
|
| 604 | 604 | $return = $object_to_cache->ID(); |
| 605 | 605 | } else { |
| 606 | 606 | // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
| 607 | - $this->_model_relations[ $relationName ][] = $object_to_cache; |
|
| 607 | + $this->_model_relations[$relationName][] = $object_to_cache; |
|
| 608 | 608 | // move the internal pointer to the end of the array |
| 609 | - end($this->_model_relations[ $relationName ]); |
|
| 609 | + end($this->_model_relations[$relationName]); |
|
| 610 | 610 | // and grab the key so that we can return it |
| 611 | - $return = key($this->_model_relations[ $relationName ]); |
|
| 611 | + $return = key($this->_model_relations[$relationName]); |
|
| 612 | 612 | } |
| 613 | 613 | } |
| 614 | 614 | return $return; |
@@ -634,7 +634,7 @@ discard block |
||
| 634 | 634 | // first make sure this property exists |
| 635 | 635 | $this->get_model()->field_settings_for($fieldname); |
| 636 | 636 | $cache_type = empty($cache_type) ? 'standard' : $cache_type; |
| 637 | - $this->_cached_properties[ $fieldname ][ $cache_type ] = $value; |
|
| 637 | + $this->_cached_properties[$fieldname][$cache_type] = $value; |
|
| 638 | 638 | } |
| 639 | 639 | |
| 640 | 640 | |
@@ -663,9 +663,9 @@ discard block |
||
| 663 | 663 | $model = $this->get_model(); |
| 664 | 664 | $model->field_settings_for($fieldname); |
| 665 | 665 | $cache_type = $pretty ? 'pretty' : 'standard'; |
| 666 | - $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : ''; |
|
| 667 | - if (isset($this->_cached_properties[ $fieldname ][ $cache_type ])) { |
|
| 668 | - return $this->_cached_properties[ $fieldname ][ $cache_type ]; |
|
| 666 | + $cache_type .= ! empty($extra_cache_ref) ? '_'.$extra_cache_ref : ''; |
|
| 667 | + if (isset($this->_cached_properties[$fieldname][$cache_type])) { |
|
| 668 | + return $this->_cached_properties[$fieldname][$cache_type]; |
|
| 669 | 669 | } |
| 670 | 670 | $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref); |
| 671 | 671 | $this->_set_cached_property($fieldname, $value, $cache_type); |
@@ -693,12 +693,12 @@ discard block |
||
| 693 | 693 | if ($field_obj instanceof EE_Datetime_Field) { |
| 694 | 694 | $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref); |
| 695 | 695 | } |
| 696 | - if (! isset($this->_fields[ $fieldname ])) { |
|
| 697 | - $this->_fields[ $fieldname ] = null; |
|
| 696 | + if ( ! isset($this->_fields[$fieldname])) { |
|
| 697 | + $this->_fields[$fieldname] = null; |
|
| 698 | 698 | } |
| 699 | 699 | $value = $pretty |
| 700 | - ? $field_obj->prepare_for_pretty_echoing($this->_fields[ $fieldname ], $extra_cache_ref) |
|
| 701 | - : $field_obj->prepare_for_get($this->_fields[ $fieldname ]); |
|
| 700 | + ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref) |
|
| 701 | + : $field_obj->prepare_for_get($this->_fields[$fieldname]); |
|
| 702 | 702 | return $value; |
| 703 | 703 | } |
| 704 | 704 | |
@@ -756,8 +756,8 @@ discard block |
||
| 756 | 756 | */ |
| 757 | 757 | protected function _clear_cached_property($property_name) |
| 758 | 758 | { |
| 759 | - if (isset($this->_cached_properties[ $property_name ])) { |
|
| 760 | - unset($this->_cached_properties[ $property_name ]); |
|
| 759 | + if (isset($this->_cached_properties[$property_name])) { |
|
| 760 | + unset($this->_cached_properties[$property_name]); |
|
| 761 | 761 | } |
| 762 | 762 | } |
| 763 | 763 | |
@@ -809,7 +809,7 @@ discard block |
||
| 809 | 809 | { |
| 810 | 810 | $relationship_to_model = $this->get_model()->related_settings_for($relationName); |
| 811 | 811 | $index_in_cache = ''; |
| 812 | - if (! $relationship_to_model) { |
|
| 812 | + if ( ! $relationship_to_model) { |
|
| 813 | 813 | throw new EE_Error( |
| 814 | 814 | sprintf( |
| 815 | 815 | esc_html__('There is no relationship to %s on a %s. Cannot clear that cache', 'event_espresso'), |
@@ -820,21 +820,21 @@ discard block |
||
| 820 | 820 | } |
| 821 | 821 | if ($clear_all) { |
| 822 | 822 | $obj_removed = true; |
| 823 | - $this->_model_relations[ $relationName ] = null; |
|
| 823 | + $this->_model_relations[$relationName] = null; |
|
| 824 | 824 | } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
| 825 | - $obj_removed = $this->_model_relations[ $relationName ]; |
|
| 826 | - $this->_model_relations[ $relationName ] = null; |
|
| 825 | + $obj_removed = $this->_model_relations[$relationName]; |
|
| 826 | + $this->_model_relations[$relationName] = null; |
|
| 827 | 827 | } else { |
| 828 | 828 | if ($object_to_remove_or_index_into_array instanceof EE_Base_Class |
| 829 | 829 | && $object_to_remove_or_index_into_array->ID() |
| 830 | 830 | ) { |
| 831 | 831 | $index_in_cache = $object_to_remove_or_index_into_array->ID(); |
| 832 | - if (is_array($this->_model_relations[ $relationName ]) |
|
| 833 | - && ! isset($this->_model_relations[ $relationName ][ $index_in_cache ]) |
|
| 832 | + if (is_array($this->_model_relations[$relationName]) |
|
| 833 | + && ! isset($this->_model_relations[$relationName][$index_in_cache]) |
|
| 834 | 834 | ) { |
| 835 | 835 | $index_found_at = null; |
| 836 | 836 | // find this object in the array even though it has a different key |
| 837 | - foreach ($this->_model_relations[ $relationName ] as $index => $obj) { |
|
| 837 | + foreach ($this->_model_relations[$relationName] as $index => $obj) { |
|
| 838 | 838 | /** @noinspection TypeUnsafeComparisonInspection */ |
| 839 | 839 | if ($obj instanceof EE_Base_Class |
| 840 | 840 | && ( |
@@ -867,9 +867,9 @@ discard block |
||
| 867 | 867 | } |
| 868 | 868 | // supposedly we've found it. But it could just be that the client code |
| 869 | 869 | // provided a bad index/object |
| 870 | - if (isset($this->_model_relations[ $relationName ][ $index_in_cache ])) { |
|
| 871 | - $obj_removed = $this->_model_relations[ $relationName ][ $index_in_cache ]; |
|
| 872 | - unset($this->_model_relations[ $relationName ][ $index_in_cache ]); |
|
| 870 | + if (isset($this->_model_relations[$relationName][$index_in_cache])) { |
|
| 871 | + $obj_removed = $this->_model_relations[$relationName][$index_in_cache]; |
|
| 872 | + unset($this->_model_relations[$relationName][$index_in_cache]); |
|
| 873 | 873 | } else { |
| 874 | 874 | // that thing was never cached anyways. |
| 875 | 875 | $obj_removed = null; |
@@ -900,7 +900,7 @@ discard block |
||
| 900 | 900 | $current_cache_id = '' |
| 901 | 901 | ) { |
| 902 | 902 | // verify that incoming object is of the correct type |
| 903 | - $obj_class = 'EE_' . $relationName; |
|
| 903 | + $obj_class = 'EE_'.$relationName; |
|
| 904 | 904 | if ($newly_saved_object instanceof $obj_class) { |
| 905 | 905 | /* @type EE_Base_Class $newly_saved_object */ |
| 906 | 906 | // now get the type of relation |
@@ -908,17 +908,17 @@ discard block |
||
| 908 | 908 | // if this is a 1:1 relationship |
| 909 | 909 | if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
| 910 | 910 | // then just replace the cached object with the newly saved object |
| 911 | - $this->_model_relations[ $relationName ] = $newly_saved_object; |
|
| 911 | + $this->_model_relations[$relationName] = $newly_saved_object; |
|
| 912 | 912 | return true; |
| 913 | 913 | // or if it's some kind of sordid feral polyamorous relationship... |
| 914 | 914 | } |
| 915 | - if (is_array($this->_model_relations[ $relationName ]) |
|
| 916 | - && isset($this->_model_relations[ $relationName ][ $current_cache_id ]) |
|
| 915 | + if (is_array($this->_model_relations[$relationName]) |
|
| 916 | + && isset($this->_model_relations[$relationName][$current_cache_id]) |
|
| 917 | 917 | ) { |
| 918 | 918 | // then remove the current cached item |
| 919 | - unset($this->_model_relations[ $relationName ][ $current_cache_id ]); |
|
| 919 | + unset($this->_model_relations[$relationName][$current_cache_id]); |
|
| 920 | 920 | // and cache the newly saved object using it's new ID |
| 921 | - $this->_model_relations[ $relationName ][ $newly_saved_object->ID() ] = $newly_saved_object; |
|
| 921 | + $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object; |
|
| 922 | 922 | return true; |
| 923 | 923 | } |
| 924 | 924 | } |
@@ -935,8 +935,8 @@ discard block |
||
| 935 | 935 | */ |
| 936 | 936 | public function get_one_from_cache($relationName) |
| 937 | 937 | { |
| 938 | - $cached_array_or_object = isset($this->_model_relations[ $relationName ]) |
|
| 939 | - ? $this->_model_relations[ $relationName ] |
|
| 938 | + $cached_array_or_object = isset($this->_model_relations[$relationName]) |
|
| 939 | + ? $this->_model_relations[$relationName] |
|
| 940 | 940 | : null; |
| 941 | 941 | if (is_array($cached_array_or_object)) { |
| 942 | 942 | return array_shift($cached_array_or_object); |
@@ -959,7 +959,7 @@ discard block |
||
| 959 | 959 | */ |
| 960 | 960 | public function get_all_from_cache($relationName) |
| 961 | 961 | { |
| 962 | - $objects = isset($this->_model_relations[ $relationName ]) ? $this->_model_relations[ $relationName ] : array(); |
|
| 962 | + $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array(); |
|
| 963 | 963 | // if the result is not an array, but exists, make it an array |
| 964 | 964 | $objects = is_array($objects) ? $objects : array($objects); |
| 965 | 965 | // bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143 |
@@ -1143,7 +1143,7 @@ discard block |
||
| 1143 | 1143 | } else { |
| 1144 | 1144 | $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db); |
| 1145 | 1145 | } |
| 1146 | - $this->_fields[ $field_name ] = $field_value; |
|
| 1146 | + $this->_fields[$field_name] = $field_value; |
|
| 1147 | 1147 | $this->_clear_cached_property($field_name); |
| 1148 | 1148 | } |
| 1149 | 1149 | } |
@@ -1183,9 +1183,9 @@ discard block |
||
| 1183 | 1183 | public function get_raw($field_name) |
| 1184 | 1184 | { |
| 1185 | 1185 | $field_settings = $this->get_model()->field_settings_for($field_name); |
| 1186 | - return $field_settings instanceof EE_Datetime_Field && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1187 | - ? $this->_fields[ $field_name ]->format('U') |
|
| 1188 | - : $this->_fields[ $field_name ]; |
|
| 1186 | + return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime |
|
| 1187 | + ? $this->_fields[$field_name]->format('U') |
|
| 1188 | + : $this->_fields[$field_name]; |
|
| 1189 | 1189 | } |
| 1190 | 1190 | |
| 1191 | 1191 | |
@@ -1207,7 +1207,7 @@ discard block |
||
| 1207 | 1207 | public function get_DateTime_object($field_name) |
| 1208 | 1208 | { |
| 1209 | 1209 | $field_settings = $this->get_model()->field_settings_for($field_name); |
| 1210 | - if (! $field_settings instanceof EE_Datetime_Field) { |
|
| 1210 | + if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
| 1211 | 1211 | EE_Error::add_error( |
| 1212 | 1212 | sprintf( |
| 1213 | 1213 | esc_html__( |
@@ -1222,8 +1222,8 @@ discard block |
||
| 1222 | 1222 | ); |
| 1223 | 1223 | return false; |
| 1224 | 1224 | } |
| 1225 | - return isset($this->_fields[ $field_name ]) && $this->_fields[ $field_name ] instanceof DateTime |
|
| 1226 | - ? clone $this->_fields[ $field_name ] |
|
| 1225 | + return isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime |
|
| 1226 | + ? clone $this->_fields[$field_name] |
|
| 1227 | 1227 | : null; |
| 1228 | 1228 | } |
| 1229 | 1229 | |
@@ -1465,7 +1465,7 @@ discard block |
||
| 1465 | 1465 | */ |
| 1466 | 1466 | public function get_i18n_datetime($field_name, $format = '') |
| 1467 | 1467 | { |
| 1468 | - $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format; |
|
| 1468 | + $format = empty($format) ? $this->_dt_frmt.' '.$this->_tm_frmt : $format; |
|
| 1469 | 1469 | return date_i18n( |
| 1470 | 1470 | $format, |
| 1471 | 1471 | EEH_DTT_Helper::get_timestamp_with_offset( |
@@ -1577,21 +1577,21 @@ discard block |
||
| 1577 | 1577 | $field->set_time_format($this->_tm_frmt); |
| 1578 | 1578 | switch ($what) { |
| 1579 | 1579 | case 'T': |
| 1580 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_time( |
|
| 1580 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time( |
|
| 1581 | 1581 | $datetime_value, |
| 1582 | - $this->_fields[ $fieldname ] |
|
| 1582 | + $this->_fields[$fieldname] |
|
| 1583 | 1583 | ); |
| 1584 | 1584 | $this->_has_changes = true; |
| 1585 | 1585 | break; |
| 1586 | 1586 | case 'D': |
| 1587 | - $this->_fields[ $fieldname ] = $field->prepare_for_set_with_new_date( |
|
| 1587 | + $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date( |
|
| 1588 | 1588 | $datetime_value, |
| 1589 | - $this->_fields[ $fieldname ] |
|
| 1589 | + $this->_fields[$fieldname] |
|
| 1590 | 1590 | ); |
| 1591 | 1591 | $this->_has_changes = true; |
| 1592 | 1592 | break; |
| 1593 | 1593 | case 'B': |
| 1594 | - $this->_fields[ $fieldname ] = $field->prepare_for_set($datetime_value); |
|
| 1594 | + $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value); |
|
| 1595 | 1595 | $this->_has_changes = true; |
| 1596 | 1596 | break; |
| 1597 | 1597 | } |
@@ -1634,7 +1634,7 @@ discard block |
||
| 1634 | 1634 | $this->set_timezone($timezone); |
| 1635 | 1635 | $fn = (array) $field_name; |
| 1636 | 1636 | $args = array_merge($fn, (array) $args); |
| 1637 | - if (! method_exists($this, $callback)) { |
|
| 1637 | + if ( ! method_exists($this, $callback)) { |
|
| 1638 | 1638 | throw new EE_Error( |
| 1639 | 1639 | sprintf( |
| 1640 | 1640 | esc_html__( |
@@ -1646,7 +1646,7 @@ discard block |
||
| 1646 | 1646 | ); |
| 1647 | 1647 | } |
| 1648 | 1648 | $args = (array) $args; |
| 1649 | - $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append; |
|
| 1649 | + $return = $prepend.call_user_func_array(array($this, $callback), $args).$append; |
|
| 1650 | 1650 | $this->set_timezone($original_timezone); |
| 1651 | 1651 | return $return; |
| 1652 | 1652 | } |
@@ -1761,8 +1761,8 @@ discard block |
||
| 1761 | 1761 | { |
| 1762 | 1762 | $model = $this->get_model(); |
| 1763 | 1763 | foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
| 1764 | - if (! empty($this->_model_relations[ $relation_name ])) { |
|
| 1765 | - $related_objects = $this->_model_relations[ $relation_name ]; |
|
| 1764 | + if ( ! empty($this->_model_relations[$relation_name])) { |
|
| 1765 | + $related_objects = $this->_model_relations[$relation_name]; |
|
| 1766 | 1766 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
| 1767 | 1767 | // this relation only stores a single model object, not an array |
| 1768 | 1768 | // but let's make it consistent |
@@ -1819,7 +1819,7 @@ discard block |
||
| 1819 | 1819 | $this->set($column, $value); |
| 1820 | 1820 | } |
| 1821 | 1821 | // no changes ? then don't do anything |
| 1822 | - if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1822 | + if ( ! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) { |
|
| 1823 | 1823 | return 0; |
| 1824 | 1824 | } |
| 1825 | 1825 | /** |
@@ -1829,7 +1829,7 @@ discard block |
||
| 1829 | 1829 | * @param EE_Base_Class $model_object the model object about to be saved. |
| 1830 | 1830 | */ |
| 1831 | 1831 | do_action('AHEE__EE_Base_Class__save__begin', $this); |
| 1832 | - if (! $this->allow_persist()) { |
|
| 1832 | + if ( ! $this->allow_persist()) { |
|
| 1833 | 1833 | return 0; |
| 1834 | 1834 | } |
| 1835 | 1835 | // now get current attribute values |
@@ -1844,10 +1844,10 @@ discard block |
||
| 1844 | 1844 | if ($model->has_primary_key_field()) { |
| 1845 | 1845 | if ($model->get_primary_key_field()->is_auto_increment()) { |
| 1846 | 1846 | // ok check if it's set, if so: update; if not, insert |
| 1847 | - if (! empty($save_cols_n_values[ $model->primary_key_name() ])) { |
|
| 1847 | + if ( ! empty($save_cols_n_values[$model->primary_key_name()])) { |
|
| 1848 | 1848 | $results = $model->update_by_ID($save_cols_n_values, $this->ID()); |
| 1849 | 1849 | } else { |
| 1850 | - unset($save_cols_n_values[ $model->primary_key_name() ]); |
|
| 1850 | + unset($save_cols_n_values[$model->primary_key_name()]); |
|
| 1851 | 1851 | $results = $model->insert($save_cols_n_values); |
| 1852 | 1852 | if ($results) { |
| 1853 | 1853 | // if successful, set the primary key |
@@ -1857,7 +1857,7 @@ discard block |
||
| 1857 | 1857 | // will get added to the mapper before we can add this one! |
| 1858 | 1858 | // but if we just avoid using the SET method, all that headache can be avoided |
| 1859 | 1859 | $pk_field_name = $model->primary_key_name(); |
| 1860 | - $this->_fields[ $pk_field_name ] = $results; |
|
| 1860 | + $this->_fields[$pk_field_name] = $results; |
|
| 1861 | 1861 | $this->_clear_cached_property($pk_field_name); |
| 1862 | 1862 | $model->add_to_entity_map($this); |
| 1863 | 1863 | $this->_update_cached_related_model_objs_fks(); |
@@ -1874,8 +1874,8 @@ discard block |
||
| 1874 | 1874 | 'event_espresso' |
| 1875 | 1875 | ), |
| 1876 | 1876 | get_class($this), |
| 1877 | - get_class($model) . '::instance()->add_to_entity_map()', |
|
| 1878 | - get_class($model) . '::instance()->get_one_by_ID()', |
|
| 1877 | + get_class($model).'::instance()->add_to_entity_map()', |
|
| 1878 | + get_class($model).'::instance()->get_one_by_ID()', |
|
| 1879 | 1879 | '<br />' |
| 1880 | 1880 | ) |
| 1881 | 1881 | ); |
@@ -1977,27 +1977,27 @@ discard block |
||
| 1977 | 1977 | public function save_new_cached_related_model_objs() |
| 1978 | 1978 | { |
| 1979 | 1979 | // make sure this has been saved |
| 1980 | - if (! $this->ID()) { |
|
| 1980 | + if ( ! $this->ID()) { |
|
| 1981 | 1981 | $id = $this->save(); |
| 1982 | 1982 | } else { |
| 1983 | 1983 | $id = $this->ID(); |
| 1984 | 1984 | } |
| 1985 | 1985 | // now save all the NEW cached model objects (ie they don't exist in the DB) |
| 1986 | 1986 | foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) { |
| 1987 | - if ($this->_model_relations[ $relationName ]) { |
|
| 1987 | + if ($this->_model_relations[$relationName]) { |
|
| 1988 | 1988 | // is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation) |
| 1989 | 1989 | // or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)? |
| 1990 | 1990 | /* @var $related_model_obj EE_Base_Class */ |
| 1991 | 1991 | if ($relationObj instanceof EE_Belongs_To_Relation) { |
| 1992 | 1992 | // add a relation to that relation type (which saves the appropriate thing in the process) |
| 1993 | 1993 | // but ONLY if it DOES NOT exist in the DB |
| 1994 | - $related_model_obj = $this->_model_relations[ $relationName ]; |
|
| 1994 | + $related_model_obj = $this->_model_relations[$relationName]; |
|
| 1995 | 1995 | // if( ! $related_model_obj->ID()){ |
| 1996 | 1996 | $this->_add_relation_to($related_model_obj, $relationName); |
| 1997 | 1997 | $related_model_obj->save_new_cached_related_model_objs(); |
| 1998 | 1998 | // } |
| 1999 | 1999 | } else { |
| 2000 | - foreach ($this->_model_relations[ $relationName ] as $related_model_obj) { |
|
| 2000 | + foreach ($this->_model_relations[$relationName] as $related_model_obj) { |
|
| 2001 | 2001 | // add a relation to that relation type (which saves the appropriate thing in the process) |
| 2002 | 2002 | // but ONLY if it DOES NOT exist in the DB |
| 2003 | 2003 | // if( ! $related_model_obj->ID()){ |
@@ -2024,7 +2024,7 @@ discard block |
||
| 2024 | 2024 | */ |
| 2025 | 2025 | public function get_model() |
| 2026 | 2026 | { |
| 2027 | - if (! $this->_model) { |
|
| 2027 | + if ( ! $this->_model) { |
|
| 2028 | 2028 | $modelName = self::_get_model_classname(get_class($this)); |
| 2029 | 2029 | $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone); |
| 2030 | 2030 | } else { |
@@ -2049,9 +2049,9 @@ discard block |
||
| 2049 | 2049 | // TODO: will not work for Term_Relationships because they have no PK! |
| 2050 | 2050 | $primary_id_ref = self::_get_primary_key_name($classname); |
| 2051 | 2051 | if (array_key_exists($primary_id_ref, $props_n_values) |
| 2052 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2052 | + && ! empty($props_n_values[$primary_id_ref]) |
|
| 2053 | 2053 | ) { |
| 2054 | - $id = $props_n_values[ $primary_id_ref ]; |
|
| 2054 | + $id = $props_n_values[$primary_id_ref]; |
|
| 2055 | 2055 | return self::_get_model($classname)->get_from_entity_map($id); |
| 2056 | 2056 | } |
| 2057 | 2057 | return false; |
@@ -2085,10 +2085,10 @@ discard block |
||
| 2085 | 2085 | if ($model->has_primary_key_field()) { |
| 2086 | 2086 | $primary_id_ref = self::_get_primary_key_name($classname); |
| 2087 | 2087 | if (array_key_exists($primary_id_ref, $props_n_values) |
| 2088 | - && ! empty($props_n_values[ $primary_id_ref ]) |
|
| 2088 | + && ! empty($props_n_values[$primary_id_ref]) |
|
| 2089 | 2089 | ) { |
| 2090 | 2090 | $existing = $model->get_one_by_ID( |
| 2091 | - $props_n_values[ $primary_id_ref ] |
|
| 2091 | + $props_n_values[$primary_id_ref] |
|
| 2092 | 2092 | ); |
| 2093 | 2093 | } |
| 2094 | 2094 | } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) { |
@@ -2100,7 +2100,7 @@ discard block |
||
| 2100 | 2100 | } |
| 2101 | 2101 | if ($existing) { |
| 2102 | 2102 | // set date formats if present before setting values |
| 2103 | - if (! empty($date_formats) && is_array($date_formats)) { |
|
| 2103 | + if ( ! empty($date_formats) && is_array($date_formats)) { |
|
| 2104 | 2104 | $existing->set_date_format($date_formats[0]); |
| 2105 | 2105 | $existing->set_time_format($date_formats[1]); |
| 2106 | 2106 | } else { |
@@ -2133,7 +2133,7 @@ discard block |
||
| 2133 | 2133 | protected static function _get_model($classname, $timezone = null) |
| 2134 | 2134 | { |
| 2135 | 2135 | // find model for this class |
| 2136 | - if (! $classname) { |
|
| 2136 | + if ( ! $classname) { |
|
| 2137 | 2137 | throw new EE_Error( |
| 2138 | 2138 | sprintf( |
| 2139 | 2139 | esc_html__( |
@@ -2182,7 +2182,7 @@ discard block |
||
| 2182 | 2182 | if (strpos($model_name, 'EE_') === 0) { |
| 2183 | 2183 | $model_classname = str_replace('EE_', 'EEM_', $model_name); |
| 2184 | 2184 | } else { |
| 2185 | - $model_classname = 'EEM_' . $model_name; |
|
| 2185 | + $model_classname = 'EEM_'.$model_name; |
|
| 2186 | 2186 | } |
| 2187 | 2187 | return $model_classname; |
| 2188 | 2188 | } |
@@ -2201,7 +2201,7 @@ discard block |
||
| 2201 | 2201 | */ |
| 2202 | 2202 | protected static function _get_primary_key_name($classname = null) |
| 2203 | 2203 | { |
| 2204 | - if (! $classname) { |
|
| 2204 | + if ( ! $classname) { |
|
| 2205 | 2205 | throw new EE_Error( |
| 2206 | 2206 | sprintf( |
| 2207 | 2207 | esc_html__('What were you thinking calling _get_primary_key_name(%s)', 'event_espresso'), |
@@ -2231,7 +2231,7 @@ discard block |
||
| 2231 | 2231 | $model = $this->get_model(); |
| 2232 | 2232 | // now that we know the name of the variable, use a variable variable to get its value and return its |
| 2233 | 2233 | if ($model->has_primary_key_field()) { |
| 2234 | - return $this->_fields[ $model->primary_key_name() ]; |
|
| 2234 | + return $this->_fields[$model->primary_key_name()]; |
|
| 2235 | 2235 | } |
| 2236 | 2236 | return $model->get_index_primary_key_string($this->_fields); |
| 2237 | 2237 | } |
@@ -2284,7 +2284,7 @@ discard block |
||
| 2284 | 2284 | } |
| 2285 | 2285 | } else { |
| 2286 | 2286 | // this thing doesn't exist in the DB, so just cache it |
| 2287 | - if (! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2287 | + if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) { |
|
| 2288 | 2288 | throw new EE_Error( |
| 2289 | 2289 | sprintf( |
| 2290 | 2290 | esc_html__( |
@@ -2449,7 +2449,7 @@ discard block |
||
| 2449 | 2449 | } else { |
| 2450 | 2450 | // did we already cache the result of this query? |
| 2451 | 2451 | $cached_results = $this->get_all_from_cache($relationName); |
| 2452 | - if (! $cached_results) { |
|
| 2452 | + if ( ! $cached_results) { |
|
| 2453 | 2453 | $related_model_objects = $this->get_model()->get_all_related( |
| 2454 | 2454 | $this, |
| 2455 | 2455 | $relationName, |
@@ -2559,7 +2559,7 @@ discard block |
||
| 2559 | 2559 | } else { |
| 2560 | 2560 | // first, check if we've already cached the result of this query |
| 2561 | 2561 | $cached_result = $this->get_one_from_cache($relationName); |
| 2562 | - if (! $cached_result) { |
|
| 2562 | + if ( ! $cached_result) { |
|
| 2563 | 2563 | $related_model_object = $model->get_first_related( |
| 2564 | 2564 | $this, |
| 2565 | 2565 | $relationName, |
@@ -2583,7 +2583,7 @@ discard block |
||
| 2583 | 2583 | } |
| 2584 | 2584 | // this doesn't exist in the DB and apparently the thing it belongs to doesn't either, |
| 2585 | 2585 | // just get what's cached on this object |
| 2586 | - if (! $related_model_object) { |
|
| 2586 | + if ( ! $related_model_object) { |
|
| 2587 | 2587 | $related_model_object = $this->get_one_from_cache($relationName); |
| 2588 | 2588 | } |
| 2589 | 2589 | } |
@@ -2665,7 +2665,7 @@ discard block |
||
| 2665 | 2665 | */ |
| 2666 | 2666 | public function is_set($field_name) |
| 2667 | 2667 | { |
| 2668 | - return isset($this->_fields[ $field_name ]); |
|
| 2668 | + return isset($this->_fields[$field_name]); |
|
| 2669 | 2669 | } |
| 2670 | 2670 | |
| 2671 | 2671 | |
@@ -2681,7 +2681,7 @@ discard block |
||
| 2681 | 2681 | { |
| 2682 | 2682 | foreach ((array) $properties as $property_name) { |
| 2683 | 2683 | // first make sure this property exists |
| 2684 | - if (! $this->_fields[ $property_name ]) { |
|
| 2684 | + if ( ! $this->_fields[$property_name]) { |
|
| 2685 | 2685 | throw new EE_Error( |
| 2686 | 2686 | sprintf( |
| 2687 | 2687 | esc_html__( |
@@ -2713,7 +2713,7 @@ discard block |
||
| 2713 | 2713 | $properties = array(); |
| 2714 | 2714 | // remove prepended underscore |
| 2715 | 2715 | foreach ($fields as $field_name => $settings) { |
| 2716 | - $properties[ $field_name ] = $this->get($field_name); |
|
| 2716 | + $properties[$field_name] = $this->get($field_name); |
|
| 2717 | 2717 | } |
| 2718 | 2718 | return $properties; |
| 2719 | 2719 | } |
@@ -2750,7 +2750,7 @@ discard block |
||
| 2750 | 2750 | { |
| 2751 | 2751 | $className = get_class($this); |
| 2752 | 2752 | $tagName = "FHEE__{$className}__{$methodName}"; |
| 2753 | - if (! has_filter($tagName)) { |
|
| 2753 | + if ( ! has_filter($tagName)) { |
|
| 2754 | 2754 | throw new EE_Error( |
| 2755 | 2755 | sprintf( |
| 2756 | 2756 | esc_html__( |
@@ -2795,7 +2795,7 @@ discard block |
||
| 2795 | 2795 | $query_params[0]['EXM_value'] = $meta_value; |
| 2796 | 2796 | } |
| 2797 | 2797 | $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params); |
| 2798 | - if (! $existing_rows_like_that) { |
|
| 2798 | + if ( ! $existing_rows_like_that) { |
|
| 2799 | 2799 | return $this->add_extra_meta($meta_key, $meta_value); |
| 2800 | 2800 | } |
| 2801 | 2801 | foreach ($existing_rows_like_that as $existing_row) { |
@@ -2913,7 +2913,7 @@ discard block |
||
| 2913 | 2913 | $values = array(); |
| 2914 | 2914 | foreach ($results as $result) { |
| 2915 | 2915 | if ($result instanceof EE_Extra_Meta) { |
| 2916 | - $values[ $result->ID() ] = $result->value(); |
|
| 2916 | + $values[$result->ID()] = $result->value(); |
|
| 2917 | 2917 | } |
| 2918 | 2918 | } |
| 2919 | 2919 | return $values; |
@@ -2958,17 +2958,17 @@ discard block |
||
| 2958 | 2958 | ); |
| 2959 | 2959 | foreach ($extra_meta_objs as $extra_meta_obj) { |
| 2960 | 2960 | if ($extra_meta_obj instanceof EE_Extra_Meta) { |
| 2961 | - $return_array[ $extra_meta_obj->key() ] = $extra_meta_obj->value(); |
|
| 2961 | + $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value(); |
|
| 2962 | 2962 | } |
| 2963 | 2963 | } |
| 2964 | 2964 | } else { |
| 2965 | 2965 | $extra_meta_objs = $this->get_many_related('Extra_Meta'); |
| 2966 | 2966 | foreach ($extra_meta_objs as $extra_meta_obj) { |
| 2967 | 2967 | if ($extra_meta_obj instanceof EE_Extra_Meta) { |
| 2968 | - if (! isset($return_array[ $extra_meta_obj->key() ])) { |
|
| 2969 | - $return_array[ $extra_meta_obj->key() ] = array(); |
|
| 2968 | + if ( ! isset($return_array[$extra_meta_obj->key()])) { |
|
| 2969 | + $return_array[$extra_meta_obj->key()] = array(); |
|
| 2970 | 2970 | } |
| 2971 | - $return_array[ $extra_meta_obj->key() ][ $extra_meta_obj->ID() ] = $extra_meta_obj->value(); |
|
| 2971 | + $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value(); |
|
| 2972 | 2972 | } |
| 2973 | 2973 | } |
| 2974 | 2974 | } |
@@ -3049,8 +3049,8 @@ discard block |
||
| 3049 | 3049 | 'event_espresso' |
| 3050 | 3050 | ), |
| 3051 | 3051 | $this->ID(), |
| 3052 | - get_class($this->get_model()) . '::instance()->add_to_entity_map()', |
|
| 3053 | - get_class($this->get_model()) . '::instance()->refresh_entity_map()' |
|
| 3052 | + get_class($this->get_model()).'::instance()->add_to_entity_map()', |
|
| 3053 | + get_class($this->get_model()).'::instance()->refresh_entity_map()' |
|
| 3054 | 3054 | ) |
| 3055 | 3055 | ); |
| 3056 | 3056 | } |
@@ -3083,7 +3083,7 @@ discard block |
||
| 3083 | 3083 | { |
| 3084 | 3084 | // First make sure this model object actually exists in the DB. It would be silly to try to update it in the DB |
| 3085 | 3085 | // if it wasn't even there to start off. |
| 3086 | - if (! $this->ID()) { |
|
| 3086 | + if ( ! $this->ID()) { |
|
| 3087 | 3087 | $this->save(); |
| 3088 | 3088 | } |
| 3089 | 3089 | global $wpdb; |
@@ -3313,7 +3313,7 @@ discard block |
||
| 3313 | 3313 | $model = $this->get_model(); |
| 3314 | 3314 | foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
| 3315 | 3315 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
| 3316 | - $classname = 'EE_' . $model->get_this_model_name(); |
|
| 3316 | + $classname = 'EE_'.$model->get_this_model_name(); |
|
| 3317 | 3317 | if ($this->get_one_from_cache($relation_name) instanceof $classname |
| 3318 | 3318 | && $this->get_one_from_cache($relation_name)->ID() |
| 3319 | 3319 | ) { |
@@ -3353,7 +3353,7 @@ discard block |
||
| 3353 | 3353 | // handle DateTimes (this is handled in here because there's no one specific child class that uses datetimes). |
| 3354 | 3354 | foreach ($this->_fields as $field => $value) { |
| 3355 | 3355 | if ($value instanceof DateTime) { |
| 3356 | - $this->_fields[ $field ] = clone $value; |
|
| 3356 | + $this->_fields[$field] = clone $value; |
|
| 3357 | 3357 | } |
| 3358 | 3358 | } |
| 3359 | 3359 | } |
@@ -38,103 +38,103 @@ |
||
| 38 | 38 | * @since 4.0 |
| 39 | 39 | */ |
| 40 | 40 | if (function_exists('espresso_version')) { |
| 41 | - if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | - /** |
|
| 43 | - * espresso_duplicate_plugin_error |
|
| 44 | - * displays if more than one version of EE is activated at the same time |
|
| 45 | - */ |
|
| 46 | - function espresso_duplicate_plugin_error() |
|
| 47 | - { |
|
| 48 | - ?> |
|
| 41 | + if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | + /** |
|
| 43 | + * espresso_duplicate_plugin_error |
|
| 44 | + * displays if more than one version of EE is activated at the same time |
|
| 45 | + */ |
|
| 46 | + function espresso_duplicate_plugin_error() |
|
| 47 | + { |
|
| 48 | + ?> |
|
| 49 | 49 | <div class="error"> |
| 50 | 50 | <p> |
| 51 | 51 | <?php |
| 52 | - echo esc_html__( |
|
| 53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | - 'event_espresso' |
|
| 55 | - ); ?> |
|
| 52 | + echo esc_html__( |
|
| 53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | + 'event_espresso' |
|
| 55 | + ); ?> |
|
| 56 | 56 | </p> |
| 57 | 57 | </div> |
| 58 | 58 | <?php |
| 59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 63 | 63 | } else { |
| 64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
| 65 | - if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | - /** |
|
| 67 | - * espresso_minimum_php_version_error |
|
| 68 | - * |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - function espresso_minimum_php_version_error() |
|
| 72 | - { |
|
| 73 | - ?> |
|
| 64 | + define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
| 65 | + if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | + /** |
|
| 67 | + * espresso_minimum_php_version_error |
|
| 68 | + * |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + function espresso_minimum_php_version_error() |
|
| 72 | + { |
|
| 73 | + ?> |
|
| 74 | 74 | <div class="error"> |
| 75 | 75 | <p> |
| 76 | 76 | <?php |
| 77 | - printf( |
|
| 78 | - esc_html__( |
|
| 79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | - 'event_espresso' |
|
| 81 | - ), |
|
| 82 | - EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | - PHP_VERSION, |
|
| 84 | - '<br/>', |
|
| 85 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 86 | - ); |
|
| 87 | - ?> |
|
| 77 | + printf( |
|
| 78 | + esc_html__( |
|
| 79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | + 'event_espresso' |
|
| 81 | + ), |
|
| 82 | + EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | + PHP_VERSION, |
|
| 84 | + '<br/>', |
|
| 85 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 86 | + ); |
|
| 87 | + ?> |
|
| 88 | 88 | </p> |
| 89 | 89 | </div> |
| 90 | 90 | <?php |
| 91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | - } |
|
| 91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | - } else { |
|
| 96 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | - /** |
|
| 98 | - * espresso_version |
|
| 99 | - * Returns the plugin version |
|
| 100 | - * |
|
| 101 | - * @return string |
|
| 102 | - */ |
|
| 103 | - function espresso_version() |
|
| 104 | - { |
|
| 105 | - return apply_filters('FHEE__espresso__espresso_version', '4.9.83.rc.013'); |
|
| 106 | - } |
|
| 94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | + } else { |
|
| 96 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | + /** |
|
| 98 | + * espresso_version |
|
| 99 | + * Returns the plugin version |
|
| 100 | + * |
|
| 101 | + * @return string |
|
| 102 | + */ |
|
| 103 | + function espresso_version() |
|
| 104 | + { |
|
| 105 | + return apply_filters('FHEE__espresso__espresso_version', '4.9.83.rc.013'); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * espresso_plugin_activation |
|
| 110 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 111 | - */ |
|
| 112 | - function espresso_plugin_activation() |
|
| 113 | - { |
|
| 114 | - update_option('ee_espresso_activation', true); |
|
| 115 | - } |
|
| 108 | + /** |
|
| 109 | + * espresso_plugin_activation |
|
| 110 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 111 | + */ |
|
| 112 | + function espresso_plugin_activation() |
|
| 113 | + { |
|
| 114 | + update_option('ee_espresso_activation', true); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 117 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 118 | 118 | |
| 119 | - require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 120 | - bootstrap_espresso(); |
|
| 121 | - } |
|
| 119 | + require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 120 | + bootstrap_espresso(); |
|
| 121 | + } |
|
| 122 | 122 | } |
| 123 | 123 | if (! function_exists('espresso_deactivate_plugin')) { |
| 124 | - /** |
|
| 125 | - * deactivate_plugin |
|
| 126 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 127 | - * |
|
| 128 | - * @access public |
|
| 129 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
| 133 | - { |
|
| 134 | - if (! function_exists('deactivate_plugins')) { |
|
| 135 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 136 | - } |
|
| 137 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
| 138 | - deactivate_plugins($plugin_basename); |
|
| 139 | - } |
|
| 124 | + /** |
|
| 125 | + * deactivate_plugin |
|
| 126 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 127 | + * |
|
| 128 | + * @access public |
|
| 129 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
| 133 | + { |
|
| 134 | + if (! function_exists('deactivate_plugins')) { |
|
| 135 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 136 | + } |
|
| 137 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
| 138 | + deactivate_plugins($plugin_basename); |
|
| 139 | + } |
|
| 140 | 140 | } |