Completed
Branch BUG-10202-persistent-admin-not... (d7b033)
by
unknown
93:46 queued 82:33
created

EE_Front_Controller   C

Complexity

Total Complexity 60

Size/Duplication

Total Lines 516
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 14

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 516
rs 5.6363
wmc 60
lcom 2
cbo 14

22 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 41 2
A Request_Handler() 0 4 1
A Module_Request_Router() 0 4 1
A getLegacyShortcodesManager() 0 4 1
A filter_wp_comments() 0 11 3
A employ_CPT_Strategy() 0 6 2
A loadPersistentAdminNoticeManager() 0 6 1
A maybe_force_admin_ajax_ssl() 0 7 3
A wp_loaded() 0 3 1
A get_request() 0 6 1
B pre_get_posts() 0 22 5
A wp() 0 3 1
A templateRedirect() 0 15 4
C espresso_widgets_in_active_sidebars() 0 23 9
B header_meta_tag() 0 26 4
B wp_print_scripts() 0 12 5
A enqueueStyle() 0 5 1
A enqueueScripts() 0 4 1
B display_errors() 0 18 7
A template_include() 0 11 4
A get_selected_template() 0 4 2
A initialize_shortcode() 0 12 1

How to fix   Complexity   

Complex Class

Complex classes like EE_Front_Controller 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_Front_Controller, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
use EventEspresso\core\exceptions\InvalidDataTypeException;
4
use EventEspresso\core\exceptions\InvalidInterfaceException;
5
use EventEspresso\core\services\loaders\LoaderFactory;
6
use EventEspresso\core\services\notifications\PersistentAdminNoticeManager;
7
use EventEspresso\core\services\shortcodes\LegacyShortcodesManager;
8
use EventEspresso\widgets\EspressoWidget;
9
10
if ( ! defined('EVENT_ESPRESSO_VERSION')) {
11
    exit('No direct script access allowed');
12
}
13
14
/**
15
 * Event Espresso
16
 * Event Registration and Management Plugin for WordPress
17
 * @ package            Event Espresso
18
 * @ author            Seth Shoultes
19
 * @ copyright        (c) 2008-2011 Event Espresso  All Rights Reserved.
20
 * @ license            http://eventespresso.com/support/terms-conditions/   * see Plugin Licensing *
21
 * @ link                    http://www.eventespresso.com
22
 * @ version            4.0
23
 * ------------------------------------------------------------------------
24
 * EE_Front_Controller
25
 *
26
 * @package               Event Espresso
27
 * @subpackage            core/
28
 * @author                Brent Christensen
29
 *                        ------------------------------------------------------------------------
30
 */
31
final class EE_Front_Controller
32
{
33
34
    /**
35
     * @var string $_template_path
36
     */
37
    private $_template_path;
38
39
    /**
40
     * @var string $_template
41
     */
42
    private $_template;
43
44
    /**
45
     * @type EE_Registry $Registry
46
     */
47
    protected $Registry;
48
49
    /**
50
     * @type EE_Request_Handler $Request_Handler
51
     */
52
    protected $Request_Handler;
53
54
    /**
55
     * @type EE_Module_Request_Router $Module_Request_Router
56
     */
57
    protected $Module_Request_Router;
58
59
    /**
60
     * @var PersistentAdminNoticeManager $persistent_admin_notice_manager
61
     */
62
    private $persistent_admin_notice_manager;
63
64
65
    /**
66
     *    class constructor
67
     *    should fire after shortcode, module, addon, or other plugin's default priority init phases have run
68
     *
69
     * @access    public
70
     * @param \EE_Registry              $Registry
71
     * @param \EE_Request_Handler       $Request_Handler
72
     * @param \EE_Module_Request_Router $Module_Request_Router
73
     */
74
    public function __construct(
75
        EE_Registry $Registry,
76
        EE_Request_Handler $Request_Handler,
77
        EE_Module_Request_Router $Module_Request_Router
78
    ) {
79
        $this->Registry              = $Registry;
80
        $this->Request_Handler       = $Request_Handler;
81
        $this->Module_Request_Router = $Module_Request_Router;
82
        // determine how to integrate WP_Query with the EE models
83
        add_action('AHEE__EE_System__initialize', array($this, 'employ_CPT_Strategy'));
84
        // just in case any nag notices are created during the request
85
        add_action('AHEE__EE_System__initialize_last', array($this, 'loadPersistentAdminNoticeManager'));
86
        // load other resources and begin to actually run shortcodes and modules
87
        add_action('wp_loaded', array($this, 'wp_loaded'), 5);
88
        // analyse the incoming WP request
89
        add_action('parse_request', array($this, 'get_request'), 1, 1);
90
        // process request with module factory
91
        add_action('pre_get_posts', array($this, 'pre_get_posts'), 10, 1);
92
        // before headers sent
93
        add_action('wp', array($this, 'wp'), 5);
94
        // primarily used to process any content shortcodes
95
        add_action('template_redirect', array($this, 'templateRedirect'), 999);
96
        // header
97
        add_action('wp_head', array($this, 'header_meta_tag'), 5);
98
        add_action('wp_print_scripts', array($this, 'wp_print_scripts'), 10);
99
        add_filter('template_include', array($this, 'template_include'), 1);
100
        // display errors
101
        add_action('loop_start', array($this, 'display_errors'), 2);
102
        // the content
103
        // add_filter( 'the_content', array( $this, 'the_content' ), 5, 1 );
104
        //exclude our private cpt comments
105
        add_filter('comments_clauses', array($this, 'filter_wp_comments'), 10, 1);
106
        //make sure any ajax requests will respect the url schema when requests are made against admin-ajax.php (http:// or https://)
107
        add_filter('admin_url', array($this, 'maybe_force_admin_ajax_ssl'), 200, 1);
108
        // action hook EE
109
        do_action('AHEE__EE_Front_Controller__construct__done', $this);
110
        // for checking that browser cookies are enabled
111
        if (apply_filters('FHEE__EE_Front_Controller____construct__set_test_cookie', true)) {
112
            setcookie('ee_cookie_test', uniqid('ect',true), time() + DAY_IN_SECONDS, '/');
113
        }
114
    }
115
116
117
    /**
118
     * @return EE_Request_Handler
119
     */
120
    public function Request_Handler()
121
    {
122
        return $this->Request_Handler;
123
    }
124
125
126
    /**
127
     * @return EE_Module_Request_Router
128
     */
129
    public function Module_Request_Router()
130
    {
131
        return $this->Module_Request_Router;
132
    }
133
134
135
136
    /**
137
     * @return LegacyShortcodesManager
138
     */
139
    public function getLegacyShortcodesManager()
140
    {
141
        return EE_Config::getLegacyShortcodesManager();
142
    }
143
144
145
146
147
148
    /***********************************************        INIT ACTION HOOK         ***********************************************/
149
150
151
152
    /**
153
     * filter_wp_comments
154
     * This simply makes sure that any "private" EE CPTs do not have their comments show up in any wp comment
155
     * widgets/queries done on frontend
156
     *
157
     * @param  array $clauses array of comment clauses setup by WP_Comment_Query
158
     * @return array array of comment clauses with modifications.
159
     */
160
    public function filter_wp_comments($clauses)
161
    {
162
        global $wpdb;
163
        if (strpos($clauses['join'], $wpdb->posts) !== false) {
164
            $cpts = EE_Register_CPTs::get_private_CPTs();
165
            foreach ($cpts as $cpt => $details) {
166
                $clauses['where'] .= $wpdb->prepare(" AND $wpdb->posts.post_type != %s", $cpt);
167
            }
168
        }
169
        return $clauses;
170
    }
171
172
173
    /**
174
     * @return void
175
     * @throws EE_Error
176
     * @throws ReflectionException
177
     */
178
    public function employ_CPT_Strategy()
179
    {
180
        if (apply_filters('FHEE__EE_Front_Controller__employ_CPT_Strategy', true)) {
181
            $this->Registry->load_core('CPT_Strategy');
182
        }
183
    }
184
185
186
    /**
187
     * @return void
188
     * @throws InvalidArgumentException
189
     * @throws InvalidDataTypeException
190
     * @throws InvalidInterfaceException
191
     */
192
    public function loadPersistentAdminNoticeManager()
193
    {
194
        $this->persistent_admin_notice_manager = LoaderFactory::getLoader()->getShared(
195
            'EventEspresso\core\services\notifications\PersistentAdminNoticeManager'
196
        );
197
    }
198
199
200
    /**
201
     * this just makes sure that if the site is using ssl that we force that for any admin ajax calls from frontend
202
     *
203
     * @param  string $url incoming url
204
     * @return string         final assembled url
205
     */
206
    public function maybe_force_admin_ajax_ssl($url)
207
    {
208
        if (is_ssl() && preg_match('/admin-ajax.php/', $url)) {
209
            $url = str_replace('http://', 'https://', $url);
210
        }
211
        return $url;
212
    }
213
214
215
216
217
218
219
    /***********************************************        WP_LOADED ACTION HOOK         ***********************************************/
220
221
222
    /**
223
     *    wp_loaded - should fire after shortcode, module, addon, or other plugin's have been registered and their
224
     *    default priority init phases have run
225
     *
226
     * @access    public
227
     * @return    void
228
     */
229
    public function wp_loaded()
230
    {
231
    }
232
233
234
235
236
237
    /***********************************************        PARSE_REQUEST HOOK         ***********************************************/
238
    /**
239
     *    _get_request
240
     *
241
     * @access public
242
     * @param WP $WP
243
     * @return void
244
     */
245
    public function get_request(WP $WP)
246
    {
247
        do_action('AHEE__EE_Front_Controller__get_request__start');
248
        $this->Request_Handler->parse_request($WP);
249
        do_action('AHEE__EE_Front_Controller__get_request__complete');
250
    }
251
252
253
254
    /**
255
     *    pre_get_posts - basically a module factory for instantiating modules and selecting the final view template
256
     *
257
     * @access    public
258
     * @param   WP_Query $WP_Query
259
     * @return    void
260
     */
261
    public function pre_get_posts($WP_Query)
262
    {
263
        // only load Module_Request_Router if this is the main query
264
        if (
265
            $this->Module_Request_Router instanceof EE_Module_Request_Router
266
            && $WP_Query->is_main_query()
267
        ) {
268
            // cycle thru module routes
269
            while ($route = $this->Module_Request_Router->get_route($WP_Query)) {
270
                // determine module and method for route
271
                $module = $this->Module_Request_Router->resolve_route($route[0], $route[1]);
272
                if ($module instanceof EED_Module) {
273
                    // get registered view for route
274
                    $this->_template_path = $this->Module_Request_Router->get_view($route);
275
                    // grab module name
276
                    $module_name = $module->module_name();
277
                    // map the module to the module objects
278
                    $this->Registry->modules->{$module_name} = $module;
279
                }
280
            }
281
        }
282
    }
283
284
285
286
287
288
    /***********************************************        WP HOOK         ***********************************************/
289
290
291
    /**
292
     *    wp - basically last chance to do stuff before headers sent
293
     *
294
     * @access    public
295
     * @return    void
296
     */
297
    public function wp()
298
    {
299
    }
300
301
302
303
    /***********************     GET_HEADER && WP_HEAD HOOK     ***********************/
304
305
306
307
    /**
308
     * callback for the "template_redirect" hook point
309
     * checks sidebars for EE widgets
310
     * loads resources and assets accordingly
311
     *
312
     * @return void
313
     */
314
    public function templateRedirect()
315
    {
316
        global $wp_query;
317
        if (empty($wp_query->posts)){
318
            return;
319
        }
320
        // if we already know this is an espresso page, then load assets
321
        $load_assets = $this->Request_Handler->is_espresso_page();
322
        // if we are already loading assets then just move along, otherwise check for widgets
323
        $load_assets = $load_assets ? $load_assets : $this->espresso_widgets_in_active_sidebars();
324
        if ( $load_assets){
325
            add_action('wp_enqueue_scripts', array($this, 'enqueueStyle'), 10);
326
            add_action('wp_print_footer_scripts', array($this, 'enqueueScripts'), 10);
327
        }
328
    }
329
330
331
332
    /**
333
     * builds list of active widgets then scans active sidebars looking for them
334
     * returns true is an EE widget is found in an active sidebar
335
     * Please Note: this does NOT mean that the sidebar or widget
336
     * is actually in use in a given template, as that is unfortunately not known
337
     * until a sidebar and it's widgets are actually loaded
338
     *
339
     * @return boolean
340
     */
341
    private function espresso_widgets_in_active_sidebars()
342
    {
343
        $espresso_widgets = array();
344
        foreach ($this->Registry->widgets as $widget_class => $widget) {
345
            $id_base = EspressoWidget::getIdBase($widget_class);
346
            if (is_active_widget(false, false, $id_base)) {
347
                $espresso_widgets[] = $id_base;
348
            }
349
        }
350
        $all_sidebar_widgets = wp_get_sidebars_widgets();
351
        foreach ($all_sidebar_widgets as $sidebar_name => $sidebar_widgets) {
352
            if (is_array($sidebar_widgets) && ! empty($sidebar_widgets)) {
353
                foreach ($sidebar_widgets as $sidebar_widget) {
354
                    foreach ($espresso_widgets as $espresso_widget) {
355
                        if (strpos($sidebar_widget, $espresso_widget) !== false) {
356
                            return true;
357
                        }
358
                    }
359
                }
360
            }
361
        }
362
        return false;
363
    }
364
365
366
367
368
    /**
369
     *    header_meta_tag
370
     *
371
     * @access    public
372
     * @return    void
373
     */
374
    public function header_meta_tag()
375
    {
376
        print(
377
            apply_filters(
378
                'FHEE__EE_Front_Controller__header_meta_tag',
379
                '<meta name="generator" content="Event Espresso Version ' . EVENT_ESPRESSO_VERSION . "\" />\n")
380
        );
381
382
        //let's exclude all event type taxonomy term archive pages from search engine indexing
383
        //@see https://events.codebasehq.com/projects/event-espresso/tickets/10249
384
        //also exclude all critical pages from indexing
385
        if (
386
            (
387
                is_tax('espresso_event_type')
388
                && get_option( 'blog_public' ) !== '0'
389
            )
390
            || is_page(EE_Registry::instance()->CFG->core->get_critical_pages_array())
391
        ) {
392
            print(
393
                apply_filters(
394
                    'FHEE__EE_Front_Controller__header_meta_tag__noindex_for_event_type',
395
                    '<meta name="robots" content="noindex,follow" />' . "\n"
396
                )
397
            );
398
        }
399
    }
400
401
402
403
    /**
404
     * wp_print_scripts
405
     *
406
     * @return void
407
     */
408
    public function wp_print_scripts()
409
    {
410
        global $post;
411
        if (
412
            isset($post->EE_Event)
413
            && $post->EE_Event instanceof EE_Event
414
            && get_post_type() === 'espresso_events'
415
            && is_singular()
416
        ) {
417
            \EEH_Schema::add_json_linked_data_for_event($post->EE_Event);
418
        }
419
    }
420
421
422
423
    public function enqueueStyle()
424
    {
425
        wp_enqueue_style('espresso_default');
426
        wp_enqueue_style('espresso_custom_css');
427
    }
428
429
430
431
432
    /***********************************************        THE_CONTENT FILTER HOOK         **********************************************
433
434
435
436
    // /**
437
    //  *    the_content
438
    //  *
439
    //  * @access    public
440
    //  * @param   $the_content
441
    //  * @return    string
442
    //  */
443
    // public function the_content( $the_content ) {
444
    // 	// nothing gets loaded at this point unless other systems turn this hookpoint on by using:  add_filter( 'FHEE_run_EE_the_content', '__return_true' );
445
    // 	if ( apply_filters( 'FHEE_run_EE_the_content', FALSE ) ) {
446
    // 	}
447
    // 	return $the_content;
448
    // }
449
450
451
452
    /***********************************************        WP_FOOTER         ***********************************************/
453
454
455
456
    public function enqueueScripts()
457
    {
458
        wp_enqueue_script('espresso_core');
459
    }
460
461
462
463
    /**
464
     * display_errors
465
     *
466
     * @access public
467
     * @return void
468
     * @throws DomainException
469
     */
470
    public function display_errors()
471
    {
472
        static $shown_already = false;
473
        do_action('AHEE__EE_Front_Controller__display_errors__begin');
474
        if (
475
            ! $shown_already
476
            && apply_filters('FHEE__EE_Front_Controller__display_errors', true)
477
            && is_main_query()
478
            && ! is_feed()
479
            && in_the_loop()
480
            && $this->Request_Handler->is_espresso_page()
481
        ) {
482
            echo EE_Error::get_notices();
483
            $shown_already = true;
484
            EEH_Template::display_template(EE_TEMPLATES . 'espresso-ajax-notices.template.php');
485
        }
486
        do_action('AHEE__EE_Front_Controller__display_errors__end');
487
    }
488
489
490
491
492
493
    /***********************************************        UTILITIES         ***********************************************/
494
    /**
495
     *    template_include
496
     *
497
     * @access    public
498
     * @param   string $template_include_path
499
     * @return    string
500
     */
501
    public function template_include($template_include_path = null)
502
    {
503
        if ($this->Request_Handler->is_espresso_page()) {
504
            $this->_template_path = ! empty($this->_template_path) ? basename($this->_template_path) : basename($template_include_path);
505
            $template_path        = EEH_Template::locate_template($this->_template_path, array(), false);
506
            $this->_template_path = ! empty($template_path) ? $template_path : $template_include_path;
507
            $this->_template      = basename($this->_template_path);
508
            return $this->_template_path;
509
        }
510
        return $template_include_path;
511
    }
512
513
514
    /**
515
     *    get_selected_template
516
     *
517
     * @access    public
518
     * @param bool $with_path
519
     * @return    string
520
     */
521
    public function get_selected_template($with_path = false)
522
    {
523
        return $with_path ? $this->_template_path : $this->_template;
524
    }
525
526
527
528
    /**
529
     * @deprecated 4.9.26
530
     * @param string $shortcode_class
531
     * @param \WP    $wp
532
     */
533
    public function initialize_shortcode($shortcode_class = '', WP $wp = null)
534
    {
535
        \EE_Error::doing_it_wrong(
536
            __METHOD__,
537
            __(
538
                'Usage is deprecated. Please use \EventEspresso\core\services\shortcodes\LegacyShortcodesManager::initializeShortcode() instead.',
539
                'event_espresso'
540
            ),
541
            '4.9.26'
542
        );
543
        $this->getLegacyShortcodesManager()->initializeShortcode($shortcode_class, $wp);
544
    }
545
546
}
547
// End of file EE_Front_Controller.core.php
548
// Location: /core/EE_Front_Controller.core.php
549