| @@ -51,15 +51,10 @@ | ||
| 51 | 51 | */ |
| 52 | 52 | public function __construct() { |
| 53 | 53 | $this->slug = 'search'; |
| 54 | 54 | |
| 55 | - $this->title = esc_html__( 'Post Search', 'elasticpress' ); | |
| 55 | + $this->group = 'core-search'; | |
| 56 | 56 | |
| 57 | - $this->summary = '<p>' . __( 'Instantly find the content you’re looking for. The first time.', 'elasticpress' ) . '</p>' . | |
| 58 | - '<p>' . __( 'Overcome higher-end performance and functional limits posed by the traditional WordPress structured (SQL) database to deliver superior keyword search, instantly. ElasticPress indexes custom fields, tags, and other metadata to improve search results. Fuzzy matching accounts for misspellings and verb tenses.', 'elasticpress' ) . '</p>'; | |
| 59 | - | |
| 60 | - $this->docs_url = __( 'https://elasticpress.zendesk.com/hc/en-us/articles/360050447492-Configuring-ElasticPress-via-the-Plugin-Dashboard#post-search', 'elasticpress' ); | |
| 61 | - | |
| 62 | 57 | $this->requires_install_reindex = false; |
| 63 | 58 | |
| 64 | 59 | $this->default_settings = [ |
| 65 | 60 | 'decaying_enabled' => '1', |
| @@ -74,8 +69,29 @@ | ||
| 74 | 69 | parent::__construct(); |
| 75 | 70 | } |
| 76 | 71 | |
| 77 | 72 | /** |
| 73 | + * Sets i18n strings. | |
| 74 | + * | |
| 75 | + * @return void | |
| 76 | + * @since 5.2.0 | |
| 77 | + */ | |
| 78 | + public function set_i18n_strings(): void { | |
| 79 | + $this->title = esc_html__( 'Post Search', 'elasticpress' ); | |
| 80 | + | |
| 81 | + $this->summary = '<p>' . __( 'Instantly find the content you’re looking for. The first time.', 'elasticpress' ) . '</p>' . | |
| 82 | + '<p>' . __( 'Overcome higher-end performance and functional limits posed by the traditional WordPress structured (SQL) database to deliver superior keyword search, instantly. ElasticPress indexes custom fields, tags, and other metadata to improve search results. Fuzzy matching accounts for misspellings and verb tenses.', 'elasticpress' ) . '</p>'; | |
| 83 | + | |
| 84 | + $this->docs_url = __( 'https://www.elasticpress.io/resources/articles/configuring-elasticpress-via-the-plugin-dashboard/#post-search', 'elasticpress' ); | |
| 85 | + | |
| 86 | + $this->field_group_map = [ | |
| 87 | + 'highlight_group' => [ | |
| 88 | + 'label' => esc_html__( 'Highlighting Options', 'elasticpress' ), | |
| 89 | + ], | |
| 90 | + ]; | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 78 | 94 | * We need to delay search setup up since it will fire after protected content and protected |
| 79 | 95 | * content filters into the search setup |
| 80 | 96 | * |
| 81 | 97 | * @since 2.2 |
| @@ -80,10 +96,11 @@ | ||
| 80 | 96 | * |
| 81 | 97 | * @since 2.2 |
| 82 | 98 | */ |
| 83 | 99 | public function setup() { |
| 100 | + Indexables::factory()->activate( 'post' ); | |
| 101 | + | |
| 84 | 102 | add_action( 'init', [ $this, 'search_setup' ] ); |
| 85 | - add_filter( 'ep_sanitize_feature_settings', [ $this, 'sanitize_highlighting_settings' ] ); | |
| 86 | 103 | |
| 87 | 104 | // Set up weighting sub-module |
| 88 | 105 | $this->weighting = new Weighting(); |
| 89 | 106 | $this->weighting->setup(); |
| @@ -284,16 +301,20 @@ | ||
| 284 | 301 | * |
| 285 | 302 | * @return string $text the new excerpt |
| 286 | 303 | */ |
| 287 | 304 | public function ep_highlight_excerpt( $text, $post ) { |
| 288 | - | |
| 289 | 305 | $settings = $this->get_settings(); |
| 290 | 306 | |
| 291 | 307 | // reproduces wp_trim_excerpt filter, preserving the excerpt_more and excerpt_length filters |
| 292 | 308 | if ( '' === $text ) { |
| 293 | 309 | $text = get_the_content( '', false, $post ); |
| 310 | + | |
| 311 | + $text = strip_shortcodes( $text ); | |
| 312 | + $text = excerpt_remove_blocks( $text ); | |
| 313 | + | |
| 294 | 314 | $text = apply_filters( 'the_content', $text ); |
| 295 | 315 | $text = str_replace( '\]\]\>', ']]>', $text ); |
| 316 | + | |
| 296 | 317 | $text = strip_tags( $text, '<' . esc_html( $settings['highlight_tag'] ) . '>' ); |
| 297 | 318 | |
| 298 | 319 | // use the defined length, if already applied... |
| 299 | 320 | $excerpt_length = apply_filters( 'excerpt_length', 55 ); |
| @@ -299,11 +320,14 @@ | ||
| 299 | 320 | $excerpt_length = apply_filters( 'excerpt_length', 55 ); |
| 300 | 321 | |
| 301 | 322 | // use defined excerpt_more filter if it is used |
| 302 | 323 | $excerpt_more = apply_filters( 'excerpt_more', $text ); |
| 303 | - | |
| 304 | 324 | $excerpt_more = $excerpt_more !== $text ? $excerpt_more : '[…]'; |
| 305 | 325 | |
| 326 | + /** | |
| 327 | + * WordPress would handle this using `wp_trim_words` but that removes all tags, | |
| 328 | + * including the one used to highlight the search term. | |
| 329 | + */ | |
| 306 | 330 | $words = explode( ' ', $text, $excerpt_length + 1 ); |
| 307 | 331 | if ( count( $words ) > $excerpt_length ) { |
| 308 | 332 | array_pop( $words ); |
| 309 | 333 | array_push( $words, $excerpt_more ); |
| @@ -340,26 +364,8 @@ | ||
| 340 | 364 | return $tag; |
| 341 | 365 | } |
| 342 | 366 | |
| 343 | 367 | /** |
| 344 | - * Sanitizes our highlighting settings. | |
| 345 | - * | |
| 346 | - * @param array $settings Array of current settings | |
| 347 | - * @return mixed | |
| 348 | - */ | |
| 349 | - public function sanitize_highlighting_settings( $settings ) { | |
| 350 | - if ( ! empty( $settings['search']['highlight_excerpt'] ) ) { | |
| 351 | - $settings['search']['highlight_excerpt'] = $settings['search']['highlight_excerpt']; | |
| 352 | - } | |
| 353 | - | |
| 354 | - if ( ! empty( $settings['search']['highlight_enabled'] ) ) { | |
| 355 | - $settings['search']['highlight_enabled'] = $settings['search']['highlight_enabled']; | |
| 356 | - } | |
| 357 | - | |
| 358 | - return $settings; | |
| 359 | - } | |
| 360 | - | |
| 361 | - /** | |
| 362 | 368 | * Returns searchable post types for the current site |
| 363 | 369 | * |
| 364 | 370 | * @since 1.9 |
| 365 | 371 | * @return mixed|void |
| @@ -562,20 +568,8 @@ | ||
| 562 | 568 | return $formatted_args; |
| 563 | 569 | } |
| 564 | 570 | |
| 565 | 571 | /** |
| 566 | - * Output feature box long text | |
| 567 | - * | |
| 568 | - * @since 3.0 | |
| 569 | - */ | |
| 570 | - public function output_feature_box_long() { | |
| 571 | - ?> | |
| 572 | - <p><?php esc_html_e( 'Overcome higher-end performance and functional limits posed by the traditional WordPress structured (SQL) database to deliver superior keyword search, instantly. ElasticPress indexes custom fields, tags, and other metadata to improve search results. Fuzzy matching accounts for misspellings and verb tenses.', 'elasticpress' ); ?></p> | |
| 573 | - | |
| 574 | - <?php | |
| 575 | - } | |
| 576 | - | |
| 577 | - /** | |
| 578 | 572 | * Enable integration on search queries |
| 579 | 573 | * |
| 580 | 574 | * @param bool $enabled Original enabled value |
| 581 | 575 | * @param WP_Query $query WP Query |
| @@ -611,72 +605,8 @@ | ||
| 611 | 605 | return apply_filters( 'ep_integrate_search_queries', $enabled, $query ); |
| 612 | 606 | } |
| 613 | 607 | |
| 614 | 608 | /** |
| 615 | - * Display decaying settings on dashboard. | |
| 616 | - * | |
| 617 | - * @since 2.4 | |
| 618 | - */ | |
| 619 | - public function output_feature_box_settings() { | |
| 620 | - $settings = $this->get_settings(); | |
| 621 | - ?> | |
| 622 | - <div class="field"> | |
| 623 | - <div class="field-name status"><?php esc_html_e( 'Weight results by date', 'elasticpress' ); ?></div> | |
| 624 | - <div class="input-wrap"> | |
| 625 | - <label><input name="settings[decaying_enabled]" type="radio" <?php checked( (bool) $settings['decaying_enabled'] ); ?> value="1"><?php esc_html_e( 'Enabled', 'elasticpress' ); ?></label><br> | |
| 626 | - <label><input name="settings[decaying_enabled]" type="radio" <?php checked( ! (bool) $settings['decaying_enabled'] ); ?> value="0"><?php esc_html_e( 'Disabled', 'elasticpress' ); ?></label><br> | |
| 627 | - <?php | |
| 628 | - /** | |
| 629 | - * Fires after the default Weight results by date settings | |
| 630 | - * | |
| 631 | - * @since 4.6.0 | |
| 632 | - * @hook ep_weight_settings_after_search | |
| 633 | - * @param {array} $settings settings array | |
| 634 | - */ | |
| 635 | - do_action( 'ep_weight_settings_after_search', $settings ); | |
| 636 | - ?> | |
| 637 | - </div> | |
| 638 | - </div> | |
| 639 | - <div class="field"> | |
| 640 | - <div class="field-name status"><?php esc_html_e( 'Highlighting status', 'elasticpress' ); ?></div> | |
| 641 | - <div class="input-wrap"> | |
| 642 | - <label><input name="settings[highlight_enabled]" type="radio" <?php checked( $settings['highlight_enabled'], '1' ); ?> value="1"><?php esc_html_e( 'Enabled', 'elasticpress' ); ?></label><br> | |
| 643 | - <label><input name="settings[highlight_enabled]" type="radio" <?php checked( $settings['highlight_enabled'], '0' ); ?> value="0"><?php esc_html_e( 'Disabled', 'elasticpress' ); ?></label> | |
| 644 | - <p class="field-description"><?php esc_html_e( 'Wrap search terms in HTML tags in results for custom styling. The wrapping HTML tag comes with the "ep-highlight" class for easy styling.' ); ?></p> | |
| 645 | - </div> | |
| 646 | - </div> | |
| 647 | - <div class="field"> | |
| 648 | - <label for="highlight-tag" class="field-name status"><?php echo esc_html_e( 'Highlight tag ', 'elasticpress' ); ?></label> | |
| 649 | - <div class="input-wrap"> | |
| 650 | - <select id="highlight-tag" name="settings[highlight_tag]"> | |
| 651 | - <?php | |
| 652 | - foreach ( self::$default_highlight_tags as $option ) : | |
| 653 | - echo '<option value="' . esc_attr( $option ) . '" ' . selected( $option, $settings['highlight_tag'] ) . '>' . esc_html( $option ) . '</option>'; | |
| 654 | - endforeach; | |
| 655 | - ?> | |
| 656 | - </select> | |
| 657 | - </div> | |
| 658 | - </div> | |
| 659 | - | |
| 660 | - <div class="field"> | |
| 661 | - <div class="field-name status"><?php esc_html_e( 'Excerpt highlighting', 'elasticpress' ); ?></div> | |
| 662 | - <div class="input-wrap"> | |
| 663 | - <label><input name="settings[highlight_excerpt]" type="radio" <?php checked( $settings['highlight_excerpt'], '1' ); ?> value="1"><?php esc_html_e( 'Enabled', 'elasticpress' ); ?></label><br> | |
| 664 | - <label><input name="settings[highlight_excerpt]" type="radio" <?php checked( $settings['highlight_excerpt'], '0' ); ?> value="0"><?php esc_html_e( 'Disabled', 'elasticpress' ); ?></label> | |
| 665 | - <p class="field-description"><?php esc_html_e( 'By default, WordPress strips HTML from content excerpts. Enable when using the_excerpt() to display search results. ', 'elasticpress' ); ?></p> | |
| 666 | - </div> | |
| 667 | - </div> | |
| 668 | - | |
| 669 | - <?php if ( ! defined( 'EP_IS_NETWORK' ) || ! EP_IS_NETWORK ) : ?> | |
| 670 | - <br class="clear"> | |
| 671 | - <p><a href="<?php echo esc_url( admin_url( 'admin.php?page=elasticpress-weighting' ) ); ?>"><?php esc_html_e( 'Advanced fields and weighting settings', 'elasticpress' ); ?></a></p> | |
| 672 | - <p><a href="<?php echo esc_url( admin_url( 'admin.php?page=elasticpress-synonyms' ) ); ?>"><?php esc_html_e( 'Add synonyms to your post searches', 'elasticpress' ); ?></a></p> | |
| 673 | - <?php endif; ?> | |
| 674 | - | |
| 675 | - <?php | |
| 676 | - } | |
| 677 | - | |
| 678 | - /** | |
| 679 | 609 | * Registers post meta for exclude from search feature. |
| 680 | 610 | */ |
| 681 | 611 | public function register_meta() { |
| 682 | 612 | register_post_meta( |
| @@ -711,15 +641,21 @@ | ||
| 711 | 641 | if ( ! $post instanceof \WP_Post ) { |
| 712 | 642 | return; |
| 713 | 643 | } |
| 714 | 644 | |
| 645 | + if ( ! $post->post_type || ! post_type_supports( $post->post_type, 'custom-fields' ) ) { | |
| 646 | + return; | |
| 647 | + } | |
| 648 | + | |
| 715 | 649 | wp_enqueue_script( |
| 716 | 650 | 'ep-search-editor', |
| 717 | - EP_URL . '/dist/js/search-editor-script.js', | |
| 651 | + EP_URL . 'dist/js/search-editor-script.js', | |
| 718 | 652 | Utils\get_asset_info( 'search-editor-script', 'dependencies' ), |
| 719 | 653 | Utils\get_asset_info( 'search-editor-script', 'version' ), |
| 720 | 654 | true |
| 721 | 655 | ); |
| 656 | + | |
| 657 | + wp_set_script_translations( 'ep-search-editor', 'elasticpress' ); | |
| 722 | 658 | } |
| 723 | 659 | |
| 724 | 660 | /** |
| 725 | 661 | * Exclude posts based on ep_exclude_from_search post meta. |
| @@ -728,9 +664,14 @@ | ||
| 728 | 664 | * @param array $args WP Query args |
| 729 | 665 | * @param WP_Query $query WP Query object |
| 730 | 666 | */ |
| 731 | 667 | public function exclude_posts_from_search( $filters, $args, $query ) { |
| 732 | - $bypass_exclusion_from_search = is_admin() || ! $query->is_search(); | |
| 668 | + if ( ! empty( $query->get( 'ep_skip_search_exclusion' ) ) ) { | |
| 669 | + return $filters; | |
| 670 | + } | |
| 671 | + | |
| 672 | + $bypass_exclusion_from_search = ( is_admin() && ! wp_doing_ajax() ) || ! $query->is_search(); | |
| 673 | + | |
| 733 | 674 | /** |
| 734 | 675 | * Filter whether the exclusion from the "exclude from search" checkbox should be applied |
| 735 | 676 | * |
| 736 | 677 | * @since 4.4.0 |
| @@ -738,9 +679,14 @@ | ||
| 738 | 679 | * @param {bool} $bypass_exclusion_from_search True means all posts will be returned |
| 739 | 680 | * @param {WP_Query} $query WP Query |
| 740 | 681 | * @return {bool} New $bypass_exclusion_from_search value |
| 741 | 682 | */ |
| 742 | - if ( apply_filters( 'ep_bypass_exclusion_from_search', $bypass_exclusion_from_search, $query ) ) { | |
| 683 | + if ( apply_filters_deprecated( | |
| 684 | + 'ep_bypass_exclusion_from_search', | |
| 685 | + [ $bypass_exclusion_from_search, $query ], | |
| 686 | + 'ElasticPress 5.3.0', | |
| 687 | + 'WP_Query->ep_skip_search_exclusion argument' | |
| 688 | + ) ) { | |
| 743 | 689 | return $filters; |
| 744 | 690 | } |
| 745 | 691 | |
| 746 | 692 | $filters[] = [ |
| @@ -806,9 +752,8 @@ | ||
| 806 | 752 | update_post_meta( $post_id, 'ep_exclude_from_search', true ); |
| 807 | 753 | } else { |
| 808 | 754 | delete_post_meta( $post_id, 'ep_exclude_from_search' ); |
| 809 | 755 | } |
| 810 | - | |
| 811 | 756 | } |
| 812 | 757 | |
| 813 | 758 | /** |
| 814 | 759 | * If WP_Query has unsupported orderby, skip ES query integration and use the WP query instead. |
| @@ -868,32 +813,30 @@ | ||
| 868 | 813 | ], |
| 869 | 814 | 'type' => 'radio', |
| 870 | 815 | ], |
| 871 | 816 | [ |
| 872 | - 'default' => '0', | |
| 873 | - 'help' => __( 'Enable to wrap search terms in HTML tags in results for custom styling. The wrapping HTML tag comes with the <code>ep-highlight</code> class for easy styling.' ), | |
| 874 | - 'key' => 'highlight_enabled', | |
| 875 | - 'label' => __( 'Highlight search terms', 'elasticpress' ), | |
| 876 | - 'type' => 'checkbox', | |
| 817 | + 'default' => '0', | |
| 818 | + 'help' => __( 'Enable to wrap search terms in HTML tags in results for custom styling. The wrapping HTML tag comes with the <code>ep-highlight</code> class for easy styling.', 'elasticpress' ), | |
| 819 | + 'key' => 'highlight_enabled', | |
| 820 | + 'label' => __( 'Highlight search terms', 'elasticpress' ), | |
| 821 | + 'type' => 'checkbox', | |
| 822 | + 'field_group_slug' => 'highlight_group', | |
| 877 | 823 | ], |
| 878 | 824 | [ |
| 879 | - 'default' => '0', | |
| 880 | - 'help' => __( 'By default, WordPress strips HTML from content excerpts. Enable when using <code>the_excerpt()</code> to display search results.', 'elasticpress' ), | |
| 881 | - 'key' => 'highlight_excerpt', | |
| 882 | - 'label' => __( 'Highlight search terms in excerpts', 'elasticpress' ), | |
| 883 | - 'type' => 'checkbox', | |
| 825 | + 'default' => '0', | |
| 826 | + 'help' => __( 'By default, WordPress strips HTML from content excerpts. Enable when using <code>the_excerpt()</code> to display search results.', 'elasticpress' ), | |
| 827 | + 'key' => 'highlight_excerpt', | |
| 828 | + 'label' => __( 'Highlight search terms in excerpts', 'elasticpress' ), | |
| 829 | + 'type' => 'checkbox', | |
| 830 | + 'field_group_slug' => 'highlight_group', | |
| 884 | 831 | ], |
| 885 | 832 | [ |
| 886 | - 'default' => 'mark', | |
| 887 | - 'help' => __( 'Select the HTML tag used to highlight search terms.', 'elasticpress' ), | |
| 888 | - 'key' => 'highlight_tag', | |
| 889 | - 'label' => __( 'Highlight tag', 'elasticpress' ), | |
| 890 | - 'options' => [ | |
| 833 | + 'default' => 'mark', | |
| 834 | + 'help' => __( 'Select the HTML tag used to highlight search terms.', 'elasticpress' ), | |
| 835 | + 'key' => 'highlight_tag', | |
| 836 | + 'label' => __( 'Highlight tag', 'elasticpress' ), | |
| 837 | + 'options' => [ | |
| 891 | 838 | [ |
| 892 | - 'label' => __( 'None', 'elasticpress' ), | |
| 893 | - 'value' => '', | |
| 894 | - ], | |
| 895 | - [ | |
| 896 | 839 | 'label' => 'mark', |
| 897 | 840 | 'value' => 'mark', |
| 898 | 841 | ], |
| 899 | 842 | [ |
| @@ -912,9 +855,17 @@ | ||
| 912 | 855 | 'label' => 'i', |
| 913 | 856 | 'value' => 'i', |
| 914 | 857 | ], |
| 915 | 858 | ], |
| 916 | - 'type' => 'select', | |
| 859 | + 'type' => 'select', | |
| 860 | + 'requires_fields' => [ | |
| 861 | + 'relationship' => 'OR', | |
| 862 | + 'conditions' => [ | |
| 863 | + 'highlight_enabled' => '1', | |
| 864 | + 'highlight_excerpt' => '1', | |
| 865 | + ], | |
| 866 | + ], | |
| 867 | + 'field_group_slug' => 'highlight_group', | |
| 917 | 868 | ], |
| 918 | 869 | [ |
| 919 | 870 | 'default' => 'simple', |
| 920 | 871 | 'key' => 'synonyms_editor_mode', |