aioseop_robots.php
18.0 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
/**
* @package All-in-One-SEO-Pack
*/
/**
* The Robots class.
*/
if ( !class_exists( 'All_in_One_SEO_Pack_Robots' ) ) {
class All_in_One_SEO_Pack_Robots extends All_in_One_SEO_Pack_Module {
function All_in_One_SEO_Pack_Robots( ) {
$this->name = __('Robots.txt', 'all_in_one_seo_pack'); // Human-readable name of the plugin
$this->prefix = 'aiosp_robots_'; // option prefix
$this->file = __FILE__; // the current file
parent::__construct();
$help_text = Array(
'additional'=> __('Rule Type', 'all_in_one_seo_pack'),
'useragent' => __('User Agent', 'all_in_one_seo_pack'),
'path' => __('Directory Path', 'all_in_one_seo_pack'),
'robotgen' => __('Robots.txt editor', 'all_in_one_seo_pack'),
);
$this->default_options = array(
'usage' => Array( 'type' => 'html', 'label' => 'none',
'default' => __( 'Use the rule builder below to add rules to create a new Robots.txt file. If you already have a Robots.txt file you should use the File Editor feature in All in One SEO Pack to edit it or you can delete your current Robots.txt file and start a new one with the rule builder below.', 'all_in_one_seo_pack' ),
'save' => false ),
'additional'=> Array( 'name' => __( 'Rule Type', 'all_in_one_seo_pack' ),
'save' => false,
'type' => 'select', 'initial_options' => Array( 'allow' => 'Allow', 'block' => 'Block' ) ),
'useragent' => Array( 'name' => __( 'User Agent', 'all_in_one_seo_pack' ),
'save' => false,
'type' => 'text' ),
'path' => Array( 'name' => __( 'Directory Path', 'all_in_one_seo_pack' ),
'save' => false,
'type' => 'text' ),
'robotgen' => Array( 'name' => __( 'Generate Robots.txt', 'all_in_one_seo_pack'),
'save' => false,
'default' => '', 'type' => 'textarea', 'cols' => 57, 'rows' => 20, 'label' => 'none', 'readonly' => 'readonly' ),
'Submit_Preview' => Array( 'type' => 'submit', 'class' => 'button-primary', 'name' => __( 'Add Rule', 'all_in_one_seo_pack' ) . ' »', 'nowrap' => 1, 'style' => 'margin-left: 20px;' ),
'Submit_Update' => Array( 'type' => 'submit', 'class' => 'button-primary', 'name' => __( 'Save Robots.txt File', 'all_in_one_seo_pack' ) . ' »', 'nowrap' => 1 ),
'Submit_Delete' => Array( 'type' => 'submit', 'class' => 'button-primary', 'name' => __( 'Delete Robots.txt File', 'all_in_one_seo_pack' ) . ' »', 'nowrap' => 1 ),
'optusage' => Array( 'type' => 'html', 'label' => 'none',
'default' => __( 'Click the Optimize button below and All in One SEO Pack will analyze your Robots.txt file to make sure it complies with the standards for Robots.txt files. The results will be displayed in a table below.', 'all_in_one_seo_pack' ),
'save' => false ),
'Submit_Opt_Update' => Array( 'type' => 'submit', 'class' => 'button-primary', 'name' => __( 'Update Robots.txt File', 'all_in_one_seo_pack' ) . ' »', 'nowrap' => 1, 'style' => 'margin-left: 20px;' ),
'Submit_Opt_Preview' => Array( 'type' => 'submit', 'class' => 'button-primary', 'name' => __( 'Disregard Changes', 'all_in_one_seo_pack' ) . ' »', 'nowrap' => 1),
'Submit_Optimize' => Array( 'type' => 'submit', 'class' => 'button-primary', 'name' => __('Optimize', 'all_in_one_seo_pack') . ' »' )
);
if ( !empty( $help_text ) )
foreach( $help_text as $k => $v )
$this->default_options[$k]['help_text'] = $v;
$this->locations = array( 'generator' => Array( 'name' => "Robots.txt", 'type' => 'settings',
'options' => Array( 'usage', 'additional', 'useragent', 'path', 'Submit_Preview', 'Submit_Update', 'Submit_Delete', 'robotgen', 'optusage', 'Submit_Opt_Update', 'Submit_Opt_Preview', 'Submit_Optimize' ) ),
);
$this->layout = Array(
'default' => Array(
'name' => __( 'Create a Robots.txt File', 'all_in_one_seo_pack' ),
'options' => Array( 'usage', 'additional', 'useragent', 'path', 'Submit_Preview', 'Submit_Update', 'Submit_Delete', 'robotgen' ) // this is set below, to the remaining options -- pdb
)
);
$this->layout['optimize'] = Array(
'name' => __( 'Optimize your Robots.txt File', 'all_in_one_seo_pack' ),
'options' => Array( 'optusage', 'Submit_Optimize' )
);
if ( isset( $_POST['Submit_Optimize'] ) ) {
$this->layout['optimize']['options'] = Array( 'optusage', 'Submit_Opt_Update', 'Submit_Opt_Preview', 'robothtml' );
$this->default_options['optusage']['default'] = __( "Your Robots.txt file has been optimized. Here are the results and recommendations. Click the Update Robots.txt File button below to write these changes to your Robots.txt file. Click the Disregard Changes button to ignore these recommendations and keep your current Robots.txt file.", 'all_in_one_seo_pack' );
}
// load initial options / set defaults
$this->update_options( );
add_action( $this->prefix . 'settings_update', Array( $this, 'do_robots' ), 10, 2 );
add_filter( $this->prefix . 'display_options', Array( $this, 'filter_options' ), 10, 2 );
add_filter( $this->prefix . 'submit_options', Array( $this, 'filter_submit' ), 10, 2 );
add_filter( $this->prefix . 'display_settings', Array( $this, 'filter_settings' ), 10, 2 );
}
function filter_settings( $settings, $location ) {
if ( $location == 'generator' ) {
$prefix = $this->get_prefix( $location ) . $location . '_';
if ( isset( $_POST['Submit_Optimize'] ) ) {
if ( isset( $settings[ $prefix . 'robotgen' ] ) ) {
$settings[ $prefix . 'robotgen' ]['type'] = 'hidden';
$settings[ $prefix . 'robotgen' ]['label'] = 'none';
$settings[ $prefix . 'robotgen' ]['help_text'] = '';
$settings[ $prefix . 'robothtml'] = Array( 'name' => __( 'Robots.txt', 'all_in_one_seo_pack'),
'save' => false, 'default' => '', 'type' => 'html', 'label' => 'none', 'style' => 'margin-top:10px;' );
}
}
}
return $settings;
}
function filter_submit( $submit, $location ) {
if ( $location == 'generator' ) {
unset( $submit['Submit_Default'] );
$submit['Submit']['type'] = 'hidden';
}
return $submit;
}
function filter_options( $options, $location ) {
if ( $location ) $prefix = $this->get_prefix( $location ) . $location . '_';
if ( $location === 'generator' ) {
$optimize = false;
$robotgen = '';
if ( !empty( $_POST[$prefix . 'robotgen'] ) ) $robotgen = str_replace( "\r\n", "\n", $_POST[$prefix . 'robotgen'] );
if ( isset( $_POST['Submit_Preview'] ) ) $options[$prefix . 'robotgen'] = $robotgen;
if ( !isset( $_POST['Submit_Preview'] ) ) {
if ( isset( $_POST['Submit_Optimize'] ) && !isset( $_POST['Submit_Delete'] ) && !isset( $_POST['Submit_Update'] ) && !isset( $_POST['Submit_Opt_Update'] ) )
$optimize = true;
if ( !isset( $options[$prefix . 'robotgen'] ) || empty( $options[$prefix . 'robotgen'] ) ) {
if ( $optimize ) $options[$prefix . 'robotgen'] = $robotgen;
if ( empty( $options[$prefix . 'robotgen'] ) )
$options = $this->load_files( $options, Array( 'robotgen' => 'robots.txt' ), $prefix );
}
}
$access = ( get_option('blog_public') ) ? 'allow' : 'block';
if ( $access ) {
$allow_rule = "User-agent: *\nDisallow: /wp-admin/\nDisallow: /wp-includes/\nDisallow: /xmlrpc.php\n\n";
$block_rule = "User-agent: *\nDisallow: /\n\n";
if ( empty( $options[$prefix . 'robotgen'] ) ) $options[$prefix . 'robotgen'] = '';
if ( isset( $_POST['Submit_Preview'] ) && ( ( $options[$prefix . 'robotgen'] == $allow_rule ) ||
( $options[$prefix . 'robotgen'] == $block_rule ) ) )
$options[$prefix . 'robotgen'] = '';
if ( $access === 'block' && empty( $options[$prefix . 'robotgen'] ) )
$options[$prefix . 'robotgen'] .= $block_rule;
elseif ( $access === 'allow' && empty( $options[$prefix . 'robotgen'] ) )
$options[$prefix . 'robotgen'] .= $allow_rule;
}
foreach ( Array( 'ad' => 'additional', 'ua' => 'useragent', 'dp' => 'path' ) as $k => $v )
if ( isset( $_POST[$prefix . $v] ) ) $$k = $_POST[$prefix . $v];
if ( !empty( $ad ) && !empty( $ua ) && !empty( $dp ) ) {
if ( $ad === 'allow' )
$ad = "Allow: ";
else
$ad = "Disallow: ";
$options[$prefix . 'robotgen'] .= "User-agent: $ua\n$ad $dp\n\n";
}
$file = explode("\n", $options[$prefix . 'robotgen'] );
if ( $optimize ) {
$rules = $this->parse_robots( $file );
$user_agents = $this->get_robot_user_agents( $rules );
foreach ($user_agents as $ua => $rules) {
$user_agents[$ua]['disallow'] = $this->opt_robot_rule($rules['disallow']);
$user_agents[$ua]['allow'] = $this->opt_robot_rule($rules['allow']);
}
$rules = $this->flatten_user_agents( $user_agents );
unset($user_agents);
foreach ($rules as $r) {
$r['disallow'] = $this->opt_robot_rule( $r['disallow'] );
$r['allow'] = $this->opt_robot_rule( $r['allow'] );
}
$options[$prefix . 'robotgen'] = $this->output_robots($rules);
$file2 = explode("\n", $options[$prefix . 'robotgen'] );
$options[$prefix . 'robothtml'] = '<table width=100%><tr><td valign=top width=45%>' . $this->annotate_robots_html( $file, true, __( "Current File", 'all_in_one_seo_pack' ) ) . '</td><td><span style="font-size: xx-large">→</span></td><td valign=top>' . $this->annotate_robots_html( $file2, true, __( "Proposed Changes", 'all_in_one_seo_pack' ) ) . '</td></tr></table>';
} else {
$options[$prefix . 'robothtml'] = $this->annotate_robots_html( $file, true, __( "Current File", 'all_in_one_seo_pack' ) );
}
}
return $options;
}
function do_robots( $options, $location ) {
if ( $location ) $prefix = $this->get_prefix( $location ) . $location . '_';
if ( $location === 'generator' ) {
if ( isset( $_POST['Submit_Update'] ) || isset( $_POST['Submit_Opt_Update'] ) ) {
$this->save_files( Array( 'robotgen' => 'robots.txt' ), $prefix );
} elseif (isset( $_POST['Submit_Delete'] ) ) {
$this->delete_files( Array( 'robotgen' => 'robots.txt' ) );
}
}
}
function annotate_robots_html( $file, $show_help = false, $title = '' ) {
$robots = $this->annotate_robots( $file );
if( !empty( $robots ) ){
$buf = '<table class="widefat" ><thead>';
if ( !empty( $title ) ) {
$buf .= "<tr><th colspan=3>" . $title . "</th></tr>";
}
$buf .= '<tr class="aioseop_optimize_thread">';
$buf .= '<th style="width:5%;"></th><th style="width:78%;"><span class="column_label" >Parameter</span></th>';
$buf .= '<th><span class="" >Status</span></th></tr></thead>';
$buf .= "<tbody>";
foreach ( $robots as $r ) {
$class = 'robots';
$status = "#9cf975";
$help = '';
if ( !$r['valid'] || !$r['strict'] ) {
if ( !$r['strict']) {
$class .= ' quirks';
$status="yellow";
}
if ( !$r['valid'] ) {
$class .= ' invalid';
$status="#f9534a";
}
if ( $show_help ) {
$help = '<a style="cursor:pointer;" class="' . $class . '" title="Click for Help!" onclick="toggleVisibility(\'aiosp_robots_main_legend_tip\');" title="Click for Help">'
. '<div class="aioseop_tip_icon"></div></a>';
}
}
$buf .= "<tr class='entry-row {$class}'><td>{$help}</td><td><span class='entry_label'>{$r['content']}</td><td><div style='background:{$status};'></div></td></tr>";
}
$buf .= '</tbody>';
$buf .= '</table>';
if ( $show_help ) {
$buf .= '<div class="aioseop_option_docs" id="aiosp_robots_main_legend_tip">
<h3>' . __( 'Legend', 'all_in_one_seo_pack' ) . '</h3>
<ul>
<li>' . __( 'The yellow indicator means that a non-standard extension was recognized; not all crawlers may recognize it or interpret it the same way. The Allow and Sitemap directives are commonly used by Google and Yahoo.', 'all_in_one_seo_pack' ) . '</li>
<li>' . __( 'The red indicator means that the syntax is invalid for a robots.txt file.', 'all_in_one_seo_pack') . '</li>
</ul>
<a target="_blank" rel="nofollow" href="http://wikipedia.org/wiki/Robots_exclusion_standard#Nonstandard_extensions">' . __('More Information', 'all_in_one_seo_pack') . '</a>
</div>';
}
} else {
$buf = '<p class="aioseop_error_notice" ><strong>Your Robots.txt file is either empty, cannot be found, or has invalid data.</strong></p>';
}
return $buf;
}
function annotate_robots( $robots ) {
$state = 0;
$rules = Array();
foreach ($robots as $l) {
$l = trim($l);
if (empty($l[0])) {
if ( $state > 1 ) {
$rules[] = Array( 'state' => 0, 'type' => 'blank', 'content' => $l, 'valid' => true, 'strict' => true );
$state = 0;
}
} elseif ($l[0] === '#') {
if ($state < 1) $state = 1;
$rules[] = Array( 'state' => $state, 'type' => 'comment', 'content' => $l, 'valid' => true, 'strict' => true );
} elseif ( stripos($l, 'sitemap') === 0) {
$state = 2;
$rules[] = Array( 'state' => $state, 'type' => 'sitemap', 'content' => $l, 'valid' => true, 'strict' => false );
} elseif ( stripos($l, 'crawl-delay') === 0) {
$state = 3;
$rules[] = Array( 'state' => $state, 'type' => 'crawl-delay', 'content' => $l, 'valid' => true, 'strict' => false );
} elseif ( stripos($l, 'user-agent') === 0) {
$state = 3;
$rules[] = Array( 'state' => $state, 'type' => 'user-agent', 'content' => $l, 'valid' => true, 'strict' => true );
} elseif ( stripos($l, 'useragent') === 0) {
$state = 3;
$rules[] = Array( 'state' => $state, 'type' => 'user-agent', 'content' => $l, 'valid' => true, 'strict' => false );
} elseif ( stripos($l, 'disallow') === 0) {
if ($state < 3) {
$rules[] = Array( 'state' => $state, 'type' => 'disallow', 'content' => $l, 'valid' => false, 'strict' => false );
continue;
}
$state = 3;
$rules[] = Array( 'state' => $state, 'type' => 'disallow', 'content' => $l, 'valid' => true, 'strict' => true );
} elseif ( stripos($l, 'allow') === 0) {
if ($state < 3) {
$rules[] = Array( 'state' => $state, 'type' => 'allow', 'content' => $l, 'valid' => false, 'strict' => false );
continue;
}
$state = 3;
$rules[] = Array( 'state' => $state, 'type' => 'allow', 'content' => $l, 'valid' => true, 'strict' => false );
} else {
$rules[] = Array( 'state' => $state, 'type' => 'unknown', 'content' => $l, 'valid' => false, 'strict' => false );
}
}
return $rules;
}
function parse_annotated_robots( $robots ) {
$state = 0;
$rules = Array();
$opts = Array( 'sitemap', 'crawl-delay', 'user-agent', 'allow', 'disallow', 'comment' );
$rule = Array();
foreach ( $opts as $o ) $rule[$o] = Array();
$blank_rule = $rule;
foreach ($robots as $l) {
switch ( $l['type'] ) {
case 'blank':
if ( $state >= 1 ) {
if ( ( $state === 1 ) && ( empty($rule['user-agent'] ) ) ) $rule['user-agent'] = Array( null );
$rules[] = $rule;
$rule = $blank_rule;
}
continue;
case 'comment':
$rule['comment'][] = $l['content'];
continue;
case 'sitemap':
$rule['sitemap'][] = trim( substr($l['content'], 8) );
break;
case 'crawl-delay':
$rule['crawl-delay'][] = trim( substr($l['content'], 12) );
break;
case 'user-agent':
if ($l['strict'])
$ua = trim( substr($l['content'], 11) );
else
$ua = trim( substr($l['content'], 10) );
$rule['user-agent'][] = $ua;
break;
case 'disallow':
if ($l['valid']) {
$rule['disallow'][] = trim( substr($l['content'], 9) );
break;
}
continue;
case 'allow':
if ($l['valid']) {
$rule['allow'][] = trim( substr($l['content'], 6) );
break;
}
continue;
case 'unknown':
default:
}
$state = $l['state'];
}
if ( ( $state === 1 ) && ( empty($rule['user-agent'] ) ) ) $rule['user-agent'] = Array( null );
if ($state >= 1) $rules[] = $rule;
return $rules;
}
function parse_robots( $robots ) {
return $this->parse_annotated_robots ( $this->annotate_robots( $robots ) );
}
function get_robot_user_agents($rules) {
$opts = Array( 'sitemap', 'crawl-delay', 'user-agent', 'allow', 'disallow', 'comment' );
$user_agents = Array();
foreach ($rules as $r) {
if ( !empty( $r['sitemap'] ) && empty( $r['user-agent'] ) ) $r['user-agent'] = Array( null );
foreach ($r['user-agent'] as $ua) {
if (!isset($user_agents[$ua])) $user_agents[$ua] = Array();
foreach ( $opts as $o ) {
if (!isset($user_agents[$ua][$o]))
$user_agents[$ua][$o] = $r[$o];
else
$user_agents[$ua][$o] = array_merge( $user_agents[$ua][$o], $r[$o] );
}
}
}
return $user_agents;
}
function flatten_user_agents( $user_agents ) {
$rules = Array();
foreach ($user_agents as $ua => $r) {
$r['user-agent'] = Array( $ua );
$rules[] = $r;
}
return $rules;
}
function opt_robot_rule($dis) {
if ( is_array($dis) ) { // unique rules only
$dis = array_unique( $dis, SORT_STRING );
$pd = null;
foreach( $dis as $k => $d ) {
$d = trim($d);
if ( !empty($pd) && !empty($d) ) {
if ( strpos( $d, $pd ) === 0 ) {
unset($dis[$k]);
continue; // get rid of subpaths of $pd
}
}
$l = strlen($d);
if ( ($l > 0) && ($d[$l - 1] !== '/')) continue;
$pd = $d; // only allow directory paths for $pd
}
}
return $dis;
}
function output_robots($rules) {
$robots = '';
foreach ($rules as $r) {
foreach ( $r['comment'] as $c) $robots .= "$c\n";
foreach ( $r['user-agent'] as $u) if ( $u != '' ) $robots .= "User-agent: $u\n";
foreach ( $r['crawl-delay'] as $c) $robots .= "Crawl-Delay: $c\n";
foreach ( $r['allow'] as $a) $robots .= "Allow: $a\n";
foreach ( $r['disallow'] as $d) $robots .= "Disallow: $d\n";
foreach ( $r['sitemap'] as $s) $robots .= "Sitemap: $s\n";
$robots .= "\n";
}
return $robots;
}
}
}