Admin.php
8 years ago
FlushRules.php
8 years ago
GetArchives.php
8 years ago
Option.php
8 years ago
Permalink.php
8 years ago
Rewrite.php
7 years ago
Setting.php
8 years ago
Permalink.php
390 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Override Output Permalinks |
| 4 | * |
| 5 | * @package Custom_Post_Type_Permalinks |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * CPTP_Module_Permalink |
| 10 | * |
| 11 | * @since 0.9.4 |
| 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 link url. |
| 49 | * @param WP_Post $post post object. |
| 50 | * @param String $leavename for edit.php. |
| 51 | * |
| 52 | * @version 2.0 |
| 53 | * |
| 54 | * @return string |
| 55 | */ |
| 56 | public function post_type_link( $post_link, $post, $leavename ) { |
| 57 | /** |
| 58 | * WP_Rewrite. |
| 59 | * |
| 60 | * @var WP_Rewrite $wp_rewrite |
| 61 | */ |
| 62 | global $wp_rewrite; |
| 63 | |
| 64 | if ( ! $wp_rewrite->permalink_structure ) { |
| 65 | return $post_link; |
| 66 | } |
| 67 | |
| 68 | $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( |
| 69 | 'draft', |
| 70 | 'pending', |
| 71 | 'auto-draft', |
| 72 | ), true ); |
| 73 | if ( $draft_or_pending && ! $leavename ) { |
| 74 | return $post_link; |
| 75 | } |
| 76 | |
| 77 | $post_type = $post->post_type; |
| 78 | $pt_object = get_post_type_object( $post_type ); |
| 79 | |
| 80 | if ( false === $pt_object->rewrite ) { |
| 81 | return $post_link; |
| 82 | } |
| 83 | |
| 84 | if ( ! in_array( $post->post_type, CPTP_Util::get_post_types(), true ) ) { |
| 85 | return $post_link; |
| 86 | } |
| 87 | |
| 88 | $permalink = $wp_rewrite->get_extra_permastruct( $post_type ); |
| 89 | |
| 90 | $permalink = str_replace( '%post_id%', $post->ID, $permalink ); |
| 91 | $permalink = str_replace( '%' . $post_type . '_slug%', $pt_object->rewrite['slug'], $permalink ); |
| 92 | |
| 93 | // has parent. |
| 94 | $parentsDirs = ''; |
| 95 | if ( $pt_object->hierarchical ) { |
| 96 | if ( ! $leavename ) { |
| 97 | $postId = $post->ID; |
| 98 | while ( $parent = get_post( $postId )->post_parent ) { |
| 99 | $parentsDirs = get_post( $parent )->post_name . '/' . $parentsDirs; |
| 100 | $postId = $parent; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | $permalink = str_replace( '%' . $post_type . '%', $parentsDirs . '%' . $post_type . '%', $permalink ); |
| 106 | |
| 107 | if ( ! $leavename ) { |
| 108 | $permalink = str_replace( '%' . $post_type . '%', $post->post_name, $permalink ); |
| 109 | } |
| 110 | |
| 111 | // %post_id%/attachment/%attachement_name%; |
| 112 | $id = filter_input( INPUT_GET, 'post' ); |
| 113 | if ( null !== $id && intval( $id ) !== $post->ID ) { |
| 114 | $parent_structure = trim( CPTP_Util::get_permalink_structure( $post->post_type ), '/' ); |
| 115 | $parent_dirs = explode( '/', $parent_structure ); |
| 116 | if ( is_array( $parent_dirs ) ) { |
| 117 | $last_dir = array_pop( $parent_dirs ); |
| 118 | } else { |
| 119 | $last_dir = $parent_dirs; |
| 120 | } |
| 121 | |
| 122 | if ( '%post_id%' === $parent_structure || '%post_id%' === $last_dir ) { |
| 123 | $permalink = $permalink . '/attachment/'; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | $search = array(); |
| 128 | $replace = array(); |
| 129 | |
| 130 | $replace_tag = $this->create_taxonomy_replace_tag( $post->ID, $permalink ); |
| 131 | $search = $search + $replace_tag['search']; |
| 132 | $replace = $replace + $replace_tag['replace']; |
| 133 | |
| 134 | // from get_permalink. |
| 135 | $category = ''; |
| 136 | if ( false !== strpos( $permalink, '%category%' ) ) { |
| 137 | $categories = get_the_category( $post->ID ); |
| 138 | if ( $categories ) { |
| 139 | $categories = CPTP_Util::sort_terms( $categories ); |
| 140 | |
| 141 | $category_object = apply_filters( 'post_link_category', $categories[0], $categories, $post ); |
| 142 | $category_object = get_term( $category_object, 'category' ); |
| 143 | $category = $category_object->slug; |
| 144 | if ( $category_object->parent ) { |
| 145 | $parent = $category_object->parent; |
| 146 | $category = get_category_parents( $parent, false, '/', true ) . $category; |
| 147 | } |
| 148 | } |
| 149 | // show default category in permalinks, without |
| 150 | // having to assign it explicitly. |
| 151 | if ( empty( $category ) ) { |
| 152 | $default_category = get_term( get_option( 'default_category' ), 'category' ); |
| 153 | $category = is_wp_error( $default_category ) ? '' : $default_category->slug; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | $author = ''; |
| 158 | if ( false !== strpos( $permalink, '%author%' ) ) { |
| 159 | $authordata = get_userdata( $post->post_author ); |
| 160 | $author = $authordata->user_nicename; |
| 161 | } |
| 162 | |
| 163 | $post_date = strtotime( $post->post_date ); |
| 164 | $permalink = str_replace( |
| 165 | array( |
| 166 | '%year%', |
| 167 | '%monthnum%', |
| 168 | '%day%', |
| 169 | '%hour%', |
| 170 | '%minute%', |
| 171 | '%second%', |
| 172 | '%category%', |
| 173 | '%author%', |
| 174 | ), |
| 175 | array( |
| 176 | date( 'Y', $post_date ), |
| 177 | date( 'm', $post_date ), |
| 178 | date( 'd', $post_date ), |
| 179 | date( 'H', $post_date ), |
| 180 | date( 'i', $post_date ), |
| 181 | date( 's', $post_date ), |
| 182 | $category, |
| 183 | $author, |
| 184 | ), |
| 185 | $permalink |
| 186 | ); |
| 187 | $permalink = str_replace( $search, $replace, $permalink ); |
| 188 | $permalink = home_url( $permalink ); |
| 189 | return $permalink; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /** |
| 194 | * Create %tax% -> term |
| 195 | * |
| 196 | * @param int $post_id post id. |
| 197 | * @param string $permalink permalink uri. |
| 198 | * |
| 199 | * @return array |
| 200 | */ |
| 201 | private function create_taxonomy_replace_tag( $post_id, $permalink ) { |
| 202 | $search = array(); |
| 203 | $replace = array(); |
| 204 | |
| 205 | $taxonomies = CPTP_Util::get_taxonomies( true ); |
| 206 | |
| 207 | // %taxnomomy% -> parent/child |
| 208 | foreach ( $taxonomies as $taxonomy => $objects ) { |
| 209 | |
| 210 | if ( false !== strpos( $permalink, '%' . $taxonomy . '%' ) ) { |
| 211 | $terms = get_the_terms( $post_id, $taxonomy ); |
| 212 | |
| 213 | if ( $terms && ! is_wp_error( $terms ) ) { |
| 214 | $parents = array_map( array( __CLASS__, 'get_term_parent' ), $terms ); |
| 215 | $newTerms = array(); |
| 216 | foreach ( $terms as $key => $term ) { |
| 217 | if ( ! in_array( $term->term_id, $parents , true ) ) { |
| 218 | $newTerms[] = $term; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | $term_obj = reset( $newTerms ); |
| 223 | $term_slug = $term_obj->slug; |
| 224 | |
| 225 | if ( isset( $term_obj->parent ) && $term_obj->parent ) { |
| 226 | $term_slug = CPTP_Util::get_taxonomy_parents_slug( $term_obj->parent, $taxonomy, '/', true ) . $term_slug; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if ( isset( $term_slug ) ) { |
| 231 | $search[] = '%' . $taxonomy . '%'; |
| 232 | $replace[] = $term_slug; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return array( |
| 238 | 'search' => $search, |
| 239 | 'replace' => $replace, |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Get parent from term Object |
| 245 | * |
| 246 | * @param WP_Term $term Term Object. |
| 247 | * |
| 248 | * @return mixed |
| 249 | */ |
| 250 | private static function get_term_parent( $term ) { |
| 251 | if ( isset( $term->parent ) && $term->parent > 0 ) { |
| 252 | return $term->parent; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * Fix attachment output |
| 259 | * |
| 260 | * @version 1.0 |
| 261 | * @since 0.8.2 |
| 262 | * |
| 263 | * @param string $link permalink URI. |
| 264 | * @param int $post_id Post ID. |
| 265 | * |
| 266 | * @return string |
| 267 | */ |
| 268 | public function attachment_link( $link, $post_id ) { |
| 269 | /** |
| 270 | * WP_Rewrite. |
| 271 | * |
| 272 | * @var WP_Rewrite $wp_rewrite |
| 273 | */ |
| 274 | global $wp_rewrite; |
| 275 | |
| 276 | if ( ! $wp_rewrite->permalink_structure ) { |
| 277 | return $link; |
| 278 | } |
| 279 | |
| 280 | $post = get_post( $post_id ); |
| 281 | if ( ! $post->post_parent ) { |
| 282 | return $link; |
| 283 | } |
| 284 | |
| 285 | $post_parent = get_post( $post->post_parent ); |
| 286 | if ( ! $post_parent ) { |
| 287 | return $link; |
| 288 | } |
| 289 | |
| 290 | $pt_object = get_post_type_object( $post_parent->post_type ); |
| 291 | |
| 292 | if ( empty( $pt_object->rewrite ) ) { |
| 293 | return $link; |
| 294 | } |
| 295 | |
| 296 | if ( ! in_array( $post->post_type, CPTP_Util::get_post_types(), true ) ) { |
| 297 | return $link; |
| 298 | } |
| 299 | |
| 300 | $permalink = CPTP_Util::get_permalink_structure( $post_parent->post_type ); |
| 301 | $post_type = get_post_type_object( $post_parent->post_type ); |
| 302 | |
| 303 | if ( empty( $post_type->_builtin ) ) { |
| 304 | if ( strpos( $permalink, '%postname%' ) < strrpos( $permalink, '%post_id%' ) && false === strrpos( $link, 'attachment/' ) ) { |
| 305 | |
| 306 | $link = str_replace( $post->post_name, 'attachment/' . $post->post_name, $link ); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return $link; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Fix taxonomy link outputs. |
| 315 | * |
| 316 | * @since 0.6 |
| 317 | * @version 1.0 |
| 318 | * |
| 319 | * @param string $termlink link URI. |
| 320 | * @param Object $term Term Object. |
| 321 | * @param Object $taxonomy Taxonomy Object. |
| 322 | * |
| 323 | * @return string |
| 324 | */ |
| 325 | public function term_link( $termlink, $term, $taxonomy ) { |
| 326 | /** |
| 327 | * WP_Rewrite. |
| 328 | * |
| 329 | * @var WP_Rewrite $wp_rewrite |
| 330 | */ |
| 331 | global $wp_rewrite; |
| 332 | |
| 333 | if ( ! $wp_rewrite->permalink_structure ) { |
| 334 | return $termlink; |
| 335 | } |
| 336 | |
| 337 | if ( CPTP_Util::get_no_taxonomy_structure() ) { |
| 338 | return $termlink; |
| 339 | } |
| 340 | |
| 341 | $taxonomy = get_taxonomy( $taxonomy ); |
| 342 | |
| 343 | if ( empty( $taxonomy ) ) { |
| 344 | return $termlink; |
| 345 | } |
| 346 | |
| 347 | if ( $taxonomy->_builtin ) { |
| 348 | return $termlink; |
| 349 | } |
| 350 | |
| 351 | if ( ! $taxonomy->public ) { |
| 352 | return $termlink; |
| 353 | } |
| 354 | |
| 355 | $wp_home = rtrim( home_url(), '/' ); |
| 356 | |
| 357 | if ( in_array( get_post_type(), $taxonomy->object_type, true ) ) { |
| 358 | $post_type = get_post_type(); |
| 359 | } else { |
| 360 | $post_type = $taxonomy->object_type[0]; |
| 361 | } |
| 362 | |
| 363 | $front = substr( $wp_rewrite->front, 1 ); |
| 364 | $termlink = str_replace( $front, '', $termlink );// remove front. |
| 365 | |
| 366 | $post_type_obj = get_post_type_object( $post_type ); |
| 367 | |
| 368 | if ( empty( $post_type_obj ) ) { |
| 369 | return $termlink; |
| 370 | } |
| 371 | |
| 372 | $slug = $post_type_obj->rewrite['slug']; |
| 373 | $with_front = $post_type_obj->rewrite['with_front']; |
| 374 | |
| 375 | if ( $with_front ) { |
| 376 | $slug = $front . $slug; |
| 377 | } |
| 378 | |
| 379 | if ( ! empty( $slug ) ) { |
| 380 | $termlink = str_replace( $wp_home, $wp_home . '/' . $slug, $termlink ); |
| 381 | } |
| 382 | |
| 383 | if ( ! $taxonomy->rewrite['hierarchical'] ) { |
| 384 | $termlink = str_replace( $term->slug . '/', CPTP_Util::get_taxonomy_parents_slug( $term->term_id, $taxonomy->name, '/', true ), $termlink ); |
| 385 | } |
| 386 | |
| 387 | return $termlink; |
| 388 | } |
| 389 | } |
| 390 |