PluginProbe ʕ •ᴥ•ʔ
Custom Post Type Permalinks / 3.3.1
Custom Post Type Permalinks v3.3.1
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 8 years ago FlushRules.php 8 years ago GetArchives.php 8 years ago Option.php 8 years ago Permalink.php 8 years ago Rewrite.php 7 years ago Setting.php 8 years ago
Rewrite.php
289 lines
1 <?php
2 /**
3 * Add Rewrite Rules
4 *
5 * @package Custom_Post_Type_Permalinks
6 */
7
8 /**
9 * Add CPTP_Module_Rewrite Rules
10 *
11 * @version 1.0.3
12 * @since 0.9.4
13 * */
14 class CPTP_Module_Rewrite extends CPTP_Module {
15
16 /**
17 * Add Actions.
18 */
19 public function add_hook() {
20 add_action( 'parse_request', array( $this, 'parse_request' ) );
21 add_action( 'registered_post_type', array( $this, 'register_post_type_rules' ), 10, 2 );
22 add_action( 'registered_taxonomy', array( $this, 'register_taxonomy_rules' ), 10, 3 );
23 }
24
25 /**
26 * Register_post_type_rules
27 * add rewrite tag for Custom Post Type.
28 *
29 * @version 1.1
30 * @since 0.9
31 *
32 * @param string $post_type Post type.
33 * @param WP_Post_Type $args Arguments used to register the post type.
34 */
35 public function register_post_type_rules( $post_type, $args ) {
36
37 /**
38 * WP_Rewrite.
39 *
40 * @var WP_Rewrite $wp_rewrite
41 */
42 global $wp_rewrite;
43
44 if ( $args->_builtin ) {
45 return;
46 }
47
48 if ( false === $args->rewrite ) {
49 return;
50 }
51
52 if ( ! in_array( $post_type, CPTP_Util::get_post_types(), true ) ) {
53 return;
54 }
55
56 $permalink = CPTP_Util::get_permalink_structure( $post_type );
57
58 if ( ! $permalink ) {
59 $permalink = CPTP_DEFAULT_PERMALINK;
60 }
61
62 $permalink = '%' . $post_type . '_slug%' . $permalink;
63 $permalink = str_replace( '%postname%', '%' . $post_type . '%', $permalink );
64
65 add_rewrite_tag( '%' . $post_type . '_slug%', '(' . $args->rewrite['slug'] . ')', 'post_type=' . $post_type . '&slug=' );
66
67 $taxonomies = CPTP_Util::get_taxonomies( true );
68 foreach ( $taxonomies as $taxonomy => $objects ) :
69 $wp_rewrite->add_rewrite_tag( "%$taxonomy%", '(.+?)', "$taxonomy=" );
70 endforeach;
71
72 $rewrite_args = $args->rewrite;
73 if ( ! is_array( $rewrite_args ) ) {
74 $rewrite_args = array(
75 'with_front' => $args->rewrite,
76 );
77 }
78
79 $slug = $args->rewrite['slug'];
80 if ( $args->has_archive ) {
81 if ( is_string( $args->has_archive ) ) {
82 $slug = $args->has_archive;
83 };
84
85 if ( $args->rewrite['with_front'] ) {
86 $slug = substr( $wp_rewrite->front, 1 ) . $slug;
87 }
88
89 if ( CPTP_Util::get_post_type_date_archive_support( $post_type ) ) {
90 $date_front = CPTP_Util::get_date_front( $post_type );
91 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' );
92 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' );
93 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' );
94 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' );
95 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' );
96 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' );
97 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' );
98 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' );
99 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' );
100 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' );
101 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' );
102 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type=' . $post_type, 'top' );
103 }
104
105 if ( CPTP_Util::get_post_type_author_archive_support( $post_type ) ) {
106
107 add_rewrite_rule( $slug . '/author/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?author_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
108 add_rewrite_rule( $slug . '/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type=' . $post_type, 'top' );
109 }
110
111 if ( in_array( 'category', $args->taxonomies, true ) ) {
112
113 $category_base = get_option( 'category_base', 'category' );
114
115 add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
116 add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/?$', 'index.php?category_name=$matches[1]&post_type=' . $post_type, 'top' );
117
118 }
119
120 do_action( 'CPTP_registered_' . $post_type . '_rules', $args, $slug );
121 }
122
123 $rewrite_args['walk_dirs'] = false;
124 add_permastruct( $post_type, $permalink, $rewrite_args );
125
126 }
127
128
129 /**
130 * Register_taxonomy_rules
131 *
132 * @param string $taxonomy Taxonomy slug.
133 * @param array|string $object_type Object type or array of object types.
134 * @param array $args Array of taxonomy registration arguments.
135 *
136 * @return void
137 */
138 public function register_taxonomy_rules( $taxonomy, $object_type, $args ) {
139 global $wp_rewrite;
140
141 /* for 4.7 */
142 $args = (array) $args;
143
144 if ( CPTP_Util::get_no_taxonomy_structure() ) {
145 return;
146 }
147
148 if ( ! empty( $args['_builtin'] ) ) {
149 return;
150 }
151
152 if ( false === $args['rewrite'] ) {
153 return;
154 }
155
156 $post_types = $args['object_type'];
157 foreach ( $post_types as $post_type ) :
158 $post_type_obj = get_post_type_object( $post_type );
159 if ( ! empty( $post_type_obj->rewrite['slug'] ) ) {
160 $slug = $post_type_obj->rewrite['slug'];
161 } else {
162 $slug = $post_type;
163 }
164
165 if ( ! empty( $post_type_obj->has_archive ) && is_string( $post_type_obj->has_archive ) ) {
166 $slug = $post_type_obj->has_archive;
167 };
168
169 if ( ! empty( $post_type_obj->rewrite['with_front'] ) ) {
170 $slug = substr( $wp_rewrite->front, 1 ) . $slug;
171 }
172
173 if ( 'category' === $taxonomy ) {
174 $cb = get_option( 'category_base' );
175 $taxonomy_slug = ( $cb ) ? $cb : $taxonomy;
176 $taxonomy_key = 'category_name';
177 } else {
178 // Edit by [Xiphe].
179 if ( isset( $args['rewrite']['slug'] ) ) {
180 $taxonomy_slug = $args['rewrite']['slug'];
181 } else {
182 $taxonomy_slug = $taxonomy;
183 }
184 // [Xiphe] stop
185 $taxonomy_key = $taxonomy;
186 }
187
188 $rules = array(
189 // feed.
190 array(
191 'regex' => '%s/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$',
192 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]",
193 ),
194 array(
195 'regex' => '%s/(.+?)/(feed|rdf|rss|rss2|atom)/?$',
196 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]",
197 ),
198 // year.
199 array(
200 'regex' => '%s/(.+?)/date/([0-9]{4})/?$',
201 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]",
202 ),
203 array(
204 'regex' => '%s/(.+?)/date/([0-9]{4})/page/?([0-9]{1,})/?$',
205 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&paged=\$matches[3]",
206 ),
207 // monthnum.
208 array(
209 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$',
210 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]",
211 ),
212 array(
213 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$',
214 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&paged=\$matches[4]",
215 ),
216 // day.
217 array(
218 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$',
219 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]",
220 ),
221 array(
222 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$',
223 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]&paged=\$matches[5]",
224 ),
225 // paging.
226 array(
227 'regex' => '%s/(.+?)/page/?([0-9]{1,})/?$',
228 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&paged=\$matches[2]",
229 ),
230 // tax archive.
231 array(
232 'regex' => '%s/(.+?)/?$',
233 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]",
234 ),
235 );
236
237 // no post_type slug.
238 foreach ( $rules as $rule ) {
239 $regex = sprintf( $rule['regex'], "{$taxonomy_slug}" );
240 $redirect = $rule['redirect'];
241 add_rewrite_rule( $regex, $redirect, 'top' );
242 }
243
244 if ( get_option( 'add_post_type_for_tax' ) ) {
245 foreach ( $rules as $rule ) {
246 $regex = sprintf( $rule['regex'], "{$slug}/{$taxonomy_slug}" );
247 $redirect = $rule['redirect'] . "&post_type={$post_type}";
248 add_rewrite_rule( $regex, $redirect, 'top' );
249 }
250 } else {
251 foreach ( $rules as $rule ) {
252 $regex = sprintf( $rule['regex'], "{$slug}/{$taxonomy_slug}" );
253 $redirect = $rule['redirect'];
254 add_rewrite_rule( $regex, $redirect, 'top' );
255 }
256 }
257
258 do_action( 'CPTP_registered_' . $taxonomy . '_rules', $object_type, $args, $taxonomy_slug );
259
260 endforeach;
261 }
262
263
264 /**
265 *
266 * Fix taxonomy = parent/child => taxonomy => child
267 *
268 * @since 0.9.3
269 *
270 * @param WP $obj WP instance.
271 */
272 public function parse_request( $obj ) {
273 $taxes = CPTP_Util::get_taxonomies( true );
274 foreach ( $taxes as $key => $tax ) {
275 $name = $tax->name;
276 if ( $tax->hierarchical ) {
277 if ( isset( $obj->query_vars[ $name ] ) && is_string( $obj->query_vars[ $name ] ) ) {
278 if ( false !== strpos( $obj->query_vars[ $name ], '/' ) ) {
279 $query_vars = explode( '/', $obj->query_vars[ $name ] );
280 if ( is_array( $query_vars ) ) {
281 $obj->query_vars[ $name ] = array_pop( $query_vars );
282 }
283 }
284 }
285 }
286 }
287 }
288 }
289