Completed
Branch BUG/11268/session-ticket-relea... (f9aa59)
by
unknown
13:30 queued 24s
created

displayUpcomingRequiredVersion()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 2
nop 0
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\domain\entities\notifications\PersistentAdminNotice;
4
use EventEspresso\core\exceptions\InvalidDataTypeException;
5
6
defined('EVENT_ESPRESSO_VERSION') || exit('No direct script access allowed');
7
8
9
10
/**
11
 * Class EE_Recommended_Versions
12
 * checks required and recommended versions for both WP and PHP
13
 * terminates the request if minimum required versions are not met
14
 *
15
 * @package       Event Espresso
16
 * @subpackage    core
17
 * @author        Brent Christensen
18
 * @since         4.8.20
19
 */
20
class EE_Recommended_Versions extends EE_Middleware
21
{
22
23
24
    /**
25
     * converts a Request to a Response
26
     *
27
     * @param EE_Request  $request
28
     * @param EE_Response $response
29
     * @return EE_Response
30
     * @throws InvalidDataTypeException
31
     */
32
    public function handle_request(EE_Request $request, EE_Response $response)
33
    {
34
        $this->_request  = $request;
35
        $this->_response = $response;
36
        //$this->_response->add_output( "\n\t IN >>  " . __CLASS__ );
37
        //$this->_response->set_notice( 1, 'hey look at this' );
38
        // check required WP version
39
        if (! $this->_minimum_wp_version_required()) {
40
            $this->_request->un_set('activate', true);
41
            add_action('admin_notices', array($this, 'minimum_wp_version_error'), 1);
42
            //$this->_response->add_output( "\n<br />" . 'minimum_wp_version_error' );
43
            $this->_response->terminate_request();
44
            $this->_response->deactivate_plugin();
45
        }
46
        // check recommended PHP version
47
        if (! $this->_minimum_php_version_recommended()) {
48
            $this->_display_minimum_recommended_php_version_notice();
49
        }
50
51
        //upcoming required version
52
        if (! $this->upcomingRequiredPhpVersion()) {
53
            $this->displayUpcomingRequiredVersion();
54
        }
55
        $this->_response = $this->process_request_stack($this->_request, $this->_response);
56
        //$this->_response->add_output( "\n\t OUT << " . __CLASS__ );
57
        return $this->_response;
58
    }
59
60
61
    /**
62
     * Helper method to assess installed wp version against given values.
63
     * By default this compares the required minimum version of WP for EE against the installed version of WP
64
     * Note, $wp_version is the first parameter sent into the PHP version_compare function (what is being checked
65
     * against) so consider that when sending in your values.
66
     *
67
     * @param string $version_to_check
68
     * @param string $operator
69
     * @return bool
70
     */
71
    public static function check_wp_version($version_to_check = EE_MIN_WP_VER_REQUIRED, $operator = '>=')
72
    {
73
        global $wp_version;
74
        return version_compare(
75
            // first account for wp_version being pre-release
76
            // (like RC, beta etc) which are usually in the format like 4.7-RC3-39519
77
            strpos($wp_version, '-') > 0
78
                ? substr($wp_version, 0, strpos($wp_version, '-'))
79
                : $wp_version,
80
            $version_to_check,
81
            $operator
82
        );
83
    }
84
85
86
87
    /**
88
     *    _minimum_wp_version_required
89
     *
90
     * @access private
91
     * @return boolean
92
     */
93
    private function _minimum_wp_version_required()
94
    {
95
        return EE_Recommended_Versions::check_wp_version();
96
    }
97
98
99
100
    /**
101
     *    _check_php_version
102
     *
103
     * @access private
104
     * @param string $min_version
105
     * @return boolean
106
     */
107
    private function _check_php_version($min_version = EE_MIN_PHP_VER_RECOMMENDED)
108
    {
109
        return version_compare(PHP_VERSION, $min_version, '>=');
110
    }
111
112
113
114
    /**
115
     *    _minimum_php_version_recommended
116
     *
117
     * @access private
118
     * @return boolean
119
     */
120
    private function _minimum_php_version_recommended()
121
    {
122
        return $this->_check_php_version();
123
    }
124
125
126
    /**
127
     * Returns whether the provided php version number is greater than the current version of php installed on the server.
128
     * @param string $version_required
129
     * @return bool
130
     */
131
    private function upcomingRequiredPhpVersion($version_required = '5.5')
132
    {
133
        return $this->_check_php_version($version_required);
134
    }
135
136
137
138
    /**
139
     *    minimum_wp_version_error
140
     *
141
     * @return void
142
     */
143
    public function minimum_wp_version_error()
144
    {
145
        global $wp_version;
146
        ?>
147
        <div class="error">
148
            <p>
149
                <?php
150
                printf(
151
                    __('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.',
152
                        'event_espresso'),
153
                    EE_MIN_WP_VER_REQUIRED,
154
                    $wp_version,
155
                    '<br/>',
156
                    '<a href="http://codex.wordpress.org/Updating_WordPress">http://codex.wordpress.org/Updating_WordPress</a>'
157
                );
158
                ?>
159
            </p>
160
        </div>
161
        <?php
162
    }
163
164
165
166
    /**
167
     *    _display_minimum_recommended_php_version_notice
168
     *
169
     * @access private
170
     * @return void
171
     * @throws InvalidDataTypeException
172
     */
173
    private function _display_minimum_recommended_php_version_notice()
174
    {
175
        if($this->_request->isAdmin()){
176
            new PersistentAdminNotice(
177
                'php_version_' . str_replace('.', '-', EE_MIN_PHP_VER_RECOMMENDED) . '_recommended',
178
                sprintf(
179
                    __(
180
                        '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.',
181
                        'event_espresso'
182
                    ),
183
                    EE_MIN_PHP_VER_RECOMMENDED,
184
                    PHP_VERSION,
185
                    '<br/>',
186
                    '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
187
                )
188
            );
189
        }
190
    }
191
192
193
    /**
194
     *  Sets a notice for an upcoming required version of PHP in the next update of EE core.
195
     */
196
    private function displayUpcomingRequiredVersion()
197
    {
198
        if ($this->_request->isAdmin()
199
            && apply_filters('FHEE__EE_Recommended_Versions__displayUpcomingRequiredVersion', true, $this->_request)
200
            && current_user_can('update_plugins')
201
        ) {
202
            add_action('admin_notices', function () {
203
                echo '<div class="notice event-espresso-admin-notice notice-warning"><p>'
204
                     . sprintf(
205
                         esc_html__(
206
                             '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',
207
                             'event_espresso'
208
                         ),
209
                         '<strong>',
210
                         '</strong>',
211
                         PHP_VERSION,
212
                         '<a href="https://wordpress.org/support/upgrade-php/">',
213
                         '</a>'
214
                     )
215
                     . '</p></div>';
216
            });
217
        }
218
    }
219
}
220