custom-post-type-permalinks
Last commit date
cptp-ja.mo
13 years ago
cptp-ja.po
13 years ago
custom-post-type-permalinks.php
13 years ago
readme.txt
13 years ago
screenshot-1.png
14 years ago
custom-post-type-permalinks.php
661 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.8.6 |
| 9 | Text Domain: cptp |
| 10 | Domain Path: / |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | /* Copyright 2012 Toro_Unit (email : mail@torounit.com) |
| 15 | |
| 16 | This program is free software; you can redistribute it and/or modify |
| 17 | it under the terms of the GNU General Public License as published by |
| 18 | the Free Software Foundation; either version 2 of the License, or |
| 19 | (at your option) any later version. |
| 20 | |
| 21 | This program is distributed in the hope that it will be useful, |
| 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | GNU General Public License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU General Public License |
| 27 | along with this program; if not, write to the Free Software |
| 28 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 29 | */ |
| 30 | |
| 31 | /* This plugin don't support Multisite yet.*/ |
| 32 | |
| 33 | /** |
| 34 | * |
| 35 | * Custom Permalink Class |
| 36 | * |
| 37 | * |
| 38 | */ |
| 39 | class Custom_Post_Type_Permalinks { |
| 40 | |
| 41 | static public $default_structure = '/%postname%/'; |
| 42 | |
| 43 | /** |
| 44 | * |
| 45 | * Constructor |
| 46 | * Add Action & filter hooks. |
| 47 | * @version 2.0 |
| 48 | */ |
| 49 | |
| 50 | public function add_hook () { |
| 51 | add_action('wp_loaded',array(&$this,'set_archive_rewrite'),99); |
| 52 | add_action('wp_loaded', array(&$this,'set_rewrite'),100); |
| 53 | add_action('wp_loaded', array(&$this,'add_tax_rewrite')); |
| 54 | |
| 55 | if(get_option("permalink_structure") != "") { |
| 56 | add_filter('post_type_link', array(&$this,'set_permalink'),10,3); |
| 57 | add_filter('getarchives_join', array(&$this,'get_archives_join'),10,2); // [steve] |
| 58 | add_filter('getarchives_where', array(&$this,'get_archives_where'), 10, 2); |
| 59 | add_filter('get_archives_link', array(&$this,'get_archives_link'),20,1); |
| 60 | add_filter('term_link', array(&$this,'set_term_link'),10,3); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * |
| 66 | * Get Custom Taxonomies parents. |
| 67 | * @version 1.0 |
| 68 | * |
| 69 | */ |
| 70 | private function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) { |
| 71 | $chain = ''; |
| 72 | $parent = &get_term( $id, $taxonomy, OBJECT, 'raw'); |
| 73 | if ( is_wp_error( $parent ) ) { |
| 74 | return $parent; |
| 75 | } |
| 76 | |
| 77 | if ( $nicename ){ |
| 78 | $name = $parent->slug; |
| 79 | }else { |
| 80 | $name = $parent->name; |
| 81 | } |
| 82 | |
| 83 | if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { |
| 84 | $visited[] = $parent->parent; |
| 85 | $chain .= $this->get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited ); |
| 86 | } |
| 87 | |
| 88 | if ( $link ) { |
| 89 | $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator; |
| 90 | }else { |
| 91 | $chain .= $name.$separator; |
| 92 | } |
| 93 | return $chain; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * |
| 98 | * Add rewrite rules for archives. |
| 99 | * @version 1.0 |
| 100 | * |
| 101 | */ |
| 102 | public function set_archive_rewrite() { |
| 103 | $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) ); |
| 104 | |
| 105 | foreach ( $post_types as $post_type ): |
| 106 | if( !$post_type ) |
| 107 | continue; |
| 108 | $permalink = get_option( $post_type.'_structure' ); |
| 109 | $post_type_obj = get_post_type_object($post_type); |
| 110 | $slug = $post_type_obj->rewrite['slug']; |
| 111 | if( !$slug ) |
| 112 | $slug = $post_type; |
| 113 | |
| 114 | if( is_string( $post_type_obj->has_archive ) ){ |
| 115 | $slug = $post_type_obj->has_archive; |
| 116 | }; |
| 117 | |
| 118 | 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' ); |
| 119 | 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' ); |
| 120 | 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' ); |
| 121 | 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' ); |
| 122 | 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' ); |
| 123 | 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' ); |
| 124 | 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' ); |
| 125 | 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' ); |
| 126 | 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' ); |
| 127 | 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' ); |
| 128 | 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' ); |
| 129 | add_rewrite_rule( $slug.'/date/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type='.$post_type, 'top' ); |
| 130 | add_rewrite_rule( $slug.'/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type='.$post_type, 'top' ); |
| 131 | add_rewrite_rule( $slug.'/page/?([0-9]{1,})/?$', 'index.php?paged=$matches[1]&post_type='.$post_type, 'top' ); |
| 132 | add_rewrite_rule( $slug.'/?$', 'index.php?post_type='.$post_type, 'top' ); |
| 133 | |
| 134 | |
| 135 | endforeach; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /** |
| 140 | * |
| 141 | * Add Rewrite rule for single posts. |
| 142 | * @version 2.0 |
| 143 | * |
| 144 | * |
| 145 | */ |
| 146 | public function set_rewrite() { |
| 147 | global $wp_rewrite; |
| 148 | |
| 149 | $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) ); |
| 150 | foreach ( $post_types as $post_type ): |
| 151 | $permalink = get_option( $post_type.'_structure' ); |
| 152 | |
| 153 | if( !$permalink ) |
| 154 | $permalink = self::$default_structure; |
| 155 | |
| 156 | $permalink = str_replace( '%postname%', '%'.$post_type.'%', $permalink ); |
| 157 | $permalink = str_replace( '%post_id%', '%'.$post_type.'_id%', $permalink ); |
| 158 | |
| 159 | $slug = get_post_type_object($post_type)->rewrite['slug']; |
| 160 | |
| 161 | if( !$slug ) |
| 162 | $slug = $post_type; |
| 163 | |
| 164 | $permalink = '/'.$slug.'/'.$permalink; |
| 165 | |
| 166 | if( strpos( $permalink , '%'.$post_type.'%' ) === false ) { |
| 167 | $permalink = $permalink.'/%'.$post_type.'_page%'; |
| 168 | } |
| 169 | |
| 170 | $permalink = str_replace( '//', '/', $permalink ); |
| 171 | |
| 172 | $wp_rewrite->add_rewrite_tag( '%post_type%', '([^/]+)', 'post_type=' ); |
| 173 | $wp_rewrite->add_rewrite_tag( '%'.$post_type.'_id%', '([0-9]{1,})','post_type='.$post_type.'&p=' ); |
| 174 | $wp_rewrite->add_rewrite_tag( '%'.$post_type.'_page%', '([0-9]{1,}?)',"page=" ); |
| 175 | |
| 176 | $param = array( |
| 177 | 'with_front' => false, |
| 178 | 'ep_mask' => EP_ALL, |
| 179 | 'paged' => true, |
| 180 | 'feed' => true, |
| 181 | 'forcomments' => true, |
| 182 | 'walk_dirs' => true, |
| 183 | 'endpoints' => true, |
| 184 | ); |
| 185 | |
| 186 | //$wp_rewrite->generate_rewrite_rules( $permalink, EP_NONE, true, true, true,true); |
| 187 | $wp_rewrite->add_permastruct( $post_type, $permalink, $param ); |
| 188 | endforeach; |
| 189 | |
| 190 | $taxonomies = get_taxonomies( array("show_ui" => true, "_builtin" => false), 'objects' ); |
| 191 | foreach ( $taxonomies as $taxonomy => $objects ): |
| 192 | $wp_rewrite->add_rewrite_tag( "%$taxonomy%", '(.+?)', "$taxonomy=" ); |
| 193 | endforeach; |
| 194 | |
| 195 | $wp_rewrite->use_verbose_page_rules = true; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * |
| 200 | * Fix permalinks output. |
| 201 | * |
| 202 | * @param String $post_link |
| 203 | * @param Object $post 投稿� |
| 204 | 報 |
| 205 | * @param String $leavename 記事編集画面でのみ渡される |
| 206 | * |
| 207 | * @version 1.2 |
| 208 | * |
| 209 | */ |
| 210 | public function set_permalink( $post_link, $post, $leavename ) { |
| 211 | global $wp_rewrite; |
| 212 | $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); |
| 213 | if( $draft_or_pending and !$leavename ) |
| 214 | return $post_link; |
| 215 | |
| 216 | $post_type = $post->post_type; |
| 217 | $permalink = $wp_rewrite->get_extra_permastruct( $post_type ); |
| 218 | |
| 219 | $permalink = str_replace( '%post_type%', get_post_type_object($post->post_type)->rewrite['slug'], $permalink ); |
| 220 | $permalink = str_replace( '%'.$post_type.'_id%', $post->ID, $permalink ); |
| 221 | $permalink = str_replace( '%'.$post_type.'_page%', "", $permalink ); |
| 222 | $permalink = str_replace( '%'.$post_type.'_cpage%', "", $permalink ); |
| 223 | |
| 224 | $parentsDirs = ""; |
| 225 | if( !$leavename ){ |
| 226 | $postId = $post->ID; |
| 227 | while ($parent = get_post($postId)->post_parent) { |
| 228 | $parentsDirs = get_post($parent)->post_name."/".$parentsDirs; |
| 229 | $postId = $parent; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | $permalink = str_replace( '%'.$post_type.'%', $parentsDirs.'%'.$post_type.'%', $permalink ); |
| 234 | |
| 235 | if( !$leavename ){ |
| 236 | $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink ); |
| 237 | } |
| 238 | |
| 239 | $taxonomies = get_taxonomies( array('show_ui' => true),'objects' ); |
| 240 | |
| 241 | foreach ( $taxonomies as $taxonomy => $objects ) { |
| 242 | if ( strpos($permalink, "%$taxonomy%") !== false ) { |
| 243 | $terms = get_the_terms( $post->ID, $taxonomy ); |
| 244 | |
| 245 | if ( $terms ) { |
| 246 | usort($terms, '_usort_terms_by_ID'); // order by ID |
| 247 | $term = $terms[0]->slug; |
| 248 | |
| 249 | if ( $parent = $terms[0]->parent ) |
| 250 | $term = $this->get_taxonomy_parents( $parent,$taxonomy, false, '/', true ) . $term; |
| 251 | } |
| 252 | |
| 253 | if( isset($term) ) { |
| 254 | $permalink = str_replace( "%$taxonomy%", $term, $permalink ); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | $user = get_userdata( $post->post_author ); |
| 260 | $permalink = str_replace( "%author%", $user->user_nicename, $permalink ); |
| 261 | |
| 262 | $post_date = strtotime( $post->post_date ); |
| 263 | $permalink = str_replace( "%year%", date("Y",$post_date), $permalink ); |
| 264 | $permalink = str_replace( "%monthnum%", date("m",$post_date), $permalink ); |
| 265 | $permalink = str_replace( "%day%", date("d",$post_date), $permalink ); |
| 266 | $permalink = str_replace( "%hour%", date("H",$post_date), $permalink ); |
| 267 | $permalink = str_replace( "%minute%", date("i",$post_date), $permalink ); |
| 268 | $permalink = str_replace( "%second%", date("s",$post_date), $permalink ); |
| 269 | |
| 270 | $permalink = str_replace('//', "/", $permalink ); |
| 271 | |
| 272 | return $permalink = home_url( user_trailingslashit( $permalink ) ); |
| 273 | //$str = rtrim( preg_replace("/%[a-z,_]*%/","",get_option("permalink_structure")) ,'/'); |
| 274 | //return $permalink = str_replace($str, "", $permalink ); |
| 275 | |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * |
| 280 | * wp_get_archives fix for custom post |
| 281 | * Ex:wp_get_archives('&post_type='.get_query_var( 'post_type' )); |
| 282 | * @version 2.0 |
| 283 | * |
| 284 | */ |
| 285 | |
| 286 | public $get_archives_where_r; |
| 287 | |
| 288 | // function modified by [steve] |
| 289 | public function get_archives_where( $where, $r ) { |
| 290 | $this->get_archives_where_r = $r; |
| 291 | if ( isset($r['post_type']) ) |
| 292 | $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where ); |
| 293 | |
| 294 | if(isset($r['taxonomy']) && is_array($r['taxonomy']) ){ |
| 295 | global $wpdb; |
| 296 | $where = $where . " AND $wpdb->term_taxonomy.taxonomy = '".$r['taxonomy']['name']."' AND $wpdb->term_taxonomy.term_id = '".$r['taxonomy']['termid']."'"; |
| 297 | } |
| 298 | |
| 299 | return $where; |
| 300 | } |
| 301 | |
| 302 | //function added by [steve] |
| 303 | /** |
| 304 | * |
| 305 | * get_archive_join |
| 306 | * @author Steve |
| 307 | * @since 0.8 |
| 308 | * @version 1.0 |
| 309 | * |
| 310 | * |
| 311 | */ |
| 312 | public function get_archives_join( $join, $r ) { |
| 313 | global $wpdb; |
| 314 | $this->get_archives_where_r = $r; |
| 315 | if(isset($r['taxonomy']) && is_array($r['taxonomy']) ) |
| 316 | $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)"; |
| 317 | |
| 318 | return $join; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * |
| 323 | * get_arcihves_link |
| 324 | * @version 2.0 |
| 325 | * |
| 326 | */ |
| 327 | public function get_archives_link( $link ) { |
| 328 | $c = isset($this->get_archives_where_r['taxonomy']) && is_array($this->get_archives_where_r['taxonomy']) ? $this->get_archives_where_r['taxonomy'] : ""; //[steve] |
| 329 | $t = $this->get_archives_where_r['post_type']; // [steve] [*** bug fixing] |
| 330 | $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] |
| 331 | |
| 332 | if (isset($this->get_archives_where_r['post_type']) and $this->get_archives_where_r['type'] != 'postbypost'){ |
| 333 | $blog_url = rtrim( get_bloginfo("url") ,'/'); |
| 334 | |
| 335 | //remove .ext |
| 336 | $str = preg_replace("/\.[a-z,_]*/","",get_option("permalink_structure")); |
| 337 | |
| 338 | if($str = rtrim( preg_replace("/%[a-z,_]*%/","",$str) ,'/')) { // /archive/%post_id% |
| 339 | $ret_link = str_replace($str, '/'.'%link_dir%', $link); |
| 340 | }else{ |
| 341 | $blog_url = preg_replace('/https?:\/\//', '', $blog_url); |
| 342 | $ret_link = str_replace($blog_url,$blog_url.'/'.'%link_dir%',$link); |
| 343 | } |
| 344 | |
| 345 | if(empty($c) ){ // [steve] |
| 346 | $link_dir = $this->get_archives_where_r['post_type']; |
| 347 | } |
| 348 | else{ // [steve] |
| 349 | $c['name'] = ($c['name'] == 'category' && get_option('category_base')) ? get_option('category_base') : $c['name']; |
| 350 | $link_dir = $this->get_archives_where_r['post_type']."/".$c['name']."/".$c['termslug']; |
| 351 | } |
| 352 | |
| 353 | if(!strstr($link,'/date/')){ |
| 354 | $link_dir = $link_dir .'/date'; |
| 355 | } |
| 356 | |
| 357 | $ret_link = str_replace('%link_dir%',$link_dir,$ret_link); |
| 358 | $this->get_archives_where_r['post_type'] = $t; // [steve] reverting post_type to previous value |
| 359 | return $ret_link; |
| 360 | } |
| 361 | $this->get_archives_where_r['post_type'] = $t; // [steve] reverting post_type to previous value |
| 362 | |
| 363 | return $link; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * |
| 368 | * Add rewrite rules for custom taxonomies. |
| 369 | * @since 0.6 |
| 370 | * @version 2.0 |
| 371 | * |
| 372 | */ |
| 373 | public function add_tax_rewrite() { |
| 374 | if(get_option('no_taxonomy_structure')) |
| 375 | return false; |
| 376 | |
| 377 | global $wp_rewrite; |
| 378 | $taxonomies = get_taxonomies(array( '_builtin' => false)); |
| 379 | $taxonomies['category'] = 'category'; |
| 380 | if(empty($taxonomies)) |
| 381 | return false; |
| 382 | |
| 383 | foreach ($taxonomies as $taxonomy) : |
| 384 | $post_types = get_taxonomy($taxonomy)->object_type; |
| 385 | |
| 386 | foreach ($post_types as $post_type): |
| 387 | $post_type_obj = get_post_type_object($post_type); |
| 388 | $slug = $post_type_obj->rewrite['slug']; |
| 389 | if(!$slug) { |
| 390 | $slug = $post_type; |
| 391 | } |
| 392 | |
| 393 | if(is_string($post_type_obj->has_archive)){ |
| 394 | $slug = $post_type_obj->has_archive; |
| 395 | }; |
| 396 | |
| 397 | if($taxonomy == 'category'){ |
| 398 | $taxonomypat = ($cb = get_option('category_base')) ? $cb : $taxonomy; |
| 399 | $tax = 'category_name'; |
| 400 | }else{ |
| 401 | $taxonomypat = $taxonomy; |
| 402 | $tax = $taxonomy; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | //add taxonomy slug |
| 407 | add_rewrite_rule( $slug.'/'.$taxonomypat.'/(.+?)/page/?([0-9]{1,})/?$', 'index.php?'.$tax.'=$matches[1]&paged=$matches[2]', 'top' ); |
| 408 | add_rewrite_rule( $slug.'/'.$taxonomypat.'/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$tax.'=$matches[1]&feed=$matches[2]', 'top' ); |
| 409 | add_rewrite_rule( $slug.'/'.$taxonomypat.'/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$tax.'=$matches[1]&feed=$matches[2]', 'top' ); |
| 410 | add_rewrite_rule( $slug.'/'.$taxonomypat.'/([^/]+)/?$', 'index.php?'.$tax.'=$matches[1]', 'top' ); // modified by [steve] [*** bug fixing] |
| 411 | |
| 412 | // below rules were added by [steve] |
| 413 | 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' ); |
| 414 | 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' ); |
| 415 | 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' ); |
| 416 | 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' ); |
| 417 | |
| 418 | endforeach; |
| 419 | endforeach; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | /** |
| 424 | * |
| 425 | * Fix taxonomy link outputs. |
| 426 | * @since 0.6 |
| 427 | * @version 1.0 |
| 428 | * |
| 429 | */ |
| 430 | public function set_term_link( $termlink, $term, $taxonomy ) { |
| 431 | if( get_option('no_taxonomy_structure') ) |
| 432 | return $termlink; |
| 433 | |
| 434 | $taxonomy = get_taxonomy($taxonomy); |
| 435 | if( $taxonomy->_builtin ) |
| 436 | return $termlink; |
| 437 | |
| 438 | if( empty($taxonomy) ) |
| 439 | return $termlink; |
| 440 | |
| 441 | $wp_home = rtrim( get_option('home'), '/' ); |
| 442 | |
| 443 | $post_type = $taxonomy->object_type[0]; |
| 444 | $slug = get_post_type_object($post_type)->rewrite['slug']; |
| 445 | |
| 446 | |
| 447 | //$termlink = str_replace( $term->slug.'/', $this->get_taxonomy_parents( $term->term_id,$taxonomy->name, false, '/', true ), $termlink ); |
| 448 | $termlink = str_replace( $wp_home, $wp_home.'/'.$slug, $termlink ); |
| 449 | $str = rtrim( preg_replace("/%[a-z_]*%/","",get_option("permalink_structure")) ,'/'); |
| 450 | return str_replace($str, "", $termlink ); |
| 451 | |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * |
| 457 | * Admin Page Class |
| 458 | * |
| 459 | */ |
| 460 | |
| 461 | class Custom_Post_Type_Permalinks_Admin { |
| 462 | |
| 463 | public function add_hook () { |
| 464 | add_action('init', array(&$this,'load_textdomain')); |
| 465 | add_action('init', array(&$this, 'update_rules')); |
| 466 | add_action('admin_init', array(&$this,'settings_api_init'),30); |
| 467 | add_action('admin_enqueue_scripts', array(&$this,'enqueue_css_js')); |
| 468 | add_action('admin_footer',array(&$this,'pointer_js')); |
| 469 | } |
| 470 | |
| 471 | public function load_textdomain() { |
| 472 | load_plugin_textdomain('cptp',false,'custom-post-type-permalinks'); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * |
| 477 | * Add hook flush_rules |
| 478 | * @since 0.7.9 |
| 479 | * |
| 480 | */ |
| 481 | public function update_rules() { |
| 482 | |
| 483 | $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) ); |
| 484 | $type_count = count($post_types); |
| 485 | $i = 0; |
| 486 | foreach ($post_types as $post_type): |
| 487 | add_action('update_option_'.$post_type.'_structure',array(&$this,'flush_rules'),10,2); |
| 488 | endforeach; |
| 489 | add_action('update_option_no_taxonomy_structure',array(&$this,'flush_rules'),10,2); |
| 490 | |
| 491 | |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Flush rules |
| 496 | * |
| 497 | * @since 0.7.9 |
| 498 | * |
| 499 | */ |
| 500 | |
| 501 | public function flush_rules(){ |
| 502 | Custom_Post_Type_Permalinks::add_tax_rewrite(); |
| 503 | Custom_Post_Type_Permalinks::set_archive_rewrite(); |
| 504 | Custom_Post_Type_Permalinks::set_rewrite(); |
| 505 | flush_rewrite_rules(); |
| 506 | } |
| 507 | |
| 508 | |
| 509 | /** |
| 510 | * |
| 511 | * Setting Init |
| 512 | * @since 0.7 |
| 513 | * |
| 514 | */ |
| 515 | public function settings_api_init() { |
| 516 | add_settings_section('cptp_setting_section', |
| 517 | __("Permalink Setting for custom post type",'cptp'), |
| 518 | array(&$this,'setting_section_callback_function'), |
| 519 | 'permalink' |
| 520 | ); |
| 521 | |
| 522 | $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) ); |
| 523 | foreach ($post_types as $post_type): |
| 524 | if(isset($_POST['submit'])){ |
| 525 | if( strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) { |
| 526 | |
| 527 | $structure = trim(esc_attr($_POST[$post_type.'_structure']));#get setting |
| 528 | |
| 529 | #default permalink structure |
| 530 | if( !$structure ) |
| 531 | $structure = Custom_Post_Type_Permalinks::$default_structure; |
| 532 | |
| 533 | $structure = str_replace('//','/','/'.$structure);# first "/" |
| 534 | |
| 535 | #last "/" |
| 536 | $lastString = substr(trim(esc_attr($_POST['permalink_structure'])),-1); |
| 537 | $structure = rtrim($structure,'/'); |
| 538 | |
| 539 | if ( $lastString == '/') |
| 540 | $structure = $structure.'/'; |
| 541 | |
| 542 | update_option($post_type.'_structure', $structure ); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | add_settings_field($post_type.'_structure', |
| 547 | $post_type, |
| 548 | array(&$this,'setting_structure_callback_function'), |
| 549 | 'permalink', |
| 550 | 'cptp_setting_section', |
| 551 | $post_type.'_structure' |
| 552 | ); |
| 553 | |
| 554 | register_setting('permalink',$post_type.'_structure'); |
| 555 | endforeach; |
| 556 | |
| 557 | add_settings_field( |
| 558 | 'no_taxonomy_structure', |
| 559 | __("Use custom permalink of custom taxonomy archive.",'cptp'), |
| 560 | array(&$this,'setting_no_tax_structure_callback_function'), |
| 561 | 'permalink', |
| 562 | 'cptp_setting_section' |
| 563 | ); |
| 564 | |
| 565 | register_setting('permalink','no_taxonomy_structure'); |
| 566 | |
| 567 | if(isset($_POST['submit']) && isset($_POST['_wp_http_referer']) && strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) { |
| 568 | |
| 569 | if(!isset($_POST['no_taxonomy_structure'])){ |
| 570 | $set = true; |
| 571 | }else { |
| 572 | $set = false; |
| 573 | } |
| 574 | update_option('no_taxonomy_structure', $set); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | public function setting_section_callback_function() { |
| 579 | ?> |
| 580 | <p><?php _e("Setting permalinks of custom post type.",'cptp');?><br /> |
| 581 | <?php _e("The tags you can use is WordPress Structure Tags and '%\"custom_taxonomy_slug\"%'. (e.g. %actors%)",'cptp');?><br /> |
| 582 | <?php _e("%\"custom_taxonomy_slug\"% is replaced the taxonomy's term.'.",'cptp');?></p> |
| 583 | |
| 584 | <p><?php _e("Presence of the trailing '/' is unified into a standard permalink structure setting.",'cptp');?> |
| 585 | <?php _e("If you don't entered permalink structure, permalink is configured /%postname%/'.",'cptp');?> |
| 586 | </p> |
| 587 | <?php |
| 588 | } |
| 589 | |
| 590 | public function setting_structure_callback_function( $option ) { |
| 591 | $post_type = str_replace('_structure',"" ,$option); |
| 592 | $slug = get_post_type_object($post_type)->rewrite['slug']; |
| 593 | if( !$slug ) |
| 594 | $slug = $post_type; |
| 595 | |
| 596 | $value = get_option($option); |
| 597 | if( !$value ) |
| 598 | $value = Custom_Post_Type_Permalinks::$default_structure; |
| 599 | |
| 600 | echo '<code>'.home_url().'/'.$slug.'</code> <input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . $value .'" />'; |
| 601 | } |
| 602 | |
| 603 | public function setting_no_tax_structure_callback_function(){ |
| 604 | echo '<input name="no_taxonomy_structure" id="no_taxonomy_structure" type="checkbox" value="1" class="code" ' . checked( false, get_option('no_taxonomy_structure'),false) . ' /> '; |
| 605 | $txt = __("If you check,The custom taxonomy's permalinks is <code>%s/post_type/taxonomy/term</code>.","cptp"); |
| 606 | printf($txt , home_url()); |
| 607 | } |
| 608 | |
| 609 | |
| 610 | /** |
| 611 | * |
| 612 | * enqueue CSS and JS |
| 613 | * @since 0.8.5 |
| 614 | * |
| 615 | */ |
| 616 | public function enqueue_css_js() { |
| 617 | wp_enqueue_style('wp-pointer'); |
| 618 | wp_enqueue_script('wp-pointer'); |
| 619 | } |
| 620 | |
| 621 | |
| 622 | /** |
| 623 | * |
| 624 | * add js for pointer |
| 625 | * @since 0.8.5 |
| 626 | */ |
| 627 | public function pointer_js() { |
| 628 | if(!is_network_admin()) { |
| 629 | $dismissed = explode(',', get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true )); |
| 630 | if(array_search('cptp_pointer085', $dismissed) === false){ |
| 631 | ?> |
| 632 | <script type="text/javascript"> |
| 633 | jQuery(function($) { |
| 634 | |
| 635 | $("#menu-settings .wp-has-submenu").pointer({ |
| 636 | content: "<h3>Custom Post Type Permalinks</h3><p><a href='options-permalink.php'>パーマリンク設定</a>より、カスタム投稿タイプごとのパーマリンクを設定できます。</p>", |
| 637 | position: {"edge":"left","align":"center"}, |
| 638 | close: function() { |
| 639 | $.post('admin-ajax.php', { |
| 640 | action:'dismiss-wp-pointer', |
| 641 | pointer: 'cptp_pointer085' |
| 642 | }) |
| 643 | |
| 644 | } |
| 645 | }).pointer("open"); |
| 646 | }); |
| 647 | </script> |
| 648 | <?php |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | $custom_post_type_permalinks = new Custom_Post_Type_Permalinks; |
| 655 | $custom_post_type_permalinks = apply_filters('custom_post_type_permalinks', $custom_post_type_permalinks); |
| 656 | $custom_post_type_permalinks->add_hook(); |
| 657 | |
| 658 | $custom_post_type_permalinks_admin = new Custom_Post_Type_Permalinks_Admin; |
| 659 | $custom_post_type_permalinks_admin = apply_filters('custom_post_type_permalinks_admin', $custom_post_type_permalinks_admin); |
| 660 | $custom_post_type_permalinks_admin->add_hook(); |
| 661 | ?> |