-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
313 lines (258 loc) · 8.1 KB
/
plugin.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
312
313
<?php
/**
* Plugin Name: Minimal Sitemap
* Description: The minimum code required to allow you to open up and manage a sitemap.xml endpoint. When enabled a dynamically generated sitemap XML will be available at /sitemap.xml. Manually add URLs or exclude URLs using Regular Expressions. Can be configured under 'Settings'.
* Version: 1.0.0
* Requires at least: 4.2
* Author: Dutchwise
* Author URI: http://www.dutchwise.nl/
* Text Domain: minsit
* Domain Path: /locale/
* Network: true
* License: MIT license (http://www.opensource.org/licenses/mit-license.php)
*/
include 'html.php';
include 'xml.php';
class MinimalSitemap {
/**
* Sanitizes sitemap settings before saving.
*
* @param array $input
* @return array
*/
public function sanitizeSitemapSettings(array $input) {
foreach($input as $key => &$value) {
$value = stripslashes(strip_tags($value));
}
$field = 'enabled';
if(array_key_exists($field, $input)) {
$input[$field] = (int)!!$input[$field];
}
return apply_filters('minsit_sanitize_sitemap_options', $input);
}
/**
* Renders the sitemap admin settings page.
*
* @return void
*/
public function renderAdminSettingsPage() {
$html = new HtmlHelper(false);
echo $html->open('div', array('class' => 'wrap'));
// start form
echo $html->open('form', array(
'action' => 'options.php',
'method' => 'POST',
'accept-charset' => get_bloginfo('charset'),
'novalidate'
));
// form title
echo $html->element('h2', __('Settings', 'minsit'));
echo $html->single('br');
// prepare form for settings (nonce, referer fields)
settings_fields('sitemap');
// renders all settings sections of the specified page
do_settings_sections('sitemap');
// renders the submit button
submit_button();
echo $html->close();
}
/**
* Renders the sitemap admin settings section.
*
* @param array $args 'id', 'title', 'callback'
* @return void
*/
public function renderAdminSitemapSettingsSection($args) {
// do nothing
}
/**
* Renders the sitemap admin settings fields.
*
* @param array $args Unknown
* @return void
*/
public function renderAdminSitemapSettingField($args) {
$options = get_option('sitemap_settings');
$html = new HtmlHelper();
$atts = array();
// if option does not exist, add to database
if($options == '') {
add_option('sitemap_settings', array());
}
// make sure the required label_for and field arguments are present to render correctly
if(!isset($args['label_for'], $args['field'])) {
throw new InvalidArgumentException('add_settings_field incorrectly configured');
}
// define attributes each field should have
$atts['id'] = $args['label_for'];
$atts['name'] = "sitemap_settings[{$args['field']}]";
// render html based on which field needs to be rendered
switch($args['field']) {
case 'enabled':
$atts['type'] = 'checkbox';
$atts['value'] = '1';
$html->single('input', array(
'id' => $atts['id'] . '_hidden',
'type' => 'hidden',
'value' => 0
) + $atts);
if(isset($options[$args['field']]) && $options[$args['field']]) {
$atts['checked'] = 'checked';
}
$html->single('input', $atts);
break;
case 'include':
case 'pattern':
$value = '';
if(isset($options[$args['field']])) {
$value = $options[$args['field']];
}
$atts['style'] = 'width: 550px;height: 199px;';
$html->element('textarea', $atts, $value);
if($args['field'] == 'pattern') {
$tip = __('Use %s to match everything before the URI.', 'minsit');
$reg = 'https?://?[da-z.-]+.[a-z.]+/';
$html->element('p', sprintf($tip, "<span style=\"color:blue\">{$reg}</span>"));
}
}
$html->close();
echo $html;
}
/**
* Runs when the WordPress admin area is initialised.
*
* @return void
*/
public function onAdminInit() {
register_setting('sitemap', 'sitemap_settings', array($this, 'sanitizeSitemapSettings'));
add_settings_section(
'sitemap_section', // ID used to identify this section and with which to register options
__( 'Sitemap', 'minsit' ), // Title to be displayed on the administration page
array($this, 'renderAdminSitemapSettingsSection'),
'sitemap' // Page on which to add this section of options
);
// regex tester app
$regex_tester_url = 'https://regex101.com/';
// field names and labels
$fields = array(
'enabled' => __('Enable Sitemap', 'minsit'),
'include' => __('Include the following return separated URLs:', 'minsit'),
'pattern' => sprintf(__('Exclude URLs matching the following return separated <a href="%s" target="_blank">Regex patterns:</a>', 'minsit'), $regex_tester_url)
);
// register and render the fields using add_settings_field and the $fields array
foreach($fields as $field => $label) {
add_settings_field(
"sitemap_settings[{$field}]",// ID used to identify the field throughout the theme
$label, // The label to the left of the option interface element
array($this, 'renderAdminSitemapSettingField'),
'sitemap', // The page on which this option will be displayed
'sitemap_section', // The name of the section to which this field belongs
array( // The array of arguments to pass to the callback.
'field' => $field,
'label_for' => $field
)
);
}
}
/**
* Adds the sitemap.xml rewrite rule.
*
* @action init
* @return void
*/
public function addRewriteRule() {
add_rewrite_rule('sitemap\.xml$', 'index.php?sitemap=xml', 'top');
}
/**
* Adds the sitemap custom query.
*
* @param array $vars
* @filter query_vars
* @return array
*/
public function addQuery($vars) {
$vars[] = 'sitemap';
return $vars;
}
/**
* Prevents the sitemap.xml being rewritten to sitemap.xml/
*
* @param string $redirect_url
* @param string|null $requested_url
* @filter redirect_canonical
* @return string|boolean
*/
public function disableCanonical($redirect_url, $requested_url = null) {
global $wp_query;
if(isset($wp_query->query_vars['sitemap'])) {
return false;
}
return $redirect_url;
}
/**
* Runs when this plugin is activated.
*
* @return void
*/
public function activate() {
$this->addRewriteRule();
flush_rewrite_rules();
}
/**
* Runs when this plugin is deactivated.
*
* @return void
*/
public function deactivate() {
flush_rewrite_rules();
}
/**
* Runs when WordPress is determining which template
* to render and send as a response.
*
* @param string $template
* @filter template_include
* @return void
*/
public function setTemplate($template) {
global $wp_query;
// load the settings array
$options = get_option('sitemap_settings');
if(!empty($options['enabled']) && isset($wp_query->query_vars['sitemap'])) {
// make sure nothing is rendered
$wp_query->is_page = 0;
$wp_query->is_archive = 0;
$wp_query->is_home = 0;
$wp_query->is_404 = 0;
$wp_query->is_post_type_archive = 0;
// set xml content type
header('Content-type: application/xml; charset=utf-8');
// return the template file that will render the sitemap
return __DIR__ . DIRECTORY_SEPARATOR . 'template.php';
}
return $template;
}
/**
* Runs when the WordPress admin menus are initialised.
*
* @return void
*/
public function onAdminMenu() {
// adds the email menu item to WordPress's main Settings menu
add_options_page(__('Sitemap Settings', 'minsit'), __('Sitemap', 'minsit'), 'manage_options', 'sitemap', array($this, 'renderAdminSettingsPage'));
}
/**
* Class constructor
*/
public function __construct() {
add_action('admin_menu', array($this, 'onAdminMenu'));
add_action('admin_init', array($this, 'onAdminInit'));
add_action('init', array($this, 'addRewriteRule'), 1);
add_filter('query_vars', array($this, 'addQuery'), 1);
add_filter('template_include', array($this, 'setTemplate'), 1);
add_filter('redirect_canonical', array($this, 'disableCanonical'));
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
}
}
new MinimalSitemap;