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 +468 -182 4.5.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,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 }
@@ -452,9 +455,9 @@
452 455 $post_modified_gmt = $post->post_modified_gmt;
453 456 $comment_count = absint( $post->comment_count );
454 457 $comment_status = $post->comment_status;
455 458 $ping_status = $post->ping_status;
456 - $menu_order = absint( $post->menu_order );
459 + $menu_order = (int) $post->menu_order;
457 460
458 461 /**
459 462 * Filter to ignore invalid dates
460 463 *
@@ -564,11 +567,8 @@
564 567
565 568 /**
566 569 * Filters the image size to use when indexing the post thumbnail.
567 570 *
568 - * Defaults to the `woocommerce_thumbnail` size if WooCommerce is in
569 - * use. Otherwise the `thumbnail` size is used.
570 - *
571 571 * @hook ep_thumbnail_image_size
572 572 * @since 4.0.0
573 573 * @param {string|int[]} $image_size Image size. Can be any registered
574 574 * image size name, or an array of
@@ -578,25 +578,31 @@
578 578 * @return {array} Image size to pass to wp_get_attachment_image_src().
579 579 */
580 580 $image_size = apply_filters(
581 581 'ep_post_thumbnail_image_size',
582 - function_exists( 'WC' ) ? 'woocommerce_thumbnail' : 'thumbnail',
582 + 'medium',
583 583 $post
584 584 );
585 585
586 - $image_src = wp_get_attachment_image_src( $attachment_id, $image_size );
586 + $image = wp_get_attachment_image_src( $attachment_id, $image_size );
587 587 $image_alt = trim( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );
588 588
589 - if ( ! $image_src ) {
589 + if ( ! $image ) {
590 590 return null;
591 591 }
592 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 +
593 598 return [
594 599 'ID' => $attachment_id,
595 - 'src' => $image_src[0],
596 - 'width' => $image_src[1],
597 - 'height' => $image_src[2],
600 + 'src' => $src,
601 + 'width' => $width,
602 + 'height' => $height,
598 603 'alt' => $image_alt,
604 + 'srcset' => $srcset,
599 605 ];
600 606 }
601 607
602 608 /**
@@ -601,9 +607,9 @@
601 607
602 608 /**
603 609 * Prepare date terms to send to ES.
604 610 *
605 - * @param string $date_to_prepare Post date
611 + * @param null|string $date_to_prepare Post date
606 612 * @since 0.1.4
607 613 * @return array
608 614 */
609 615 public function prepare_date_terms( $date_to_prepare ) {
@@ -622,9 +628,9 @@
622 628 ];
623 629
624 630 // Combine all the date term formats and perform one single call to date_i18n() for performance.
625 631 $date_format = implode( '||', array_values( $terms_to_prepare ) );
626 - $combined_dates = explode( '||', date_i18n( $date_format, strtotime( $date_to_prepare ) ) );
632 + $combined_dates = explode( '||', date_i18n( $date_format, strtotime( (string) $date_to_prepare ) ) );
627 633
628 634 // Then split up the results for individual indexing.
629 635 $date_terms = [];
630 636 foreach ( $terms_to_prepare as $term_name => $date_format ) {
@@ -759,9 +765,9 @@
759 765 * @param \WP_Term $term Term to be formatted
760 766 * @param int $post_id The post ID
761 767 * @return array
762 768 */
763 - 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 {
764 770 $formatted_term = [
765 771 'term_id' => $term->term_id,
766 772 'slug' => $term->slug,
767 773 'name' => $term->name,
@@ -782,9 +788,9 @@
782 788 return $formatted_term;
783 789 }
784 790
785 791 /**
786 - * Retreives term order for the object/term_taxonomy_id combination
792 + * Retrieves term order for the object/term_taxonomy_id combination
787 793 *
788 794 * @param int $term_taxonomy_id Term Taxonomy ID
789 795 * @param int $object_id Post ID
790 796 *
@@ -796,9 +802,9 @@
796 802 $cache_key = "{$object_id}_term_order";
797 803 $term_orders = wp_cache_get( $cache_key );
798 804
799 805 if ( false === $term_orders ) {
800 - $results = $wpdb->get_results(
806 + $results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
801 807 $wpdb->prepare(
802 808 "SELECT term_taxonomy_id, term_order from $wpdb->term_relationships where object_id=%d;",
803 809 $object_id
804 810 ),
@@ -814,9 +820,8 @@
814 820 wp_cache_set( $cache_key, $term_orders );
815 821 }
816 822
817 823 return isset( $term_orders[ $term_taxonomy_id ] ) ? (int) $term_orders[ $term_taxonomy_id ] : 0;
818 -
819 824 }
820 825
821 826 /**
822 827 * Checks if meta key is allowed
@@ -846,59 +851,15 @@
846 851 */
847 852 public function filter_allowed_metas( $metas, $post ) {
848 853 $filtered_metas = [];
849 854
850 - /**
851 - * Filter indexable protected meta keys for posts
852 - *
853 - * @hook ep_prepare_meta_allowed_protected_keys
854 - * @param {array} $keys Allowed protected keys
855 - * @param {WP_Post} $post Post object
856 - * @since 1.7
857 - * @return {array} New keys
858 - */
859 - $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 + }
860 861
861 - /**
862 - * Filter public keys to exclude from indexed post
863 - *
864 - * @hook ep_prepare_meta_excluded_public_keys
865 - * @param {array} $keys Excluded protected keys
866 - * @param {WP_Post} $post Post object
867 - * @since 1.7
868 - * @return {array} New keys
869 - */
870 - $excluded_public_keys = apply_filters( 'ep_prepare_meta_excluded_public_keys', [], $post );
871 -
872 - foreach ( $metas as $key => $value ) {
873 -
874 - $allow_index = false;
875 -
876 - if ( is_protected_meta( $key ) ) {
877 -
878 - if ( true === $allowed_protected_keys || in_array( $key, $allowed_protected_keys, true ) ) {
879 - $allow_index = true;
880 - }
881 - } else {
882 -
883 - if ( true !== $excluded_public_keys && ! in_array( $key, $excluded_public_keys, true ) ) {
884 - $allow_index = true;
885 - }
886 - }
887 -
888 - /**
889 - * Filter force whitelisting a meta key
890 - *
891 - * @hook ep_prepare_meta_whitelist_key
892 - * @param {bool} $whitelist True to whitelist key
893 - * @param {string} $key Meta key
894 - * @param {WP_Post} $post Post object
895 - * @return {bool} New whitelist value
896 - */
897 - if ( true === $allow_index || apply_filters( 'ep_prepare_meta_whitelist_key', false, $key, $post ) ) {
898 - $filtered_metas[ $key ] = $value;
899 - }
900 - }
901 862 return $filtered_metas;
902 863 }
903 864
904 865 /**
@@ -935,9 +896,11 @@
935 896 $filtered_metas = $this->filter_allowed_metas( $meta, $post );
936 897 $prepared_meta = [];
937 898
938 899 foreach ( $filtered_metas as $key => $value ) {
939 - $prepared_meta[ $key ] = maybe_unserialize( $value );
900 + if ( ! empty( $key ) ) {
901 + $prepared_meta[ $key ] = maybe_unserialize( $value );
902 + }
940 903 }
941 904
942 905 /**
943 906 * Filter final list of prepared meta.
@@ -948,9 +911,8 @@
948 911 * @since 3.4
949 912 * @return {array} Prepared meta
950 913 */
951 914 return apply_filters( 'ep_prepared_post_meta', $prepared_meta, $post );
952 -
953 915 }
954 916
955 917 /**
956 918 * Format WP query args for ES
@@ -980,26 +942,26 @@
980 942 $formatted_args = $this->maybe_add_sticky_posts( $formatted_args, $args );
981 943 $formatted_args = $this->maybe_set_aggs( $formatted_args, $args, $filters );
982 944
983 945 /**
984 - * Filter formatted Elasticsearch [ost ]query (entire query)
946 + * Filter formatted Elasticsearch query (entire query)
985 947 *
986 948 * @hook ep_formatted_args
987 949 * @param {array} $formatted_args Formatted Elasticsearch query
988 - * @param {array} $query_vars Query variables
989 - * @param {array} $query Query part
990 - * @return {array} New query
950 + * @param {array} $args WP_Query variables
951 + * @param {object} $wp_query WP_Query object
952 + * @return {array} New query
991 953 */
992 954 $formatted_args = apply_filters( 'ep_formatted_args', $formatted_args, $args, $wp_query );
993 955
994 956 /**
995 - * Filter formatted Elasticsearch [ost ]query (entire query)
957 + * Filter formatted Elasticsearch post query (entire query)
996 958 *
997 959 * @hook ep_post_formatted_args
998 960 * @param {array} $formatted_args Formatted Elasticsearch query
999 - * @param {array} $query_vars Query variables
1000 - * @param {array} $query Query part
1001 - * @return {array} New query
961 + * @param {array} $args WP_Query variables
962 + * @param {object} $wp_query WP_Query object
963 + * @return {array} New query
1002 964 */
1003 965 $formatted_args = apply_filters( 'ep_post_formatted_args', $formatted_args, $args, $wp_query );
1004 966
1005 967 return $formatted_args;
@@ -1236,9 +1198,9 @@
1236 1198 $orderby_clause = $value;
1237 1199 $order = $default_order;
1238 1200 }
1239 1201
1240 - if ( empty( $orderby_clause ) || 'rand' === $orderby_clause ) {
1202 + if ( empty( $orderby_clause ) || 'rand' === $orderby_clause || preg_match( '/^rand\((\d+)\)$/i', $orderby_clause ) ) {
1241 1203 continue;
1242 1204 }
1243 1205
1244 1206 /**
@@ -1249,19 +1211,14 @@
1249 1211 $orderby_clause = 'ID';
1250 1212 $order = 'asc';
1251 1213 }
1252 1214
1253 - if ( in_array( $orderby_clause, [ 'meta_value', 'meta_value_num' ], true ) ) {
1254 - if ( empty( $args['meta_key'] ) ) {
1255 - continue;
1256 - } else {
1257 - $from_to['meta_value'] = 'meta.' . $args['meta_key'] . '.raw';
1258 - $from_to['meta_value_num'] = 'meta.' . $args['meta_key'] . '.long';
1259 - }
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 );
1260 1219 }
1261 1220
1262 - $orderby_clause = $from_to[ $orderby_clause ] ?? $orderby_clause;
1263 -
1264 1221 $sort[] = array(
1265 1222 $orderby_clause => array(
1266 1223 'order' => $order,
1267 1224 ),
@@ -1271,8 +1228,97 @@
1271 1228 return $sort;
1272 1229 }
1273 1230
1274 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 + /**
1275 1321 * Get Order by args Array
1276 1322 *
1277 1323 * @param string|array $orderbys Order by string or array
1278 1324 * @since 2.1
@@ -1337,28 +1383,8 @@
1337 1383 if ( isset( $post_title_sortable['normalizer'] ) ) {
1338 1384 return '5-2.php';
1339 1385 }
1340 1386
1341 - /**
1342 - * Check for 5-0 mapping.
1343 - * `keyword` fields were only made available in ES 5.0
1344 - *
1345 - * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.0/release-notes-5.0.0.html
1346 - */
1347 - if ( 'keyword' === $post_title_sortable['type'] ) {
1348 - return '5-0.php';
1349 - }
1350 -
1351 - /**
1352 - * Check for pre-5-0 mapping.
1353 - * `string` fields were deprecated in ES 5.0 in favor of text/keyword
1354 - *
1355 - * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.0/release-notes-5.0.0.html
1356 - */
1357 - if ( 'string' === $post_title_sortable['type'] ) {
1358 - return 'pre-5-0.php';
1359 - }
1360 -
1361 1387 return 'unknown';
1362 1388 }
1363 1389
1364 1390 /**
@@ -1400,9 +1426,9 @@
1400 1426 * @param array $search_fields Search fields
1401 1427 * @param array $query_vars Query vars
1402 1428 * @return SearchAlgorithm Instance of search algorithm to be used
1403 1429 */
1404 - 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 {
1405 1431 $search_algorithm_version_option = \ElasticPress\Utils\get_option( 'ep_search_algorithm_version', '4.0' );
1406 1432
1407 1433 /**
1408 1434 * Filter the algorithm version to be used.
@@ -1459,8 +1485,9 @@
1459 1485 'date' => $this->parse_date( $args ),
1460 1486 'meta_query' => $this->parse_meta_queries( $args ),
1461 1487 'post_type' => $this->parse_post_type( $args ),
1462 1488 'post_status' => $this->parse_post_status( $args ),
1489 + 'painless_script' => $this->painless_script_query( $args ),
1463 1490 ];
1464 1491
1465 1492 /**
1466 1493 * Filter the ES filters that will be applied to the ES query.
@@ -1664,19 +1691,33 @@
1664 1691 $formatted_args['sort'] = $default_sort;
1665 1692 }
1666 1693
1667 1694 /**
1668 - * Order by 'rand' support
1695 + * Order by 'rand' and 'Rand(x)' support
1669 1696 *
1670 1697 * Ref: https://github.com/elastic/elasticsearch/issues/1170
1671 1698 */
1672 1699 if ( ! empty( $args['orderby'] ) ) {
1673 - $orderbys = $this->get_orderby_array( $args['orderby'] );
1674 - if ( in_array( 'rand', $orderbys, true ) ) {
1675 - $formatted_args_query = $formatted_args['query'];
1676 - $formatted_args['query'] = [];
1677 - $formatted_args['query']['function_score']['query'] = $formatted_args_query;
1678 - $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 + }
1679 1720 }
1680 1721 }
1681 1722
1682 1723 return $formatted_args;
@@ -1868,9 +1909,9 @@
1868 1909 return [
1869 1910 'bool' => [
1870 1911 'must_not' => [
1871 1912 'terms' => [
1872 - 'post_id' => (array) $args['post__not_in'],
1913 + 'post_id' => array_values( (array) $args['post__not_in'] ),
1873 1914 ],
1874 1915 ],
1875 1916 ],
1876 1917 ];
@@ -2005,9 +2046,12 @@
2005 2046 $args_post_mime_type[] = $mime_type;
2006 2047 } else {
2007 2048 $filtered_mime_type_by_type = wp_match_mime_types( $mime_type, wp_get_mime_types() );
2008 2049
2009 - $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 + );
2010 2054 }
2011 2055 }
2012 2056
2013 2057 return [
@@ -2049,8 +2093,10 @@
2049 2093 $date_filter = $date_query->get_es_filter();
2050 2094
2051 2095 if ( array_key_exists( 'and', $date_filter ) ) {
2052 2096 return $date_filter['and'];
2097 + } elseif ( array_key_exists( 'or', $date_filter ) ) {
2098 + return $date_filter['or'];
2053 2099 }
2054 2100 }
2055 2101 }
2056 2102
@@ -2180,35 +2226,14 @@
2180 2226 }
2181 2227
2182 2228 return [
2183 2229 $terms_map_name => [
2184 - 'post_status' => $post_status,
2230 + 'post_status' => is_array( $post_status ) ? array_values( $post_status ) : $post_status,
2185 2231 ],
2186 2232 ];
2187 2233 }
2188 - } else {
2234 + } elseif ( ! is_admin() ) {
2189 2235 $statuses = get_post_stati( array( 'public' => true ) );
2190 -
2191 - if ( is_admin() ) {
2192 - /**
2193 - * In the admin we will add protected and private post statuses to the default query
2194 - * per WP default behavior.
2195 - */
2196 - $statuses = array_merge(
2197 - $statuses,
2198 - get_post_stati(
2199 - array(
2200 - 'protected' => true,
2201 - 'show_in_admin_all_list' => true,
2202 - )
2203 - )
2204 - );
2205 -
2206 - if ( is_user_logged_in() ) {
2207 - $statuses = array_merge( $statuses, get_post_stati( array( 'private' => true ) ) );
2208 - }
2209 - }
2210 -
2211 2236 $statuses = array_values( $statuses );
2212 2237
2213 2238 $post_status_filter_type = 'terms';
2214 2239
@@ -2222,8 +2247,55 @@
2222 2247 return [];
2223 2248 }
2224 2249
2225 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 + /**
2226 2298 * If in a search context set search fields, otherwise query everything.
2227 2299 *
2228 2300 * @since 4.4.0
2229 2301 * @param array $formatted_args Formatted Elasticsearch query
@@ -2447,9 +2519,9 @@
2447 2519 * @since 4.4.0
2448 2520 * @param string $field Field name
2449 2521 * @return string
2450 2522 */
2451 - protected function parse_tax_query_field( string $field ) : string {
2523 + protected function parse_tax_query_field( string $field ): string {
2452 2524
2453 2525 $from_to = [
2454 2526 'name' => 'name.raw',
2455 2527 'slug' => 'slug',
@@ -2459,8 +2531,143 @@
2459 2531 return $from_to[ $field ] ?? 'term_id';
2460 2532 }
2461 2533
2462 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 + /**
2463 2670 * Return all distinct meta fields in the database.
2464 2671 *
2465 2672 * @since 4.4.0
2466 2673 * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached.
@@ -2465,9 +2672,9 @@
2465 2672 * @since 4.4.0
2466 2673 * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached.
2467 2674 * @return array
2468 2675 */
2469 - 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 {
2470 2677 global $wpdb;
2471 2678
2472 2679 /**
2473 2680 * Short-circuits the process of getting distinct meta keys from the database.
@@ -2506,10 +2713,10 @@
2506 2713 $placeholders = implode( ',', array_fill( 0, count( $allowed_protected_keys ), '%s' ) );
2507 2714 $allowed_protected_keys_sql = " OR meta_key IN ( {$placeholders} ) ";
2508 2715 }
2509 2716
2717 + // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
2510 2718 $meta_keys = $wpdb->get_col(
2511 - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
2512 2719 $wpdb->prepare(
2513 2720 "SELECT DISTINCT meta_key
2514 2721 FROM {$wpdb->postmeta}
2515 2722 WHERE meta_key NOT LIKE %s {$allowed_protected_keys_sql}
@@ -2516,10 +2723,11 @@
2516 2723 LIMIT 800",
2517 2724 '\_%',
2518 2725 ...$allowed_protected_keys
2519 2726 )
2520 - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
2521 2727 );
2728 + // phpcs:enable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
2729 +
2522 2730 sort( $meta_keys );
2523 2731
2524 2732 // Make sure the size of the transient will not be bigger than 1MB
2525 2733 do {
@@ -2550,9 +2758,9 @@
2550 2758 * @param string $post_type Post type slug
2551 2759 * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached.
2552 2760 * @return array
2553 2761 */
2554 - 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 {
2555 2763 $allowed_screen = 'status-report' === \ElasticPress\Screen::factory()->get_current_screen();
2556 2764
2557 2765 /**
2558 2766 * Filter if the current screen is allowed or not to use the function.
@@ -2638,9 +2846,9 @@
2638 2846 * @param string $post_type Post type slug
2639 2847 * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached.
2640 2848 * @return array
2641 2849 */
2642 - 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 {
2643 2851 $mock_post = new \WP_Post( (object) [ 'post_type' => $post_type ] );
2644 2852 $meta_keys = $this->get_distinct_meta_field_keys_db_per_post_type( $post_type, $force_refresh );
2645 2853
2646 2854 $fake_meta_values = array_combine( $meta_keys, array_fill( 0, count( $meta_keys ), 'test-value' ) );
@@ -2665,18 +2873,21 @@
2665 2873 * @since 4.4.0
2666 2874 * @param bool $force_refresh Whether to use or not a cached value. Default false, use cached.
2667 2875 * @return array
2668 2876 */
2669 - 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 {
2670 2878 $empty_post = new \WP_Post( (object) [] );
2671 2879 $meta_keys = $this->get_distinct_meta_field_keys_db( $force_refresh );
2672 2880
2673 - $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 + );
2674 2885 $filtered_meta = apply_filters( 'ep_prepare_meta_data', $fake_meta_values, $empty_post );
2675 2886
2676 2887 $all_keys = array_filter(
2677 2888 array_keys( $filtered_meta ),
2678 - function( $meta_key ) use ( $empty_post ) {
2889 + function ( $meta_key ) use ( $empty_post ) {
2679 2890 return $this->is_meta_allowed( $meta_key, $empty_post );
2680 2891 }
2681 2892 );
2682 2893
@@ -2685,8 +2896,26 @@
2685 2896 return $all_keys;
2686 2897 }
2687 2898
2688 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 + /**
2689 2918 * Given a post type, *yields* their Post IDs.
2690 2919 *
2691 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.
2692 2921 *
@@ -2697,9 +2926,14 @@
2697 2926 */
2698 2927 protected function get_lazy_post_type_ids( string $post_type ) {
2699 2928 global $wpdb;
2700 2929
2701 - $total = $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM {$wpdb->posts} WHERE post_type = %s", $post_type ) );
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 + );
2702 2936
2703 2937 if ( ! $total ) {
2704 2938 return [];
2705 2939 }
@@ -2730,9 +2964,9 @@
2730 2964 $pages = apply_filters( 'ep_post_meta_by_type_number_of_pages', $pages, $per_page, $post_type );
2731 2965
2732 2966 for ( $page = 0; $page < $pages; $page++ ) {
2733 2967 $start = $per_page * $page;
2734 - $ids = $wpdb->get_col(
2968 + $ids = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
2735 2969 $wpdb->prepare(
2736 2970 "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s LIMIT %d, %d",
2737 2971 $post_type,
2738 2972 $start,
@@ -2749,9 +2983,9 @@
2749 2983 * @since 4.4.0
2750 2984 * @param array $post_ids Set of post IDs
2751 2985 * @return array
2752 2986 */
2753 - 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 {
2754 2988 global $wpdb;
2755 2989
2756 2990 if ( empty( $post_ids ) ) {
2757 2991 return [];
@@ -2757,9 +2991,9 @@
2757 2991 return [];
2758 2992 }
2759 2993
2760 2994 $placeholders = implode( ',', array_fill( 0, count( $post_ids ), '%d' ) );
2761 - $meta_keys = $wpdb->get_col(
2995 + $meta_keys = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
2762 2996 $wpdb->prepare(
2763 2997 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
2764 2998 "SELECT DISTINCT meta_key FROM {$wpdb->postmeta} WHERE post_id IN ( {$placeholders} )",
2765 2999 $post_ids
@@ -2777,10 +3011,10 @@
2777 3011 * @since 4.5.0
2778 3012 * @param array $mapping The mapping array
2779 3013 * @return array
2780 3014 */
2781 - public function add_term_suggest_field( array $mapping ) : array {
2782 - if ( version_compare( Elasticsearch::factory()->get_elasticsearch_version(), '7.0', '<' ) ) {
3015 + public function add_term_suggest_field( array $mapping ): array {
3016 + if ( version_compare( (string) Elasticsearch::factory()->get_elasticsearch_version(), '7.0', '<' ) ) {
2783 3017 $mapping_properties = &$mapping['mappings']['post']['properties'];
2784 3018 } else {
2785 3019 $mapping_properties = &$mapping['mappings']['properties'];
2786 3020 }
@@ -2793,6 +3027,58 @@
2793 3027 'search_analyzer' => 'standard',
2794 3028 );
2795 3029
2796 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";
2797 3083 }
2798 3084 }