'', 'input_background_color' => '', 'input_border_color' => '', 'input_focus_border_color' => '', 'input_border_width' => '', 'input_border_radius' => '', 'input_font_family' => '', 'input_font_weight' => '', 'input_font_size' => '', 'input_letter_spacing' => '', 'input_text_transform' => '', 'input_padding_y' => '', 'input_padding_x' => '', 'placeholder_color' => '', 'dropdown_background_color' => '', 'dropdown_border_color' => '', 'dropdown_border_width' => '', 'dropdown_border_radius' => '', 'dropdown_font_family' => '', 'dropdown_font_weight' => '', 'dropdown_letter_spacing' => '', 'dropdown_text_transform' => '', 'result_title_color' => '', 'result_title_hover_color' => '', 'result_title_font_size' => '', 'result_title_font_weight' => '', 'result_title_letter_spacing' => '', 'result_title_text_transform' => '', 'result_excerpt_color' => '', 'result_excerpt_hover_color' => '', 'result_excerpt_font_size' => '', 'result_excerpt_font_weight' => '', 'result_type_color' => '', 'result_type_hover_color' => '', 'result_type_font_size' => '', 'result_type_font_weight' => '', 'result_hover_background_color' => '', 'image_size' => '', 'image_border_radius' => '', 'view_all_color' => '', 'view_all_hover_color' => '', 'view_all_hover_background_color' => '', 'view_all_font_size' => '', 'view_all_font_weight' => '', 'empty_text_color' => '', 'empty_font_size' => '', ); } function acewpcf_sanitize_content_search_styles( $styles ) { $defaults = acewpcf_get_default_content_search_styles(); $styles = is_array( $styles ) ? $styles : array(); $sanitized = array( 'input_text_color' => isset( $styles['input_text_color'] ) ? sanitize_hex_color( $styles['input_text_color'] ) : '', 'input_background_color' => isset( $styles['input_background_color'] ) ? sanitize_hex_color( $styles['input_background_color'] ) : '', 'input_border_color' => isset( $styles['input_border_color'] ) ? sanitize_hex_color( $styles['input_border_color'] ) : '', 'input_focus_border_color' => isset( $styles['input_focus_border_color'] ) ? sanitize_hex_color( $styles['input_focus_border_color'] ) : '', 'input_border_width' => isset( $styles['input_border_width'] ) ? acewpcf_sanitize_size_value( $styles['input_border_width'] ) : '', 'input_border_radius' => isset( $styles['input_border_radius'] ) ? acewpcf_sanitize_size_value( $styles['input_border_radius'] ) : '', 'input_font_family' => isset( $styles['input_font_family'] ) ? acewpcf_sanitize_font_family_choice( $styles['input_font_family'] ) : '', 'input_font_weight' => isset( $styles['input_font_weight'] ) ? acewpcf_sanitize_font_weight_value( $styles['input_font_weight'] ) : '', 'input_font_size' => isset( $styles['input_font_size'] ) ? acewpcf_sanitize_size_value( $styles['input_font_size'] ) : '', 'input_letter_spacing' => isset( $styles['input_letter_spacing'] ) ? acewpcf_sanitize_letter_spacing_value( $styles['input_letter_spacing'] ) : '', 'input_text_transform' => isset( $styles['input_text_transform'] ) ? acewpcf_sanitize_text_transform_value( $styles['input_text_transform'] ) : '', 'input_padding_y' => isset( $styles['input_padding_y'] ) ? acewpcf_sanitize_size_value( $styles['input_padding_y'] ) : '', 'input_padding_x' => isset( $styles['input_padding_x'] ) ? acewpcf_sanitize_size_value( $styles['input_padding_x'] ) : '', 'placeholder_color' => isset( $styles['placeholder_color'] ) ? sanitize_hex_color( $styles['placeholder_color'] ) : '', 'dropdown_background_color' => isset( $styles['dropdown_background_color'] ) ? sanitize_hex_color( $styles['dropdown_background_color'] ) : '', 'dropdown_border_color' => isset( $styles['dropdown_border_color'] ) ? sanitize_hex_color( $styles['dropdown_border_color'] ) : '', 'dropdown_border_width' => isset( $styles['dropdown_border_width'] ) ? acewpcf_sanitize_size_value( $styles['dropdown_border_width'] ) : '', 'dropdown_border_radius' => isset( $styles['dropdown_border_radius'] ) ? acewpcf_sanitize_size_value( $styles['dropdown_border_radius'] ) : '', 'dropdown_font_family' => isset( $styles['dropdown_font_family'] ) ? acewpcf_sanitize_font_family_choice( $styles['dropdown_font_family'] ) : '', 'dropdown_font_weight' => isset( $styles['dropdown_font_weight'] ) ? acewpcf_sanitize_font_weight_value( $styles['dropdown_font_weight'] ) : '', 'dropdown_letter_spacing' => isset( $styles['dropdown_letter_spacing'] ) ? acewpcf_sanitize_letter_spacing_value( $styles['dropdown_letter_spacing'] ) : '', 'dropdown_text_transform' => isset( $styles['dropdown_text_transform'] ) ? acewpcf_sanitize_text_transform_value( $styles['dropdown_text_transform'] ) : '', 'result_title_color' => isset( $styles['result_title_color'] ) ? sanitize_hex_color( $styles['result_title_color'] ) : '', 'result_title_hover_color' => isset( $styles['result_title_hover_color'] ) ? sanitize_hex_color( $styles['result_title_hover_color'] ) : '', 'result_title_font_size' => isset( $styles['result_title_font_size'] ) ? acewpcf_sanitize_size_value( $styles['result_title_font_size'] ) : '', 'result_title_font_weight' => isset( $styles['result_title_font_weight'] ) ? acewpcf_sanitize_font_weight_value( $styles['result_title_font_weight'] ) : '', 'result_title_letter_spacing' => isset( $styles['result_title_letter_spacing'] ) ? acewpcf_sanitize_letter_spacing_value( $styles['result_title_letter_spacing'] ) : '', 'result_title_text_transform' => isset( $styles['result_title_text_transform'] ) ? acewpcf_sanitize_text_transform_value( $styles['result_title_text_transform'] ) : '', 'result_excerpt_color' => isset( $styles['result_excerpt_color'] ) ? sanitize_hex_color( $styles['result_excerpt_color'] ) : '', 'result_excerpt_hover_color' => isset( $styles['result_excerpt_hover_color'] ) ? sanitize_hex_color( $styles['result_excerpt_hover_color'] ) : '', 'result_excerpt_font_size' => isset( $styles['result_excerpt_font_size'] ) ? acewpcf_sanitize_size_value( $styles['result_excerpt_font_size'] ) : '', 'result_excerpt_font_weight' => isset( $styles['result_excerpt_font_weight'] ) ? acewpcf_sanitize_font_weight_value( $styles['result_excerpt_font_weight'] ) : '', 'result_type_color' => isset( $styles['result_type_color'] ) ? sanitize_hex_color( $styles['result_type_color'] ) : '', 'result_type_hover_color' => isset( $styles['result_type_hover_color'] ) ? sanitize_hex_color( $styles['result_type_hover_color'] ) : '', 'result_type_font_size' => isset( $styles['result_type_font_size'] ) ? acewpcf_sanitize_size_value( $styles['result_type_font_size'] ) : '', 'result_type_font_weight' => isset( $styles['result_type_font_weight'] ) ? acewpcf_sanitize_font_weight_value( $styles['result_type_font_weight'] ) : '', 'result_hover_background_color' => isset( $styles['result_hover_background_color'] ) ? sanitize_hex_color( $styles['result_hover_background_color'] ) : '', 'image_size' => isset( $styles['image_size'] ) ? acewpcf_sanitize_size_value( $styles['image_size'] ) : '', 'image_border_radius' => isset( $styles['image_border_radius'] ) ? acewpcf_sanitize_size_value( $styles['image_border_radius'] ) : '', 'view_all_color' => isset( $styles['view_all_color'] ) ? sanitize_hex_color( $styles['view_all_color'] ) : '', 'view_all_hover_color' => isset( $styles['view_all_hover_color'] ) ? sanitize_hex_color( $styles['view_all_hover_color'] ) : '', 'view_all_hover_background_color' => isset( $styles['view_all_hover_background_color'] ) ? sanitize_hex_color( $styles['view_all_hover_background_color'] ) : '', 'view_all_font_size' => isset( $styles['view_all_font_size'] ) ? acewpcf_sanitize_size_value( $styles['view_all_font_size'] ) : '', 'view_all_font_weight' => isset( $styles['view_all_font_weight'] ) ? acewpcf_sanitize_font_weight_value( $styles['view_all_font_weight'] ) : '', 'empty_text_color' => isset( $styles['empty_text_color'] ) ? sanitize_hex_color( $styles['empty_text_color'] ) : '', 'empty_font_size' => isset( $styles['empty_font_size'] ) ? acewpcf_sanitize_size_value( $styles['empty_font_size'] ) : '', ); return wp_parse_args( $sanitized, $defaults ); } function acewpcf_get_default_content_search() { return array( 'id' => '', 'name' => '', 'content_type' => 'post', 'placeholder' => '', 'match_title' => 1, 'match_excerpt' => 0, 'max_results' => 8, 'min_chars' => 2, 'empty_message' => '', 'show_image' => 1, 'show_title' => 1, 'show_excerpt' => 1, 'show_type' => 0, 'show_view_all' => 1, 'view_all_label' => '', 'limit_taxonomy' => '', 'limit_term_ids' => array(), 'limit_include_children' => 1, 'styles' => acewpcf_get_default_content_search_styles(), ); } function acewpcf_get_content_search_limit_taxonomy_choices( $content_type = 'post' ) { return acewpcf_get_content_taxonomy_choices( $content_type ); } function acewpcf_sanitize_content_search_limit_taxonomy( $value, $content_type = 'post' ) { $value = sanitize_key( (string) $value ); $content_type = acewpcf_sanitize_content_type( $content_type ); if ( '' === $value ) { return ''; } $available = acewpcf_get_content_search_limit_taxonomy_choices( $content_type ); return isset( $available[ $value ] ) ? $value : ''; } function acewpcf_sanitize_content_search_limit_term_ids( $taxonomy, $term_ids ) { $taxonomy = sanitize_key( (string) $taxonomy ); if ( '' === $taxonomy || ! taxonomy_exists( $taxonomy ) ) { return array(); } return acewpcf_validate_filter_term_ids( $taxonomy, $term_ids ); } function acewpcf_get_content_search_limit_clause( $search ) { $search = is_array( $search ) ? $search : array(); $content_type = acewpcf_sanitize_content_type( isset( $search['content_type'] ) ? $search['content_type'] : 'post' ); $taxonomy = acewpcf_sanitize_content_search_limit_taxonomy( isset( $search['limit_taxonomy'] ) ? $search['limit_taxonomy'] : '', $content_type ); $term_ids = acewpcf_sanitize_content_search_limit_term_ids( $taxonomy, isset( $search['limit_term_ids'] ) ? $search['limit_term_ids'] : array() ); if ( '' === $taxonomy || empty( $term_ids ) ) { return array(); } return array( 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $term_ids, 'operator' => 'IN', 'include_children' => is_taxonomy_hierarchical( $taxonomy ) && ( ! isset( $search['limit_include_children'] ) || ! empty( $search['limit_include_children'] ) ), ); } function acewpcf_get_content_search_limit_summary( $search ) { $search = is_array( $search ) ? $search : array(); $content_type = acewpcf_sanitize_content_type( isset( $search['content_type'] ) ? $search['content_type'] : 'post' ); $choices = acewpcf_get_content_type_choices(); $type_label = isset( $choices[ $content_type ] ) ? $choices[ $content_type ] : $content_type; $taxonomy = acewpcf_sanitize_content_search_limit_taxonomy( isset( $search['limit_taxonomy'] ) ? $search['limit_taxonomy'] : '', $content_type ); $term_ids = acewpcf_sanitize_content_search_limit_term_ids( $taxonomy, isset( $search['limit_term_ids'] ) ? $search['limit_term_ids'] : array() ); /* translators: %s: content type label. */ $all_content_label = __( 'All %s', 'ace-content-filters' ); if ( '' === $taxonomy || empty( $term_ids ) ) { return sprintf( $all_content_label, $type_label ); } $terms = get_terms( array( 'taxonomy' => $taxonomy, 'include' => $term_ids, 'hide_empty' => false, 'orderby' => 'include', ) ); if ( is_wp_error( $terms ) || empty( $terms ) ) { return sprintf( $all_content_label, $type_label ); } $names = array(); foreach ( $terms as $term ) { if ( $term instanceof WP_Term ) { $names[] = $term->name; } } if ( empty( $names ) ) { return sprintf( $all_content_label, $type_label ); } if ( count( $names ) <= 2 ) { return $type_label . ': ' . implode( ', ', $names ); } /* translators: 1: taxonomy label, 2: number of selected terms. */ $term_summary_text = _n( '%1$s: %2$d term', '%1$s: %2$d terms', count( $names ), 'ace-content-filters' ); return sprintf( $term_summary_text, acewpcf_get_admin_taxonomy_label( $taxonomy ), count( $names ) ); } function acewpcf_sanitize_content_search_max_results( $value ) { $value = absint( $value ); if ( $value < 1 ) { return 8; } return min( 20, $value ); } function acewpcf_sanitize_content_search_min_chars( $value ) { $value = absint( $value ); if ( $value < 1 ) { return 2; } return min( 5, $value ); } function acewpcf_sanitize_content_search( $search_id, $search_data, $existing_ids = null ) { $defaults = acewpcf_get_default_content_search(); $search_data = is_array( $search_data ) ? $search_data : array(); $search_id = sanitize_key( $search_id ); $requested_id = isset( $search_data['id'] ) ? sanitize_key( (string) $search_data['id'] ) : ''; $name_source = isset( $search_data['name'] ) ? sanitize_text_field( (string) $search_data['name'] ) : 'search'; $stored_ids = is_array( $existing_ids ) ? $existing_ids : array_keys( (array) get_option( ACEWPCF_OPTION_CONTENT_SEARCHES, array() ) ); $search_id = acewpcf_resolve_editable_object_id( $search_id, $requested_id, $name_source, $stored_ids, 'search' ); $content_type = acewpcf_sanitize_content_type( isset( $search_data['content_type'] ) ? $search_data['content_type'] : 'post' ); $match_title = ! isset( $search_data['match_title'] ) || ! empty( $search_data['match_title'] ) ? 1 : 0; $match_excerpt = ! empty( $search_data['match_excerpt'] ) ? 1 : 0; if ( ! $match_title && ! $match_excerpt ) { $match_title = 1; } $limit_taxonomy = acewpcf_sanitize_content_search_limit_taxonomy( isset( $search_data['limit_taxonomy'] ) ? $search_data['limit_taxonomy'] : '', $content_type ); $limit_term_ids = acewpcf_sanitize_content_search_limit_term_ids( $limit_taxonomy, isset( $search_data['limit_term_ids'] ) ? $search_data['limit_term_ids'] : array() ); if ( '' === $limit_taxonomy || empty( $limit_term_ids ) ) { $limit_taxonomy = ''; $limit_term_ids = array(); } $sanitized = array( 'id' => $search_id, 'name' => isset( $search_data['name'] ) ? sanitize_text_field( (string) $search_data['name'] ) : '', 'content_type' => $content_type, 'placeholder' => isset( $search_data['placeholder'] ) ? acewpcf_sanitize_panel_control_label( $search_data['placeholder'] ) : '', 'match_title' => $match_title, 'match_excerpt' => $match_excerpt, 'max_results' => acewpcf_sanitize_content_search_max_results( isset( $search_data['max_results'] ) ? $search_data['max_results'] : $defaults['max_results'] ), 'min_chars' => acewpcf_sanitize_content_search_min_chars( isset( $search_data['min_chars'] ) ? $search_data['min_chars'] : $defaults['min_chars'] ), 'empty_message' => isset( $search_data['empty_message'] ) ? acewpcf_sanitize_empty_results_message( $search_data['empty_message'] ) : '', 'show_image' => ! isset( $search_data['show_image'] ) || ! empty( $search_data['show_image'] ) ? 1 : 0, 'show_title' => ! isset( $search_data['show_title'] ) || ! empty( $search_data['show_title'] ) ? 1 : 0, 'show_excerpt' => ! isset( $search_data['show_excerpt'] ) || ! empty( $search_data['show_excerpt'] ) ? 1 : 0, 'show_type' => ! empty( $search_data['show_type'] ) ? 1 : 0, 'show_view_all' => ! isset( $search_data['show_view_all'] ) || ! empty( $search_data['show_view_all'] ) ? 1 : 0, 'view_all_label' => isset( $search_data['view_all_label'] ) ? acewpcf_sanitize_panel_control_label( $search_data['view_all_label'] ) : '', 'limit_taxonomy' => $limit_taxonomy, 'limit_term_ids' => $limit_term_ids, 'limit_include_children' => ! isset( $search_data['limit_include_children'] ) || ! empty( $search_data['limit_include_children'] ) ? 1 : 0, 'styles' => acewpcf_sanitize_content_search_styles( isset( $search_data['styles'] ) ? $search_data['styles'] : array() ), ); if ( ! $sanitized['show_title'] && ! $sanitized['show_excerpt'] && ! $sanitized['show_type'] ) { $sanitized['show_title'] = 1; } if ( '' === $sanitized['name'] ) { $sanitized['name'] = __( 'Content Search', 'ace-content-filters' ); } return wp_parse_args( $sanitized, $defaults ); } function acewpcf_get_content_searches() { $stored_searches = get_option( ACEWPCF_OPTION_CONTENT_SEARCHES, array() ); $searches = array(); if ( ! is_array( $stored_searches ) ) { return $searches; } foreach ( $stored_searches as $search_id => $search_data ) { $search = acewpcf_sanitize_content_search( $search_id, $search_data ); if ( '' !== $search['id'] ) { $searches[ $search['id'] ] = $search; } } return $searches; } function acewpcf_get_content_search( $search_id = '' ) { $searches = acewpcf_get_content_searches(); if ( '' !== $search_id ) { $search_id = sanitize_key( $search_id ); if ( isset( $searches[ $search_id ] ) ) { return $searches[ $search_id ]; } return array(); } return ! empty( $searches ) ? reset( $searches ) : array(); } function acewpcf_get_content_search_choices() { $choices = array(); foreach ( acewpcf_get_content_searches() as $search ) { $choices[ $search['id'] ] = $search['name']; } return $choices; } function acewpcf_get_content_search_placeholder( $search ) { $placeholder = isset( $search['placeholder'] ) ? trim( (string) $search['placeholder'] ) : ''; return '' !== $placeholder ? $placeholder : __( 'Search', 'ace-content-filters' ); } function acewpcf_get_content_search_empty_message( $search ) { $message = isset( $search['empty_message'] ) ? trim( (string) $search['empty_message'] ) : ''; return '' !== $message ? $message : __( 'No results found', 'ace-content-filters' ); } function acewpcf_get_content_search_view_all_label( $search ) { $label = isset( $search['view_all_label'] ) ? trim( (string) $search['view_all_label'] ) : ''; return '' !== $label ? $label : __( 'View all results', 'ace-content-filters' ); } function acewpcf_get_content_search_match_summary( $search ) { $parts = array(); if ( ! empty( $search['match_title'] ) ) { $parts[] = __( 'Title', 'ace-content-filters' ); } if ( ! empty( $search['match_excerpt'] ) ) { $parts[] = __( 'Excerpt', 'ace-content-filters' ); } return ! empty( $parts ) ? implode( ' + ', $parts ) : __( 'Title', 'ace-content-filters' ); } function acewpcf_normalize_content_search_query( $value ) { $value = sanitize_text_field( (string) $value ); $value = preg_replace( '/\s+/', ' ', trim( $value ) ); return '' === $value ? '' : ( function_exists( 'mb_substr' ) ? mb_substr( $value, 0, 80, 'UTF-8' ) : substr( $value, 0, 80 ) ); } function acewpcf_content_search_query_length( $value ) { $value = (string) $value; if ( function_exists( 'mb_strlen' ) ) { return (int) mb_strlen( $value, 'UTF-8' ); } return strlen( $value ); } function acewpcf_get_content_search_view_all_url( $query, $search = array() ) { $query = acewpcf_normalize_content_search_query( $query ); $search = is_array( $search ) ? $search : array(); $content_type = acewpcf_sanitize_content_type( isset( $search['content_type'] ) ? $search['content_type'] : 'post' ); if ( '' === $query ) { return ''; } $args = array( 's' => $query, 'post_type' => $content_type, ); $limit_clause = acewpcf_get_content_search_limit_clause( $search ); if ( ! empty( $limit_clause['taxonomy'] ) && ! empty( $limit_clause['terms'] ) ) { $slugs = array(); $terms = get_terms( array( 'taxonomy' => $limit_clause['taxonomy'], 'include' => $limit_clause['terms'], 'hide_empty' => false, ) ); if ( ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { if ( $term instanceof WP_Term && '' !== $term->slug ) { $slugs[] = $term->slug; } } } if ( ! empty( $slugs ) ) { $args[ $limit_clause['taxonomy'] ] = implode( ',', $slugs ); } } return add_query_arg( $args, home_url( '/' ) ); } function acewpcf_content_search_query_is_active( $query ) { return ( $query instanceof WP_Query ) && $query->get( 'acewpcf_content_search' ); } function acewpcf_content_search_posts_where( $where, $query ) { if ( ! acewpcf_content_search_query_is_active( $query ) ) { return $where; } global $wpdb; $term = acewpcf_normalize_content_search_query( $query->get( 'acewpcf_content_search_term' ) ); $match_title = (bool) $query->get( 'acewpcf_content_search_match_title' ); $match_excerpt = (bool) $query->get( 'acewpcf_content_search_match_excerpt' ); if ( '' === $term ) { return $where . ' AND 1=0'; } if ( ! $match_title && ! $match_excerpt ) { $match_title = true; } $like = '%' . $wpdb->esc_like( $term ) . '%'; $parts = array(); if ( $match_title ) { $parts[] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like ); } if ( $match_excerpt ) { $parts[] = $wpdb->prepare( "{$wpdb->posts}.post_excerpt LIKE %s", $like ); } if ( empty( $parts ) ) { return $where . ' AND 1=0'; } return $where . ' AND (' . implode( ' OR ', $parts ) . ')'; } function acewpcf_content_search_posts_orderby( $orderby, $query ) { if ( ! acewpcf_content_search_query_is_active( $query ) ) { return $orderby; } global $wpdb; $term = acewpcf_normalize_content_search_query( $query->get( 'acewpcf_content_search_term' ) ); if ( '' === $term ) { return $orderby; } $prefix_like = $wpdb->esc_like( $term ) . '%'; return $wpdb->prepare( "CASE WHEN {$wpdb->posts}.post_title LIKE %s THEN 0 ELSE 1 END ASC, {$wpdb->posts}.post_title ASC", $prefix_like ); } function acewpcf_get_content_search_image_url( $post_id ) { $post_id = absint( $post_id ); if ( ! $post_id ) { return ''; } $url = get_the_post_thumbnail_url( $post_id, 'thumbnail' ); return $url ? $url : ''; } function acewpcf_decode_search_display_text( $text ) { $text = wp_strip_all_tags( (string) $text ); if ( '' === $text ) { return ''; } $charset = get_bloginfo( 'charset' ); if ( ! is_string( $charset ) || '' === $charset ) { $charset = 'UTF-8'; } $decoded = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5, $charset ); if ( $decoded !== $text && false !== strpos( $decoded, '&#' ) ) { $decoded = html_entity_decode( $decoded, ENT_QUOTES | ENT_HTML5, $charset ); } return $decoded; } function acewpcf_get_content_search_excerpt( $post ) { if ( ! ( $post instanceof WP_Post ) ) { return ''; } $excerpt = has_excerpt( $post ) ? $post->post_excerpt : wp_trim_words( wp_strip_all_tags( (string) $post->post_content ), 16 ); return acewpcf_decode_search_display_text( wp_trim_words( wp_strip_all_tags( (string) $excerpt ), 16 ) ); } function acewpcf_find_content_search_matches( $query, $search ) { $query = acewpcf_normalize_content_search_query( $query ); $search = is_array( $search ) ? $search : array(); $min = acewpcf_sanitize_content_search_min_chars( isset( $search['min_chars'] ) ? $search['min_chars'] : 2 ); if ( '' === $query || acewpcf_content_search_query_length( $query ) < $min ) { return array(); } $content_type = acewpcf_sanitize_content_type( isset( $search['content_type'] ) ? $search['content_type'] : 'post' ); $limit = acewpcf_sanitize_content_search_max_results( isset( $search['max_results'] ) ? $search['max_results'] : 8 ); $tax_query = array(); $limit_clause = acewpcf_get_content_search_limit_clause( $search ); if ( ! empty( $limit_clause ) ) { $tax_query[] = $limit_clause; } $query_args = array( 'post_type' => $content_type, 'post_status' => 'publish', 'posts_per_page' => $limit, 'ignore_sticky_posts' => true, 'has_password' => false, 'no_found_rows' => true, 'suppress_filters' => false, 'acewpcf_content_search' => 1, 'acewpcf_content_search_term' => $query, 'acewpcf_content_search_match_title' => ! empty( $search['match_title'] ) ? 1 : 0, 'acewpcf_content_search_match_excerpt' => ! empty( $search['match_excerpt'] ) ? 1 : 0, ); if ( ! empty( $tax_query ) ) { $query_args['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Optional search scope limits results to selected taxonomy terms. } add_filter( 'posts_where', 'acewpcf_content_search_posts_where', 10, 2 ); add_filter( 'posts_orderby', 'acewpcf_content_search_posts_orderby', 10, 2 ); $content_query = new WP_Query( $query_args ); remove_filter( 'posts_where', 'acewpcf_content_search_posts_where', 10 ); remove_filter( 'posts_orderby', 'acewpcf_content_search_posts_orderby', 10 ); $items = array(); $show_image = ! empty( $search['show_image'] ); $show_excerpt = ! empty( $search['show_excerpt'] ); $show_type = ! empty( $search['show_type'] ); $type_choices = acewpcf_get_content_type_choices(); $type_label = isset( $type_choices[ $content_type ] ) ? $type_choices[ $content_type ] : $content_type; foreach ( $content_query->posts as $post ) { if ( ! ( $post instanceof WP_Post ) ) { continue; } $items[] = array( 'id' => (int) $post->ID, 'title' => acewpcf_decode_search_display_text( get_the_title( $post ) ), 'url' => get_permalink( $post ), 'excerpt' => $show_excerpt ? acewpcf_get_content_search_excerpt( $post ) : '', 'type' => $show_type ? $type_label : '', 'image' => $show_image ? acewpcf_get_content_search_image_url( $post->ID ) : '', 'image_alt' => $show_image ? acewpcf_decode_search_display_text( get_the_title( $post ) ) : '', ); } return $items; } function acewpcf_get_content_search_response( $search_id, $query ) { $search_id = sanitize_key( (string) $search_id ); $query = acewpcf_normalize_content_search_query( $query ); $search = acewpcf_get_content_search( $search_id ); if ( empty( $search ) ) { return new WP_Error( 'acewpcf_content_search_missing', __( 'That content search no longer exists.', 'ace-content-filters' ), array( 'status' => 404 ) ); } $results = acewpcf_find_content_search_matches( $query, $search ); $view_all = null; $min_chars = acewpcf_sanitize_content_search_min_chars( $search['min_chars'] ); if ( ! empty( $search['show_view_all'] ) && '' !== $query && acewpcf_content_search_query_length( $query ) >= $min_chars ) { $view_all_url = acewpcf_get_content_search_view_all_url( $query, $search ); if ( '' !== $view_all_url ) { $view_all = array( 'url' => $view_all_url, 'label' => acewpcf_get_content_search_view_all_label( $search ), ); } } return array( 'results' => $results, 'view_all' => $view_all, 'empty_message' => acewpcf_get_content_search_empty_message( $search ), ); } function acewpcf_register_content_search_rest_route() { register_rest_route( 'acewpcf/v1', '/content-search', array( 'methods' => 'GET', 'callback' => 'acewpcf_handle_content_search_rest', 'permission_callback' => '__return_true', 'args' => array( 'id' => array( 'required' => true, 'sanitize_callback' => 'sanitize_key', ), 'q' => array( 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ), ), ) ); } add_action( 'rest_api_init', 'acewpcf_register_content_search_rest_route' ); function acewpcf_handle_content_search_rest( WP_REST_Request $request ) { $payload = acewpcf_get_content_search_response( $request->get_param( 'id' ), $request->get_param( 'q' ) ); if ( is_wp_error( $payload ) ) { return $payload; } return rest_ensure_response( $payload ); } function acewpcf_handle_content_search_ajax() { $search_id = isset( $_REQUEST['id'] ) ? sanitize_key( wp_unslash( (string) $_REQUEST['id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public read-only content lookup. $query = isset( $_REQUEST['q'] ) ? wp_unslash( (string) $_REQUEST['q'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized in acewpcf_get_content_search_response. $payload = acewpcf_get_content_search_response( $search_id, $query ); if ( is_wp_error( $payload ) ) { $data = $payload->get_error_data(); $status = is_array( $data ) && isset( $data['status'] ) ? absint( $data['status'] ) : 400; wp_send_json_error( array( 'message' => $payload->get_error_message(), ), $status ? $status : 400 ); } wp_send_json_success( $payload ); } add_action( 'wp_ajax_acewpcf_content_search', 'acewpcf_handle_content_search_ajax' ); add_action( 'wp_ajax_nopriv_acewpcf_content_search', 'acewpcf_handle_content_search_ajax' ); function acewpcf_enqueue_content_search_assets() { static $assets_enqueued = false; if ( ! wp_style_is( 'acewpcf-filter-style', 'registered' ) || ! wp_script_is( 'acewpcf-content-search-script', 'registered' ) ) { acewpcf_register_assets(); } wp_enqueue_style( 'acewpcf-filter-style' ); wp_enqueue_script( 'acewpcf-content-search-script' ); if ( $assets_enqueued ) { return; } $assets_enqueued = true; wp_localize_script( 'acewpcf-content-search-script', 'acewpcfContentSearch', array( 'restUrl' => esc_url_raw( rest_url( 'acewpcf/v1/content-search' ) ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), ) ); } function acewpcf_enqueue_content_search_style_block( $style_block ) { static $enqueued_blocks = array(); $style_block = trim( (string) $style_block ); if ( '' === $style_block ) { return; } acewpcf_enqueue_content_search_assets(); $style_hash = md5( $style_block ); if ( isset( $enqueued_blocks[ $style_hash ] ) ) { return; } $enqueued_blocks[ $style_hash ] = true; if ( wp_style_is( 'acewpcf-filter-style', 'done' ) ) { acewpcf_late_inline_style_blocks( $style_block ); return; } wp_add_inline_style( 'acewpcf-filter-style', $style_block ); } function acewpcf_content_search_append_style_var( &$root, $styles, $key, $css_var, $type = 'color' ) { $styles = is_array( $styles ) ? $styles : array(); if ( ! isset( $styles[ $key ] ) || ! acewpcf_style_value_is_set( $styles[ $key ] ) ) { return; } $value = $styles[ $key ]; if ( 'color' === $type ) { if ( empty( $value ) ) { return; } $root[] = $css_var . ':' . $value; return; } if ( 'font' === $type ) { $font = acewpcf_get_font_family_css_value( $value ); if ( '' === $font ) { return; } $root[] = $css_var . ':' . $font; return; } if ( 'px' === $type ) { $root[] = $css_var . ':' . $value . 'px'; return; } $root[] = $css_var . ':' . $value; } function acewpcf_enqueue_content_search_font_stylesheet( $search ) { $styles = isset( $search['styles'] ) && is_array( $search['styles'] ) ? $search['styles'] : array(); acewpcf_enqueue_google_font_stylesheet( $styles, array( 'input_font_family', 'dropdown_font_family' ) ); } function acewpcf_content_search_input_selectors( $scope ) { $scope = (string) $scope; return array( 'html body ' . $scope . '.cpcf-content-search .cpcf-content-search__field input.cpcf-content-search__input[type="search"]', 'html body ' . $scope . ' .cpcf-content-search__field input.cpcf-content-search__input[type="search"]', 'html body .elementor-widget-container ' . $scope . ' input.cpcf-content-search__input[type="search"]', $scope . ' .cpcf-content-search__field input.cpcf-content-search__input', $scope . ' input.cpcf-content-search__input[type="search"]', $scope . ' input.cpcf-content-search__input', ); } function acewpcf_content_search_map_selectors( $selectors, $suffix ) { $mapped = array(); foreach ( (array) $selectors as $selector ) { $mapped[] = $selector . $suffix; } return $mapped; } function acewpcf_content_search_border_color_declarations( $color ) { $color = (string) $color; return array( 'border-color:' . $color . ' !important', 'border-top-color:' . $color . ' !important', 'border-right-color:' . $color . ' !important', 'border-bottom-color:' . $color . ' !important', 'border-left-color:' . $color . ' !important', 'border-image:none !important', ); } function acewpcf_get_content_search_style_block( $search ) { $search = is_array( $search ) ? $search : array(); $styles = isset( $search['styles'] ) && is_array( $search['styles'] ) ? $search['styles'] : array(); $scope = '.cpcf-content-search--' . sanitize_html_class( isset( $search['id'] ) ? $search['id'] : '' ); $rules = array(); $root = array(); acewpcf_content_search_append_style_var( $root, $styles, 'input_text_color', '--cpcf-cs-input-text' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_background_color', '--cpcf-cs-input-bg' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_border_color', '--cpcf-cs-input-border' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_focus_border_color', '--cpcf-cs-input-focus-border' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_border_width', '--cpcf-cs-input-border-width', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_border_radius', '--cpcf-cs-input-radius', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_font_family', '--cpcf-cs-input-font', 'font' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_font_weight', '--cpcf-cs-input-weight', 'raw' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_font_size', '--cpcf-cs-input-size', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_letter_spacing', '--cpcf-cs-input-tracking', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_text_transform', '--cpcf-cs-input-transform', 'raw' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_padding_y', '--cpcf-cs-input-padding-y', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'input_padding_x', '--cpcf-cs-input-padding-x', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'placeholder_color', '--cpcf-cs-placeholder' ); acewpcf_content_search_append_style_var( $root, $styles, 'dropdown_background_color', '--cpcf-cs-dropdown-bg' ); acewpcf_content_search_append_style_var( $root, $styles, 'dropdown_border_color', '--cpcf-cs-dropdown-border' ); acewpcf_content_search_append_style_var( $root, $styles, 'dropdown_border_width', '--cpcf-cs-dropdown-border-width', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'dropdown_border_radius', '--cpcf-cs-dropdown-radius', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'dropdown_font_family', '--cpcf-cs-dropdown-font', 'font' ); acewpcf_content_search_append_style_var( $root, $styles, 'dropdown_font_weight', '--cpcf-cs-dropdown-weight', 'raw' ); acewpcf_content_search_append_style_var( $root, $styles, 'dropdown_letter_spacing', '--cpcf-cs-dropdown-tracking', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'dropdown_text_transform', '--cpcf-cs-dropdown-transform', 'raw' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_title_color', '--cpcf-cs-title' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_title_hover_color', '--cpcf-cs-title-hover' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_title_font_size', '--cpcf-cs-title-size', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_title_font_weight', '--cpcf-cs-title-weight', 'raw' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_title_letter_spacing', '--cpcf-cs-title-tracking', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_title_text_transform', '--cpcf-cs-title-transform', 'raw' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_excerpt_color', '--cpcf-cs-excerpt' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_excerpt_hover_color', '--cpcf-cs-excerpt-hover' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_excerpt_font_size', '--cpcf-cs-excerpt-size', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_excerpt_font_weight', '--cpcf-cs-excerpt-weight', 'raw' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_type_color', '--cpcf-cs-type' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_type_hover_color', '--cpcf-cs-type-hover' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_type_font_size', '--cpcf-cs-type-size', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_type_font_weight', '--cpcf-cs-type-weight', 'raw' ); acewpcf_content_search_append_style_var( $root, $styles, 'result_hover_background_color', '--cpcf-cs-hover-bg' ); acewpcf_content_search_append_style_var( $root, $styles, 'image_size', '--cpcf-cs-image-size', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'image_border_radius', '--cpcf-cs-image-radius', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'view_all_color', '--cpcf-cs-view-all' ); acewpcf_content_search_append_style_var( $root, $styles, 'view_all_hover_color', '--cpcf-cs-view-all-hover' ); acewpcf_content_search_append_style_var( $root, $styles, 'view_all_hover_background_color', '--cpcf-cs-view-all-hover-bg' ); acewpcf_content_search_append_style_var( $root, $styles, 'view_all_font_size', '--cpcf-cs-view-all-size', 'px' ); acewpcf_content_search_append_style_var( $root, $styles, 'view_all_font_weight', '--cpcf-cs-view-all-weight', 'raw' ); acewpcf_content_search_append_style_var( $root, $styles, 'empty_text_color', '--cpcf-cs-empty' ); acewpcf_content_search_append_style_var( $root, $styles, 'empty_font_size', '--cpcf-cs-empty-size', 'px' ); $root_rule = acewpcf_build_css_rule( $scope, $root ); if ( '' !== $root_rule ) { $rules[] = $root_rule; } $input_selectors = acewpcf_content_search_input_selectors( $scope ); $input_selector = implode( ',', $input_selectors ); $input_declarations = array( 'outline:none !important', 'outline-width:0 !important', 'outline-offset:0 !important', 'outline-color:transparent !important', 'box-shadow:none !important', '-webkit-box-shadow:none !important', ); if ( ! empty( $styles['input_text_color'] ) ) { $input_declarations[] = 'color:' . $styles['input_text_color'] . ' !important'; } if ( ! empty( $styles['input_background_color'] ) ) { $input_declarations[] = 'background:' . $styles['input_background_color'] . ' !important'; $input_declarations[] = 'background-color:' . $styles['input_background_color'] . ' !important'; } if ( ! empty( $styles['input_border_color'] ) ) { $input_declarations = array_merge( $input_declarations, acewpcf_content_search_border_color_declarations( $styles['input_border_color'] ) ); } if ( isset( $styles['input_border_width'] ) && acewpcf_style_value_is_set( $styles['input_border_width'] ) ) { $input_declarations[] = 'border-width:' . absint( $styles['input_border_width'] ) . 'px !important'; $input_declarations[] = 'border-style:solid !important'; } if ( isset( $styles['input_border_radius'] ) && acewpcf_style_value_is_set( $styles['input_border_radius'] ) ) { $input_declarations[] = 'border-radius:' . absint( $styles['input_border_radius'] ) . 'px !important'; } $input_font = isset( $styles['input_font_family'] ) ? acewpcf_get_font_family_css_value( $styles['input_font_family'] ) : ''; if ( '' !== $input_font ) { $input_declarations[] = 'font-family:' . $input_font . ' !important'; } if ( isset( $styles['input_font_weight'] ) && acewpcf_style_value_is_set( $styles['input_font_weight'] ) ) { $input_declarations[] = 'font-weight:' . $styles['input_font_weight'] . ' !important'; } if ( isset( $styles['input_font_size'] ) && acewpcf_style_value_is_set( $styles['input_font_size'] ) ) { $input_declarations[] = 'font-size:' . absint( $styles['input_font_size'] ) . 'px !important'; } if ( isset( $styles['input_letter_spacing'] ) && acewpcf_style_value_is_set( $styles['input_letter_spacing'] ) ) { $input_declarations[] = 'letter-spacing:' . $styles['input_letter_spacing'] . 'px !important'; } if ( isset( $styles['input_text_transform'] ) && acewpcf_style_value_is_set( $styles['input_text_transform'] ) ) { $input_declarations[] = 'text-transform:' . $styles['input_text_transform'] . ' !important'; } if ( isset( $styles['input_padding_y'] ) && acewpcf_style_value_is_set( $styles['input_padding_y'] ) ) { $input_declarations[] = 'padding-top:' . absint( $styles['input_padding_y'] ) . 'px !important'; $input_declarations[] = 'padding-bottom:' . absint( $styles['input_padding_y'] ) . 'px !important'; } if ( isset( $styles['input_padding_x'] ) && acewpcf_style_value_is_set( $styles['input_padding_x'] ) ) { $input_declarations[] = 'padding-left:' . absint( $styles['input_padding_x'] ) . 'px !important'; $input_declarations[] = 'padding-right:36px !important'; } $input_rule = acewpcf_build_css_rule( $input_selector, $input_declarations ); if ( '' !== $input_rule ) { $rules[] = $input_rule; } $placeholder_declarations = array(); if ( ! empty( $styles['placeholder_color'] ) ) { $placeholder_declarations[] = 'color:' . $styles['placeholder_color'] . ' !important'; $placeholder_declarations[] = 'opacity:1 !important'; } $placeholder_rule = acewpcf_build_css_rule( implode( ',', array_merge( acewpcf_content_search_map_selectors( $input_selectors, '::placeholder' ), acewpcf_content_search_map_selectors( $input_selectors, '::-webkit-input-placeholder' ), acewpcf_content_search_map_selectors( $input_selectors, '::-moz-placeholder' ) ) ), $placeholder_declarations ); if ( '' !== $placeholder_rule ) { $rules[] = $placeholder_rule; } if ( ! empty( $styles['input_border_color'] ) ) { $idle_rule = acewpcf_build_css_rule( implode( ',', acewpcf_content_search_map_selectors( $input_selectors, ':not(:focus):not(:focus-visible)' ) ), acewpcf_content_search_border_color_declarations( $styles['input_border_color'] ) ); if ( '' !== $idle_rule ) { $rules[] = $idle_rule; } } $focus_declarations = array( 'outline:none !important', 'outline-width:0 !important', 'outline-offset:0 !important', 'outline-color:transparent !important', 'box-shadow:none !important', '-webkit-box-shadow:none !important', ); if ( ! empty( $styles['input_focus_border_color'] ) ) { $focus_declarations = array_merge( $focus_declarations, acewpcf_content_search_border_color_declarations( $styles['input_focus_border_color'] ) ); } if ( ! empty( $styles['input_background_color'] ) ) { $focus_declarations[] = 'background:' . $styles['input_background_color'] . ' !important'; $focus_declarations[] = 'background-color:' . $styles['input_background_color'] . ' !important'; } if ( ! empty( $styles['input_text_color'] ) ) { $focus_declarations[] = 'color:' . $styles['input_text_color'] . ' !important'; } if ( isset( $styles['input_border_width'] ) && acewpcf_style_value_is_set( $styles['input_border_width'] ) ) { $focus_declarations[] = 'border-width:' . absint( $styles['input_border_width'] ) . 'px !important'; $focus_declarations[] = 'border-style:solid !important'; } $focus_rule = acewpcf_build_css_rule( implode( ',', array_merge( acewpcf_content_search_map_selectors( $input_selectors, ':focus' ), acewpcf_content_search_map_selectors( $input_selectors, ':focus-visible' ) ) ), $focus_declarations ); if ( '' !== $focus_rule && ! empty( $styles['input_focus_border_color'] ) ) { $rules[] = $focus_rule; } return implode( '', $rules ); } function acewpcf_render_content_search_shortcode( $atts = array() ) { $atts = shortcode_atts( array( 'id' => '', ), $atts, 'acewpcf_content_search' ); $search_id = sanitize_key( (string) $atts['id'] ); $search = acewpcf_get_content_search( $search_id ); if ( empty( $search ) ) { return ''; } static $instance = 0; ++$instance; $dom_id = 'cpcf-content-search-' . sanitize_html_class( $search['id'] ) . '-' . $instance; $input_id = $dom_id . '-input'; $listbox_id = $dom_id . '-listbox'; $placeholder = acewpcf_get_content_search_placeholder( $search ); $style_block = acewpcf_get_content_search_style_block( $search ); acewpcf_enqueue_content_search_font_stylesheet( $search ); acewpcf_enqueue_content_search_assets(); acewpcf_enqueue_content_search_style_block( $style_block ); ob_start(); ?>