Completed
Branch FET/11174/bot-detection-middle... (17f260)
by
unknown
93:18 queued 82:13
created

minimumWordPressVersionRequired()   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
3
namespace EventEspresso\core\services\request_stack\middleware;
4
5
use EE_Request;
6
use EE_Response;
7
use EventEspresso\core\domain\entities\notifications\PersistentAdminNotice;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
10
defined('EVENT_ESPRESSO_VERSION') || exit;
11
12
13
14
/**
15
 * Class RecommendedVersions
16
 * checks required and recommended versions for both WP and PHP
17
 * terminates the request if minimum required versions are not met
18
 *
19
 * @package EventEspresso\core\services\request_stack\middleware
20
 * @author  Brent Christensen
21
 * @since   4.9.52
22
 */
23
class RecommendedVersions extends Middleware
24
{
25
26
    /**
27
     * converts a Request to a Response
28
     *
29
     * @param EE_Request  $request
30
     * @param EE_Response $response
31
     * @return EE_Response
32
     * @throws InvalidDataTypeException
33
     */
34
    public function handle_request(EE_Request $request, EE_Response $response)
35
    {
36
        $this->request  = $request;
37
        $this->response = $response;
38
        // check required WP version
39
        if (! $this->minimumWordPressVersionRequired()) {
40
            $this->request->un_set('activate', true);
41
            add_action('admin_notices', array($this, 'minimum_wp_version_error'), 1);
42
            $this->response->terminate_request();
43
            $this->response->deactivate_plugin();
44
        }
45
        // check recommended PHP version
46
        if (! $this->minimumPhpVersionRecommended()) {
47
            $this->displayMinimumRecommendedPhpVersionNotice();
48
        }
49
        $this->response = $this->process_request_stack($this->request, $this->response);
50
        return $this->response;
51
    }
52
53
54
    /**
55
     * Helper method to assess installed wp version against given values.
56
     * By default this compares the required minimum version of WP for EE against the installed version of WP
57
     * Note, $wp_version is the first parameter sent into the PHP version_compare function (what is being checked
58
     * against) so consider that when sending in your values.
59
     *
60
     * @param string $version_to_check
61
     * @param string $operator
62
     * @return bool
63
     */
64
    public static function compareWordPressVersion($version_to_check = EE_MIN_WP_VER_REQUIRED, $operator = '>=')
65
    {
66
        global $wp_version;
67
        return version_compare(
68
        // first account for wp_version being pre-release
69
        // (like RC, beta etc) which are usually in the format like 4.7-RC3-39519
70
            strpos($wp_version, '-') > 0
71
                ? substr($wp_version, 0, strpos($wp_version, '-'))
72
                : $wp_version,
73
            $version_to_check,
74
            $operator
75
        );
76
    }
77
78
79
80
    /**
81
     * @return boolean
82
     */
83
    private function minimumWordPressVersionRequired()
84
    {
85
        return RecommendedVersions::compareWordPressVersion();
86
    }
87
88
89
90
    /**
91
     * @param string $min_version
92
     * @return boolean
93
     */
94
    private function checkPhpVersion($min_version = EE_MIN_PHP_VER_RECOMMENDED)
95
    {
96
        return version_compare(PHP_VERSION, $min_version, '>=') ? true : false;
97
    }
98
99
100
101
    /**
102
     * @return boolean
103
     */
104
    private function minimumPhpVersionRecommended()
105
    {
106
        return $this->checkPhpVersion();
107
    }
108
109
110
111
    /**
112
     * @return void
113
     */
114
    public function minimumWpVersionError()
115
    {
116
        global $wp_version;
117
        ?>
118
        <div class="error">
119
            <p>
120
                <?php
121
                printf(
122
                    __('We\'re sorry, but Event Espresso requires WordPress version %1$s or greater in order to operate. You are currently running version %2$s.%3$sFor information on how to update your version of WordPress, please go to %4$s.',
123
                        'event_espresso'),
124
                    EE_MIN_WP_VER_REQUIRED,
125
                    $wp_version,
126
                    '<br/>',
127
                    '<a href="http://codex.wordpress.org/Updating_WordPress">http://codex.wordpress.org/Updating_WordPress</a>'
128
                );
129
                ?>
130
            </p>
131
        </div>
132
        <?php
133
    }
134
135
136
137
    /**
138
     *    _display_minimum_recommended_php_version_notice
139
     *
140
     * @access private
141
     * @return void
142
     * @throws InvalidDataTypeException
143
     */
144
    private function displayMinimumRecommendedPhpVersionNotice()
145
    {
146
        if ($this->request->isAdmin()) {
147
            new PersistentAdminNotice(
148
                'php_version_' . str_replace('.', '-', EE_MIN_PHP_VER_RECOMMENDED) . '_recommended',
149
                sprintf(
150
                    esc_html__(
151
                        'Event Espresso recommends PHP version %1$s or greater for optimal performance. 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.',
152
                        'event_espresso'
153
                    ),
154
                    EE_MIN_PHP_VER_RECOMMENDED,
155
                    PHP_VERSION,
156
                    '<br/>',
157
                    '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
158
                )
159
            );
160
        }
161
    }
162
163
}
164
// Location: RecommendedVersions.php
165