Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a warning related to unwanted behaviors of the Search Everything option on the Search Bar #2280

Open
1 task
rafaehlers opened this issue Feb 7, 2025 · 2 comments

Comments

@rafaehlers
Copy link
Contributor

rafaehlers commented Feb 7, 2025

The "Search Everything" field/choice on the Search Bar can search for information inside entries not displayed on the View as fields/columns.

Image

This is not a bug because we are replicating the "Any form field" option of Gravity Forms on the Entries page:

Image

  • We should display a warning, like a call out, once the Search Everything option is selected/added to the Search Bar configuration.

Example:

Image

But the information appears in another field:

Image

The same is true for the View:

Image

@rafaehlers
Copy link
Contributor Author

@doekenorg
Copy link
Contributor

doekenorg commented Feb 9, 2025

We can fix this pretty easily for GravityView; but I think it should be a View setting.

We only need to

  1. retrieve "all" conditions with a left column field_id of GF_Query_Column::META
  2. Foreach of these add a additional AND GF_Query_Condition on the meta_key column of that same field.

partial code:

add_action( 'gravityview/view/query', function ( $query, $view ) {
	$update_conditions = static function (
		GF_Query $query,
		GF_Query_Condition $condition,
		$fields
	) use ( & $update_conditions ): GF_Query_Condition {
		if ( in_array( $condition->operator, [ GF_Query_Condition::_AND, GF_Query_Condition::_OR ], true ) ) {
			$expressions = array_map(
				static function ( GF_Query_Condition $expression ) use ( $query, $fields, $update_conditions ) {
					return $update_conditions( $query, $expression, $fields );
				},
				$condition->expressions ?? []
			);

			return $condition->operator === GF_Query_Condition::_AND
				? GF_Query_Condition::_and( ...$expressions )
				: GF_Query_Condition::_or( ...$expressions );
		}

		if (
			! $condition->left instanceof GF_Query_Column
			|| $condition->left->field_id !== GF_Query_Column::META
			|| ! array_key_exists( $condition->left->source, $fields )
		) {
			return $condition;
		}

		$source = $condition->left->source;
		// If an array is set, but it is empty; it should exclude aLL values; so we replace it with an invalid statement.
		if ( ! $fields[ $source ] ) {
			return new GF_Query_Condition(
				$condition->left,
				GF_Query_Condition::EQ,
				new GF_Query_Literal( '__GK_NO_MATCH__' ),
			);
		}

		$key_condition = new GF_Query_Condition(
			new GF_Query_Column(
				'meta_key',
				$condition->left->source,
				$query->_alias( GF_Query_Column::META, $source, 'm' )
			),
			GF_Query_Condition::IN,
			new GF_Query_Series(
				array_map( static function ( $field_id ) {
					return new GF_Query_Literal( $field_id );
				}, $fields[ $source ] )
			)
		);

		return GF_Query_Condition::_and( $condition, $key_condition );
	};

	$where = $query->_introspect()['where'];

	// Retrieve visible fields per form on the View.
	$fields = [
		'6' => [ '1' ],
		'7' => [ '1', '3'],
	];

	$where = $update_conditions( $query, $where, $fields );

	$query->where( $where ); // lets-a-go!
}, 2048, 2 ); // Extra late execution for Multiple Forms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants