Advertisement
Guest User

Untitled

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