forked from aTool-org/canvas-nest-for-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.php
264 lines (244 loc) · 10.6 KB
/
settings.php
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
if(!class_exists('WP_Canvas_Nest_Settings')) {
class WP_Canvas_Nest_Settings {
/**
* Construct the plugin object
*/
public function __construct() {
// register actions
add_action('admin_init', array(&$this, 'admin_init'));
add_action('admin_menu', array(&$this, 'add_menu'));
add_action('wp_footer', array(&$this, 'add_canvas_nest'));
} // END public function __construct
private function wrap_var($v) {
if ($v === false) return true;
if ($v) return true;
return false;
}
private function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
return array($r, $g, $b);
}
public function add_canvas_nest() {
//判断当前页面是否开启
//is_home 、 is_archive 、 is_singular 、 is_search 、 is_404
$will_show = false;
$is_home = $this->wrap_var(get_option('cn_setting_ishome'));
$is_archive = $this->wrap_var(get_option('cn_setting_isarchive'));
$is_singular = $this->wrap_var(get_option('cn_setting_issingular'));
$is_search = $this->wrap_var(get_option('cn_setting_issearch'));
$is_404 = $this->wrap_var(get_option('cn_setting_is404'));
if (is_home() && $is_home === is_home()) $will_show = true;
else if (is_singular() && $is_singular === is_singular()) $will_show = true;
else if (is_archive() && $is_archive === is_archive()) $will_show = true;
else if (is_search() && $is_search === is_search()) $will_show = true;
else if (is_404() && $is_404 === is_404()) $will_show = true;
if ($will_show) {
$setting_color = get_option('cn_setting_color');
if ($setting_color === false) $setting_color = '0,0,0';
$setting_opacity = get_option('cn_setting_opacity');
if ($setting_opacity === false) $setting_opacity = '0.5';
$setting_count = get_option('cn_setting_count');
if ($setting_count === false) $setting_count = '99';
$setting_zindex = get_option('cn_setting_zindex');
if ($setting_zindex === false) $setting_zindex = '-1';
if (strpos($setting_color, '#') === 0) {
$setting_color = $this->hex2rgb($setting_color);
$setting_color = join(',', $setting_color);
}
echo "<script type='text/javascript' color='$setting_color' zIndex='$setting_zindex' opacity='$setting_opacity' count='$setting_count' src='//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js'></script>";
}
}
/**
* hook into WP's admin_init action hook
*/
public function admin_init() {
// register your plugin's settings
register_setting('WP_Canvas_Nest-group', 'cn_setting_color');
register_setting('WP_Canvas_Nest-group', 'cn_setting_opacity');
register_setting('WP_Canvas_Nest-group', 'cn_setting_count');
register_setting('WP_Canvas_Nest-group', 'cn_setting_zindex');
// add your settings section
add_settings_section(
'WP_Canvas_Nest-section',
'1. Canvas-Nest.js Confuages(配置参数)',
array(&$this, 'settings_section_WP_Canvas_Nest'),
'WP_Canvas_Nest'
);
// add your setting's fields
add_settings_field(
'WP_Canvas_Nest-setting_color',
'Line Color: ',
array(&$this, 'settings_field_input_text'),
'WP_Canvas_Nest',
'WP_Canvas_Nest-section',
array(
'field' => 'cn_setting_color',
'value' => '0,0,0',
'type' => 'color'
)
);
add_settings_field(
'WP_Canvas_Nest-setting_opacity',
'Line Opacity: ',
array(&$this, 'settings_field_input_text'),
'WP_Canvas_Nest',
'WP_Canvas_Nest-section',
array(
'field' => 'cn_setting_opacity',
'value' => '0.5',
'type' => 'text'
)
);
add_settings_field(
'WP_Canvas_Nest-setting_count',
'Line Count: ',
array(&$this, 'settings_field_input_text'),
'WP_Canvas_Nest',
'WP_Canvas_Nest-section',
array(
'field' => 'cn_setting_count',
'value' => '99',
'type' => 'number'
)
);
add_settings_field(
'WP_Canvas_Nest-setting_zindex',
'Css zIndex): ',
array(&$this, 'settings_field_input_text'),
'WP_Canvas_Nest',
'WP_Canvas_Nest-section',
array(
'field' => 'cn_setting_zindex',
'value' => '-1',
'type' => 'number'
)
);
// setting 2
register_setting('WP_Canvas_Nest-checkbox-group', 'cn_setting_ishome');
register_setting('WP_Canvas_Nest-checkbox-group', 'cn_setting_issearch');
register_setting('WP_Canvas_Nest-checkbox-group', 'cn_setting_issingular');
register_setting('WP_Canvas_Nest-checkbox-group', 'cn_setting_isarchive');
register_setting('WP_Canvas_Nest-checkbox-group', 'cn_setting_is404');
// add your settings section
add_settings_section(
'WP_Canvas_Nest-checkbox-section',
'2. Launch in Which pages(在哪些页面开启)',
array(&$this, 'settings_section_WP_Canvas_Nest_Checkbox'),
'WP_Canvas_Nest_Checkbox'
);
add_settings_field(
'WP_Canvas_Nest-setting_ishome',
'Index(首页): ',
array(&$this, 'settings_field_checkbox'),
'WP_Canvas_Nest_Checkbox',
'WP_Canvas_Nest-checkbox-section',
array(
'field' => 'cn_setting_ishome', 'value' => true
)
);
add_settings_field(
'WP_Canvas_Nest-setting_isarchive',
'Archive(归档页): ',
array(&$this, 'settings_field_checkbox'),
'WP_Canvas_Nest_Checkbox',
'WP_Canvas_Nest-checkbox-section',
array(
'field' => 'cn_setting_isarchive', 'value' => true
)
);
add_settings_field(
'WP_Canvas_Nest-setting_issingular',
'Singular(文章单页): ',
array(&$this, 'settings_field_checkbox'),
'WP_Canvas_Nest_Checkbox',
'WP_Canvas_Nest-checkbox-section',
array(
'field' => 'cn_setting_issingular', 'value' => true
)
);
add_settings_field(
'WP_Canvas_Nest-setting_issearch',
'Search(搜索页): ',
array(&$this, 'settings_field_checkbox'),
'WP_Canvas_Nest_Checkbox',
'WP_Canvas_Nest-checkbox-section',
array(
'field' => 'cn_setting_issearch', 'value' => true
)
);
add_settings_field(
'WP_Canvas_Nest-setting_is404',
'404(404页): ',
array(&$this, 'settings_field_checkbox'),
'WP_Canvas_Nest_Checkbox',
'WP_Canvas_Nest-checkbox-section',
array(
'field' => 'cn_setting_is404', 'value' => true
)
);
//is_home、is_archive、is_singular、is_search、is_404
} // END public static function activate
public function settings_section_WP_Canvas_Nest_Checkbox() {
echo 'These configuages are setting in which pages, open the canvas-nest.js';
}
public function settings_section_WP_Canvas_Nest() {
echo 'These configuages are setting for <a target="_blank" href="https://github.com/aTool-org/canvas-nest-for-wp">Canvas-Nest.js</a>. Need Help? <a target="_blank" href="http://www.atool.org/">Click Here</a>.';
}
public function settings_field_checkbox($args) {
$field = $args['field'];
$value = $this->wrap_var(get_option($field));
if ($value) {
$value = "checked='checked'";
}
else {
$value = '';
}
echo sprintf('<input type="checkbox" name="%s" id="%s" %s />', $field, $field, $value);
}
/**
* This function provides text inputs for settings fields
*/
public function settings_field_input_text($args) {
$field = $args['field'];
$type = $args['type'];
$value = get_option($field);
if ($value === false) {
$value = $args['value'];
}
echo sprintf('<input type="%s" name="%s" id="%s" value="%s" />', $type, $field, $field, $value);
}
/**
* add a menu
*/
public function add_menu() {
// Add a page to manage this plugin's settings
add_options_page(
'Canvas-Nest.js Setting',
'Canvas-Nest.js',
'manage_options',
'WP_Canvas_Nest',
array(&$this, 'plugin_settings_page')
);
} // END public function add_menu()
/**
* Menu Callback
*/
public function plugin_settings_page() {
if(!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
// Render the settings template
include(sprintf("%s/templates/settings.php", dirname(__FILE__)));
} // END public function plugin_settings_page()
} // END class WP_Canvas_Nest_Settings
} // END if(!class_exists('WP_Canvas_Nest_Settings'))