Completed
Branch FET/11399/verify-paypal-creden... (c7ad03)
by
unknown
66:22 queued 52:43
created

RecommendedVersions   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 185
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
B handleRequest() 0 22 4
A compareWordPressVersion() 0 13 2
A minimumWordPressVersionRequired() 0 4 1
A checkPhpVersion() 0 4 2
A minimumPhpVersionRecommended() 0 4 1
A minimumWpVersionError() 0 20 1
A displayMinimumRecommendedPhpVersionNotice() 0 18 2
A upcomingRequiredPhpVersion() 0 4 1
B displayUpcomingRequiredVersion() 0 24 4
1
<?php
2
3
namespace EventEspresso\core\services\request\middleware;
4
5
use EventEspresso\core\services\request\RequestInterface;
6
use EventEspresso\core\services\request\ResponseInterface;
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\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 RequestInterface $request
30
     * @param ResponseInterface      $response
31
     * @return ResponseInterface
32
     * @throws InvalidDataTypeException
33
     */
34
    public function handleRequest(RequestInterface $request, ResponseInterface $response)
35
    {
36
        $this->request  = $request;
37
        $this->response = $response;
38
        // check required WP version
39
        if (! $this->minimumWordPressVersionRequired()) {
40
            $this->request->unSetRequestParam('activate', true);
41
            add_action('admin_notices', array($this, 'minimum_wp_version_error'), 1);
42
            $this->response->terminateRequest();
43
            $this->response->deactivatePlugin();
44
        }
45
        // check recommended PHP version
46
        if (! $this->minimumPhpVersionRecommended()) {
47
            $this->displayMinimumRecommendedPhpVersionNotice();
48
        }
49
        //upcoming required version
50
        if (! $this->upcomingRequiredPhpVersion()) {
51
            $this->displayUpcomingRequiredVersion();
52
        }
53
        $this->response = $this->processRequestStack($this->request, $this->response);
54
        return $this->response;
55
    }
56
57
58
    /**
59
     * Helper method to assess installed wp version against given values.
60
     * By default this compares the required minimum version of WP for EE against the installed version of WP
61
     * Note, $wp_version is the first parameter sent into the PHP version_compare function (what is being checked
62
     * against) so consider that when sending in your values.
63
     *
64
     * @param string $version_to_check
65
     * @param string $operator
66
     * @return bool
67
     */
68
    public static function compareWordPressVersion($version_to_check = EE_MIN_WP_VER_REQUIRED, $operator = '>=')
69
    {
70
        global $wp_version;
71
        return version_compare(
72
        // first account for wp_version being pre-release
73
        // (like RC, beta etc) which are usually in the format like 4.7-RC3-39519
74
            strpos($wp_version, '-') > 0
75
                ? substr($wp_version, 0, strpos($wp_version, '-'))
76
                : $wp_version,
77
            $version_to_check,
78
            $operator
79
        );
80
    }
81
82
83
84
    /**
85
     * @return boolean
86
     */
87
    private function minimumWordPressVersionRequired()
88
    {
89
        return RecommendedVersions::compareWordPressVersion();
90
    }
91
92
93
94
    /**
95
     * @param string $min_version
96
     * @return boolean
97
     */
98
    private function checkPhpVersion($min_version = EE_MIN_PHP_VER_RECOMMENDED)
99
    {
100
        return version_compare(PHP_VERSION, $min_version, '>=') ? true : false;
101
    }
102
103
104
105
    /**
106
     * @return boolean
107
     */
108
    private function minimumPhpVersionRecommended()
109
    {
110
        return $this->checkPhpVersion();
111
    }
112
113
114
115
    /**
116
     * @return void
117
     */
118
    public function minimumWpVersionError()
119
    {
120
        global $wp_version;
121
        ?>
122
        <div class="error">
123
            <p>
124
                <?php
125
                printf(
126
                    __('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.',
127
                        'event_espresso'),
128
                    EE_MIN_WP_VER_REQUIRED,
129
                    $wp_version,
130
                    '<br/>',
131
                    '<a href="http://codex.wordpress.org/Updating_WordPress">http://codex.wordpress.org/Updating_WordPress</a>'
132
                );
133
                ?>
134
            </p>
135
        </div>
136
        <?php
137
    }
138
139
140
141
    /**
142
     *    _display_minimum_recommended_php_version_notice
143
     *
144
     * @access private
145
     * @return void
146
     * @throws InvalidDataTypeException
147
     */
148
    private function displayMinimumRecommendedPhpVersionNotice()
149
    {
150
        if ($this->request->isAdmin()) {
151
            new PersistentAdminNotice(
152
                'php_version_' . str_replace('.', '-', EE_MIN_PHP_VER_RECOMMENDED) . '_recommended',
153
                sprintf(
154
                    esc_html__(
155
                        '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.',
156
                        'event_espresso'
157
                    ),
158
                    EE_MIN_PHP_VER_RECOMMENDED,
159
                    PHP_VERSION,
160
                    '<br/>',
161
                    '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
162
                )
163
            );
164
        }
165
    }
166
167
168
    /**
169
     * Returns whether the provided php version number is greater than the current version of php installed on the server.
170
     *
171
     * @param string $version_required
172
     * @return bool
173
     */
174
    private function upcomingRequiredPhpVersion($version_required = '5.5')
175
    {
176
        return $this->checkPhpVersion($version_required);
177
    }
178
179
180
    /**
181
     *  Sets a notice for an upcoming required version of PHP in the next update of EE core.
182
     */
183
    private function displayUpcomingRequiredVersion()
184
    {
185
        if ($this->request->isAdmin()
186
            && apply_filters('FHEE__EE_Recommended_Versions__displayUpcomingRequiredVersion', true, $this->request)
187
            && current_user_can('update_plugins')
188
        ) {
189
            add_action('admin_notices', function ()
190
            {
191
                echo '<div class="notice event-espresso-admin-notice notice-warning"><p>'
192
                     . sprintf(
193
                         esc_html__(
194
                             'Please note: The next update of Event Espresso 4 will %1$srequire%2$s PHP 5.4.45 or greater.  Your web server\'s PHP version is %3$s.  You can contact your host and ask them to update your PHP version to at least PHP 5.6.  Please do not update to the new version of Event Espresso 4 until the PHP update is completed. Read about why keeping your server on the latest version of PHP is a good idea %4$shere%5$s',
195
                             'event_espresso'
196
                         ),
197
                         '<strong>',
198
                         '</strong>',
199
                         PHP_VERSION,
200
                         '<a href="https://wordpress.org/support/upgrade-php/">',
201
                         '</a>'
202
                     )
203
                     . '</p></div>';
204
            });
205
        }
206
    }
207
}
208
// Location: RecommendedVersions.php
209