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