Advertisement
mbis

Filter Everything Pro & WOOF hooks

Nov 28th, 2023 (edited)
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. /**
  2.  * 1. Detects Filter Everything Pro filter URLs
  3.  *
  4.  * @param string $element_id    The current element ID.
  5.  * @param array  $uri_parts     An array containing URI parts.
  6.  * @param string $request_url   The URL of the current request.
  7.  *
  8.  * @return string The filtered element ID.
  9.  */
  10. function pm_detect_filter_everything_url( $element_id, $uri_parts, $request_url ) {
  11.     global $permalink_manager_uris;
  12.  
  13.     if ( empty( $element_id ) && ! empty( $uri_parts['uri'] ) && strpos( $uri_parts['uri'], '/' ) !== false ) {
  14.         $uri           = $uri_parts['uri'];
  15.         $path_segments = explode( '/', trim( $uri, '/' ) );
  16.         $loop_counter  = 0; // Counter for loop iterations
  17.  
  18.         // Loop through the path segments in reverse order
  19.         while ( ! empty( $path_segments ) && $loop_counter < 6 ) {
  20.             $candidate_uri = implode( '/', $path_segments );
  21.  
  22.             if ( in_array( $candidate_uri, $permalink_manager_uris ) ) {
  23.                 $new_element_id = array_search( $candidate_uri, $permalink_manager_uris );
  24.  
  25.                 // Check if the new element ID contains 'tax-'
  26.                 if ( strpos( $new_element_id, 'tax-' ) !== false ) {
  27.                     $element_id = $new_element_id;
  28.                 }
  29.  
  30.                 break;
  31.             }
  32.  
  33.             // Remove the last path segment and continue the loop
  34.             array_pop( $path_segments );
  35.  
  36.             // Increment the loop counter
  37.             $loop_counter ++;
  38.         }
  39.     }
  40.  
  41.     return $element_id;
  42. }
  43. add_filter( 'permalink_manager_detected_element_id', 'pm_detect_filter_everything_url', 10, 3 );
  44.  
  45.  
  46. /**
  47.  * 2. Detects WOOF (HUSKY – Products Filter for WooCommerce Professional) filter URLs
  48.  *
  49.  * @param string $element_id    The current element ID.
  50.  * @param array  $uri_parts     An array containing URI parts.
  51.  * @param string $request_url   The request URL.
  52.  *
  53.  * @return string The updated element ID.
  54.  */
  55. function pm_detect_woof_filter_url( $element_id, $uri_parts, $request_url ) {
  56.     global $permalink_manager_uris;
  57.  
  58.     // Check if the 'woof' function is available and callable
  59.     if ( ! function_exists( 'woof' ) || ! is_callable( 'woof' ) ) {
  60.         return $element_id;
  61.     }
  62.  
  63.     $woof_instance = woof();
  64.  
  65.     if ( method_exists( $woof_instance, 'get_swoof_search_slug' ) ) {
  66.         // Get the filter slug
  67.         $filter_slug = woof()->get_swoof_search_slug();
  68.  
  69.         if ( empty( $element_id ) && ! empty( $uri_parts['uri'] ) && strpos( $uri_parts['uri'], '/' ) !== false && strpos( $uri_parts['uri'], $filter_slug ) !== false ) {
  70.             // Extract the new URI and find the corresponding element ID
  71.             $new_uri        = substr( $uri_parts['uri'], 0, strpos( $uri_parts['uri'], "/{$filter_slug}/" ) );
  72.             $new_element_id = array_search( $new_uri, $permalink_manager_uris );
  73.  
  74.             // Change the element ID
  75.             if ( $new_element_id !== false ) {
  76.                 $element_id = $new_element_id;
  77.             }
  78.         }
  79.     }
  80.  
  81.     return $element_id;
  82. }
  83. add_filter('permalink_manager_detected_element_id', 'pm_detect_woof_filter_url', 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement