is_search();
}
/**
* Initialize class.
*
* @since 1.7.2
*/
public function init() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$this->term = isset( $_GET['search']['term'] ) ? sanitize_text_field( wp_unslash( $_GET['search']['term'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$this->term_escaped = isset( $_GET['search']['term'] ) ? esc_html( wp_unslash( $_GET['search']['term'] ) ) : '';
if ( ! $this->allow_load() ) {
return;
}
$this->hooks();
}
/**
* Hooks.
*
* @since 1.7.2
*/
private function hooks() {
// Use filter to add the search term to the get forms arguments.
add_filter( 'wpforms_get_multiple_forms_args', [ $this, 'get_forms_args' ] );
// Encapsulate search into posts_where.
add_action( 'wpforms_form_handler_get_multiple_before_get_posts', [ $this, 'before_get_posts' ] );
add_action( 'wpforms_form_handler_get_multiple_after_get_posts', [ $this, 'after_get_posts' ], 10, 2 );
}
/**
* Determine whether a search is performing.
*
* @since 1.7.2
*
* @return bool
*/
private function is_search() {
return ! wpforms_is_empty_string( $this->term_escaped );
}
/**
* Count search results.
*
* @since 1.7.2
* @deprecated 1.7.5
*
* @param array $count Number of forms in different views.
* @param array $args Get forms arguments.
*
* @return array
*/
public function update_count( $count, $args ) {
_deprecated_function( __METHOD__, '1.7.5 of the WPForms plugin', "wpforms()->obj( 'forms_views' )->update_count()" );
return wpforms()->obj( 'forms_views' )->update_count();
}
/**
* Pass the search term to the arguments array.
*
* @since 1.7.2
*
* @param array $args Get posts arguments.
*
* @return array
*/
public function get_forms_args( $args ) {
if ( is_numeric( $this->term ) ) {
$args['post__in'] = [ absint( $this->term ) ];
} else {
$args['search']['term'] = $this->term;
$args['search']['term_escaped'] = $this->term_escaped;
}
return $args;
}
/**
* Before get_posts() call routine.
*
* @since 1.7.2
*
* @param array $args Arguments of the `get_posts()`.
*/
public function before_get_posts( $args ) {
// The `posts_where` hook is very general and has broad usage across the WP core and tons of plugins.
// Therefore, in order to do not break something,
// we should add this hook right before the call of `get_posts()` inside \WPForms_Form_Handler::get_multiple().
add_filter( 'posts_where', [ $this, 'search_by_term_where' ], 10, 2 );
}
/**
* After get_posts() call routine.
*
* @since 1.7.2
*
* @param array $args Arguments of the get_posts().
* @param array $forms Forms data. Result of getting multiple forms.
*/
public function after_get_posts( $args, $forms ) {
// The `posts_where` hook is very general and has broad usage across the WP core and tons of plugins.
// Therefore, in order to do not break something,
// we should remove this hook right after the call of `get_posts()` inside \WPForms_Form_Handler::get_multiple().
remove_filter( 'posts_where', [ $this, 'search_by_term_where' ] );
}
/**
* Modify the WHERE clause of the SQL query in order to search forms by given term.
*
* @since 1.7.2
*
* @param string $where WHERE clause.
* @param \WP_Query $wp_query The WP_Query instance.
*
* @return string
*/
public function search_by_term_where( $where, $wp_query ) {
if ( is_numeric( $this->term ) ) {
return $where;
}
global $wpdb;
// When user types only HTML tag (