Custom Post Type Permalinks - Version 0.7.3

Version Description

Download this release

Release Info

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

Code changes from version 0.7.2.1 to 0.7.3

Files changed (4) hide show
  1. cptp-ja.mo +0 -0
  2. cptp-ja.po +13 -4
  3. custom-post-type-permalinks.php +111 -141
  4. readme.txt +3 -1
cptp-ja.mo CHANGED
Binary file
cptp-ja.po CHANGED
@@ -8,7 +8,7 @@ msgstr ""
8
  "Project-Id-Version: Custom Post Type Permalinks 0.7.0 Ja\n"
9
  "Report-Msgid-Bugs-To: \n"
10
  "POT-Creation-Date: 2011-09-21 11:00+0900\n"
11
- "PO-Revision-Date: 2011-12-14 21:19+0900\n"
12
  "Last-Translator: Toro_Unit <mail@torounit.com>\n"
13
  "Language-Team: Toro_Unit <mail@torounit.com>\n"
14
  "MIME-Version: 1.0\n"
@@ -18,6 +18,9 @@ msgstr ""
18
  "X-Poedit-Language: Japanese\n"
19
  "X-Poedit-Country: JAPAN\n"
20
 
 
 
 
21
  msgid "Permalinks of Custom post type"
22
  msgstr "カスタム投稿タイプのパーマリンク"
23
 
@@ -27,9 +30,15 @@ msgstr "カスタム投稿タイプのパーマリンク設定"
27
  msgid "Setting permalinks of custom post type."
28
  msgstr "カスタム投稿タイプごとのパーマリンク構造を設定できます。"
29
 
30
- msgid "The tags you can use is '%year%','%monthnum%','%day%','%hour%','%minute%','%second%','%postname%','%post_id%','%author%','%category%','%tag%' and '%{custom_taxonomy_slug}%(Replace the taxomomy term)'."
31
- msgstr "使用できるタグは、'%year%','%monthnum%','%day%','%hour%','%minute%','%second%','%postname%','%post_id%','%author%','%category%','%tag%'、そして、'%{カスタム分類名}%'(その分類のタームに置換されます)です。"
 
 
 
 
 
 
32
 
33
  msgid "If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/."
34
- msgstr "未入力のときは、/%year%/%monthnum%/%day%/%post_id%/に設定されます。"
35
 
8
  "Project-Id-Version: Custom Post Type Permalinks 0.7.0 Ja\n"
9
  "Report-Msgid-Bugs-To: \n"
10
  "POT-Creation-Date: 2011-09-21 11:00+0900\n"
11
+ "PO-Revision-Date: 2011-12-30 10:57+0900\n"
12
  "Last-Translator: Toro_Unit <mail@torounit.com>\n"
13
  "Language-Team: Toro_Unit <mail@torounit.com>\n"
14
  "MIME-Version: 1.0\n"
18
  "X-Poedit-Language: Japanese\n"
19
  "X-Poedit-Country: JAPAN\n"
20
 
21
+ msgid "Add post archives of custom post type and customizable permalinks."
22
+ msgstr "カスタム投稿タイプのアーカイブの追加と、パーマリンクをカスタマイズできるようにします。"
23
+
24
  msgid "Permalinks of Custom post type"
25
  msgstr "カスタム投稿タイプのパーマリンク"
26
 
30
  msgid "Setting permalinks of custom post type."
31
  msgstr "カスタム投稿タイプごとのパーマリンク構造を設定できます。"
32
 
33
+ msgid "The tags you can use is WordPress Structure Tags and '%{custom_taxonomy_slug}%'."
34
+ msgstr "使用できるタグは、WordPress標準のタグと '%{カスタム分類名}%' です。"
35
+
36
+ msgid "%{custom_taxonomy_slug}% is replaced the taxonomy's term.'."
37
+ msgstr "'%{カスタム分類名}%' は、そのカスタム分類のタームに置換されます。"
38
+
39
+ msgid "Presence of the trailing '/' is unified into a standard permalink structure setting."
40
+ msgstr "末尾の '/' の有無は、標準のパーマリンク構造の設定に統一されます。"
41
 
42
  msgid "If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/."
43
+ msgstr "未入力のときは、/%year%/%monthnum%/%day%/%post_id%/ に設定されます。"
44
 
custom-post-type-permalinks.php CHANGED
@@ -5,7 +5,9 @@ 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.7.2.1
 
 
9
  */
10
 
11
 
@@ -33,56 +35,50 @@ Version: 0.7.2.1
33
  /* This plugin don't support Multisite yet.*/
34
 
35
 
36
- function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
37
- $chain = '';
38
- $parent = &get_term( $id, $taxonomy, OBJECT, 'raw');
39
- if ( is_wp_error( $parent ) )
40
- return $parent;
41
-
42
- if ( $nicename )
43
- $name = $parent->slug;
44
- else
45
- $name = $parent->name;
46
-
47
- if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
48
- $visited[] = $parent->parent;
49
- $chain .= get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
50
- }
51
-
52
- if ( $link )
53
- $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
54
- else
55
- $chain .= $name.$separator;
56
- return $chain;
57
- }
58
-
59
-
60
-
61
 
62
  class Custom_Post_Type_Permalinks {
63
 
64
- function add_hooks(){
65
  add_action('wp_loaded',array(&$this,'set_archive_rewrite'),99);
66
  add_action('wp_loaded', array(&$this,'set_rewrite'),100);
67
  add_filter('post_type_link', array(&$this,'set_permalink'),10,3);
68
-
69
  add_filter('getarchives_where', array(&$this,'get_archives_where'), 10, 2);
70
  add_filter('get_archives_link', array(&$this,'get_archives_link'));
71
-
72
  add_action('generate_rewrite_rules', array(&$this,'add_tax_rewrite'));
73
  add_filter('term_link', array(&$this,'set_term_link'),10,3);
74
-
75
-
76
  }
77
 
78
- static function uninstall_hook(){
79
  if ( function_exists('register_uninstall_hook') ) {
80
  register_uninstall_hook(__FILE__, array(&$this,'uninstall_hook_custom_permalink'));
81
  }
82
  }
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
 
 
86
  function set_archive_rewrite() {
87
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
88
 
@@ -91,46 +87,39 @@ class Custom_Post_Type_Permalinks {
91
  $permalink = get_option( $post_type.'_structure' );
92
  $slug = get_post_type_object($post_type)->rewrite['slug'];
93
 
94
- if($slug){
95
-
96
- 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');
97
- 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');
98
- 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');
99
- 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');
100
- 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');
101
-
102
- 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');
103
- 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');
104
- 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');
105
- 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');
106
- 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');
107
- 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');
108
- add_rewrite_rule($slug.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,'top');
109
- add_rewrite_rule($slug.'/([0-9]{1,})/?$','index.php?p=$matches[1]&post_type='.$post_type,'top');
110
- add_rewrite_rule($slug.'/?$','index.php?post_type='.$post_type,'top');
111
  }
112
 
113
-
114
- add_rewrite_rule($post_type.'/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');
115
- add_rewrite_rule($post_type.'/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');
116
- add_rewrite_rule($post_type.'/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');
117
- add_rewrite_rule($post_type.'/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');
118
- add_rewrite_rule($post_type.'/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');
119
- add_rewrite_rule($post_type.'/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');
120
- add_rewrite_rule($post_type.'/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');
121
- add_rewrite_rule($post_type.'/date/([0-9]{4})/([0-9]{1,2})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&post_type='.$post_type,'top');
122
- add_rewrite_rule($post_type.'/date/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type,'top');
123
- add_rewrite_rule($post_type.'/date/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type,'top');
124
- add_rewrite_rule($post_type.'/date/([0-9]{4})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&paged=$matches[2]&post_type='.$post_type,'top');
125
- add_rewrite_rule($post_type.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,'top');
126
- add_rewrite_rule($post_type.'/([0-9]{1,})/?$','index.php?p=$matches[1]&post_type='.$post_type,'top');
127
- add_rewrite_rule($post_type.'/?$','index.php?post_type='.$post_type,'top');
128
  endforeach;
129
  }
130
-
131
-
132
-
133
-
134
  //rewrite_tagの追加
135
  function set_rewrite() {
136
  global $wp_rewrite;
@@ -145,7 +134,6 @@ class Custom_Post_Type_Permalinks {
145
  $permalink = '/%year%/%monthnum%/%day%/%post_id%/';
146
  }
147
 
148
- //$permalink = str_replace('%postname%','%'.$post_type.'_name%',$permalink);
149
  $permalink = str_replace('%postname%','%'.$post_type.'%',$permalink);
150
  $permalink = str_replace('%post_id%','%'.$post_type.'_id%',$permalink);
151
 
@@ -153,29 +141,26 @@ class Custom_Post_Type_Permalinks {
153
  if( !$slug ) {
154
  $slug = $post_type;
155
  }
156
-
157
- $permalink = '/'.$slug.'/'.$permalink.'/%'.$post_type.'_page%';
 
158
  $permalink = str_replace('//','/',$permalink);
159
 
160
  $wp_rewrite->add_rewrite_tag('%post_type%', '([^/]+)','post_type=');
161
  $wp_rewrite->add_rewrite_tag('%'.$post_type.'_id%', '([0-9]{1,})','post_type='.$post_type.'&p=');
162
- $wp_rewrite->add_rewrite_tag('%'.$post_type.'_name%', '([^/]+)',$post_type.'=');
163
- $wp_rewrite->add_rewrite_tag('%'.$post_type.'_page%', '([0-9]{1,}?)/?',"page=");
164
  $wp_rewrite->add_permastruct($post_type,$permalink, false);
165
 
166
  endforeach;
167
 
168
  $taxonomies = get_taxonomies(array("show_ui" => true),'objects');
169
  foreach ( $taxonomies as $taxonomy => $objects ):
170
-
171
  $wp_rewrite->add_rewrite_tag("%$taxonomy%", '(.+?)',"$taxonomy=");
172
-
173
  endforeach;
174
 
175
  $wp_rewrite->use_verbose_page_rules = true;
176
  }
177
 
178
-
179
  function set_permalink( $post_link, $post,$leavename ) {
180
  global $wp_rewrite;
181
  $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
@@ -183,7 +168,6 @@ class Custom_Post_Type_Permalinks {
183
  return $post_link;
184
 
185
  $post_type = $post->post_type;
186
-
187
  $permalink = $wp_rewrite->get_extra_permastruct($post_type);
188
 
189
  $permalink = str_replace('%post_type%', $post->post_type, $permalink);
@@ -192,9 +176,7 @@ class Custom_Post_Type_Permalinks {
192
 
193
  if( !$leavename ){
194
  $permalink = str_replace('%'.$post_type.'%', $post->post_name, $permalink);
195
- $permalink = str_replace('%'.$post_type.'_name%', $post->post_name, $permalink);
196
  }
197
-
198
 
199
  $taxonomies = get_taxonomies( array('show_ui' => true),'objects');
200
  foreach ( $taxonomies as $taxonomy => $objects ) {
@@ -206,25 +188,17 @@ class Custom_Post_Type_Permalinks {
206
  $term = $terms[0]->slug;
207
 
208
  if ( $parent = $terms[0]->parent ) {
209
- $term = get_taxonomy_parents($parent,$taxonomy, false, '/', true) . $term;
210
  }
211
  }
212
-
213
  $permalink = str_replace("%$taxonomy%", $term, $permalink);
214
  }
215
-
216
  }
217
 
218
-
219
-
220
-
221
  $user = get_userdata($post->post_author);
222
- $permalink = str_replace("%author%", $user->user_login, $permalink);
223
-
224
-
225
 
226
  $post_date = strtotime($post->post_date);
227
-
228
  $permalink = str_replace("%year%",date("Y",$post_date), $permalink);
229
  $permalink = str_replace("%monthnum%",date("m",$post_date), $permalink);
230
  $permalink = str_replace("%day%",date("d",$post_date), $permalink);
@@ -232,13 +206,10 @@ class Custom_Post_Type_Permalinks {
232
  $permalink = str_replace("%minute%",date("i",$post_date), $permalink);
233
  $permalink = str_replace("%second%",date("s",$post_date), $permalink);
234
 
235
- $permalink = str_replace('//',"/",$permalink);
236
-
237
  $permalink = home_url(user_trailingslashit($permalink));
238
  return $permalink;
239
  }
240
-
241
-
242
 
243
  /**
244
  *
@@ -258,7 +229,7 @@ class Custom_Post_Type_Permalinks {
258
 
259
  public $post_type_archives;
260
 
261
- function get_archives_where($where, $r) {
262
  if ( isset($r['post_type']) ) {
263
  $this->post_type_archives = $r['post_type'];
264
  $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
@@ -268,15 +239,13 @@ class Custom_Post_Type_Permalinks {
268
  return $where;
269
  }
270
 
271
- function get_archives_link($link_html) {
272
  if ( '' != $this->post_type_archives )
273
  $blog_url = get_bloginfo("url");
274
  $link_html = str_replace($blog_url,$blog_url.$this->post_type_archives,$link_html);
275
  return $link_html;
276
  }
277
 
278
-
279
-
280
  /**
281
  *
282
  *fix permalink custom taxonomy
@@ -286,27 +255,30 @@ class Custom_Post_Type_Permalinks {
286
  *
287
  */
288
 
289
- function add_tax_rewrite(){
290
  global $wp_rewrite;
291
- $taxonomies = get_taxonomies(array("_builtin"=>false));
292
  if(empty($taxonomies)){
293
  return ;
294
  }
295
 
296
  foreach ($taxonomies as $taxonomy) :
297
-
298
  $post_types = get_taxonomy($taxonomy)->object_type;
299
  foreach ($post_types as $post_type){
300
  $slug = get_post_type_object($post_type)->rewrite["slug"];
301
  //add taxonomy slug
302
- add_rewrite_rule($slug.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]');
303
- add_rewrite_rule($post_type.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]');
304
  }
305
  endforeach;
306
  }
307
 
308
- function set_term_link($termlink,$term,$taxonomy){
309
  $taxonomy = get_taxonomy($taxonomy);
 
 
 
 
310
  if(empty($taxonomy)){
311
  return $termlink;
312
  }
@@ -318,8 +290,6 @@ class Custom_Post_Type_Permalinks {
318
  return str_replace($wp_home,$wp_home.$slug,$termlink);
319
  }
320
 
321
-
322
-
323
  static function uninstall_hook_custom_permalink () {
324
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
325
  foreach ( $post_types as $post_type ):
@@ -327,39 +297,49 @@ class Custom_Post_Type_Permalinks {
327
  endforeach;
328
  }
329
 
330
-
331
-
332
  }
333
 
334
-
335
-
336
-
337
  class Custom_Post_Type_Permalinks_Admin {
338
 
339
- function add_hooks(){
340
  add_action('init', array(&$this,'load_textdomain'));
341
  add_action('admin_init', array(&$this,'settings_api_init'),10);
342
  add_action('admin_init', array(&$this,'set_rules'),50);
343
-
344
  }
345
 
346
- function load_textdomain(){
347
  load_plugin_textdomain('cptp',false,'custom-post-type-permalinks');
348
  }
349
 
350
- function settings_api_init(){
351
  add_settings_section('setting_section',
352
  __("Permalink Setting for custom post type",'cptp'),
353
- array(&$this,'setting_section_callback_function'),
354
  'permalink');
355
-
356
-
357
 
358
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
359
  foreach ($post_types as $post_type):
360
  if(isset($_POST['submit'])){
361
- if( strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE )
362
- update_option($post_type.'_structure',esc_attr($_POST[$post_type.'_structure']));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
363
  }
364
 
365
  add_settings_field($post_type.'_structure',
@@ -368,52 +348,42 @@ class Custom_Post_Type_Permalinks_Admin {
368
  'permalink',
369
  'setting_section',
370
  $post_type.'_structure');
371
-
372
- register_setting('permalink',$post_type.'_structure');
373
- endforeach;
374
 
 
 
375
  }
376
 
377
- function set_rules(){
378
  global $wp_rewrite;
379
  $wp_rewrite->flush_rules();
380
  }
381
-
382
-
383
-
384
  function setting_section_callback_function() {
385
  ?>
386
  <p><?php _e("Setting permalinks of custom post type.",'cptp');?><br />
387
- <?php _e("The tags you can use is '%year%','%monthnum%','%day%','%hour%','%minute%','%second%','%postname%','%post_id%','%author%','%category%','%tag%' and '%{custom_taxonomy_slug}%(Replace the taxomomy term)'.",'cptp');?><br />
 
 
 
388
  <?php _e("If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/.",'cptp');?>
389
  </p>
390
  <?php
391
  }
392
-
393
-
394
- function setting_callback_function( $option ) {
395
-
396
  $post_type = str_replace('_structure',"" ,$option);
397
  $slug = get_post_type_object($post_type)->rewrite['slug'];
398
  if( !$slug ) {
399
  $slug = $post_type;
400
  }
401
- echo '/'.$slug.'<input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . get_option($option) .'" />';
402
-
403
  }
404
-
405
-
406
-
407
  }
408
 
409
 
410
  $custom_post_type_permalinks = new Custom_Post_Type_Permalinks;
411
-
412
- if(get_option("permalink_structure") != ""){
413
- $custom_post_type_permalinks->add_hooks();
414
- }
415
  $custom_post_type_permalinks->uninstall_hook();
416
-
417
  $custom_post_type_permalinks_admin = new Custom_Post_Type_Permalinks_Admin;
418
  $custom_post_type_permalinks_admin->add_hooks();
419
 
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.7.3
9
+ Text Domain: cptp
10
+ Domain Path: /
11
  */
12
 
13
 
35
  /* This plugin don't support Multisite yet.*/
36
 
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  class Custom_Post_Type_Permalinks {
40
 
41
+ function add_hooks() {
42
  add_action('wp_loaded',array(&$this,'set_archive_rewrite'),99);
43
  add_action('wp_loaded', array(&$this,'set_rewrite'),100);
44
  add_filter('post_type_link', array(&$this,'set_permalink'),10,3);
 
45
  add_filter('getarchives_where', array(&$this,'get_archives_where'), 10, 2);
46
  add_filter('get_archives_link', array(&$this,'get_archives_link'));
 
47
  add_action('generate_rewrite_rules', array(&$this,'add_tax_rewrite'));
48
  add_filter('term_link', array(&$this,'set_term_link'),10,3);
 
 
49
  }
50
 
51
+ static function uninstall_hook() {
52
  if ( function_exists('register_uninstall_hook') ) {
53
  register_uninstall_hook(__FILE__, array(&$this,'uninstall_hook_custom_permalink'));
54
  }
55
  }
56
 
57
+ function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
58
+ $chain = '';
59
+ $parent = &get_term( $id, $taxonomy, OBJECT, 'raw');
60
+ if ( is_wp_error( $parent ) )
61
+ return $parent;
62
+
63
+ if ( $nicename )
64
+ $name = $parent->slug;
65
+ else
66
+ $name = $parent->name;
67
+
68
+ if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
69
+ $visited[] = $parent->parent;
70
+ $chain .= $this->get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
71
+ }
72
+
73
+ if ( $link )
74
+ $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
75
+ else
76
+ $chain .= $name.$separator;
77
+ return $chain;
78
+ }
79
 
80
 
81
+
82
  function set_archive_rewrite() {
83
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
84
 
87
  $permalink = get_option( $post_type.'_structure' );
88
  $slug = get_post_type_object($post_type)->rewrite['slug'];
89
 
90
+ if( $slug ){
91
+ 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,'bottom');
92
+ 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,'bottom');
93
+ 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,'bottom');
94
+ 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,'bottom');
95
+ 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,'bottom');
96
+ 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,'bottom');
97
+ 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,'bottom');
98
+ add_rewrite_rule($slug.'/date/([0-9]{4})/([0-9]{1,2})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&post_type='.$post_type,'bottom');
99
+ 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,'bottom');
100
+ 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,'bottom');
101
+ add_rewrite_rule($slug.'/date/([0-9]{4})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&paged=$matches[2]&post_type='.$post_type,'bottom');
102
+ add_rewrite_rule($slug.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,'bottom');
103
+ add_rewrite_rule($slug.'/author/([^/]+)/?$','index.php?author=$matches[1]&post_type='.$post_type,'bottom');
104
+ add_rewrite_rule($slug.'/?$','index.php?post_type='.$post_type,'bottom');
 
 
105
  }
106
 
107
+ add_rewrite_rule($post_type.'/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,'bottom');
108
+ add_rewrite_rule($post_type.'/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,'bottom');
109
+ add_rewrite_rule($post_type.'/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,'bottom');
110
+ add_rewrite_rule($post_type.'/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,'bottom');
111
+ add_rewrite_rule($post_type.'/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,'bottom');
112
+ add_rewrite_rule($post_type.'/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,'bottom');
113
+ add_rewrite_rule($post_type.'/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,'bottom');
114
+ add_rewrite_rule($post_type.'/date/([0-9]{4})/([0-9]{1,2})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&post_type='.$post_type,'bottom');
115
+ add_rewrite_rule($post_type.'/date/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type,'bottom');
116
+ add_rewrite_rule($post_type.'/date/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&feed=$matches[2]&post_type='.$post_type,'bottom');
117
+ add_rewrite_rule($post_type.'/date/([0-9]{4})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&paged=$matches[2]&post_type='.$post_type,'bottom');
118
+ add_rewrite_rule($post_type.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,'bottom');
119
+ add_rewrite_rule($post_type.'/date/([0-9]{4})/?$','index.php?author=$matches[1]&post_type='.$post_type,'bottom');
120
+ add_rewrite_rule($post_type.'/?$','index.php?post_type='.$post_type,'bottom');
 
121
  endforeach;
122
  }
 
 
 
 
123
  //rewrite_tagの追加
124
  function set_rewrite() {
125
  global $wp_rewrite;
134
  $permalink = '/%year%/%monthnum%/%day%/%post_id%/';
135
  }
136
 
 
137
  $permalink = str_replace('%postname%','%'.$post_type.'%',$permalink);
138
  $permalink = str_replace('%post_id%','%'.$post_type.'_id%',$permalink);
139
 
141
  if( !$slug ) {
142
  $slug = $post_type;
143
  }
144
+
145
+ $permalink = '/'.$slug.'/'.$permalink;
146
+ $permalink = $permalink.'/%'.$post_type.'_page%';
147
  $permalink = str_replace('//','/',$permalink);
148
 
149
  $wp_rewrite->add_rewrite_tag('%post_type%', '([^/]+)','post_type=');
150
  $wp_rewrite->add_rewrite_tag('%'.$post_type.'_id%', '([0-9]{1,})','post_type='.$post_type.'&p=');
151
+ $wp_rewrite->add_rewrite_tag('%'.$post_type.'_page%', '/?([0-9]{1,}?)/?',"page=");
 
152
  $wp_rewrite->add_permastruct($post_type,$permalink, false);
153
 
154
  endforeach;
155
 
156
  $taxonomies = get_taxonomies(array("show_ui" => true),'objects');
157
  foreach ( $taxonomies as $taxonomy => $objects ):
 
158
  $wp_rewrite->add_rewrite_tag("%$taxonomy%", '(.+?)',"$taxonomy=");
 
159
  endforeach;
160
 
161
  $wp_rewrite->use_verbose_page_rules = true;
162
  }
163
 
 
164
  function set_permalink( $post_link, $post,$leavename ) {
165
  global $wp_rewrite;
166
  $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
168
  return $post_link;
169
 
170
  $post_type = $post->post_type;
 
171
  $permalink = $wp_rewrite->get_extra_permastruct($post_type);
172
 
173
  $permalink = str_replace('%post_type%', $post->post_type, $permalink);
176
 
177
  if( !$leavename ){
178
  $permalink = str_replace('%'.$post_type.'%', $post->post_name, $permalink);
 
179
  }
 
180
 
181
  $taxonomies = get_taxonomies( array('show_ui' => true),'objects');
182
  foreach ( $taxonomies as $taxonomy => $objects ) {
188
  $term = $terms[0]->slug;
189
 
190
  if ( $parent = $terms[0]->parent ) {
191
+ $term = $this->get_taxonomy_parents($parent,$taxonomy, false, '/', true) . $term;
192
  }
193
  }
 
194
  $permalink = str_replace("%$taxonomy%", $term, $permalink);
195
  }
 
196
  }
197
 
 
 
 
198
  $user = get_userdata($post->post_author);
199
+ $permalink = str_replace("%author%", $user->user_nicename, $permalink);
 
 
200
 
201
  $post_date = strtotime($post->post_date);
 
202
  $permalink = str_replace("%year%",date("Y",$post_date), $permalink);
203
  $permalink = str_replace("%monthnum%",date("m",$post_date), $permalink);
204
  $permalink = str_replace("%day%",date("d",$post_date), $permalink);
206
  $permalink = str_replace("%minute%",date("i",$post_date), $permalink);
207
  $permalink = str_replace("%second%",date("s",$post_date), $permalink);
208
 
209
+ $permalink = str_replace('//',"/",$permalink);
 
210
  $permalink = home_url(user_trailingslashit($permalink));
211
  return $permalink;
212
  }
 
 
213
 
214
  /**
215
  *
229
 
230
  public $post_type_archives;
231
 
232
+ function get_archives_where( $where, $r ) {
233
  if ( isset($r['post_type']) ) {
234
  $this->post_type_archives = $r['post_type'];
235
  $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
239
  return $where;
240
  }
241
 
242
+ function get_archives_link( $link_html ) {
243
  if ( '' != $this->post_type_archives )
244
  $blog_url = get_bloginfo("url");
245
  $link_html = str_replace($blog_url,$blog_url.$this->post_type_archives,$link_html);
246
  return $link_html;
247
  }
248
 
 
 
249
  /**
250
  *
251
  *fix permalink custom taxonomy
255
  *
256
  */
257
 
258
+ function add_tax_rewrite() {
259
  global $wp_rewrite;
260
+ $taxonomies = get_taxonomies(array( '_builtin' => false));
261
  if(empty($taxonomies)){
262
  return ;
263
  }
264
 
265
  foreach ($taxonomies as $taxonomy) :
 
266
  $post_types = get_taxonomy($taxonomy)->object_type;
267
  foreach ($post_types as $post_type){
268
  $slug = get_post_type_object($post_type)->rewrite["slug"];
269
  //add taxonomy slug
270
+ add_rewrite_rule($slug.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]','bottom');
271
+ add_rewrite_rule($post_type.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]','bottom');
272
  }
273
  endforeach;
274
  }
275
 
276
+ function set_term_link( $termlink, $term, $taxonomy ) {
277
  $taxonomy = get_taxonomy($taxonomy);
278
+ if( $taxonomy->_builtin ) {
279
+ return $termlink;
280
+ }
281
+
282
  if(empty($taxonomy)){
283
  return $termlink;
284
  }
290
  return str_replace($wp_home,$wp_home.$slug,$termlink);
291
  }
292
 
 
 
293
  static function uninstall_hook_custom_permalink () {
294
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
295
  foreach ( $post_types as $post_type ):
297
  endforeach;
298
  }
299
 
 
 
300
  }
301
 
 
 
 
302
  class Custom_Post_Type_Permalinks_Admin {
303
 
304
+ function add_hooks() {
305
  add_action('init', array(&$this,'load_textdomain'));
306
  add_action('admin_init', array(&$this,'settings_api_init'),10);
307
  add_action('admin_init', array(&$this,'set_rules'),50);
 
308
  }
309
 
310
+ function load_textdomain() {
311
  load_plugin_textdomain('cptp',false,'custom-post-type-permalinks');
312
  }
313
 
314
+ function settings_api_init() {
315
  add_settings_section('setting_section',
316
  __("Permalink Setting for custom post type",'cptp'),
317
+ array(&$this,'setting_section_callback_function'),
318
  'permalink');
 
 
319
 
320
  $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
321
  foreach ($post_types as $post_type):
322
  if(isset($_POST['submit'])){
323
+ if( strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) {
324
+
325
+ $structure = trim(esc_attr($_POST[$post_type.'_structure']));#get setting
326
+
327
+ #default permalink structure
328
+ if( !$structure ) {
329
+ $structure = '/%year%/%monthnum%/%day%/%post_id%/';
330
+ }
331
+ $structure = str_replace('//','/','/'.$structure);# first "/"
332
+
333
+ #last "/"
334
+ $lastString = substr(trim(esc_attr($_POST['permalink_structure'])),-1);
335
+ $structure = rtrim($structure,'/');
336
+ if ( $lastString == '/') {
337
+ $structure = $structure.'/';
338
+ }
339
+
340
+ update_option($post_type.'_structure', $structure );
341
+ }
342
+
343
  }
344
 
345
  add_settings_field($post_type.'_structure',
348
  'permalink',
349
  'setting_section',
350
  $post_type.'_structure');
 
 
 
351
 
352
+ register_setting('permalink',$post_type.'_structure');
353
+ endforeach;
354
  }
355
 
356
+ function set_rules() {
357
  global $wp_rewrite;
358
  $wp_rewrite->flush_rules();
359
  }
360
+
 
 
361
  function setting_section_callback_function() {
362
  ?>
363
  <p><?php _e("Setting permalinks of custom post type.",'cptp');?><br />
364
+ <?php _e("The tags you can use is WordPress Structure Tags and '%{custom_taxonomy_slug}%'.",'cptp');?><br />
365
+ <?php _e("%{custom_taxonomy_slug}% is replaced the taxonomy's term.'.",'cptp');?></p>
366
+
367
+ <p><?php _e("Presence of the trailing '/' is unified into a standard permalink structure setting.",'cptp');?><br />
368
  <?php _e("If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/.",'cptp');?>
369
  </p>
370
  <?php
371
  }
372
+
373
+ function setting_callback_function( $option ) {
 
 
374
  $post_type = str_replace('_structure',"" ,$option);
375
  $slug = get_post_type_object($post_type)->rewrite['slug'];
376
  if( !$slug ) {
377
  $slug = $post_type;
378
  }
379
+ echo '/'.$slug.' <input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . get_option($option) .'" />';
 
380
  }
 
 
 
381
  }
382
 
383
 
384
  $custom_post_type_permalinks = new Custom_Post_Type_Permalinks;
385
+ if(get_option("permalink_structure") != ""){ $custom_post_type_permalinks->add_hooks(); }
 
 
 
386
  $custom_post_type_permalinks->uninstall_hook();
 
387
  $custom_post_type_permalinks_admin = new Custom_Post_Type_Permalinks_Admin;
388
  $custom_post_type_permalinks_admin->add_hooks();
389
 
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: Toro_Unit
3
  Tags: custom post type,permalink
4
  Requires at least: 3.2
5
  Tested up to: 3.3
6
- Stable tag: 0.7.2.1
7
 
8
  Lets you edit the permalink of custom post type.
9
 
@@ -30,6 +30,8 @@ That's it. You can access the permalinks setting by going to *Settings -> Permal
30
 
31
  == Changelog ==
32
 
 
 
33
 
34
  =0.7.2=
35
  * Reweite bug fix.
3
  Tags: custom post type,permalink
4
  Requires at least: 3.2
5
  Tested up to: 3.3
6
+ Stable tag: 0.7.3
7
 
8
  Lets you edit the permalink of custom post type.
9
 
30
 
31
  == Changelog ==
32
 
33
+ =0.7.3=
34
+ * Changed part for saving the data.
35
 
36
  =0.7.2=
37
  * Reweite bug fix.