Custom Permalinks - Version 0.3

Version Description

Download this release

Release Info

Developer michaeltyson
Plugin Icon Custom Permalinks
Version 0.3
Comparing to
See all releases

Code changes from version 0.2.2 to 0.3

Files changed (2) hide show
  1. custom-permalinks.php +104 -27
  2. readme.txt +3 -1
custom-permalinks.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Custom Permalinks
4
  Plugin URI: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
5
  Donate link: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
6
  Description: Set custom permalinks on a per-post basis
7
- Version: 0.2.2
8
  Author: Michael Tyson
9
  Author URI: http://michael.tyson.id.au
10
  */
@@ -77,30 +77,39 @@ function custom_permalinks_term_link($permalink, $term) {
77
  function custom_permalinks_redirect() {
78
 
79
  // Get request URI, strip parameters
80
- $request = trim($_SERVER['REQUEST_URI'],'/');
81
  if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);
82
 
83
  global $wp_query;
84
- $post = $wp_query->post;
85
- if ( !$post ) return;
86
 
 
 
 
 
87
  if ( is_single() ) {
 
88
  $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
89
- if ( $custom_permalink && $custom_permalink != $request ) {
90
- // There's a post with a matching custom permalink
91
- wp_redirect( get_option('home')."/".$custom_permalink, 301 );
92
- exit();
93
- }
94
  } else if ( is_tag() || is_category() ) {
95
  $theTerm = $wp_query->get_queried_object();
96
- $permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
97
-
98
- if ( $permalink && $permalink != $request ) {
99
- // The current term has a permalink that isn't where we're currently at
100
- wp_redirect( get_option('home')."/".$permalink, 301 );
101
- exit();
102
- }
103
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  }
105
 
106
  /**
@@ -110,31 +119,98 @@ function custom_permalinks_redirect() {
110
  * @since 0.1
111
  */
112
  function custom_permalinks_request($query) {
 
 
 
 
 
 
 
113
 
114
  // Get request URI, strip parameters and /'s
115
  $request = (($pos=strpos($_SERVER['REQUEST_URI'], '?')) ? substr($_SERVER['REQUEST_URI'], 0, $pos) : $_SERVER['REQUEST_URI']);
116
- $request = preg_replace('@/+@','/', ltrim($request, '/'));
117
 
118
  if ( !$request ) return $query;
119
 
120
- $posts = get_posts( array('meta_key' => 'custom_permalink', 'meta_value' => $request) );
121
- if ( !$posts && $request{strlen($request)-1} != '/' ) // Try adding trailing /
122
- $posts = get_posts( array('meta_key' => 'custom_permalink', 'meta_value' => $request.'/') );
123
- if ( !$posts && $request{strlen($request)-1} == '/' ) // Try removing trailing /
124
- $posts = get_posts( array('meta_key' => 'custom_permalink', 'meta_value' => rtrim($request,'/')) );
 
 
 
125
  if ( $posts ) {
126
  // A post matches our request
127
- return array('p' => $posts[0]->ID);
 
 
 
 
 
 
 
128
  }
129
 
130
- $table = get_option('custom_permalink_table');
131
- if ( $table && ($term = $table[$request]) || ($term = $table[$request.'/']) || $term = $table[rtrim($request,'/')] ) {
132
- return array(($term['kind'] == 'category' ? 'category_name' : 'tag') => $term['slug']);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  }
134
-
135
  return $query;
136
  }
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
 
140
  /**
@@ -511,6 +587,7 @@ add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
511
  add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
512
  add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
513
  add_filter( 'request', 'custom_permalinks_request', 10, 1 );
 
514
 
515
  add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
516
  add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
4
  Plugin URI: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
5
  Donate link: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
6
  Description: Set custom permalinks on a per-post basis
7
+ Version: 0.3
8
  Author: Michael Tyson
9
  Author URI: http://michael.tyson.id.au
10
  */
77
  function custom_permalinks_redirect() {
78
 
79
  // Get request URI, strip parameters
80
+ $request = ltrim($_SERVER['REQUEST_URI'],'/');
81
  if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);
82
 
83
  global $wp_query;
 
 
84
 
85
+ $custom_permalink = '';
86
+ $original_permalink = '';
87
+
88
+ // If the post/tag/category we're on has a custom permalink, get it and check against the request
89
  if ( is_single() ) {
90
+ $post = $wp_query->post;
91
  $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
92
+ $original_permalink = custom_permalinks_original_post_link( $post->ID );
 
 
 
 
93
  } else if ( is_tag() || is_category() ) {
94
  $theTerm = $wp_query->get_queried_object();
95
+ $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
96
+ $original_permalink = (is_tag() ? custom_permalinks_original_tag_link($theTerm->term_id) :
97
+ custom_permalinks_original_category_link($theTerm->term_id));
 
 
 
 
98
  }
99
+
100
+ if ( $custom_permalink &&
101
+ (substr($request, 0, strlen($custom_permalink)) != $custom_permalink ||
102
+ $request == $custom_permalink."/" ) ) {
103
+ // Request doesn't match permalink - redirect
104
+ $url = $custom_permalink;
105
+ if ( substr($request, 0, strlen($original_permalink)) == $original_permalink &&
106
+ trim($request,'/') != trim($original_permalink,'/') ) {
107
+ // This is the original link; we can use this url to derive the new one
108
+ $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request));
109
+ }
110
+ wp_redirect( get_option('home')."/".$url, 301 );
111
+ exit();
112
+ }
113
  }
114
 
115
  /**
119
  * @since 0.1
120
  */
121
  function custom_permalinks_request($query) {
122
+ global $wpdb;
123
+ global $_CPRegisteredURL;
124
+
125
+ // First, search for a matching custom permalink, and if found, generate the corresponding
126
+ // original URL
127
+
128
+ $originalUrl = '';
129
 
130
  // Get request URI, strip parameters and /'s
131
  $request = (($pos=strpos($_SERVER['REQUEST_URI'], '?')) ? substr($_SERVER['REQUEST_URI'], 0, $pos) : $_SERVER['REQUEST_URI']);
132
+ $request = preg_replace('@/+@','/', trim($request, '/'));
133
 
134
  if ( !$request ) return $query;
135
 
136
+ $sql = "SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value FROM $wpdb->posts ".
137
+ " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
138
+ " meta_key = 'custom_permalink' AND ".
139
+ " meta_value != '' AND ".
140
+ " ( meta_value = LEFT('".mysql_escape_string($request)."', LENGTH(meta_value)) OR ".
141
+ " meta_value = LEFT('".mysql_escape_string($request."/")."', LENGTH(meta_value)) )";
142
+
143
+ $posts = $wpdb->get_results($sql);
144
  if ( $posts ) {
145
  // A post matches our request
146
+
147
+ // Preserve this url for later if it's the same as the permalink (no extra stuff)
148
+ if ( trim($request,'/') == trim($posts[0]->meta_value,'/') )
149
+ $_CPRegisteredURL = $request;
150
+
151
+ $originalUrl = str_replace(trim($posts[0]->meta_value,'/'),
152
+ custom_permalinks_original_post_link($posts[0]->ID),
153
+ trim($request,'/'));
154
  }
155
 
156
+ if ( !$originalUrl ) {
157
+ $table = get_option('custom_permalink_table');
158
+ if ( !$table ) return $query;
159
+
160
+ foreach ( array_keys($table) as $permalink ) {
161
+ if ( $permalink == substr($request, 0, strlen($permalink)) || $permalink == substr($request."/", 0, strlen($permalink)) ) {
162
+ $term = $table[$permalink];
163
+
164
+ // Preserve this url for later if it's the same as the permalink (no extra stuff)
165
+ if ( trim($request,'/') == trim($permalink,'/') )
166
+ $_CPRegisteredURL = $request;
167
+
168
+
169
+ if ( $term['kind'] == 'category ') {
170
+ $originalUrl = str_replace(trim($permalink,'/'),
171
+ custom_permalinks_original_category_link($term['id']),
172
+ trim($request,'/'));
173
+ } else {
174
+ $originalUrl = str_replace(trim($permalink,'/'),
175
+ custom_permalinks_original_tag_link($term['id']),
176
+ trim($request,'/'));
177
+ }
178
+ }
179
+ }
180
+ }
181
+
182
+ if ( $originalUrl ) {
183
+ $originalUrl = preg_replace("@//*@", "/", $originalUrl);
184
+
185
+ // Now we have the original URL, run this back through WP->parse_request, in order to
186
+ // parse parameters properly. We set $_SERVER variables to fool the function.
187
+ $oldPathInfo = $_SERVER["PATH_INFO"]; $oldRequestUri = $_SERVER["REQUEST_URI"];
188
+ $_SERVER["PATH_INFO"] = $originalUrl; $_SERVER["REQUEST_URI"] = $originalUrl;
189
+
190
+ remove_filter( 'request', 'custom_permalinks_request', 10, 1 );
191
+ global $wp;
192
+ $wp->parse_request();
193
+ $query = $wp->query_vars;
194
+ add_filter( 'request', 'custom_permalinks_request', 10, 1 );
195
+
196
+ $_SERVER["PATH_INFO"] = $oldPathInfo; $_SERVER["REQUEST_URI"] = $oldRequestUri;
197
  }
 
198
  return $query;
199
  }
200
 
201
+ /**
202
+ * Filter to handle trailing slashes correctly
203
+ *
204
+ * @package CustomPermalinks
205
+ * @since 0.3
206
+ */
207
+ function custom_permalinks_trailingslash($string, $type) {
208
+ global $_CPRegisteredURL;
209
+ if ( trim($_CPRegisteredURL,'/') == trim($string,'/') ) {
210
+ return $_CPRegisteredURL;
211
+ }
212
+ return $string;
213
+ }
214
 
215
 
216
  /**
587
  add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
588
  add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
589
  add_filter( 'request', 'custom_permalinks_request', 10, 1 );
590
+ add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
591
 
592
  add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
593
  add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
4
  Tags: permalink, url, link, address, custom, redirect
5
  Requires at least: 2.6
6
  Tested up to: 2.6.2
7
- Stable tag: 0.2.2
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
@@ -23,6 +23,8 @@ over your site structure.
23
 
24
  == Changelog ==
25
 
 
 
26
  0.2.2: Fixed bug with not matching permalinks when / appended to the URL, and workaround for infinite redirect when another plugin is enforcing trailing /
27
 
28
  0.2.1: Better handling of trailing slashes
4
  Tags: permalink, url, link, address, custom, redirect
5
  Requires at least: 2.6
6
  Tested up to: 2.6.2
7
+ Stable tag: 0.3
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
23
 
24
  == Changelog ==
25
 
26
+ 0.3: Largely rewritten to provide more robust handling of trailing slashes, proper support for trailing URL components (eg. paging)
27
+
28
  0.2.2: Fixed bug with not matching permalinks when / appended to the URL, and workaround for infinite redirect when another plugin is enforcing trailing /
29
 
30
  0.2.1: Better handling of trailing slashes