custom-post-type-permalinks
Last commit date
language
12 years ago
custom-post-type-permalinks.php
12 years ago
readme.txt
12 years ago
screenshot-1.png
14 years ago
custom-post-type-permalinks.php
808 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Custom Post Type Permalinks |
| 4 | Plugin URI: http://www.torounit.com |
| 5 | Description: Add post archives of custom post type and customizable permalinks. |
| 6 | Author: Toro_Unit |
| 7 | Author URI: http://www.torounit.com/plugins/custom-post-type-permalinks/ |
| 8 | Version: 0.9.3.2 |
| 9 | Text Domain: cptp |
| 10 | License: GPL2 or later |
| 11 | Domain Path: /language/ |
| 12 | */ |
| 13 | |
| 14 | |
| 15 | /* Copyright 2012 Toro_Unit (email : mail@torounit.com) |
| 16 | |
| 17 | This program is free software; you can redistribute it and/or modify |
| 18 | it under the terms of the GNU General Public License as published by |
| 19 | the Free Software Foundation; either version 2 of the License, or |
| 20 | (at your option) any later version. |
| 21 | |
| 22 | This program is distributed in the hope that it will be useful, |
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | GNU General Public License for more details. |
| 26 | |
| 27 | You should have received a copy of the GNU General Public License |
| 28 | along with this program; if not, write to the Free Software |
| 29 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * |
| 36 | * Custom Post Type Permalinks |
| 37 | * |
| 38 | * |
| 39 | */ |
| 40 | class Custom_Post_Type_Permalinks { |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | public $version = "0.9"; |
| 46 | |
| 47 | public $default_structure = '/%postname%/'; |
| 48 | |
| 49 | /** |
| 50 | * |
| 51 | * Add Action & filter hooks. |
| 52 | * |
| 53 | */ |
| 54 | public function add_hook () { |
| 55 | |
| 56 | add_action( 'plugins_loaded', array(&$this,'check_version') ); |
| 57 | add_action( 'wp_loaded', array(&$this,'add_archive_rewrite_rules'), 99 ); |
| 58 | add_action( 'wp_loaded', array(&$this,'add_tax_rewrite_rules') ); |
| 59 | add_action( 'wp_loaded', array(&$this, "dequeue_flush_rules"),100); |
| 60 | add_action( 'parse_request', array(&$this, "parse_request") ); |
| 61 | add_action( 'registered_post_type', array(&$this,'registered_post_type'), 10, 2 ); |
| 62 | |
| 63 | |
| 64 | if(get_option( "permalink_structure") != "") { |
| 65 | add_filter( 'post_type_link', array(&$this,'post_type_link'), 10, 4 ); |
| 66 | add_filter( 'getarchives_join', array(&$this,'getarchives_join'), 10, 2 ); // [steve] |
| 67 | add_filter( 'getarchives_where', array(&$this,'getarchives_where'), 10 , 2 ); |
| 68 | add_filter( 'get_archives_link', array(&$this,'get_archives_link'), 20, 1 ); |
| 69 | add_filter( 'term_link', array(&$this,'term_link'), 10, 3 ); |
| 70 | add_filter( 'attachment_link', array(&$this, 'attachment_link'), 20 , 2); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | add_action( 'init', array(&$this,'load_textdomain') ); |
| 75 | add_action( 'init', array(&$this, 'update_rules') ); |
| 76 | add_action( 'update_option_cptp_version', array(&$this, 'update_rules') ); |
| 77 | add_action( 'admin_init', array(&$this,'settings_api_init'), 30 ); |
| 78 | add_action( 'admin_enqueue_scripts', array(&$this,'enqueue_css_js') ); |
| 79 | add_action( 'admin_footer', array(&$this,'pointer_js') ); |
| 80 | |
| 81 | |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * |
| 86 | * dequeue flush rules |
| 87 | * @since 0.9 |
| 88 | * |
| 89 | */ |
| 90 | |
| 91 | public function dequeue_flush_rules () { |
| 92 | if(get_option("queue_flush_rules")){ |
| 93 | flush_rewrite_rules(); |
| 94 | update_option( "queue_flush_rules", 0 ); |
| 95 | |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * |
| 102 | * dequeue flush rules |
| 103 | * @since 0.8.6 |
| 104 | * |
| 105 | */ |
| 106 | |
| 107 | public function check_version() { |
| 108 | $version = get_option('cptp_version', 0); |
| 109 | if($version != $this->version) { |
| 110 | update_option('cptp_version', $this->version); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * |
| 116 | * Get Custom Taxonomies parents. |
| 117 | * @version 1.0 |
| 118 | * |
| 119 | */ |
| 120 | private function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) { |
| 121 | $chain = ''; |
| 122 | $parent = &get_term( $id, $taxonomy, OBJECT, 'raw'); |
| 123 | if ( is_wp_error( $parent ) ) { |
| 124 | return $parent; |
| 125 | } |
| 126 | |
| 127 | if ( $nicename ){ |
| 128 | $name = $parent->slug; |
| 129 | }else { |
| 130 | $name = $parent->name; |
| 131 | } |
| 132 | |
| 133 | if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { |
| 134 | $visited[] = $parent->parent; |
| 135 | $chain .= $this->get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited ); |
| 136 | } |
| 137 | |
| 138 | if ( $link ) { |
| 139 | $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator; |
| 140 | }else { |
| 141 | $chain .= $name.$separator; |
| 142 | } |
| 143 | return $chain; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * |
| 150 | * Add rewrite rules for archives. |
| 151 | * @version 1.1 |
| 152 | * |
| 153 | */ |
| 154 | public function add_archive_rewrite_rules() { |
| 155 | $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) ); |
| 156 | |
| 157 | foreach ( $post_types as $post_type ): |
| 158 | if( !$post_type ) { |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | $permalink = get_option( $post_type.'_structure' ); |
| 163 | $post_type_obj = get_post_type_object($post_type); |
| 164 | $slug = $post_type_obj->rewrite['slug']; |
| 165 | if( !$slug ) |
| 166 | $slug = $post_type; |
| 167 | |
| 168 | if( $post_type_obj->has_archive ){ |
| 169 | if( is_string( $post_type_obj->has_archive ) ){ |
| 170 | $slug = $post_type_obj->has_archive; |
| 171 | }; |
| 172 | |
| 173 | |
| 174 | add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$post_type, 'top' ); |
| 175 | add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$post_type, 'top' ); |
| 176 | add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type='.$post_type, 'top' ); |
| 177 | add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type='.$post_type, 'top' ); |
| 178 | add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$post_type, 'top' ); |
| 179 | add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$post_type, 'top' ); |
| 180 | add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type='.$post_type, 'top' ); |
| 181 | add_rewrite_rule( $slug.'/date/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&post_type='.$post_type, 'top' ); |
| 182 | add_rewrite_rule( $slug.'/date/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type, 'top' ); |
| 183 | add_rewrite_rule( $slug.'/date/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type, 'top' ); |
| 184 | add_rewrite_rule( $slug.'/date/([0-9]{4})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&paged=$matches[2]&post_type='.$post_type, 'top' ); |
| 185 | add_rewrite_rule( $slug.'/date/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type='.$post_type, 'top' ); |
| 186 | add_rewrite_rule( $slug.'/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type='.$post_type, 'top' ); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | endforeach; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * |
| 195 | * registered_post_type |
| 196 | * ** add rewrite tag for Custom Post Type. |
| 197 | * @version 1.1 |
| 198 | * @since 0.9 |
| 199 | * |
| 200 | */ |
| 201 | |
| 202 | public function registered_post_type( $post_type, $args ) { |
| 203 | |
| 204 | global $wp_post_types, $wp_rewrite, $wp; |
| 205 | |
| 206 | if( $args->_builtin or !$args->publicly_queryable or !$args->show_ui ){ |
| 207 | return false; |
| 208 | } |
| 209 | $permalink = get_option( $post_type.'_structure' ); |
| 210 | |
| 211 | if( !$permalink ) { |
| 212 | $permalink = $this->default_structure; |
| 213 | } |
| 214 | |
| 215 | $permalink = '%'.$post_type.'_slug%'.$permalink; |
| 216 | $permalink = str_replace( '%postname%', '%'.$post_type.'%', $permalink ); |
| 217 | |
| 218 | add_rewrite_tag( '%'.$post_type.'_slug%', '('.$args->rewrite['slug'].')','post_type='.$post_type.'&slug=' ); |
| 219 | |
| 220 | $taxonomies = get_taxonomies( array("show_ui" => true, "_builtin" => false), 'objects' ); |
| 221 | foreach ( $taxonomies as $taxonomy => $objects ): |
| 222 | $wp_rewrite->add_rewrite_tag( "%$taxonomy%", '(.+?)', "$taxonomy=" ); |
| 223 | endforeach; |
| 224 | |
| 225 | $permalink = trim($permalink, "/" ); |
| 226 | add_permastruct( $post_type, $permalink, $args->rewrite ); |
| 227 | |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | /** |
| 235 | * |
| 236 | * fix attachment output |
| 237 | * |
| 238 | * @version 1.0 |
| 239 | * @since 0.8.2 |
| 240 | * |
| 241 | */ |
| 242 | |
| 243 | public function attachment_link( $link , $postID ) { |
| 244 | $post = get_post( $postID ); |
| 245 | if (!$post->post_parent){ |
| 246 | return $link; |
| 247 | } |
| 248 | $post_parent = get_post( $post->post_parent ); |
| 249 | $permalink = get_option( $post_parent->post_type.'_structure' ); |
| 250 | $post_type = get_post_type_object( $post_parent->post_type ); |
| 251 | |
| 252 | if( $post_type->_builtin == false ) { |
| 253 | if(strpos( $permalink, "%postname%" ) < strrpos( $permalink, "%post_id%" ) && strrpos( $permalink, "attachment/" ) === FALSE ){ |
| 254 | $link = str_replace($post->post_name , "attachment/".$post->post_name, $link); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return $link; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | |
| 263 | /** |
| 264 | * |
| 265 | * Fix permalinks output. |
| 266 | * |
| 267 | * @param String $post_link |
| 268 | * @param Object $post 投稿� |
| 269 | 報 |
| 270 | * @param String $leavename 記事編集画面でのみ渡される |
| 271 | * |
| 272 | * @version 2.0 |
| 273 | * |
| 274 | */ |
| 275 | public function post_type_link( $post_link, $post, $leavename ) { |
| 276 | |
| 277 | global $wp_rewrite; |
| 278 | $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); |
| 279 | if( $draft_or_pending and !$leavename ) |
| 280 | return $post_link; |
| 281 | |
| 282 | $post_type = $post->post_type; |
| 283 | $permalink = $wp_rewrite->get_extra_permastruct( $post_type ); |
| 284 | $permalink = str_replace( '%post_id%', $post->ID, $permalink ); |
| 285 | $permalink = str_replace( '%'.$post_type.'_slug%', get_post_type_object( $post_type )->rewrite['slug'], $permalink ); |
| 286 | |
| 287 | |
| 288 | $parentsDirs = ""; |
| 289 | if( !$leavename ){ |
| 290 | $postId = $post->ID; |
| 291 | while ($parent = get_post($postId)->post_parent) { |
| 292 | $parentsDirs = get_post($parent)->post_name."/".$parentsDirs; |
| 293 | $postId = $parent; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | $permalink = str_replace( '%'.$post_type.'%', $parentsDirs.'%'.$post_type.'%', $permalink ); |
| 298 | |
| 299 | if( !$leavename ){ |
| 300 | $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink ); |
| 301 | } |
| 302 | |
| 303 | //%post_id%/attachment/%attachement_name%; |
| 304 | //画像の編集ページでのリンク |
| 305 | if( isset($_GET["post"]) && $_GET["post"] != $post->ID ) { |
| 306 | $parent_structure = trim(get_option( $post->post_type.'_structure' ), "/"); |
| 307 | if( "%post_id%" == $parent_structure or "%post_id%" == array_pop( explode( "/", $parent_structure ) ) ) { |
| 308 | $permalink = $permalink."/attachment/"; |
| 309 | }; |
| 310 | } |
| 311 | |
| 312 | $taxonomies = get_taxonomies( array('show_ui' => true),'objects' ); |
| 313 | |
| 314 | //%taxnomomy% -> parent/child |
| 315 | //運用でケアすべきかも。 |
| 316 | foreach ( $taxonomies as $taxonomy => $objects ) { |
| 317 | if ( strpos($permalink, "%$taxonomy%") !== false ) { |
| 318 | $terms = wp_get_post_terms( $post->ID, $taxonomy, array('orderby' => 'term_id')); |
| 319 | if ( $terms and count($terms) > 1 ) { |
| 320 | if(reset($terms)->parent == 0){ |
| 321 | |
| 322 | $keys = array_keys($terms); |
| 323 | $term = $terms[$keys[1]]->slug; |
| 324 | if ( $terms[$keys[0]]->term_id == $terms[$keys[1]]->parent ) { |
| 325 | $term = $this->get_taxonomy_parents( $terms[$keys[1]]->parent,$taxonomy, false, '/', true ) . $term; |
| 326 | } |
| 327 | }else{ |
| 328 | $keys = array_keys($terms); |
| 329 | $term = $terms[$keys[0]]->slug; |
| 330 | if ( $terms[$keys[1]]->term_id == $terms[$keys[0]]->parent ) { |
| 331 | $term = $this->get_taxonomy_parents( $terms[$keys[0]]->parent,$taxonomy, false, '/', true ) . $term; |
| 332 | } |
| 333 | } |
| 334 | }else if( $terms ){ |
| 335 | |
| 336 | $term_obj = array_shift($terms); |
| 337 | $term = $term_obj->slug; |
| 338 | |
| 339 | if(isset($term_obj->parent) and $term_obj->parent != 0) { |
| 340 | $term = $this->get_taxonomy_parents( $term_obj->parent,$taxonomy, false, '/', true ) . $term; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if( isset($term) ) { |
| 345 | $permalink = str_replace( "%$taxonomy%", $term, $permalink ); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | $user = get_userdata( $post->post_author ); |
| 351 | if(isset($user->user_nicename)) { |
| 352 | $permalink = str_replace( "%author%", $user->user_nicename, $permalink ); |
| 353 | } |
| 354 | |
| 355 | $post_date = strtotime( $post->post_date ); |
| 356 | $permalink = str_replace( "%year%", date("Y",$post_date), $permalink ); |
| 357 | $permalink = str_replace( "%monthnum%", date("m",$post_date), $permalink ); |
| 358 | $permalink = str_replace( "%day%", date("d",$post_date), $permalink ); |
| 359 | $permalink = str_replace( "%hour%", date("H",$post_date), $permalink ); |
| 360 | $permalink = str_replace( "%minute%", date("i",$post_date), $permalink ); |
| 361 | $permalink = str_replace( "%second%", date("s",$post_date), $permalink ); |
| 362 | |
| 363 | |
| 364 | $permalink = home_url()."/".user_trailingslashit( $permalink ); |
| 365 | $permalink = str_replace("//", "/", $permalink); |
| 366 | $permalink = str_replace(":/", "://", $permalink); |
| 367 | return $permalink; |
| 368 | } |
| 369 | |
| 370 | |
| 371 | |
| 372 | /** |
| 373 | * |
| 374 | * wp_get_archives fix for custom post |
| 375 | * Ex:wp_get_archives('&post_type='.get_query_var( 'post_type' )); |
| 376 | * @version 2.0 |
| 377 | * |
| 378 | */ |
| 379 | |
| 380 | public $get_archives_where_r; |
| 381 | |
| 382 | // function modified by [steve] |
| 383 | public function getarchives_where( $where, $r ) { |
| 384 | $this->get_archives_where_r = $r; |
| 385 | if ( isset($r['post_type']) ) { |
| 386 | $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where ); |
| 387 | } |
| 388 | |
| 389 | if(isset($r['taxonomy']) && is_array($r['taxonomy']) ){ |
| 390 | global $wpdb; |
| 391 | $where = $where . " AND $wpdb->term_taxonomy.taxonomy = '".$r['taxonomy']['name']."' AND $wpdb->term_taxonomy.term_id = '".$r['taxonomy']['termid']."'"; |
| 392 | } |
| 393 | |
| 394 | return $where; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | |
| 399 | //function added by [steve] |
| 400 | /** |
| 401 | * |
| 402 | * get_archive_join |
| 403 | * @author Steve |
| 404 | * @since 0.8 |
| 405 | * @version 1.0 |
| 406 | * |
| 407 | * |
| 408 | */ |
| 409 | public function getarchives_join( $join, $r ) { |
| 410 | global $wpdb; |
| 411 | $this->get_archives_where_r = $r; |
| 412 | if(isset($r['taxonomy']) && is_array($r['taxonomy']) ) |
| 413 | $join = $join . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)"; |
| 414 | |
| 415 | return $join; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | |
| 420 | /** |
| 421 | * |
| 422 | * get_arcihves_link |
| 423 | * @version 2.1 |
| 424 | * |
| 425 | */ |
| 426 | public function get_archives_link( $link ) { |
| 427 | if(!isset($this->get_archives_where_r['post_type'])) { |
| 428 | return $link; |
| 429 | } |
| 430 | $c = isset($this->get_archives_where_r['taxonomy']) && is_array($this->get_archives_where_r['taxonomy']) ? $this->get_archives_where_r['taxonomy'] : ""; //[steve] |
| 431 | $t = $this->get_archives_where_r['post_type']; |
| 432 | |
| 433 | |
| 434 | $this->get_archives_where_r['post_type'] = isset($this->get_archives_where_r['post_type_slug']) ? $this->get_archives_where_r['post_type_slug'] : $t; // [steve] [*** bug fixing] |
| 435 | |
| 436 | if (isset($this->get_archives_where_r['post_type']) and $this->get_archives_where_r['type'] != 'postbypost'){ |
| 437 | $blog_url = rtrim( get_bloginfo("url") ,'/'); |
| 438 | |
| 439 | //remove .ext |
| 440 | $str = preg_replace("/\.[a-z,_]*/","",get_option("permalink_structure")); |
| 441 | |
| 442 | if($str = rtrim( preg_replace("/%[a-z,_]*%/","",$str) ,'/')) { // /archive/%post_id% |
| 443 | $ret_link = str_replace($str, '/'.'%link_dir%', $link); |
| 444 | }else{ |
| 445 | $blog_url = preg_replace('/https?:\/\//', '', $blog_url); |
| 446 | $ret_link = str_replace($blog_url,$blog_url.'/'.'%link_dir%',$link); |
| 447 | } |
| 448 | |
| 449 | $post_type = get_post_type_object( $this->get_archives_where_r['post_type'] ); |
| 450 | if(empty($c) ){ // [steve] |
| 451 | if(isset( $post_type->rewrite["slug"] )) { |
| 452 | $link_dir = $post_type->rewrite["slug"]; |
| 453 | } |
| 454 | else{ |
| 455 | $link_dir = $this->get_archives_where_r['post_type']; |
| 456 | } |
| 457 | } |
| 458 | else{ // [steve] |
| 459 | $c['name'] = ($c['name'] == 'category' && get_option('category_base')) ? get_option('category_base') : $c['name']; |
| 460 | $link_dir = $post_type->rewrite["slug"]."/".$c['name']."/".$c['termslug']; |
| 461 | } |
| 462 | |
| 463 | if(!strstr($link,'/date/')){ |
| 464 | $link_dir = $link_dir .'/date'; |
| 465 | } |
| 466 | |
| 467 | $ret_link = str_replace('%link_dir%',$link_dir,$ret_link); |
| 468 | }else { |
| 469 | $ret_link = $link; |
| 470 | } |
| 471 | $this->get_archives_where_r['post_type'] = $t; // [steve] reverting post_type to previous value |
| 472 | |
| 473 | return $ret_link; |
| 474 | } |
| 475 | |
| 476 | |
| 477 | |
| 478 | /** |
| 479 | * |
| 480 | * Add rewrite rules for custom taxonomies. |
| 481 | * @since 0.6 |
| 482 | * @version 2.1 |
| 483 | * |
| 484 | */ |
| 485 | public function add_tax_rewrite_rules() { |
| 486 | if(get_option('no_taxonomy_structure')) { |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | global $wp_rewrite; |
| 492 | $taxonomies = get_taxonomies(array( '_builtin' => false)); |
| 493 | $taxonomies['category'] = 'category'; |
| 494 | |
| 495 | if(empty($taxonomies)) { |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | foreach ($taxonomies as $taxonomy) : |
| 500 | $taxonomyObject = get_taxonomy($taxonomy); |
| 501 | $post_types = $taxonomyObject->object_type; |
| 502 | |
| 503 | foreach ($post_types as $post_type): |
| 504 | $post_type_obj = get_post_type_object($post_type); |
| 505 | $slug = $post_type_obj->rewrite['slug']; |
| 506 | if(!$slug) { |
| 507 | $slug = $post_type; |
| 508 | } |
| 509 | |
| 510 | if(is_string($post_type_obj->has_archive)) { |
| 511 | $slug = $post_type_obj->has_archive; |
| 512 | }; |
| 513 | |
| 514 | if ( $taxonomy == 'category' ){ |
| 515 | $taxonomypat = ($cb = get_option('category_base')) ? $cb : $taxonomy; |
| 516 | $tax = 'category_name'; |
| 517 | } else { |
| 518 | // Edit by [Xiphe] |
| 519 | if (isset($taxonomyObject->rewrite['slug'])) { |
| 520 | $taxonomypat = $taxonomyObject->rewrite['slug']; |
| 521 | } else { |
| 522 | $taxonomypat = $taxonomy; |
| 523 | } |
| 524 | // [Xiphe] stop |
| 525 | |
| 526 | $tax = $taxonomy; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | //add taxonomy slug |
| 531 | add_rewrite_rule( $slug.'/'.$taxonomypat.'/(.+?)/page/?([0-9]{1,})/?$', 'index.php?'.$tax.'=$matches[1]&paged=$matches[2]', 'top' ); |
| 532 | add_rewrite_rule( $slug.'/'.$taxonomypat.'/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$tax.'=$matches[1]&feed=$matches[2]', 'top' ); |
| 533 | add_rewrite_rule( $slug.'/'.$taxonomypat.'/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$tax.'=$matches[1]&feed=$matches[2]', 'top' ); |
| 534 | add_rewrite_rule( $slug.'/'.$taxonomypat.'/(.+?)/?$', 'index.php?'.$tax.'=$matches[1]', 'top' ); // modified by [steve] [*** bug fixing] |
| 535 | |
| 536 | // below rules were added by [steve] |
| 537 | add_rewrite_rule( $taxonomypat.'/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?'.$tax.'=$matches[1]&year=$matches[2]&monthnum=$matches[3]&post_type='.$post_type, 'top' ); |
| 538 | add_rewrite_rule( $taxonomypat.'/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?'.$tax.'=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]&post_type='.$post_type, 'top' ); |
| 539 | add_rewrite_rule( $slug.'/'.$taxonomypat.'/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?'.$tax.'=$matches[1]&year=$matches[2]&monthnum=$matches[3]&post_type='.$post_type, 'top' ); |
| 540 | add_rewrite_rule( $slug.'/'.$taxonomypat.'/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?'.$tax.'=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]&post_type='.$post_type, 'top' ); |
| 541 | |
| 542 | endforeach; |
| 543 | endforeach; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | /** |
| 550 | * |
| 551 | * Fix taxonomy link outputs. |
| 552 | * @since 0.6 |
| 553 | * @version 1.0 |
| 554 | * |
| 555 | */ |
| 556 | public function term_link( $termlink, $term, $taxonomy ) { |
| 557 | if( get_option('no_taxonomy_structure') ) { |
| 558 | return $termlink; |
| 559 | } |
| 560 | |
| 561 | $taxonomy = get_taxonomy($taxonomy); |
| 562 | if( $taxonomy->_builtin ) |
| 563 | return $termlink; |
| 564 | |
| 565 | if( empty($taxonomy) ) |
| 566 | return $termlink; |
| 567 | |
| 568 | $wp_home = rtrim( home_url(), '/' ); |
| 569 | |
| 570 | $post_type = $taxonomy->object_type[0]; |
| 571 | $slug = get_post_type_object($post_type)->rewrite['slug']; |
| 572 | $with_front = get_post_type_object($post_type)->rewrite['with_front']; |
| 573 | |
| 574 | //$termlink = str_replace( $term->slug.'/', $this->get_taxonomy_parents( $term->term_id,$taxonomy->name, false, '/', true ), $termlink ); |
| 575 | $str = rtrim( preg_replace( "/%[a-z_]*%/", "" ,get_option("permalink_structure")) ,'/' );//remove with front |
| 576 | $termlink = str_replace($str."/", "/", $termlink ); |
| 577 | |
| 578 | if( $with_front === false ) { |
| 579 | $str = ""; |
| 580 | } |
| 581 | $slug = $str."/".$slug; |
| 582 | |
| 583 | $termlink = str_replace( $wp_home, $wp_home.$slug, $termlink ); |
| 584 | $termlink = str_replace( $term->slug.'/', $this->get_taxonomy_parents( $term->term_id,$taxonomy->name, false, '/', true ), $termlink ); |
| 585 | return $termlink; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * |
| 590 | * Fix taxonomy = parent/child => taxonomy => child |
| 591 | * @since 0.9.3 |
| 592 | * |
| 593 | */ |
| 594 | public function parse_request($obj) { |
| 595 | $taxes = get_taxonomies(array( '_builtin' => false)); |
| 596 | foreach ($taxes as $key => $tax) { |
| 597 | if(isset($obj->query_vars[$tax])) { |
| 598 | if(strpos( $obj->query_vars[$tax] ,"/") !== false ) { |
| 599 | $obj->query_vars[$tax] = array_pop(explode("/", $obj->query_vars[$tax])); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | |
| 606 | |
| 607 | /** |
| 608 | * |
| 609 | * load textdomain |
| 610 | * @since 0.6.2 |
| 611 | * |
| 612 | */ |
| 613 | public function load_textdomain() { |
| 614 | load_plugin_textdomain('cptp',false,'custom-post-type-permalinks/language'); |
| 615 | } |
| 616 | |
| 617 | |
| 618 | |
| 619 | /** |
| 620 | * |
| 621 | * Add hook flush_rules |
| 622 | * @since 0.7.9 |
| 623 | * |
| 624 | */ |
| 625 | public function update_rules() { |
| 626 | |
| 627 | $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) ); |
| 628 | $type_count = count($post_types); |
| 629 | $i = 0; |
| 630 | foreach ($post_types as $post_type): |
| 631 | add_action('update_option_'.$post_type.'_structure',array(&$this,'queue_flush_rules'),10,2); |
| 632 | endforeach; |
| 633 | add_action('update_option_no_taxonomy_structure',array(&$this,'queue_flush_rules'),10,2); |
| 634 | } |
| 635 | |
| 636 | |
| 637 | |
| 638 | /** |
| 639 | * Flush rules |
| 640 | * |
| 641 | * @since 0.7.9 |
| 642 | * |
| 643 | */ |
| 644 | |
| 645 | public function queue_flush_rules(){ |
| 646 | update_option( "queue_flush_rules", 1 ); |
| 647 | } |
| 648 | |
| 649 | |
| 650 | |
| 651 | /** |
| 652 | * |
| 653 | * Setting Init |
| 654 | * @since 0.7 |
| 655 | * |
| 656 | */ |
| 657 | public function settings_api_init() { |
| 658 | add_settings_section('cptp_setting_section', |
| 659 | __("Permalink Setting for custom post type",'cptp'), |
| 660 | array(&$this,'setting_section_callback_function'), |
| 661 | 'permalink' |
| 662 | ); |
| 663 | |
| 664 | $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) ); |
| 665 | foreach ($post_types as $post_type): |
| 666 | if(isset($_POST['submit']) and isset($_POST['_wp_http_referer'])){ |
| 667 | if( strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) { |
| 668 | |
| 669 | $structure = trim(esc_attr($_POST[$post_type.'_structure']));#get setting |
| 670 | |
| 671 | #default permalink structure |
| 672 | if( !$structure ) |
| 673 | $structure = $this->default_structure; |
| 674 | |
| 675 | $structure = str_replace('//','/','/'.$structure);# first "/" |
| 676 | |
| 677 | #last "/" |
| 678 | $lastString = substr(trim(esc_attr($_POST['permalink_structure'])),-1); |
| 679 | $structure = rtrim($structure,'/'); |
| 680 | |
| 681 | if ( $lastString == '/') |
| 682 | $structure = $structure.'/'; |
| 683 | |
| 684 | update_option($post_type.'_structure', $structure ); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | add_settings_field($post_type.'_structure', |
| 689 | $post_type, |
| 690 | array(&$this,'setting_structure_callback_function'), |
| 691 | 'permalink', |
| 692 | 'cptp_setting_section', |
| 693 | $post_type.'_structure' |
| 694 | ); |
| 695 | |
| 696 | register_setting('permalink',$post_type.'_structure'); |
| 697 | endforeach; |
| 698 | |
| 699 | add_settings_field( |
| 700 | 'no_taxonomy_structure', |
| 701 | __("Use custom permalink of custom taxonomy archive.",'cptp'), |
| 702 | array(&$this,'setting_no_tax_structure_callback_function'), |
| 703 | 'permalink', |
| 704 | 'cptp_setting_section' |
| 705 | ); |
| 706 | |
| 707 | register_setting('permalink','no_taxonomy_structure'); |
| 708 | |
| 709 | if(isset($_POST['submit']) && isset($_POST['_wp_http_referer']) && strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) { |
| 710 | |
| 711 | if(!isset($_POST['no_taxonomy_structure'])){ |
| 712 | $set = true; |
| 713 | }else { |
| 714 | $set = false; |
| 715 | } |
| 716 | update_option('no_taxonomy_structure', $set); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | public function setting_section_callback_function() { |
| 721 | ?> |
| 722 | <p><?php _e("Setting permalinks of custom post type.",'cptp');?><br /> |
| 723 | <?php _e("The tags you can use is WordPress Structure Tags and '%\"custom_taxonomy_slug\"%'. (e.g. %actors%)",'cptp');?><br /> |
| 724 | <?php _e("%\"custom_taxonomy_slug\"% is replaced the taxonomy's term.'.",'cptp');?></p> |
| 725 | |
| 726 | <p><?php _e("Presence of the trailing '/' is unified into a standard permalink structure setting.",'cptp');?> |
| 727 | <?php _e("If you don't entered permalink structure, permalink is configured /%postname%/'.",'cptp');?> |
| 728 | </p> |
| 729 | <?php |
| 730 | } |
| 731 | |
| 732 | public function setting_structure_callback_function( $option ) { |
| 733 | $post_type = str_replace('_structure',"" ,$option); |
| 734 | $slug = get_post_type_object($post_type)->rewrite['slug']; |
| 735 | $with_front = get_post_type_object($post_type)->rewrite['with_front']; |
| 736 | |
| 737 | $value = get_option($option); |
| 738 | if( !$value ) |
| 739 | $value = $this->default_structure; |
| 740 | |
| 741 | global $wp_rewrite; |
| 742 | $front = substr( $wp_rewrite->front, 1 ); |
| 743 | if( $front and $with_front ) { |
| 744 | $slug = $front.$slug; |
| 745 | } |
| 746 | |
| 747 | echo '<code>'.home_url().'/'.$slug.'</code> <input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . $value .'" />'; |
| 748 | } |
| 749 | |
| 750 | public function setting_no_tax_structure_callback_function(){ |
| 751 | echo '<input name="no_taxonomy_structure" id="no_taxonomy_structure" type="checkbox" value="1" class="code" ' . checked( false, get_option('no_taxonomy_structure'),false) . ' /> '; |
| 752 | $txt = __("If you check,The custom taxonomy's permalinks is <code>%s/post_type/taxonomy/term</code>.","cptp"); |
| 753 | printf($txt , home_url()); |
| 754 | } |
| 755 | |
| 756 | |
| 757 | |
| 758 | /** |
| 759 | * |
| 760 | * enqueue CSS and JS |
| 761 | * @since 0.8.5 |
| 762 | * |
| 763 | */ |
| 764 | public function enqueue_css_js() { |
| 765 | wp_enqueue_style('wp-pointer'); |
| 766 | wp_enqueue_script('wp-pointer'); |
| 767 | } |
| 768 | |
| 769 | |
| 770 | |
| 771 | /** |
| 772 | * |
| 773 | * add js for pointer |
| 774 | * @since 0.8.5 |
| 775 | */ |
| 776 | public function pointer_js() { |
| 777 | if(!is_network_admin()) { |
| 778 | $dismissed = explode(',', get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true )); |
| 779 | if(array_search('cptp_pointer0871', $dismissed) === false){ |
| 780 | $content = __("<h3>Custom Post Type Permalinks</h3><p>From <a href='options-permalink.php'>Permalinks</a>, set a custom permalink for each post type.</p>", "cptp"); |
| 781 | ?> |
| 782 | <script type="text/javascript"> |
| 783 | jQuery(function($) { |
| 784 | |
| 785 | $("#menu-settings .wp-has-submenu").pointer({ |
| 786 | content: "<?php echo $content;?>", |
| 787 | position: {"edge":"left","align":"center"}, |
| 788 | close: function() { |
| 789 | $.post('admin-ajax.php', { |
| 790 | action:'dismiss-wp-pointer', |
| 791 | pointer: 'cptp_pointer0871' |
| 792 | }) |
| 793 | |
| 794 | } |
| 795 | }).pointer("open"); |
| 796 | }); |
| 797 | </script> |
| 798 | <?php |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | $custom_post_type_permalinks = new Custom_Post_Type_Permalinks; |
| 805 | $custom_post_type_permalinks = apply_filters('custom_post_type_permalinks', $custom_post_type_permalinks); |
| 806 | $custom_post_type_permalinks->add_hook(); |
| 807 | ?> |
| 808 |