Completed
Branch FET-3467-waitlists (871a70)
by
unknown
96:23 queued 82:42
created

EE_Front_Controller   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 480
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
dl 0
loc 480
rs 6.433
c 0
b 0
f 0
wmc 57
lcom 1
cbo 12

19 Methods

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