Custom Post Type Permalinks - Version 0.9

Version Description

  • Add custom post type archive only has_archive->true
  • Change method name.
  • Change hook custom post link.
  • Use Slug in wp_get_archive().
  • Fix attachment link. *
Download this release

Release Info

Developer Toro_Unit
Plugin Icon 128x128 Custom Post Type Permalinks
Version 0.9
Comparing to
See all releases

Code changes from version 0.8.7.6 to 0.9

Files changed (2) hide show
  1. custom-post-type-permalinks.php +142 -125
  2. readme.txt +12 -2
custom-post-type-permalinks.php CHANGED
@@ -5,7 +5,7 @@ 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.7.5
9
  Text Domain: cptp
10
  License: GPL2 or later
11
  Domain Path: /
@@ -39,35 +39,39 @@ Domain Path: /
39
  */
40
  class Custom_Post_Type_Permalinks {
41
 
42
- public $version = "0.8.7.5";
 
 
 
43
  public $default_structure = '/%postname%/';
44
 
45
  /**
46
  *
47
  * Add Action & filter hooks.
48
- * @version 2.0
49
  */
50
  public function add_hook () {
51
 
52
- add_action( 'update_option_cptp_version', array(&$this, 'update_rules') );
53
  add_action( 'plugins_loaded', array(&$this,'check_version') );
54
-
55
- add_action( 'wp_loaded', array(&$this,'set_archive_rewrite'), 99 );
56
- add_action( 'wp_loaded', array(&$this,'set_rewrite'), 100 );
57
- add_action( 'wp_loaded', array(&$this,'add_tax_rewrite') );
58
 
59
 
60
  if(get_option( "permalink_structure") != "") {
61
- add_filter( 'post_type_link', array(&$this,'set_permalink'), 10, 3 );
62
- add_filter( 'getarchives_join', array(&$this,'get_archives_join'), 10, 2 ); // [steve]
63
- add_filter( 'getarchives_where', array(&$this,'get_archives_where'), 10 , 2 );
64
  add_filter( 'get_archives_link', array(&$this,'get_archives_link'), 20, 1 );
65
- add_filter( 'term_link', array(&$this,'set_term_link'), 10, 3 );
66
  add_filter( 'attachment_link', array(&$this, 'attachment_link'), 20 , 2);
67
  }
68
 
 
69
  add_action( 'init', array(&$this,'load_textdomain') );
70
  add_action( 'init', array(&$this, 'update_rules') );
 
71
  add_action( 'admin_init', array(&$this,'settings_api_init'), 30 );
72
  add_action( 'admin_enqueue_scripts', array(&$this,'enqueue_css_js') );
73
  add_action( 'admin_footer', array(&$this,'pointer_js') );
@@ -75,6 +79,28 @@ class Custom_Post_Type_Permalinks {
75
 
76
  }
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  public function check_version() {
80
  $version = get_option('cptp_version', 0);
@@ -120,97 +146,74 @@ class Custom_Post_Type_Permalinks {
120
  /**
121
  *
122
  * Add rewrite rules for archives.
123
- * @version 1.0
124
  *
125
  */
126
- public function set_archive_rewrite() {
127
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
128
 
129
  foreach ( $post_types as $post_type ):
130
- if( !$post_type )
131
  continue;
 
 
132
  $permalink = get_option( $post_type.'_structure' );
133
  $post_type_obj = get_post_type_object($post_type);
134
  $slug = $post_type_obj->rewrite['slug'];
135
  if( !$slug )
136
  $slug = $post_type;
137
 
138
- if( is_string( $post_type_obj->has_archive ) ){
139
- $slug = $post_type_obj->has_archive;
140
- };
 
141
 
142
- 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' );
143
- 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' );
144
- 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' );
145
- 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' );
146
- 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' );
147
- 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' );
148
- 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' );
149
- 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' );
150
- 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' );
151
- 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' );
152
- 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' );
153
- add_rewrite_rule( $slug.'/date/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type='.$post_type, 'top' );
154
- add_rewrite_rule( $slug.'/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type='.$post_type, 'top' );
155
- add_rewrite_rule( $slug.'/page/?([0-9]{1,})/?$', 'index.php?paged=$matches[1]&post_type='.$post_type, 'top' );
156
- add_rewrite_rule( $slug.'/?$', 'index.php?post_type='.$post_type, 'top' );
157
- endforeach;
158
- }
159
 
160
 
 
 
161
 
162
  /**
163
  *
164
- * Add Rewrite rule for single posts.
165
- * @version 2.0
166
- *
 
167
  *
168
  */
169
- public function set_rewrite() {
170
- global $wp_rewrite;
171
 
172
- $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
173
- foreach ( $post_types as $post_type ):
174
- $permalink = get_option( $post_type.'_structure' );
175
-
176
- if( !$permalink ) {
177
- $permalink = $this->default_structure;
178
- }
179
-
180
- $permalink = str_replace( '%postname%', '%'.$post_type.'%', $permalink );
181
- $permalink = str_replace( '%post_id%', '%'.$post_type.'_id%', $permalink );
182
-
183
- $slug = get_post_type_object($post_type)->rewrite['slug'];
184
-
185
- if( !$slug ) {
186
- $slug = $post_type;
187
- }
188
-
189
- $permalink = '/'.$slug.'/'.$permalink;
190
 
191
- if( strpos( $permalink , '%'.$post_type.'%' ) === false ) {
192
- $permalink = $permalink.'/%'.$post_type.'_page%';
193
- }
194
 
195
- $permalink = str_replace( '//', '/', $permalink );
 
 
 
196
 
197
- $wp_rewrite->add_rewrite_tag( '%post_type%', '([^/]+)', 'post_type=' );
198
- $wp_rewrite->add_rewrite_tag( '%'.$post_type.'_id%', '([0-9]{1,})','post_type='.$post_type.'&p=' );
199
- $wp_rewrite->add_rewrite_tag( '%'.$post_type.'_page%', '([0-9]{1,}?)',"page=" );
200
 
201
- $param = array(
202
- 'with_front' => false,
203
- 'ep_mask' => EP_ALL,
204
- 'paged' => true,
205
- 'feed' => true,
206
- 'forcomments' => true,
207
- 'walk_dirs' => true,
208
- 'endpoints' => true,
209
- );
210
 
211
- //$wp_rewrite->generate_rewrite_rules( $permalink, EP_NONE, true, true, true,true);
212
- $wp_rewrite->add_permastruct( $post_type, $permalink, $param );
213
- endforeach;
214
 
215
  $taxonomies = get_taxonomies( array("show_ui" => true, "_builtin" => false), 'objects' );
216
  foreach ( $taxonomies as $taxonomy => $objects ):
@@ -218,19 +221,25 @@ class Custom_Post_Type_Permalinks {
218
  endforeach;
219
 
220
  $wp_rewrite->use_verbose_page_rules = true;
 
 
 
 
221
  }
222
 
223
 
 
 
 
224
  /**
225
  *
226
  * fix attachment output
227
  *
228
- * @version 1
229
  * @since 0.8.2
230
  *
231
  */
232
 
233
-
234
  public function attachment_link( $link , $postID ) {
235
  $post = get_post( $postID );
236
  $post_parent = get_post( $post->post_parent );
@@ -238,7 +247,7 @@ class Custom_Post_Type_Permalinks {
238
  $post_type = get_post_type_object( $post_parent->post_type );
239
 
240
  if( $post_type->_builtin == false ) {
241
- if(strpos( $permalink, "%postname%" ) < strrpos( $permalink, "%post_id%" )){
242
  $link = str_replace($post->post_name , "attachment/".$post->post_name, $link);
243
  }
244
  }
@@ -256,10 +265,11 @@ class Custom_Post_Type_Permalinks {
256
  * @param Object $post 投稿情報
257
  * @param String $leavename 記事編集画面でのみ渡される
258
  *
259
- * @version 1.3
260
  *
261
  */
262
- public function set_permalink( $post_link, $post, $leavename ) {
 
263
  global $wp_rewrite;
264
  $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
265
  if( $draft_or_pending and !$leavename )
@@ -267,12 +277,11 @@ class Custom_Post_Type_Permalinks {
267
 
268
  $post_type = $post->post_type;
269
  $permalink = $wp_rewrite->get_extra_permastruct( $post_type );
 
 
 
270
 
271
 
272
- $permalink = str_replace( '%post_type%', get_post_type_object($post->post_type)->rewrite['slug'], $permalink );
273
- $permalink = str_replace( '%'.$post_type.'_id%', $post->ID, $permalink );
274
- $permalink = str_replace( '%'.$post_type.'_page%', "", $permalink );
275
- $permalink = str_replace( '%'.$post_type.'_cpage%', "", $permalink );
276
 
277
  $parentsDirs = "";
278
  if( !$leavename ){
@@ -290,30 +299,42 @@ class Custom_Post_Type_Permalinks {
290
  }
291
 
292
  //%post_id%/attachment/%attachement_name%;
293
- if( isset($_GET["post"]) &&$_GET["post"] != $post->ID ) {
294
-
295
- if( "%post_id%" == array_pop(explode("/",get_option( $post_type.'_structure' )))) {
 
296
  $permalink = $permalink."/attachment/";
297
  };
298
  }
299
 
300
-
301
  $taxonomies = get_taxonomies( array('show_ui' => true),'objects' );
302
 
 
 
303
  foreach ( $taxonomies as $taxonomy => $objects ) {
304
  if ( strpos($permalink, "%$taxonomy%") !== false ) {
305
  $terms = get_the_terms( $post->ID, $taxonomy );
306
 
307
- if ( $terms ) {
308
- usort($terms, '_usort_terms_by_ID'); // order by ID
309
- $term = $terms[0]->slug;
310
 
311
- if ( $parent = $terms[0]->parent ) {
312
- $term = $this->get_taxonomy_parents( $parent,$taxonomy, false, '/', true ) . $term;
 
 
 
 
 
 
 
 
 
 
313
  }
314
-
 
 
315
  }
316
-
317
  if( isset($term) ) {
318
  $permalink = str_replace( "%$taxonomy%", $term, $permalink );
319
  }
@@ -331,12 +352,10 @@ class Custom_Post_Type_Permalinks {
331
  $permalink = str_replace( "%minute%", date("i",$post_date), $permalink );
332
  $permalink = str_replace( "%second%", date("s",$post_date), $permalink );
333
 
334
- $permalink = str_replace('//', "/", $permalink );
335
-
336
- return $permalink = home_url( user_trailingslashit( $permalink ) );
337
- //$str = rtrim( preg_replace("/%[a-z,_]*%/","",get_option("permalink_structure")) ,'/');
338
- //return $permalink = str_replace($str, "", $permalink );
339
 
 
 
 
340
  }
341
 
342
 
@@ -352,7 +371,7 @@ class Custom_Post_Type_Permalinks {
352
  public $get_archives_where_r;
353
 
354
  // function modified by [steve]
355
- public function get_archives_where( $where, $r ) {
356
  $this->get_archives_where_r = $r;
357
  if ( isset($r['post_type']) ) {
358
  $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
@@ -378,7 +397,7 @@ class Custom_Post_Type_Permalinks {
378
  *
379
  *
380
  */
381
- public function get_archives_join( $join, $r ) {
382
  global $wpdb;
383
  $this->get_archives_where_r = $r;
384
  if(isset($r['taxonomy']) && is_array($r['taxonomy']) )
@@ -392,7 +411,7 @@ class Custom_Post_Type_Permalinks {
392
  /**
393
  *
394
  * get_arcihves_link
395
- * @version 2.0
396
  *
397
  */
398
  public function get_archives_link( $link ) {
@@ -413,12 +432,13 @@ class Custom_Post_Type_Permalinks {
413
  $ret_link = str_replace($blog_url,$blog_url.'/'.'%link_dir%',$link);
414
  }
415
 
 
416
  if(empty($c) ){ // [steve]
417
- $link_dir = $this->get_archives_where_r['post_type'];
418
  }
419
  else{ // [steve]
420
  $c['name'] = ($c['name'] == 'category' && get_option('category_base')) ? get_option('category_base') : $c['name'];
421
- $link_dir = $this->get_archives_where_r['post_type']."/".$c['name']."/".$c['termslug'];
422
  }
423
 
424
  if(!strstr($link,'/date/')){
@@ -443,7 +463,7 @@ class Custom_Post_Type_Permalinks {
443
  * @version 2.1
444
  *
445
  */
446
- public function add_tax_rewrite() {
447
  if(get_option('no_taxonomy_structure')) {
448
  return false;
449
  }
@@ -477,14 +497,14 @@ class Custom_Post_Type_Permalinks {
477
  $tax = 'category_name';
478
  } else {
479
  // Edit by [Xiphe]
480
- if (isset($taxonomyObject->rewrite['slug'])) {
481
- $taxonomypat = $taxonomyObject->rewrite['slug'];
482
- } else {
483
- $taxonomypat = $taxonomy;
484
- }
485
- // [Xiphe] stop
486
-
487
- $tax = $taxonomy;
488
  }
489
 
490
 
@@ -514,7 +534,7 @@ class Custom_Post_Type_Permalinks {
514
  * @version 1.0
515
  *
516
  */
517
- public function set_term_link( $termlink, $term, $taxonomy ) {
518
  if( get_option('no_taxonomy_structure') ) {
519
  return $termlink;
520
  }
@@ -526,7 +546,7 @@ class Custom_Post_Type_Permalinks {
526
  if( empty($taxonomy) )
527
  return $termlink;
528
 
529
- $wp_home = rtrim( get_option('home'), '/' );
530
 
531
  $post_type = $taxonomy->object_type[0];
532
  $slug = get_post_type_object($post_type)->rewrite['slug'];
@@ -565,9 +585,9 @@ class Custom_Post_Type_Permalinks {
565
  $type_count = count($post_types);
566
  $i = 0;
567
  foreach ($post_types as $post_type):
568
- add_action('update_option_'.$post_type.'_structure',array(&$this,'flush_rules'),10,2);
569
  endforeach;
570
- add_action('update_option_no_taxonomy_structure',array(&$this,'flush_rules'),10,2);
571
  }
572
 
573
 
@@ -579,11 +599,8 @@ class Custom_Post_Type_Permalinks {
579
  *
580
  */
581
 
582
- public function flush_rules(){
583
- $this->add_tax_rewrite();
584
- $this->set_archive_rewrite();
585
- $this->set_rewrite();
586
- flush_rewrite_rules();
587
  }
588
 
589
 
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
9
  Text Domain: cptp
10
  License: GPL2 or later
11
  Domain Path: /
39
  */
40
  class Custom_Post_Type_Permalinks {
41
 
42
+
43
+
44
+ public $version = "0.9";
45
+
46
  public $default_structure = '/%postname%/';
47
 
48
  /**
49
  *
50
  * Add Action & filter hooks.
51
+ *
52
  */
53
  public function add_hook () {
54
 
 
55
  add_action( 'plugins_loaded', array(&$this,'check_version') );
56
+ add_action( 'wp_loaded', array(&$this,'add_archive_rewrite_rules'), 99 );
57
+ add_action( 'wp_loaded', array(&$this,'add_tax_rewrite_rules') );
58
+ add_action( 'wp_loaded', array(&$this, "dequeue_flush_rules"),100);
59
+ add_action( 'registered_post_type', array(&$this,'registered_post_type'), 10, 2 );
60
 
61
 
62
  if(get_option( "permalink_structure") != "") {
63
+ add_filter( 'post_type_link', array(&$this,'post_type_link'), 10, 4 );
64
+ add_filter( 'getarchives_join', array(&$this,'getarchives_join'), 10, 2 ); // [steve]
65
+ add_filter( 'getarchives_where', array(&$this,'getarchives_where'), 10 , 2 );
66
  add_filter( 'get_archives_link', array(&$this,'get_archives_link'), 20, 1 );
67
+ add_filter( 'term_link', array(&$this,'term_link'), 10, 3 );
68
  add_filter( 'attachment_link', array(&$this, 'attachment_link'), 20 , 2);
69
  }
70
 
71
+
72
  add_action( 'init', array(&$this,'load_textdomain') );
73
  add_action( 'init', array(&$this, 'update_rules') );
74
+ add_action( 'update_option_cptp_version', array(&$this, 'update_rules') );
75
  add_action( 'admin_init', array(&$this,'settings_api_init'), 30 );
76
  add_action( 'admin_enqueue_scripts', array(&$this,'enqueue_css_js') );
77
  add_action( 'admin_footer', array(&$this,'pointer_js') );
79
 
80
  }
81
 
82
+ /**
83
+ *
84
+ * dequeue flush rules
85
+ * @since 0.9
86
+ *
87
+ */
88
+
89
+ public function dequeue_flush_rules () {
90
+ if(get_option("queue_flush_rules")){
91
+ flush_rewrite_rules();
92
+ update_option( "queue_flush_rules", 0 );
93
+
94
+ }
95
+ }
96
+
97
+
98
+ /**
99
+ *
100
+ * dequeue flush rules
101
+ * @since 0.8.6
102
+ *
103
+ */
104
 
105
  public function check_version() {
106
  $version = get_option('cptp_version', 0);
146
  /**
147
  *
148
  * Add rewrite rules for archives.
149
+ * @version 1.1
150
  *
151
  */
152
+ public function add_archive_rewrite_rules() {
153
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
154
 
155
  foreach ( $post_types as $post_type ):
156
+ if( !$post_type ) {
157
  continue;
158
+ }
159
+
160
  $permalink = get_option( $post_type.'_structure' );
161
  $post_type_obj = get_post_type_object($post_type);
162
  $slug = $post_type_obj->rewrite['slug'];
163
  if( !$slug )
164
  $slug = $post_type;
165
 
166
+ if( $post_type_obj->has_archive ){
167
+ if( is_string( $post_type_obj->has_archive ) ){
168
+ $slug = $post_type_obj->has_archive;
169
+ };
170
 
171
+
172
+ 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' );
173
+ 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' );
174
+ 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' );
175
+ 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' );
176
+ 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' );
177
+ 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' );
178
+ 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' );
179
+ 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' );
180
+ 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' );
181
+ 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' );
182
+ 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' );
183
+ add_rewrite_rule( $slug.'/date/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type='.$post_type, 'top' );
184
+ add_rewrite_rule( $slug.'/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type='.$post_type, 'top' );
185
+ }
 
 
186
 
187
 
188
+ endforeach;
189
+ }
190
 
191
  /**
192
  *
193
+ * registered_post_type
194
+ * ** add rewrite tag for Custom Post Type.
195
+ * @version 1.0
196
+ * @since 0.9
197
  *
198
  */
 
 
199
 
200
+ public function registered_post_type( $post_type, $args ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
+ global $wp_post_types, $wp_rewrite, $wp;
 
 
203
 
204
+ if( $args->_builtin or !$args->publicly_queryable or !$args->show_ui ){
205
+ return false;
206
+ }
207
+ $permalink = get_option( $post_type.'_structure' );
208
 
209
+ if( !$permalink ) {
210
+ $permalink = $this->default_structure;
211
+ }
212
 
213
+ $permalink = str_replace( '%postname%', '%'.$post_type.'%', $permalink );
214
+ $permalink = str_replace( '%post_id%', '%'.$post_type.'_id%', $permalink );
 
 
 
 
 
 
 
215
 
216
+ add_rewrite_tag( '%'.$post_type.'_id%', '([0-9]{1,})','post_type='.$post_type.'&p=' );
 
 
217
 
218
  $taxonomies = get_taxonomies( array("show_ui" => true, "_builtin" => false), 'objects' );
219
  foreach ( $taxonomies as $taxonomy => $objects ):
221
  endforeach;
222
 
223
  $wp_rewrite->use_verbose_page_rules = true;
224
+
225
+ $permalink = trim($permalink, "/" );
226
+ add_permastruct( $post_type, $args->rewrite['slug']."/".$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
  $post_parent = get_post( $post->post_parent );
247
  $post_type = get_post_type_object( $post_parent->post_type );
248
 
249
  if( $post_type->_builtin == false ) {
250
+ if(strpos( $permalink, "%postname%" ) < strrpos( $permalink, "%post_id%" ) && strrpos( $permalink, "attachment/" ) === FALSE ){
251
  $link = str_replace($post->post_name , "attachment/".$post->post_name, $link);
252
  }
253
  }
265
  * @param Object $post 投稿情報
266
  * @param String $leavename 記事編集画面でのみ渡される
267
  *
268
+ * @version 2.0
269
  *
270
  */
271
+ public function post_type_link( $post_link, $post, $leavename ) {
272
+
273
  global $wp_rewrite;
274
  $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
275
  if( $draft_or_pending and !$leavename )
277
 
278
  $post_type = $post->post_type;
279
  $permalink = $wp_rewrite->get_extra_permastruct( $post_type );
280
+ $permalink = str_replace( '%post_id%', $post->ID, $permalink );
281
+ $permalink = str_replace( '%'.$post_type.'_id%', $post->ID, $permalink );
282
+
283
 
284
 
 
 
 
 
285
 
286
  $parentsDirs = "";
287
  if( !$leavename ){
299
  }
300
 
301
  //%post_id%/attachment/%attachement_name%;
302
+ //画像の編集ページでのリンク
303
+ if( isset($_GET["post"]) && $_GET["post"] != $post->ID ) {
304
+ $parent_structure = trim(get_option( $post->post_type.'_structure' ), "/");
305
+ if( "%post_id%" == $parent_structure or "%post_id%" == array_pop( explode( "/", $parent_structure ) ) ) {
306
  $permalink = $permalink."/attachment/";
307
  };
308
  }
309
 
 
310
  $taxonomies = get_taxonomies( array('show_ui' => true),'objects' );
311
 
312
+ //%taxnomomy% -> parent/child
313
+ //運用でケアすべきかも。
314
  foreach ( $taxonomies as $taxonomy => $objects ) {
315
  if ( strpos($permalink, "%$taxonomy%") !== false ) {
316
  $terms = get_the_terms( $post->ID, $taxonomy );
317
 
318
+ if ( $terms and count($terms) > 1 ) {
319
+ if(reset($terms)->parent == 0){
 
320
 
321
+ $keys = array_keys($terms);
322
+ var_dump($keys);
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
+ $term = array_shift($terms);
336
+ $term = $term->slug;
337
  }
 
338
  if( isset($term) ) {
339
  $permalink = str_replace( "%$taxonomy%", $term, $permalink );
340
  }
352
  $permalink = str_replace( "%minute%", date("i",$post_date), $permalink );
353
  $permalink = str_replace( "%second%", date("s",$post_date), $permalink );
354
 
 
 
 
 
 
355
 
356
+ $permalink = home_url().user_trailingslashit( $permalink );
357
+ //$permalink = str_replace("//", "/", $permalink);
358
+ return $permalink;
359
  }
360
 
361
 
371
  public $get_archives_where_r;
372
 
373
  // function modified by [steve]
374
+ public function getarchives_where( $where, $r ) {
375
  $this->get_archives_where_r = $r;
376
  if ( isset($r['post_type']) ) {
377
  $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
397
  *
398
  *
399
  */
400
+ public function getarchives_join( $join, $r ) {
401
  global $wpdb;
402
  $this->get_archives_where_r = $r;
403
  if(isset($r['taxonomy']) && is_array($r['taxonomy']) )
411
  /**
412
  *
413
  * get_arcihves_link
414
+ * @version 2.1
415
  *
416
  */
417
  public function get_archives_link( $link ) {
432
  $ret_link = str_replace($blog_url,$blog_url.'/'.'%link_dir%',$link);
433
  }
434
 
435
+ $post_type = get_post_type_object( $this->get_archives_where_r['post_type'] );
436
  if(empty($c) ){ // [steve]
437
+ $link_dir = $post_type->rewrite["slug"];
438
  }
439
  else{ // [steve]
440
  $c['name'] = ($c['name'] == 'category' && get_option('category_base')) ? get_option('category_base') : $c['name'];
441
+ $link_dir = $post_type->rewrite["slug"]."/".$c['name']."/".$c['termslug'];
442
  }
443
 
444
  if(!strstr($link,'/date/')){
463
  * @version 2.1
464
  *
465
  */
466
+ public function add_tax_rewrite_rules() {
467
  if(get_option('no_taxonomy_structure')) {
468
  return false;
469
  }
497
  $tax = 'category_name';
498
  } else {
499
  // Edit by [Xiphe]
500
+ if (isset($taxonomyObject->rewrite['slug'])) {
501
+ $taxonomypat = $taxonomyObject->rewrite['slug'];
502
+ } else {
503
+ $taxonomypat = $taxonomy;
504
+ }
505
+ // [Xiphe] stop
506
+
507
+ $tax = $taxonomy;
508
  }
509
 
510
 
534
  * @version 1.0
535
  *
536
  */
537
+ public function term_link( $termlink, $term, $taxonomy ) {
538
  if( get_option('no_taxonomy_structure') ) {
539
  return $termlink;
540
  }
546
  if( empty($taxonomy) )
547
  return $termlink;
548
 
549
+ $wp_home = rtrim( home_url(), '/' );
550
 
551
  $post_type = $taxonomy->object_type[0];
552
  $slug = get_post_type_object($post_type)->rewrite['slug'];
585
  $type_count = count($post_types);
586
  $i = 0;
587
  foreach ($post_types as $post_type):
588
+ add_action('update_option_'.$post_type.'_structure',array(&$this,'queue_flush_rules'),10,2);
589
  endforeach;
590
+ add_action('update_option_no_taxonomy_structure',array(&$this,'queue_flush_rules'),10,2);
591
  }
592
 
593
 
599
  *
600
  */
601
 
602
+ public function queue_flush_rules(){
603
+ update_option( "queue_flush_rules", 1 );
 
 
 
604
  }
605
 
606
 
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: Toro_Unit
3
  Tags: permalink,permalinks,custom post type,custom taxonomy,cms
4
  Requires at least: 3.4
5
- Tested up to: 3.5
6
- Stable tag: 0.8.7.5
7
 
8
  Lets you edit the permalink of custom post type.
9
 
@@ -12,6 +12,8 @@ Custom Post Type Permalinks lets you edit the permalink structure of custom post
12
 
13
  Change custom taxonomy archive's permalink to "example.org/post_type/taxonomy_name/term_slug". Can disable this fix.
14
 
 
 
15
 
16
 
17
 
@@ -32,6 +34,14 @@ That's it. You can access the permalinks setting by going to *Settings -> Permal
32
 
33
  == Changelog ==
34
 
 
 
 
 
 
 
 
 
35
  = 0.8.7 =
36
  * Translate Bug Fix.
37
 
2
  Contributors: Toro_Unit
3
  Tags: permalink,permalinks,custom post type,custom taxonomy,cms
4
  Requires at least: 3.4
5
+ Tested up to: 3.6
6
+ Stable tag: 0.9
7
 
8
  Lets you edit the permalink of custom post type.
9
 
12
 
13
  Change custom taxonomy archive's permalink to "example.org/post_type/taxonomy_name/term_slug". Can disable this fix.
14
 
15
+ Nightly Build https://github.com/Toro-Unit/custom-post-type-permalinks
16
+
17
 
18
 
19
 
34
 
35
  == Changelog ==
36
 
37
+ = 0.9 =
38
+ * Add custom post type archive only has_archive->true
39
+ * Change method name.
40
+ * Change hook custom post link.
41
+ * Use Slug in wp_get_archive().
42
+ * Fix attachment link.
43
+ *
44
+
45
  = 0.8.7 =
46
  * Translate Bug Fix.
47