Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/wpforms-.../src/Admin/Forms
File: Search.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace WPForms\Admin\Forms;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Search Forms feature.
[5] Fix | Delete
*
[6] Fix | Delete
* @since 1.7.2
[7] Fix | Delete
*/
[8] Fix | Delete
class Search {
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Current search term.
[12] Fix | Delete
*
[13] Fix | Delete
* @since 1.7.2
[14] Fix | Delete
*
[15] Fix | Delete
* @var string
[16] Fix | Delete
*/
[17] Fix | Delete
private $term;
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Current search term escaped.
[21] Fix | Delete
*
[22] Fix | Delete
* @since 1.7.2
[23] Fix | Delete
*
[24] Fix | Delete
* @var string
[25] Fix | Delete
*/
[26] Fix | Delete
private $term_escaped;
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Determine if the class is allowed to load.
[30] Fix | Delete
*
[31] Fix | Delete
* @since 1.7.2
[32] Fix | Delete
*
[33] Fix | Delete
* @return bool
[34] Fix | Delete
*/
[35] Fix | Delete
private function allow_load() {
[36] Fix | Delete
[37] Fix | Delete
// Load only on the `All Forms` admin page and only if the search should be performed.
[38] Fix | Delete
return wpforms_is_admin_page( 'overview' ) && $this->is_search();
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Initialize class.
[43] Fix | Delete
*
[44] Fix | Delete
* @since 1.7.2
[45] Fix | Delete
*/
[46] Fix | Delete
public function init() {
[47] Fix | Delete
[48] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
[49] Fix | Delete
$this->term = isset( $_GET['search']['term'] ) ? sanitize_text_field( wp_unslash( $_GET['search']['term'] ) ) : '';
[50] Fix | Delete
[51] Fix | Delete
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
[52] Fix | Delete
$this->term_escaped = isset( $_GET['search']['term'] ) ? esc_html( wp_unslash( $_GET['search']['term'] ) ) : '';
[53] Fix | Delete
[54] Fix | Delete
if ( ! $this->allow_load() ) {
[55] Fix | Delete
return;
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
$this->hooks();
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Hooks.
[63] Fix | Delete
*
[64] Fix | Delete
* @since 1.7.2
[65] Fix | Delete
*/
[66] Fix | Delete
private function hooks() {
[67] Fix | Delete
[68] Fix | Delete
// Use filter to add the search term to the get forms arguments.
[69] Fix | Delete
add_filter( 'wpforms_get_multiple_forms_args', [ $this, 'get_forms_args' ] );
[70] Fix | Delete
[71] Fix | Delete
// Encapsulate search into posts_where.
[72] Fix | Delete
add_action( 'wpforms_form_handler_get_multiple_before_get_posts', [ $this, 'before_get_posts' ] );
[73] Fix | Delete
add_action( 'wpforms_form_handler_get_multiple_after_get_posts', [ $this, 'after_get_posts' ], 10, 2 );
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Determine whether a search is performing.
[78] Fix | Delete
*
[79] Fix | Delete
* @since 1.7.2
[80] Fix | Delete
*
[81] Fix | Delete
* @return bool
[82] Fix | Delete
*/
[83] Fix | Delete
private function is_search() {
[84] Fix | Delete
[85] Fix | Delete
return ! wpforms_is_empty_string( $this->term_escaped );
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Pass the search term to the arguments array.
[90] Fix | Delete
*
[91] Fix | Delete
* @since 1.7.2
[92] Fix | Delete
*
[93] Fix | Delete
* @param array $args Get posts arguments.
[94] Fix | Delete
*
[95] Fix | Delete
* @return array
[96] Fix | Delete
*/
[97] Fix | Delete
public function get_forms_args( $args ) {
[98] Fix | Delete
[99] Fix | Delete
if ( is_numeric( $this->term ) ) {
[100] Fix | Delete
$args['post__in'] = [ absint( $this->term ) ];
[101] Fix | Delete
} else {
[102] Fix | Delete
$args['search']['term'] = $this->term;
[103] Fix | Delete
$args['search']['term_escaped'] = $this->term_escaped;
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
return $args;
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* Before get_posts() call routine.
[111] Fix | Delete
*
[112] Fix | Delete
* @since 1.7.2
[113] Fix | Delete
*
[114] Fix | Delete
* @param array $args Arguments of the `get_posts()`.
[115] Fix | Delete
*/
[116] Fix | Delete
public function before_get_posts( $args ) {
[117] Fix | Delete
[118] Fix | Delete
// The `posts_where` hook is very general and has broad usage across the WP core and tons of plugins.
[119] Fix | Delete
// Therefore, in order to do not break something,
[120] Fix | Delete
// we should add this hook right before the call of `get_posts()` inside \WPForms_Form_Handler::get_multiple().
[121] Fix | Delete
add_filter( 'posts_where', [ $this, 'search_by_term_where' ], 10, 2 );
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* After get_posts() call routine.
[126] Fix | Delete
*
[127] Fix | Delete
* @since 1.7.2
[128] Fix | Delete
*
[129] Fix | Delete
* @param array $args Arguments of the get_posts().
[130] Fix | Delete
* @param array $forms Forms data. Result of getting multiple forms.
[131] Fix | Delete
*/
[132] Fix | Delete
public function after_get_posts( $args, $forms ) {
[133] Fix | Delete
[134] Fix | Delete
// The `posts_where` hook is very general and has broad usage across the WP core and tons of plugins.
[135] Fix | Delete
// Therefore, in order to do not break something,
[136] Fix | Delete
// we should remove this hook right after the call of `get_posts()` inside \WPForms_Form_Handler::get_multiple().
[137] Fix | Delete
remove_filter( 'posts_where', [ $this, 'search_by_term_where' ] );
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
/**
[141] Fix | Delete
* Modify the WHERE clause of the SQL query in order to search forms by given term.
[142] Fix | Delete
*
[143] Fix | Delete
* @since 1.7.2
[144] Fix | Delete
*
[145] Fix | Delete
* @param string $where WHERE clause.
[146] Fix | Delete
* @param \WP_Query $wp_query The WP_Query instance.
[147] Fix | Delete
*
[148] Fix | Delete
* @return string
[149] Fix | Delete
*/
[150] Fix | Delete
public function search_by_term_where( $where, $wp_query ) {
[151] Fix | Delete
[152] Fix | Delete
if ( is_numeric( $this->term ) ) {
[153] Fix | Delete
return $where;
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
global $wpdb;
[157] Fix | Delete
[158] Fix | Delete
// When user types only HTML tag (<section> for example), the sanitized term we will be empty.
[159] Fix | Delete
// In this case, it's better to return an empty result set than all the forms. It's not the same as the empty search term.
[160] Fix | Delete
if ( wpforms_is_empty_string( $this->term ) && ! wpforms_is_empty_string( $this->term_escaped ) ) {
[161] Fix | Delete
$where .= ' AND 1<>1';
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
if ( wpforms_is_empty_string( $this->term ) ) {
[165] Fix | Delete
return $where;
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
// Prepare the WHERE clause to search form title and description.
[169] Fix | Delete
$where .= $wpdb->prepare(
[170] Fix | Delete
" AND (
[171] Fix | Delete
$wpdb->posts.post_title LIKE %s OR
[172] Fix | Delete
$wpdb->posts.post_excerpt LIKE %s
[173] Fix | Delete
)",
[174] Fix | Delete
'%' . $wpdb->esc_like( esc_html( $this->term ) ) . '%',
[175] Fix | Delete
'%' . $wpdb->esc_like( $this->term ) . '%'
[176] Fix | Delete
);
[177] Fix | Delete
[178] Fix | Delete
return $where;
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
/**
[182] Fix | Delete
* Forms search markup.
[183] Fix | Delete
*
[184] Fix | Delete
* @since 1.7.2
[185] Fix | Delete
*
[186] Fix | Delete
* @param string $text The 'submit' button label.
[187] Fix | Delete
* @param string $input_id ID attribute value for the search input field.
[188] Fix | Delete
*/
[189] Fix | Delete
public function search_box( $text, $input_id ) {
[190] Fix | Delete
[191] Fix | Delete
$search_term = wpforms_is_empty_string( $this->term ) ? $this->term_escaped : $this->term;
[192] Fix | Delete
[193] Fix | Delete
// Display search reset block.
[194] Fix | Delete
$this->search_reset_block( $search_term );
[195] Fix | Delete
[196] Fix | Delete
// Display search box.
[197] Fix | Delete
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[198] Fix | Delete
echo wpforms_render(
[199] Fix | Delete
'admin/forms/search-box',
[200] Fix | Delete
[
[201] Fix | Delete
'term_input_id' => $input_id . '-term',
[202] Fix | Delete
'text' => $text,
[203] Fix | Delete
'search_term' => $search_term,
[204] Fix | Delete
],
[205] Fix | Delete
true
[206] Fix | Delete
);
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
/**
[210] Fix | Delete
* Forms search reset block.
[211] Fix | Delete
*
[212] Fix | Delete
* @since 1.7.2
[213] Fix | Delete
*
[214] Fix | Delete
* @param string $search_term Current search term.
[215] Fix | Delete
*/
[216] Fix | Delete
private function search_reset_block( $search_term ) {
[217] Fix | Delete
[218] Fix | Delete
if ( wpforms_is_empty_string( $search_term ) ) {
[219] Fix | Delete
return;
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
$views = wpforms()->obj( 'forms_views' );
[223] Fix | Delete
$count = $views->get_count();
[224] Fix | Delete
$view = $views->get_current_view();
[225] Fix | Delete
[226] Fix | Delete
$count['all'] = ! empty( $count['all'] ) ? $count['all'] : 0;
[227] Fix | Delete
[228] Fix | Delete
$message = sprintf(
[229] Fix | Delete
wp_kses( /* translators: %1$d - number of forms found, %2$s - search term. */
[230] Fix | Delete
_n(
[231] Fix | Delete
'Found <strong>%1$d form</strong> containing <em>"%2$s"</em>',
[232] Fix | Delete
'Found <strong>%1$d forms</strong> containing <em>"%2$s"</em>',
[233] Fix | Delete
(int) $count['all'],
[234] Fix | Delete
'wpforms-lite'
[235] Fix | Delete
),
[236] Fix | Delete
[
[237] Fix | Delete
'strong' => [],
[238] Fix | Delete
'em' => [],
[239] Fix | Delete
]
[240] Fix | Delete
),
[241] Fix | Delete
(int) $count['all'],
[242] Fix | Delete
esc_html( $search_term )
[243] Fix | Delete
);
[244] Fix | Delete
[245] Fix | Delete
/**
[246] Fix | Delete
* Filters the message in the search reset block.
[247] Fix | Delete
*
[248] Fix | Delete
* @since 1.7.3
[249] Fix | Delete
*
[250] Fix | Delete
* @param string $message Message text.
[251] Fix | Delete
* @param string $search_term Search term.
[252] Fix | Delete
* @param array $count Count forms in different views.
[253] Fix | Delete
* @param string $view Current view.
[254] Fix | Delete
*/
[255] Fix | Delete
$message = apply_filters( 'wpforms_admin_forms_search_search_reset_block_message', $message, $search_term, $count, $view );
[256] Fix | Delete
[257] Fix | Delete
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[258] Fix | Delete
echo wpforms_render(
[259] Fix | Delete
'admin/forms/search-reset',
[260] Fix | Delete
[
[261] Fix | Delete
'message' => $message,
[262] Fix | Delete
],
[263] Fix | Delete
true
[264] Fix | Delete
);
[265] Fix | Delete
}
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function