Completed
Branch BUG-10878-event-spaces-remaini... (07bd4d)
by
unknown
42:38 queued 31:47
created

EE_System   F

Complexity

Total Complexity 126

Size/Duplication

Total Lines 1190
Duplicated Lines 1.6 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
dl 19
loc 1190
rs 0.6314
c 0
b 0
f 0
wmc 126
lcom 1
cbo 16

40 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 13 2
A reset() 0 11 1
A __construct() 0 71 1
A loadCapabilities() 0 10 1
A loadCommandBus() 0 16 1
A loadPluginApi() 0 6 1
C load_espresso_addons() 0 24 7
A detect_activations_or_upgrades() 0 11 3
B detect_if_activation_or_upgrade() 0 36 6
A _handle_core_version_change() 0 9 1
C fix_espresso_db_upgrade_option() 0 39 7
C initialize_db_if_no_migrations_required() 0 27 8
A initialize_addons() 0 9 3
A update_list_of_installed_versions() 0 12 3
A detect_req_type() 0 14 3
A _detect_major_version_change() 0 10 3
A is_major_version_change() 0 4 1
C detect_req_type_given_activation_history() 19 48 9
A _new_version_is_higher() 0 7 1
D _get_most_recently_active_version_from_activation_history() 0 26 9
D redirect_to_about_ee() 0 25 9
B load_core_configuration() 0 26 3
B _parse_model_names() 0 22 4
A _maybe_brew_regular() 0 6 4
A register_shortcodes_modules_and_widgets() 0 20 3
B _incompatible_addon_error() 0 30 4
A brew_espresso() 0 18 3
A set_hooks_for_core() 0 8 1
B _deactivate_incompatible_addons() 0 15 5
A perform_activations_upgrades_and_migrations() 0 8 2
A load_CPTs_and_session() 0 7 1
A load_controllers() 0 13 4
A core_loaded_and_ready() 0 11 2
A initialize() 0 4 1
A initialize_last() 0 5 1
A addEspressoToolbar() 0 7 1
A do_not_cache() 0 19 4
A extra_nocache_headers() 0 8 1
A nocache_headers() 0 4 1
A remove_pages_from_wp_list_pages() 0 4 1

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 EE_System 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 EE_System, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
use EventEspresso\core\exceptions\ExceptionStackTraceDisplay;
4
use EventEspresso\core\interfaces\ResettableInterface;
5
use EventEspresso\core\services\loaders\LoaderFactory;
6
use EventEspresso\core\services\loaders\LoaderInterface;
7
8
defined('EVENT_ESPRESSO_VERSION') || exit('No direct script access allowed');
9
10
11
12
/**
13
 * EE_System
14
 * The backbone of the core application that the rest of the system builds off of once bootstrapping is complete
15
 *
16
 * @package        Event Espresso
17
 * @subpackage     core/
18
 * @author         Brent Christensen, Michael Nelson
19
 */
20
final class EE_System implements ResettableInterface
21
{
22
23
24
    /**
25
     * indicates this is a 'normal' request. Ie, not activation, nor upgrade, nor activation.
26
     * So examples of this would be a normal GET request on the frontend or backend, or a POST, etc
27
     */
28
    const req_type_normal = 0;
29
30
    /**
31
     * Indicates this is a brand new installation of EE so we should install
32
     * tables and default data etc
33
     */
34
    const req_type_new_activation = 1;
35
36
    /**
37
     * we've detected that EE has been reactivated (or EE was activated during maintenance mode,
38
     * and we just exited maintenance mode). We MUST check the database is setup properly
39
     * and that default data is setup too
40
     */
41
    const req_type_reactivation = 2;
42
43
    /**
44
     * indicates that EE has been upgraded since its previous request.
45
     * We may have data migration scripts to call and will want to trigger maintenance mode
46
     */
47
    const req_type_upgrade = 3;
48
49
    /**
50
     * TODO  will detect that EE has been DOWNGRADED. We probably don't want to run in this case...
51
     */
52
    const req_type_downgrade = 4;
53
54
    /**
55
     * @deprecated since version 4.6.0.dev.006
56
     * Now whenever a new_activation is detected the request type is still just
57
     * new_activation (same for reactivation, upgrade, downgrade etc), but if we'r ein maintenance mode
58
     * EE_System::initialize_db_if_no_migrations_required and EE_Addon::initialize_db_if_no_migrations_required
59
     * will instead enqueue that EE plugin's db initialization for when we're taken out of maintenance mode.
60
     * (Specifically, when the migration manager indicates migrations are finished
61
     * EE_Data_Migration_Manager::initialize_db_for_enqueued_ee_plugins() will be called)
62
     */
63
    const req_type_activation_but_not_installed = 5;
64
65
    /**
66
     * option prefix for recording the activation history (like core's "espresso_db_update") of addons
67
     */
68
    const addon_activation_history_option_prefix = 'ee_addon_activation_history_';
69
70
71
    /**
72
     * @var EE_System $_instance
73
     */
74
    private static $_instance;
75
76
    /**
77
     * @var EE_Registry $registry
78
     */
79
    private $registry;
80
81
    /**
82
     * @var LoaderInterface $loader
83
     */
84
    private $loader;
85
86
    /**
87
     * @var EE_Capabilities $capabilities
88
     */
89
    private $capabilities;
90
91
    /**
92
     * @var EE_Request $request
93
     */
94
    private $request;
95
96
    /**
97
     * @var EE_Maintenance_Mode $maintenance_mode
98
     */
99
    private $maintenance_mode;
100
101
    /**
102
     * Stores which type of request this is, options being one of the constants on EE_System starting with req_type_*.
103
     * It can be a brand-new activation, a reactivation, an upgrade, a downgrade, or a normal request.
104
     *
105
     * @var int $_req_type
106
     */
107
    private $_req_type;
108
109
    /**
110
     * Whether or not there was a non-micro version change in EE core version during this request
111
     *
112
     * @var boolean $_major_version_change
113
     */
114
    private $_major_version_change = false;
115
116
117
118
    /**
119
     * @singleton method used to instantiate class object
120
     * @param EE_Registry|null         $registry
121
     * @param LoaderInterface|null     $loader
122
     * @param EE_Capabilities|null     $capabilities
123
     * @param EE_Request|null          $request
124
     * @param EE_Maintenance_Mode|null $maintenance_mode
125
     * @return EE_System
126
     */
127
    public static function instance(
128
        EE_Registry $registry = null,
129
        LoaderInterface $loader = null,
130
        EE_Capabilities $capabilities = null,
131
        EE_Request $request = null,
132
        EE_Maintenance_Mode $maintenance_mode = null
133
    ) {
134
        // check if class object is instantiated
135
        if (! self::$_instance instanceof EE_System) {
136
            self::$_instance = new self($registry, $loader, $capabilities, $request, $maintenance_mode);
0 ignored issues
show
Bug introduced by
It seems like $registry defined by parameter $registry on line 128 can be null; however, EE_System::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
Bug introduced by
It seems like $loader defined by parameter $loader on line 129 can be null; however, EE_System::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
Bug introduced by
It seems like $capabilities defined by parameter $capabilities on line 130 can be null; however, EE_System::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
Bug introduced by
It seems like $request defined by parameter $request on line 131 can be null; however, EE_System::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
Bug introduced by
It seems like $maintenance_mode defined by parameter $maintenance_mode on line 132 can be null; however, EE_System::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
137
        }
138
        return self::$_instance;
139
    }
140
141
142
143
    /**
144
     * resets the instance and returns it
145
     *
146
     * @return EE_System
147
     */
148
    public static function reset()
149
    {
150
        self::$_instance->_req_type = null;
151
        //make sure none of the old hooks are left hanging around
152
        remove_all_actions('AHEE__EE_System__perform_activations_upgrades_and_migrations');
153
        //we need to reset the migration manager in order for it to detect DMSs properly
154
        EE_Data_Migration_Manager::reset();
155
        self::instance()->detect_activations_or_upgrades();
156
        self::instance()->perform_activations_upgrades_and_migrations();
157
        return self::instance();
158
    }
159
160
161
162
    /**
163
     * sets hooks for running rest of system
164
     * provides "AHEE__EE_System__construct__complete" hook for EE Addons to use as their starting point
165
     * starting EE Addons from any other point may lead to problems
166
     *
167
     * @param EE_Registry         $registry
168
     * @param LoaderInterface     $loader
169
     * @param EE_Capabilities     $capabilities
170
     * @param EE_Request          $request
171
     * @param EE_Maintenance_Mode $maintenance_mode
172
     */
173
    private function __construct(
174
        EE_Registry $registry,
175
        LoaderInterface $loader,
176
        EE_Capabilities $capabilities,
177
        EE_Request $request,
178
        EE_Maintenance_Mode $maintenance_mode
179
    ) {
180
        $this->registry = $registry;
181
        $this->loader = $loader;
182
        $this->capabilities = $capabilities;
183
        $this->request = $request;
184
        $this->maintenance_mode = $maintenance_mode;
185
        do_action('AHEE__EE_System__construct__begin', $this);
186
        add_action(
187
            'AHEE__EE_Bootstrap__load_espresso_addons',
188
            array($this, 'loadCapabilities'),
189
            5
190
        );
191
        add_action(
192
            'AHEE__EE_Bootstrap__load_espresso_addons',
193
            array($this, 'loadCommandBus'),
194
            7
195
        );
196
        add_action(
197
            'AHEE__EE_Bootstrap__load_espresso_addons',
198
            array($this, 'loadPluginApi'),
199
            9
200
        );
201
        // allow addons to load first so that they can register autoloaders, set hooks for running DMS's, etc
202
        add_action(
203
            'AHEE__EE_Bootstrap__load_espresso_addons',
204
            array($this, 'load_espresso_addons')
205
        );
206
        // when an ee addon is activated, we want to call the core hook(s) again
207
        // because the newly-activated addon didn't get a chance to run at all
208
        add_action('activate_plugin', array($this, 'load_espresso_addons'), 1);
209
        // detect whether install or upgrade
210
        add_action(
211
            'AHEE__EE_Bootstrap__detect_activations_or_upgrades',
212
            array($this, 'detect_activations_or_upgrades'),
213
            3
214
        );
215
        // load EE_Config, EE_Textdomain, etc
216
        add_action(
217
            'AHEE__EE_Bootstrap__load_core_configuration',
218
            array($this, 'load_core_configuration'),
219
            5
220
        );
221
        // load EE_Config, EE_Textdomain, etc
222
        add_action(
223
            'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets',
224
            array($this, 'register_shortcodes_modules_and_widgets'),
225
            7
226
        );
227
        // you wanna get going? I wanna get going... let's get going!
228
        add_action(
229
            'AHEE__EE_Bootstrap__brew_espresso',
230
            array($this, 'brew_espresso'),
231
            9
232
        );
233
        //other housekeeping
234
        //exclude EE critical pages from wp_list_pages
235
        add_filter(
236
            'wp_list_pages_excludes',
237
            array($this, 'remove_pages_from_wp_list_pages'),
238
            10
239
        );
240
        // ALL EE Addons should use the following hook point to attach their initial setup too
241
        // it's extremely important for EE Addons to register any class autoloaders so that they can be available when the EE_Config loads
242
        do_action('AHEE__EE_System__construct__complete', $this);
243
    }
244
245
246
247
    /**
248
     * load and setup EE_Capabilities
249
     *
250
     * @return void
251
     * @throws EE_Error
252
     */
253
    public function loadCapabilities()
254
    {
255
        $this->loader->getShared('EE_Capabilities');
256
        add_action(
257
            'AHEE__EE_Capabilities__init_caps__before_initialization',
258
            function() {
259
                LoaderFactory::getLoader()->getShared('Payment_Method_Manager');
260
            }
261
        );
262
    }
263
264
265
266
    /**
267
     * create and cache the CommandBus, and also add middleware
268
     * The CapChecker middleware requires the use of EE_Capabilities
269
     * which is why we need to load the CommandBus after Caps are set up
270
     *
271
     * @return void
272
     * @throws EE_Error
273
     */
274
    public function loadCommandBus()
275
    {
276
        $this->loader->getShared(
277
            'CommandBusInterface',
278
            array(
279
                null,
280
                apply_filters(
281
                    'FHEE__EE_Load_Espresso_Core__handle_request__CommandBus_middleware',
282
                    array(
283
                        $this->loader->getShared('EventEspresso\core\services\commands\middleware\CapChecker'),
284
                        $this->loader->getShared('EventEspresso\core\services\commands\middleware\AddActionHook'),
285
                    )
286
                ),
287
            )
288
        );
289
    }
290
291
292
293
    /**
294
     * @return void
295
     * @throws EE_Error
296
     */
297
    public function loadPluginApi()
298
    {
299
        // set autoloaders for all of the classes implementing EEI_Plugin_API
300
        // which provide helpers for EE plugin authors to more easily register certain components with EE.
301
        EEH_Autoloader::instance()->register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'plugin_api');
302
    }
303
304
305
306
    /**
307
     * load_espresso_addons
308
     * allow addons to load first so that they can set hooks for running DMS's, etc
309
     * this is hooked into both:
310
     *    'AHEE__EE_Bootstrap__load_core_configuration'
311
     *        which runs during the WP 'plugins_loaded' action at priority 5
312
     *    and the WP 'activate_plugin' hook point
313
     *
314
     * @access public
315
     * @return void
316
     * @throws EE_Error
317
     */
318
    public function load_espresso_addons()
319
    {
320
        do_action('AHEE__EE_System__load_espresso_addons');
321
        //if the WP API basic auth plugin isn't already loaded, load it now.
322
        //We want it for mobile apps. Just include the entire plugin
323
        //also, don't load the basic auth when a plugin is getting activated, because
324
        //it could be the basic auth plugin, and it doesn't check if its methods are already defined
325
        //and causes a fatal error
326
        if (
327
            ! (
328
                isset($_GET['activate'])
329
                && $_GET['activate'] === 'true'
330
            )
331
            && ! function_exists('json_basic_auth_handler')
332
            && ! function_exists('json_basic_auth_error')
333
            && ! (
334
                isset($_GET['action'])
335
                && in_array($_GET['action'], array('activate', 'activate-selected'), true)
336
            )
337
        ) {
338
            include_once EE_THIRD_PARTY . 'wp-api-basic-auth' . DS . 'basic-auth.php';
339
        }
340
        do_action('AHEE__EE_System__load_espresso_addons__complete');
341
    }
342
343
344
345
    /**
346
     * detect_activations_or_upgrades
347
     * Checks for activation or upgrade of core first;
348
     * then also checks if any registered addons have been activated or upgraded
349
     * This is hooked into 'AHEE__EE_Bootstrap__detect_activations_or_upgrades'
350
     * which runs during the WP 'plugins_loaded' action at priority 3
351
     *
352
     * @access public
353
     * @return void
354
     */
355
    public function detect_activations_or_upgrades()
356
    {
357
        //first off: let's make sure to handle core
358
        $this->detect_if_activation_or_upgrade();
359
        foreach ($this->registry->addons as $addon) {
360
            if ($addon instanceof EE_Addon) {
361
                //detect teh request type for that addon
362
                $addon->detect_activation_or_upgrade();
363
            }
364
        }
365
    }
366
367
368
369
    /**
370
     * detect_if_activation_or_upgrade
371
     * Takes care of detecting whether this is a brand new install or code upgrade,
372
     * and either setting up the DB or setting up maintenance mode etc.
373
     *
374
     * @access public
375
     * @return void
376
     */
377
    public function detect_if_activation_or_upgrade()
378
    {
379
        do_action('AHEE__EE_System___detect_if_activation_or_upgrade__begin');
380
        // check if db has been updated, or if its a brand-new installation
381
        $espresso_db_update = $this->fix_espresso_db_upgrade_option();
382
        $request_type = $this->detect_req_type($espresso_db_update);
383
        //EEH_Debug_Tools::printr( $request_type, '$request_type', __FILE__, __LINE__ );
384
        switch ($request_type) {
385
            case EE_System::req_type_new_activation:
386
                do_action('AHEE__EE_System__detect_if_activation_or_upgrade__new_activation');
387
                $this->_handle_core_version_change($espresso_db_update);
388
                break;
389
            case EE_System::req_type_reactivation:
390
                do_action('AHEE__EE_System__detect_if_activation_or_upgrade__reactivation');
391
                $this->_handle_core_version_change($espresso_db_update);
392
                break;
393
            case EE_System::req_type_upgrade:
394
                do_action('AHEE__EE_System__detect_if_activation_or_upgrade__upgrade');
395
                //migrations may be required now that we've upgraded
396
                $this->maintenance_mode->set_maintenance_mode_if_db_old();
397
                $this->_handle_core_version_change($espresso_db_update);
398
                //				echo "done upgrade";die;
399
                break;
400
            case EE_System::req_type_downgrade:
401
                do_action('AHEE__EE_System__detect_if_activation_or_upgrade__downgrade');
402
                //its possible migrations are no longer required
403
                $this->maintenance_mode->set_maintenance_mode_if_db_old();
404
                $this->_handle_core_version_change($espresso_db_update);
405
                break;
406
            case EE_System::req_type_normal:
407
            default:
408
                //				$this->_maybe_redirect_to_ee_about();
409
                break;
410
        }
411
        do_action('AHEE__EE_System__detect_if_activation_or_upgrade__complete');
412
    }
413
414
415
416
    /**
417
     * Updates the list of installed versions and sets hooks for
418
     * initializing the database later during the request
419
     *
420
     * @param array $espresso_db_update
421
     */
422
    private function _handle_core_version_change($espresso_db_update)
423
    {
424
        $this->update_list_of_installed_versions($espresso_db_update);
425
        //get ready to verify the DB is ok (provided we aren't in maintenance mode, of course)
426
        add_action(
427
            'AHEE__EE_System__perform_activations_upgrades_and_migrations',
428
            array($this, 'initialize_db_if_no_migrations_required')
429
        );
430
    }
431
432
433
434
    /**
435
     * standardizes the wp option 'espresso_db_upgrade' which actually stores
436
     * information about what versions of EE have been installed and activated,
437
     * NOT necessarily the state of the database
438
     *
439
     * @param mixed $espresso_db_update the value of the WordPress option.
440
     *                                            If not supplied, fetches it from the options table
441
     * @return array the correct value of 'espresso_db_upgrade', after saving it, if it needed correction
442
     */
443
    private function fix_espresso_db_upgrade_option($espresso_db_update = null)
444
    {
445
        do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__begin', $espresso_db_update);
446
        if (! $espresso_db_update) {
447
            $espresso_db_update = get_option('espresso_db_update');
448
        }
449
        // check that option is an array
450
        if (! is_array($espresso_db_update)) {
451
            // if option is FALSE, then it never existed
452
            if ($espresso_db_update === false) {
453
                // make $espresso_db_update an array and save option with autoload OFF
454
                $espresso_db_update = array();
455
                add_option('espresso_db_update', $espresso_db_update, '', 'no');
456
            } else {
457
                // option is NOT FALSE but also is NOT an array, so make it an array and save it
458
                $espresso_db_update = array($espresso_db_update => array());
459
                update_option('espresso_db_update', $espresso_db_update);
460
            }
461
        } else {
462
            $corrected_db_update = array();
463
            //if IS an array, but is it an array where KEYS are version numbers, and values are arrays?
464
            foreach ($espresso_db_update as $should_be_version_string => $should_be_array) {
465
                if (is_int($should_be_version_string) && ! is_array($should_be_array)) {
466
                    //the key is an int, and the value IS NOT an array
467
                    //so it must be numerically-indexed, where values are versions installed...
468
                    //fix it!
469
                    $version_string = $should_be_array;
470
                    $corrected_db_update[$version_string] = array('unknown-date');
471
                } else {
472
                    //ok it checks out
473
                    $corrected_db_update[$should_be_version_string] = $should_be_array;
474
                }
475
            }
476
            $espresso_db_update = $corrected_db_update;
477
            update_option('espresso_db_update', $espresso_db_update);
478
        }
479
        do_action('FHEE__EE_System__manage_fix_espresso_db_upgrade_option__complete', $espresso_db_update);
480
        return $espresso_db_update;
481
    }
482
483
484
485
    /**
486
     * Does the traditional work of setting up the plugin's database and adding default data.
487
     * If migration script/process did not exist, this is what would happen on every activation/reactivation/upgrade.
488
     * NOTE: if we're in maintenance mode (which would be the case if we detect there are data
489
     * migration scripts that need to be run and a version change happens), enqueues core for database initialization,
490
     * so that it will be done when migrations are finished
491
     *
492
     * @param boolean $initialize_addons_too if true, we double-check addons' database tables etc too;
493
     * @param boolean $verify_schema         if true will re-check the database tables have the correct schema.
494
     *                                       This is a resource-intensive job
495
     *                                       so we prefer to only do it when necessary
496
     * @return void
497
     * @throws EE_Error
498
     */
499
    public function initialize_db_if_no_migrations_required($initialize_addons_too = false, $verify_schema = true)
500
    {
501
        $request_type = $this->detect_req_type();
502
        //only initialize system if we're not in maintenance mode.
503
        if ($this->maintenance_mode->level() !== EE_Maintenance_Mode::level_2_complete_maintenance) {
504
            update_option('ee_flush_rewrite_rules', true);
505
            if ($verify_schema) {
506
                EEH_Activation::initialize_db_and_folders();
507
            }
508
            EEH_Activation::initialize_db_content();
509
            EEH_Activation::system_initialization();
510
            if ($initialize_addons_too) {
511
                $this->initialize_addons();
512
            }
513
        } else {
514
            EE_Data_Migration_Manager::instance()->enqueue_db_initialization_for('Core');
515
        }
516
        if ($request_type === EE_System::req_type_new_activation
517
            || $request_type === EE_System::req_type_reactivation
518
            || (
519
                $request_type === EE_System::req_type_upgrade
520
                && $this->is_major_version_change()
521
            )
522
        ) {
523
            add_action('AHEE__EE_System__initialize_last', array($this, 'redirect_to_about_ee'), 9);
524
        }
525
    }
526
527
528
529
    /**
530
     * Initializes the db for all registered addons
531
     *
532
     * @throws EE_Error
533
     */
534
    public function initialize_addons()
535
    {
536
        //foreach registered addon, make sure its db is up-to-date too
537
        foreach ($this->registry->addons as $addon) {
538
            if ($addon instanceof EE_Addon) {
539
                $addon->initialize_db_if_no_migrations_required();
540
            }
541
        }
542
    }
543
544
545
546
    /**
547
     * Adds the current code version to the saved wp option which stores a list of all ee versions ever installed.
548
     *
549
     * @param    array  $version_history
550
     * @param    string $current_version_to_add version to be added to the version history
551
     * @return    boolean success as to whether or not this option was changed
552
     */
553
    public function update_list_of_installed_versions($version_history = null, $current_version_to_add = null)
554
    {
555
        if (! $version_history) {
556
            $version_history = $this->fix_espresso_db_upgrade_option($version_history);
557
        }
558
        if ($current_version_to_add === null) {
559
            $current_version_to_add = espresso_version();
560
        }
561
        $version_history[$current_version_to_add][] = date('Y-m-d H:i:s', time());
562
        // re-save
563
        return update_option('espresso_db_update', $version_history);
564
    }
565
566
567
568
    /**
569
     * Detects if the current version indicated in the has existed in the list of
570
     * previously-installed versions of EE (espresso_db_update). Does NOT modify it (ie, no side-effect)
571
     *
572
     * @param array $espresso_db_update array from the wp option stored under the name 'espresso_db_update'.
573
     *                                  If not supplied, fetches it from the options table.
574
     *                                  Also, caches its result so later parts of the code can also know whether
575
     *                                  there's been an update or not. This way we can add the current version to
576
     *                                  espresso_db_update, but still know if this is a new install or not
577
     * @return int one of the constants on EE_System::req_type_
578
     */
579
    public function detect_req_type($espresso_db_update = null)
580
    {
581
        if ($this->_req_type === null) {
582
            $espresso_db_update = ! empty($espresso_db_update)
583
                ? $espresso_db_update
584
                : $this->fix_espresso_db_upgrade_option();
585
            $this->_req_type = EE_System::detect_req_type_given_activation_history(
586
                $espresso_db_update,
587
                'ee_espresso_activation', espresso_version()
588
            );
589
            $this->_major_version_change = $this->_detect_major_version_change($espresso_db_update);
590
        }
591
        return $this->_req_type;
592
    }
593
594
595
596
    /**
597
     * Returns whether or not there was a non-micro version change (ie, change in either
598
     * the first or second number in the version. Eg 4.9.0.rc.001 to 4.10.0.rc.000,
599
     * but not 4.9.0.rc.0001 to 4.9.1.rc.0001
600
     *
601
     * @param $activation_history
602
     * @return bool
603
     */
604
    private function _detect_major_version_change($activation_history)
605
    {
606
        $previous_version = EE_System::_get_most_recently_active_version_from_activation_history($activation_history);
607
        $previous_version_parts = explode('.', $previous_version);
608
        $current_version_parts = explode('.', espresso_version());
609
        return isset($previous_version_parts[0], $previous_version_parts[1], $current_version_parts[0], $current_version_parts[1])
610
               && ($previous_version_parts[0] !== $current_version_parts[0]
611
                   || $previous_version_parts[1] !== $current_version_parts[1]
612
               );
613
    }
614
615
616
617
    /**
618
     * Returns true if either the major or minor version of EE changed during this request.
619
     * Eg 4.9.0.rc.001 to 4.10.0.rc.000, but not 4.9.0.rc.0001 to 4.9.1.rc.0001
620
     *
621
     * @return bool
622
     */
623
    public function is_major_version_change()
624
    {
625
        return $this->_major_version_change;
626
    }
627
628
629
630
    /**
631
     * Determines the request type for any ee addon, given three piece of info: the current array of activation
632
     * histories (for core that' 'espresso_db_update' wp option); the name of the WordPress option which is temporarily
633
     * set upon activation of the plugin (for core it's 'ee_espresso_activation'); and the version that this plugin was
634
     * just activated to (for core that will always be espresso_version())
635
     *
636
     * @param array  $activation_history_for_addon     the option's value which stores the activation history for this
637
     *                                                 ee plugin. for core that's 'espresso_db_update'
638
     * @param string $activation_indicator_option_name the name of the WordPress option that is temporarily set to
639
     *                                                 indicate that this plugin was just activated
640
     * @param string $version_to_upgrade_to            the version that was just upgraded to (for core that will be
641
     *                                                 espresso_version())
642
     * @return int one of the constants on EE_System::req_type_*
643
     */
644
    public static function detect_req_type_given_activation_history(
645
        $activation_history_for_addon,
646
        $activation_indicator_option_name,
647
        $version_to_upgrade_to
648
    ) {
649
        $version_is_higher = self::_new_version_is_higher($activation_history_for_addon, $version_to_upgrade_to);
650
        if ($activation_history_for_addon) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $activation_history_for_addon of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
651
            //it exists, so this isn't a completely new install
652
            //check if this version already in that list of previously installed versions
653
            if (! isset($activation_history_for_addon[$version_to_upgrade_to])) {
654
                //it a version we haven't seen before
655
                if ($version_is_higher === 1) {
656
                    $req_type = EE_System::req_type_upgrade;
657
                } else {
658
                    $req_type = EE_System::req_type_downgrade;
659
                }
660
                delete_option($activation_indicator_option_name);
661
            } else {
662
                // its not an update. maybe a reactivation?
663
                if (get_option($activation_indicator_option_name, false)) {
664 View Code Duplication
                    if ($version_is_higher === -1) {
665
                        $req_type = EE_System::req_type_downgrade;
666
                    } else if ($version_is_higher === 0) {
667
                        //we've seen this version before, but it's an activation. must be a reactivation
668
                        $req_type = EE_System::req_type_reactivation;
669
                    } else {//$version_is_higher === 1
670
                        $req_type = EE_System::req_type_upgrade;
671
                    }
672
                    delete_option($activation_indicator_option_name);
673 View Code Duplication
                } else {
674
                    //we've seen this version before and the activation indicate doesn't show it was just activated
675
                    if ($version_is_higher === -1) {
676
                        $req_type = EE_System::req_type_downgrade;
677
                    } else if ($version_is_higher === 0) {
678
                        //we've seen this version before and it's not an activation. its normal request
679
                        $req_type = EE_System::req_type_normal;
680
                    } else {//$version_is_higher === 1
681
                        $req_type = EE_System::req_type_upgrade;
682
                    }
683
                }
684
            }
685
        } else {
686
            //brand new install
687
            $req_type = EE_System::req_type_new_activation;
688
            delete_option($activation_indicator_option_name);
689
        }
690
        return $req_type;
691
    }
692
693
694
695
    /**
696
     * Detects if the $version_to_upgrade_to is higher than the most recent version in
697
     * the $activation_history_for_addon
698
     *
699
     * @param array  $activation_history_for_addon (keys are versions, values are arrays of times activated,
700
     *                                             sometimes containing 'unknown-date'
701
     * @param string $version_to_upgrade_to        (current version)
702
     * @return int results of version_compare( $version_to_upgrade_to, $most_recently_active_version ).
703
     *                                             ie, -1 if $version_to_upgrade_to is LOWER (downgrade);
704
     *                                             0 if $version_to_upgrade_to MATCHES (reactivation or normal request);
705
     *                                             1 if $version_to_upgrade_to is HIGHER (upgrade) ;
706
     */
707
    private static function _new_version_is_higher($activation_history_for_addon, $version_to_upgrade_to)
708
    {
709
        //find the most recently-activated version
710
        $most_recently_active_version =
711
            EE_System::_get_most_recently_active_version_from_activation_history($activation_history_for_addon);
712
        return version_compare($version_to_upgrade_to, $most_recently_active_version);
713
    }
714
715
716
717
    /**
718
     * Gets the most recently active version listed in the activation history,
719
     * and if none are found (ie, it's a brand new install) returns '0.0.0.dev.000'.
720
     *
721
     * @param array $activation_history  (keys are versions, values are arrays of times activated,
722
     *                                   sometimes containing 'unknown-date'
723
     * @return string
724
     */
725
    private static function _get_most_recently_active_version_from_activation_history($activation_history)
726
    {
727
        $most_recently_active_version_activation = '1970-01-01 00:00:00';
728
        $most_recently_active_version = '0.0.0.dev.000';
729
        if (is_array($activation_history)) {
730
            foreach ($activation_history as $version => $times_activated) {
731
                //check there is a record of when this version was activated. Otherwise,
732
                //mark it as unknown
733
                if (! $times_activated) {
734
                    $times_activated = array('unknown-date');
735
                }
736
                if (is_string($times_activated)) {
737
                    $times_activated = array($times_activated);
738
                }
739
                foreach ($times_activated as $an_activation) {
740
                    if ($an_activation !== 'unknown-date' && $an_activation > $most_recently_active_version_activation) {
741
                        $most_recently_active_version = $version;
742
                        $most_recently_active_version_activation = $an_activation === 'unknown-date'
743
                            ? '1970-01-01 00:00:00'
744
                            : $an_activation;
745
                    }
746
                }
747
            }
748
        }
749
        return $most_recently_active_version;
750
    }
751
752
753
754
    /**
755
     * This redirects to the about EE page after activation
756
     *
757
     * @return void
758
     */
759
    public function redirect_to_about_ee()
760
    {
761
        $notices = EE_Error::get_notices(false);
762
        //if current user is an admin and it's not an ajax or rest request
763
        if (
764
            ! (defined('DOING_AJAX') && DOING_AJAX)
765
            && ! (defined('REST_REQUEST') && REST_REQUEST)
766
            && ! isset($notices['errors'])
767
            && apply_filters(
768
                'FHEE__EE_System__redirect_to_about_ee__do_redirect',
769
                $this->capabilities->current_user_can('manage_options', 'espresso_about_default')
770
            )
771
        ) {
772
            $query_params = array('page' => 'espresso_about');
773
            if (EE_System::instance()->detect_req_type() === EE_System::req_type_new_activation) {
774
                $query_params['new_activation'] = true;
775
            }
776
            if (EE_System::instance()->detect_req_type() === EE_System::req_type_reactivation) {
777
                $query_params['reactivation'] = true;
778
            }
779
            $url = add_query_arg($query_params, admin_url('admin.php'));
780
            wp_safe_redirect($url);
781
            exit();
782
        }
783
    }
784
785
786
787
    /**
788
     * load_core_configuration
789
     * this is hooked into 'AHEE__EE_Bootstrap__load_core_configuration'
790
     * which runs during the WP 'plugins_loaded' action at priority 5
791
     *
792
     * @return void
793
     * @throws ReflectionException
794
     */
795
    public function load_core_configuration()
796
    {
797
        do_action('AHEE__EE_System__load_core_configuration__begin', $this);
798
        $this->loader->getShared('EE_Load_Textdomain');
799
        //load textdomain
800
        EE_Load_Textdomain::load_textdomain();
801
        // load and setup EE_Config and EE_Network_Config
802
        $config = $this->loader->getShared('EE_Config');
803
        $this->loader->getShared('EE_Network_Config');
804
        // setup autoloaders
805
        // enable logging?
806
        if ($config->admin->use_full_logging) {
807
            $this->loader->getShared('EE_Log');
808
        }
809
        // check for activation errors
810
        $activation_errors = get_option('ee_plugin_activation_errors', false);
811
        if ($activation_errors) {
812
            EE_Error::add_error($activation_errors, __FILE__, __FUNCTION__, __LINE__);
813
            update_option('ee_plugin_activation_errors', false);
814
        }
815
        // get model names
816
        $this->_parse_model_names();
817
        //load caf stuff a chance to play during the activation process too.
818
        $this->_maybe_brew_regular();
819
        do_action('AHEE__EE_System__load_core_configuration__complete', $this);
820
    }
821
822
823
824
    /**
825
     * cycles through all of the models/*.model.php files, and assembles an array of model names
826
     *
827
     * @return void
828
     * @throws ReflectionException
829
     */
830
    private function _parse_model_names()
831
    {
832
        //get all the files in the EE_MODELS folder that end in .model.php
833
        $models = glob(EE_MODELS . '*.model.php');
834
        $model_names = array();
835
        $non_abstract_db_models = array();
836
        foreach ($models as $model) {
837
            // get model classname
838
            $classname = EEH_File::get_classname_from_filepath_with_standard_filename($model);
839
            $short_name = str_replace('EEM_', '', $classname);
840
            $reflectionClass = new ReflectionClass($classname);
841
            if ($reflectionClass->isSubclassOf('EEM_Base') && ! $reflectionClass->isAbstract()) {
842
                $non_abstract_db_models[$short_name] = $classname;
843
            }
844
            $model_names[$short_name] = $classname;
845
        }
846
        $this->registry->models = apply_filters('FHEE__EE_System__parse_model_names', $model_names);
847
        $this->registry->non_abstract_db_models = apply_filters(
848
            'FHEE__EE_System__parse_implemented_model_names',
849
            $non_abstract_db_models
850
        );
851
    }
852
853
854
855
    /**
856
     * The purpose of this method is to simply check for a file named "caffeinated/brewing_regular.php" for any hooks
857
     * that need to be setup before our EE_System launches.
858
     *
859
     * @return void
860
     */
861
    private function _maybe_brew_regular()
862
    {
863
        if ((! defined('EE_DECAF') || EE_DECAF !== true) && is_readable(EE_CAFF_PATH . 'brewing_regular.php')) {
864
            require_once EE_CAFF_PATH . 'brewing_regular.php';
865
        }
866
    }
867
868
869
870
    /**
871
     * register_shortcodes_modules_and_widgets
872
     * generate lists of shortcodes and modules, then verify paths and classes
873
     * This is hooked into 'AHEE__EE_Bootstrap__register_shortcodes_modules_and_widgets'
874
     * which runs during the WP 'plugins_loaded' action at priority 7
875
     *
876
     * @access public
877
     * @return void
878
     * @throws Exception
879
     */
880
    public function register_shortcodes_modules_and_widgets()
881
    {
882
        try {
883
            // load, register, and add shortcodes the new way
884
            $this->loader->getShared(
885
                'EventEspresso\core\services\shortcodes\ShortcodesManager',
886
                array(
887
                    // and the old way, but we'll put it under control of the new system
888
                    EE_Config::getLegacyShortcodesManager()
889
                )
890
            );
891
        } catch (Exception $exception) {
892
            new ExceptionStackTraceDisplay($exception);
893
        }
894
        do_action('AHEE__EE_System__register_shortcodes_modules_and_widgets');
895
        // check for addons using old hook point
896
        if (has_action('AHEE__EE_System__register_shortcodes_modules_and_addons')) {
897
            $this->_incompatible_addon_error();
898
        }
899
    }
900
901
902
903
    /**
904
     * _incompatible_addon_error
905
     *
906
     * @access public
907
     * @return void
908
     */
909
    private function _incompatible_addon_error()
910
    {
911
        // get array of classes hooking into here
912
        $class_names = EEH_Class_Tools::get_class_names_for_all_callbacks_on_hook(
913
            'AHEE__EE_System__register_shortcodes_modules_and_addons'
914
        );
915
        if (! empty($class_names)) {
916
            $msg = __(
917
                'The following plugins, addons, or modules appear to be incompatible with this version of Event Espresso and were automatically deactivated to avoid fatal errors:',
918
                'event_espresso'
919
            );
920
            $msg .= '<ul>';
921
            foreach ($class_names as $class_name) {
922
                $msg .= '<li><b>Event Espresso - ' . str_replace(
923
                        array('EE_', 'EEM_', 'EED_', 'EES_', 'EEW_'), '',
924
                        $class_name
925
                    ) . '</b></li>';
926
            }
927
            $msg .= '</ul>';
928
            $msg .= __(
929
                'Compatibility issues can be avoided and/or resolved by keeping addons and plugins updated to the latest version.',
930
                'event_espresso'
931
            );
932
            // save list of incompatible addons to wp-options for later use
933
            add_option('ee_incompatible_addons', $class_names, '', 'no');
934
            if (is_admin()) {
935
                EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__);
936
            }
937
        }
938
    }
939
940
941
942
    /**
943
     * brew_espresso
944
     * begins the process of setting hooks for initializing EE in the correct order
945
     * This is happening on the 'AHEE__EE_Bootstrap__brew_espresso' hook point
946
     * which runs during the WP 'plugins_loaded' action at priority 9
947
     *
948
     * @return void
949
     */
950
    public function brew_espresso()
951
    {
952
        do_action('AHEE__EE_System__brew_espresso__begin', $this);
953
        // load some final core systems
954
        add_action('init', array($this, 'set_hooks_for_core'), 1);
955
        add_action('init', array($this, 'perform_activations_upgrades_and_migrations'), 3);
956
        add_action('init', array($this, 'load_CPTs_and_session'), 5);
957
        add_action('init', array($this, 'load_controllers'), 7);
958
        add_action('init', array($this, 'core_loaded_and_ready'), 9);
959
        add_action('init', array($this, 'initialize'), 10);
960
        add_action('init', array($this, 'initialize_last'), 100);
961
        if (is_admin() && apply_filters('FHEE__EE_System__brew_espresso__load_pue', true)) {
962
            // pew pew pew
963
            $this->loader->getShared('EE_PUE');
964
            do_action('AHEE__EE_System__brew_espresso__after_pue_init');
965
        }
966
        do_action('AHEE__EE_System__brew_espresso__complete', $this);
967
    }
968
969
970
971
    /**
972
     *    set_hooks_for_core
973
     *
974
     * @access public
975
     * @return    void
976
     * @throws EE_Error
977
     */
978
    public function set_hooks_for_core()
979
    {
980
        $this->_deactivate_incompatible_addons();
981
        do_action('AHEE__EE_System__set_hooks_for_core');
982
        //caps need to be initialized on every request so that capability maps are set.
983
        //@see https://events.codebasehq.com/projects/event-espresso/tickets/8674
984
        $this->registry->CAP->init_caps();
985
    }
986
987
988
989
    /**
990
     * Using the information gathered in EE_System::_incompatible_addon_error,
991
     * deactivates any addons considered incompatible with the current version of EE
992
     */
993
    private function _deactivate_incompatible_addons()
994
    {
995
        $incompatible_addons = get_option('ee_incompatible_addons', array());
996
        if (! empty($incompatible_addons)) {
997
            $active_plugins = get_option('active_plugins', array());
998
            foreach ($active_plugins as $active_plugin) {
999
                foreach ($incompatible_addons as $incompatible_addon) {
1000
                    if (strpos($active_plugin, $incompatible_addon) !== false) {
1001
                        unset($_GET['activate']);
1002
                        espresso_deactivate_plugin($active_plugin);
1003
                    }
1004
                }
1005
            }
1006
        }
1007
    }
1008
1009
1010
1011
    /**
1012
     *    perform_activations_upgrades_and_migrations
1013
     *
1014
     * @access public
1015
     * @return    void
1016
     */
1017
    public function perform_activations_upgrades_and_migrations()
1018
    {
1019
        //first check if we had previously attempted to setup EE's directories but failed
1020
        if (EEH_Activation::upload_directories_incomplete()) {
1021
            EEH_Activation::create_upload_directories();
1022
        }
1023
        do_action('AHEE__EE_System__perform_activations_upgrades_and_migrations');
1024
    }
1025
1026
1027
1028
    /**
1029
     *    load_CPTs_and_session
1030
     *
1031
     * @access public
1032
     * @return    void
1033
     */
1034
    public function load_CPTs_and_session()
1035
    {
1036
        do_action('AHEE__EE_System__load_CPTs_and_session__start');
1037
        // register Custom Post Types
1038
        $this->loader->getShared('EE_Register_CPTs');
1039
        do_action('AHEE__EE_System__load_CPTs_and_session__complete');
1040
    }
1041
1042
1043
1044
    /**
1045
     * load_controllers
1046
     * this is the best place to load any additional controllers that needs access to EE core.
1047
     * it is expected that all basic core EE systems, that are not dependant on the current request are loaded at this
1048
     * time
1049
     *
1050
     * @access public
1051
     * @return void
1052
     */
1053
    public function load_controllers()
1054
    {
1055
        do_action('AHEE__EE_System__load_controllers__start');
1056
        // let's get it started
1057
        if (! is_admin() && ! $this->maintenance_mode->level()) {
1058
            do_action('AHEE__EE_System__load_controllers__load_front_controllers');
1059
            $this->loader->getShared('EE_Front_Controller');
1060
        } else if (! EE_FRONT_AJAX) {
1061
            do_action('AHEE__EE_System__load_controllers__load_admin_controllers');
1062
            $this->loader->getShared('EE_Admin');
1063
        }
1064
        do_action('AHEE__EE_System__load_controllers__complete');
1065
    }
1066
1067
1068
1069
    /**
1070
     * core_loaded_and_ready
1071
     * all of the basic EE core should be loaded at this point and available regardless of M-Mode
1072
     *
1073
     * @access public
1074
     * @return void
1075
     */
1076
    public function core_loaded_and_ready()
1077
    {
1078
        $this->loader->getShared('EE_Session');
1079
        do_action('AHEE__EE_System__core_loaded_and_ready');
1080
        // load_espresso_template_tags
1081
        if (is_readable(EE_PUBLIC . 'template_tags.php')) {
1082
            require_once(EE_PUBLIC . 'template_tags.php');
1083
        }
1084
        do_action('AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons');
1085
        $this->loader->getShared('EventEspresso\core\services\assets\Registry');
1086
    }
1087
1088
1089
1090
    /**
1091
     * initialize
1092
     * this is the best place to begin initializing client code
1093
     *
1094
     * @access public
1095
     * @return void
1096
     */
1097
    public function initialize()
1098
    {
1099
        do_action('AHEE__EE_System__initialize');
1100
    }
1101
1102
1103
1104
    /**
1105
     * initialize_last
1106
     * this is run really late during the WP init hook point, and ensures that mostly everything else that needs to
1107
     * initialize has done so
1108
     *
1109
     * @access public
1110
     * @return void
1111
     */
1112
    public function initialize_last()
1113
    {
1114
        do_action('AHEE__EE_System__initialize_last');
1115
        add_action('admin_bar_init', array($this, 'addEspressoToolbar'));
1116
    }
1117
1118
1119
1120
    /**
1121
     * @return void
1122
     * @throws EE_Error
1123
     */
1124
    public function addEspressoToolbar()
1125
    {
1126
        $this->loader->getShared(
1127
            'EventEspresso\core\domain\services\admin\AdminToolBar',
1128
            array($this->registry->CAP)
1129
        );
1130
    }
1131
1132
1133
1134
    /**
1135
     * do_not_cache
1136
     * sets no cache headers and defines no cache constants for WP plugins
1137
     *
1138
     * @access public
1139
     * @return void
1140
     */
1141
    public static function do_not_cache()
1142
    {
1143
        // set no cache constants
1144
        if (! defined('DONOTCACHEPAGE')) {
1145
            define('DONOTCACHEPAGE', true);
1146
        }
1147
        if (! defined('DONOTCACHCEOBJECT')) {
1148
            define('DONOTCACHCEOBJECT', true);
1149
        }
1150
        if (! defined('DONOTCACHEDB')) {
1151
            define('DONOTCACHEDB', true);
1152
        }
1153
        // add no cache headers
1154
        add_action('send_headers', array('EE_System', 'nocache_headers'), 10);
1155
        // plus a little extra for nginx and Google Chrome
1156
        add_filter('nocache_headers', array('EE_System', 'extra_nocache_headers'), 10, 1);
1157
        // prevent browsers from prefetching of the rel='next' link, because it may contain content that interferes with the registration process
1158
        remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
1159
    }
1160
1161
1162
1163
    /**
1164
     *    extra_nocache_headers
1165
     *
1166
     * @access    public
1167
     * @param $headers
1168
     * @return    array
1169
     */
1170
    public static function extra_nocache_headers($headers)
1171
    {
1172
        // for NGINX
1173
        $headers['X-Accel-Expires'] = 0;
1174
        // plus extra for Google Chrome since it doesn't seem to respect "no-cache", but WILL respect "no-store"
1175
        $headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0';
1176
        return $headers;
1177
    }
1178
1179
1180
1181
    /**
1182
     *    nocache_headers
1183
     *
1184
     * @access    public
1185
     * @return    void
1186
     */
1187
    public static function nocache_headers()
1188
    {
1189
        nocache_headers();
1190
    }
1191
1192
1193
1194
1195
    /**
1196
     * simply hooks into "wp_list_pages_exclude" filter (for wp_list_pages method) and makes sure EE critical pages are
1197
     * never returned with the function.
1198
     *
1199
     * @param  array $exclude_array any existing pages being excluded are in this array.
1200
     * @return array
1201
     */
1202
    public function remove_pages_from_wp_list_pages($exclude_array)
1203
    {
1204
        return array_merge($exclude_array, $this->registry->CFG->core->get_critical_pages_array());
1205
    }
1206
1207
1208
1209
}
1210
// End of file EE_System.core.php
1211
// Location: /core/EE_System.core.php
1212