WP htaccess Control - Version 3.4.2

Version Description

(20/4/2014) =

  • Fix: taxonomy base removal.
Download this release

Release Info

Developer antonioandra.de
Plugin Icon wp plugin WP htaccess Control
Version 3.4.2
Comparing to
See all releases

Code changes from version 3.4.1 to 3.4.2

Files changed (2) hide show
  1. readme.txt +7 -3
  2. wp-htaccess-control.php +726 -678
readme.txt CHANGED
@@ -7,7 +7,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=anton
7
  Tags: permalinks, permalink, author, htaccess, rewrite, redirect, admin, maintenance, pagination, category, category base, archive, archives
8
  Requires at least: 3.4
9
  Tested up to: 3.9
10
- Stable tag: 3.4.1
11
 
12
  Interface to customize the permalinks (author, category, archives and pagination) and htaccess file generated by WordPress.
13
 
@@ -93,9 +93,13 @@ For WordPress versions prior to 3.4 use the plugin version 2.7.2.1.
93
 
94
  == Changelog ==
95
 
96
- = 3.4.4 (20/4/2014) =
97
 
98
- * Fix: implicit array with break the admin interface on some php versions;
 
 
 
 
99
 
100
  = 3.4 (19/4/2014) =
101
 
7
  Tags: permalinks, permalink, author, htaccess, rewrite, redirect, admin, maintenance, pagination, category, category base, archive, archives
8
  Requires at least: 3.4
9
  Tested up to: 3.9
10
+ Stable tag: 3.4.2
11
 
12
  Interface to customize the permalinks (author, category, archives and pagination) and htaccess file generated by WordPress.
13
 
93
 
94
  == Changelog ==
95
 
96
+ = 3.4.2 (20/4/2014) =
97
 
98
+ * Fix: taxonomy base removal.
99
+
100
+ = 3.4.1 (20/4/2014) =
101
+
102
+ * Fix: implicit array with break the admin interface on some php versions.
103
 
104
  = 3.4 (19/4/2014) =
105
 
wp-htaccess-control.php CHANGED
@@ -27,268 +27,304 @@ Author URI: http://antonioandra.de
27
  Search redirection is based on Nice Search by Mark Jaquith, 2011 */
28
 
29
  if (!class_exists("WPhtc")) {
30
- class WPhtc {
31
-
32
- public $data;
33
-
34
- function WPhtc() {
35
- $this->data=get_option('WPhtc_data');
36
- }
 
 
 
37
  /*
38
  * @param $key
39
  */
40
- function get_data($key, $key_inner=""){
41
- if(isset($this->data[$key])) {
42
- if($key_inner!="" && isset($this->data[$key][$key_inner])){
 
43
  return $this->data[$key][$key_inner];
 
 
44
  }
45
- else{
46
- return $this->data[$key];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  }
 
 
 
48
  }
49
- else{
50
- return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  }
52
  }
53
- function init(){
54
- # set locale
55
- $currentLocale = get_locale();
56
- if(!empty($currentLocale)) {
57
- $moFile = dirname(__FILE__) . "/lang/wp-htaccess-control-" . $currentLocale . ".mo";
58
- if(@file_exists($moFile) && is_readable($moFile)) load_textdomain('wp-htaccess-control', $moFile);
59
- }
60
- }
61
- function search_template_redirect(){
62
- # redirect "?s=*" to "/search-base/*"
63
- if($_GET['s']){
64
- wp_redirect( home_url( $this->data['custom_search_permalink']. "/" . rawurlencode(get_query_var( 's' )) ) );
65
- }
66
-
67
- # rewrite query
68
- if(strpos($_SERVER["REQUEST_URI"], $this->data['custom_search_permalink'])){
69
- global $wp_query;
70
- if(strpos($_SERVER["REQUEST_URI"], '/feed')){
71
- preg_match("/feed\/(feed|rdf|rss|rss2|atom)?/",$_SERVER["REQUEST_URI"], $feed);
72
- if($feed[1]){
73
- $format="&feed=".$feed[1];
74
- }
75
- else{
76
- $format="&feed=feed";
77
- }
78
- }
79
- $page_base=($this->data['cpp']!='')?$this->data['cpp']:'page';
80
-
81
- # in need of better regex
82
- if(!strpos($_SERVER["REQUEST_URI"], '/feed')&&!strpos($_SERVER["REQUEST_URI"],$page_base)){
83
- $pattern="/\/".$this->data['custom_search_permalink']."\/(.+)/";
84
- }
85
- else{
86
- $pattern="/\/".$this->data['custom_search_permalink']."\/(.+)\/feed|".$page_base."?/";
87
- }
88
-
89
- $pattern="/\/".$this->data['custom_search_permalink']."\/(.+)/";
90
- preg_match($pattern,$_SERVER["REQUEST_URI"], $matches);
91
- $results=split("/",$matches[1]);
92
- if($results[1]==$page_base){
93
- $page="&paged=".$results[2];
94
- }
95
- $wp_query=new WP_Query('s='.$results[0].$page.$format );
96
- }
97
- }
98
- # return get_search_query on custom search base
99
- function get_search_query_filter($query){
100
-
101
- if($this->data['custom_search_permalink']!=''&&strpos($_SERVER["REQUEST_URI"], $this->data['custom_search_permalink'])){
102
- $page_base=($this->data['cpp']!='')?$this->data['cpp']:'page';
103
- # in need of better regex
104
- if(!strpos($_SERVER["REQUEST_URI"], '/feed')&&!strpos($_SERVER["REQUEST_URI"],"/".$page_base)){
105
- $pattern="/\/".$this->data['custom_search_permalink']."\/(.+)/";
106
- }
107
- else{
108
- $pattern="/\/".$this->data['custom_search_permalink']."\/(.+)\/feed|".$page_base."?/";
109
- }
110
- preg_match($pattern,$_SERVER["REQUEST_URI"], $matches);
111
- $results=split("/",$matches[1]);
112
- return urldecode($results[0]);
113
- }
114
- return $query;
115
- }
116
- function search_feed_link($link){
117
-
118
- $link=str_replace("search",$this->data['custom_search_permalink'],$link);
119
- return $link;
120
- }
121
- function check_first_run(){
122
-
123
- # flush rules to get some data filled on first usage
124
- if(!isset($this->data['htaccess_original'])){
125
- $this->refresh_rewrite_rules();
126
- }
127
- }
128
- # Flush rules
129
  /*
130
  * @var $wp_rewrite WP_Rewrite
131
  */
132
- function refresh_rewrite_rules(){
133
- global $wp_rewrite;
134
- $wp_rewrite->flush_rules();
135
- }
136
- function refresh_rewrite_rules_later(){
137
- wp_schedule_single_event(time(), 'flush_event');
138
- }
139
- # Filter pagination links generated by WordPress
140
- function filter_get_pagenum_link($result){
141
- $result=preg_replace("/".urlencode($this->data['cpp'])."\/[0-9]+[\/]?/","",$result);
142
- $result=str_replace("page",urlencode($this->data['cpp']),$result);
143
- return $result;
144
- }
145
- # Filter link generated by get_author_posts_url() to use the Custom Author Permalink
146
- function filter_author_link($link){
147
-
148
- if($this->data['remove_author_base']){
149
- $link=str_replace("author/","",$link);
150
- }
151
- else if(isset($this->data['cap']) && $this->data['cap']!=''){
152
- $link=str_replace("author",urlencode($this->data['cap']),$link);
153
- }
154
- return $link;
155
- }
156
-
157
- function filter_redirect_canonical($requested_url){
158
- global $wp;
159
-
160
- # Disable canonical redirection on urls using custom pagination permalink
161
- if(isset($this->data['cpp'])&&$this->data['cpp']!=''&&get_query_var('paged') > 1&&preg_match("/".urlencode($this->data['cpp'])."/",$wp->request)){
162
- return false;
163
- }
164
- else{
165
- return $requested_url;
166
- }
167
- }
168
-
169
- # ancestors nesting method
170
- function term_ancestors($tax,$id){
171
- $term=get_term($id,$tax);
172
- $ancestor=$term->slug;
173
-
174
- if($term->parent!=0 && !$this->data['remove_hierarchy']){
175
- $ancestor=$this->term_ancestors($tax,$term->parent)."/".$ancestor;
176
- }
177
- return $ancestor;
178
- }
179
- # Rewrite Rules: Add Category + Tag + Author Archives; Custom Pagination; Custom Author Base
180
- function filter_rewrite_rules($rewrite_rules){
181
-
182
- $new_rules=array();
183
-
184
- $page_base=($this->data['cpp']!='')?$this->data['cpp']:'page';
185
-
186
- if(isset($this->data['create_archive']) || isset($this->data['remove_taxonomy_base'])){
187
- foreach (get_taxonomies('','objects') as $taxonomy){
188
- if(!$taxonomy->rewrite || ( (isset($this->data['remove_taxonomy_base'][$taxonomy->name]) && (isset($this->data['remove_taxonomy_base'][$taxonomy->name]) && !$this->data['remove_taxonomy_base'][$taxonomy->name])) && !$this->data['create_archive'][$taxonomy->name] )){continue;}
189
- $terms=get_terms($taxonomy->name, array("hide_empty"=>false));
190
- foreach($terms as $term){
191
- $base=$this->data['remove_taxonomy_base'][$taxonomy->name]?"":$taxonomy->rewrite->slug."/";
192
- if($term->parent!=0 && !$this->data['remove_hierarchy']){
193
- $ancestors=$this->term_ancestors($taxonomy->name,$term->parent)."/";
194
- }
195
- else{
196
- $ancestors="";
197
- }
198
-
199
- $tax_name=$taxonomy->name;
200
-
201
- # create archives
202
- if(isset($this->data['create_archive'][$tax_name])){
203
- #year
204
- $new_rules[$base.$ancestors.'('.$term->slug.')/([0-9]{4})/?$'] = 'index.php?'.$tax_name.'=$matches[1]&year=$matches[2]';
205
- $new_rules[$base.$ancestors.'('.$term->slug.')/([0-9]{4})/'.$page_base.'/?([0-9]{1,})/?$'] = 'index.php?'.$tax_name.'=$matches[1]&year=$matches[2]&paged=$matches[3]';
206
- #year/month
207
- $new_rules[$base.$ancestors.'('.$term->slug.')/([0-9]{4})/([0-9]{2})/?$'] = 'index.php?'.$tax_name.'=$matches[1]&year=$matches[2]&monthnum=$matches[3]';
208
- $new_rules[$base.$ancestors.'('.$term->slug.')/([0-9]{4})/([0-9]{2})/'.$page_base.'/?([0-9]{1,})/?$'] = 'index.php?'.$tax_name.'=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]';
209
- #year/month/day
210
- $new_rules[$base.$ancestors.'('.$term->slug.')/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$'] = 'index.php?'.$tax_name.'=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]';
211
- $new_rules[$base.$ancestors.'('.$term->slug.')/([0-9]{4})/([0-9]{2})/([0-9]{2})/'.$page_base.'/?([0-9]{1,})/?$'] = 'index.php?'.$tax_name.'=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]&paged=$matches[5]';
212
- }
213
- # create rewrite rules with tax base
214
-
215
- # appending ".html" to taxonomy permalinks
216
- $suffix=$this->data['suffix_html']?".html":"";
217
-
218
- if(!$base){
219
- $new_rules[$ancestors.'('.$term->slug.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?'.$tax_name.'=$matches[1]&feed=$matches[2]';
220
- $new_rules[$ancestors.'('.$term->slug.')/page/?([0-9]{1,})/?$'] = 'index.php?'.$tax_name.'=$matches[1]&paged=$matches[2]';
221
- $new_rules[$ancestors.'('.$term->slug.')'.$suffix.'/?$'] = 'index.php?'.$tax_name.'=$matches[1]';
222
- }
223
- $rewrite_rules = $new_rules + $rewrite_rules;
224
- }
225
- }
226
- }
227
-
228
- # Author Archives
229
- if($this->data['remove_author_base']){
230
- $blogusers = get_users('who=authors');
231
- foreach ($blogusers as $user) {
232
-
233
- $new_rules = array(
234
- '('.$user->user_nicename.')/([0-9]{4})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]',
235
- '('.$user->user_nicename.')/([0-9]{4})/'.$page_base.'/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&paged=$matches[3]',
236
-
237
- '('.$user->user_nicename.')/([0-9]{4})/([0-9]{2})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]',
238
- '('.$user->user_nicename.')/([0-9]{4})/([0-9]{2})/'.$page_base.'/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]',
239
-
240
- '('.$user->user_nicename.')/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]',
241
- '('.$user->user_nicename.')/([0-9]{4})/([0-9]{2})/([0-9]{2})/'.$page_base.'/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]&paged=$matches[5]');
242
-
243
- $rewrite_rules = $new_rules + $rewrite_rules;
244
- }
245
- }
246
- else if(isset($this->data['author_archives'])){
247
- $author_base = ($this->data['cap']!='')?$this->data['cap']:'author';
248
- $new_rules = array(
249
- $author_base.'/([^/]+)/([0-9]{4})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]',
250
- $author_base.'/([^/]+)/([0-9]{4})/'.$page_base.'/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&paged=$matches[3]',
251
-
252
- $author_base.'/([^/]+)/([0-9]{4})/([0-9]{2})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]',
253
- $author_base.'/([^/]+)/([0-9]{4})/([0-9]{2})/'.$page_base.'/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]',
254
-
255
- $author_base.'/([^/]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]',
256
- $author_base.'/([^/]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/'.$page_base.'/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]&paged=$matches[5]');
257
-
258
- $rewrite_rules = $new_rules + $rewrite_rules;
259
- }
260
-
261
- # Custom Pagination; Custom Author Permalink
262
- if((isset($this->data['cap'])&&$this->data['cap']!='')||(isset($this->data['cpp'])&&$this->data['cpp']!='')){
263
- $rewrite_rules=serialize($rewrite_rules);
264
- # Author
265
- if(isset($this->data['cap'])&&$this->data['cap']!=''){
266
- $rewrite_rules=str_replace('author/',$this->data['cap'].'/',$rewrite_rules);
267
- }
268
- # Pagination
269
- if(isset($this->data['cpp'])&&$this->data['cpp']!=''){
270
- $rewrite_rules=str_replace('page/',$this->data['cpp'].'/',$rewrite_rules);
271
- }
272
- # Search
273
- if(isset($this->data['custom_search_permalink'])&&$this->data['custom_search_permalink']!=''){
274
- $rewrite_rules=str_replace('search/',$this->data['custom_search_permalink'].'/',$rewrite_rules);
275
- }
276
-
277
- $rewrite_rules=unserialize(preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'",$rewrite_rules));
278
- }
279
- return $rewrite_rules;
280
- }
281
-
282
- # Adding our Custom Author Base to the sitemap generated by Google XML Sitemaps
283
- # Adapted from sitemap-core.php (line ~2036)
284
- function set_sm(){
285
-
286
- if(class_exists('GoogleSitemapGenerator')&&$this->data['cap']!=''){
287
- $generatorObject = &GoogleSitemapGenerator::GetInstance();
288
- if ($generatorObject != null){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  /* var $wpdb WP_DB */
290
- global $wpdb, $wp_version;
291
- $sql = "SELECT DISTINCT
292
  p.ID,
293
  u.user_nicename,
294
  MAX(p.post_modified_gmt) AS last_post
@@ -300,443 +336,455 @@ if (!class_exists("WPhtc")) {
300
  AND p.post_status = 'publish'
301
  AND p.post_type = 'post'
302
  AND p.post_password = ''
303
- " . (floatval($wp_version) < 2.1?"AND p.post_date_gmt <= '" . gmdate('Y-m-d H:i:59') . "'":"") . "
304
  GROUP BY
305
  u.ID,
306
  u.user_nicename";
307
- $authors = $wpdb->get_results($sql);
308
- if($authors && is_array($authors)) {
309
- foreach($authors as $author) {
310
- $url=get_bloginfo('url')."/".$this->data['cap']."/".$author->user_nicename."/";
311
- $generatorObject->AddUrl($url,$generatorObject->GetTimestampFromMySql($author->last_post),$generatorObject->GetOption("cf_auth"),$generatorObject->GetOption("pr_auth"));
312
- }
313
- }
314
- }
315
- }
316
- }
317
- # Adding rewrite pairs to the .htaccess generated by WordPress, everytime the rules are flush
 
318
  /*
319
  * @var $wp_rewrite WP_Rewrite
320
  * @return WP_Rewrite
321
  */
322
- function wp_rewrite_rules($wp_rewrite){
323
- # Keeping a copy of the generated htaccess in our option array for later reviewing
324
- $this->data['cur_hta']='# BEGIN WordPress<br/>'.nl2br($wp_rewrite->mod_rewrite_rules())."# END WordPress";
325
- update_option('WPhtc_data',$this->data);
326
- return $wp_rewrite;
327
- }
328
- # Adding Custom Rules to htaccess file generated by WordPress, everytime the rules are flush
329
- function hta_rules($rules){
330
- $new_rules="";
331
- # keeping original hta
332
- if(!isset($this->data['htaccess_original'])){
333
- $this->data['htaccess_original']=$rules;
334
- update_option('WPhtc_data',$this->data);
335
- }
336
- # Maintenance Mode
337
- if($this->data['maintenance_active']&&isset($this->data['maintenance_ips'][0])){
338
- $no_ips=count($this->data['maintenance_ips']);
339
- $new_rules.="\n# WPhtc: Begin Maintenance Mode\n";
340
- # redirect
341
- if($this->data['maintenance_redirection']!=''){
342
- $new_rules.="RewriteEngine on\n";
343
- $new_rules.="RewriteCond %{REQUEST_URI} !\.(jpe?g|png|gif) [NC]\n";
344
- for($i=0;$i<$no_ips;$i++){
345
- $new_rules.="RewriteCond %{REMOTE_HOST} !^".str_replace(".","\.",trim($this->data['maintenance_ips'][$i]))."\n";
346
- }
347
- if(substr($this->data['maintenance_redirection'],0,1)=="/"){
348
- $new_rules.="RewriteCond %{REQUEST_URI} !".$this->data['maintenance_redirection']."$ [NC]\n";
349
- }
350
- $new_rules.="RewriteRule .* ".$this->data['maintenance_redirection']." [R=302,L]\n";
351
- }
352
- # no redirection
353
- else{
354
- $new_rules.="order deny,allow\n";
355
- $new_rules.="deny from all\n";
356
- foreach($this->data['maintenance_ips'] as $ip){
357
- $new_rules.="allow from ".$ip."\n";
358
- }
359
- }
360
- $new_rules.="# WPhtc: End Maintenance Mode\n";
361
- }
362
-
363
- # Login Control
364
- if($this->data['login_disabled']){
365
- $no_ips=count($this->data['login_ips']);
366
- $new_rules.="\n# WPhtc: Begin Login Control (start deleting here if you're having trouble logging in)\n";
367
- # redirect
368
- $new_rules.="RewriteEngine on\n";
369
- $new_rules.="RewriteCond %{REQUEST_URI} .wp-login\.php* [NC]\n";
370
- if($this->data['login_half_mode']){
371
- $new_rules.="RewriteCond %{REQUEST_METHOD} !=POST\n";
372
- $new_rules.="RewriteCond %{QUERY_STRING} !action=(logout|lostpassword|postpass|retrievepassword|resetpass|rp)*\n";
373
- }
374
- if($no_ips>0){
375
- for($i=0;$i<$no_ips;$i++){
376
- $new_rules.="RewriteCond %{REMOTE_HOST} !^".str_replace(".","\.",trim($this->data['login_ips'][$i]))."\n";
377
- }
378
- }
379
- $new_rules.="RewriteRule .* ".$this->data['login_redirection']." [R=301,L]\n";
380
- $new_rules.="# WPhtc: End Login Control Mode (stop deleting here if you're having trouble logging in)\n";
381
- }
382
- # Custom htaccess
383
- if($this->data['hta']){
384
- $new_rules.="\n# WPhtc: Begin Custom htaccess\n";
385
- $new_rules.=stripslashes($this->data['hta'])."\n";
386
- $new_rules.="# WPhtc: End Custom htaccess\n";
387
- }
388
- # htaccess suggestions
389
- if($this->data['disable_serversignature']){
390
- $new_rules.="\n# WPhtC: Disable ServerSignature on generated error pages\n";
391
- $new_rules.="ServerSignature Off\n";
392
- }
393
- if($this->data['admin_email']){
394
- $new_rules.="\n# WPhtC: Set admin email\n";
395
- $new_rules.="SetEnv SERVER_ADMIN ".$this->data['admin_email']."\n\n";
396
- }
397
- if($this->data['disable_indexes']){
398
- $new_rules.="\n# WPhtC: Disable directory browsing\n";
399
- $new_rules.="Options All -Indexes\n";
400
- }
401
- if($this->data['up_limit']){
402
- $new_rules.="\n# WPhtC: Limit upload size to ".$this->data['up_limit']." MB\n";
403
- $new_rules.="LimitRequestBody ".($this->data['up_limit']*1024*1024)." \n";
404
- }
405
- if($this->data['redirect_500']){
406
- $new_rules.="\n# WPhtC: Setting 500 Error page\n";
407
- $new_rules.="ErrorDocument 500 ".$this->data['redirect_500']."\n";
408
- }
409
- if($this->data['redirect_403']){
410
- $new_rules.="\n# WPhtC: Setting 403 Error page\n";
411
- $new_rules.="ErrorDocument 403 ".$this->data['redirect_403']."\n";
412
- }
413
- if($this->data['protect_wp_config']){
414
- $new_rules.="\n# WPhtC: Protect WP-config.php\n";
415
- $new_rules.="<files wp-config.php>\n";
416
- $new_rules.="order allow,deny\n";
417
- $new_rules.="deny from all\n";
418
- $new_rules.="</files>\n";
419
- }
420
- if($this->data['protect_htaccess']){
421
- $new_rules.="\n# WPhtC: Protect .htaccess file\n";
422
- $new_rules.="<files ~ \"^.*\.([Hh][Tt][Aa])\">\n";
423
- $new_rules.="order allow,deny\n";
424
- $new_rules.="deny from all\n";
425
- $new_rules.="</files>\n";
426
- }
427
- if($this->data['protect_comments']){
428
- $new_rules.="\n# WPhtC: Protect comments.php\n";
429
- $new_rules.="RewriteCond %{REQUEST_METHOD} POST\n";
430
- $new_rules.="RewriteCond %{REQUEST_URI} .wp-comments-post\.php*\n";
431
- $new_rules.="RewriteCond %{HTTP_REFERER} !.*".get_bloginfo('url').".* [OR]\n";
432
- $new_rules.="RewriteCond %{HTTP_USER_AGENT} ^$\n";
433
- $new_rules.="RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]\n";
434
- }
435
- if($this->data['disable_hotlink']){
436
- $new_rules.="\n# WPhtC: Disable image hotlinking\n";
437
- $new_rules.="<IfModule mod_rewrite.c>\n";
438
- $new_rules.="RewriteEngine on\n";
439
- $new_rules.="RewriteCond %{HTTP_REFERER} !^$\n";
440
- $new_rules.="RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?".str_ireplace(array("http://","www."),"",get_bloginfo("url"))."/.*$ [NC]\n";
441
- $new_rules.="RewriteRule \.(jpg|jpeg|png|gif)$ ".$this->data['disable_hotlink']." [NC,R,L]\n";
442
- $new_rules.="</IfModule>\n";
443
- }
444
- if($this->data['disable_file_hotlink_ext']){
445
- $redir = $this->data['disable_file_hotlink_redir'] ? $this->data['disable_file_hotlink_redir'] : "_";
446
- $new_rules.="\n# WPhtC: Disable file hotlinking\n";
447
- $new_rules.="<IfModule mod_rewrite.c>\n";
448
- $new_rules.="RewriteEngine on\n";
449
- $new_rules.="RewriteCond %{HTTP_REFERER} !^$\n";
450
- $new_rules.="RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?".str_ireplace(array("http://","www."),"",get_bloginfo("url"))."/.*$ [NC]\n";
451
- $new_rules.="RewriteRule \.(".str_replace(" ","|",$this->data['disable_file_hotlink_ext']).")$ ".$redir." [NC,R,L]\n";
452
- $new_rules.="</IfModule>\n";
453
- }
454
- if($this->data['canon']){
455
- $url=str_ireplace(array("http://","www."),"",get_bloginfo("url"));
456
- $domain=explode("/",$url);
457
- $escaped_domain=str_ireplace(".","\.",$domain[0]);
458
- if($this->data['canon']=='www'){
459
- $siteurl=get_option('siteurl');
460
- if (!preg_match('/^http:\/\/www\./', $siteurl)) {
461
- $siteurl=str_replace("http://","http://www.",$siteurl);
462
- update_option('siteurl',$siteurl);
463
- }
464
- $homeurl=get_option('home');
465
- if (!preg_match('/^http:\/\/www\./', $homeurl)) {
466
- $homeurl=str_replace("http://","http://www.",$homeurl);
467
- update_option('home',$homeurl);
468
- }
469
- }
470
- else if($this->data['canon']=='simple'){
471
- $siteurl=get_option('siteurl');
472
- if (preg_match('/^http:\/\/www\./', $siteurl)) {
473
- $siteurl=str_replace("http://www.","http://",$siteurl);
474
- update_option('siteurl',$siteurl);
475
- }
476
- $homeurl=get_option('home');
477
- if (preg_match('/^http:\/\/www\./', $homeurl)) {
478
- $homeurl=str_replace("http://www.","http://",$homeurl);
479
- update_option('home',$homeurl);
480
- }
481
- }
482
- }
483
- if($this->data['gzip']){
484
- $new_rules.="\n# WPhtC: Setting mod_gzip\n";
485
- $new_rules.="<ifModule mod_gzip.c>\n";
486
- $new_rules.="mod_gzip_on Yes\n";
487
- $new_rules.="mod_gzip_dechunk Yes\n";
488
- $new_rules.="mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$\n";
489
- $new_rules.="mod_gzip_item_include handler ^cgi-script$\n";
490
- $new_rules.="mod_gzip_item_include mime ^text/.*\n";
491
- $new_rules.="mod_gzip_item_include mime ^application/x-javascript.*\n";
492
- $new_rules.="mod_gzip_item_exclude mime ^image/.*\n";
493
- $new_rules.="mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*\n";
494
- $new_rules.="</ifModule>\n";
495
- }
496
- if($this->data['deflate']){
497
- $new_rules.="\n# WPhtC: Setting mod_deflate\n";
498
- $new_rules.="<IfModule mod_deflate.c>\n";
499
- $new_rules.="AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript\n";
500
- $new_rules.="BrowserMatch ^Mozilla/4 gzip-only-text/html\n";
501
- $new_rules.="BrowserMatch ^Mozilla/4.0[678] no-gzip\n";
502
- $new_rules.="BrowserMatch bMSIE !no-gzip !gzip-only-text/html\n";
503
- $new_rules.="Header append Vary User-Agent env=!dont-vary\n";
504
- $new_rules.="</IfModule>\n";
505
- }
506
- if($this->data['wp_hta']&&trim($this->data['wp_hta'])!=''){
507
- $new_rules.="\n".$this->data['wp_hta'];
508
- }
509
- else{
510
- $new_rules.="\n".$rules;
511
- }
512
- return $new_rules;
513
- }
514
-
515
- # Filter Term Link
516
- function filter_term_link($termlink, $term, $taxonomy){
517
- // WPhtc -> this
518
- if($this->data['remove_taxonomy_base'][$taxonomy]){
519
- $txs=get_taxonomies(array('name'=>$taxonomy),"object");
520
- foreach($txs as $t){
521
- //return str_replace($t->rewrite['slug']."/","",$termlink);
522
- if($term->parent!=0 && !$this->data['remove_hierarchy']){
523
- return get_bloginfo('url')."/".$this->term_ancestors($taxonomy,$term->parent)."/".$term->slug;
524
- }
525
- else{
526
- return get_bloginfo('url')."/".$term->slug;
527
- }
528
- }
529
- }
530
- else
531
- return $termlink;
532
- }
533
-
534
- # Filter Taxonomy Base
535
- function remove_taxonomy_base_from_rewrite_rules($rules){
536
- # Let's remove every taxonomy rule here, we'll reacreate them at filter_rewrite_rules()
537
- return array();
538
- //return $rules;
539
- }
540
-
541
- # Filter Author Rewrite Rules
542
- function remove_author_base_from_rewrite_rules($author_rewrite) {
543
-
544
- if($this->data['remove_author_base']){
545
- $author_rewrite=array();
546
- $blogusers = get_users('who=authors');
547
- foreach($blogusers as $user) {
548
- $author_rewrite['('.$user->user_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?author_name=$matches[1]&feed=$matches[2]';
549
- $author_rewrite['('.$user->user_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
550
- $author_rewrite['('.$user->user_nicename.')/?$'] = 'index.php?author_name=$matches[1]';
551
- }
552
- }
553
- return $author_rewrite;
554
- }
555
- # WP-htaccess Control options page interface
556
- function page(){
557
- include (dirname (__FILE__).'/wp-htaccess-control-ui.php');
558
- }
559
- # Adding our options page to the admin menu
560
- function configure_menu(){
561
- if(current_user_can("administrator")){
562
- $page=add_submenu_page("options-general.php","WP htaccess Control", "htaccess Control", 'activate_plugins', __FILE__, array('WPhtc','page'));
563
- add_action('admin_print_scripts-'.$page, array('WPhtc','page_script'));
564
- add_action('admin_print_styles-'.$page, array('WPhtc','page_style'));
565
- }
566
- }
567
- # Enqueue Ui Scripts on Plugin page
568
- function page_script(){
569
- wp_enqueue_script("htaccess-control-js", WP_PLUGIN_URL . '/wp-htaccess-control/wp-htaccess-control-ui.js');
570
- }
571
- # Enqueue Ui Scripts on Plugin page
572
- function page_style(){
573
- wp_enqueue_style("htaccess-control-css", WP_PLUGIN_URL . '/wp-htaccess-control/wp-htaccess-control-ui.css');
574
- }
575
-
576
- # Filter "View" Link on Administration
577
- function filter_tax_table_actions( $actions, $tag){
578
- if($tag->parent!=0){
579
- $actions['view']='<a href="'.get_bloginfo('url').'/'.$this->term_ancestors($tag->taxonomy,$tag->parent).'/'.$tag->slug.'">View</a>';
580
- }
581
- return $actions;
582
- }
583
-
584
- # Options Page Actions
585
- function page_action(){
586
- $this->check_first_run();
587
- if(isset($_REQUEST['action'])){
588
- $action=$_REQUEST['action'];
 
 
 
 
 
 
 
 
 
 
 
589
  global $WPhtcEcho;
590
-
591
- switch($action){
592
- # Hide donation message for one month
593
- case 'hide_donation_message':
594
- $this->data['donation_hidden_time']=time()+ 90 * 24 * 60 * 60;
595
- update_option('WPhtc_data',$this->data);
596
- break;
597
- # if reseting everything just delete the option array
598
- case 'reset_rules':
599
- # nonce
600
- if(!check_admin_referer( 'WPhtc_reset_settings')){
601
- die("You have no permission to do this.");
602
- }
603
- $this->data=array();
604
- delete_option('WPhtc_data');
605
- //update_option('WPhtc_data',$this->data);
606
- $WPhtcEcho.=__('All rules reset.', 'wp-htaccess-control');
607
- $this->refresh_rewrite_rules();
608
- break;
609
- # if updating, save new rules in database and flush rewrite rules
610
- case 'update':
611
- # first donation hidding time 'now'
612
- if(!$this->data['donation_hidden_time']){
613
- $this->data['donation_hidden_time']=time();
614
- }
615
- # nonce
616
- if(!check_admin_referer( 'WPhtc_settings')){
617
- die("You have no permission to do this.");
618
- }
619
-
620
- foreach(array("hta", "cap", "cpp", "custom_search_permalink", "remove_author_base", "category_archives", "author_archives", "tag_archives", "disable_serversignature", "disable_indexes", "protect_wp_config", "protect_htaccess", "protect_comments", "disable_hotlink", "disable_file_hotlink_ext", "disable_file_hotlink_redir", "redirect_500", "redirect_403", "canon", "admin_email", "deflate", "gzip", "maintenance_active", "maintenance_redirection", "login_disabled", "login_half_mode", "login_redirection", "sm_enabled", "jim_morgan_hta", "remove_hierarchy", "suffix_html")
621
- as $k){
622
- $this->data[$k]=isset($_POST['WPhtc_'.$k])?$_POST['WPhtc_'.$k]:'';
623
- }
624
-
625
- # wordpress htaccess and jim morgan's htaccess
626
- $this->data['wp_hta']=$_POST['WPhtc_wp_hta']."\n";
627
-
628
- # get htaccess Suggestions
629
- $this->data['up_limit']=(is_numeric($_POST['WPhtc_up_limit'])&&$_POST['WPhtc_up_limit']>0)?$_POST['WPhtc_up_limit']:'';
630
- # get maintenance
631
- $lines=preg_split("/\n|,/",$_POST['WPhtc_maintenance_ips']);
632
- $this->data['maintenance_ips']=array();
633
- foreach($lines as $line){
634
- trim($line);
635
- if(preg_match("/[a-z,0-9,\.]/",$line)){
636
- $this->data['maintenance_ips'][]=$line;
637
- }
638
- }
639
- # get login control data
640
- $lines=preg_split("/\n|,/",$_POST['WPhtc_login_ips']);
641
- $this->data['login_ips']=array();
642
- foreach($lines as $line){
643
- trim($line);
644
- if(preg_match("/[a-z,0-9,\.]/",$line)){
645
- $this->data['login_ips'][]=$line;
646
- }
647
- }
648
-
649
- # Unsetting inclusion of Author pages on Google XML Sitemap options, we'll add those links to the sitemap later with our Custom Author Permalink
650
- if($this->data['sm_enabled']==true){
651
- $SMoptions=get_option("sm_options");
652
- if($SMoptions&&is_array($SMoptions)){
653
- $SMoptions=get_option("sm_options");
654
- $SMoptions['sm_in_auth']=0;
655
- update_option('sm_options',$SMoptions);
656
- # Try to rebuild Sitemap
657
- do_action("sm_rebuild");
658
- }
659
- }
660
-
661
- # Taxonomy Base removal options
662
- unset($this->data['remove_taxonomy_base']);
663
- if($_POST['WPhtc_remove_base']){
664
- foreach($_POST['WPhtc_remove_base'] as $tax_key=>$remove){
665
- $this->data['remove_taxonomy_base'][$tax_key]=$remove;
666
- }
667
- }
668
-
669
- # Advanced archives
670
- unset($this->data['create_archive']);
671
- if(isset($_POST['WPhtc_create_archive'])){
672
- foreach($_POST['WPhtc_create_archive'] as $tax_key=>$create_archive){
673
- $this->data['create_archive'][$tax_key]=$create_archive;
674
- }
675
- }
676
-
677
- # Update WP htaccess Control options
678
- update_option('WPhtc_data',$this->data);
679
-
680
- # Flush Rewrite Rules
681
- $this->refresh_rewrite_rules();
682
- $WPhtcEcho.=__('All options updated.', 'wp-htaccess-control');
683
- break;
684
- }
685
- }
686
- }
687
- }
688
- }
689
  if (class_exists("WPhtc")) {
690
- $WPhtc = new WPhtc();
691
- }
692
  if (isset($WPhtc)) {
693
-
694
- add_action('init', array($WPhtc,'init'));
695
- add_filter('mod_rewrite_rules', array($WPhtc,'hta_rules'));
696
- add_filter('redirect_canonical',array($WPhtc,'filter_redirect_canonical'),10,10);
697
- add_action('admin_menu', array($WPhtc,'configure_menu'));
698
- add_action('flush_event',array($WPhtc,'refresh_rewrite_rules'));
699
- add_filter('generate_rewrite_rules', array($WPhtc,'wp_rewrite_rules'));
700
- add_filter('rewrite_rules_array',array($WPhtc,'filter_rewrite_rules'));
701
-
702
- // AUTHOR improve this
703
- if($WPhtc->data['cap']!='' || $WPhtc->data['remove_author_base']){
704
- add_filter('author_link',array($WPhtc,'filter_author_link'));
705
- add_filter('author_rewrite_rules', array($WPhtc,'remove_author_base_from_rewrite_rules'));
706
- }
707
-
708
- // Filter pagination links
709
- if($WPhtc->data['cpp']!='') add_filter('get_pagenum_link',array($WPhtc,'filter_get_pagenum_link'));
710
-
711
- // Filter search
712
- if(trim($WPhtc->data['custom_search_permalink'])!=''){
713
- add_filter('search_feed_link',array($WPhtc,'search_feed_link'),10,10);
714
- add_filter('get_search_query',array($WPhtc,'get_search_query_filter'),10,10);
715
- add_action('template_redirect', array($WPhtc,'search_template_redirect') );
716
- }
717
-
718
-
719
- add_action('sm_buildmap',array($WPhtc,'set_sm'));
720
-
721
- // Taxonomy Base Removal
722
- if($WPhtc->data['remove_taxonomy_base']){
723
- add_filter('term_link',array($WPhtc,'filter_term_link'),10,3);
724
- foreach($WPhtc->data['remove_taxonomy_base'] as $tax=>$v){
725
- if($v) {
726
- add_filter($tax.'_rewrite_rules', array($WPhtc,'remove_taxonomy_base_from_rewrite_rules'));
727
- add_filter($tax."_row_actions",array($WPhtc,'filter_tax_table_actions'), 10,2 );
728
- }
729
- }
730
- }
731
- /* Term management actions*/
732
- if($WPhtc->data['remove_taxonomy_base'] || $WPhtc->data['create_archive']){
733
- add_action('created_term',array($WPhtc,'refresh_rewrite_rules_later'));
734
- add_action('edited_term',array($WPhtc,'refresh_rewrite_rules_later'));
735
- add_action('delete_term',array($WPhtc,'refresh_rewrite_rules_later'));
736
- }
737
-
738
- /* This flush should maybe be conditional to content authors only, maybe not */
739
- add_action('user_register',array($WPhtc,'refresh_rewrite_rules_later'));
740
- add_action('delete_user',array($WPhtc,'refresh_rewrite_rules_later'));
741
- }
742
  ?>
27
  Search redirection is based on Nice Search by Mark Jaquith, 2011 */
28
 
29
  if (!class_exists("WPhtc")) {
30
+ class WPhtc
31
+ {
32
+
33
+ public $data;
34
+
35
+ function WPhtc()
36
+ {
37
+ $this->data = get_option('WPhtc_data');
38
+ }
39
+
40
  /*
41
  * @param $key
42
  */
43
+ function get_data($key, $key_inner = "")
44
+ {
45
+ if (isset($this->data[$key])) {
46
+ if ($key_inner != "" && isset($this->data[$key][$key_inner])) {
47
  return $this->data[$key][$key_inner];
48
+ } else {
49
+ return $this->data[$key];
50
  }
51
+ } else {
52
+ return false;
53
+ }
54
+ }
55
+
56
+ function init()
57
+ {
58
+ # set locale
59
+ $currentLocale = get_locale();
60
+ if (!empty($currentLocale)) {
61
+ $moFile = dirname(__FILE__) . "/lang/wp-htaccess-control-" . $currentLocale . ".mo";
62
+ if (@file_exists($moFile) && is_readable($moFile)) load_textdomain('wp-htaccess-control', $moFile);
63
+ }
64
+ }
65
+
66
+ function search_template_redirect()
67
+ {
68
+ # redirect "?s=*" to "/search-base/*"
69
+ if ($_GET['s']) {
70
+ wp_redirect(home_url($this->data['custom_search_permalink'] . "/" . rawurlencode(get_query_var('s'))));
71
+ }
72
+
73
+ # rewrite query
74
+ if (strpos($_SERVER["REQUEST_URI"], $this->data['custom_search_permalink'])) {
75
+ global $wp_query;
76
+ if (strpos($_SERVER["REQUEST_URI"], '/feed')) {
77
+ preg_match("/feed\/(feed|rdf|rss|rss2|atom)?/", $_SERVER["REQUEST_URI"], $feed);
78
+ if ($feed[1]) {
79
+ $format = "&feed=" . $feed[1];
80
+ } else {
81
+ $format = "&feed=feed";
82
+ }
83
+ }
84
+ $page_base = ($this->data['cpp'] != '') ? $this->data['cpp'] : 'page';
85
+
86
+ # in need of better regex
87
+ if (!strpos($_SERVER["REQUEST_URI"], '/feed') && !strpos($_SERVER["REQUEST_URI"], $page_base)) {
88
+ $pattern = "/\/" . $this->data['custom_search_permalink'] . "\/(.+)/";
89
+ } else {
90
+ $pattern = "/\/" . $this->data['custom_search_permalink'] . "\/(.+)\/feed|" . $page_base . "?/";
91
+ }
92
+
93
+ $pattern = "/\/" . $this->data['custom_search_permalink'] . "\/(.+)/";
94
+ preg_match($pattern, $_SERVER["REQUEST_URI"], $matches);
95
+ $results = split("/", $matches[1]);
96
+ if ($results[1] == $page_base) {
97
+ $page = "&paged=" . $results[2];
98
+ }
99
+ $wp_query = new WP_Query('s=' . $results[0] . $page . $format);
100
+ }
101
+ }
102
+
103
+ # return get_search_query on custom search base
104
+ function get_search_query_filter($query)
105
+ {
106
+
107
+ if ($this->data['custom_search_permalink'] != '' && strpos($_SERVER["REQUEST_URI"], $this->data['custom_search_permalink'])) {
108
+ $page_base = ($this->data['cpp'] != '') ? $this->data['cpp'] : 'page';
109
+ # in need of better regex
110
+ if (!strpos($_SERVER["REQUEST_URI"], '/feed') && !strpos($_SERVER["REQUEST_URI"], "/" . $page_base)) {
111
+ $pattern = "/\/" . $this->data['custom_search_permalink'] . "\/(.+)/";
112
+ } else {
113
+ $pattern = "/\/" . $this->data['custom_search_permalink'] . "\/(.+)\/feed|" . $page_base . "?/";
114
  }
115
+ preg_match($pattern, $_SERVER["REQUEST_URI"], $matches);
116
+ $results = split("/", $matches[1]);
117
+ return urldecode($results[0]);
118
  }
119
+ return $query;
120
+ }
121
+
122
+ function search_feed_link($link)
123
+ {
124
+
125
+ $link = str_replace("search", $this->data['custom_search_permalink'], $link);
126
+ return $link;
127
+ }
128
+
129
+ function check_first_run()
130
+ {
131
+
132
+ # flush rules to get some data filled on first usage
133
+ if (!isset($this->data['htaccess_original'])) {
134
+ $this->refresh_rewrite_rules();
135
  }
136
  }
137
+
138
+ # Flush rules
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  /*
140
  * @var $wp_rewrite WP_Rewrite
141
  */
142
+ function refresh_rewrite_rules()
143
+ {
144
+ global $wp_rewrite;
145
+ $wp_rewrite->flush_rules();
146
+ }
147
+
148
+ function refresh_rewrite_rules_later()
149
+ {
150
+ wp_schedule_single_event(time(), 'flush_event');
151
+ }
152
+
153
+ # Filter pagination links generated by WordPress
154
+ function filter_get_pagenum_link($result)
155
+ {
156
+ $result = preg_replace("/" . urlencode($this->data['cpp']) . "\/[0-9]+[\/]?/", "", $result);
157
+ $result = str_replace("page", urlencode($this->data['cpp']), $result);
158
+ return $result;
159
+ }
160
+
161
+ # Filter link generated by get_author_posts_url() to use the Custom Author Permalink
162
+ function filter_author_link($link)
163
+ {
164
+
165
+ if ($this->data['remove_author_base']) {
166
+ $link = str_replace("author/", "", $link);
167
+ } else if (isset($this->data['cap']) && $this->data['cap'] != '') {
168
+ $link = str_replace("author", urlencode($this->data['cap']), $link);
169
+ }
170
+ return $link;
171
+ }
172
+
173
+ function filter_redirect_canonical($requested_url)
174
+ {
175
+ global $wp;
176
+
177
+ # Disable canonical redirection on urls using custom pagination permalink
178
+ if (isset($this->data['cpp']) && $this->data['cpp'] != '' && get_query_var('paged') > 1 && preg_match("/" . urlencode($this->data['cpp']) . "/", $wp->request)) {
179
+ return false;
180
+ } else {
181
+ return $requested_url;
182
+ }
183
+ }
184
+
185
+ # ancestors nesting method
186
+ function term_ancestors($tax, $id)
187
+ {
188
+ $term = get_term($id, $tax);
189
+ $ancestor = $term->slug;
190
+
191
+ if ($term->parent != 0 && !$this->data['remove_hierarchy']) {
192
+ $ancestor = $this->term_ancestors($tax, $term->parent) . "/" . $ancestor;
193
+ }
194
+ return $ancestor;
195
+ }
196
+
197
+ # Rewrite Rules: Add Category + Tag + Author Archives; Custom Pagination; Custom Author Base
198
+ function filter_rewrite_rules($rewrite_rules)
199
+ {
200
+
201
+ $new_rules = array();
202
+
203
+ $page_base = ($this->data['cpp'] != '') ? $this->data['cpp'] : 'page';
204
+
205
+ // if we have either to remove the tax base or create tax archives
206
+ if (isset($this->data['create_archive']) or isset($this->data['remove_taxonomy_base'])) {
207
+ foreach (get_taxonomies('', 'objects') as $taxonomy) {
208
+ if (!$taxonomy->rewrite
209
+ or (
210
+ (
211
+ isset($this->data['remove_taxonomy_base'][$taxonomy->name])
212
+ and (
213
+ isset($this->data['remove_taxonomy_base'][$taxonomy->name])
214
+ and !$this->data['remove_taxonomy_base'][$taxonomy->name])
215
+ )
216
+ and !$this->data['create_archive'][$taxonomy->name]
217
+ )
218
+ ) {
219
+ continue;
220
+ }
221
+ /*
222
+ * @todo this should be rewritten: UNLESS we are removing the tax base we should be using a wildcard for tax term
223
+ */
224
+ $terms = get_terms($taxonomy->name, array("hide_empty" => false));
225
+ foreach ($terms as $term) {
226
+ $base = isset($this->data['remove_taxonomy_base'], $this->data['remove_taxonomy_base'][$taxonomy->name])
227
+ ? ""
228
+ : $taxonomy->rewrite['slug'] . "/";
229
+ if ($term->parent != 0 && !$this->data['remove_hierarchy']) {
230
+ $ancestors = $this->term_ancestors($taxonomy->name, $term->parent) . "/";
231
+ } else {
232
+ $ancestors = "";
233
+ }
234
+
235
+ $tax_name = $taxonomy->name;
236
+
237
+ # create archives
238
+ if (isset($this->data['create_archive'][$tax_name])) {
239
+ #year
240
+ $new_rules[$base . $ancestors . '(' . $term->slug . ')/([0-9]{4})/?$'] = 'index.php?' . $tax_name . '=$matches[1]&year=$matches[2]';
241
+ $new_rules[$base . $ancestors . '(' . $term->slug . ')/([0-9]{4})/' . $page_base . '/?([0-9]{1,})/?$'] = 'index.php?' . $tax_name . '=$matches[1]&year=$matches[2]&paged=$matches[3]';
242
+ #year/month
243
+ $new_rules[$base . $ancestors . '(' . $term->slug . ')/([0-9]{4})/([0-9]{2})/?$'] = 'index.php?' . $tax_name . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]';
244
+ $new_rules[$base . $ancestors . '(' . $term->slug . ')/([0-9]{4})/([0-9]{2})/' . $page_base . '/?([0-9]{1,})/?$'] = 'index.php?' . $tax_name . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]';
245
+ #year/month/day
246
+ $new_rules[$base . $ancestors . '(' . $term->slug . ')/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$'] = 'index.php?' . $tax_name . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]';
247
+ $new_rules[$base . $ancestors . '(' . $term->slug . ')/([0-9]{4})/([0-9]{2})/([0-9]{2})/' . $page_base . '/?([0-9]{1,})/?$'] = 'index.php?' . $tax_name . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]&paged=$matches[5]';
248
+ }
249
+ # create rewrite rules with tax base
250
+
251
+ # appending ".html" to taxonomy permalinks
252
+ $suffix = $this->data['suffix_html'] ? ".html" : "";
253
+
254
+ if (!$base) {
255
+ $new_rules[$ancestors . '(' . $term->slug . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?' . $tax_name . '=$matches[1]&feed=$matches[2]';
256
+ $new_rules[$ancestors . '(' . $term->slug . ')/page/?([0-9]{1,})/?$'] = 'index.php?' . $tax_name . '=$matches[1]&paged=$matches[2]';
257
+ $new_rules[$ancestors . '(' . $term->slug . ')' . $suffix . '/?$'] = 'index.php?' . $tax_name . '=$matches[1]';
258
+ }
259
+ $rewrite_rules = $new_rules + $rewrite_rules;
260
+ }
261
+ }
262
+ }
263
+
264
+ # Author Archives
265
+ if ($this->data['remove_author_base']) {
266
+ $blogusers = get_users('who=authors');
267
+ foreach ($blogusers as $user) {
268
+
269
+ $new_rules = array(
270
+ '(' . $user->user_nicename . ')/([0-9]{4})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]',
271
+ '(' . $user->user_nicename . ')/([0-9]{4})/' . $page_base . '/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&paged=$matches[3]',
272
+
273
+ '(' . $user->user_nicename . ')/([0-9]{4})/([0-9]{2})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]',
274
+ '(' . $user->user_nicename . ')/([0-9]{4})/([0-9]{2})/' . $page_base . '/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]',
275
+
276
+ '(' . $user->user_nicename . ')/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]',
277
+ '(' . $user->user_nicename . ')/([0-9]{4})/([0-9]{2})/([0-9]{2})/' . $page_base . '/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]&paged=$matches[5]');
278
+
279
+ $rewrite_rules = $new_rules + $rewrite_rules;
280
+ }
281
+ } else if (isset($this->data['author_archives'])) {
282
+ $author_base = ($this->data['cap'] != '') ? $this->data['cap'] : 'author';
283
+ $new_rules = array(
284
+ $author_base . '/([^/]+)/([0-9]{4})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]',
285
+ $author_base . '/([^/]+)/([0-9]{4})/' . $page_base . '/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&paged=$matches[3]',
286
+
287
+ $author_base . '/([^/]+)/([0-9]{4})/([0-9]{2})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]',
288
+ $author_base . '/([^/]+)/([0-9]{4})/([0-9]{2})/' . $page_base . '/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]',
289
+
290
+ $author_base . '/([^/]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]',
291
+ $author_base . '/([^/]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/' . $page_base . '/?([0-9]{1,})/?$' => 'index.php?author_name=$matches[1]&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]&paged=$matches[5]');
292
+
293
+ $rewrite_rules = $new_rules + $rewrite_rules;
294
+ }
295
+
296
+ # Custom Pagination; Custom Author Permalink
297
+ if ((isset($this->data['cap']) && $this->data['cap'] != '') || (isset($this->data['cpp']) && $this->data['cpp'] != '')) {
298
+ $rewrite_rules = serialize($rewrite_rules);
299
+ # Author
300
+ if (isset($this->data['cap']) && $this->data['cap'] != '') {
301
+ $rewrite_rules = str_replace('author/', $this->data['cap'] . '/', $rewrite_rules);
302
+ }
303
+ # Pagination
304
+ if (isset($this->data['cpp']) && $this->data['cpp'] != '') {
305
+ $rewrite_rules = str_replace('page/', $this->data['cpp'] . '/', $rewrite_rules);
306
+ }
307
+ # Search
308
+ if (isset($this->data['custom_search_permalink']) && $this->data['custom_search_permalink'] != '') {
309
+ $rewrite_rules = str_replace('search/', $this->data['custom_search_permalink'] . '/', $rewrite_rules);
310
+ }
311
+
312
+ $rewrite_rules = unserialize(preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $rewrite_rules));
313
+ }
314
+ return $rewrite_rules;
315
+ }
316
+
317
+ # Adding our Custom Author Base to the sitemap generated by Google XML Sitemaps
318
+ # Adapted from sitemap-core.php (line ~2036)
319
+ function set_sm()
320
+ {
321
+
322
+ if (class_exists('GoogleSitemapGenerator') && $this->data['cap'] != '') {
323
+ $generatorObject = & GoogleSitemapGenerator::GetInstance();
324
+ if ($generatorObject != null) {
325
  /* var $wpdb WP_DB */
326
+ global $wpdb, $wp_version;
327
+ $sql = "SELECT DISTINCT
328
  p.ID,
329
  u.user_nicename,
330
  MAX(p.post_modified_gmt) AS last_post
336
  AND p.post_status = 'publish'
337
  AND p.post_type = 'post'
338
  AND p.post_password = ''
339
+ " . (floatval($wp_version) < 2.1 ? "AND p.post_date_gmt <= '" . gmdate('Y-m-d H:i:59') . "'" : "") . "
340
  GROUP BY
341
  u.ID,
342
  u.user_nicename";
343
+ $authors = $wpdb->get_results($sql);
344
+ if ($authors && is_array($authors)) {
345
+ foreach ($authors as $author) {
346
+ $url = get_bloginfo('url') . "/" . $this->data['cap'] . "/" . $author->user_nicename . "/";
347
+ $generatorObject->AddUrl($url, $generatorObject->GetTimestampFromMySql($author->last_post), $generatorObject->GetOption("cf_auth"), $generatorObject->GetOption("pr_auth"));
348
+ }
349
+ }
350
+ }
351
+ }
352
+ }
353
+
354
+ # Adding rewrite pairs to the .htaccess generated by WordPress, everytime the rules are flush
355
  /*
356
  * @var $wp_rewrite WP_Rewrite
357
  * @return WP_Rewrite
358
  */
359
+ function wp_rewrite_rules($wp_rewrite)
360
+ {
361
+ # Keeping a copy of the generated htaccess in our option array for later reviewing
362
+ $this->data['cur_hta'] = '# BEGIN WordPress<br/>' . nl2br($wp_rewrite->mod_rewrite_rules()) . "# END WordPress";
363
+ update_option('WPhtc_data', $this->data);
364
+ return $wp_rewrite;
365
+ }
366
+
367
+ # Adding Custom Rules to htaccess file generated by WordPress, everytime the rules are flush
368
+ function hta_rules($rules)
369
+ {
370
+ $new_rules = "";
371
+ # keeping original hta
372
+ if (!isset($this->data['htaccess_original'])) {
373
+ $this->data['htaccess_original'] = $rules;
374
+ update_option('WPhtc_data', $this->data);
375
+ }
376
+ # Maintenance Mode
377
+ if ($this->data['maintenance_active'] && isset($this->data['maintenance_ips'][0])) {
378
+ $no_ips = count($this->data['maintenance_ips']);
379
+ $new_rules .= "\n# WPhtc: Begin Maintenance Mode\n";
380
+ # redirect
381
+ if ($this->data['maintenance_redirection'] != '') {
382
+ $new_rules .= "RewriteEngine on\n";
383
+ $new_rules .= "RewriteCond %{REQUEST_URI} !\.(jpe?g|png|gif) [NC]\n";
384
+ for ($i = 0; $i < $no_ips; $i++) {
385
+ $new_rules .= "RewriteCond %{REMOTE_HOST} !^" . str_replace(".", "\.", trim($this->data['maintenance_ips'][$i])) . "\n";
386
+ }
387
+ if (substr($this->data['maintenance_redirection'], 0, 1) == "/") {
388
+ $new_rules .= "RewriteCond %{REQUEST_URI} !" . $this->data['maintenance_redirection'] . "$ [NC]\n";
389
+ }
390
+ $new_rules .= "RewriteRule .* " . $this->data['maintenance_redirection'] . " [R=302,L]\n";
391
+ } # no redirection
392
+ else {
393
+ $new_rules .= "order deny,allow\n";
394
+ $new_rules .= "deny from all\n";
395
+ foreach ($this->data['maintenance_ips'] as $ip) {
396
+ $new_rules .= "allow from " . $ip . "\n";
397
+ }
398
+ }
399
+ $new_rules .= "# WPhtc: End Maintenance Mode\n";
400
+ }
401
+
402
+ # Login Control
403
+ if ($this->data['login_disabled']) {
404
+ $no_ips = count($this->data['login_ips']);
405
+ $new_rules .= "\n# WPhtc: Begin Login Control (start deleting here if you're having trouble logging in)\n";
406
+ # redirect
407
+ $new_rules .= "RewriteEngine on\n";
408
+ $new_rules .= "RewriteCond %{REQUEST_URI} .wp-login\.php* [NC]\n";
409
+ if ($this->data['login_half_mode']) {
410
+ $new_rules .= "RewriteCond %{REQUEST_METHOD} !=POST\n";
411
+ $new_rules .= "RewriteCond %{QUERY_STRING} !action=(logout|lostpassword|postpass|retrievepassword|resetpass|rp)*\n";
412
+ }
413
+ if ($no_ips > 0) {
414
+ for ($i = 0; $i < $no_ips; $i++) {
415
+ $new_rules .= "RewriteCond %{REMOTE_HOST} !^" . str_replace(".", "\.", trim($this->data['login_ips'][$i])) . "\n";
416
+ }
417
+ }
418
+ $new_rules .= "RewriteRule .* " . $this->data['login_redirection'] . " [R=301,L]\n";
419
+ $new_rules .= "# WPhtc: End Login Control Mode (stop deleting here if you're having trouble logging in)\n";
420
+ }
421
+ # Custom htaccess
422
+ if ($this->data['hta']) {
423
+ $new_rules .= "\n# WPhtc: Begin Custom htaccess\n";
424
+ $new_rules .= stripslashes($this->data['hta']) . "\n";
425
+ $new_rules .= "# WPhtc: End Custom htaccess\n";
426
+ }
427
+ # htaccess suggestions
428
+ if ($this->data['disable_serversignature']) {
429
+ $new_rules .= "\n# WPhtC: Disable ServerSignature on generated error pages\n";
430
+ $new_rules .= "ServerSignature Off\n";
431
+ }
432
+ if ($this->data['admin_email']) {
433
+ $new_rules .= "\n# WPhtC: Set admin email\n";
434
+ $new_rules .= "SetEnv SERVER_ADMIN " . $this->data['admin_email'] . "\n\n";
435
+ }
436
+ if ($this->data['disable_indexes']) {
437
+ $new_rules .= "\n# WPhtC: Disable directory browsing\n";
438
+ $new_rules .= "Options All -Indexes\n";
439
+ }
440
+ if ($this->data['up_limit']) {
441
+ $new_rules .= "\n# WPhtC: Limit upload size to " . $this->data['up_limit'] . " MB\n";
442
+ $new_rules .= "LimitRequestBody " . ($this->data['up_limit'] * 1024 * 1024) . " \n";
443
+ }
444
+ if ($this->data['redirect_500']) {
445
+ $new_rules .= "\n# WPhtC: Setting 500 Error page\n";
446
+ $new_rules .= "ErrorDocument 500 " . $this->data['redirect_500'] . "\n";
447
+ }
448
+ if ($this->data['redirect_403']) {
449
+ $new_rules .= "\n# WPhtC: Setting 403 Error page\n";
450
+ $new_rules .= "ErrorDocument 403 " . $this->data['redirect_403'] . "\n";
451
+ }
452
+ if ($this->data['protect_wp_config']) {
453
+ $new_rules .= "\n# WPhtC: Protect WP-config.php\n";
454
+ $new_rules .= "<files wp-config.php>\n";
455
+ $new_rules .= "order allow,deny\n";
456
+ $new_rules .= "deny from all\n";
457
+ $new_rules .= "</files>\n";
458
+ }
459
+ if ($this->data['protect_htaccess']) {
460
+ $new_rules .= "\n# WPhtC: Protect .htaccess file\n";
461
+ $new_rules .= "<files ~ \"^.*\.([Hh][Tt][Aa])\">\n";
462
+ $new_rules .= "order allow,deny\n";
463
+ $new_rules .= "deny from all\n";
464
+ $new_rules .= "</files>\n";
465
+ }
466
+ if ($this->data['protect_comments']) {
467
+ $new_rules .= "\n# WPhtC: Protect comments.php\n";
468
+ $new_rules .= "RewriteCond %{REQUEST_METHOD} POST\n";
469
+ $new_rules .= "RewriteCond %{REQUEST_URI} .wp-comments-post\.php*\n";
470
+ $new_rules .= "RewriteCond %{HTTP_REFERER} !.*" . get_bloginfo('url') . ".* [OR]\n";
471
+ $new_rules .= "RewriteCond %{HTTP_USER_AGENT} ^$\n";
472
+ $new_rules .= "RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]\n";
473
+ }
474
+ if ($this->data['disable_hotlink']) {
475
+ $new_rules .= "\n# WPhtC: Disable image hotlinking\n";
476
+ $new_rules .= "<IfModule mod_rewrite.c>\n";
477
+ $new_rules .= "RewriteEngine on\n";
478
+ $new_rules .= "RewriteCond %{HTTP_REFERER} !^$\n";
479
+ $new_rules .= "RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?" . str_ireplace(array("http://", "www."), "", get_bloginfo("url")) . "/.*$ [NC]\n";
480
+ $new_rules .= "RewriteRule \.(jpg|jpeg|png|gif)$ " . $this->data['disable_hotlink'] . " [NC,R,L]\n";
481
+ $new_rules .= "</IfModule>\n";
482
+ }
483
+ if ($this->data['disable_file_hotlink_ext']) {
484
+ $redir = $this->data['disable_file_hotlink_redir'] ? $this->data['disable_file_hotlink_redir'] : "_";
485
+ $new_rules .= "\n# WPhtC: Disable file hotlinking\n";
486
+ $new_rules .= "<IfModule mod_rewrite.c>\n";
487
+ $new_rules .= "RewriteEngine on\n";
488
+ $new_rules .= "RewriteCond %{HTTP_REFERER} !^$\n";
489
+ $new_rules .= "RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?" . str_ireplace(array("http://", "www."), "", get_bloginfo("url")) . "/.*$ [NC]\n";
490
+ $new_rules .= "RewriteRule \.(" . str_replace(" ", "|", $this->data['disable_file_hotlink_ext']) . ")$ " . $redir . " [NC,R,L]\n";
491
+ $new_rules .= "</IfModule>\n";
492
+ }
493
+ if ($this->data['canon']) {
494
+ $url = str_ireplace(array("http://", "www."), "", get_bloginfo("url"));
495
+ $domain = explode("/", $url);
496
+ $escaped_domain = str_ireplace(".", "\.", $domain[0]);
497
+ if ($this->data['canon'] == 'www') {
498
+ $siteurl = get_option('siteurl');
499
+ if (!preg_match('/^http:\/\/www\./', $siteurl)) {
500
+ $siteurl = str_replace("http://", "http://www.", $siteurl);
501
+ update_option('siteurl', $siteurl);
502
+ }
503
+ $homeurl = get_option('home');
504
+ if (!preg_match('/^http:\/\/www\./', $homeurl)) {
505
+ $homeurl = str_replace("http://", "http://www.", $homeurl);
506
+ update_option('home', $homeurl);
507
+ }
508
+ } else if ($this->data['canon'] == 'simple') {
509
+ $siteurl = get_option('siteurl');
510
+ if (preg_match('/^http:\/\/www\./', $siteurl)) {
511
+ $siteurl = str_replace("http://www.", "http://", $siteurl);
512
+ update_option('siteurl', $siteurl);
513
+ }
514
+ $homeurl = get_option('home');
515
+ if (preg_match('/^http:\/\/www\./', $homeurl)) {
516
+ $homeurl = str_replace("http://www.", "http://", $homeurl);
517
+ update_option('home', $homeurl);
518
+ }
519
+ }
520
+ }
521
+ if ($this->data['gzip']) {
522
+ $new_rules .= "\n# WPhtC: Setting mod_gzip\n";
523
+ $new_rules .= "<ifModule mod_gzip.c>\n";
524
+ $new_rules .= "mod_gzip_on Yes\n";
525
+ $new_rules .= "mod_gzip_dechunk Yes\n";
526
+ $new_rules .= "mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$\n";
527
+ $new_rules .= "mod_gzip_item_include handler ^cgi-script$\n";
528
+ $new_rules .= "mod_gzip_item_include mime ^text/.*\n";
529
+ $new_rules .= "mod_gzip_item_include mime ^application/x-javascript.*\n";
530
+ $new_rules .= "mod_gzip_item_exclude mime ^image/.*\n";
531
+ $new_rules .= "mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*\n";
532
+ $new_rules .= "</ifModule>\n";
533
+ }
534
+ if ($this->data['deflate']) {
535
+ $new_rules .= "\n# WPhtC: Setting mod_deflate\n";
536
+ $new_rules .= "<IfModule mod_deflate.c>\n";
537
+ $new_rules .= "AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript\n";
538
+ $new_rules .= "BrowserMatch ^Mozilla/4 gzip-only-text/html\n";
539
+ $new_rules .= "BrowserMatch ^Mozilla/4.0[678] no-gzip\n";
540
+ $new_rules .= "BrowserMatch bMSIE !no-gzip !gzip-only-text/html\n";
541
+ $new_rules .= "Header append Vary User-Agent env=!dont-vary\n";
542
+ $new_rules .= "</IfModule>\n";
543
+ }
544
+ if ($this->data['wp_hta'] && trim($this->data['wp_hta']) != '') {
545
+ $new_rules .= "\n" . $this->data['wp_hta'];
546
+ } else {
547
+ $new_rules .= "\n" . $rules;
548
+ }
549
+ return $new_rules;
550
+ }
551
+
552
+ # Filter Term Link
553
+ function filter_term_link($termlink, $term, $taxonomy)
554
+ {
555
+ // WPhtc -> this
556
+ if ($this->data['remove_taxonomy_base'][$taxonomy]) {
557
+ $txs = get_taxonomies(array('name' => $taxonomy), "object");
558
+ foreach ($txs as $t) {
559
+ //return str_replace($t->rewrite['slug']."/","",$termlink);
560
+ if ($term->parent != 0 && !$this->data['remove_hierarchy']) {
561
+ return get_bloginfo('url') . "/" . $this->term_ancestors($taxonomy, $term->parent) . "/" . $term->slug;
562
+ } else {
563
+ return get_bloginfo('url') . "/" . $term->slug;
564
+ }
565
+ }
566
+ } else
567
+ return $termlink;
568
+ }
569
+
570
+ # Filter Taxonomy Base
571
+ function remove_taxonomy_base_from_rewrite_rules($rules)
572
+ {
573
+ # Let's remove every taxonomy rule here, we'll reacreate them at filter_rewrite_rules()
574
+ return array();
575
+ //return $rules;
576
+ }
577
+
578
+ # Filter Author Rewrite Rules
579
+ function remove_author_base_from_rewrite_rules($author_rewrite)
580
+ {
581
+
582
+ if ($this->data['remove_author_base']) {
583
+ $author_rewrite = array();
584
+ $blogusers = get_users('who=authors');
585
+ foreach ($blogusers as $user) {
586
+ $author_rewrite['(' . $user->user_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?author_name=$matches[1]&feed=$matches[2]';
587
+ $author_rewrite['(' . $user->user_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
588
+ $author_rewrite['(' . $user->user_nicename . ')/?$'] = 'index.php?author_name=$matches[1]';
589
+ }
590
+ }
591
+ return $author_rewrite;
592
+ }
593
+
594
+ # WP-htaccess Control options page interface
595
+ function page()
596
+ {
597
+ include(dirname(__FILE__) . '/wp-htaccess-control-ui.php');
598
+ }
599
+
600
+ # Adding our options page to the admin menu
601
+ function configure_menu()
602
+ {
603
+ if (current_user_can("administrator")) {
604
+ $page = add_submenu_page("options-general.php", "WP htaccess Control", "htaccess Control", 'activate_plugins', __FILE__, array('WPhtc', 'page'));
605
+ add_action('admin_print_scripts-' . $page, array('WPhtc', 'page_script'));
606
+ add_action('admin_print_styles-' . $page, array('WPhtc', 'page_style'));
607
+ }
608
+ }
609
+
610
+ # Enqueue Ui Scripts on Plugin page
611
+ function page_script()
612
+ {
613
+ wp_enqueue_script("htaccess-control-js", WP_PLUGIN_URL . '/wp-htaccess-control/wp-htaccess-control-ui.js');
614
+ }
615
+
616
+ # Enqueue Ui Scripts on Plugin page
617
+ function page_style()
618
+ {
619
+ wp_enqueue_style("htaccess-control-css", WP_PLUGIN_URL . '/wp-htaccess-control/wp-htaccess-control-ui.css');
620
+ }
621
+
622
+ # Filter "View" Link on Administration
623
+ function filter_tax_table_actions($actions, $tag)
624
+ {
625
+ if ($tag->parent != 0) {
626
+ $actions['view'] = '<a href="' . get_bloginfo('url') . '/' . $this->term_ancestors($tag->taxonomy, $tag->parent) . '/' . $tag->slug . '">View</a>';
627
+ }
628
+ return $actions;
629
+ }
630
+
631
+ # Options Page Actions
632
+ function page_action()
633
+ {
634
+ $this->check_first_run();
635
+ if (isset($_REQUEST['action'])) {
636
+ $action = $_REQUEST['action'];
637
  global $WPhtcEcho;
638
+
639
+ switch ($action) {
640
+ # Hide donation message for one month
641
+ case 'hide_donation_message':
642
+ $this->data['donation_hidden_time'] = time() + 90 * 24 * 60 * 60;
643
+ update_option('WPhtc_data', $this->data);
644
+ break;
645
+ # if reseting everything just delete the option array
646
+ case 'reset_rules':
647
+ # nonce
648
+ if (!check_admin_referer('WPhtc_reset_settings')) {
649
+ die("You have no permission to do this.");
650
+ }
651
+ $this->data = array();
652
+ delete_option('WPhtc_data');
653
+ //update_option('WPhtc_data',$this->data);
654
+ $WPhtcEcho .= __('All rules reset.', 'wp-htaccess-control');
655
+ $this->refresh_rewrite_rules();
656
+ break;
657
+ # if updating, save new rules in database and flush rewrite rules
658
+ case 'update':
659
+ # first donation hidding time 'now'
660
+ if (!$this->data['donation_hidden_time']) {
661
+ $this->data['donation_hidden_time'] = time();
662
+ }
663
+ # nonce
664
+ if (!check_admin_referer('WPhtc_settings')) {
665
+ die("You have no permission to do this.");
666
+ }
667
+
668
+ foreach (array("hta", "cap", "cpp", "custom_search_permalink", "remove_author_base", "category_archives", "author_archives", "tag_archives", "disable_serversignature", "disable_indexes", "protect_wp_config", "protect_htaccess", "protect_comments", "disable_hotlink", "disable_file_hotlink_ext", "disable_file_hotlink_redir", "redirect_500", "redirect_403", "canon", "admin_email", "deflate", "gzip", "maintenance_active", "maintenance_redirection", "login_disabled", "login_half_mode", "login_redirection", "sm_enabled", "jim_morgan_hta", "remove_hierarchy", "suffix_html")
669
+ as $k) {
670
+ $this->data[$k] = isset($_POST['WPhtc_' . $k]) ? $_POST['WPhtc_' . $k] : '';
671
+ }
672
+
673
+ # wordpress htaccess and jim morgan's htaccess
674
+ $this->data['wp_hta'] = $_POST['WPhtc_wp_hta'] . "\n";
675
+
676
+ # get htaccess Suggestions
677
+ $this->data['up_limit'] = (is_numeric($_POST['WPhtc_up_limit']) && $_POST['WPhtc_up_limit'] > 0) ? $_POST['WPhtc_up_limit'] : '';
678
+ # get maintenance
679
+ $lines = preg_split("/\n|,/", $_POST['WPhtc_maintenance_ips']);
680
+ $this->data['maintenance_ips'] = array();
681
+ foreach ($lines as $line) {
682
+ trim($line);
683
+ if (preg_match("/[a-z,0-9,\.]/", $line)) {
684
+ $this->data['maintenance_ips'][] = $line;
685
+ }
686
+ }
687
+ # get login control data
688
+ $lines = preg_split("/\n|,/", $_POST['WPhtc_login_ips']);
689
+ $this->data['login_ips'] = array();
690
+ foreach ($lines as $line) {
691
+ trim($line);
692
+ if (preg_match("/[a-z,0-9,\.]/", $line)) {
693
+ $this->data['login_ips'][] = $line;
694
+ }
695
+ }
696
+
697
+ # Unsetting inclusion of Author pages on Google XML Sitemap options, we'll add those links to the sitemap later with our Custom Author Permalink
698
+ if ($this->data['sm_enabled'] == true) {
699
+ $SMoptions = get_option("sm_options");
700
+ if ($SMoptions && is_array($SMoptions)) {
701
+ $SMoptions = get_option("sm_options");
702
+ $SMoptions['sm_in_auth'] = 0;
703
+ update_option('sm_options', $SMoptions);
704
+ # Try to rebuild Sitemap
705
+ do_action("sm_rebuild");
706
+ }
707
+ }
708
+
709
+ # Taxonomy Base removal options
710
+ unset($this->data['remove_taxonomy_base']);
711
+ if ($_POST['WPhtc_remove_base']) {
712
+ foreach ($_POST['WPhtc_remove_base'] as $tax_key => $remove) {
713
+ $this->data['remove_taxonomy_base'][$tax_key] = $remove;
714
+ }
715
+ }
716
+
717
+ # Advanced archives
718
+ unset($this->data['create_archive']);
719
+ if (isset($_POST['WPhtc_create_archive'])) {
720
+ foreach ($_POST['WPhtc_create_archive'] as $tax_key => $create_archive) {
721
+ $this->data['create_archive'][$tax_key] = $create_archive;
722
+ }
723
+ }
724
+
725
+ # Update WP htaccess Control options
726
+ update_option('WPhtc_data', $this->data);
727
+
728
+ # Flush Rewrite Rules
729
+ $this->refresh_rewrite_rules();
730
+ $WPhtcEcho .= __('All options updated.', 'wp-htaccess-control');
731
+ break;
732
+ }
733
+ }
734
+ }
735
+ }
736
+ }
737
  if (class_exists("WPhtc")) {
738
+ $WPhtc = new WPhtc();
739
+ }
740
  if (isset($WPhtc)) {
741
+
742
+ add_action('init', array($WPhtc, 'init'));
743
+ add_filter('mod_rewrite_rules', array($WPhtc, 'hta_rules'));
744
+ add_filter('redirect_canonical', array($WPhtc, 'filter_redirect_canonical'), 10, 10);
745
+ add_action('admin_menu', array($WPhtc, 'configure_menu'));
746
+ add_action('flush_event', array($WPhtc, 'refresh_rewrite_rules'));
747
+ add_filter('generate_rewrite_rules', array($WPhtc, 'wp_rewrite_rules'));
748
+ add_filter('rewrite_rules_array', array($WPhtc, 'filter_rewrite_rules'));
749
+
750
+ // AUTHOR improve this
751
+ if ($WPhtc->data['cap'] != '' || $WPhtc->data['remove_author_base']) {
752
+ add_filter('author_link', array($WPhtc, 'filter_author_link'));
753
+ add_filter('author_rewrite_rules', array($WPhtc, 'remove_author_base_from_rewrite_rules'));
754
+ }
755
+
756
+ // Filter pagination links
757
+ if ($WPhtc->data['cpp'] != '') add_filter('get_pagenum_link', array($WPhtc, 'filter_get_pagenum_link'));
758
+
759
+ // Filter search
760
+ if (trim($WPhtc->data['custom_search_permalink']) != '') {
761
+ add_filter('search_feed_link', array($WPhtc, 'search_feed_link'), 10, 10);
762
+ add_filter('get_search_query', array($WPhtc, 'get_search_query_filter'), 10, 10);
763
+ add_action('template_redirect', array($WPhtc, 'search_template_redirect'));
764
+ }
765
+
766
+
767
+ add_action('sm_buildmap', array($WPhtc, 'set_sm'));
768
+
769
+ // Taxonomy Base Removal
770
+ if ($WPhtc->data['remove_taxonomy_base']) {
771
+ add_filter('term_link', array($WPhtc, 'filter_term_link'), 10, 3);
772
+ foreach ($WPhtc->data['remove_taxonomy_base'] as $tax => $v) {
773
+ if ($v) {
774
+ add_filter($tax . '_rewrite_rules', array($WPhtc, 'remove_taxonomy_base_from_rewrite_rules'));
775
+ add_filter($tax . "_row_actions", array($WPhtc, 'filter_tax_table_actions'), 10, 2);
776
+ }
777
+ }
778
+ }
779
+ /* Term management actions*/
780
+ if ($WPhtc->data['remove_taxonomy_base'] || $WPhtc->data['create_archive']) {
781
+ add_action('created_term', array($WPhtc, 'refresh_rewrite_rules_later'));
782
+ add_action('edited_term', array($WPhtc, 'refresh_rewrite_rules_later'));
783
+ add_action('delete_term', array($WPhtc, 'refresh_rewrite_rules_later'));
784
+ }
785
+
786
+ /* This flush should maybe be conditional to content authors only, maybe not */
787
+ add_action('user_register', array($WPhtc, 'refresh_rewrite_rules_later'));
788
+ add_action('delete_user', array($WPhtc, 'refresh_rewrite_rules_later'));
789
+ }
790
  ?>