Custom Permalinks - Version 0.7.20

Version Description

  • Addressed a noisy warning
    • Revised addition of admin forms js (don't use is_admin())
    • Updated Roles and Capabilities from depreciated numerical to label capabilities (by OF-6)
    • Added css/html to match WP 3.5+ layout (by OF-6)
Download this release

Release Info

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

Code changes from version 0.7.19 to 0.7.20

Files changed (2) hide show
  1. custom-permalinks.php +513 -502
  2. readme.txt +9 -2
custom-permalinks.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Custom Permalinks
4
  Plugin URI: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
5
  Donate link: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
6
  Description: Set custom permalinks on a per-post basis
7
- Version: 0.7.19
8
  Author: Michael Tyson
9
  Author URI: http://atastypixel.com/blog
10
  */
@@ -39,12 +39,12 @@ Author URI: http://atastypixel.com/blog
39
  * @since 0.1
40
  */
41
  function custom_permalinks_post_link($permalink, $post) {
42
- $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
43
- if ( $custom_permalink ) {
44
- return home_url()."/".$custom_permalink;
45
- }
46
-
47
- return $permalink;
48
  }
49
 
50
 
@@ -55,12 +55,12 @@ function custom_permalinks_post_link($permalink, $post) {
55
  * @since 0.4
56
  */
57
  function custom_permalinks_page_link($permalink, $page) {
58
- $custom_permalink = get_post_meta( $page, 'custom_permalink', true );
59
- if ( $custom_permalink ) {
60
- return home_url()."/".$custom_permalink;
61
- }
62
-
63
- return $permalink;
64
  }
65
 
66
 
@@ -71,16 +71,16 @@ function custom_permalinks_page_link($permalink, $page) {
71
  * @since 0.1
72
  */
73
  function custom_permalinks_term_link($permalink, $term) {
74
- $table = get_option('custom_permalink_table');
75
- if ( is_object($term) ) $term = $term->term_id;
76
-
77
- $custom_permalink = custom_permalinks_permalink_for_term($term);
78
-
79
- if ( $custom_permalink ) {
80
- return home_url()."/".$custom_permalink;
81
- }
82
-
83
- return $permalink;
84
  }
85
 
86
 
@@ -91,49 +91,49 @@ function custom_permalinks_term_link($permalink, $term) {
91
  * @since 0.1
92
  */
93
  function custom_permalinks_redirect() {
94
-
95
- // Get request URI, strip parameters
96
- $url = parse_url(get_bloginfo('url'));
97
- $url = isset($url['path']) ? $url['path'] : '';
98
- $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
99
- if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);
100
-
101
- global $wp_query;
102
-
103
- $custom_permalink = '';
104
- $original_permalink = '';
105
-
106
- // If the post/tag/category we're on has a custom permalink, get it and check against the request
107
- if ( is_single() || is_page() ) {
108
- $post = $wp_query->post;
109
- $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
110
- $original_permalink = ( $post->post_type == 'page' ? custom_permalinks_original_page_link( $post->ID ) : custom_permalinks_original_post_link( $post->ID ) );
111
- } else if ( is_tag() || is_category() ) {
112
- $theTerm = $wp_query->get_queried_object();
113
- $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
114
- $original_permalink = (is_tag() ? custom_permalinks_original_tag_link($theTerm->term_id) :
115
- custom_permalinks_original_category_link($theTerm->term_id));
116
- }
117
-
118
- if ( $custom_permalink &&
119
- (substr($request, 0, strlen($custom_permalink)) != $custom_permalink ||
120
- $request == $custom_permalink."/" ) ) {
121
- // Request doesn't match permalink - redirect
122
- $url = $custom_permalink;
123
-
124
- if ( substr($request, 0, strlen($original_permalink)) == $original_permalink &&
125
- trim($request,'/') != trim($original_permalink,'/') ) {
126
- // This is the original link; we can use this url to derive the new one
127
- $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request));
128
- $url = preg_replace('@([^?]*)&@', '\1?', $url);
129
- }
130
-
131
- // Append any query compenent
132
- $url .= strstr($_SERVER['REQUEST_URI'], "?");
133
-
134
- wp_redirect( home_url()."/".$url, 301 );
135
- exit();
136
- }
137
  }
138
 
139
  /**
@@ -143,119 +143,119 @@ function custom_permalinks_redirect() {
143
  * @since 0.1
144
  */
145
  function custom_permalinks_request($query) {
146
- global $wpdb;
147
- global $_CPRegisteredURL;
148
-
149
- // First, search for a matching custom permalink, and if found, generate the corresponding
150
- // original URL
151
-
152
- $originalUrl = NULL;
153
-
154
- // Get request URI, strip parameters and /'s
155
- $url = parse_url(get_bloginfo('url'));
156
- $url = isset($url['path']) ? $url['path'] : '';
157
- $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
158
- $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request);
159
- $request_noslash = preg_replace('@/+@','/', trim($request, '/'));
160
-
161
- if ( !$request ) return $query;
162
-
163
- // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
164
  $sql = $wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts ".
165
- "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
166
- " meta_key = 'custom_permalink' AND ".
167
- " meta_value != '' AND ".
168
- " ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR ".
169
- " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) ".
170
- " AND post_status != 'trash' AND post_type != 'nav_menu_item'".
171
- " ORDER BY LENGTH(meta_value) DESC, ".
172
- " FIELD(post_status,'publish','private','draft','auto-draft','inherit'),".
173
- " FIELD(post_type,'post','page'),".
174
- "$wpdb->posts.ID ASC LIMIT 1",
175
- $request_noslash,
176
- $request_noslash."/"
177
- );
178
-
179
- $posts = $wpdb->get_results($sql);
180
-
181
- if ( $posts ) {
182
- // A post matches our request
183
-
184
- // Preserve this url for later if it's the same as the permalink (no extra stuff)
185
- if ( $request_noslash == trim($posts[0]->meta_value,'/') )
186
- $_CPRegisteredURL = $request;
187
-
188
- $originalUrl = preg_replace( '@/+@', '/', str_replace( trim( strtolower($posts[0]->meta_value),'/' ),
189
- ( $posts[0]->post_type == 'page' ?
190
- custom_permalinks_original_page_link($posts[0]->ID)
191
- : custom_permalinks_original_post_link($posts[0]->ID) ),
192
- strtolower($request_noslash) ) );
193
- }
194
-
195
- if ( $originalUrl === NULL ) {
196
- // See if any terms have a matching permalink
197
- $table = get_option('custom_permalink_table');
198
- if ( !$table ) return $query;
199
-
200
- foreach ( array_keys($table) as $permalink ) {
201
- if ( $permalink == substr($request_noslash, 0, strlen($permalink)) ||
202
- $permalink == substr($request_noslash."/", 0, strlen($permalink)) ) {
203
- $term = $table[$permalink];
204
-
205
- // Preserve this url for later if it's the same as the permalink (no extra stuff)
206
- if ( $request_noslash == trim($permalink,'/') )
207
- $_CPRegisteredURL = $request;
208
-
209
-
210
- if ( $term['kind'] == 'category') {
211
- $originalUrl = str_replace(trim($permalink,'/'),
212
- custom_permalinks_original_category_link($term['id']),
213
- trim($request,'/'));
214
- } else {
215
- $originalUrl = str_replace(trim($permalink,'/'),
216
- custom_permalinks_original_tag_link($term['id']),
217
- trim($request,'/'));
218
- }
219
- }
220
- }
221
- }
222
-
223
- if ( $originalUrl !== NULL ) {
224
- $originalUrl = str_replace('//', '/', $originalUrl);
225
-
226
- if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) {
227
- $queryVars = substr($_SERVER['REQUEST_URI'], $pos+1);
228
- $originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars;
229
- }
230
-
231
- // Now we have the original URL, run this back through WP->parse_request, in order to
232
- // parse parameters properly. We set $_SERVER variables to fool the function.
233
- $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING'];
234
- $_SERVER['REQUEST_URI'] = '/'.ltrim($originalUrl,'/');
235
- $_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : '');
236
- parse_str($_SERVER['QUERY_STRING'], $queryArray);
237
- $oldValues = array();
238
- if ( is_array($queryArray) )
239
- foreach ( $queryArray as $key => $value ) {
240
- $oldValues[$key] = $_REQUEST[$key];
241
- $_REQUEST[$key] = $_GET[$key] = $value;
242
- }
243
-
244
- // Re-run the filter, now with original environment in place
245
- remove_filter( 'request', 'custom_permalinks_request', 10, 1 );
246
- global $wp;
247
- $wp->parse_request();
248
- $query = $wp->query_vars;
249
- add_filter( 'request', 'custom_permalinks_request', 10, 1 );
250
-
251
- // Restore values
252
- $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString;
253
- foreach ( $oldValues as $key => $value ) {
254
- $_REQUEST[$key] = $value;
255
- }
256
- }
257
-
258
- return $query;
259
  }
260
 
261
  /**
@@ -265,17 +265,17 @@ function custom_permalinks_request($query) {
265
  * @since 0.3
266
  */
267
  function custom_permalinks_trailingslash($string, $type) {
268
- global $_CPRegisteredURL;
269
 
270
- $url = parse_url(get_bloginfo('url'));
271
- $request = ltrim(substr($string, strlen($url['path'])),'/');
272
 
273
- if ( !trim($request) ) return $string;
274
 
275
- if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
276
- return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
277
- }
278
- return $string;
279
  }
280
 
281
  /**
@@ -291,30 +291,30 @@ function custom_permalinks_trailingslash($string, $type) {
291
  */
292
  function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
293
  $permalink = get_post_meta( $id, 'custom_permalink', true );
294
- $post = &get_post($id);
295
-
296
- ob_start();
297
- ?>
298
- <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>
299
- <?php
300
- $content = ob_get_contents();
301
- ob_end_clean();
302
 
303
  if ( 'publish' == $post->post_status ) {
304
  $view_post = 'page' == $post->post_type ? __('View Page') : __('View Post');
305
- }
306
-
307
- if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) {
308
- $permalink = $matches[1];
309
  } else {
310
  list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
311
  if ( false !== strpos($permalink, '%postname%') || false !== strpos($permalink, '%pagename%') ) {
312
  $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
313
  }
314
  }
315
-
316
- return '<strong>' . __('Permalink:') . "</strong>\n" . $content .
317
- ( isset($view_post) ? "<span id='view-post-btn'><a href='$permalink' class='button' target='_blank'>$view_post</a></span>\n" : "" );
318
  }
319
 
320
 
@@ -325,22 +325,22 @@ function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new
325
  * @since 0.1
326
  */
327
  function custom_permalinks_post_options() {
328
- global $post;
329
- $post_id = $post;
330
- if (is_object($post_id)) {
331
- $post_id = $post_id->ID;
332
- }
333
-
334
- $permalink = get_post_meta( $post_id, 'custom_permalink', true );
335
-
336
- ?>
337
- <div class="postbox closed">
338
- <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
339
- <div class="inside">
340
- <?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?>
341
- </div>
342
- </div>
343
- <?php
344
  }
345
 
346
 
@@ -351,22 +351,22 @@ function custom_permalinks_post_options() {
351
  * @since 0.4
352
  */
353
  function custom_permalinks_page_options() {
354
- global $post;
355
- $post_id = $post;
356
- if (is_object($post_id)) {
357
- $post_id = $post_id->ID;
358
- }
359
-
360
- $permalink = get_post_meta( $post_id, 'custom_permalink', true );
361
-
362
- ?>
363
- <div class="postbox closed">
364
- <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
365
- <div class="inside">
366
- <?php custom_permalinks_form($permalink, custom_permalinks_original_page_link($post_id)); ?>
367
- </div>
368
- </div>
369
- <?php
370
  }
371
 
372
 
@@ -377,26 +377,26 @@ function custom_permalinks_page_options() {
377
  * @since 0.1
378
  */
379
  function custom_permalinks_term_options($object) {
380
- $permalink = custom_permalinks_permalink_for_term($object->term_id);
381
-
382
- if ( $object->term_id ) {
383
- $originalPermalink = ($object->taxonomy == 'post_tag' ?
384
- custom_permalinks_original_tag_link($object->term_id) :
385
- custom_permalinks_original_category_link($object->term_id) );
386
  }
387
-
388
- custom_permalinks_form($permalink, $originalPermalink);
389
-
390
- // Move the save button to above this form
391
- wp_enqueue_script('jquery');
392
- ?>
393
- <script type="text/javascript">
394
- jQuery(document).ready(function() {
395
- var button = jQuery('#custom_permalink_form').parent().find('.submit');
396
- button.remove().insertAfter(jQuery('#custom_permalink_form'));
397
- });
398
- </script>
399
- <?php
400
  }
401
 
402
  /**
@@ -406,30 +406,32 @@ function custom_permalinks_term_options($object) {
406
  * @since 0.1
407
  */
408
  function custom_permalinks_form($permalink, $original="", $renderContainers=true) {
409
- ?>
410
- <input value="true" type="hidden" name="custom_permalinks_edit" />
411
- <input value="<?php echo htmlspecialchars(urldecode($permalink)) ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
412
-
413
- <?php if ( $renderContainers ) : ?>
414
- <table class="form-table" id="custom_permalink_form">
415
- <tr>
416
- <th scope="row"><?php _e('Custom Permalink', 'custom-permalink') ?></th>
417
- <td>
418
- <?php endif; ?>
419
- <?php echo home_url() ?>/
420
- <input type="text" class="text" value="<?php echo htmlspecialchars($permalink ? urldecode($permalink) : urldecode($original)) ?>"
421
- style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>"
422
- onfocus="if ( this.style.color = '#ddd' ) { this.style.color = '#000'; }"
423
- onblur="document.getElementById('custom_permalink').value = this.value; if ( this.value == '' || this.value == '<?php echo htmlspecialchars(urldecode($original)) ?>' ) { this.value = '<?php echo htmlspecialchars(urldecode($original)) ?>'; this.style.color = '#ddd'; }"/>
424
- <?php if ( $renderContainers ) : ?>
425
- <br />
426
- <small><?php _e('Leave blank to disable', 'custom-permalink') ?></small>
427
-
428
- </td>
429
- </tr>
430
- </table>
431
- <?php
432
- endif;
 
 
433
  }
434
 
435
 
@@ -440,16 +442,16 @@ function custom_permalinks_form($permalink, $original="", $renderContainers=true
440
  * @since 0.1
441
  */
442
  function custom_permalinks_save_post($id) {
443
- if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
444
-
445
- delete_post_meta( $id, 'custom_permalink' );
446
-
447
- $original_link = custom_permalinks_original_post_link($id);
448
- $permalink_structure = get_option('permalink_structure');
449
-
450
- if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != $original_link ) {
451
- add_post_meta( $id, 'custom_permalink', str_replace('%2F', '/', urlencode(ltrim(stripcslashes($_REQUEST['custom_permalink']),"/"))) );
452
- }
453
  }
454
 
455
 
@@ -460,14 +462,14 @@ function custom_permalinks_save_post($id) {
460
  * @since 0.1
461
  */
462
  function custom_permalinks_save_tag($id) {
463
- if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
464
- $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
465
-
466
- if ( $newPermalink == custom_permalinks_original_tag_link($id) )
467
- $newPermalink = '';
468
-
469
- $term = get_term($id, 'post_tag');
470
- custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
471
  }
472
 
473
  /**
@@ -477,14 +479,14 @@ function custom_permalinks_save_tag($id) {
477
  * @since 0.1
478
  */
479
  function custom_permalinks_save_category($id) {
480
- if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
481
- $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
482
-
483
- if ( $newPermalink == custom_permalinks_original_category_link($id) )
484
- $newPermalink = '';
485
-
486
- $term = get_term($id, 'category');
487
- custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
488
  }
489
 
490
  /**
@@ -494,16 +496,16 @@ function custom_permalinks_save_category($id) {
494
  * @since 0.1
495
  */
496
  function custom_permalinks_save_term($term, $permalink) {
497
-
498
- custom_permalinks_delete_term($term->term_id);
499
- $table = get_option('custom_permalink_table');
500
- if ( $permalink )
501
- $table[$permalink] = array(
502
- 'id' => $term->term_id,
503
- 'kind' => ($term->taxonomy == 'category' ? 'category' : 'tag'),
504
- 'slug' => $term->slug);
505
-
506
- update_option('custom_permalink_table', $table);
507
  }
508
 
509
  /**
@@ -514,9 +516,9 @@ function custom_permalinks_save_term($term, $permalink) {
514
  * @author Piero <maltesepiero@gmail.com>
515
  */
516
  function custom_permalinks_delete_permalink( $id ){
517
- global $wpdb;
518
- // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
519
- $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE `meta_key` = 'custom_permalink' AND `post_id` = %d",$id));
520
  }
521
 
522
  /**
@@ -526,17 +528,17 @@ function custom_permalinks_delete_permalink( $id ){
526
  * @since 0.1
527
  */
528
  function custom_permalinks_delete_term($id) {
529
-
530
- $table = get_option('custom_permalink_table');
531
- if ( $table )
532
- foreach ( $table as $link => $info ) {
533
- if ( $info['id'] == $id ) {
534
- unset($table[$link]);
535
- break;
536
- }
537
- }
538
-
539
- update_option('custom_permalink_table', $table);
540
  }
541
 
542
  /**
@@ -546,78 +548,78 @@ function custom_permalinks_delete_term($id) {
546
  * @since 0.1
547
  */
548
  function custom_permalinks_options_page() {
549
-
550
- // Handle revert
551
- if ( isset($_REQUEST['revertit']) && isset($_REQUEST['revert']) ) {
552
- check_admin_referer('custom-permalinks-bulk');
553
- foreach ( (array)$_REQUEST['revert'] as $identifier ) {
554
- list($kind, $id) = explode('.', $identifier);
555
- switch ( $kind ) {
556
- case 'post':
557
- case 'page':
558
- delete_post_meta( $id, 'custom_permalink' );
559
- break;
560
- case 'tag':
561
- case 'category':
562
- custom_permalinks_delete_term($id);
563
- break;
564
- }
565
- }
566
-
567
- // Redirect
568
- $redirectUrl = $_SERVER['REQUEST_URI'];
569
- ?>
570
- <script type="text/javascript">
571
- document.location = '<?php echo $redirectUrl ?>'
572
- </script>
573
- <?php ;
574
- }
575
-
576
- ?>
577
- <div class="wrap">
578
- <h2><?php _e('Custom Permalinks', 'custom-permalinks') ?></h2>
579
-
580
- <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
581
- <?php wp_nonce_field('custom-permalinks-bulk') ?>
582
-
583
- <div class="tablenav">
584
- <div class="alignleft">
585
- <input type="submit" value="<?php _e('Revert', 'custom-permalinks'); ?>" name="revertit" class="button-secondary delete" />
586
- </div>
587
- <br class="clear" />
588
- </div>
589
- <br class="clear" />
590
- <table class="widefat">
591
- <thead>
592
- <tr>
593
- <th scope="col" class="check-column"><input type="checkbox" /></th>
594
- <th scope="col"><?php _e('Title', 'custom-permalinks') ?></th>
595
- <th scope="col"><?php _e('Type', 'custom-permalinks') ?></th>
596
- <th scope="col"><?php _e('Permalink', 'custom-permalinks') ?></th>
597
- </tr>
598
- </thead>
599
- <tbody>
600
- <?php
601
- $rows = custom_permalinks_admin_rows();
602
- foreach ( $rows as $row ) {
603
- ?>
604
- <tr valign="top">
605
- <th scope="row" class="check-column"><input type="checkbox" name="revert[]" value="<?php echo $row['id'] ?>" /></th>
606
- <td><strong><a class="row-title" href="<?php echo htmlspecialchars($row['editlink']) ?>"><?php echo htmlspecialchars($row['title']) ?></a></strong></td>
607
- <td><?php echo htmlspecialchars($row['type']) ?></td>
608
- <td><a href="<?php echo $row['permalink'] ?>" target="_blank" title="Visit <?php echo htmlspecialchars($row['title']) ?>">
609
- <?php echo htmlspecialchars(urldecode($row['permalink'])) ?>
610
- </a>
611
- </td>
612
- </tr>
613
- <?php
614
- }
615
- ?>
616
- </tbody>
617
- </table>
618
- </form>
619
- </div>
620
- <?php
621
  }
622
 
623
  /**
@@ -627,39 +629,39 @@ function custom_permalinks_options_page() {
627
  * @since 0.1
628
  */
629
  function custom_permalinks_admin_rows() {
630
- $rows = array();
631
-
632
- // List tags/categories
633
- $table = get_option('custom_permalink_table');
634
- if ( $table && is_array($table) ) {
635
- foreach ( $table as $permalink => $info ) {
636
- $row = array();
637
- $term = get_term($info['id'], ($info['kind'] == 'tag' ? 'post_tag' : 'category'));
638
- $row['id'] = $info['kind'].'.'.$info['id'];
639
- $row['permalink'] = home_url()."/".$permalink;
640
- $row['type'] = ucwords($info['kind']);
641
- $row['title'] = $term->name;
642
- $row['editlink'] = ( $info['kind'] == 'tag' ? 'edit-tags.php?action=edit&taxonomy=post_tag&tag_ID='.$info['id'] : 'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$info['id'] );
643
- $rows[] = $row;
644
- }
645
- }
646
-
647
- // List posts/pages
648
- global $wpdb;
649
- $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE
650
- $wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != '';";
651
- $posts = $wpdb->get_results($query);
652
- foreach ( $posts as $post ) {
653
- $row = array();
654
- $row['id'] = 'post.'.$post->ID;
655
- $row['permalink'] = get_permalink($post->ID);
656
- $row['type'] = ucwords( $post->post_type );
657
- $row['title'] = $post->post_title;
658
- $row['editlink'] = 'post.php?action=edit&post='.$post->ID;
659
- $rows[] = $row;
660
- }
661
-
662
- return $rows;
663
  }
664
 
665
 
@@ -670,12 +672,12 @@ function custom_permalinks_admin_rows() {
670
  * @since 0.1
671
  */
672
  function custom_permalinks_original_post_link($post_id) {
673
- remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook
674
- remove_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
675
- $originalPermalink = ltrim(str_replace(home_url(), '', get_permalink( $post_id )), '/');
676
- add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook
677
- add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
678
- return $originalPermalink;
679
  }
680
 
681
  /**
@@ -685,12 +687,12 @@ function custom_permalinks_original_post_link($post_id) {
685
  * @since 0.4
686
  */
687
  function custom_permalinks_original_page_link($post_id) {
688
- remove_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
689
- remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
690
- $originalPermalink = ltrim(str_replace(home_url(), '', get_permalink( $post_id )), '/');
691
- add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
692
- add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
693
- return $originalPermalink;
694
  }
695
 
696
 
@@ -701,12 +703,12 @@ function custom_permalinks_original_page_link($post_id) {
701
  * @since 0.1
702
  */
703
  function custom_permalinks_original_tag_link($tag_id) {
704
- remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
705
- remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
706
- $originalPermalink = ltrim(str_replace(home_url(), '', get_tag_link($tag_id)), '/');
707
- add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
708
- add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
709
- return $originalPermalink;
710
  }
711
 
712
  /**
@@ -716,12 +718,12 @@ function custom_permalinks_original_tag_link($tag_id) {
716
  * @since 0.1
717
  */
718
  function custom_permalinks_original_category_link($category_id) {
719
- remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
720
- remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
721
- $originalPermalink = ltrim(str_replace(home_url(), '', get_category_link($category_id)), '/');
722
- add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
723
- add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
724
- return $originalPermalink;
725
  }
726
 
727
  /**
@@ -731,26 +733,34 @@ function custom_permalinks_original_category_link($category_id) {
731
  * @since 0.1
732
  */
733
  function custom_permalinks_permalink_for_term($id) {
734
- $table = get_option('custom_permalink_table');
735
- if ( $table )
736
- foreach ( $table as $link => $info ) {
737
- if ( $info['id'] == $id ) {
738
- return $link;
739
- }
740
- }
741
- return false;
742
  }
743
 
744
  /**
745
- * Set up administration
746
  *
747
  * @package CustomPermalinks
748
  * @since 0.1
749
  */
750
- function custom_permalinks_setup_admin() {
751
- add_management_page( 'Custom Permalinks', 'Custom Permalinks', 5, 'custom_permalinks', 'custom_permalinks_options_page' );
752
- if ( is_admin() )
753
- wp_enqueue_script('admin-forms');
 
 
 
 
 
 
 
 
754
  }
755
 
756
  # Check whether we're running within the WP environment, to avoid showing errors like
@@ -758,38 +768,39 @@ function custom_permalinks_setup_admin() {
758
  # and similar errors that occurs when the script is called directly to e.g. find out the full path.
759
 
760
  if (function_exists("add_action") && function_exists("add_filter")) {
761
- add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
762
- add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
763
- add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
764
- add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
765
- add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
766
- add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
767
- add_filter( 'request', 'custom_permalinks_request', 10, 1 );
768
- add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
769
-
770
- if (function_exists("get_bloginfo")) {
771
- $v = explode('.', get_bloginfo('version'));
772
- }
773
-
774
- if ( $v[0] >= 2 ) {
775
- add_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 10, 4 );
776
- } else {
777
- add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
778
- add_action( 'edit_page_form', 'custom_permalinks_page_options' );
779
- }
780
-
781
- add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
782
- add_action( 'add_tag_form', 'custom_permalinks_term_options' );
783
- add_action( 'edit_category_form', 'custom_permalinks_term_options' );
784
- add_action( 'save_post', 'custom_permalinks_save_post' );
785
- add_action( 'save_page', 'custom_permalinks_save_post' );
786
- add_action( 'edited_post_tag', 'custom_permalinks_save_tag' );
787
- add_action( 'edited_category', 'custom_permalinks_save_category' );
788
- add_action( 'create_post_tag', 'custom_permalinks_save_tag' );
789
- add_action( 'create_category', 'custom_permalinks_save_category' );
790
- add_action( 'delete_post', 'custom_permalinks_delete_permalink', 10);
791
- add_action( 'delete_post_tag', 'custom_permalinks_delete_term' );
792
- add_action( 'delete_post_category', 'custom_permalinks_delete_term' );
793
- add_action( 'admin_menu', 'custom_permalinks_setup_admin' );
 
794
  }
795
- ?>
4
  Plugin URI: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
5
  Donate link: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
6
  Description: Set custom permalinks on a per-post basis
7
+ Version: 0.7.20
8
  Author: Michael Tyson
9
  Author URI: http://atastypixel.com/blog
10
  */
39
  * @since 0.1
40
  */
41
  function custom_permalinks_post_link($permalink, $post) {
42
+ $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
43
+ if ( $custom_permalink ) {
44
+ return home_url()."/".$custom_permalink;
45
+ }
46
+
47
+ return $permalink;
48
  }
49
 
50
 
55
  * @since 0.4
56
  */
57
  function custom_permalinks_page_link($permalink, $page) {
58
+ $custom_permalink = get_post_meta( $page, 'custom_permalink', true );
59
+ if ( $custom_permalink ) {
60
+ return home_url()."/".$custom_permalink;
61
+ }
62
+
63
+ return $permalink;
64
  }
65
 
66
 
71
  * @since 0.1
72
  */
73
  function custom_permalinks_term_link($permalink, $term) {
74
+ $table = get_option('custom_permalink_table');
75
+ if ( is_object($term) ) $term = $term->term_id;
76
+
77
+ $custom_permalink = custom_permalinks_permalink_for_term($term);
78
+
79
+ if ( $custom_permalink ) {
80
+ return home_url()."/".$custom_permalink;
81
+ }
82
+
83
+ return $permalink;
84
  }
85
 
86
 
91
  * @since 0.1
92
  */
93
  function custom_permalinks_redirect() {
94
+
95
+ // Get request URI, strip parameters
96
+ $url = parse_url(get_bloginfo('url'));
97
+ $url = isset($url['path']) ? $url['path'] : '';
98
+ $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
99
+ if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);
100
+
101
+ global $wp_query;
102
+
103
+ $custom_permalink = '';
104
+ $original_permalink = '';
105
+
106
+ // If the post/tag/category we're on has a custom permalink, get it and check against the request
107
+ if ( is_single() || is_page() ) {
108
+ $post = $wp_query->post;
109
+ $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
110
+ $original_permalink = ( $post->post_type == 'page' ? custom_permalinks_original_page_link( $post->ID ) : custom_permalinks_original_post_link( $post->ID ) );
111
+ } else if ( is_tag() || is_category() ) {
112
+ $theTerm = $wp_query->get_queried_object();
113
+ $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
114
+ $original_permalink = (is_tag() ? custom_permalinks_original_tag_link($theTerm->term_id) :
115
+ custom_permalinks_original_category_link($theTerm->term_id));
116
+ }
117
+
118
+ if ( $custom_permalink &&
119
+ (substr($request, 0, strlen($custom_permalink)) != $custom_permalink ||
120
+ $request == $custom_permalink."/" ) ) {
121
+ // Request doesn't match permalink - redirect
122
+ $url = $custom_permalink;
123
+
124
+ if ( substr($request, 0, strlen($original_permalink)) == $original_permalink &&
125
+ trim($request,'/') != trim($original_permalink,'/') ) {
126
+ // This is the original link; we can use this url to derive the new one
127
+ $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request));
128
+ $url = preg_replace('@([^?]*)&@', '\1?', $url);
129
+ }
130
+
131
+ // Append any query compenent
132
+ $url .= strstr($_SERVER['REQUEST_URI'], "?");
133
+
134
+ wp_redirect( home_url()."/".$url, 301 );
135
+ exit();
136
+ }
137
  }
138
 
139
  /**
143
  * @since 0.1
144
  */
145
  function custom_permalinks_request($query) {
146
+ global $wpdb;
147
+ global $_CPRegisteredURL;
148
+
149
+ // First, search for a matching custom permalink, and if found, generate the corresponding
150
+ // original URL
151
+
152
+ $originalUrl = NULL;
153
+
154
+ // Get request URI, strip parameters and /'s
155
+ $url = parse_url(get_bloginfo('url'));
156
+ $url = isset($url['path']) ? $url['path'] : '';
157
+ $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
158
+ $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request);
159
+ $request_noslash = preg_replace('@/+@','/', trim($request, '/'));
160
+
161
+ if ( !$request ) return $query;
162
+
163
+ // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
164
  $sql = $wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts ".
165
+ "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
166
+ " meta_key = 'custom_permalink' AND ".
167
+ " meta_value != '' AND ".
168
+ " ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR ".
169
+ " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) ".
170
+ " AND post_status != 'trash' AND post_type != 'nav_menu_item'".
171
+ " ORDER BY LENGTH(meta_value) DESC, ".
172
+ " FIELD(post_status,'publish','private','draft','auto-draft','inherit'),".
173
+ " FIELD(post_type,'post','page'),".
174
+ "$wpdb->posts.ID ASC LIMIT 1",
175
+ $request_noslash,
176
+ $request_noslash."/"
177
+ );
178
+
179
+ $posts = $wpdb->get_results($sql);
180
+
181
+ if ( $posts ) {
182
+ // A post matches our request
183
+
184
+ // Preserve this url for later if it's the same as the permalink (no extra stuff)
185
+ if ( $request_noslash == trim($posts[0]->meta_value,'/') )
186
+ $_CPRegisteredURL = $request;
187
+
188
+ $originalUrl = preg_replace( '@/+@', '/', str_replace( trim( strtolower($posts[0]->meta_value),'/' ),
189
+ ( $posts[0]->post_type == 'page' ?
190
+ custom_permalinks_original_page_link($posts[0]->ID)
191
+ : custom_permalinks_original_post_link($posts[0]->ID) ),
192
+ strtolower($request_noslash) ) );
193
+ }
194
+
195
+ if ( $originalUrl === NULL ) {
196
+ // See if any terms have a matching permalink
197
+ $table = get_option('custom_permalink_table');
198
+ if ( !$table ) return $query;
199
+
200
+ foreach ( array_keys($table) as $permalink ) {
201
+ if ( $permalink == substr($request_noslash, 0, strlen($permalink)) ||
202
+ $permalink == substr($request_noslash."/", 0, strlen($permalink)) ) {
203
+ $term = $table[$permalink];
204
+
205
+ // Preserve this url for later if it's the same as the permalink (no extra stuff)
206
+ if ( $request_noslash == trim($permalink,'/') )
207
+ $_CPRegisteredURL = $request;
208
+
209
+
210
+ if ( $term['kind'] == 'category') {
211
+ $originalUrl = str_replace(trim($permalink,'/'),
212
+ custom_permalinks_original_category_link($term['id']),
213
+ trim($request,'/'));
214
+ } else {
215
+ $originalUrl = str_replace(trim($permalink,'/'),
216
+ custom_permalinks_original_tag_link($term['id']),
217
+ trim($request,'/'));
218
+ }
219
+ }
220
+ }
221
+ }
222
+
223
+ if ( $originalUrl !== NULL ) {
224
+ $originalUrl = str_replace('//', '/', $originalUrl);
225
+
226
+ if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) {
227
+ $queryVars = substr($_SERVER['REQUEST_URI'], $pos+1);
228
+ $originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars;
229
+ }
230
+
231
+ // Now we have the original URL, run this back through WP->parse_request, in order to
232
+ // parse parameters properly. We set $_SERVER variables to fool the function.
233
+ $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING'];
234
+ $_SERVER['REQUEST_URI'] = '/'.ltrim($originalUrl,'/');
235
+ $_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : '');
236
+ parse_str($_SERVER['QUERY_STRING'], $queryArray);
237
+ $oldValues = array();
238
+ if ( is_array($queryArray) )
239
+ foreach ( $queryArray as $key => $value ) {
240
+ $oldValues[$key] = $_REQUEST[$key];
241
+ $_REQUEST[$key] = $_GET[$key] = $value;
242
+ }
243
+
244
+ // Re-run the filter, now with original environment in place
245
+ remove_filter( 'request', 'custom_permalinks_request', 'edit_files', 1 );
246
+ global $wp;
247
+ $wp->parse_request();
248
+ $query = $wp->query_vars;
249
+ add_filter( 'request', 'custom_permalinks_request', 'edit_files', 1 );
250
+
251
+ // Restore values
252
+ $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString;
253
+ foreach ( $oldValues as $key => $value ) {
254
+ $_REQUEST[$key] = $value;
255
+ }
256
+ }
257
+
258
+ return $query;
259
  }
260
 
261
  /**
265
  * @since 0.3
266
  */
267
  function custom_permalinks_trailingslash($string, $type) {
268
+ global $_CPRegisteredURL;
269
 
270
+ $url = parse_url(get_bloginfo('url'));
271
+ $request = ltrim(isset($url['path']) ? substr($string, strlen($url['path'])) : $string, '/');
272
 
273
+ if ( !trim($request) ) return $string;
274
 
275
+ if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
276
+ return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
277
+ }
278
+ return $string;
279
  }
280
 
281
  /**
291
  */
292
  function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
293
  $permalink = get_post_meta( $id, 'custom_permalink', true );
294
+ $post = &get_post($id);
295
+
296
+ ob_start();
297
+ ?>
298
+ <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>
299
+ <?php
300
+ $content = ob_get_contents();
301
+ ob_end_clean();
302
 
303
  if ( 'publish' == $post->post_status ) {
304
  $view_post = 'page' == $post->post_type ? __('View Page') : __('View Post');
305
+ }
306
+
307
+ if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) {
308
+ $permalink = $matches[1];
309
  } else {
310
  list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
311
  if ( false !== strpos($permalink, '%postname%') || false !== strpos($permalink, '%pagename%') ) {
312
  $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
313
  }
314
  }
315
+
316
+ return '<strong>' . __('Permalink:') . "</strong>\n" . $content .
317
+ ( isset($view_post) ? "<span id='view-post-btn'><a href='$permalink' class='button button-small' target='_blank'>$view_post</a></span>\n" : "" );
318
  }
319
 
320
 
325
  * @since 0.1
326
  */
327
  function custom_permalinks_post_options() {
328
+ global $post;
329
+ $post_id = $post;
330
+ if (is_object($post_id)) {
331
+ $post_id = $post_id->ID;
332
+ }
333
+
334
+ $permalink = get_post_meta( $post_id, 'custom_permalink', true );
335
+
336
+ ?>
337
+ <div class="postbox closed">
338
+ <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
339
+ <div class="inside">
340
+ <?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?>
341
+ </div>
342
+ </div>
343
+ <?php
344
  }
345
 
346
 
351
  * @since 0.4
352
  */
353
  function custom_permalinks_page_options() {
354
+ global $post;
355
+ $post_id = $post;
356
+ if (is_object($post_id)) {
357
+ $post_id = $post_id->ID;
358
+ }
359
+
360
+ $permalink = get_post_meta( $post_id, 'custom_permalink', true );
361
+
362
+ ?>
363
+ <div class="postbox closed">
364
+ <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
365
+ <div class="inside">
366
+ <?php custom_permalinks_form($permalink, custom_permalinks_original_page_link($post_id)); ?>
367
+ </div>
368
+ </div>
369
+ <?php
370
  }
371
 
372
 
377
  * @since 0.1
378
  */
379
  function custom_permalinks_term_options($object) {
380
+ $permalink = custom_permalinks_permalink_for_term($object->term_id);
381
+
382
+ if ( $object->term_id ) {
383
+ $originalPermalink = ($object->taxonomy == 'post_tag' ?
384
+ custom_permalinks_original_tag_link($object->term_id) :
385
+ custom_permalinks_original_category_link($object->term_id) );
386
  }
387
+
388
+ custom_permalinks_form($permalink, $originalPermalink);
389
+
390
+ // Move the save button to above this form
391
+ wp_enqueue_script('jquery');
392
+ ?>
393
+ <script type="text/javascript">
394
+ jQuery(document).ready(function() {
395
+ var button = jQuery('#custom_permalink_form').parent().find('.submit');
396
+ button.remove().insertAfter(jQuery('#custom_permalink_form'));
397
+ });
398
+ </script>
399
+ <?php
400
  }
401
 
402
  /**
406
  * @since 0.1
407
  */
408
  function custom_permalinks_form($permalink, $original="", $renderContainers=true) {
409
+ ?>
410
+ <input value="true" type="hidden" name="custom_permalinks_edit" />
411
+ <input value="<?php echo htmlspecialchars(urldecode($permalink)) ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
412
+
413
+ <?php if ( $renderContainers ) : ?>
414
+ <table class="form-table" id="custom_permalink_form">
415
+ <tr>
416
+ <th scope="row"><?php _e('Custom Permalink', 'custom-permalink') ?></th>
417
+ <td>
418
+ <?php endif; ?>
419
+ <?php echo home_url() ?>/
420
+ <span id="editable-post-name" title="Click to edit this part of the permalink">
421
+ <input type="text" id="new-post-slug" class="text" value="<?php echo htmlspecialchars($permalink ? urldecode($permalink) : urldecode($original)) ?>"
422
+ style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>"
423
+ onfocus="if ( this.style.color = '#ddd' ) { this.style.color = '#000'; }"
424
+ onblur="document.getElementById('custom_permalink').value = this.value; if ( this.value == '' || this.value == '<?php echo htmlspecialchars(urldecode($original)) ?>' ) { this.value = '<?php echo htmlspecialchars(urldecode($original)) ?>'; this.style.color = '#ddd'; }"/>
425
+ </span>
426
+ <?php if ( $renderContainers ) : ?>
427
+ <br />
428
+ <small><?php _e('Leave blank to disable', 'custom-permalink') ?></small>
429
+
430
+ </td>
431
+ </tr>
432
+ </table>
433
+ <?php
434
+ endif;
435
  }
436
 
437
 
442
  * @since 0.1
443
  */
444
  function custom_permalinks_save_post($id) {
445
+ if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
446
+
447
+ delete_post_meta( $id, 'custom_permalink' );
448
+
449
+ $original_link = custom_permalinks_original_post_link($id);
450
+ $permalink_structure = get_option('permalink_structure');
451
+
452
+ if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != $original_link ) {
453
+ add_post_meta( $id, 'custom_permalink', str_replace('%2F', '/', urlencode(ltrim(stripcslashes($_REQUEST['custom_permalink']),"/"))) );
454
+ }
455
  }
456
 
457
 
462
  * @since 0.1
463
  */
464
  function custom_permalinks_save_tag($id) {
465
+ if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
466
+ $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
467
+
468
+ if ( $newPermalink == custom_permalinks_original_tag_link($id) )
469
+ $newPermalink = '';
470
+
471
+ $term = get_term($id, 'post_tag');
472
+ custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
473
  }
474
 
475
  /**
479
  * @since 0.1
480
  */
481
  function custom_permalinks_save_category($id) {
482
+ if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
483
+ $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
484
+
485
+ if ( $newPermalink == custom_permalinks_original_category_link($id) )
486
+ $newPermalink = '';
487
+
488
+ $term = get_term($id, 'category');
489
+ custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
490
  }
491
 
492
  /**
496
  * @since 0.1
497
  */
498
  function custom_permalinks_save_term($term, $permalink) {
499
+
500
+ custom_permalinks_delete_term($term->term_id);
501
+ $table = get_option('custom_permalink_table');
502
+ if ( $permalink )
503
+ $table[$permalink] = array(
504
+ 'id' => $term->term_id,
505
+ 'kind' => ($term->taxonomy == 'category' ? 'category' : 'tag'),
506
+ 'slug' => $term->slug);
507
+
508
+ update_option('custom_permalink_table', $table);
509
  }
510
 
511
  /**
516
  * @author Piero <maltesepiero@gmail.com>
517
  */
518
  function custom_permalinks_delete_permalink( $id ){
519
+ global $wpdb;
520
+ // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
521
+ $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE `meta_key` = 'custom_permalink' AND `post_id` = %d",$id));
522
  }
523
 
524
  /**
528
  * @since 0.1
529
  */
530
  function custom_permalinks_delete_term($id) {
531
+
532
+ $table = get_option('custom_permalink_table');
533
+ if ( $table )
534
+ foreach ( $table as $link => $info ) {
535
+ if ( $info['id'] == $id ) {
536
+ unset($table[$link]);
537
+ break;
538
+ }
539
+ }
540
+
541
+ update_option('custom_permalink_table', $table);
542
  }
543
 
544
  /**
548
  * @since 0.1
549
  */
550
  function custom_permalinks_options_page() {
551
+
552
+ // Handle revert
553
+ if ( isset($_REQUEST['revertit']) && isset($_REQUEST['revert']) ) {
554
+ check_admin_referer('custom-permalinks-bulk');
555
+ foreach ( (array)$_REQUEST['revert'] as $identifier ) {
556
+ list($kind, $id) = explode('.', $identifier);
557
+ switch ( $kind ) {
558
+ case 'post':
559
+ case 'page':
560
+ delete_post_meta( $id, 'custom_permalink' );
561
+ break;
562
+ case 'tag':
563
+ case 'category':
564
+ custom_permalinks_delete_term($id);
565
+ break;
566
+ }
567
+ }
568
+
569
+ // Redirect
570
+ $redirectUrl = $_SERVER['REQUEST_URI'];
571
+ ?>
572
+ <script type="text/javascript">
573
+ document.location = '<?php echo $redirectUrl ?>'
574
+ </script>
575
+ <?php ;
576
+ }
577
+
578
+ ?>
579
+ <div class="wrap">
580
+ <h2><?php _e('Custom Permalinks', 'custom-permalinks') ?></h2>
581
+
582
+ <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
583
+ <?php wp_nonce_field('custom-permalinks-bulk') ?>
584
+
585
+ <div class="tablenav">
586
+ <div class="alignleft">
587
+ <input type="submit" value="<?php _e('Revert', 'custom-permalinks'); ?>" name="revertit" class="button-secondary delete" />
588
+ </div>
589
+ <br class="clear" />
590
+ </div>
591
+ <br class="clear" />
592
+ <table class="widefat">
593
+ <thead>
594
+ <tr>
595
+ <th scope="col" class="check-column"><input type="checkbox" /></th>
596
+ <th scope="col"><?php _e('Title', 'custom-permalinks') ?></th>
597
+ <th scope="col"><?php _e('Type', 'custom-permalinks') ?></th>
598
+ <th scope="col"><?php _e('Permalink', 'custom-permalinks') ?></th>
599
+ </tr>
600
+ </thead>
601
+ <tbody>
602
+ <?php
603
+ $rows = custom_permalinks_admin_rows();
604
+ foreach ( $rows as $row ) {
605
+ ?>
606
+ <tr valign="top">
607
+ <th scope="row" class="check-column"><input type="checkbox" name="revert[]" value="<?php echo $row['id'] ?>" /></th>
608
+ <td><strong><a class="row-title" href="<?php echo htmlspecialchars($row['editlink']) ?>"><?php echo htmlspecialchars($row['title']) ?></a></strong></td>
609
+ <td><?php echo htmlspecialchars($row['type']) ?></td>
610
+ <td><a href="<?php echo $row['permalink'] ?>" target="_blank" title="Visit <?php echo htmlspecialchars($row['title']) ?>">
611
+ <?php echo htmlspecialchars(urldecode($row['permalink'])) ?>
612
+ </a>
613
+ </td>
614
+ </tr>
615
+ <?php
616
+ }
617
+ ?>
618
+ </tbody>
619
+ </table>
620
+ </form>
621
+ </div>
622
+ <?php
623
  }
624
 
625
  /**
629
  * @since 0.1
630
  */
631
  function custom_permalinks_admin_rows() {
632
+ $rows = array();
633
+
634
+ // List tags/categories
635
+ $table = get_option('custom_permalink_table');
636
+ if ( $table && is_array($table) ) {
637
+ foreach ( $table as $permalink => $info ) {
638
+ $row = array();
639
+ $term = get_term($info['id'], ($info['kind'] == 'tag' ? 'post_tag' : 'category'));
640
+ $row['id'] = $info['kind'].'.'.$info['id'];
641
+ $row['permalink'] = home_url()."/".$permalink;
642
+ $row['type'] = ucwords($info['kind']);
643
+ $row['title'] = $term->name;
644
+ $row['editlink'] = ( $info['kind'] == 'tag' ? 'edit-tags.php?action=edit&taxonomy=post_tag&tag_ID='.$info['id'] : 'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$info['id'] );
645
+ $rows[] = $row;
646
+ }
647
+ }
648
+
649
+ // List posts/pages
650
+ global $wpdb;
651
+ $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE
652
+ $wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != '';";
653
+ $posts = $wpdb->get_results($query);
654
+ foreach ( $posts as $post ) {
655
+ $row = array();
656
+ $row['id'] = 'post.'.$post->ID;
657
+ $row['permalink'] = get_permalink($post->ID);
658
+ $row['type'] = ucwords( $post->post_type );
659
+ $row['title'] = $post->post_title;
660
+ $row['editlink'] = 'post.php?action=edit&post='.$post->ID;
661
+ $rows[] = $row;
662
+ }
663
+
664
+ return $rows;
665
  }
666
 
667
 
672
  * @since 0.1
673
  */
674
  function custom_permalinks_original_post_link($post_id) {
675
+ remove_filter( 'post_link', 'custom_permalinks_post_link', 'edit_files', 2 ); // original hook
676
+ remove_filter( 'post_type_link', 'custom_permalinks_post_link', 'edit_files', 2 );
677
+ $originalPermalink = ltrim(str_replace(home_url(), '', get_permalink( $post_id )), '/');
678
+ add_filter( 'post_link', 'custom_permalinks_post_link', 'edit_files', 2 ); // original hook
679
+ add_filter( 'post_type_link', 'custom_permalinks_post_link', 'edit_files', 2 );
680
+ return $originalPermalink;
681
  }
682
 
683
  /**
687
  * @since 0.4
688
  */
689
  function custom_permalinks_original_page_link($post_id) {
690
+ remove_filter( 'page_link', 'custom_permalinks_page_link', 'edit_files', 2 );
691
+ remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 'edit_files', 2 );
692
+ $originalPermalink = ltrim(str_replace(home_url(), '', get_permalink( $post_id )), '/');
693
+ add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 'edit_files', 2 );
694
+ add_filter( 'page_link', 'custom_permalinks_page_link', 'edit_files', 2 );
695
+ return $originalPermalink;
696
  }
697
 
698
 
703
  * @since 0.1
704
  */
705
  function custom_permalinks_original_tag_link($tag_id) {
706
+ remove_filter( 'tag_link', 'custom_permalinks_term_link', 'edit_files', 2 );
707
+ remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 'edit_files', 2 );
708
+ $originalPermalink = ltrim(str_replace(home_url(), '', get_tag_link($tag_id)), '/');
709
+ add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 'edit_files', 2 );
710
+ add_filter( 'tag_link', 'custom_permalinks_term_link', 'edit_files', 2 );
711
+ return $originalPermalink;
712
  }
713
 
714
  /**
718
  * @since 0.1
719
  */
720
  function custom_permalinks_original_category_link($category_id) {
721
+ remove_filter( 'category_link', 'custom_permalinks_term_link', 'edit_files', 2 );
722
+ remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 'edit_files', 2 );
723
+ $originalPermalink = ltrim(str_replace(home_url(), '', get_category_link($category_id)), '/');
724
+ add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 'edit_files', 2 );
725
+ add_filter( 'category_link', 'custom_permalinks_term_link', 'edit_files', 2 );
726
+ return $originalPermalink;
727
  }
728
 
729
  /**
733
  * @since 0.1
734
  */
735
  function custom_permalinks_permalink_for_term($id) {
736
+ $table = get_option('custom_permalink_table');
737
+ if ( $table )
738
+ foreach ( $table as $link => $info ) {
739
+ if ( $info['id'] == $id ) {
740
+ return $link;
741
+ }
742
+ }
743
+ return false;
744
  }
745
 
746
  /**
747
+ * Set up administration menu
748
  *
749
  * @package CustomPermalinks
750
  * @since 0.1
751
  */
752
+ function custom_permalinks_setup_admin_menu() {
753
+ add_management_page( 'Custom Permalinks', 'Custom Permalinks', 'edit_others_pages', 'custom_permalinks', 'custom_permalinks_options_page' );
754
+ }
755
+
756
+ /**
757
+ * Set up administration header
758
+ *
759
+ * @package CustomPermalinks
760
+ * @since 0.7.20
761
+ */
762
+ function custom_permalinks_setup_admin_head() {
763
+ wp_enqueue_script('admin-forms');
764
  }
765
 
766
  # Check whether we're running within the WP environment, to avoid showing errors like
768
  # and similar errors that occurs when the script is called directly to e.g. find out the full path.
769
 
770
  if (function_exists("add_action") && function_exists("add_filter")) {
771
+ add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
772
+ add_filter( 'post_link', 'custom_permalinks_post_link', 'edit_files', 2 );
773
+ add_filter( 'post_type_link', 'custom_permalinks_post_link', 'edit_files', 2 );
774
+ add_filter( 'page_link', 'custom_permalinks_page_link', 'edit_files', 2 );
775
+ add_filter( 'tag_link', 'custom_permalinks_term_link', 'edit_files', 2 );
776
+ add_filter( 'category_link', 'custom_permalinks_term_link', 'edit_files', 2 );
777
+ add_filter( 'request', 'custom_permalinks_request', 'edit_files', 1 );
778
+ add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 'edit_files', 2 );
779
+
780
+ if (function_exists("get_bloginfo")) {
781
+ $v = explode('.', get_bloginfo('version'));
782
+ }
783
+
784
+ if ( $v[0] >= 2 ) {
785
+ add_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 'edit_files', 4 );
786
+ } else {
787
+ add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
788
+ add_action( 'edit_page_form', 'custom_permalinks_page_options' );
789
+ }
790
+
791
+ add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
792
+ add_action( 'add_tag_form', 'custom_permalinks_term_options' );
793
+ add_action( 'edit_category_form', 'custom_permalinks_term_options' );
794
+ add_action( 'save_post', 'custom_permalinks_save_post' );
795
+ add_action( 'save_page', 'custom_permalinks_save_post' );
796
+ add_action( 'edited_post_tag', 'custom_permalinks_save_tag' );
797
+ add_action( 'edited_category', 'custom_permalinks_save_category' );
798
+ add_action( 'create_post_tag', 'custom_permalinks_save_tag' );
799
+ add_action( 'create_category', 'custom_permalinks_save_category' );
800
+ add_action( 'delete_post', 'custom_permalinks_delete_permalink', 'edit_files');
801
+ add_action( 'delete_post_tag', 'custom_permalinks_delete_term' );
802
+ add_action( 'delete_post_category', 'custom_permalinks_delete_term' );
803
+ add_action( 'admin_head', 'custom_permalinks_setup_admin_head' );
804
+ add_action( 'admin_menu', 'custom_permalinks_setup_admin_menu' );
805
  }
806
+ ?>
readme.txt CHANGED
@@ -3,8 +3,8 @@
3
  Donate link: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
4
  Tags: permalink, url, link, address, custom, redirect
5
  Requires at least: 2.6
6
- Tested up to: 3.3.1
7
- Stable tag: 0.7.19
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
@@ -29,6 +29,13 @@ within that category.
29
 
30
  == Changelog ==
31
 
 
 
 
 
 
 
 
32
  = 0.7.19 =
33
 
34
  * WP 3.9 compatibility fix, thanks to Sowmedia
3
  Donate link: http://atastypixel.com/blog/wordpress/plugins/custom-permalinks/
4
  Tags: permalink, url, link, address, custom, redirect
5
  Requires at least: 2.6
6
+ Tested up to: 4.1
7
+ Stable tag: 0.7.20
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
29
 
30
  == Changelog ==
31
 
32
+ = 0.7.20 =
33
+
34
+ * Addressed a noisy warning
35
+ * Revised addition of admin forms js (don't use is_admin())
36
+ * Updated Roles and Capabilities from depreciated numerical to label capabilities (by OF-6)
37
+ * Added css/html to match WP 3.5+ layout (by OF-6)
38
+
39
  = 0.7.19 =
40
 
41
  * WP 3.9 compatibility fix, thanks to Sowmedia