This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /** ** Actions and filters ** **/ /** * Filter to replace the post permalink with the custom one * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_post_link($permalink, $post) { $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true ); if ( $custom_permalink ) { return get_home_url()."/".$custom_permalink; } return $permalink; } /** * Filter to replace the page permalink with the custom one * * @package CustomPermalinks * @since 0.4 */ function custom_permalinks_page_link($permalink, $page) { $custom_permalink = get_post_meta( $page, 'custom_permalink', true ); if ( $custom_permalink ) { return get_home_url()."/".$custom_permalink; } return $permalink; } /** * Filter to replace the term permalink with the custom one * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_term_link($permalink, $term) { $table = get_option('custom_permalink_table'); if ( is_object($term) ) $term = $term->term_id; $custom_permalink = custom_permalinks_permalink_for_term($term); if ( $custom_permalink ) { return get_home_url()."/".$custom_permalink; } return $permalink; } /** * Action to redirect to the custom permalink * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_redirect() { // Get request URI, strip parameters $url = parse_url(get_bloginfo('url')); $url = isset($url['path']) ? $url['path'] : ''; $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/'); if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos); global $wp_query; $custom_permalink = ''; $original_permalink = ''; // If the post/tag/category we're on has a custom permalink, get it and check against the request if ( is_single() || is_page() ) { $post = $wp_query->post; $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true ); $original_permalink = ( $post->post_type == 'page' ? custom_permalinks_original_page_link( $post->ID ) : custom_permalinks_original_post_link( $post->ID ) ); } else if ( is_tag() || is_category() ) { $theTerm = $wp_query->get_queried_object(); $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id); $original_permalink = (is_tag() ? custom_permalinks_original_tag_link($theTerm->term_id) : custom_permalinks_original_category_link($theTerm->term_id)); } if ( $custom_permalink && (substr($request, 0, strlen($custom_permalink)) != $custom_permalink || $request == $custom_permalink."/" ) ) { // Request doesn't match permalink - redirect $url = $custom_permalink; if ( substr($request, 0, strlen($original_permalink)) == $original_permalink && trim($request,'/') != trim($original_permalink,'/') ) { // This is the original link; we can use this url to derive the new one $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request)); $url = preg_replace('@([^?]*)&@', '\1?', $url); } // Append any query compenent $url .= strstr($_SERVER['REQUEST_URI'], "?"); wp_redirect( get_home_url()."/".$url, 301 ); exit(); } } /** * Filter to rewrite the query if we have a matching post * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_request($query) { global $wpdb; global $_CPRegisteredURL; // First, search for a matching custom permalink, and if found, generate the corresponding // original URL $originalUrl = NULL; // Get request URI, strip parameters and /'s $url = parse_url(get_bloginfo('url')); $url = isset($url['path']) ? $url['path'] : ''; $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/'); $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request); $request_noslash = preg_replace('@/+@','/', trim($request, '/')); if ( !$request ) return $query; $sql = "SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts ". "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ". " meta_key = 'custom_permalink' AND ". " meta_value != '' AND ". " ( LOWER(meta_value) = LEFT(LOWER('".mysql_escape_string($request_noslash)."'), LENGTH(meta_value)) OR ". " LOWER(meta_value) = LEFT(LOWER('".mysql_escape_string($request_noslash."/")."'), LENGTH(meta_value)) ) ". "ORDER BY LENGTH(meta_value) DESC LIMIT 1"; $posts = $wpdb->get_results($sql); if ( $posts ) { // A post matches our request // Preserve this url for later if it's the same as the permalink (no extra stuff) if ( $request_noslash == trim($posts[0]->meta_value,'/') ) $_CPRegisteredURL = $request; $originalUrl = preg_replace( '@/+@', '/', str_replace( trim( strtolower($posts[0]->meta_value),'/' ), ( $posts[0]->post_type == 'page' ? custom_permalinks_original_page_link($posts[0]->ID) : custom_permalinks_original_post_link($posts[0]->ID) ), strtolower($request_noslash) ) ); } if ( $originalUrl === NULL ) { // See if any terms have a matching permalink $table = get_option('custom_permalink_table'); if ( !$table ) return $query; foreach ( array_keys($table) as $permalink ) { if ( $permalink == substr($request_noslash, 0, strlen($permalink)) || $permalink == substr($request_noslash."/", 0, strlen($permalink)) ) { $term = $table[$permalink]; // Preserve this url for later if it's the same as the permalink (no extra stuff) if ( $request_noslash == trim($permalink,'/') ) $_CPRegisteredURL = $request; if ( $term['kind'] == 'category') { $originalUrl = str_replace(trim($permalink,'/'), custom_permalinks_original_category_link($term['id']), trim($request,'/')); } else { $originalUrl = str_replace(trim($permalink,'/'), custom_permalinks_original_tag_link($term['id']), trim($request,'/')); } } } } if ( $originalUrl !== NULL ) { $originalUrl = str_replace('//', '/', $originalUrl); if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) { $queryVars = substr($_SERVER['REQUEST_URI'], $pos+1); $originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars; } // Now we have the original URL, run this back through WP->parse_request, in order to // parse parameters properly. We set $_SERVER variables to fool the function. $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING']; $_SERVER['REQUEST_URI'] = '/'.ltrim($originalUrl,'/'); $_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : ''); parse_str($_SERVER['QUERY_STRING'], $queryArray); $oldValues = array(); if ( is_array($queryArray) ) foreach ( $queryArray as $key => $value ) { $oldValues[$key] = $_REQUEST[$key]; $_REQUEST[$key] = $_GET[$key] = $value; } // Re-run the filter, now with original environment in place remove_filter( 'request', 'custom_permalinks_request', 10, 1 ); global $wp; $wp->parse_request(); $query = $wp->query_vars; add_filter( 'request', 'custom_permalinks_request', 10, 1 ); // Restore values $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString; foreach ( $oldValues as $key => $value ) { $_REQUEST[$key] = $value; } } return $query; } /** * Filter to handle trailing slashes correctly * * @package CustomPermalinks * @since 0.3 */ function custom_permalinks_trailingslash($string, $type) { global $_CPRegisteredURL; $url = parse_url(get_bloginfo('url')); $request = ltrim(substr($string, strlen($url['path'])),'/'); if ( !trim($request) ) return $string; if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) { return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL; } return $string; } /** ** Administration ** **/ /** * Per-post/page options (Wordpress > 2.9) * * @package CustomPermalinks * @since 0.6 */ function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) { $permalink = get_post_meta( $id, 'custom_permalink', true ); $post = &get_post($id); if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) { $permalink = $matches[1]; } else { list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug); if ( false !== strpos($permalink, '%postname%') || false !== strpos($permalink, '%pagename%') ) { $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink); } } ob_start(); custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false, $post->ID); $content = ob_get_contents(); ob_end_clean(); if ( 'publish' == $post->post_status ) { $view_post = 'page' == $post->post_type ? __('View Page') : __('View Post'); } return '' . __('Permalink:') . "\n" . $content . ( isset($view_post) ? "$view_post\n" : "" ); } /** * Per-post options (Wordpress < 2.9) * * @package CustomPermalinks * @since 0.1 */ function custom_permalinks_post_options() { global $post; $post_id = $post; if (is_object($post_id)) { $post_id = $post_id->ID; } $permalink = get_post_meta( $post_id, 'custom_permalink', true ); ?>
/
|