Advertisement
mbis

MegaLED

Jan 24th, 2019
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.46 KB | None | 0 0
  1. /**
  2.  * Display second level term (product category) in permalinks & breadcrumbs
  3.  */
  4. //ini_set('display_errors', 1);
  5. //ini_set('display_startup_errors', 1);
  6. //error_reporting(E_ALL);
  7.  
  8. function pm_get_product_term($post = null, $last_level_term = null) {
  9.     if(!empty($post)) {
  10.         $post = (is_numeric()) ? get_post($post) : $post;
  11.  
  12.         // Get last-level terms ancestors
  13.         $post_terms = (array) wp_get_post_terms($post->ID, 'product_cat', array('orderby' => 'term_id'));
  14.         $last_level_term = end($post_terms);
  15.  
  16.         // Use last-levet term in permalinks & breadcrumbs if the product is assigned to 'programmable-dmx-controllers' category
  17.         if(has_term(array('programmable-dmx-controllers', 'stand-alone-dmx-controllers'), 'product_cat', $post)) {
  18.             return $last_level_term;
  19.         }
  20.     }
  21.  
  22.     // Stop if no term set
  23.     if(empty($last_level_term->term_id)) { return ''; }
  24.  
  25.     $ancestors = get_ancestors($last_level_term->term_id, 'product_cat', 'taxonomy');
  26.  
  27.     // How many levels we have?
  28.     $count = count($ancestors);
  29.  
  30.     if($count >= 1) {
  31.         $replacement_term = get_term($ancestors[0], 'product_cat');
  32.     }
  33.  
  34.     return (!empty($replacement_term)) ? $replacement_term : '';
  35. }
  36.  
  37. function pm_shorten_category_base($replacement, $term, $element) {
  38.   if(!empty($element->ID) && $term->taxonomy == 'product_cat') {
  39.         // Get last-level terms ancestors
  40.         $replacement_term = pm_get_product_term($post);
  41.         $replacement = $replacement_term->slug;
  42.   } else {
  43.         $replacement_term = pm_get_product_term(null, $term);
  44.         $replacement = "{$replacement_term->slug}/{$term->slug}";
  45.     }
  46.  
  47.   return $replacement;
  48. }
  49. add_filter('permalink_manager_filter_term_slug', 'pm_shorten_category_base', 9, 3);
  50. add_filter('permalink_manager_filter_default_term_slug', 'pm_shorten_category_base', 9, 3);
  51.  
  52. function pm_filter_breadcrumbs($trail) {
  53.     global $post, $wp;
  54.  
  55.     $breadcrumbs = $trail->breadcrumbs;
  56.  
  57.     if(is_product()) {
  58.         $new_breadcrumbs[0] = $breadcrumbs[0];
  59.  
  60.         if(strpos(trim($wp->request, '/'), '/') !== false) {
  61.             $second_level_term = pm_get_product_term($post);
  62.             if(!empty($second_level_term->term_id)) {
  63.                 $new_breadcrumbs[1] = new bcn_breadcrumb($second_level_term->name, null, array(), get_term_link($second_level_term->term_id, $second_level_term->taxonomy), $second_level_term->term_id);
  64.             }
  65.         }
  66.     } else if(is_product_category()) {
  67.         $new_breadcrumbs[0] = $breadcrumbs[0];
  68.  
  69.         if(strpos(trim($wp->request, '/'), '/') !== false) {
  70.             $second_level_term = pm_get_product_term(null, get_queried_object());
  71.  
  72.             if(!empty($second_level_term->term_id)) {
  73.                 $new_breadcrumbs[1] = new bcn_breadcrumb($second_level_term->name, null, array(), get_term_link($second_level_term->term_id, $second_level_term->taxonomy), $second_level_term->term_id);
  74.             }
  75.         }
  76.     }
  77.  
  78.     // Set new breadcrumbs
  79.     if(!empty($new_breadcrumbs)) {
  80.         $trail->breadcrumbs = $new_breadcrumbs;
  81.     }
  82. }
  83. add_action('bcn_after_fill', 'pm_filter_breadcrumbs');
  84.  
  85. function pm_redirect_posts_from_404() {
  86.     global $wp, $wpdb, $wp_query, $pm_query;
  87.  
  88.     if(is_front_page() || is_home()) {
  89.         return;
  90.     }
  91.  
  92.     if(is_404() || (empty($pm_query))) {
  93.         $slug = basename($wp->request);
  94.  
  95.         $element = $wpdb->get_row($wpdb->prepare("
  96.             SELECT * FROM {$wpdb->posts}
  97.             WHERE post_name = %s
  98.             AND post_status = 'publish'
  99.             ",
  100.             $slug
  101.         ));
  102.  
  103.         if(!empty($element->ID)) {
  104.             $url = get_permalink($element->ID);
  105.         }
  106.  
  107.         if(!empty($url)) {
  108.             wp_safe_redirect($url, 301);
  109.             exit();
  110.         }
  111.     }
  112. }
  113. add_action('template_redirect', 'pm_redirect_posts_from_404', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement