PluginProbe ʕ •ᴥ•ʔ
Custom Post Type Permalinks / 0.7.9
Custom Post Type Permalinks v0.7.9
1.2.0 1.3.0 1.3.1 1.4.0 1.5.1 1.5.2 1.5.4 2.0.0 2.0.1 2.0.2 2.1.1 2.1.2 2.1.3 2.2.0 3.0.0 3.0.1 3.1.0 3.1.1 3.1.3 3.1.4 3.1.5 3.2.0 3.2.1 3.2.2 3.3.0 3.3.1 3.3.4 3.3.5 3.4.0 3.4.0-rc.1 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.5.2 3.5.3 3.5.4 3.5.5 trunk 0.6 0.6.1 0.6.2 0.7 0.7.1 0.7.10 0.7.2 0.7.2.1 0.7.3 0.7.3.1 0.7.4 0.7.4.1 0.7.5 0.7.5.1 0.7.5.2 0.7.5.6 0.7.6 0.7.8 0.7.9 0.7.9.1 0.7.9.2 0.8 0.8.1 0.8.6 0.8.7 0.8.7.1 0.8.7.5 0.8.7.6 0.9 0.9.1 0.9.2.1 0.9.3.1 0.9.3.2 0.9.3.3 0.9.5 0.9.5.1 0.9.5.2 0.9.5.3 0.9.5.4 0.9.5.6 0.9.6 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0
custom-post-type-permalinks / custom-post-type-permalinks.php
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
494 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.7.9
9 Text Domain: cptp
10 Domain Path: /
11 */
12
13
14 /* Copyright 2011 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 class Custom_Post_Type_Permalinks {
35
36 static public $default_structure = '/%postname%/';
37
38 public function __construct () {
39 add_action('wp_loaded',array(&$this,'set_archive_rewrite'),99);
40 add_action('wp_loaded', array(&$this,'set_rewrite'),100);
41 add_action('wp_loaded', array(&$this,'add_tax_rewrite'));
42
43 if(get_option("permalink_structure") != "") {
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'),20,1);
47 add_filter('term_link', array(&$this,'set_term_link'),10,3);
48 }
49 }
50
51 /**
52 *
53 * Get Custom Taxonomies parents.
54 *
55 */
56 private function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
57 $chain = '';
58 $parent = &get_term( $id, $taxonomy, OBJECT, 'raw');
59 if ( is_wp_error( $parent ) ) {
60 return $parent;
61 }
62
63 if ( $nicename ){
64 $name = $parent->slug;
65 }else {
66 $name = $parent->name;
67 }
68
69 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
70 $visited[] = $parent->parent;
71 $chain .= $this->get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
72 }
73
74 if ( $link ) {
75 $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
76 }else {
77 $chain .= $name.$separator;
78 }
79 return $chain;
80 }
81
82 /**
83 *
84 * Add rewrite rules for archives.
85 *
86 */
87 public function set_archive_rewrite() {
88 $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
89
90 foreach ( $post_types as $post_type ):
91 if( !$post_type )
92 continue;
93 $permalink = get_option( $post_type.'_structure' );
94 $post_type_obj = get_post_type_object($post_type);
95 $slug = $post_type_obj->rewrite['slug'];
96 if( !$slug )
97 $slug = $post_type;
98
99 if( is_string( $post_type_obj->has_archive ) ){
100 $slug = $post_type_obj->has_archive;
101 };
102
103 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' );
104 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' );
105 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' );
106 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' );
107 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' );
108 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' );
109 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' );
110 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' );
111 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' );
112 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' );
113 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' );
114 add_rewrite_rule( $slug.'/date/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type='.$post_type, 'top' );
115 add_rewrite_rule( $slug.'/author/([^/]+)/?$', 'index.php?author=$matches[1]&post_type='.$post_type, 'top' );
116 add_rewrite_rule( $slug.'/page/?([0-9]{1,})/?$', 'index.php?paged=$matches[1]&post_type='.$post_type, 'top' );
117 add_rewrite_rule( $slug.'/?$', 'index.php?post_type='.$post_type, 'top' );
118
119
120 endforeach;
121 }
122
123
124 /**
125 *
126 * Add Rewrite rule for single posts.
127 *
128 */
129 public function set_rewrite() {
130 global $wp_rewrite;
131
132 $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
133 foreach ( $post_types as $post_type ):
134 $permalink = get_option( $post_type.'_structure' );
135
136 if( !$permalink )
137 $permalink = self::$default_structure;
138
139 $permalink = str_replace( '%postname%', '%'.$post_type.'%', $permalink );
140 $permalink = str_replace( '%post_id%', '%'.$post_type.'_id%', $permalink );
141
142 $slug = get_post_type_object($post_type)->rewrite['slug'];
143
144 if( !$slug )
145 $slug = $post_type;
146
147 $permalink = '/'.$slug.'/'.$permalink;
148 $permalink = $permalink.'/%'.$post_type.'_page%';
149 $permalink = str_replace( '//', '/', $permalink );
150
151 $wp_rewrite->add_rewrite_tag( '%post_type%', '([^/]+)', 'post_type=' );
152 $wp_rewrite->add_rewrite_tag( '%'.$post_type.'_id%', '([0-9]{1,})','post_type='.$post_type.'&p=' );
153 $wp_rewrite->add_rewrite_tag( '%'.$post_type.'_page%', '([0-9]{1,}?)',"page=" );
154
155 //test
156 if( is_post_type_hierarchical($post_type) ) {
157 $wp_rewrite->add_rewrite_tag( '%'.$post_type.'%', '(?:[^/]+/){1}([^/]+)/?','name=' );
158 }
159 //$wp_rewrite->add_permastruct( $post_type, $permalink, false );
160 $wp_rewrite->generate_rewrite_rules( $permalink, EP_NONE, true, true, true,true);
161
162 endforeach;
163
164 $taxonomies = get_taxonomies( array("show_ui" => true, "_builtin" => false), 'objects' );
165 foreach ( $taxonomies as $taxonomy => $objects ):
166 $wp_rewrite->add_rewrite_tag( "%$taxonomy%", '(.+?)', "$taxonomy=" );
167 endforeach;
168
169 $wp_rewrite->use_verbose_page_rules = true;
170 }
171
172 /**
173 *
174 * Fix permalinks output.
175 *
176 */
177 public function set_permalink( $post_link, $post,$leavename ) {
178 global $wp_rewrite;
179 $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
180 if( $draft_or_pending and !$leavename )
181 return $post_link;
182
183 $post_type = $post->post_type;
184 $permalink = $wp_rewrite->get_extra_permastruct( $post_type );
185
186 $permalink = str_replace( '%post_type%', get_post_type_object($post->post_type)->rewrite['slug'], $permalink );
187 $permalink = str_replace( '%'.$post_type.'_id%', $post->ID, $permalink );
188 $permalink = str_replace( '%'.$post_type.'_page%', "", $permalink );
189 $permalink = str_replace( '%'.$post_type.'_cpage%', "", $permalink );
190
191 $parentsDirs = "";
192 $postId = $post->ID;
193 while ($parent = get_post($postId)->post_parent) {
194 $parentsDirs = get_post($parent)->post_name."/".$parentsDirs;
195 $postId = $parent;
196 }
197
198 $permalink = str_replace( '%'.$post_type.'%', $parentsDirs.'%'.$post_type.'%', $permalink );
199
200 if( !$leavename ){
201 $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink );
202 }
203
204 $taxonomies = get_taxonomies( array('show_ui' => true),'objects' );
205
206 foreach ( $taxonomies as $taxonomy => $objects ) {
207 if ( strpos($permalink, "%$taxonomy%") !== false ) {
208 $terms = get_the_terms( $post->ID, $taxonomy );
209
210 if ( $terms ) {
211 usort($terms, '_usort_terms_by_ID'); // order by ID
212 $term = $terms[0]->slug;
213
214 if ( $parent = $terms[0]->parent )
215 $term = $this->get_taxonomy_parents( $parent,$taxonomy, false, '/', true ) . $term;
216 }
217
218 if( isset($term) ) {
219 $permalink = str_replace( "%$taxonomy%", $term, $permalink );
220 }
221 }
222 }
223
224 $user = get_userdata( $post->post_author );
225 $permalink = str_replace( "%author%", $user->user_nicename, $permalink );
226
227 $post_date = strtotime( $post->post_date );
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 );
231 $permalink = str_replace( "%hour%", date("H",$post_date), $permalink );
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 $str = rtrim( preg_replace("/%[a-z,_]*%/","",get_option("permalink_structure")) ,'/');
239 return $permalink = str_replace($str, "", $permalink );
240
241 }
242
243 /**
244 *
245 * wp_get_archives fix for custom post
246 * Ex:wp_get_archives('&post_type='.get_query_var( 'post_type' ));
247 *
248 */
249
250 public $get_archives_where_r;
251
252 public function get_archives_where( $where, $r ) {
253 $this->get_archives_where_r = $r;
254 if ( isset($r['post_type']) )
255 $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
256
257 return $where;
258 }
259
260 public function get_archives_link( $link ) {
261 if (isset($this->get_archives_where_r['post_type']) and $this->get_archives_where_r['type'] != 'postbypost'){
262 $blog_url = get_bloginfo("url");
263
264 // /archive/%post_id%
265 if($str = rtrim( preg_replace("/%[a-z,_]*%/","",get_option("permalink_structure")) ,'/')) {
266 $ret_link = str_replace($str, '/'.'%link_dir%', $link);
267 }else{
268 $blog_url = rtrim($blog_url,"/");
269 $ret_link = str_replace($blog_url,$blog_url.'/'.'%link_dir%',$link);
270 }
271 $link_dir = $this->get_archives_where_r['post_type'];
272
273 if(!strstr($link,'/date/')){
274 $link_dir = $link_dir .'/date';
275 }
276
277 $ret_link = str_replace('%link_dir%',$link_dir,$ret_link);
278
279 return $ret_link;
280 }
281 return $link;
282 }
283
284 /**
285 *
286 * Add rewrite rules for custom taxonomies.
287 * @since 0.6
288 *
289 */
290 public function add_tax_rewrite() {
291 if(get_option('no_taxonomy_structure'))
292 return false;
293
294 global $wp_rewrite;
295 $taxonomies = get_taxonomies(array( '_builtin' => false));
296 if(empty($taxonomies))
297 return false;
298
299 foreach ($taxonomies as $taxonomy) :
300 $post_types = get_taxonomy($taxonomy)->object_type;
301
302 foreach ($post_types as $post_type):
303 $post_type_obj = get_post_type_object($post_type);
304 $slug = $post_type_obj->rewrite['slug'];
305 if(!$slug) {
306 $slug = $post_type;
307 }
308
309 if(is_string($post_type_obj->has_archive)){
310 $slug = $post_type_obj->has_archive;
311 };
312
313 //add taxonomy slug
314 add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/page/?([0-9]{1,})/?$', 'index.php?'.$taxonomy.'=$matches[1]&paged=$matches[2]', 'top' );
315 add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$taxonomy.'=$matches[1]&feed=$matches[2]', 'top' );
316 add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$taxonomy.'=$matches[1]&feed=$matches[2]', 'top' );
317 add_rewrite_rule( $slug.'/'.$taxonomy.'/(.+?)/?$', 'index.php?'.$taxonomy.'=$matches[1]', 'top' );
318
319 endforeach;
320 endforeach;
321 }
322
323
324 /**
325 *
326 * Fix taxonomy link outputs.
327 * @since 0.6
328 *
329 */
330 public function set_term_link( $termlink, $term, $taxonomy ) {
331 if( get_option('no_taxonomy_structure') )
332 return $termlink;
333
334 $taxonomy = get_taxonomy($taxonomy);
335 if( $taxonomy->_builtin )
336 return $termlink;
337
338 if( empty($taxonomy) )
339 return $termlink;
340
341 $wp_home = rtrim( get_option('home'), '/' );
342
343 $post_type = $taxonomy->object_type[0];
344 $slug = get_post_type_object($post_type)->rewrite['slug'];
345
346
347 //$termlink = str_replace( $term->slug.'/', $this->get_taxonomy_parents( $term->term_id,$taxonomy->name, false, '/', true ), $termlink );
348 $termlink = str_replace( $wp_home, $wp_home.'/'.$slug, $termlink );
349 $str = rtrim( preg_replace("/%[a-z_]*%/","",get_option("permalink_structure")) ,'/');
350 return str_replace($str, "", $termlink );
351
352 }
353 }
354
355 class Custom_Post_Type_Permalinks_Admin {
356
357 public function __construct () {
358 add_action('init', array(&$this,'load_textdomain'));
359 add_action('init', array(&$this, 'update_rules'));
360 add_action('admin_init', array(&$this,'settings_api_init'),30);
361 }
362
363 public function load_textdomain() {
364 load_plugin_textdomain('cptp',false,'custom-post-type-permalinks');
365 }
366
367 /**
368 *
369 * Add hook flush_rules
370 * @since 0.7.9
371 *
372 */
373 public function update_rules() {
374
375 $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
376 $type_count = count($post_types);
377 $i = 0;
378 foreach ($post_types as $post_type):
379 add_action('update_option_'.$post_type.'_structure',array(&$this,'flush_rules'),10,2);
380 endforeach;
381 add_action('update_option_no_taxonomy_structure',array(&$this,'flush_rules'),10,2);
382
383
384 }
385
386 /**
387 * Flush rules
388 *
389 * @since 0.7.9
390 *
391 */
392
393 public function flush_rules(){
394 Custom_Post_Type_Permalinks::add_tax_rewrite();
395 Custom_Post_Type_Permalinks::set_archive_rewrite();
396 Custom_Post_Type_Permalinks::set_rewrite();
397 flush_rewrite_rules();
398 }
399
400
401
402 public function settings_api_init() {
403 add_settings_section('cptp_setting_section',
404 __("Permalink Setting for custom post type",'cptp'),
405 array(&$this,'setting_section_callback_function'),
406 'permalink'
407 );
408
409 $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
410 foreach ($post_types as $post_type):
411 if(isset($_POST['submit'])){
412 if( strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) {
413
414 $structure = trim(esc_attr($_POST[$post_type.'_structure']));#get setting
415
416 #default permalink structure
417 if( !$structure )
418 $structure = Custom_Post_Type_Permalinks::$default_structure;
419
420 $structure = str_replace('//','/','/'.$structure);# first "/"
421
422 #last "/"
423 $lastString = substr(trim(esc_attr($_POST['permalink_structure'])),-1);
424 $structure = rtrim($structure,'/');
425
426 if ( $lastString == '/')
427 $structure = $structure.'/';
428
429 update_option($post_type.'_structure', $structure );
430 }
431 }
432
433 add_settings_field($post_type.'_structure',
434 $post_type,
435 array(&$this,'setting_structure_callback_function'),
436 'permalink',
437 'cptp_setting_section',
438 $post_type.'_structure'
439 );
440
441 register_setting('permalink',$post_type.'_structure');
442 endforeach;
443
444 add_settings_field(
445 'no_taxonomy_structure',
446 __("Use custom permalink of custom taxonomy archive.",'cptp'),
447 array(&$this,'setting_no_tax_structure_callback_function'),
448 'permalink',
449 'cptp_setting_section'
450 );
451
452 register_setting('permalink','no_taxonomy_structure');
453
454 if(isset($_POST['submit']) && isset($_POST['_wp_http_referer']) && strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) {
455
456 if(!isset($_POST['no_taxonomy_structure'])){
457 $set = true;
458 }else {
459 $set = false;
460 }
461 update_option('no_taxonomy_structure', $set);
462 }
463 }
464
465 public function setting_section_callback_function() {
466 ?>
467 <p><?php _e("Setting permalinks of custom post type.",'cptp');?><br />
468 <?php _e("The tags you can use is WordPress Structure Tags and '%\"custom_taxonomy_slug\"%'. (e.g. %actors%)",'cptp');?><br />
469 <?php _e("%\"custom_taxonomy_slug\"% is replaced the taxonomy's term.'.",'cptp');?></p>
470
471 <p><?php _e("Presence of the trailing '/' is unified into a standard permalink structure setting.",'cptp');?><br />
472 <?php _e("If you don't entered permalink structure, permalink is configured /%postname%/'.",'cptp');?>
473 </p>
474 <?php
475 }
476
477 public function setting_structure_callback_function( $option ) {
478 $post_type = str_replace('_structure',"" ,$option);
479 $slug = get_post_type_object($post_type)->rewrite['slug'];
480 if( !$slug )
481 $slug = $post_type;
482
483 echo '/'.$slug.' <input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . get_option($option) .'" />';
484 }
485
486 public function setting_no_tax_structure_callback_function(){
487 echo '<input name="no_taxonomy_structure" id="no_taxonomy_structure" type="checkbox" value="1" class="code" ' . checked( false, get_option('no_taxonomy_structure'),false) . ' /> ';
488 _e("If you check,The custom taxonomy's permalinks is example.com/post_type/taxonomy/term.","cptp");
489 }
490 }
491
492 new Custom_Post_Type_Permalinks;
493 new Custom_Post_Type_Permalinks_Admin;
494