← All changes
|
includes/classes/Feature/Autosuggest/Autosuggest.php
+244
-219
4.5.0
→
5.3.5
View file →
| @@ -8,14 +8,14 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | namespace ElasticPress\Feature\Autosuggest; |
| 11 | 11 | |
| 12 | -use ElasticPress\Feature as Feature; | |
| 13 | -use ElasticPress\Features as Features; | |
| 14 | -use ElasticPress\Utils as Utils; | |
| 15 | -use ElasticPress\FeatureRequirementsStatus as FeatureRequirementsStatus; | |
| 16 | -use ElasticPress\Indexables as Indexables; | |
| 17 | 12 | use ElasticPress\Elasticsearch; |
| 13 | +use ElasticPress\Feature; | |
| 14 | +use ElasticPress\FeatureRequirementsStatus; | |
| 15 | +use ElasticPress\Features; | |
| 16 | +use ElasticPress\Indexables; | |
| 17 | +use ElasticPress\Utils; | |
| 18 | 18 | |
| 19 | 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | 20 | exit; // Exit if accessed directly. |
| 21 | 21 | } |
| @@ -39,16 +39,10 @@ | ||
| 39 | 39 | */ |
| 40 | 40 | public function __construct() { |
| 41 | 41 | $this->slug = 'autosuggest'; |
| 42 | 42 | |
| 43 | - $this->title = $this->get_title(); | |
| 43 | + $this->group = 'live-search'; | |
| 44 | 44 | |
| 45 | - $this->short_title = esc_html__( 'Autosuggest', 'elasticpress' ); | |
| 46 | - | |
| 47 | - $this->summary = __( 'Suggest relevant content as text is entered into the search field.', 'elasticpress' ); | |
| 48 | - | |
| 49 | - $this->docs_url = __( 'https://elasticpress.zendesk.com/hc/en-us/articles/360050447492-Configuring-ElasticPress-via-the-Plugin-Dashboard#autosuggest', 'elasticpress' ); | |
| 50 | - | |
| 51 | 45 | $this->requires_install_reindex = true; |
| 52 | 46 | |
| 53 | 47 | $this->default_settings = [ |
| 54 | 48 | 'endpoint_url' => '', |
| @@ -57,20 +51,27 @@ | ||
| 57 | 51 | ]; |
| 58 | 52 | |
| 59 | 53 | $this->available_during_installation = true; |
| 60 | 54 | |
| 55 | + $this->is_powered_by_epio = Utils\is_epio(); | |
| 56 | + | |
| 61 | 57 | parent::__construct(); |
| 62 | 58 | } |
| 63 | 59 | |
| 64 | 60 | /** |
| 65 | - * Output feature box long | |
| 61 | + * Sets i18n strings. | |
| 66 | 62 | * |
| 67 | - * @since 2.4 | |
| 63 | + * @return void | |
| 64 | + * @since 5.2.0 | |
| 68 | 65 | */ |
| 69 | - public function output_feature_box_long() { | |
| 70 | - ?> | |
| 71 | - <p><?php esc_html_e( 'Input fields of type "search" or with the CSS class "search-field" or "ep-autosuggest" will be enhanced with autosuggest functionality. As text is entered into the search field, suggested content will appear below it, based on top search results for the text. Suggestions link directly to the content.', 'elasticpress' ); ?></p> | |
| 72 | - <?php | |
| 66 | + public function set_i18n_strings(): void { | |
| 67 | + $this->title = esc_html__( 'Autosuggest', 'elasticpress' ); | |
| 68 | + | |
| 69 | + $this->short_title = esc_html__( 'Autosuggest', 'elasticpress' ); | |
| 70 | + | |
| 71 | + $this->summary = '<p>' . __( 'Input fields of type "search" or with the CSS class "search-field" or "ep-autosuggest" will be enhanced with autosuggest functionality. As text is entered into the search field, suggested content will appear below it, based on top search results for the text. Suggestions link directly to the content.', 'elasticpress' ) . '</p>'; | |
| 72 | + | |
| 73 | + $this->docs_url = __( 'https://www.elasticpress.io/resources/articles/configuring-elasticpress-via-the-plugin-dashboard/#autosuggest', 'elasticpress' ); | |
| 73 | 74 | } |
| 74 | 75 | |
| 75 | 76 | /** |
| 76 | 77 | * Setup feature functionality |
| @@ -84,76 +85,12 @@ | ||
| 84 | 85 | add_filter( 'ep_post_fuzziness_arg', [ $this, 'set_fuzziness' ], 10, 3 ); |
| 85 | 86 | add_filter( 'ep_weighted_query_for_post_type', [ $this, 'adjust_fuzzy_fields' ], 10, 3 ); |
| 86 | 87 | add_filter( 'ep_saved_weighting_configuration', [ $this, 'epio_send_autosuggest_public_request' ] ); |
| 87 | 88 | add_filter( 'wp', [ $this, 'epio_send_autosuggest_allowed' ] ); |
| 88 | - add_filter( 'ep_pre_dashboard_index', [ $this, 'epio_send_autosuggest_public_request' ] ); | |
| 89 | - add_filter( 'ep_wp_cli_pre_index', [ $this, 'epio_send_autosuggest_public_request' ] ); | |
| 90 | - | |
| 91 | - add_action( 'ep_cli_after_set_search_algorithm_version', [ $this, 'delete_cached_query' ] ); | |
| 92 | - add_action( 'ep_wp_cli_after_index', [ $this, 'delete_cached_query' ] ); | |
| 93 | - add_action( 'ep_after_dashboard_index', [ $this, 'delete_cached_query' ] ); | |
| 94 | - add_action( 'ep_after_update_feature', [ $this, 'delete_cached_query' ] ); | |
| 95 | - add_action( 'ep_cli_after_clear_index', [ $this, 'delete_cached_query' ] ); | |
| 89 | + add_filter( 'ep_pre_sync_index', [ $this, 'epio_send_autosuggest_public_request' ] ); | |
| 96 | 90 | } |
| 97 | 91 | |
| 98 | 92 | /** |
| 99 | - * Display decaying settings on dashboard. | |
| 100 | - * | |
| 101 | - * @since 2.4 | |
| 102 | - */ | |
| 103 | - public function output_feature_box_settings() { | |
| 104 | - $settings = $this->get_settings(); | |
| 105 | - | |
| 106 | - if ( ! $settings ) { | |
| 107 | - $settings = []; | |
| 108 | - } | |
| 109 | - | |
| 110 | - $settings = wp_parse_args( $settings, $this->default_settings ); | |
| 111 | - | |
| 112 | - ?> | |
| 113 | - <div class="field"> | |
| 114 | - <div class="field-name status"><label for="feature_autosuggest_selector"><?php esc_html_e( 'Autosuggest Selector', 'elasticpress' ); ?></label></div> | |
| 115 | - <div class="input-wrap"> | |
| 116 | - <input value="<?php echo empty( $settings['autosuggest_selector'] ) ? '.ep-autosuggest' : esc_attr( $settings['autosuggest_selector'] ); ?>" type="text" name="settings[autosuggest_selector]" id="feature_autosuggest_selector"> | |
| 117 | - <p class="field-description"><?php esc_html_e( 'Input additional selectors where you would like to include autosuggest separated by a comma. Example: .custom-selector, #custom-id, input[type="text"]', 'elasticpress' ); ?></p> | |
| 118 | - </div> | |
| 119 | - </div> | |
| 120 | - | |
| 121 | - <div class="field"> | |
| 122 | - <div class="field-name status"><?php esc_html_e( 'Google Analytics Events', 'elasticpress' ); ?></div> | |
| 123 | - <div class="input-wrap"> | |
| 124 | - <label><input name="settings[trigger_ga_event]" <?php checked( (bool) $settings['trigger_ga_event'] ); ?> type="radio" value="1"><?php esc_html_e( 'Enabled', 'elasticpress' ); ?></label><br> | |
| 125 | - <label><input name="settings[trigger_ga_event]" <?php checked( ! (bool) $settings['trigger_ga_event'] ); ?> type="radio" value="0"><?php esc_html_e( 'Disabled', 'elasticpress' ); ?></label> | |
| 126 | - <p class="field-description"><?php esc_html_e( 'When enabled, a gtag tracking event is fired when an autosuggest result is clicked.', 'elasticpress' ); ?></p> | |
| 127 | - </div> | |
| 128 | - </div> | |
| 129 | - <?php | |
| 130 | - | |
| 131 | - if ( Utils\is_epio() ) { | |
| 132 | - $this->epio_allowed_parameters(); | |
| 133 | - return; | |
| 134 | - } | |
| 135 | - | |
| 136 | - $endpoint_url = ( defined( 'EP_AUTOSUGGEST_ENDPOINT' ) && EP_AUTOSUGGEST_ENDPOINT ) ? EP_AUTOSUGGEST_ENDPOINT : $settings['endpoint_url']; | |
| 137 | - ?> | |
| 138 | - | |
| 139 | - <div class="field"> | |
| 140 | - <div class="field-name status"><label for="feature_autosuggest_endpoint_url"><?php esc_html_e( 'Endpoint URL', 'elasticpress' ); ?></label></div> | |
| 141 | - <div class="input-wrap"> | |
| 142 | - <input <?php disabled( defined( 'EP_AUTOSUGGEST_ENDPOINT' ) && EP_AUTOSUGGEST_ENDPOINT ); ?> value="<?php echo esc_url( $endpoint_url ); ?>" type="text" name="settings[endpoint_url]" id="feature_autosuggest_endpoint_url"> | |
| 143 | - | |
| 144 | - <?php if ( defined( 'EP_AUTOSUGGEST_ENDPOINT' ) && EP_AUTOSUGGEST_ENDPOINT ) : ?> | |
| 145 | - <p class="field-description"><?php esc_html_e( 'Your autosuggest endpoint is set in wp-config.php', 'elasticpress' ); ?></p> | |
| 146 | - <?php endif; ?> | |
| 147 | - | |
| 148 | - <p class="field-description"><?php esc_html_e( 'This address will be exposed to the public.', 'elasticpress' ); ?></p> | |
| 149 | - </div> | |
| 150 | - </div> | |
| 151 | - | |
| 152 | - <?php | |
| 153 | - } | |
| 154 | - | |
| 155 | - /** | |
| 156 | 93 | * Add mapping for suggest fields |
| 157 | 94 | * |
| 158 | 95 | * @param array $mapping ES mapping. |
| 159 | 96 | * @since 2.4 |
| @@ -165,9 +102,9 @@ | ||
| 165 | 102 | $mapping = $post_indexable->add_ngram_analyzer( $mapping ); |
| 166 | 103 | $mapping = $post_indexable->add_term_suggest_field( $mapping ); |
| 167 | 104 | |
| 168 | 105 | // Note the assignment by reference below. |
| 169 | - if ( version_compare( Elasticsearch::factory()->get_elasticsearch_version(), '7.0', '<' ) ) { | |
| 106 | + if ( version_compare( (string) Elasticsearch::factory()->get_elasticsearch_version(), '7.0', '<' ) ) { | |
| 170 | 107 | $mapping_properties = &$mapping['mappings']['post']['properties']; |
| 171 | 108 | } else { |
| 172 | 109 | $mapping_properties = &$mapping['mappings']['properties']; |
| 173 | 110 | } |
| @@ -191,9 +128,9 @@ | ||
| 191 | 128 | * @param array $args Array of ES args |
| 192 | 129 | * @return array |
| 193 | 130 | */ |
| 194 | 131 | public function set_fuzziness( $fuzziness, $search_fields, $args ) { |
| 195 | - if ( Utils\is_integrated_request( $this->slug, [ 'public' ] ) && ! empty( $args['s'] ) ) { | |
| 132 | + if ( Utils\is_integrated_request( $this->slug, $this->get_contexts() ) && ! empty( $args['s'] ) ) { | |
| 196 | 133 | return 'auto'; |
| 197 | 134 | } |
| 198 | 135 | return $fuzziness; |
| 199 | 136 | } |
| @@ -206,9 +143,9 @@ | ||
| 206 | 143 | * @param array $args WP_Query args |
| 207 | 144 | * @return array $query adjusted ES Query arguments |
| 208 | 145 | */ |
| 209 | 146 | public function adjust_fuzzy_fields( $query, $post_type, $args ) { |
| 210 | - if ( ! Utils\is_integrated_request( $this->slug, [ 'public' ] ) || empty( $args['s'] ) ) { | |
| 147 | + if ( ! Utils\is_integrated_request( $this->slug, $this->get_contexts() ) || empty( $args['s'] ) ) { | |
| 211 | 148 | return $query; |
| 212 | 149 | } |
| 213 | 150 | |
| 214 | 151 | if ( ! isset( $query['bool'] ) || ! isset( $query['bool']['must'] ) ) { |
| @@ -355,30 +292,21 @@ | ||
| 355 | 292 | if ( Utils\is_indexing() ) { |
| 356 | 293 | return; |
| 357 | 294 | } |
| 358 | 295 | |
| 359 | - $host = Utils\get_host(); | |
| 360 | - $endpoint_url = false; | |
| 361 | - $settings = $this->get_settings(); | |
| 296 | + $host = Utils\get_host(); | |
| 297 | + $settings = $this->get_settings(); | |
| 362 | 298 | |
| 363 | 299 | if ( defined( 'EP_AUTOSUGGEST_ENDPOINT' ) && EP_AUTOSUGGEST_ENDPOINT ) { |
| 364 | 300 | $endpoint_url = EP_AUTOSUGGEST_ENDPOINT; |
| 301 | + } elseif ( Utils\is_epio() ) { | |
| 302 | + $endpoint_url = trailingslashit( $host ) . Indexables::factory()->get( 'post' )->get_index_name() . '/autosuggest'; | |
| 365 | 303 | } else { |
| 366 | - if ( Utils\is_epio() ) { | |
| 367 | - $endpoint_url = trailingslashit( $host ) . Indexables::factory()->get( 'post' )->get_index_name() . '/autosuggest'; | |
| 368 | - } else { | |
| 369 | - if ( ! $settings ) { | |
| 370 | - $settings = []; | |
| 371 | - } | |
| 304 | + $endpoint_url = $settings['endpoint_url']; | |
| 305 | + } | |
| 372 | 306 | |
| 373 | - $settings = wp_parse_args( $settings, $this->default_settings ); | |
| 374 | - | |
| 375 | - if ( empty( $settings['endpoint_url'] ) ) { | |
| 376 | - return; | |
| 377 | - } | |
| 378 | - | |
| 379 | - $endpoint_url = $settings['endpoint_url']; | |
| 380 | - } | |
| 307 | + if ( empty( $endpoint_url ) ) { | |
| 308 | + return; | |
| 381 | 309 | } |
| 382 | 310 | |
| 383 | 311 | wp_enqueue_script( |
| 384 | 312 | 'elasticpress-autosuggest', |
| @@ -402,16 +330,8 @@ | ||
| 402 | 330 | |
| 403 | 331 | /** Search Feature @var Feature\Search\Search $search */ |
| 404 | 332 | $search = $features->get_registered_feature( 'search' ); |
| 405 | 333 | |
| 406 | - $post_types = $search->get_searchable_post_types(); | |
| 407 | - $post_status = get_post_stati( | |
| 408 | - [ | |
| 409 | - 'public' => true, | |
| 410 | - 'exclude_from_search' => false, | |
| 411 | - ] | |
| 412 | - ); | |
| 413 | - | |
| 414 | 334 | $query = $this->generate_search_query(); |
| 415 | 335 | |
| 416 | 336 | $epas_options = [ |
| 417 | 337 | 'query' => $query['body'], |
| @@ -530,47 +450,47 @@ | ||
| 530 | 450 | * @return {array} New post statuses |
| 531 | 451 | */ |
| 532 | 452 | $post_status = apply_filters( 'ep_term_suggest_post_status', array_values( $post_status ) ); |
| 533 | 453 | |
| 534 | - add_filter( 'ep_intercept_remote_request', '__return_true' ); | |
| 535 | - add_filter( 'ep_weighting_configuration', [ $features->get_registered_feature( $this->slug ), 'apply_autosuggest_weighting' ], 10, 1 ); | |
| 454 | + add_filter( 'ep_weighting_configuration', [ $features->get_registered_feature( $this->slug ), 'apply_autosuggest_weighting' ] ); | |
| 536 | 455 | |
| 537 | - add_filter( 'ep_do_intercept_request', [ $features->get_registered_feature( $this->slug ), 'intercept_search_request' ], 10, 4 ); | |
| 456 | + add_filter( 'ep_do_intercept_request', [ $features->get_registered_feature( $this->slug ), 'intercept_search_request' ], 10, 2 ); | |
| 538 | 457 | |
| 539 | 458 | add_filter( 'posts_pre_query', [ $features->get_registered_feature( $this->slug ), 'return_empty_posts' ], 100, 1 ); // after ES Query to ensure we are not falling back to DB in any case |
| 540 | 459 | |
| 541 | - new \WP_Query( | |
| 542 | - /** | |
| 543 | - * Filter WP Query args of the autosuggest query template. | |
| 544 | - * | |
| 545 | - * If you want to display 20 posts in autosuggest: | |
| 546 | - * | |
| 547 | - * ``` | |
| 548 | - * add_filter( | |
| 549 | - * 'ep_autosuggest_query_args', | |
| 550 | - * function( $args ) { | |
| 551 | - * $args['posts_per_page'] = 20; | |
| 552 | - * return $args; | |
| 553 | - * } | |
| 554 | - * ); | |
| 555 | - * ``` | |
| 556 | - * | |
| 557 | - * @since 4.4.0 | |
| 558 | - * @hook ep_autosuggest_query_args | |
| 559 | - * @param {array} $args Query args | |
| 560 | - * @return {array} New query args | |
| 561 | - */ | |
| 562 | - apply_filters( | |
| 563 | - 'ep_autosuggest_query_args', | |
| 564 | - [ | |
| 565 | - 'post_type' => $post_type, | |
| 566 | - 'post_status' => $post_status, | |
| 567 | - 's' => $placeholder, | |
| 568 | - 'ep_integrate' => true, | |
| 569 | - ] | |
| 570 | - ) | |
| 460 | + /** | |
| 461 | + * Filter WP Query args of the autosuggest query template. | |
| 462 | + * | |
| 463 | + * If you want to display 20 posts in autosuggest: | |
| 464 | + * | |
| 465 | + * ``` | |
| 466 | + * add_filter( | |
| 467 | + * 'ep_autosuggest_query_args', | |
| 468 | + * function( $args ) { | |
| 469 | + * $args['posts_per_page'] = 20; | |
| 470 | + * return $args; | |
| 471 | + * } | |
| 472 | + * ); | |
| 473 | + * ``` | |
| 474 | + * | |
| 475 | + * @since 4.4.0 | |
| 476 | + * @hook ep_autosuggest_query_args | |
| 477 | + * @param {array} $args Query args | |
| 478 | + * @return {array} New query args | |
| 479 | + */ | |
| 480 | + $args = apply_filters( | |
| 481 | + 'ep_autosuggest_query_args', | |
| 482 | + [ | |
| 483 | + 'post_type' => $post_type, | |
| 484 | + 'post_status' => $post_status, | |
| 485 | + 's' => $placeholder, | |
| 486 | + 'ep_integrate' => true, | |
| 487 | + 'ep_intercept_request' => true, | |
| 488 | + ] | |
| 571 | 489 | ); |
| 572 | 490 | |
| 491 | + new \WP_Query( $args ); | |
| 492 | + | |
| 573 | 493 | remove_filter( 'posts_pre_query', [ $features->get_registered_feature( $this->slug ), 'return_empty_posts' ], 100 ); |
| 574 | 494 | |
| 575 | 495 | remove_filter( 'ep_do_intercept_request', [ $features->get_registered_feature( $this->slug ), 'intercept_search_request' ] ); |
| 576 | 496 | |
| @@ -575,13 +495,12 @@ | ||
| 575 | 495 | remove_filter( 'ep_do_intercept_request', [ $features->get_registered_feature( $this->slug ), 'intercept_search_request' ] ); |
| 576 | 496 | |
| 577 | 497 | remove_filter( 'ep_weighting_configuration', [ $features->get_registered_feature( $this->slug ), 'apply_autosuggest_weighting' ] ); |
| 578 | 498 | |
| 579 | - remove_filter( 'ep_intercept_remote_request', '__return_true' ); | |
| 580 | - | |
| 581 | 499 | return [ |
| 582 | 500 | 'body' => $this->autosuggest_query, |
| 583 | 501 | 'placeholder' => $placeholder, |
| 502 | + 'query_vars' => $args, | |
| 584 | 503 | ]; |
| 585 | 504 | } |
| 586 | 505 | |
| 587 | 506 | /** |
| @@ -612,68 +531,34 @@ | ||
| 612 | 531 | return $config; |
| 613 | 532 | } |
| 614 | 533 | |
| 615 | 534 | /** |
| 616 | - * Store intercepted request value and return (cached) request result | |
| 535 | + * Store intercepted request value and return a fake successful request result | |
| 617 | 536 | * |
| 618 | - * @param object $response Response | |
| 619 | - * @param array $query Query | |
| 620 | - * @param array $args WP_Query Argument array | |
| 621 | - * @param int $failures Count of failures in request loop | |
| 622 | - * @return object $response Response | |
| 537 | + * @param array $response Response | |
| 538 | + * @param array $query ES Query | |
| 539 | + * @return array $response Response | |
| 623 | 540 | */ |
| 624 | - public function intercept_search_request( $response, $query = [], $args = [], $failures = 0 ) { | |
| 541 | + public function intercept_search_request( $response, $query = [] ) { | |
| 625 | 542 | $this->autosuggest_query = $query['args']['body']; |
| 626 | 543 | |
| 627 | - // Let's make sure we also fire off the dummy request if settings have changed. | |
| 628 | - // But only fire this if we have object caching as otherwise this comes with a performance penalty. | |
| 629 | - // If we do not have object caching we cache only one value for 5 minutes in a transient. | |
| 630 | - if ( wp_using_ext_object_cache() ) { | |
| 631 | - $cache_key = md5( wp_json_encode( $query['url'] ) . wp_json_encode( $args ) ); | |
| 632 | - $request = wp_cache_get( $cache_key, 'ep_autosuggest' ); | |
| 633 | - if ( false === $request ) { | |
| 634 | - $request = wp_remote_request( $query['url'], $args ); | |
| 635 | - if ( isset( $request->http_response ) && isset( $request->http_response->body ) ) { | |
| 636 | - $request->http_response->body = ''; | |
| 637 | - } | |
| 638 | - wp_cache_set( $cache_key, $request, 'ep_autosuggest' ); | |
| 639 | - } | |
| 640 | - } else { | |
| 641 | - $cache_key = 'ep_autosuggest_query_request_cache'; | |
| 642 | - $request = get_transient( $cache_key ); | |
| 643 | - if ( false === $request ) { | |
| 644 | - $request = wp_remote_request( $query['url'], $args ); | |
| 645 | - if ( isset( $request->http_response ) && isset( $request->http_response->body ) ) { | |
| 646 | - $request->http_response->body = ''; | |
| 647 | - } | |
| 648 | - set_transient( $cache_key, $request, 5 * MINUTE_IN_SECONDS ); | |
| 649 | - } | |
| 650 | - } | |
| 544 | + $message = wp_json_encode( | |
| 545 | + [ | |
| 546 | + esc_html__( 'This is a fake request to build the ElasticPress Autosuggest query. It is not really sent.', 'elasticpress' ), | |
| 547 | + ] | |
| 548 | + ); | |
| 651 | 549 | |
| 652 | - return $request; | |
| 550 | + return [ | |
| 551 | + 'is_ep_fake_request' => true, | |
| 552 | + 'body' => $message, | |
| 553 | + 'response' => [ | |
| 554 | + 'code' => 200, | |
| 555 | + 'message' => $message, | |
| 556 | + ], | |
| 557 | + ]; | |
| 653 | 558 | } |
| 654 | 559 | |
| 655 | 560 | /** |
| 656 | - * Delete the cached query for autosuggest. | |
| 657 | - * | |
| 658 | - * @since 3.5.5 | |
| 659 | - */ | |
| 660 | - public function delete_cached_query() { | |
| 661 | - global $wp_object_cache; | |
| 662 | - if ( wp_using_ext_object_cache() ) { | |
| 663 | - if ( function_exists( 'wp_cache_supports_group_flush' ) && wp_cache_supports_group_flush() ) { | |
| 664 | - wp_cache_flush_group( 'ep_autosuggest' ); | |
| 665 | - } else { | |
| 666 | - // Try to delete the entire group. | |
| 667 | - // This may fail because the `$cache` property is not standardized. | |
| 668 | - unset( $wp_object_cache->cache['ep_autosuggest'] ); | |
| 669 | - } | |
| 670 | - } else { | |
| 671 | - delete_transient( 'ep_autosuggest_query_request_cache' ); | |
| 672 | - } | |
| 673 | - } | |
| 674 | - | |
| 675 | - /** | |
| 676 | 561 | * Tell user whether requirements for feature are met or not. |
| 677 | 562 | * |
| 678 | 563 | * @return array $status Status array |
| 679 | 564 | * @since 2.4 |
| @@ -678,9 +563,9 @@ | ||
| 678 | 563 | * @return array $status Status array |
| 679 | 564 | * @since 2.4 |
| 680 | 565 | */ |
| 681 | 566 | public function requirements_status() { |
| 682 | - $status = new FeatureRequirementsStatus( 0 ); | |
| 567 | + $status = new FeatureRequirementsStatus( 0, null, $this ); | |
| 683 | 568 | |
| 684 | 569 | $status->message = []; |
| 685 | 570 | |
| 686 | 571 | $status->message[] = esc_html__( 'This feature modifies the site’s default user experience by presenting a list of suggestions below detected search fields as text is entered into the field.', 'elasticpress' ); |
| @@ -705,21 +590,17 @@ | ||
| 705 | 590 | if ( ! Utils\is_epio() ) { |
| 706 | 591 | return; |
| 707 | 592 | } |
| 708 | 593 | |
| 709 | - $url = add_query_arg( | |
| 710 | - [ | |
| 711 | - 's' => 'search test', | |
| 712 | - 'ep_epio_set_autosuggest' => 1, | |
| 713 | - 'ep_epio_nonce' => wp_create_nonce( 'ep-epio-set-autosuggest' ), | |
| 714 | - 'nocache' => time(), // Here just to avoid the request hitting a CDN. | |
| 715 | - ], | |
| 716 | - home_url( '/' ) | |
| 717 | - ); | |
| 594 | + $url = $this->get_epio_public_request_url(); | |
| 718 | 595 | |
| 719 | 596 | // Pass the same cookies, so the same authenticated user is used (and we can check the nonce). |
| 720 | 597 | $cookies = []; |
| 721 | 598 | foreach ( $_COOKIE as $name => $value ) { |
| 599 | + if ( ! is_string( $name ) || ! is_string( $value ) ) { | |
| 600 | + continue; | |
| 601 | + } | |
| 602 | + | |
| 722 | 603 | $cookies[] = new \WP_Http_Cookie( |
| 723 | 604 | [ |
| 724 | 605 | 'name' => $name, |
| 725 | 606 | 'value' => $value, |
| @@ -736,14 +617,33 @@ | ||
| 736 | 617 | ); |
| 737 | 618 | } |
| 738 | 619 | |
| 739 | 620 | /** |
| 621 | + * Get the public request URL that saves the autosuggest allowed parameters. | |
| 622 | + * | |
| 623 | + * @since 5.3.0 | |
| 624 | + * @return string | |
| 625 | + */ | |
| 626 | + public function get_epio_public_request_url(): string { | |
| 627 | + return add_query_arg( | |
| 628 | + [ | |
| 629 | + 's' => 'search test', | |
| 630 | + 'ep_epio_set_autosuggest' => 1, | |
| 631 | + 'ep_epio_nonce' => wp_create_nonce( 'ep-epio-set-autosuggest' ), | |
| 632 | + 'nocache' => time(), // Here just to avoid the request hitting a CDN. | |
| 633 | + ], | |
| 634 | + home_url( '/' ) | |
| 635 | + ); | |
| 636 | + } | |
| 637 | + | |
| 638 | + /** | |
| 740 | 639 | * Send the allowed parameters for autosuggest to ElasticPress.io. |
| 741 | 640 | */ |
| 742 | 641 | public function epio_send_autosuggest_allowed() { |
| 743 | - if ( empty( $_REQUEST['ep_epio_nonce'] ) || ! wp_verify_nonce( $_REQUEST['ep_epio_nonce'], 'ep-epio-set-autosuggest' ) ) { | |
| 642 | + if ( empty( $_REQUEST['ep_epio_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['ep_epio_nonce'] ), 'ep-epio-set-autosuggest' ) ) { | |
| 744 | 643 | return; |
| 745 | 644 | } |
| 645 | + | |
| 746 | 646 | if ( empty( $_GET['ep_epio_set_autosuggest'] ) ) { |
| 747 | 647 | return; |
| 748 | 648 | } |
| 749 | 649 | |
| @@ -754,14 +654,16 @@ | ||
| 754 | 654 | * @since 3.5.x |
| 755 | 655 | */ |
| 756 | 656 | do_action( 'ep_epio_pre_send_autosuggest_allowed' ); |
| 757 | 657 | |
| 658 | + $search_query = $this->generate_search_query(); | |
| 659 | + | |
| 758 | 660 | /** |
| 759 | 661 | * The same ES query sent by autosuggest. |
| 760 | 662 | * |
| 761 | 663 | * Sometimes it'll be a string, sometimes it'll be already an array. |
| 762 | 664 | */ |
| 763 | - $es_search_query = $this->generate_search_query()['body']; | |
| 665 | + $es_search_query = $search_query['body']; | |
| 764 | 666 | $es_search_query = ( is_array( $es_search_query ) ) ? $es_search_query : json_decode( $es_search_query, true ); |
| 765 | 667 | |
| 766 | 668 | /** |
| 767 | 669 | * Filter autosuggest ES query |
| @@ -783,9 +685,10 @@ | ||
| 783 | 685 | $index = Indexables::factory()->get( 'post' )->get_index_name(); |
| 784 | 686 | |
| 785 | 687 | add_filter( 'ep_format_request_headers', [ $this, 'add_ep_set_autosuggest_header' ] ); |
| 786 | 688 | |
| 787 | - Elasticsearch::factory()->query( $index, 'post', $es_search_query, [] ); | |
| 689 | + $search_query['query_vars']['ep_intercept_request'] = false; | |
| 690 | + Elasticsearch::factory()->query( $index, 'post', $es_search_query, $search_query['query_vars'] ); | |
| 788 | 691 | |
| 789 | 692 | remove_filter( 'ep_format_request_headers', [ $this, 'add_ep_set_autosuggest_header' ] ); |
| 790 | 693 | |
| 791 | 694 | /** |
| @@ -839,9 +742,9 @@ | ||
| 839 | 742 | <div class="field-name status"><?php esc_html_e( 'Connection', 'elasticpress' ); ?></div> |
| 840 | 743 | <div class="input-wrap"> |
| 841 | 744 | <?php |
| 842 | 745 | $epio_link = 'https://elasticpress.io'; |
| 843 | - $epio_autosuggest_kb_link = 'https://elasticpress.zendesk.com/hc/en-us/articles/360055402791'; | |
| 746 | + $epio_autosuggest_kb_link = 'https://www.elasticpress.io/resources/articles/elasticpress-io-autosuggest/'; | |
| 844 | 747 | $status_report_link = defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ? network_admin_url( 'admin.php?page=elasticpress-status-report' ) : admin_url( 'admin.php?page=elasticpress-status-report' ); |
| 845 | 748 | |
| 846 | 749 | printf( |
| 847 | 750 | /* translators: 1: <a> tag (ElasticPress.io); 2. </a>; 3: <a> tag (KB article); 4. </a>; 5: <a> tag (Site Health Debug Section); 6. </a>; */ |
| @@ -888,18 +791,140 @@ | ||
| 888 | 791 | return $allowed_params; |
| 889 | 792 | } |
| 890 | 793 | |
| 891 | 794 | /** |
| 892 | - * Returns the title. | |
| 795 | + * Send a request to EP.io to reset the allowed parameters for autosuggest. | |
| 893 | 796 | * |
| 894 | - * @since 4.4.1 | |
| 895 | - * @return string | |
| 797 | + * @since 5.3.2 | |
| 896 | 798 | */ |
| 897 | - public function get_title() : string { | |
| 799 | + public function post_deactivation() { | |
| 800 | + $index = Indexables::factory()->get( 'post' )->get_index_name(); | |
| 801 | + | |
| 802 | + add_filter( 'ep_format_request_headers', [ $this, 'add_ep_set_autosuggest_header' ] ); | |
| 803 | + | |
| 804 | + Elasticsearch::factory()->query( $index, 'post', [], [] ); | |
| 805 | + | |
| 806 | + remove_filter( 'ep_format_request_headers', [ $this, 'add_ep_set_autosuggest_header' ] ); | |
| 807 | + | |
| 808 | + // this action is documented in Feature.php | |
| 809 | + do_action( 'ep_feature_post_deactivation', $this->slug, $this ); | |
| 810 | + } | |
| 811 | + | |
| 812 | + /** | |
| 813 | + * Return true, so EP knows we want to intercept the remote request | |
| 814 | + * | |
| 815 | + * As we add and remove this function from `ep_intercept_remote_request`, | |
| 816 | + * using `__return_true` could remove a *real* `__return_true` added by someone else. | |
| 817 | + * | |
| 818 | + * @since 4.7.0 | |
| 819 | + * @see https://github.com/10up/ElasticPress/issues/2887 | |
| 820 | + * @return true | |
| 821 | + */ | |
| 822 | + public function intercept_remote_request() { | |
| 823 | + _doing_it_wrong( | |
| 824 | + __METHOD__, | |
| 825 | + esc_html__( 'Use the WP_Query argument `ep_intercept_request` instead.', 'elasticpress' ), | |
| 826 | + 'ElasticPress 5.3.0' | |
| 827 | + ); | |
| 828 | + | |
| 829 | + return true; | |
| 830 | + } | |
| 831 | + | |
| 832 | + /** | |
| 833 | + * Conditionally add EP.io information to the settings schema | |
| 834 | + * | |
| 835 | + * @since 5.0.0 | |
| 836 | + */ | |
| 837 | + protected function maybe_add_epio_settings_schema() { | |
| 898 | 838 | if ( ! Utils\is_epio() ) { |
| 899 | - return esc_html__( 'Autosuggest', 'elasticpress' ); | |
| 839 | + return; | |
| 900 | 840 | } |
| 901 | 841 | |
| 902 | - /* translators: 1. elasticpress.io logo; */ | |
| 903 | - return sprintf( esc_html__( 'Autosuggest By %s', 'elasticpress' ), $this->get_epio_logo() ); | |
| 842 | + $epio_link = 'https://elasticpress.io'; | |
| 843 | + $epio_autosuggest_kb_link = 'https://www.elasticpress.io/resources/articles/elasticpress-io-autosuggest/'; | |
| 844 | + $status_report_link = defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ? network_admin_url( 'admin.php?page=elasticpress-status-report' ) : admin_url( 'admin.php?page=elasticpress-status-report' ); | |
| 845 | + | |
| 846 | + $this->settings_schema[] = [ | |
| 847 | + 'key' => 'epio', | |
| 848 | + 'label' => sprintf( | |
| 849 | + /* translators: 1: <a> tag (ElasticPress.io); 2. </a>; 3: <a> tag (KB article); 4. </a>; 5: <a> tag (Site Health Debug Section); 6. </a>; */ | |
| 850 | + __( 'You are directly connected to %1$sElasticPress.io%2$s, ensuring the most performant Autosuggest experience. %3$sLearn more about what this means%4$s or %5$sclick here for debug information%6$s.', 'elasticpress' ), | |
| 851 | + '<a href="' . esc_url( $epio_link ) . '">', | |
| 852 | + '</a>', | |
| 853 | + '<a href="' . esc_url( $epio_autosuggest_kb_link ) . '">', | |
| 854 | + '</a>', | |
| 855 | + '<a href="' . esc_url( $status_report_link ) . '">', | |
| 856 | + '</a>' | |
| 857 | + ), | |
| 858 | + 'type' => 'markup', | |
| 859 | + ]; | |
| 860 | + } | |
| 861 | + | |
| 862 | + /** | |
| 863 | + * Set the `settings_schema` attribute | |
| 864 | + * | |
| 865 | + * @since 5.0.0 | |
| 866 | + */ | |
| 867 | + protected function set_settings_schema() { | |
| 868 | + $this->settings_schema = [ | |
| 869 | + [ | |
| 870 | + 'default' => '.ep-autosuggest', | |
| 871 | + 'help' => __( 'Input additional selectors where you would like to include autosuggest, separated by a comma. Example: <code>.custom-selector, #custom-id, input[type="text"]</code>', 'elasticpress' ), | |
| 872 | + 'key' => 'autosuggest_selector', | |
| 873 | + 'label' => __( 'Additional selectors', 'elasticpress' ), | |
| 874 | + 'type' => 'text', | |
| 875 | + ], | |
| 876 | + [ | |
| 877 | + 'default' => '0', | |
| 878 | + 'key' => 'trigger_ga_event', | |
| 879 | + 'help' => __( 'Enable to fire a gtag tracking event when an autosuggest result is clicked.', 'elasticpress' ), | |
| 880 | + 'label' => __( 'Trigger Google Analytics events', 'elasticpress' ), | |
| 881 | + 'type' => 'checkbox', | |
| 882 | + ], | |
| 883 | + ]; | |
| 884 | + | |
| 885 | + $this->maybe_add_epio_settings_schema(); | |
| 886 | + | |
| 887 | + if ( ! Utils\is_epio() ) { | |
| 888 | + $set_in_wp_config = defined( 'EP_AUTOSUGGEST_ENDPOINT' ) && EP_AUTOSUGGEST_ENDPOINT; | |
| 889 | + | |
| 890 | + $this->settings_schema[] = [ | |
| 891 | + 'disabled' => $set_in_wp_config, | |
| 892 | + 'help' => ! $set_in_wp_config ? __( 'A valid URL starting with <code>http://</code> or <code>https://</code>. This address will be exposed to the public.', 'elasticpress' ) : '', | |
| 893 | + 'key' => 'endpoint_url', | |
| 894 | + 'label' => __( 'Endpoint URL', 'elasticpress' ), | |
| 895 | + 'type' => 'url', | |
| 896 | + ]; | |
| 897 | + } | |
| 898 | + } | |
| 899 | + | |
| 900 | + /** | |
| 901 | + * DEPRECATED. Delete the cached query for autosuggest. | |
| 902 | + * | |
| 903 | + * @since 3.5.5 | |
| 904 | + */ | |
| 905 | + public function delete_cached_query() { | |
| 906 | + _doing_it_wrong( | |
| 907 | + __METHOD__, | |
| 908 | + esc_html__( 'This method should not be called anymore, as autosuggest requests are not sent regularly anymore.', 'elasticpress' ), | |
| 909 | + 'ElasticPress 4.7.0' | |
| 910 | + ); | |
| 911 | + } | |
| 912 | + | |
| 913 | + /** | |
| 914 | + * Get the contexts for autosuggest. | |
| 915 | + * | |
| 916 | + * @since 5.1.0 | |
| 917 | + * @return array | |
| 918 | + */ | |
| 919 | + protected function get_contexts(): array { | |
| 920 | + /** | |
| 921 | + * Filter contexts for autosuggest. | |
| 922 | + * | |
| 923 | + * @hook ep_autosuggest_contexts | |
| 924 | + * @since 5.1.0 | |
| 925 | + * @param {array} $contexts Contexts for autosuggest | |
| 926 | + * @return {array} New contexts | |
| 927 | + */ | |
| 928 | + return apply_filters( 'ep_autosuggest_contexts', [ 'public', 'ajax' ] ); | |
| 904 | 929 | } |
| 905 | 930 | } |