-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-no-taxonomy-base.php
214 lines (141 loc) · 5.43 KB
/
wp-no-taxonomy-base.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
<?php
/*
Plugin Name: WP No Taxonomy Base
Plugin URI:
Description: Remove base slug from your custom taxonomy terms.
Author: <a href="http://twitter.com/luk3thomas">@luk3thomas</a> & <a href="http://twitter.com/DavidDiGiovanni">@DavidDiGiovanni</a>
Version: 1.0
*/
/*
Copyright 2012 Luke Thomas (email: luk3thomas at gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
class WP_No_Taxonomy_Base {
public function __construct() {
add_action('admin_menu' , array($this , 'add_page' ) ) ;
add_action('created_category' , array($this , 'flush_rules' ) ) ;
add_action('delete_category' , array($this , 'flush_rules' ) ) ;
add_action('edited_category' , array($this , 'flush_rules' ) ) ;
add_action('init' , array($this , 'redirect' ) ) ;
add_filter('category_rewrite_rules' , array($this , 'add_rules' ) ) ;
}
public function flush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
public function redirect() {
$request = $_SERVER['REQUEST_URI'];
$host = $_SERVER['HTTP_HOST'];
$redirect = false;
$blogurl = get_bloginfo('url');
$taxonomies = get_option('WP_No_Taxonomy_Base');
$http = ( strrpos($blogurl, 'https://') === false ) ? 'http://' : 'https://';
/** Bail */
if(!$taxonomies)
return false;
/** build the URL */
$url = $http . $host . $request;
foreach( $taxonomies as $term ) {
/**
* If the url contains a taxonomy term base
* then redirect it to the new page.
* Only redirect one time.
* -------------------------------------------- */
if( strrpos($url, '/' . $term . '/') && !$redirect) {
$new_url = str_replace('/' . $term . '/', '/', $url);
wp_redirect($new_url, 301);
$redirect = true;
}
}
}
public function add_rules($rules) {
/**
* @todo
*
* Create rewrite rules for terms when
* they are nested under a parent term.
*
* Example:
* http://#{base_url}/#{parent_term}/#{child_term}
*
* -------------------------------------------- */
$taxonomies = get_option('WP_No_Taxonomy_Base');
/** Time to bail. */
if(!$taxonomies)
return $rules;
$args = array('hide_empty' => false);
/**
* Loop em.
* -------------------------------------------- */
foreach( $taxonomies as $taxonomy ) {
$categories = get_terms($taxonomy, $args);
foreach($categories as $category) {
$slug = $category->slug;
$feed_rule = sprintf('index.php?taxonomy=%s&term=%s&feed=$matches[1]' , $taxonomy , $slug);
$paged_rule = sprintf('index.php?taxonomy=%s&term=%s&paged=$matches[1]' , $taxonomy , $slug);
$base_rule = sprintf('index.php?taxonomy=%s&term=%s' , $taxonomy , $slug);
$rules[$slug . '/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = $feed_rule;
$rules[$slug . '/page/?([0-9]{1,})/?$'] = $paged_rule;
$rules[$slug . '/?$'] = $base_rule;
}
}
return $rules;
}
public function add_page() {
add_submenu_page('options-general.php', 'WP No Taxonomy Base', 'WP No Taxonomy Base', 'manage_options', 'wp-no-taxonomy-base', array($this, 'show_page'));
}
public function show_page() {
/** @todo internationalization */
/** @todo make frontend look better */
/** @todo UX - notifications after an update */
if(!current_user_can('manage_options'))
wp_die( __('You do not have sufficient permissions to access this page.') );
if($_POST['vesave'] == 'save') {
update_option( 'WP_No_Taxonomy_Base', $_POST['WP_No_Taxonomy_Base'] );
$this->flush_rules();
}
$taxonomies = get_taxonomies( array('public' => true) );
$selected = get_option('WP_No_Taxonomy_Base');
if(!$selected)
$selected = array();
?>
<div class="wrap">
<h1>WP No Taxonomy Base</h1>
<p>Want to remove the base for a taxonomy? Just select the taxonomy below and click "Save".</p>
<form method="post">
<input type="hidden" name="vesave" value="save" />
<table>
<?php
foreach( $taxonomies as $taxonomy ) {
$active = in_array($taxonomy, $selected) ? 'checked="checked"' : '';
printf(
'
<tr>
<td> <label>%s</label> </td>
<td> <input type="checkbox" name="WP_No_Taxonomy_Base[]" value="%s" %s /> </td>
</tr>
'
, $taxonomy
, $taxonomy
, $active
);
}
?>
</table>
<button class="button-primary">Save</button>
</form>
</div>
<?php
}
}
$no_base = new WP_No_Taxonomy_Base();