Admin.php
10 years ago
FlushRules.php
11 years ago
GetArchives.php
10 years ago
Option.php
11 years ago
Permalink.php
10 years ago
Rewrite.php
11 years ago
Setting.php
10 years ago
Permalink.php
344 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 = home_url( $permalink ); |
| 175 | return $permalink; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /** |
| 180 | * |
| 181 | * create %tax% -> term |
| 182 | * |
| 183 | * @param int $post_id |
| 184 | * @param string $permalink |
| 185 | * |
| 186 | * @return array |
| 187 | */ |
| 188 | private function create_taxonomy_replace_tag( $post_id, $permalink ) { |
| 189 | $search = array(); |
| 190 | $replace = array(); |
| 191 | |
| 192 | $taxonomies = CPTP_Util::get_taxonomies( true ); |
| 193 | |
| 194 | //%taxnomomy% -> parent/child |
| 195 | //運用でケアすべきかも。 |
| 196 | |
| 197 | foreach ( $taxonomies as $taxonomy => $objects ) { |
| 198 | |
| 199 | if ( false !== strpos( $permalink, '%' . $taxonomy . '%' ) ) { |
| 200 | $terms = wp_get_post_terms( $post_id, $taxonomy, array( 'orderby' => 'term_id' ) ); |
| 201 | |
| 202 | if ( $terms and ! is_wp_error( $terms ) ) { |
| 203 | $parents = array_map( array( __CLASS__, 'get_term_parent' ), $terms ); //親の一覧 |
| 204 | $newTerms = array(); |
| 205 | foreach ( $terms as $key => $term ) { |
| 206 | if ( ! in_array( $term->term_id, $parents ) ) { |
| 207 | $newTerms[] = $term; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | //このブロックだけで良いはず。 |
| 212 | $term_obj = reset( $newTerms ); //最初のOBjectのみを対象。 |
| 213 | $term_slug = $term_obj->slug; |
| 214 | |
| 215 | if ( isset( $term_obj->parent ) and 0 != $term_obj->parent ) { |
| 216 | $term_slug = CPTP_Util::get_taxonomy_parents( $term_obj->parent, $taxonomy, false, '/', true ) . $term_slug; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if ( isset( $term_slug ) ) { |
| 221 | $search[] = '%' . $taxonomy . '%'; |
| 222 | $replace[] = $term_slug; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return array( 'search' => $search, 'replace' => $replace ); |
| 228 | } |
| 229 | |
| 230 | private static function get_term_parent( $term ) { |
| 231 | if ( isset( $term->parent ) and $term->parent > 0 ) { |
| 232 | return $term->parent; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | /** |
| 238 | * |
| 239 | * fix attachment output |
| 240 | * |
| 241 | * @version 1.0 |
| 242 | * @since 0.8.2 |
| 243 | * |
| 244 | * @param string $link |
| 245 | * @param int $postID |
| 246 | * |
| 247 | * @return string |
| 248 | */ |
| 249 | |
| 250 | public function attachment_link( $link, $postID ) { |
| 251 | /** @var WP_Rewrite $wp_rewrite */ |
| 252 | global $wp_rewrite; |
| 253 | |
| 254 | if ( ! $wp_rewrite->permalink_structure ) { |
| 255 | return $link; |
| 256 | } |
| 257 | |
| 258 | $post = get_post( $postID ); |
| 259 | if ( ! $post->post_parent ) { |
| 260 | return $link; |
| 261 | } |
| 262 | $post_parent = get_post( $post->post_parent ); |
| 263 | $permalink = CPTP_Util::get_permalink_structure( $post_parent->post_type ); |
| 264 | $post_type = get_post_type_object( $post_parent->post_type ); |
| 265 | |
| 266 | if ( false == $post_type->_builtin ) { |
| 267 | if ( strpos( $permalink, '%postname%' ) < strrpos( $permalink, '%post_id%' ) && false === strrpos( $permalink, 'attachment/' ) ) { |
| 268 | $link = str_replace( $post->post_name, 'attachment/' . $post->post_name, $link ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return $link; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * |
| 277 | * Fix taxonomy link outputs. |
| 278 | * @since 0.6 |
| 279 | * @version 1.0 |
| 280 | * |
| 281 | * @param string $termlink |
| 282 | * @param Object $term |
| 283 | * @param Object $taxonomy |
| 284 | * |
| 285 | * @return string |
| 286 | */ |
| 287 | public function term_link( $termlink, $term, $taxonomy ) { |
| 288 | /** @var WP_Rewrite $wp_rewrite */ |
| 289 | global $wp_rewrite; |
| 290 | |
| 291 | if ( ! $wp_rewrite->permalink_structure ) { |
| 292 | return $termlink; |
| 293 | } |
| 294 | |
| 295 | if ( get_option( 'no_taxonomy_structure' ) ) { |
| 296 | return $termlink; |
| 297 | } |
| 298 | |
| 299 | $taxonomy = get_taxonomy( $taxonomy ); |
| 300 | if ( $taxonomy->_builtin ) { |
| 301 | return $termlink; |
| 302 | } |
| 303 | |
| 304 | if ( empty( $taxonomy ) ) { |
| 305 | return $termlink; |
| 306 | } |
| 307 | |
| 308 | $wp_home = rtrim( home_url(), '/' ); |
| 309 | |
| 310 | if ( in_array( get_post_type(), $taxonomy->object_type ) ) { |
| 311 | $post_type = get_post_type(); |
| 312 | } else { |
| 313 | $post_type = $taxonomy->object_type[0]; |
| 314 | } |
| 315 | |
| 316 | $front = substr( $wp_rewrite->front, 1 ); |
| 317 | $termlink = str_replace( $front, '', $termlink );//remove front. |
| 318 | |
| 319 | $post_type_obj = get_post_type_object( $post_type ); |
| 320 | |
| 321 | if( empty( $post_type_obj ) ) { |
| 322 | return $termlink; |
| 323 | } |
| 324 | |
| 325 | $slug = $post_type_obj->rewrite['slug']; |
| 326 | $with_front = $post_type_obj->rewrite['with_front']; |
| 327 | |
| 328 | if ( $with_front ) { |
| 329 | $slug = $front . $slug; |
| 330 | } |
| 331 | |
| 332 | if( !empty( $slug )) { |
| 333 | $termlink = str_replace( $wp_home, $wp_home . '/' . $slug, $termlink ); |
| 334 | } |
| 335 | |
| 336 | if ( ! $taxonomy->rewrite['hierarchical'] ) { |
| 337 | $termlink = str_replace( $term->slug . '/', CPTP_Util::get_taxonomy_parents( $term->term_id, $taxonomy->name, false, '/', true ), $termlink ); |
| 338 | } |
| 339 | |
| 340 | return $termlink; |
| 341 | } |
| 342 | |
| 343 | } |
| 344 |