PluginProbe ʕ •ᴥ•ʔ
Custom Post Type Permalinks / 3.4.1
Custom Post Type Permalinks v3.4.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 5 years ago FlushRules.php 8 years ago GetArchives.php 5 years ago Option.php 5 years ago Permalink.php 5 years ago Rewrite.php 5 years ago Setting.php 6 years ago
Rewrite.php
296 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 $slug_placeholder = self::get_slug_placeholder( $post_type );
63 $permalink = $slug_placeholder . $permalink;
64 $permalink = str_replace( '%postname%', '%' . $post_type . '%', $permalink );
65
66 add_rewrite_tag( $slug_placeholder, '(' . $args->rewrite['slug'] . ')', 'post_type=' . $post_type . '&slug=' );
67
68 $taxonomies = CPTP_Util::get_taxonomies( true );
69 foreach ( $taxonomies as $taxonomy => $objects ) :
70 $wp_rewrite->add_rewrite_tag( "%$taxonomy%", '(.+?)', "$taxonomy=" );
71 endforeach;
72
73 $rewrite_args = $args->rewrite;
74 if ( ! is_array( $rewrite_args ) ) {
75 $rewrite_args = array(
76 'with_front' => $args->rewrite,
77 );
78 }
79
80 $slug = $args->rewrite['slug'];
81 if ( $args->has_archive ) {
82 if ( is_string( $args->has_archive ) ) {
83 $slug = $args->has_archive;
84 };
85
86 if ( $args->rewrite['with_front'] ) {
87 $slug = substr( $wp_rewrite->front, 1 ) . $slug;
88 }
89
90 if ( CPTP_Util::get_post_type_date_archive_support( $post_type ) ) {
91 $date_front = CPTP_Util::get_date_front( $post_type );
92 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' );
93 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' );
94 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' );
95 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' );
96 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' );
97 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' );
98 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' );
99 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' );
100 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' );
101 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' );
102 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' );
103 add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type=' . $post_type, 'top' );
104 }
105
106 if ( CPTP_Util::get_post_type_author_archive_support( $post_type ) ) {
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 $category_base = get_option( 'category_base', 'category' );
113
114 add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
115 add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/?$', 'index.php?category_name=$matches[1]&post_type=' . $post_type, 'top' );
116 }
117
118 do_action( 'CPTP_registered_' . $post_type . '_rules', $args, $slug );
119 }
120
121 $rewrite_args['walk_dirs'] = false;
122 add_permastruct( $post_type, $permalink, $rewrite_args );
123 }
124
125
126 /**
127 * Register_taxonomy_rules
128 *
129 * @param string $taxonomy Taxonomy slug.
130 * @param array|string $object_type Object type or array of object types.
131 * @param array $args Array of taxonomy registration arguments.
132 *
133 * @return void
134 */
135 public function register_taxonomy_rules( $taxonomy, $object_type, $args ) {
136 global $wp_rewrite;
137
138 /* for 4.7 */
139 $args = (array) $args;
140
141 if ( CPTP_Util::get_no_taxonomy_structure() ) {
142 return;
143 }
144
145 if ( ! empty( $args['_builtin'] ) ) {
146 return;
147 }
148
149 if ( false === $args['rewrite'] ) {
150 return;
151 }
152
153 $post_types = $args['object_type'];
154 foreach ( $post_types as $post_type ) :
155 $post_type_obj = get_post_type_object( $post_type );
156 if ( ! empty( $post_type_obj->rewrite['slug'] ) ) {
157 $slug = $post_type_obj->rewrite['slug'];
158 } else {
159 $slug = $post_type;
160 }
161
162 if ( ! empty( $post_type_obj->has_archive ) && is_string( $post_type_obj->has_archive ) ) {
163 $slug = $post_type_obj->has_archive;
164 };
165
166 if ( ! empty( $post_type_obj->rewrite['with_front'] ) ) {
167 $slug = substr( $wp_rewrite->front, 1 ) . $slug;
168 }
169
170 if ( 'category' === $taxonomy ) {
171 $cb = get_option( 'category_base' );
172 $taxonomy_slug = ( $cb ) ? $cb : $taxonomy;
173 $taxonomy_key = 'category_name';
174 } else {
175 // Edit by [Xiphe].
176 if ( isset( $args['rewrite']['slug'] ) ) {
177 $taxonomy_slug = $args['rewrite']['slug'];
178 } else {
179 $taxonomy_slug = $taxonomy;
180 }
181 // [Xiphe] stop
182 $taxonomy_key = $taxonomy;
183 }
184
185 $rules = array(
186 // feed.
187 array(
188 'regex' => '%s/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$',
189 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]",
190 ),
191 array(
192 'regex' => '%s/(.+?)/(feed|rdf|rss|rss2|atom)/?$',
193 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]",
194 ),
195 // year.
196 array(
197 'regex' => '%s/(.+?)/date/([0-9]{4})/?$',
198 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]",
199 ),
200 array(
201 'regex' => '%s/(.+?)/date/([0-9]{4})/page/?([0-9]{1,})/?$',
202 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&paged=\$matches[3]",
203 ),
204 // monthnum.
205 array(
206 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$',
207 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]",
208 ),
209 array(
210 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$',
211 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&paged=\$matches[4]",
212 ),
213 // day.
214 array(
215 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$',
216 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]",
217 ),
218 array(
219 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$',
220 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]&paged=\$matches[5]",
221 ),
222 // paging.
223 array(
224 'regex' => '%s/(.+?)/page/?([0-9]{1,})/?$',
225 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&paged=\$matches[2]",
226 ),
227 // tax archive.
228 array(
229 'regex' => '%s/(.+?)/?$',
230 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]",
231 ),
232 );
233
234 // no post_type slug.
235 foreach ( $rules as $rule ) {
236 $regex = sprintf( $rule['regex'], "{$taxonomy_slug}" );
237 $redirect = $rule['redirect'];
238 add_rewrite_rule( $regex, $redirect, 'top' );
239 }
240
241 if ( get_option( 'add_post_type_for_tax' ) ) {
242 foreach ( $rules as $rule ) {
243 $regex = sprintf( $rule['regex'], "{$slug}/{$taxonomy_slug}" );
244 $redirect = $rule['redirect'] . "&post_type={$post_type}";
245 add_rewrite_rule( $regex, $redirect, 'top' );
246 }
247 } else {
248 foreach ( $rules as $rule ) {
249 $regex = sprintf( $rule['regex'], "{$slug}/{$taxonomy_slug}" );
250 $redirect = $rule['redirect'];
251 add_rewrite_rule( $regex, $redirect, 'top' );
252 }
253 }
254
255 do_action( 'CPTP_registered_' . $taxonomy . '_rules', $object_type, $args, $taxonomy_slug );
256 endforeach;
257 }
258
259
260 /**
261 *
262 * Fix taxonomy = parent/child => taxonomy => child
263 *
264 * @since 0.9.3
265 *
266 * @param WP $obj WP instance.
267 */
268 public function parse_request( $obj ) {
269 $taxes = CPTP_Util::get_taxonomies( true );
270 foreach ( $taxes as $key => $tax ) {
271 $name = $tax->name;
272 if ( $tax->hierarchical ) {
273 if ( isset( $obj->query_vars[ $name ] ) && is_string( $obj->query_vars[ $name ] ) ) {
274 if ( false !== strpos( $obj->query_vars[ $name ], '/' ) ) {
275 $query_vars = explode( '/', $obj->query_vars[ $name ] );
276 if ( is_array( $query_vars ) ) {
277 $obj->query_vars[ $name ] = array_pop( $query_vars );
278 }
279 }
280 }
281 }
282 }
283 }
284
285 /**
286 * Returns the slug placeholder user in the permalink structure.
287 *
288 * @param string $post_type The post type.
289 *
290 * @return string
291 */
292 public static function get_slug_placeholder( $post_type ) {
293 return '%' . $post_type . '_slug%';
294 }
295 }
296