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