-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathPermalink.php
445 lines (376 loc) · 10.9 KB
/
Permalink.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/**
* Override Output Permalinks
*
* @package Custom_Post_Type_Permalinks
*/
/**
* CPTP_Module_Permalink
*
* @since 0.9.4
* */
class CPTP_Module_Permalink extends CPTP_Module {
/**
* Add Filter Hooks.
*/
public function add_hook() {
add_filter(
'post_type_link',
array( $this, 'post_type_link' ),
apply_filters( 'cptp_post_type_link_priority', 0 ),
4
);
add_filter(
'term_link',
array( $this, 'term_link' ),
apply_filters( 'cptp_term_link_priority', 0 ),
3
);
add_filter(
'attachment_link',
array( $this, 'attachment_link' ),
apply_filters( 'cptp_attachment_link_priority', 20 ),
2
);
add_filter(
'wpml_st_post_type_link_filter_original_slug',
array( $this, 'replace_post_slug_with_placeholder' ),
10,
3
);
}
/**
*
* Fix permalinks output.
*
* @param String $post_link link url.
* @param WP_Post $post post object.
* @param String $leavename for edit.php.
*
* @return string
* @version 2.0
*/
public function post_type_link( $post_link, $post, $leavename ) {
/**
* WP_Rewrite.
*
* @var WP_Rewrite $wp_rewrite
*/
global $wp_rewrite;
if ( ! $wp_rewrite->using_permalinks() ) {
return $post_link;
}
$draft_or_pending = isset( $post->post_status ) && in_array(
$post->post_status,
array(
'draft',
'pending',
'auto-draft',
),
true
);
if ( $draft_or_pending && ! $leavename ) {
return $post_link;
}
$post_type = $post->post_type;
$pt_object = get_post_type_object( $post_type );
if ( false === $pt_object->rewrite ) {
return $post_link;
}
if ( ! in_array( $post->post_type, CPTP_Util::get_post_types(), true ) ) {
return $post_link;
}
$permalink = $wp_rewrite->get_extra_permastruct( $post_type );
$permalink = str_replace( '%post_id%', $post->ID, $permalink );
$permalink = str_replace( CPTP_Module_Rewrite::get_slug_placeholder( $post_type ), $pt_object->rewrite['slug'], $permalink );
// has parent.
$parentsDirs = '';
if ( $pt_object->hierarchical ) {
if ( ! $leavename ) {
$postId = $post->ID;
while ( $parent = get_post( $postId )->post_parent ) {
$parentsDirs = get_post( $parent )->post_name . '/' . $parentsDirs;
$postId = $parent;
}
}
}
$permalink = str_replace( '%' . $post_type . '%', $parentsDirs . '%' . $post_type . '%', $permalink );
if ( ! $leavename ) {
$permalink = str_replace( '%' . $post_type . '%', $post->post_name, $permalink );
}
// %post_id%/attachment/%attachement_name%;
$id = filter_input( INPUT_GET, 'post' );
if ( 'attachment' === $post->post_type ) {
if ( null !== $id && intval( $id ) !== $post->ID ) {
$parent_structure = trim( CPTP_Util::get_permalink_structure( $post->post_type ), '/' );
$parent_dirs = explode( '/', $parent_structure );
if ( is_array( $parent_dirs ) ) {
$last_dir = array_pop( $parent_dirs );
} else {
$last_dir = $parent_dirs;
}
if ( '%post_id%' === $parent_structure || '%post_id%' === $last_dir ) {
$permalink = untrailingslashit( $permalink ) . '/attachment/';
}
}
}
$search = array();
$replace = array();
$replace_tag = $this->create_taxonomy_replace_tag( $post->ID, $permalink );
$search = $search + $replace_tag['search'];
$replace = $replace + $replace_tag['replace'];
// from get_permalink.
$category = '';
if ( false !== strpos( $permalink, '%category%' ) ) {
$categories = get_the_category( $post->ID );
if ( $categories ) {
$categories = CPTP_Util::sort_terms( $categories );
$category_object = reset( $categories );
// phpcs:ignore
$category_object = apply_filters( 'post_link_category', $category_object, $categories, $post );
/**
* Filters the category for a post of a custom post type.
*
* @since 3.4.0
*
* @param WP_Term $category_object Selected category.
* @param WP_Term[] $categories Categories set in post.
* @param WP_Post $post Post object.
*/
$category_object = apply_filters( 'cptp_post_link_category', $category_object, $categories, $post );
$category_object = get_term( $category_object, 'category' );
$category = $category_object->slug;
if ( $category_object->parent ) {
$parent = $category_object->parent;
$category = get_category_parents( $parent, false, '/', true ) . $category;
}
}
// show default category in permalinks, without
// having to assign it explicitly.
if ( empty( $category ) ) {
$default_category = get_term( get_option( 'default_category' ), 'category' );
$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
}
}
$author = '';
if ( false !== strpos( $permalink, '%author%' ) ) {
$authordata = get_userdata( $post->post_author );
$author = $authordata->user_nicename;
}
$post_date = strtotime( $post->post_date );
$permalink = str_replace(
array(
'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
'%category%',
'%author%',
),
array(
gmdate( 'Y', $post_date ),
gmdate( 'm', $post_date ),
gmdate( 'd', $post_date ),
gmdate( 'H', $post_date ),
gmdate( 'i', $post_date ),
gmdate( 's', $post_date ),
$category,
$author,
),
$permalink
);
$permalink = str_replace( $search, $replace, $permalink );
$permalink = home_url( $permalink );
return $permalink;
}
/**
* Create %tax% -> term
*
* @param int $post_id post id.
* @param string $permalink permalink uri.
*
* @return array
*/
private function create_taxonomy_replace_tag( $post_id, $permalink ) {
$search = array();
$replace = array();
$post = get_post( $post_id );
$taxonomies = CPTP_Util::get_taxonomies( true );
// %taxnomomy% -> parent/child
foreach ( $taxonomies as $taxonomy => $objects ) {
if ( false !== strpos( $permalink, '%' . $taxonomy . '%' ) ) {
$terms = get_the_terms( $post_id, $taxonomy );
if ( $terms && ! is_wp_error( $terms ) ) {
$parents = array_map( array( __CLASS__, 'get_term_parent' ), $terms );
$newTerms = array();
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->term_id, $parents, true ) ) {
$newTerms[] = $term;
}
}
$term_obj = reset( $newTerms );
/**
* Filters the term for a post of a custom post type.
*
* @since 3.4.0
*
* @param WP_Term $term_obj Selected term.
* @param WP_Term[] $terms Terms set in post.
* @param WP_Taxonomy $taxonomy Taxonomy object.
* @param WP_Post $post Post object.
*/
$term_obj = apply_filters( 'cptp_post_link_term', $term_obj, $terms, $taxonomy, $post );
$term_slug = $term_obj->slug;
if ( isset( $term_obj->parent ) && $term_obj->parent ) {
$term_slug = CPTP_Util::get_taxonomy_parents_slug( $term_obj->parent, $taxonomy, '/', true ) . $term_slug;
}
}
if ( isset( $term_slug ) ) {
$search[] = '%' . $taxonomy . '%';
$replace[] = $term_slug;
}
}
}
return array(
'search' => $search,
'replace' => $replace,
);
}
/**
* Get parent from term Object
*
* @param WP_Term $term Term Object.
*
* @return mixed
*/
private static function get_term_parent( $term ) {
if ( isset( $term->parent ) && $term->parent > 0 ) {
return $term->parent;
}
}
/**
* Fix attachment output
*
* @param string $link permalink URI.
* @param int $post_id Post ID.
*
* @return string
* @since 0.8.2
*
* @version 1.0
*/
public function attachment_link( $link, $post_id ) {
/**
* WP_Rewrite.
*
* @var WP_Rewrite $wp_rewrite
*/
global $wp_rewrite;
if ( ! $wp_rewrite->using_permalinks() ) {
return $link;
}
$post = get_post( $post_id );
if ( ! $post->post_parent ) {
return $link;
}
$post_parent = get_post( $post->post_parent );
if ( ! $post_parent ) {
return $link;
}
$pt_object = get_post_type_object( $post_parent->post_type );
if ( empty( $pt_object->rewrite ) ) {
return $link;
}
if ( ! in_array( $post->post_type, CPTP_Util::get_post_types(), true ) ) {
return $link;
}
$permalink = CPTP_Util::get_permalink_structure( $post_parent->post_type );
$post_type = get_post_type_object( $post_parent->post_type );
if ( empty( $post_type->_builtin ) ) {
if ( strpos( $permalink, '%postname%' ) < strrpos( $permalink, '%post_id%' ) && false === strrpos( $link, 'attachment/' ) ) {
$link = str_replace( $post->post_name, 'attachment/' . $post->post_name, $link );
}
}
return $link;
}
/**
* Fix taxonomy link outputs.
*
* @param string $termlink link URI.
* @param Object $term Term Object.
* @param Object $taxonomy Taxonomy Object.
*
* @return string
* @since 0.6
* @version 1.0
*/
public function term_link( $termlink, $term, $taxonomy ) {
/**
* WP_Rewrite.
*
* @var WP_Rewrite $wp_rewrite
*/
global $wp_rewrite;
if ( ! $wp_rewrite->using_permalinks() ) {
return $termlink;
}
if ( CPTP_Util::get_no_taxonomy_structure() ) {
return $termlink;
}
$taxonomy = get_taxonomy( $taxonomy );
if ( empty( $taxonomy ) ) {
return $termlink;
}
if ( $taxonomy->_builtin ) {
return $termlink;
}
if ( ! $taxonomy->public ) {
return $termlink;
}
$wp_home = rtrim( home_url(), '/' );
if ( in_array( get_post_type(), $taxonomy->object_type, true ) ) {
$post_type = get_post_type();
} else {
$post_type = $taxonomy->object_type[0];
}
$front = substr( $wp_rewrite->front, 1 );
$termlink = str_replace( $front, '', $termlink );// remove front.
$post_type_obj = get_post_type_object( $post_type );
if ( empty( $post_type_obj ) ) {
return $termlink;
}
if ( ! isset( $post_type_obj->rewrite['slug'] ) || ! isset( $post_type_obj->rewrite['with_front'] ) ) {
return $termlink;
}
$slug = $post_type_obj->rewrite['slug'];
$with_front = $post_type_obj->rewrite['with_front'];
if ( $with_front ) {
$slug = $front . $slug;
}
if ( ! empty( $slug ) ) {
$termlink = str_replace( $wp_home, $wp_home . '/' . $slug, $termlink );
}
if ( false !== $taxonomy->rewrite && ! $taxonomy->rewrite['hierarchical'] ) {
$termlink = str_replace( $term->slug . '/', CPTP_Util::get_taxonomy_parents_slug( $term->term_id, $taxonomy->name, '/', true ), $termlink );
}
return $termlink;
}
/**
* This filter is needed for WPML's compatibility. It will return
* the slug placeholder instead of the original CPT slug.
*
* @param string $original_slug The original CPT slug.
* @param string $post_link The post link.
* @param WP_Post $post The post.
*
* @return string
*/
public function replace_post_slug_with_placeholder( $original_slug, $post_link, $post ) {
if ( ! in_array( $post->post_type, CPTP_Util::get_post_types(), true ) ) {
return $original_slug;
}
return CPTP_Module_Rewrite::get_slug_placeholder( $post->post_type );
}
}