Completed
Branch BUG-10381-asset-loading (2d15a5)
by
unknown
127:00 queued 116:19
created

EspressoThankYou::cacheExpiration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\domain\entities\shortcodes;
3
4
use EE_Registry;
5
use EventEspresso\core\services\shortcodes\EspressoShortcode;
6
use WP_Post;
7
8
defined('EVENT_ESPRESSO_VERSION') || exit;
9
10
11
12
/**
13
 * Class EspressoThankYou
14
 * ESPRESSO_THANK_YOU shortcode
15
 *
16
 * @package       Event Espresso
17
 * @author        Brent Christensen
18
 * @since         $VID:$
19
 */
20
class EspressoThankYou extends EspressoShortcode
21
{
22
23
    /**
24
     * @var boolean $is_thank_you_page
25
     */
26
    private $is_thank_you_page = false;
27
28
    /**
29
     * the actual shortcode tag that gets registered with WordPress
30
     *
31
     * @return string
32
     */
33
    public function getTag()
34
    {
35
        return 'ESPRESSO_THANK_YOU';
36
    }
37
38
39
40
    /**
41
     * the time in seconds to cache the results of the processShortcode() method
42
     * 0 means the processShortcode() results will NOT be cached at all
43
     *
44
     * @return int
45
     */
46
    public function cacheExpiration()
47
    {
48
        return 0;
49
    }
50
51
52
    /**
53
     * a place for adding any initialization code that needs to run prior to wp_header().
54
     * this may be required for shortcodes that utilize a corresponding module,
55
     * and need to enqueue assets for that module
56
     *
57
     * @return void
58
     * @throws \EE_Error
59
     */
60
    public function initializeShortcode()
61
    {
62
        global $wp_query;
63
        if (empty($wp_query->posts) || count($wp_query->posts) > 1) {
64
            return;
65
        }
66
        $post = reset($wp_query->posts);
67
        if ( ! $post instanceof WP_Post || $post->ID !== EE_Registry::instance()->CFG->core->thank_you_page_id ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
68
            return;
69
        }
70
        $this->is_thank_you_page = true;
71
        \EED_Thank_You_Page::instance()->load_resources();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class EED_Module as the method load_resources() does only exist in the following sub-classes of EED_Module: EED_Thank_You_Page. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
72
    }
73
74
75
76
    /**
77
     * callback that runs when the shortcode is encountered in post content.
78
     * IMPORTANT !!!
79
     * remember that shortcode content should be RETURNED and NOT echoed out
80
     *
81
     * @param array $attributes
82
     * @return string
83
     * @throws \EE_Error
84
     */
85
    public function processShortcode($attributes = array())
86
    {
87
        return $this->is_thank_you_page
88
            ? \EED_Thank_You_Page::instance()->thank_you_page_results()
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class EED_Module as the method thank_you_page_results() does only exist in the following sub-classes of EED_Module: EED_Thank_You_Page. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
89
            : '';
90
    }
91
92
93
94
}
95
// End of file EspressoThankYou.php
96
// Location: EventEspresso\core\domain\entities\shortcodes/EspressoThankYou.php