WC_CLI_REST_Command::do_request
Do a REST Request
Метод класса: WC_CLI_REST_Command{}
Хуков нет.
Возвращает
Массив.
Использование
// private - только в коде основоного (родительского) класса $result = $this->do_request( $method, $route, $assoc_args );
- $method(строка) (обязательный)
- Request method. Examples: 'POST', 'PUT', 'DELETE' or 'GET'.
- $route(строка) (обязательный)
- Resource route.
- $assoc_args(массив) (обязательный)
- Associative arguments passed to the originating WP-CLI command.
Код WC_CLI_REST_Command::do_request() WC CLI REST Command::do request WC 10.4.2
private function do_request( $method, $route, $assoc_args ) {
wc_maybe_define_constant( 'REST_REQUEST', true );
$request = new WP_REST_Request( $method, $route );
if ( in_array( $method, array( 'POST', 'PUT' ), true ) ) {
$request->set_body_params( $assoc_args );
} else {
foreach ( $assoc_args as $key => $value ) {
$request->set_param( $key, $value );
}
}
if ( Constants::is_true( 'SAVEQUERIES' ) ) {
$original_queries = is_array( $GLOBALS['wpdb']->queries ) ? array_keys( $GLOBALS['wpdb']->queries ) : array();
}
$response = rest_do_request( $request );
if ( Constants::is_true( 'SAVEQUERIES' ) ) {
$performed_queries = array();
foreach ( (array) $GLOBALS['wpdb']->queries as $key => $query ) {
if ( in_array( $key, $original_queries, true ) ) {
continue;
}
$performed_queries[] = $query;
}
usort(
$performed_queries,
function( $a, $b ) {
if ( $a[1] === $b[1] ) {
return 0;
}
return ( $a[1] > $b[1] ) ? -1 : 1;
}
);
$query_count = count( $performed_queries );
$query_total_time = 0;
foreach ( $performed_queries as $query ) {
$query_total_time += $query[1];
}
$slow_query_message = '';
if ( $performed_queries && 'wc' === WP_CLI::get_config( 'debug' ) ) {
$slow_query_message .= '. Ordered by slowness, the queries are:' . PHP_EOL;
foreach ( $performed_queries as $i => $query ) {
$i++;
$bits = explode( ', ', $query[2] );
$backtrace = implode( ', ', array_slice( $bits, 13 ) );
$seconds = NumberUtil::round( $query[1], 6 );
$slow_query_message .= <<<EOT
{$i}:
- {$seconds} seconds
- {$backtrace}
- {$query[0]}
EOT;
$slow_query_message .= PHP_EOL;
}
} elseif ( 'wc' !== WP_CLI::get_config( 'debug' ) ) {
$slow_query_message = '. Use --debug=wc to see all queries.';
}
$query_total_time = NumberUtil::round( $query_total_time, 6 );
WP_CLI::debug( "wc command executed {$query_count} queries in {$query_total_time} seconds{$slow_query_message}", 'wc' );
}
$error = $response->as_error();
if ( $error ) {
// For authentication errors (status 401), include a reminder to set the --user flag.
// WP_CLI::error will only return the first message from WP_Error, so we will pass a string containing both instead.
if ( 401 === $response->get_status() ) {
$errors = $error->get_error_messages();
$errors[] = __( 'Make sure to include the --user flag with an account that has permissions for this action.', 'woocommerce' ) . ' {"status":401}';
$error = implode( "\n", $errors );
}
WP_CLI::error( $error );
}
return array( $response->get_status(), $response->get_data(), $response->get_headers() );
}