PluginProbe ʕ •ᴥ•ʔ
Custom Post Type Permalinks / 1.0.2
Custom Post Type Permalinks v1.0.2
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 / Permalink.php
custom-post-type-permalinks / CPTP / Module Last commit date
Admin.php 11 years ago FlushRules.php 11 years ago GetArchives.php 11 years ago Option.php 11 years ago Permalink.php 11 years ago Rewrite.php 11 years ago Setting.php 11 years ago
Permalink.php
317 lines
1 <?php
2
3
4 /**
5 *
6 * CPTP_Permalink
7 *
8 * Override Permalinks
9 * @package Custom_Post_Type_Permalinks
10 * @since 0.9.4
11 *
12 * */
13
14
15 class CPTP_Module_Permalink extends CPTP_Module {
16
17
18 public function add_hook() {
19 add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 4 );
20 add_filter( 'term_link', array( $this, 'term_link' ), 10, 3 );
21 add_filter( 'attachment_link', array( $this, 'attachment_link' ), 20 , 2 );
22 }
23
24
25 /**
26 *
27 * Fix permalinks output.
28 *
29 * @param String $post_link
30 * @param Object $post 投稿�
31
32 * @param String $leavename 記事編集画面でのみ渡される
33 *
34 * @version 2.0
35 *
36 * @return string
37 */
38 public function post_type_link( $post_link, $post, $leavename ) {
39 global /** @var WP_Rewrite $wp_rewrite */
40 $wp_rewrite;
41
42 if ( !$wp_rewrite->permalink_structure ) {
43 return $post_link;
44 }
45
46
47 $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
48 if ( $draft_or_pending and ! $leavename ) {
49 return $post_link;
50 }
51
52 $post_type = $post->post_type;
53 $permalink = $wp_rewrite->get_extra_permastruct( $post_type );
54 $pt_object = get_post_type_object( $post_type );
55
56 $permalink = str_replace( '%post_id%', $post->ID, $permalink );
57 $permalink = str_replace( '%'.$post_type.'_slug%', $pt_object->rewrite['slug'], $permalink );
58
59 //親ページが有るとき。
60 $parentsDirs = '';
61 if ( $pt_object->hierarchical ) {
62 if ( ! $leavename ){
63 $postId = $post->ID;
64 while ( $parent = get_post( $postId )->post_parent ) {
65 $parentsDirs = get_post( $parent )->post_name.'/'.$parentsDirs;
66 $postId = $parent;
67 }
68 }
69 }
70
71 $permalink = str_replace( '%'.$post_type.'%', $parentsDirs.'%'.$post_type.'%', $permalink );
72
73 if ( ! $leavename ) {
74 $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink );
75 }
76
77 //%post_id%/attachment/%attachement_name%;
78 //画像の編集ページでのリンク
79 if ( isset( $_GET['post'] ) && $_GET['post'] != $post->ID ) {
80 $parent_structure = trim( CPTP_Util::get_permalink_structure( $post->post_type ), '/' );
81 $parent_dirs = explode( '/', $parent_structure );
82 if ( is_array( $parent_dirs ) ) {
83 $last_dir = array_pop( $parent_dirs );
84 }
85 else {
86 $last_dir = $parent_dirs;
87 }
88
89 if ( '%post_id%' == $parent_structure or '%post_id%' == $last_dir ) {
90 $permalink = $permalink.'/attachment/';
91 }
92 }
93
94 $search = array();
95 $replace = array();
96
97 $replace_tag = $this->create_taxonomy_replace_tag( $post->ID , $permalink );
98 $search = $search + $replace_tag['search'];
99 $replace = $replace + $replace_tag['replace'];
100
101 //from get_permalink.
102 $category = '';
103 if ( false !== strpos( $permalink, '%category%' ) ) {
104 $categories = get_the_category( $post->ID );
105 if ( $categories ) {
106 usort( $categories, '_usort_terms_by_ID' ); // order by ID
107 $category_object = apply_filters( 'post_link_category', $categories[0], $categories, $post );
108 $category_object = get_term( $category_object, 'category' );
109 $category = $category_object->slug;
110 if ( $parent = $category_object->parent ) {
111 $category = get_category_parents( $parent, false, '/', true ) . $category;
112 }
113 }
114 // show default category in permalinks, without
115 // having to assign it explicitly
116 if ( empty( $category ) ) {
117 $default_category = get_term( get_option( 'default_category' ), 'category' );
118 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
119 }
120 }
121
122 $author = '';
123 if ( false !== strpos( $permalink, '%author%' ) ) {
124 $authordata = get_userdata( $post->post_author );
125 $author = $authordata->user_nicename;
126 }
127
128 $post_date = strtotime( $post->post_date );
129 $permalink = str_replace(
130 array(
131 '%year%',
132 '%monthnum%',
133 '%day%',
134 '%hour%',
135 '%minute%',
136 '%second%',
137 '%category%',
138 '%author%',
139 ),
140 array(
141 date( 'Y', $post_date ),
142 date( 'm', $post_date ),
143 date( 'd', $post_date ),
144 date( 'H', $post_date ),
145 date( 'i', $post_date ),
146 date( 's', $post_date ),
147 $category,
148 $author,
149 ),
150 $permalink
151 );
152 $permalink = str_replace( $search, $replace, $permalink );
153 $permalink = rtrim( home_url(), '/' ).'/'.ltrim( $permalink, '/' );
154
155 return $permalink;
156 }
157
158
159 /**
160 *
161 * create %tax% -> term
162 *
163 * @param int $post_id
164 * @param string $permalink
165 *
166 * @return array
167 */
168 private function create_taxonomy_replace_tag( $post_id, $permalink ) {
169 $search = array();
170 $replace = array();
171
172 $taxonomies = CPTP_Util::get_taxonomies( true );
173
174 //%taxnomomy% -> parent/child
175 //運用でケアすべきかも。
176
177 foreach ( $taxonomies as $taxonomy => $objects ) {
178
179 if ( false !== strpos( $permalink, '%'.$taxonomy.'%' ) ) {
180 $terms = wp_get_post_terms( $post_id, $taxonomy, array( 'orderby' => 'term_id' ) );
181
182 if ( $terms and ! is_wp_error( $terms ) ) {
183 $parents = array_map( array( __CLASS__, 'get_term_parent' ), $terms ); //親の一覧
184 $newTerms = array();
185 foreach ( $terms as $key => $term ) {
186 if ( ! in_array( $term->term_id, $parents ) ) {
187 $newTerms[] = $term;
188 }
189 }
190
191 //このブロックだけで良いはず。
192 $term_obj = reset( $newTerms ); //最初のOBjectのみを対象。
193 $term_slug = $term_obj->slug;
194
195 if ( isset( $term_obj->parent ) and 0 != $term_obj->parent ) {
196 $term_slug = CPTP_Util::get_taxonomy_parents( $term_obj->parent,$taxonomy, false, '/', true ) . $term_slug;
197 }
198 }
199
200 if ( isset( $term_slug ) ) {
201 $search[] = '%'.$taxonomy.'%';
202 $replace[] = $term_slug;
203 }
204 }
205 }
206
207 return array( 'search' => $search, 'replace' => $replace );
208 }
209
210 private static function get_term_parent( $term ) {
211 if ( isset( $term->parent ) and $term->parent > 0 ) {
212 return $term->parent;
213 }
214 }
215
216
217 /**
218 *
219 * fix attachment output
220 *
221 * @version 1.0
222 * @since 0.8.2
223 *
224 * @param string $link
225 * @param int $postID
226 *
227 * @return string
228 */
229
230 public function attachment_link( $link, $postID ) {
231 /** @var WP_Rewrite $wp_rewrite */
232 global $wp_rewrite;
233
234 if ( !$wp_rewrite->permalink_structure ) {
235 return $link;
236 }
237
238
239 $post = get_post( $postID );
240 if ( ! $post->post_parent ){
241 return $link;
242 }
243 $post_parent = get_post( $post->post_parent );
244 $permalink = CPTP_Util::get_permalink_structure( $post_parent->post_type );
245 $post_type = get_post_type_object( $post_parent->post_type );
246
247 if ( false == $post_type->_builtin ) {
248 if ( strpos( $permalink, '%postname%' ) < strrpos( $permalink, '%post_id%' ) && false === strrpos( $permalink, 'attachment/' ) ){
249 $link = str_replace( $post->post_name, 'attachment/'.$post->post_name, $link );
250 }
251 }
252
253 return $link;
254 }
255
256 /**
257 *
258 * Fix taxonomy link outputs.
259 * @since 0.6
260 * @version 1.0
261 *
262 * @param string $termlink
263 * @param Object $term
264 * @param Object $taxonomy
265 *
266 * @return string
267 */
268 public function term_link( $termlink, $term, $taxonomy ) {
269 /** @var WP_Rewrite $wp_rewrite */
270 global $wp_rewrite;
271
272 if ( !$wp_rewrite->permalink_structure ) {
273 return $termlink;
274 }
275
276 if ( get_option( 'no_taxonomy_structure' ) ) {
277 return $termlink;
278 }
279
280 $taxonomy = get_taxonomy( $taxonomy );
281 if ( $taxonomy->_builtin ) {
282 return $termlink;
283 }
284
285 if ( empty( $taxonomy ) ) {
286 return $termlink;
287 }
288
289 $wp_home = rtrim( home_url(), '/' );
290
291 if ( in_array( get_post_type(), $taxonomy->object_type ) ) {
292 $post_type = get_post_type();
293 }
294 else {
295 $post_type = $taxonomy->object_type[0];
296 }
297 $post_type_obj = get_post_type_object( $post_type );
298 $slug = $post_type_obj->rewrite['slug'];
299 $with_front = $post_type_obj->rewrite['with_front'];
300 $front = substr( $wp_rewrite->front, 1 );
301 $termlink = str_replace( $front, '', $termlink );
302
303 if ( $with_front ) {
304 $slug = $front.$slug;
305 }
306
307 $termlink = str_replace( $wp_home, $wp_home.'/'.$slug, $termlink );
308
309 if ( ! $taxonomy->rewrite['hierarchical'] ) {
310 $termlink = str_replace( $term->slug.'/', CPTP_Util::get_taxonomy_parents( $term->term_id,$taxonomy->name, false, '/', true ), $termlink );
311 }
312
313 return $termlink;
314 }
315
316 }
317