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