/
settings.php
311 lines (275 loc) · 8.78 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* settings page
*/
class wtb_seo_settings
{
/**
* register settings for our plugin
*/
function registerSettings()
{
//register our settings
register_setting('wtb_seo_settings_main_group', 'wtb_seo_main', array($this, 'sanitize'));
add_settings_section(
'wtb_seo_settings_main_section',
__('On-Page and Post Options', 'wtb_seo'),
null,
'wtb_seo_settings'
);
// ideal density
add_settings_field(
'ideal_density',
__('Ideal keyword density (default: 3%)', 'wtb_seo'),
array($this, 'create_an_ideal_density_field'),
'wtb_seo_settings',
'wtb_seo_settings_main_section'
);
// ideal lenght
add_settings_field(
'ideal_lenght',
__('Ideal post lenght (default: 250)', 'wtb_seo'),
array($this, 'create_an_ideal_lenght_field'),
'wtb_seo_settings',
'wtb_seo_settings_main_section'
);
// checkboxes to check
add_settings_field(
'to_check',
__('Parameter to check', 'wtb_seo'),
array($this, 'create_to_check_fields'),
'wtb_seo_settings',
'wtb_seo_settings_main_section'
);
// wfd or density
add_settings_field(
'wdf_vs_density',
__('Keyword density or WDF to show', 'wtb_seo'),
array($this, 'create_an_wdf_vs_density_field'),
'wtb_seo_settings',
'wtb_seo_settings_main_section'
);
// custom post types
add_settings_section(
'wtb_seo_settings_cpt_section',
__('Activate in custom post types', 'wtb_seo'),
null,
'wtb_seo_settings'
);
add_settings_field(
'cpt',
__('Custom post types', 'wtb_seo'),
array($this, 'create_an_custom_post_types_fields'),
'wtb_seo_settings',
'wtb_seo_settings_cpt_section'
);
// woocommerce
if (is_plugin_active('woocommerce/woocommerce.php')) {
add_settings_section(
'wtb_seo_settings_woo_section',
__('WooCommerce settings', 'wtb_seo'),
null,
'wtb_seo_settings'
);
add_settings_field(
'check_short_description',
__('Check short description', 'wtb_seo'),
array($this, 'create_check_short_description'),
'wtb_seo_settings',
'wtb_seo_settings_woo_section'
);
}
// auto refresh
add_settings_section(
'wtb_seo_settings_ar_section',
__('Auto refresh settings', 'wtb_seo'),
null,
'wtb_seo_settings'
);
add_settings_field(
'disable_auto_refresh',
__('Disabled', 'wtb_seo'),
array($this, 'create_auto_refresh_disable'),
'wtb_seo_settings',
'wtb_seo_settings_ar_section'
);
add_settings_field(
'auto_refresh_time',
__('Auto refresh after (sec.)', 'wtb_seo'),
array($this, 'create_auto_refresh_time'),
'wtb_seo_settings',
'wtb_seo_settings_ar_section'
);
}
/**
* renders plugin settings page
*/
function renderSettingsPage()
{
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
} ?>
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php echo __('OnPage Post SEO Settings', 'wtb_seo'); ?></h2>
<form method="post" action="options.php">
<?php
settings_fields('wtb_seo_settings_main_group');
do_settings_sections('wtb_seo_settings');
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* check input from settings page
* @param type $input
* @return type
*/
public function sanitize($input)
{
$ideal_density = round((float)str_replace(',', '.', $input['ideal_density']), 2);
if ($ideal_density) {
if ($ideal_density < 0) {
$ideal_density = 3;
}
if ($ideal_density > 100) {
$ideal_density = 100;
}
if (get_option('wtb_seo_ideal_density') === FALSE) {
add_option('wtb_seo_ideal_density', $ideal_density);
} else {
update_option('wtb_seo_ideal_density', $ideal_density);
}
}
$ideal_lenght = (int)$input['ideal_lenght'];
if ($ideal_lenght) {
if ($ideal_lenght < 0) {
$ideal_lenght = 0;
}
if (get_option('wtb_seo_ideal_lenght') === FALSE) {
add_option('wtb_seo_ideal_lenght', $ideal_lenght);
} else {
update_option('wtb_seo_ideal_lenght', $ideal_lenght);
}
}
$auto_refresh_time = (int)$input['auto_refresh_time'];
if ($auto_refresh_time > 999999) {
$auto_refresh_time = 999999;
} else if ($auto_refresh_time < 1) {
$auto_refresh_time = 1;
}
return array(
'cpt' => $input['cpt'],
'to_check' => $input['to_check'],
'disable_auto_refresh' => (int)$input['disable_auto_refresh'],
'wdf_vs_density' => $input['wdf_vs_density'],
'auto_refresh_time' => $auto_refresh_time,
'ideal_lenght' => $ideal_lenght,
'ideal_density' => $ideal_density,
'check_short_description' => (int)$input['check_short_description']
);
}
/**
* render ideal density field
*/
public function create_an_ideal_density_field()
{
?><input type="text" id="" name="wtb_seo_main[ideal_density]" value="<?php echo get_option('wtb_seo_ideal_density');?>" />% <?php
}
/**
* render ideal lenght field
*/
public function create_an_ideal_lenght_field()
{
?><input type="text" id="" name="wtb_seo_main[ideal_lenght]" value="<?php echo get_option('wtb_seo_ideal_lenght');?>" />
<?php echo __('words', 'wtb_seo');
}
/**
* render custom post types selection fields
*/
public function create_an_custom_post_types_fields()
{
$settings = get_option('wtb_seo_main', array());
$cptSettings = !empty($settings['cpt']) ? (array)$settings['cpt'] : array();
foreach (get_post_types() as $cpt) {
if (!in_array($cpt, array('attachment', 'revision', 'nav_menu_item'))) { ?>
<input type="hidden" name="wtb_seo_main[cpt][<?php echo $cpt ?>]" value="-1" />
<label>
<input type="checkbox" name="wtb_seo_main[cpt][<?php echo $cpt ?>]"
<?php checked(!empty($cptSettings[$cpt]) and $cptSettings[$cpt] == 1) ?>
value="1" />
<?php echo $cpt; ?>
</label><br />
<?php
}
}
echo '<br />';
echo '<br />';
}
public function create_to_check_fields()
{
$settings = get_option('wtb_seo_main', array());
$cptSettings = !empty($settings['to_check']) ? (array)$settings['to_check'] : array();
foreach (array(
'title' => __('Title tag', 'wtb_seo'),
'h1' => __('H1 tag', 'wtb_seo'),
'h2' => __('H2 tag', 'wtb_seo'),
'h3' => __('H3 tag', 'wtb_seo'),
'bold' => __('bold', 'wtb_seo'),
'italic' => __('italicize', 'wtb_seo'),
'underline' => __('underline', 'wtb_seo'),
'alt' => __('picture', 'wtb_seo')
) as $key => $txt) { ?>
<input type="hidden" name="wtb_seo_main[to_check][<?php echo $key ?>]" value="0" />
<label>
<input type="checkbox" name="wtb_seo_main[to_check][<?php echo $key ?>]"
<?php checked(!isset($cptSettings[$key]) or $cptSettings[$key] == 1) ?>
value="1" />
<?php echo $txt; ?>
</label><br />
<?php
}
echo '<br />';
echo '<br />';
}
public function create_check_short_description()
{
$settings = get_option('wtb_seo_main', array());
$wooSettings = isset($settings['check_short_description']) ? (int)$settings['check_short_description'] : 0;
?>
<input type="hidden" name="wtb_seo_main[check_short_description]" value="0" />
<input type="checkbox" name="wtb_seo_main[check_short_description]" value="1" <?php checked(!empty($wooSettings)) ?> /> <?php
echo '<br />';
echo '<br />';
}
public function create_auto_refresh_disable()
{
$settings = get_option('wtb_seo_main', array());
$arSettings = isset($settings['disable_auto_refresh']) ? (int)$settings['disable_auto_refresh'] : 0;
?>
<input type="hidden" name="wtb_seo_main[disable_auto_refresh]" value="0" />
<input type="checkbox" name="wtb_seo_main[disable_auto_refresh]" value="1" <?php checked(!empty($arSettings)) ?> /> <?php
}
public function create_auto_refresh_time()
{
$settings = get_option('wtb_seo_main', array());
$arSettings = isset($settings['auto_refresh_time']) ? (int)$settings['auto_refresh_time'] : 15;
?>
<input type="text" name="wtb_seo_main[auto_refresh_time]" value="<?php echo $arSettings ?>" /> <?php
}
public function create_an_wdf_vs_density_field()
{
$settings = get_option('wtb_seo_main', array());
$cptSettings = !empty($settings['wdf_vs_density']) ? $settings['wdf_vs_density'] : 'density';
?>
<select name="wtb_seo_main[wdf_vs_density]">
<option value="density" <?php echo selected($cptSettings, 'density') ?>>Density</option>
<option value="wdf" <?php echo selected($cptSettings, 'wdf') ?>>WDF</option>
</select>
<?php
echo '<br />';
echo '<br />';
}
}