PluginProbe ʕ •ᴥ•ʔ
Custom Post Type Permalinks / 0.7.4
Custom Post Type Permalinks v0.7.4
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 14 years ago cptp-ja.po 14 years ago custom-post-type-permalinks.php 14 years ago readme.txt 14 years ago screenshot-1.png 14 years ago
custom-post-type-permalinks.php
395 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.4
9 Text Domain: cptp
10 Domain Path: /
11 */
12
13
14
15 /* Copyright 2011 Toro_Unit (email : mail@torounit.com)
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 */
31
32
33
34
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('wp_loaded', 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
85 foreach ( $post_types as $post_type ):
86 if( !$post_type ) continue;
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
102 add_rewrite_rule($slug.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,'top');
103 add_rewrite_rule($slug.'/author/([^/]+)/?$','index.php?author=$matches[1]&post_type='.$post_type,'top');
104 add_rewrite_rule($slug.'/?$','index.php?post_type='.$post_type,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
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,'top');
118 add_rewrite_rule($post_type.'/date/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$post_type,'top');
119 add_rewrite_rule($post_type.'/date/([0-9]{4})/?$','index.php?author=$matches[1]&post_type='.$post_type,'top');
120 add_rewrite_rule($post_type.'/?$','index.php?post_type='.$post_type,'top');
121 endforeach;
122 }
123 //rewrite_tagの追加
124 function set_rewrite() {
125 global $wp_rewrite;
126
127 $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true,'show_ui' => true) );
128
129 foreach ( $post_types as $post_type ):
130
131 $permalink = get_option($post_type.'_structure');
132
133 if( !$permalink ){
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
140 $slug = get_post_type_object($post_type)->rewrite['slug'];
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, "_builtin" => false),'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' ) );
167 if( $draft_or_pending and !$leavename )
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);
174 $permalink = str_replace('%'.$post_type.'_id%', $post->ID, $permalink);
175 $permalink = str_replace('%'.$post_type.'_page%', "", $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 ) {
183 if ( strpos($permalink, "%$taxonomy%") !== false ) {
184 $terms = get_the_terms($post->ID,$taxonomy);
185
186 if ( $terms ) {
187 usort($terms, '_usort_terms_by_ID'); // order by ID
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);
205 $permalink = str_replace("%hour%",date("H",$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 *
216 *wp_get_archives fix for custom post
217 *
218 *How To Use:
219 *
220 * $arg = 'type=monthly';
221 * if(is_post_type_archive()) {
222 * $arg .= '&post_type='.get_query_var( 'post_type' );
223 * }
224 * wp_get_archives($arg);
225 *
226 *
227 *
228 */
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
236 $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
237 }else {
238 $this->post_type_archives = '';
239 }
240 return $where;
241 }
242
243 function get_archives_link( $link_html ) {
244 if ( '' != $this->post_type_archives ){
245 $blog_url = get_bloginfo("url");
246 $blog_url = rtrim($blog_url,"/");
247 $link_html = str_replace($blog_url,$blog_url.'/'.$this->post_type_archives.'/date',$link_html);
248 }
249
250 return $link_html;
251 }
252
253 /**
254 *
255 *fix permalink custom taxonomy
256 *
257 *Ex:
258 * example.org/posttype/taxonomy/term/
259 *
260 */
261
262 function add_tax_rewrite() {
263 global $wp_rewrite;
264 $taxonomies = get_taxonomies(array( '_builtin' => false));
265 if(empty($taxonomies)){
266 return ;
267 }
268
269 foreach ($taxonomies as $taxonomy) :
270 $post_types = get_taxonomy($taxonomy)->object_type;
271 foreach ($post_types as $post_type){
272 $slug = get_post_type_object($post_type)->rewrite["slug"];
273 //add taxonomy slug
274 add_rewrite_rule($slug.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]','top');
275 add_rewrite_rule($post_type.'/'.$taxonomy.'/(.+?)/?$','index.php?taxonomy='.$taxonomy.'&term=$matches[1]','top');
276 }
277 endforeach;
278 }
279
280 function set_term_link( $termlink, $term, $taxonomy ) {
281 $taxonomy = get_taxonomy($taxonomy);
282 if( $taxonomy->_builtin ) {
283 return $termlink;
284 }
285
286 if(empty($taxonomy)){
287 return $termlink;
288 }
289 $wp_home = get_option('home');
290 $wp_home = $wp_home.'/';
291 $wp_home = str_replace('//','/',$wp_home);
292 $wp_home = str_replace(':/', '://', $wp_home);
293 $post_type = $taxonomy->object_type[0];
294 $slug = get_post_type_object($post_type)->rewrite['slug'];
295 return str_replace($wp_home,$wp_home.$slug.'/',$termlink);
296 }
297
298 static function uninstall_hook_custom_permalink () {
299 $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
300 foreach ( $post_types as $post_type ):
301 delete_option( $post_type.'_structure' );
302 endforeach;
303 }
304
305 }
306
307 class Custom_Post_Type_Permalinks_Admin {
308
309 function add_hooks() {
310 add_action('init', array(&$this,'load_textdomain'));
311 add_action('admin_init', array(&$this,'settings_api_init'),10);
312 add_action('admin_init', array(&$this,'set_rules'),50);
313 }
314
315 function load_textdomain() {
316 load_plugin_textdomain('cptp',false,'custom-post-type-permalinks');
317 }
318
319 function settings_api_init() {
320 add_settings_section('setting_section',
321 __("Permalink Setting for custom post type",'cptp'),
322 array(&$this,'setting_section_callback_function'),
323 'permalink');
324
325 $post_types = get_post_types( array('_builtin'=>false, 'publicly_queryable'=>true, 'show_ui' => true) );
326 foreach ($post_types as $post_type):
327 if(isset($_POST['submit'])){
328 if( strpos($_POST['_wp_http_referer'],'options-permalink.php') !== FALSE ) {
329
330 $structure = trim(esc_attr($_POST[$post_type.'_structure']));#get setting
331
332 #default permalink structure
333 if( !$structure ) {
334 $structure = '/%year%/%monthnum%/%day%/%post_id%/';
335 }
336 $structure = str_replace('//','/','/'.$structure);# first "/"
337
338 #last "/"
339 $lastString = substr(trim(esc_attr($_POST['permalink_structure'])),-1);
340 $structure = rtrim($structure,'/');
341 if ( $lastString == '/') {
342 $structure = $structure.'/';
343 }
344
345 update_option($post_type.'_structure', $structure );
346 }
347
348 }
349
350 add_settings_field($post_type.'_structure',
351 $post_type,
352 array(&$this,'setting_callback_function'),
353 'permalink',
354 'setting_section',
355 $post_type.'_structure');
356
357 register_setting('permalink',$post_type.'_structure');
358 endforeach;
359 }
360
361 function set_rules() {
362 global $wp_rewrite;
363 $wp_rewrite->flush_rules();
364 }
365
366 function setting_section_callback_function() {
367 ?>
368 <p><?php _e("Setting permalinks of custom post type.",'cptp');?><br />
369 <?php _e("The tags you can use is WordPress Structure Tags and '%{custom_taxonomy_slug}%'.",'cptp');?><br />
370 <?php _e("%{custom_taxonomy_slug}% is replaced the taxonomy's term.'.",'cptp');?></p>
371
372 <p><?php _e("Presence of the trailing '/' is unified into a standard permalink structure setting.",'cptp');?><br />
373 <?php _e("If you don't entered permalink structure, permalink is configured /%year%/%monthnum%/%day%/%post_id%/.",'cptp');?>
374 </p>
375 <?php
376 }
377
378 function setting_callback_function( $option ) {
379 $post_type = str_replace('_structure',"" ,$option);
380 $slug = get_post_type_object($post_type)->rewrite['slug'];
381 if( !$slug ) {
382 $slug = $post_type;
383 }
384 echo '/'.$slug.' <input name="'.$option.'" id="'.$option.'" type="text" class="regular-text code" value="' . get_option($option) .'" />';
385 }
386 }
387
388
389 $custom_post_type_permalinks = new Custom_Post_Type_Permalinks;
390 if(get_option("permalink_structure") != ""){ $custom_post_type_permalinks->add_hooks(); }
391 $custom_post_type_permalinks->uninstall_hook();
392 $custom_post_type_permalinks_admin = new Custom_Post_Type_Permalinks_Admin;
393 $custom_post_type_permalinks_admin->add_hooks();
394
395