| 1 |
<?php |
| 2 |
/* |
| 3 |
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.4 |
| 8 |
Author: Michael Tyson |
| 9 |
Author URI: http://michael.tyson.id.au |
| 10 |
*/ |
| 11 |
|
| 12 |
/* Copyright 2008 Michael Tyson <mike@tyson.id.au> |
| 13 |
|
| 14 |
This program is free software; you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License as published by |
| 16 |
the Free Software Foundation; either version 2 of the License, or |
| 17 |
(at your option) any later version. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
GNU General Public License for more details. |
| 23 |
|
| 24 |
You should have received a copy of the GNU General Public License |
| 25 |
along with this program; if not, write to the Free Software |
| 26 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 27 |
*/ |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
** Actions and filters |
| 32 |
** |
| 33 |
**/ |
| 34 |
|
| 35 |
/** |
| 36 |
* Filter to replace the post permalink with the custom one |
| 37 |
* |
| 38 |
* @package CustomPermalinks |
| 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 get_option('home')."/".$custom_permalink; |
| 45 |
} |
| 46 |
|
| 47 |
return $permalink; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Filter to replace the page permalink with the custom one |
| 53 |
* |
| 54 |
* @package CustomPermalinks |
| 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 get_option('home')."/".$custom_permalink; |
| 61 |
} |
| 62 |
|
| 63 |
return $permalink; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* Filter to replace the term permalink with the custom one |
| 69 |
* |
| 70 |
* @package CustomPermalinks |
| 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 get_option('home')."/".$custom_permalink; |
| 81 |
} |
| 82 |
|
| 83 |
return $permalink; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Action to redirect to the custom permalink |
| 89 |
* |
| 90 |
* @package CustomPermalinks |
| 91 |
* @since 0.1 |
| 92 |
*/ |
| 93 |
function custom_permalinks_redirect() { |
| 94 |
|
| 95 |
// Get request URI, strip parameters |
| 96 |
$request = ltrim($_SERVER['REQUEST_URI'],'/'); |
| 97 |
if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos); |
| 98 |
|
| 99 |
global $wp_query; |
| 100 |
|
| 101 |
$custom_permalink = ''; |
| 102 |
$original_permalink = ''; |
| 103 |
|
| 104 |
// If the post/tag/category we're on has a custom permalink, get it and check against the request |
| 105 |
if ( is_single() || is_page() ) { |
| 106 |
$post = $wp_query->post; |
| 107 |
$custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true ); |
| 108 |
$original_permalink = ( $post->post_type == 'post' ? custom_permalinks_original_post_link( $post->ID ) : custom_permalinks_original_page_link( $post->ID ) ); |
| 109 |
} else if ( is_tag() || is_category() ) { |
| 110 |
$theTerm = $wp_query->get_queried_object(); |
| 111 |
$custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id); |
| 112 |
$original_permalink = (is_tag() ? custom_permalinks_original_tag_link($theTerm->term_id) : |
| 113 |
custom_permalinks_original_category_link($theTerm->term_id)); |
| 114 |
} |
| 115 |
|
| 116 |
if ( $custom_permalink && |
| 117 |
(substr($request, 0, strlen($custom_permalink)) != $custom_permalink || |
| 118 |
$request == $custom_permalink."/" ) ) { |
| 119 |
// Request doesn't match permalink - redirect |
| 120 |
$url = $custom_permalink; |
| 121 |
if ( substr($request, 0, strlen($original_permalink)) == $original_permalink && |
| 122 |
trim($request,'/') != trim($original_permalink,'/') ) { |
| 123 |
// This is the original link; we can use this url to derive the new one |
| 124 |
$url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request)); |
| 125 |
$url = preg_replace('@([^?]*)&@', '\1?', $url); |
| 126 |
} |
| 127 |
wp_redirect( get_option('home')."/".$url, 301 ); |
| 128 |
exit(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Filter to rewrite the query if we have a matching post |
| 134 |
* |
| 135 |
* @package CustomPermalinks |
| 136 |
* @since 0.1 |
| 137 |
*/ |
| 138 |
function custom_permalinks_request($query) { |
| 139 |
global $wpdb; |
| 140 |
global $_CPRegisteredURL; |
| 141 |
|
| 142 |
// First, search for a matching custom permalink, and if found, generate the corresponding |
| 143 |
// original URL |
| 144 |
|
| 145 |
$originalUrl = ''; |
| 146 |
|
| 147 |
// Get request URI, strip parameters and /'s |
| 148 |
$request = (($pos=strpos($_SERVER['REQUEST_URI'], '?')) ? substr($_SERVER['REQUEST_URI'], 0, $pos) : $_SERVER['REQUEST_URI']); |
| 149 |
$request = preg_replace('@/+@','/', trim($request, '/')); |
| 150 |
|
| 151 |
if ( !$request ) return $query; |
| 152 |
|
| 153 |
$sql = "SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts ". |
| 154 |
" LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ". |
| 155 |
" meta_key = 'custom_permalink' AND ". |
| 156 |
" meta_value != '' AND ". |
| 157 |
" ( meta_value = LEFT('".mysql_escape_string($request)."', LENGTH(meta_value)) OR ". |
| 158 |
" meta_value = LEFT('".mysql_escape_string($request."/")."', LENGTH(meta_value)) )"; |
| 159 |
|
| 160 |
$posts = $wpdb->get_results($sql); |
| 161 |
if ( $posts ) { |
| 162 |
// A post matches our request |
| 163 |
|
| 164 |
// Preserve this url for later if it's the same as the permalink (no extra stuff) |
| 165 |
if ( trim($request,'/') == trim($posts[0]->meta_value,'/') ) |
| 166 |
$_CPRegisteredURL = $request; |
| 167 |
|
| 168 |
$originalUrl = str_replace( trim($posts[0]->meta_value,'/' ), |
| 169 |
( $posts[0]->post_type == 'post' ? |
| 170 |
custom_permalinks_original_post_link($posts[0]->ID) |
| 171 |
: custom_permalinks_original_page_link($posts[0]->ID) ), |
| 172 |
trim( $request,'/' ) ); |
| 173 |
} |
| 174 |
|
| 175 |
if ( !$originalUrl ) { |
| 176 |
$table = get_option('custom_permalink_table'); |
| 177 |
if ( !$table ) return $query; |
| 178 |
|
| 179 |
foreach ( array_keys($table) as $permalink ) { |
| 180 |
if ( $permalink == substr($request, 0, strlen($permalink)) || $permalink == substr($request."/", 0, strlen($permalink)) ) { |
| 181 |
$term = $table[$permalink]; |
| 182 |
|
| 183 |
// Preserve this url for later if it's the same as the permalink (no extra stuff) |
| 184 |
if ( trim($request,'/') == trim($permalink,'/') ) |
| 185 |
$_CPRegisteredURL = $request; |
| 186 |
|
| 187 |
|
| 188 |
if ( $term['kind'] == 'category') { |
| 189 |
$originalUrl = str_replace(trim($permalink,'/'), |
| 190 |
custom_permalinks_original_category_link($term['id']), |
| 191 |
trim($request,'/')); |
| 192 |
} else { |
| 193 |
$originalUrl = str_replace(trim($permalink,'/'), |
| 194 |
custom_permalinks_original_tag_link($term['id']), |
| 195 |
trim($request,'/')); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if ( $originalUrl ) { |
| 202 |
|
| 203 |
if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) { |
| 204 |
$queryVars = substr($_SERVER['REQUEST_URI'], $pos+1); |
| 205 |
$originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars; |
| 206 |
} |
| 207 |
|
| 208 |
// Now we have the original URL, run this back through WP->parse_request, in order to |
| 209 |
// parse parameters properly. We set $_SERVER variables to fool the function. |
| 210 |
$oldPathInfo = $_SERVER['PATH_INFO']; $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING']; |
| 211 |
$_SERVER['PATH_INFO'] = $originalUrl; $_SERVER['REQUEST_URI'] = $originalUrl; |
| 212 |
$_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : ''); |
| 213 |
parse_str($_SERVER['QUERY_STRING'], $queryArray); |
| 214 |
$oldValues = array(); |
| 215 |
if ( is_array($queryArray) ) |
| 216 |
foreach ( $queryArray as $key => $value ) { |
| 217 |
$oldValues[$key] = $_REQUEST[$key]; |
| 218 |
$_REQUEST[$key] = $_GET[$key] = $value; |
| 219 |
} |
| 220 |
|
| 221 |
// Re-run the filter, now with original environment in place |
| 222 |
remove_filter( 'request', 'custom_permalinks_request', 10, 1 ); |
| 223 |
global $wp; |
| 224 |
$wp->parse_request(); |
| 225 |
$query = $wp->query_vars; |
| 226 |
add_filter( 'request', 'custom_permalinks_request', 10, 1 ); |
| 227 |
|
| 228 |
// Restore values |
| 229 |
$_SERVER['PATH_INFO'] = $oldPathInfo; $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString; |
| 230 |
foreach ( $oldValues as $key => $value ) { |
| 231 |
$_REQUEST[$key] = $value; |
| 232 |
} |
| 233 |
} |
| 234 |
return $query; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Filter to handle trailing slashes correctly |
| 239 |
* |
| 240 |
* @package CustomPermalinks |
| 241 |
* @since 0.3 |
| 242 |
*/ |
| 243 |
function custom_permalinks_trailingslash($string, $type) { |
| 244 |
global $_CPRegisteredURL; |
| 245 |
if ( trim($_CPRegisteredURL,'/') == trim($string,'/') ) { |
| 246 |
return $_CPRegisteredURL; |
| 247 |
} |
| 248 |
return $string; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
/** |
| 253 |
** Administration |
| 254 |
** |
| 255 |
**/ |
| 256 |
|
| 257 |
|
| 258 |
/** |
| 259 |
* Per-post options |
| 260 |
* |
| 261 |
* @package CustomPermalinks |
| 262 |
* @since 0.1 |
| 263 |
*/ |
| 264 |
function custom_permalinks_post_options() { |
| 265 |
global $post; |
| 266 |
$post_id = $post; |
| 267 |
if (is_object($post_id)) { |
| 268 |
$post_id = $post_id->ID; |
| 269 |
} |
| 270 |
|
| 271 |
$permalink = get_post_meta( $post_id, 'custom_permalink', true ); |
| 272 |
|
| 273 |
?> |
| 274 |
<div class="postbox closed"> |
| 275 |
<h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3> |
| 276 |
<div class="inside"> |
| 277 |
<?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?> |
| 278 |
</div> |
| 279 |
</div> |
| 280 |
<?php |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* Per-page options |
| 286 |
* |
| 287 |
* @package CustomPermalinks |
| 288 |
* @since 0.4 |
| 289 |
*/ |
| 290 |
function custom_permalinks_page_options() { |
| 291 |
global $post; |
| 292 |
$post_id = $post; |
| 293 |
if (is_object($post_id)) { |
| 294 |
$post_id = $post_id->ID; |
| 295 |
} |
| 296 |
|
| 297 |
$permalink = get_post_meta( $post_id, 'custom_permalink', true ); |
| 298 |
|
| 299 |
?> |
| 300 |
<div class="postbox closed"> |
| 301 |
<h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3> |
| 302 |
<div class="inside"> |
| 303 |
<?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?> |
| 304 |
</div> |
| 305 |
</div> |
| 306 |
<?php |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/** |
| 311 |
* Per-category/tag options |
| 312 |
* |
| 313 |
* @package CustomPermalinks |
| 314 |
* @since 0.1 |
| 315 |
*/ |
| 316 |
function custom_permalinks_term_options($object) { |
| 317 |
|
| 318 |
$permalink = custom_permalinks_permalink_for_term($object->term_id); |
| 319 |
|
| 320 |
$originalPermalink = ($object->taxonomy == 'post_tag' ? |
| 321 |
custom_permalinks_original_tag_link($object->term_id) : |
| 322 |
custom_permalinks_original_category_link($object->term_id) ); |
| 323 |
|
| 324 |
custom_permalinks_form($permalink, $originalPermalink); |
| 325 |
|
| 326 |
// Move the save button to above this form |
| 327 |
wp_enqueue_script('jquery'); |
| 328 |
?> |
| 329 |
<script type="text/javascript"> |
| 330 |
jQuery(document).ready(function() { |
| 331 |
var button = jQuery('#custom_permalink_form').parent().find('.submit'); |
| 332 |
button.remove().insertAfter(jQuery('#custom_permalink_form')); |
| 333 |
}); |
| 334 |
</script> |
| 335 |
<?php |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Helper function to render form |
| 340 |
* |
| 341 |
* @package CustomPermalinks |
| 342 |
* @since 0.1 |
| 343 |
*/ |
| 344 |
function custom_permalinks_form($permalink, $original="") { |
| 345 |
?> |
| 346 |
<input value="true" type="hidden" name="custom_permalinks_edit" /> |
| 347 |
<table class="form-table" id="custom_permalink_form"> |
| 348 |
<tr> |
| 349 |
<th scope="row"><?php _e('Custom Permalink', 'custom-permalink') ?></th> |
| 350 |
<td> |
| 351 |
<?php echo get_option('home') ?>/ |
| 352 |
<input type="text" name="custom_permalink" class="text" value="<?php echo htmlspecialchars($permalink ? $permalink : $original) ?>" |
| 353 |
style="width: 300px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>" |
| 354 |
onfocus="if ( this.value == '<?php echo htmlspecialchars($original) ?>' ) { this.value = ''; this.style.color = '#000'; }" |
| 355 |
onblur="if ( this.value == '' ) { this.value = '<?php echo htmlspecialchars($original) ?>'; this.style.color = '#ddd'; }"/><br /> |
| 356 |
<small><?php _e('Leave blank to disable', 'custom-permalink') ?></small> |
| 357 |
</td> |
| 358 |
</tr> |
| 359 |
</table> |
| 360 |
<?php |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
/** |
| 365 |
* Save per-post options |
| 366 |
* |
| 367 |
* @package CustomPermalinks |
| 368 |
* @since 0.1 |
| 369 |
*/ |
| 370 |
function custom_permalinks_save_post($id) { |
| 371 |
if ( !isset($_REQUEST['custom_permalinks_edit']) ) return; |
| 372 |
|
| 373 |
delete_post_meta( $id, 'custom_permalink' ); |
| 374 |
if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != custom_permalinks_original_post_link($id) ) |
| 375 |
add_post_meta( $id, 'custom_permalink', ltrim(stripcslashes($_REQUEST['custom_permalink']),"/") ); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
/** |
| 380 |
* Save per-tag options |
| 381 |
* |
| 382 |
* @package CustomPermalinks |
| 383 |
* @since 0.1 |
| 384 |
*/ |
| 385 |
function custom_permalinks_save_tag($id) { |
| 386 |
if ( !isset($_REQUEST['custom_permalinks_edit']) ) return; |
| 387 |
$newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/"); |
| 388 |
|
| 389 |
if ( $newPermalink == custom_permalinks_original_tag_link($id) ) |
| 390 |
$newPermalink = ''; |
| 391 |
|
| 392 |
$term = get_term($id, 'post_tag'); |
| 393 |
custom_permalinks_save_term($term, $newPermalink); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Save per-category options |
| 398 |
* |
| 399 |
* @package CustomPermalinks |
| 400 |
* @since 0.1 |
| 401 |
*/ |
| 402 |
function custom_permalinks_save_category($id) { |
| 403 |
if ( !isset($_REQUEST['custom_permalinks_edit']) ) return; |
| 404 |
$newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/"); |
| 405 |
|
| 406 |
if ( $newPermalink == custom_permalinks_original_category_link($id) ) |
| 407 |
$newPermalink = ''; |
| 408 |
|
| 409 |
$term = get_term($id, 'category'); |
| 410 |
custom_permalinks_save_term($term, $newPermalink); |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Save term (common to tags and categories) |
| 415 |
* |
| 416 |
* @package CustomPermalinks |
| 417 |
* @since 0.1 |
| 418 |
*/ |
| 419 |
function custom_permalinks_save_term($term, $permalink) { |
| 420 |
|
| 421 |
custom_permalinks_delete_term($term->term_id); |
| 422 |
$table = get_option('custom_permalink_table'); |
| 423 |
if ( $permalink ) |
| 424 |
$table[$permalink] = array( |
| 425 |
'id' => $term->term_id, |
| 426 |
'kind' => ($term->taxonomy == 'category' ? 'category' : 'tag'), |
| 427 |
'slug' => $term->slug); |
| 428 |
|
| 429 |
update_option('custom_permalink_table', $table); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
/** |
| 434 |
* Delete term |
| 435 |
* |
| 436 |
* @package CustomPermalinks |
| 437 |
* @since 0.1 |
| 438 |
*/ |
| 439 |
function custom_permalinks_delete_term($id) { |
| 440 |
|
| 441 |
$table = get_option('custom_permalink_table'); |
| 442 |
if ( $table ) |
| 443 |
foreach ( $table as $link => $info ) { |
| 444 |
if ( $info['id'] == $id ) { |
| 445 |
unset($table[$link]); |
| 446 |
break; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
update_option('custom_permalink_table', $table); |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Options page |
| 455 |
* |
| 456 |
* @package CustomPermalinks |
| 457 |
* @since 0.1 |
| 458 |
*/ |
| 459 |
function custom_permalinks_options_page() { |
| 460 |
|
| 461 |
// Handle revert |
| 462 |
if ( isset($_REQUEST['revertit']) && isset($_REQUEST['revert']) ) { |
| 463 |
check_admin_referer('custom-permalinks-bulk'); |
| 464 |
foreach ( (array)$_REQUEST['revert'] as $identifier ) { |
| 465 |
list($kind, $id) = explode('.', $identifier); |
| 466 |
switch ( $kind ) { |
| 467 |
case 'post': |
| 468 |
case 'page': |
| 469 |
delete_post_meta( $id, 'custom_permalink' ); |
| 470 |
break; |
| 471 |
case 'tag': |
| 472 |
case 'category': |
| 473 |
custom_permalinks_delete_term($id); |
| 474 |
break; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
// Redirect |
| 479 |
$redirectUrl = $_SERVER['REQUEST_URI']; |
| 480 |
?> |
| 481 |
<script type="text/javascript"> |
| 482 |
document.location = '<?php echo $redirectUrl ?>'; |
| 483 |
</script> |
| 484 |
<?php |
| 485 |
} |
| 486 |
|
| 487 |
?> |
| 488 |
<div class="wrap"> |
| 489 |
<h2><?php _e('Custom Permalinks', 'custom-permalinks') ?></h2> |
| 490 |
|
| 491 |
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>"> |
| 492 |
<?php wp_nonce_field('custom-permalinks-bulk') ?> |
| 493 |
|
| 494 |
<div class="tablenav"> |
| 495 |
<div class="alignleft"> |
| 496 |
<input type="submit" value="<?php _e('Revert', 'custom-permalinks'); ?>" name="revertit" class="button-secondary delete" /> |
| 497 |
</div> |
| 498 |
<br class="clear" /> |
| 499 |
</div> |
| 500 |
<br class="clear" /> |
| 501 |
<table class="widefat"> |
| 502 |
<thead> |
| 503 |
<tr> |
| 504 |
<th scope="col" class="check-column"><input type="checkbox" /></th> |
| 505 |
<th scope="col"><?php _e('Title', 'custom-permalinks') ?></th> |
| 506 |
<th scope="col"><?php _e('Type', 'custom-permalinks') ?></th> |
| 507 |
<th scope="col"><?php _e('Permalink', 'custom-permalinks') ?></th> |
| 508 |
</tr> |
| 509 |
</thead> |
| 510 |
<tbody> |
| 511 |
<?php |
| 512 |
$rows = custom_permalinks_admin_rows(); |
| 513 |
foreach ( $rows as $row ) { |
| 514 |
?> |
| 515 |
<tr valign="top"> |
| 516 |
<th scope="row" class="check-column"><input type="checkbox" name="revert[]" value="<?php echo $row['id'] ?>" /></th> |
| 517 |
<td><strong><a class="row-title" href="<?php echo htmlspecialchars($row['editlink']) ?>"><?php echo htmlspecialchars($row['title']) ?></a></strong></td> |
| 518 |
<td><?php echo htmlspecialchars($row['type']) ?></td> |
| 519 |
<td><a href="<?php echo $row['permalink'] ?>" target="_blank" title="Visit <?php echo htmlspecialchars($row['title']) ?>"> |
| 520 |
<?php echo htmlspecialchars($row['permalink']) ?> |
| 521 |
</a> |
| 522 |
</td> |
| 523 |
</tr> |
| 524 |
<?php |
| 525 |
} |
| 526 |
?> |
| 527 |
</tbody> |
| 528 |
</table> |
| 529 |
</form> |
| 530 |
</div> |
| 531 |
<?php |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* Get rows for management view |
| 536 |
* |
| 537 |
* @package CustomPermalinks |
| 538 |
* @since 0.1 |
| 539 |
*/ |
| 540 |
function custom_permalinks_admin_rows() { |
| 541 |
$rows = array(); |
| 542 |
|
| 543 |
// List tags/categories |
| 544 |
$table = get_option('custom_permalink_table'); |
| 545 |
if ( $table && is_array($table) ) { |
| 546 |
foreach ( $table as $permalink => $info ) { |
| 547 |
$row = array(); |
| 548 |
$term = get_term($info['id'], ($info['kind'] == 'tag' ? 'post_tag' : 'category')); |
| 549 |
$row['id'] = $info['kind'].'.'.$info['id']; |
| 550 |
$row['permalink'] = get_option('home')."/".$permalink; |
| 551 |
$row['type'] = ucwords($info['kind']); |
| 552 |
$row['title'] = $term->name; |
| 553 |
$row['editlink'] = ( $info['kind'] == 'tag' ? 'edit-tags.php?action=edit&tag_ID='.$info['id'] : 'categories.php?action=edit&cat_ID='.$info['id'] ); |
| 554 |
$rows[] = $row; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
// List posts/pages |
| 559 |
global $wpdb; |
| 560 |
$query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ". |
| 561 |
"$wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != ''"; |
| 562 |
$posts = $wpdb->get_results($query); |
| 563 |
foreach ( $posts as $post ) { |
| 564 |
$row = array(); |
| 565 |
$row['id'] = 'post.'.$post->ID; |
| 566 |
$row['permalink'] = get_permalink($post->ID); |
| 567 |
$row['type'] = ucwords( $post->post_type ); |
| 568 |
$row['title'] = $post->post_title; |
| 569 |
$row['editlink'] = ( $post->post_type == 'post' ? 'post.php' : 'page.php' ) . '?action=edit&post='.$post->ID; |
| 570 |
$rows[] = $row; |
| 571 |
} |
| 572 |
|
| 573 |
return $rows; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
/** |
| 578 |
* Get original permalink for post |
| 579 |
* |
| 580 |
* @package CustomPermalinks |
| 581 |
* @since 0.1 |
| 582 |
*/ |
| 583 |
function custom_permalinks_original_post_link($post_id) { |
| 584 |
remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); |
| 585 |
$originalPermalink = ltrim(str_replace(get_option('home'), '', get_permalink( $post_id )), '/'); |
| 586 |
add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); |
| 587 |
return $originalPermalink; |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Get original permalink for page |
| 592 |
* |
| 593 |
* @package CustomPermalinks |
| 594 |
* @since 0.4 |
| 595 |
*/ |
| 596 |
function custom_permalinks_original_page_link($post_id) { |
| 597 |
remove_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 ); |
| 598 |
$originalPermalink = ltrim(str_replace(get_option('home'), '', get_permalink( $post_id )), '/'); |
| 599 |
add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 ); |
| 600 |
return $originalPermalink; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
/** |
| 605 |
* Get original permalink for tag |
| 606 |
* |
| 607 |
* @package CustomPermalinks |
| 608 |
* @since 0.1 |
| 609 |
*/ |
| 610 |
function custom_permalinks_original_tag_link($tag_id) { |
| 611 |
remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 ); |
| 612 |
$originalPermalink = ltrim(str_replace(get_option('home'), '', get_tag_link($tag_id)), '/'); |
| 613 |
add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 ); |
| 614 |
return $originalPermalink; |
| 615 |
} |
| 616 |
|
| 617 |
/** |
| 618 |
* Get original permalink for category |
| 619 |
* |
| 620 |
* @package CustomPermalinks |
| 621 |
* @since 0.1 |
| 622 |
*/ |
| 623 |
function custom_permalinks_original_category_link($category_id) { |
| 624 |
remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 ); |
| 625 |
$originalPermalink = ltrim(str_replace(get_option('home'), '', get_category_link($category_id)), '/'); |
| 626 |
add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 ); |
| 627 |
return $originalPermalink; |
| 628 |
} |
| 629 |
|
| 630 |
/** |
| 631 |
* Get permalink for term |
| 632 |
* |
| 633 |
* @package CustomPermalinks |
| 634 |
* @since 0.1 |
| 635 |
*/ |
| 636 |
function custom_permalinks_permalink_for_term($id) { |
| 637 |
$table = get_option('custom_permalink_table'); |
| 638 |
if ( $table ) |
| 639 |
foreach ( $table as $link => $info ) { |
| 640 |
if ( $info['id'] == $id ) { |
| 641 |
return $link; |
| 642 |
} |
| 643 |
} |
| 644 |
return false; |
| 645 |
} |
| 646 |
|
| 647 |
/** |
| 648 |
* Set up administration |
| 649 |
* |
| 650 |
* @package CustomPermalinks |
| 651 |
* @since 0.1 |
| 652 |
*/ |
| 653 |
function custom_permalinks_setup_admin() { |
| 654 |
add_management_page( 'Custom Permalinks', 'Custom Permalinks', 5, __FILE__, 'custom_permalinks_options_page' ); |
| 655 |
if ( is_admin() ) |
| 656 |
wp_enqueue_script('admin-forms'); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
add_action( 'template_redirect', 'custom_permalinks_redirect', 5 ); |
| 661 |
add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); |
| 662 |
add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 ); |
| 663 |
add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 ); |
| 664 |
add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 ); |
| 665 |
add_filter( 'request', 'custom_permalinks_request', 10, 1 ); |
| 666 |
add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 ); |
| 667 |
|
| 668 |
add_action( 'edit_form_advanced', 'custom_permalinks_post_options' ); |
| 669 |
add_action( 'edit_page_form', 'custom_permalinks_page_options' ); |
| 670 |
add_action( 'edit_tag_form', 'custom_permalinks_term_options' ); |
| 671 |
add_action( 'edit_category_form', 'custom_permalinks_term_options' ); |
| 672 |
add_action( 'save_post', 'custom_permalinks_save_post' ); |
| 673 |
add_action( 'edited_post_tag', 'custom_permalinks_save_tag' ); |
| 674 |
add_action( 'edited_category', 'custom_permalinks_save_category' ); |
| 675 |
add_action( 'delete_post_tag', 'custom_permalinks_delete_term' ); |
| 676 |
add_action( 'delete_post_category', 'custom_permalinks_delete_term' ); |
| 677 |
add_action( 'admin_menu', 'custom_permalinks_setup_admin' ); |
| 678 |
|
| 679 |
?> |
| 680 |
|