Advertisement
mbis

Rewrite CPT permalinks (alt way)

Jan 26th, 2023
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.29 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Plugin Name: Rewrite "Hersteller" & "Dienstleistungen" permalinks
  5.  * Plugin URI: http://maciejbis.net
  6.  * Description: A plugin that adjusts the functionality of this website
  7.  * Version: 1.0
  8.  * Author: Maciej Bis
  9.  * Author URI: http://www.maciejbis.net
  10.  * License: GPL2
  11.  */
  12.  
  13. /**
  14.  * Detect ad permalinks
  15.  */
  16. function bis_detect_cpt_permalinks( $query ) {
  17.     global $wpdb, $pm_query, $wp, $wp_rewrite;
  18.  
  19.     // 1. Extract the slugs
  20.     preg_match( '/datenrettung-([^\/]+)\/(([^\/]*)(-[^-\/]+))(?:\/|$)/Ui', $wp->request, $parts );
  21.  
  22.     if ( ! empty( $parts[2] ) ) {
  23.         // 2A. Get all posts that have exact or similar slug extracted from URL
  24.         $post_slug     = basename( $parts[2] );
  25.         $post_slug_alt = basename( $parts[3] );
  26.  
  27.         $sql_query = sprintf( 'SELECT ID FROM %1$s WHERE 1=1 AND
  28.         (
  29.             (post_type = "dienstleistung" AND (post_name = "%2$s" OR post_name LIKE "%2$s-%%")) OR
  30.             (post_type = "hersteller" AND (post_name = "%3$s" OR post_name LIKE "%3$s-%%"))
  31.         )
  32.         ', $wpdb->posts, esc_sql( $post_slug ), esc_sql( $post_slug_alt ) );
  33.         $url_posts = $wpdb->get_col( $sql_query );
  34.  
  35.         // 2B. Get all posts that are assigned to a "ort" term extracted from URL
  36.         $term_slug = basename( $parts[1] );
  37.  
  38.         $sql_query = sprintf( 'SELECT object_id FROM %s AS tr LEFT JOIN %s AS t ON tr.term_taxonomy_id = t.term_id WHERE t.slug = "%s"', $wpdb->term_relationships, $wpdb->terms, esc_sql( $term_slug ) );
  39.  
  40.         $ort_posts = $wpdb->get_col( $sql_query );
  41.  
  42.         // 2C. Check what posts are in both arrays (there should be only one post)
  43.         if ( ! empty( $url_posts ) && ! empty( $ort_posts ) ) {
  44.             $found_posts = array_intersect( $url_posts, $ort_posts );
  45.  
  46.             if ( ! empty( $found_posts ) ) {
  47.                 $post_id = (int) reset( $found_posts );
  48.                 $post    = get_post( $post_id );
  49.             }
  50.         }
  51.  
  52.         if ( ! empty( $post->post_name ) ) {
  53.             $new_query = array(
  54.                 'post_type'       => $post->post_type,
  55.                 $post->post_type  => $post->post_name,
  56.                 'name'            => $post->post_name,
  57.                 'do_not_redirect' => 1,
  58.             );
  59.         }
  60.  
  61.         // 3. Debug the query parameters
  62.         if ( isset( $_GET['debug_bis_query'] ) ) {
  63.             printf( '<pre>%s</pre> <pre>%s</pre> <pre>%s</pre>', print_r( $parts, true ), print_r( $query, true ), print_r( $new_query, true ) );
  64.             die();
  65.         }
  66.  
  67.         // 4. Overwrite the query object & Disable canonical redirect
  68.         if ( ! empty( $new_query ) ) {
  69.             remove_action( 'template_redirect', 'wp_old_slug_redirect' );
  70.             remove_action( 'template_redirect', 'redirect_canonical' );
  71.             add_filter( 'wpml_is_redirected', '__return_false', 99, 2 );
  72.             add_filter( 'pll_check_canonical_url', '__return_false', 99, 2 );
  73.  
  74.             $query = $new_query;
  75.         }
  76.     }
  77.  
  78.     return $query;
  79. }
  80.  
  81. add_filter( 'request', 'bis_detect_cpt_permalinks', 9999 );
  82.  
  83. /**
  84.  * Rewrite permalinks
  85.  */
  86. function pm_filter_cpt_permalinks( $permalink, $post ) {
  87.     if ( empty( $post->post_type ) || $post->post_status !== 'publish' ) {
  88.         return $permalink;
  89.     }
  90.  
  91.     if ( in_array( $post->post_type, array( 'hersteller', 'dienstleistung' ) ) ) {
  92.         // Get the slug of assigned 'orte' term
  93.         $o_terms       = wp_get_object_terms( $post->ID, 'orte' );
  94.         $o_lowest_term = ( ! is_wp_error( $o_terms ) && ! empty( $o_terms ) && is_object( $o_terms[0] ) ) ? pm_get_lowest_element( $o_terms[0], $o_terms ) : "";
  95.         $o_slug        = ( ! empty( $o_lowest_term->slug ) ) ? $o_lowest_term->slug : '';
  96.  
  97.         // A. Hersteller (datenrettung-%orte_flat%/%hersteller%-%herstellerkategorie%)
  98.         if ( $post->post_type == 'hersteller' ) {
  99.  
  100.             // Get the slug of assigned 'herstellerkategorie' term
  101.             $h_terms       = wp_get_object_terms( $post->ID, 'herstellerkategorie' );
  102.             $h_lowest_term = ( ! is_wp_error( $h_terms ) && ! empty( $h_terms ) && is_object( $h_terms[0] ) ) ? pm_get_lowest_element( $h_terms[0], $h_terms ) : "";
  103.             $h_slug        = ( ! empty( $h_lowest_term->slug ) ) ? $h_lowest_term->slug : '';
  104.  
  105.             $new_permalink = sprintf( '%s/datenrettung-%s/%s-%s', trim( get_option( 'home' ), '/' ), $o_slug, sanitize_title( $post->post_title ), $h_slug );
  106.         }
  107.  
  108.         // B. Datenrettung (datenrettung-%orte_flat%/%dienstleistung%)
  109.         if ( $post->post_type == 'dienstleistung' ) {
  110.             $new_permalink = sprintf( '%s/datenrettung-%s/%s', trim( get_option( 'home' ), '/' ), $o_slug, sanitize_title( $post->post_title ) );
  111.         }
  112.  
  113.         $permalink = user_trailingslashit( $new_permalink );
  114.     }
  115.  
  116.     return $permalink;
  117. }
  118.  
  119. add_filter( 'post_type_link', 'pm_filter_cpt_permalinks', 1000, 2 );
  120.  
  121. /**
  122.  * Get the lowest-level term
  123.  */
  124. function pm_get_lowest_element( $first_element, $elements ) {
  125.     if ( ! empty( $elements ) && ! empty( $first_element ) ) {
  126.         // Get the ID of first element
  127.         if ( ! empty( $first_element->term_id ) ) {
  128.             $first_element_id = $first_element->term_id;
  129.             $parent_key       = 'parent';
  130.         } else if ( ! empty( $first_element->ID ) ) {
  131.             $first_element_id = $first_element->ID;
  132.             $parent_key       = 'post_parent';
  133.         } else if ( is_numeric( $first_element ) ) {
  134.             $first_element_id = $first_element;
  135.             $parent_key       = 'post_parent';
  136.         } else {
  137.             return false;
  138.         }
  139.  
  140.         $children = wp_filter_object_list( $elements, array( $parent_key => $first_element_id ) );
  141.         if ( ! empty( $children ) ) {
  142.             // Get the first term
  143.             $child_term    = reset( $children );
  144.             $first_element = pm_get_lowest_element( $child_term, $elements );
  145.         }
  146.     }
  147.  
  148.     return $first_element;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement