| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * User indexable | |
| 3 | + * Post indexable | |
| 4 | 4 | * |
| 5 | 5 | * @since 3.0 |
| 6 | 6 | * @package elasticpress |
| 7 | 7 | */ |
| @@ -7,12 +7,12 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ElasticPress\Indexable\Post; |
| 10 | 10 | |
| 11 | -use ElasticPress\Indexable as Indexable; | |
| 12 | -use ElasticPress\Elasticsearch as Elasticsearch; | |
| 13 | -use \WP_Query as WP_Query; | |
| 14 | -use \WP_User as WP_User; | |
| 11 | +use WP_Query; | |
| 12 | +use WP_User; | |
| 13 | +use ElasticPress\Elasticsearch; | |
| 14 | +use ElasticPress\Indexable; | |
| 15 | 15 | |
| 16 | 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | 17 | // @codeCoverageIgnoreStart |
| 18 | 18 | exit; // Exit if accessed directly. |
| @@ -41,13 +41,14 @@ | ||
| 41 | 41 | */ |
| 42 | 42 | public $support_indexing_advanced_pagination = true; |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | - * Create indexable and initialize dependencies | |
| 45 | + * Instantiate the indexable SyncManager and QueryIntegration | |
| 46 | 46 | * |
| 47 | - * @since 3.0 | |
| 47 | + * @since 5.2.0 | |
| 48 | + * @return void | |
| 48 | 49 | */ |
| 49 | - public function __construct() { | |
| 50 | + public function setup() { | |
| 50 | 51 | $this->labels = [ |
| 51 | 52 | 'plural' => esc_html__( 'Posts', 'elasticpress' ), |
| 52 | 53 | 'singular' => esc_html__( 'Post', 'elasticpress' ), |
| 53 | 54 | ]; |
| @@ -102,8 +103,11 @@ | ||
| 102 | 103 | // Disable advanced pagination. Not useful if only indexing specific IDs. |
| 103 | 104 | $args['ep_indexing_advanced_pagination'] = false; |
| 104 | 105 | } |
| 105 | 106 | |
| 107 | + // Explicitly set the orderby to ID to prevent accidental modifications by other code. | |
| 108 | + add_filter( 'posts_orderby', [ $this, 'set_posts_orderby' ], 9999, 2 ); | |
| 109 | + | |
| 106 | 110 | // Enforce the following query args during advanced pagination to ensure things work correctly. |
| 107 | 111 | if ( $args['ep_indexing_advanced_pagination'] ) { |
| 108 | 112 | $args = array_merge( |
| 109 | 113 | $args, |
| @@ -115,19 +119,21 @@ | ||
| 115 | 119 | 'offset' => 0, |
| 116 | 120 | 'no_found_rows' => true, |
| 117 | 121 | ] |
| 118 | 122 | ); |
| 119 | - add_filter( 'posts_where', array( $this, 'bulk_indexing_filter_posts_where' ), 9999, 2 ); | |
| 123 | + add_filter( 'posts_where', [ $this, 'bulk_indexing_filter_posts_where' ], 9999, 2 ); | |
| 120 | 124 | |
| 121 | 125 | $query = new WP_Query( $args ); |
| 122 | 126 | $total_objects = $this->get_total_objects_for_query( $args ); |
| 123 | 127 | |
| 124 | - remove_filter( 'posts_where', array( $this, 'bulk_indexing_filter_posts_where' ), 9999, 2 ); | |
| 128 | + remove_filter( 'posts_where', [ $this, 'bulk_indexing_filter_posts_where' ], 9999, 2 ); | |
| 125 | 129 | } else { |
| 126 | 130 | $query = new WP_Query( $args ); |
| 127 | 131 | $total_objects = $query->found_posts; |
| 128 | 132 | } |
| 129 | 133 | |
| 134 | + remove_filter( 'posts_orderby', [ $this, 'set_posts_orderby' ], 9999, 2 ); | |
| 135 | + | |
| 130 | 136 | return [ |
| 131 | 137 | 'objects' => $query->posts, |
| 132 | 138 | 'total_objects' => $total_objects, |
| 133 | 139 | ]; |
| @@ -132,16 +138,18 @@ | ||
| 132 | 138 | 'total_objects' => $total_objects, |
| 133 | 139 | ]; |
| 134 | 140 | } |
| 135 | 141 | |
| 136 | - /** | |
| 137 | - * Manipulate the WHERE clause of the bulk indexing query to paginate by ID in order to avoid performance issues with SQL offset. | |
| 138 | - * | |
| 139 | - * @param string $where The current $where clause. | |
| 140 | - * @param WP_Query $query WP_Query object. | |
| 141 | - * @return string WHERE clause with our pagination added if needed. | |
| 142 | - */ | |
| 142 | + /** | |
| 143 | + * Manipulate the WHERE clause of the bulk indexing query to paginate by ID in order to avoid performance issues with SQL offset. | |
| 144 | + * | |
| 145 | + * @param string $where The current $where clause. | |
| 146 | + * @param WP_Query $query WP_Query object. | |
| 147 | + * @return string WHERE clause with our pagination added if needed. | |
| 148 | + */ | |
| 143 | 149 | public function bulk_indexing_filter_posts_where( $where, $query ) { |
| 150 | + global $wpdb; | |
| 151 | + | |
| 144 | 152 | $using_advanced_pagination = $query->get( 'ep_indexing_advanced_pagination', false ); |
| 145 | 153 | |
| 146 | 154 | if ( $using_advanced_pagination ) { |
| 147 | 155 | $requested_upper_limit_id = $query->get( 'ep_indexing_upper_limit_object_id', PHP_INT_MAX ); |
| @@ -159,10 +167,10 @@ | ||
| 159 | 167 | return $where; |
| 160 | 168 | } |
| 161 | 169 | |
| 162 | 170 | $range = [ |
| 163 | - 'upper_limit' => "{$GLOBALS['wpdb']->posts}.ID <= {$upper_limit_range_post_id}", | |
| 164 | - 'lower_limit' => "{$GLOBALS['wpdb']->posts}.ID >= {$requested_lower_limit_post_id}", | |
| 171 | + 'upper_limit' => "{$wpdb->posts}.ID <= {$upper_limit_range_post_id}", | |
| 172 | + 'lower_limit' => "{$wpdb->posts}.ID >= {$requested_lower_limit_post_id}", | |
| 165 | 173 | ]; |
| 166 | 174 | |
| 167 | 175 | // Skip the end range if it's unnecessary. |
| 168 | 176 | $skip_ending_range = 0 === $requested_lower_limit_post_id; |
| @@ -244,9 +252,9 @@ | ||
| 244 | 252 | * |
| 245 | 253 | * The if below will pass if `has_password` is false but not null. |
| 246 | 254 | */ |
| 247 | 255 | if ( isset( $query_args['has_password'] ) && ! $query_args['has_password'] ) { |
| 248 | - $posts_with_password = (int) $wpdb->get_var( "SELECT COUNT(1) AS posts_with_password FROM {$wpdb->posts} WHERE post_password != ''" ); | |
| 256 | + $posts_with_password = (int) $wpdb->get_var( "SELECT COUNT(1) AS posts_with_password FROM {$wpdb->posts} WHERE post_password != ''" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery | |
| 249 | 257 | |
| 250 | 258 | $post_count -= $posts_with_password; |
| 251 | 259 | } |
| 252 | 260 | |
| @@ -314,19 +322,14 @@ | ||
| 314 | 322 | * @return {string} New version |
| 315 | 323 | */ |
| 316 | 324 | $es_version = apply_filters( 'ep_fallback_elasticsearch_version', '2.0' ); |
| 317 | 325 | } |
| 326 | + $es_version = (string) $es_version; | |
| 318 | 327 | |
| 319 | - $mapping_file = '5-2.php'; | |
| 328 | + $mapping_file = '7-0.php'; | |
| 320 | 329 | |
| 321 | - if ( ! $es_version || version_compare( $es_version, '5.0' ) < 0 ) { | |
| 322 | - $mapping_file = 'pre-5-0.php'; | |
| 323 | - } elseif ( version_compare( $es_version, '5.0', '>=' ) && version_compare( $es_version, '5.2', '<' ) ) { | |
| 324 | - $mapping_file = '5-0.php'; | |
| 325 | - } elseif ( version_compare( $es_version, '5.2', '>=' ) && version_compare( $es_version, '7.0', '<' ) ) { | |
| 330 | + if ( version_compare( $es_version, '7.0', '<' ) ) { | |
| 326 | 331 | $mapping_file = '5-2.php'; |
| 327 | - } elseif ( version_compare( $es_version, '7.0', '>=' ) ) { | |
| 328 | - $mapping_file = '7-0.php'; | |
| 329 | 332 | } |
| 330 | 333 | |
| 331 | 334 | return apply_filters( 'ep_post_mapping_version', $mapping_file ); |
| 332 | 335 | } |
| @@ -419,9 +422,11 @@ | ||
| 419 | 422 | * @since 0.9.1 |
| 420 | 423 | * @return bool|array |
| 421 | 424 | */ |
| 422 | 425 | public function prepare_document( $post_id ) { |
| 426 | + global $post; | |
| 423 | 427 | $post = get_post( $post_id ); |
| 428 | + setup_postdata( $post ); | |
| 424 | 429 | |
| 425 | 430 | if ( empty( $post ) ) { |
| 426 | 431 | return false; |
| 427 | 432 | } |
| @@ -450,9 +455,9 @@ | ||
| 450 | 455 | $post_modified_gmt = $post->post_modified_gmt; |
| 451 | 456 | $comment_count = absint( $post->comment_count ); |
| 452 | 457 | $comment_status = $post->comment_status; |
| 453 | 458 | $ping_status = $post->ping_status; |
| 454 | - $menu_order = absint( $post->menu_order ); | |
| 459 | + $menu_order = (int) $post->menu_order; | |
| 455 | 460 | |
| 456 | 461 | /** |
| 457 | 462 | * Filter to ignore invalid dates |
| 458 | 463 | * |
| @@ -562,11 +567,8 @@ | ||
| 562 | 567 | |
| 563 | 568 | /** |
| 564 | 569 | * Filters the image size to use when indexing the post thumbnail. |
| 565 | 570 | * |
| 566 | - * Defaults to the `woocommerce_thumbnail` size if WooCommerce is in | |
| 567 | - * use. Otherwise the `thumbnail` size is used. | |
| 568 | - * | |
| 569 | 571 | * @hook ep_thumbnail_image_size |
| 570 | 572 | * @since 4.0.0 |
| 571 | 573 | * @param {string|int[]} $image_size Image size. Can be any registered |
| 572 | 574 | * image size name, or an array of |
| @@ -576,25 +578,31 @@ | ||
| 576 | 578 | * @return {array} Image size to pass to wp_get_attachment_image_src(). |
| 577 | 579 | */ |
| 578 | 580 | $image_size = apply_filters( |
| 579 | 581 | 'ep_post_thumbnail_image_size', |
| 580 | - function_exists( 'WC' ) ? 'woocommerce_thumbnail' : 'thumbnail', | |
| 582 | + 'medium', | |
| 581 | 583 | $post |
| 582 | 584 | ); |
| 583 | 585 | |
| 584 | - $image_src = wp_get_attachment_image_src( $attachment_id, $image_size ); | |
| 586 | + $image = wp_get_attachment_image_src( $attachment_id, $image_size ); | |
| 585 | 587 | $image_alt = trim( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ); |
| 586 | 588 | |
| 587 | - if ( ! $image_src ) { | |
| 589 | + if ( ! $image ) { | |
| 588 | 590 | return null; |
| 589 | 591 | } |
| 590 | 592 | |
| 593 | + list( $src, $width, $height ) = $image; | |
| 594 | + | |
| 595 | + $image_meta = wp_get_attachment_metadata( $attachment_id ); | |
| 596 | + $srcset = wp_calculate_image_srcset( [ absint( $width ), absint( $height ) ], $src, $image_meta, $attachment_id ); | |
| 597 | + | |
| 591 | 598 | return [ |
| 592 | 599 | 'ID' => $attachment_id, |
| 593 | - 'src' => $image_src[0], | |
| 594 | - 'width' => $image_src[1], | |
| 595 | - 'height' => $image_src[2], | |
| 600 | + 'src' => $src, | |
| 601 | + 'width' => $width, | |
| 602 | + 'height' => $height, | |
| 596 | 603 | 'alt' => $image_alt, |
| 604 | + 'srcset' => $srcset, | |
| 597 | 605 | ]; |
| 598 | 606 | } |
| 599 | 607 | |
| 600 | 608 | /** |
| @@ -599,9 +607,9 @@ | ||
| 599 | 607 | |
| 600 | 608 | /** |
| 601 | 609 | * Prepare date terms to send to ES. |
| 602 | 610 | * |
| 603 | - * @param string $date_to_prepare Post date | |
| 611 | + * @param null|string $date_to_prepare Post date | |
| 604 | 612 | * @since 0.1.4 |
| 605 | 613 | * @return array |
| 606 | 614 | */ |
| 607 | 615 | public function prepare_date_terms( $date_to_prepare ) { |
| @@ -620,9 +628,9 @@ | ||
| 620 | 628 | ]; |
| 621 | 629 | |
| 622 | 630 | // Combine all the date term formats and perform one single call to date_i18n() for performance. |
| 623 | 631 | $date_format = implode( '||', array_values( $terms_to_prepare ) ); |
| 624 | - $combined_dates = explode( '||', date_i18n( $date_format, strtotime( $date_to_prepare ) ) ); | |
| 632 | + $combined_dates = explode( '||', date_i18n( $date_format, strtotime( (string) $date_to_prepare ) ) ); | |
| 625 | 633 | |
| 626 | 634 | // Then split up the results for individual indexing. |
| 627 | 635 | $date_terms = []; |
| 628 | 636 | foreach ( $terms_to_prepare as $term_name => $date_format ) { |
| @@ -714,19 +722,10 @@ | ||
| 714 | 722 | $terms_dic = []; |
| 715 | 723 | |
| 716 | 724 | foreach ( $object_terms as $term ) { |
| 717 | 725 | if ( ! isset( $terms_dic[ $term->term_id ] ) ) { |
| 718 | - $terms_dic[ $term->term_id ] = array( | |
| 719 | - 'term_id' => $term->term_id, | |
| 720 | - 'slug' => $term->slug, | |
| 721 | - 'name' => $term->name, | |
| 722 | - 'parent' => $term->parent, | |
| 723 | - 'term_taxonomy_id' => $term->term_taxonomy_id, | |
| 724 | - 'term_order' => (int) $this->get_term_order( $term->term_taxonomy_id, $post->ID ), | |
| 725 | - ); | |
| 726 | + $terms_dic[ $term->term_id ] = $this->get_formatted_term( $term, $post->ID ); | |
| 726 | 727 | |
| 727 | - $terms_dic[ $term->term_id ]['facet'] = wp_json_encode( $terms_dic[ $term->term_id ] ); | |
| 728 | - | |
| 729 | 728 | if ( $allow_hierarchy ) { |
| 730 | 729 | $terms_dic = $this->get_parent_terms( $terms_dic, $term, $taxonomy->name, $post->ID ); |
| 731 | 730 | } |
| 732 | 731 | } |
| @@ -752,26 +751,47 @@ | ||
| 752 | 751 | if ( ! $parent_term || is_wp_error( $parent_term ) ) { |
| 753 | 752 | return $terms; |
| 754 | 753 | } |
| 755 | 754 | if ( ! isset( $terms[ $parent_term->term_id ] ) ) { |
| 756 | - $terms[ $parent_term->term_id ] = array( | |
| 757 | - 'term_id' => $parent_term->term_id, | |
| 758 | - 'slug' => $parent_term->slug, | |
| 759 | - 'name' => $parent_term->name, | |
| 760 | - 'parent' => $parent_term->parent, | |
| 761 | - 'term_taxonomy_id' => $parent_term->term_taxonomy_id, | |
| 762 | - 'term_order' => $this->get_term_order( $parent_term->term_taxonomy_id, $object_id ), | |
| 763 | - ); | |
| 755 | + $terms[ $parent_term->term_id ] = $this->get_formatted_term( $parent_term, $object_id ); | |
| 764 | 756 | |
| 765 | - $terms[ $parent_term->term_id ]['facet'] = wp_json_encode( $terms[ $parent_term->term_id ] ); | |
| 766 | - | |
| 767 | 757 | } |
| 768 | 758 | return $this->get_parent_terms( $terms, $parent_term, $tax_name, $object_id ); |
| 769 | 759 | } |
| 770 | 760 | |
| 771 | 761 | /** |
| 772 | - * Retreives term order for the object/term_taxonomy_id combination | |
| 762 | + * Given a term, format it to be appended to the post ES document. | |
| 773 | 763 | * |
| 764 | + * @since 4.5.0 | |
| 765 | + * @param \WP_Term $term Term to be formatted | |
| 766 | + * @param int $post_id The post ID | |
| 767 | + * @return array | |
| 768 | + */ | |
| 769 | + private function get_formatted_term( \WP_Term $term, int $post_id ): array { | |
| 770 | + $formatted_term = [ | |
| 771 | + 'term_id' => $term->term_id, | |
| 772 | + 'slug' => $term->slug, | |
| 773 | + 'name' => $term->name, | |
| 774 | + 'parent' => $term->parent, | |
| 775 | + 'term_taxonomy_id' => $term->term_taxonomy_id, | |
| 776 | + 'term_order' => (int) $this->get_term_order( $term->term_taxonomy_id, $post_id ), | |
| 777 | + ]; | |
| 778 | + | |
| 779 | + /** | |
| 780 | + * As the name implies, the facet attribute is used to list all terms in facets. | |
| 781 | + * As in facets, the term_order associated with a post does not matter, we set it as 0 here. | |
| 782 | + * Note that this is set as 0 instead of simply removed to keep backward compatibility. | |
| 783 | + */ | |
| 784 | + $term_facet = $formatted_term; | |
| 785 | + $term_facet['term_order'] = 0; | |
| 786 | + $formatted_term['facet'] = wp_json_encode( $term_facet ); | |
| 787 | + | |
| 788 | + return $formatted_term; | |
| 789 | + } | |
| 790 | + | |
| 791 | + /** | |
| 792 | + * Retrieves term order for the object/term_taxonomy_id combination | |
| 793 | + * | |
| 774 | 794 | * @param int $term_taxonomy_id Term Taxonomy ID |
| 775 | 795 | * @param int $object_id Post ID |
| 776 | 796 | * |
| 777 | 797 | * @return int Term Order |
| @@ -782,9 +802,9 @@ | ||
| 782 | 802 | $cache_key = "{$object_id}_term_order"; |
| 783 | 803 | $term_orders = wp_cache_get( $cache_key ); |
| 784 | 804 | |
| 785 | 805 | if ( false === $term_orders ) { |
| 786 | - $results = $wpdb->get_results( | |
| 806 | + $results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 787 | 807 | $wpdb->prepare( |
| 788 | 808 | "SELECT term_taxonomy_id, term_order from $wpdb->term_relationships where object_id=%d;", |
| 789 | 809 | $object_id |
| 790 | 810 | ), |
| @@ -800,9 +820,8 @@ | ||
| 800 | 820 | wp_cache_set( $cache_key, $term_orders ); |
| 801 | 821 | } |
| 802 | 822 | |
| 803 | 823 | return isset( $term_orders[ $term_taxonomy_id ] ) ? (int) $term_orders[ $term_taxonomy_id ] : 0; |
| 804 | - | |
| 805 | 824 | } |
| 806 | 825 | |
| 807 | 826 | /** |
| 808 | 827 | * Checks if meta key is allowed |
| @@ -832,59 +851,15 @@ | ||
| 832 | 851 | */ |
| 833 | 852 | public function filter_allowed_metas( $metas, $post ) { |
| 834 | 853 | $filtered_metas = []; |
| 835 | 854 | |
| 836 | - /** | |
| 837 | - * Filter indexable protected meta keys for posts | |
| 838 | - * | |
| 839 | - * @hook ep_prepare_meta_allowed_protected_keys | |
| 840 | - * @param {array} $keys Allowed protected keys | |
| 841 | - * @param {WP_Post} $post Post object | |
| 842 | - * @since 1.7 | |
| 843 | - * @return {array} New keys | |
| 844 | - */ | |
| 845 | - $allowed_protected_keys = apply_filters( 'ep_prepare_meta_allowed_protected_keys', [], $post ); | |
| 855 | + $search = \ElasticPress\Features::factory()->get_registered_feature( 'search' ); | |
| 856 | + if ( $search && ! empty( $search->weighting ) && 'manual' === $search->weighting->get_meta_mode() ) { | |
| 857 | + $filtered_metas = $this->filter_allowed_metas_manual( $metas, $post ); | |
| 858 | + } else { | |
| 859 | + $filtered_metas = $this->filter_allowed_metas_auto( $metas, $post ); | |
| 860 | + } | |
| 846 | 861 | |
| 847 | - /** | |
| 848 | - * Filter public keys to exclude from indexed post | |
| 849 | - * | |
| 850 | - * @hook ep_prepare_meta_excluded_public_keys | |
| 851 | - * @param {array} $keys Excluded protected keys | |
| 852 | - * @param {WP_Post} $post Post object | |
| 853 | - * @since 1.7 | |
| 854 | - * @return {array} New keys | |
| 855 | - */ | |
| 856 | - $excluded_public_keys = apply_filters( 'ep_prepare_meta_excluded_public_keys', [], $post ); | |
| 857 | - | |
| 858 | - foreach ( $metas as $key => $value ) { | |
| 859 | - | |
| 860 | - $allow_index = false; | |
| 861 | - | |
| 862 | - if ( is_protected_meta( $key ) ) { | |
| 863 | - | |
| 864 | - if ( true === $allowed_protected_keys || in_array( $key, $allowed_protected_keys, true ) ) { | |
| 865 | - $allow_index = true; | |
| 866 | - } | |
| 867 | - } else { | |
| 868 | - | |
| 869 | - if ( true !== $excluded_public_keys && ! in_array( $key, $excluded_public_keys, true ) ) { | |
| 870 | - $allow_index = true; | |
| 871 | - } | |
| 872 | - } | |
| 873 | - | |
| 874 | - /** | |
| 875 | - * Filter force whitelisting a meta key | |
| 876 | - * | |
| 877 | - * @hook ep_prepare_meta_whitelist_key | |
| 878 | - * @param {bool} $whitelist True to whitelist key | |
| 879 | - * @param {string} $key Meta key | |
| 880 | - * @param {WP_Post} $post Post object | |
| 881 | - * @return {bool} New whitelist value | |
| 882 | - */ | |
| 883 | - if ( true === $allow_index || apply_filters( 'ep_prepare_meta_whitelist_key', false, $key, $post ) ) { | |
| 884 | - $filtered_metas[ $key ] = $value; | |
| 885 | - } | |
| 886 | - } | |
| 887 | 862 | return $filtered_metas; |
| 888 | 863 | } |
| 889 | 864 | |
| 890 | 865 | /** |
| @@ -921,9 +896,11 @@ | ||
| 921 | 896 | $filtered_metas = $this->filter_allowed_metas( $meta, $post ); |
| 922 | 897 | $prepared_meta = []; |
| 923 | 898 | |
| 924 | 899 | foreach ( $filtered_metas as $key => $value ) { |
| 925 | - $prepared_meta[ $key ] = maybe_unserialize( $value ); | |
| 900 | + if ( ! empty( $key ) ) { | |
| 901 | + $prepared_meta[ $key ] = maybe_unserialize( $value ); | |
| 902 | + } | |
| 926 | 903 | } |
| 927 | 904 | |
| 928 | 905 | /** |
| 929 | 906 | * Filter final list of prepared meta. |
| @@ -934,9 +911,8 @@ | ||
| 934 | 911 | * @since 3.4 |
| 935 | 912 | * @return {array} Prepared meta |
| 936 | 913 | */ |
| 937 | 914 | return apply_filters( 'ep_prepared_post_meta', $prepared_meta, $post ); |
| 938 | - | |
| 939 | 915 | } |
| 940 | 916 | |
| 941 | 917 | /** |
| 942 | 918 | * Format WP query args for ES |
| @@ -946,44 +922,711 @@ | ||
| 946 | 922 | * @since 0.9.0 |
| 947 | 923 | * @return array |
| 948 | 924 | */ |
| 949 | 925 | public function format_args( $args, $wp_query ) { |
| 950 | - if ( ! empty( $args['posts_per_page'] ) ) { | |
| 951 | - $posts_per_page = (int) $args['posts_per_page']; | |
| 926 | + $args = $this->sanitize_wp_query_args( $args ); | |
| 952 | 927 | |
| 953 | - // ES have a maximum size allowed so we have to convert "-1" to a maximum size. | |
| 954 | - if ( -1 === $posts_per_page ) { | |
| 955 | - /** | |
| 956 | - * Set the maximum results window size. | |
| 957 | - * | |
| 958 | - * The request will return a HTTP 500 Internal Error if the size of the | |
| 959 | - * request is larger than the [index.max_result_window] parameter in ES. | |
| 960 | - * See the scroll api for a more efficient way to request large data sets. | |
| 961 | - * | |
| 962 | - * @return int The max results window size. | |
| 963 | - * | |
| 964 | - * @since 2.3.0 | |
| 965 | - */ | |
| 928 | + $formatted_args = [ | |
| 929 | + 'from' => $this->parse_from( $args ), | |
| 930 | + 'size' => $this->parse_size( $args ), | |
| 931 | + ]; | |
| 966 | 932 | |
| 967 | - /** | |
| 968 | - * Filter max result size if set to -1 | |
| 969 | - * | |
| 970 | - * @hook ep_max_results_window | |
| 971 | - * @param {int} Max result window | |
| 972 | - * @return {int} New window | |
| 973 | - */ | |
| 974 | - $posts_per_page = apply_filters( 'ep_max_results_window', 10000 ); | |
| 933 | + $filters = $this->parse_filters( $args, $wp_query ); | |
| 934 | + | |
| 935 | + if ( ! empty( $filters ) ) { | |
| 936 | + $formatted_args['post_filter'] = $filters; | |
| 937 | + } | |
| 938 | + | |
| 939 | + $formatted_args = $this->maybe_set_search_fields( $formatted_args, $args ); | |
| 940 | + $formatted_args = $this->maybe_set_fields( $formatted_args, $args ); | |
| 941 | + $formatted_args = $this->maybe_orderby( $formatted_args, $args ); | |
| 942 | + $formatted_args = $this->maybe_add_sticky_posts( $formatted_args, $args ); | |
| 943 | + $formatted_args = $this->maybe_set_aggs( $formatted_args, $args, $filters ); | |
| 944 | + | |
| 945 | + /** | |
| 946 | + * Filter formatted Elasticsearch query (entire query) | |
| 947 | + * | |
| 948 | + * @hook ep_formatted_args | |
| 949 | + * @param {array} $formatted_args Formatted Elasticsearch query | |
| 950 | + * @param {array} $args WP_Query variables | |
| 951 | + * @param {object} $wp_query WP_Query object | |
| 952 | + * @return {array} New query | |
| 953 | + */ | |
| 954 | + $formatted_args = apply_filters( 'ep_formatted_args', $formatted_args, $args, $wp_query ); | |
| 955 | + | |
| 956 | + /** | |
| 957 | + * Filter formatted Elasticsearch post query (entire query) | |
| 958 | + * | |
| 959 | + * @hook ep_post_formatted_args | |
| 960 | + * @param {array} $formatted_args Formatted Elasticsearch query | |
| 961 | + * @param {array} $args WP_Query variables | |
| 962 | + * @param {object} $wp_query WP_Query object | |
| 963 | + * @return {array} New query | |
| 964 | + */ | |
| 965 | + $formatted_args = apply_filters( 'ep_post_formatted_args', $formatted_args, $args, $wp_query ); | |
| 966 | + | |
| 967 | + return $formatted_args; | |
| 968 | + } | |
| 969 | + | |
| 970 | + /** | |
| 971 | + * Adjust the fuzziness parameter if needed. | |
| 972 | + * | |
| 973 | + * If using fields with type `long`, queries should not have a fuzziness parameter. | |
| 974 | + * | |
| 975 | + * @param array $query Current query | |
| 976 | + * @param array $query_vars Query variables | |
| 977 | + * @param string $search_text Search text | |
| 978 | + * @param array $search_fields Search fields | |
| 979 | + * @return array New query | |
| 980 | + */ | |
| 981 | + public function adjust_query_fuzziness( $query, $query_vars, $search_text, $search_fields ) { | |
| 982 | + if ( empty( array_intersect( $search_fields, [ 'ID', 'post_id', 'post_parent' ] ) ) ) { | |
| 983 | + return $query; | |
| 984 | + } | |
| 985 | + | |
| 986 | + if ( ! isset( $query['bool'] ) || ! isset( $query['bool']['should'] ) ) { | |
| 987 | + return $query; | |
| 988 | + } | |
| 989 | + | |
| 990 | + foreach ( $query['bool']['should'] as &$clause ) { | |
| 991 | + if ( ! isset( $clause['multi_match'] ) ) { | |
| 992 | + continue; | |
| 975 | 993 | } |
| 994 | + | |
| 995 | + if ( isset( $clause['multi_match']['fuzziness'] ) ) { | |
| 996 | + unset( $clause['multi_match']['fuzziness'] ); | |
| 997 | + } | |
| 998 | + } | |
| 999 | + | |
| 1000 | + return $query; | |
| 1001 | + } | |
| 1002 | + | |
| 1003 | + /** | |
| 1004 | + * Parse and build out our tax query. | |
| 1005 | + * | |
| 1006 | + * @access protected | |
| 1007 | + * | |
| 1008 | + * @param array $query Tax query | |
| 1009 | + * @return array | |
| 1010 | + */ | |
| 1011 | + protected function parse_tax_query( $query ) { | |
| 1012 | + $tax_query = [ | |
| 1013 | + 'tax_filter' => [], | |
| 1014 | + 'tax_must_not_filter' => [], | |
| 1015 | + ]; | |
| 1016 | + $relation = ''; | |
| 1017 | + | |
| 1018 | + foreach ( $query as $tax_queries ) { | |
| 1019 | + // If we have a nested tax query, recurse through that | |
| 1020 | + if ( is_array( $tax_queries ) && empty( $tax_queries['taxonomy'] ) ) { | |
| 1021 | + $result = $this->parse_tax_query( $tax_queries ); | |
| 1022 | + $relation = ( ! empty( $tax_queries['relation'] ) ) ? strtolower( $tax_queries['relation'] ) : 'and'; | |
| 1023 | + $filter_type = 'and' === $relation ? 'must' : 'should'; | |
| 1024 | + | |
| 1025 | + // Set the proper filter type and must_not filter, as needed | |
| 1026 | + if ( ! empty( $result['tax_must_not_filter'] ) ) { | |
| 1027 | + $tax_query['tax_filter'][] = [ | |
| 1028 | + 'bool' => [ | |
| 1029 | + $filter_type => $result['tax_filter'], | |
| 1030 | + 'must_not' => $result['tax_must_not_filter'], | |
| 1031 | + ], | |
| 1032 | + ]; | |
| 1033 | + } else { | |
| 1034 | + $tax_query['tax_filter'][] = [ | |
| 1035 | + 'bool' => [ | |
| 1036 | + $filter_type => $result['tax_filter'], | |
| 1037 | + ], | |
| 1038 | + ]; | |
| 1039 | + } | |
| 1040 | + } | |
| 1041 | + | |
| 1042 | + // Parse each individual tax query part | |
| 1043 | + $single_tax_query = $tax_queries; | |
| 1044 | + if ( ! empty( $single_tax_query['taxonomy'] ) ) { | |
| 1045 | + $terms = isset( $single_tax_query['terms'] ) ? (array) $single_tax_query['terms'] : array(); | |
| 1046 | + $field = $this->parse_tax_query_field( $single_tax_query['field'] ); | |
| 1047 | + | |
| 1048 | + if ( 'slug' === $field ) { | |
| 1049 | + $terms = array_map( 'sanitize_title', $terms ); | |
| 1050 | + } | |
| 1051 | + | |
| 1052 | + // Set up our terms object | |
| 1053 | + $terms_obj = array( | |
| 1054 | + 'terms.' . $single_tax_query['taxonomy'] . '.' . $field => array_values( array_filter( $terms ) ), | |
| 1055 | + ); | |
| 1056 | + | |
| 1057 | + $operator = ( ! empty( $single_tax_query['operator'] ) ) ? strtolower( $single_tax_query['operator'] ) : 'in'; | |
| 1058 | + | |
| 1059 | + switch ( $operator ) { | |
| 1060 | + case 'exists': | |
| 1061 | + /** | |
| 1062 | + * add support for "EXISTS" operator | |
| 1063 | + * | |
| 1064 | + * @since 2.5 | |
| 1065 | + */ | |
| 1066 | + $tax_query['tax_filter'][]['bool'] = array( | |
| 1067 | + 'must' => array( | |
| 1068 | + array( | |
| 1069 | + 'exists' => array( | |
| 1070 | + 'field' => key( $terms_obj ), | |
| 1071 | + ), | |
| 1072 | + ), | |
| 1073 | + ), | |
| 1074 | + ); | |
| 1075 | + | |
| 1076 | + break; | |
| 1077 | + case 'not exists': | |
| 1078 | + /** | |
| 1079 | + * add support for "NOT EXISTS" operator | |
| 1080 | + * | |
| 1081 | + * @since 2.5 | |
| 1082 | + */ | |
| 1083 | + $tax_query['tax_filter'][]['bool'] = array( | |
| 1084 | + 'must_not' => array( | |
| 1085 | + array( | |
| 1086 | + 'exists' => array( | |
| 1087 | + 'field' => key( $terms_obj ), | |
| 1088 | + ), | |
| 1089 | + ), | |
| 1090 | + ), | |
| 1091 | + ); | |
| 1092 | + | |
| 1093 | + break; | |
| 1094 | + case 'not in': | |
| 1095 | + /** | |
| 1096 | + * add support for "NOT IN" operator | |
| 1097 | + * | |
| 1098 | + * @since 2.1 | |
| 1099 | + */ | |
| 1100 | + // If "NOT IN" than it should filter as must_not | |
| 1101 | + $tax_query['tax_must_not_filter'][]['terms'] = $terms_obj; | |
| 1102 | + | |
| 1103 | + break; | |
| 1104 | + case 'and': | |
| 1105 | + /** | |
| 1106 | + * add support for "and" operator | |
| 1107 | + * | |
| 1108 | + * @since 2.4 | |
| 1109 | + */ | |
| 1110 | + $and_nest = array( | |
| 1111 | + 'bool' => array( | |
| 1112 | + 'must' => array(), | |
| 1113 | + ), | |
| 1114 | + ); | |
| 1115 | + | |
| 1116 | + foreach ( $terms as $term ) { | |
| 1117 | + $and_nest['bool']['must'][] = array( | |
| 1118 | + 'terms' => array( | |
| 1119 | + 'terms.' . $single_tax_query['taxonomy'] . '.' . $field => (array) $term, | |
| 1120 | + ), | |
| 1121 | + ); | |
| 1122 | + } | |
| 1123 | + | |
| 1124 | + $tax_query['tax_filter'][] = $and_nest; | |
| 1125 | + | |
| 1126 | + break; | |
| 1127 | + case 'in': | |
| 1128 | + default: | |
| 1129 | + /** | |
| 1130 | + * Default to IN operator | |
| 1131 | + */ | |
| 1132 | + // Add the tax query filter | |
| 1133 | + $tax_query['tax_filter'][]['terms'] = $terms_obj; | |
| 1134 | + | |
| 1135 | + break; | |
| 1136 | + } | |
| 1137 | + } | |
| 1138 | + } | |
| 1139 | + | |
| 1140 | + return $tax_query; | |
| 1141 | + } | |
| 1142 | + | |
| 1143 | + /** | |
| 1144 | + * Parse an 'order' query variable and cast it to ASC or DESC as necessary. | |
| 1145 | + * | |
| 1146 | + * @since 1.1 | |
| 1147 | + * @access protected | |
| 1148 | + * | |
| 1149 | + * @param string $order The 'order' query variable. | |
| 1150 | + * @return string The sanitized 'order' query variable. | |
| 1151 | + */ | |
| 1152 | + protected function parse_order( $order ) { | |
| 1153 | + // Core will always set sort order to DESC for any invalid value, | |
| 1154 | + // so we can't do any automated testing of this function. | |
| 1155 | + // @codeCoverageIgnoreStart | |
| 1156 | + if ( ! is_string( $order ) || empty( $order ) ) { | |
| 1157 | + return 'desc'; | |
| 1158 | + } | |
| 1159 | + // @codeCoverageIgnoreEnd | |
| 1160 | + | |
| 1161 | + if ( 'ASC' === strtoupper( $order ) ) { | |
| 1162 | + return 'asc'; | |
| 976 | 1163 | } else { |
| 977 | - $posts_per_page = (int) get_option( 'posts_per_page' ); | |
| 1164 | + return 'desc'; | |
| 978 | 1165 | } |
| 1166 | + } | |
| 979 | 1167 | |
| 980 | - $formatted_args = array( | |
| 981 | - 'from' => 0, | |
| 982 | - 'size' => $posts_per_page, | |
| 983 | - ); | |
| 1168 | + /** | |
| 1169 | + * Convert the alias to a properly-prefixed sort value. | |
| 1170 | + * | |
| 1171 | + * @since 1.1 | |
| 1172 | + * @access protected | |
| 1173 | + * | |
| 1174 | + * @param string $orderbys Alias or path for the field to order by. | |
| 1175 | + * @param string $default_order Default order direction | |
| 1176 | + * @param array $args Query args | |
| 1177 | + * @return array | |
| 1178 | + */ | |
| 1179 | + protected function parse_orderby( $orderbys, $default_order, $args ) { | |
| 1180 | + $orderbys = $this->get_orderby_array( $orderbys ); | |
| 984 | 1181 | |
| 1182 | + $from_to = [ | |
| 1183 | + 'relevance' => '_score', | |
| 1184 | + 'date' => 'post_date', | |
| 1185 | + 'type' => 'post_type.raw', | |
| 1186 | + 'modified' => 'post_modified', | |
| 1187 | + 'name' => 'post_name.raw', | |
| 1188 | + 'title' => 'post_title.sortable', | |
| 1189 | + ]; | |
| 1190 | + | |
| 1191 | + $sort = []; | |
| 1192 | + | |
| 1193 | + foreach ( $orderbys as $key => $value ) { | |
| 1194 | + if ( is_string( $key ) ) { | |
| 1195 | + $orderby_clause = $key; | |
| 1196 | + $order = $value; | |
| 1197 | + } else { | |
| 1198 | + $orderby_clause = $value; | |
| 1199 | + $order = $default_order; | |
| 1200 | + } | |
| 1201 | + | |
| 1202 | + if ( empty( $orderby_clause ) || 'rand' === $orderby_clause || preg_match( '/^rand\((\d+)\)$/i', $orderby_clause ) ) { | |
| 1203 | + continue; | |
| 1204 | + } | |
| 1205 | + | |
| 1206 | + /** | |
| 1207 | + * If `orderby` is 'none', WordPress will let the database decide on what should be used to order. | |
| 1208 | + * It will use the primary key ASC. | |
| 1209 | + */ | |
| 1210 | + if ( 'none' === $orderby_clause ) { | |
| 1211 | + $orderby_clause = 'ID'; | |
| 1212 | + $order = 'asc'; | |
| 1213 | + } | |
| 1214 | + | |
| 1215 | + if ( ! empty( $from_to[ $orderby_clause ] ) ) { | |
| 1216 | + $orderby_clause = $from_to[ $orderby_clause ]; | |
| 1217 | + } else { | |
| 1218 | + $orderby_clause = $this->parse_orderby_meta_fields( $orderby_clause, $args ); | |
| 1219 | + } | |
| 1220 | + | |
| 1221 | + $sort[] = array( | |
| 1222 | + $orderby_clause => array( | |
| 1223 | + 'order' => $order, | |
| 1224 | + ), | |
| 1225 | + ); | |
| 1226 | + } | |
| 1227 | + | |
| 1228 | + return $sort; | |
| 1229 | + } | |
| 1230 | + | |
| 1231 | + /** | |
| 1232 | + * Try to parse orderby meta fields | |
| 1233 | + * | |
| 1234 | + * @since 4.6.0 | |
| 1235 | + * @param string $orderby_clause Current orderby value | |
| 1236 | + * @param array $args Query args | |
| 1237 | + * @return string New orderby value | |
| 1238 | + */ | |
| 1239 | + protected function parse_orderby_meta_fields( $orderby_clause, $args ) { | |
| 1240 | + global $wpdb; | |
| 1241 | + | |
| 1242 | + $from_to_metatypes = [ | |
| 1243 | + 'num' => 'long', | |
| 1244 | + 'numeric' => 'long', | |
| 1245 | + 'binary' => 'value.sortable', | |
| 1246 | + 'char' => 'value.sortable', | |
| 1247 | + 'date' => 'date', | |
| 1248 | + 'datetime' => 'datetime', | |
| 1249 | + 'decimal' => 'double', | |
| 1250 | + 'signed' => 'long', | |
| 1251 | + 'time' => 'time', | |
| 1252 | + 'unsigned' => 'long', | |
| 1253 | + ]; | |
| 1254 | + | |
| 1255 | + // Code is targeting Elasticsearch directly | |
| 1256 | + if ( preg_match( '/^meta\.(.*?)\.(.*)/', $orderby_clause, $match_meta ) ) { | |
| 1257 | + return $orderby_clause; | |
| 1258 | + } | |
| 1259 | + | |
| 1260 | + // WordPress meta_value_* compatibility | |
| 1261 | + if ( preg_match( '/^meta_value_?(.*)/', $orderby_clause, $match_type ) ) { | |
| 1262 | + $meta_type = $from_to_metatypes[ strtolower( $match_type[1] ) ] ?? 'value.sortable'; | |
| 1263 | + } | |
| 1264 | + | |
| 1265 | + if ( ! empty( $args['meta_key'] ) ) { | |
| 1266 | + $meta_field = $args['meta_key']; | |
| 1267 | + } | |
| 1268 | + | |
| 1269 | + // Already have everything needed | |
| 1270 | + if ( isset( $meta_type ) && isset( $meta_field ) ) { | |
| 1271 | + return "meta.{$meta_field}.{$meta_type}"; | |
| 1272 | + } | |
| 1273 | + | |
| 1274 | + // Don't have any other ways to guess | |
| 1275 | + if ( empty( $args['meta_query'] ) ) { | |
| 1276 | + return $orderby_clause; | |
| 1277 | + } | |
| 1278 | + | |
| 1279 | + $meta_query = new \WP_Meta_Query( $args['meta_query'] ); | |
| 1280 | + // Calling get_sql() to populate the WP_Meta_Query->clauses attribute | |
| 1281 | + $meta_query->get_sql( 'post', $wpdb->posts, 'ID' ); | |
| 1282 | + | |
| 1283 | + $clauses = $meta_query->get_clauses(); | |
| 1284 | + | |
| 1285 | + // If it refers to a named meta_query clause | |
| 1286 | + if ( ! empty( $clauses[ $orderby_clause ] ) ) { | |
| 1287 | + $meta_field = $clauses[ $orderby_clause ]['key']; | |
| 1288 | + $clause_meta_type = strtolower( $clauses[ $orderby_clause ]['type'] ?? $clauses[ $orderby_clause ]['cast'] ); | |
| 1289 | + } else { | |
| 1290 | + /** | |
| 1291 | + * At this point we: | |
| 1292 | + * 1. Try to find the meta key in any meta_query clause and use the type WP found | |
| 1293 | + * 2. If ordering by `meta_value*`, use the first meta_query clause | |
| 1294 | + * 3. Give up and use the orderby clause as is (code could be capturing it later on) | |
| 1295 | + */ | |
| 1296 | + $meta_keys_and_types = wp_list_pluck( $clauses, 'cast', 'key' ); | |
| 1297 | + if ( isset( $meta_keys_and_types[ $orderby_clause ] ) ) { | |
| 1298 | + $meta_field = $orderby_clause; | |
| 1299 | + $clause_meta_type = strtolower( $meta_keys_and_types[ $orderby_clause ] ?? $meta_keys_and_types[ $orderby_clause ] ); | |
| 1300 | + } elseif ( isset( $meta_type ) ) { | |
| 1301 | + $primary_clause = reset( $clauses ); | |
| 1302 | + $meta_field = $primary_clause['key']; | |
| 1303 | + } else { | |
| 1304 | + unset( $meta_type ); | |
| 1305 | + unset( $meta_field ); | |
| 1306 | + } | |
| 1307 | + } | |
| 1308 | + | |
| 1309 | + if ( ! isset( $meta_type ) && isset( $clause_meta_type ) ) { | |
| 1310 | + $meta_type = $from_to_metatypes[ $clause_meta_type ] ?? 'value.sortable'; | |
| 1311 | + } | |
| 1312 | + | |
| 1313 | + if ( isset( $meta_type ) && isset( $meta_field ) ) { | |
| 1314 | + $orderby_clause = "meta.{$meta_field}.{$meta_type}"; | |
| 1315 | + } | |
| 1316 | + | |
| 1317 | + return $orderby_clause; | |
| 1318 | + } | |
| 1319 | + | |
| 1320 | + /** | |
| 1321 | + * Get Order by args Array | |
| 1322 | + * | |
| 1323 | + * @param string|array $orderbys Order by string or array | |
| 1324 | + * @since 2.1 | |
| 1325 | + * @return array | |
| 1326 | + */ | |
| 1327 | + protected function get_orderby_array( $orderbys ) { | |
| 1328 | + if ( ! is_array( $orderbys ) ) { | |
| 1329 | + $orderbys = explode( ' ', $orderbys ); | |
| 1330 | + } | |
| 1331 | + | |
| 1332 | + return $orderbys; | |
| 1333 | + } | |
| 1334 | + | |
| 1335 | + /** | |
| 1336 | + * Given a mapping content, try to determine the version used. | |
| 1337 | + * | |
| 1338 | + * @since 3.6.3 | |
| 1339 | + * | |
| 1340 | + * @param array $mapping Mapping content. | |
| 1341 | + * @param string $index Index name | |
| 1342 | + * @return string Version of the mapping being used. | |
| 1343 | + */ | |
| 1344 | + protected function determine_mapping_version_based_on_existing( $mapping, $index ) { | |
| 1345 | + if ( isset( $mapping[ $index ]['mappings']['post']['_meta']['mapping_version'] ) ) { | |
| 1346 | + return $mapping[ $index ]['mappings']['post']['_meta']['mapping_version']; | |
| 1347 | + } | |
| 1348 | + if ( isset( $mapping[ $index ]['mappings']['_meta']['mapping_version'] ) ) { | |
| 1349 | + return $mapping[ $index ]['mappings']['_meta']['mapping_version']; | |
| 1350 | + } | |
| 1351 | + | |
| 985 | 1352 | /** |
| 1353 | + * Check for 7-0 mapping. | |
| 1354 | + * If mapping has a `post` type, it can't be ES 7, as mapping types were removed in that release. | |
| 1355 | + * | |
| 1356 | + * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html | |
| 1357 | + */ | |
| 1358 | + if ( ! isset( $mapping[ $index ]['mappings']['post'] ) ) { | |
| 1359 | + return '7-0.php'; | |
| 1360 | + } | |
| 1361 | + | |
| 1362 | + $post_mapping = $mapping[ $index ]['mappings']['post']; | |
| 1363 | + | |
| 1364 | + /** | |
| 1365 | + * Starting at this point, our tests rely on the post_title.fields.sortable field. | |
| 1366 | + * As this field is present in all our mappings, if this field is not present in | |
| 1367 | + * the mapping, this is a custom mapping. | |
| 1368 | + * | |
| 1369 | + * To have this code working with custom mappings, use the `ep_post_mapping_version_determined` filter. | |
| 1370 | + */ | |
| 1371 | + if ( ! isset( $post_mapping['properties']['post_title']['fields']['sortable'] ) ) { | |
| 1372 | + return 'unknown'; | |
| 1373 | + } | |
| 1374 | + | |
| 1375 | + $post_title_sortable = $post_mapping['properties']['post_title']['fields']['sortable']; | |
| 1376 | + | |
| 1377 | + /** | |
| 1378 | + * Check for 5-2 mapping. | |
| 1379 | + * Normalizers on keyword fields were only made available in ES 5.2 | |
| 1380 | + * | |
| 1381 | + * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.2/release-notes-5.2.0.html | |
| 1382 | + */ | |
| 1383 | + if ( isset( $post_title_sortable['normalizer'] ) ) { | |
| 1384 | + return '5-2.php'; | |
| 1385 | + } | |
| 1386 | + | |
| 1387 | + return 'unknown'; | |
| 1388 | + } | |
| 1389 | + | |
| 1390 | + /** | |
| 1391 | + * Given ES args, add aggregations to it. | |
| 1392 | + * | |
| 1393 | + * @since 4.1.0 | |
| 1394 | + * @param array $formatted_args Formatted Elasticsearch query | |
| 1395 | + * @param array $agg Aggregation data. | |
| 1396 | + * @param boolean $use_filters Whether filters should be used or not. | |
| 1397 | + * @param array $filter Filters defined so far. | |
| 1398 | + * @return array Formatted Elasticsearch query with the aggregation added. | |
| 1399 | + */ | |
| 1400 | + protected function apply_aggregations( $formatted_args, $agg, $use_filters, $filter ) { | |
| 1401 | + if ( empty( $agg['aggs'] ) ) { | |
| 1402 | + return $formatted_args; | |
| 1403 | + } | |
| 1404 | + | |
| 1405 | + // Add a name to the aggregation if it was passed through | |
| 1406 | + $agg_name = ( ! empty( $agg['name'] ) ) ? $agg['name'] : 'aggregation_name'; | |
| 1407 | + | |
| 1408 | + // Add/use the filter if warranted | |
| 1409 | + if ( isset( $agg['use-filter'] ) && false !== $agg['use-filter'] && $use_filters ) { | |
| 1410 | + | |
| 1411 | + // If a filter is being used, use it on the aggregation as well to receive relevant information to the query | |
| 1412 | + $formatted_args['aggs'][ $agg_name ]['filter'] = $filter; | |
| 1413 | + $formatted_args['aggs'][ $agg_name ]['aggs'] = $agg['aggs']; | |
| 1414 | + } else { | |
| 1415 | + $formatted_args['aggs'][ $agg_name ] = $agg['aggs']; | |
| 1416 | + } | |
| 1417 | + | |
| 1418 | + return $formatted_args; | |
| 1419 | + } | |
| 1420 | + | |
| 1421 | + /** | |
| 1422 | + * Get the search algorithm that should be used. | |
| 1423 | + * | |
| 1424 | + * @since 4.3.0 | |
| 1425 | + * @param string $search_text Search term(s) | |
| 1426 | + * @param array $search_fields Search fields | |
| 1427 | + * @param array $query_vars Query vars | |
| 1428 | + * @return SearchAlgorithm Instance of search algorithm to be used | |
| 1429 | + */ | |
| 1430 | + public function get_search_algorithm( string $search_text, array $search_fields, array $query_vars ): \ElasticPress\SearchAlgorithm { | |
| 1431 | + $search_algorithm_version_option = \ElasticPress\Utils\get_option( 'ep_search_algorithm_version', '4.0' ); | |
| 1432 | + | |
| 1433 | + /** | |
| 1434 | + * Filter the algorithm version to be used. | |
| 1435 | + * | |
| 1436 | + * @since 3.5 | |
| 1437 | + * @hook ep_search_algorithm_version | |
| 1438 | + * @param {string} $search_algorithm_version Algorithm version. | |
| 1439 | + * @return {string} New algorithm version | |
| 1440 | + */ | |
| 1441 | + $search_algorithm = apply_filters( 'ep_search_algorithm_version', $search_algorithm_version_option ); | |
| 1442 | + | |
| 1443 | + /** | |
| 1444 | + * Filter the search algorithm to be used | |
| 1445 | + * | |
| 1446 | + * @hook ep_{$indexable_slug}_search_algorithm | |
| 1447 | + * @since 4.3.0 | |
| 1448 | + * @param {string} $search_algorithm Slug of the search algorithm used as fallback | |
| 1449 | + * @param {string} $search_term Search term | |
| 1450 | + * @param {array} $search_fields Fields to be searched | |
| 1451 | + * @param {array} $query_vars Query variables | |
| 1452 | + * @return {string} New search algorithm slug | |
| 1453 | + */ | |
| 1454 | + $search_algorithm = apply_filters( "ep_{$this->slug}_search_algorithm", $search_algorithm, $search_text, $search_fields, $query_vars ); | |
| 1455 | + | |
| 1456 | + return \ElasticPress\SearchAlgorithms::factory()->get( $search_algorithm ); | |
| 1457 | + } | |
| 1458 | + | |
| 1459 | + /** | |
| 1460 | + * Based on WP_Query arguments, parses the various filters that could be applied into the ES query. | |
| 1461 | + * | |
| 1462 | + * @since 4.4.0 | |
| 1463 | + * @param array $args WP_Query arguments | |
| 1464 | + * @param WP_Query $query WP_Query object | |
| 1465 | + * @return array | |
| 1466 | + */ | |
| 1467 | + protected function parse_filters( $args, $query ) { | |
| 1468 | + /** | |
| 1469 | + * A note about the order of this array indices: | |
| 1470 | + * As previously there was no way to access each part, some snippets might be accessing | |
| 1471 | + * these filters by its usual numeric indices (see the array_values() call below.) | |
| 1472 | + */ | |
| 1473 | + $filters = [ | |
| 1474 | + 'tax_query' => $this->parse_tax_queries( $args, $query ), | |
| 1475 | + 'post_parent' => $this->parse_post_parent( $args ), | |
| 1476 | + 'post_parent__in' => $this->parse_post_parent__in( $args ), | |
| 1477 | + 'post_parent__not_in' => $this->parse_post_parent__not_in( $args ), | |
| 1478 | + 'post__in' => $this->parse_post__in( $args ), | |
| 1479 | + 'post_name__in' => $this->parse_post_name__in( $args ), | |
| 1480 | + 'post__not_in' => $this->parse_post__not_in( $args ), | |
| 1481 | + 'category__not_in' => $this->parse_category__not_in( $args ), | |
| 1482 | + 'tag__not_in' => $this->parse_tag__not_in( $args ), | |
| 1483 | + 'author' => $this->parse_author( $args ), | |
| 1484 | + 'post_mime_type' => $this->parse_post_mime_type( $args ), | |
| 1485 | + 'date' => $this->parse_date( $args ), | |
| 1486 | + 'meta_query' => $this->parse_meta_queries( $args ), | |
| 1487 | + 'post_type' => $this->parse_post_type( $args ), | |
| 1488 | + 'post_status' => $this->parse_post_status( $args ), | |
| 1489 | + 'painless_script' => $this->painless_script_query( $args ), | |
| 1490 | + ]; | |
| 1491 | + | |
| 1492 | + /** | |
| 1493 | + * Filter the ES filters that will be applied to the ES query. | |
| 1494 | + * | |
| 1495 | + * Although each index of the `$filters` array contains the related WP Query argument, | |
| 1496 | + * it will be removed before applied to the ES query. | |
| 1497 | + * | |
| 1498 | + * @hook ep_post_filters | |
| 1499 | + * @param {array} Current filters | |
| 1500 | + * @param {array} WP Query args | |
| 1501 | + * @param {WP_Query} WP Query object | |
| 1502 | + * @return {array} New filters | |
| 1503 | + */ | |
| 1504 | + $filters = apply_filters( 'ep_post_filters', $filters, $args, $query ); | |
| 1505 | + | |
| 1506 | + $filters = array_values( array_filter( $filters ) ); | |
| 1507 | + | |
| 1508 | + if ( ! empty( $filters ) ) { | |
| 1509 | + $filters = [ | |
| 1510 | + 'bool' => [ | |
| 1511 | + 'must' => $filters, | |
| 1512 | + ], | |
| 1513 | + ]; | |
| 1514 | + } | |
| 1515 | + | |
| 1516 | + return $filters; | |
| 1517 | + } | |
| 1518 | + | |
| 1519 | + /** | |
| 1520 | + * Sanitize WP_Query arguments to be used to create the ES query. | |
| 1521 | + * | |
| 1522 | + * Elasticsearch will error if a terms query contains empty items like an empty string. | |
| 1523 | + * | |
| 1524 | + * @since 4.4.0 | |
| 1525 | + * @param array $args WP_Query arguments | |
| 1526 | + * @return array | |
| 1527 | + */ | |
| 1528 | + protected function sanitize_wp_query_args( $args ) { | |
| 1529 | + $keys_to_sanitize = [ | |
| 1530 | + 'author__in', | |
| 1531 | + 'author__not_in', | |
| 1532 | + 'category__and', | |
| 1533 | + 'category__in', | |
| 1534 | + 'category__not_in', | |
| 1535 | + 'tag__and', | |
| 1536 | + 'tag__in', | |
| 1537 | + 'tag__not_in', | |
| 1538 | + 'tag_slug__and', | |
| 1539 | + 'tag_slug__in', | |
| 1540 | + 'post_parent__in', | |
| 1541 | + 'post_parent__not_in', | |
| 1542 | + 'post__in', | |
| 1543 | + 'post__not_in', | |
| 1544 | + 'post_name__in', | |
| 1545 | + ]; | |
| 1546 | + foreach ( $keys_to_sanitize as $key ) { | |
| 1547 | + if ( ! isset( $args[ $key ] ) ) { | |
| 1548 | + continue; | |
| 1549 | + } | |
| 1550 | + $args[ $key ] = array_filter( (array) $args[ $key ] ); | |
| 1551 | + } | |
| 1552 | + | |
| 1553 | + return $args; | |
| 1554 | + } | |
| 1555 | + | |
| 1556 | + /** | |
| 1557 | + * Parse the `from` clause of the ES Query. | |
| 1558 | + * | |
| 1559 | + * @since 4.4.0 | |
| 1560 | + * @param array $args WP_Query arguments | |
| 1561 | + * @return int | |
| 1562 | + */ | |
| 1563 | + protected function parse_from( $args ) { | |
| 1564 | + $from = 0; | |
| 1565 | + | |
| 1566 | + if ( isset( $args['offset'] ) ) { | |
| 1567 | + $from = (int) $args['offset']; | |
| 1568 | + } | |
| 1569 | + | |
| 1570 | + if ( isset( $args['paged'] ) && $args['paged'] > 1 ) { | |
| 1571 | + $from = $args['posts_per_page'] * ( $args['paged'] - 1 ); | |
| 1572 | + } | |
| 1573 | + | |
| 1574 | + /** | |
| 1575 | + * Fix negative offset. This happens, for example, on hierarchical post types. | |
| 1576 | + * | |
| 1577 | + * Ref: https://github.com/10up/ElasticPress/issues/2480 | |
| 1578 | + */ | |
| 1579 | + if ( $from < 0 ) { | |
| 1580 | + $from = 0; | |
| 1581 | + } | |
| 1582 | + | |
| 1583 | + return $from; | |
| 1584 | + } | |
| 1585 | + | |
| 1586 | + /** | |
| 1587 | + * Parse the `size` clause of the ES Query. | |
| 1588 | + * | |
| 1589 | + * @since 4.4.0 | |
| 1590 | + * @param array $args WP_Query arguments | |
| 1591 | + * @return int | |
| 1592 | + */ | |
| 1593 | + protected function parse_size( $args ) { | |
| 1594 | + if ( empty( $args['posts_per_page'] ) ) { | |
| 1595 | + return (int) get_option( 'posts_per_page' ); | |
| 1596 | + } | |
| 1597 | + | |
| 1598 | + $posts_per_page = (int) $args['posts_per_page']; | |
| 1599 | + | |
| 1600 | + // ES have a maximum size allowed so we have to convert "-1" to a maximum size. | |
| 1601 | + if ( -1 === $posts_per_page ) { | |
| 1602 | + /** | |
| 1603 | + * Filter max result size if set to -1 | |
| 1604 | + * | |
| 1605 | + * The request will return a HTTP 500 Internal Error if the size of the | |
| 1606 | + * request is larger than the [index.max_result_window] parameter in ES. | |
| 1607 | + * See the scroll api for a more efficient way to request large data sets. | |
| 1608 | + * | |
| 1609 | + * @hook ep_max_results_window | |
| 1610 | + * @param {int} Max result window | |
| 1611 | + * @return {int} New window | |
| 1612 | + */ | |
| 1613 | + $posts_per_page = apply_filters( 'ep_max_results_window', 10000 ); | |
| 1614 | + } | |
| 1615 | + | |
| 1616 | + return $posts_per_page; | |
| 1617 | + } | |
| 1618 | + | |
| 1619 | + /** | |
| 1620 | + * Parse the order of results in the ES query. It could simply be a `sort` clause or a function score query if using RAND. | |
| 1621 | + * | |
| 1622 | + * @since 4.4.0 | |
| 1623 | + * @param array $formatted_args Formatted Elasticsearch query | |
| 1624 | + * @param array $args WP_Query arguments | |
| 1625 | + * @return array | |
| 1626 | + */ | |
| 1627 | + protected function maybe_orderby( $formatted_args, $args ) { | |
| 1628 | + /** | |
| 986 | 1629 | * Order and Orderby arguments |
| 987 | 1630 | * |
| 988 | 1631 | * Used for how Elasticsearch will sort results |
| 989 | 1632 | * |
| @@ -1047,41 +1690,51 @@ | ||
| 1047 | 1690 | |
| 1048 | 1691 | $formatted_args['sort'] = $default_sort; |
| 1049 | 1692 | } |
| 1050 | 1693 | |
| 1051 | - $filter = array( | |
| 1052 | - 'bool' => array( | |
| 1053 | - 'must' => [], | |
| 1054 | - ), | |
| 1055 | - ); | |
| 1056 | - $use_filters = false; | |
| 1694 | + /** | |
| 1695 | + * Order by 'rand' and 'Rand(x)' support | |
| 1696 | + * | |
| 1697 | + * Ref: https://github.com/elastic/elasticsearch/issues/1170 | |
| 1698 | + */ | |
| 1699 | + if ( ! empty( $args['orderby'] ) ) { | |
| 1700 | + $orderbys = $this->get_orderby_array( $args['orderby'] ); | |
| 1701 | + $random_orderbys = preg_grep( '/^rand(?:\((\d+)\))?$/i', $orderbys ); | |
| 1057 | 1702 | |
| 1058 | - // Sanitize array query args. Elasticsearch will error if a terms query contains empty items like an | |
| 1059 | - // empty string. | |
| 1060 | - $keys_to_sanitize = [ | |
| 1061 | - 'author__in', | |
| 1062 | - 'author__not_in', | |
| 1063 | - 'category__and', | |
| 1064 | - 'category__in', | |
| 1065 | - 'category__not_in', | |
| 1066 | - 'tag__and', | |
| 1067 | - 'tag__in', | |
| 1068 | - 'tag__not_in', | |
| 1069 | - 'tag_slug__and', | |
| 1070 | - 'tag_slug__in', | |
| 1071 | - 'post_parent__in', | |
| 1072 | - 'post_parent__not_in', | |
| 1073 | - 'post__in', | |
| 1074 | - 'post__not_in', | |
| 1075 | - 'post_name__in', | |
| 1076 | - ]; | |
| 1077 | - foreach ( $keys_to_sanitize as $key ) { | |
| 1078 | - if ( ! isset( $args[ $key ] ) ) { | |
| 1079 | - continue; | |
| 1703 | + if ( ! empty( $random_orderbys ) ) { | |
| 1704 | + $formatted_args_query = $formatted_args['query']; | |
| 1705 | + $formatted_args['query'] = []; | |
| 1706 | + $formatted_args['query']['function_score']['query'] = $formatted_args_query; | |
| 1707 | + | |
| 1708 | + // Get the first random orderby found. | |
| 1709 | + $random_orderby = reset( $random_orderbys ); | |
| 1710 | + | |
| 1711 | + // check if rand with seed. | |
| 1712 | + if ( preg_match( '/^rand\((\d+)\)$/i', $random_orderby, $matches ) ) { | |
| 1713 | + $formatted_args['query']['function_score']['random_score'] = (object) [ | |
| 1714 | + 'seed' => (int) $matches[1], | |
| 1715 | + 'field' => '_seq_no', | |
| 1716 | + ]; | |
| 1717 | + } else { | |
| 1718 | + $formatted_args['query']['function_score']['random_score'] = (object) []; | |
| 1719 | + } | |
| 1080 | 1720 | } |
| 1081 | - $args[ $key ] = array_filter( (array) $args[ $key ] ); | |
| 1082 | 1721 | } |
| 1083 | 1722 | |
| 1723 | + return $formatted_args; | |
| 1724 | + } | |
| 1725 | + | |
| 1726 | + /** | |
| 1727 | + * Parse all taxonomy queries. | |
| 1728 | + * | |
| 1729 | + * Although the name may be misleading, it handles the `tax_query` argument. There is a `parse_tax_query` that handles each "small" query. | |
| 1730 | + * | |
| 1731 | + * @since 4.4.0 | |
| 1732 | + * @param array $args WP_Query arguments | |
| 1733 | + * @param WP_Query $query WP_Query object | |
| 1734 | + * @return array | |
| 1735 | + */ | |
| 1736 | + protected function parse_tax_queries( $args, $query ) { | |
| 1084 | 1737 | /** |
| 1085 | 1738 | * Tax Query support |
| 1086 | 1739 | * |
| 1087 | 1740 | * Support for the tax_query argument of WP_Query. Currently only provides support for the 'AND' relation |
| @@ -1090,232 +1743,350 @@ | ||
| 1090 | 1743 | * @use field = slug |
| 1091 | 1744 | * terms array |
| 1092 | 1745 | * @since 0.9.1 |
| 1093 | 1746 | */ |
| 1094 | - if ( ! empty( $wp_query->tax_query ) && ! empty( $wp_query->tax_query->queries ) ) { | |
| 1095 | - $args['tax_query'] = $wp_query->tax_query->queries; | |
| 1747 | + if ( ! empty( $query->tax_query ) && ! empty( $query->tax_query->queries ) ) { | |
| 1748 | + $args['tax_query'] = $query->tax_query->queries; | |
| 1096 | 1749 | } |
| 1097 | 1750 | |
| 1098 | - if ( ! empty( $args['tax_query'] ) ) { | |
| 1099 | - // Main tax_query array for ES. | |
| 1100 | - $es_tax_query = []; | |
| 1751 | + if ( empty( $args['tax_query'] ) ) { | |
| 1752 | + return []; | |
| 1753 | + } | |
| 1101 | 1754 | |
| 1102 | - $tax_queries = $this->parse_tax_query( $args['tax_query'] ); | |
| 1755 | + // Main tax_query array for ES. | |
| 1756 | + $es_tax_query = []; | |
| 1103 | 1757 | |
| 1104 | - if ( ! empty( $tax_queries['tax_filter'] ) ) { | |
| 1105 | - $relation = 'must'; | |
| 1758 | + $tax_queries = $this->parse_tax_query( $args['tax_query'] ); | |
| 1106 | 1759 | |
| 1107 | - if ( ! empty( $args['tax_query']['relation'] ) && 'or' === strtolower( $args['tax_query']['relation'] ) ) { | |
| 1108 | - $relation = 'should'; | |
| 1109 | - } | |
| 1760 | + if ( ! empty( $tax_queries['tax_filter'] ) ) { | |
| 1761 | + $relation = 'must'; | |
| 1110 | 1762 | |
| 1111 | - $es_tax_query[ $relation ] = $tax_queries['tax_filter']; | |
| 1763 | + if ( ! empty( $args['tax_query']['relation'] ) && 'or' === strtolower( $args['tax_query']['relation'] ) ) { | |
| 1764 | + $relation = 'should'; | |
| 1112 | 1765 | } |
| 1113 | 1766 | |
| 1114 | - if ( ! empty( $tax_queries['tax_must_not_filter'] ) ) { | |
| 1115 | - $es_tax_query['must_not'] = $tax_queries['tax_must_not_filter']; | |
| 1116 | - } | |
| 1767 | + $es_tax_query[ $relation ] = $tax_queries['tax_filter']; | |
| 1768 | + } | |
| 1117 | 1769 | |
| 1118 | - if ( ! empty( $es_tax_query ) ) { | |
| 1119 | - $filter['bool']['must'][]['bool'] = $es_tax_query; | |
| 1120 | - } | |
| 1770 | + if ( ! empty( $tax_queries['tax_must_not_filter'] ) ) { | |
| 1771 | + $es_tax_query['must_not'] = $tax_queries['tax_must_not_filter']; | |
| 1772 | + } | |
| 1121 | 1773 | |
| 1122 | - $use_filters = true; | |
| 1774 | + if ( ! empty( $es_tax_query ) ) { | |
| 1775 | + return [ 'bool' => $es_tax_query ]; | |
| 1123 | 1776 | } |
| 1124 | 1777 | |
| 1125 | - /** | |
| 1126 | - * 'post_parent' arg support. | |
| 1127 | - * | |
| 1128 | - * @since 2.0 | |
| 1129 | - */ | |
| 1130 | - if ( isset( $args['post_parent'] ) && '' !== $args['post_parent'] && 'any' !== strtolower( $args['post_parent'] ) ) { | |
| 1131 | - $filter['bool']['must'][]['bool']['must'] = array( | |
| 1132 | - 'term' => array( | |
| 1133 | - 'post_parent' => $args['post_parent'], | |
| 1134 | - ), | |
| 1135 | - ); | |
| 1778 | + return []; | |
| 1779 | + } | |
| 1136 | 1780 | |
| 1137 | - $use_filters = true; | |
| 1781 | + /** | |
| 1782 | + * Parse the `post_parent` WP Query arg and transform it into an ES query clause. | |
| 1783 | + * | |
| 1784 | + * @since 4.4.0 | |
| 1785 | + * @param array $args WP_Query arguments | |
| 1786 | + * @return array | |
| 1787 | + */ | |
| 1788 | + protected function parse_post_parent( $args ) { | |
| 1789 | + $has_post_parent = isset( $args['post_parent'] ) && ( in_array( $args['post_parent'], [ 0, '0' ], true ) || ! empty( $args['post_parent'] ) ); | |
| 1790 | + if ( ! $has_post_parent || 'any' === strtolower( $args['post_parent'] ) ) { | |
| 1791 | + return []; | |
| 1138 | 1792 | } |
| 1139 | 1793 | |
| 1140 | - /** | |
| 1141 | - * 'post__in' arg support. | |
| 1142 | - * | |
| 1143 | - * @since x.x | |
| 1144 | - */ | |
| 1145 | - if ( ! empty( $args['post__in'] ) ) { | |
| 1146 | - $filter['bool']['must'][]['bool']['must'] = array( | |
| 1147 | - 'terms' => array( | |
| 1148 | - 'post_id' => array_values( (array) $args['post__in'] ), | |
| 1149 | - ), | |
| 1150 | - ); | |
| 1794 | + return [ | |
| 1795 | + 'bool' => [ | |
| 1796 | + 'must' => [ | |
| 1797 | + 'term' => [ | |
| 1798 | + 'post_parent' => (int) $args['post_parent'], | |
| 1799 | + ], | |
| 1800 | + ], | |
| 1801 | + ], | |
| 1802 | + ]; | |
| 1803 | + } | |
| 1151 | 1804 | |
| 1152 | - $use_filters = true; | |
| 1805 | + /** | |
| 1806 | + * Parse the `post_parent__in` WP Query arg and transform it into an ES query clause. | |
| 1807 | + * | |
| 1808 | + * @since 4.5.0 | |
| 1809 | + * @param array $args WP_Query arguments | |
| 1810 | + * @return array | |
| 1811 | + */ | |
| 1812 | + protected function parse_post_parent__in( $args ) { | |
| 1813 | + if ( empty( $args['post_parent__in'] ) ) { | |
| 1814 | + return []; | |
| 1153 | 1815 | } |
| 1154 | 1816 | |
| 1155 | - /** | |
| 1156 | - * 'post_name__in' arg support. | |
| 1157 | - * | |
| 1158 | - * @since 3.6.0 | |
| 1159 | - */ | |
| 1160 | - if ( ! empty( $args['post_name__in'] ) ) { | |
| 1161 | - $filter['bool']['must'][]['bool']['must'] = array( | |
| 1162 | - 'terms' => array( | |
| 1163 | - 'post_name.raw' => array_values( (array) $args['post_name__in'] ), | |
| 1164 | - ), | |
| 1165 | - ); | |
| 1817 | + return [ | |
| 1818 | + 'bool' => [ | |
| 1819 | + 'must' => [ | |
| 1820 | + 'terms' => [ | |
| 1821 | + 'post_parent' => array_values( (array) $args['post_parent__in'] ), | |
| 1822 | + ], | |
| 1823 | + ], | |
| 1824 | + ], | |
| 1825 | + ]; | |
| 1826 | + } | |
| 1166 | 1827 | |
| 1167 | - $use_filters = true; | |
| 1828 | + /** | |
| 1829 | + * Parse the `post_parent__not_in` WP Query arg and transform it into an ES query clause. | |
| 1830 | + * | |
| 1831 | + * @since 4.5.0 | |
| 1832 | + * @param array $args WP_Query arguments | |
| 1833 | + * @return array | |
| 1834 | + */ | |
| 1835 | + protected function parse_post_parent__not_in( $args ) { | |
| 1836 | + if ( empty( $args['post_parent__not_in'] ) ) { | |
| 1837 | + return []; | |
| 1168 | 1838 | } |
| 1169 | 1839 | |
| 1170 | - /** | |
| 1171 | - * 'post__not_in' arg support. | |
| 1172 | - * | |
| 1173 | - * @since x.x | |
| 1174 | - */ | |
| 1175 | - if ( ! empty( $args['post__not_in'] ) ) { | |
| 1176 | - $filter['bool']['must'][]['bool']['must_not'] = array( | |
| 1177 | - 'terms' => array( | |
| 1178 | - 'post_id' => (array) $args['post__not_in'], | |
| 1179 | - ), | |
| 1180 | - ); | |
| 1840 | + return [ | |
| 1841 | + 'bool' => [ | |
| 1842 | + 'must_not' => [ | |
| 1843 | + 'terms' => [ | |
| 1844 | + 'post_parent' => array_values( (array) $args['post_parent__not_in'] ), | |
| 1845 | + ], | |
| 1846 | + ], | |
| 1847 | + ], | |
| 1848 | + ]; | |
| 1849 | + } | |
| 1181 | 1850 | |
| 1182 | - $use_filters = true; | |
| 1851 | + /** | |
| 1852 | + * Parse the `post__in` WP Query arg and transform it into an ES query clause. | |
| 1853 | + * | |
| 1854 | + * @since 4.4.0 | |
| 1855 | + * @param array $args WP_Query arguments | |
| 1856 | + * @return array | |
| 1857 | + */ | |
| 1858 | + protected function parse_post__in( $args ) { | |
| 1859 | + if ( empty( $args['post__in'] ) ) { | |
| 1860 | + return []; | |
| 1183 | 1861 | } |
| 1184 | 1862 | |
| 1185 | - /** | |
| 1186 | - * 'category__not_in' arg support. | |
| 1187 | - * | |
| 1188 | - * @since 3.6.0 | |
| 1189 | - */ | |
| 1190 | - if ( ! empty( $args['category__not_in'] ) ) { | |
| 1191 | - $filter['bool']['must'][]['bool']['must_not'] = array( | |
| 1192 | - 'terms' => array( | |
| 1193 | - 'terms.category.term_id' => array_values( (array) $args['category__not_in'] ), | |
| 1194 | - ), | |
| 1195 | - ); | |
| 1863 | + return [ | |
| 1864 | + 'bool' => [ | |
| 1865 | + 'must' => [ | |
| 1866 | + 'terms' => [ | |
| 1867 | + 'post_id' => array_values( (array) $args['post__in'] ), | |
| 1868 | + ], | |
| 1869 | + ], | |
| 1870 | + ], | |
| 1871 | + ]; | |
| 1872 | + } | |
| 1196 | 1873 | |
| 1197 | - $use_filters = true; | |
| 1874 | + /** | |
| 1875 | + * Parse the `post_name__in` WP Query arg and transform it into an ES query clause. | |
| 1876 | + * | |
| 1877 | + * @since 4.4.0 | |
| 1878 | + * @param array $args WP_Query arguments | |
| 1879 | + * @return array | |
| 1880 | + */ | |
| 1881 | + protected function parse_post_name__in( $args ) { | |
| 1882 | + if ( empty( $args['post_name__in'] ) ) { | |
| 1883 | + return []; | |
| 1198 | 1884 | } |
| 1199 | 1885 | |
| 1200 | - /** | |
| 1201 | - * 'tag__not_in' arg support. | |
| 1202 | - * | |
| 1203 | - * @since 3.6.0 | |
| 1204 | - */ | |
| 1205 | - if ( ! empty( $args['tag__not_in'] ) ) { | |
| 1206 | - $filter['bool']['must'][]['bool']['must_not'] = array( | |
| 1207 | - 'terms' => array( | |
| 1208 | - 'terms.post_tag.term_id' => array_values( (array) $args['tag__not_in'] ), | |
| 1209 | - ), | |
| 1210 | - ); | |
| 1886 | + return [ | |
| 1887 | + 'bool' => [ | |
| 1888 | + 'must' => [ | |
| 1889 | + 'terms' => [ | |
| 1890 | + 'post_name.raw' => array_values( (array) $args['post_name__in'] ), | |
| 1891 | + ], | |
| 1892 | + ], | |
| 1893 | + ], | |
| 1894 | + ]; | |
| 1895 | + } | |
| 1211 | 1896 | |
| 1212 | - $use_filters = true; | |
| 1897 | + /** | |
| 1898 | + * Parse the `post__not_in` WP Query arg and transform it into an ES query clause. | |
| 1899 | + * | |
| 1900 | + * @since 4.4.0 | |
| 1901 | + * @param array $args WP_Query arguments | |
| 1902 | + * @return array | |
| 1903 | + */ | |
| 1904 | + protected function parse_post__not_in( $args ) { | |
| 1905 | + if ( empty( $args['post__not_in'] ) ) { | |
| 1906 | + return []; | |
| 1213 | 1907 | } |
| 1214 | 1908 | |
| 1215 | - /** | |
| 1216 | - * Author query support | |
| 1217 | - * | |
| 1218 | - * @since 1.0 | |
| 1219 | - */ | |
| 1909 | + return [ | |
| 1910 | + 'bool' => [ | |
| 1911 | + 'must_not' => [ | |
| 1912 | + 'terms' => [ | |
| 1913 | + 'post_id' => array_values( (array) $args['post__not_in'] ), | |
| 1914 | + ], | |
| 1915 | + ], | |
| 1916 | + ], | |
| 1917 | + ]; | |
| 1918 | + } | |
| 1919 | + | |
| 1920 | + /** | |
| 1921 | + * Parse the `category__not_in` WP Query arg and transform it into an ES query clause. | |
| 1922 | + * | |
| 1923 | + * @since 4.4.0 | |
| 1924 | + * @param array $args WP_Query arguments | |
| 1925 | + * @return array | |
| 1926 | + */ | |
| 1927 | + protected function parse_category__not_in( $args ) { | |
| 1928 | + if ( empty( $args['category__not_in'] ) ) { | |
| 1929 | + return []; | |
| 1930 | + } | |
| 1931 | + | |
| 1932 | + return [ | |
| 1933 | + 'bool' => [ | |
| 1934 | + 'must_not' => [ | |
| 1935 | + 'terms' => [ | |
| 1936 | + 'terms.category.term_id' => array_values( (array) $args['category__not_in'] ), | |
| 1937 | + ], | |
| 1938 | + ], | |
| 1939 | + ], | |
| 1940 | + ]; | |
| 1941 | + } | |
| 1942 | + | |
| 1943 | + /** | |
| 1944 | + * Parse the `tag__not_in` WP Query arg and transform it into an ES query clause. | |
| 1945 | + * | |
| 1946 | + * @since 4.4.0 | |
| 1947 | + * @param array $args WP_Query arguments | |
| 1948 | + * @return array | |
| 1949 | + */ | |
| 1950 | + protected function parse_tag__not_in( $args ) { | |
| 1951 | + if ( empty( $args['tag__not_in'] ) ) { | |
| 1952 | + return []; | |
| 1953 | + } | |
| 1954 | + | |
| 1955 | + return [ | |
| 1956 | + 'bool' => [ | |
| 1957 | + 'must_not' => [ | |
| 1958 | + 'terms' => [ | |
| 1959 | + 'terms.post_tag.term_id' => array_values( (array) $args['tag__not_in'] ), | |
| 1960 | + ], | |
| 1961 | + ], | |
| 1962 | + ], | |
| 1963 | + ]; | |
| 1964 | + } | |
| 1965 | + | |
| 1966 | + /** | |
| 1967 | + * Parse the various author-related WP Query args and transform them into ES query clauses. | |
| 1968 | + * | |
| 1969 | + * @since 4.4.0 | |
| 1970 | + * @param array $args WP_Query arguments | |
| 1971 | + * @return array | |
| 1972 | + */ | |
| 1973 | + protected function parse_author( $args ) { | |
| 1220 | 1974 | if ( ! empty( $args['author'] ) ) { |
| 1221 | - $filter['bool']['must'][] = array( | |
| 1222 | - 'term' => array( | |
| 1975 | + return [ | |
| 1976 | + 'term' => [ | |
| 1223 | 1977 | 'post_author.id' => $args['author'], |
| 1224 | - ), | |
| 1225 | - ); | |
| 1978 | + ], | |
| 1979 | + ]; | |
| 1980 | + } | |
| 1226 | 1981 | |
| 1227 | - $use_filters = true; | |
| 1228 | - } elseif ( ! empty( $args['author_name'] ) ) { | |
| 1982 | + if ( ! empty( $args['author_name'] ) ) { | |
| 1229 | 1983 | // Since this was set to use the display name initially, there might be some code that used this feature. |
| 1230 | 1984 | // Let's ensure that any query vars coming in using author_name are in fact slugs. |
| 1231 | 1985 | // This was changed back in ticket #1622 to use the display name, so we removed the sanitize_user() call. |
| 1232 | - $filter['bool']['must'][] = array( | |
| 1233 | - 'term' => array( | |
| 1986 | + return [ | |
| 1987 | + 'term' => [ | |
| 1234 | 1988 | 'post_author.display_name' => $args['author_name'], |
| 1235 | - ), | |
| 1236 | - ); | |
| 1989 | + ], | |
| 1990 | + ]; | |
| 1991 | + } | |
| 1237 | 1992 | |
| 1238 | - $use_filters = true; | |
| 1239 | - } elseif ( ! empty( $args['author__in'] ) ) { | |
| 1240 | - $filter['bool']['must'][]['bool']['must'] = array( | |
| 1241 | - 'terms' => array( | |
| 1242 | - 'post_author.id' => array_values( (array) $args['author__in'] ), | |
| 1243 | - ), | |
| 1244 | - ); | |
| 1993 | + if ( ! empty( $args['author__in'] ) ) { | |
| 1994 | + return [ | |
| 1995 | + 'bool' => [ | |
| 1996 | + 'must' => [ | |
| 1997 | + 'terms' => [ | |
| 1998 | + 'post_author.id' => array_values( (array) $args['author__in'] ), | |
| 1999 | + ], | |
| 2000 | + ], | |
| 2001 | + ], | |
| 2002 | + ]; | |
| 2003 | + } | |
| 1245 | 2004 | |
| 1246 | - $use_filters = true; | |
| 1247 | - } elseif ( ! empty( $args['author__not_in'] ) ) { | |
| 1248 | - $filter['bool']['must'][]['bool']['must_not'] = array( | |
| 1249 | - 'terms' => array( | |
| 1250 | - 'post_author.id' => array_values( (array) $args['author__not_in'] ), | |
| 1251 | - ), | |
| 1252 | - ); | |
| 2005 | + if ( ! empty( $args['author__not_in'] ) ) { | |
| 2006 | + return [ | |
| 2007 | + 'bool' => [ | |
| 2008 | + 'must_not' => [ | |
| 2009 | + 'terms' => [ | |
| 2010 | + 'post_author.id' => array_values( (array) $args['author__not_in'] ), | |
| 2011 | + ], | |
| 2012 | + ], | |
| 2013 | + ], | |
| 2014 | + ]; | |
| 2015 | + } | |
| 1253 | 2016 | |
| 1254 | - $use_filters = true; | |
| 2017 | + return []; | |
| 2018 | + } | |
| 2019 | + | |
| 2020 | + /** | |
| 2021 | + * Parse the `post_mime_type` WP Query arg and transform it into an ES query clause. | |
| 2022 | + * | |
| 2023 | + * If we have array, it will be fool text search filter. | |
| 2024 | + * If we have string(like filter images in media screen), we will have mime type "image" so need to check it as | |
| 2025 | + * regexp filter. | |
| 2026 | + * | |
| 2027 | + * @since 4.4.0 | |
| 2028 | + * @param array $args WP_Query arguments | |
| 2029 | + * @return array | |
| 2030 | + */ | |
| 2031 | + protected function parse_post_mime_type( $args ) { | |
| 2032 | + if ( empty( $args['post_mime_type'] ) ) { | |
| 2033 | + return []; | |
| 1255 | 2034 | } |
| 1256 | 2035 | |
| 1257 | - /** | |
| 1258 | - * Add support for post_mime_type | |
| 1259 | - * | |
| 1260 | - * If we have array, it will be fool text search filter. | |
| 1261 | - * If we have string(like filter images in media screen), we will have mime type "image" so need to check it as | |
| 1262 | - * regexp filter. | |
| 1263 | - * | |
| 1264 | - * @since 2.3 | |
| 1265 | - */ | |
| 1266 | - if ( ! empty( $args['post_mime_type'] ) ) { | |
| 1267 | - if ( is_array( $args['post_mime_type'] ) ) { | |
| 2036 | + if ( is_array( $args['post_mime_type'] ) ) { | |
| 1268 | 2037 | |
| 1269 | - $args_post_mime_type = []; | |
| 2038 | + $args_post_mime_type = []; | |
| 1270 | 2039 | |
| 1271 | - foreach ( $args['post_mime_type'] as $mime_type ) { | |
| 1272 | - /** | |
| 1273 | - * check if matches the MIME type pattern: type/subtype and | |
| 1274 | - * leave an empty string as posts, pages and CPTs don't have a MIME type | |
| 1275 | - */ | |
| 1276 | - if ( preg_match( '/^[-._a-z0-9]+\/[-._a-z0-9]+$/i', $mime_type ) || empty( $mime_type ) ) { | |
| 1277 | - $args_post_mime_type[] = $mime_type; | |
| 1278 | - } else { | |
| 1279 | - $filtered_mime_type_by_type = wp_match_mime_types( $mime_type, wp_get_mime_types() ); | |
| 2040 | + foreach ( $args['post_mime_type'] as $mime_type ) { | |
| 2041 | + /** | |
| 2042 | + * check if matches the MIME type pattern: type/subtype and | |
| 2043 | + * leave an empty string as posts, pages and CPTs don't have a MIME type | |
| 2044 | + */ | |
| 2045 | + if ( preg_match( '/^[-._a-z0-9]+\/[-._a-z0-9]+$/i', $mime_type ) || empty( $mime_type ) ) { | |
| 2046 | + $args_post_mime_type[] = $mime_type; | |
| 2047 | + } else { | |
| 2048 | + $filtered_mime_type_by_type = wp_match_mime_types( $mime_type, wp_get_mime_types() ); | |
| 1280 | 2049 | |
| 1281 | - $args_post_mime_type = array_merge( $args_post_mime_type, $filtered_mime_type_by_type[ $mime_type ] ); | |
| 1282 | - } | |
| 2050 | + $args_post_mime_type = array_merge( | |
| 2051 | + $args_post_mime_type, | |
| 2052 | + $filtered_mime_type_by_type[ $mime_type ] ?? [ $mime_type ] | |
| 2053 | + ); | |
| 1283 | 2054 | } |
| 2055 | + } | |
| 1284 | 2056 | |
| 1285 | - $filter['bool']['must'][] = array( | |
| 1286 | - 'terms' => array( | |
| 1287 | - 'post_mime_type' => $args_post_mime_type, | |
| 1288 | - ), | |
| 1289 | - ); | |
| 2057 | + return [ | |
| 2058 | + 'terms' => [ | |
| 2059 | + 'post_mime_type' => $args_post_mime_type, | |
| 2060 | + ], | |
| 2061 | + ]; | |
| 2062 | + } | |
| 1290 | 2063 | |
| 1291 | - $use_filters = true; | |
| 1292 | - } elseif ( is_string( $args['post_mime_type'] ) ) { | |
| 1293 | - $filter['bool']['must'][] = array( | |
| 1294 | - 'regexp' => array( | |
| 1295 | - 'post_mime_type' => $args['post_mime_type'] . '.*', | |
| 1296 | - ), | |
| 1297 | - ); | |
| 2064 | + if ( is_string( $args['post_mime_type'] ) ) { | |
| 2065 | + return [ | |
| 2066 | + 'regexp' => array( | |
| 2067 | + 'post_mime_type' => $args['post_mime_type'] . '.*', | |
| 2068 | + ), | |
| 2069 | + ]; | |
| 2070 | + } | |
| 1298 | 2071 | |
| 1299 | - $use_filters = true; | |
| 1300 | - } | |
| 1301 | - } | |
| 2072 | + return []; | |
| 2073 | + } | |
| 1302 | 2074 | |
| 1303 | - /** | |
| 1304 | - * Simple date params support | |
| 1305 | - * | |
| 1306 | - * @since 1.3 | |
| 1307 | - */ | |
| 2075 | + /** | |
| 2076 | + * Parse the various date-related WP Query args and transform them into ES query clauses. | |
| 2077 | + * | |
| 2078 | + * @since 4.4.0 | |
| 2079 | + * @param array $args WP_Query arguments | |
| 2080 | + * @return array | |
| 2081 | + */ | |
| 2082 | + protected function parse_date( $args ) { | |
| 1308 | 2083 | $date_filter = DateQuery::simple_es_date_filter( $args ); |
| 1309 | 2084 | |
| 1310 | 2085 | if ( ! empty( $date_filter ) ) { |
| 1311 | - $filter['bool']['must'][] = $date_filter; | |
| 1312 | - $use_filters = true; | |
| 2086 | + return $date_filter; | |
| 1313 | 2087 | } |
| 1314 | 2088 | |
| 1315 | - /** | |
| 1316 | - * 'date_query' arg support. | |
| 1317 | - */ | |
| 1318 | 2089 | if ( ! empty( $args['date_query'] ) ) { |
| 1319 | 2090 | |
| 1320 | 2091 | $date_query = new DateQuery( $args['date_query'] ); |
| 1321 | 2092 | |
| @@ -1321,16 +2092,41 @@ | ||
| 1321 | 2092 | |
| 1322 | 2093 | $date_filter = $date_query->get_es_filter(); |
| 1323 | 2094 | |
| 1324 | 2095 | if ( array_key_exists( 'and', $date_filter ) ) { |
| 1325 | - $filter['bool']['must'][] = $date_filter['and']; | |
| 1326 | - $use_filters = true; | |
| 2096 | + return $date_filter['and']; | |
| 2097 | + } elseif ( array_key_exists( 'or', $date_filter ) ) { | |
| 2098 | + return $date_filter['or']; | |
| 1327 | 2099 | } |
| 1328 | 2100 | } |
| 2101 | + } | |
| 1329 | 2102 | |
| 1330 | - $meta_queries = []; | |
| 2103 | + /** | |
| 2104 | + * Parse all meta queries. | |
| 2105 | + * | |
| 2106 | + * Although the name may be misleading, it handles the `meta_query` argument. There is a `build_meta_query` that handles each "small" query. | |
| 2107 | + * | |
| 2108 | + * @since 4.4.0 | |
| 2109 | + * @param array $args WP_Query arguments | |
| 2110 | + * @return array | |
| 2111 | + */ | |
| 2112 | + protected function parse_meta_queries( $args ) { | |
| 2113 | + /** | |
| 2114 | + * 'meta_query' arg support. | |
| 2115 | + * | |
| 2116 | + * Relation supports 'AND' and 'OR'. 'AND' is the default. For each individual query, the | |
| 2117 | + * following 'compare' values are supported: =, !=, EXISTS, NOT EXISTS. '=' is the default. | |
| 2118 | + * | |
| 2119 | + * @since 1.3 | |
| 2120 | + */ | |
| 2121 | + $meta_queries = ( ! empty( $args['meta_query'] ) ) ? $args['meta_query'] : []; | |
| 2122 | + $meta_queries = ( new \WP_Meta_Query() )->sanitize_query( $meta_queries ); | |
| 1331 | 2123 | |
| 1332 | 2124 | /** |
| 2125 | + * Todo: Support meta_type | |
| 2126 | + */ | |
| 2127 | + | |
| 2128 | + /** | |
| 1333 | 2129 | * Support `meta_key`, `meta_value`, `meta_value_num`, and `meta_compare` query args |
| 1334 | 2130 | */ |
| 1335 | 2131 | if ( ! empty( $args['meta_key'] ) ) { |
| 1336 | 2132 | $meta_query_array = [ |
| @@ -1346,44 +2142,168 @@ | ||
| 1346 | 2142 | if ( isset( $args['meta_compare'] ) ) { |
| 1347 | 2143 | $meta_query_array['compare'] = $args['meta_compare']; |
| 1348 | 2144 | } |
| 1349 | 2145 | |
| 1350 | - $meta_queries[] = $meta_query_array; | |
| 2146 | + if ( ! empty( $meta_queries ) ) { | |
| 2147 | + $meta_queries = [ | |
| 2148 | + 'relation' => 'AND', | |
| 2149 | + $meta_query_array, | |
| 2150 | + $meta_queries, | |
| 2151 | + ]; | |
| 2152 | + } else { | |
| 2153 | + $meta_queries = [ $meta_query_array ]; | |
| 2154 | + } | |
| 1351 | 2155 | } |
| 1352 | 2156 | |
| 2157 | + if ( ! empty( $meta_queries ) ) { | |
| 2158 | + // get meta query filter | |
| 2159 | + $meta_filter = $this->build_meta_query( $meta_queries ); | |
| 2160 | + | |
| 2161 | + if ( ! empty( $meta_filter ) ) { | |
| 2162 | + return $meta_filter; | |
| 2163 | + } | |
| 2164 | + } | |
| 2165 | + | |
| 2166 | + return []; | |
| 2167 | + } | |
| 2168 | + | |
| 2169 | + /** | |
| 2170 | + * Parse the `post_type` WP Query arg and transform it into an ES query clause. | |
| 2171 | + * | |
| 2172 | + * @since 4.4.0 | |
| 2173 | + * @param array $args WP_Query arguments | |
| 2174 | + * @return array | |
| 2175 | + */ | |
| 2176 | + protected function parse_post_type( $args ) { | |
| 1353 | 2177 | /** |
| 1354 | - * Todo: Support meta_type | |
| 2178 | + * If not set default to post. If search and not set, default to "any". | |
| 1355 | 2179 | */ |
| 2180 | + if ( ! empty( $args['post_type'] ) ) { | |
| 2181 | + // should NEVER be "any" but just in case | |
| 2182 | + if ( 'any' !== $args['post_type'] ) { | |
| 2183 | + $post_types = (array) $args['post_type']; | |
| 2184 | + $terms_map_name = 'terms'; | |
| 1356 | 2185 | |
| 2186 | + return [ | |
| 2187 | + $terms_map_name => [ | |
| 2188 | + 'post_type.raw' => array_values( $post_types ), | |
| 2189 | + ], | |
| 2190 | + ]; | |
| 2191 | + } | |
| 2192 | + } elseif ( empty( $args['s'] ) ) { | |
| 2193 | + return [ | |
| 2194 | + 'term' => [ | |
| 2195 | + 'post_type.raw' => 'post', | |
| 2196 | + ], | |
| 2197 | + ]; | |
| 2198 | + } | |
| 2199 | + | |
| 2200 | + return []; | |
| 2201 | + } | |
| 2202 | + | |
| 2203 | + /** | |
| 2204 | + * Parse the `post_status` WP Query arg and transform it into an ES query clause. | |
| 2205 | + * | |
| 2206 | + * @since 4.4.0 | |
| 2207 | + * @param array $args WP_Query arguments | |
| 2208 | + * @return array | |
| 2209 | + */ | |
| 2210 | + protected function parse_post_status( $args ) { | |
| 1357 | 2211 | /** |
| 1358 | - * 'meta_query' arg support. | |
| 2212 | + * Like WP_Query in search context, if no post_status is specified we default to "any". To | |
| 2213 | + * be safe you should ALWAYS specify the post_status parameter UNLIKE with WP_Query. | |
| 1359 | 2214 | * |
| 1360 | - * Relation supports 'AND' and 'OR'. 'AND' is the default. For each individual query, the | |
| 1361 | - * following 'compare' values are supported: =, !=, EXISTS, NOT EXISTS. '=' is the default. | |
| 1362 | - * | |
| 1363 | - * @since 1.3 | |
| 2215 | + * @since 2.1 | |
| 1364 | 2216 | */ |
| 1365 | - if ( ! empty( $args['meta_query'] ) ) { | |
| 1366 | - $meta_queries = array_merge( $meta_queries, $args['meta_query'] ); | |
| 2217 | + if ( ! empty( $args['post_status'] ) ) { | |
| 2218 | + // should NEVER be "any" but just in case | |
| 2219 | + if ( 'any' !== $args['post_status'] ) { | |
| 2220 | + $post_status = (array) ( is_string( $args['post_status'] ) ? explode( ',', $args['post_status'] ) : $args['post_status'] ); | |
| 2221 | + $post_status = array_map( 'trim', $post_status ); | |
| 2222 | + $terms_map_name = 'terms'; | |
| 2223 | + if ( count( $post_status ) < 2 ) { | |
| 2224 | + $terms_map_name = 'term'; | |
| 2225 | + $post_status = $post_status[0]; | |
| 2226 | + } | |
| 2227 | + | |
| 2228 | + return [ | |
| 2229 | + $terms_map_name => [ | |
| 2230 | + 'post_status' => is_array( $post_status ) ? array_values( $post_status ) : $post_status, | |
| 2231 | + ], | |
| 2232 | + ]; | |
| 2233 | + } | |
| 2234 | + } elseif ( ! is_admin() ) { | |
| 2235 | + $statuses = get_post_stati( array( 'public' => true ) ); | |
| 2236 | + $statuses = array_values( $statuses ); | |
| 2237 | + | |
| 2238 | + $post_status_filter_type = 'terms'; | |
| 2239 | + | |
| 2240 | + return [ | |
| 2241 | + $post_status_filter_type => [ | |
| 2242 | + 'post_status' => $statuses, | |
| 2243 | + ], | |
| 2244 | + ]; | |
| 1367 | 2245 | } |
| 1368 | 2246 | |
| 1369 | - if ( ! empty( $meta_queries ) ) { | |
| 2247 | + return []; | |
| 2248 | + } | |
| 1370 | 2249 | |
| 1371 | - $relation = 'must'; | |
| 1372 | - if ( ! empty( $args['meta_query'] ) && ! empty( $args['meta_query']['relation'] ) && 'or' === strtolower( $args['meta_query']['relation'] ) ) { | |
| 1373 | - $relation = 'should'; | |
| 1374 | - } | |
| 2250 | + /** | |
| 2251 | + * Parse the `painless_script` WP Query arg and transform it into an ES query clause. | |
| 2252 | + * | |
| 2253 | + * @since 5.3.0 | |
| 2254 | + * @param array $args WP_Query arguments | |
| 2255 | + * @return array | |
| 2256 | + */ | |
| 2257 | + protected function painless_script_query( $args ) { | |
| 2258 | + $scripts = $args['painless_script'] ?? []; | |
| 1375 | 2259 | |
| 1376 | - // get meta query filter | |
| 1377 | - $meta_filter = $this->build_meta_query( $meta_queries ); | |
| 2260 | + if ( empty( $scripts ) ) { | |
| 2261 | + return []; | |
| 2262 | + } | |
| 1378 | 2263 | |
| 1379 | - if ( ! empty( $meta_filter ) ) { | |
| 1380 | - $filter['bool']['must'][] = $meta_filter; | |
| 2264 | + $normalized_scripts = array_map( | |
| 2265 | + function ( $script_config ) { | |
| 2266 | + return is_string( $script_config ) | |
| 2267 | + ? [ 'source' => $script_config ] | |
| 2268 | + : $script_config; | |
| 2269 | + }, | |
| 2270 | + $scripts | |
| 2271 | + ); | |
| 1381 | 2272 | |
| 1382 | - $use_filters = true; | |
| 2273 | + $normalized_scripts = array_filter( $normalized_scripts ); | |
| 2274 | + | |
| 2275 | + $filters = []; | |
| 2276 | + foreach ( $normalized_scripts as $script_config ) { | |
| 2277 | + $script_query = [ 'source' => $script_config['source'] ]; | |
| 2278 | + | |
| 2279 | + if ( ! empty( $script_config['params'] ) && is_array( $script_config['params'] ) ) { | |
| 2280 | + $script_query['params'] = $script_config['params']; | |
| 1383 | 2281 | } |
| 2282 | + | |
| 2283 | + $filters['bool']['must'][] = [ | |
| 2284 | + 'bool' => [ | |
| 2285 | + 'filter' => [ | |
| 2286 | + 'script' => [ | |
| 2287 | + 'script' => $script_query, | |
| 2288 | + ], | |
| 2289 | + ], | |
| 2290 | + ], | |
| 2291 | + ]; | |
| 1384 | 2292 | } |
| 1385 | 2293 | |
| 2294 | + return $filters; | |
| 2295 | + } | |
| 2296 | + | |
| 2297 | + /** | |
| 2298 | + * If in a search context set search fields, otherwise query everything. | |
| 2299 | + * | |
| 2300 | + * @since 4.4.0 | |
| 2301 | + * @param array $formatted_args Formatted Elasticsearch query | |
| 2302 | + * @param array $args WP_Query arguments | |
| 2303 | + * @return array | |
| 2304 | + */ | |
| 2305 | + protected function maybe_set_search_fields( $formatted_args, $args ) { | |
| 1386 | 2306 | /** |
| 1387 | 2307 | * Allow for search field specification |
| 1388 | 2308 | * |
| 1389 | 2309 | * @since 1.0 |
| @@ -1460,23 +2380,20 @@ | ||
| 1460 | 2380 | 'boost' => 1, |
| 1461 | 2381 | ); |
| 1462 | 2382 | } |
| 1463 | 2383 | |
| 1464 | - /** | |
| 1465 | - * Order by 'rand' support | |
| 1466 | - * | |
| 1467 | - * Ref: https://github.com/elastic/elasticsearch/issues/1170 | |
| 1468 | - */ | |
| 1469 | - if ( ! empty( $args['orderby'] ) ) { | |
| 1470 | - $orderbys = $this->get_orderby_array( $args['orderby'] ); | |
| 1471 | - if ( in_array( 'rand', $orderbys, true ) ) { | |
| 1472 | - $formatted_args_query = $formatted_args['query']; | |
| 1473 | - $formatted_args['query'] = []; | |
| 1474 | - $formatted_args['query']['function_score']['query'] = $formatted_args_query; | |
| 1475 | - $formatted_args['query']['function_score']['random_score'] = (object) []; | |
| 1476 | - } | |
| 1477 | - } | |
| 2384 | + return $formatted_args; | |
| 2385 | + } | |
| 1478 | 2386 | |
| 2387 | + /** | |
| 2388 | + * If needed bring sticky posts and order them. | |
| 2389 | + * | |
| 2390 | + * @since 4.4.0 | |
| 2391 | + * @param array $formatted_args Formatted Elasticsearch query | |
| 2392 | + * @param array $args WP_Query arguments | |
| 2393 | + * @return array | |
| 2394 | + */ | |
| 2395 | + protected function maybe_add_sticky_posts( $formatted_args, $args ) { | |
| 1479 | 2396 | /** |
| 1480 | 2397 | * Sticky posts support |
| 1481 | 2398 | */ |
| 1482 | 2399 | |
| @@ -1524,118 +2441,21 @@ | ||
| 1524 | 2441 | ), |
| 1525 | 2442 | ); |
| 1526 | 2443 | } |
| 1527 | 2444 | |
| 1528 | - /** | |
| 1529 | - * If not set default to post. If search and not set, default to "any". | |
| 1530 | - */ | |
| 1531 | - if ( ! empty( $args['post_type'] ) ) { | |
| 1532 | - // should NEVER be "any" but just in case | |
| 1533 | - if ( 'any' !== $args['post_type'] ) { | |
| 1534 | - $post_types = (array) $args['post_type']; | |
| 1535 | - $terms_map_name = 'terms'; | |
| 2445 | + return $formatted_args; | |
| 2446 | + } | |
| 1536 | 2447 | |
| 1537 | - $filter['bool']['must'][] = array( | |
| 1538 | - $terms_map_name => array( | |
| 1539 | - 'post_type.raw' => array_values( $post_types ), | |
| 1540 | - ), | |
| 1541 | - ); | |
| 1542 | - | |
| 1543 | - $use_filters = true; | |
| 1544 | - } | |
| 1545 | - } elseif ( empty( $args['s'] ) ) { | |
| 1546 | - $filter['bool']['must'][] = array( | |
| 1547 | - 'term' => array( | |
| 1548 | - 'post_type.raw' => 'post', | |
| 1549 | - ), | |
| 1550 | - ); | |
| 1551 | - | |
| 1552 | - $use_filters = true; | |
| 1553 | - } | |
| 1554 | - | |
| 2448 | + /** | |
| 2449 | + * If needed set the `fields` ES query clause. | |
| 2450 | + * | |
| 2451 | + * @since 4.4.0 | |
| 2452 | + * @param array $formatted_args Formatted Elasticsearch query | |
| 2453 | + * @param array $args WP_Query arguments | |
| 2454 | + * @return array | |
| 2455 | + */ | |
| 2456 | + protected function maybe_set_fields( $formatted_args, $args ) { | |
| 1555 | 2457 | /** |
| 1556 | - * Like WP_Query in search context, if no post_status is specified we default to "any". To | |
| 1557 | - * be safe you should ALWAYS specify the post_status parameter UNLIKE with WP_Query. | |
| 1558 | - * | |
| 1559 | - * @since 2.1 | |
| 1560 | - */ | |
| 1561 | - if ( ! empty( $args['post_status'] ) ) { | |
| 1562 | - // should NEVER be "any" but just in case | |
| 1563 | - if ( 'any' !== $args['post_status'] ) { | |
| 1564 | - $post_status = (array) ( is_string( $args['post_status'] ) ? explode( ',', $args['post_status'] ) : $args['post_status'] ); | |
| 1565 | - $post_status = array_map( 'trim', $post_status ); | |
| 1566 | - $terms_map_name = 'terms'; | |
| 1567 | - if ( count( $post_status ) < 2 ) { | |
| 1568 | - $terms_map_name = 'term'; | |
| 1569 | - $post_status = $post_status[0]; | |
| 1570 | - } | |
| 1571 | - | |
| 1572 | - $filter['bool']['must'][] = array( | |
| 1573 | - $terms_map_name => array( | |
| 1574 | - 'post_status' => $post_status, | |
| 1575 | - ), | |
| 1576 | - ); | |
| 1577 | - | |
| 1578 | - $use_filters = true; | |
| 1579 | - } | |
| 1580 | - } else { | |
| 1581 | - $statuses = get_post_stati( array( 'public' => true ) ); | |
| 1582 | - | |
| 1583 | - if ( is_admin() ) { | |
| 1584 | - /** | |
| 1585 | - * In the admin we will add protected and private post statuses to the default query | |
| 1586 | - * per WP default behavior. | |
| 1587 | - */ | |
| 1588 | - $statuses = array_merge( | |
| 1589 | - $statuses, | |
| 1590 | - get_post_stati( | |
| 1591 | - array( | |
| 1592 | - 'protected' => true, | |
| 1593 | - 'show_in_admin_all_list' => true, | |
| 1594 | - ) | |
| 1595 | - ) | |
| 1596 | - ); | |
| 1597 | - | |
| 1598 | - if ( is_user_logged_in() ) { | |
| 1599 | - $statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) ); | |
| 1600 | - } | |
| 1601 | - } | |
| 1602 | - | |
| 1603 | - $statuses = array_values( $statuses ); | |
| 1604 | - | |
| 1605 | - $post_status_filter_type = 'terms'; | |
| 1606 | - | |
| 1607 | - $filter['bool']['must'][] = array( | |
| 1608 | - $post_status_filter_type => array( | |
| 1609 | - 'post_status' => $statuses, | |
| 1610 | - ), | |
| 1611 | - ); | |
| 1612 | - | |
| 1613 | - $use_filters = true; | |
| 1614 | - } | |
| 1615 | - | |
| 1616 | - if ( isset( $args['offset'] ) ) { | |
| 1617 | - $formatted_args['from'] = (int) $args['offset']; | |
| 1618 | - } | |
| 1619 | - | |
| 1620 | - if ( isset( $args['paged'] ) && $args['paged'] > 1 ) { | |
| 1621 | - $formatted_args['from'] = $args['posts_per_page'] * ( $args['paged'] - 1 ); | |
| 1622 | - } | |
| 1623 | - | |
| 1624 | - /** | |
| 1625 | - * Fix negative offset. This happens, for example, on hierarchical post types. | |
| 1626 | - * | |
| 1627 | - * Ref: https://github.com/10up/ElasticPress/issues/2480 | |
| 1628 | - */ | |
| 1629 | - if ( $formatted_args['from'] < 0 ) { | |
| 1630 | - $formatted_args['from'] = 0; | |
| 1631 | - } | |
| 1632 | - | |
| 1633 | - if ( $use_filters ) { | |
| 1634 | - $formatted_args['post_filter'] = $filter; | |
| 1635 | - } | |
| 1636 | - | |
| 1637 | - /** | |
| 1638 | 2458 | * Support fields. |
| 1639 | 2459 | */ |
| 1640 | 2460 | if ( isset( $args['fields'] ) ) { |
| 1641 | 2461 | switch ( $args['fields'] ) { |
| @@ -1657,8 +2477,21 @@ | ||
| 1657 | 2477 | break; |
| 1658 | 2478 | } |
| 1659 | 2479 | } |
| 1660 | 2480 | |
| 2481 | + return $formatted_args; | |
| 2482 | + } | |
| 2483 | + | |
| 2484 | + /** | |
| 2485 | + * If needed set the `aggs` ES query clause. | |
| 2486 | + * | |
| 2487 | + * @since 4.4.0 | |
| 2488 | + * @param array $formatted_args Formatted Elasticsearch query. | |
| 2489 | + * @param array $args WP_Query arguments | |
| 2490 | + * @param array $filters Filters to be applied to the ES query | |
| 2491 | + * @return array | |
| 2492 | + */ | |
| 2493 | + protected function maybe_set_aggs( $formatted_args, $args, $filters ) { | |
| 1661 | 2494 | /** |
| 1662 | 2495 | * Aggregations |
| 1663 | 2496 | */ |
| 1664 | 2497 | if ( ! empty( $args['aggs'] ) && is_array( $args['aggs'] ) ) { |
| @@ -1668,489 +2501,584 @@ | ||
| 1668 | 2501 | $has_only_num_keys = count( $agg_num_keys ) === count( $args['aggs'] ); |
| 1669 | 2502 | |
| 1670 | 2503 | if ( $has_only_num_keys ) { |
| 1671 | 2504 | foreach ( $args['aggs'] as $agg ) { |
| 1672 | - $formatted_args = $this->apply_aggregations( $formatted_args, $agg, $use_filters, $filter ); | |
| 2505 | + $formatted_args = $this->apply_aggregations( $formatted_args, $agg, ! empty( $filters ), $filters ); | |
| 1673 | 2506 | } |
| 1674 | 2507 | } else { |
| 1675 | 2508 | // Single aggregation. |
| 1676 | - $formatted_args = $this->apply_aggregations( $formatted_args, $args['aggs'], $use_filters, $filter ); | |
| 2509 | + $formatted_args = $this->apply_aggregations( $formatted_args, $args['aggs'], ! empty( $filters ), $filters ); | |
| 1677 | 2510 | } |
| 1678 | 2511 | } |
| 1679 | 2512 | |
| 1680 | - /** | |
| 1681 | - * Filter formatted Elasticsearch [ost ]query (entire query) | |
| 1682 | - * | |
| 1683 | - * @hook ep_formatted_args | |
| 1684 | - * @param {array} $formatted_args Formatted Elasticsearch query | |
| 1685 | - * @param {array} $query_vars Query variables | |
| 1686 | - * @param {array} $query Query part | |
| 1687 | - * @return {array} New query | |
| 1688 | - */ | |
| 1689 | - $formatted_args = apply_filters( 'ep_formatted_args', $formatted_args, $args, $wp_query ); | |
| 2513 | + return $formatted_args; | |
| 2514 | + } | |
| 1690 | 2515 | |
| 1691 | - /** | |
| 1692 | - * Filter formatted Elasticsearch [ost ]query (entire query) | |
| 1693 | - * | |
| 1694 | - * @hook ep_post_formatted_args | |
| 1695 | - * @param {array} $formatted_args Formatted Elasticsearch query | |
| 1696 | - * @param {array} $query_vars Query variables | |
| 1697 | - * @param {array} $query Query part | |
| 1698 | - * @return {array} New query | |
| 1699 | - */ | |
| 1700 | - $formatted_args = apply_filters( 'ep_post_formatted_args', $formatted_args, $args, $wp_query ); | |
| 2516 | + /** | |
| 2517 | + * Parse tax query field value. | |
| 2518 | + * | |
| 2519 | + * @since 4.4.0 | |
| 2520 | + * @param string $field Field name | |
| 2521 | + * @return string | |
| 2522 | + */ | |
| 2523 | + protected function parse_tax_query_field( string $field ): string { | |
| 1701 | 2524 | |
| 1702 | - return $formatted_args; | |
| 2525 | + $from_to = [ | |
| 2526 | + 'name' => 'name.raw', | |
| 2527 | + 'slug' => 'slug', | |
| 2528 | + 'term_taxonomy_id' => 'term_taxonomy_id', | |
| 2529 | + ]; | |
| 2530 | + | |
| 2531 | + return $from_to[ $field ] ?? 'term_id'; | |
| 1703 | 2532 | } |
| 1704 | 2533 | |
| 1705 | 2534 | /** |
| 1706 | - * Adjust the fuzziness parameter if needed. | |
| 2535 | + * Filter a list of meta keys down to those chosen by the user or | |
| 2536 | + * allowed via a hook. | |
| 1707 | 2537 | * |
| 1708 | - * If using fields with type `long`, queries should not have a fuzziness parameter. | |
| 2538 | + * This function is used when manual management of metadata fields is | |
| 2539 | + * enabled. This is the default behaviour as of 5.0.0 and controlled by the | |
| 2540 | + * `ep_meta_mode` filter. | |
| 1709 | 2541 | * |
| 1710 | - * @param array $query Current query | |
| 1711 | - * @param array $query_vars Query variables | |
| 1712 | - * @param string $search_text Search text | |
| 1713 | - * @param array $search_fields Search fields | |
| 1714 | - * @return array New query | |
| 2542 | + * @param array $metas Key => value pairs of post meta | |
| 2543 | + * @param WP_Post $post Post object | |
| 2544 | + * @since 5.0.0 | |
| 2545 | + * @return array | |
| 1715 | 2546 | */ |
| 1716 | - public function adjust_query_fuzziness( $query, $query_vars, $search_text, $search_fields ) { | |
| 1717 | - if ( empty( array_intersect( $search_fields, [ 'ID', 'post_id', 'post_parent' ] ) ) ) { | |
| 1718 | - return $query; | |
| 2547 | + protected function filter_allowed_metas_manual( $metas, $post ) { | |
| 2548 | + $filtered_metas = []; | |
| 2549 | + $search_feature = \ElasticPress\Features::factory()->get_registered_feature( 'search' ); | |
| 2550 | + | |
| 2551 | + if ( empty( $post->post_type ) ) { | |
| 2552 | + return $filtered_metas; | |
| 1719 | 2553 | } |
| 1720 | 2554 | |
| 1721 | - if ( ! isset( $query['bool'] ) || ! isset( $query['bool']['should'] ) ) { | |
| 1722 | - return $query; | |
| 2555 | + $weighting = $search_feature->weighting->get_weighting_configuration_with_defaults(); | |
| 2556 | + $is_searchable = in_array( $search_feature, $search_feature->get_searchable_post_types(), true ); | |
| 2557 | + if ( empty( $weighting[ $post->post_type ] ) && $is_searchable ) { | |
| 2558 | + return $filtered_metas; | |
| 1723 | 2559 | } |
| 1724 | 2560 | |
| 1725 | - foreach ( $query['bool']['should'] as &$clause ) { | |
| 1726 | - if ( ! isset( $clause['multi_match'] ) ) { | |
| 2561 | + /** This filter is documented in includes/classes/Indexable/Post/Post.php */ | |
| 2562 | + $allowed_protected_keys = apply_filters( 'ep_prepare_meta_allowed_protected_keys', [], $post ); | |
| 2563 | + | |
| 2564 | + $selected_keys = []; | |
| 2565 | + if ( ! empty( $weighting[ $post->post_type ] ) ) { | |
| 2566 | + $selected_keys = array_map( | |
| 2567 | + function ( $field ) { | |
| 2568 | + if ( false === strpos( $field, 'meta.' ) ) { | |
| 2569 | + return null; | |
| 2570 | + } | |
| 2571 | + $field_name_parts = explode( '.', $field ); | |
| 2572 | + return $field_name_parts[1]; | |
| 2573 | + }, | |
| 2574 | + array_keys( $weighting[ $post->post_type ] ) | |
| 2575 | + ); | |
| 2576 | + $selected_keys = array_filter( $selected_keys ); | |
| 2577 | + } | |
| 2578 | + | |
| 2579 | + /** | |
| 2580 | + * Filter indexable meta keys for posts | |
| 2581 | + * | |
| 2582 | + * @hook ep_prepare_meta_allowed_keys | |
| 2583 | + * @param {array} $keys Allowed keys | |
| 2584 | + * @param {WP_Post} $post Post object | |
| 2585 | + * @since 5.0.0 | |
| 2586 | + * @return {array} New keys | |
| 2587 | + */ | |
| 2588 | + $allowed_keys = apply_filters( 'ep_prepare_meta_allowed_keys', array_merge( $allowed_protected_keys, $selected_keys ), $post ); | |
| 2589 | + | |
| 2590 | + foreach ( $metas as $key => $value ) { | |
| 2591 | + if ( ! in_array( $key, $allowed_keys, true ) ) { | |
| 1727 | 2592 | continue; |
| 1728 | 2593 | } |
| 1729 | 2594 | |
| 1730 | - if ( isset( $clause['multi_match']['fuzziness'] ) ) { | |
| 1731 | - unset( $clause['multi_match']['fuzziness'] ); | |
| 1732 | - } | |
| 2595 | + $filtered_metas[ $key ] = $value; | |
| 1733 | 2596 | } |
| 1734 | 2597 | |
| 1735 | - return $query; | |
| 2598 | + return $filtered_metas; | |
| 1736 | 2599 | } |
| 1737 | 2600 | |
| 1738 | 2601 | /** |
| 1739 | - * Parse and build out our tax query. | |
| 2602 | + * Filter a list of meta keys down to public keys or protected keys | |
| 2603 | + * allowed via a hook. | |
| 1740 | 2604 | * |
| 1741 | - * @access protected | |
| 2605 | + * This function is used to filter meta keys when ElasticPress is in | |
| 2606 | + * network mode or when the meta mode is set to `auto` via the | |
| 2607 | + * `ep_meta_mode` hook. This was the default behaviour prior to 5.0.0. | |
| 1742 | 2608 | * |
| 1743 | - * @param array $query Tax query | |
| 2609 | + * @param array $metas Key => value pairs of post meta | |
| 2610 | + * @param WP_Post $post Post object | |
| 2611 | + * @since 5.0.0 | |
| 1744 | 2612 | * @return array |
| 1745 | 2613 | */ |
| 1746 | - protected function parse_tax_query( $query ) { | |
| 1747 | - $tax_query = [ | |
| 1748 | - 'tax_filter' => [], | |
| 1749 | - 'tax_must_not_filter' => [], | |
| 1750 | - ]; | |
| 1751 | - $relation = ''; | |
| 2614 | + protected function filter_allowed_metas_auto( $metas, $post ) { | |
| 2615 | + $filtered_metas = []; | |
| 1752 | 2616 | |
| 1753 | - foreach ( $query as $tax_queries ) { | |
| 1754 | - // If we have a nested tax query, recurse through that | |
| 1755 | - if ( is_array( $tax_queries ) && empty( $tax_queries['taxonomy'] ) ) { | |
| 1756 | - $result = $this->parse_tax_query( $tax_queries ); | |
| 1757 | - $relation = ( ! empty( $tax_queries['relation'] ) ) ? strtolower( $tax_queries['relation'] ) : 'and'; | |
| 1758 | - $filter_type = 'and' === $relation ? 'must' : 'should'; | |
| 2617 | + /** | |
| 2618 | + * Filter indexable protected meta keys for posts | |
| 2619 | + * | |
| 2620 | + * @hook ep_prepare_meta_allowed_protected_keys | |
| 2621 | + * @param {array} $keys Allowed protected keys | |
| 2622 | + * @param {WP_Post} $post Post object | |
| 2623 | + * @since 1.7 | |
| 2624 | + * @return {array} New keys | |
| 2625 | + */ | |
| 2626 | + $allowed_protected_keys = apply_filters( 'ep_prepare_meta_allowed_protected_keys', [], $post ); | |
| 1759 | 2627 | |
| 1760 | - // Set the proper filter type and must_not filter, as needed | |
| 1761 | - if ( ! empty( $result['tax_must_not_filter'] ) ) { | |
| 1762 | - $tax_query['tax_filter'][] = [ | |
| 1763 | - 'bool' => [ | |
| 1764 | - $filter_type => $result['tax_filter'], | |
| 1765 | - 'must_not' => $result['tax_must_not_filter'], | |
| 1766 | - ], | |
| 1767 | - ]; | |
| 1768 | - } else { | |
| 1769 | - $tax_query['tax_filter'][] = [ | |
| 1770 | - 'bool' => [ | |
| 1771 | - $filter_type => $result['tax_filter'], | |
| 1772 | - ], | |
| 1773 | - ]; | |
| 1774 | - } | |
| 1775 | - } | |
| 2628 | + /** | |
| 2629 | + * Filter public keys to exclude from indexed post | |
| 2630 | + * | |
| 2631 | + * @hook ep_prepare_meta_excluded_public_keys | |
| 2632 | + * @param {array} $keys Excluded protected keys | |
| 2633 | + * @param {WP_Post} $post Post object | |
| 2634 | + * @since 1.7 | |
| 2635 | + * @return {array} New keys | |
| 2636 | + */ | |
| 2637 | + $excluded_public_keys = apply_filters( 'ep_prepare_meta_excluded_public_keys', [], $post ); | |
| 1776 | 2638 | |
| 1777 | - // Parse each individual tax query part | |
| 1778 | - $single_tax_query = $tax_queries; | |
| 1779 | - if ( ! empty( $single_tax_query['taxonomy'] ) ) { | |
| 1780 | - $terms = isset( $single_tax_query['terms'] ) ? (array) $single_tax_query['terms'] : array(); | |
| 1781 | - $field = ( ! empty( $single_tax_query['field'] ) ) ? $single_tax_query['field'] : 'term_id'; | |
| 2639 | + foreach ( $metas as $key => $value ) { | |
| 1782 | 2640 | |
| 1783 | - if ( 'name' === $field ) { | |
| 1784 | - $field = 'name.raw'; | |
| 1785 | - } | |
| 2641 | + $allow_index = false; | |
| 1786 | 2642 | |
| 1787 | - if ( 'slug' === $field ) { | |
| 1788 | - $terms = array_map( 'sanitize_title', $terms ); | |
| 2643 | + if ( is_protected_meta( $key ) ) { | |
| 2644 | + | |
| 2645 | + if ( true === $allowed_protected_keys || in_array( $key, $allowed_protected_keys, true ) ) { | |
| 2646 | + $allow_index = true; | |
| 1789 | 2647 | } |
| 2648 | + } elseif ( true !== $excluded_public_keys && ! in_array( $key, $excluded_public_keys, true ) ) { | |
| 1790 | 2649 | |
| 1791 | - // Set up our terms object | |
| 1792 | - $terms_obj = array( | |
| 1793 | - 'terms.' . $single_tax_query['taxonomy'] . '.' . $field => array_values( array_filter( $terms ) ), | |
| 1794 | - ); | |
| 2650 | + $allow_index = true; | |
| 2651 | + } | |
| 1795 | 2652 | |
| 1796 | - $operator = ( ! empty( $single_tax_query['operator'] ) ) ? strtolower( $single_tax_query['operator'] ) : 'in'; | |
| 2653 | + /** | |
| 2654 | + * Filter force whitelisting a meta key | |
| 2655 | + * | |
| 2656 | + * @hook ep_prepare_meta_whitelist_key | |
| 2657 | + * @param {bool} $whitelist True to whitelist key | |
| 2658 | + * @param {string} $key Meta key | |
| 2659 | + * @param {WP_Post} $post Post object | |
| 2660 | + * @return {bool} New whitelist value | |
| 2661 | + */ | |
| 2662 | + if ( true === $allow_index || apply_filters( 'ep_prepare_meta_whitelist_key', false, $key, $post ) ) { | |
| 2663 | + $filtered_metas[ $key ] = $value; | |
| 2664 | + } | |
| 2665 | + } | |
| 2666 | + return $filtered_metas; | |
| 2667 | + } | |
| 1797 | 2668 | |
| 1798 | - switch ( $operator ) { | |
| 1799 | - case 'exists': | |
| 1800 | - /** | |
| 1801 | - * add support for "EXISTS" operator | |
| 1802 | - * | |
| 1803 | - * @since 2.5 | |
| 1804 | - */ | |
| 1805 | - $tax_query['tax_filter'][]['bool'] = array( | |
| 1806 | - 'must' => array( | |
| 1807 | - array( | |
| 1808 | - 'exists' => array( | |
| 1809 | - 'field' => key( $terms_obj ), | |
| 1810 | - ), | |
| 1811 | - ), | |
| 1812 | - ), | |
| 1813 | - ); | |
| 2669 | + /** | |
| 2670 | + * Return all distinct meta fields in the database. | |
| 2671 | + * | |
| 2672 | + * @since 4.4.0 | |
| 2673 | + * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached. | |
| 2674 | + * @return array | |
| 2675 | + */ | |
| 2676 | + public function get_distinct_meta_field_keys_db( bool $force_refresh = false ): array { | |
| 2677 | + global $wpdb; | |
| 1814 | 2678 | |
| 1815 | - break; | |
| 1816 | - case 'not exists': | |
| 1817 | - /** | |
| 1818 | - * add support for "NOT EXISTS" operator | |
| 1819 | - * | |
| 1820 | - * @since 2.5 | |
| 1821 | - */ | |
| 1822 | - $tax_query['tax_filter'][]['bool'] = array( | |
| 1823 | - 'must_not' => array( | |
| 1824 | - array( | |
| 1825 | - 'exists' => array( | |
| 1826 | - 'field' => key( $terms_obj ), | |
| 1827 | - ), | |
| 1828 | - ), | |
| 1829 | - ), | |
| 1830 | - ); | |
| 2679 | + /** | |
| 2680 | + * Short-circuits the process of getting distinct meta keys from the database. | |
| 2681 | + * | |
| 2682 | + * Returning a non-null value will effectively short-circuit the function. | |
| 2683 | + * | |
| 2684 | + * @since 4.4.0 | |
| 2685 | + * @hook ep_post_pre_meta_keys_db | |
| 2686 | + * @param {null} $meta_keys Distinct meta keys array | |
| 2687 | + * @return {null|array} Distinct meta keys array or `null` to keep default behavior | |
| 2688 | + */ | |
| 2689 | + $pre_meta_keys = apply_filters( 'ep_post_pre_meta_keys_db', null ); | |
| 2690 | + if ( null !== $pre_meta_keys ) { | |
| 2691 | + return $pre_meta_keys; | |
| 2692 | + } | |
| 1831 | 2693 | |
| 1832 | - break; | |
| 1833 | - case 'not in': | |
| 1834 | - /** | |
| 1835 | - * add support for "NOT IN" operator | |
| 1836 | - * | |
| 1837 | - * @since 2.1 | |
| 1838 | - */ | |
| 1839 | - // If "NOT IN" than it should filter as must_not | |
| 1840 | - $tax_query['tax_must_not_filter'][]['terms'] = $terms_obj; | |
| 2694 | + $cache_key = 'ep_meta_field_keys'; | |
| 1841 | 2695 | |
| 1842 | - break; | |
| 1843 | - case 'and': | |
| 1844 | - /** | |
| 1845 | - * add support for "and" operator | |
| 1846 | - * | |
| 1847 | - * @since 2.4 | |
| 1848 | - */ | |
| 1849 | - $and_nest = array( | |
| 1850 | - 'bool' => array( | |
| 1851 | - 'must' => array(), | |
| 1852 | - ), | |
| 1853 | - ); | |
| 2696 | + if ( ! $force_refresh ) { | |
| 2697 | + $cached = get_transient( $cache_key ); | |
| 2698 | + if ( false !== $cached ) { | |
| 2699 | + $cached = (array) json_decode( (string) $cached ); | |
| 2700 | + /* this filter is documented below */ | |
| 2701 | + return (array) apply_filters( 'ep_post_meta_keys_db', $cached ); | |
| 2702 | + } | |
| 2703 | + } | |
| 1854 | 2704 | |
| 1855 | - foreach ( $terms as $term ) { | |
| 1856 | - $and_nest['bool']['must'][] = array( | |
| 1857 | - 'terms' => array( | |
| 1858 | - 'terms.' . $single_tax_query['taxonomy'] . '.' . $field => (array) $term, | |
| 1859 | - ), | |
| 1860 | - ); | |
| 1861 | - } | |
| 2705 | + /** | |
| 2706 | + * To avoid running a too expensive SQL query, we run a query getting all public keys | |
| 2707 | + * and only the private keys allowed by the `ep_prepare_meta_allowed_protected_keys` filter. | |
| 2708 | + * This query does not order by on purpose, as that also brings a performance penalty. | |
| 2709 | + */ | |
| 2710 | + $allowed_protected_keys = apply_filters( 'ep_prepare_meta_allowed_protected_keys', [], new \WP_Post( (object) [] ) ); | |
| 2711 | + $allowed_protected_keys_sql = ''; | |
| 2712 | + if ( ! empty( $allowed_protected_keys ) ) { | |
| 2713 | + $placeholders = implode( ',', array_fill( 0, count( $allowed_protected_keys ), '%s' ) ); | |
| 2714 | + $allowed_protected_keys_sql = " OR meta_key IN ( {$placeholders} ) "; | |
| 2715 | + } | |
| 1862 | 2716 | |
| 1863 | - $tax_query['tax_filter'][] = $and_nest; | |
| 2717 | + // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber | |
| 2718 | + $meta_keys = $wpdb->get_col( | |
| 2719 | + $wpdb->prepare( | |
| 2720 | + "SELECT DISTINCT meta_key | |
| 2721 | + FROM {$wpdb->postmeta} | |
| 2722 | + WHERE meta_key NOT LIKE %s {$allowed_protected_keys_sql} | |
| 2723 | + LIMIT 800", | |
| 2724 | + '\_%', | |
| 2725 | + ...$allowed_protected_keys | |
| 2726 | + ) | |
| 2727 | + ); | |
| 2728 | + // phpcs:enable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber | |
| 1864 | 2729 | |
| 1865 | - break; | |
| 1866 | - case 'in': | |
| 1867 | - default: | |
| 1868 | - /** | |
| 1869 | - * Default to IN operator | |
| 1870 | - */ | |
| 1871 | - // Add the tax query filter | |
| 1872 | - $tax_query['tax_filter'][]['terms'] = $terms_obj; | |
| 2730 | + sort( $meta_keys ); | |
| 1873 | 2731 | |
| 1874 | - break; | |
| 1875 | - } | |
| 2732 | + // Make sure the size of the transient will not be bigger than 1MB | |
| 2733 | + do { | |
| 2734 | + $transient_size = strlen( wp_json_encode( $meta_keys ) ); | |
| 2735 | + if ( $transient_size >= MB_IN_BYTES ) { | |
| 2736 | + array_pop( $meta_keys ); | |
| 2737 | + } else { | |
| 2738 | + break; | |
| 1876 | 2739 | } |
| 1877 | - } | |
| 2740 | + } while ( true ); | |
| 2741 | + set_transient( $cache_key, wp_json_encode( $meta_keys ), DAY_IN_SECONDS ); | |
| 1878 | 2742 | |
| 1879 | - return $tax_query; | |
| 2743 | + /** | |
| 2744 | + * Filter the distinct meta keys fetched from the database. | |
| 2745 | + * | |
| 2746 | + * @since 4.4.0 | |
| 2747 | + * @hook ep_post_meta_keys_db | |
| 2748 | + * @param {array} $meta_keys Distinct meta keys array | |
| 2749 | + * @return {array} New distinct meta keys array | |
| 2750 | + */ | |
| 2751 | + return (array) apply_filters( 'ep_post_meta_keys_db', $meta_keys ); | |
| 1880 | 2752 | } |
| 1881 | 2753 | |
| 1882 | 2754 | /** |
| 1883 | - * Parse an 'order' query variable and cast it to ASC or DESC as necessary. | |
| 2755 | + * Return all distinct meta fields in the database per post type. | |
| 1884 | 2756 | * |
| 1885 | - * @since 1.1 | |
| 1886 | - * @access protected | |
| 1887 | - * | |
| 1888 | - * @param string $order The 'order' query variable. | |
| 1889 | - * @return string The sanitized 'order' query variable. | |
| 2757 | + * @since 4.4.0 | |
| 2758 | + * @param string $post_type Post type slug | |
| 2759 | + * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached. | |
| 2760 | + * @return array | |
| 1890 | 2761 | */ |
| 1891 | - protected function parse_order( $order ) { | |
| 1892 | - // Core will always set sort order to DESC for any invalid value, | |
| 1893 | - // so we can't do any automated testing of this function. | |
| 1894 | - // @codeCoverageIgnoreStart | |
| 1895 | - if ( ! is_string( $order ) || empty( $order ) ) { | |
| 1896 | - return 'desc'; | |
| 2762 | + public function get_distinct_meta_field_keys_db_per_post_type( string $post_type, bool $force_refresh = false ): array { | |
| 2763 | + $allowed_screen = 'status-report' === \ElasticPress\Screen::factory()->get_current_screen(); | |
| 2764 | + | |
| 2765 | + /** | |
| 2766 | + * Filter if the current screen is allowed or not to use the function. | |
| 2767 | + * | |
| 2768 | + * This method can be too resource intensive, use it with caution. | |
| 2769 | + * | |
| 2770 | + * @since 4.4.0 | |
| 2771 | + * @hook ep_post_meta_keys_db_per_post_type_allowed_screen | |
| 2772 | + * @param {bool} $allowed_screen Whether this is an allowed screen or not. | |
| 2773 | + * @return {bool} New value of $allowed_screen | |
| 2774 | + */ | |
| 2775 | + if ( ! apply_filters( 'ep_post_meta_keys_db_per_post_type_allowed_screen', $allowed_screen ) ) { | |
| 2776 | + _doing_it_wrong( | |
| 2777 | + __METHOD__, | |
| 2778 | + esc_html__( 'This method should not be called outside specific pages. Use the `ep_post_meta_keys_db_per_post_type_allowed_screen` filter if you need to use it in your custom screen.' ), | |
| 2779 | + 'ElasticPress 4.4.0' | |
| 2780 | + ); | |
| 2781 | + return []; | |
| 1897 | 2782 | } |
| 1898 | - // @codeCoverageIgnoreEnd | |
| 1899 | 2783 | |
| 1900 | - if ( 'ASC' === strtoupper( $order ) ) { | |
| 1901 | - return 'asc'; | |
| 1902 | - } else { | |
| 1903 | - return 'desc'; | |
| 2784 | + /** | |
| 2785 | + * Short-circuits the process of getting distinct meta keys from the database per post type. | |
| 2786 | + * | |
| 2787 | + * Returning a non-null value will effectively short-circuit the function. | |
| 2788 | + * | |
| 2789 | + * @since 4.4.0 | |
| 2790 | + * @hook ep_post_pre_meta_keys_db_per_post_type | |
| 2791 | + * @param {null} $meta_keys Distinct meta keys array | |
| 2792 | + * @param {string} $post_type Post type slug | |
| 2793 | + * @return {null|array} Distinct meta keys array or `null` to keep default behavior | |
| 2794 | + */ | |
| 2795 | + $pre_meta_keys = apply_filters( 'ep_post_pre_meta_keys_db_per_post_type', null, $post_type ); | |
| 2796 | + if ( null !== $pre_meta_keys ) { | |
| 2797 | + return $pre_meta_keys; | |
| 1904 | 2798 | } |
| 2799 | + | |
| 2800 | + $cache_key = 'ep_meta_field_keys_' . $post_type; | |
| 2801 | + | |
| 2802 | + if ( ! $force_refresh ) { | |
| 2803 | + $cached = get_transient( $cache_key ); | |
| 2804 | + if ( false !== $cached ) { | |
| 2805 | + $cached = (array) json_decode( (string) $cached ); | |
| 2806 | + /* this filter is documented below */ | |
| 2807 | + return (array) apply_filters( 'ep_post_meta_keys_db_per_post_type', $cached, $post_type ); | |
| 2808 | + } | |
| 2809 | + } | |
| 2810 | + | |
| 2811 | + $meta_keys = []; | |
| 2812 | + $post_ids_batches = $this->get_lazy_post_type_ids( $post_type ); | |
| 2813 | + foreach ( $post_ids_batches as $post_ids ) { | |
| 2814 | + $new_meta_keys = $this->get_meta_keys_from_post_ids( $post_ids ); | |
| 2815 | + | |
| 2816 | + $meta_keys = array_unique( array_merge( $meta_keys, $new_meta_keys ) ); | |
| 2817 | + } | |
| 2818 | + | |
| 2819 | + // Make sure the size of the transient will not be bigger than 1MB | |
| 2820 | + do { | |
| 2821 | + $transient_size = strlen( wp_json_encode( $meta_keys ) ); | |
| 2822 | + if ( $transient_size >= MB_IN_BYTES ) { | |
| 2823 | + array_pop( $meta_keys ); | |
| 2824 | + } else { | |
| 2825 | + break; | |
| 2826 | + } | |
| 2827 | + } while ( true ); | |
| 2828 | + set_transient( $cache_key, wp_json_encode( $meta_keys ), DAY_IN_SECONDS ); | |
| 2829 | + | |
| 2830 | + /** | |
| 2831 | + * Filter the distinct meta keys fetched from the database per post type. | |
| 2832 | + * | |
| 2833 | + * @since 4.4.0 | |
| 2834 | + * @hook ep_post_meta_keys_db_per_post_type | |
| 2835 | + * @param {array} $meta_keys Distinct meta keys array | |
| 2836 | + * @param {string} $post_type Post type slug | |
| 2837 | + * @return {array} New distinct meta keys array | |
| 2838 | + */ | |
| 2839 | + return (array) apply_filters( 'ep_post_meta_keys_db_per_post_type', $meta_keys, $post_type ); | |
| 1905 | 2840 | } |
| 1906 | 2841 | |
| 1907 | 2842 | /** |
| 1908 | - * Convert the alias to a properly-prefixed sort value. | |
| 2843 | + * Return all distinct meta fields in the database per post type. | |
| 1909 | 2844 | * |
| 1910 | - * @since 1.1 | |
| 1911 | - * @access protected | |
| 1912 | - * | |
| 1913 | - * @param string $orderbys Alias or path for the field to order by. | |
| 1914 | - * @param string $default_order Default order direction | |
| 1915 | - * @param array $args Query args | |
| 2845 | + * @since 4.4.0 | |
| 2846 | + * @param string $post_type Post type slug | |
| 2847 | + * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached. | |
| 1916 | 2848 | * @return array |
| 1917 | 2849 | */ |
| 1918 | - protected function parse_orderby( $orderbys, $default_order, $args ) { | |
| 1919 | - $orderbys = $this->get_orderby_array( $orderbys ); | |
| 2850 | + public function get_indexable_meta_keys_per_post_type( string $post_type, bool $force_refresh = false ): array { | |
| 2851 | + $mock_post = new \WP_Post( (object) [ 'post_type' => $post_type ] ); | |
| 2852 | + $meta_keys = $this->get_distinct_meta_field_keys_db_per_post_type( $post_type, $force_refresh ); | |
| 1920 | 2853 | |
| 1921 | - $sort = []; | |
| 2854 | + $fake_meta_values = array_combine( $meta_keys, array_fill( 0, count( $meta_keys ), 'test-value' ) ); | |
| 2855 | + $filtered_meta = apply_filters( 'ep_prepare_meta_data', $fake_meta_values, $mock_post ); | |
| 1922 | 2856 | |
| 1923 | - foreach ( $orderbys as $key => $value ) { | |
| 1924 | - if ( is_string( $key ) ) { | |
| 1925 | - $orderby_clause = $key; | |
| 1926 | - $order = $value; | |
| 1927 | - } else { | |
| 1928 | - $orderby_clause = $value; | |
| 1929 | - $order = $default_order; | |
| 2857 | + return array_filter( | |
| 2858 | + array_keys( $filtered_meta ), | |
| 2859 | + function ( $meta_key ) use ( $mock_post ) { | |
| 2860 | + return $this->is_meta_allowed( $meta_key, $mock_post ); | |
| 1930 | 2861 | } |
| 1931 | - | |
| 1932 | - if ( ! empty( $orderby_clause ) && 'rand' !== $orderby_clause ) { | |
| 1933 | - if ( 'relevance' === $orderby_clause ) { | |
| 1934 | - $sort[] = array( | |
| 1935 | - '_score' => array( | |
| 1936 | - 'order' => $order, | |
| 1937 | - ), | |
| 1938 | - ); | |
| 1939 | - } elseif ( 'date' === $orderby_clause ) { | |
| 1940 | - $sort[] = array( | |
| 1941 | - 'post_date' => array( | |
| 1942 | - 'order' => $order, | |
| 1943 | - ), | |
| 1944 | - ); | |
| 1945 | - } elseif ( 'type' === $orderby_clause ) { | |
| 1946 | - $sort[] = array( | |
| 1947 | - 'post_type.raw' => array( | |
| 1948 | - 'order' => $order, | |
| 1949 | - ), | |
| 1950 | - ); | |
| 1951 | - } elseif ( 'modified' === $orderby_clause ) { | |
| 1952 | - $sort[] = array( | |
| 1953 | - 'post_modified' => array( | |
| 1954 | - 'order' => $order, | |
| 1955 | - ), | |
| 1956 | - ); | |
| 1957 | - } elseif ( 'name' === $orderby_clause ) { | |
| 1958 | - $sort[] = array( | |
| 1959 | - 'post_' . $orderby_clause . '.raw' => array( | |
| 1960 | - 'order' => $order, | |
| 1961 | - ), | |
| 1962 | - ); | |
| 1963 | - } elseif ( 'title' === $orderby_clause ) { | |
| 1964 | - $sort[] = array( | |
| 1965 | - 'post_' . $orderby_clause . '.sortable' => array( | |
| 1966 | - 'order' => $order, | |
| 1967 | - ), | |
| 1968 | - ); | |
| 1969 | - } elseif ( 'meta_value' === $orderby_clause ) { | |
| 1970 | - if ( ! empty( $args['meta_key'] ) ) { | |
| 1971 | - $sort[] = array( | |
| 1972 | - 'meta.' . $args['meta_key'] . '.raw' => array( | |
| 1973 | - 'order' => $order, | |
| 1974 | - ), | |
| 1975 | - ); | |
| 1976 | - } | |
| 1977 | - } elseif ( 'meta_value_num' === $orderby_clause ) { | |
| 1978 | - if ( ! empty( $args['meta_key'] ) ) { | |
| 1979 | - $sort[] = array( | |
| 1980 | - 'meta.' . $args['meta_key'] . '.long' => array( | |
| 1981 | - 'order' => $order, | |
| 1982 | - ), | |
| 1983 | - ); | |
| 1984 | - } | |
| 1985 | - } else { | |
| 1986 | - $sort[] = array( | |
| 1987 | - $orderby_clause => array( | |
| 1988 | - 'order' => $order, | |
| 1989 | - ), | |
| 1990 | - ); | |
| 1991 | - } | |
| 1992 | - } | |
| 1993 | - } | |
| 1994 | - | |
| 1995 | - return $sort; | |
| 2862 | + ); | |
| 1996 | 2863 | } |
| 1997 | 2864 | |
| 1998 | 2865 | /** |
| 1999 | - * Get Order by args Array | |
| 2866 | + * Return the meta keys that will (possibly) be indexed. | |
| 2000 | 2867 | * |
| 2001 | - * @param string|array $orderbys Order by string or array | |
| 2002 | - * @since 2.1 | |
| 2868 | + * This function gets all the meta keys in the database, creates a fake post without a type and with all the meta fields, | |
| 2869 | + * runs the `ep_prepare_meta_data` filter against it and checks if meta keys are allowed or not. | |
| 2870 | + * Although it provides a good indicator, it is not 100% correct as developers could create code using the | |
| 2871 | + * `ep_prepare_meta_data` filter that would depend on "real" data. | |
| 2872 | + * | |
| 2873 | + * @since 4.4.0 | |
| 2874 | + * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached. | |
| 2003 | 2875 | * @return array |
| 2004 | 2876 | */ |
| 2005 | - protected function get_orderby_array( $orderbys ) { | |
| 2006 | - if ( ! is_array( $orderbys ) ) { | |
| 2007 | - $orderbys = explode( ' ', $orderbys ); | |
| 2008 | - } | |
| 2877 | + public function get_predicted_indexable_meta_keys( bool $force_refresh = false ): array { | |
| 2878 | + $empty_post = new \WP_Post( (object) [] ); | |
| 2879 | + $meta_keys = $this->get_distinct_meta_field_keys_db( $force_refresh ); | |
| 2009 | 2880 | |
| 2010 | - return $orderbys; | |
| 2881 | + $fake_meta_values = array_combine( | |
| 2882 | + $meta_keys, | |
| 2883 | + array_fill( 0, count( $meta_keys ), $this->get_test_meta_value() ) | |
| 2884 | + ); | |
| 2885 | + $filtered_meta = apply_filters( 'ep_prepare_meta_data', $fake_meta_values, $empty_post ); | |
| 2886 | + | |
| 2887 | + $all_keys = array_filter( | |
| 2888 | + array_keys( $filtered_meta ), | |
| 2889 | + function ( $meta_key ) use ( $empty_post ) { | |
| 2890 | + return $this->is_meta_allowed( $meta_key, $empty_post ); | |
| 2891 | + } | |
| 2892 | + ); | |
| 2893 | + | |
| 2894 | + sort( $all_keys ); | |
| 2895 | + | |
| 2896 | + return $all_keys; | |
| 2011 | 2897 | } |
| 2012 | 2898 | |
| 2013 | 2899 | /** |
| 2014 | - * Given a mapping content, try to determine the version used. | |
| 2900 | + * Return the value used to fill meta fields while predicting indexable content. | |
| 2015 | 2901 | * |
| 2016 | - * @since 3.6.3 | |
| 2017 | - * | |
| 2018 | - * @param array $mapping Mapping content. | |
| 2019 | - * @param string $index Index name | |
| 2020 | - * @return string Version of the mapping being used. | |
| 2902 | + * @since 5.1.0 | |
| 2903 | + * @return string | |
| 2021 | 2904 | */ |
| 2022 | - protected function determine_mapping_version_based_on_existing( $mapping, $index ) { | |
| 2023 | - if ( isset( $mapping[ $index ]['mappings']['post']['_meta']['mapping_version'] ) ) { | |
| 2024 | - return $mapping[ $index ]['mappings']['post']['_meta']['mapping_version']; | |
| 2025 | - } | |
| 2026 | - if ( isset( $mapping[ $index ]['mappings']['_meta']['mapping_version'] ) ) { | |
| 2027 | - return $mapping[ $index ]['mappings']['_meta']['mapping_version']; | |
| 2028 | - } | |
| 2029 | - | |
| 2905 | + public function get_test_meta_value(): string { | |
| 2030 | 2906 | /** |
| 2031 | - * Check for 7-0 mapping. | |
| 2032 | - * If mapping has a `post` type, it can't be ES 7, as mapping types were removed in that release. | |
| 2907 | + * Filter the value used to fill meta fields while predicting indexable content. | |
| 2033 | 2908 | * |
| 2034 | - * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html | |
| 2909 | + * @hook ep_post_test_meta_value | |
| 2910 | + * @since 5.1.0 | |
| 2911 | + * @param {string} $test_meta_value The test meta value. Default: test-value | |
| 2912 | + * @return {string} New test meta value | |
| 2035 | 2913 | */ |
| 2036 | - if ( ! isset( $mapping[ $index ]['mappings']['post'] ) ) { | |
| 2037 | - return '7-0.php'; | |
| 2038 | - } | |
| 2914 | + return (string) apply_filters( 'ep_post_test_meta_value', 'test-value' ); | |
| 2915 | + } | |
| 2039 | 2916 | |
| 2040 | - $post_mapping = $mapping[ $index ]['mappings']['post']; | |
| 2917 | + /** | |
| 2918 | + * Given a post type, *yields* their Post IDs. | |
| 2919 | + * | |
| 2920 | + * If post IDs are found, this function will return a PHP Generator. To avoid timeout, it will yield 8 groups or 11,000 IDs. | |
| 2921 | + * | |
| 2922 | + * @since 4.4.0 | |
| 2923 | + * @see https://www.php.net/manual/en/language.generators.overview.php | |
| 2924 | + * @param string $post_type The post type slug | |
| 2925 | + * @return iterator | |
| 2926 | + */ | |
| 2927 | + protected function get_lazy_post_type_ids( string $post_type ) { | |
| 2928 | + global $wpdb; | |
| 2041 | 2929 | |
| 2042 | - /** | |
| 2043 | - * Starting at this point, our tests rely on the post_title.fields.sortable field. | |
| 2044 | - * As this field is present in all our mappings, if this field is not present in | |
| 2045 | - * the mapping, this is a custom mapping. | |
| 2046 | - * | |
| 2047 | - * To have this code working with custom mappings, use the `ep_post_mapping_version_determined` filter. | |
| 2048 | - */ | |
| 2049 | - if ( ! isset( $post_mapping['properties']['post_title']['fields']['sortable'] ) ) { | |
| 2050 | - return 'unknown'; | |
| 2930 | + $total = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery | |
| 2931 | + $wpdb->prepare( | |
| 2932 | + "SELECT count(*) FROM {$wpdb->posts} WHERE post_type = %s", | |
| 2933 | + $post_type | |
| 2934 | + ) | |
| 2935 | + ); | |
| 2936 | + | |
| 2937 | + if ( ! $total ) { | |
| 2938 | + return []; | |
| 2051 | 2939 | } |
| 2052 | 2940 | |
| 2053 | - $post_title_sortable = $post_mapping['properties']['post_title']['fields']['sortable']; | |
| 2054 | - | |
| 2055 | 2941 | /** |
| 2056 | - * Check for 5-2 mapping. | |
| 2057 | - * Normalizers on keyword fields were only made available in ES 5.2 | |
| 2942 | + * Filter the number of IDs to be fetched per page to discover distinct meta fields per post type. | |
| 2058 | 2943 | * |
| 2059 | - * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.2/release-notes-5.2.0.html | |
| 2944 | + * @hook ep_post_meta_by_type_ids_per_page | |
| 2945 | + * @since 4.4.0 | |
| 2946 | + * @param {int} $per_page Number of IDs | |
| 2947 | + * @param {string} $post_type The post type slug | |
| 2948 | + * @return {string} New number of IDs | |
| 2060 | 2949 | */ |
| 2061 | - if ( isset( $post_title_sortable['normalizer'] ) ) { | |
| 2062 | - return '5-2.php'; | |
| 2063 | - } | |
| 2950 | + $per_page = apply_filters( 'ep_post_meta_by_type_ids_per_page', 11000, $post_type ); | |
| 2064 | 2951 | |
| 2952 | + $pages = min( ceil( $total / $per_page ), 8 ); | |
| 2953 | + | |
| 2065 | 2954 | /** |
| 2066 | - * Check for 5-0 mapping. | |
| 2067 | - * `keyword` fields were only made available in ES 5.0 | |
| 2955 | + * Filter the number of times EP will fetch IDs from the database | |
| 2068 | 2956 | * |
| 2069 | - * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.0/release-notes-5.0.0.html | |
| 2957 | + * @hook ep_post_meta_by_type_number_of_pages | |
| 2958 | + * @since 4.4.0 | |
| 2959 | + * @param {int} $pages Number of "pages" (not WP post type) | |
| 2960 | + * @param {int} $per_page Number of IDs per page | |
| 2961 | + * @param {string} $post_type The post type slug | |
| 2962 | + * @return {string} New number of pages | |
| 2070 | 2963 | */ |
| 2071 | - if ( 'keyword' === $post_title_sortable['type'] ) { | |
| 2072 | - return '5-0.php'; | |
| 2073 | - } | |
| 2964 | + $pages = apply_filters( 'ep_post_meta_by_type_number_of_pages', $pages, $per_page, $post_type ); | |
| 2074 | 2965 | |
| 2075 | - /** | |
| 2076 | - * Check for pre-5-0 mapping. | |
| 2077 | - * `string` fields were deprecated in ES 5.0 in favor of text/keyword | |
| 2078 | - * | |
| 2079 | - * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.0/release-notes-5.0.0.html | |
| 2080 | - */ | |
| 2081 | - if ( 'string' === $post_title_sortable['type'] ) { | |
| 2082 | - return 'pre-5-0.php'; | |
| 2966 | + for ( $page = 0; $page < $pages; $page++ ) { | |
| 2967 | + $start = $per_page * $page; | |
| 2968 | + $ids = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery | |
| 2969 | + $wpdb->prepare( | |
| 2970 | + "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s LIMIT %d, %d", | |
| 2971 | + $post_type, | |
| 2972 | + $start, | |
| 2973 | + $per_page | |
| 2974 | + ) | |
| 2975 | + ); | |
| 2976 | + yield $ids; | |
| 2083 | 2977 | } |
| 2084 | - | |
| 2085 | - return 'unknown'; | |
| 2086 | 2978 | } |
| 2087 | 2979 | |
| 2088 | 2980 | /** |
| 2089 | - * Given ES args, add aggregations to it. | |
| 2981 | + * Given a set of post IDs, return distinct meta keys associated with them. | |
| 2090 | 2982 | * |
| 2091 | - * @since 4.1.0 | |
| 2092 | - * @param array $formatted_args Formatted Elasticsearch query. | |
| 2093 | - * @param array $agg Aggregation data. | |
| 2094 | - * @param boolean $use_filters Whether filters should be used or not. | |
| 2095 | - * @param array $filter Filters defined so far. | |
| 2096 | - * @return array Formatted Elasticsearch query with the aggregation added. | |
| 2983 | + * @since 4.4.0 | |
| 2984 | + * @param array $post_ids Set of post IDs | |
| 2985 | + * @return array | |
| 2097 | 2986 | */ |
| 2098 | - protected function apply_aggregations( $formatted_args, $agg, $use_filters, $filter ) { | |
| 2099 | - if ( empty( $agg['aggs'] ) ) { | |
| 2100 | - return $formatted_args; | |
| 2987 | + protected function get_meta_keys_from_post_ids( array $post_ids ): array { | |
| 2988 | + global $wpdb; | |
| 2989 | + | |
| 2990 | + if ( empty( $post_ids ) ) { | |
| 2991 | + return []; | |
| 2101 | 2992 | } |
| 2102 | 2993 | |
| 2103 | - // Add a name to the aggregation if it was passed through | |
| 2104 | - $agg_name = ( ! empty( $agg['name'] ) ) ? $agg['name'] : 'aggregation_name'; | |
| 2994 | + $placeholders = implode( ',', array_fill( 0, count( $post_ids ), '%d' ) ); | |
| 2995 | + $meta_keys = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery | |
| 2996 | + $wpdb->prepare( | |
| 2997 | + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare | |
| 2998 | + "SELECT DISTINCT meta_key FROM {$wpdb->postmeta} WHERE post_id IN ( {$placeholders} )", | |
| 2999 | + $post_ids | |
| 3000 | + ) | |
| 3001 | + ); | |
| 2105 | 3002 | |
| 2106 | - // Add/use the filter if warranted | |
| 2107 | - if ( isset( $agg['use-filter'] ) && false !== $agg['use-filter'] && $use_filters ) { | |
| 3003 | + return $meta_keys; | |
| 3004 | + } | |
| 2108 | 3005 | |
| 2109 | - // If a filter is being used, use it on the aggregation as well to receive relevant information to the query | |
| 2110 | - $formatted_args['aggs'][ $agg_name ]['filter'] = $filter; | |
| 2111 | - $formatted_args['aggs'][ $agg_name ]['aggs'] = $agg['aggs']; | |
| 3006 | + /** | |
| 3007 | + * Add a `term_suggest` field to the mapping. | |
| 3008 | + * | |
| 3009 | + * This method assumes the `edge_ngram_analyzer` analyzer was already added to the mapping. | |
| 3010 | + * | |
| 3011 | + * @since 4.5.0 | |
| 3012 | + * @param array $mapping The mapping array | |
| 3013 | + * @return array | |
| 3014 | + */ | |
| 3015 | + public function add_term_suggest_field( array $mapping ): array { | |
| 3016 | + if ( version_compare( (string) Elasticsearch::factory()->get_elasticsearch_version(), '7.0', '<' ) ) { | |
| 3017 | + $mapping_properties = &$mapping['mappings']['post']['properties']; | |
| 2112 | 3018 | } else { |
| 2113 | - $formatted_args['aggs'][ $agg_name ] = $agg['aggs']; | |
| 3019 | + $mapping_properties = &$mapping['mappings']['properties']; | |
| 2114 | 3020 | } |
| 2115 | 3021 | |
| 2116 | - return $formatted_args; | |
| 3022 | + $text_type = $mapping_properties['post_content']['type']; | |
| 3023 | + | |
| 3024 | + $mapping_properties['term_suggest'] = array( | |
| 3025 | + 'type' => $text_type, | |
| 3026 | + 'analyzer' => 'edge_ngram_analyzer', | |
| 3027 | + 'search_analyzer' => 'standard', | |
| 3028 | + ); | |
| 3029 | + | |
| 3030 | + return $mapping; | |
| 2117 | 3031 | } |
| 2118 | 3032 | |
| 2119 | 3033 | /** |
| 2120 | - * Get the search algorithm that should be used. | |
| 3034 | + * Return all meta data added to the Weighting Dashboard plus all allowed keys via code. | |
| 2121 | 3035 | * |
| 2122 | - * @since 4.3.0 | |
| 2123 | - * @param string $search_text Search term(s) | |
| 2124 | - * @param array $search_fields Search fields | |
| 2125 | - * @param array $query_vars Query vars | |
| 2126 | - * @return SearchAlgorithm Instance of search algorithm to be used | |
| 3036 | + * @since 5.1.4 | |
| 3037 | + * @return array | |
| 2127 | 3038 | */ |
| 2128 | - public function get_search_algorithm( string $search_text, array $search_fields, array $query_vars ) : \ElasticPress\SearchAlgorithm { | |
| 2129 | - $search_algorithm_version_option = \ElasticPress\Utils\get_option( 'ep_search_algorithm_version', '4.0' ); | |
| 3039 | + public function get_all_allowed_metas_manual(): array { | |
| 3040 | + $post_types = \ElasticPress\Indexables::factory()->get( 'post' )->get_indexable_post_types(); | |
| 3041 | + $search_feature = \ElasticPress\Features::factory()->get_registered_feature( 'search' ); | |
| 3042 | + $weighting = $search_feature->weighting->get_weighting_configuration_with_defaults(); | |
| 3043 | + $fake_post = new \WP_Post( new \stdClass() ); | |
| 2130 | 3044 | |
| 2131 | - /** | |
| 2132 | - * Filter the algorithm version to be used. | |
| 2133 | - * | |
| 2134 | - * @since 3.5 | |
| 2135 | - * @hook ep_search_algorithm_version | |
| 2136 | - * @param {string} $search_algorithm_version Algorithm version. | |
| 2137 | - * @return {string} New algorithm version | |
| 2138 | - */ | |
| 2139 | - $search_algorithm = apply_filters( 'ep_search_algorithm_version', $search_algorithm_version_option ); | |
| 3045 | + $all_allowed_metas = []; | |
| 3046 | + foreach ( $post_types as $post_type ) { | |
| 3047 | + $fake_post->post_type = $post_type; | |
| 3048 | + $allowed_protected_keys = apply_filters( 'ep_prepare_meta_allowed_protected_keys', [], $fake_post ); | |
| 2140 | 3049 | |
| 2141 | - /** | |
| 2142 | - * Filter the search algorithm to be used | |
| 2143 | - * | |
| 2144 | - * @hook ep_{$indexable_slug}_search_algorithm | |
| 2145 | - * @since 4.3.0 | |
| 2146 | - * @param {string} $search_algorithm Slug of the search algorithm used as fallback | |
| 2147 | - * @param {string} $search_term Search term | |
| 2148 | - * @param {array} $search_fields Fields to be searched | |
| 2149 | - * @param {array} $query_vars Query variables | |
| 2150 | - * @return {string} New search algorithm slug | |
| 2151 | - */ | |
| 2152 | - $search_algorithm = apply_filters( "ep_{$this->slug}_search_algorithm", $search_algorithm, $search_text, $search_fields, $query_vars ); | |
| 3050 | + $selected_keys = []; | |
| 3051 | + if ( ! empty( $weighting[ $post_type ] ) ) { | |
| 3052 | + $selected_keys = array_map( | |
| 3053 | + function ( $field ) { | |
| 3054 | + if ( false === strpos( $field, 'meta.' ) ) { | |
| 3055 | + return null; | |
| 3056 | + } | |
| 3057 | + $field_name_parts = explode( '.', $field ); | |
| 3058 | + return $field_name_parts[1]; | |
| 3059 | + }, | |
| 3060 | + array_keys( $weighting[ $post_type ] ) | |
| 3061 | + ); | |
| 3062 | + $selected_keys = array_filter( $selected_keys ); | |
| 3063 | + } | |
| 2153 | 3064 | |
| 2154 | - return \ElasticPress\SearchAlgorithms::factory()->get( $search_algorithm ); | |
| 3065 | + $allowed_keys = apply_filters( 'ep_prepare_meta_allowed_keys', array_merge( $allowed_protected_keys, $selected_keys ), $fake_post ); | |
| 3066 | + | |
| 3067 | + $all_allowed_metas = array_merge( $all_allowed_metas, $allowed_keys ); | |
| 3068 | + } | |
| 3069 | + | |
| 3070 | + return array_unique( $all_allowed_metas ); | |
| 3071 | + } | |
| 3072 | + | |
| 3073 | + /** | |
| 3074 | + * Sets the ORDER BY clause to sort posts by post ID in descending order. | |
| 3075 | + * | |
| 3076 | + * @return string The modified order by clause. | |
| 3077 | + * | |
| 3078 | + * @since 5.2.0 | |
| 3079 | + */ | |
| 3080 | + public function set_posts_orderby(): string { | |
| 3081 | + global $wpdb; | |
| 3082 | + return "{$wpdb->posts}.ID DESC"; | |
| 2155 | 3083 | } |
| 2156 | 3084 | } |