Custom Post Type Permalinks - Version 0.8.7.5

Version Description

Download this release

Release Info

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

Code changes from version 0.8.7.1 to 0.8.7.5

Files changed (2) hide show
  1. custom-post-type-permalinks.php +71 -24
  2. readme.txt +2 -2
custom-post-type-permalinks.php CHANGED
@@ -5,8 +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.8.7.1
9
  Text Domain: cptp
 
10
  Domain Path: /
11
  */
12
 
@@ -28,17 +29,17 @@ Domain Path: /
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
- public $version = "0.8.7.1";
42
  public $default_structure = '/%postname%/';
43
 
44
  /**
@@ -55,12 +56,14 @@ class Custom_Post_Type_Permalinks {
55
  add_action( 'wp_loaded', array(&$this,'set_rewrite'), 100 );
56
  add_action( 'wp_loaded', array(&$this,'add_tax_rewrite') );
57
 
 
58
  if(get_option( "permalink_structure") != "") {
59
  add_filter( 'post_type_link', array(&$this,'set_permalink'), 10, 3 );
60
  add_filter( 'getarchives_join', array(&$this,'get_archives_join'), 10, 2 ); // [steve]
61
  add_filter( 'getarchives_where', array(&$this,'get_archives_where'), 10 , 2 );
62
  add_filter( 'get_archives_link', array(&$this,'get_archives_link'), 20, 1 );
63
  add_filter( 'term_link', array(&$this,'set_term_link'), 10, 3 );
 
64
  }
65
 
66
  add_action( 'init', array(&$this,'load_textdomain') );
@@ -160,7 +163,7 @@ class Custom_Post_Type_Permalinks {
160
  *
161
  * Add Rewrite rule for single posts.
162
  * @version 2.0
163
- *
164
  *
165
  */
166
  public function set_rewrite() {
@@ -171,7 +174,7 @@ class Custom_Post_Type_Permalinks {
171
  $permalink = get_option( $post_type.'_structure' );
172
 
173
  if( !$permalink ) {
174
- $permalink = $this->default_structure;
175
  }
176
 
177
  $permalink = str_replace( '%postname%', '%'.$post_type.'%', $permalink );
@@ -180,13 +183,13 @@ class Custom_Post_Type_Permalinks {
180
  $slug = get_post_type_object($post_type)->rewrite['slug'];
181
 
182
  if( !$slug ) {
183
- $slug = $post_type;
184
  }
185
 
186
  $permalink = '/'.$slug.'/'.$permalink;
187
-
188
  if( strpos( $permalink , '%'.$post_type.'%' ) === false ) {
189
- $permalink = $permalink.'/%'.$post_type.'_page%';
190
  }
191
 
192
  $permalink = str_replace( '//', '/', $permalink );
@@ -218,16 +221,42 @@ class Custom_Post_Type_Permalinks {
218
  }
219
 
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
  /**
223
  *
224
  * Fix permalinks output.
225
  *
226
- * @param String $post_link
227
  * @param Object $post 投稿情報
228
  * @param String $leavename 記事編集画面でのみ渡される
229
  *
230
- * @version 1.2
231
  *
232
  */
233
  public function set_permalink( $post_link, $post, $leavename ) {
@@ -239,6 +268,7 @@ class Custom_Post_Type_Permalinks {
239
  $post_type = $post->post_type;
240
  $permalink = $wp_rewrite->get_extra_permastruct( $post_type );
241
 
 
242
  $permalink = str_replace( '%post_type%', get_post_type_object($post->post_type)->rewrite['slug'], $permalink );
243
  $permalink = str_replace( '%'.$post_type.'_id%', $post->ID, $permalink );
244
  $permalink = str_replace( '%'.$post_type.'_page%', "", $permalink );
@@ -259,6 +289,15 @@ class Custom_Post_Type_Permalinks {
259
  $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink );
260
  }
261
 
 
 
 
 
 
 
 
 
 
262
  $taxonomies = get_taxonomies( array('show_ui' => true),'objects' );
263
 
264
  foreach ( $taxonomies as $taxonomy => $objects ) {
@@ -270,7 +309,7 @@ class Custom_Post_Type_Permalinks {
270
  $term = $terms[0]->slug;
271
 
272
  if ( $parent = $terms[0]->parent ) {
273
- $term = $this->get_taxonomy_parents( $parent,$taxonomy, false, '/', true ) . $term;
274
  }
275
 
276
  }
@@ -316,7 +355,7 @@ class Custom_Post_Type_Permalinks {
316
  public function get_archives_where( $where, $r ) {
317
  $this->get_archives_where_r = $r;
318
  if ( isset($r['post_type']) ) {
319
- $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
320
  }
321
 
322
  if(isset($r['taxonomy']) && is_array($r['taxonomy']) ){
@@ -401,12 +440,12 @@ class Custom_Post_Type_Permalinks {
401
  *
402
  * Add rewrite rules for custom taxonomies.
403
  * @since 0.6
404
- * @version 2.0
405
  *
406
  */
407
  public function add_tax_rewrite() {
408
  if(get_option('no_taxonomy_structure')) {
409
- return false;
410
  }
411
 
412
 
@@ -415,11 +454,12 @@ class Custom_Post_Type_Permalinks {
415
  $taxonomies['category'] = 'category';
416
 
417
  if(empty($taxonomies)) {
418
- return false;
419
  }
420
 
421
  foreach ($taxonomies as $taxonomy) :
422
- $post_types = get_taxonomy($taxonomy)->object_type;
 
423
 
424
  foreach ($post_types as $post_type):
425
  $post_type_obj = get_post_type_object($post_type);
@@ -432,13 +472,20 @@ class Custom_Post_Type_Permalinks {
432
  $slug = $post_type_obj->has_archive;
433
  };
434
 
435
- if($taxonomy == 'category'){
436
  $taxonomypat = ($cb = get_option('category_base')) ? $cb : $taxonomy;
437
  $tax = 'category_name';
438
- }else{
439
- $taxonomypat = $taxonomy;
 
 
 
 
 
 
 
440
  $tax = $taxonomy;
441
- }
442
 
443
 
444
  //add taxonomy slug
@@ -469,7 +516,7 @@ class Custom_Post_Type_Permalinks {
469
  */
470
  public function set_term_link( $termlink, $term, $taxonomy ) {
471
  if( get_option('no_taxonomy_structure') ) {
472
- return $termlink;
473
  }
474
 
475
  $taxonomy = get_taxonomy($taxonomy);
@@ -487,7 +534,7 @@ class Custom_Post_Type_Permalinks {
487
 
488
  //$termlink = str_replace( $term->slug.'/', $this->get_taxonomy_parents( $term->term_id,$taxonomy->name, false, '/', true ), $termlink );
489
  $termlink = str_replace( $wp_home, $wp_home.'/'.$slug, $termlink );
490
- $str = rtrim( preg_replace("/%[a-z_]*%/","",get_option("permalink_structure")) ,'/');
491
  return str_replace($str, "", $termlink );
492
 
493
  }
@@ -669,7 +716,7 @@ class Custom_Post_Type_Permalinks {
669
  ?>
670
  <script type="text/javascript">
671
  jQuery(function($) {
672
-
673
  $("#menu-settings .wp-has-submenu").pointer({
674
  content: "<?php echo $content;?>",
675
  position: {"edge":"left","align":"center"},
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: /
12
  */
13
 
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
+ public $version = "0.8.7.5";
43
  public $default_structure = '/%postname%/';
44
 
45
  /**
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') );
163
  *
164
  * Add Rewrite rule for single posts.
165
  * @version 2.0
166
+ *
167
  *
168
  */
169
  public function set_rewrite() {
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 );
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 );
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 );
237
+ $permalink = get_option( $post_parent->post_type.'_structure' );
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
+ }
245
+
246
+ return $link;
247
+ }
248
+
249
+
250
 
251
  /**
252
  *
253
  * Fix permalinks output.
254
  *
255
+ * @param String $post_link
256
  * @param Object $post 投稿情報
257
  * @param String $leavename 記事編集画面でのみ渡される
258
  *
259
+ * @version 1.3
260
  *
261
  */
262
  public function set_permalink( $post_link, $post, $leavename ) {
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 );
289
  $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink );
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 ) {
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
  }
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 );
359
  }
360
 
361
  if(isset($r['taxonomy']) && is_array($r['taxonomy']) ){
440
  *
441
  * Add rewrite rules for custom taxonomies.
442
  * @since 0.6
443
+ * @version 2.1
444
  *
445
  */
446
  public function add_tax_rewrite() {
447
  if(get_option('no_taxonomy_structure')) {
448
+ return false;
449
  }
450
 
451
 
454
  $taxonomies['category'] = 'category';
455
 
456
  if(empty($taxonomies)) {
457
+ return false;
458
  }
459
 
460
  foreach ($taxonomies as $taxonomy) :
461
+ $taxonomyObject = get_taxonomy($taxonomy);
462
+ $post_types = $taxonomyObject->object_type;
463
 
464
  foreach ($post_types as $post_type):
465
  $post_type_obj = get_post_type_object($post_type);
472
  $slug = $post_type_obj->has_archive;
473
  };
474
 
475
+ if ( $taxonomy == 'category' ){
476
  $taxonomypat = ($cb = get_option('category_base')) ? $cb : $taxonomy;
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
 
491
  //add taxonomy slug
516
  */
517
  public function set_term_link( $termlink, $term, $taxonomy ) {
518
  if( get_option('no_taxonomy_structure') ) {
519
+ return $termlink;
520
  }
521
 
522
  $taxonomy = get_taxonomy($taxonomy);
534
 
535
  //$termlink = str_replace( $term->slug.'/', $this->get_taxonomy_parents( $term->term_id,$taxonomy->name, false, '/', true ), $termlink );
536
  $termlink = str_replace( $wp_home, $wp_home.'/'.$slug, $termlink );
537
+ $str = rtrim( preg_replace("/%[a-z_]*%/","",get_option("permalink_structure")) ,'/');//remove with front
538
  return str_replace($str, "", $termlink );
539
 
540
  }
716
  ?>
717
  <script type="text/javascript">
718
  jQuery(function($) {
719
+
720
  $("#menu-settings .wp-has-submenu").pointer({
721
  content: "<?php echo $content;?>",
722
  position: {"edge":"left","align":"center"},
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === Custom Post Type Permalinks ===
2
  Contributors: Toro_Unit
3
- Tags: custom post type,permalink
4
  Requires at least: 3.4
5
  Tested up to: 3.5
6
- Stable tag: 0.8.7.1
7
 
8
  Lets you edit the permalink of custom post type.
9
 
1
  === Custom Post Type Permalinks ===
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