PluginProbe
ElasticPress / 5.3.5
ElasticPress v5.3.5
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
← All changes | includes/classes/Indexable/Post/Post.php +352 -159 4.7.25.3.5 View file →
@@ -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,10 +7,10 @@
7 7 */
8 8
9 9 namespace ElasticPress\Indexable\Post;
10 10
11 -use \WP_Query;
12 -use \WP_User;
11 +use WP_Query;
12 +use WP_User;
13 13 use ElasticPress\Elasticsearch;
14 14 use ElasticPress\Indexable;
15 15
16 16 if ( ! defined( 'ABSPATH' ) ) {
@@ -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;
@@ -316,18 +324,12 @@
316 324 $es_version = apply_filters( 'ep_fallback_elasticsearch_version', '2.0' );
317 325 }
318 326 $es_version = (string) $es_version;
319 327
320 - $mapping_file = '5-2.php';
328 + $mapping_file = '7-0.php';
321 329
322 - if ( ! $es_version || version_compare( $es_version, '5.0' ) < 0 ) {
323 - $mapping_file = 'pre-5-0.php';
324 - } elseif ( version_compare( $es_version, '5.0', '>=' ) && version_compare( $es_version, '5.2', '<' ) ) {
325 - $mapping_file = '5-0.php';
326 - } elseif ( version_compare( $es_version, '5.2', '>=' ) && version_compare( $es_version, '7.0', '<' ) ) {
330 + if ( version_compare( $es_version, '7.0', '<' ) ) {
327 331 $mapping_file = '5-2.php';
328 - } elseif ( version_compare( $es_version, '7.0', '>=' ) ) {
329 - $mapping_file = '7-0.php';
330 332 }
331 333
332 334 return apply_filters( 'ep_post_mapping_version', $mapping_file );
333 335 }
@@ -565,11 +567,8 @@
565 567
566 568 /**
567 569 * Filters the image size to use when indexing the post thumbnail.
568 570 *
569 - * Defaults to the `woocommerce_thumbnail` size if WooCommerce is in
570 - * use. Otherwise the `thumbnail` size is used.
571 - *
572 571 * @hook ep_thumbnail_image_size
573 572 * @since 4.0.0
574 573 * @param {string|int[]} $image_size Image size. Can be any registered
575 574 * image size name, or an array of
@@ -579,25 +578,31 @@
579 578 * @return {array} Image size to pass to wp_get_attachment_image_src().
580 579 */
581 580 $image_size = apply_filters(
582 581 'ep_post_thumbnail_image_size',
583 - function_exists( 'WC' ) ? 'woocommerce_thumbnail' : 'thumbnail',
582 + 'medium',
584 583 $post
585 584 );
586 585
587 - $image_src = wp_get_attachment_image_src( $attachment_id, $image_size );
586 + $image = wp_get_attachment_image_src( $attachment_id, $image_size );
588 587 $image_alt = trim( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );
589 588
590 - if ( ! $image_src ) {
589 + if ( ! $image ) {
591 590 return null;
592 591 }
593 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 +
594 598 return [
595 599 'ID' => $attachment_id,
596 - 'src' => $image_src[0],
597 - 'width' => $image_src[1],
598 - 'height' => $image_src[2],
600 + 'src' => $src,
601 + 'width' => $width,
602 + 'height' => $height,
599 603 'alt' => $image_alt,
604 + 'srcset' => $srcset,
600 605 ];
601 606 }
602 607
603 608 /**
@@ -602,9 +607,9 @@
602 607
603 608 /**
604 609 * Prepare date terms to send to ES.
605 610 *
606 - * @param string $date_to_prepare Post date
611 + * @param null|string $date_to_prepare Post date
607 612 * @since 0.1.4
608 613 * @return array
609 614 */
610 615 public function prepare_date_terms( $date_to_prepare ) {
@@ -623,9 +628,9 @@
623 628 ];
624 629
625 630 // Combine all the date term formats and perform one single call to date_i18n() for performance.
626 631 $date_format = implode( '||', array_values( $terms_to_prepare ) );
627 - $combined_dates = explode( '||', date_i18n( $date_format, strtotime( $date_to_prepare ) ) );
632 + $combined_dates = explode( '||', date_i18n( $date_format, strtotime( (string) $date_to_prepare ) ) );
628 633
629 634 // Then split up the results for individual indexing.
630 635 $date_terms = [];
631 636 foreach ( $terms_to_prepare as $term_name => $date_format ) {
@@ -760,9 +765,9 @@
760 765 * @param \WP_Term $term Term to be formatted
761 766 * @param int $post_id The post ID
762 767 * @return array
763 768 */
764 - private function get_formatted_term( \WP_Term $term, int $post_id ) : array {
769 + private function get_formatted_term( \WP_Term $term, int $post_id ): array {
765 770 $formatted_term = [
766 771 'term_id' => $term->term_id,
767 772 'slug' => $term->slug,
768 773 'name' => $term->name,
@@ -783,9 +788,9 @@
783 788 return $formatted_term;
784 789 }
785 790
786 791 /**
787 - * Retreives term order for the object/term_taxonomy_id combination
792 + * Retrieves term order for the object/term_taxonomy_id combination
788 793 *
789 794 * @param int $term_taxonomy_id Term Taxonomy ID
790 795 * @param int $object_id Post ID
791 796 *
@@ -815,9 +820,8 @@
815 820 wp_cache_set( $cache_key, $term_orders );
816 821 }
817 822
818 823 return isset( $term_orders[ $term_taxonomy_id ] ) ? (int) $term_orders[ $term_taxonomy_id ] : 0;
819 -
820 824 }
821 825
822 826 /**
823 827 * Checks if meta key is allowed
@@ -847,59 +851,15 @@
847 851 */
848 852 public function filter_allowed_metas( $metas, $post ) {
849 853 $filtered_metas = [];
850 854
851 - /**
852 - * Filter indexable protected meta keys for posts
853 - *
854 - * @hook ep_prepare_meta_allowed_protected_keys
855 - * @param {array} $keys Allowed protected keys
856 - * @param {WP_Post} $post Post object
857 - * @since 1.7
858 - * @return {array} New keys
859 - */
860 - $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 + }
861 861
862 - /**
863 - * Filter public keys to exclude from indexed post
864 - *
865 - * @hook ep_prepare_meta_excluded_public_keys
866 - * @param {array} $keys Excluded protected keys
867 - * @param {WP_Post} $post Post object
868 - * @since 1.7
869 - * @return {array} New keys
870 - */
871 - $excluded_public_keys = apply_filters( 'ep_prepare_meta_excluded_public_keys', [], $post );
872 -
873 - foreach ( $metas as $key => $value ) {
874 -
875 - $allow_index = false;
876 -
877 - if ( is_protected_meta( $key ) ) {
878 -
879 - if ( true === $allowed_protected_keys || in_array( $key, $allowed_protected_keys, true ) ) {
880 - $allow_index = true;
881 - }
882 - } else {
883 -
884 - if ( true !== $excluded_public_keys && ! in_array( $key, $excluded_public_keys, true ) ) {
885 - $allow_index = true;
886 - }
887 - }
888 -
889 - /**
890 - * Filter force whitelisting a meta key
891 - *
892 - * @hook ep_prepare_meta_whitelist_key
893 - * @param {bool} $whitelist True to whitelist key
894 - * @param {string} $key Meta key
895 - * @param {WP_Post} $post Post object
896 - * @return {bool} New whitelist value
897 - */
898 - if ( true === $allow_index || apply_filters( 'ep_prepare_meta_whitelist_key', false, $key, $post ) ) {
899 - $filtered_metas[ $key ] = $value;
900 - }
901 - }
902 862 return $filtered_metas;
903 863 }
904 864
905 865 /**
@@ -951,9 +911,8 @@
951 911 * @since 3.4
952 912 * @return {array} Prepared meta
953 913 */
954 914 return apply_filters( 'ep_prepared_post_meta', $prepared_meta, $post );
955 -
956 915 }
957 916
958 917 /**
959 918 * Format WP query args for ES
@@ -983,26 +942,26 @@
983 942 $formatted_args = $this->maybe_add_sticky_posts( $formatted_args, $args );
984 943 $formatted_args = $this->maybe_set_aggs( $formatted_args, $args, $filters );
985 944
986 945 /**
987 - * Filter formatted Elasticsearch [ost ]query (entire query)
946 + * Filter formatted Elasticsearch query (entire query)
988 947 *
989 948 * @hook ep_formatted_args
990 949 * @param {array} $formatted_args Formatted Elasticsearch query
991 - * @param {array} $query_vars Query variables
992 - * @param {array} $query Query part
993 - * @return {array} New query
950 + * @param {array} $args WP_Query variables
951 + * @param {object} $wp_query WP_Query object
952 + * @return {array} New query
994 953 */
995 954 $formatted_args = apply_filters( 'ep_formatted_args', $formatted_args, $args, $wp_query );
996 955
997 956 /**
998 - * Filter formatted Elasticsearch [ost ]query (entire query)
957 + * Filter formatted Elasticsearch post query (entire query)
999 958 *
1000 959 * @hook ep_post_formatted_args
1001 960 * @param {array} $formatted_args Formatted Elasticsearch query
1002 - * @param {array} $query_vars Query variables
1003 - * @param {array} $query Query part
1004 - * @return {array} New query
961 + * @param {array} $args WP_Query variables
962 + * @param {object} $wp_query WP_Query object
963 + * @return {array} New query
1005 964 */
1006 965 $formatted_args = apply_filters( 'ep_post_formatted_args', $formatted_args, $args, $wp_query );
1007 966
1008 967 return $formatted_args;
@@ -1239,9 +1198,9 @@
1239 1198 $orderby_clause = $value;
1240 1199 $order = $default_order;
1241 1200 }
1242 1201
1243 - if ( empty( $orderby_clause ) || 'rand' === $orderby_clause ) {
1202 + if ( empty( $orderby_clause ) || 'rand' === $orderby_clause || preg_match( '/^rand\((\d+)\)$/i', $orderby_clause ) ) {
1244 1203 continue;
1245 1204 }
1246 1205
1247 1206 /**
@@ -1424,28 +1383,8 @@
1424 1383 if ( isset( $post_title_sortable['normalizer'] ) ) {
1425 1384 return '5-2.php';
1426 1385 }
1427 1386
1428 - /**
1429 - * Check for 5-0 mapping.
1430 - * `keyword` fields were only made available in ES 5.0
1431 - *
1432 - * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.0/release-notes-5.0.0.html
1433 - */
1434 - if ( 'keyword' === $post_title_sortable['type'] ) {
1435 - return '5-0.php';
1436 - }
1437 -
1438 - /**
1439 - * Check for pre-5-0 mapping.
1440 - * `string` fields were deprecated in ES 5.0 in favor of text/keyword
1441 - *
1442 - * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.0/release-notes-5.0.0.html
1443 - */
1444 - if ( 'string' === $post_title_sortable['type'] ) {
1445 - return 'pre-5-0.php';
1446 - }
1447 -
1448 1387 return 'unknown';
1449 1388 }
1450 1389
1451 1390 /**
@@ -1487,9 +1426,9 @@
1487 1426 * @param array $search_fields Search fields
1488 1427 * @param array $query_vars Query vars
1489 1428 * @return SearchAlgorithm Instance of search algorithm to be used
1490 1429 */
1491 - public function get_search_algorithm( string $search_text, array $search_fields, array $query_vars ) : \ElasticPress\SearchAlgorithm {
1430 + public function get_search_algorithm( string $search_text, array $search_fields, array $query_vars ): \ElasticPress\SearchAlgorithm {
1492 1431 $search_algorithm_version_option = \ElasticPress\Utils\get_option( 'ep_search_algorithm_version', '4.0' );
1493 1432
1494 1433 /**
1495 1434 * Filter the algorithm version to be used.
@@ -1546,8 +1485,9 @@
1546 1485 'date' => $this->parse_date( $args ),
1547 1486 'meta_query' => $this->parse_meta_queries( $args ),
1548 1487 'post_type' => $this->parse_post_type( $args ),
1549 1488 'post_status' => $this->parse_post_status( $args ),
1489 + 'painless_script' => $this->painless_script_query( $args ),
1550 1490 ];
1551 1491
1552 1492 /**
1553 1493 * Filter the ES filters that will be applied to the ES query.
@@ -1751,19 +1691,33 @@
1751 1691 $formatted_args['sort'] = $default_sort;
1752 1692 }
1753 1693
1754 1694 /**
1755 - * Order by 'rand' support
1695 + * Order by 'rand' and 'Rand(x)' support
1756 1696 *
1757 1697 * Ref: https://github.com/elastic/elasticsearch/issues/1170
1758 1698 */
1759 1699 if ( ! empty( $args['orderby'] ) ) {
1760 - $orderbys = $this->get_orderby_array( $args['orderby'] );
1761 - if ( in_array( 'rand', $orderbys, true ) ) {
1762 - $formatted_args_query = $formatted_args['query'];
1763 - $formatted_args['query'] = [];
1764 - $formatted_args['query']['function_score']['query'] = $formatted_args_query;
1765 - $formatted_args['query']['function_score']['random_score'] = (object) [];
1700 + $orderbys = $this->get_orderby_array( $args['orderby'] );
1701 + $random_orderbys = preg_grep( '/^rand(?:\((\d+)\))?$/i', $orderbys );
1702 +
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 + }
1766 1720 }
1767 1721 }
1768 1722
1769 1723 return $formatted_args;
@@ -2092,9 +2046,12 @@
2092 2046 $args_post_mime_type[] = $mime_type;
2093 2047 } else {
2094 2048 $filtered_mime_type_by_type = wp_match_mime_types( $mime_type, wp_get_mime_types() );
2095 2049
2096 - $args_post_mime_type = array_merge( $args_post_mime_type, $filtered_mime_type_by_type[ $mime_type ] );
2050 + $args_post_mime_type = array_merge(
2051 + $args_post_mime_type,
2052 + $filtered_mime_type_by_type[ $mime_type ] ?? [ $mime_type ]
2053 + );
2097 2054 }
2098 2055 }
2099 2056
2100 2057 return [
@@ -2136,8 +2093,10 @@
2136 2093 $date_filter = $date_query->get_es_filter();
2137 2094
2138 2095 if ( array_key_exists( 'and', $date_filter ) ) {
2139 2096 return $date_filter['and'];
2097 + } elseif ( array_key_exists( 'or', $date_filter ) ) {
2098 + return $date_filter['or'];
2140 2099 }
2141 2100 }
2142 2101 }
2143 2102
@@ -2271,31 +2230,10 @@
2271 2230 'post_status' => is_array( $post_status ) ? array_values( $post_status ) : $post_status,
2272 2231 ],
2273 2232 ];
2274 2233 }
2275 - } else {
2234 + } elseif ( ! is_admin() ) {
2276 2235 $statuses = get_post_stati( array( 'public' => true ) );
2277 -
2278 - if ( is_admin() ) {
2279 - /**
2280 - * In the admin we will add protected and private post statuses to the default query
2281 - * per WP default behavior.
2282 - */
2283 - $statuses = array_merge(
2284 - $statuses,
2285 - get_post_stati(
2286 - array(
2287 - 'protected' => true,
2288 - 'show_in_admin_all_list' => true,
2289 - )
2290 - )
2291 - );
2292 -
2293 - if ( is_user_logged_in() ) {
2294 - $statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) );
2295 - }
2296 - }
2297 -
2298 2236 $statuses = array_values( $statuses );
2299 2237
2300 2238 $post_status_filter_type = 'terms';
2301 2239
@@ -2309,8 +2247,55 @@
2309 2247 return [];
2310 2248 }
2311 2249
2312 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'] ?? [];
2259 +
2260 + if ( empty( $scripts ) ) {
2261 + return [];
2262 + }
2263 +
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 + );
2272 +
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'];
2281 + }
2282 +
2283 + $filters['bool']['must'][] = [
2284 + 'bool' => [
2285 + 'filter' => [
2286 + 'script' => [
2287 + 'script' => $script_query,
2288 + ],
2289 + ],
2290 + ],
2291 + ];
2292 + }
2293 +
2294 + return $filters;
2295 + }
2296 +
2297 + /**
2313 2298 * If in a search context set search fields, otherwise query everything.
2314 2299 *
2315 2300 * @since 4.4.0
2316 2301 * @param array $formatted_args Formatted Elasticsearch query
@@ -2534,9 +2519,9 @@
2534 2519 * @since 4.4.0
2535 2520 * @param string $field Field name
2536 2521 * @return string
2537 2522 */
2538 - protected function parse_tax_query_field( string $field ) : string {
2523 + protected function parse_tax_query_field( string $field ): string {
2539 2524
2540 2525 $from_to = [
2541 2526 'name' => 'name.raw',
2542 2527 'slug' => 'slug',
@@ -2546,8 +2531,143 @@
2546 2531 return $from_to[ $field ] ?? 'term_id';
2547 2532 }
2548 2533
2549 2534 /**
2535 + * Filter a list of meta keys down to those chosen by the user or
2536 + * allowed via a hook.
2537 + *
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.
2541 + *
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
2546 + */
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;
2553 + }
2554 +
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;
2559 + }
2560 +
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 ) ) {
2592 + continue;
2593 + }
2594 +
2595 + $filtered_metas[ $key ] = $value;
2596 + }
2597 +
2598 + return $filtered_metas;
2599 + }
2600 +
2601 + /**
2602 + * Filter a list of meta keys down to public keys or protected keys
2603 + * allowed via a hook.
2604 + *
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.
2608 + *
2609 + * @param array $metas Key => value pairs of post meta
2610 + * @param WP_Post $post Post object
2611 + * @since 5.0.0
2612 + * @return array
2613 + */
2614 + protected function filter_allowed_metas_auto( $metas, $post ) {
2615 + $filtered_metas = [];
2616 +
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 );
2627 +
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 );
2638 +
2639 + foreach ( $metas as $key => $value ) {
2640 +
2641 + $allow_index = false;
2642 +
2643 + if ( is_protected_meta( $key ) ) {
2644 +
2645 + if ( true === $allowed_protected_keys || in_array( $key, $allowed_protected_keys, true ) ) {
2646 + $allow_index = true;
2647 + }
2648 + } elseif ( true !== $excluded_public_keys && ! in_array( $key, $excluded_public_keys, true ) ) {
2649 +
2650 + $allow_index = true;
2651 + }
2652 +
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 + }
2668 +
2669 + /**
2550 2670 * Return all distinct meta fields in the database.
2551 2671 *
2552 2672 * @since 4.4.0
2553 2673 * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached.
@@ -2552,9 +2672,9 @@
2552 2672 * @since 4.4.0
2553 2673 * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached.
2554 2674 * @return array
2555 2675 */
2556 - public function get_distinct_meta_field_keys_db( bool $force_refresh = false ) : array {
2676 + public function get_distinct_meta_field_keys_db( bool $force_refresh = false ): array {
2557 2677 global $wpdb;
2558 2678
2559 2679 /**
2560 2680 * Short-circuits the process of getting distinct meta keys from the database.
@@ -2638,9 +2758,9 @@
2638 2758 * @param string $post_type Post type slug
2639 2759 * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached.
2640 2760 * @return array
2641 2761 */
2642 - public function get_distinct_meta_field_keys_db_per_post_type( string $post_type, bool $force_refresh = false ) : array {
2762 + public function get_distinct_meta_field_keys_db_per_post_type( string $post_type, bool $force_refresh = false ): array {
2643 2763 $allowed_screen = 'status-report' === \ElasticPress\Screen::factory()->get_current_screen();
2644 2764
2645 2765 /**
2646 2766 * Filter if the current screen is allowed or not to use the function.
@@ -2726,9 +2846,9 @@
2726 2846 * @param string $post_type Post type slug
2727 2847 * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached.
2728 2848 * @return array
2729 2849 */
2730 - public function get_indexable_meta_keys_per_post_type( string $post_type, bool $force_refresh = false ) : array {
2850 + public function get_indexable_meta_keys_per_post_type( string $post_type, bool $force_refresh = false ): array {
2731 2851 $mock_post = new \WP_Post( (object) [ 'post_type' => $post_type ] );
2732 2852 $meta_keys = $this->get_distinct_meta_field_keys_db_per_post_type( $post_type, $force_refresh );
2733 2853
2734 2854 $fake_meta_values = array_combine( $meta_keys, array_fill( 0, count( $meta_keys ), 'test-value' ) );
@@ -2753,18 +2873,21 @@
2753 2873 * @since 4.4.0
2754 2874 * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached.
2755 2875 * @return array
2756 2876 */
2757 - public function get_predicted_indexable_meta_keys( bool $force_refresh = false ) : array {
2877 + public function get_predicted_indexable_meta_keys( bool $force_refresh = false ): array {
2758 2878 $empty_post = new \WP_Post( (object) [] );
2759 2879 $meta_keys = $this->get_distinct_meta_field_keys_db( $force_refresh );
2760 2880
2761 - $fake_meta_values = array_combine( $meta_keys, array_fill( 0, count( $meta_keys ), 'test-value' ) );
2881 + $fake_meta_values = array_combine(
2882 + $meta_keys,
2883 + array_fill( 0, count( $meta_keys ), $this->get_test_meta_value() )
2884 + );
2762 2885 $filtered_meta = apply_filters( 'ep_prepare_meta_data', $fake_meta_values, $empty_post );
2763 2886
2764 2887 $all_keys = array_filter(
2765 2888 array_keys( $filtered_meta ),
2766 - function( $meta_key ) use ( $empty_post ) {
2889 + function ( $meta_key ) use ( $empty_post ) {
2767 2890 return $this->is_meta_allowed( $meta_key, $empty_post );
2768 2891 }
2769 2892 );
2770 2893
@@ -2773,8 +2896,26 @@
2773 2896 return $all_keys;
2774 2897 }
2775 2898
2776 2899 /**
2900 + * Return the value used to fill meta fields while predicting indexable content.
2901 + *
2902 + * @since 5.1.0
2903 + * @return string
2904 + */
2905 + public function get_test_meta_value(): string {
2906 + /**
2907 + * Filter the value used to fill meta fields while predicting indexable content.
2908 + *
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
2913 + */
2914 + return (string) apply_filters( 'ep_post_test_meta_value', 'test-value' );
2915 + }
2916 +
2917 + /**
2777 2918 * Given a post type, *yields* their Post IDs.
2778 2919 *
2779 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.
2780 2921 *
@@ -2842,9 +2983,9 @@
2842 2983 * @since 4.4.0
2843 2984 * @param array $post_ids Set of post IDs
2844 2985 * @return array
2845 2986 */
2846 - protected function get_meta_keys_from_post_ids( array $post_ids ) : array {
2987 + protected function get_meta_keys_from_post_ids( array $post_ids ): array {
2847 2988 global $wpdb;
2848 2989
2849 2990 if ( empty( $post_ids ) ) {
2850 2991 return [];
@@ -2870,9 +3011,9 @@
2870 3011 * @since 4.5.0
2871 3012 * @param array $mapping The mapping array
2872 3013 * @return array
2873 3014 */
2874 - public function add_term_suggest_field( array $mapping ) : array {
3015 + public function add_term_suggest_field( array $mapping ): array {
2875 3016 if ( version_compare( (string) Elasticsearch::factory()->get_elasticsearch_version(), '7.0', '<' ) ) {
2876 3017 $mapping_properties = &$mapping['mappings']['post']['properties'];
2877 3018 } else {
2878 3019 $mapping_properties = &$mapping['mappings']['properties'];
@@ -2886,6 +3027,58 @@
2886 3027 'search_analyzer' => 'standard',
2887 3028 );
2888 3029
2889 3030 return $mapping;
3031 + }
3032 +
3033 + /**
3034 + * Return all meta data added to the Weighting Dashboard plus all allowed keys via code.
3035 + *
3036 + * @since 5.1.4
3037 + * @return array
3038 + */
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() );
3044 +
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 );
3049 +
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 + }
3064 +
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";
2890 3083 }
2891 3084 }