| @@ -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 ) { |
| @@ -249,15 +255,11 @@ | ||
| 249 | 255 | if ( ! is_numeric( $assoc_args['network-wide'] ) ) { |
| 250 | 256 | $assoc_args['network-wide'] = 0; |
| 251 | 257 | } |
| 252 | 258 | |
| 253 | - $sites = Utils\get_sites( $assoc_args['network-wide'] ); | |
| 259 | + $sites = Utils\get_sites( $assoc_args['network-wide'], true ); | |
| 254 | 260 | |
| 255 | 261 | foreach ( $sites as $site ) { |
| 256 | - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { | |
| 257 | - continue; | |
| 258 | - } | |
| 259 | - | |
| 260 | 262 | switch_to_blog( $site['blog_id'] ); |
| 261 | 263 | |
| 262 | 264 | foreach ( $non_global_indexable_objects as $indexable ) { |
| 263 | 265 | /** |
| @@ -289,9 +291,9 @@ | ||
| 289 | 291 | WP_CLI::error( |
| 290 | 292 | sprintf( |
| 291 | 293 | /* translators: Error message */ |
| 292 | 294 | esc_html__( 'Mapping failed: %s', 'elasticpress' ), |
| 293 | - $result->get_error_message() | |
| 295 | + Utils\get_elasticsearch_error_reason( $result->get_error_message() ) | |
| 294 | 296 | ) |
| 295 | 297 | ); |
| 296 | 298 | } |
| 297 | 299 | } |
| @@ -329,9 +331,9 @@ | ||
| 329 | 331 | WP_CLI::error( |
| 330 | 332 | sprintf( |
| 331 | 333 | /* translators: Error message */ |
| 332 | 334 | esc_html__( 'Mapping failed: %s', 'elasticpress' ), |
| 333 | - $result->get_error_message() | |
| 335 | + Utils\get_elasticsearch_error_reason( $result->get_error_message() ) | |
| 334 | 336 | ) |
| 335 | 337 | ); |
| 336 | 338 | } |
| 337 | 339 | } |
| @@ -370,9 +372,9 @@ | ||
| 370 | 372 | WP_CLI::error( |
| 371 | 373 | sprintf( |
| 372 | 374 | /* translators: Error message */ |
| 373 | 375 | esc_html__( 'Mapping failed: %s', 'elasticpress' ), |
| 374 | - $result->get_error_message() | |
| 376 | + Utils\get_elasticsearch_error_reason( $result->get_error_message() ) | |
| 375 | 377 | ) |
| 376 | 378 | ); |
| 377 | 379 | } |
| 378 | 380 | } |
| @@ -512,9 +514,9 @@ | ||
| 512 | 514 | if ( isset( $assoc_args['network-wide'] ) && defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { |
| 513 | 515 | if ( ! is_numeric( $assoc_args['network-wide'] ) ) { |
| 514 | 516 | $assoc_args['network-wide'] = 0; |
| 515 | 517 | } |
| 516 | - $sites = Utils\get_sites( $assoc_args['network-wide'] ); | |
| 518 | + $sites = Utils\get_sites( $assoc_args['network-wide'], false ); | |
| 517 | 519 | |
| 518 | 520 | foreach ( $sites as $site ) { |
| 519 | 521 | switch_to_blog( $site['blog_id'] ); |
| 520 | 522 | |
| @@ -620,8 +622,20 @@ | ||
| 620 | 622 | WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) ); |
| 621 | 623 | } |
| 622 | 624 | |
| 623 | 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 | + /** | |
| 624 | 638 | * Helper method for creating the network alias for an indexable |
| 625 | 639 | * |
| 626 | 640 | * @param Indexable $indexable Instance of indexable. |
| 627 | 641 | * @since 0.9 |
| @@ -627,16 +641,12 @@ | ||
| 627 | 641 | * @since 0.9 |
| 628 | 642 | * @return array|bool |
| 629 | 643 | */ |
| 630 | 644 | private function create_network_alias_helper( Indexable $indexable ) { |
| 631 | - $sites = Utils\get_sites(); | |
| 645 | + $sites = Utils\get_sites( 0, true ); | |
| 632 | 646 | $indexes = []; |
| 633 | 647 | |
| 634 | 648 | foreach ( $sites as $site ) { |
| 635 | - if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { | |
| 636 | - continue; | |
| 637 | - } | |
| 638 | - | |
| 639 | 649 | switch_to_blog( $site['blog_id'] ); |
| 640 | 650 | |
| 641 | 651 | $indexes[] = $indexable->get_index_name(); |
| 642 | 652 | |
| @@ -667,8 +677,11 @@ | ||
| 667 | 677 | * |
| 668 | 678 | * [--setup] |
| 669 | 679 | * : Clear the index first and re-send the put mapping. Use `--yes` to skip the confirmation |
| 670 | 680 | * |
| 681 | + * [--force] | |
| 682 | + * : Stop any ongoing sync | |
| 683 | + * | |
| 671 | 684 | * [--per-page=<per_page_number>] |
| 672 | 685 | * : Determine the amount of posts to be indexed per bulk index (or cycle) |
| 673 | 686 | * |
| 674 | 687 | * [--nobulk] |
| @@ -685,8 +698,11 @@ | ||
| 685 | 698 | * |
| 686 | 699 | * [--show-nobulk-errors] |
| 687 | 700 | * : Display the error message returned from Elasticsearch when a post fails to index while not using the /_bulk endpoint |
| 688 | 701 | * |
| 702 | + * [--stop-on-error] | |
| 703 | + * : Stop indexing if an error is encountered and display the error. | |
| 704 | + * | |
| 689 | 705 | * [--offset=<offset_number>] |
| 690 | 706 | * : Skip the first n posts (don't forget to remove the `--setup` flag when resuming or the index will be emptied before starting again). |
| 691 | 707 | * |
| 692 | 708 | * [--indexables=<indexables>] |
| @@ -721,13 +737,23 @@ | ||
| 721 | 737 | * @param array $assoc_args Associative CLI args. |
| 722 | 738 | */ |
| 723 | 739 | public function sync( $args, $assoc_args ) { |
| 724 | 740 | $setup_option = \WP_CLI\Utils\get_flag_value( $assoc_args, 'setup', false ); |
| 741 | + $force_option = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false ); | |
| 725 | 742 | |
| 726 | 743 | if ( $setup_option ) { |
| 727 | - WP_CLI::confirm( esc_html__( 'Indexing with setup option needs to delete Elasticsearch index first, are you sure you want to delete your Elasticsearch index?', 'elasticpress' ), $assoc_args ); | |
| 744 | + $message = sprintf( | |
| 745 | + /* translators: ElasticPress.io or Elasticsearch */ | |
| 746 | + esc_html__( 'Syncing with the --setup option will delete your existing index in %s. Are you sure you want to delete your Elasticsearch index', 'elasticpress' ), | |
| 747 | + Utils\is_epio() ? 'ElasticPress.io' : 'Elasticsearch' | |
| 748 | + ); | |
| 749 | + WP_CLI::confirm( $message, $assoc_args ); | |
| 728 | 750 | } |
| 729 | 751 | |
| 752 | + if ( $force_option ) { | |
| 753 | + WP_CLI::confirm( esc_html__( 'Are you sure you want to stop any other ongoing sync?', 'elasticpress' ), $assoc_args ); | |
| 754 | + } | |
| 755 | + | |
| 730 | 756 | if ( ! function_exists( 'pcntl_signal' ) ) { |
| 731 | 757 | WP_CLI::warning( esc_html__( 'Function pcntl_signal not available. Make sure to run `wp elasticpress clear-sync` in case the process is killed.', 'elasticpress' ) ); |
| 732 | 758 | } else { |
| 733 | 759 | declare( ticks = 1 ); |
| @@ -736,10 +762,15 @@ | ||
| 736 | 762 | |
| 737 | 763 | $this->maybe_change_host( $assoc_args ); |
| 738 | 764 | $this->maybe_change_index_prefix( $assoc_args ); |
| 739 | 765 | $this->connect_check(); |
| 740 | - $this->index_occurring(); | |
| 741 | 766 | |
| 767 | + if ( $force_option ) { | |
| 768 | + $this->clear_sync(); | |
| 769 | + } else { | |
| 770 | + $this->index_occurring(); | |
| 771 | + } | |
| 772 | + | |
| 742 | 773 | $indexables = null; |
| 743 | 774 | |
| 744 | 775 | if ( ! empty( $assoc_args['indexables'] ) ) { |
| 745 | 776 | $indexables = explode( ',', str_replace( ' ', '', $assoc_args['indexables'] ) ); |
| @@ -781,9 +812,16 @@ | ||
| 781 | 812 | 'offset' => ( ! empty( $assoc_args['offset'] ) ) ? absint( $assoc_args['offset'] ) : 0, |
| 782 | 813 | 'static_bulk' => $static_bulk, |
| 783 | 814 | ]; |
| 784 | 815 | |
| 785 | - if ( isset( $assoc_args['show-errors'] ) || ( isset( $assoc_args['show-bulk-errors'] ) && ! $no_bulk ) || ( isset( $assoc_args['show-nobulk-errors'] ) && $no_bulk ) ) { | |
| 816 | + $index_args['stop_on_error'] = WP_CLI\Utils\get_flag_value( $assoc_args, 'stop-on-error', false ); | |
| 817 | + | |
| 818 | + $show_errors = $index_args['stop_on_error'] || | |
| 819 | + WP_CLI\Utils\get_flag_value( $assoc_args, 'show-errors', false ) || | |
| 820 | + ( WP_CLI\Utils\get_flag_value( $assoc_args, 'show-bulk-errors', false ) && ! $no_bulk ) || | |
| 821 | + ( WP_CLI\Utils\get_flag_value( $assoc_args, 'show-nobulk-errors', false ) && $no_bulk ); | |
| 822 | + | |
| 823 | + if ( $show_errors ) { | |
| 786 | 824 | $index_args['show_errors'] = true; |
| 787 | 825 | } |
| 788 | 826 | |
| 789 | 827 | if ( ! empty( $assoc_args['post-ids'] ) ) { |
| @@ -848,10 +886,8 @@ | ||
| 848 | 886 | */ |
| 849 | 887 | public function status() { |
| 850 | 888 | $this->connect_check(); |
| 851 | 889 | |
| 852 | - $request_args = [ 'headers' => Elasticsearch::factory()->format_request_headers() ]; | |
| 853 | - | |
| 854 | 890 | $registered_index_names = $this->get_index_names(); |
| 855 | 891 | |
| 856 | 892 | $response_cat_indices = Elasticsearch::factory()->remote_request( '_cat/indices?format=json' ); |
| 857 | 893 | |
| @@ -870,9 +906,9 @@ | ||
| 870 | 906 | } |
| 871 | 907 | |
| 872 | 908 | $index_names_imploded = implode( ',', $index_names ); |
| 873 | 909 | |
| 874 | - $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' ); | |
| 875 | 911 | |
| 876 | 912 | if ( is_wp_error( $request ) ) { |
| 877 | 913 | WP_CLI::error( implode( "\n", $request->get_error_messages() ) ); |
| 878 | 914 | } |
| @@ -893,10 +929,8 @@ | ||
| 893 | 929 | */ |
| 894 | 930 | public function stats() { |
| 895 | 931 | $this->connect_check(); |
| 896 | 932 | |
| 897 | - $request_args = array( 'headers' => Elasticsearch::factory()->format_request_headers() ); | |
| 898 | - | |
| 899 | 933 | $registered_index_names = $this->get_index_names(); |
| 900 | 934 | |
| 901 | 935 | $response_cat_indices = Elasticsearch::factory()->remote_request( '_cat/indices?format=json' ); |
| 902 | 936 | |
| @@ -915,9 +949,10 @@ | ||
| 915 | 949 | } |
| 916 | 950 | |
| 917 | 951 | $index_names_imploded = implode( ',', $index_names ); |
| 918 | 952 | |
| 919 | - $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/' ); | |
| 920 | 955 | |
| 921 | 956 | if ( is_wp_error( $request ) ) { |
| 922 | 957 | WP_CLI::error( implode( "\n", $request->get_error_messages() ) ); |
| 923 | 958 | } |
| @@ -993,9 +1028,9 @@ | ||
| 993 | 1028 | * @since 3.5.5 |
| 994 | 1029 | */ |
| 995 | 1030 | do_action( 'ep_cli_after_clear_index' ); |
| 996 | 1031 | |
| 997 | - WP_CLI::log( esc_html__( 'Index cleared.', 'elasticpress' ) ); | |
| 1032 | + WP_CLI::log( esc_html__( 'Sync cleared.', 'elasticpress' ) ); | |
| 998 | 1033 | } |
| 999 | 1034 | |
| 1000 | 1035 | /** |
| 1001 | 1036 | * Returns the status of an ongoing index operation in JSON array. |
| @@ -1047,9 +1082,9 @@ | ||
| 1047 | 1082 | * @param array $assoc_args Associative CLI args. |
| 1048 | 1083 | */ |
| 1049 | 1084 | public function get_last_sync( $args, $assoc_args ) { |
| 1050 | 1085 | $pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' ); |
| 1051 | - $last_sync = \ElasticPress\IndexHelper::factory()->get_last_index(); | |
| 1086 | + $last_sync = \ElasticPress\IndexHelper::factory()->get_last_sync(); | |
| 1052 | 1087 | |
| 1053 | 1088 | $this->pretty_json_encode( $last_sync, $pretty ); |
| 1054 | 1089 | } |
| 1055 | 1090 | |
| @@ -1091,9 +1126,9 @@ | ||
| 1091 | 1126 | private function maybe_change_host( $assoc_args ) { |
| 1092 | 1127 | if ( isset( $assoc_args['ep-host'] ) ) { |
| 1093 | 1128 | add_filter( |
| 1094 | 1129 | 'ep_host', |
| 1095 | - function ( $host ) use ( $assoc_args ) { | |
| 1130 | + function () use ( $assoc_args ) { | |
| 1096 | 1131 | return $assoc_args['ep-host']; |
| 1097 | 1132 | } |
| 1098 | 1133 | ); |
| 1099 | 1134 | } |
| @@ -1098,9 +1133,8 @@ | ||
| 1098 | 1133 | ); |
| 1099 | 1134 | } |
| 1100 | 1135 | } |
| 1101 | 1136 | |
| 1102 | - | |
| 1103 | 1137 | /** |
| 1104 | 1138 | * maybe change index prefix on the fly |
| 1105 | 1139 | * |
| 1106 | 1140 | * @param array $assoc_args Associative CLI args. |
| @@ -1110,9 +1144,9 @@ | ||
| 1110 | 1144 | private function maybe_change_index_prefix( $assoc_args ) { |
| 1111 | 1145 | if ( isset( $assoc_args['ep-prefix'] ) ) { |
| 1112 | 1146 | add_filter( |
| 1113 | 1147 | 'ep_index_prefix', |
| 1114 | - function ( $prefix ) use ( $assoc_args ) { | |
| 1148 | + function ( $prefix ) use ( $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found | |
| 1115 | 1149 | return $assoc_args['ep-prefix']; |
| 1116 | 1150 | } |
| 1117 | 1151 | ); |
| 1118 | 1152 | } |
| @@ -1252,9 +1286,9 @@ | ||
| 1252 | 1286 | return Utility::custom_get_transient( $pre_transient, $transient ); |
| 1253 | 1287 | } |
| 1254 | 1288 | |
| 1255 | 1289 | /** |
| 1256 | - * Utilitary function to render Stats for a given index. | |
| 1290 | + * Utility function to render Stats for a given index. | |
| 1257 | 1291 | * |
| 1258 | 1292 | * @since 3.5.6 |
| 1259 | 1293 | * @param string $current_index The index name. |
| 1260 | 1294 | * @param array $body The response body. |
| @@ -1291,8 +1325,9 @@ | ||
| 1291 | 1325 | case 'warning': |
| 1292 | 1326 | if ( empty( $args['show_errors'] ) ) { |
| 1293 | 1327 | return; |
| 1294 | 1328 | } |
| 1329 | + | |
| 1295 | 1330 | WP_CLI::warning( $message['message'] ); |
| 1296 | 1331 | break; |
| 1297 | 1332 | |
| 1298 | 1333 | case 'error': |
| @@ -1305,17 +1340,19 @@ | ||
| 1305 | 1340 | break; |
| 1306 | 1341 | } |
| 1307 | 1342 | |
| 1308 | 1343 | if ( 'index_next_batch' === $context ) { |
| 1309 | - $counter++; | |
| 1344 | + ++$counter; | |
| 1310 | 1345 | if ( ( $counter % 10 ) === 0 ) { |
| 1311 | 1346 | $time_elapsed_diff = $time_elapsed > 0 ? ' (+' . (string) ( Utility::timer_stop() - $time_elapsed ) . ')' : ''; |
| 1312 | 1347 | $time_elapsed = Utility::timer_stop( 2 ); |
| 1313 | 1348 | WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Time elapsed: ', 'elasticpress' ) . '%N' . Utility::timer_format( $time_elapsed ) . $time_elapsed_diff ) ); |
| 1314 | 1349 | |
| 1315 | - $current_memory = round( memory_get_usage() / 1024 / 1024, 2 ) . 'mb'; | |
| 1316 | - $peak_memory = ' (Peak: ' . round( memory_get_peak_usage() / 1024 / 1024, 2 ) . 'mb)'; | |
| 1317 | - 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 . ')' ) ); | |
| 1318 | 1355 | } |
| 1319 | 1356 | } |
| 1320 | 1357 | } |
| 1321 | 1358 | |
| @@ -1372,14 +1409,13 @@ | ||
| 1372 | 1409 | * @param array $args Positional CLI args. |
| 1373 | 1410 | * @param array $assoc_args Associative CLI args. |
| 1374 | 1411 | */ |
| 1375 | 1412 | public function request( $args, $assoc_args ) { |
| 1376 | - $pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' ); | |
| 1377 | - $debug_http_request = \WP_CLI\Utils\get_flag_value( $assoc_args, 'debug-http-request' ); | |
| 1378 | - $path = $args[0]; | |
| 1379 | - $method = isset( $assoc_args['method'] ) ? $assoc_args['method'] : 'GET'; | |
| 1380 | - $body = isset( $assoc_args['body'] ) ? $assoc_args['body'] : ''; | |
| 1381 | - $request_args = [ | |
| 1413 | + $pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' ); | |
| 1414 | + $path = $args[0]; | |
| 1415 | + $method = isset( $assoc_args['method'] ) ? $assoc_args['method'] : 'GET'; | |
| 1416 | + $body = isset( $assoc_args['body'] ) ? $assoc_args['body'] : ''; | |
| 1417 | + $request_args = [ | |
| 1382 | 1418 | 'method' => $method, |
| 1383 | 1419 | ]; |
| 1384 | 1420 | if ( 'GET' !== $method && ! empty( $body ) ) { |
| 1385 | 1421 | $request_args['body'] = $body; |
| @@ -1384,54 +1420,9 @@ | ||
| 1384 | 1420 | if ( 'GET' !== $method && ! empty( $body ) ) { |
| 1385 | 1421 | $request_args['body'] = $body; |
| 1386 | 1422 | } |
| 1387 | 1423 | |
| 1388 | - if ( ! empty( $debug_http_request ) ) { | |
| 1389 | - add_filter( | |
| 1390 | - 'http_api_debug', | |
| 1391 | - function ( $response, $context, $transport, $request_args, $url ) { | |
| 1392 | - // phpcs:disable WordPress.PHP.DevelopmentFunctions | |
| 1393 | - WP_CLI::line( | |
| 1394 | - sprintf( | |
| 1395 | - /* translators: URL of the request */ | |
| 1396 | - esc_html__( 'URL: %s', 'elasticpress' ), | |
| 1397 | - $url | |
| 1398 | - ) | |
| 1399 | - ); | |
| 1400 | - WP_CLI::line( | |
| 1401 | - sprintf( | |
| 1402 | - /* translators: Request arguments (outputted with print_r()) */ | |
| 1403 | - esc_html__( 'Request Args: %s', 'elasticpress' ), | |
| 1404 | - print_r( $request_args, true ) | |
| 1405 | - ) | |
| 1406 | - ); | |
| 1407 | - WP_CLI::line( | |
| 1408 | - sprintf( | |
| 1409 | - /* translators: HTTP transport used */ | |
| 1410 | - esc_html__( 'Transport: %s', 'elasticpress' ), | |
| 1411 | - $transport | |
| 1412 | - ) | |
| 1413 | - ); | |
| 1414 | - WP_CLI::line( | |
| 1415 | - sprintf( | |
| 1416 | - /* translators: Context under which the http_api_debug hook is fired */ | |
| 1417 | - esc_html__( 'Context: %s', 'elasticpress' ), | |
| 1418 | - $context | |
| 1419 | - ) | |
| 1420 | - ); | |
| 1421 | - WP_CLI::line( | |
| 1422 | - sprintf( | |
| 1423 | - /* translators: HTTP response (outputted with print_r()) */ | |
| 1424 | - esc_html__( 'Response: %s', 'elasticpress' ), | |
| 1425 | - print_r( $response, true ) | |
| 1426 | - ) | |
| 1427 | - ); | |
| 1428 | - // phpcs:enable WordPress.PHP.DevelopmentFunctions | |
| 1429 | - }, | |
| 1430 | - 10, | |
| 1431 | - 5 | |
| 1432 | - ); | |
| 1433 | - } | |
| 1424 | + $this->maybe_add_http_api_debug_filter( $assoc_args ); | |
| 1434 | 1425 | $response = Elasticsearch::factory()->remote_request( $path, $request_args, [], 'wp_cli_request' ); |
| 1435 | 1426 | |
| 1436 | 1427 | if ( is_wp_error( $response ) ) { |
| 1437 | 1428 | WP_CLI::error( $response->get_error_message() ); |
| @@ -1497,9 +1488,9 @@ | ||
| 1497 | 1488 | * @param array $json_obj The JSON object or array. |
| 1498 | 1489 | * @param boolean $pretty_print_flag Whether it should or not be formatted. |
| 1499 | 1490 | */ |
| 1500 | 1491 | protected function pretty_json_encode( $json_obj, $pretty_print_flag ) { |
| 1501 | - $flag = $pretty_print_flag ? JSON_PRETTY_PRINT : null; | |
| 1492 | + $flag = $pretty_print_flag ? JSON_PRETTY_PRINT : 0; | |
| 1502 | 1493 | WP_CLI::line( wp_json_encode( $json_obj, $flag ) ); |
| 1503 | 1494 | } |
| 1504 | 1495 | |
| 1505 | 1496 | /** |
| @@ -1526,14 +1517,24 @@ | ||
| 1526 | 1517 | |
| 1527 | 1518 | /** |
| 1528 | 1519 | * Saves the Instant Results search template to EPIO. |
| 1529 | 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 | + * | |
| 1530 | 1526 | * @since 4.5.0 |
| 1527 | + * @param array $args Positional CLI args. | |
| 1528 | + * @param array $assoc_args Associative CLI args. | |
| 1531 | 1529 | * @subcommand put-search-template |
| 1532 | 1530 | */ |
| 1533 | - 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 ); | |
| 1534 | 1533 | $instant_results = Features::factory()->get_registered_feature( 'instant-results' ); |
| 1535 | - $instant_results->epio_save_search_template(); | |
| 1534 | + | |
| 1535 | + $instant_results->epio_save_site_search_template( $network_wide ); | |
| 1536 | + | |
| 1536 | 1537 | WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) ); |
| 1537 | 1538 | } |
| 1538 | 1539 | |
| 1539 | 1540 | /** |
| @@ -1538,13 +1539,152 @@ | ||
| 1538 | 1539 | |
| 1539 | 1540 | /** |
| 1540 | 1541 | * Deletes the Instant Results search template. |
| 1541 | 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 | + * | |
| 1542 | 1548 | * @since 4.5.0 |
| 1549 | + * @param array $args Positional CLI args. | |
| 1550 | + * @param array $assoc_args Associative CLI args. | |
| 1543 | 1551 | * @subcommand delete-search-template |
| 1544 | 1552 | */ |
| 1545 | - 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 ); | |
| 1546 | 1555 | $instant_results = Features::factory()->get_registered_feature( 'instant-results' ); |
| 1547 | - $instant_results->epio_delete_search_template(); | |
| 1556 | + | |
| 1557 | + $instant_results->epio_delete_site_search_template( $network_wide ); | |
| 1558 | + | |
| 1548 | 1559 | WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) ); |
| 1560 | + } | |
| 1561 | + | |
| 1562 | + /** | |
| 1563 | + * Get an index settings | |
| 1564 | + * | |
| 1565 | + * ## OPTIONS | |
| 1566 | + * | |
| 1567 | + * <index_name> | |
| 1568 | + * : Index name | |
| 1569 | + * | |
| 1570 | + * [--pretty] | |
| 1571 | + * : Use this flag to render a pretty-printed version of the JSON response. | |
| 1572 | + * | |
| 1573 | + * @subcommand get-index-settings | |
| 1574 | + * | |
| 1575 | + * @since 4.7.0 | |
| 1576 | + * | |
| 1577 | + * @param array $args Positional CLI args. | |
| 1578 | + * @param array $assoc_args Associative CLI args. | |
| 1579 | + */ | |
| 1580 | + public function get_index_settings( $args, $assoc_args ) { | |
| 1581 | + $response = Elasticsearch::factory()->get_index_settings( $args[0], true ); | |
| 1582 | + $pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' ); | |
| 1583 | + | |
| 1584 | + $this->pretty_json_encode( $response, $pretty ); | |
| 1585 | + } | |
| 1586 | + | |
| 1587 | + /** | |
| 1588 | + * Get a specific content in Elasticsearch | |
| 1589 | + * | |
| 1590 | + * ## OPTIONS | |
| 1591 | + * | |
| 1592 | + * <indexable> | |
| 1593 | + * : Indexable slug. Example: `post` | |
| 1594 | + * | |
| 1595 | + * <ID> | |
| 1596 | + * : Content ID | |
| 1597 | + * | |
| 1598 | + * [--debug-http-request] | |
| 1599 | + * : Enable debugging | |
| 1600 | + * | |
| 1601 | + * [--pretty] | |
| 1602 | + * : Use this flag to render a pretty-printed version of the JSON response. | |
| 1603 | + * | |
| 1604 | + * @since 4.7.0 | |
| 1605 | + * | |
| 1606 | + * @param array $args Positional CLI args. | |
| 1607 | + * @param array $assoc_args Associative CLI args. | |
| 1608 | + */ | |
| 1609 | + public function get( $args, $assoc_args ) { | |
| 1610 | + $indexables = Indexables::factory(); | |
| 1611 | + | |
| 1612 | + $indexable = $indexables->get( $args[0] ); | |
| 1613 | + if ( ! $indexable || ! $indexables->is_active( $args[0] ) ) { | |
| 1614 | + $message = wp_sprintf( | |
| 1615 | + /* translators: list of active indexables slugs */ | |
| 1616 | + esc_html__( 'Indexable not found or inactive. Active indexables are: %l', 'elasticpress' ), | |
| 1617 | + $indexables->get_all( null, true ) | |
| 1618 | + ); | |
| 1619 | + WP_CLI::error( $message ); | |
| 1620 | + } | |
| 1621 | + | |
| 1622 | + $this->maybe_add_http_api_debug_filter( $assoc_args ); | |
| 1623 | + | |
| 1624 | + $object = $indexable->get( $args[1] ); | |
| 1625 | + if ( ! $object ) { | |
| 1626 | + WP_CLI::error( esc_html__( 'Not found', 'elasticpress' ) ); | |
| 1627 | + } | |
| 1628 | + | |
| 1629 | + $pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' ); | |
| 1630 | + | |
| 1631 | + $this->pretty_json_encode( $object, $pretty ); | |
| 1632 | + } | |
| 1633 | + | |
| 1634 | + /** | |
| 1635 | + * Given associative CLI args, conditionally displays HTTP debug info | |
| 1636 | + * | |
| 1637 | + * @since 4.7.0 | |
| 1638 | + * @param array $assoc_args Associative CLI args. | |
| 1639 | + */ | |
| 1640 | + protected function maybe_add_http_api_debug_filter( $assoc_args ) { | |
| 1641 | + $debug_http_request = \WP_CLI\Utils\get_flag_value( $assoc_args, 'debug-http-request' ); | |
| 1642 | + | |
| 1643 | + if ( ! empty( $debug_http_request ) ) { | |
| 1644 | + add_filter( | |
| 1645 | + 'http_api_debug', | |
| 1646 | + function ( $response, $context, $transport, $request_args, $url ) { | |
| 1647 | + // phpcs:disable WordPress.PHP.DevelopmentFunctions | |
| 1648 | + WP_CLI::line( | |
| 1649 | + sprintf( | |
| 1650 | + /* translators: URL of the request */ | |
| 1651 | + esc_html__( 'URL: %s', 'elasticpress' ), | |
| 1652 | + $url | |
| 1653 | + ) | |
| 1654 | + ); | |
| 1655 | + WP_CLI::line( | |
| 1656 | + sprintf( | |
| 1657 | + /* translators: Request arguments (outputted with print_r()) */ | |
| 1658 | + esc_html__( 'Request Args: %s', 'elasticpress' ), | |
| 1659 | + print_r( $request_args, true ) | |
| 1660 | + ) | |
| 1661 | + ); | |
| 1662 | + WP_CLI::line( | |
| 1663 | + sprintf( | |
| 1664 | + /* translators: HTTP transport used */ | |
| 1665 | + esc_html__( 'Transport: %s', 'elasticpress' ), | |
| 1666 | + $transport | |
| 1667 | + ) | |
| 1668 | + ); | |
| 1669 | + WP_CLI::line( | |
| 1670 | + sprintf( | |
| 1671 | + /* translators: Context under which the http_api_debug hook is fired */ | |
| 1672 | + esc_html__( 'Context: %s', 'elasticpress' ), | |
| 1673 | + $context | |
| 1674 | + ) | |
| 1675 | + ); | |
| 1676 | + WP_CLI::line( | |
| 1677 | + sprintf( | |
| 1678 | + /* translators: HTTP response (outputted with print_r()) */ | |
| 1679 | + esc_html__( 'Response: %s', 'elasticpress' ), | |
| 1680 | + print_r( $response, true ) | |
| 1681 | + ) | |
| 1682 | + ); | |
| 1683 | + // phpcs:enable WordPress.PHP.DevelopmentFunctions | |
| 1684 | + }, | |
| 1685 | + 10, | |
| 1686 | + 5 | |
| 1687 | + ); | |
| 1688 | + } | |
| 1549 | 1689 | } |
| 1550 | 1690 | } |