From 05d23ae32db9ffee02de472f761979f5ba6d267b Mon Sep 17 00:00:00 2001 From: nithinjohn22 Date: Thu, 26 Dec 2024 11:04:43 +0530 Subject: [PATCH] FIX: url filtering issue in block --- inc/class-awsm-job-openings-block.php | 13 +++-- inc/template-functions-block.php | 55 ++++++++++++++++++- inc/template-functions.php | 13 +---- .../block-files/block-job-openings-view.php | 46 ++++++++++++++-- 4 files changed, 104 insertions(+), 23 deletions(-) diff --git a/inc/class-awsm-job-openings-block.php b/inc/class-awsm-job-openings-block.php index a416f5df..df676373 100644 --- a/inc/class-awsm-job-openings-block.php +++ b/inc/class-awsm-job-openings-block.php @@ -448,22 +448,25 @@ public function awsm_block_posts_filters() { // phpcs:enable } - public static function awsm_block_job_query_args( $filters = array(), $attributes = array() ) { + public static function awsm_block_job_query_args( $filters = array(), $attributes = array(), $is_term_or_slug = array() ) { $args = array(); if ( is_tax() ) { $q_obj = get_queried_object(); $taxonomy = $q_obj->taxonomy; $term_id = $q_obj->term_id; $filters = array( $taxonomy => $term_id ); + $is_term_or_slug[ $taxonomy ] = 'term_id'; } + if ( ! empty( $filters ) ) { - foreach ( $filters as $taxonomy => $term_id ) { - if ( ! empty( $term_id ) ) { + foreach ( $filters as $taxonomy => $value ) { + if ( ! empty( $value ) ) { + $field_type = isset( $is_term_or_slug[ $taxonomy ] ) ? $is_term_or_slug[ $taxonomy ] : 'term_id'; $spec = array( 'taxonomy' => $taxonomy, - 'field' => 'term_id', - 'terms' => $term_id, + 'field' => $field_type, + 'terms' => (array) $value, ); $args['tax_query'][] = $spec; } diff --git a/inc/template-functions-block.php b/inc/template-functions-block.php index aeafebdf..f8533a9c 100644 --- a/inc/template-functions-block.php +++ b/inc/template-functions-block.php @@ -31,9 +31,62 @@ function awsm_block_job_filters_explode( $filter_data ) { } } +if ( ! function_exists( 'get_block_filtered_job_terms' ) ) { + function get_block_filtered_job_terms( $attributes ) { + $filter_suffix = '_spec'; + $filters = explode(',', $attributes["filter_options"]); + $filtered_terms = array(); + + error_log( json_encode( 'enters get_block_filtered_job_terms', JSON_PRETTY_PRINT ) ); + + if ( ! empty( $filters ) ) { + foreach ( $filters as $filter ) { + $current_filter_key = str_replace( '-', '__', $filter ) . $filter_suffix; + + if ( isset( $_GET[ $current_filter_key ] ) ) { + $term_slug = sanitize_title( $_GET[ $current_filter_key ] ); + $term = get_term_by( 'slug', $term_slug, $filter ); + + if ( $term && ! is_wp_error( $term ) ) { + $filtered_terms[ $filter ] = $term; + } else { + $filtered_terms[ $filter ] = null; + } + } + } + } + + return $filtered_terms; + } +} + if ( ! function_exists( 'awsm_block_jobs_query' ) ) { function awsm_block_jobs_query( $attributes = array() ) { - $args = AWSM_Job_Openings_Block::awsm_block_job_query_args( array(), $attributes ); + $query_args = array(); + $is_term_or_slug = array(); + $filter_suffix = '_spec'; + + + $filter_options_array = explode(',', $attributes["filter_options"]); + + if ( ! empty( $filter_options_array ) ) { + foreach ( $filter_options_array as $filter ) { + $current_filter_key = str_replace( '-', '__', $filter ) . $filter_suffix; + if ( isset( $_GET[ $current_filter_key ] ) ) { + $term_slug = sanitize_title( $_GET[ $current_filter_key ] ); + $term = get_term_by( 'slug', $term_slug, $filter ); + if ( $term && ! is_wp_error( $term ) ) { + $query_args[ $filter ] = $term->term_id; + $is_term_or_slug[ $filter ] = 'term_id'; + } else { + $query_args[ $filter ] = $term_slug; + $is_term_or_slug[ $filter ] = 'slug'; + } + } + } + } + + $args = AWSM_Job_Openings_Block::awsm_block_job_query_args( $query_args, $attributes, $is_term_or_slug = array() ); $query = new WP_Query( $args ); return $query; } diff --git a/inc/template-functions.php b/inc/template-functions.php index 25a56d3d..e0a02901 100644 --- a/inc/template-functions.php +++ b/inc/template-functions.php @@ -44,7 +44,6 @@ function awsm_jobs_query( $shortcode_atts = array() ) { $filters = get_option( 'awsm_jobs_listing_available_filters' ); - // if ( $_GET ) { if ( ! empty( $filters ) ) { foreach ( $filters as $filter ) { $current_filter_key = str_replace( '-', '__', $filter ) . $filter_suffix; @@ -61,7 +60,6 @@ function awsm_jobs_query( $shortcode_atts = array() ) { } } } - // } $args = AWSM_Job_Openings::awsm_job_query_args( $query_args, $shortcode_atts, $is_term_or_slug ); $query = new WP_Query( $args ); @@ -400,13 +398,4 @@ function awsm_jobs_single_featured_image() { } add_action( 'before_awsm_jobs_single_content', 'awsm_jobs_single_featured_image' ); -add_filter( 'query_vars', 'register_custom_query_vars' ); -function register_custom_query_vars( $vars ) { - $filters = get_option( 'awsm_jobs_listing_available_filters' ); - if ( $filters ) { - foreach ( $filters as $filter ) { - $vars[] = str_replace( '-', '__', $filter ) . '_spec'; - } - } - return $vars; -} + diff --git a/inc/templates/block-files/block-job-openings-view.php b/inc/templates/block-files/block-job-openings-view.php index 65c7b398..4df3d40e 100644 --- a/inc/templates/block-files/block-job-openings-view.php +++ b/inc/templates/block-files/block-job-openings-view.php @@ -38,9 +38,45 @@ -
-

-
- $value ) { + if ( substr( $key, -strlen( $filter_suffix ) ) === $filter_suffix ) { + $job_spec[ $key ] = sanitize_text_field( $value ); + } + } + } + + if ( ! empty( $job_spec ) ) { + print_r('sefdsd'); + ?> +
+ +

%s

', + esc_html__( 'Sorry! No jobs to show.', 'wp-job-openings' ) + ); + echo $no_jobs_content; + ?> +
> + +
+ + +
+

+
+