diff --git a/README.md b/README.md index 48eaab0..5d2978c 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,39 @@ within that category. > If anyone wants the different Structure Tags for their Post types or use symbols in the URLs So, use the [Permalinks Customizer](https://wordpress.org/plugins/permalinks-customizer/) which is a fork of this plugin and contains the enhancement of this plugin. +## Filters + +If you want to exclude some Permalink to processed with the plugin so, just add the filter looks like this: +``` +function check_xml_sitemap_url( $permalink ) { + if ( false !== strpos( $permalink, 'sitemap.xml' )) { + return '__true'; + } + return; +} +add_filter( 'custom_permalinks_request_ignore', 'check_xml_sitemap_url' ); +``` + +If you want to exclude permalink from any post type so, use `custom_permalinks_exclude_post_type` filter. + +`custom_permalinks_exclude_post_type` filter looks like this: +``` +function yasglobal_exclude_post_types( $post_type ) { + if ( $post_type == 'custompost' ) { + return '__true'; + } + return '__false'; +} +add_filter( 'custom_permalinks_exclude_post_type', 'yasglobal_exclude_post_types'); +``` +Note: `custom_permalinks_exclude_post_type` doesn't work on the posts permalink which has been created previously. + +## Thanks for the Support! + +The support from the users that love Custom Permalinks is huge. You can support Custom Permalinks's future development and help to make it even better by donating or even giving a 5 star rating with a nice message to me :) + +[Donate to Custom Permalinks](https://www.paypal.me/yasglobal) + ## Installation 1. Unzip the package, and upload `custom-permalinks` to the `/wp-content/plugins/` directory diff --git a/custom-permalinks-main.php b/custom-permalinks-main.php index 1cc5361..8349bad 100644 --- a/custom-permalinks-main.php +++ b/custom-permalinks-main.php @@ -15,7 +15,7 @@ exit(); } -define( 'CUSTOM_PERMALINKS_PLUGIN_VERSION', '1.2.4' ); +define( 'CUSTOM_PERMALINKS_PLUGIN_VERSION', '1.2.6' ); if ( ! defined( 'CUSTOM_PERMALINKS_PATH' ) ) { define( 'CUSTOM_PERMALINKS_PATH', plugin_dir_path( __FILE__ ) ); diff --git a/custom-permalinks.php b/custom-permalinks.php index daba7c3..3c348df 100644 --- a/custom-permalinks.php +++ b/custom-permalinks.php @@ -4,7 +4,7 @@ * Plugin Name: Custom Permalinks * Plugin URI: https://wordpress.org/plugins/custom-permalinks/ * Description: Set custom permalinks on a per-post basis - * Version: 1.2.5 + * Version: 1.2.6 * Author: Sami Ahmed Siddiqui * Author URI: https://www.yasglobal.com/web-design-development/wordpress/custom-permalinks/ * Donate link: https://www.paypal.me/yasglobal diff --git a/frontend/class-custom-permalinks-form.php b/frontend/class-custom-permalinks-form.php index bfc257a..53b4893 100644 --- a/frontend/class-custom-permalinks-form.php +++ b/frontend/class-custom-permalinks-form.php @@ -69,7 +69,14 @@ public function custom_permalinks_get_sample_permalink_html( $html, $id, $new_ti if ( $post->post_type == 'attachment' || $post->ID == get_option( 'page_on_front' ) ) { return $html; } - ob_start(); + + $exclude_post_types = $post->post_type; + $excluded = apply_filters( 'custom_permalinks_exclude_post_type', $exclude_post_types ); + if ( '__true' === $excluded ) { + return $html; + } + + ob_start(); require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' ); $custom_permalinks_frontend = new Custom_Permalinks_Frontend(); diff --git a/frontend/class-custom-permalinks-frontend.php b/frontend/class-custom-permalinks-frontend.php index ba527b2..0523a19 100644 --- a/frontend/class-custom-permalinks-frontend.php +++ b/frontend/class-custom-permalinks-frontend.php @@ -39,7 +39,10 @@ public function custom_permalinks_request( $query ) { $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 ); + $pos = strpos( $request, '?' ); + if ( $pos ) { + $request = substr( $request, 0, $pos ); + } if ( ! $request ) { return $query; @@ -58,18 +61,29 @@ public function custom_permalinks_request( $query ) { } $request_noslash = preg_replace( '@/+@','/', trim( $request, '/' ) ); - $sql = $wpdb->prepare( "SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type, $wpdb->posts.post_status FROM $wpdb->posts " . - " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE " . + $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status " . + " FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON (pm.post_id = p.ID) " . + " WHERE pm.meta_key = 'custom_permalink' " . + " AND (pm.meta_value = '%s' OR pm.meta_value = '%s') " . + " AND p.post_status != 'trash' AND p.post_type != 'nav_menu_item' " . + " LIMIT 1", $request_noslash, $request_noslash . "/" ); + + $posts = $wpdb->get_results( $sql ); + + if ( ! $posts ) { + $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status FROM $wpdb->posts AS p " . + " LEFT JOIN $wpdb->postmeta AS pm ON (p.ID = pm.post_id) WHERE " . " meta_key = 'custom_permalink' AND meta_value != '' AND " . " ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR " . " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) " . " AND post_status != 'trash' AND post_type != 'nav_menu_item'" . " ORDER BY LENGTH(meta_value) DESC, " . " FIELD(post_status,'publish','private','draft','auto-draft','inherit')," . - " FIELD(post_type,'post','page'), $wpdb->posts.ID ASC LIMIT 1", - $request_noslash, $request_noslash . "/" ); + " FIELD(post_type,'post','page'), p.ID ASC LIMIT 1", + $request_noslash, $request_noslash . "/" ); - $posts = $wpdb->get_results( $sql ); + $posts = $wpdb->get_results( $sql ); + } if ( $posts ) { // A post matches our request @@ -81,9 +95,9 @@ public function custom_permalinks_request( $query ) { if ( $posts[0]->post_status == 'draft' ) { if ( $posts[0]->post_type == 'page' ) { - $original_url = "?page_id=" . $posts[0]->ID; + $original_url = '?page_id=' . $posts[0]->ID; } else { - $original_url = "?post_type=".$posts[0]->post_type."&p=" . $posts[0]->ID; + $original_url = '?post_type=' . $posts[0]->post_type . '&p=' . $posts[0]->ID; } } else { $post_meta = trim( strtolower( $posts[0]->meta_value ), '/' ); @@ -110,7 +124,7 @@ public function custom_permalinks_request( $query ) { foreach ( array_keys( $table ) as $permalink ) { if ( $permalink == substr( $request_noslash, 0, strlen( $permalink ) ) - || $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) @@ -172,11 +186,17 @@ public function custom_permalinks_request( $query ) { * Action to redirect to the custom permalink */ public function custom_permalinks_redirect() { + + global $wpdb; + + $custom_permalink = ''; + $original_permalink = ''; + // 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 ) ), '/' ); - $pos = strpos( $request, "?" ); + $pos = strpos( $request, '?' ); if ( $pos ) { $request = substr( $request, 0, $pos ); } @@ -186,34 +206,66 @@ public function custom_permalinks_redirect() { $custom_permalinks_form = new Custom_Permalinks_Form(); $request = $custom_permalinks_form->custom_permalinks_check_conflicts( $request ); } + $request_noslash = preg_replace( '@/+@','/', trim( $request, '/' ) ); - global $wp_query; - $custom_permalink = ''; - $original_permalink = ''; + $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status " . + " FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON (pm.post_id = p.ID) " . + " WHERE pm.meta_key = 'custom_permalink' " . + " AND (pm.meta_value = '%s' OR pm.meta_value = '%s') " . + " AND p.post_status != 'trash' AND p.post_type != 'nav_menu_item' " . + " LIMIT 1", $request_noslash, $request_noslash . "/" ); + $posts = $wpdb->get_results( $sql ); - // If the post/tag/category we're on has a custom permalink, get it and check against the request - if ( ( is_single() || is_page() ) && ! empty( $wp_query->post ) ) { - $post = $wp_query->post; - $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true ); - if ( $post->post_type == 'page' ) { - $original_permalink = $this->custom_permalinks_original_page_link( $post->ID ); - } else { - $original_permalink = $this->custom_permalinks_original_post_link( $post->ID ); + if ( ! $posts ) { + $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status FROM $wpdb->posts AS p " . + " LEFT JOIN $wpdb->postmeta AS pm ON (p.ID = pm.post_id) WHERE " . + " meta_key = 'custom_permalink' AND meta_value != '' AND " . + " ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR " . + " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) " . + " AND post_status != 'trash' AND post_type != 'nav_menu_item'" . + " ORDER BY LENGTH(meta_value) DESC, " . + " FIELD(post_status,'publish','private','draft','auto-draft','inherit')," . + " FIELD(post_type,'post','page'), p.ID ASC LIMIT 1", + $request_noslash, $request_noslash . "/" ); + + $posts = $wpdb->get_results( $sql ); + } + + if ( ! isset( $posts[0]->ID ) || ! isset( $posts[0]->meta_value ) + && empty( ($posts[0]->meta_value)) ) { + global $wp_query; + + // If the post/tag/category we're on has a custom permalink, get it and check against the request + if ( ( is_single() || is_page() ) && ! empty( $wp_query->post ) ) { + $post = $wp_query->post; + $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true ); + if ( $post->post_type == 'page' ) { + $original_permalink = $this->custom_permalinks_original_page_link( $post->ID ); + } else { + $original_permalink = $this->custom_permalinks_original_post_link( $post->ID ); + } + } elseif ( is_tag() || is_category() ) { + $theTerm = $wp_query->get_queried_object(); + $custom_permalink = $this->custom_permalinks_permalink_for_term( $theTerm->term_id ); + if ( is_tag() ) { + $original_permalink = $this->custom_permalinks_original_tag_link( $theTerm->term_id ); + } else { + $original_permalink = $this->custom_permalinks_original_category_link( $theTerm->term_id ); + } } - } elseif ( is_tag() || is_category() ) { - $theTerm = $wp_query->get_queried_object(); - $custom_permalink = $this->custom_permalinks_permalink_for_term( $theTerm->term_id ); - if ( is_tag() ) { - $original_permalink = $this->custom_permalinks_original_tag_link( $theTerm->term_id ); + } else { + $custom_permalink = $posts[0]->meta_value; + if ( $posts[0]->post_type == 'page' ) { + $original_permalink = $this->custom_permalinks_original_page_link( $posts[0]->ID ); } else { - $original_permalink = $this->custom_permalinks_original_category_link( $theTerm->term_id ); + $original_permalink = $this->custom_permalinks_original_post_link( $posts[0]->ID ); } } if ( $custom_permalink && ( substr( $request, 0, strlen( $custom_permalink ) ) != $custom_permalink - || $request == $custom_permalink . "/" ) ) { + || $request == $custom_permalink . '/' ) ) { // Request doesn't match permalink - redirect $url = $custom_permalink; @@ -226,9 +278,9 @@ public function custom_permalinks_redirect() { } // Append any query compenent - $url .= strstr( $_SERVER['REQUEST_URI'], "?" ); + $url .= strstr( $_SERVER['REQUEST_URI'], '?' ); - wp_redirect( home_url() . "/" . $url, 301 ); + wp_redirect( home_url() . '/' . $url, 301 ); exit(); } } @@ -242,9 +294,9 @@ public function custom_permalinks_post_link( $permalink, $post ) { $post_type = isset( $post->post_type ) ? $post->post_type : 'post'; $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $post->ID, 'element_type' => $post_type ) ); if ( $language_code ) - return apply_filters( 'wpml_permalink', home_url() . "/" . $custom_permalink, $language_code ); + return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink, $language_code ); else - return apply_filters( 'wpml_permalink', home_url() . "/" . $custom_permalink ); + return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink ); } return $permalink; @@ -258,9 +310,9 @@ public function custom_permalinks_page_link( $permalink, $page ) { if ( $custom_permalink ) { $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $page, 'element_type' => 'page' ) ); if ( $language_code ) - return apply_filters( 'wpml_permalink', home_url() . "/" . $custom_permalink, $language_code ); + return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink, $language_code ); else - return apply_filters( 'wpml_permalink', home_url() . "/" . $custom_permalink ); + return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink ); } return $permalink; @@ -281,9 +333,9 @@ public function custom_permalinks_term_link( $permalink, $term ) { if ( isset( $taxonomy ) && isset( $taxonomy->term_taxonomy_id ) ) { $term_type = isset( $taxonomy->taxonomy ) ? $taxonomy->taxonomy : 'category'; $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $taxonomy->term_taxonomy_id, 'element_type' => $term_type ) ); - return apply_filters( 'wpml_permalink', home_url() . "/" . $custom_permalink, $language_code ); + return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink, $language_code ); } else { - return apply_filters( 'wpml_permalink', home_url() . "/" . $custom_permalink ); + return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink ); } } diff --git a/readme.txt b/readme.txt index b2500aa..f234914 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Donate link: https://www.paypal.me/yasglobal Tags: permalink, url, link, address, custom, redirect, custom post type Requires at least: 2.6 Tested up to: 4.8 -Stable tag: 1.2.5 +Stable tag: 1.2.6 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -26,7 +26,7 @@ within that category. > If anyone wants the different Structure Tags for their Post types or use symbols in the URLs So, use the [Permalinks Customizer](https://wordpress.org/plugins/permalinks-customizer/) which is a fork of this plugin and contains the enhancement of this plugin. -== Filter == +== Filters == If you want to exclude some Permalink to processed with the plugin so, just add the filter looks like this: ` @@ -39,6 +39,26 @@ function check_xml_sitemap_url( $permalink ) { add_filter( 'custom_permalinks_request_ignore', 'check_xml_sitemap_url' ); ` +If you want to exclude permalink from any post type so, use `custom_permalinks_exclude_post_type` filter. + +`custom_permalinks_exclude_post_type` filter looks like this: +` +function yasglobal_exclude_post_types( $post_type ) { + if ( $post_type == 'custompost' ) { + return '__true'; + } + return '__false'; +} +add_filter( 'custom_permalinks_exclude_post_type', 'yasglobal_exclude_post_types'); +` +Note: `custom_permalinks_exclude_post_type` doesn't work on the posts permalink which has been created previously. + +== Thanks for the Support! == + +The support from the users that love Custom Permalinks is huge. You can support Custom Permalinks's future development and help to make it even better by donating or even giving a 5 star rating with a nice message to me :) + +[Donate to Custom Permalinks](https://www.paypal.me/yasglobal) + == Installation == 1. Unzip the package, and upload `custom-permalinks` to the `/wp-content/plugins/` directory @@ -47,6 +67,14 @@ add_filter( 'custom_permalinks_request_ignore', 'check_xml_sitemap_url' ); == Changelog == += 1.2.6 = + + * Enhancements + * Added Filter to Exclude Post types + * Bugs + * Fixed Query Issue on parse_request + * Resolving Issues with Cornerstone + = 1.2.5 = * Fixed Category/Tag Update Issue + Typo on Admin Page