Advertisement
sarahn

edited-custom-permalinks.php

Dec 27th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. 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.15
  8. Author: Michael Tyson
  9. Author URI: http://atastypixel.com/blog
  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 $permalink;//get_home_url()."/".$custom_permalink; // WPML Support
  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 $permalink;//get_home_url()."/".$custom_permalink; // WPML Support
  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_home_url()."/".$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. $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( get_home_url()."/".$url, 301 );
  135. exit();
  136. }
  137. }
  138.  
  139. /**
  140. * Filter to rewrite the query if we have a matching post
  141. *
  142. * @package CustomPermalinks
  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. $sql = "SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts ".
  164. "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
  165. " meta_key = 'custom_permalink' AND ".
  166. " meta_value != '' AND ".
  167. " ( LOWER(meta_value) = LEFT(LOWER('".mysql_escape_string($request_noslash)."'), LENGTH(meta_value)) OR ".
  168. " LOWER(meta_value) = LEFT(LOWER('".mysql_escape_string($request_noslash."/")."'), LENGTH(meta_value)) ) ".
  169. "ORDER BY LENGTH(meta_value) DESC LIMIT 1";
  170.  
  171. $posts = $wpdb->get_results($sql);
  172.  
  173. if ( $posts ) {
  174. // A post matches our request
  175.  
  176. // Preserve this url for later if it's the same as the permalink (no extra stuff)
  177. if ( $request_noslash == trim($posts[0]->meta_value,'/') )
  178. $_CPRegisteredURL = $request;
  179.  
  180. $originalUrl = preg_replace( '@/+@', '/', str_replace( trim( strtolower($posts[0]->meta_value),'/' ),
  181. ( $posts[0]->post_type == 'page' ?
  182. custom_permalinks_original_page_link($posts[0]->ID)
  183. : custom_permalinks_original_post_link($posts[0]->ID) ),
  184. strtolower($request_noslash) ) );
  185. }
  186.  
  187. if ( $originalUrl === NULL ) {
  188. // See if any terms have a matching permalink
  189. $table = get_option('custom_permalink_table');
  190. if ( !$table ) return $query;
  191.  
  192. foreach ( array_keys($table) as $permalink ) {
  193. if ( $permalink == substr($request_noslash, 0, strlen($permalink)) ||
  194. $permalink == substr($request_noslash."/", 0, strlen($permalink)) ) {
  195. $term = $table[$permalink];
  196.  
  197. // Preserve this url for later if it's the same as the permalink (no extra stuff)
  198. if ( $request_noslash == trim($permalink,'/') )
  199. $_CPRegisteredURL = $request;
  200.  
  201.  
  202. if ( $term['kind'] == 'category') {
  203. $originalUrl = str_replace(trim($permalink,'/'),
  204. custom_permalinks_original_category_link($term['id']),
  205. trim($request,'/'));
  206. } else {
  207. $originalUrl = str_replace(trim($permalink,'/'),
  208. custom_permalinks_original_tag_link($term['id']),
  209. trim($request,'/'));
  210. }
  211. }
  212. }
  213. }
  214.  
  215. if ( $originalUrl !== NULL ) {
  216. $originalUrl = str_replace('//', '/', $originalUrl);
  217.  
  218. if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) {
  219. $queryVars = substr($_SERVER['REQUEST_URI'], $pos+1);
  220. $originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars;
  221. }
  222.  
  223. // Now we have the original URL, run this back through WP->parse_request, in order to
  224. // parse parameters properly. We set $_SERVER variables to fool the function.
  225. $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING'];
  226. $_SERVER['REQUEST_URI'] = '/'.ltrim($originalUrl,'/');
  227. $_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : '');
  228. parse_str($_SERVER['QUERY_STRING'], $queryArray);
  229. $oldValues = array();
  230. if ( is_array($queryArray) )
  231. foreach ( $queryArray as $key => $value ) {
  232. $oldValues[$key] = $_REQUEST[$key];
  233. $_REQUEST[$key] = $_GET[$key] = $value;
  234. }
  235.  
  236. // Re-run the filter, now with original environment in place
  237. remove_filter( 'request', 'custom_permalinks_request', 10, 1 );
  238. global $wp;
  239. $wp->parse_request();
  240. $query = $wp->query_vars;
  241. add_filter( 'request', 'custom_permalinks_request', 10, 1 );
  242.  
  243. // Restore values
  244. $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString;
  245. foreach ( $oldValues as $key => $value ) {
  246. $_REQUEST[$key] = $value;
  247. }
  248. }
  249.  
  250. return $query;
  251. }
  252.  
  253. /**
  254. * Filter to handle trailing slashes correctly
  255. *
  256. * @package CustomPermalinks
  257. * @since 0.3
  258. */
  259. function custom_permalinks_trailingslash($string, $type) {
  260. global $_CPRegisteredURL;
  261.  
  262. $url = parse_url(get_bloginfo('url'));
  263. $request = ltrim(substr($string, strlen($url['path'])),'/');
  264.  
  265. if ( !trim($request) ) return $string;
  266.  
  267. if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
  268. return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
  269. }
  270. return $string;
  271. }
  272.  
  273. /**
  274. ** Administration
  275. **
  276. **/
  277.  
  278. /**
  279. * Per-post/page options (Wordpress > 2.9)
  280. *
  281. * @package CustomPermalinks
  282. * @since 0.6
  283. */
  284. function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
  285. $permalink = get_post_meta( $id, 'custom_permalink', true );
  286. $post = &get_post($id);
  287.  
  288. ob_start();
  289. ?>
  290. <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>
  291. <?php
  292. $content = ob_get_contents();
  293. ob_end_clean();
  294.  
  295. if ( 'publish' == $post->post_status ) {
  296. $view_post = 'page' == $post->post_type ? __('View Page') : __('View Post');
  297. }
  298.  
  299. if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) {
  300. $permalink = $matches[1];
  301. } else {
  302. list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
  303. if ( false !== strpos($permalink, '%postname%') || false !== strpos($permalink, '%pagename%') ) {
  304. $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
  305. }
  306. }
  307.  
  308. return '<strong>' . __('Permalink:') . "</strong>\n" . $content .
  309. ( isset($view_post) ? "<span id='view-post-btn'><a href='$permalink' class='button' target='_blank'>$view_post</a></span>\n" : "" );
  310. }
  311.  
  312.  
  313. /**
  314. * Per-post options (Wordpress < 2.9)
  315. *
  316. * @package CustomPermalinks
  317. * @since 0.1
  318. */
  319. function custom_permalinks_post_options() {
  320. global $post;
  321. $post_id = $post;
  322. if (is_object($post_id)) {
  323. $post_id = $post_id->ID;
  324. }
  325.  
  326. $permalink = get_post_meta( $post_id, 'custom_permalink', true );
  327.  
  328. ?>
  329. <div class="postbox closed">
  330. <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
  331. <div class="inside">
  332. <?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?>
  333. </div>
  334. </div>
  335. <?php
  336. }
  337.  
  338.  
  339. /**
  340. * Per-page options (Wordpress < 2.9)
  341. *
  342. * @package CustomPermalinks
  343. * @since 0.4
  344. */
  345. function custom_permalinks_page_options() {
  346. global $post;
  347. $post_id = $post;
  348. if (is_object($post_id)) {
  349. $post_id = $post_id->ID;
  350. }
  351.  
  352. $permalink = get_post_meta( $post_id, 'custom_permalink', true );
  353.  
  354. ?>
  355. <div class="postbox closed">
  356. <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
  357. <div class="inside">
  358. <?php custom_permalinks_form($permalink, custom_permalinks_original_page_link($post_id)); ?>
  359. </div>
  360. </div>
  361. <?php
  362. }
  363.  
  364.  
  365. /**
  366. * Per-category/tag options
  367. *
  368. * @package CustomPermalinks
  369. * @since 0.1
  370. */
  371. function custom_permalinks_term_options($object) {
  372. $permalink = custom_permalinks_permalink_for_term($object->term_id);
  373.  
  374. if ( $object->term_id ) {
  375. $originalPermalink = ($object->taxonomy == 'post_tag' ?
  376. custom_permalinks_original_tag_link($object->term_id) :
  377. custom_permalinks_original_category_link($object->term_id) );
  378. }
  379.  
  380. custom_permalinks_form($permalink, $originalPermalink);
  381.  
  382. // Move the save button to above this form
  383. wp_enqueue_script('jquery');
  384. ?>
  385. <script type="text/javascript">
  386. jQuery(document).ready(function() {
  387. var button = jQuery('#custom_permalink_form').parent().find('.submit');
  388. button.remove().insertAfter(jQuery('#custom_permalink_form'));
  389. });
  390. </script>
  391. <?php
  392. }
  393.  
  394. /**
  395. * Helper function to render form
  396. *
  397. * @package CustomPermalinks
  398. * @since 0.1
  399. */
  400. function custom_permalinks_form($permalink, $original="", $renderContainers=true) {
  401. ?>
  402. <input value="true" type="hidden" name="custom_permalinks_edit" />
  403. <input value="<?php echo htmlspecialchars(urldecode($permalink)) ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
  404.  
  405. <?php if ( $renderContainers ) : ?>
  406. <table class="form-table" id="custom_permalink_form">
  407. <tr>
  408. <th scope="row"><?php _e('Custom Permalink', 'custom-permalink') ?></th>
  409. <td>
  410. <?php endif; ?>
  411. <?php echo get_home_url() ?>/
  412. <input type="text" class="text" value="<?php echo htmlspecialchars($permalink ? urldecode($permalink) : urldecode($original)) ?>"
  413. style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>"
  414. onfocus="if ( this.style.color = '#ddd' ) { this.style.color = '#000'; }"
  415. 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'; }"/>
  416. <?php if ( $renderContainers ) : ?>
  417. <br />
  418. <small><?php _e('Leave blank to disable', 'custom-permalink') ?></small>
  419.  
  420. </td>
  421. </tr>
  422. </table>
  423. <?php
  424. endif;
  425. }
  426.  
  427.  
  428. /**
  429. * Save per-post options
  430. *
  431. * @package CustomPermalinks
  432. * @since 0.1
  433. */
  434. function custom_permalinks_save_post($id) {
  435. global $wpdb;
  436. if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
  437.  
  438. delete_post_meta( $id, 'custom_permalink' );
  439.  
  440. $original_link = custom_permalinks_original_post_link($id);
  441. $permalink_structure = get_option('permalink_structure');
  442.  
  443. // WPML Support
  444. if ( $_REQUEST['custom_permalink']) {
  445. $c_permalink = $_REQUEST['custom_permalink'];
  446. $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET post_name=%s WHERE ID = %d", $_REQUEST['custom_permalink'], $id));
  447. add_post_meta( $id, 'custom_permalink', str_replace('%2F', '/', urlencode(ltrim(stripcslashes($c_permalink),"/"))) );
  448.  
  449. }
  450. //\ WPML Support
  451. }
  452.  
  453.  
  454. /**
  455. * Save per-tag options
  456. *
  457. * @package CustomPermalinks
  458. * @since 0.1
  459. */
  460. function custom_permalinks_save_tag($id) {
  461. if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
  462. $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
  463.  
  464. if ( $newPermalink == custom_permalinks_original_tag_link($id) )
  465. $newPermalink = '';
  466.  
  467. $term = get_term($id, 'post_tag');
  468. custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
  469. }
  470.  
  471. /**
  472. * Save per-category options
  473. *
  474. * @package CustomPermalinks
  475. * @since 0.1
  476. */
  477. function custom_permalinks_save_category($id) {
  478. if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
  479. $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
  480.  
  481. if ( $newPermalink == custom_permalinks_original_category_link($id) )
  482. $newPermalink = '';
  483.  
  484. $term = get_term($id, 'category');
  485. custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
  486. }
  487.  
  488. /**
  489. * Save term (common to tags and categories)
  490. *
  491. * @package CustomPermalinks
  492. * @since 0.1
  493. */
  494. function custom_permalinks_save_term($term, $permalink) {
  495.  
  496. custom_permalinks_delete_term($term->term_id);
  497. $table = get_option('custom_permalink_table');
  498. if ( $permalink )
  499. $table[$permalink] = array(
  500. 'id' => $term->term_id,
  501. 'kind' => ($term->taxonomy == 'category' ? 'category' : 'tag'),
  502. 'slug' => $term->slug);
  503.  
  504. update_option('custom_permalink_table', $table);
  505. }
  506.  
  507. /**
  508. * Delete post
  509. *
  510. * @package CustomPermalinks
  511. * @since 0.7.14
  512. * @author Piero <maltesepiero@gmail.com>
  513. */
  514. function custom_permalinks_delete_permalink( $id ){
  515. global $wpdb;
  516. $wpdb->query("DELETE FROM $wpdb->postmeta WHERE `meta_key` = 'custom_permalink' AND `post_id` = '".mysql_escape_string($id)."'");
  517. }
  518.  
  519. /**
  520. * Delete term
  521. *
  522. * @package CustomPermalinks
  523. * @since 0.1
  524. */
  525. function custom_permalinks_delete_term($id) {
  526.  
  527. $table = get_option('custom_permalink_table');
  528. if ( $table )
  529. foreach ( $table as $link => $info ) {
  530. if ( $info['id'] == $id ) {
  531. unset($table[$link]);
  532. break;
  533. }
  534. }
  535.  
  536. update_option('custom_permalink_table', $table);
  537. }
  538.  
  539. /**
  540. * Options page
  541. *
  542. * @package CustomPermalinks
  543. * @since 0.1
  544. */
  545. function custom_permalinks_options_page() {
  546.  
  547. // Handle revert
  548. if ( isset($_REQUEST['revertit']) && isset($_REQUEST['revert']) ) {
  549. check_admin_referer('custom-permalinks-bulk');
  550. foreach ( (array)$_REQUEST['revert'] as $identifier ) {
  551. list($kind, $id) = explode('.', $identifier);
  552. switch ( $kind ) {
  553. case 'post':
  554. case 'page':
  555. delete_post_meta( $id, 'custom_permalink' );
  556. break;
  557. case 'tag':
  558. case 'category':
  559. custom_permalinks_delete_term($id);
  560. break;
  561. }
  562. }
  563.  
  564. // Redirect
  565. $redirectUrl = $_SERVER['REQUEST_URI'];
  566. ?>
  567. <script type="text/javascript">
  568. document.location = '<?php echo $redirectUrl ?>';
  569. </script>
  570. <?php
  571. }
  572.  
  573. ?>
  574. <div class="wrap">
  575. <h2><?php _e('Custom Permalinks', 'custom-permalinks') ?></h2>
  576.  
  577. <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
  578. <?php wp_nonce_field('custom-permalinks-bulk') ?>
  579.  
  580. <div class="tablenav">
  581. <div class="alignleft">
  582. <input type="submit" value="<?php _e('Revert', 'custom-permalinks'); ?>" name="revertit" class="button-secondary delete" />
  583. </div>
  584. <br class="clear" />
  585. </div>
  586. <br class="clear" />
  587. <table class="widefat">
  588. <thead>
  589. <tr>
  590. <th scope="col" class="check-column"><input type="checkbox" /></th>
  591. <th scope="col"><?php _e('Title', 'custom-permalinks') ?></th>
  592. <th scope="col"><?php _e('Type', 'custom-permalinks') ?></th>
  593. <th scope="col"><?php _e('Permalink', 'custom-permalinks') ?></th>
  594. </tr>
  595. </thead>
  596. <tbody>
  597. <?php
  598. $rows = custom_permalinks_admin_rows();
  599. foreach ( $rows as $row ) {
  600. ?>
  601. <tr valign="top">
  602. <th scope="row" class="check-column"><input type="checkbox" name="revert[]" value="<?php echo $row['id'] ?>" /></th>
  603. <td><strong><a class="row-title" href="<?php echo htmlspecialchars($row['editlink']) ?>"><?php echo htmlspecialchars($row['title']) ?></a></strong></td>
  604. <td><?php echo htmlspecialchars($row['type']) ?></td>
  605. <td><a href="<?php echo $row['permalink'] ?>" target="_blank" title="Visit <?php echo htmlspecialchars($row['title']) ?>">
  606. <?php echo htmlspecialchars(urldecode($row['permalink'])) ?>
  607. </a>
  608. </td>
  609. </tr>
  610. <?php
  611. }
  612. ?>
  613. </tbody>
  614. </table>
  615. </form>
  616. </div>
  617. <?php
  618. }
  619.  
  620. /**
  621. * Get rows for management view
  622. *
  623. * @package CustomPermalinks
  624. * @since 0.1
  625. */
  626. function custom_permalinks_admin_rows() {
  627. $rows = array();
  628.  
  629. // List tags/categories
  630. $table = get_option('custom_permalink_table');
  631. if ( $table && is_array($table) ) {
  632. foreach ( $table as $permalink => $info ) {
  633. $row = array();
  634. $term = get_term($info['id'], ($info['kind'] == 'tag' ? 'post_tag' : 'category'));
  635. $row['id'] = $info['kind'].'.'.$info['id'];
  636. $row['permalink'] = get_home_url()."/".$permalink;
  637. $row['type'] = ucwords($info['kind']);
  638. $row['title'] = $term->name;
  639. $row['editlink'] = ( $info['kind'] == 'tag' ? 'edit-tags.php?action=edit&tag_ID='.$info['id'] : 'categories.php?action=edit&cat_ID='.$info['id'] );
  640. $rows[] = $row;
  641. }
  642. }
  643.  
  644. // List posts/pages
  645. global $wpdb;
  646. $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE
  647. $wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != '';";
  648. $posts = $wpdb->get_results($query);
  649. foreach ( $posts as $post ) {
  650. $row = array();
  651. $row['id'] = 'post.'.$post->ID;
  652. $row['permalink'] = get_permalink($post->ID);
  653. $row['type'] = ucwords( $post->post_type );
  654. $row['title'] = $post->post_title;
  655. $row['editlink'] = 'post.php?action=edit&post='.$post->ID;
  656. $rows[] = $row;
  657. }
  658.  
  659. return $rows;
  660. }
  661.  
  662.  
  663. /**
  664. * Get original permalink for post
  665. *
  666. * @package CustomPermalinks
  667. * @since 0.1
  668. */
  669. function custom_permalinks_original_post_link($post_id) {
  670. remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook
  671. remove_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
  672. $originalPermalink = ltrim(str_replace(get_option('home'), '', get_permalink( $post_id )), '/');
  673. add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 ); // original hook
  674. add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
  675. return $originalPermalink;
  676. }
  677.  
  678. /**
  679. * Get original permalink for page
  680. *
  681. * @package CustomPermalinks
  682. * @since 0.4
  683. */
  684. function custom_permalinks_original_page_link($post_id) {
  685. remove_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
  686. remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
  687. $originalPermalink = ltrim(str_replace(get_home_url(), '', get_permalink( $post_id )), '/');
  688. add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
  689. add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
  690. return $originalPermalink;
  691. }
  692.  
  693.  
  694. /**
  695. * Get original permalink for tag
  696. *
  697. * @package CustomPermalinks
  698. * @since 0.1
  699. */
  700. function custom_permalinks_original_tag_link($tag_id) {
  701. remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
  702. remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
  703. $originalPermalink = ltrim(str_replace(get_home_url(), '', get_tag_link($tag_id)), '/');
  704. add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
  705. add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
  706. return $originalPermalink;
  707. }
  708.  
  709. /**
  710. * Get original permalink for category
  711. *
  712. * @package CustomPermalinks
  713. * @since 0.1
  714. */
  715. function custom_permalinks_original_category_link($category_id) {
  716. remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
  717. remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
  718. $originalPermalink = ltrim(str_replace(get_home_url(), '', get_category_link($category_id)), '/');
  719. add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
  720. add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
  721. return $originalPermalink;
  722. }
  723.  
  724. /**
  725. * Get permalink for term
  726. *
  727. * @package CustomPermalinks
  728. * @since 0.1
  729. */
  730. function custom_permalinks_permalink_for_term($id) {
  731. $table = get_option('custom_permalink_table');
  732. if ( $table )
  733. foreach ( $table as $link => $info ) {
  734. if ( $info['id'] == $id ) {
  735. return $link;
  736. }
  737. }
  738. return false;
  739. }
  740.  
  741. /**
  742. * Set up administration
  743. *
  744. * @package CustomPermalinks
  745. * @since 0.1
  746. */
  747. function custom_permalinks_setup_admin() {
  748. add_management_page( 'Custom Permalinks', 'Custom Permalinks', 5, 'custom_permalinks', 'custom_permalinks_options_page' );
  749. if ( is_admin() )
  750. wp_enqueue_script('admin-forms');
  751. }
  752.  
  753. if ( !function_exists("get_home_url") ) {
  754. function get_home_url() {
  755. return get_option('home');
  756. }
  757. }
  758.  
  759. $v = explode('.', get_bloginfo('version'));
  760.  
  761. //add_action( 'template_redirect', 'custom_permalinks_redirect', 5 ); // WPML Support
  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 ( $v[0] >= 2 ) {
  771. add_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 10, 4 );
  772. } else {
  773. add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
  774. add_action( 'edit_page_form', 'custom_permalinks_page_options' );
  775. }
  776.  
  777. add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
  778. add_action( 'add_tag_form', 'custom_permalinks_term_options' );
  779. add_action( 'edit_category_form', 'custom_permalinks_term_options' );
  780. add_action( 'save_post', 'custom_permalinks_save_post' );
  781. add_action( 'save_page', 'custom_permalinks_save_post' );
  782. add_action( 'edited_post_tag', 'custom_permalinks_save_tag' );
  783. add_action( 'edited_category', 'custom_permalinks_save_category' );
  784. add_action( 'create_post_tag', 'custom_permalinks_save_tag' );
  785. add_action( 'create_category', 'custom_permalinks_save_category' );
  786. add_action( 'delete_post', 'custom_permalinks_delete_permalink', 10);
  787. add_action( 'delete_post_tag', 'custom_permalinks_delete_term' );
  788. add_action( 'delete_post_category', 'custom_permalinks_delete_term' );
  789. add_action( 'admin_menu', 'custom_permalinks_setup_admin' );
  790.  
  791. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement