| @@ -9,15 +9,16 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | namespace ElasticPress; |
| 12 | 12 | |
| 13 | -use \WP_CLI_Command; | |
| 14 | -use \WP_CLI; | |
| 13 | +use WP_CLI_Command; | |
| 14 | +use WP_CLI; | |
| 15 | +use ElasticPress\Command\Utility; | |
| 16 | +use ElasticPress\Elasticsearch; | |
| 17 | +use ElasticPress\FeatureRequirementsStatus; | |
| 15 | 18 | use ElasticPress\Features; |
| 19 | +use ElasticPress\Indexables; | |
| 16 | 20 | use ElasticPress\Utils; |
| 17 | -use ElasticPress\Elasticsearch; | |
| 18 | -use ElasticPress\Indexables; | |
| 19 | -use ElasticPress\Command\Utility; | |
| 20 | 21 | |
| 21 | 22 | if ( ! defined( 'ABSPATH' ) ) { |
| 22 | 23 | // @codeCoverageIgnoreStart |
| 23 | 24 | exit; // Exit if accessed directly. |
| @@ -104,14 +105,14 @@ | ||
| 104 | 105 | } |
| 105 | 106 | |
| 106 | 107 | $status = $feature->requirements_status(); |
| 107 | 108 | |
| 108 | - if ( 2 === $status->code ) { | |
| 109 | + if ( FeatureRequirementsStatus::FORCE_DISABLED === $status->get_code() ) { | |
| 109 | 110 | /* translators: Error message */ |
| 110 | - WP_CLI::error( sprintf( esc_html__( 'Feature requirements are not met: %s', 'elasticpress' ), implode( "\n\n", (array) $status->message ) ) ); | |
| 111 | - } elseif ( 1 === $status->code ) { | |
| 111 | + WP_CLI::error( sprintf( esc_html__( 'Feature requirements are not met: %s', 'elasticpress' ), implode( "\n\n", (array) $status->get_message() ) ) ); | |
| 112 | + } elseif ( FeatureRequirementsStatus::MANUALLY_ENABLED === $status->get_code() && ! empty( $status->get_message() ) ) { | |
| 112 | 113 | /* translators: Warning message */ |
| 113 | - WP_CLI::warning( sprintf( esc_html__( 'Feature is usable but there are warnings: %s', 'elasticpress' ), implode( "\n\n", (array) $status->message ) ) ); | |
| 114 | + WP_CLI::warning( sprintf( esc_html__( 'Feature is usable but there are warnings: %s', 'elasticpress' ), implode( "\n\n", (array) $status->get_message() ) ) ); | |
| 114 | 115 | } |
| 115 | 116 | |
| 116 | 117 | Features::factory()->activate_feature( $feature->slug ); |
| 117 | 118 | |
| @@ -621,8 +622,20 @@ | ||
| 621 | 622 | WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) ); |
| 622 | 623 | } |
| 623 | 624 | |
| 624 | 625 | /** |
| 626 | + * A WP-CLI wrapper to run `Autosuggest::post_deactivation()`. | |
| 627 | + * | |
| 628 | + * @subcommand epio-reset-autosuggest | |
| 629 | + * @since 5.3.2 | |
| 630 | + */ | |
| 631 | + public function epio_reset_autosuggest() { | |
| 632 | + Features::factory()->get_registered_feature( 'autosuggest' )->post_deactivation(); | |
| 633 | + | |
| 634 | + WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) ); | |
| 635 | + } | |
| 636 | + | |
| 637 | + /** | |
| 625 | 638 | * Helper method for creating the network alias for an indexable |
| 626 | 639 | * |
| 627 | 640 | * @param Indexable $indexable Instance of indexable. |
| 628 | 641 | * @since 0.9 |
| @@ -873,10 +886,8 @@ | ||
| 873 | 886 | */ |
| 874 | 887 | public function status() { |
| 875 | 888 | $this->connect_check(); |
| 876 | 889 | |
| 877 | - $request_args = [ 'headers' => Elasticsearch::factory()->format_request_headers() ]; | |
| 878 | - | |
| 879 | 890 | $registered_index_names = $this->get_index_names(); |
| 880 | 891 | |
| 881 | 892 | $response_cat_indices = Elasticsearch::factory()->remote_request( '_cat/indices?format=json' ); |
| 882 | 893 | |
| @@ -895,9 +906,9 @@ | ||
| 895 | 906 | } |
| 896 | 907 | |
| 897 | 908 | $index_names_imploded = implode( ',', $index_names ); |
| 898 | 909 | |
| 899 | - $request = wp_remote_get( trailingslashit( Utils\get_host( true ) ) . $index_names_imploded . '/_recovery/?pretty', $request_args ); | |
| 910 | + $request = Elasticsearch::factory()->remote_request( $index_names_imploded . '/_recovery/?pretty' ); | |
| 900 | 911 | |
| 901 | 912 | if ( is_wp_error( $request ) ) { |
| 902 | 913 | WP_CLI::error( implode( "\n", $request->get_error_messages() ) ); |
| 903 | 914 | } |
| @@ -918,10 +929,8 @@ | ||
| 918 | 929 | */ |
| 919 | 930 | public function stats() { |
| 920 | 931 | $this->connect_check(); |
| 921 | 932 | |
| 922 | - $request_args = array( 'headers' => Elasticsearch::factory()->format_request_headers() ); | |
| 923 | - | |
| 924 | 933 | $registered_index_names = $this->get_index_names(); |
| 925 | 934 | |
| 926 | 935 | $response_cat_indices = Elasticsearch::factory()->remote_request( '_cat/indices?format=json' ); |
| 927 | 936 | |
| @@ -941,9 +950,9 @@ | ||
| 941 | 950 | |
| 942 | 951 | $index_names_imploded = implode( ',', $index_names ); |
| 943 | 952 | |
| 944 | 953 | Elasticsearch::factory()->refresh_indices(); |
| 945 | - $request = wp_remote_get( trailingslashit( Utils\get_host( true ) ) . $index_names_imploded . '/_stats/', $request_args ); | |
| 954 | + $request = Elasticsearch::factory()->remote_request( $index_names_imploded . '/_stats/' ); | |
| 946 | 955 | |
| 947 | 956 | if ( is_wp_error( $request ) ) { |
| 948 | 957 | WP_CLI::error( implode( "\n", $request->get_error_messages() ) ); |
| 949 | 958 | } |
| @@ -1117,9 +1126,9 @@ | ||
| 1117 | 1126 | private function maybe_change_host( $assoc_args ) { |
| 1118 | 1127 | if ( isset( $assoc_args['ep-host'] ) ) { |
| 1119 | 1128 | add_filter( |
| 1120 | 1129 | 'ep_host', |
| 1121 | - function ( $host ) use ( $assoc_args ) { | |
| 1130 | + function () use ( $assoc_args ) { | |
| 1122 | 1131 | return $assoc_args['ep-host']; |
| 1123 | 1132 | } |
| 1124 | 1133 | ); |
| 1125 | 1134 | } |
| @@ -1124,9 +1133,8 @@ | ||
| 1124 | 1133 | ); |
| 1125 | 1134 | } |
| 1126 | 1135 | } |
| 1127 | 1136 | |
| 1128 | - | |
| 1129 | 1137 | /** |
| 1130 | 1138 | * maybe change index prefix on the fly |
| 1131 | 1139 | * |
| 1132 | 1140 | * @param array $assoc_args Associative CLI args. |
| @@ -1136,9 +1144,9 @@ | ||
| 1136 | 1144 | private function maybe_change_index_prefix( $assoc_args ) { |
| 1137 | 1145 | if ( isset( $assoc_args['ep-prefix'] ) ) { |
| 1138 | 1146 | add_filter( |
| 1139 | 1147 | 'ep_index_prefix', |
| 1140 | - function ( $prefix ) use ( $assoc_args ) { | |
| 1148 | + function ( $prefix ) use ( $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found | |
| 1141 | 1149 | return $assoc_args['ep-prefix']; |
| 1142 | 1150 | } |
| 1143 | 1151 | ); |
| 1144 | 1152 | } |
| @@ -1278,9 +1286,9 @@ | ||
| 1278 | 1286 | return Utility::custom_get_transient( $pre_transient, $transient ); |
| 1279 | 1287 | } |
| 1280 | 1288 | |
| 1281 | 1289 | /** |
| 1282 | - * Utilitary function to render Stats for a given index. | |
| 1290 | + * Utility function to render Stats for a given index. | |
| 1283 | 1291 | * |
| 1284 | 1292 | * @since 3.5.6 |
| 1285 | 1293 | * @param string $current_index The index name. |
| 1286 | 1294 | * @param array $body The response body. |
| @@ -1332,17 +1340,19 @@ | ||
| 1332 | 1340 | break; |
| 1333 | 1341 | } |
| 1334 | 1342 | |
| 1335 | 1343 | if ( 'index_next_batch' === $context ) { |
| 1336 | - $counter++; | |
| 1344 | + ++$counter; | |
| 1337 | 1345 | if ( ( $counter % 10 ) === 0 ) { |
| 1338 | 1346 | $time_elapsed_diff = $time_elapsed > 0 ? ' (+' . (string) ( Utility::timer_stop() - $time_elapsed ) . ')' : ''; |
| 1339 | 1347 | $time_elapsed = Utility::timer_stop( 2 ); |
| 1340 | 1348 | WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Time elapsed: ', 'elasticpress' ) . '%N' . Utility::timer_format( $time_elapsed ) . $time_elapsed_diff ) ); |
| 1341 | 1349 | |
| 1342 | - $current_memory = round( memory_get_usage() / 1024 / 1024, 2 ) . 'mb'; | |
| 1343 | - $peak_memory = ' (Peak: ' . round( memory_get_peak_usage() / 1024 / 1024, 2 ) . 'mb)'; | |
| 1344 | - WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Memory Usage: ', 'elasticpress' ) . '%N' . $current_memory . $peak_memory ) ); | |
| 1350 | + $current_memory = memory_get_usage() / 1024 / 1024; | |
| 1351 | + $current_memory = ( $current_memory > 1000 ) ? round( $current_memory / 1024, 2 ) . 'gb' : round( $current_memory, 2 ) . 'mb'; | |
| 1352 | + $peak_memory = memory_get_peak_usage() / 1024 / 1024; | |
| 1353 | + $peak_memory = ( $peak_memory > 1000 ) ? round( $peak_memory / 1024, 2 ) . 'gb' : round( $peak_memory, 2 ) . 'mb'; | |
| 1354 | + WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Memory Usage: ', 'elasticpress' ) . '%N' . $current_memory . ' (Peak: ' . $peak_memory . ')' ) ); | |
| 1345 | 1355 | } |
| 1346 | 1356 | } |
| 1347 | 1357 | } |
| 1348 | 1358 | |
| @@ -1507,14 +1517,24 @@ | ||
| 1507 | 1517 | |
| 1508 | 1518 | /** |
| 1509 | 1519 | * Saves the Instant Results search template to EPIO. |
| 1510 | 1520 | * |
| 1521 | + * ## OPTIONS | |
| 1522 | + * | |
| 1523 | + * [--network-wide] | |
| 1524 | + * : Save the template for all sites in the network if plugin is network activated. Otherwise, save the template for the current site only. | |
| 1525 | + * | |
| 1511 | 1526 | * @since 4.5.0 |
| 1527 | + * @param array $args Positional CLI args. | |
| 1528 | + * @param array $assoc_args Associative CLI args. | |
| 1512 | 1529 | * @subcommand put-search-template |
| 1513 | 1530 | */ |
| 1514 | - public function put_search_template() { | |
| 1531 | + public function put_search_template( $args, $assoc_args ) { | |
| 1532 | + $network_wide = \WP_CLI\Utils\get_flag_value( $assoc_args, 'network-wide', false ); | |
| 1515 | 1533 | $instant_results = Features::factory()->get_registered_feature( 'instant-results' ); |
| 1516 | - $instant_results->epio_save_search_template(); | |
| 1534 | + | |
| 1535 | + $instant_results->epio_save_site_search_template( $network_wide ); | |
| 1536 | + | |
| 1517 | 1537 | WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) ); |
| 1518 | 1538 | } |
| 1519 | 1539 | |
| 1520 | 1540 | /** |
| @@ -1519,14 +1539,24 @@ | ||
| 1519 | 1539 | |
| 1520 | 1540 | /** |
| 1521 | 1541 | * Deletes the Instant Results search template. |
| 1522 | 1542 | * |
| 1543 | + * ## OPTIONS | |
| 1544 | + * | |
| 1545 | + * [--network-wide] | |
| 1546 | + * : Delete the template for all sites in the network if plugin is network activated. Otherwise, delete the template for the current site only. | |
| 1547 | + * | |
| 1523 | 1548 | * @since 4.5.0 |
| 1549 | + * @param array $args Positional CLI args. | |
| 1550 | + * @param array $assoc_args Associative CLI args. | |
| 1524 | 1551 | * @subcommand delete-search-template |
| 1525 | 1552 | */ |
| 1526 | - public function delete_search_template() { | |
| 1553 | + public function delete_search_template( $args, $assoc_args ) { | |
| 1554 | + $network_wide = \WP_CLI\Utils\get_flag_value( $assoc_args, 'network-wide', false ); | |
| 1527 | 1555 | $instant_results = Features::factory()->get_registered_feature( 'instant-results' ); |
| 1528 | - $instant_results->epio_delete_search_template(); | |
| 1556 | + | |
| 1557 | + $instant_results->epio_delete_site_search_template( $network_wide ); | |
| 1558 | + | |
| 1529 | 1559 | WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) ); |
| 1530 | 1560 | } |
| 1531 | 1561 | |
| 1532 | 1562 | /** |