Created
April 16, 2025 06:37
-
-
Save saifsultanc/5675f98da0114cacbe7cbf6967dd24a4 to your computer and use it in GitHub Desktop.
gw-snippet-gcgs_gppa_query_builder_args-78195.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'gcgs_gppa_query_builder_args', function( $query_builder_args, $args, $object ) { | |
/** @var string|string[] */ | |
$filter_value = null; | |
/** @var array */ | |
$filter = null; | |
/** @var int */ | |
$filter_group_index = null; | |
/** @var string */ | |
$property_id = null; | |
/** @var object */ | |
$field = null; | |
// phpcs:ignore WordPress.PHP.DontExtract.extract_extract | |
extract( $args ); | |
// Only process for form ID 3 and 'is' operator | |
if ( $field->formId != 3 || $filter['operator'] != 'is' ) { | |
return $query_builder_args; | |
} | |
$column_letter = $object->get_column_letter( $args['primary_property_value'], $property_id ); | |
if ( ! empty( $filter_value ) ) { | |
$conditions = array(); | |
// Ensure filter_value is an array | |
$filter_values = is_array( $filter_value ) ? $filter_value : array( $filter_value ); | |
foreach ( $filter_values as $v ) { | |
if ( ! empty( $v ) ) { | |
$conditions[] = sprintf( "lower(%s) = '%s'", $column_letter, strtolower( sanitize_text_field( $v ) ) ); | |
} | |
} | |
if ( ! empty( $conditions ) ) { | |
// Make sure the filter group exists | |
if ( ! isset( $query_builder_args['where'][ $filter_group_index ] ) ) { | |
$query_builder_args['where'][ $filter_group_index ] = array(); | |
} | |
// Replace the last condition in this filter group with our OR conditions | |
if ( ! empty( $query_builder_args['where'][ $filter_group_index ] ) ) { | |
array_pop( $query_builder_args['where'][ $filter_group_index ] ); | |
} | |
$query_builder_args['where'][ $filter_group_index ][] = '(' . implode( ' OR ', $conditions ) . ')'; | |
} | |
} | |
return $query_builder_args; | |
}, 10, 3); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment