PluginProbe ʕ •ᴥ•ʔ
Custom Post Type Permalinks / 3.3.4
Custom Post Type Permalinks v3.3.4
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 6 years ago FlushRules.php 8 years ago GetArchives.php 6 years ago Option.php 6 years ago Permalink.php 6 years ago Rewrite.php 6 years ago Setting.php 6 years ago
Rewrite.php
284 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 add_rewrite_rule( $slug . '/author/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?author_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
107 add_rewrite_rule( $slug . '/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type=' . $post_type, 'top' );
108 }
109
110 if ( in_array( 'category', $args->taxonomies, true ) ) {
111 $category_base = get_option( 'category_base', 'category' );
112
113 add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
114 add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/?$', 'index.php?category_name=$matches[1]&post_type=' . $post_type, 'top' );
115 }
116
117 do_action( 'CPTP_registered_' . $post_type . '_rules', $args, $slug );
118 }
119
120 $rewrite_args['walk_dirs'] = false;
121 add_permastruct( $post_type, $permalink, $rewrite_args );
122 }
123
124
125 /**
126 * Register_taxonomy_rules
127 *
128 * @param string $taxonomy Taxonomy slug.
129 * @param array|string $object_type Object type or array of object types.
130 * @param array $args Array of taxonomy registration arguments.
131 *
132 * @return void
133 */
134 public function register_taxonomy_rules( $taxonomy, $object_type, $args ) {
135 global $wp_rewrite;
136
137 /* for 4.7 */
138 $args = (array) $args;
139
140 if ( CPTP_Util::get_no_taxonomy_structure() ) {
141 return;
142 }
143
144 if ( ! empty( $args['_builtin'] ) ) {
145 return;
146 }
147
148 if ( false === $args['rewrite'] ) {
149 return;
150 }
151
152 $post_types = $args['object_type'];
153 foreach ( $post_types as $post_type ) :
154 $post_type_obj = get_post_type_object( $post_type );
155 if ( ! empty( $post_type_obj->rewrite['slug'] ) ) {
156 $slug = $post_type_obj->rewrite['slug'];
157 } else {
158 $slug = $post_type;
159 }
160
161 if ( ! empty( $post_type_obj->has_archive ) && is_string( $post_type_obj->has_archive ) ) {
162 $slug = $post_type_obj->has_archive;
163 };
164
165 if ( ! empty( $post_type_obj->rewrite['with_front'] ) ) {
166 $slug = substr( $wp_rewrite->front, 1 ) . $slug;
167 }
168
169 if ( 'category' === $taxonomy ) {
170 $cb = get_option( 'category_base' );
171 $taxonomy_slug = ( $cb ) ? $cb : $taxonomy;
172 $taxonomy_key = 'category_name';
173 } else {
174 // Edit by [Xiphe].
175 if ( isset( $args['rewrite']['slug'] ) ) {
176 $taxonomy_slug = $args['rewrite']['slug'];
177 } else {
178 $taxonomy_slug = $taxonomy;
179 }
180 // [Xiphe] stop
181 $taxonomy_key = $taxonomy;
182 }
183
184 $rules = array(
185 // feed.
186 array(
187 'regex' => '%s/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$',
188 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]",
189 ),
190 array(
191 'regex' => '%s/(.+?)/(feed|rdf|rss|rss2|atom)/?$',
192 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]",
193 ),
194 // year.
195 array(
196 'regex' => '%s/(.+?)/date/([0-9]{4})/?$',
197 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]",
198 ),
199 array(
200 'regex' => '%s/(.+?)/date/([0-9]{4})/page/?([0-9]{1,})/?$',
201 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&paged=\$matches[3]",
202 ),
203 // monthnum.
204 array(
205 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$',
206 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]",
207 ),
208 array(
209 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$',
210 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&paged=\$matches[4]",
211 ),
212 // day.
213 array(
214 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$',
215 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]",
216 ),
217 array(
218 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$',
219 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]&paged=\$matches[5]",
220 ),
221 // paging.
222 array(
223 'regex' => '%s/(.+?)/page/?([0-9]{1,})/?$',
224 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&paged=\$matches[2]",
225 ),
226 // tax archive.
227 array(
228 'regex' => '%s/(.+?)/?$',
229 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]",
230 ),
231 );
232
233 // no post_type slug.
234 foreach ( $rules as $rule ) {
235 $regex = sprintf( $rule['regex'], "{$taxonomy_slug}" );
236 $redirect = $rule['redirect'];
237 add_rewrite_rule( $regex, $redirect, 'top' );
238 }
239
240 if ( get_option( 'add_post_type_for_tax' ) ) {
241 foreach ( $rules as $rule ) {
242 $regex = sprintf( $rule['regex'], "{$slug}/{$taxonomy_slug}" );
243 $redirect = $rule['redirect'] . "&post_type={$post_type}";
244 add_rewrite_rule( $regex, $redirect, 'top' );
245 }
246 } else {
247 foreach ( $rules as $rule ) {
248 $regex = sprintf( $rule['regex'], "{$slug}/{$taxonomy_slug}" );
249 $redirect = $rule['redirect'];
250 add_rewrite_rule( $regex, $redirect, 'top' );
251 }
252 }
253
254 do_action( 'CPTP_registered_' . $taxonomy . '_rules', $object_type, $args, $taxonomy_slug );
255 endforeach;
256 }
257
258
259 /**
260 *
261 * Fix taxonomy = parent/child => taxonomy => child
262 *
263 * @since 0.9.3
264 *
265 * @param WP $obj WP instance.
266 */
267 public function parse_request( $obj ) {
268 $taxes = CPTP_Util::get_taxonomies( true );
269 foreach ( $taxes as $key => $tax ) {
270 $name = $tax->name;
271 if ( $tax->hierarchical ) {
272 if ( isset( $obj->query_vars[ $name ] ) && is_string( $obj->query_vars[ $name ] ) ) {
273 if ( false !== strpos( $obj->query_vars[ $name ], '/' ) ) {
274 $query_vars = explode( '/', $obj->query_vars[ $name ] );
275 if ( is_array( $query_vars ) ) {
276 $obj->query_vars[ $name ] = array_pop( $query_vars );
277 }
278 }
279 }
280 }
281 }
282 }
283 }
284