Completed
Branch BUG-10725-paypal-express-dupli... (d2dd3a)
by
unknown
12:24 queued 12s
created

EE_Front_Controller   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 481
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

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