Completed
Branch BUG-10626-dst-unit-test (cc62a6)
by
unknown
37:15 queued 23:58
created

EED_Thank_You_Page   F

Complexity

Total Complexity 100

Size/Duplication

Total Lines 970
Duplicated Lines 5.15 %

Coupling/Cohesion

Components 1
Dependencies 21

Importance

Changes 0
Metric Value
dl 50
loc 970
rs 1.263
c 0
b 0
f 0
wmc 100
lcom 1
cbo 21

25 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 4 1
A set_hooks() 0 4 1
A set_hooks_admin() 0 23 1
A set_definitions() 0 5 1
B get_txn() 0 32 5
A get_txn_payments() 0 12 3
A _get_reg_url_link() 12 21 4
A set_reg_url_link() 0 4 2
A run() 0 4 1
A load_resources() 0 11 2
B _translate_strings() 0 31 1
A load_js() 12 12 1
F init() 0 85 19
B thank_you_page_results() 0 32 5
D thank_you_page_IPN_monitor() 0 100 17
A _update_server_wait_time() 0 10 2
B get_registration_details() 0 25 1
B resend_reg_confirmation_email() 0 50 4
C get_ajax_content() 0 23 7
B display_details_for_events() 0 35 4
B display_details_for_events_requiring_pre_approval() 26 29 4
B get_transaction_details() 0 27 1
C get_payment_row_html() 0 40 7
B get_payment_details() 0 48 4
A get_new_payments() 0 9 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like EED_Thank_You_Page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EED_Thank_You_Page, and based on these observations, apply Extract Interface, too.

1
<?php
2
defined('EVENT_ESPRESSO_VERSION') || exit;
3
4
5
6
/**
7
 * Class EED_Thank_You_Page
8
 * Description
9
 *
10
 * @package       Event Espresso
11
 * @author        Brent Christensen
12
 * @since         $VID:$
13
 */
14
class EED_Thank_You_Page extends EED_Module
15
{
16
17
    /**
18
     * time in seconds to wait for the IPN to arrive before telling the registrant to bugger off ( 1200s = 20 minutes )
19
     */
20
    const IPN_wait_time = 1200;
21
22
    /**
23
     * The transaction specified by the reg_url_link passed from the Request, or from the Session
24
     *
25
     * @var EE_Transaction $_current_txn
26
     */
27
    private $_current_txn;
28
29
    /**
30
     * @var EE_Registration $_primary_registrant
31
     */
32
    private $_primary_registrant;
33
34
    /**
35
     * The reg_url_link passed from the Request, or from the Session
36
     *
37
     * @var string $_reg_url_link
38
     */
39
    private $_reg_url_link;
40
41
    /**
42
     * whether the incoming reg_url_link is for the primary registrant or not
43
     *
44
     * @var boolean $_is_primary
45
     */
46
    private $_is_primary;
47
48
    /**
49
     * The URL for revisiting the SPCO attendee information step
50
     *
51
     * @var string $_SPCO_attendee_information_url
52
     */
53
    private $_SPCO_attendee_information_url;
54
55
    /**
56
     * The URL for revisiting the SPCO payment options step
57
     *
58
     * @var string $_SPCO_payment_options_url
59
     */
60
    private $_SPCO_payment_options_url;
61
62
    /**
63
     * whether to display the Payment Options link
64
     *
65
     * @var boolean $_show_try_pay_again_link
66
     */
67
    private $_show_try_pay_again_link = false;
68
69
    /**
70
     * whether payments are allowed at this time
71
     *
72
     * @var boolean $_payments_closed
73
     */
74
    private $_payments_closed = false;
75
76
    /**
77
     * whether the selected payment method is Bank, Check , Invoice, etc
78
     *
79
     * @var boolean $_is_offline_payment_method
80
     */
81
    private $_is_offline_payment_method = true;
82
83
84
85
    /**
86
     * @return EED_Module|EED_Thank_You_Page
87
     */
88
    public static function instance()
89
    {
90
        return parent::get_instance(__CLASS__);
91
    }
92
93
94
    /**
95
     * set_hooks - for hooking into EE Core, modules, etc
96
     *
97
     * @return void
98
     */
99
    public static function set_hooks()
100
    {
101
        add_action('wp_loaded', array('EED_Thank_You_Page', 'set_definitions'), 2);
102
    }
103
104
105
106
    /**
107
     * set_hooks_admin - for hooking into EE Admin Core, modules, etc
108
     *
109
     * @return void
110
     */
111
    public static function set_hooks_admin()
112
    {
113
        // AJAX for IPN monitoring
114
        add_filter('heartbeat_received', array('EED_Thank_You_Page', 'thank_you_page_IPN_monitor'), 10, 3);
115
        add_filter(
116
            'heartbeat_nopriv_received',
117
            array('EED_Thank_You_Page', 'thank_you_page_IPN_monitor'),
118
            10,
119
            3
120
        );
121
        add_action(
122
            'wp_ajax_espresso_resend_reg_confirmation_email',
123
            array('EED_Thank_You_Page', 'resend_reg_confirmation_email'),
124
            10,
125
            2
126
        );
127
        add_action(
128
            'wp_ajax_nopriv_espresso_resend_reg_confirmation_email',
129
            array('EED_Thank_You_Page', 'resend_reg_confirmation_email'),
130
            10,
131
            2
132
        );
133
    }
134
135
136
137
    /**
138
     * set_definitions
139
     *
140
     * @return void
141
     */
142
    public static function set_definitions()
143
    {
144
        define('THANK_YOU_ASSETS_URL', plugin_dir_url(__FILE__) . 'assets' . DS);
145
        define('THANK_YOU_TEMPLATES_PATH', str_replace('\\', DS, plugin_dir_path(__FILE__)) . 'templates' . DS);
146
    }
147
148
149
150
    /**
151
     * get_txn
152
     *
153
     * @return EE_Transaction
154
     */
155
    public function get_txn()
156
    {
157
        if ($this->_current_txn instanceof EE_Transaction) {
158
            return $this->_current_txn;
159
        }
160
        $TXN_model = EE_Registry::instance()->load_model('Transaction');
161
        if ( ! $TXN_model instanceof EEM_Transaction) {
162
            EE_Error::add_error(
163
                __('The transaction model could not be established.', 'event_espresso'),
164
                __FILE__,
165
                __FUNCTION__,
166
                __LINE__
167
            );
168
            return null;
169
        }
170
        //get the transaction. yes, we may have just loaded it, but it may have been updated, or this may be via an ajax request
171
        $this->_current_txn = $TXN_model->get_transaction_from_reg_url_link($this->_reg_url_link);
172
        // verify TXN
173
        if (WP_DEBUG && ! $this->_current_txn instanceof EE_Transaction) {
174
            EE_Error::add_error(
175
                __(
176
                    'No transaction information could be retrieved or the transaction data is not of the correct type.',
177
                    'event_espresso'
178
                ),
179
                __FILE__,
180
                __FUNCTION__,
181
                __LINE__
182
            );
183
            return null;
184
        }
185
        return $this->_current_txn;
186
    }
187
188
189
190
    /**
191
     * get_txn_payments
192
     *
193
     * @param int $since
194
     * @return mixed array of EE_Payment || FALSE
195
     * @throws \EE_Error
196
     */
197
    public function get_txn_payments($since = 0)
198
    {
199
        if ( ! $this->get_txn()) {
200
            return false;
201
        }
202
        $args = array('order_by' => array('PAY_timestamp' => 'ASC'));
203
        if ($since > 0) {
204
            $args[0] = array('PAY_timestamp' => array('>', $since));
205
        }
206
        // get array of payments with most recent first
207
        return $this->_current_txn->payments($args);
208
    }
209
210
211
212
    /**
213
     * get_reg_url_link
214
     *
215
     * @return void
216
     */
217
    private function _get_reg_url_link()
218
    {
219
        if ( ! empty($this->_reg_url_link)) {
220
            return;
221
        }
222
        // only do thank you page stuff if we have a REG_url_link in the url
223 View Code Duplication
        if (WP_DEBUG && ! EE_Registry::instance()->REQ->is_set('e_reg_url_link')) {
224
            EE_Error::add_error(
225
                __(
226
                    'No transaction information could be retrieved because the registration URL link is missing or invalid.',
227
                    'event_espresso'
228
                ),
229
                __FILE__,
230
                __FUNCTION__,
231
                __LINE__
232
            );
233
            return;
234
        }
235
        // check for reg_url_link
236
        $this->_reg_url_link = EE_Registry::instance()->REQ->get('e_reg_url_link');
237
    }
238
239
240
241
    /**
242
     * set_reg_url_link
243
     *
244
     * @param string $reg_url_link
245
     */
246
    public function set_reg_url_link($reg_url_link = null)
247
    {
248
        $this->_reg_url_link = ! empty($reg_url_link) ? $reg_url_link : $this->_reg_url_link;
249
    }
250
251
252
253
    /**
254
     * run - initial module setup
255
     * this method is primarily used for activating resources in the EE_Front_Controller thru the use of filters
256
     *
257
     * @param WP $WP
258
     * @return void
259
     * @throws \EE_Error
260
     */
261
    public function run($WP)
262
    {
263
264
    }
265
266
267
268
    /**
269
     * load_resources
270
     *
271
     * @return void
272
     * @throws \EE_Error
273
     */
274
    public function load_resources() {
275
        $this->_get_reg_url_link();
276
        // resend_reg_confirmation_email ?
277
        if (EE_Registry::instance()->REQ->is_set('resend')) {
278
            EED_Thank_You_Page::resend_reg_confirmation_email();
279
        }
280
        EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__);
281
        $this->_translate_strings();
282
        // load assets
283
        add_action('wp_enqueue_scripts', array($this, 'load_js'), 10);
284
    }
285
286
287
288
    /**
289
     * load_js
290
     *
291
     * @return void
292
     */
293
    protected function _translate_strings()
294
    {
295
        EE_Registry::$i18n_js_strings['e_reg_url_link'] = $this->_reg_url_link;
296
        EE_Registry::$i18n_js_strings['initial_access'] = time();
297
        EE_Registry::$i18n_js_strings['IPN_wait_time'] = EED_Thank_You_Page::IPN_wait_time;
298
        EE_Registry::$i18n_js_strings['TXN_complete'] = EEM_Transaction::complete_status_code;
299
        EE_Registry::$i18n_js_strings['TXN_incomplete'] = EEM_Transaction::incomplete_status_code;
300
        EE_Registry::$i18n_js_strings['checking_for_new_payments'] = __(
301
            'checking for new payments...',
302
            'event_espresso'
303
        );
304
        EE_Registry::$i18n_js_strings['loading_payment_info'] = __(
305
            'loading payment information...',
306
            'event_espresso'
307
        );
308
        EE_Registry::$i18n_js_strings['server_error'] = __(
309
            'An unknown error occurred on the server while attempting to process your request. Please refresh the page and try again.',
310
            'event_espresso'
311
        );
312
        EE_Registry::$i18n_js_strings['slow_IPN'] = apply_filters(
313
            'EED_Thank_You_Page__load_js__slow_IPN',
314
            sprintf(
315
                __(
316
                    '%sThe Payment Notification appears to be taking longer than usual to arrive. Maybe check back later or just wait for your payment and registration confirmation results to be sent to you via email. We apologize for any inconvenience this may have caused.%s',
317
                    'event_espresso'
318
                ),
319
                '<div id="espresso-thank-you-page-slow-IPN-dv" class="ee-attention jst-left">',
320
                '</div>'
321
            )
322
        );
323
    }
324
325
326
327
    /**
328
     * load_js
329
     *
330
     * @return void
331
     */
332 View Code Duplication
    public function load_js()
333
    {
334
        wp_register_script(
335
            'thank_you_page',
336
            THANK_YOU_ASSETS_URL . 'thank_you_page.js',
337
            array('espresso_core', 'heartbeat'),
338
            EVENT_ESPRESSO_VERSION,
339
            true
340
        );
341
        wp_enqueue_script('thank_you_page');
342
        wp_enqueue_style('espresso_default');
343
    }
344
345
346
347
    /**
348
     * init
349
     *
350
     * @return void
351
     * @throws \EE_Error
352
     */
353
    public function init()
354
    {
355
        $this->_get_reg_url_link();
356
        if ( ! $this->get_txn()) {
357
            echo EEH_HTML::div(
358
                EEH_HTML::h4(__('We\'re sorry...', 'event_espresso'), '', '') .
359
                sprintf(
360
                    __(
361
                        'This is a system page for displaying transaction information after a purchase.%1$sYou are most likely seeing this notice because you have navigated to this page%1$sthrough some means other than completing a transaction.%1$sSorry for the disappointment, but you will most likely find nothing of interest here.%1$s%1$s',
362
                        'event_espresso'
363
                    ),
364
                    '<br/>'
365
                ),
366
                '',
367
                'ee-attention'
368
            );
369
            return null;
370
        }
371
        // if we've made it to the Thank You page, then let's toggle any "Failed" transactions to "Incomplete"
372
        if ($this->_current_txn->status_ID() === EEM_Transaction::failed_status_code) {
373
            $this->_current_txn->set_status(EEM_Transaction::incomplete_status_code);
374
            $this->_current_txn->save();
375
        }
376
        $this->_primary_registrant = $this->_current_txn->primary_registration() instanceof EE_Registration
377
            ? $this->_current_txn->primary_registration()
378
            : null;
379
        $this->_is_primary = $this->_primary_registrant->reg_url_link() === $this->_reg_url_link ? true : false;
380
        $show_try_pay_again_link_default = apply_filters(
381
            'AFEE__EED_Thank_You_Page__init__show_try_pay_again_link_default',
382
            true
383
        );
384
        $this->_show_try_pay_again_link = $show_try_pay_again_link_default;
385
        // txn status ?
386
        if ($this->_current_txn->is_completed()) {
387
            $this->_show_try_pay_again_link = $show_try_pay_again_link_default;
388
        } else if (
389
            $this->_current_txn->is_incomplete()
390
            && ($this->_primary_registrant->is_approved()
391
                || $this->_primary_registrant->is_pending_payment())
392
        ) {
393
            $this->_show_try_pay_again_link = true;
394
        } else if ($this->_primary_registrant->is_approved() || $this->_primary_registrant->is_pending_payment()) {
395
            // its pending
396
            $this->_show_try_pay_again_link = isset(
397
                                                  EE_Registry::instance()->CFG->registration->show_pending_payment_options
398
                                              )
399
                                              && EE_Registry::instance()->CFG->registration->show_pending_payment_options
400
                ? true
401
                : $show_try_pay_again_link_default;
402
        }
403
        $this->_payments_closed = ! $this->_current_txn->payment_method() instanceof EE_Payment_Method
404
            ? true
405
            : false;
406
        $this->_is_offline_payment_method = false;
407
        if (
408
            // if payment method is unknown
409
            ! $this->_current_txn->payment_method() instanceof EE_Payment_Method
410
            || (
411
                // or is an offline payment method
412
                $this->_current_txn->payment_method() instanceof EE_Payment_Method
413
                && $this->_current_txn->payment_method()->is_off_line()
414
            )
415
        ) {
416
            $this->_is_offline_payment_method = true;
417
        }
418
        // link to SPCO
419
        $revisit_spco_url = add_query_arg(
420
            array('ee' => '_register', 'revisit' => true, 'e_reg_url_link' => $this->_reg_url_link),
421
            EE_Registry::instance()->CFG->core->reg_page_url()
422
        );
423
        // link to SPCO payment_options
424
        $this->_SPCO_payment_options_url = $this->_primary_registrant instanceof EE_Registration
425
            ? $this->_primary_registrant->payment_overview_url()
426
            : add_query_arg(
427
                array('step' => 'payment_options'),
428
                $revisit_spco_url
429
            );
430
        // link to SPCO attendee_information
431
        $this->_SPCO_attendee_information_url = $this->_primary_registrant instanceof EE_Registration
432
            ? $this->_primary_registrant->edit_attendee_information_url()
433
            : false;
434
        do_action('AHEE__EED_Thank_You_Page__init_end', $this->_current_txn);
435
        // set no cache headers and constants
436
        EE_System::do_not_cache();
437
    }
438
439
440
441
    /**
442
     * display_thank_you_page_results
443
     *
444
     * @return string
445
     * @throws \EE_Error
446
     */
447
    public function thank_you_page_results()
448
    {
449
        $this->init();
450
        if ( ! $this->_current_txn instanceof EE_Transaction) {
451
            return EE_Error::get_notices();
452
        }
453
        // link to receipt
454
        $template_args['TXN_receipt_url'] = $this->_current_txn->receipt_url('html');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$template_args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $template_args = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
455
        if ( ! empty($template_args['TXN_receipt_url'])) {
456
            $template_args['order_conf_desc'] = __(
457
                '%1$sCongratulations%2$sYour registration has been successfully processed.%3$sCheck your email for your registration confirmation or click the button below to view / download / print a full description of your purchases and registration information.',
458
                'event_espresso'
459
            );
460
        } else {
461
            $template_args['order_conf_desc'] = __(
462
                '%1$sCongratulations%2$sYour registration has been successfully processed.%3$sCheck your email for your registration confirmation.',
463
                'event_espresso'
464
            );
465
        }
466
        $template_args['transaction'] = $this->_current_txn;
467
        $template_args['revisit'] = EE_Registry::instance()->REQ->get('revisit', false);
468
        add_action('AHEE__thank_you_page_overview_template__content', array($this, 'get_registration_details'));
469
        if ($this->_is_primary && ! $this->_current_txn->is_free()) {
470
            add_action('AHEE__thank_you_page_overview_template__content', array($this, 'get_ajax_content'));
471
        }
472
        return EEH_Template::locate_template(
473
            THANK_YOU_TEMPLATES_PATH . 'thank-you-page-overview.template.php',
474
            $template_args,
475
            true,
476
            true
477
        );
478
    }
479
480
481
482
    /**
483
     * thank_you_page_IPN_monitor
484
     * this basically just pulls the TXN based on the reg_url_link sent from the server,
485
     * then checks that the TXN status is not failed, and that no other errors have been generated.
486
     * it also calculates the IPN wait time since the Thank You page was first loaded
487
     *
488
     * @param array $response
489
     * @param array $data
490
     * @return array
491
     * @throws \EE_Error
492
     */
493
    public static function thank_you_page_IPN_monitor($response = array(), $data = array())
494
    {
495
        // does this heartbeat contain our data ?
496
        if ( ! isset($data['espresso_thank_you_page'])) {
497
            return $response;
498
        }
499
        // check for reg_url_link in the incoming heartbeat data
500
        if ( ! isset($data['espresso_thank_you_page']['e_reg_url_link'])) {
501
            $response['espresso_thank_you_page'] = array(
502
                'errors' => ! empty($notices['errors'])
503
                    ? $notices['errors']
504
                    : __(
505
                        'No transaction information could be retrieved because the registration URL link is missing or invalid.',
506
                        'event_espresso'
507
                    )
508
            );
509
            return $response;
510
        }
511
        // kk heartbeat has our data
512
        $response['espresso_thank_you_page'] = array();
513
        // set_definitions, instantiate the thank you page class, and get the ball rolling
514
        EED_Thank_You_Page::set_definitions();
515
        /** @var $espresso_thank_you_page EED_Thank_You_Page */
516
        $espresso_thank_you_page = EED_Thank_You_Page::instance();
517
        $espresso_thank_you_page->set_reg_url_link($data['espresso_thank_you_page']['e_reg_url_link']);
518
        $espresso_thank_you_page->init();
519
        //get TXN
520
        $TXN = $espresso_thank_you_page->get_txn();
521
        // no TXN? then get out
522
        if ( ! $TXN instanceof EE_Transaction) {
523
            $notices = EE_Error::get_notices();
524
            $response['espresso_thank_you_page'] = array(
525
                'errors' => ! empty($notices['errors'])
526
                    ? $notices['errors']
527
                    : sprintf(
528
                        __(
529
                            'The information for your transaction could not be retrieved from the server or the transaction data received was invalid because of a technical reason. (%s)',
530
                            'event_espresso'
531
                        ),
532
                        __LINE__
533
                    )
534
            );
535
            return $response;
536
        }
537
        // grab transient of TXN's status
538
        $txn_status = isset($data['espresso_thank_you_page']['txn_status'])
539
            ? $data['espresso_thank_you_page']['txn_status']
540
            : null;
541
        // has the TXN status changed since we last checked (or empty because this is the first time running through this code)?
542
        if ($txn_status !== $TXN->status_ID()) {
543
            // switch between two possible basic outcomes
544
            switch ($TXN->status_ID()) {
545
                // TXN has been updated in some way
546
                case EEM_Transaction::overpaid_status_code:
547
                case EEM_Transaction::complete_status_code:
548
                case EEM_Transaction::incomplete_status_code:
549
                    // send updated TXN results back to client,
550
                    $response['espresso_thank_you_page'] = array(
551
                        'transaction_details' => $espresso_thank_you_page->get_transaction_details(),
552
                        'txn_status'          => $TXN->status_ID()
553
                    );
554
                    break;
555
                // or we have a bad TXN, or really slow IPN, so calculate the wait time and send that back...
556
                case EEM_Transaction::failed_status_code:
557
                default:
558
                    // keep on waiting...
559
                    return $espresso_thank_you_page->_update_server_wait_time($data['espresso_thank_you_page']);
560
            }
561
            // or is the TXN still failed (never been updated) ???
562
        } else if ($TXN->failed()) {
563
            // keep on waiting...
564
            return $espresso_thank_you_page->_update_server_wait_time($data['espresso_thank_you_page']);
565
        }
566
        // TXN is happening so let's get the payments now
567
        // if we've already gotten payments then the heartbeat data will contain the timestamp of the last time we checked
568
        $since = isset($data['espresso_thank_you_page']['get_payments_since'])
569
            ? $data['espresso_thank_you_page']['get_payments_since']
570
            : 0;
571
        // then check for payments
572
        $payments = $espresso_thank_you_page->get_txn_payments($since);
573
        // has a payment been processed ?
574
        if ( ! empty($payments) || $espresso_thank_you_page->_is_offline_payment_method) {
575
            if ($since) {
576
                $response['espresso_thank_you_page'] = array(
577
                    'new_payments'        => $espresso_thank_you_page->get_new_payments($payments),
578
                    'transaction_details' => $espresso_thank_you_page->get_transaction_details(),
579
                    'txn_status'          => $TXN->status_ID()
580
                );
581
            } else {
582
                $response['espresso_thank_you_page']['payment_details'] = $espresso_thank_you_page->get_payment_details(
583
                    $payments
584
                );
585
            }
586
            // reset time to check for payments
587
            $response['espresso_thank_you_page']['get_payments_since'] = time();
588
        } else {
589
            $response['espresso_thank_you_page']['get_payments_since'] = $since;
590
        }
591
        return $response;
592
    }
593
594
595
596
    /**
597
     * _update_server_wait_time
598
     *
599
     * @param array $thank_you_page_data thank you page portion of the incoming JSON array from the WP heartbeat data
600
     * @return array
601
     * @throws \EE_Error
602
     */
603
    private function _update_server_wait_time($thank_you_page_data = array())
604
    {
605
        $response['espresso_thank_you_page'] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
606
            'still_waiting' => isset($thank_you_page_data['initial_access'])
607
                ? time() - $thank_you_page_data['initial_access']
608
                : 0,
609
            'txn_status'    => $this->_current_txn->status_ID()
610
        );
611
        return $response;
612
    }
613
614
615
616
    /**
617
     * get_registration_details
618
     *
619
     * @throws \EE_Error
620
     */
621
    public function get_registration_details()
622
    {
623
        //prepare variables for displaying
624
        $template_args = array();
625
        $template_args['transaction'] = $this->_current_txn;
626
        $template_args['reg_url_link'] = $this->_reg_url_link;
627
        $template_args['is_primary'] = $this->_is_primary;
628
        $template_args['SPCO_attendee_information_url'] = $this->_SPCO_attendee_information_url;
629
        $template_args['resend_reg_confirmation_url'] = add_query_arg(
630
            array('token' => $this->_reg_url_link, 'resend_reg_confirmation' => 'true'),
631
            EE_Registry::instance()->CFG->core->thank_you_page_url()
632
        );
633
        // verify template arguments
634
        EEH_Template_Validator::verify_instanceof($template_args['transaction'], '$transaction', 'EE_Transaction');
635
        EEH_Template_Validator::verify_isnt_null(
636
            $template_args['SPCO_attendee_information_url'],
637
            '$SPCO_attendee_information_url'
638
        );
639
        echo EEH_Template::locate_template(
640
            THANK_YOU_TEMPLATES_PATH . 'thank-you-page-registration-details.template.php',
641
            $template_args,
642
            true,
643
            true
644
        );
645
    }
646
647
648
649
    /**
650
     * resend_reg_confirmation_email
651
     *
652
     * @throws \EE_Error
653
     */
654
    public static function resend_reg_confirmation_email()
655
    {
656
        EE_Registry::instance()->load_core('Request_Handler');
657
        $reg_url_link = EE_Registry::instance()->REQ->get('token');
658
        // was a REG_ID passed ?
659
        if ($reg_url_link) {
660
            $registration = EE_Registry::instance()->load_model('Registration')->get_one(
661
                array(array('REG_url_link' => $reg_url_link))
662
            );
663
            if ($registration instanceof EE_Registration) {
664
                // resend email
665
                EED_Messages::process_resend(array('_REG_ID' => $registration->ID()));
666
            } else {
667
                EE_Error::add_error(
668
                    __(
669
                        'The Registration Confirmation email could not be sent because a valid Registration could not be retrieved from the database.',
670
                        'event_espresso'
671
                    ),
672
                    __FILE__,
673
                    __FUNCTION__,
674
                    __LINE__
675
                );
676
            }
677
        } else {
678
            EE_Error::add_error(
679
                __(
680
                    'The Registration Confirmation email could not be sent because a registration token is missing or invalid.',
681
                    'event_espresso'
682
                ),
683
                __FILE__,
684
                __FUNCTION__,
685
                __LINE__
686
            );
687
        }
688
        // request sent via AJAX ?
689
        if (EE_FRONT_AJAX) {
690
            echo wp_json_encode(EE_Error::get_notices(false));
691
            die();
692
            // or was JS disabled ?
693
        } else {
694
            // save errors so that they get picked up on the next request
695
            EE_Error::get_notices(true, true);
696
            wp_safe_redirect(
697
                add_query_arg(
698
                    array('e_reg_url_link' => $reg_url_link),
699
                    EE_Registry::instance()->CFG->core->thank_you_page_url()
700
                )
701
            );
702
        }
703
    }
704
705
706
707
    /**
708
     * get_ajax_content
709
     *
710
     * @return void
711
     * @throws \EE_Error
712
     */
713
    public function get_ajax_content()
714
    {
715
        if ( ! $this->get_txn()) {
716
            return;
717
        }
718
        // first determine which event(s) require pre-approval or not
719
        $events = array();
720
        $events_requiring_pre_approval = array();
721
        foreach ($this->_current_txn->registrations() as $registration) {
722
            if ($registration instanceof EE_Registration) {
723
                $event = $registration->event();
724
                if ($event instanceof EE_Event) {
725
                    if ($registration->is_not_approved() && $registration->event() instanceof EE_Event) {
726
                        $events_requiring_pre_approval[$event->ID()] = $event;
727
                    } else {
728
                        $events[$event->ID()] = $event;
729
                    }
730
                }
731
            }
732
        }
733
        $this->display_details_for_events_requiring_pre_approval($events_requiring_pre_approval);
734
        $this->display_details_for_events($events);
735
    }
736
737
738
739
    /**
740
     * display_details_for_events
741
     *
742
     * @param EE_Event[] $events
743
     * @return void
744
     */
745
    public function display_details_for_events($events = array())
746
    {
747
        if ( ! empty($events)) {
748
            ?>
749
            <div id="espresso-thank-you-page-ajax-content-dv">
750
                <div id="espresso-thank-you-page-ajax-transaction-dv"></div>
751
                <div id="espresso-thank-you-page-ajax-payment-dv"></div>
752
                <div id="espresso-thank-you-page-ajax-loading-dv">
753
                    <div id="ee-ajax-loading-dv" class="float-left lt-blue-text">
754
                        <span class="dashicons dashicons-upload"></span><span id="ee-ajax-loading-msg-spn"><?php _e(
755
                                'loading transaction and payment information...',
756
                                'event_espresso'
757
                            ); ?></span>
758
                    </div>
759
                    <?php if ( ! $this->_is_offline_payment_method && ! $this->_payments_closed) : ?>
760
                        <p id="ee-ajax-loading-pg" class="highlight-bg small-text clear">
761
                            <?php echo apply_filters(
762
                                'EED_Thank_You_Page__get_ajax_content__waiting_for_IPN_msg',
763
                                __(
764
                                    'Some payment gateways can take 15 minutes or more to return their payment notification, so please be patient if you require payment confirmation as soon as possible. Please note that as soon as everything is finalized, we will send your full payment and registration confirmation results to you via email.',
765
                                    'event_espresso'
766
                                )
767
                            ); ?>
768
                            <br/>
769
                            <span class="jst-rght ee-block small-text lt-grey-text">
770
								<?php _e('current wait time ', 'event_espresso'); ?>
771
                                <span id="espresso-thank-you-page-ajax-time-dv">00:00:00</span></span>
772
                        </p>
773
                    <?php endif; ?>
774
                </div>
775
                <div class="clear"></div>
776
            </div>
777
            <?php
778
        }
779
    }
780
781
782
783
    /**
784
     * display_details_for_events_requiring_pre_approval
785
     *
786
     * @param EE_Event[] $events
787
     * @return void
788
     */
789
    public function display_details_for_events_requiring_pre_approval($events = array())
790
    {
791 View Code Duplication
        if ( ! empty($events)) {
792
            ?>
793
            <div id="espresso-thank-you-page-not-approved-message-dv">
794
                <h4 class="orange-text"><?php _e('Important Notice:', 'event_espresso'); ?></h4>
795
                <p id="events-requiring-pre-approval-pg" class="small-text">
796
                    <?php echo apply_filters(
797
                        'AHEE__EED_Thank_You_Page__get_ajax_content__not_approved_message',
798
                        __(
799
                            'The following Event(s) you have registered for do not require payment at this time and will not be billed for during this transaction. Billing will only occur after all attendees have 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.',
800
                            'event_espresso'
801
                        )
802
                    ); ?>
803
                </p>
804
                <ul class="events-requiring-pre-approval-ul">
805
                    <?php foreach ($events as $event) {
806
                        if ($event instanceof EE_Event) {
807
                            echo '<li><span class="dashicons dashicons-marker ee-icon-size-16 orange-text"></span>',
808
                            $event->name(),
809
                            '</li>';
810
                        }
811
                    } ?>
812
                </ul>
813
                <div class="clear"></div>
814
            </div>
815
            <?php
816
        }
817
    }
818
819
820
821
    /**
822
     * get_transaction_details
823
     *
824
     * @return string
825
     * @throws \EE_Error
826
     */
827
    public function get_transaction_details()
828
    {
829
        //prepare variables for displaying
830
        $template_args = array();
831
        $template_args['transaction'] = $this->_current_txn;
832
        $template_args['reg_url_link'] = $this->_reg_url_link;
833
        $template_args['primary_registrant_name'] = $this->_primary_registrant->attendee()->full_name(true);
834
        // link to SPCO payment_options
835
        $template_args['show_try_pay_again_link'] = $this->_show_try_pay_again_link;
836
        $template_args['SPCO_payment_options_url'] = $this->_SPCO_payment_options_url;
837
        // verify template arguments
838
        EEH_Template_Validator::verify_instanceof($template_args['transaction'], '$transaction', 'EE_Transaction');
839
        EEH_Template_Validator::verify_isnt_null(
840
            $template_args['show_try_pay_again_link'],
841
            '$show_try_pay_again_link'
842
        );
843
        EEH_Template_Validator::verify_isnt_null(
844
            $template_args['SPCO_payment_options_url'],
845
            '$SPCO_payment_options_url'
846
        );
847
        return EEH_Template::locate_template(
848
            THANK_YOU_TEMPLATES_PATH . 'thank-you-page-transaction-details.template.php',
849
            $template_args,
850
            true,
851
            true
852
        );
853
    }
854
855
856
857
    /**
858
     * get_payment_row_html
859
     *
860
     * @param EE_Payment $payment
861
     * @return string
862
     * @throws \EE_Error
863
     */
864
    public function get_payment_row_html($payment = null)
865
    {
866
        $html = '';
867
        if ($payment instanceof EE_Payment) {
868
            if (
869
                $payment->payment_method() instanceof EE_Payment_Method
870
                && $payment->status() === EEM_Payment::status_id_failed
871
                && $payment->payment_method()->is_off_site()
872
            ) {
873
                // considering the registrant has made it to the Thank You page,
874
                // any failed payments may actually be pending and the IPN is just slow
875
                // so let's
876
                $payment->set_status(EEM_Payment::status_id_pending);
877
            }
878
            $payment_declined_msg = $payment->STS_ID() === EEM_Payment::status_id_declined
879
                ? '<br /><span class="small-text">' . $payment->gateway_response() . '</span>'
880
                : '';
881
            $html .= '
882
				<tr>
883
					<td>
884
						' . $payment->timestamp() . '
885
					</td>
886
					<td>
887
						' . (
888
                $payment->payment_method() instanceof EE_Payment_Method
889
                    ? $payment->payment_method()->name()
890
                    : __('Unknown', 'event_espresso')
891
                ) . '
892
					</td>
893
					<td class="jst-rght">
894
						' . EEH_Template::format_currency($payment->amount()) . '
895
					</td>
896
					<td class="jst-rght" style="line-height:1;">
897
						' . $payment->pretty_status(true) . $payment_declined_msg . '
898
					</td>
899
				</tr>';
900
            do_action('AHEE__thank_you_page_payment_details_template__after_each_payment', $payment);
901
        }
902
        return $html;
903
    }
904
905
906
907
    /**
908
     * get_payment_details
909
     *
910
     * @param array $payments
911
     * @return string
912
     * @throws \EE_Error
913
     */
914
    public function get_payment_details($payments = array())
915
    {
916
        //prepare variables for displaying
917
        $template_args = array();
918
        $template_args['transaction'] = $this->_current_txn;
919
        $template_args['reg_url_link'] = $this->_reg_url_link;
920
        $template_args['payments'] = array();
921
        foreach ($payments as $payment) {
922
            $template_args['payments'][] = $this->get_payment_row_html($payment);
923
        }
924
        //create a hacky payment object, but dont save it
925
        $payment = EE_Payment::new_instance(
926
            array(
927
                'TXN_ID'        => $this->_current_txn->ID(),
928
                'STS_ID'        => EEM_Payment::status_id_pending,
929
                'PAY_timestamp' => time(),
930
                'PAY_amount'    => $this->_current_txn->total(),
931
                'PMD_ID'        => $this->_current_txn->payment_method_ID()
932
            )
933
        );
934
        $payment_method = $this->_current_txn->payment_method();
935
        if ($payment_method instanceof EE_Payment_Method && $payment_method->type_obj() instanceof EE_PMT_Base) {
936
            $template_args['gateway_content'] = $payment_method->type_obj()->payment_overview_content($payment);
937
        } else {
938
            $template_args['gateway_content'] = '';
939
        }
940
        // link to SPCO payment_options
941
        $template_args['show_try_pay_again_link'] = $this->_show_try_pay_again_link;
942
        $template_args['SPCO_payment_options_url'] = $this->_SPCO_payment_options_url;
943
        // verify template arguments
944
        EEH_Template_Validator::verify_instanceof($template_args['transaction'], '$transaction', 'EE_Transaction');
945
        EEH_Template_Validator::verify_isnt_null($template_args['payments'], '$payments');
946
        EEH_Template_Validator::verify_isnt_null(
947
            $template_args['show_try_pay_again_link'],
948
            '$show_try_pay_again_link'
949
        );
950
        EEH_Template_Validator::verify_isnt_null($template_args['gateway_content'], '$gateway_content');
951
        EEH_Template_Validator::verify_isnt_null(
952
            $template_args['SPCO_payment_options_url'],
953
            '$SPCO_payment_options_url'
954
        );
955
        return EEH_Template::locate_template(
956
            THANK_YOU_TEMPLATES_PATH . 'thank-you-page-payment-details.template.php',
957
            $template_args,
958
            true,
959
            true
960
        );
961
    }
962
963
964
965
    /**
966
     * get_payment_details
967
     *
968
     * @param array $payments
969
     * @return string
970
     * @throws \EE_Error
971
     */
972
    public function get_new_payments($payments = array())
973
    {
974
        $payments_html = '';
975
        //prepare variables for displaying
976
        foreach ($payments as $payment) {
977
            $payments_html .= $this->get_payment_row_html($payment);
978
        }
979
        return $payments_html;
980
    }
981
982
983
}
984
// End of file EED_Thank_You_Page.php
985
// Location: ${NAMESPACE}/EED_Thank_You_Page.php