-
Notifications
You must be signed in to change notification settings - Fork 16
/
jigsaw-permalinks.php
155 lines (141 loc) · 5.06 KB
/
jigsaw-permalinks.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
<?php
class JigsawPermalinks {
public static function set_author_base( $base, $with_front = true ) {
global $wp_rewrite;
if ( $wp_rewrite ) {
$wp_rewrite->author_base = $base;
}
if ( !$with_front && $wp_rewrite ) {
$wp_rewrite->author_structure = '/' . $wp_rewrite->author_base. '/%author%';
}
}
public static function set_permalink( $post_type, $struc ) {
global $wp_rewrite;
$gallery_structure = $struc;
$wp_rewrite->add_rewrite_tag( "%".$post_type."%", '([^/]+)', $post_type."=" );
$wp_rewrite->add_permastruct( $post_type, $gallery_structure, false );
// add_action('init', function() use ($struc){
// global $wp_rewrite;
// $gallery_structure = $struc;
// $wp_rewrite->add_rewrite_tag("%foundation-post%", '([^/]+)', "foundation-post=");
// $wp_rewrite->add_permastruct('foundation-post', $gallery_structure, false);
// });
add_filter( 'post_type_link', array( 'JigsawPermalinks', '_post_type_permalink' ), 10, 3 );
}
static function _post_type_permalink( $permalink, $post_id, $leavename ) {
$post = get_post( $post_id );
$rewritecode = array(
'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
$leavename? '' : '%postname%',
'%post_id%',
'%category%',
'%author%',
$leavename? '' : '%pagename%',
);
if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
$unixtime = strtotime( $post->post_date );
$category = '';
if ( strpos( $permalink, '%category%' ) !== false ) {
$cats = get_the_category( $post->ID );
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' ); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->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_category( get_option( 'default_category' ) );
$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
}
}
$author = '';
if ( strpos( $permalink, '%author%' ) !== false ) {
$authordata = get_userdata( $post->post_author );
$author = $authordata->user_nicename;
}
$date = explode( " ", date( 'Y m d H i s', $unixtime ) );
$rewritereplace =
array(
$date[0],
$date[1],
$date[2],
$date[3],
$date[4],
$date[5],
$post->post_name,
$post->ID,
$category,
$author,
$post->post_name,
);
$permalink = str_replace( $rewritecode, $rewritereplace, $permalink );
} else { // if they're not using the fancy permalink option
}
return $permalink;
}
public static function set_search_permalink( $base = 'search' ) {
add_action( 'template_redirect', function() use ( $base ) {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( ( "/".$base."/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
} );
}
public static function add_cpt_to_authors( $cpt_slugs ) {
add_action( 'pre_get_posts', function( &$query ) use ( $cpt_slugs ) {
if ( $query->is_author ) {
$query->set( 'post_type', $cpt_slugs );
}
} );
}
public static function remove_permalink_slug( $cpt_slugs ) {
if ( is_string( $cpt_slugs ) ) {
$cpt_slugs = array( $cpt_slugs );
}
$removed_permalink_slugs = array();
if ( isset( $GLOBALS['removed_permalink_slugs'] ) ) {
$removed_permalink_slugs = $GLOBALS['removed_permalink_slugs'];
}
if ( is_array( $removed_permalink_slugs ) ) {
$removed_permalink_slugs = array_merge( $removed_permalink_slugs, $cpt_slugs );
} else {
$removed_permalink_slugs = $cpt_slugs;
}
$GLOBALS['removed_permalink_slugs'] = $removed_permalink_slugs;
if ( !has_filter( 'post_type_link', array( 'JigsawPermalinks', 'remove_permalink_slug_post_type_link' ) ) ) {
add_filter( 'post_type_link', array( 'JigsawPermalinks', 'remove_permalink_slug_post_type_link' ), 10, 3 );
}
if ( !has_action( 'pre_get_posts', array( 'JigsawPermalinks', 'remove_permalink_slug_pre_get_posts' ) ) ) {
add_action( 'pre_get_posts', array( 'JigsawPermalinks', 'remove_permalink_slug_pre_get_posts' ) );
}
}
static function remove_permalink_slug_post_type_link( $post_link, $post, $leavename ) {
$post_types = $GLOBALS['removed_permalink_slugs'];
if ( ! in_array( $post->post_type, $post_types ) || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
static function remove_permalink_slug_pre_get_posts( $query ) {
$post_types = $GLOBALS['removed_permalink_slugs'];
$post_types[] = 'page';
$post_types[] = 'post';
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) || !empty( $query->query['pagename'] ) )
$query->set( 'post_type', $post_types );
}
}