← All changes
|
includes/classes/Indexable/Post/QueryIntegration.php
+33
-29
4.6.0
→
5.3.5
View file →
| @@ -7,11 +7,11 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ElasticPress\Indexable\Post; |
| 10 | 10 | |
| 11 | -use ElasticPress\Indexables as Indexables; | |
| 12 | -use \WP_Query as WP_Query; | |
| 13 | -use ElasticPress\Utils as Utils; | |
| 11 | +use WP_Query; | |
| 12 | +use ElasticPress\Indexables; | |
| 13 | +use ElasticPress\Utils; | |
| 14 | 14 | |
| 15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | 16 | // @codeCoverageIgnoreStart |
| 17 | 17 | exit; // Exit if accessed directly. |
| @@ -63,9 +63,9 @@ | ||
| 63 | 63 | // Query ES for posts |
| 64 | 64 | add_filter( 'posts_pre_query', array( $this, 'get_es_posts' ), 10, 2 ); |
| 65 | 65 | |
| 66 | 66 | // Properly restore blog if necessary |
| 67 | - add_action( 'loop_end', array( $this, 'maybe_restore_blog' ), 10, 1 ); | |
| 67 | + add_action( 'loop_end', array( $this, 'maybe_restore_blog' ), 10 ); | |
| 68 | 68 | |
| 69 | 69 | // Properly switch to blog if necessary |
| 70 | 70 | add_action( 'the_post', array( $this, 'maybe_switch_to_blog' ), 10, 2 ); |
| 71 | 71 | |
| @@ -97,9 +97,9 @@ | ||
| 97 | 97 | return $query->num_posts; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | - * Disables cache_results, adds header. | |
| 101 | + * Adds header. | |
| 102 | 102 | * |
| 103 | 103 | * @param WP_Query $query WP_Query instance |
| 104 | 104 | * @since 0.9 |
| 105 | 105 | */ |
| @@ -115,18 +115,8 @@ | ||
| 115 | 115 | if ( ! Indexables::factory()->get( 'post' )->elasticpress_enabled( $query ) || apply_filters( 'ep_skip_query_integration', false, $query ) ) { |
| 116 | 116 | return; |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - /** | |
| 120 | - * `cache_results` defaults to false but can be enabled. | |
| 121 | - * | |
| 122 | - * @since 1.5 | |
| 123 | - */ | |
| 124 | - $query->set( 'cache_results', false ); | |
| 125 | - if ( ! empty( $query->query['cache_results'] ) ) { | |
| 126 | - $query->set( 'cache_results', true ); | |
| 127 | - } | |
| 128 | - | |
| 129 | 119 | if ( ! headers_sent() ) { |
| 130 | 120 | /** |
| 131 | 121 | * Manually setting a header as $wp_query isn't yet initialized when we |
| 132 | 122 | * call: add_filter('wp_headers', 'filter_wp_headers'); |
| @@ -188,18 +178,18 @@ | ||
| 188 | 178 | |
| 189 | 179 | $this->switched = false; |
| 190 | 180 | } |
| 191 | 181 | } |
| 192 | - | |
| 193 | 182 | } |
| 194 | 183 | |
| 195 | 184 | /** |
| 196 | 185 | * Make sure the correct blog is restored |
| 197 | 186 | * |
| 198 | - * @param WP_Query $query WP_Query instance | |
| 187 | + * @param WP_Query $query WP_Query instance | |
| 188 | + * | |
| 199 | 189 | * @since 0.9 |
| 200 | 190 | */ |
| 201 | - public function maybe_restore_blog( $query ) { | |
| 191 | + public function maybe_restore_blog( $query ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found | |
| 202 | 192 | if ( ! is_multisite() ) { |
| 203 | 193 | // @codeCoverageIgnoreStart |
| 204 | 194 | return; |
| 205 | 195 | // @codeCoverageIgnoreEnd |
| @@ -220,10 +210,8 @@ | ||
| 220 | 210 | * @since 3.0 |
| 221 | 211 | * @return string |
| 222 | 212 | */ |
| 223 | 213 | public function get_es_posts( $posts, $query ) { |
| 224 | - global $wpdb; | |
| 225 | - | |
| 226 | 214 | /** |
| 227 | 215 | * Filter to skip WP Query integration |
| 228 | 216 | * |
| 229 | 217 | * @hook ep_skip_query_integration |
| @@ -245,9 +233,9 @@ | ||
| 245 | 233 | * @param {string|array} $post_types Post types |
| 246 | 234 | * @param {WP_Query} $query WP Query object |
| 247 | 235 | * @return {string|array} New post types |
| 248 | 236 | */ |
| 249 | - $query_vars['post_type'] = apply_filters( 'ep_query_post_type', $query_vars['post_type'], $query ); | |
| 237 | + $query_vars['post_type'] = apply_filters( 'ep_query_post_type', $query_vars['post_type'] ?? '', $query ); | |
| 250 | 238 | |
| 251 | 239 | if ( 'any' === $query_vars['post_type'] ) { |
| 252 | 240 | unset( $query_vars['post_type'] ); |
| 253 | 241 | } |
| @@ -252,13 +240,15 @@ | ||
| 252 | 240 | unset( $query_vars['post_type'] ); |
| 253 | 241 | } |
| 254 | 242 | |
| 255 | 243 | /** |
| 256 | - * If not search and not set default to post. If not set and is search, use searchable post types | |
| 244 | + * If not search and not set, default to post. If not set and is search, use searchable post types. | |
| 257 | 245 | */ |
| 258 | 246 | if ( empty( $query_vars['post_type'] ) ) { |
| 259 | - if ( $query->is_tax() ) { | |
| 260 | - $query_vars['post_type'] = get_taxonomy( $query->get_queried_object()->taxonomy )->object_type; | |
| 247 | + $tax_post_type = Utils\get_post_types_for_tax_query( $query ); | |
| 248 | + | |
| 249 | + if ( ! empty( $tax_post_type ) ) { | |
| 250 | + $query_vars['post_type'] = $tax_post_type; | |
| 261 | 251 | } elseif ( empty( $query_vars['s'] ) ) { |
| 262 | 252 | $query_vars['post_type'] = 'post'; |
| 263 | 253 | } else { |
| 264 | 254 | $query_vars['post_type'] = array_values( get_post_types( array( 'exclude_from_search' => false ) ) ); |
| @@ -326,8 +316,16 @@ | ||
| 326 | 316 | $scope = 'current'; |
| 327 | 317 | // @codeCoverageIgnoreEnd |
| 328 | 318 | } |
| 329 | 319 | |
| 320 | + /** | |
| 321 | + * Disable the post cache when using a persistent object cache or querying | |
| 322 | + * across sites, where identical post IDs can cause cache collisions. | |
| 323 | + */ | |
| 324 | + if ( wp_using_ext_object_cache() || 'all' === $scope || ! empty( $site__in ) || ! empty( $site__not_in ) ) { | |
| 325 | + $query->set( 'cache_results', false ); | |
| 326 | + } | |
| 327 | + | |
| 330 | 328 | $index = null; |
| 331 | 329 | |
| 332 | 330 | if ( 'all' === $scope ) { |
| 333 | 331 | $index = Indexables::factory()->get( 'post' )->get_network_alias(); |
| @@ -340,9 +338,9 @@ | ||
| 340 | 338 | |
| 341 | 339 | $index = implode( ',', $index ); |
| 342 | 340 | } elseif ( ! empty( $site__not_in ) ) { |
| 343 | 341 | |
| 344 | - $sites = get_sites( | |
| 342 | + $sites = \get_sites( | |
| 345 | 343 | array( |
| 346 | 344 | 'fields' => 'ids', |
| 347 | 345 | 'site__not_in' => $site__not_in, |
| 348 | 346 | ) |
| @@ -369,9 +367,9 @@ | ||
| 369 | 367 | |
| 370 | 368 | $found_documents = is_array( $ep_query['found_documents'] ) ? $ep_query['found_documents']['value'] : $ep_query['found_documents']; // 7.0+ have this as an array rather than int |
| 371 | 369 | $query->found_posts = $found_documents; |
| 372 | 370 | $query->num_posts = $query->found_posts; |
| 373 | - $query->max_num_pages = ceil( $found_documents / $query->get( 'posts_per_page' ) ); | |
| 371 | + $query->max_num_pages = -1 === $query->get( 'posts_per_page' ) ? 0 : ceil( $found_documents / $query->get( 'posts_per_page' ) ); | |
| 374 | 372 | $query->suggested_terms = $this->maybe_sanitize_suggestion( $ep_query ); |
| 375 | 373 | $query->elasticsearch_success = true; |
| 376 | 374 | |
| 377 | 375 | // Determine how we should format the results from ES based on the fields parameter. |
| @@ -482,11 +480,17 @@ | ||
| 482 | 480 | ); |
| 483 | 481 | |
| 484 | 482 | foreach ( $post_return_args as $key ) { |
| 485 | 483 | if ( 'post_author' === $key ) { |
| 486 | - $post->$key = $post_array[ $key ]['id']; | |
| 484 | + if ( isset( $post_array[ $key ]['id'] ) ) { | |
| 485 | + $post->$key = $post_array[ $key ]['id']; | |
| 486 | + } | |
| 487 | 487 | } elseif ( isset( $post_array[ $key ] ) ) { |
| 488 | - $post->$key = $post_array[ $key ]; | |
| 488 | + if ( in_array( $key, [ 'terms', 'meta', 'post_meta' ], true ) && is_array( $post_array[ $key ] ) ) { | |
| 489 | + $post->$key = wp_json_encode( $post_array[ $key ] ); | |
| 490 | + } else { | |
| 491 | + $post->$key = $post_array[ $key ]; | |
| 492 | + } | |
| 489 | 493 | } |
| 490 | 494 | } |
| 491 | 495 | |
| 492 | 496 | /** |
| @@ -587,9 +591,9 @@ | ||
| 587 | 591 | $min_score = (float) apply_filters( 'ep_suggestion_minimum_score', 0.0001 ); |
| 588 | 592 | |
| 589 | 593 | $suggestion['options'] = array_filter( |
| 590 | 594 | $suggestion['options'], |
| 591 | - function( $option ) use ( $min_score ) { | |
| 595 | + function ( $option ) use ( $min_score ) { | |
| 592 | 596 | return number_format( $option['score'], 10 ) > $min_score; |
| 593 | 597 | } |
| 594 | 598 | ); |
| 595 | 599 | |