Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Command::do_request( string $method,  $route,  $assoc_args )

Do a REST Request


Parameters Parameters

$method

(string) (Required)


Top ↑

Source Source

File: libraries/lifterlms-cli/src/Commands/Restful/Command.php

	private function do_request( $method, $route, $assoc_args ) {
		if ( 'internal' === $this->scope ) {
			if ( ! defined( 'REST_REQUEST' ) ) {
				define( 'REST_REQUEST', true );
			}
			$request = new \WP_REST_Request( $method, $route );
			if ( in_array( $method, array( 'POST', 'PUT' ) ) ) {
				$request->set_body_params( $assoc_args );
			} else {
				foreach ( $assoc_args as $key => $value ) {
					$request->set_param( $key, $value );
				}
			}
			if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
				$original_queries = is_array( $GLOBALS['wpdb']->queries ) ? array_keys( $GLOBALS['wpdb']->queries ) : array();
			}
			$response = rest_do_request( $request );
			if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
				$performed_queries = array();
				foreach ( (array) $GLOBALS['wpdb']->queries as $key => $query ) {
					if ( in_array( $key, $original_queries ) ) {
						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 && 'rest' === \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             = round( $query[1], 6 );
						$slow_query_message .= <<<EOT
{$i}:
  - {$seconds} seconds
  - {$backtrace}
  - {$query[0]}
EOT;
						$slow_query_message .= PHP_EOL;
					}
				} elseif ( 'rest' !== \WP_CLI::get_config( 'debug' ) ) {
					$slow_query_message = '. Use --debug=rest to see all queries.';
				}
				$query_total_time = round( $query_total_time, 6 );
				\WP_CLI::debug( "REST command executed {$query_count} queries in {$query_total_time} seconds{$slow_query_message}", 'rest' );
			}
			if ( $error = $response->as_error() ) {
				\WP_CLI::error( $error );
			}
			return array( $response->get_status(), $response->get_data(), $response->get_headers() );
		} elseif ( 'http' === $this->scope ) {
			$headers = array();
			if ( ! empty( $this->auth ) && 'basic' === $this->auth['type'] ) {
				$headers['Authorization'] = 'Basic ' . base64_encode( $this->auth['username'] . ':' . $this->auth['password'] );
			}
			if ( 'OPTIONS' === $method ) {
				$method                = 'GET';
				$assoc_args['_method'] = 'OPTIONS';
			}
			$response = \WP_CLI\Utils\http_request( $method, rtrim( $this->api_url, '/' ) . $route, $assoc_args, $headers );
			$body     = json_decode( $response->body, true );
			if ( $response->status_code >= 400 ) {
				if ( ! empty( $body['message'] ) ) {
					\WP_CLI::error( $body['message'] . ' ' . json_encode( array( 'status' => $response->status_code ) ) );
				} else {
					switch ( $response->status_code ) {
						case 404:
							\WP_CLI::error( "No {$this->name} found." );
							break;
						default:
							\WP_CLI::error( 'Could not complete request.' );
							break;
					}
				}
			}
			return array( $response->status_code, json_decode( $response->body, true ), $response->headers->getAll() );
		}
		\WP_CLI::error( 'Invalid scope for REST command.' );
	}

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.