Skip to content

Commit

Permalink
Merge pull request #3567 from 10up/feature/issue-3565
Browse files Browse the repository at this point in the history
New `get` WP-CLI command
  • Loading branch information
felipeelia authored Aug 3, 2023
2 parents 87bd0dd + b5e1067 commit 87a5c40
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 52 deletions.
162 changes: 110 additions & 52 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1402,64 +1402,18 @@ public function call_ep_cli_put_mapping( $index_meta, $indexable ) {
* @param array $assoc_args Associative CLI args.
*/
public function request( $args, $assoc_args ) {
$pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' );
$debug_http_request = \WP_CLI\Utils\get_flag_value( $assoc_args, 'debug-http-request' );
$path = $args[0];
$method = isset( $assoc_args['method'] ) ? $assoc_args['method'] : 'GET';
$body = isset( $assoc_args['body'] ) ? $assoc_args['body'] : '';
$request_args = [
$pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' );
$path = $args[0];
$method = isset( $assoc_args['method'] ) ? $assoc_args['method'] : 'GET';
$body = isset( $assoc_args['body'] ) ? $assoc_args['body'] : '';
$request_args = [
'method' => $method,
];
if ( 'GET' !== $method && ! empty( $body ) ) {
$request_args['body'] = $body;
}

if ( ! empty( $debug_http_request ) ) {
add_filter(
'http_api_debug',
function ( $response, $context, $transport, $request_args, $url ) {
// phpcs:disable WordPress.PHP.DevelopmentFunctions
WP_CLI::line(
sprintf(
/* translators: URL of the request */
esc_html__( 'URL: %s', 'elasticpress' ),
$url
)
);
WP_CLI::line(
sprintf(
/* translators: Request arguments (outputted with print_r()) */
esc_html__( 'Request Args: %s', 'elasticpress' ),
print_r( $request_args, true )
)
);
WP_CLI::line(
sprintf(
/* translators: HTTP transport used */
esc_html__( 'Transport: %s', 'elasticpress' ),
$transport
)
);
WP_CLI::line(
sprintf(
/* translators: Context under which the http_api_debug hook is fired */
esc_html__( 'Context: %s', 'elasticpress' ),
$context
)
);
WP_CLI::line(
sprintf(
/* translators: HTTP response (outputted with print_r()) */
esc_html__( 'Response: %s', 'elasticpress' ),
print_r( $response, true )
)
);
// phpcs:enable WordPress.PHP.DevelopmentFunctions
},
10,
5
);
}
$this->maybe_add_http_api_debug_filter( $assoc_args );
$response = Elasticsearch::factory()->remote_request( $path, $request_args, [], 'wp_cli_request' );

if ( is_wp_error( $response ) ) {
Expand Down Expand Up @@ -1601,4 +1555,108 @@ public function get_index_settings( $args, $assoc_args ) {

$this->pretty_json_encode( $response, $pretty );
}

/**
* Get a specific content in Elasticsearch
*
* ## OPTIONS
*
* <indexable>
* : Indexable slug. Example: `post`
*
* <ID>
* : Content ID
*
* [--debug-http-request]
* : Enable debugging
*
* [--pretty]
* : Use this flag to render a pretty-printed version of the JSON response.
*
* @since 4.7.0
*
* @param array $args Positional CLI args.
* @param array $assoc_args Associative CLI args.
*/
public function get( $args, $assoc_args ) {
$indexables = Indexables::factory();

$indexable = $indexables->get( $args[0] );
if ( ! $indexable || ! $indexables->is_active( $args[0] ) ) {
$message = wp_sprintf(
/* translators: list of active indexables slugs */
esc_html__( 'Indexable not found or inactive. Active indexables are: %l', 'elasticpress' ),
$indexables->get_all( null, true )
);
WP_CLI::error( $message );
}

$this->maybe_add_http_api_debug_filter( $assoc_args );

$object = $indexable->get( $args[1] );
if ( ! $object ) {
WP_CLI::error( esc_html__( 'Not found', 'elasticpress' ) );
}

$pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' );

$this->pretty_json_encode( $object, $pretty );
}

/**
* Given associative CLI args, conditionally displays HTTP debug info
*
* @since 4.7.0
* @param array $assoc_args Associative CLI args.
*/
protected function maybe_add_http_api_debug_filter( $assoc_args ) {
$debug_http_request = \WP_CLI\Utils\get_flag_value( $assoc_args, 'debug-http-request' );

if ( ! empty( $debug_http_request ) ) {
add_filter(
'http_api_debug',
function ( $response, $context, $transport, $request_args, $url ) {
// phpcs:disable WordPress.PHP.DevelopmentFunctions
WP_CLI::line(
sprintf(
/* translators: URL of the request */
esc_html__( 'URL: %s', 'elasticpress' ),
$url
)
);
WP_CLI::line(
sprintf(
/* translators: Request arguments (outputted with print_r()) */
esc_html__( 'Request Args: %s', 'elasticpress' ),
print_r( $request_args, true )
)
);
WP_CLI::line(
sprintf(
/* translators: HTTP transport used */
esc_html__( 'Transport: %s', 'elasticpress' ),
$transport
)
);
WP_CLI::line(
sprintf(
/* translators: Context under which the http_api_debug hook is fired */
esc_html__( 'Context: %s', 'elasticpress' ),
$context
)
);
WP_CLI::line(
sprintf(
/* translators: HTTP response (outputted with print_r()) */
esc_html__( 'Response: %s', 'elasticpress' ),
print_r( $response, true )
)
);
// phpcs:enable WordPress.PHP.DevelopmentFunctions
},
10,
5
);
}
}
}
65 changes: 65 additions & 0 deletions tests/php/TestCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,74 @@ public function testGetIndexSettings() {

$output = $this->getActualOutputForAssertion();
$this->assertStringStartsWith( "{\n", $output );
}

/**
* Test the `get` command
*
* @since 4.7.0
* @group commands
*/
public function test_get() {
$post_id = $this->ep_factory->post->create();

$this->command->get( [ 'post', $post_id ], [] );

$output = $this->getActualOutputForAssertion();
$this->assertStringStartsWith( '{', $output );
$this->assertStringContainsString( '"post_id":' . $post_id, $output );

// clean output buffer
ob_clean();

// test with --pretty flag
$this->command->get( [ 'post', $post_id ], [ 'pretty' => true ] );

$output = $this->getActualOutputForAssertion();
$this->assertStringStartsWith( "{\n", $output );
$this->assertStringContainsString( '"post_id": ' . $post_id, $output );
}

/**
* Test the `get` command when an indexable does not exist
*
* @since 4.7.0
* @group commands
*/
public function test_get_wrong_indexable() {
$this->expectException( '\Exception' );
$this->command->get( [ 'absent', '1' ], [] );
}

/**
* Test the `get` command when a post is not found
*
* @since 4.7.0
* @group commands
*/
public function test_get_not_found() {
$this->expectExceptionMessage( 'Not found' );
$this->command->get( [ 'post', '99999' ], [] );
}

/**
* Test the `get` command when `--debug-http-request` is passed
*
* @since 4.7.0
* @group commands
*/
public function test_get_debug_http_request() {
$this->expectExceptionMessage( 'Not found' );

// test with --debug-http-request flag
$this->command->get( [ 'post', '99999' ], [ 'debug-http-request' => true ] );

$output = $this->getActualOutputForAssertion();
$this->assertStringContainsString( 'URL:', $output );
$this->assertStringContainsString( 'Request Args:', $output );
$this->assertStringContainsString( 'Transport:', $output );
$this->assertStringContainsString( 'Context:', $output );
$this->assertStringContainsString( 'Response:', $output );
}

/**
Expand Down

0 comments on commit 87a5c40

Please sign in to comment.