Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Utils | |
0.00% |
0 / 57 |
|
0.00% |
0 / 5 |
462 | |
0.00% |
0 / 1 |
| standardize_error | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
| force_url_to_absolute | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get_post_type_label | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| get_taxonomy_label | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| send_wpcom_request | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
132 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Utility class. |
| 4 | * |
| 5 | * @package automattic/jetpack-boost-core |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Boost_Core\Lib; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | |
| 12 | /** |
| 13 | * Class Utils |
| 14 | */ |
| 15 | class Utils { |
| 16 | /** |
| 17 | * Standardize error format. |
| 18 | * |
| 19 | * @param mixed $error Error. |
| 20 | * |
| 21 | * @return array |
| 22 | */ |
| 23 | public static function standardize_error( $error ) { |
| 24 | if ( is_wp_error( $error ) ) { |
| 25 | return array( |
| 26 | 'name' => $error->get_error_code(), |
| 27 | 'message' => $error->get_error_message(), |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | if ( is_string( $error ) ) { |
| 32 | return array( |
| 33 | 'name' => 'Error', |
| 34 | 'message' => $error, |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | if ( is_object( $error ) ) { |
| 39 | return array( |
| 40 | 'name' => 'Error', |
| 41 | 'message' => json_decode( wp_json_encode( $error, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ), true ), |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | return $error; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Convert relative url to absolute. |
| 50 | * |
| 51 | * @param string $url The URL. |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | public static function force_url_to_absolute( $url ) { |
| 56 | if ( str_starts_with( $url, '/' ) ) { |
| 57 | return home_url( $url ); |
| 58 | } |
| 59 | |
| 60 | return $url; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Given a post type, look up its label (if available). Returns |
| 65 | * raw post type string if not found. |
| 66 | * |
| 67 | * @param string $post_type Post type to look up. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public static function get_post_type_label( $post_type ) { |
| 72 | $post_type_object = get_post_type_object( $post_type ); |
| 73 | if ( ! $post_type_object ) { |
| 74 | return $post_type; |
| 75 | } |
| 76 | |
| 77 | return $post_type_object->labels->name; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Given a taxonomy name, look up its label. Returns raw taxonomy name if |
| 82 | * not found. |
| 83 | * |
| 84 | * @param string $taxonomy_name Taxonomy to look up. |
| 85 | * |
| 86 | * @return string |
| 87 | */ |
| 88 | public static function get_taxonomy_label( $taxonomy_name ) { |
| 89 | $taxonomy = get_taxonomy( $taxonomy_name ); |
| 90 | if ( ! $taxonomy ) { |
| 91 | return $taxonomy_name; |
| 92 | } |
| 93 | |
| 94 | return $taxonomy->label; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Make a Jetpack-authenticated request to the WPCOM servers |
| 99 | * |
| 100 | * @param string $method Request method. |
| 101 | * @param string $endpoint Endpoint to contact. |
| 102 | * @param array $args Request args. |
| 103 | * @param array $body Request body. |
| 104 | * |
| 105 | * @return \WP_Error|array |
| 106 | */ |
| 107 | public static function send_wpcom_request( $method, $endpoint, $args = null, $body = null ) { |
| 108 | $default_args = array( |
| 109 | 'method' => $method, |
| 110 | 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
| 111 | ); |
| 112 | |
| 113 | $response = Client::wpcom_json_api_request_as_blog( |
| 114 | $endpoint, |
| 115 | '2', |
| 116 | array_merge( $default_args, empty( $args ) ? array() : $args ), |
| 117 | empty( $body ) ? null : wp_json_encode( $body, JSON_UNESCAPED_SLASHES ), |
| 118 | 'wpcom' |
| 119 | ); |
| 120 | |
| 121 | if ( is_wp_error( $response ) ) { |
| 122 | return $response; |
| 123 | } |
| 124 | |
| 125 | // Check for HTTP errors. |
| 126 | $code = wp_remote_retrieve_response_code( $response ); |
| 127 | $data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 128 | |
| 129 | if ( 200 !== $code ) { |
| 130 | $default_message = sprintf( |
| 131 | /* translators: %d is a numeric HTTP error code */ |
| 132 | __( 'HTTP %d while communicating with WordPress.com', 'jetpack-boost-core' ), |
| 133 | $code |
| 134 | ); |
| 135 | |
| 136 | /* |
| 137 | * Normalize error responses from WordPress.com. |
| 138 | * |
| 139 | * When WordPress.com returns an error from Boost Cloud, the body contains |
| 140 | * statusCode and error. When it returns a WP_Error, it contains code and message. |
| 141 | */ |
| 142 | // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 143 | if ( isset( $data['statusCode'] ) && isset( $data['error'] ) ) { |
| 144 | // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 145 | $data_code = $data['statusCode']; |
| 146 | $data_message = $data['error']; |
| 147 | } elseif ( isset( $data['code'] ) && isset( $data['message'] ) ) { |
| 148 | $data_code = $data['code']; |
| 149 | $data_message = $data['message']; |
| 150 | } |
| 151 | |
| 152 | $error_code = empty( $data_code ) ? 'http_error' : $data_code; |
| 153 | $message = empty( $data_message ) ? $default_message : $data_message; |
| 154 | |
| 155 | return new \WP_Error( $error_code, $message ); |
| 156 | } |
| 157 | |
| 158 | return $data; |
| 159 | } |
| 160 | } |