Completed
Branch BUG-10636-remove-unnecessary-b... (5637a2)
by
unknown
32:51 queued 21:51
created

EE_Front_Controller::enqueueStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
            add_action('wp_enqueue_scripts', array($this, 'enqueueStyle'), 10);
301
            add_action('wp_print_footer_scripts', array($this, 'enqueueScripts'), 10);
302
        }
303
    }
304
305
306
307
    /**
308
     * builds list of active widgets then scans active sidebars looking for them
309
     * returns true is an EE widget is found in an active sidebar
310
     * Please Note: this does NOT mean that the sidebar or widget
311
     * is actually in use in a given template, as that is unfortunately not known
312
     * until a sidebar and it's widgets are actually loaded
313
     *
314
     * @return boolean
315
     */
316
    private function espresso_widgets_in_active_sidebars()
317
    {
318
        $espresso_widgets = array();
319
        foreach ($this->Registry->widgets as $widget_class => $widget) {
320
            $id_base = EspressoWidget::getIdBase($widget_class);
321
            if (is_active_widget(false, false, $id_base)) {
322
                $espresso_widgets[] = $id_base;
323
            }
324
        }
325
        $all_sidebar_widgets = wp_get_sidebars_widgets();
326
        foreach ($all_sidebar_widgets as $sidebar_name => $sidebar_widgets) {
327
            if (is_array($sidebar_widgets) && ! empty($sidebar_widgets)) {
328
                foreach ($sidebar_widgets as $sidebar_widget) {
329
                    foreach ($espresso_widgets as $espresso_widget) {
330
                        if (strpos($sidebar_widget, $espresso_widget) !== false) {
331
                            return true;
332
                        }
333
                    }
334
                }
335
            }
336
        }
337
        return false;
338
    }
339
340
341
342
343
    /**
344
     *    header_meta_tag
345
     *
346
     * @access    public
347
     * @return    void
348
     */
349
    public function header_meta_tag()
350
    {
351
        print(
352
            apply_filters(
353
                'FHEE__EE_Front_Controller__header_meta_tag',
354
                '<meta name="generator" content="Event Espresso Version ' . EVENT_ESPRESSO_VERSION . "\" />\n")
355
        );
356
357
        //let's exclude all event type taxonomy term archive pages from search engine indexing
358
        //@see https://events.codebasehq.com/projects/event-espresso/tickets/10249
359
        //also exclude all critical pages from indexing
360
        if (
361
            (
362
                is_tax('espresso_event_type')
363
                && get_option( 'blog_public' ) !== '0'
364
            )
365
            || is_page(EE_Registry::instance()->CFG->core->get_critical_pages_array())
366
        ) {
367
            print(
368
                apply_filters(
369
                    'FHEE__EE_Front_Controller__header_meta_tag__noindex_for_event_type',
370
                    '<meta name="robots" content="noindex,follow" />' . "\n"
371
                )
372
            );
373
        }
374
    }
375
376
377
378
    /**
379
     * wp_print_scripts
380
     *
381
     * @return void
382
     */
383
    public function wp_print_scripts()
384
    {
385
        global $post;
386
        if (
387
            isset($post->EE_Event)
388
            && $post->EE_Event instanceof EE_Event
389
            && get_post_type() === 'espresso_events'
390
            && is_singular()
391
        ) {
392
            \EEH_Schema::add_json_linked_data_for_event($post->EE_Event);
393
        }
394
    }
395
396
397
398
    public function enqueueStyle()
399
    {
400
        wp_enqueue_style('espresso_default');
401
        wp_enqueue_style('espresso_custom_css');
402
    }
403
404
405
406
407
    /***********************************************        THE_CONTENT FILTER HOOK         **********************************************
408
409
410
411
    // /**
412
    //  *    the_content
413
    //  *
414
    //  * @access    public
415
    //  * @param   $the_content
416
    //  * @return    string
417
    //  */
418
    // public function the_content( $the_content ) {
419
    // 	// nothing gets loaded at this point unless other systems turn this hookpoint on by using:  add_filter( 'FHEE_run_EE_the_content', '__return_true' );
420
    // 	if ( apply_filters( 'FHEE_run_EE_the_content', FALSE ) ) {
421
    // 	}
422
    // 	return $the_content;
423
    // }
424
425
426
427
    /***********************************************        WP_FOOTER         ***********************************************/
428
429
430
431
    public function enqueueScripts()
432
    {
433
        wp_enqueue_script('espresso_core');
434
    }
435
436
437
438
    /**
439
     * display_errors
440
     *
441
     * @access public
442
     * @return void
443
     * @throws DomainException
444
     */
445
    public function display_errors()
446
    {
447
        static $shown_already = false;
448
        do_action('AHEE__EE_Front_Controller__display_errors__begin');
449
        if (
450
            ! $shown_already
451
            && apply_filters('FHEE__EE_Front_Controller__display_errors', true)
452
            && is_main_query()
453
            && ! is_feed()
454
            && in_the_loop()
455
            && $this->Request_Handler->is_espresso_page()
456
        ) {
457
            echo EE_Error::get_notices();
458
            $shown_already = true;
459
            EEH_Template::display_template(EE_TEMPLATES . 'espresso-ajax-notices.template.php');
460
        }
461
        do_action('AHEE__EE_Front_Controller__display_errors__end');
462
    }
463
464
465
466
467
468
    /***********************************************        UTILITIES         ***********************************************/
469
    /**
470
     *    template_include
471
     *
472
     * @access    public
473
     * @param   string $template_include_path
474
     * @return    string
475
     */
476
    public function template_include($template_include_path = null)
477
    {
478
        if ($this->Request_Handler->is_espresso_page()) {
479
            $this->_template_path = ! empty($this->_template_path) ? basename($this->_template_path) : basename($template_include_path);
480
            $template_path        = EEH_Template::locate_template($this->_template_path, array(), false);
481
            $this->_template_path = ! empty($template_path) ? $template_path : $template_include_path;
482
            $this->_template      = basename($this->_template_path);
483
            return $this->_template_path;
484
        }
485
        return $template_include_path;
486
    }
487
488
489
    /**
490
     *    get_selected_template
491
     *
492
     * @access    public
493
     * @param bool $with_path
494
     * @return    string
495
     */
496
    public function get_selected_template($with_path = false)
497
    {
498
        return $with_path ? $this->_template_path : $this->_template;
499
    }
500
501
502
503
    /**
504
     * @deprecated 4.9.26
505
     * @param string $shortcode_class
506
     * @param \WP    $wp
507
     */
508
    public function initialize_shortcode($shortcode_class = '', WP $wp = null)
509
    {
510
        \EE_Error::doing_it_wrong(
511
            __METHOD__,
512
            __(
513
                'Usage is deprecated. Please use \EventEspresso\core\services\shortcodes\LegacyShortcodesManager::initializeShortcode() instead.',
514
                'event_espresso'
515
            ),
516
            '4.9.26'
517
        );
518
        $this->getLegacyShortcodesManager()->initializeShortcode($shortcode_class, $wp);
519
    }
520
521
}
522
// End of file EE_Front_Controller.core.php
523
// Location: /core/EE_Front_Controller.core.php
524