Custom Permalinks - Version 0.6

Version Description

  • Fix infinite redirect for permalinks ending in a / (critical fix)
    • Moved post/page permalinks settings to top of edit form, replacing prior permalink display
Download this release

Release Info

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

Code changes from version 0.5.3 to 0.6

Files changed (2) hide show
  1. custom-permalinks.php +55 -15
  2. readme.txt +11 -2
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.5.3
8
  Author: Michael Tyson
9
  Author URI: http://michael.tyson.id.au
10
  */
@@ -119,6 +119,7 @@ function custom_permalinks_redirect() {
119
  $request == $custom_permalink."/" ) ) {
120
  // Request doesn't match permalink - redirect
121
  $url = $custom_permalink;
 
122
  if ( substr($request, 0, strlen($original_permalink)) == $original_permalink &&
123
  trim($request,'/') != trim($original_permalink,'/') ) {
124
  // This is the original link; we can use this url to derive the new one
@@ -149,7 +150,7 @@ function custom_permalinks_request($query) {
149
  $url = parse_url(get_bloginfo('url')); $url = $url['path'];
150
  $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
151
  $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request);
152
- $request = preg_replace('@/+@','/', trim($request, '/'));
153
 
154
  if ( !$request ) return $query;
155
 
@@ -157,8 +158,8 @@ function custom_permalinks_request($query) {
157
  " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
158
  " meta_key = 'custom_permalink' AND ".
159
  " meta_value != '' AND ".
160
- " ( meta_value = LEFT('".mysql_escape_string($request)."', LENGTH(meta_value)) OR ".
161
- " meta_value = LEFT('".mysql_escape_string($request."/")."', LENGTH(meta_value)) ) ".
162
  " ORDER BY LENGTH(meta_value) DESC";
163
 
164
  $posts = $wpdb->get_results($sql);
@@ -167,14 +168,14 @@ function custom_permalinks_request($query) {
167
  // A post matches our request
168
 
169
  // Preserve this url for later if it's the same as the permalink (no extra stuff)
170
- if ( trim($request,'/') == trim($posts[0]->meta_value,'/') )
171
  $_CPRegisteredURL = $request;
172
 
173
  $originalUrl = preg_replace('@/+@', '/', str_replace( trim($posts[0]->meta_value,'/'),
174
  ( $posts[0]->post_type == 'post' ?
175
  custom_permalinks_original_post_link($posts[0]->ID)
176
  : custom_permalinks_original_page_link($posts[0]->ID) ),
177
- trim( $request,'/' ) ));
178
  }
179
 
180
  if ( !$originalUrl ) {
@@ -182,11 +183,11 @@ function custom_permalinks_request($query) {
182
  if ( !$table ) return $query;
183
 
184
  foreach ( array_keys($table) as $permalink ) {
185
- if ( $permalink == substr($request, 0, strlen($permalink)) || $permalink == substr($request."/", 0, strlen($permalink)) ) {
186
  $term = $table[$permalink];
187
 
188
  // Preserve this url for later if it's the same as the permalink (no extra stuff)
189
- if ( trim($request,'/') == trim($permalink,'/') )
190
  $_CPRegisteredURL = $request;
191
 
192
 
@@ -254,15 +255,40 @@ function custom_permalinks_trailingslash($string, $type) {
254
  return $string;
255
  }
256
 
257
-
258
  /**
259
  ** Administration
260
  **
261
  **/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
 
263
 
264
  /**
265
- * Per-post options
266
  *
267
  * @package CustomPermalinks
268
  * @since 0.1
@@ -288,7 +314,7 @@ function custom_permalinks_post_options() {
288
 
289
 
290
  /**
291
- * Per-page options
292
  *
293
  * @package CustomPermalinks
294
  * @since 0.4
@@ -349,25 +375,31 @@ function custom_permalinks_term_options($object) {
349
  * @package CustomPermalinks
350
  * @since 0.1
351
  */
352
- function custom_permalinks_form($permalink, $original="") {
353
  ?>
354
  <input value="true" type="hidden" name="custom_permalinks_edit" />
355
  <input value="<?php echo htmlspecialchars($permalink) ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
356
 
 
357
  <table class="form-table" id="custom_permalink_form">
358
  <tr>
359
  <th scope="row"><?php _e('Custom Permalink', 'custom-permalink') ?></th>
360
  <td>
 
361
  <?php echo get_option('home') ?>/
362
  <input type="text" class="text" value="<?php echo htmlspecialchars($permalink ? $permalink : $original) ?>"
363
  style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>"
364
  onfocus="if ( this.value == '<?php echo htmlspecialchars($original) ?>' ) { this.value = ''; this.style.color = '#000'; }"
365
- onblur="document.getElementById('custom_permalink').value = this.value; if ( this.value == '' ) { this.value = '<?php echo htmlspecialchars($original) ?>'; this.style.color = '#ddd'; }"/><br />
 
 
366
  <small><?php _e('Leave blank to disable', 'custom-permalink') ?></small>
 
367
  </td>
368
  </tr>
369
  </table>
370
  <?php
 
371
  }
372
 
373
 
@@ -667,6 +699,8 @@ function custom_permalinks_setup_admin() {
667
  }
668
 
669
 
 
 
670
  add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
671
  add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
672
  add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
@@ -675,12 +709,18 @@ add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
675
  add_filter( 'request', 'custom_permalinks_request', 10, 1 );
676
  add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
677
 
678
- add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
679
- add_action( 'edit_page_form', 'custom_permalinks_page_options' );
 
 
 
 
 
680
  add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
681
  add_action( 'add_tag_form', 'custom_permalinks_term_options' );
682
  add_action( 'edit_category_form', 'custom_permalinks_term_options' );
683
  add_action( 'save_post', 'custom_permalinks_save_post' );
 
684
  add_action( 'edited_post_tag', 'custom_permalinks_save_tag' );
685
  add_action( 'edited_category', 'custom_permalinks_save_category' );
686
  add_action( 'create_post_tag', 'custom_permalinks_save_tag' );
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.6
8
  Author: Michael Tyson
9
  Author URI: http://michael.tyson.id.au
10
  */
119
  $request == $custom_permalink."/" ) ) {
120
  // Request doesn't match permalink - redirect
121
  $url = $custom_permalink;
122
+
123
  if ( substr($request, 0, strlen($original_permalink)) == $original_permalink &&
124
  trim($request,'/') != trim($original_permalink,'/') ) {
125
  // This is the original link; we can use this url to derive the new one
150
  $url = parse_url(get_bloginfo('url')); $url = $url['path'];
151
  $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
152
  $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request);
153
+ $request_noslash = preg_replace('@/+@','/', trim($request, '/'));
154
 
155
  if ( !$request ) return $query;
156
 
158
  " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
159
  " meta_key = 'custom_permalink' AND ".
160
  " meta_value != '' AND ".
161
+ " ( meta_value = LEFT('".mysql_escape_string($request_noslash)."', LENGTH(meta_value)) OR ".
162
+ " meta_value = LEFT('".mysql_escape_string($request_noslash."/")."', LENGTH(meta_value)) ) ".
163
  " ORDER BY LENGTH(meta_value) DESC";
164
 
165
  $posts = $wpdb->get_results($sql);
168
  // A post matches our request
169
 
170
  // Preserve this url for later if it's the same as the permalink (no extra stuff)
171
+ if ( $request_noslash == trim($posts[0]->meta_value,'/') )
172
  $_CPRegisteredURL = $request;
173
 
174
  $originalUrl = preg_replace('@/+@', '/', str_replace( trim($posts[0]->meta_value,'/'),
175
  ( $posts[0]->post_type == 'post' ?
176
  custom_permalinks_original_post_link($posts[0]->ID)
177
  : custom_permalinks_original_page_link($posts[0]->ID) ),
178
+ $request_noslash ));
179
  }
180
 
181
  if ( !$originalUrl ) {
183
  if ( !$table ) return $query;
184
 
185
  foreach ( array_keys($table) as $permalink ) {
186
+ if ( $permalink == substr($request_noslash, 0, strlen($permalink)) || $permalink == substr($request_noslash."/", 0, strlen($permalink)) ) {
187
  $term = $table[$permalink];
188
 
189
  // Preserve this url for later if it's the same as the permalink (no extra stuff)
190
+ if ( $request_noslash == trim($permalink,'/') )
191
  $_CPRegisteredURL = $request;
192
 
193
 
255
  return $string;
256
  }
257
 
 
258
  /**
259
  ** Administration
260
  **
261
  **/
262
+
263
+ /**
264
+ * Per-post/page options (Wordpress > 2.9)
265
+ *
266
+ * @package CustomPermalinks
267
+ * @since 0.6
268
+ */
269
+ function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
270
+ $permalink = get_post_meta( $id, 'custom_permalink', true );
271
+ $post = &get_post($id);
272
+
273
+ ob_start();
274
+ ?>
275
+ <?php custom_permalinks_form($permalink, ($post->post_type == "post" ? custom_permalinks_original_post_link($id) : custom_permalinks_original_page_link($id)), false); ?>
276
+ <?php
277
+ $content = ob_get_contents();
278
+ ob_end_clean();
279
+
280
+ if ( 'publish' == $post->post_status ) {
281
+ $view_post = 'post' == $post->post_type ? __('View Post') : __('View Page');
282
+ }
283
+
284
+ list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
285
+ return '<strong>' . __('Permalink:') . "</strong>\n" . $content .
286
+ ( isset($view_post) ? "<span id='view-post-btn'><a href='$permalink' class='button' target='_blank'>$view_post</a></span>\n" : "" );
287
+ }
288
 
289
 
290
  /**
291
+ * Per-post options (Wordpress < 2.9)
292
  *
293
  * @package CustomPermalinks
294
  * @since 0.1
314
 
315
 
316
  /**
317
+ * Per-page options (Wordpress < 2.9)
318
  *
319
  * @package CustomPermalinks
320
  * @since 0.4
375
  * @package CustomPermalinks
376
  * @since 0.1
377
  */
378
+ function custom_permalinks_form($permalink, $original="", $renderContainers=true) {
379
  ?>
380
  <input value="true" type="hidden" name="custom_permalinks_edit" />
381
  <input value="<?php echo htmlspecialchars($permalink) ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
382
 
383
+ <?php if ( $renderContainers ) : ?>
384
  <table class="form-table" id="custom_permalink_form">
385
  <tr>
386
  <th scope="row"><?php _e('Custom Permalink', 'custom-permalink') ?></th>
387
  <td>
388
+ <?php endif; ?>
389
  <?php echo get_option('home') ?>/
390
  <input type="text" class="text" value="<?php echo htmlspecialchars($permalink ? $permalink : $original) ?>"
391
  style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>"
392
  onfocus="if ( this.value == '<?php echo htmlspecialchars($original) ?>' ) { this.value = ''; this.style.color = '#000'; }"
393
+ onblur="document.getElementById('custom_permalink').value = this.value; if ( this.value == '' ) { this.value = '<?php echo htmlspecialchars($original) ?>'; this.style.color = '#ddd'; }"/>
394
+ <?php if ( $renderContainers ) : ?>
395
+ <br />
396
  <small><?php _e('Leave blank to disable', 'custom-permalink') ?></small>
397
+
398
  </td>
399
  </tr>
400
  </table>
401
  <?php
402
+ endif;
403
  }
404
 
405
 
699
  }
700
 
701
 
702
+ $v = explode('.', get_bloginfo('version'));
703
+
704
  add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
705
  add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
706
  add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
709
  add_filter( 'request', 'custom_permalinks_request', 10, 1 );
710
  add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
711
 
712
+ if ( $v[0] >= 2 ) {
713
+ add_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 10, 4 );
714
+ } else {
715
+ add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
716
+ add_action( 'edit_page_form', 'custom_permalinks_page_options' );
717
+ }
718
+
719
  add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
720
  add_action( 'add_tag_form', 'custom_permalinks_term_options' );
721
  add_action( 'edit_category_form', 'custom_permalinks_term_options' );
722
  add_action( 'save_post', 'custom_permalinks_save_post' );
723
+ add_action( 'save_page', 'custom_permalinks_save_post' );
724
  add_action( 'edited_post_tag', 'custom_permalinks_save_tag' );
725
  add_action( 'edited_category', 'custom_permalinks_save_category' );
726
  add_action( 'create_post_tag', 'custom_permalinks_save_tag' );
readme.txt CHANGED
@@ -3,8 +3,8 @@
3
  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.8.4
7
- Stable tag: 0.5.3
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
@@ -23,6 +23,11 @@ over your site structure.
23
 
24
  == Changelog ==
25
 
 
 
 
 
 
26
  = 0.5.3 =
27
 
28
  * Fix for invalid URL redirect (eg. http://domain.comfolder/file.html instead of http://domain.com/folder/file.html) when using permalinks without a trailing slash (like .../%postname%.html)
@@ -76,6 +81,10 @@ over your site structure.
76
 
77
  == Changelog ==
78
 
 
 
 
 
79
  = 0.5.3 =
80
 
81
  If you are having problems with Custom Permalinks causing an invalid URL redirect (eg. http://domain.comfolder/file.html instead of http://domain.com/folder/file.html),
3
  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.9.2
7
+ Stable tag: 0.6
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
23
 
24
  == Changelog ==
25
 
26
+ = 0.6 =
27
+
28
+ * Fix infinite redirect for permalinks ending in a / (critical fix)
29
+ * Moved post/page permalinks settings to top of edit form, replacing prior permalink display
30
+
31
  = 0.5.3 =
32
 
33
  * Fix for invalid URL redirect (eg. http://domain.comfolder/file.html instead of http://domain.com/folder/file.html) when using permalinks without a trailing slash (like .../%postname%.html)
81
 
82
  == Changelog ==
83
 
84
+ = 0.6 =
85
+
86
+ In the process of fixing one issue, version 0.5.3 broke permalinks ending with a "/". Update now to fix this, and sorry for the inconvenience!
87
+
88
  = 0.5.3 =
89
 
90
  If you are having problems with Custom Permalinks causing an invalid URL redirect (eg. http://domain.comfolder/file.html instead of http://domain.com/folder/file.html),