16
16
* @author Jory Hogeveen <[email protected] >
17
17
* @package Off_Canvas_Sidebars
18
18
* @since 0.1.0
19
- * @version 0.5.7
19
+ * @version 0.5.8
20
20
* @uses \OCS_Off_Canvas_Sidebars_Base Extends class
21
21
*/
22
22
final class OCS_Off_Canvas_Sidebars_Frontend extends OCS_Off_Canvas_Sidebars_Base
@@ -32,16 +32,63 @@ final class OCS_Off_Canvas_Sidebars_Frontend extends OCS_Off_Canvas_Sidebars_Bas
32
32
/**
33
33
* Class constructor.
34
34
* @since 0.3.0 Private constructor.
35
+ * @since 0.5.8 Check if init hook is running or already done.
35
36
* @access private
36
37
*/
37
38
private function __construct () {
38
-
39
- if ( $ this ->get_settings ( 'enable_frontend ' ) ) {
40
- $ this ->default_actions ();
39
+ if ( did_action ( 'init ' ) || doing_action ( 'init ' ) ) {
40
+ $ this ->init ();
41
+ } else {
42
+ add_action ( 'init ' , array ( $ this , 'init ' ) );
41
43
}
42
44
43
45
add_action ( 'wp_enqueue_scripts ' , array ( $ this , 'add_styles_scripts ' ) );
44
- add_action ( 'wp_head ' , array ( $ this , 'add_inline_styles ' ) );
46
+ }
47
+
48
+ /**
49
+ * Initialize frontend.
50
+ * Add default actions.
51
+ *
52
+ * @since 0.1.0
53
+ * @since 0.5.8 Renamed from `default_actions` and made public.
54
+ */
55
+ public function init () {
56
+ /**
57
+ * Enable or disable frontend rendering.
58
+ *
59
+ * @since 0.5.8
60
+ *
61
+ * @param bool $enabled Whether to enable frontend.
62
+ */
63
+ if ( ! apply_filters ( 'ocs_enable_frontend ' , (bool ) $ this ->get_settings ( 'enable_frontend ' ) ) ) {
64
+ return ;
65
+ }
66
+
67
+ $ before_hook = $ this ->get_website_before_hook ();
68
+ $ after_hook = $ this ->get_website_after_hook ();
69
+
70
+ $ before_prio = $ this ->get_settings ( 'website_before_hook_priority ' );
71
+ $ after_prio = $ this ->get_settings ( 'website_after_hook_priority ' );
72
+
73
+ if ( ! is_numeric ( $ before_prio ) ) {
74
+ $ before_prio = 5 ; // Early addition.
75
+ }
76
+ if ( ! is_numeric ( $ after_prio ) ) {
77
+ $ after_prio = 50 ; // Late addition.
78
+ if ( 'wp_footer ' === $ after_hook ) {
79
+ $ after_prio = -50 ; // Early addition (before scripts).
80
+ }
81
+ }
82
+
83
+ $ before_prio = apply_filters ( 'ocs_website_before_hook_priority ' , $ before_prio );
84
+ $ after_prio = apply_filters ( 'ocs_website_after_hook_priority ' , $ after_prio );
85
+
86
+ add_action ( $ before_hook , array ( $ this , 'before_site ' ), $ before_prio );
87
+ add_action ( $ after_hook , array ( $ this , 'after_site ' ), $ after_prio );
88
+
89
+ /* EXPERIMENTAL */
90
+ //add_action( 'wp_footer', array( $this, 'after_site' ), 0 ); // enforce first addition.
91
+ //add_action( 'wp_footer', array( $this, 'after_site_script' ), 99999 ); // enforce almost last addition.
45
92
46
93
add_filter ( 'body_class ' , array ( $ this , 'filter_body_class ' ) );
47
94
}
@@ -64,6 +111,7 @@ public function filter_body_class( $classes ) {
64
111
* Get the hook to open the website wrapper.
65
112
*
66
113
* @since 0.5.6
114
+ * @since 0.5.8 Set default to wp_body_open instead of website_before.
67
115
* @return string
68
116
*/
69
117
public function get_website_before_hook () {
@@ -73,7 +121,7 @@ public function get_website_before_hook() {
73
121
if ( 'genesis ' === get_template () ) {
74
122
$ before_hook = 'genesis_before ' ;
75
123
} else {
76
- $ before_hook = 'website_before ' ;
124
+ $ before_hook = 'wp_body_open ' ;
77
125
}
78
126
}
79
127
@@ -84,6 +132,7 @@ public function get_website_before_hook() {
84
132
* Get the hook to close the website wrapper.
85
133
*
86
134
* @since 0.5.6
135
+ * @since 0.5.8 Set default to wp_footer instead of website_after.
87
136
* @return string
88
137
*/
89
138
public function get_website_after_hook () {
@@ -93,47 +142,13 @@ public function get_website_after_hook() {
93
142
if ( 'genesis ' === get_template () ) {
94
143
$ after_hook = 'genesis_after ' ;
95
144
} else {
96
- $ after_hook = 'website_after ' ;
145
+ $ after_hook = 'wp_footer ' ;
97
146
}
98
147
}
99
148
100
149
return trim ( apply_filters ( 'ocs_website_after_hook ' , $ after_hook ) );
101
150
}
102
151
103
- /**
104
- * Add default actions
105
- *
106
- * @since 0.1.0
107
- */
108
- private function default_actions () {
109
-
110
- $ before_hook = $ this ->get_website_before_hook ();
111
- $ after_hook = $ this ->get_website_after_hook ();
112
-
113
- $ before_prio = $ this ->get_settings ( 'website_before_hook_priority ' );
114
- $ after_prio = $ this ->get_settings ( 'website_after_hook_priority ' );
115
-
116
- if ( ! is_numeric ( $ before_prio ) ) {
117
- $ before_prio = 5 ; // Early addition.
118
- }
119
- if ( ! is_numeric ( $ after_prio ) ) {
120
- $ after_prio = 50 ; // Late addition.
121
- if ( 'wp_footer ' === $ after_hook ) {
122
- $ after_prio = -50 ; // Early addition (before scripts).
123
- }
124
- }
125
-
126
- $ before_prio = apply_filters ( 'ocs_website_before_hook_priority ' , $ before_prio );
127
- $ after_prio = apply_filters ( 'ocs_website_after_hook_priority ' , $ after_prio );
128
-
129
- add_action ( $ before_hook , array ( $ this , 'before_site ' ), $ before_prio );
130
- add_action ( $ after_hook , array ( $ this , 'after_site ' ), $ after_prio );
131
-
132
- /* EXPERIMENTAL */
133
- //add_action( 'wp_footer', array( $this, 'after_site' ), 0 ); // enforce first addition.
134
- //add_action( 'wp_footer', array( $this, 'after_site_script' ), 99999 ); // enforce almost last addition.
135
- }
136
-
137
152
/**
138
153
* before_site action hook
139
154
*
@@ -531,6 +546,16 @@ public function do_control_trigger( $sidebar_id, $args = array() ) {
531
546
* @since 0.2.2 Add FastClick library.
532
547
*/
533
548
public function add_styles_scripts () {
549
+ /**
550
+ * Enable or disable frontend assets.
551
+ *
552
+ * @since 0.5.8
553
+ *
554
+ * @param bool $enabled Whether to enable frontend.
555
+ */
556
+ if ( ! apply_filters ( 'ocs_enable_assets ' , true ) ) {
557
+ return ;
558
+ }
534
559
535
560
// @todo Validate and use minified files
536
561
$ suffix = '' ;//defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
@@ -567,22 +592,22 @@ public function add_styles_scripts() {
567
592
'_debug ' => (bool ) ( defined ( 'WP_DEBUG ' ) && WP_DEBUG ),
568
593
)
569
594
);
595
+
596
+ wp_add_inline_style ( 'off-canvas-sidebars ' , $ this ->get_inline_styles () );
570
597
}
571
598
572
599
/**
573
- * Add inline styles.
600
+ * Get inline styles.
574
601
*
575
602
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
576
603
* @SuppressWarnings(PHPMD.NPathComplexity)
577
604
* @todo Refactor to enable above checks?
578
605
*
579
- * @since 0.1.0
606
+ * @since 0.1.0 Added as output method `add_inline_styles` in `wp_head` action.
607
+ * @since 0.5.8 Refactored and renamed to a getter method.
608
+ * @return string
580
609
*/
581
- public function add_inline_styles () {
582
- if ( is_admin () ) {
583
- return ;
584
- }
585
-
610
+ public function get_inline_styles () {
586
611
$ prefix = $ this ->get_settings ( 'css_prefix ' );
587
612
$ css = '' ;
588
613
@@ -635,9 +660,19 @@ public function add_inline_styles() {
635
660
}
636
661
} // End foreach().
637
662
638
- if ( $ css ) {
639
- echo '<style type="text/css"> ' . $ css . '</style> ' ;
640
- }
663
+ /**
664
+ * Modify the inline styles.
665
+ *
666
+ * @since 0.5.8
667
+ *
668
+ * @param string $css The rendered inline styles.
669
+ * @param string $prefix The CSS prefix.
670
+ *
671
+ * @return string
672
+ */
673
+ $ css = apply_filters ( 'ocs_inline_styles ' , $ css , $ prefix );
674
+
675
+ return $ css ;
641
676
}
642
677
643
678
/**
0 commit comments