| @@ -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 | |
| @@ -143,13 +144,18 @@ | ||
| 143 | 144 | if ( empty( $feature ) ) { |
| 144 | 145 | WP_CLI::error( esc_html__( 'No feature with that slug is registered', 'elasticpress' ) ); |
| 145 | 146 | } |
| 146 | 147 | |
| 147 | - $active_features = Utils\get_option( 'ep_feature_settings', [] ); | |
| 148 | + $active_features = (array) Features::factory()->get_feature_settings(); | |
| 149 | + $active_features_draft = (array) Features::factory()->get_feature_settings_draft(); | |
| 148 | 150 | |
| 149 | - $key = array_search( $feature->slug, array_keys( $active_features ), true ); | |
| 151 | + $key_current = array_search( $feature->slug, array_keys( $active_features ), true ); | |
| 152 | + $key_draft = array_search( $feature->slug, array_keys( $active_features_draft ), true ); | |
| 150 | 153 | |
| 151 | - if ( false === $key || empty( $active_features[ $feature->slug ]['active'] ) ) { | |
| 154 | + $in_current = false !== $key_current && ! empty( $active_features[ $feature->slug ]['active'] ); | |
| 155 | + $in_draft = false !== $key_draft && ! empty( $active_features_draft[ $feature->slug ]['active'] ); | |
| 156 | + | |
| 157 | + if ( ! $in_current && ! $in_draft ) { | |
| 152 | 158 | WP_CLI::error( esc_html__( 'Feature is not active', 'elasticpress' ) ); |
| 153 | 159 | } |
| 154 | 160 | |
| 155 | 161 | Features::factory()->deactivate_feature( $feature->slug ); |
| @@ -173,9 +179,9 @@ | ||
| 173 | 179 | public function list_features( $args, $assoc_args ) { |
| 174 | 180 | $list_all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', null ); |
| 175 | 181 | |
| 176 | 182 | if ( empty( $list_all ) ) { |
| 177 | - $features = Utils\get_option( 'ep_feature_settings', [] ); | |
| 183 | + $features = Features::factory()->get_feature_settings(); | |
| 178 | 184 | |
| 179 | 185 | WP_CLI::line( esc_html__( 'Active features:', 'elasticpress' ) ); |
| 180 | 186 | |
| 181 | 187 | foreach ( array_keys( $features ) as $feature_slug ) { |
| @@ -616,8 +622,20 @@ | ||
| 616 | 622 | WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) ); |
| 617 | 623 | } |
| 618 | 624 | |
| 619 | 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 | + /** | |
| 620 | 638 | * Helper method for creating the network alias for an indexable |
| 621 | 639 | * |
| 622 | 640 | * @param Indexable $indexable Instance of indexable. |
| 623 | 641 | * @since 0.9 |
| @@ -868,10 +886,8 @@ | ||
| 868 | 886 | */ |
| 869 | 887 | public function status() { |
| 870 | 888 | $this->connect_check(); |
| 871 | 889 | |
| 872 | - $request_args = [ 'headers' => Elasticsearch::factory()->format_request_headers() ]; | |
| 873 | - | |
| 874 | 890 | $registered_index_names = $this->get_index_names(); |
| 875 | 891 | |
| 876 | 892 | $response_cat_indices = Elasticsearch::factory()->remote_request( '_cat/indices?format=json' ); |
| 877 | 893 | |
| @@ -890,9 +906,9 @@ | ||
| 890 | 906 | } |
| 891 | 907 | |
| 892 | 908 | $index_names_imploded = implode( ',', $index_names ); |
| 893 | 909 | |
| 894 | - $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' ); | |
| 895 | 911 | |
| 896 | 912 | if ( is_wp_error( $request ) ) { |
| 897 | 913 | WP_CLI::error( implode( "\n", $request->get_error_messages() ) ); |
| 898 | 914 | } |
| @@ -913,10 +929,8 @@ | ||
| 913 | 929 | */ |
| 914 | 930 | public function stats() { |
| 915 | 931 | $this->connect_check(); |
| 916 | 932 | |
| 917 | - $request_args = array( 'headers' => Elasticsearch::factory()->format_request_headers() ); | |
| 918 | - | |
| 919 | 933 | $registered_index_names = $this->get_index_names(); |
| 920 | 934 | |
| 921 | 935 | $response_cat_indices = Elasticsearch::factory()->remote_request( '_cat/indices?format=json' ); |
| 922 | 936 | |
| @@ -935,9 +949,10 @@ | ||
| 935 | 949 | } |
| 936 | 950 | |
| 937 | 951 | $index_names_imploded = implode( ',', $index_names ); |
| 938 | 952 | |
| 939 | - $request = wp_remote_get( trailingslashit( Utils\get_host( true ) ) . $index_names_imploded . '/_stats/', $request_args ); | |
| 953 | + Elasticsearch::factory()->refresh_indices(); | |
| 954 | + $request = Elasticsearch::factory()->remote_request( $index_names_imploded . '/_stats/' ); | |
| 940 | 955 | |
| 941 | 956 | if ( is_wp_error( $request ) ) { |
| 942 | 957 | WP_CLI::error( implode( "\n", $request->get_error_messages() ) ); |
| 943 | 958 | } |
| @@ -1067,9 +1082,9 @@ | ||
| 1067 | 1082 | * @param array $assoc_args Associative CLI args. |
| 1068 | 1083 | */ |
| 1069 | 1084 | public function get_last_sync( $args, $assoc_args ) { |
| 1070 | 1085 | $pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' ); |
| 1071 | - $last_sync = \ElasticPress\IndexHelper::factory()->get_last_index(); | |
| 1086 | + $last_sync = \ElasticPress\IndexHelper::factory()->get_last_sync(); | |
| 1072 | 1087 | |
| 1073 | 1088 | $this->pretty_json_encode( $last_sync, $pretty ); |
| 1074 | 1089 | } |
| 1075 | 1090 | |
| @@ -1111,9 +1126,9 @@ | ||
| 1111 | 1126 | private function maybe_change_host( $assoc_args ) { |
| 1112 | 1127 | if ( isset( $assoc_args['ep-host'] ) ) { |
| 1113 | 1128 | add_filter( |
| 1114 | 1129 | 'ep_host', |
| 1115 | - function ( $host ) use ( $assoc_args ) { | |
| 1130 | + function () use ( $assoc_args ) { | |
| 1116 | 1131 | return $assoc_args['ep-host']; |
| 1117 | 1132 | } |
| 1118 | 1133 | ); |
| 1119 | 1134 | } |
| @@ -1118,9 +1133,8 @@ | ||
| 1118 | 1133 | ); |
| 1119 | 1134 | } |
| 1120 | 1135 | } |
| 1121 | 1136 | |
| 1122 | - | |
| 1123 | 1137 | /** |
| 1124 | 1138 | * maybe change index prefix on the fly |
| 1125 | 1139 | * |
| 1126 | 1140 | * @param array $assoc_args Associative CLI args. |
| @@ -1130,9 +1144,9 @@ | ||
| 1130 | 1144 | private function maybe_change_index_prefix( $assoc_args ) { |
| 1131 | 1145 | if ( isset( $assoc_args['ep-prefix'] ) ) { |
| 1132 | 1146 | add_filter( |
| 1133 | 1147 | 'ep_index_prefix', |
| 1134 | - function ( $prefix ) use ( $assoc_args ) { | |
| 1148 | + function ( $prefix ) use ( $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found | |
| 1135 | 1149 | return $assoc_args['ep-prefix']; |
| 1136 | 1150 | } |
| 1137 | 1151 | ); |
| 1138 | 1152 | } |
| @@ -1272,9 +1286,9 @@ | ||
| 1272 | 1286 | return Utility::custom_get_transient( $pre_transient, $transient ); |
| 1273 | 1287 | } |
| 1274 | 1288 | |
| 1275 | 1289 | /** |
| 1276 | - * Utilitary function to render Stats for a given index. | |
| 1290 | + * Utility function to render Stats for a given index. | |
| 1277 | 1291 | * |
| 1278 | 1292 | * @since 3.5.6 |
| 1279 | 1293 | * @param string $current_index The index name. |
| 1280 | 1294 | * @param array $body The response body. |
| @@ -1326,17 +1340,19 @@ | ||
| 1326 | 1340 | break; |
| 1327 | 1341 | } |
| 1328 | 1342 | |
| 1329 | 1343 | if ( 'index_next_batch' === $context ) { |
| 1330 | - $counter++; | |
| 1344 | + ++$counter; | |
| 1331 | 1345 | if ( ( $counter % 10 ) === 0 ) { |
| 1332 | 1346 | $time_elapsed_diff = $time_elapsed > 0 ? ' (+' . (string) ( Utility::timer_stop() - $time_elapsed ) . ')' : ''; |
| 1333 | 1347 | $time_elapsed = Utility::timer_stop( 2 ); |
| 1334 | 1348 | WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Time elapsed: ', 'elasticpress' ) . '%N' . Utility::timer_format( $time_elapsed ) . $time_elapsed_diff ) ); |
| 1335 | 1349 | |
| 1336 | - $current_memory = round( memory_get_usage() / 1024 / 1024, 2 ) . 'mb'; | |
| 1337 | - $peak_memory = ' (Peak: ' . round( memory_get_peak_usage() / 1024 / 1024, 2 ) . 'mb)'; | |
| 1338 | - 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 . ')' ) ); | |
| 1339 | 1355 | } |
| 1340 | 1356 | } |
| 1341 | 1357 | } |
| 1342 | 1358 | |
| @@ -1501,14 +1517,24 @@ | ||
| 1501 | 1517 | |
| 1502 | 1518 | /** |
| 1503 | 1519 | * Saves the Instant Results search template to EPIO. |
| 1504 | 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 | + * | |
| 1505 | 1526 | * @since 4.5.0 |
| 1527 | + * @param array $args Positional CLI args. | |
| 1528 | + * @param array $assoc_args Associative CLI args. | |
| 1506 | 1529 | * @subcommand put-search-template |
| 1507 | 1530 | */ |
| 1508 | - 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 ); | |
| 1509 | 1533 | $instant_results = Features::factory()->get_registered_feature( 'instant-results' ); |
| 1510 | - $instant_results->epio_save_search_template(); | |
| 1534 | + | |
| 1535 | + $instant_results->epio_save_site_search_template( $network_wide ); | |
| 1536 | + | |
| 1511 | 1537 | WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) ); |
| 1512 | 1538 | } |
| 1513 | 1539 | |
| 1514 | 1540 | /** |
| @@ -1513,14 +1539,24 @@ | ||
| 1513 | 1539 | |
| 1514 | 1540 | /** |
| 1515 | 1541 | * Deletes the Instant Results search template. |
| 1516 | 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 | + * | |
| 1517 | 1548 | * @since 4.5.0 |
| 1549 | + * @param array $args Positional CLI args. | |
| 1550 | + * @param array $assoc_args Associative CLI args. | |
| 1518 | 1551 | * @subcommand delete-search-template |
| 1519 | 1552 | */ |
| 1520 | - 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 ); | |
| 1521 | 1555 | $instant_results = Features::factory()->get_registered_feature( 'instant-results' ); |
| 1522 | - $instant_results->epio_delete_search_template(); | |
| 1556 | + | |
| 1557 | + $instant_results->epio_delete_site_search_template( $network_wide ); | |
| 1558 | + | |
| 1523 | 1559 | WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) ); |
| 1524 | 1560 | } |
| 1525 | 1561 | |
| 1526 | 1562 | /** |