Skip to content

Commit

Permalink
FIX: url filtering issue in block
Browse files Browse the repository at this point in the history
  • Loading branch information
nithinjohn22 committed Dec 26, 2024
1 parent ab1e440 commit 05d23ae
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 23 deletions.
13 changes: 8 additions & 5 deletions inc/class-awsm-job-openings-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
55 changes: 54 additions & 1 deletion inc/template-functions-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 1 addition & 12 deletions inc/template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 );
Expand Down Expand Up @@ -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;
}

46 changes: 41 additions & 5 deletions inc/templates/block-files/block-job-openings-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,45 @@
</div>
<?php
else :
?>
<div class="jobs-none-container">
<p><?php awsm_no_jobs_msg(); ?></p>
</div>
<?php
$filter_suffix = '_spec';
$job_spec = array();

if ( ! empty( $_GET ) ) {
foreach ( $_GET as $key => $value ) {
if ( substr( $key, -strlen( $filter_suffix ) ) === $filter_suffix ) {
$job_spec[ $key ] = sanitize_text_field( $value );
}
}
}

if ( ! empty( $job_spec ) ) {
print_r('sefdsd');
?>
<div class="awsm-b-job-wrap<?php awsm_jobs_wrapper_class(); ?>">
<?php
do_action( 'awsm_block_filter_form', $attributes );
do_action( 'awsm_block_form_outside', $attributes );
?>
<?php
get_block_filtered_job_terms( $attributes);
$no_jobs_content = sprintf(
'<div class="awsm-jobs-pagination awsm-load-more-main awsm-no-more-jobs-container awsm-job-no-more-jobs-get"><p>%s</p></div>',
esc_html__( 'Sorry! No jobs to show.', 'wp-job-openings' )
);
echo $no_jobs_content;
?>
<div <?php awsm_block_jobs_view_class( '', $attributes ); ?><?php awsm_block_jobs_data_attrs( array(), $attributes ); ?>>
<?php
include get_awsm_jobs_template_path( 'block-main', 'block-files' );
?>
</div>
</div>
<?php
} else {
?>
<div class="jobs-none-container">
<p><?php awsm_no_jobs_msg(); ?></p>
</div>
<?php
}
endif;

0 comments on commit 05d23ae

Please sign in to comment.