PluginProbe ʕ •ᴥ•ʔ
Custom Post Type Permalinks / 1.4.0
Custom Post Type Permalinks v1.4.0
1.2.0 1.3.0 1.3.1 1.4.0 1.5.1 1.5.2 1.5.4 2.0.0 2.0.1 2.0.2 2.1.1 2.1.2 2.1.3 2.2.0 3.0.0 3.0.1 3.1.0 3.1.1 3.1.3 3.1.4 3.1.5 3.2.0 3.2.1 3.2.2 3.3.0 3.3.1 3.3.4 3.3.5 3.4.0 3.4.0-rc.1 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.5.2 3.5.3 3.5.4 3.5.5 trunk 0.6 0.6.1 0.6.2 0.7 0.7.1 0.7.10 0.7.2 0.7.2.1 0.7.3 0.7.3.1 0.7.4 0.7.4.1 0.7.5 0.7.5.1 0.7.5.2 0.7.5.6 0.7.6 0.7.8 0.7.9 0.7.9.1 0.7.9.2 0.8 0.8.1 0.8.6 0.8.7 0.8.7.1 0.8.7.5 0.8.7.6 0.9 0.9.1 0.9.2.1 0.9.3.1 0.9.3.2 0.9.3.3 0.9.5 0.9.5.1 0.9.5.2 0.9.5.3 0.9.5.4 0.9.5.6 0.9.6 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0
custom-post-type-permalinks / CPTP / Module / Rewrite.php
custom-post-type-permalinks / CPTP / Module Last commit date
Admin.php 10 years ago FlushRules.php 11 years ago GetArchives.php 10 years ago Option.php 11 years ago Permalink.php 10 years ago Rewrite.php 11 years ago Setting.php 10 years ago
Rewrite.php
311 lines
1 <?php
2
3
4 /**
5 *
6 * Add Rewrite Rules
7 *
8 * @package Custom_Post_Type_Permalinks
9 * @version 1.0.3
10 * @since 0.9.4
11 *
12 * */
13 class CPTP_Module_Rewrite extends CPTP_Module {
14
15 /** @var Array */
16 private $post_type_args;
17 /** @var Array */
18 private $taxonomy_args;
19
20 public function add_hook() {
21 add_action( 'parse_request', array( $this, 'parse_request' ) );
22
23 add_action( 'registered_post_type', array( $this, 'registered_post_type' ), 10, 2 );
24 add_action( 'registered_taxonomy', array( $this, 'registered_taxonomy' ), 10, 3 );
25
26 add_action( 'wp_loaded', array( $this, 'add_rewrite_rules' ), 100 );
27 }
28
29
30 public function add_rewrite_rules() {
31
32 foreach ( $this->taxonomy_args as $args ) {
33 call_user_func_array( array( $this, 'register_taxonomy_rules' ), $args );
34 }
35
36 foreach ( $this->post_type_args as $args ) {
37 call_user_func_array( array( $this, 'register_post_type_rules' ), $args );
38 }
39
40 }
41
42 /**
43 *
44 * registered_post_type
45 *
46 * queue post_type rewrite.
47 *
48 * @param string $post_type Post type.
49 * @param object $args Arguments used to register the post type.
50 */
51 public function registered_post_type( $post_type, $args ) {
52 $this->post_type_args[] = func_get_args();
53 }
54
55 /**
56 *
57 * registered_taxonomy
58 *
59 * queue taxonomy rewrite.
60 *
61 * @param string $taxonomy Taxonomy slug.
62 * @param array|string $object_type Object type or array of object types.
63 * @param array $args Array of taxonomy registration arguments.
64 */
65 public function registered_taxonomy( $taxonomy, $object_type, $args ) {
66 $this->taxonomy_args[] = func_get_args();
67 }
68
69
70 /**
71 *
72 * register_post_type_rules
73 * ** add rewrite tag for Custom Post Type.
74 * @version 1.1
75 * @since 0.9
76 *
77 * @param string $post_type
78 * @param object $args
79 *
80 */
81
82 public function register_post_type_rules( $post_type, $args ) {
83
84 /** @var WP_Rewrite $wp_rewrite */
85 global $wp_rewrite;
86
87 if ( $args->_builtin or ! $args->publicly_queryable or ! $args->show_ui ) {
88 return;
89 }
90 $permalink = CPTP_Util::get_permalink_structure( $post_type );
91
92 if ( ! $permalink ) {
93 $permalink = CPTP_DEFAULT_PERMALINK;
94 }
95
96 $permalink = '%' . $post_type . '_slug%' . $permalink;
97 $permalink = str_replace( '%postname%', '%' . $post_type . '%', $permalink );
98
99 add_rewrite_tag( '%' . $post_type . '_slug%', '(' . $args->rewrite['slug'] . ')', 'post_type=' . $post_type . '&slug=' );
100
101 $taxonomies = CPTP_Util::get_taxonomies( true );
102 foreach ( $taxonomies as $taxonomy => $objects ):
103 $wp_rewrite->add_rewrite_tag( "%$taxonomy%", '(.+?)', "$taxonomy=" );
104 endforeach;
105
106 $rewrite_args = $args->rewrite;
107 if ( ! is_array( $rewrite_args ) ) {
108 $rewrite_args = array( 'with_front' => $args->rewrite );
109 }
110
111 $slug = $args->rewrite['slug'];
112 if ( $args->has_archive ) {
113 if ( is_string( $args->has_archive ) ) {
114 $slug = $args->has_archive;
115 };
116
117 if ( $args->rewrite['with_front'] ) {
118 $slug = substr( $wp_rewrite->front, 1 ) . $slug;
119 }
120
121 $date_front = CPTP_Util::get_date_front( $post_type );
122
123 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=' . $post_type, 'top' );
124 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=' . $post_type, 'top' );
125 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type=' . $post_type, 'top' );
126 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type=' . $post_type, 'top' );
127 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=' . $post_type, 'top' );
128 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=' . $post_type, 'top' );
129 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type=' . $post_type, 'top' );
130 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&post_type=' . $post_type, 'top' );
131 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type=' . $post_type, 'top' );
132 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type=' . $post_type, 'top' );
133 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
134 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type=' . $post_type, 'top' );
135 add_rewrite_rule( $slug . '/author/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?author_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
136 add_rewrite_rule( $slug . '/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type=' . $post_type, 'top' );
137
138 if( in_array( 'category', $args->taxonomies ) ) {
139
140 $category_base = get_option( 'category_base' );
141 if( !$category_base ) {
142 $category_base = 'category';
143 }
144
145 add_rewrite_rule( $slug . '/'. $category_base . '/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
146 add_rewrite_rule( $slug . '/'. $category_base . '/([^/]+)/?$', 'index.php?category_name=$matches[1]&post_type=' . $post_type, 'top' );
147
148 }
149
150 do_action( 'CPTP_registered_' . $post_type . '_rules', $args, $slug );
151 }
152
153 $rewrite_args['walk_dirs'] = false;
154 add_permastruct( $post_type, $permalink, $rewrite_args );
155
156 }
157
158
159 /**
160 *
161 * register_taxonomy_rules
162 *
163 * @param string $taxonomy
164 * @param array|string $object_type
165 * @param array $args
166 *
167 * @return void
168 */
169 public function register_taxonomy_rules( $taxonomy, $object_type, $args ) {
170
171 if ( get_option( 'no_taxonomy_structure' ) ) {
172 return;
173 }
174 if ( $args['_builtin'] ) {
175 return;
176 }
177
178 global $wp_rewrite;
179
180 $post_types = $args['object_type'];
181 foreach ( $post_types as $post_type ):
182 $post_type_obj = get_post_type_object( $post_type );
183 if ( ! empty( $post_type_obj->rewrite['slug'] ) ) {
184 $slug = $post_type_obj->rewrite['slug'];
185 } else {
186 $slug = $post_type;
187 }
188
189 if ( ! empty( $post_type_obj->has_archive ) && is_string( $post_type_obj->has_archive ) ) {
190 $slug = $post_type_obj->has_archive;
191 };
192
193
194 if ( ! empty( $post_type_obj->rewrite['with_front'] ) ) {
195 $slug = substr( $wp_rewrite->front, 1 ) . $slug;
196 }
197
198 if ( 'category' == $taxonomy ) {
199 $taxonomy_slug = ( $cb = get_option( 'category_base' ) ) ? $cb : $taxonomy;
200 $taxonomy_key = 'category_name';
201 } else {
202 // Edit by [Xiphe]
203 if ( isset( $args['rewrite']['slug'] ) ) {
204 $taxonomy_slug = $args['rewrite']['slug'];
205 } else {
206 $taxonomy_slug = $taxonomy;
207 }
208 // [Xiphe] stop
209
210 $taxonomy_key = $taxonomy;
211 }
212
213 $rules = array(
214 //feed.
215 array(
216 "regex" => "%s/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$",
217 "redirect" => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]"
218 ),
219 array(
220 "regex" => "%s/(.+?)/(feed|rdf|rss|rss2|atom)/?$",
221 "redirect" => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]"
222 ),
223 //year
224 array(
225 "regex" => "%s/(.+?)/date/([0-9]{4})/?$",
226 "redirect" => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]"
227 ),
228 array(
229 "regex" => "%s/(.+?)/date/([0-9]{4})/page/?([0-9]{1,})/?$",
230 "redirect" => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&paged=\$matches[3]"
231 ),
232 //monthnum
233 array(
234 "regex" => "%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$",
235 "redirect" => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]"
236 ),
237 array(
238 "regex" => "%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$",
239 "redirect" => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&paged=\$matches[4]"
240 ),
241 //day
242 array(
243 "regex" => "%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$",
244 "redirect" => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]"
245 ),
246 array(
247 "regex" => "%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$",
248 "redirect" => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]&paged=\$matches[5]"
249 ),
250 //paging
251 array(
252 "regex" => "%s/(.+?)/page/?([0-9]{1,})/?$",
253 "redirect" => "index.php?{$taxonomy_key}=\$matches[1]&paged=\$matches[2]"
254 ),
255 //tax archive.
256 array(
257 "regex" => "%s/(.+?)/?$",
258 "redirect" => "index.php?{$taxonomy_key}=\$matches[1]"
259 ),
260 );
261
262 //no post_type slug.
263 foreach ( $rules as $rule ) {
264 $regex = sprintf( $rule['regex'], "{$taxonomy_slug}" );
265 $redirect = $rule['redirect'];
266 add_rewrite_rule( $regex, $redirect, 'top' );
267 }
268
269 if ( get_option( 'add_post_type_for_tax' ) ) {
270 foreach ( $rules as $rule ) {
271 $regex = sprintf( $rule['regex'], "{$slug}/{$taxonomy_slug}" );
272 $redirect = $rule['redirect'] . "&post_type={$post_type}";
273 add_rewrite_rule( $regex, $redirect, 'top' );
274 }
275
276 } else {
277 foreach ( $rules as $rule ) {
278 $regex = sprintf( $rule['regex'], "{$slug}/{$taxonomy_slug}" );
279 $redirect = $rule['redirect'];
280 add_rewrite_rule( $regex, $redirect, 'top' );
281 }
282 }
283
284 do_action( 'CPTP_registered_' . $taxonomy . '_rules', $object_type, $args, $taxonomy_slug );
285
286 endforeach;
287 }
288
289
290 /**
291 *
292 * Fix taxonomy = parent/child => taxonomy => child
293 * @since 0.9.3
294 *
295 * @param WP $obj
296 */
297 public function parse_request( $obj ) {
298 $taxes = CPTP_Util::get_taxonomies();
299 foreach ( $taxes as $key => $tax ) {
300 if ( isset( $obj->query_vars[ $tax ] ) ) {
301 if ( false !== strpos( $obj->query_vars[ $tax ], '/' ) ) {
302 $query_vars = explode( '/', $obj->query_vars[ $tax ] );
303 if ( is_array( $query_vars ) ) {
304 $obj->query_vars[ $tax ] = array_pop( $query_vars );
305 }
306 }
307 }
308 }
309 }
310 }
311