Advertisement
mbis

Filter Everything Pro & WOOF hooks

Nov 28th, 2023 (edited)
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 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. function pm_maybe_disable_wpc_filter_request( $do_filter ) {
  46.     global $pm_query;
  47.  
  48.     if ( ! empty( $pm_query['id'] ) && empty( $pm_query['endpoint'] ) ) {
  49.         return false;
  50.     }
  51.  
  52.     return $do_filter;
  53. }
  54. add_filter( 'wpc_do_filter_request', 'pm_maybe_disable_wpc_filter_request', 10, 1 );
  55.  
  56.  
  57. /**
  58.  * 2. Detects WOOF (HUSKY – Products Filter for WooCommerce Professional) filter URLs
  59.  *
  60.  * @param string $element_id    The current element ID.
  61.  * @param array  $uri_parts     An array containing URI parts.
  62.  * @param string $request_url   The request URL.
  63.  *
  64.  * @return string The updated element ID.
  65.  */
  66. function pm_detect_woof_filter_url( $element_id, $uri_parts, $request_url ) {
  67.     global $permalink_manager_uris;
  68.  
  69.     // Check if the 'woof' function is available and callable
  70.     if ( ! function_exists( 'woof' ) || ! is_callable( 'woof' ) ) {
  71.         return $element_id;
  72.     }
  73.  
  74.     $woof_instance = woof();
  75.  
  76.     if ( method_exists( $woof_instance, 'get_swoof_search_slug' ) ) {
  77.         // Get the filter slug
  78.         $filter_slug = woof()->get_swoof_search_slug();
  79.  
  80.         if ( empty( $element_id ) && ! empty( $uri_parts['uri'] ) && strpos( $uri_parts['uri'], '/' ) !== false && strpos( $uri_parts['uri'], $filter_slug ) !== false ) {
  81.             // Extract the new URI and find the corresponding element ID
  82.             $new_uri        = substr( $uri_parts['uri'], 0, strpos( $uri_parts['uri'], "/{$filter_slug}/" ) );
  83.             $new_element_id = array_search( $new_uri, $permalink_manager_uris );
  84.  
  85.             // Change the element ID
  86.             if ( $new_element_id !== false ) {
  87.                 $element_id = $new_element_id;
  88.             }
  89.         }
  90.     }
  91.  
  92.     return $element_id;
  93. }
  94. add_filter('permalink_manager_detected_element_id', 'pm_detect_woof_filter_url', 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement