| @@ -7,11 +7,11 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ElasticPress; |
| 10 | 10 | |
| 11 | -use ElasticPress\Utils as Utils; | |
| 11 | +use WP_Error; | |
| 12 | 12 | use ElasticPress\Indexables; |
| 13 | -use \WP_Error as WP_Error; | |
| 13 | +use ElasticPress\Utils; | |
| 14 | 14 | |
| 15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | 16 | exit; // Exit if accessed directly. |
| 17 | 17 | } |
| @@ -77,9 +77,9 @@ | ||
| 77 | 77 | * @param string $type Index type. Previously this was used for index type. Now it's just passed to hooks for legacy reasons. |
| 78 | 78 | * @param array $document Formatted Elasticsearch document. |
| 79 | 79 | * @param boolean $blocking Blocking HTTP request or not. |
| 80 | 80 | * @since 3.0 |
| 81 | - * @return boolean|array | |
| 81 | + * @return boolean|object | |
| 82 | 82 | */ |
| 83 | 83 | public function index_document( $index, $type, $document, $blocking = true ) { |
| 84 | 84 | /** |
| 85 | 85 | * Filter Elasticsearch index document request path |
| @@ -403,8 +403,16 @@ | ||
| 403 | 403 | * @param {string} $scope Backwards compat for scope parameter. |
| 404 | 404 | * @param {array} $query_args Current WP Query arguments |
| 405 | 405 | */ |
| 406 | 406 | do_action( 'ep_retrieve_aggregations', $response['aggregations'], $query, '', $query_args ); |
| 407 | + | |
| 408 | + if ( is_object( $query_object ) ) { | |
| 409 | + if ( method_exists( $query_object, 'set' ) ) { | |
| 410 | + $query_object->set( 'ep_aggregations', $response['aggregations'] ); | |
| 411 | + } else { | |
| 412 | + $query_object->query_vars['ep_aggregations'] = $response['aggregations']; | |
| 413 | + } | |
| 414 | + } | |
| 407 | 415 | } |
| 408 | 416 | |
| 409 | 417 | /** |
| 410 | 418 | * Fires after valid Elasticsearch query |
| @@ -455,13 +463,17 @@ | ||
| 455 | 463 | * Filter Elasticsearch query results |
| 456 | 464 | * |
| 457 | 465 | * @hook ep_es_query_results |
| 458 | 466 | * @param {array} $results Results from Elasticsearch |
| 459 | - * @param {response} $response Raw response from Elasticsearch | |
| 460 | - * @param {array} $query Raw Elasticsearch query | |
| 461 | - * @param {array} $query_args Query arguments | |
| 462 | - * @param {mixed} $query_object Could be WP_Query, WP_User_Query, etc. | |
| 463 | - * @return {array} New results | |
| 467 | + * @param {int} $results.found_documents Total number of documents. | |
| 468 | + * @param {array} $results.documents Array of documents. | |
| 469 | + * @param {array} $results.aggregations Array of aggregations. | |
| 470 | + * @param {array} $results.suggest Array of suggestions. | |
| 471 | + * @param {response} $response Raw response from Elasticsearch | |
| 472 | + * @param {array} $query Raw Elasticsearch query | |
| 473 | + * @param {array} $query_args Query arguments | |
| 474 | + * @param {mixed} $query_object Could be WP_Query, WP_User_Query, etc. | |
| 475 | + * @return {array} New results | |
| 464 | 476 | */ |
| 465 | 477 | return apply_filters( |
| 466 | 478 | 'ep_es_query_results', |
| 467 | 479 | [ |
| @@ -1192,9 +1204,17 @@ | ||
| 1192 | 1204 | * @since 1.8 |
| 1193 | 1205 | * @return array |
| 1194 | 1206 | */ |
| 1195 | 1207 | public function get_query_log() { |
| 1196 | - return $this->queries; | |
| 1208 | + /** | |
| 1209 | + * Filter the query log | |
| 1210 | + * | |
| 1211 | + * @hook ep_get_query_log | |
| 1212 | + * @since 5.3.0 | |
| 1213 | + * @param {array} $queries The query log | |
| 1214 | + * @return {array} The query log | |
| 1215 | + */ | |
| 1216 | + return apply_filters( 'ep_get_query_log', $this->queries ); | |
| 1197 | 1217 | } |
| 1198 | 1218 | |
| 1199 | 1219 | /** |
| 1200 | 1220 | * Wrapper for wp_remote_request |
| @@ -1209,9 +1229,9 @@ | ||
| 1209 | 1229 | * @param string $type Type of request, used for debugging. |
| 1210 | 1230 | * |
| 1211 | 1231 | * @return WP_Error|array The response or WP_Error on failure. |
| 1212 | 1232 | */ |
| 1213 | - public function remote_request( $path, $args = [], $query_args = [], $type = null ) { | |
| 1233 | + public function remote_request( $path, $args = [], $query_args = [], $type = '' ) { | |
| 1214 | 1234 | |
| 1215 | 1235 | if ( empty( $args['method'] ) ) { |
| 1216 | 1236 | $args['method'] = 'GET'; |
| 1217 | 1237 | } |
| @@ -1287,9 +1307,9 @@ | ||
| 1287 | 1307 | * @hook ep_intercept_remote_request |
| 1288 | 1308 | * @param {boolean} $intercept True to intercept |
| 1289 | 1309 | * @return {boolean} New value |
| 1290 | 1310 | */ |
| 1291 | - if ( true === apply_filters( 'ep_intercept_remote_request', false ) ) { | |
| 1311 | + if ( true === apply_filters( 'ep_intercept_remote_request', false ) || ! empty( $query_args['ep_intercept_request'] ) ) { | |
| 1292 | 1312 | /** |
| 1293 | 1313 | * Filter intercepted request |
| 1294 | 1314 | * |
| 1295 | 1315 | * @hook ep_do_intercept_request |
| @@ -1312,9 +1332,9 @@ | ||
| 1312 | 1332 | $is_valid_res = ( $request_response_code >= 200 && $request_response_code <= 299 ); |
| 1313 | 1333 | $is_non_blocking_request = ( 0 === $request_response_code ); |
| 1314 | 1334 | |
| 1315 | 1335 | if ( false === $request || is_wp_error( $request ) || ( ! $is_valid_res && ! $is_non_blocking_request ) ) { |
| 1316 | - $failures++; | |
| 1336 | + ++$failures; | |
| 1317 | 1337 | |
| 1318 | 1338 | /** |
| 1319 | 1339 | * Filter max number of times to attempt remote requests |
| 1320 | 1340 | * |
| @@ -1339,8 +1359,17 @@ | ||
| 1339 | 1359 | $query['blocking'] = true; |
| 1340 | 1360 | $query['request'] = $request; |
| 1341 | 1361 | $this->add_query_log( $query ); |
| 1342 | 1362 | |
| 1363 | + /** | |
| 1364 | + * Fires after Elasticsearch remote request | |
| 1365 | + * | |
| 1366 | + * @hook ep_remote_request | |
| 1367 | + * @param {array} $query Remote request arguments | |
| 1368 | + * @param {string} $type Request type | |
| 1369 | + */ | |
| 1370 | + do_action( 'ep_remote_request', $query, $type ); | |
| 1371 | + | |
| 1343 | 1372 | return $request; |
| 1344 | 1373 | } |
| 1345 | 1374 | |
| 1346 | 1375 | $query['time_finish'] = microtime( true ); |
| @@ -1346,19 +1375,12 @@ | ||
| 1346 | 1375 | $query['time_finish'] = microtime( true ); |
| 1347 | 1376 | $query['request'] = $request; |
| 1348 | 1377 | $this->add_query_log( $query ); |
| 1349 | 1378 | |
| 1350 | - /** | |
| 1351 | - * Fires after Elasticsearch remote request | |
| 1352 | - * | |
| 1353 | - * @hook ep_remote_request | |
| 1354 | - * @param {array} $query Remote request arguments | |
| 1355 | - * @param {string} $type Request type | |
| 1356 | - */ | |
| 1379 | + // This action is documented above | |
| 1357 | 1380 | do_action( 'ep_remote_request', $query, $type ); |
| 1358 | 1381 | |
| 1359 | 1382 | return $request; |
| 1360 | - | |
| 1361 | 1383 | } |
| 1362 | 1384 | |
| 1363 | 1385 | /** |
| 1364 | 1386 | * Parse response from Elasticsearch |
| @@ -1406,9 +1428,8 @@ | ||
| 1406 | 1428 | return array( |
| 1407 | 1429 | 'status' => true, |
| 1408 | 1430 | 'data' => $response->_all->primaries->indexing, |
| 1409 | 1431 | ); |
| 1410 | - | |
| 1411 | 1432 | } |
| 1412 | 1433 | |
| 1413 | 1434 | /** |
| 1414 | 1435 | * Set ES plugins and version, detect server type, and cache everything |
| @@ -1482,20 +1503,24 @@ | ||
| 1482 | 1503 | $node = end( $response['nodes'] ); |
| 1483 | 1504 | // Save version of last node. We assume all nodes are same version. |
| 1484 | 1505 | $this->elasticsearch_version = $node['version']; |
| 1485 | 1506 | |
| 1507 | + // Elasticsearch calls "modules" all default plugins that can't be uninstalled | |
| 1508 | + if ( isset( $node['modules'] ) && is_array( $node['modules'] ) ) { | |
| 1509 | + foreach ( $node['modules'] as $plugin ) { | |
| 1510 | + $this->elasticsearch_plugins[ $plugin['name'] ] = $plugin['version']; | |
| 1511 | + } | |
| 1512 | + | |
| 1513 | + if ( ! empty( $node['modules'] ) && ! empty( $node['modules'][0]['opensearch_version'] ) ) { | |
| 1514 | + $this->server_type = 'opensearch'; | |
| 1515 | + } | |
| 1516 | + } | |
| 1517 | + | |
| 1486 | 1518 | if ( isset( $node['plugins'] ) && is_array( $node['plugins'] ) ) { |
| 1487 | 1519 | foreach ( $node['plugins'] as $plugin ) { |
| 1488 | 1520 | $this->elasticsearch_plugins[ $plugin['name'] ] = $plugin['version']; |
| 1489 | 1521 | } |
| 1490 | 1522 | } |
| 1491 | - if ( isset( $node['modules'] ) | |
| 1492 | - && is_array( $node['modules'] ) | |
| 1493 | - && ! empty( $node['modules'] ) | |
| 1494 | - && ! empty( $node['modules'][0]['opensearch_version'] ) | |
| 1495 | - ) { | |
| 1496 | - $this->server_type = 'opensearch'; | |
| 1497 | - } | |
| 1498 | 1523 | } |
| 1499 | 1524 | |
| 1500 | 1525 | /** |
| 1501 | 1526 | * Cache ES info |
| @@ -1694,9 +1719,9 @@ | ||
| 1694 | 1719 | /** |
| 1695 | 1720 | * Filter the User Agent header when submitting requests to Elasticsearch. |
| 1696 | 1721 | * |
| 1697 | 1722 | * @hook ep_remote_request_add_ep_user_agent |
| 1698 | - * @param {bool} $should_add_ep_verion Whether the ElasticPress version should be added to the User Agent string. | |
| 1723 | + * @param {bool} $should_add_ep_version Whether the ElasticPress version should be added to the User Agent string. | |
| 1699 | 1724 | * @return {bool} New value |
| 1700 | 1725 | * @since 3.6.1 |
| 1701 | 1726 | */ |
| 1702 | 1727 | if ( apply_filters( 'ep_remote_request_add_ep_user_agent', Utils\is_epio() ) ) { |
| @@ -1713,14 +1738,27 @@ | ||
| 1713 | 1738 | /** |
| 1714 | 1739 | * Query logging. Don't log anything to the queries property when |
| 1715 | 1740 | * WP_DEBUG is not enabled. Calls action 'ep_add_query_log' if you |
| 1716 | 1741 | * want to access the query outside of the ElasticPress plugin. This |
| 1717 | - * runs regardless of debufg settings. | |
| 1742 | + * runs regardless of debug settings. | |
| 1718 | 1743 | * |
| 1719 | 1744 | * @param array $query Query to log. |
| 1720 | 1745 | */ |
| 1721 | 1746 | protected function add_query_log( $query ) { |
| 1722 | - if ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'WP_EP_DEBUG' ) && WP_EP_DEBUG ) ) { | |
| 1747 | + $wp_debug = defined( 'WP_DEBUG' ) && WP_DEBUG; | |
| 1748 | + $wp_ep_debug = defined( 'WP_EP_DEBUG' ) && WP_EP_DEBUG; | |
| 1749 | + | |
| 1750 | + /** | |
| 1751 | + * Filter query logging. Don't log anything to the queries property when true. | |
| 1752 | + * | |
| 1753 | + * @hook ep_disable_query_logging | |
| 1754 | + * @param {bool} Whether to log to the queries property. Defaults to false. | |
| 1755 | + * @return {bool} New value | |
| 1756 | + * @since 5.1.4 | |
| 1757 | + */ | |
| 1758 | + $disable_query_logging = apply_filters( 'ep_disable_query_logging', false ); | |
| 1759 | + | |
| 1760 | + if ( ! $disable_query_logging && ( $wp_debug || $wp_ep_debug ) ) { | |
| 1723 | 1761 | $this->queries[] = $query; |
| 1724 | 1762 | } |
| 1725 | 1763 | |
| 1726 | 1764 | /** |
| @@ -1767,9 +1805,9 @@ | ||
| 1767 | 1805 | * |
| 1768 | 1806 | * @since 4.4.0 |
| 1769 | 1807 | * @return array Array of indices in Elasticsearch |
| 1770 | 1808 | */ |
| 1771 | - public function get_cluster_indices() : array { | |
| 1809 | + public function get_cluster_indices(): array { | |
| 1772 | 1810 | $path = '_cat/indices?format=json'; |
| 1773 | 1811 | |
| 1774 | 1812 | $response = $this->remote_request( $path ); |
| 1775 | 1813 | |