Advertisement
mbis

Remove WooCommerce rewrite bases

May 5th, 2020 (edited)
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.00 KB | None | 0 0
  1. /**
  2.  * Detect posts
  3.  */
  4. function pm_detect_woo_permalinks($query) {
  5.     global $wpdb, $pm_query, $wp, $wp_rewrite;
  6.  
  7.     // Do not run when Elementor is opened
  8.     if((!empty($_REQUEST['action']) && strpos($_REQUEST['action'], 'elementor') !== false) || isset($_REQUEST['elementor-preview'])) {
  9.         return $query;
  10.     }
  11.  
  12.     // Do not run if custom permalink was detected
  13.     if(!empty($pm_query['id'])) {
  14.         return $query;
  15.     }
  16.  
  17.     // 1. Get the slug
  18.     preg_match("/([^\/]+)(?:\/{$wp_rewrite->pagination_base}\/([\d]+))?$/", $wp->request, $parts);
  19.  
  20.     if(!empty($parts[1])) {
  21.         $slug = basename($parts[1]);
  22.  
  23.         // 2A. Check if the slug is assigned to any post item
  24.         $sql_query = "SELECT post_name, post_type FROM {$wpdb->posts} WHERE 1=1 AND post_name = %s AND post_type = 'product'";
  25.         $post = $wpdb->get_row($wpdb->prepare($sql_query, array($slug)));
  26.  
  27.         if(!empty($post->post_name)) {
  28.             $page = (!empty($query['page'])) ? $query['page'] : 1;
  29.  
  30.             $new_query = array(
  31.                 'post_type' => $post->post_type,
  32.                 $post->post_type => $post->post_name,
  33.                 'name' => $post->post_name,
  34.                 // 'page' => $page,
  35.                 'do_not_redirect' => 1,
  36.             );
  37.         }
  38.         // 2B. Check if the slug is assigned to any post item
  39.         else {
  40.             $sql_query = "SELECT t.slug, t.term_id, tt.taxonomy FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON(t.term_id = tt.term_id) WHERE 1=1 AND slug = %s";
  41.             $term = $wpdb->get_row($wpdb->prepare($sql_query, array($slug)));
  42.  
  43.             if(!empty($term->taxonomy)) {
  44.                 $page = (!empty($parts[2])) ? (int) $parts[2] : 1;
  45.  
  46.                 $new_query = array(
  47.                     'term' => $term->slug,
  48.                     $term->taxonomy => $term->slug,
  49.                     'taxonomy' => $term->taxonomy,
  50.                     'paged' => $page
  51.                 );
  52.             }
  53.         }
  54.  
  55.         // 3. Overwrite the query object & Disable canonical redirect
  56.         if(!empty($new_query)) {
  57.             remove_action('template_redirect', 'wp_old_slug_redirect');
  58.             remove_action('template_redirect', 'redirect_canonical');
  59.             add_filter('wpml_is_redirected', '__return_false', 99, 2);
  60.             add_filter('pll_check_canonical_url', '__return_false', 99, 2);
  61.  
  62.             $query = $new_query;
  63.         }
  64.     }
  65.  
  66.     return $query;
  67. }
  68. add_filter('request', 'pm_detect_woo_permalinks', 9999);
  69.  
  70. function bis_filter_woo_permalinks($url, $element) {
  71.     if(!empty($element->post_type) && ($element->post_type == 'product')) {
  72.         $url = trim(get_option('home'), '/') . "/{$element->post_name}";
  73.     } else if(!empty($element->taxonomy) && (in_array($element->taxonomy, array('product_cat', 'product_tag')))) {
  74.         $woocommerce_permalinks = get_option('woocommerce_permalinks');
  75.         $url = (!empty($woocommerce_permalinks['category_base'])) ? str_replace(array("{$woocommerce_permalinks['category_base']}/", "{$woocommerce_permalinks['tag_base']}/"), '', $url) : $url;
  76.     }
  77.  
  78.     return $url;
  79. }
  80. add_filter('post_type_link', 'bis_filter_woo_permalinks', 999, 2);
  81. add_filter('term_link', 'bis_filter_woo_permalinks', 999, 2);
  82.  
  83. function pm_redirect_old_woo_permalinks() {
  84.     global $wp, $wp_query, $wp_rewrite;
  85.  
  86.     if(empty($wp->request)) { return; }
  87.  
  88.     $woocommerce_permalinks = get_option('woocommerce_permalinks');
  89.  
  90.     // A. Redirect old product permalinks
  91.     if(is_singular('product') && !empty($woocommerce_permalinks['product_base'])) {
  92.         $product_permalink_base = preg_quote(preg_replace('/(%[^%]+%)/', '', $woocommerce_permalinks['product_base']), "/");
  93.  
  94.         if(preg_match("/^({$product_permalink_base})/", $wp->request)) {
  95.             $url = get_permalink();
  96.         }
  97.     }
  98.  
  99.     // B. Redirect old category permalinks
  100.     if(is_tax('product_cat') && !empty($woocommerce_permalinks['category_base'])) {
  101.         $product_cat_permalink_base = preg_quote(preg_replace('/(%[^%]+%)/', '', $woocommerce_permalinks['category_base']), "/");
  102.  
  103.         if(preg_match("/^({$product_cat_permalink_base})/", $wp->request)) {
  104.             $term = get_queried_object();
  105.             $url = get_term_link($term, $term->taxonomy);
  106.         }
  107.     }
  108.  
  109.     // C. Redirect old tag permalinks
  110.     if(is_tax('product_tag') && !empty($woocommerce_permalinks['tag_base'])) {
  111.         $product_tag_permalink_base = preg_quote(preg_replace('/(%[^%]+%)/', '', $woocommerce_permalinks['tag_base']), "/");
  112.  
  113.         if(preg_match("/^({$product_tag_permalink_base})/", $wp->request)) {
  114.             $term = get_queried_object();
  115.             $url = get_term_link($term, $term->taxonomy);
  116.         }
  117.     }
  118.  
  119.     // Do the redirect
  120.     if(!empty($url)) {
  121.         // Support pagination
  122.         $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  123.  
  124.         if(!empty($paged) && $paged > 1) {
  125.             $url = sprintf("%s/%s/%s", trim($url, "/"), $wp_rewrite->pagination_base, $paged);
  126.             $url = user_trailingslashit($url);
  127.         }
  128.  
  129.         wp_safe_redirect($url, 301);
  130.         exit();
  131.     }
  132. }
  133. add_action('template_redirect', 'pm_redirect_old_woo_permalinks', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement