woodojo-custom-code.class.php
3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
die ( 'Please do not load this screen directly. Thanks!' );
}
/**
* WooDojo - Custom Code
*
* Add custom CSS code or HTML in the <head> or before the closing </body> tag.
*
* @package WordPress
* @subpackage WooDojo
* @category Bundled
* @author WooThemes
* @since 1.0.0
*
* TABLE OF CONTENTS
*
* var $token
* var $settings_screen
* var $settings
*
* - __construct()
* - load_settings_screen()
* - enqueue_custom_css()
* - output_custom_css()
* - output_custom_html()
*/
class WooDojo_CustomCode {
/* Variable Declarations */
var $token;
var $settings_screen;
var $settings;
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct () {
/* Class Settings */
$this->token = 'woodojo-custom-code';
/* Settings Screen */
$this->load_settings_screen();
$this->settings = $this->settings_screen->get_settings();
/* Output custom CSS optionally */
if ( true == $this->settings['custom-css-enable'] && '' != $this->settings['custom-css-code'] ) {
if ( isset( $_GET[$this->token] ) && ( 'css' == $_GET[$this->token] ) ) {
add_action( 'template_redirect', array( &$this, 'output_custom_css' ), 0 );
}
add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_custom_css' ) );
}
/* Output custom HTML optionally */
if ( true == $this->settings['custom-html-enable'] ) {
if ( '' != $this->settings['custom-html-code-head'] ) {
add_action( 'wp_head', array( &$this, 'output_custom_html' ), 100 );
}
if ( '' != $this->settings['custom-html-code-footer'] ) {
add_action( 'wp_footer', array( &$this, 'output_custom_html' ), 100 );
}
}
} // End __construct()
/**
* load_settings_screen function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function load_settings_screen () {
/* Settings Screen */
require_once( 'settings.class.php' );
$this->settings_screen = new WooDojo_CustomCode_Settings();
/* Setup Data */
$this->settings_screen->token = $this->token;
if ( is_admin() ) {
if ( current_user_can( 'unfiltered_html' ) ) {
$this->settings_screen->name = __( 'WooDojo Custom CSS/HTML', 'woodojo' );
$this->settings_screen->menu_label = __( 'Custom CSS/HTML', 'woodojo' );
} else {
$this->settings_screen->name = __( 'WooDojo Custom CSS', 'woodojo' );
$this->settings_screen->menu_label = __( 'Custom CSS', 'woodojo' );
}
$this->settings_screen->page_slug = 'woodojo-custom-code';
}
$this->settings_screen->setup_settings();
} // End load_settings_screen()
/**
* enqueue_custom_css function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function enqueue_custom_css () {
wp_register_style( $this->token, home_url( '/?' . $this->token . '=css' ), 'style', '1.0.0', 'screen' );
wp_enqueue_style( $this->token );
} // End enqueue_custom_css()
/**
* output_custom_css function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function output_custom_css () {
header( 'Content-Type: text/css' );
echo stripslashes( $this->settings['custom-css-code'] );
die();
} // End output_custom_css()
/**
* output_custom_html function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function output_custom_html () {
switch ( current_filter() ) {
case 'wp_head':
echo html_entity_decode( $this->settings['custom-html-code-head'] );
break;
case 'wp_footer':
echo html_entity_decode( $this->settings['custom-html-code-footer'] );
break;
}
} // End output_custom_html()
} // End Class WooDojo_CustomCode
?>