<?php
/**
 * The GravityView New Search widget
 *
 * @license   GPL2+
 * @author    GravityKit <hello@gravitykit.com>
 * @link      http://www.gravitykit.com
 * @copyright Copyright 2014, Katz Web Services, Inc.
 */

use GV\GF_Form;
use GV\Grid;
use GV\Search\Fields\Search_Field;
use GV\Search\Fields\Search_Field_All;
use GV\Search\Fields\Search_Field_Gravity_Forms;
use GV\Search\Fields\Search_Field_Search_Mode;
use GV\Search\Fields\Search_Field_Submit;
use GV\Search\Querying\Search_Filter_Builder;
use GV\Search\Querying\Search_Request;
use GV\Search\Search_Field_Collection;
use GV\Search\Search_Policy;
use GV\View;

if ( ! defined( 'WPINC' ) ) {
	die;
}

require_once __DIR__ . '/settings/class-search-widget-settings-visible-fields-only.php';

class GravityView_Widget_Search extends \GV\Widget {

	public        $icon           = 'dashicons-search';

	public static $file;

	public static $instance;

	/**
	 * Holds the recorded areas for rendering the settings.
	 *
	 * @since 2.44
	 *
	 * @var array
	 */
	private array $area_settings = [];

	/**
	 * Contains the context for the search fields to render.
	 *
	 * @since 2.42
	 *
	 * @var array{template_id: string, form_id: int}
	 */
	private array $search_fields_context = [];

	public function __construct() {
		$this->widget_id          = 'search_bar';
		$this->widget_description = esc_html__( 'Search form for searching entries.', 'gk-gravityview' );
		$this->widget_subtitle    = '';

		self::$instance = &$this;
		self::$file = plugin_dir_path( __FILE__ );

		$settings = [
			'search_fields_section' => [
				'type' => 'html',
				'desc' => \Closure::fromCallable( [ $this, 'get_search_sections' ] ),
			],
		];

		if ( ! $this->is_registered() ) {
			// frontend - filter entries
			add_filter( 'gravityview_fe_search_criteria', [ $this, 'filter_entries' ], 10, 3 );

			// frontend - add template path
			add_filter( 'gravityview_template_paths', [ $this, 'add_template_path' ] );

			// admin - add scripts - run at 1100 to make sure GravityView_Admin_Views::add_scripts_and_styles() runs first at 999
			add_action( 'admin_enqueue_scripts', [ $this, 'add_scripts_and_styles' ], 1100 );
			add_filter( 'gravityview_noconflict_scripts', [ $this, 'register_no_conflict' ] );

			// ajax - get the searchable fields
			add_action( 'wp_ajax_gv_searchable_fields', [ 'GravityView_Widget_Search', 'get_searchable_fields' ] );
			add_action( 'wp_ajax_nopriv_gv_searchable_fields', [ 'GravityView_Widget_Search', 'get_searchable_fields' ] );

			add_action( 'gravityview_search_widget_fields_after', [ $this, 'add_preview_inputs' ] );

			add_filter( 'gravityview/api/reserved_query_args', [ $this, 'add_reserved_args' ] );

			add_filter( 'gk/gravityview/search/available-fields', [ $this, 'add_form_search_fields' ], 0, 2 );
			add_filter( 'gravityview_template_search_options', [ $this, 'set_search_field_options' ], 10, 6 );

			add_action( 'gravityview_render_search_active_areas', [ $this, 'render_search_active_areas' ], 10, 3 );
			add_action( 'gravityview_render_available_search_fields', [ $this, 'render_available_search_fields' ], 10, 2 );
			add_action( 'gk/gravityview/template/before-field-render', [ $this, 'record_search_field_context' ], 9, 5 );

			add_action( 'gk/gravityview/admin-views/row/before', [ $this, 'reset_area_recording' ], 10, 4 );
			add_action( 'gk/gravityview/admin-views/row/after', [ $this, 'render_area_settings' ], 10, 5 );
			add_action( 'gk/gravityview/admin-views/area/actions', [ $this, 'add_search_area_settings_button' ], 10, 6 );
			add_filter( 'gravityview_template_area_options', [ $this, 'add_search_area_settings' ], 10, 3 );
		}

		parent::__construct( esc_html__( 'Search Bar', 'gk-gravityview' ), null, [], $settings );
	}

	/**
	 * @return GravityView_Widget_Search
	 */
	public static function getInstance() {
		if ( empty( self::$instance ) ) {
			self::$instance = new GravityView_Widget_Search();
		}

		return self::$instance;
	}

	/**
	 * Add reserved arguments for $_GET specifically for links.
	 *
	 * @since 2.10
	 *
	 * @param array $args The existing arguments.
	 *
	 * @return array The reserved arguments.
	 */
	public function add_reserved_args( $args ) {
		$get = (array) ( $_GET ?? [] );

		/**
		 * Add additional reserved arguments for the search widget.
		 *
		 * @since 2.42
		 *
		 * @param array $args The reserved arguments.
		 */
		$additional_args = apply_filters( 'gk/gravityview/search/additional-reserved-args', [] );

		// Maintain required arguments and add additional arguments.
		$args = array_unique(
			array_merge(
				$args,
				Search_Request::get_reserved_keys( $get ),
				$additional_args
			)
		);

		return $args;
	}

	/**
	 * Returns the search method
	 *
	 * @since 1.16.4
	 * @return string
	 */
	public function get_search_method() {
		return Search_Request::method();
	}

	/**
	 * Get the input types available for different field types
	 *
	 * @since 1.17.5
	 *
	 * @return array [field type name] => (array|string) search bar input types
	 */
	public static function get_input_types_by_field_type() {
		/**
		 * Input Type groups
		 *
		 * @see admin-search-widget.js (getSelectInput)
		 */
		$input_types = [
			'text'        => [ 'input_text' ],
			'address'     => [ 'input_text' ],
			'number'      => [ 'input_text', 'number_range' ],
			'date'        => [ 'date', 'date_range' ],
			'entry_date'  => [ 'date_range', 'date' ], // `date_range` is the default for backwards compatibility.
			'boolean'     => [ 'single_checkbox' ],
			'select'      => [ 'select', 'radio', 'link' ],
			'multi'       => [ 'select', 'multiselect', 'radio', 'checkbox', 'link' ],
			'multiselect' => [ 'select', 'multiselect', 'radio', 'checkbox', 'link' ],
			'checkbox'    => [ 'select', 'multiselect', 'radio', 'checkbox', 'link' ],
			'submit'      => [ 'submit' ],
			'search_mode' => [ 'hidden', 'radio' ],

			// hybrids
			'created_by'  => [ 'select', 'radio', 'checkbox', 'multiselect', 'link', 'input_text' ],
			'multi_text'  => [ 'select', 'radio', 'checkbox', 'multiselect', 'link', 'input_text' ],
			'product'     => [ 'select', 'radio', 'link', 'input_text', 'number_range' ],
		];

		/**
		 * Change the types of search fields available to a field type.
		 *
		 * @see GravityView_Widget_Search::get_search_input_labels() for the available input types
		 *
		 * @param array $input_types Associative array: key is field `name`, value is array of GravityView input types (note: use `input_text` for `text`)
		 */
		$input_types = apply_filters( 'gravityview/search/input_types', $input_types );

		return $input_types;
	}

	public static function get_input_types_by_gf_field( $gf_field ) {
		if ( ! $gf_field instanceof GF_Field ) {
			return [ 'input_text' ];
		}

		$field_type = $gf_field->get_input_type();

		$input_types = self::get_input_types_by_field_type();

		// If the field type is not in the array, use the default input type
		if ( ! isset( $input_types[ $field_type ] ) ) {
			$field_type = 'input_text';
		}

		return $input_types[ $field_type ] ?? [ 'input_text' ];
	}

	/**
	 * Get labels for different types of search bar inputs
	 *
	 * @since 1.17.5
	 *
	 * @return array [input type] => input type label
	 */
	public static function get_search_input_labels() {
		/**
		 * Input Type labels l10n
		 *
		 * @see admin-search-widget.js (getSelectInput)
		 */
		$input_labels = [
			'input_text'      => esc_html__( 'Text', 'gk-gravityview' ),
			'date'            => esc_html__( 'Date', 'gk-gravityview' ),
			'select'          => esc_html__( 'Select', 'gk-gravityview' ),
			'multiselect'     => esc_html__( 'Select (multiple values)', 'gk-gravityview' ),
			'radio'           => esc_html__( 'Radio', 'gk-gravityview' ),
			'checkbox'        => esc_html__( 'Checkbox', 'gk-gravityview' ),
			'single_checkbox' => esc_html__( 'Checkbox', 'gk-gravityview' ),
			'link'            => esc_html__( 'Links', 'gk-gravityview' ),
			'date_range'      => esc_html__( 'Date range', 'gk-gravityview' ),
			'number_range'    => esc_html__( 'Number range', 'gk-gravityview' ),
			'submit'          => esc_html__( 'Submit Button', 'gk-gravityview' ),
			'hidden'          => esc_html__( 'Hidden Field', 'gk-gravityview' ),
		];

		/**
		 * Change the label of search field input types.
		 *
		 * @param array $input_types Associative array: key is input type name, value is label
		 */
		$input_labels = apply_filters( 'gravityview/search/input_labels', $input_labels );

		return $input_labels;
	}

	public static function get_search_input_label( $input_type ) {
		$labels = self::get_search_input_labels();

		return \GV\Utils::get( $labels, $input_type, false );
	}

	/**
	 * Add script to Views edit screen (admin)
	 *
	 * @param mixed $hook
	 */
	public function add_scripts_and_styles( $hook ) {
		global $pagenow;

		// Don't process any scripts below here if it's not a GravityView page or the widgets screen
		if ( ! gravityview()->request->is_admin( $hook, 'single' ) && ( 'widgets.php' !== $pagenow ) ) {
			return;
		}

		$script_min    = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
		$script_source = empty( $script_min ) ? '/source' : '';

		wp_enqueue_script( 'gravityview_searchwidget_admin',
			plugins_url( 'assets/js' . $script_source . '/admin-search-widget' . $script_min . '.js', __FILE__ ),
			[ 'jquery', 'gravityview_views_scripts' ],
			\GV\Plugin::$version );

		wp_localize_script(
			'gravityview_searchwidget_admin',
			'gvSearchVar',
			[
				'nonce'             => wp_create_nonce( 'gravityview_ajaxsearchwidget' ),
				'label_nofields'    => esc_html__( 'No search fields configured yet.', 'gk-gravityview' ),
				'label_addfield'    => esc_html__( 'Add Search Field', 'gk-gravityview' ),
				'label_label'       => esc_html__( 'Label', 'gk-gravityview' ),
				'label_searchfield' => esc_html__( 'Search Field', 'gk-gravityview' ),
				'label_inputtype'   => esc_html__( 'Input Type', 'gk-gravityview' ),
				'label_ajaxerror'   => esc_html__( 'There was an error loading searchable fields. Save the View or refresh the page to fix this issue.',
					'gk-gravityview' ),
				'input_labels'      => json_encode( self::get_search_input_labels() ),
				'input_types'       => json_encode( self::get_input_types_by_field_type() ),
			]
		);
	}

	/**
	 * Add admin script to the no-conflict scripts allowlist
	 *
	 * @param array $allowed Scripts allowed in no-conflict mode
	 *
	 * @return array Scripts allowed in no-conflict mode, plus the search widget script
	 */
	public function register_no_conflict( $allowed ) {
		$allowed[] = 'gravityview_searchwidget_admin';

		return $allowed;
	}

	/**
	 * Ajax
	 * Returns the form fields ( only the searchable ones )
	 *
	 * @return void
	 */
	public static function get_searchable_fields() {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'gravityview_ajaxsearchwidget' ) ) {
			exit( '0' );
		}

		$form = '';

		// Fetch the form for the current View
		if ( ! empty( $_POST['view_id'] ) ) {
			$form = gravityview_get_form_id( $_POST['view_id'] );
		} elseif ( ! empty( $_POST['formid'] ) ) {
			$form = (int) $_POST['formid'];
		} elseif ( ! empty( $_POST['template_id'] ) && class_exists( 'GravityView_Ajax' ) ) {
			$form = GravityView_Ajax::pre_get_form_fields( $_POST['template_id'] );
		}

		// fetch form id assigned to the view
		$response = self::render_searchable_fields( $form );

		exit( $response );
	}

	/**
	 * Generates html for the available Search Fields dropdown
	 *
	 * @param int|array $form
	 * @param string    $current (for future use)
	 *
	 * @return string
	 */
	public static function render_searchable_fields( $form = null, $current = '' ) {
		if ( is_null( $form ) ) {
			return '';
		}

		$form_id = (int) ( $form['id'] ?? $form );
		// start building output

		$output = '<select class="gv-search-fields">';

		$search_fields = Search_Field_Collection::available_fields( $form_id );

		foreach ( $search_fields->all() as $field ) {
			if (
				$field instanceof Search_Field_Submit
				|| $field instanceof Search_Field_Search_Mode
			) {
				continue;
			}
			$custom_field = $field->to_legacy_format();

			$output .= sprintf(
				'<option value="%s" %s data-inputtypes="%s" data-placeholder="%s">%s</option>',
				$custom_field['field'],
				selected( $custom_field['field'], $current, false ),
				$custom_field['input'],
				$field->get_frontend_label(),
				$custom_field['title'],
			);
		}

		$output .= '</select>';

		return $output;
	}

	/**
	 * Assign an input type according to the form field type
	 *
	 * @see admin-search-widget.js
	 *
	 * @param string|int|float $field_id   Gravity Forms field ID
	 * @param string           $field_type Gravity Forms field type (also the `name` parameter of GravityView_Field
	 *                                     classes)
	 *
	 * @return string GV field search input type ('multi', 'boolean', 'select', 'date', 'text')
	 */
	public static function get_search_input_types( $field_id = '', $field_type = null ) {
		// @todo - This needs to be improved - many fields have . including products and addresses
		if ( false !== strpos( (string) $field_id, '.' ) && in_array( $field_type,
				[ 'checkbox' ] ) || in_array( $field_id, [ 'is_fulfilled' ] ) ) {
			$input_type = 'boolean'; // on/off checkbox
		} elseif ( in_array( $field_type,
			[ 'checkbox', 'post_category', 'multiselect', 'image_choice', 'multi_choice' ] ) ) {
			$input_type = 'multi'; // multiselect
		} elseif ( in_array( $field_id, [ 'payment_status' ] ) ) {
			$input_type = 'multi_text';
		} elseif ( in_array( $field_type, [ 'select', 'radio' ] ) ) {
			$input_type = 'select';
		} elseif ( in_array( $field_type, [ 'date' ] ) || in_array( $field_id, [ 'payment_date' ] ) ) {
			$input_type = 'date';
		} elseif ( in_array( $field_type, [ 'number', 'quantity', 'total' ] ) || in_array( $field_id,
				[ 'payment_amount' ] ) ) {
			$input_type = 'number';
		} elseif ( in_array( $field_type, [ 'product' ] ) ) {
			$input_type = 'product';
		} else {
			$input_type = 'text';
		}

		/**
		 * Modify the search form input type based on field type.
		 *
		 * @since 1.2
		 * @since 1.19.2 Added $field_id parameter
		 *
		 * @param string           $input_type Assign an input type according to the form field type. Defaults: `boolean`, `multi`, `select`, `date`, `text`
		 * @param string           $field_type Gravity Forms field type (also the `name` parameter of GravityView_Field classes)
		 * @param string|int|float $field_id   ID of the field being processed
		 */
		$input_type = apply_filters( 'gravityview/extension/search/input_type', $input_type, $field_type, $field_id );

		return $input_type;
	}

	/**
	 * Display hidden fields to add support for sites using Default permalink structure
	 *
	 * @since 1.8
	 * @return array Search fields, modified if not using permalinks
	 */
	public function add_no_permalink_fields( $search_fields, $object, $widget_args = [] ) {
		/** @global WP_Rewrite $wp_rewrite */
		global $wp_rewrite;

		// Support default permalink structure
		if ( false === $wp_rewrite->using_permalinks() ) {
			// By default, use current post.
			$post_id = 0;

			// We're in the WordPress Widget context, and an overriding post ID has been set.
			if ( ! empty( $widget_args['post_id'] ) ) {
				$post_id = absint( $widget_args['post_id'] );
			} // We're in the WordPress Widget context, and the base View ID should be used
			elseif ( ! empty( $widget_args['view_id'] ) ) {
				$post_id = absint( $widget_args['view_id'] );
			}

			$args = gravityview_get_permalink_query_args( $post_id );

			// Add hidden fields to the search form
			foreach ( $args as $key => $value ) {
				$search_fields[] = [
					'name'  => $key,
					'input' => 'hidden',
					'value' => $value,
				];
			}
		}

		return $search_fields;
	}

	/** --- Frontend --- */

	/**
	 * Calculate the search criteria to filter entries
	 *
	 * @param array $search_criteria       The search criteria
	 * @param int   $form_id               The form ID
	 * @param array $args                  Some args
	 *
	 * @param bool  $force_search_criteria Whether to suppress GF_Query filter, internally used in self::gf_query_filter
	 *
	 * @return array
	 */
	public function filter_entries( $search_criteria, $form_id = null, $args = [], $force_search_criteria = false ) {
		if ( ! $force_search_criteria && gravityview()->plugin->supports( \GV\Plugin::FEATURE_GFQUERY ) ) {
			/**
			 * If GF_Query is available, we can construct custom conditions with nested
			 * booleans on the query, giving up the old ways of flat search_criteria field_filters.
			 */
			add_action( 'gravityview/view/query', [ $this, 'gf_query_filter' ], 10, 3 );

			return $search_criteria; // Return the original criteria, GF_Query modification kicks in later
		}

		$search_method = Search_Request::method();
		$get           = 'post' === $search_method ? $_POST : $_GET;
		$view          = \GV\View::by_id( \GV\Utils::get( $args, 'id' ) );

		gravityview()->log->debug(
			'Requested $_{method}: ',
			[
				'method' => $search_method,
				'data'   => $get,
			]
		);

		if ( empty( $get ) || ! is_array( $get ) ) {
			return $search_criteria;
		}

		$get = stripslashes_deep( $get );

		if ( ! is_null( $get ) ) {
			$get = gv_map_deep( $get, 'rawurldecode' );
		}

		$search_request = Search_Request::from_arguments( $get );
		if ( ! $search_request ) {
			return $search_criteria;
		}

		// Make sure array key is set up.
		$search_criteria = Search_Filter_Builder::to_search_criteria( $search_request, $view, $search_criteria );

		gravityview()->log->debug( 'Returned Search Criteria: ', [ 'data' => $search_criteria ] );

		unset( $get );

		return $search_criteria;
	}

	/**
	 * Filters the \GF_Query with advanced logic.
	 *
	 * Drop-in for the legacy flat filters when \GF_Query is available.
	 *
	 * @param \GF_Query   $query   The current query object reference
	 * @param \GV\View    $this    The current view object
	 * @param \GV\Request $request The request object
	 */
	public function gf_query_filter( &$query, $view, $request ) {
		// Check if this View is currently in the rendering process.
		// This helps identify Views embedded via [gravityview] shortcode.
		$is_view_rendering = \GV\View::is_rendering( $view->ID );

		// Handle Mock_Request (used for Views rendered via shortcode).
		if ( $request instanceof \GV\Mock_Request ) {
			// Mock requests indicate shortcode-rendered Views, allow filters.
			$is_view_rendering = true;
		}

		// For Views being rendered (e.g., via shortcode in Single Entry), always apply filters.
		if ( $is_view_rendering ) {
			// Continue processing filters below.
		} elseif ( $request && $request->is_entry() ) {
			// Don't apply search filters when viewing a single entry with a valid (non-mock) request.
			return;
		} elseif ( ! $request && gravityview()->request && gravityview()->request->is_entry() ) {
			// When $request is null but context is single entry, check the main View.
			$main_view = gravityview()->request->is_view();

			// Suppress filters if no main View, or if this is the main View.
			if ( ! $main_view || $view->ID === $main_view->ID ) {
				return;
			}
			// Otherwise it's a different (embedded) View → continue processing filters.
		}

		/**
		 * This is a shortcut to get all the needed search criteria.
		 * We feed these into an new GF_Query and tack them onto the current object.
		 */
		$search_criteria = $this->filter_entries( [], null, [ 'id' => $view->ID ], true /** force search_criteria */ );

		/**
		 * Call any userland filters that they might have.
		 */
		remove_filter( 'gravityview_fe_search_criteria', [ $this, 'filter_entries' ], 10, 3 );
		$search_criteria = apply_filters( 'gravityview_fe_search_criteria',
			$search_criteria,
			$view->form->ID,
			$view->settings->as_atts() );
		add_filter( 'gravityview_fe_search_criteria', [ $this, 'filter_entries' ], 10, 3 );

		$query_class = $view->get_query_class();

		if ( empty( $search_criteria['field_filters'] ) ) {
			return;
		}

		$include_global_search_words = $exclude_global_search_words = [];

		foreach ( $search_criteria['field_filters'] as $i => $criterion ) {
			if ( ! empty( $criterion['key'] ?? null ) ) {
				continue;
			}

			if ( 'not contains' === ( $criterion['operator'] ?? '' ) ) {
				$exclude_global_search_words[] = $criterion['value'];
				unset( $search_criteria['field_filters'][ $i ] );
			} elseif ( true === ( $criterion['required'] ?? false ) ) {
				$include_global_search_words[] = $criterion['value'];
				unset( $search_criteria['field_filters'][ $i ] );
			}
		}

		$widgets = $view->widgets->by_id( $this->widget_id );
		if ( $widgets->count() ) {
			/** @var GravityView_Widget_Search $widget */
			foreach ( $widgets->all() as $widget ) {
				$search_fields = $widget->get_search_fields( $view );

				foreach ( $search_fields as $search_field ) {
					if ( 'created_by' === $search_field['field'] && 'input_text' === $search_field['input'] ) {
						$created_by_text_mode = true;
						break 2;
					}
				}
			}
		}
		$extra_conditions = [];
		$mode             = 'any';

		foreach ( $search_criteria['field_filters'] as $key => &$filter ) {
			if ( ! is_array( $filter ) ) {
				if ( in_array( strtolower( $filter ), [ 'any', 'all' ] ) ) {
					$mode = $filter;
				}
				continue;
			}

			// Construct a manual query for unapproved statuses
			if (
				'is_approved' === $filter['key']
				&& in_array( \GravityView_Entry_Approval_Status::UNAPPROVED, (array) $filter['value'], false )
			) {
				$_tmp_query       = new $query_class(
					$view->form->ID,
					[
						'field_filters' => [
							[
								'operator' => 'in',
								'key'      => 'is_approved',
								'value'    => (array) $filter['value'],
							],
							[
								'operator' => 'is',
								'key'      => 'is_approved',
								'value'    => '',
							],
							'mode' => 'any',
						],
					]
				);
				$_tmp_query_parts = $_tmp_query->_introspect();

				$extra_conditions[] = $_tmp_query_parts['where'];

				$filter = false;
				continue;
			}

			// Construct manual query for text mode creator search
			if ( 'created_by' === $filter['key'] && ! empty( $created_by_text_mode ) ) {
				$extra_conditions[] = new GravityView_Widget_Search_Author_GF_Query_Condition( $filter, $view );
				$filter             = false;
				continue;
			}

			// By default, we want searches to be wildcard for each field.
			$filter['operator'] = empty( $filter['operator'] ) ? 'contains' : $filter['operator'];

			// For multichoice, let's have an in (OR) search.
			if ( is_array( $filter['value'] ) ) {
				$filter['operator'] = 'in'; // @todo what about in contains (OR LIKE chains)?
			}

			// Default form with joins functionality
			if ( empty( $filter['form_id'] ) ) {
				$filter['form_id'] = $view->form ? $view->form->ID : 0;
			}

			/**
			 * Modify the search operator for the field (contains, is, isnot, etc)
			 *
			 * @since  2.0 Added $view parameter
			 *
			 * @param string   $operator Existing search operator
			 * @param array    $filter   array with `key`, `value`, `operator`, `type` keys
			 * @param \GV\View $view     The View we're operating on.
			 */
			$filter['operator'] = apply_filters( 'gravityview_search_operator', $filter['operator'], $filter, $view );

			if ( 'is' !== $filter['operator'] && '' === $filter['value'] ) {
				unset( $search_criteria['field_filters'][ $key ] );
			}
		}
		unset( $filter );

		if ( ! empty( $search_criteria['start_date'] ) || ! empty( $search_criteria['end_date'] ) ) {
			$date_criteria = [];

			if ( isset( $search_criteria['start_date'] ) ) {
				$date_criteria['start_date'] = $search_criteria['start_date'];
			}

			if ( isset( $search_criteria['end_date'] ) ) {
				$date_criteria['end_date'] = $search_criteria['end_date'];
			}

			$_tmp_query         = new $query_class( $view->form->ID, $date_criteria );
			$_tmp_query_parts   = $_tmp_query->_introspect();
			$extra_conditions[] = $_tmp_query_parts['where'];
		}

		$search_conditions = [];

		if ( $filters = array_filter( $search_criteria['field_filters'] ) ) {
			foreach ( $filters as $filter ) {
				if ( ! is_array( $filter ) ) {
					continue;
				}

				/**
				 * Parse the filter criteria to generate the needed
				 * WHERE condition. This is a trick to not write our own generation
				 * code by reusing what's inside GF_Query already as they
				 * take care of many small things like forcing numeric, etc.
				 */
				$_tmp_query       = new $query_class(
					$filter['form_id'],
					[
						'mode'          => 'any',
						'field_filters' => [ $filter ],
					]
				);
				$_tmp_query_parts = $_tmp_query->_introspect();

				/**
				 * @var GF_Query_Condition $search_condition
				 * */
				$search_condition = $_tmp_query_parts['where'];

				if ( empty( $filter['key'] ) && $search_condition->expressions ) {
					$search_conditions[] = $search_condition;
				} else {
					// If the left condition is empty, it is likely a multiple forms filter. In this case, we should retrieve the search condition from the main form.
					if ( ! $search_condition->left && $search_condition->expressions ) {
						$search_condition = $search_condition->expressions[0];
					}

					$left = $search_condition->left;

					// When casting a column value to a certain type (e.g., happens with the Number field), GF_Query_Column is wrapped in a GF_Query_Call class.
					if ( $left instanceof GF_Query_Call && $left->parameters ) {
						// Update columns to include the correct alias.
						$parameters = array_map( static function ( $parameter ) use ( $query ) {
							return $parameter instanceof GF_Query_Column
								? new GF_Query_Column(
									$parameter->field_id,
									$parameter->source,
									$query->_alias( $parameter->field_id,
										$parameter->source,
										$parameter->is_entry_column() ? 't' : 'm' )
								)
								: $parameter;
						}, $left->parameters );

						$left = new GF_Query_Call( $left->function_name, $parameters );
					} elseif ( $left ) {
						$alias = $query->_alias( $left->field_id, $left->source, $left->is_entry_column() ? 't' : 'm' );
						$left  = new GF_Query_Column( $left->field_id, $left->source, $alias );
					}

					if ( $this->is_product_field( $filter ) && ( $filter['is_numeric'] ?? false ) ) {
						$original_left = clone $left;
						$column        = $left instanceof GF_Query_Call ? $left->columns[0] ?? null : $left;
						$column_name   = sprintf( '`%s`.`%s`',
							$column->alias,
							$column->is_entry_column() ? $column->field_id : 'meta_value' );

						// Add the original join back.
						$search_conditions[] = new GF_Query_Condition( $column, null, $column );

						// Split product name for.
						$position = new GF_Query_Call( 'POSITION', [ sprintf( '"|" IN %s', $column_name ) ] );
						$left     = new GF_Query_Call( 'SUBSTR', [
							$column_name,
							sprintf( "%s + 1", $position->sql( $query ) ),
						] );

						// Remove currency symbol and format properly.
						$currency           = RGCurrency::get_currency( GFCommon::get_currency() );
						$symbol             = html_entity_decode( rgar( $currency, 'symbol_left' ) );
						$thousand_separator = rgar( $currency, 'thousand_separator' );
						$decimal_separator  = rgar( $currency, 'decimal_separator' );

						$replacements = [ $symbol => '', $thousand_separator => '' ];
						if ( ',' === $decimal_separator ) {
							$replacements[','] = '.';
						}

						foreach ( $replacements as $key => $value ) {
							$left = new GF_Query_Call( 'REPLACE', [
								$left->sql( $query ),
								'"' . $key . '"',
								'"' . $value . '"',
							] );
						}

						// Return original function call.
						if ( $original_left instanceof GF_Query_Call ) {
							$parameters    = $original_left->parameters;
							$function_name = $original_left->function_name;

							$parameters[0] = $left->sql( $query );
							if ( $function_name === 'CAST' ) {
								$function_name = ' ' . $function_name; // prevent regular `CAST` sql.
								if ( GF_Query::TYPE_DECIMAL === ( $parameters[1] ?? '' ) ) {
									$parameters[1] = 'DECIMAL(65,6)';
								}
								// CAST needs 'AND' as a separator.
								$parameters = [ implode( ' AS ', $parameters ) ];
							}

							$left = new GF_Query_Call( $function_name, $parameters );
						}
					} elseif ( $this->is_repeater_field( $filter ) ) {
						$field = GFAPI::get_field( $filter['form_id'] ?? 0, $filter['key'] ?? 0 );
						if ( ! $field ) {
							continue;
						}

						$repeater_conditions = [];
						foreach ( $this->get_nested_fields( $field ) as $sub_field ) {
							$repeater_conditions[] = new GF_Query_Condition(
								new GF_Query_Column( $sub_field ),
								$search_condition->operator,
								$search_condition->right
							);
						}
						if ( $repeater_conditions ) {
							$merged_condition    = GF_Query_Condition::_or( ...$repeater_conditions );
							$search_conditions[] = $merged_condition;
							continue;
						}
					}

					if ( $view->joins && GF_Query_Column::META == $left->field_id ) {
						foreach ( $view->joins as $_join ) {
							$on   = $_join->join_on;
							$join = $_join->join;

							$search_conditions[] = GF_Query_Condition::_or(
							// Join
								new GF_Query_Condition(
									new GF_Query_Column( GF_Query_Column::META,
										$join->ID,
										$query->_alias( GF_Query_Column::META, $join->ID, 'm' ) ),
									$search_condition->operator,
									$search_condition->right
								),
								// On
								new GF_Query_Condition(
									new GF_Query_Column( GF_Query_Column::META,
										$on->ID,
										$query->_alias( GF_Query_Column::META, $on->ID, 'm' ) ),
									$search_condition->operator,
									$search_condition->right
								)
							);
						}
					} else {
						$search_conditions[] = new GF_Query_Condition(
							$left,
							$search_condition->operator,
							$search_condition->right
						);
					}
				}
			}

			if ( $search_conditions ) {
				$search_conditions = 'all' === $mode
					? [ GF_Query_Condition::_and( ...$search_conditions ) ]
					: [ GF_Query_Condition::_or( ...$search_conditions ) ];
			}
		}

		/**
		 * Grab the current clauses. We'll be combining them shortly.
		 */
		$query_parts = $query->_introspect();

		if ( $include_global_search_words ) {
			global $wpdb;
			$extra_conditions[] = new GF_Query_Condition( new GF_Query_Call(
				'EXISTS',
				[
					sprintf(
						'SELECT 1 FROM `%s` WHERE `form_id` = %d AND `entry_id` = `%s`.`id` AND (%s)',
						GFFormsModel::get_entry_meta_table_name(),
						$view->form ? $view->form->ID : 0,
						$query->_alias( null, $view->form ? $view->form->ID : 0 ),
						implode( ' AND ', array_map( static function ( string $word ) use ( $wpdb ) {
							return $wpdb->prepare( '`meta_value` LIKE "%%%s%%"', $word );
						}, $include_global_search_words ) )
					),
				]
			) );
		}

		if ( $exclude_global_search_words ) {
			global $wpdb;
			$extra_conditions[] = new GF_Query_Condition( new GF_Query_Call(
				'NOT EXISTS',
				[
					sprintf(
						'SELECT 1 FROM `%s` WHERE `form_id` = %d AND `entry_id` = `%s`.`id` AND (%s)',
						GFFormsModel::get_entry_meta_table_name(),
						$view->form ? $view->form->ID : 0,
						$query->_alias( null, $view->form ? $view->form->ID : 0 ),
						implode( ' OR ', array_map( static function ( string $word ) use ( $wpdb ) {
							return $wpdb->prepare( '`meta_value` LIKE "%%%s%%"', $word );
						}, $exclude_global_search_words ) )
					),
				]
			) );
		}

		/**
		 * Combine the parts as a new WHERE clause.
		 */
		$where = \GF_Query_Condition::_and(
			...array_merge(
				[ $query_parts['where'] ],
				$search_conditions,
				$extra_conditions
			)
		);
		$query->where( $where );
	}

	/**
	 * Whether the field in the filter is a product field.
	 *
	 * @since 2.22
	 *
	 * @param array $filter The filter object.
	 *
	 * @return bool
	 */
	private function is_product_field( array $filter ): bool {
		$field = GFAPI::get_field( $filter['form_id'] ?? 0, $filter['key'] ?? 0 );

		return $field && \GFCommon::is_product_field( $field->type );
	}

	/**
	 * Whether the field in the filter is a repeater field.
	 *
	 * @since 2.51.0
	 *
	 * @param array $filter The filter object.
	 *
	 * @return bool
	 */
	private function is_repeater_field( array $filter ): bool {
		$field = GFAPI::get_field( $filter['form_id'] ?? 0, $filter['key'] ?? 0 );

		return $field instanceof GF_Field_Repeater;
	}

	/**
	 * Returns the nested field IDs of fields that have values.
	 *
	 * @since 2.51.0
	 *
	 * @param GF_Field $field The field to retrieve the nested field IDs for.
	 *
	 * @return int[] The nested field ID's.
	 */
	private function get_nested_fields( GF_Field $field ): array {
		$result = [];
		foreach ( $field->fields ?? [] as $sub_field ) {
			if ( ! $sub_field instanceof GF_Field_Repeater ) {
				$result[] = [ $sub_field->id ];
				continue;
			}

			$result[] = $this->get_nested_fields( $sub_field );
		}

		return array_merge( [], ...$result );
	}

	/**
	 * Get the Field Format form GravityForms
	 *
	 * @since 1.10
	 *
	 * @param GF_Field_Date $field The field object
	 *
	 * @return string Format of the date in the database
	 */
	public static function get_date_field_format( GF_Field_Date $field ) {
		$format     = 'm/d/Y';
		$datepicker = [
			'mdy'       => 'm/d/Y',
			'dmy'       => 'd/m/Y',
			'dmy_dash'  => 'd-m-Y',
			'dmy_dot'   => 'd.m.Y',
			'ymd_slash' => 'Y/m/d',
			'ymd_dash'  => 'Y-m-d',
			'ymd_dot'   => 'Y.m.d',
		];

		if ( ! empty( $field->dateFormat ) && isset( $datepicker[ $field->dateFormat ] ) ) {
			$format = $datepicker[ $field->dateFormat ];
		}

		return $format;
	}

	/**
	 * Format a date value
	 *
	 * @since 2.1.2
	 *
	 * @param string $format       Wanted formatted date
	 *
	 * @param string $value        Date value input
	 * @param string $value_format The value format. Default: Y-m-d
	 *
	 * @return string
	 */
	public static function get_formatted_date( $value = '', $format = 'Y-m-d', $value_format = 'Y-m-d' ) {
		$date = date_create_from_format( $value_format, $value );

		if ( empty( $date ) ) {
			gravityview()->log->debug( 'Date format not valid: {value}', [ 'value' => $value ] );

			return '';
		}

		return $date->format( $format );
	}

	/**
	 * Include this extension templates path
	 *
	 * @param array $file_paths List of template paths ordered
	 */
	public function add_template_path( $file_paths ) {
		// Index 100 is the default GravityView template path.
		$file_paths[102] = self::$file . 'templates/';

		return $file_paths;
	}

	/**
	 * Retrieves the search fields based on the (legacy) configuration.
	 *
	 * @since 2.42
	 *
	 * @param array     $widget_args        The widget's configuration.
	 * @param View|null $view               The View.
	 * @param array     $additional_context Any additional context.
	 *
	 * @return Search_Field_Collection The Search Field Collection.
	 */
	private function get_search_field_collection(
		array $widget_args,
		?View $view,
		array $additional_context = []
	): Search_Field_Collection {
		if ( isset( $widget_args['search_fields_section'] ) ) {
			return Search_Field_Collection::from_configuration(
				(array) $widget_args['search_fields_section'],
				$view,
				$additional_context
			);
		}

		return Search_Field_Collection::from_legacy_configuration(
			$widget_args,
			$view,
			$additional_context
		);
	}
	/**
	 * Renders the Search Widget
	 *
	 * @param array                       $widget_args
	 * @param string                      $content
	 * @param string|\GV\Template_Context $context
	 *
	 * @return void
	 */
	public function render_frontend( $widget_args, $content = '', $context = '' ) {
		if ( $context instanceof \GV\Template_Context ) {
			$view_id = $context->view->ID;
			$view    = $context->view;
		} else {
			$view_id = \GV\Utils::get( $widget_args, 'view_id', 0 );
			$view    = \GV\View::by_id( $view_id );
		}

		$additional_context           = compact( 'context', 'widget_args' );
		$additional_context['widget'] = $this;

		if ( ! $view ) {
			gravityview()->log->error( 'View not found', [ 'data' => $widget_args ] );

			return;
		}

		$search_fields = $this->get_search_field_collection( $widget_args, $view, $additional_context );

		if ( ! $search_fields->count() ) {
			gravityview()->log->debug( 'No search fields configured for widget:', [ 'data' => $widget_args ] );
			return;
		}

		$submit_field = $search_fields->by_type( Search_Field_Submit::class )->first();
		$search_clear = $submit_field && ( $submit_field->to_configuration()['search_clear'] ?? false );

		$search_mode_field = $search_fields->by_type( Search_Field_Search_Mode::class )->first();
		$search_mode       = $search_mode_field && ( $search_mode_field->to_configuration()['mode'] ?? 'any' );

		// Before rendering, we want to make sure the submit and search mode field are added.
		$search_fields = $search_fields->ensure_required_search_fields();

		if ( $search_fields->has_date_field() ) {
			// enqueue datepicker stuff only if needed!
			$this->enqueue_datepicker();
		}

		$search_layout = ( ! empty( $widget_args['search_layout'] ) ? $widget_args['search_layout'] . ' gv-search-rows' : 'rows' );
		$custom_class  = ! empty( $widget_args['custom_class'] ) ? $widget_args['custom_class'] : '';

		$data = [
			'datepicker_class'            => $this->get_datepicker_class(),
			'search_method'               => $this->get_search_method(),
			'search_layout'               => $search_layout,
			'search_mode'                 => ( ! empty( $widget_args['search_mode'] ) ? $widget_args['search_mode'] : $search_mode ),
			'search_clear'                => ( ! empty( $widget_args['search_clear'] ) ? $widget_args['search_clear'] : $search_clear ),
			'view_id'                     => $view_id,
			'form_id'                     => $view->form ? $view->form->ID : 0,
			'search_class'                => self::get_search_class( $custom_class, $search_layout ),
			'permalink_fields'            => $this->add_no_permalink_fields( [], $this, $widget_args ),
			'search_form_action'          => self::get_search_form_action(),
			'search_fields'               => $search_fields,
			'search_rows_search-general'  => Grid::get_rows_from_collection( $search_fields, 'search-general' ),
			'search_rows_search-advanced' => Grid::get_rows_from_collection( $search_fields, 'search-advanced' ),
		];

		GravityView_View::getInstance()->render( 'widget', 'search', false, $data );
	}

	/**
	 * Get the search class for a search form.
	 *
	 * @since 1.5.4
	 * @since 2.42
	 *
	 * @param string $custom_class  Custom class to add to the search form
	 * @param string $search_layout Search layout ("horizontal" or "vertical"). Default: "horizontal".
	 *
	 * @return string Sanitized CSS class for the search form
	 */
	public static function get_search_class( $custom_class = '', $search_layout = 'horizontal' ) {
		$search_class = 'gv-search-' . $search_layout;

		if ( ! empty( $custom_class ) ) {
			$search_class .= ' ' . $custom_class;
		}

		/**
		 * Modify the CSS class for the search form.
		 *
		 * @param string $search_class The CSS class for the search form
		 */
		$search_class = apply_filters( 'gravityview_search_class', $search_class );

		// Is there an active search being performed? Used by fe-views.js
		$search_class .= gravityview()->request->is_search() || GravityView_frontend::getInstance()->isSearch() ? ' gv-is-search' : '';

		return gravityview_sanitize_html_class( $search_class );
	}

	/**
	 * Calculate the search form action
	 *
	 * @since 1.6
	 * @since 2.42
	 *
	 * @return string
	 */
	public static function get_search_form_action( $post_id = 0 ) {
		if ( empty( $post_id ) ) {
			$gravityview_view = GravityView_View::getInstance();

			$post_id = $gravityview_view->getPostId() ? $gravityview_view->getPostId() : $gravityview_view->getViewId();
		}

		$url = add_query_arg( [], get_permalink( $post_id ) );

		/**
		 * Override the search URL.
		 *
		 * @param string $action Where the form submits to.
		 *
		 * Further parameters will be added once adhoc context is added.
		 * Use gravityview()->request until then.
		 */
		return apply_filters( 'gravityview/widget/search/form/action', $url );
	}

	/**
	 * Output the Clear Search Results button
	 *
	 * @since 1.5.4
	 */
	public static function the_clear_search_button() {
		_deprecated_function( __METHOD__,
			'$ver$',
			'The button is now available in the templates as global $data[\'search_clear\']' );
	}

	/**
	 * Require the datepicker script for the frontend GV script
	 *
	 * @param array $js_dependencies Array of existing required scripts for the fe-views.js script
	 *
	 * @return array Array required scripts, with `jquery-ui-datepicker` added
	 */
	public function add_datepicker_js_dependency( $js_dependencies ) {
		$js_dependencies[] = 'jquery-ui-datepicker';

		return $js_dependencies;
	}

	/**
	 * Modify the array passed to wp_localize_script()
	 *
	 * @param array $js_localization The data padded to the Javascript file
	 * @param array $view_data       View data array with View settings
	 *
	 * @return array
	 */
	public function add_datepicker_localization( $localizations = [], $view_data = [] ) {
		global $wp_locale;

		/**
		 * Modify the datepicker settings.
		 *
		 * @see http://api.jqueryui.com/datepicker/ Learn what settings are available
		 * @see http://www.renegadetechconsulting.com/tutorials/jquery-datepicker-and-wordpress-i18n Thanks for the helpful information on $wp_locale
		 *
		 * @param array $js_localization The data padded to the Javascript file
		 * @param array $view_data       View data array with View settings
		 */
		$datepicker_settings = apply_filters(
			'gravityview_datepicker_settings',
			[
				'yearRange'       => '-5:+5',
				'changeMonth'     => true,
				'changeYear'      => true,
				'closeText'       => esc_attr_x( 'Close', 'Close calendar', 'gk-gravityview' ),
				'prevText'        => esc_attr_x( 'Prev', 'Previous month in calendar', 'gk-gravityview' ),
				'nextText'        => esc_attr_x( 'Next', 'Next month in calendar', 'gk-gravityview' ),
				'currentText'     => esc_attr_x( 'Today', 'Today in calendar', 'gk-gravityview' ),
				'weekHeader'      => esc_attr_x( 'Week', 'Week in calendar', 'gk-gravityview' ),
				'monthStatus'     => __( 'Show a different month', 'gk-gravityview' ),
				'monthNames'      => array_values( $wp_locale->month ),
				'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
				'dayNames'        => array_values( $wp_locale->weekday ),
				'dayNamesShort'   => array_values( $wp_locale->weekday_abbrev ),
				'dayNamesMin'     => array_values( $wp_locale->weekday_initial ),
				// get the start of week from WP general setting
				'firstDay'        => get_option( 'start_of_week' ),
				// is Right to left language? default is false
				'isRTL'           => is_rtl(),
			],
			$view_data
		);

		$localizations['datepicker'] = $datepicker_settings;

		return $localizations;
	}

	/**
	 * Enqueue the datepicker script
	 *
	 * @return void
	 * @todo Use own datepicker javascript instead of GF datepicker.js - that way, we can localize the settings and not
	 *       require the changeMonth and changeYear pickers.
	 */
	public function enqueue_datepicker() {
		$gravityview_view = GravityView_View::getInstance();

		wp_enqueue_script( 'jquery-ui-datepicker' );

		add_filter( 'gravityview_js_dependencies', [ $this, 'add_datepicker_js_dependency' ] );
		add_filter( 'gravityview_js_localization', [ $this, 'add_datepicker_localization' ], 10, 2 );

		$scheme = is_ssl() ? 'https://' : 'http://';
		wp_enqueue_style(
			'jquery-ui-datepicker',
			$scheme . 'ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/smoothness/jquery-ui.css',
		);
	}

	private function get_datepicker_class() {
		/**
		 * @filter `gravityview_search_datepicker_class`
		 * Modify the CSS class for the datepicker, used by the CSS class is used by Gravity Forms' javascript to determine the format for the date picker. The `gv-datepicker` class is required by the GravityView datepicker javascript.
		 *
		 * @param string $css_class CSS class to use. Default: `gv-datepicker datepicker mdy` \n
		 *                          Options are:
		 *                          - `mdy` (mm/dd/yyyy)
		 *                          - `dmy` (dd/mm/yyyy)
		 *                          - `dmy_dash` (dd-mm-yyyy)
		 *                          - `dmy_dot` (dd.mm.yyyy)
		 *                          - `ymd_slash` (yyyy/mm/dd)
		 *                          - `ymd_dash` (yyyy-mm-dd)
		 *                          - `ymd_dot` (yyyy.mm.dd)
		 */
		$datepicker_class = apply_filters(
			'gravityview_search_datepicker_class',
			'gv-datepicker datepicker ' . $this->get_datepicker_format(),
		);

		return $datepicker_class;
	}

	/**
	 * Retrieve the datepicker format.
	 *
	 * @see https://docs.gravitykit.com/article/115-changing-the-format-of-the-search-widgets-date-picker
	 *
	 * @param bool $date_format Whether to return the PHP date format or the datepicker class name. Default: false.
	 *
	 * @return string The datepicker format placeholder, or the PHP date format.
	 */
	private function get_datepicker_format( $date_format = false ) {
		return $date_format
			? Search_Policy::get_date_php_format()
			: Search_Policy::get_date_format_key();
	}

	/**
	 * If previewing a View or page with embedded Views, make the search work properly by adding hidden fields with
	 * query vars
	 *
	 * @since 2.2.1
	 *
	 * @return void
	 */
	public function add_preview_inputs() {
		global $wp;

		if ( ! is_preview() || ! current_user_can( 'publish_gravityviews' ) ) {
			return;
		}

		// Outputs `preview` and `post_id` variables
		foreach ( $wp->query_vars as $key => $value ) {
			printf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $key ), esc_attr( $value ) );
		}
	}

	/**
	 * Adds search fields for a specific form.
	 *
	 * @since 2.42
	 *
	 * @param Search_Field[] $search_fields The fields.
	 * @param int            $form_id       The form ID.
	 *
	 * @return Search_Field[] The update fields.
	 */
	public function add_form_search_fields( array $search_fields, int $form_id ): array {
		if ( ! $form_id ) {
			return $search_fields;
		}

		$fields = gravityview_get_form_fields( $form_id, true, true );

		/**
		 * Modify the fields that are displayed as searchable in the Search Bar dropdown.
		 *
		 * @since 1.17
		 * @see   gravityview_get_form_fields() Used to fetch the fields
		 * @see   GravityView_Widget_Search::get_search_input_types See this method to modify the type of input types allowed for a field
		 *
		 * @param array $fields  Array of searchable fields, as fetched by gravityview_get_form_fields()
		 * @param int   $form_id The form ID.
		 */
		$fields = apply_filters( 'gravityview/search/searchable_fields', $fields, $form_id );

		$blocklist_field_types = apply_filters(
			'gravityview_blocklist_field_types',
			[ 'fileupload', 'post_image', 'post_id', 'section' ]
		);
		$blocklist_sub_fields  = apply_filters(
			'gravityview_blocklist_sub_fields',
			[ 'image_choice', 'multi_choice' ]
		);

		foreach ( $fields as $id => $field ) {
			if (
				in_array( $field['type'], $blocklist_field_types, true )
				|| ( in_array( $field['type'], $blocklist_sub_fields, true ) && null !== $field['parent'] )
			) {
				continue;
			}

			$field['id']      = $id;
			$field['form_id'] = $form_id;

			$field_instance = Search_Field_Gravity_Forms::from_field( $field );
			if ( ! $field_instance ) {
				continue;
			}

			$search_fields[] = $field_instance;
		}

		return $search_fields;
	}

	/**
	 * Returns all the searchable fields for a View in the legacy format.
	 *
	 * @since 2.42
	 *
	 * @param View $view The View.
	 *
	 * @internal Do not depend on this method.
	 *
	 * @return array{field: string, label:string, input_type:string}[] The searchable fields in the legacy format.
	 */
	final public function get_search_fields( View $view ): array {
		$search_fields = [];
		$collection    = $this->get_search_field_collection( $this->configuration->all(), $view );

		foreach ( $collection->all() as $field ) {
			$search_fields[] = $field->to_legacy_format();
		}

		return $search_fields;
	}

	/**
	 * Add the settings for this field.
	 *
	 * @since 2.42
	 *
	 * @param array      $options     The original options.
	 * @param string     $template_id The template ID.
	 * @param string     $field_id    The field ID.
	 * @param string     $context     The area ID.
	 * @param string     $input_type  The (optional) input type.
	 * @param string|int $form_id     The form ID.
	 *
	 * @return array The updated options.
	 */
	final public function set_search_field_options(
		$options = [],
		$template_id = '',
		$field_id = '',
		$context = '',
		$input_type = '',
		$form_id = 0
	): array {
		$search_field = Search_Field_Collection::get_field_by_field_id( (int) $form_id, (string) $field_id );
		if ( ! $search_field ) {
			return $options;
		}

		return $search_field->merge_options( $options );
	}

	/**
	 * Renders the search areas in the settings field.
	 *
	 * @since 2.42
	 *
	 * @param array $field The field configuration.
	 *
	 * @return string
	 */
	private function get_search_sections( array $field ): string {
		global $post;

		$directory_entries_template = $this->search_fields_context['rendering']['template_id'] ?? 'default_table';

		// If no value is present, check if we have a legacy configuration.
		if ( null === ( $field['value'] ?? null ) ) {
			$search_fields = Search_Field_Collection::from_legacy_configuration(
				$this->search_fields_context['settings'] ?? [],
				View::from_post( $post )
			);

			if ( $search_fields->count() > 0 ) {
				// Set the legacy configuration on the field value as the Search Fields configuration.
				$field['value'] = $search_fields->to_configuration();
			}
		}

		ob_start();
		?>

		<div data-search-fields="<?php echo esc_attr( $field['name'] ?? '' ); ?>">
			<div class="gv-section">
				<h4><?php esc_html_e( 'Search fields shown', 'gk-gravityview' ); ?></h4>

				<div class="search-active-fields">
					<?php
					do_action(
						'gravityview_render_search_active_areas',
						$directory_entries_template,
						'search-general',
						$field
					);
					?>
				</div>

				<h4>
					<?php esc_html_e( 'Advanced Search fields shown', 'gk-gravityview' ); ?>
					<span>
						<?php
						esc_html_e(
							'If any Advanced Search fields exist, a link will show to toggle them.',
							'gk-gravityview'
						);
						?>
					</span>
				</h4>

				<div class="search-advanced-active-fields">
					<?php
					do_action(
						'gravityview_render_search_active_areas',
						$directory_entries_template,
						'search-advanced',
						$field
					);
					?>
				</div>
			</div>
		</div>
		<?php

		return ob_get_clean();
	}

	/**
	 * Render the search areas.
	 *
	 * @since 2.42
	 *
	 * @param string                          $template_id The current slug of the selected View template.
	 * @param string                          $zone        Either 'search-general' or 'search-advanced'.
	 * @param array{name:string, value:mixed} $data        The search field data.
	 */
	public function render_search_active_areas( string $template_id, string $zone, array $data ): void {
		global $post;
		$admin_views = GravityView_Admin_Views::get_instance();

		$fields    = $data['value'] ?? null;
		$rows      = [ Grid::get_row_by_type( '100' ) ];
		$name      = $data['name'] ?? null;
		$has_value = null !== $fields;

		$is_new = 'auto-draft' === get_post_status( $post );

		if ( $has_value ) {
			$collection = Search_Field_Collection::from_configuration( $fields );
			$rows       = Grid::get_rows_from_collection( $collection, $zone );
		} elseif ( $is_new && 'search-general' === $zone ) {
			$area_key = key( $rows[0] );
			$zone_100 = $zone . '_' . ( $rows[0][ $area_key ][0]['areaid'] ?? 'top' );

			$fields = [
				$zone_100 => [
					Grid::uid() => ( new Search_Field_All() )->to_configuration(),
					Grid::uid() => ( new Search_Field_Submit() )->to_configuration(),
					Grid::uid() => ( new Search_Field_Search_Mode() )->to_configuration(),
				],
			];
		}
		?>

		<div data-grid-connect="search" data-grid-context="<?php echo esc_attr($zone); ?>" class="gv-grid gv-grid-pad gv-grid-border" id="search-<?php echo $zone; ?>-fields">
			<?php
			$type       = 'search';
			$is_dynamic = true;

			echo '<div class="gv-grid-rows-container">';
			ob_start();
			$admin_views->render_active_areas( $template_id, $type, $zone, $rows, $fields );
			$content = ob_get_clean();

			// replace input names.
			echo str_replace(
				[ sprintf( 'name="%ss[', $type ), 'name="areas[' ],
				[ sprintf( 'name="%s[', $name ), sprintf( 'name="%s[', $name ) ],
				$content
			);
			echo '</div>';

			/**
			 * Allows additional content after the zone was rendered.
			 *
			 * @filter `gk/gravityview/admin/view/after-zone`
			 *
			 * @param string $template_id Template ID.
			 * @param string $type        The zone type (field or widget).
			 * @param string $zone        Current View zone: `directory`, `single`, `edit`, `search-general`, or `search-advanced`.
			 * @param bool   $is_dynamic  Whether the zone is dynamic.
			 */
			do_action( 'gk/gravityview/admin-views/view/after-zone', $template_id, $type, $zone, $is_dynamic );
			?>
		</div>
		<?php
	}

	/**
	 * Render html for displaying available search fields.
	 *
	 * @since 2.42
	 */
	public function render_available_search_fields( ?int $form_id = 0, ?string $section = null ): void {
		global $post;

		if ( ! $form_id ) {
			$view = View::by_id( $post->ID ?? 0 );
			if ( ! $view instanceof View || ! $view->form instanceof GF_Form ) {
				return;
			}
			$form_id = $view->form->ID ?? 0;
		}

		$search_fields = Search_Field_Collection::available_fields( $form_id, $section );
		if ( ! $search_fields->count() ) {
			return;
		}

		foreach ( $search_fields as $search_field ) {
			echo $search_field;
		}
	}

	/**
	 * Records the rendering context of a search field about to be rendered.
	 *
	 * @since 2.42
	 *
	 * @param string $field_type Either 'widget', 'field' or 'search'.
	 * @param string $key        The key of the settings field.
	 * @param array  $option     The configuration of the settings field.
	 * @param array  $settings   All the values for the current item being rendered.
	 * @param array  $rendering  Extra rendering context added to the action.
	 */
	public function record_search_field_context( string $field_type, string $key, array $option, array $settings, array $rendering ): void {
		if (
			'widget' !== $field_type
			|| 'search_fields_section' !== $key
		) {
			return;
		}

		$this->search_fields_context = [
			'settings'  => $settings,
			'rendering' => $rendering,
		];
	}

	/**
	 * Resets the recorded areas for the next row.
	 *
	 * @since 2.44
	 *
	 * @param bool   $is_dynamic  Whether the area is dynamic.
	 * @param string $template_id The template ID.
	 * @param string $type        The object type (widget or field).
	 * @param string $zone        The render zone.
	 */
	public function reset_area_recording( $is_dynamic, $template_id, $type, $zone ): void {
		if ( 'search' !== $type ) {
			return;
		}

		$this->area_settings = [];
	}

	/**
	 * Renders a "Clear all fields" button in the View configuration.
	 *
	 * @since 2.44
	 *
	 * @param array  $area        The area.
	 * @param string $type        The type.
	 * @param array  $values      The values in the area.
	 * @param bool   $is_dynamic  Whether the zone is dynamic.
	 * @param string $template_id The template ID.
	 * @param string $zone        The zone.
	 */
	public function add_search_area_settings_button( $area, $type, $values, $is_dynamic, $template_id, $zone ): void {
		if ( 'search' !== $type ) {
			return;
		}

		$area['settings'] = $values[ $zone . '_' . $area['areaid'] ]['area_settings'] ?? [];
		// Record the area for rendering the settings after the row.
		$this->area_settings[] = $area;

		printf(
			'<a role="button" href="javascript:void(0);" class="gv-search-area-settings" data-areaid="%s" title="%s"><i class="dashicons dashicons-admin-generic"></i></a>',
			esc_attr( $zone . '_' . $area['areaid'] ),
			esc_attr__( 'Configure Area Settings', 'gk-gravityview' ),
		);
	}

	/**
	 * Registers the area settings for the search fields.
	 *
	 * @since 2.44
	 *
	 * @param array  $settings    The area settings.
	 * @param string $template_id The template ID.
	 * @param string $field_id    The Field ID.
	 *
	 * @return array
	 */
	public function add_search_area_settings( $settings, $template_id, $field_id ): array {
		if ( ! is_array( $settings ) ) {
			$settings = [];
		}

		if ( 'area_settings' !== $field_id ) {
			return $settings;
		}

		$settings['layout'] = [
			'type'    => 'select',
			'label'   => __( 'Arrange Fields:', 'gk-gravityview' ),
			'choices' => [
				'column' => esc_html__( 'Stacked (vertical)', 'gk-gravityview' ),
				'row'    => esc_html__( 'Side by side (horizontal)', 'gk-gravityview' ),
			],
			'value'   => 'column',
		];

		return $settings;
	}

	/**
	 * Renders the settings for a search area.
	 *
	 * @since 2.44
	 *
	 * @param bool   $is_dynamic  Whether the area is dynamic.
	 * @param View   $view        The View.
	 * @param string $template_id The template ID.
	 * @param string $type        The object type (widget or field).
	 * @param string $zone        The render zone.
	 */
	public function render_area_settings( $is_dynamic, $view, $template_id, $type, $zone ): void {
		if ( 'search' !== $type || ! $this->area_settings ) {
			return;
		}

		$html = '';

		foreach ( $this->area_settings as $area ) {
			if ( ! isset( $area['areaid'] ) ) {
				continue;
			}

			$settings = GravityView_Render_Settings::render_field_options(
				0,
				'area',
				$template_id,
				'area_settings',
				esc_html__( 'Column', 'gk-gravityview' ),
				$zone . '_' . $area['areaid'],
				null,
				'area_settings',
				$area['settings'] ?? [],
				'area_settings',
				[
					'label' => esc_html__( 'Column Settings', 'gk-gravityview' ),
				]
			);

			// Remove no options indicator to avoid disabling the search widget settings icon.
			$settings = str_replace( GravityView_Render_Settings::NO_OPTIONS, '', $settings );

			$html .= sprintf(
				'<div class="area-settings-container" data-areaid="%s">%s</div>',
				$zone . '_' . $area['areaid'],
				$settings
			);
		}

		if ( $html ) {
			printf( '<div style="display:none;" class="area-settings-wrapper">%s</div>', $html );
		}

		// Reset areas for next rendering.
		$this->area_settings = [];
	}

} // end class

new GravityView_Widget_Search();

if ( ! gravityview()->plugin->supports( \GV\Plugin::FEATURE_GFQUERY ) ) {
	return;
}

/**
 * A GF_Query condition that allows user data searches.
 */
class GravityView_Widget_Search_Author_GF_Query_Condition extends \GF_Query_Condition {
	/**
	 * The View object.
	 *
	 * @since 2.2.2
	 *
	 * @var View
	 */
	private $view;

	/**
	 * The value to search.
	 *
	 * @since 2.2.2
	 *
	 * @var mixed
	 */
	private $value;

	public function __construct( $filter, $view ) {
		$this->value = $filter['value'];
		$this->view  = $view;
	}

	/**
	 * Serializes the object.
	 *
	 * @since 2.42
	 *
	 * @return array THe serialized data.
	 */
	public function __serialize(): array {
		return [
			'view_id' => $this->view->ID,
			'value'   => $this->value,
		];
	}

	/**
	 * Deserializes the object.
	 *
	 * @since 2.42
	 */
	public function __unserialize( array $data ): void {
		$this->value = $data['value'];
		$this->view  = View::by_id( $data['view_id'] ?? 0 );
	}

	public function sql( $query ) {
		global $wpdb;

		$user_meta_fields = [
			'nickname',
			'first_name',
			'last_name',
		];

		/**
		 * Filter the user meta fields to search.
		 *
		 * @param array    $user_meta_fields The user meta fields.
		 * @param \GV\View $view             The View.
		 */
		$user_meta_fields = apply_filters(
			'gravityview/widgets/search/created_by/user_meta_fields',
			$user_meta_fields,
			$this->view
		);

		$user_fields = [
			'user_nicename',
			'user_login',
			'display_name',
			'user_email',
		];

		/**
		 * Filter the user fields to search.
		 *
		 * @param array    $user_fields The user fields.
		 * @param \GV\View $view        The View.
		 */
		$user_fields = apply_filters( 'gravityview/widgets/search/created_by/user_fields', $user_fields, $this->view );

		$conditions = [];

		foreach ( $user_fields as $user_field ) {
			$conditions[] = $wpdb->prepare( "`u`.`$user_field` LIKE %s", '%' . $wpdb->esc_like( $this->value ) . '%' );
		}

		foreach ( $user_meta_fields as $meta_field ) {
			$conditions[] = $wpdb->prepare(
				'(`um`.`meta_key` = %s AND `um`.`meta_value` LIKE %s)',
				$meta_field,
				'%' . $wpdb->esc_like( $this->value ) . '%'
			);
		}

		$conditions = '(' . implode( ' OR ', $conditions ) . ')';

		$alias = $query->_alias( null );

		return "(EXISTS (SELECT 1 FROM $wpdb->users u LEFT JOIN $wpdb->usermeta um ON u.ID = um.user_id WHERE (u.ID = `$alias`.`created_by` AND $conditions)))";
	}
}
